Skip to content
This repository has been archived by the owner on Sep 13, 2023. It is now read-only.

Commit

Permalink
fix: errors in iq
Browse files Browse the repository at this point in the history
fix: unicode
  • Loading branch information
disabler committed Feb 1, 2014
1 parent e76dde3 commit ef0e7c4
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 98 deletions.
15 changes: 3 additions & 12 deletions kernel.py
Expand Up @@ -562,7 +562,7 @@ def no_joke(text): return text
jokes = [joke_blond,no_joke,joke_upyachka]
return random.choice(jokes)(text)

def msg_validator(t): return ''.join([[l,'?'][l<' ' and l not in '\n\t\r'] for l in unicode(t)])
def msg_validator(t): return ''.join([[l,'?'][l<' ' and l not in '\n\t\r' or l>u'\uff00'] for l in unicode(t)])

def message_exclude_update():
global messages_excl
Expand Down Expand Up @@ -833,16 +833,7 @@ def iqCB(sess,iq):
if getServer(Settings['jid']) == room: nnj = True

if iq.getType()=='error' and was_request:
iq_err,er_name = get_tag(unicode(iq),'error').replace('\n',''),L('Unknown error!',room)
detect_error = False
for tmp in iq_error.keys():
if tmp in iq_err:
er_name = '%s %s!' % (L('Error!',room),iq_error[tmp])
detect_error = True
break
if not detect_error:
if iq_err: er_name = '%s %s!' % (L('Error!',room),iq_err)
else: er_name = '%s %s!' % (L('Error!',room),er_name)
er_name = get_tag(unicode(iq),'error').replace('<','').split()[0]
iq_async(id,time.time(),er_name,'error')

elif iq.getType()=='result' and was_request:
Expand Down Expand Up @@ -915,7 +906,7 @@ def iq_async(*answ):
req = iq_request.pop(answ[0])
try: er_code = answ[3]
except: er_code = None
if er_code == 'error': is_answ = (answ[1]-req[0],(answ[2],))
if er_code == 'error': is_answ = (answ[1]-req[0],(answ[2],'error'))
elif req[3] == xmpp.NS_URN_PING or req[3] == answ[1]: is_answ = (answ[2]-req[0],answ[3:])
elif req[3] == xmpp.NS_VCARD and answ[1] == 'None':
answ[4].setTag('vCard',namespace=xmpp.NS_VCARD)
Expand Down
16 changes: 7 additions & 9 deletions plugins/disco.py
Expand Up @@ -762,8 +762,8 @@ def features(type, jid, nick, text):
sender(i)

def features_async(type, jid, nick, what, where, is_answ):
isa = is_answ[1]
if isa[0].startswith(L('Error! %s','%s/%s'%(jid,nick))%''): msg = isa[0]
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')]:
Expand All @@ -786,10 +786,7 @@ def features_async(type, jid, nick, what, where, is_answ):
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 = ''
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))}
Expand Down Expand Up @@ -851,8 +848,9 @@ def disco_r(type, jid, nick, text, raw_type):

def disco_features_async(type, jid, nick, what, where, hm, raw_type, is_answ):
isa = is_answ[1]
if isa[0].startswith(L('Error! %s','%s/%s'%(jid,nick))%''):
send_msg(type, jid, nick, isa[0])
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')]
Expand All @@ -862,7 +860,7 @@ def disco_features_async(type, jid, nick, what, where, hm, raw_type, is_answ):
sender(i)

