Skip to content

Instantly share code, notes, and snippets.

@RobertSzkutak
Created October 30, 2011 21:15
Show Gist options
  • Save RobertSzkutak/1326452 to your computer and use it in GitHub Desktop.
Save RobertSzkutak/1326452 to your computer and use it in GitHub Desktop.
Echo, a simple IRC bot written in Python 3
#!/usr/bin/env python3
# ircecho.py
# Copyright (C) 2011 : Robert L Szkutak II - http://robertszkutak.com
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import sys
import socket
import string
HOST = "CHANGETHIS"
PORT = 6667
NICK = "CHANGETHIS"
IDENT = "CHANGETHIS"
REALNAME = "CHANGETHIS"
MASTER = "CHANGETHIS"
readbuffer = ""
s=socket.socket( )
s.connect((HOST, PORT))
s.send(bytes("NICK %s\r\n" % NICK, "UTF-8"))
s.send(bytes("USER %s %s bla :%s\r\n" % (IDENT, HOST, REALNAME), "UTF-8"))
s.send(bytes("JOIN #CHANGETHIS\r\n", "UTF-8"));
s.send(bytes("PRIVMSG %s :Hello Master\r\n" % MASTER, "UTF-8"))
while 1:
readbuffer = readbuffer+s.recv(1024).decode("UTF-8")
temp = str.split(readbuffer, "\n")
readbuffer=temp.pop( )
for line in temp:
line = str.rstrip(line)
line = str.split(line)
if(line[0] == "PING"):
s.send(bytes("PONG %s\r\n" % line[1], "UTF-8"))
if(line[1] == "PRIVMSG"):
sender = ""
for char in line[0]:
if(char == "!"):
break
if(char != ":"):
sender += char
size = len(line)
i = 3
message = ""
while(i < size):
message += line[i] + " "
i = i + 1
message.lstrip(":")
s.send(bytes("PRIVMSG %s %s \r\n" % (sender, message), "UTF-8"))
for index, i in enumerate(line):
print(line[index])
@naoliv
Copy link

naoliv commented Jul 14, 2014

Copied/inspired from http://oreilly.com/pub/h/1968? (note that even the "bla" is the same)

@RobertSzkutak
Copy link
Author

Entirely possible that's the original source for the line to establish and listen from the connection. You can find that code in a lot of places though. Honestly couldn't tell you where I grabbed it from.

@diveyez
Copy link

diveyez commented Mar 24, 2018

COPYPASTA! WATCH OUT, BAD SOCKET BAD SOCKET.
Seriously though, Python is horrible for the purpose of irc bots. I am a fan of python, but I WONT DO THAT.

@diveyez
Copy link

diveyez commented Mar 24, 2018

Suggestion, Do not put a copyright claimer on some code you copy pasted. Bad idea.

@rjunior8
Copy link

rjunior8 commented Jul 8, 2018

How to make it with Tor network?

@Y4kuzi
Copy link

Y4kuzi commented Aug 27, 2019

COPYPASTA! WATCH OUT, BAD SOCKET BAD SOCKET.
Seriously though, Python is horrible for the purpose of irc bots. I am a fan of python, but I WONT DO THAT.

Why do you think that? I've written multiple fully featured IRC bots without any issues.
I'm curious about your point of view on Python being horrible for IRC bots.

@gufril15
Copy link

nice script

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment