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.

74 regels
2.5 KiB

  1. # -*- coding: utf-8 -*-
  2. ## Imports
  3. import re
  4. from config.irc_config import *
  5. from config.secure_config 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. if line[1] == "JOIN":
  32. data.nick, data.ident, data.host = re.findall(":(.*?)!(.*?)@(.*?)\Z", line[0])[0]
  33. data.chan = line[2][1:]
  34. command_handler.check("join", data) # check if there's anything we can respond to, and if so, respond
  35. if line[1] == "PRIVMSG":
  36. data.nick, data.ident, data.host = re.findall(":(.*?)!(.*?)@(.*?)\Z", line[0])[0]
  37. data.msg = ' '.join(line[3:])[1:]
  38. data.chan = line[2]
  39. if data.chan == NICK: # this is a privmsg to us, so set 'chan' as the nick of the sender
  40. data.chan = data.nick
  41. command_handler.check("msg_private", data) # only respond if it's a private message
  42. else:
  43. command_handler.check("msg_public", data) # only respond if it's a public (channel) message
  44. command_handler.check("msg", data) # check for general messages
  45. if data.msg.startswith("!restart"): # hardcode the !restart command (we can't restart from within an ordinary command)
  46. if data.host in OWNERS:
  47. print "restarting bot per owner request..."
  48. return
  49. if line[0] == "PING": # If we are pinged, pong back to the server
  50. connection.send("PONG %s" % line[1])
  51. if line[1] == "376":
  52. if NS_AUTH: # if we're supposed to auth to nickserv, do that
  53. connection.say("NickServ", "IDENTIFY %s %s" % (NS_USER, NS_PASS))
  54. for chan in CHANS: # join all of our startup channels
  55. connection.join(chan)