def disco_async(type, jid, nick, what, where, hm, disco_type, raw_type, isa_prev, is_answ):
if is_answ[1][0].startswith(L('Error! %s','%s/%s'%(jid,nick))%''): msg = is_answ[1][0]
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 = []
Expand Down
4 changes: 2 additions & 2 deletions plugins/googleplus.py
Expand Up @@ -27,8 +27,8 @@ def gcalc(type, jid, nick, text):
if not text.strip(): msg = L('What?','%s/%s'%(jid,nick))
else:
try:
data = load_page('http://www.google.ru/search?', {'q': text.encode('utf-8'), 'hl': GT('youtube_default_lang')})
msg = ' '.join(re.findall('<div class="vk_gy vk_sh" style="margin-bottom:5px">(.*?)</div><div class="vk_ans vk_bk" style="margin-bottom:0">(.*?)</div>',data)[0]).decode('utf-8', 'ignore')
data = load_page('http://www.google.ru/search?', {'q': text.encode('utf-8'), 'hl': GT('youtube_default_lang')}).decode('utf-8', 'ignore')
msg = ' '.join(re.findall('<div class="vk_gy vk_sh" style="margin-bottom:5px">(.*?)</div><div class="vk_ans vk_bk" style="margin-bottom:0">(.*?)</div>',data)[0])
msg = msg.replace('<sup>2</sup>',u'²').replace('<sup>3</sup>',u'³')
except:
try: msg = reduce_spaces_all(' '.join(re.findall('<span class="cwclet" id="cwles">(.*?)</span>.*?<span class="cwcot" id="cwos">(.*?)</span>',data,re.S)[0])).strip()
Expand Down
8 changes: 4 additions & 4 deletions plugins/inlist.py
Expand Up @@ -33,8 +33,8 @@ def reban(type, jid, nick, text):
sender(i)

