A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

69 рядки
2.5 KiB

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