A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bot.py 2.6 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # -*- coding: utf-8 -*-
  2. ## Imports
  3. import socket, string, re
  4. from actions import *
  5. from config import *
  6. from secure_config import *
  7. def send(msg): # send a message 'msg' to the server
  8. s.send(msg + "\r\n")
  9. print " %s" % msg
  10. def say(target, msg): # send a private message 'msg' to 'target'
  11. send("PRIVMSG %s :%s" % (target, msg))
  12. def action(target, msg): # send a message as an action
  13. say(target,"%sACTION %s%s" % (chr(1), msg, chr(1)))
  14. def notice(target, msg): # send a notice 'msg' to 'target'
  15. send("NOTICE %s :%s" % (target, msg))
  16. def join(chan): # join channel 'chan'
  17. send("JOIN %s" % chan)
  18. cmds = (s, send, say, action, notice, join) # pack up commands
  19. def main():
  20. readbuffer = str()
  21. data = [s, send, say, notice, join]
  22. while 1:
  23. readbuffer = readbuffer + s.recv(1024)
  24. temp = string.split(readbuffer, "\n")
  25. readbuffer = temp.pop()
  26. for line in temp:
  27. line2 = string.split(string.rstrip(line))
  28. if line2[1] == "JOIN":
  29. nick, ident, host = re.findall(":(.*?)!(.*?)@(.*?)\Z", line2[0])[0]
  30. chan = line2[2][1:]
  31. check_triggers(cmds, "join", nick, ident, host, chan) # check if there's anything we can respond to, and if so, respond
  32. if line2[1] == "PRIVMSG":
  33. nick, ident, host = re.findall(":(.*?)!(.*?)@(.*?)\Z", line2[0])[0]
  34. msg = ' '.join(line2[3:])[1:]
  35. chan = line2[2]
  36. if chan == NICK: # if line2[2] is us, this is a privmsg to us, so set 'chan' as the nick of the sender
  37. chan = nick
  38. check_triggers(cmds, "msg_private", nick, ident, host, chan, msg) # only respond if it's a private message
  39. else:
  40. check_triggers(cmds, "msg_public", nick, ident, host, chan, msg) # only respond if it's a public (channel) message
  41. check_triggers(cmds, "msg", nick, ident, host, chan, msg) # check for general messages
  42. if msg == "!restart": # hardcode the !restart command (we can't return from within actions.py)
  43. if host in ADMINS:
  44. return True
  45. if line2[0] == "PING": # If we are pinged, pong back to the server
  46. send("PONG %s" % line2[1])
  47. if line2[1] == "376":
  48. if NS_AUTH:
  49. say("NickServ", "IDENTIFY %s %s" % (NS_USER, NS_PASS))
  50. join(CHAN)
  51. if __name__ == "__main__":
  52. s = socket.socket()
  53. s.connect((HOST, PORT))
  54. send("NICK %s" % NICK)
  55. send("USER %s %s bla :%s" % (IDENT, HOST, REALNAME))
  56. main()