module.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from logger import Logger
  2. from event import Event
  3. class Module:
  4. def __init__(self, events=None, printer_handle=None, bot=None, say=None):
  5. self.events = events
  6. self.printer = printer_handle
  7. self.bot = bot
  8. self.interests = []
  9. self.cmd = None
  10. self.help = None
  11. mod = Event("__.module__")
  12. mod.define(msg_definition="^\.module ")
  13. mod.subscribe(self)
  14. self.bot.register_event(mod, self)
  15. #for event in events:
  16. # if event._type in self.interests:
  17. # event.subscribe(self)
  18. def handle(self, event):
  19. #if not self.bot.conf.getOwner(self.bot.network) == event.user:
  20. if not self.bot.brain._isAdmin(event.user):
  21. return
  22. if event.msg.startswith(".module load"):
  23. self.bot.logger.write(Logger.INFO, " loading " + event.msg.split()[2] + "...", self.bot.NICK)
  24. retval = self.load(event.msg.split()[2])
  25. if retval == 0:
  26. self.bot.logger.write(Logger.INFO, " loaded " + event.msg.split()[2])
  27. self.bot.brain.notice(event.channel, "loaded " + event.msg.split()[2])
  28. elif retval == 2:
  29. self.bot.logger.write(Logger.INFO, " load failed; " + event.msg.split()[2] + " is already loaded")
  30. self.bot.brain.notice(event.channel, "load failed; " + event.msg.split()[2] + " is already loaded")
  31. else:
  32. self.bot.logger.write(Logger.WARNING, " failed to load " + event.msg.split()[2], self.bot.NICK)
  33. self.bot.brain.notice(event.channel, "failed to load " + event.msg.split()[2])
  34. if event.msg == ".module eventslist":
  35. for m in self.bot.events_list:
  36. print m._type
  37. if event.msg.startswith(".module list"):
  38. # the set prevents a module with multiple events being printed more than once
  39. modules_set = set()
  40. for m in self.bot.events_list:
  41. for s in m.subscribers:
  42. modules_set.add(s.__class__.__name__)
  43. print modules_set
  44. return
  45. if event.msg.startswith(".module unload"):
  46. for m in self.bot.events_list:
  47. for s in m.subscribers:
  48. if event.msg.split()[2].lower() == s.__class__.__name__.lower():
  49. self.printer("NOTICE " + event.channel + " :unloaded " + event.msg.split()[2] + '\n')
  50. # the events themselves hold onto the subscribing modules, so just remove that one.
  51. m.subscribers.remove(s)
  52. return
  53. if event.msg.startswith(".module reload"): # perform both unloading and reloading
  54. # first unload
  55. for m in self.bot.events_list:
  56. for s in m.subscribers:
  57. try:
  58. if event.msg.split()[2].lower() == s.__class__.__name__.lower():
  59. #self.printer("NOTICE " + event.channel + " :unloaded " + event.msg.split()[2] + '\n')
  60. # the events themselves hold onto the subscribing modules, so just remove that one.
  61. m.subscribers.remove(s)
  62. except IndexError:
  63. return
  64. # then load
  65. #self.bot.logger.write(Logger.INFO, " loading " + event.msg.split()[2] + "...")
  66. retval = self.load(event.msg.split()[2])
  67. if retval == 0:
  68. self.bot.logger.write(Logger.INFO, " reloaded " + event.msg.split()[2], self.bot.NICK)
  69. self.bot.brain.notice(event.channel, "reloaded " + event.msg.split()[2])
  70. else:
  71. self.bot.logger.write(Logger.WARNING, " failed to reload " + event.msg.split()[2], self.bot.NICK)
  72. self.bot.brain.notice(event.channel, "failed to reload " + event.msg.split()[2])
  73. def load(self, modulename):
  74. # this may seem redundant, but modules may become unloaded in between calls.
  75. modules_set = set()
  76. for m in self.bot.events_list:
  77. for s in m.subscribers:
  78. modules_set.add(s.__class__.__name__.lower())
  79. if modulename in modules_set:
  80. return 2
  81. return self.bot.load_modules(specific=modulename)