def presence_handler self if self bot stopped return self bot log log

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
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]
except: item.realjid = 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 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)