def reban_async(type, jid, nick, lim, iq_stanza):
is_answ = unicode(iq_stanza[1][0])
if is_answ.startswith(L('Error!','%s/%s'%(jid,nick))): msg = is_answ
isa = iq_stanza[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:
_trusted = get_config(getRoom(jid),'trusted_servers').split()
_jids = [tmp.getAttr('jid') for tmp in iq_stanza[1][0].getTag('query',namespace=xmpp.NS_MUC_ADMIN).getTags('item')]
Expand Down Expand Up @@ -80,8 +80,8 @@ def inlist_raw(type, jid, nick, text, affil, message):
sender(i)

def inlist_raw_async(type, jid, nick, text, message, iq_stanza):
is_answ = unicode(iq_stanza[1][0])
if is_answ.startswith(L('Error!','%s/%s'%(jid,nick))): msg = is_answ
isa = iq_stanza[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:
bb = [[tmp.getAttr('jid'),['',tmp.getTagData('reason')][tmp.getTagData('reason') != None]] for tmp in iq_stanza[1][0].getTag('query',namespace=xmpp.NS_MUC_ADMIN).getTags('item')]
bb.sort()
Expand Down
102 changes: 55 additions & 47 deletions plugins/iq.py
Expand Up @@ -112,49 +112,52 @@ def iq_vcard(type, jid, nick, text):
sender(i)

def vcard_async(type, jid, nick, text, args, is_answ):
try: vc,err = is_answ[1][1].getTag('vCard',namespace=xmpp.NS_VCARD),False
except: vc,err = is_answ[1][0],True
if not vc or unicode(vc) == '<vCard xmlns="vcard-temp" />': msg = '%s %s' % (L('vCard:','%s/%s'%(jid,nick)),L('Empty!','%s/%s'%(jid,nick)))
elif err: msg = '%s %s' % (L('vCard:','%s/%s'%(jid,nick)),vc[:VCARD_LIMIT_LONG])
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:
data = []
for t in vc.getChildren():
if t.getChildren():
cm = []
for r in t.getChildren():
if r.getData(): cm.append(('%s.%s' % (t.getName(),r.getName()),unicode(r.getData())))
data += cm
elif t.getData(): data.append((t.getName(),t.getData()))
if data:
try:
photo_size = sys.getsizeof(get_value_from_array2(data,'PHOTO.BINVAL').decode('base64'))
photo_type = get_value_from_array2(data,'PHOTO.TYPE')
data_photo = L('type %s, %s','%s/%s'%(jid,nick)) % (photo_type,get_size_human(photo_size))
data = [t for t in list(data) if t[0] not in ['PHOTO.BINVAL','PHOTO.TYPE']]
data.append(('PHOTO',data_photo))
except: pass
args = args.lower()
if not args:
dd = get_array_from_array2(data,['NICKNAME','FN','BDAY','URL','PHOTO','DESC'])
if dd: msg = '%s\n%s' % (L('vCard:','%s/%s'%(jid,nick)),'\n'.join(['%s: %s' % ([L(t[0]),t[0].capitalize()][L(t[0])==t[0]],[u'%s…' % t[1][:VCARD_LIMIT_LONG].strip(),t[1].strip()][len(t[1])<VCARD_LIMIT_LONG]) for t in dd]))
else: msg = '%s %s' % (L('vCard:','%s/%s'%(jid,nick)),L('Not found!','%s/%s'%(jid,nick)))
elif args == 'all': msg = '%s\n%s' % (L('vCard:','%s/%s'%(jid,nick)),'\n'.join(['%s: %s' % ([L(t[0]),t[0].capitalize()][L(t[0])==t[0]],[u'%s…' % t[1][:VCARD_LIMIT_SHORT].strip(),t[1].strip()][len(t[1])<VCARD_LIMIT_SHORT]) for t in data]))
elif args == 'show':
dd = []
for t in data:
if t[0] not in dd: dd.append(t[0])
msg = '%s %s' % (L('vCard:','%s/%s'%(jid,nick)),', '.join([[t.capitalize(),'%s (%s)' % (t.capitalize(),L(t))][L(t)!=t] for t in dd]))
else:
args,dd = args.split('|'),[]
for t in args:
if ':' in t: val,loc = t.split(':',1)
else: val,loc = t,t.upper()
val = val.upper()
dv = get_array_from_array2(data,(val))
if dv: dd += [[loc,dv[0][1]]]
if dd: msg = '%s\n%s' % (L('vCard:','%s/%s'%(jid,nick)),'\n'.join(['%s: %s' % ([L(t[0]),t[0].capitalize()][L(t[0])==t[0]],[u'%s…' % t[1][:VCARD_LIMIT_LONG],t[1]][len(t[1])<VCARD_LIMIT_LONG]) for t in dd]))
else: msg = '%s %s' % (L('vCard:','%s/%s'%(jid,nick)),L('Not found!','%s/%s'%(jid,nick)))
else: msg = '%s %s' % (L('vCard:','%s/%s'%(jid,nick)),L('Empty!','%s/%s'%(jid,nick)))
try: vc,err = isa[1].getTag('vCard',namespace=xmpp.NS_VCARD),False
except: vc,err = L('Error! %s','%s/%s'%(jid,nick)) % L(isa[0].capitalize().replace('-',' '),'%s/%s'%(jid,nick)),True
if not vc or unicode(vc) == '<vCard xmlns="vcard-temp" />': msg = '%s %s' % (L('vCard:','%s/%s'%(jid,nick)),L('Empty!','%s/%s'%(jid,nick)))
elif err: msg = '%s %s' % (L('vCard:','%s/%s'%(jid,nick)),vc[:VCARD_LIMIT_LONG])
else:
data = []
for t in vc.getChildren():
if t.getChildren():
cm = []
for r in t.getChildren():
if r.getData(): cm.append(('%s.%s' % (t.getName(),r.getName()),unicode(r.getData())))
data += cm
elif t.getData(): data.append((t.getName(),t.getData()))
if data:
try:
photo_size = sys.getsizeof(get_value_from_array2(data,'PHOTO.BINVAL').decode('base64'))
photo_type = get_value_from_array2(data,'PHOTO.TYPE')
data_photo = L('type %s, %s','%s/%s'%(jid,nick)) % (photo_type,get_size_human(photo_size))
data = [t for t in list(data) if t[0] not in ['PHOTO.BINVAL','PHOTO.TYPE']]
data.append(('PHOTO',data_photo))
except: pass
args = args.lower()
if not args:
dd = get_array_from_array2(data,['NICKNAME','FN','BDAY','URL','PHOTO','DESC'])
if dd: msg = '%s\n%s' % (L('vCard:','%s/%s'%(jid,nick)),'\n'.join(['%s: %s' % ([L(t[0]),t[0].capitalize()][L(t[0])==t[0]],[u'%s…' % t[1][:VCARD_LIMIT_LONG].strip(),t[1].strip()][len(t[1])<VCARD_LIMIT_LONG]) for t in dd]))
else: msg = '%s %s' % (L('vCard:','%s/%s'%(jid,nick)),L('Not found!','%s/%s'%(jid,nick)))
elif args == 'all': msg = '%s\n%s' % (L('vCard:','%s/%s'%(jid,nick)),'\n'.join(['%s: %s' % ([L(t[0]),t[0].capitalize()][L(t[0])==t[0]],[u'%s…' % t[1][:VCARD_LIMIT_SHORT].strip(),t[1].strip()][len(t[1])<VCARD_LIMIT_SHORT]) for t in data]))
elif args == 'show':
dd = []
for t in data:
if t[0] not in dd: dd.append(t[0])
msg = '%s %s' % (L('vCard:','%s/%s'%(jid,nick)),', '.join([[t.capitalize(),'%s (%s)' % (t.capitalize(),L(t))][L(t)!=t] for t in dd]))
else:
args,dd = args.split('|'),[]
for t in args:
if ':' in t: val,loc = t.split(':',1)
else: val,loc = t,t.upper()
val = val.upper()
dv = get_array_from_array2(data,(val))
if dv: dd += [[loc,dv[0][1]]]
if dd: msg = '%s\n%s' % (L('vCard:','%s/%s'%(jid,nick)),'\n'.join(['%s: %s' % ([L(t[0]),t[0].capitalize()][L(t[0])==t[0]],[u'%s…' % t[1][:VCARD_LIMIT_LONG],t[1]][len(t[1])<VCARD_LIMIT_LONG]) for t in dd]))
else: msg = '%s %s' % (L('vCard:','%s/%s'%(jid,nick)),L('Not found!','%s/%s'%(jid,nick)))
else: msg = '%s %s' % (L('vCard:','%s/%s'%(jid,nick)),L('Empty!','%s/%s'%(jid,nick)))
send_msg(type, jid, nick, msg)

def iq_uptime(type, jid, nick, text):
Expand Down Expand Up @@ -189,7 +192,8 @@ def ping(type, jid, nick, text):

def ping_async(type, jid, nick, text, is_answ):
global iq_ping_minimal
if '%s %s!' % (L('Error!','%s/%s'%(jid,nick)),L('Remote server not found','%s/%s'%(jid,nick))) == is_answ[1][0]: msg = is_answ[1][0]
isa = is_answ[1]
if len(isa) >= 2 and isa[1] == 'error' and isa[0] == 'remote-server-not-found': msg = L('Error! %s','%s/%s'%(jid,nick)) % L(isa[0].capitalize().replace('-',' '),'%s/%s'%(jid,nick))
else:
p_digits = GT('ping_digits')
original_ping = float(is_answ[0])
Expand Down Expand Up @@ -237,7 +241,7 @@ def iq_utime_get(type, jid, nick, text, mode):

def utime_async(type, jid, nick, text, mode, is_answ):
isa = is_answ[1]
if isa[0].startswith(L('Error! %s','%s/%s'%(jid,nick))%''): msg = isa[0]
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:
try:
ttup = isa[0].replace('T','-').replace('Z','').replace(':','-').split('-')+['0','0',str(tuple(time.localtime())[8])]
Expand Down Expand Up @@ -265,7 +269,9 @@ def iq_version_raw(type, jid, nick, text, with_caps):
sender(i)

def version_async(type, jid, nick, text, with_caps, is_answ):
msg = is_answ[1][0]
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: msg = isa[0]
if with_caps:
caps = get_caps(jid,[text,nick][text == ''])
if caps: msg += ' || %s' % caps
Expand All @@ -283,7 +289,9 @@ def iq_stats(type, jid, nick, text):

def stats_async_features(type, jid, nick, text, is_answ):
isa = is_answ[1]
if isa[0].startswith(L('Error! %s','%s/%s'%(jid,nick))%''): send_msg(type, jid, nick, isa[0])
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)
else:
try: stats_list = [t.getAttr('name') for t in isa[1].getTag('query',namespace=xmpp.NS_STATS).getTags('stat')]
except: stats_list = []
Expand All @@ -296,7 +304,7 @@ def stats_async_features(type, jid, nick, text, is_answ):

def stats_async(type, jid, nick, text, is_answ):
isa = is_answ[1]
if isa[0].startswith(L('Error! %s','%s/%s'%(jid,nick))%''): msg = isa[0]
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:
try: stats_list = [[L(t.getAttr('name')),L(t.getAttr('value')),L(t.getAttr('units'))] for t in isa[1].getTag('query',namespace=xmpp.NS_STATS).getTags('stat')]
except: stats_list = []
Expand Down
25 changes: 1 addition & 24 deletions plugins/main.py
Expand Up @@ -21,7 +21,7 @@
# #
# --------------------------------------------------------------------------- #

# translate: random,smart,full,partial,on,off,kick,ban,replace,mute,visitor,truncate,paste,chat,online,away,xa,dnd,on start,on shutdown,by time,black,white,without highlight,all,ban server,Loading...,Mon,Tue,Wed,Thu,Fri,Sat,Sun,_Jan,_Feb,_Mar,_Apr,_May,_Jun,_Jul,_Aug,_Sep,_Oct,_Nov,_Dec,no limit,visitor/none,visitor/member,participant/none,participant/member,moderator/none,moderator/member,moderator/admin,moderator/owner,bot owner,You should be at least %s to do it.,You must be a %s to do it.,absent
# translate: random,smart,full,partial,on,off,kick,ban,replace,mute,visitor,truncate,paste,chat,online,away,xa,dnd,on start,on shutdown,by time,black,white,without highlight,all,ban server,Loading...,Mon,Tue,Wed,Thu,Fri,Sat,Sun,_Jan,_Feb,_Mar,_Apr,_May,_Jun,_Jul,_Aug,_Sep,_Oct,_Nov,_Dec,no limit,visitor/none,visitor/member,participant/none,participant/member,moderator/none,moderator/member,moderator/admin,moderator/owner,bot owner,You should be at least %s to do it.,You must be a %s to do it.,absent,Bad request,Conflict,Feature not implemented,Forbidden,Gone,Internal server error,Item not found,Jid malformed,Not acceptable,Not allowed,Not authorized,Payment required,Recipient unavailable,Redirect,Registration required,Remote server not found,Remote server timeout,Resource constraint,Service unavailable,Subscription required,Undefined condition,Unexpected request

rlmas_min = (('&','&amp;'),('\"','&quot;'),('\'','&apos;'),('<','&lt;'),('>','&gt;'))

Expand Down Expand Up @@ -76,29 +76,6 @@

unlevlnum = [0,0,0,0,0,0,0,0,0,1]

iq_error = {'bad-request':L('Bad request'),
'conflict':L('Conflict'),
'feature-not-implemented':L('Feature not implemented'),
'forbidden':L('Forbidden'),
'gone':L('Gone'),
'internal-server-error':L('Internal server error'),
'item-not-found':L('Item not found'),
'jid-malformed':L('Jid malformed'),
'not-acceptable':L('Not acceptable'),
'not-allowed':L('Not allowed'),
'not-authorized':L('Not authorized'),
'payment-required':L('Payment required'),
'recipient-unavailable':L('Recipient unavailable'),
'redirect':L('Redirect'),
'registration-required':L('Registration required'),
'remote-server-not-found':L('Remote server not found'),
'remote-server-timeout':L('Remote server timeout'),
'resource-constraint':L('Resource constraint'),
'service-unavailable':L('Service unavailable'),
'subscription-required':L('Subscription required'),
'undefined-condition':L('Undefined condition'),
'unexpected-request':L('Unexpected request')}

presence_error = {'401':L('Password required'),
'403':L('Forbidden'),
'407':L('Members-only')}
Expand Down

0 comments on commit ef0e7c4

Please sign in to comment.