plugin_spreed.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import threading
  2. import time
  3. from kopano import log_exc
  4. class AutoUnavailableThread(threading.Thread):
  5. """ make users 'unavailable' after a configurable number of minutes """
  6. def run(self):
  7. while not self.stop:
  8. with log_exc(self.service.log):
  9. for info in self.data.values():
  10. if int(time.time()) - info['last_update'] > self.limit*60:
  11. self.service.log.info('spreed: auto unavailable')
  12. self.data.pop(info['user_id'])
  13. self.service.data_set(info['user_id'], 'spreed', 'unavailable', '')
  14. time.sleep(1)
  15. class Plugin:
  16. def __init__(self, service):
  17. """ just need to setup auto-unavailability thread """
  18. self.service = service
  19. self.log = service.log
  20. self.thread = AutoUnavailableThread()
  21. self.thread.data = {}
  22. self.thread.limit = service.config['spreed_auto_unavailable']
  23. self.thread.service = service
  24. self.thread.stop = False
  25. self.thread.start()
  26. service.log.info('spreed: plugin enabled')
  27. def update(self, user_id, info):
  28. """ when user becomes other than 'unavailable', add it to thread to monitor time """
  29. if info['status'] != 'unavailable':
  30. self.thread.data[user_id] = info
  31. def disconnect(self):
  32. self.thread.stop = True