connect.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import json
  2. from bot import Bot
  3. from confman import ConfManager
  4. from event import Event
  5. try:
  6. from basemodule import BaseModule
  7. except ImportError:
  8. from modules.basemodule import BaseModule
  9. class Connect(BaseModule):
  10. def post_init(self):
  11. connect = Event("__.connect__")
  12. connect.define(msg_definition="^\.connect")
  13. connect.subscribe(self)
  14. self.cmd = '.connect <servername> <channelname> <port> <password (optional)>'
  15. self.help = self.cmd
  16. self.bot.register_event(connect, self)
  17. self.CONF_STRING = '{ "__pybot_conf":{ "db_type": "%s" }, "%s": '
  18. self.CONF_STRING += '{ "channels": [ "%s" ], "port":"%s", "nick": "%s","ircpass": "%s", "owner": "%s",'
  19. self.CONF_STRING += '"dbusername": "%s","dbpass": "%s","dbname": "%s" } }'
  20. def fill_conf(self, event):
  21. linesplit = event.msg.split()
  22. try:
  23. server = linesplit[1] # .connect servername channelname port nick password
  24. channel = linesplit[2]
  25. port = linesplit[3]
  26. except IndexError:
  27. self.say(event.user, self.cmd)
  28. return None
  29. try:
  30. nick = linesplit[4]
  31. except IndexError:
  32. nick = self.bot.NICK
  33. try:
  34. password = linesplit[5]
  35. except IndexError:
  36. password = ""
  37. db_type = self.bot.conf.getDBType()
  38. db_name = self.bot.conf.getDBName(self.bot.network)
  39. db_username = self.bot.conf.getDBUsername(self.bot.network)
  40. db_pass = self.bot.conf.getDBPass(self.bot.network)
  41. owner = self.bot.conf.getOwner(self.bot.network)
  42. output = self.CONF_STRING % (db_type, server, channel, port, nick, password,
  43. owner, db_username, db_pass, db_name)
  44. return output
  45. """
  46. we artificially generate a config and spawn another bot, then stick it in the botslist, then into the queue, so the original parent thread still has a handle on him
  47. """
  48. def handle(self, event):
  49. if not self.bot.brain._isAdmin(event.user):
  50. return
  51. import Queue
  52. q = Queue.Queue()
  53. a = self.fill_conf(event)
  54. f = self.bot.blist.get() # get the botslist out
  55. q.put(f)
  56. try:
  57. cm = ConfManager(a, string=True)
  58. except TypeError as e:
  59. e.args += (a,)
  60. self.bot.debug_print("error: ")
  61. self.bot.debug_print(e)
  62. self.bot.debug_print("a: ")
  63. self.bot.debug_print(a)
  64. return
  65. c = cm.getNetworks()
  66. b = Bot(conf=cm, network=c, d=self.bot.DEBUG, blist=q)
  67. b.start()
  68. self.bot.botslist.append(f)