nicklist.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from util import strip_nick
  2. from event import Event
  3. try:
  4. from basemodule import BaseModule
  5. except ImportError:
  6. from modules.basemodule import BaseModule
  7. class Nicklist(BaseModule):
  8. def post_init(self):
  9. nicklisting_self_join = Event("__.nicklisting_self_join__")
  10. nicklisting_other_join = Event("__.nicklisting_other_join__")
  11. nicklisting_quit = Event("__.nicklisting_quit__")
  12. nicklisting_part = Event("__.nicklisting_part__")
  13. nicklisting_command = Event("__.nicklist__")
  14. nicklisting_command.define(msg_definition="^\.nicklist")
  15. nicklisting_self_join.define(message_id=353)
  16. nicklisting_other_join.define(definition="JOIN")
  17. nicklisting_quit.define(definition="QUIT")
  18. nicklisting_part.define(definition="PART")
  19. nicklisting_command.subscribe(self)
  20. nicklisting_self_join.subscribe(self)
  21. nicklisting_other_join.subscribe(self)
  22. nicklisting_quit.subscribe(self)
  23. nicklisting_part.subscribe(self)
  24. # register ourself to our new custom event(s)
  25. self.bot.register_event(nicklisting_self_join, self)
  26. self.bot.register_event(nicklisting_other_join, self)
  27. self.bot.register_event(nicklisting_quit, self)
  28. self.bot.register_event(nicklisting_command,self)
  29. self.bot.register_event(nicklisting_part,self)
  30. self.bot.mem_store['nicklist'] = dict()
  31. def handle(self, event):
  32. if event.message_id == 353:
  33. self.bot.mem_store['nicklist'][event.channel] = event.line.split(":")[2].split()
  34. if event.msg.startswith(".nicklist"):
  35. print self.bot.mem_store['nicklist'][event.channel]
  36. if event._type == "__.nicklisting_other_join__":
  37. try:
  38. self.bot.mem_store['nicklist'][event.channel].append(strip_nick(event.user))
  39. except KeyError:
  40. self.bot.mem_store['nicklist'][event.channel] = list()
  41. if event._type == "__.nicklisting_part__":
  42. try:
  43. self.bot.mem_store['nicklist'][event.channel].remove(strip_nick(event.user))
  44. except ValueError:
  45. pass
  46. if event._type == "__.nicklisting_quit__":
  47. try:
  48. for name, chan in self.bot.mem_store['nicklist'].iteritems():
  49. chan.remove(strip_nick(event.user))
  50. except ValueError:
  51. pass