- if pieces[1] == "PRIVMSG":
- nick = pieces[0].split('!')[0][1:]
- user = self.parent.user(nick)
- chan = self.parent.channel(pieces[2])
- msg = ' '.join(pieces[3:])[1:]
- self.parsemsg(user, chan, msg)
- elif pieces[0] == "PING":
- self.conn.send("PONG %s" % (pieces[1]))
- elif pieces[1] == "JOIN":
- nick = pieces[0].split('!')[0][1:]
- user = self.parent.user(nick)
- chan = self.parent.channel(pieces[2]) #TODO TODO TODO
-
- def parsemsg(self, user, chan, msg):
- if msg[0] == '!': #TODO check config for trigger
- msg = msg[1:]
+
+ # dispatch dict
+ zero = { #things to look for without source
+ 'NOTICE': self._gotregistered,
+ 'PING': self._gotping,
+ 'ERROR': self._goterror,
+ }
+ one = { #things to look for after source
+ '001': self._got001,
+ 'PRIVMSG': self._gotprivmsg,
+ '353': self._got353, #NAMES
+ '354': self._got354, #WHO
+ 'JOIN': self._gotjoin,
+ 'PART': self._gotpart,
+ 'QUIT': self._gotquit,
+ 'NICK': self._gotnick,
+ 'MODE': self._gotmode,
+ }
+
+ if self.parent.hasnumhook(pieces[1]):
+ hooks = self.parent.getnumhook(pieces[1])
+ for callback in hooks:
+ try:
+ callback(self, line)
+ except Exception:
+ self.__debug_cbexception("numhook", line)
+
+ if pieces[0] in zero:
+ zero[pieces[0]](pieces)
+ elif pieces[1] in one:
+ one[pieces[1]](pieces)
+
+ def _gotregistered(self, pieces):
+ if not self.conn.registered():
+ self.conn.register()
+ def _gotping(self, pieces):
+ self.conn.send("PONG %s" % (pieces[1]))
+ def _goterror(self, pieces): #TODO handle more gracefully
+ curs = self.parent.db.cursor()
+ curs.execute("UPDATE bots SET connected = 0")
+ curs.close()
+ sys.exit(2)
+ os._exit(2)
+ def _got001(self, pieces):
+ self.conn.registered(True)
+
+ curs = self.parent.db.cursor()
+ curs.execute("UPDATE bots SET connected = 1 WHERE nick = %s", (self.nick,))
+ curs.close()
+
+ self.conn.send("MODE %s +x" % (pieces[2]))
+ if self.authname is not None and self.authpass is not None:
+ self.conn.send("AUTH %s %s" % (self.authname, self.authpass))
+ for c in self.chans:
+ self.join(c.name)
+ def _gotprivmsg(self, pieces):
+ nick = pieces[0].split('!')[0][1:]
+ user = self.parent.user(nick)
+ target = pieces[2]
+ msg = ' '.join(pieces[3:])[1:]
+ self.parsemsg(user, target, msg)
+ def _got353(self, pieces):
+ chan = self.parent.channel(pieces[4])
+ names = pieces[5:]
+ names[0] = names[0][1:] #remove colon
+ for n in names:
+ user = self.parent.user(n.lstrip('@+'))
+ if n[0] == '@':
+ chan.userjoin(user, 'op')
+ elif n[0] == '+':
+ chan.userjoin(user, 'voice')
+ else:
+ chan.userjoin(user)
+ user.join(chan)
+ def _got354(self, pieces):
+ qt = int(pieces[3])
+ if qt < 3:
+ nick, auth = pieces[4:6]
+ chan = None
+ else:
+ chan, nick, auth = pieces[4:7]
+ chan = self.parent.channel(chan)
+ user = self.parent.user(nick)
+ user.authed(auth)
+
+ if chan is not None:
+ user.join(chan)
+ chan.userjoin(user)
+
+ if qt == 2: # triggered by !auth
+ if user.isauthed():
+ if user.glevel > 0:
+ self.msg(nick, "You are now known as #%s (access level: %s)" % (auth, user.glevel))
+ else:
+ self.msg(nick, "You are now known as #%s (not staff)" % (auth))
+ else:
+ self.msg(nick, "I tried, but you're not authed!")
+ def _gotjoin(self, pieces):
+ nick = pieces[0].split('!')[0][1:]
+ chan = self.parent.channel(pieces[2])
+
+ if nick == self.nick:
+ self.conn.send("WHO %s c%%cant,3" % (chan))