Skip to content

Instantly share code, notes, and snippets.

@aji
Created March 26, 2012 01:40
Show Gist options
  • Save aji/2202168 to your computer and use it in GitHub Desktop.
Save aji/2202168 to your computer and use it in GitHub Desktop.
autojoin -- IRC bot to autojoin a channel and quit on a particular PRIVMSG. Does not respond to PING :/
#!/usr/bin/env python
import socket
import select
class AutoJoiner:
def __init__(self, server, port, nick, channel, deathmsg):
self.sock = socket.socket()
self.sock.connect((server, port))
self.sock.sendall('NICK {0}\r\n'.format(nick))
self.sock.sendall('USER autojoin 0 0 :AutoJoiner\r\n')
self.buf = ['']
self.channel = channel
self.deathmsg = deathmsg
def fileno(self):
return self.sock.fileno()
def dorecv(self):
recv = self.sock.recv(1024)
if recv == '':
return False
self.buf[-1] += recv.replace('\r', '')
while '\n' in self.buf[-1]:
before, sep, after = self.buf[-1].partition('\n')
self.buf[-1:] = [before, after]
retval = True
for line in self.buf[:-1]:
retval = self.recvline(line) and retval
self.buf = self.buf[-1:]
return retval
def recvline(self, line):
s = line.split()
splitline = line
if line[0] == ':':
splitline = line[1:]
msg = splitline.split(':', 1)
if len(msg) > 1:
msg = msg[1]
else:
msg = ''
if s[1] == '001':
self.sock.send('JOIN {0}\r\n'.format(self.channel))
if s[1] == 'PRIVMSG' and msg == self.deathmsg:
self.sock.send('QUIT :vanish!\r\n')
self.sock.close()
return False
return True
if __name__ == "__main__":
serv = raw_input("Enter server name (e.g. irc.freenode.net): ")
port = raw_input("Enter port number (probably 6667): ")
if port == '':
port = 6667
else:
port = int(port)
fix = raw_input("Nickname prefix: ")
count = int(raw_input("Number of joiners to spawn (no more than 5): "))
chan = raw_input("Channel to join: ")
deathmsg = raw_input("Enter secret quit phrase: ")
joiners = []
for i in range(count):
joiners.append(AutoJoiner(serv, port, '{0}-{1}'.format(fix, i), chan, deathmsg))
while len(joiners) > 0:
r, w, x = select.select(joiners, [], [])
for joiner in r:
if not joiner.dorecv():
joiners.remove(joiner)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment