module.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. if event.msg.split()[2].lower() == s.__class__.__name__.lower():
  58. #self.printer("NOTICE " + event.channel + " :unloaded " + event.msg.split()[2] + '\n')
  59. # the events themselves hold onto the subscribing modules, so just remove that one.
  60. m.subscribers.remove(s)
  61. # then load
  62. #self.bot.logger.write(Logger.INFO, " loading " + event.msg.split()[2] + "...")
  63. retval = self.load(event.msg.split()[2])
  64. if retval == 0:
  65. self.bot.logger.write(Logger.INFO, " reloaded " + event.msg.split()[2], self.bot.NICK)
  66. self.bot.brain.notice(event.channel, "reloaded " + event.msg.split()[2])
  67. else:
  68. self.bot.logger.write(Logger.WARNING, " failed to reload " + event.msg.split()[2], self.bot.NICK)
  69. self.bot.brain.notice(event.channel, "failed to reload " + event.msg.split()[2])
  70. def load(self, modulename):
  71. # this may seem redundant, but modules may become unloaded in between calls.
  72. modules_set = set()
  73. for m in self.bot.events_list:
  74. for s in m.subscribers:
  75. modules_set.add(s.__class__.__name__.lower())
  76. if modulename in modules_set:
  77. return 2
  78. return self.bot.load_modules(specific=modulename)