File: __init__.py

package info (click to toggle)
kopanocore 8.7.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 15,400 kB
  • sloc: cpp: 175,422; python: 24,623; perl: 7,319; php: 6,056; sh: 2,172; makefile: 1,294; xml: 45; ansic: 1
file content (146 lines) | stat: -rw-r--r-- 5,763 bytes parent folder | download
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/python3
# SPDX-License-Identifier: AGPL-3.0-only
from .version import __version__

from copy import deepcopy
import hashlib
import hmac
import json
import os.path
import signal
import sys
sys.path.insert(0, os.path.dirname(__file__)) # XXX for __import__ to work
import time
import threading

import kopano
from kopano import log_exc, Config

from flask import Flask, request, abort

CONFIG = {
    'data_path': Config.string(default='/var/lib/kopano/presence/'),
    'data_save_interval': Config.integer(default=5),
    'plugins': Config.string(multiple=True, default=['spreed']),
    'run_as_user': Config.string(default="kopano"),
    'run_as_group': Config.string(default="kopano"),
    'server_bind': Config.string(default="127.0.0.1"),
    'server_port': Config.integer(default="1234"),
    'server_auth_user': Config.string(default="presence"),
    'server_auth_password': Config.string(default="presence"),
    'server_secret_key': Config.string(),
    'server_token_expire': Config.integer(default="5"),
    'xmpp_jid': Config.string(default=None),
    'xmpp_password': Config.string(default=None),
    'xmpp_user_id_strip_domain': Config.boolean(default=None),
    'spreed_auto_unavailable': Config.integer(default="2"),
}

STATUSES = ['available', 'busy', 'away', 'unavailable'] # XXX check these?

class Service(kopano.Service):
    def main(self):
        """ setup internal data, load plugins, setup signal handling, route GET/SET requests via Flask """

        self.data = {}
        self.lock = threading.Lock()

        self.plugins = {}
        for plugin in self.config['plugins']:
            self.plugins[plugin] = __import__('plugin_%s' % plugin).Plugin(self)

        for sig in (signal.SIGINT, signal.SIGTERM):
            signal.signal(sig, self.signal_handler)

        app = Flask('kopano_presence')
        app.add_url_rule('/', 'get', self.get, methods=['GET'])
        app.add_url_rule('/', 'put', self.put, methods=['PUT'])
        app.add_url_rule('/', 'post', self.post, methods=['POST'])
        app.run(host=self.config['server_bind'], port=self.config['server_port']) #, debug=True)

    def signal_handler(self, sig, frame):
        """ gracefully disconnect plugins on ctrl-c/kill signal """

        for plugin in self.plugins.values():
            plugin.disconnect()
        sys.exit(0)

    def check_auth(self):
        """ check shared-secret based authentication token """ 

        secret_key = str(self.config['server_secret_key'])
        t, userid, sha256 = str(request.json['AuthenticationToken']).split(':')
        if (sha256 != hmac.new(secret_key, '%s:%s' % (t, userid), hashlib.sha256).digest().encode('base64').strip().upper()) or \
           ((int(time.time()) - int(t)) > (self.config['server_token_expire'] * 60)):
            self.log.warning('unauthorized access; please check shared key settings in presence.cfg and client configuration.')
            abort(401)

    def get(self):
        """ return status for one or more users """

        self.check_auth()
        with log_exc(self.log):
            data = []
            for userstatus in request.json['UserStatus']:
                user_id = userstatus['user_id']
                userdata = self.data_get(user_id) or {}
                userdata['user_id'] = user_id
                data.append(userdata)
            return json.dumps({"Type": "UserStatus", "UserStatus": data}, indent=4)

    def put(self):
        """ update status for one or more users """

        self.check_auth()
        with log_exc(self.log):
            for userstatus in request.json['UserStatus']:
                user_id = userstatus['user_id']
                for plugin, plugin_data in userstatus.items():
                    if plugin != 'user_id': # XXX
                        plugin_data['user_id'] = user_id
                        plugin_data['last_update'] = int(time.time())
                        self.data_set(user_id, plugin, plugin_data.get('status'), plugin_data.get('message'))
                        self.plugins[plugin].update(user_id, plugin_data)
            return ''

    def post(self):
        """ update status for one or more users and return status for one or more users """

        self.check_auth()
        with log_exc(self.log):
            data = []
            for userstatus in request.json['UserStatus']:
                user_id = userstatus['user_id']
                for plugin, plugin_data in userstatus.items():
                    if plugin != 'user_id': # XXX
                        plugin_data['user_id'] = user_id
                        plugin_data['last_update'] = int(time.time())
                        self.data_set(user_id, plugin, plugin_data.get('status'), plugin_data.get('message'))
                        self.plugins[plugin].update(user_id, plugin_data)
                userdata = self.data_get(user_id) or {}
                userdata['user_id'] = user_id
                data.append(userdata)
            return json.dumps({"Type": "UserStatus", "UserStatus": data}, indent=4)

    def data_get(self, username):
        """ atomic get of user data """

        with self.lock:
            return deepcopy(self.data.get(username))

    def data_set(self, username, plugin, status, message):
        """ atomic update of user data """

        self.log.debug('%s: %s %s' % (plugin, username, status))
        with self.lock:
            userplugin = self.data.setdefault(username, {}).setdefault(plugin, {})
            userplugin['status'] = status
            userplugin['message'] = message

def main():
    parser = kopano.parser('ckpsF')
    options, args = parser.parse_args()
    Service('presence', config=CONFIG, options=options).start()
    
if __name__ == '__main__':
    main()