plugin_xmpp.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. from kopano import log_exc
  2. import sleekxmpp
  3. STATUS_MAP = { # map xmpp statuses to statuses supported by kopano-presence
  4. 'available': 'available',
  5. 'chat': 'available',
  6. 'dnd': 'busy',
  7. 'away': 'away',
  8. 'xa': 'away',
  9. 'unavailable': 'unavailable',
  10. }
  11. class XmppPresence(sleekxmpp.ClientXMPP):
  12. """ monitor status events """
  13. def __init__(self, jid, password, service):
  14. """ setup event handlers """
  15. sleekxmpp.ClientXMPP.__init__(self, jid, password)
  16. self.service = service
  17. self.add_event_handler("session_start", self.start)
  18. self.add_event_handler("changed_status", self.status_event)
  19. def start(self, event):
  20. """ this is needed to get presence updates """
  21. self.send_presence()
  22. def status_event(self, msg):
  23. """ parse incoming status, and update presence service """
  24. with log_exc(self.service.log):
  25. if self.service.config['xmpp_user_id_strip_domain']:
  26. username = unicode(msg['from']).split('/')[0].split('@')[0] # strip entire domain
  27. else:
  28. username = unicode(msg['from']).split('/')[0].replace('@chat.', '@') # XXX chat?
  29. self.service.data_set(username, 'xmpp', STATUS_MAP[msg['type']], msg['status'])
  30. class Plugin:
  31. def __init__(self, service):
  32. """ setup xmpp background thread(s) """
  33. self.service = service
  34. self.log = service.log
  35. self.log.info('xmpp: connecting to server')
  36. self.xmpp = XmppPresence(service.config['xmpp_jid'], service.config['xmpp_password'], service)
  37. if self.xmpp.connect(reattempt=False):
  38. self.xmpp.process()
  39. self.log.info('xmpp: plugin enabled')
  40. else:
  41. self.log.error('xmpp: could not connect to server')
  42. def disconnect(self):
  43. self.xmpp.disconnect()