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.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # -*- coding: utf-8 -*-
  2. ## Imports
  3. import socket, string, re
  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. read_buffer = read_buffer + actions.sock.recv(1024)
  13. temp = string.split(read_buffer, "\n")
  14. read_buffer = temp.pop()
  15. for line in temp:
  16. line = string.split(string.rstrip(line))
  17. data = Data()
  18. if line[1] == "JOIN":
  19. data.nick, data.ident, data.host = re.findall(":(.*?)!(.*?)@(.*?)\Z", line[0])[0]
  20. data.chan = line[2][1:]
  21. triggers.check(actions, data, "join") # check if there's anything we can respond to, and if so, respond
  22. if line[1] == "PRIVMSG":
  23. data.nick, data.ident, data.host = re.findall(":(.*?)!(.*?)@(.*?)\Z", line[0])[0]
  24. data.msg = ' '.join(line[3:])[1:]
  25. data.chan = line[2]
  26. if data.chan == NICK: # this is a privmsg to us, so set 'chan' as the nick of the sender
  27. data.chan = data.nick
  28. triggers.check(actions, data, "msg_private") # only respond if it's a private message
  29. else:
  30. triggers.check(actions, data, "msg_public") # only respond if it's a public (channel) message
  31. triggers.check(actions, data, "msg") # check for general messages
  32. if data.msg == "!restart": # hardcode the !restart command (we can't return from within actions.py)
  33. if data.host in ADMINS:
  34. return True
  35. if line[0] == "PING": # If we are pinged, pong back to the server
  36. actions.send("PONG %s" % line[1])
  37. if line[1] == "376":
  38. if NS_AUTH: # if we're supposed to auth to nickserv, do that
  39. actions.say("NickServ", "IDENTIFY %s %s" % (NS_USER, NS_PASS))
  40. for chan in CHANS: # join all of our startup channels
  41. actions.join(chan)
  42. if __name__ == "__main__":
  43. sock = socket.socket()
  44. sock.connect((HOST, PORT))
  45. actions = Actions(sock)
  46. actions.send("NICK %s" % NICK)
  47. actions.send("USER %s %s bla :%s" % (IDENT, HOST, REALNAME))
  48. main()