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.

bot.py 2.5 KiB

13 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # -*- coding: utf-8 -*-
  2. ## Imports
  3. import socket, re, time
  4. from config.irc_config import *
  5. from config.secure_config import *
  6. from irc import triggers
  7. from irc.actions import *
  8. from irc.data import *
  9. def main():
  10. read_buffer = str()
  11. while 1:
  12. try:
  13. read_buffer = read_buffer + actions.get()
  14. except RuntimeError: # socket broke
  15. print "socket has broken, sleeping for a minute and restarting..."
  16. time.sleep(60) # sleep for sixty seconds
  17. return # then exit our loop and restart the bot
  18. lines = read_buffer.split("\n")
  19. read_buffer = lines.pop()
  20. for line in lines:
  21. line = line.strip().split()
  22. data = Data()
  23. if line[1] == "JOIN":
  24. data.nick, data.ident, data.host = re.findall(":(.*?)!(.*?)@(.*?)\Z", line[0])[0]
  25. data.chan = line[2][1:]
  26. triggers.check(actions, data, "join") # check if there's anything we can respond to, and if so, respond
  27. if line[1] == "PRIVMSG":
  28. data.nick, data.ident, data.host = re.findall(":(.*?)!(.*?)@(.*?)\Z", line[0])[0]
  29. data.msg = ' '.join(line[3:])[1:]
  30. data.chan = line[2]
  31. if data.chan == NICK: # this is a privmsg to us, so set 'chan' as the nick of the sender
  32. data.chan = data.nick
  33. triggers.check(actions, data, "msg_private") # only respond if it's a private message
  34. else:
  35. triggers.check(actions, data, "msg_public") # only respond if it's a public (channel) message
  36. triggers.check(actions, data, "msg") # check for general messages
  37. if data.msg == "!restart": # hardcode the !restart command (we can't return from within actions.py)
  38. if data.host in ADMINS:
  39. return True
  40. if line[0] == "PING": # If we are pinged, pong back to the server
  41. actions.send("PONG %s" % line[1])
  42. if line[1] == "376":
  43. if NS_AUTH: # if we're supposed to auth to nickserv, do that
  44. actions.say("NickServ", "IDENTIFY %s %s" % (NS_USER, NS_PASS))
  45. for chan in CHANS: # join all of our startup channels
  46. actions.join(chan)
  47. if __name__ == "__main__":
  48. sock = socket.socket()
  49. sock.connect((HOST, PORT))
  50. actions = Actions(sock)
  51. actions.send("NICK %s" % NICK)
  52. actions.send("USER %s %s bla :%s" % (IDENT, HOST, REALNAME))
  53. main()