#!/usr/bin/python # -*- coding: utf-8 -*- # --------------------------------------------------------------------------- # # # # Plugin for iSida Jabber Bot # # Copyright (C) diSabler # # # # This program is free software: you can redistribute it and/or modify # # it under the terms of the GNU General Public License as published by # # the Free Software Foundation, either version 3 of the License, or # # (at your option) any later version. # # # # This program is distributed in the hope that it will be useful, # # but WITHOUT ANY WARRANTY; without even the implied warranty of # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # # GNU General Public License for more details. # # # # You should have received a copy of the GNU General Public License # # along with this program. If not, see . # # # # --------------------------------------------------------------------------- # whereis_answers = {} whereis_regx = re.compile(' 3: i=xmpp.Iq(to=room, typ='result') i.setAttr(key='id', val=id) i.setQueryNS(namespace=xmpp.NS_DISCO_ITEMS) if acclvl: i.getTag('query').setTag('item',attrs={'node':xmpp.NS_COMMANDS, 'name':L('AD-Hoc commands'),'jid':towh}) i.getTag('query').setTag('item',attrs={'node':xmpp.NS_MUC_ROOMS, 'name':L('Current bot rooms'),'jid':towh}) return i return None def disco_iq_set(iq,id,room,acclvl,query,towh,al): if iq.getTag(name='command', namespace=xmpp.NS_COMMANDS) and acclvl: node=get_tag_item(unicode(iq),'command','node') if get_tag_item(unicode(iq),'command','action') == 'execute' and (node.split('#')[0] in ['', disco_config_node, xmpp.NS_COMMANDS] or node == xmpp.NS_MUC_ROOMS): pprint('*** iq:ad-hoc commands from %s node "%s"' % (unicode(room),node),'magenta') i=xmpp.Iq(to=room, typ='result') i.setAttr(key='id', val=id) if node == '': i.setQueryNS(namespace=xmpp.NS_DISCO_INFO) else: i.setTag('query',namespace=xmpp.NS_DISCO_ITEMS,attrs={'node':node}) if node == '': i = disco_features_add(i) sender(disco_ext_info_add(i)) raise xmpp.NodeProcessed elif node == xmpp.NS_MUC_ROOMS and GT('iq_show_rooms_disco'): if al == 9: cnf = cur_execute_fetchall("select room from conference;") else: cnf = cur_execute_fetchall("select room from conference where split_part(room,'/',1) not in (select room from hiden_rooms as hrr);") trooms = [t[0] for t in cnf] trooms.sort() trooms = [xmpp.Node('item',attrs={'jid':t}) for t in trooms] i.getTag('query',namespace=xmpp.NS_DISCO_ITEMS).setPayload(trooms) return i elif node.split('#')[0] == disco_config_node or node == xmpp.NS_COMMANDS: if node != xmpp.NS_COMMANDS: i.getTag('query').setTag('feature',attrs={'var':xmpp.NS_COMMANDS}) #i.getTag('query').setTag('feature',attrs={'var':xmpp.NS_DATA}) #i.getTag('query').setTag('feature',attrs={'var':disco_config_node}) try: tn = '#' + node.split('#')[1] except: tn = '' try: tmpn = tn.split('-',1)[1] except: tmpn = '' if tmpn: action=get_tag_item(unicode(iq),'command','action') i=xmpp.Iq(to=room, typ='result') i.setAttr(key='id', val=id) if action == 'cancel': i.setTag('command',namespace=xmpp.NS_COMMANDS,attrs={'status':'canceled', 'node':disco_config_node+tn,'sessionid':id}) elif towh == selfjid: if get_tag_item(unicode(iq),'x','type') == 'submit': varz = iq.getTag('command').getTag('x') sucess_label,unsucess = True,[] for t in owner_prefs.keys(): try: tp = owner_prefs[t][1] tm = varz.getTag('field',attrs={'var':t}).getTags('value') tm = '\n'.join([tm2.getData() for tm2 in tm]) old_tm = GT(t) try: if tp[0] == 'b': tm = [False,True][int(tm)] elif tp[0] == 'f': tm = float(tm) elif tp[0] == 'i': tm = int(tm) elif tp[0] == 't': tm = tm[:int(tp.replace('e','')[1:])] elif tp[0] == 'm': tm = tm[:int(tp.replace('e','')[1:])] elif tp[0] == 'l' and len(json.loads(tm)) == int(tp.replace('e','')[1:]): tm = json.loads(tm) elif tp[0] == 'd' and tm not in owner_prefs[t][3]: tm = owner_prefs[t][2] except: tm,sucess_label = GT(t),False unsucess.append(L(owner_prefs[t][0],room)) PT(t,tm) if tp[-1] == 'e' and old_tm != tm: eval(owner_prefs[t][-1]) except: pass sucess_answer = [L('Settings unsuccesfully accepted:\n%s',room) % '\n'.join(unsucess),L('Settings succesfully accepted',room)][sucess_label] i.setTag('command',namespace=xmpp.NS_COMMANDS,attrs={'status':'completed', 'node':disco_config_node+tn,'sessionid':id}) i.getTag('command').setTag('note',attrs={'type':'info'}) i.getTag('command').setTagData('note',sucess_answer) i.getTag('command').setTag('x',namespace=xmpp.NS_DATA) i.getTag('command').getTag('x').setTagData('title',L('Settings',room)) i.getTag('command').getTag('x').setTagData('instrustion',sucess_answer) pprint('*** bot reconfigure by %s' % unicode(room),'purple') else: i.setTag('command',namespace=xmpp.NS_COMMANDS,attrs={'status':'executing', 'node':disco_config_node+tn,'sessionid':id}) i.getTag('command').setTag('x',namespace=xmpp.NS_DATA,attrs={'type':'form'}) #i.getTag('command').getTag('x').setTag('item',attrs={'node':disco_config_node+tn, 'name':'Configuration','jid':selfjid}) #i.getTag('command').getTag('x').setTagData('instructions',L('For configure required x:data-compatible client',room)) tkeys = [] for tmp in owner_groups: tkeys.append(tmp[1]) if tn in tkeys: for tmp in owner_groups: if tn == tmp[1]: c_prefs,c_name = tmp[2],tmp[0] break i.getTag('command').getTag('x').setTagData('title',L(c_name,room)) cnf_prefs = {} for tmp in c_prefs: cnf_prefs[tmp] = owner_prefs[tmp] tmp = cnf_prefs.keys() tt = [] for t in tmp: tt.append((L(owner_prefs[t][0],room),t)) tt.sort() tmp = [] for t in tt: tmp.append(t[1]) for t in tmp: itm = owner_prefs[t] itm_label = L(itm[0],room).replace('%s','').replace(':','').strip() if itm[1][0] == 'b': dc = GT(t) in [True,1,'1','on'] i.getTag('command').getTag('x').setTag('field',attrs={'type':'boolean','label':itm_label,'var':t})\ .setTagData('value',[0,1][dc]) elif itm[1][0] in ['t','i','f','l']: i.getTag('command').getTag('x').setTag('field',attrs={'type':'text-single','label':itm_label,'var':t})\ .setTagData('value',unicode(GT(t))) elif itm[1][0] == 'm': tprm = [xmpp.Node('value',payload=prm) for prm in unicode(GT(t)).split('\n')] i.getTag('command').getTag('x').setTag('field',attrs={'type':'text-multi','label':itm_label,'var':t})\ .setPayload(tprm) else: i.getTag('command').getTag('x').setTag('field',\ attrs={'type':'list-single','label':itm_label,'var':t})\ .setTagData('value',GT(t)) for t2 in itm[3]: i.getTag('command').getTag('x').getTag('field',\ attrs={'type':'list-single','label':itm_label,'var':t})\ .setTag('option',attrs={'label':L(t2,room)})\ .setTagData('value',t2) else: if get_tag_item(unicode(iq),'x','type') == 'submit': varz = iq.getTag('command').getTag('x') for t in config_prefs.keys(): try: tmtype = varz.getTagAttr('field','type') tm = varz.getTag('field',attrs={'var':t}).getTags('value') tm = '\n'.join([tm2.getData() for tm2 in tm]) if tmtype == 'boolean' and tm in ['0','1']: tm = [False,True][int(tm)] elif config_prefs[t][2] != None: if config_prefs[t][2] == [True,False] and tm in ['0','1']: tm = [False,True][int(tm)] elif tm in config_prefs[t][2]: pass else: tm = config_prefs[t][3] put_config(getRoom(room),t,tm) except: pass sucess_answer = L('Settings succesfully accepted',room) i.setTag('command',namespace=xmpp.NS_COMMANDS,attrs={'status':'completed', 'node':disco_config_node+tn,'sessionid':id}) i.getTag('command').setTag('note',attrs={'type':'info'}) i.getTag('command').setTagData('note',sucess_answer) i.getTag('command').setTag('x',namespace=xmpp.NS_DATA) i.getTag('command').getTag('x').setTagData('title',L('Settings',room)) i.getTag('command').getTag('x').setTagData('instrustion',sucess_answer) pprint('*** reconfigure by %s' % unicode(room),'purple') else: i.setTag('command',namespace=xmpp.NS_COMMANDS,attrs={'status':'executing', 'node':disco_config_node+tn,'sessionid':id}) i.getTag('command').setTag('x',namespace=xmpp.NS_DATA,attrs={'type':'form'}) #i.getTag('command').getTag('x').setTag('item',attrs={'node':disco_config_node+tn, 'name':'Configuration','jid':selfjid}) #i.getTag('command').getTag('x').setTagData('instructions',L('For configure required x:data-compatible client',room)) tkeys = [] for tmp in config_groups: tkeys.append(tmp[1]) if tn in tkeys: for tmp in config_groups: if tn == tmp[1]: c_prefs,c_name = tmp[2],tmp[0] break i.getTag('command').getTag('x').setTagData('title',L(c_name,room)) cnf_prefs = {} for tmp in c_prefs: cnf_prefs[tmp] = config_prefs[tmp] tmp = cnf_prefs.keys() tmp.sort() for t in tmp: itm = config_prefs[t] itm_label = L(itm[0],room).replace('%s','').replace(':','').strip() itm_desc = L(itm[1],room).strip() if itm[2] == [True,False]: dc = get_config(getRoom(room),t) in [True,1,'1','on'] i.getTag('command').getTag('x').setTag('field',attrs={'type':'boolean','label':itm_label,'var':t})\ .setTagData('value',[0,1][dc]) elif itm[2] == None: if '\n' in itm[3]: tprm = [xmpp.Node('value',payload=prm) for prm in get_config(getRoom(room),t).split('\n')] i.getTag('command').getTag('x').setTag('field',attrs={'type':'text-multi','label':itm_label,'var':t})\ .setPayload(tprm) else: i.getTag('command').getTag('x').setTag('field',attrs={'type':'text-single','label':itm_label,'var':t})\ .setTagData('value',get_config(getRoom(room),t)) else: i.getTag('command').getTag('x').setTag('field',\ attrs={'type':'list-single','label':itm_label,'var':t})\ .setTagData('value',get_config(getRoom(room),t)) for t2 in itm[2]: i.getTag('command').getTag('x').getTag('field',\ attrs={'type':'list-single','label':itm_label,'var':t})\ .setTag('option',attrs={'label':onoff(t2,room)})\ .setTagData('value',t2) i.getTag('command').getTag('x').getTag('field',attrs={'label':itm_label,'var':t})\ .setTagData('desc',itm_desc) else: i.getTag('command').getTag('x').setTagData('title',L('Unknown configuration request!',room)) return i else: if tn: if tn.split('-',1)[0] == '#owner': settz = owner_groups elif tn.split('-',1)[0] == '#room': settz = config_groups else: settz = None if settz: for tmp in settz: if tn == tmp[1]: i.getTag('query').setTag('identity',attrs={'category':'automation','type':'command-node','name':tmp[0]}) break return i else: pprint('*** iq:disco_set from %s node "%s"' % (unicode(room),node),'magenta') try: tn = '#' + node.split('#')[1] except: tn = '' if node.split('#')[0] == disco_config_node or node == xmpp.NS_COMMANDS: action=get_tag_item(unicode(iq),'command','action') i=xmpp.Iq(to=room, typ='result') i.setAttr(key='id', val=id) if action == 'cancel': i.setTag('command',namespace=xmpp.NS_COMMANDS,attrs={'status':'canceled', 'node':disco_config_node+tn,'sessionid':id}) elif towh == selfjid: if get_tag_item(unicode(iq),'x','type') == 'submit': varz = iq.getTag('command').getTag('x') sucess_label,unsucess = True,[] for t in owner_prefs.keys(): try: tp = owner_prefs[t][1] tm = varz.getTag('field',attrs={'var':t}).getTags('value') tm = '\n'.join([tm2.getData() for tm2 in tm]) old_tm = GT(t) try: if tp[0] == 'b': tm = [False,True][int(tm)] elif tp[0] == 'f': tm = float(tm) elif tp[0] == 'i': tm = int(tm) elif tp[0] == 't': tm = tm[:int(tp.replace('e','')[1:])] elif tp[0] == 'm': tm = tm[:int(tp.replace('e','')[1:])] elif tp[0] == 'l' and len(json.loads(tm)) == int(tp.replace('e','')[1:]): tm = json.loads(tm) elif tp[0] == 'd' and tm not in owner_prefs[t][3]: tm = owner_prefs[t][2] except: tm,sucess_label = GT(t),False unsucess.append(L(owner_prefs[t][0],room)) PT(t,tm) if tp[-1] == 'e' and old_tm != tm: eval(owner_prefs[t][-1]) except: pass sucess_answer = [L('Settings unsuccesfully accepted:\n%s',room) % '\n'.join(unsucess),L('Settings succesfully accepted',room)][sucess_label] i.setTag('command',namespace=xmpp.NS_COMMANDS,attrs={'status':'completed', 'node':disco_config_node+tn,'sessionid':id}) i.getTag('command').setTag('note',attrs={'type':'info'}) i.getTag('command').setTagData('note',sucess_answer) i.getTag('command').setTag('x',namespace=xmpp.NS_DATA) i.getTag('command').getTag('x').setTagData('title',L('Settings',room)) i.getTag('command').getTag('x').setTagData('instrustion',sucess_answer) pprint('*** bot reconfigure by %s' % unicode(room),'purple') else: i.setTag('command',namespace=xmpp.NS_COMMANDS,attrs={'status':'executing', 'node':disco_config_node+tn,'sessionid':id}) i.getTag('command').setTag('x',namespace=xmpp.NS_DATA,attrs={'type':'form'}) #i.getTag('command').getTag('x').setTag('item',attrs={'node':disco_config_node+tn, 'name':'Configuration','jid':selfjid}) #i.getTag('command').getTag('x').setTagData('instructions',L('For configure required x:data-compatible client',room)) tkeys = [] for tmp in owner_groups: tkeys.append(tmp[1]) if tn in tkeys: for tmp in owner_groups: if tn == tmp[1]: c_prefs,c_name = tmp[2],tmp[0] break i.getTag('command').getTag('x').setTagData('title',L(c_name,room)) cnf_prefs = {} for tmp in c_prefs: cnf_prefs[tmp] = owner_prefs[tmp] tmp = cnf_prefs.keys() tt = [] for t in tmp: tt.append((L(owner_prefs[t][0],room),t)) tt.sort() tmp = [] for t in tt: tmp.append(t[1]) for t in tmp: itm = owner_prefs[t] itm_label = L(itm[0],room).replace('%s','').replace(':','').strip() if itm[1] == 'b': dc = GT(t) in [True,1,'1','on'] i.getTag('command').getTag('x').setTag('field',attrs={'type':'boolean','label':itm_label,'var':t})\ .setTagData('value',[0,1][dc]) elif itm[1][0] in ['t','i','f','l']: i.getTag('command').getTag('x').setTag('field',attrs={'type':'text-single','label':itm_label,'var':t})\ .setTagData('value',unicode(GT(t))) elif itm[1][0] == 'm': tprm = [xmpp.Node('value',payload=prm) for prm in unicode(GT(t)).split('\n')] i.getTag('command').getTag('x').setTag('field',attrs={'type':'text-multi','label':itm_label,'var':t})\ .setPayload(tprm) else: i.getTag('command').getTag('x').setTag('field',\ attrs={'type':'list-single','label':itm_label,'var':t})\ .setTagData('value',GT(t)) for t2 in itm[3]: i.getTag('command').getTag('x').getTag('field',\ attrs={'type':'list-single','label':itm_label,'var':t})\ .setTag('option',attrs={'label':L(t2,room)})\ .setTagData('value',t2) else: if get_tag_item(unicode(iq),'x','type') == 'submit': varz = iq.getTag('command').getTag('x') for t in config_prefs.keys(): try: tmtype = varz.getTagAttr('field','type') tm = varz.getTag('field',attrs={'var':t}).getTags('value') tm = '\n'.join([tm2.getData() for tm2 in tm]) if tmtype == 'boolean' and tm in ['0','1']: tm = [False,True][int(tm)] elif config_prefs[t][2] != None: if config_prefs[t][2] == [True,False] and tm in ['0','1']: tm = [False,True][int(tm)] elif tm in config_prefs[t][2]: pass else: tm = config_prefs[t][3] put_config(getRoom(room),t,tm) except: pass sucess_answer = L('Settings succesfully accepted',room) i.setTag('command',namespace=xmpp.NS_COMMANDS,attrs={'status':'completed', 'node':disco_config_node+tn,'sessionid':id}) i.getTag('command').setTag('note',attrs={'type':'info'}) i.getTag('command').setTagData('note',sucess_answer) i.getTag('command').setTag('x',namespace=xmpp.NS_DATA) i.getTag('command').getTag('x').setTagData('title',L('Settings',room)) i.getTag('command').getTag('x').setTagData('instrustion',sucess_answer) pprint('*** reconfigure by %s' % unicode(room),'purple') else: i.setTag('command',namespace=xmpp.NS_COMMANDS,attrs={'status':'executing', 'node':disco_config_node+tn,'sessionid':id}) i.getTag('command').setTag('x',namespace=xmpp.NS_DATA,attrs={'type':'form'}) #i.getTag('command').getTag('x').setTag('item',attrs={'node':disco_config_node+tn, 'name':'Configuration','jid':selfjid}) #i.getTag('command').getTag('x').setTagData('instructions',L('For configure required x:data-compatible client',room)) tkeys = [] for tmp in config_groups: tkeys.append(tmp[1]) if tn in tkeys: for tmp in config_groups: if tn == tmp[1]: c_prefs,c_name = tmp[2],tmp[0] break i.getTag('command').getTag('x').setTagData('title',L(c_name,room)) cnf_prefs = {} for tmp in c_prefs: cnf_prefs[tmp] = config_prefs[tmp] tmp = cnf_prefs.keys() tmp.sort() for t in tmp: itm = config_prefs[t] itm_label = L(itm[0],room).replace('%s','').replace(':','').strip() itm_desc = L(itm[1],room).strip() if itm[2] == [True,False]: dc = get_config(getRoom(room),t) in [True,1,'1','on'] i.getTag('command').getTag('x').setTag('field',attrs={'type':'boolean','label':itm_label,'var':t})\ .setTagData('value',[0,1][dc]) elif itm[2] == None: if '\n' in itm[3]: tprm = [xmpp.Node('value',payload=prm) for prm in get_config(getRoom(room),t).split('\n')] i.getTag('command').getTag('x').setTag('field',attrs={'type':'text-multi','label':itm_label,'var':t})\ .setPayload(tprm) else: i.getTag('command').getTag('x').setTag('field',attrs={'type':'text-single','label':itm_label,'var':t})\ .setTagData('value',get_config(getRoom(room),t)) else: i.getTag('command').getTag('x').setTag('field',\ attrs={'type':'list-single','label':itm_label,'var':t})\ .setTagData('value',get_config(getRoom(room),t)) for t2 in itm[2]: i.getTag('command').getTag('x').getTag('field',\ attrs={'type':'list-single','label':itm_label,'var':t})\ .setTag('option',attrs={'label':onoff(t2,room)})\ .setTagData('value',t2) i.getTag('command').getTag('x').getTag('field',attrs={'label':itm_label,'var':t})\ .setTagData('desc',itm_desc) else: i.getTag('command').getTag('x').setTagData('title',L('Unknown configuration request!',room)) return i return None def disco_exclude_update(): global disco_excl excl = GT('disco_exclude').replace('\r','').replace('\t','').split('\n') disco_excl = [] for c in excl: if '#' not in c and len(c): disco_excl.append(c) def disco_validate(item): for c in disco_excl: if re.findall(c,' %s ' % item,re.S|re.I|re.U): return None return item def smart_sort(item): itm1,itm2,cnt = [],[],0 for t in item: itm1.append([t.lower(),cnt]) cnt += 1 itm1.sort() for t in itm1: itm2.append(item[t[1]]) return itm2 def features(type, jid, nick, text): global iq_answer,iq_request text = text.strip() if text == '': where,what = '%s/%s' % (getRoom(jid),nick),'' else: text = text.split('\n') where = text[0] try: what = text[1] except: what = '' for mega1 in megabase: if mega1[0] == jid and mega1[1] == text[0]: where = '%s/%s' % (getRoom(jid),text[0]) break iqid = get_id() i = xmpp.Node('iq', {'id': iqid, 'type': 'get', 'to':where}, payload = [xmpp.Node('query', {'xmlns': xmpp.NS_DISCO_INFO},[])]) iq_request[iqid]=(time.time(),features_async,[type, jid, nick, what, where],xmpp.NS_DISCO_INFO) sender(i) def features_async(type, jid, nick, what, where, is_answ): isa = is_answ[1] if len(isa) >= 2 and isa[1] == 'error': msg = L('Error! %s','%s/%s'%(jid,nick)) % L(isa[0].capitalize().replace('-',' '),'%s/%s'%(jid,nick)) else: isa, ftr, client_features = isa[1], [], [] for f in [t.getAttr('var') for t in isa.getTag('query',namespace=xmpp.NS_DISCO_INFO).getTags('feature')]: client_features.append(f) if disco_features_list.has_key(f): ft = '- %s' % disco_features_list[f] else: ft = L('- Unknown feature: %s','%s/%s'%(jid,nick)) % f if (what and (what.lower() in ft.lower() or what.lower() in f.lower())) or not what: ftr.append(ft) ftrs,erc,q_features = {},0,['os_version','os','software_version','software'] client_softwareinfo = {} for t in q_features: try: res = isa.getTag('query').getTag('x',namespace=xmpp.NS_DATA).getTag('field',attrs={'var':t}).getTagData('value') client_softwareinfo[t] = res except: res = 'N/A' erc += 1 ftrs[t] = res if erc != len(q_features): f = L('Software: %s | Version: %s\nOS: %s | Version: %s','%s/%s'%(jid,nick)) % (ftrs['software'],ftrs['software_version'],ftrs['os'],ftrs['os_version']) if (what and what.lower() in f.lower()) or not what: ftr.append(f) id_category,id_type,id_name,id_lang = '','','','' try: ids_t = isa.getTag('query').getTags('identity') idk = {'type':L('Type: %s','%s/%s'%(jid,nick)), 'name':L('Name: %s','%s/%s'%(jid,nick)), 'category':L('Category: %s','%s/%s'%(jid,nick)), 'xml:lang':L('Language: %s','%s/%s'%(jid,nick))} for tg in ids_t: ids = tg.getAttrs() idf = [] for t in idk.keys(): if ids.has_key(t): idf.append(idk[t] % ids[t]) if t == 'type': id_type = ids[t] elif t == 'name': id_name = ids[t] elif t == 'category': id_category = ids[t] elif t == 'xml:lang': id_lang = ids[t] if idf: f = ' | '.join(idf) if (what and what.lower() in f.lower()) or not what: ftr.append(f) except: pass if ftr: f = [] for tmp in ftr: if tmp not in f: f.append(tmp) f.sort() msg = L('Features list:\n%s','%s/%s'%(jid,nick)) % '\n'.join(f) _hash = '%s<' % '/'.join([t.replace('/','//') for t in [id_category,id_type,id_lang,id_name]]) client_features.sort() _hash += ''.join(['%s<' % t for t in client_features]) if client_softwareinfo: _hash += '%s<' % xmpp.NS_SOFTWAREINFO tmp = ['%s<%s' % (t,client_softwareinfo[t]) for t in client_softwareinfo.keys()] tmp.sort() _hash += ''.join(['%s<' % t for t in tmp]) _hash = _hash.encode('utf-8') _hash_sha1 = 'SHA1: %s' % hashlib.sha1(_hash).digest().encode('base64').replace('\n','') _hash_md5 = 'MD5: %s' % hashlib.md5(_hash).digest().encode('base64').replace('\n','') _hashes = '%s | %s' % (_hash_sha1,_hash_md5) if (what and what.lower() in _hashes.lower()) or not what: msg = '%s\n%s' % (msg, _hashes) else: msg = L('Unable to get features list','%s/%s'%(jid,nick)) send_msg(type, jid, nick, msg) def disco(type, jid, nick, text): disco_r(type, jid, nick, text, True) def disco_raw(type, jid, nick, text): disco_r(type, jid, nick, text, False) def disco_r(type, jid, nick, text, raw_type): global iq_answer,iq_request text = text.strip() if text == '': send_msg(type, jid, nick, L('What?','%s/%s'%(jid,nick))) return where = text.lower().split('\n',1)[0].split(' ',1)[0] try: what = text.lower().split('\n',1)[0].split(' ',1)[1] except: what = '' try: hm = int(text.lower().split('\n',1)[1]) except: hm = GT('disco_max_limit') iqid = get_id() i = xmpp.Node('iq', {'id': iqid, 'type': 'get', 'to':where}, payload = [xmpp.Node('query', {'xmlns': xmpp.NS_DISCO_INFO},[])]) iq_request[iqid]=(time.time(),disco_features_async,[type, jid, nick, what, where, hm, raw_type],xmpp.NS_DISCO_INFO) sender(i) def disco_features_async(type, jid, nick, what, where, hm, raw_type, is_answ): isa = is_answ[1] if len(isa) >= 2 and isa[1] == 'error': msg = L('Error! %s','%s/%s'%(jid,nick)) % L(isa[0].capitalize().replace('-',' '),'%s/%s'%(jid,nick)) send_msg(type, jid, nick, msg) return else: disco_type = xmpp.NS_MUC in [t.getAttr('var') for t in isa[1].getTag('query',namespace=xmpp.NS_DISCO_INFO).getTags('feature')] iqid = get_id() i = xmpp.Node('iq', {'id': iqid, 'type': 'get', 'to':where}, payload = [xmpp.Node('query', {'xmlns': xmpp.NS_DISCO_ITEMS},[])]) iq_request[iqid]=(time.time(),disco_async,[type, jid, nick, what, where, hm, disco_type, raw_type, isa[1]],xmpp.NS_DISCO_ITEMS) sender(i) def disco_async(type, jid, nick, what, where, hm, disco_type, raw_type, isa_prev, is_answ): if len(is_answ[1]) >= 2 and is_answ[1][1] == 'error': msg = L('Error! %s','%s/%s'%(jid,nick)) % L(is_answ[1][0].capitalize().replace('-',' '),'%s/%s'%(jid,nick)) else: if disco_type and '@' not in where: cm = [] for ii in [[t.getAttr('name'),t.getAttr('jid')] for t in is_answ[1][1].getTag('query',namespace=xmpp.NS_DISCO_ITEMS).getTags('item')]: dname,djid = ii if not dname: dname = '' elif '(' in dname and ')' in dname: try: dsize = int(dname.split('(')[-1].split(')')[0]) except: dsize = -1 dname = '('.join(dname.split('(')[:-1]) else: dsize = -1 if dname == djid: djid = '' if not what or what in dname.lower() or what in djid.lower(): if raw_type and disco_validate(dname) and disco_validate(djid): cm.append(('%04d' % dsize, dname, djid)) elif not raw_type: cm.append(('%04d' % dsize, dname, djid)) if len(cm): cm.sort(reverse=True) msg,cnt = L('Total: %s','%s/%s'%(jid,nick)) % len(cm),1 for i in cm[:hm]: vl = [int(i[0]),'n/a'][int(i[0]) == -1] if len(i[2]): msg += '\n%s. %s [%s] . %s' % (cnt,i[1],i[2],vl) else: msg += '\n%s. %s . %s' % (cnt,i[1],vl) cnt += 1 while ' ' in msg: msg = msg.replace(' ',' ') elif len(what): msg = L('\"%s\" not found','%s/%s'%(jid,nick)) % what else: msg = L('Not found.','%s/%s'%(jid,nick)) elif disco_type and '@' in where: cm = [] for ii in [t.getAttr('name') for t in is_answ[1][1].getTag('query',namespace=xmpp.NS_DISCO_ITEMS).getTags('item')]: if ii and (not what or what in ii.lower()): cm.append(ii) cm = smart_sort(cm) if len(cm): d_name = reduce_spaces_all(isa_prev.getTag('query',namespace=xmpp.NS_DISCO_INFO).getTagAttr('identity','name')) d_deskr = reduce_spaces_all(isa_prev.getTag('query',namespace=xmpp.NS_DISCO_INFO).getTag('x',namespace=xmpp.NS_DATA).getTag('field',attrs={'var':'muc#roominfo_description'}).getTagData('value')) d_occup = isa_prev.getTag('query',namespace=xmpp.NS_DISCO_INFO).getTag('x',namespace=xmpp.NS_DATA).getTag('field',attrs={'var':'muc#roominfo_occupants'}).getTagData('value') if d_name == d_deskr or not d_deskr: msg = '%s\n%s' % (d_name,L('Total: %s%s','%s/%s'%(jid,nick)) % (d_occup,' - %s' % ', '.join(cm))) else: msg = '%s [%s]\n%s' % (d_name,d_deskr,L('Total: %s%s','%s/%s'%(jid,nick)) % (d_occup,' - %s' % ', '.join(cm))) elif len(what): msg = L('\"%s\" not found','%s/%s'%(jid,nick)) % what else: msg = L('Not found.','%s/%s'%(jid,nick)) else: cm = [t.getAttrs() for t in is_answ[1][1].getTag('query',namespace=xmpp.NS_DISCO_ITEMS).getTags('item')] if len(cm): cm.sort() cnt = 1 msg = L('Total: %s','%s/%s'%(jid,nick)) % len(cm) for i in cm[:hm]: msg += '\n%s. ' % cnt if i.has_key('name'): msg += '%s ' % i['name'] if i.has_key('node'): msg += '[%s] ' % i['node'] msg += '%s' % i['jid'] cnt += 1 else: msg = L('Not found.','%s/%s'%(jid,nick)) msg = rss_replace(msg) send_msg(type, jid, nick, msg) def whereis(type, jid, nick, text): global iq_request,whereis_lock if whereis_lock: send_msg(type, jid, nick, L('This command in use somewhere else. Please try later.','%s/%s'%(jid,nick))) else: if len(text): text = text.split('\n') who = text[0] else: who = nick if len(text)<2: where = getServer(jid) else: if 'conference' in text[1]: where = text[1] else: where = 'conference.'+text[1] iqid = get_id() i = xmpp.Node('iq', {'id': iqid, 'type': 'get', 'to':where}, payload = [xmpp.Node('query', {'xmlns': xmpp.NS_DISCO_ITEMS},[])]) iq_request[iqid]=(time.time(),whereis_async,[type, jid, nick, who, where],xmpp.NS_DISCO_ITEMS) sender(i) def whereis_async(type, jid, nick, who, where, is_answ): global iq_request, whereis_answers, whereis_lock isan = unicode(is_answ[1][0]) isa = isan.split('