def read(data): # irc msg format = :<prefix> <command> <params> :<trailing> # regex created from: http://calebdelnay.com/blog/2010/11/parsing-the-irc-message-format-as-a-client match = re.search(r'^(:(?P<prefix>\S+) )?(?P<command>\S+)(?: (?!:)(?P<params>.+?))?( :(?P<trail>.+))?$', data) prefix = match.group('prefix') command = match.group('command') params = match.group('params') trail = match.group('trail') # user message: prefix = username, command = PRIMSG, trail = msg if command == 'PRIVMSG': m = re.search(r'^{}: kill (\w+)'.format(NICK), trail) if m: read.kill_user = m.groups()[0] print read.kill_user if read.kill_user and read.kill_user in prefix: stream.write("PRIVMSG {} :{}: f**k you\r\n".format(CHAN, read.kill_user)) if '<sloth>' in trail: try: search_terms = re.findall(r'<sloth> (\w+\S*)*', trail) if search_terms: search_terms = search_terms[0] img_name = find_sloth.find_sloth(search_terms) url = imgup.upload_sloth(img_name) stream.write("PRIVMSG {} :{}\r\n".format(CHAN, url)) except: pass elif command == 'PING': # don't kick the sloth :( stream.write("PONG {}\r\n".format(trail)) stream.read_until('\r\n', read)
PORT=6667 NICK="sloth_bot" IDENT="sloth_bot" REALNAME="sloth_both" CHAN= # INSERT CHANNEL HERE readbuffer="" s=socket.socket( ) s.connect((HOST, PORT)) s.send("NICK %s\r\n" % NICK) s.send("USER %s %s bla :%s\r\n" % (IDENT, HOST, REALNAME)) s.send("JOIN :%s\r\n" % CHAN) while 1: readbuffer=readbuffer+s.recv(1024) temp=string.split(readbuffer, "\n") readbuffer=temp.pop( ) for line in temp: line=string.rstrip(line) line=string.split(line) print line if (len(line) != 0 and line[1] == 'PRIVMSG'): text = line.pop() # last element if (len(re.findall(r'<sloth>',text)) != 0): img_name = find_sloth.find_sloth() url = imgup.upload_sloth(img_name) s.send("PRIVMSG %s :%s\r\n" % (CHAN, url))