A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

frontend.py 2.6 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # -*- coding: utf-8 -*-
  2. ## Imports
  3. import re
  4. from config.irc import *
  5. from config.secure import *
  6. from irc import command_handler
  7. from irc.connection import Connection
  8. from irc.data import Data
  9. connection = None
  10. def get_connection():
  11. connection = Connection(HOST, PORT, NICK, IDENT, REALNAME)
  12. return connection
  13. def startup(conn):
  14. global connection
  15. connection = conn
  16. command_handler.init_commands(connection)
  17. connection.connect()
  18. def main():
  19. read_buffer = str()
  20. while 1:
  21. try:
  22. read_buffer = read_buffer + connection.get()
  23. except RuntimeError: # socket broke
  24. print "socket has broken on front-end; restarting bot..."
  25. return
  26. lines = read_buffer.split("\n")
  27. read_buffer = lines.pop()
  28. for line in lines:
  29. line = line.strip().split()
  30. data = Data()
  31. data.line = line
  32. if line[1] == "JOIN":
  33. data.nick, data.ident, data.host = re.findall(":(.*?)!(.*?)@(.*?)\Z", line[0])[0]
  34. data.chan = line[2][1:]
  35. command_handler.check("join", data) # check if there's anything we can respond to, and if so, respond
  36. if line[1] == "PRIVMSG":
  37. data.nick, data.ident, data.host = re.findall(":(.*?)!(.*?)@(.*?)\Z", line[0])[0]
  38. data.msg = ' '.join(line[3:])[1:]
  39. data.chan = line[2]
  40. if data.chan == NICK: # this is a privmsg to us, so set 'chan' as the nick of the sender
  41. data.chan = data.nick
  42. command_handler.check("msg_private", data) # only respond if it's a private message
  43. else:
  44. command_handler.check("msg_public", data) # only respond if it's a public (channel) message
  45. command_handler.check("msg", data) # check for general messages
  46. if data.msg.startswith("!restart"): # hardcode the !restart command (we can't restart from within an ordinary command)
  47. if data.host in OWNERS:
  48. print "restarting bot per owner request..."
  49. return
  50. if line[0] == "PING": # If we are pinged, pong back to the server
  51. connection.send("PONG %s" % line[1])
  52. if line[1] == "376":
  53. if NS_AUTH: # if we're supposed to auth to nickserv, do that
  54. connection.say("NickServ", "IDENTIFY %s %s" % (NS_USER, NS_PASS))
  55. for chan in CHANS: # join all of our startup channels
  56. connection.join(chan)