def call_cmd_handlers(self, t, s, b): groupchat = s.split("/")[0] b = self.alias_engine(groupchat, b) params = b.split() nick = s[len(groupchat) + 1 :] try: item = self.g[groupchat][nick] except: item = new_item(self) item.jid = s item.realjid = s if len(params): cmd = b.split()[0] params = b[len(cmd) + 1 :] for i in self.cmdhandlers: if cmd.lower() == i[1]: if item.allowed(i[2]): if (item.room and item.room.bot) or not i[3]: try: i[0](t, item, params) except: item.msg( t, u"".join( traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback) ), ) else: item.lmsg(t, "muc_only") else: item.lmsg(t, "not_allowed")
def call_cmd_handlers(self, t, s, body, subject, stanza): if subject: return #found or create item groupchat = s.split('/')[0] nick = s[len(groupchat)+1:] try: item = self.g[groupchat][nick] except: item = new_item(self) item.jid = s item.realjid = s if item.room and (item.nick == self.muc.get_nick(item.room.jid)): self.log.log(u'own message from %s ignored' % (escape(item.jid), ), 2) return #check for bad words if item.room and (t == 'groupchat'): self.check_text(item, body) #launch rewrite engines commands = [(t, item, body, stanza)] itercount = 0 rl = config.REWRITE_POWER changed = True while changed and (len(commands) < rl) and (itercount < rl): itercount += 1 self.log.log(escape('launch rewrite engines.. step %s, commands=%s' % (itercount, commands)), 1) changed = False for engine in self.rewriteengines: r_commands = [] for command in commands: r_command = self.call(engine, command) if r_command: r_commands = r_commands + r_command changed = True else: r_commands = r_commands + [command] commands = r_commands self.log.log(escape('rewrite engines done, itercount=%s, commands=%s' % (itercount, commands)), 1) if itercount == rl: item.lmsg(t, 'rewrite_cycle', rl) elif len(commands) >= rl: item.lmsg(t, 'rewrite_too_many_commands', rl) else: #execute commands, generated by rewrite engines commands = [command for command in commands if self.check_for_ddos(command[1].jid)] # check if not DDOS for command in commands: [t, s, b, stanza] = command params = b.split() if len(params): cmd = b.split()[0] params = b[len(cmd)+1:] for i in self.cmdhandlers: if cmd.lower() == i[1]: if s.allowed(i[2]): if (s.room and s.room.bot) or not i[3]: self.log.log(u'Calling command handler <b>%s</b> for command <font color=red>%s</font> from <font color=blue>%s (%s)</font><br/>stanza: <font color=grey>%s</font>' % (escape(repr(i[0])), escape(b), escape(s.jid), escape(repr(s)), escape(stanza.toXml())), 4) try: i[0](t, s, params) except: m = ''.join(traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback)) self.log.err(escape(m)) item.lmsg(t, 'ERROR', config.ERRLOGFILE) else: s.lmsg(t, 'muc_only') else: s.lmsg(t, 'not_allowed')
def presence_handler(self, x): self.bot.log.log(u'presence_handler..', 2) try: typ = x['type'] except: typ = 'available' jid = x['from'].split('/') groupchat = jid[0] nick = x['from'][len(groupchat)+1:] groupchat = self.bot.g.get(groupchat, 0) if groupchat: if typ == 'available': item = groupchat.setdefault(nick, new_item(self.bot, groupchat)) item.jid = x['from'] try: item.status = [i for i in x.children if i.name=='status'][0].children[0] except: item.status = '' try: item.show = [i for i in x.children if i.name=='show'][0].children[0] except: item.show = 'online' item.nick = nick try: _x = [i for i in x.children if (i.name=='x') and (i.uri=='http://jabber.org/protocol/muc#user')][0] _item = [i for i in _x.children if i.name=='item'][0] item.affiliation = _item['affiliation'] item.role = _item['role'] try: item.realjid = _item['jid'].split('/')[0] except: item.realjid = item.jid if not item.handled: self.call_join_handlers(item) except: self.bot.log.err(u"Got invalid presence from '%s'?\n%s: %s" % (x['from'], sys.exc_info()[0], sys.exc_info()[1])) if item.nick == self.get_nick(groupchat.jid): groupchat.bot = item else: item = groupchat.pop(nick, 0) if item: self.call_leave_handlers(item) if item.nick == self.get_nick(groupchat.jid): self.bot.g.pop(groupchat.jid) else: self.bot.log.err("'unavailable' presence from %s, but %s not in groupchatmap" % (x['from'], x['from'])) else: if typ in ('subscribe', 'subscribed', 'unsubscribe', 'unsubscribed'): p = domish.Element(('jabber:client', 'presence')) p['type'] = typ p['to'] = x['from'] self.bot.wrapper.send(p) self.bot.log.log('ROSTER: %s - %s' % (typ, p['to']), 5)
def presence_handler(self, x): if self.bot.stopped: return self.bot.log.log(u'presence_handler..', 1) try: typ = x['type'] except: typ = 'available' jid = x['from'].split('/') groupchat = jid[0] nick = x['from'][len(groupchat)+1:] groupchat = self.bot.g.get(groupchat) if groupchat: if typ == 'available': item = groupchat.setdefault(nick, new_item(self.bot, groupchat)) item.jid = x['from'] try: item.status = [i for i in x.children if i.name=='status'][0].children[0] except: item.status = '' try: item.show = [i for i in x.children if i.name=='show'][0].children[0] except: item.show = 'online' item.nick = nick try: _x = [i for i in x.children if (i.name=='x') and (i.uri=='http://jabber.org/protocol/muc#user')][0] _item = [i for i in _x.children if i.name=='item'][0] item.affiliation = _item['affiliation'] item.role = _item['role'] try: item.realjid = _item['jid'].split('/')[0] item.fulljid = _item['jid'] except: item.realjid = item.jid item.fulljid = item.jid except: self.bot.log.err(u"Got invalid presence from '%s'?\n%s: %s<br/><font color=grey>%s</font>" % (x['from'], escape(repr(sys.exc_info()[0])), escape(repr(sys.exc_info()[1])), escape(x.toXml()))) if not item.handled: if item.nick == self.get_nick(groupchat.jid): # if item is bot... groupchat.bot = item gj = groupchat.joiner if gj: gj[0].lmsg(gj[1], 'join_success', groupchat.jid, nick) self.bot.log.log(u'reporting to %s about successful joining..' % (gj[0].jid, ), 6) groupchat.joiner = None item.handled = True self.bot.call_join_handlers(item) if not(item.nick == self.get_nick(groupchat.jid)): #if item isn't bot... self.bot.check_text(item, item.nick) self.bot.check_text(item, item.status) else: item = groupchat.pop(nick, None) if item: # parse leave_type, reason # leave_type: 0: leave # 1: kick # 2: ban # 3: rename if typ == 'unavailable': #unavailable try: _x = [i for i in x.children if (i.name=='x') and (i.uri == 'http://jabber.org/protocol/muc#user')][0] _item = [i for i in _x.children if i.name=='item'][0] _status = [i['code'] for i in _x.children if i.name=='status'] try: new_nick = _item['nick'] except: new_nick = '[unknown nick]' if '303' in _status: leave_type = 3 elif '301' in _status: leave_type = 2 elif '307' in _status: leave_type = 1 else: leave_type = 0 if leave_type == 0: try: reason = [i for i in x.children if i.name=='status'][0].children[0] except: reason = '' else: try: reason = [i for i in _item.children if i.name=='reason'][0].children[0] except: reason = '' except: self.bot.log.err(u"Got invalid presence from '%s'?\n%s: %s<br/><font color=grey>%s</font>" % (x['from'], escape(repr(sys.exc_info()[0])), escape(repr(sys.exc_info()[1])), escape(x.toXml()))) leave_type = 0 reason = '' if leave_type == 3: #if item changes nickname reason = item.nick item.jid = u'%s/%s' % (groupchat.jid, new_nick) item.nick = new_nick groupchat[new_nick] = item self.bot.call_leave_handlers(item, leave_type, reason) if config.ROOM_LIMIT: if (item.nick == self.get_nick(groupchat.jid)) and (leave_type <> 3): self.leave(groupchat.jid) else: #error self.bot.log.err(u'unknown error presence: ' + escape(x.toXml())) else: if typ == 'error': # check error code error = [q for q in x.elements() if q.name=='error'] if error: code = error[0]['code'] else: code = None if '409' in code: self.join(groupchat.jid, '%s-%s' % (nick, randint(1,100))) if '503' in code: pass if not code is None: if code == '': pass else: pass gj = groupchat.joiner if gj: self.bot.log.log(u'reporting to %s about failed joining..' % \ (escape(gj[0].jid), ), 6) gj[0].lmsg(gj[1], 'join_failed', groupchat.jid, nick) groupchat.joiner = None if x['from'].endswith(self.get_nick(groupchat.jid)) and config.ROOM_LIMIT: self.bot.log.log_e(u'leave because of error presence from %s\nstanza:\n%s' % \ (x['from'], x.toXml()), 7) self.leave(groupchat.jid, 'error presence...', True) else: self.bot.log.err(u'unexpected error presence from %s\nstanza:\n%s' % \ (escape(x['from']), escape(x.toXml()))) else: self.bot.log.err(u'unexpected unavailable presence from %s\nstanza:\n%s' % \ (escape(x['from']), escape(x.toXml()))) else: if typ in ('subscribe', 'subscribed', 'unsubscribe', 'unsubscribed'): p = domish.Element(('jabber:client', 'presence')) p['type'] = typ p['to'] = x['from'] self.bot.wrapper.send(p) self.bot.log.log('ROSTER: %s - %s' % (typ, p['to']), 5)
def presence_handler(self, x): if self.bot.stopped: return self.bot.log.log(u'presence_handler..', 1) try: typ = x['type'] except: typ = 'available' jid = x['from'].split('/') groupchat = jid[0] nick = x['from'][len(groupchat) + 1:] groupchat = self.bot.g.get(groupchat) if groupchat: if typ == 'available': item = groupchat.setdefault(nick, new_item(self.bot, groupchat)) item.jid = x['from'] try: item.status = [ i for i in x.children if i.name == 'status' ][0].children[0] except: item.status = '' try: item.show = [i for i in x.children if i.name == 'show'][0].children[0] except: item.show = 'online' item.nick = nick try: _x = [ i for i in x.children if (i.name == 'x') and ( i.uri == 'http://jabber.org/protocol/muc#user') ][0] _item = [i for i in _x.children if i.name == 'item'][0] item.affiliation = _item['affiliation'] item.role = _item['role'] try: item.realjid = _item['jid'].split('/')[0] item.fulljid = _item['jid'] except: item.realjid = item.jid item.fulljid = item.jid except: self.bot.log.err( u"Got invalid presence from '%s'?\n%s: %s<br/><font color=grey>%s</font>" % (x['from'], escape(repr(sys.exc_info()[0])), escape(repr(sys.exc_info()[1])), escape(x.toXml()))) if not item.handled: if item.nick == self.get_nick( groupchat.jid): # if item is bot... groupchat.bot = item gj = groupchat.joiner if gj: gj[0].lmsg(gj[1], 'join_success', groupchat.jid, nick) self.bot.log.log( u'reporting to %s about successful joining..' % (gj[0].jid, ), 6) groupchat.joiner = None item.handled = True self.bot.call_join_handlers(item) if not (item.nick == self.get_nick( groupchat.jid)): #if item isn't bot... self.bot.check_text(item, item.nick) self.bot.check_text(item, item.status) else: item = groupchat.pop(nick, None) if item: # parse leave_type, reason # leave_type: 0: leave # 1: kick # 2: ban # 3: rename if typ == 'unavailable': #unavailable try: _x = [ i for i in x.children if (i.name == 'x') and (i.uri == 'http://jabber.org/protocol/muc#user' ) ][0] _item = [ i for i in _x.children if i.name == 'item' ][0] _status = [ i['code'] for i in _x.children if i.name == 'status' ] try: new_nick = _item['nick'] except: new_nick = '[unknown nick]' if '303' in _status: leave_type = 3 elif '301' in _status: leave_type = 2 elif '307' in _status: leave_type = 1 else: leave_type = 0 if leave_type == 0: try: reason = [ i for i in x.children if i.name == 'status' ][0].children[0] except: reason = '' else: try: reason = [ i for i in _item.children if i.name == 'reason' ][0].children[0] except: reason = '' except: self.bot.log.err( u"Got invalid presence from '%s'?\n%s: %s<br/><font color=grey>%s</font>" % (x['from'], escape(repr(sys.exc_info()[0])), escape(repr( sys.exc_info()[1])), escape(x.toXml()))) leave_type = 0 reason = '' if leave_type == 3: #if item changes nickname reason = item.nick item.jid = u'%s/%s' % (groupchat.jid, new_nick) item.nick = new_nick groupchat[new_nick] = item self.bot.call_leave_handlers(item, leave_type, reason) if config.ROOM_LIMIT: if (item.nick == self.get_nick( groupchat.jid)) and (leave_type <> 3): self.leave(groupchat.jid) else: #error self.bot.log.err(u'unknown error presence: ' + escape(x.toXml())) else: if typ == 'error': # check error code error = [q for q in x.elements() if q.name == 'error'] if error: code = error[0]['code'] else: code = None if '409' in code: self.join(groupchat.jid, '%s-%s' % (nick, randint(1, 100))) if '503' in code: pass if not code is None: if code == '': pass else: pass gj = groupchat.joiner if gj: self.bot.log.log(u'reporting to %s about failed joining..' % \ (escape(gj[0].jid), ), 6) gj[0].lmsg(gj[1], 'join_failed', groupchat.jid, nick) groupchat.joiner = None if x['from'].endswith(self.get_nick( groupchat.jid)) and config.ROOM_LIMIT: self.bot.log.log_e(u'leave because of error presence from %s\nstanza:\n%s' % \ (x['from'], x.toXml()), 7) self.leave(groupchat.jid, 'error presence...', True) else: self.bot.log.err(u'unexpected error presence from %s\nstanza:\n%s' % \ (escape(x['from']), escape(x.toXml()))) else: self.bot.log.err(u'unexpected unavailable presence from %s\nstanza:\n%s' % \ (escape(x['from']), escape(x.toXml()))) else: if typ in ('subscribe', 'subscribed', 'unsubscribe', 'unsubscribed'): p = domish.Element(('jabber:client', 'presence')) p['type'] = typ p['to'] = x['from'] self.bot.wrapper.send(p) self.bot.log.log('ROSTER: %s - %s' % (typ, p['to']), 5)
def do_createitem(self, line): item.new_item(line)
def call_cmd_handlers(self, t, s, body, subject, stanza): if t == 'error': return if subject: return delayed = [ i for i in stanza.children if (i.__class__ == domish.Element) and ((i.name == 'delay') or ( (i.name == 'x') and (i.uri == 'jabber:x:delay'))) ] if delayed: return #found or create item groupchat = s.split('/')[0] nick = s[len(groupchat) + 1:] try: item = self.g[groupchat][nick] except: item = new_item(self) item.jid = s item.fulljid = s item.realjid = JID(s).userhost() if item.room and (item.nick == self.muc.get_nick(item.room.jid)): self.log.log(u'own message from %s ignored' % (escape(item.jid), ), 2) return #check for bad words if item.room and (t == 'groupchat'): self.check_text(item, body) #launch rewrite engines commands = [(t, item, body, stanza)] itercount = 0 rl = config.REWRITE_POWER changed = True while changed and (len(commands) < rl) and (itercount < rl): itercount += 1 self.log.log( escape('launch rewrite engines.. step %s, commands=%s' % (itercount, commands)), 1) changed = False for engine in self.rewriteengines: r_commands = [] for command in commands: r_command = self.call(engine, command) if r_command: r_commands = r_commands + r_command changed = True else: r_commands = r_commands + [command] commands = r_commands self.log.log( escape('rewrite engines done, itercount=%s, commands=%s' % (itercount, commands)), 1) if itercount == rl: item.lmsg(t, 'rewrite_cycle', rl) elif len(commands) >= rl: item.lmsg(t, 'rewrite_too_many_commands', rl) else: #execute commands, generated by rewrite engines commands = [ command for command in commands if self.check_for_ddos(command[1].jid) ] # check if not DDOS for command in commands: [t, s, b, stanza] = command params = b.split() if len(params): cmd = b.split()[0] params = b[len(cmd) + 1:] for i in self.cmdhandlers: if cmd.lower() == i[1]: if s.allowed(i[2]): if (s.room and s.room.bot) or not i[3]: self.log.log( u'Calling command handler %s for command <font color=red>%s</font> from <font color=blue>%s </font><br/><font color=grey>%s</font>' % (escape(repr(i[0])), escape(b), escape(s.jid), escape( stanza.toXml())), 4) try: i[0](t, s, params) except: m = ''.join( traceback.format_exception( sys.exc_type, sys.exc_value, sys.exc_traceback)) self.log.err(escape(m)) item.lmsg(t, 'ERROR', config.ERRLOGFILE) else: s.lmsg(t, 'muc_only') else: s.lmsg(t, 'not_allowed')