summary refs log tree commit diff
path: root/synapse/handlers/appservice.py
blob: 9cdeaa2d9474a1dff6a4f041a9beee71a12da9e4 (plain) (blame)
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
# -*- coding: utf-8 -*-
# Copyright 2015 OpenMarket Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from twisted.internet import defer

from ._base import BaseHandler
from synapse.api.errors import Codes, StoreError, SynapseError
from synapse.appservice import ApplicationService
from synapse.appservice.api import ApplicationServiceApi

import logging


logger = logging.getLogger(__name__)


class ApplicationServicesHandler(BaseHandler):

    def __init__(self, hs):
        super(ApplicationServicesHandler, self).__init__(hs)
        self.appservice_api = ApplicationServiceApi(hs)

    @defer.inlineCallbacks
    def register(self, app_service):
        logger.info("Register -> %s", app_service)
        # check the token is recognised
        try:
            stored_service = yield self.store.get_app_service_by_token(
                app_service.token
            )
            if not stored_service:
                raise StoreError(404, "Application service not found")
        except StoreError:
            raise SynapseError(
                403, "Unrecognised application services token. "
                "Consult the home server admin.",
                errcode=Codes.FORBIDDEN
            )
        logger.info("Updating application service info...")
        yield self.store.update_app_service(app_service)

    def unregister(self, token):
        logger.info("Unregister as_token=%s", token)
        yield self.store.unregister_app_service(token)

    def get_services_for_event(self, event, restrict_to=""):
        """Retrieve a list of application services interested in this event.

        Args:
            event(Event): The event to check.
            restrict_to(str): The namespace to restrict regex tests to.
        Returns:
            list<ApplicationService>: A list of services interested in this
            event based on the service regex.
        """
        # We need to know the aliases associated with this event.room_id, if any
        alias_list = []  # TODO

        interested_list = [
            s for s in self.store.get_app_services() if (
                s.is_interested(event, restrict_to, alias_list)
            )
        ]
        return interested_list

    def notify_interested_services(self, event):
        """Notifies (pushes) all application services interested in this event.

        Pushing is done asynchronously, so this method won't block for any
        prolonged length of time.

        Args:
            event(Event): The event to push out to interested services.
        """
        # Gather interested services
        services = self.get_services_for_event(event)
        if len(services) == 0:
            return  # no services need notifying

        # Do we know this user exists? If not, poke the user query API for
        # all services which match that user regex.
        unknown_user = False  # TODO check
        if unknown_user:
            user_query_services = self.get_services_for_event(
                event=event,
                restrict_to=ApplicationService.NS_USERS
            )
            for user_service in user_query_services:
                # this needs to block XXX: Need to feed response back to caller
                is_known_user = self.appservice_api.query_user(
                    user_service, event
                )
                if is_known_user:
                    # the user exists now,so don't query more ASes.
                    break

        # Do we know this room alias exists? If not, poke the room alias query
        # API for all services which match that room alias regex.
        unknown_room_alias = False  # TODO check
        if unknown_room_alias:
            alias_query_services = self.get_services_for_event(
                event=event,
                restrict_to=ApplicationService.NS_ALIASES
            )
            for alias_service in alias_query_services:
                # this needs to block XXX: Need to feed response back to caller
                is_known_alias = self.appservice_api.query_alias(
                    alias_service, event
                )
                if is_known_alias:
                    # the alias exists now so don't query more ASes.
                    break

        # Fork off pushes to these services - XXX First cut, best effort
        for service in services:
            self.appservice_api.push(service, event)