+ def __init__(self, cfg):
+ self.cfg = cfg
+ self.trigger = cfg.trigger
+ if os.name == "posix":
+ self.potype = "poll"
+ self.po = select.poll()
+ else: # f.e. os.name == "nt" (Windows)
+ self.potype = "select"
+ self.fdlist = []
+
+ def newbot(self, nick, user, bind, authname, authpass, server, port, realname):
+ if bind is None: bind = ''
+ obj = bot.Bot(self, nick, user, bind, authname, authpass, server, port, realname)
+ self.bots[nick.lower()] = obj
+
+ def newfd(self, obj, fileno):
+ self.fds[fileno] = obj
+ if self.potype == "poll":
+ self.po.register(fileno, select.POLLIN)
+ elif self.potype == "select":
+ self.fdlist.append(fileno)
+
+ def bot(self, name): #get Bot() by name (nick)
+ return self.bots[name.lower()]
+ def fd(self, fileno): #get Bot() by fd/fileno
+ return self.fds[fileno]
+ def randbot(self): #get Bot() randomly
+ return self.bots[random.choice(self.bots.keys())]
+
+ def user(self, _nick, justjoined=False):
+ nick = _nick.lower()
+ if nick in self.users:
+ return self.users[nick]
+ else:
+ user = self.User(_nick)
+ self.users[nick] = user
+
+ if justjoined:
+ self.randbot().conn.send("WHO %s n%%ant,2" % (nick))
+
+ return user
+ def channel(self, name): #get Channel() by name
+ if name.lower() in self.chans:
+ return self.chans[name.lower()]
+ else:
+ return None
+
+ def newchannel(self, bot, name):
+ chan = self.Channel(name.lower(), bot)
+ self.chans[name.lower()] = chan
+ return chan
+
+ def poll(self):
+ if self.potype == "poll":
+ return [fd for (fd, ev) in self.po.poll()]
+ elif self.potype == "select":
+ return select.select(self.fdlist, [], [])[0]
+
+ def connectall(self):
+ for bot in self.bots.itervalues():
+ if bot.conn.state == 0:
+ bot.connect()
+
+ def module(self, name):
+ return ctlmod.modules[name]
+
+ #bind functions
+ def hook(self, word, handler):
+ try:
+ self.msghandlers[word].append(handler)
+ except:
+ self.msghandlers[word] = [handler]
+ def unhook(self, word, handler):
+ if word in self.msghandlers and handler in self.msghandlers[word]:
+ self.msghandlers[word].remove(handler)
+ def hashook(self, word):
+ return word in self.msghandlers and len(self.msghandlers[word]) != 0
+ def gethook(self, word):
+ return self.msghandlers[word]
+
+ def hooknum(self, word, handler):
+ try:
+ self.numhandlers[word].append(handler)
+ except:
+ self.numhandlers[word] = [handler]
+ def unhooknum(self, word, handler):
+ if word in self.numhandlers and handler in self.numhandlers[word]:
+ self.numhandlers[word].remove(handler)
+ def hasnumhook(self, word):
+ return word in self.numhandlers and len(self.numhandlers[word]) != 0
+ def getnumhook(self, word):
+ return self.numhandlers[word]
+
+ def hookchan(self, chan, handler):
+ try:
+ self.chanhandlers[chan].append(handler)
+ except:
+ self.chanhandlers[chan] = [handler]
+ def unhookchan(self, chan, handler):
+ if chan in self.chanhandlers and handler in self.chanhandlers[chan]:
+ self.chanhandlers[chan].remove(handler)
+ def haschanhook(self, chan):
+ return chan in self.chanhandlers and len(self.chanhandlers[chan]) != 0
+ def getchanhook(self, chan):
+ return self.chanhandlers[chan]
+
+
+class MyCursor(MySQLdb.cursors.DictCursor):
+ def execute(self, *args, **kwargs):
+ print "[SQL] [#] MyCursor.execute(self, %s, %s)" % (', '.join([repr(i) for i in args]), ', '.join([str(key)+"="+repr(kwargs[key]) for key in kwargs]))
+ try:
+ super(self.__class__, self).execute(*args, **kwargs)
+ except MySQLdb.MySQLError as e:
+ print "[SQL] [!] MySQL error! %r" % (e)
+ dbsetup()
+ return False
+ return True
+
+
+def dbsetup():
+ main.db = None
+ main.db = MySQLdb.connect(host=cfg.dbhost, user=cfg.dbuser, passwd=cfg.dbpass, db=cfg.dbname, cursorclass=MyCursor)