module.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. else:
  29. self.bot.logger.write(Logger.WARNING, " failed to load " + event.msg.split()[2], self.bot.NICK)
  30. self.bot.brain.notice(event.channel, "failed to load " + event.msg.split()[2])
  31. if event.msg == ".module eventslist":
  32. for m in self.bot.events_list:
  33. print m._type
  34. if event.msg.startswith(".module list"):
  35. # the set prevents a module with multiple events being printed more than once
  36. modules_set = set()
  37. for m in self.bot.events_list:
  38. for s in m.subscribers:
  39. modules_set.add(s.__class__.__name__)
  40. print modules_set
  41. return
  42. if event.msg.startswith(".module unload"):
  43. for m in self.bot.events_list:
  44. for s in m.subscribers:
  45. if event.msg.split()[2].lower() == s.__class__.__name__.lower():
  46. self.printer("NOTICE " + event.channel + " :unloaded " + event.msg.split()[2] + '\n')
  47. # the events themselves hold onto the subscribing modules, so just remove that one.
  48. m.subscribers.remove(s)
  49. return
  50. if event.msg.startswith(".module reload"): # perform both unloading and reloading
  51. # first unload
  52. for m in self.bot.events_list:
  53. for s in m.subscribers:
  54. if event.msg.split()[2].lower() == s.__class__.__name__.lower():
  55. #self.printer("NOTICE " + event.channel + " :unloaded " + event.msg.split()[2] + '\n')
  56. # the events themselves hold onto the subscribing modules, so just remove that one.
  57. m.subscribers.remove(s)
  58. # then load
  59. #self.bot.logger.write(Logger.INFO, " loading " + event.msg.split()[2] + "...")
  60. retval = self.load(event.msg.split()[2])
  61. if retval == 0:
  62. self.bot.logger.write(Logger.INFO, " reloaded " + event.msg.split()[2], self.bot.NICK)
  63. self.bot.brain.notice(event.channel, "reloaded " + event.msg.split()[2])
  64. else:
  65. self.bot.logger.write(Logger.WARNING, " failed to reload " + event.msg.split()[2], self.bot.NICK)
  66. self.bot.brain.notice(event.channel, "failed to reload " + event.msg.split()[2])
  67. def load(self, modulename):
  68. return self.bot.load_modules(specific=modulename)