summaryrefslogtreecommitdiff
path: root/service/src/pixelated/resources/mixnet_resource.py
blob: aa895c91588bb1ec6cec178d769a9cfc1bd96bf8 (plain)
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
#
# Copyright (c) 2017 LEAP
#
# Pixelated is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pixelated is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.

import json

from email.utils import parseaddr
from pixelated.resources import respond_json_deferred, BaseResource
from twisted.web import server
from twisted.logger import Logger
from txzmq import ZmqEndpoint, ZmqEndpointType
from txzmq import ZmqFactory, ZmqREQConnection

from leap.bitmask.core import ENDPOINT


class MixnetResource(BaseResource):
    # XXX: this is hacky, we should use bitmask.js properly in the web-ui
    #      But, if we and up doing zmq interface, we can do
    #      something more generic for all the API.

    isLeaf = True
    log = Logger()

    def render_GET(self, request):
        zf = ZmqFactory()
        e = ZmqEndpoint(ZmqEndpointType.connect, ENDPOINT)
        _conn = ZmqREQConnection(zf, e)

        _mail = self.mail_service(request)
        userid = _mail.account_email
        _, address = parseaddr(request.args.get('search')[0])

        def callback(resp_json):
            response = json.loads(resp_json[0])
            if response['error'] is not None:
                respond_json_deferred(response['error'], request, status_code=404)
            else:
                respond_json_deferred(response['result'], request, status_code=200)

        def err(fail):
            respond_json_deferred(str(fail), request, status_code=404)

        data = ["mail", "mixnet_status", userid, address]
        d = _conn.sendMsg(*data)
        d.addCallback(callback)
        d.addErrback(err)

        return server.NOT_DONE_YET