Mercurial > libervia-backend
annotate sat/plugins/plugin_xep_0384.py @ 2787:298408833ec2
memory (sqlite): optimizations
indexes were missing on foreign keys for "message", "subject" and "thread" tables, seriously impacting performances.
In addition to those indexes, two indexes have been added to speed ordering by timestamp on "history", and one on "files" table.
history.rowid is not used anymore as there is an index on (profile_id, received_timestamp) which will speed up the query.
Primary keys order has been changed to use automatic index in most common cases (filtering by profile_id then namespace).
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 19 Jan 2019 22:49:32 +0100 |
parents | 003b8b4b56a7 |
children | 0ab62dd3cf05 |
rev | line source |
---|---|
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python2 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # SAT plugin for OMEMO encryption |
2771 | 5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # (at your option) any later version. |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
20 from sat.core.i18n import _, D_ |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 from sat.core.constants import Const as C |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from sat.core.log import getLogger |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 from sat.core import exceptions |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
24 from omemo import exceptions as omemo_excpt |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 from twisted.internet import defer |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 from twisted.words.xish import domish |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 from twisted.words.protocols.jabber import jid |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 from twisted.words.protocols.jabber import error |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 from sat.memory import persistent |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 from functools import partial |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
31 from sat.tools import xml_tools |
2654
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
32 import logging |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 import random |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 import base64 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 try: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 import omemo |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 from omemo.extendedpublicbundle import ExtendedPublicBundle |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
38 from omemo_backend_signal import BACKEND as omemo_backend |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
39 # from omemo import wireformat |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
40 except ImportError as e: |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 raise exceptions.MissingModule( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 u'Missing module omemo, please download/install it. You can use ' |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 u'"pip install omemo"' |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 ) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 log = getLogger(__name__) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 PLUGIN_INFO = { |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 C.PI_NAME: u"OMEMO", |
2750
ae495f27b316
plugin XEP-0380: Explicit Message Encryption implementation
Goffi <goffi@goffi.org>
parents:
2745
diff
changeset
|
50 C.PI_IMPORT_NAME: u"XEP-0384", |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 C.PI_TYPE: u"SEC", |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 C.PI_PROTOCOLS: [u"XEP-0384"], |
2662
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
53 C.PI_DEPENDENCIES: [u"XEP-0163", u"XEP-0280", u"XEP-0334", u"XEP-0060"], |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 C.PI_MAIN: u"OMEMO", |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 C.PI_HANDLER: u"no", |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 C.PI_DESCRIPTION: _(u"""Implementation of OMEMO"""), |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 } |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
59 OMEMO_MIN_VER = (0, 10, 3) |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 NS_OMEMO = "eu.siacs.conversations.axolotl" |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 NS_OMEMO_DEVICES = NS_OMEMO + ".devicelist" |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 NS_OMEMO_BUNDLE = NS_OMEMO + ".bundles:{device_id}" |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 KEY_STATE = "STATE" |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 KEY_DEVICE_ID = "DEVICE_ID" |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 KEY_SESSION = "SESSION" |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
66 KEY_TRUST = "TRUST" |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 KEY_ACTIVE_DEVICES = "DEVICES" |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
68 KEY_INACTIVE_DEVICES = "INACTIVE_DEVICES" |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
69 KEY_ALL_JIDS = "ALL_JIDS" |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
72 # we want to manage log emitted by omemo module ourselves |
2654
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
73 |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
74 class SatHandler(logging.Handler): |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
75 |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
76 def emit(self, record): |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
77 log.log(record.levelname, record.getMessage()) |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
78 |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
79 @staticmethod |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
80 def install(): |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
81 omemo_sm_logger = logging.getLogger("omemo.SessionManager") |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
82 omemo_sm_logger.propagate = False |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
83 omemo_sm_logger.addHandler(SatHandler()) |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
84 |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
85 |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
86 SatHandler.install() |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
87 |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
88 |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 def b64enc(data): |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
90 return base64.b64encode(bytes(bytearray(data))).decode("US-ASCII") |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
91 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
92 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
93 def promise2Deferred(promise_): |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
94 """Create a Deferred and fire it when promise is resolved |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
95 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
96 @param promise_(promise.Promise): promise to convert |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
97 @return (defer.Deferred): deferred instance linked to the promise |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
98 """ |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
99 d = defer.Deferred() |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
100 promise_.then(d.callback, d.errback) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
101 return d |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 class OmemoStorage(omemo.Storage): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
106 def __init__(self, client, device_id, all_jids, persistent_dict): |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 """ |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 @param persistent_dict(persistent.LazyPersistentBinaryDict): object which will |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 store data in SàT database |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 """ |
2654
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
111 self.own_bare_jid_s = client.jid.userhost() |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
112 self.device_id = device_id |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
113 self.all_jids = all_jids |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 self.data = persistent_dict |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 @property |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 def is_async(self): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 return True |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
119 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
120 def setCb(self, deferred, callback): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
121 """Associate Deferred and callback |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
122 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
123 callback of omemo.Storage expect a boolean with success state then result |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 Deferred on the other hand use 2 methods for callback and errback |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 This method use partial to call callback with boolean then result when |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
126 Deferred is called |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 """ |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 deferred.addCallback(partial(callback, True)) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 deferred.addErrback(partial(callback, False)) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
131 def _checkJid(self, bare_jid): |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
132 """Check if jid is know, and store it if not |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
133 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
134 @param bare_jid(unicode): bare jid to check |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
135 @return (D): Deferred fired when jid is stored |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
136 """ |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
137 if bare_jid in self.all_jids: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
138 return defer.succeed(None) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
139 else: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
140 self.all_jids.add(bare_jid) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
141 d = self.data.force(KEY_ALL_JIDS, self.all_jids) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
142 return d |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
143 |
2654
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
144 def loadOwnData(self, callback): |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
145 callback(True, {'own_bare_jid': self.own_bare_jid_s, |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
146 'own_device_id': self.device_id}) |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
147 |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
148 def storeOwnData(self, callback, own_bare_jid, own_device_id): |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
149 if own_bare_jid != self.own_bare_jid_s or own_device_id != self.device_id: |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
150 raise exceptions.InternalError('bare jid or device id inconsistency!') |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
151 callback(True, None) |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
152 |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
153 def loadState(self, callback): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
154 d = self.data.get(KEY_STATE) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
155 self.setCb(d, callback) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
156 |
2654
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
157 def storeState(self, callback, state): |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
158 d = self.data.force(KEY_STATE, state) |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
159 self.setCb(d, callback) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
160 |
2654
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
161 def loadSession(self, callback, bare_jid, device_id): |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
162 key = u'\n'.join([KEY_SESSION, bare_jid, unicode(device_id)]) |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
163 d = self.data.get(key) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
164 self.setCb(d, callback) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
165 |
2654
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
166 def storeSession(self, callback, bare_jid, device_id, session): |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
167 key = u'\n'.join([KEY_SESSION, bare_jid, unicode(device_id)]) |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
168 d = self.data.force(key, session) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
169 self.setCb(d, callback) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
170 |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
171 def deleteSession(self, callback, bare_jid, device_id): |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
172 key = u'\n'.join([KEY_SESSION, bare_jid, unicode(device_id)]) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
173 d = self.data.remove(key) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
174 self.setCb(d, callback) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
175 |
2654
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
176 def loadActiveDevices(self, callback, bare_jid): |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
177 key = u'\n'.join([KEY_ACTIVE_DEVICES, bare_jid]) |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
178 d = self.data.get(key, {}) |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
179 if callback is not None: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
180 self.setCb(d, callback) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
181 return d |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
182 |
2654
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
183 def loadInactiveDevices(self, callback, bare_jid): |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
184 key = u'\n'.join([KEY_INACTIVE_DEVICES, bare_jid]) |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
185 d = self.data.get(key, {}) |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
186 if callback is not None: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
187 self.setCb(d, callback) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
188 return d |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
189 |
2654
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
190 def storeActiveDevices(self, callback, bare_jid, devices): |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
191 key = u'\n'.join([KEY_ACTIVE_DEVICES, bare_jid]) |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
192 d = self._checkJid(bare_jid) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
193 d.addCallback(lambda _: self.data.force(key, devices)) |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
194 self.setCb(d, callback) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
195 |
2654
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
196 def storeInactiveDevices(self, callback, bare_jid, devices): |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
197 key = u'\n'.join([KEY_INACTIVE_DEVICES, bare_jid]) |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
198 d = self._checkJid(bare_jid) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
199 d.addCallback(lambda _: self.data.force(key, devices)) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
200 self.setCb(d, callback) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
201 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
202 def storeTrust(self, callback, bare_jid, device_id, trust): |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
203 key = u'\n'.join([KEY_TRUST, bare_jid, unicode(device_id)]) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
204 d = self.data.force(key, trust) |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
205 self.setCb(d, callback) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
206 |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
207 def loadTrust(self, callback, bare_jid, device_id): |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
208 key = u'\n'.join([KEY_TRUST, bare_jid, unicode(device_id)]) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
209 d = self.data.get(key) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
210 if callback is not None: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
211 self.setCb(d, callback) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
212 return d |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
213 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
214 def listJIDs(self, callback): |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
215 d = defer.succeed(self.all_jids) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
216 if callback is not None: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
217 self.setCb(d, callback) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
218 return d |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
219 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
220 def _deleteJID_logResults(self, results): |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
221 failed = [success for success, __ in results if not success] |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
222 if failed: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
223 log.warning( |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
224 u"delete JID failed for {failed_count} on {total_count} operations" |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
225 .format(failed_count=len(failed), total_count=len(results))) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
226 else: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
227 log.info( |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
228 u"Delete JID operation succeed ({total_count} operations)." |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
229 .format(total_count=len(results))) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
230 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
231 def _deleteJID_gotDevices(self, results, bare_jid): |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
232 assert len(results) == 2 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
233 active_success, active_devices = results[0] |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
234 inactive_success, inactive_devices = results[0] |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
235 d_list = [] |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
236 for success, devices in results: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
237 if not success: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
238 log.warning("Can't retrieve devices for {bare_jid}: {reason}" |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
239 .format(bare_jid=bare_jid, reason=active_devices)) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
240 else: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
241 for device_id in devices: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
242 for key in (KEY_SESSION, KEY_TRUST): |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
243 k = u'\n'.join([key, bare_jid, unicode(device_id)]) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
244 d_list.append(self.data.remove(k)) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
245 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
246 d_list.append(self.data.remove(KEY_ACTIVE_DEVICES, bare_jid)) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
247 d_list.append(self.data.remove(KEY_INACTIVE_DEVICES, bare_jid)) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
248 d_list.append(lambda __: self.all_jids.discard(bare_jid)) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
249 # FIXME: there is a risk of race condition here, |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
250 # if self.all_jids is modified between discard and force) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
251 d_list.append(lambda __: self.data.force(KEY_ALL_JIDS, self.all_jids)) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
252 d = defer.DeferredList(d_list) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
253 d.addCallback(self._deleteJID_logResults) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
254 return d |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
255 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
256 def deleteJID(self, callback, bare_jid): |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
257 """Retrieve all (in)actives of bare_jid, and delete all related keys""" |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
258 d_list = [] |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
259 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
260 key = u'\n'.join([KEY_ACTIVE_DEVICES, bare_jid]) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
261 d_list.append(self.data.get(key, [])) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
262 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
263 key = u'\n'.join([KEY_INACTIVE_DEVICES, bare_jid]) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
264 d_inactive = self.data.get(key, {}) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
265 # inactive devices are returned as a dict mapping from devices_id to timestamp |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
266 # but we only need devices ids |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
267 d_inactive.addCallback(lambda devices: [k for k, __ in devices]) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
268 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
269 d_list.append(d_inactive) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
270 d = defer.DeferredList(d_list) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
271 d.addCallback(self._deleteJID_gotDevices, bare_jid) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
272 if callback is not None: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
273 self.setCb(d, callback) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
274 return d |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
275 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
276 |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
277 class SatOTPKPolicy(omemo.DefaultOTPKPolicy): |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
278 pass |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
279 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
280 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
281 class OmemoSession(object): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
282 """Wrapper to use omemo.OmemoSession with Deferred""" |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
283 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
284 def __init__(self, session): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
285 self._session = session |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
286 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
287 @property |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
288 def republish_bundle(self): |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
289 return self._session.republish_bundle |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
290 |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
291 @property |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
292 def public_bundle(self): |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
293 return self._session.public_bundle |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
294 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
295 @classmethod |
2654
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
296 def create(cls, client, storage, my_device_id = None): |
2744
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
297 omemo_session_p = omemo.SessionManager.create( |
2654
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
298 storage, |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
299 SatOTPKPolicy, |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
300 omemo_backend, |
2654
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
301 client.jid.userhost(), |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
302 my_device_id) |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
303 d = promise2Deferred(omemo_session_p) |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
304 d.addCallback(lambda session: cls(session)) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
305 return d |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
306 |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
307 def newDeviceList(self, jid, devices): |
2654
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
308 jid = jid.userhost() |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
309 new_device_p = self._session.newDeviceList(jid, devices) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
310 return promise2Deferred(new_device_p) |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
311 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
312 def getDevices(self, bare_jid=None): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
313 get_devices_p = self._session.getDevices(bare_jid=bare_jid) |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
314 return promise2Deferred(get_devices_p) |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
315 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
316 def buildSession(self, bare_jid, device, bundle): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
317 bare_jid = bare_jid.userhost() |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
318 build_session_p = self._session.buildSession(bare_jid, device, bundle) |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
319 return promise2Deferred(build_session_p) |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
320 |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
321 def encryptMessage(self, bare_jids, message, bundles=None, expect_problems=None): |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
322 """Encrypt a message |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
323 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
324 @param bare_jids(iterable[jid.JID]): destinees of the message |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
325 @param message(unicode): message to encode |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
326 @param bundles(dict[jid.JID, dict[int, ExtendedPublicBundle]): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
327 entities => devices => bundles map |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
328 @return D(dict): encryption data |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
329 """ |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
330 if isinstance(bare_jids, jid.JID): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
331 bare_jids = bare_jids.userhost() |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
332 else: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
333 bare_jids = [e.userhost() for e in bare_jids] |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
334 if bundles is not None: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
335 bundles = {e.userhost(): v for e, v in bundles.iteritems()} |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
336 encrypt_mess_p = self._session.encryptMessage( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
337 bare_jids=bare_jids, |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
338 plaintext=message.encode('utf-8'), |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
339 bundles=bundles, |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
340 expect_problems=expect_problems) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
341 return promise2Deferred(encrypt_mess_p) |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
342 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
343 def decryptMessage(self, bare_jid, device, iv, message, is_pre_key_message, |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
344 ciphertext, additional_information=None, allow_untrusted=False): |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
345 bare_jid = bare_jid.userhost() |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
346 decrypt_mess_p = self._session.decryptMessage( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
347 bare_jid=bare_jid, |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
348 device=device, |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
349 iv=iv, |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
350 message=message, |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
351 is_pre_key_message=is_pre_key_message, |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
352 ciphertext=ciphertext, |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
353 additional_information=additional_information, |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
354 allow_untrusted=allow_untrusted |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
355 ) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
356 return promise2Deferred(decrypt_mess_p) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
357 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
358 def trust(self, bare_jid, device, key): |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
359 bare_jid = bare_jid.userhost() |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
360 trust_p = self._session.trust( |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
361 bare_jid=bare_jid, |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
362 device=device, |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
363 key=key) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
364 return promise2Deferred(trust_p) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
365 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
366 def distrust(self, bare_jid, device, key): |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
367 bare_jid = bare_jid.userhost() |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
368 distrust_p = self._session.distrust( |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
369 bare_jid=bare_jid, |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
370 device=device, |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
371 key=key) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
372 return promise2Deferred(distrust_p) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
373 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
374 def getTrustForJID(self, bare_jid): |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
375 bare_jid = bare_jid.userhost() |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
376 get_trust_p = self._session.getTrustForJID(bare_jid=bare_jid) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
377 return promise2Deferred(get_trust_p) |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
378 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
379 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
380 class OMEMO(object): |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
381 |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
382 def __init__(self, host): |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
383 log.info(_(u"OMEMO plugin initialization (omemo module v{version})").format( |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
384 version=omemo.__version__)) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
385 version = tuple(map(int, omemo.__version__.split(u'.')[:3])) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
386 if version < OMEMO_MIN_VER: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
387 log.warning(_( |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
388 u"Your version of omemo module is too old: {v[0]}.{v[1]}.{v[2]} is " |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
389 u"minimum required), please update.").format(v=OMEMO_MIN_VER)) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
390 raise exceptions.CancelError("module is too old") |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
391 self.host = host |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
392 self._p_hints = host.plugins[u"XEP-0334"] |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
393 self._p_carbons = host.plugins[u"XEP-0280"] |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
394 self._p = host.plugins[u"XEP-0060"] |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
395 host.trigger.add("MessageReceived", self._messageReceivedTrigger, priority=100050) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
396 host.trigger.add("sendMessageData", self._sendMessageDataTrigger) |
2654
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
397 self.host.registerEncryptionPlugin(self, u"OMEMO", NS_OMEMO, 100) |
2662
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
398 pep = host.plugins['XEP-0163'] |
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
399 pep.addPEPEvent("OMEMO_DEVICES", NS_OMEMO_DEVICES, self.onNewDevices) |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
400 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
401 @defer.inlineCallbacks |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
402 def trustUICb(self, xmlui_data, trust_data, expect_problems=None, |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
403 profile=C.PROF_KEY_NONE): |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
404 if C.bool(xmlui_data.get('cancelled', 'false')): |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
405 defer.returnValue({}) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
406 client = self.host.getClient(profile) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
407 session = client._xep_0384_session |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
408 answer = xml_tools.XMLUIResult2DataFormResult(xmlui_data) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
409 for key, value in answer.iteritems(): |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
410 if key.startswith(u'trust_'): |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
411 trust_id = key[6:] |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
412 else: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
413 continue |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
414 data = trust_data[trust_id] |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
415 trust = C.bool(value) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
416 if trust: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
417 yield session.trust(data[u"jid"], |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
418 data[u"device"], |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
419 data[u"ik"]) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
420 else: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
421 yield session.distrust(data[u"jid"], |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
422 data[u"device"], |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
423 data[u"ik"]) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
424 if expect_problems is not None: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
425 expect_problems.setdefault(data.bare_jid, set()).add(data.device) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
426 defer.returnValue({}) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
427 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
428 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
429 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
430 @defer.inlineCallbacks |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
431 def getTrustUI(self, client, entity_jid=None, trust_data=None, submit_id=None): |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
432 """Generate a XMLUI to manage trust |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
433 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
434 @param entity_jid(None, jid.JID): jid of entity to manage |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
435 None to use trust_data |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
436 @param trust_data(None, dict): devices data: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
437 None to use entity_jid |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
438 else a dict mapping from trust ids (unicode) to devices data, |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
439 where a device data must have the following keys: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
440 - jid(jid.JID): bare jid of the device owner |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
441 - device(int): device id |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
442 - ik(bytes): identity key |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
443 and may have the following key: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
444 - trusted(bool): True if device is trusted |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
445 @param submit_id(None, unicode): submit_id to use |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
446 if None set UI callback to trustUICb |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
447 @return D(xmlui): trust management form |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
448 """ |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
449 # we need entity_jid xor trust_data |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
450 assert entity_jid and not trust_data or not entity_jid and trust_data |
2744
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
451 if entity_jid and entity_jid.resource: |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
452 raise ValueError(u"A bare jid is expected") |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
453 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
454 session = client._xep_0384_session |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
455 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
456 if trust_data is None: |
2744
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
457 cache = client._xep_0384_cache.setdefault(entity_jid, {}) |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
458 trust_data = {} |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
459 trust_session_data = yield session.getTrustForJID(entity_jid) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
460 bare_jid_s = entity_jid.userhost() |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
461 for device_id, trust_info in trust_session_data['active'].iteritems(): |
2744
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
462 if trust_info is None: |
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
463 # device has never been (un)trusted, we have to retrieve its |
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
464 # fingerprint (i.e. identity key or "ik") through public bundle |
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
465 if device_id not in cache: |
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
466 bundles, missing = yield self.getBundles(client, |
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
467 entity_jid, |
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
468 [device_id]) |
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
469 if device_id not in bundles: |
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
470 log.warning(_( |
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
471 u"Can't find bundle for device {device_id} of user " |
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
472 u"{bare_jid}, ignoring").format(device_id=device_id, |
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
473 bare_jid=bare_jid_s)) |
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
474 continue |
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
475 cache[device_id] = bundles[device_id] |
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
476 # TODO: replace False below by None when undecided |
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
477 # trusts are handled |
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
478 trust_info = { |
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
479 u"key": cache[device_id].ik, |
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
480 u"trusted": False |
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
481 } |
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
482 |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
483 ik = trust_info["key"] |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
484 trust_id = unicode(hash((bare_jid_s, device_id, ik))) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
485 trust_data[trust_id] = { |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
486 u"jid": entity_jid, |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
487 u"device": device_id, |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
488 u"ik": ik, |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
489 u"trusted": trust_info[u"trusted"], |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
490 } |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
491 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
492 if submit_id is None: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
493 submit_id = self.host.registerCallback(partial(self.trustUICb, |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
494 trust_data=trust_data), |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
495 with_data=True, |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
496 one_shot=True) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
497 xmlui = xml_tools.XMLUI( |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
498 panel_type = C.XMLUI_FORM, |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
499 title = D_(u"OMEMO trust management"), |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
500 submit_id = submit_id |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
501 ) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
502 xmlui.addText(D_( |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
503 u"This is OMEMO trusting system. You'll see below the devices of your " |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
504 u"contacts, and a checkbox to trust them or not. A trusted device " |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
505 u"can read your messages in plain text, so be sure to only validate " |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
506 u"devices that you are sure are belonging to your contact. It's better " |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
507 u"to do this when you are next to your contact and her/his device, so " |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
508 u"you can check the \"fingerprint\" (the number next to the device) " |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
509 u"yourself. Do *not* validate a device if the fingerprint is wrong!")) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
510 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
511 xmlui.changeContainer("label") |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
512 xmlui.addLabel(D_(u"This device ID")) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
513 xmlui.addText(unicode(client._xep_0384_device_id)) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
514 xmlui.addLabel(D_(u"This device fingerprint")) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
515 ik_hex = session.public_bundle.ik.encode('hex').upper() |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
516 fp_human = u' '.join([ik_hex[i:i+8] for i in range(0, len(ik_hex), 8)]) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
517 xmlui.addText(fp_human) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
518 xmlui.addEmpty() |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
519 xmlui.addEmpty() |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
520 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
521 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
522 for trust_id, data in trust_data.iteritems(): |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
523 xmlui.addLabel(D_(u"Contact")) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
524 xmlui.addJid(data[u'jid']) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
525 xmlui.addLabel(D_(u"Device ID")) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
526 xmlui.addText(unicode(data[u'device'])) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
527 xmlui.addLabel(D_(u"Fingerprint")) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
528 ik_hex = data[u'ik'].encode('hex').upper() |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
529 fp_human = u' '.join([ik_hex[i:i+8] for i in range(0, len(ik_hex), 8)]) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
530 xmlui.addText(fp_human) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
531 xmlui.addLabel(D_(u"Trust this device?")) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
532 xmlui.addBool(u"trust_{}".format(trust_id), |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
533 value=C.boolConst(data.get(u'trusted', False))) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
534 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
535 xmlui.addEmpty() |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
536 xmlui.addEmpty() |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
537 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
538 defer.returnValue(xmlui) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
539 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
540 @defer.inlineCallbacks |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
541 def _purgeOldData(self, client, persistent_dict): |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
542 # FIXME: temporary method to deal with data change in omemo module |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
543 # We remove the old data, which is acceptable as |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
544 # no release of SàT (beside alpha versions) has been done |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
545 # since this data has been used. |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
546 # /!\ this method must be removed before 0.7 release /!\ |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
547 log.warning(u"FIXME: Using temporary purgeOldData code, to be removed before 0.7 release.") |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
548 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
549 state = yield persistent_dict.get(KEY_STATE) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
550 if state and "version" not in state: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
551 log.info(u"Old data found, purging it") |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
552 self.host.memory.storage.delPrivateNamespace("XEP-0384", binary=True, |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
553 profile=client.profile) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
554 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
555 @defer.inlineCallbacks |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
556 def profileConnected(self, client): |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
557 client._xep_0384_ready = defer.Deferred() |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
558 # we first need to get devices ids (including our own) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
559 persistent_dict = persistent.LazyPersistentBinaryDict("XEP-0384", client.profile) |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
560 yield self._purgeOldData(client, persistent_dict) |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
561 # all known devices of profile |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
562 devices = yield self.getDevices(client) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
563 # and our own device id |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
564 device_id = yield persistent_dict.get(KEY_DEVICE_ID) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
565 if device_id is None: |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
566 log.info(_(u"We have no identity for this device yet, let's generate one")) |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
567 # we have a new device, we create device_id |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
568 device_id = random.randint(1, 2**31-1) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
569 # we check that it's really unique |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
570 while device_id in devices: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
571 device_id = random.randint(1, 2**31-1) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
572 # and we save it |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
573 persistent_dict[KEY_DEVICE_ID] = device_id |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
574 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
575 if device_id not in devices: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
576 devices.add(device_id) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
577 yield self.setDevices(client, devices) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
578 |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
579 all_jids = yield persistent_dict.get(KEY_ALL_JIDS, set()) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
580 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
581 omemo_storage = OmemoStorage(client, device_id, all_jids, persistent_dict) |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
582 omemo_session = yield OmemoSession.create(client, omemo_storage, device_id) |
2662
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
583 client._xep_0384_cache = {} |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
584 client._xep_0384_session = omemo_session |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
585 client._xep_0384_device_id = device_id |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
586 yield omemo_session.newDeviceList(client.jid, devices) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
587 if omemo_session.republish_bundle: |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
588 log.info(_(u"Saving public bundle for this device ({device_id})").format( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
589 device_id=device_id)) |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
590 yield self.setBundle(client, omemo_session.public_bundle, device_id) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
591 client._xep_0384_ready.callback(None) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
592 del client._xep_0384_ready |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
593 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
594 ## XMPP PEP nodes manipulation |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
595 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
596 # devices |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
597 |
2662
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
598 def parseDevices(self, items): |
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
599 """Parse devices found in items |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
600 |
2662
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
601 @param items(iterable[domish.Element]): items as retrieved by getItems |
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
602 @return set[int]: parsed devices |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
603 """ |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
604 devices = set() |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
605 if len(items) > 1: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
606 log.warning(_(u"OMEMO devices list is stored in more that one items, " |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
607 u"this is not expected")) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
608 if items: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
609 try: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
610 list_elt = next(items[0].elements(NS_OMEMO, 'list')) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
611 except StopIteration: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
612 log.warning(_(u"no list element found in OMEMO devices list")) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
613 return |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
614 for device_elt in list_elt.elements(NS_OMEMO, 'device'): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
615 try: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
616 device_id = int(device_elt['id']) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
617 except KeyError: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
618 log.warning(_(u'device element is missing "id" attribute: {elt}') |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
619 .format(elt=device_elt.toXml())) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
620 except ValueError: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
621 log.warning(_(u'invalid device id: {device_id}').format( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
622 device_id=device_elt['id'])) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
623 else: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
624 devices.add(device_id) |
2662
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
625 return devices |
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
626 |
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
627 @defer.inlineCallbacks |
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
628 def getDevices(self, client, entity_jid=None): |
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
629 """Retrieve list of registered OMEMO devices |
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
630 |
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
631 @param entity_jid(jid.JID, None): get devices from this entity |
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
632 None to get our own devices |
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
633 @return (set(int)): list of devices |
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
634 """ |
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
635 if entity_jid is not None: |
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
636 assert not entity_jid.resource |
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
637 try: |
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
638 items, metadata = yield self._p.getItems(client, entity_jid, NS_OMEMO_DEVICES) |
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
639 except error.StanzaError as e: |
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
640 if e.condition == 'item-not-found': |
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
641 log.info(_(u"there is no node to handle OMEMO devices")) |
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
642 defer.returnValue(set()) |
2745
3ee396b2ecf3
plugin XEP-0384: don't ignore StanzaError if it's not item-not-found
Goffi <goffi@goffi.org>
parents:
2744
diff
changeset
|
643 raise e |
2662
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
644 |
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
645 devices = self.parseDevices(items) |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
646 defer.returnValue(devices) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
647 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
648 def setDevicesEb(self, failure_): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
649 log.warning(_(u"Can't set devices: {reason}").format(reason=failure_)) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
650 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
651 def setDevices(self, client, devices): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
652 list_elt = domish.Element((NS_OMEMO, 'list')) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
653 for device in devices: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
654 device_elt = list_elt.addElement('device') |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
655 device_elt['id'] = unicode(device) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
656 d = self._p.sendItem( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
657 client, None, NS_OMEMO_DEVICES, list_elt, item_id=self._p.ID_SINGLETON) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
658 d.addErrback(self.setDevicesEb) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
659 return d |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
660 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
661 # bundles |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
662 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
663 @defer.inlineCallbacks |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
664 def getBundles(self, client, entity_jid, devices_ids): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
665 """Retrieve public bundles of an entity devices |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
666 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
667 @param entity_jid(jid.JID): bare jid of entity |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
668 @param devices_id(iterable[int]): ids of the devices bundles to retrieve |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
669 @return (tuple(dict[int, ExtendedPublicBundle], list(int))): |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
670 - bundles collection: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
671 * key is device_id |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
672 * value is parsed bundle |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
673 - set of bundles not found |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
674 """ |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
675 assert not entity_jid.resource |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
676 bundles = {} |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
677 missing = set() |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
678 for device_id in devices_ids: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
679 node = NS_OMEMO_BUNDLE.format(device_id=device_id) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
680 try: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
681 items, metadata = yield self._p.getItems(client, entity_jid, node) |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
682 except error.StanzaError as e: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
683 if e.condition == u"item-not-found": |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
684 log.warning(_(u"Bundle missing for device {device_id}") |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
685 .format(device_id=device_id)) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
686 missing.add(device_id) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
687 continue |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
688 else: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
689 log.warning(_(u"Can't get bundle for device {device_id}: {reason}") |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
690 .format(device_id=device_id, reason=e)) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
691 continue |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
692 if not items: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
693 log.warning(_(u"no item found in node {node}, can't get public bundle " |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
694 u"for device {device_id}").format(node=node, |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
695 device_id=device_id)) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
696 continue |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
697 if len(items) > 1: |
2654
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
698 log.warning(_(u"more than one item found in {node}, " |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
699 u"this is not expected").format(node=node)) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
700 item = items[0] |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
701 try: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
702 bundle_elt = next(item.elements(NS_OMEMO, 'bundle')) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
703 signedPreKeyPublic_elt = next(bundle_elt.elements( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
704 NS_OMEMO, 'signedPreKeyPublic')) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
705 signedPreKeySignature_elt = next(bundle_elt.elements( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
706 NS_OMEMO, 'signedPreKeySignature')) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
707 identityKey_elt = next(bundle_elt.elements( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
708 NS_OMEMO, 'identityKey')) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
709 prekeys_elt = next(bundle_elt.elements( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
710 NS_OMEMO, 'prekeys')) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
711 except StopIteration: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
712 log.warning(_(u"invalid bundle for device {device_id}, ignoring").format( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
713 device_id=device_id)) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
714 continue |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
715 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
716 try: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
717 spkPublic = base64.b64decode(unicode(signedPreKeyPublic_elt)) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
718 spkSignature = base64.b64decode( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
719 unicode(signedPreKeySignature_elt)) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
720 |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
721 ik = base64.b64decode(unicode(identityKey_elt)) |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
722 spk = { |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
723 "key": spkPublic, |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
724 "id": int(signedPreKeyPublic_elt['signedPreKeyId']) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
725 } |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
726 otpks = [] |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
727 for preKeyPublic_elt in prekeys_elt.elements(NS_OMEMO, 'preKeyPublic'): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
728 preKeyPublic = base64.b64decode(unicode(preKeyPublic_elt)) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
729 otpk = { |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
730 "key": preKeyPublic, |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
731 "id": int(preKeyPublic_elt['preKeyId']) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
732 } |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
733 otpks.append(otpk) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
734 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
735 except Exception as e: |
2654
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
736 log.warning(_(u"error while decoding key for device {device_id}: {msg}") |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
737 .format(device_id=device_id, msg=e)) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
738 continue |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
739 |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
740 bundles[device_id] = ExtendedPublicBundle.parse(omemo_backend, ik, spk, |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
741 spkSignature, otpks) |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
742 |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
743 defer.returnValue((bundles, missing)) |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
744 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
745 def setBundleEb(self, failure_): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
746 log.warning(_(u"Can't set bundle: {reason}").format(reason=failure_)) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
747 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
748 def setBundle(self, client, bundle, device_id): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
749 """Set public bundle for this device. |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
750 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
751 @param bundle(ExtendedPublicBundle): bundle to publish |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
752 """ |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
753 log.debug(_(u"updating bundle for {device_id}").format(device_id=device_id)) |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
754 bundle = bundle.serialize(omemo_backend) |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
755 bundle_elt = domish.Element((NS_OMEMO, 'bundle')) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
756 signedPreKeyPublic_elt = bundle_elt.addElement( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
757 "signedPreKeyPublic", |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
758 content=b64enc(bundle["spk"]['key'])) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
759 signedPreKeyPublic_elt['signedPreKeyId'] = unicode(bundle["spk"]['id']) |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
760 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
761 bundle_elt.addElement( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
762 "signedPreKeySignature", |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
763 content=b64enc(bundle["spk_signature"])) |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
764 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
765 bundle_elt.addElement( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
766 "identityKey", |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
767 content=b64enc(bundle["ik"])) |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
768 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
769 prekeys_elt = bundle_elt.addElement('prekeys') |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
770 for otpk in bundle["otpks"]: |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
771 preKeyPublic_elt = prekeys_elt.addElement( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
772 'preKeyPublic', |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
773 content=b64enc(otpk["key"])) |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
774 preKeyPublic_elt['preKeyId'] = unicode(otpk['id']) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
775 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
776 node = NS_OMEMO_BUNDLE.format(device_id=device_id) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
777 d = self._p.sendItem(client, None, node, bundle_elt, item_id=self._p.ID_SINGLETON) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
778 d.addErrback(self.setBundleEb) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
779 return d |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
780 |
2662
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
781 ## PEP node events callbacks |
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
782 |
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
783 @defer.inlineCallbacks |
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
784 def onNewDevices(self, itemsEvent, profile): |
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
785 client = self.host.getClient(profile) |
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
786 cache = client._xep_0384_cache |
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
787 omemo_session = client._xep_0384_session |
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
788 entity = itemsEvent.sender |
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
789 entity_cache = cache.setdefault(entity, {}) |
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
790 devices = self.parseDevices(itemsEvent.items) |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
791 omemo_session.newDeviceList(entity, devices) |
2662
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
792 missing_devices = devices.difference(entity_cache.keys()) |
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
793 if missing_devices: |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
794 bundles, bundles_not_found = yield self.getBundles( |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
795 client, entity, missing_devices) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
796 entity_cache.update(bundles) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
797 if bundles_not_found and entity == client.jid.userhostJID(): |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
798 # we have devices announced in our own public list |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
799 # with missing bundles |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
800 own_device = client._xep_0384_device_id |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
801 if own_device in bundles_not_found: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
802 log.warning(_(u"Our own device has no attached bundle, fixing it")) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
803 bundles_not_found.remove(own_device) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
804 yield self.setBundle(client, omemo_session.public_bundle, own_device) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
805 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
806 if bundles_not_found: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
807 # some announced devices have no bundle, we update our public |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
808 # list to remove missing devices. |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
809 log.warning(_( |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
810 u"Some devices have missing bundles, cleaning out public " |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
811 u"devices list")) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
812 existing_devices = devices - bundles_not_found |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
813 yield self.setDevices(client, existing_devices) |
2744
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
814 # we check that our device has not been removed from the list |
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
815 if entity == client.jid.userhostJID(): |
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
816 own_device = client._xep_0384_device_id |
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
817 if own_device not in devices: |
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
818 log.warning(_(u"Our own device is missing from devices list, fixing it")) |
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
819 devices.add(own_device) |
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
820 yield self.setDevices(client, devices) |
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
821 |
2662
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
822 |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
823 ## triggers |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
824 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
825 @defer.inlineCallbacks |
2757
1f612547fb2e
plugin XEP-0384: give feedback to user when trust must be handled before sending a message
Goffi <goffi@goffi.org>
parents:
2754
diff
changeset
|
826 def handleProblems(self, client, entity, bundles, problems): |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
827 """Try to solve problem found by EncryptMessage |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
828 |
2757
1f612547fb2e
plugin XEP-0384: give feedback to user when trust must be handled before sending a message
Goffi <goffi@goffi.org>
parents:
2754
diff
changeset
|
829 @param entity(jid.JID): bare jid of the destinee |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
830 @param bundles(dict): bundles data as used in EncryptMessage |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
831 already filled with known bundles, missing bundles |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
832 need to be added to it |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
833 @param problems(list): exceptions raised by EncryptMessage |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
834 @return (dict): expect_problems arguments, used in EncryptMessage |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
835 this dict will list devices where problems can be ignored |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
836 (those devices won't receive the encrypted data) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
837 """ |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
838 # FIXME: not all problems are handled yet |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
839 untrusted = {} |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
840 expect_problems = {} |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
841 for problem in problems: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
842 if isinstance(problem, omemo_excpt.UntrustedException): |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
843 untrusted[unicode(hash(problem))] = problem |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
844 elif isinstance(problem, omemo_excpt.NoEligibleDevicesException): |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
845 pass |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
846 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
847 if untrusted: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
848 trust_data = {} |
2744
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
849 for trust_id, data in untrusted.iteritems(): |
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
850 trust_data[trust_id] = { |
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
851 'jid': jid.JID(data.bare_jid), |
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
852 'device': data.device, |
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
853 'ik': data.ik} |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
854 |
2757
1f612547fb2e
plugin XEP-0384: give feedback to user when trust must be handled before sending a message
Goffi <goffi@goffi.org>
parents:
2754
diff
changeset
|
855 user_msg = D_(u"Not all destination devices are trusted, we can't encrypt " |
1f612547fb2e
plugin XEP-0384: give feedback to user when trust must be handled before sending a message
Goffi <goffi@goffi.org>
parents:
2754
diff
changeset
|
856 u"message in such a situation. Please indicate if you trust " |
1f612547fb2e
plugin XEP-0384: give feedback to user when trust must be handled before sending a message
Goffi <goffi@goffi.org>
parents:
2754
diff
changeset
|
857 u"those devices or not in the trust manager before we can " |
1f612547fb2e
plugin XEP-0384: give feedback to user when trust must be handled before sending a message
Goffi <goffi@goffi.org>
parents:
2754
diff
changeset
|
858 "send this message") |
1f612547fb2e
plugin XEP-0384: give feedback to user when trust must be handled before sending a message
Goffi <goffi@goffi.org>
parents:
2754
diff
changeset
|
859 client.feedback(entity, user_msg) |
2744
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
860 xmlui = yield self.getTrustUI(client, trust_data=trust_data, submit_id=u"") |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
861 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
862 answer = yield xml_tools.deferXMLUI( |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
863 self.host, |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
864 xmlui, |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
865 action_extra={ |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
866 u"meta_encryption_trust": NS_OMEMO, |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
867 }, |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
868 profile=client.profile) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
869 yield self.trustUICb(answer, trust_data, expect_problems, client.profile) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
870 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
871 defer.returnValue(expect_problems) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
872 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
873 @defer.inlineCallbacks |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
874 def encryptMessage(self, client, entity_bare_jid, message): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
875 omemo_session = client._xep_0384_session |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
876 cache = client._xep_0384_cache |
2662
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
877 try: |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
878 bundles = {entity_bare_jid: cache[entity_bare_jid]} |
2662
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
879 except KeyError: |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
880 # No devices know for this entity, let try to find them. |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
881 # This can happen if the entity is not in our roster, or doesn't handle OMEMO |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
882 # or if we haven't received the devices from PEP yet. |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
883 try: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
884 devices = yield self.getDevices(client, entity_bare_jid) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
885 bundles, __ = yield self.getBundles(client, entity_bare_jid, devices) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
886 except Exception as e: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
887 raise exceptions.NotFound( |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
888 _(u"Can retrieve bundles for {entity}: {reason}" .format( |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
889 entity=entity_bare_jid.full(), reason=e))) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
890 else: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
891 cache[entity_bare_jid] = bundles |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
892 bundles = {entity_bare_jid: bundles} |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
893 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
894 own_jid = client.jid.userhostJID() |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
895 if entity_bare_jid != own_jid: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
896 # message will be copied to our devices, so we need to add our own bundles |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
897 bundles[own_jid] = cache[own_jid] |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
898 |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
899 try: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
900 # first try may fail, in case of e.g. trust issue or missing bundle |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
901 encrypted = yield omemo_session.encryptMessage( |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
902 entity_bare_jid, |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
903 message, |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
904 bundles) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
905 except omemo_excpt.EncryptionProblemsException as e: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
906 # we know the problem to solve, we can try to fix them |
2757
1f612547fb2e
plugin XEP-0384: give feedback to user when trust must be handled before sending a message
Goffi <goffi@goffi.org>
parents:
2754
diff
changeset
|
907 expect_problems = yield self.handleProblems(client, entity_bare_jid, bundles, e.problems) |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
908 # and try an encryption again. |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
909 try: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
910 encrypted = yield omemo_session.encryptMessage( |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
911 entity_bare_jid, |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
912 message, |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
913 bundles, |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
914 expect_problems = expect_problems) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
915 except omemo_excpt.EncryptionProblemsException as e: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
916 log.warning( |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
917 _(u"Can't encrypt message for {entity}: {reason}".format( |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
918 entity=entity_bare_jid.full(), reason=e))) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
919 raise e |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
920 |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
921 defer.returnValue(encrypted) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
922 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
923 @defer.inlineCallbacks |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
924 def _messageReceivedTrigger(self, client, message_elt, post_treat): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
925 if message_elt.getAttribute("type") == C.MESS_TYPE_GROUPCHAT: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
926 defer.returnValue(True) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
927 try: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
928 encrypted_elt = next(message_elt.elements(NS_OMEMO, u"encrypted")) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
929 except StopIteration: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
930 # no OMEMO message here |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
931 defer.returnValue(True) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
932 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
933 # we have an encrypted message let's decrypt it |
2654
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
934 from_jid = jid.JID(message_elt['from']) |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
935 try: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
936 omemo_session = client._xep_0384_session |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
937 except AttributeError: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
938 # on startup, message can ve received before session actually exists |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
939 # so we need to synchronise here |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
940 yield client._xep_0384_ready |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
941 omemo_session = client._xep_0384_session |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
942 |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
943 device_id = client._xep_0384_device_id |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
944 try: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
945 header_elt = next(encrypted_elt.elements(NS_OMEMO, u'header')) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
946 iv_elt = next(header_elt.elements(NS_OMEMO, u'iv')) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
947 except StopIteration: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
948 log.warning(_(u"Invalid OMEMO encrypted stanza, ignoring: {xml}") |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
949 .format(xml=message_elt.toXml())) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
950 defer.returnValue(False) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
951 try: |
2654
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
952 s_device_id = header_elt['sid'] |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
953 except KeyError: |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
954 log.warning(_(u"Invalid OMEMO encrypted stanza, missing sender device ID, " |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
955 u"ignoring: {xml}") |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
956 .format(xml=message_elt.toXml())) |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
957 defer.returnValue(False) |
e7bfbded652a
plugin XEP-0384, install: adapted plugin to omemo module changes + added omemo module to dependencies:
Goffi <goffi@goffi.org>
parents:
2648
diff
changeset
|
958 try: |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
959 key_elt = next((e for e in header_elt.elements(NS_OMEMO, u'key') |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
960 if int(e[u'rid']) == device_id)) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
961 except StopIteration: |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
962 log.warning(_(u"This OMEMO encrypted stanza has not been encrypted " |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
963 u"for our device (device_id: {device_id}, fingerprint: " |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
964 u"{fingerprint}): {xml}").format( |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
965 device_id=device_id, |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
966 fingerprint=omemo_session.public_bundle.ik.encode('hex'), |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
967 xml=encrypted_elt.toXml())) |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
968 defer.returnValue(False) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
969 except ValueError as e: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
970 log.warning(_(u"Invalid recipient ID: {msg}".format(msg=e))) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
971 defer.returnValue(False) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
972 is_pre_key = C.bool(key_elt.getAttribute('prekey', 'false')) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
973 payload_elt = next(encrypted_elt.elements(NS_OMEMO, u'payload'), None) |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
974 additional_information = { |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
975 "from_storage": bool(message_elt.delay) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
976 } |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
977 |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
978 kwargs = { |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
979 "bare_jid": from_jid.userhostJID(), |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
980 "device": s_device_id, |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
981 "iv": base64.b64decode(bytes(iv_elt)), |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
982 "message": base64.b64decode(bytes(key_elt)), |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
983 "is_pre_key_message": is_pre_key, |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
984 "ciphertext": base64.b64decode(bytes(payload_elt)) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
985 if payload_elt is not None else None, |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
986 "additional_information": additional_information, |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
987 } |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
988 try: |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
989 try: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
990 plaintext = yield omemo_session.decryptMessage(**kwargs) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
991 except omemo_excpt.UntrustedException: |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
992 post_treat.addCallback(client.encryption.markAsUntrusted) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
993 kwargs['allow_untrusted'] = True |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
994 plaintext = yield omemo_session.decryptMessage(**kwargs) |
2744
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
995 else: |
e6716d90c2fe
plugin XEP-0384: various bug fixes:
Goffi <goffi@goffi.org>
parents:
2738
diff
changeset
|
996 post_treat.addCallback(client.encryption.markAsTrusted) |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
997 except Exception as e: |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
998 log.warning(_(u"Can't decrypt message: {reason}\n{xml}").format( |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
999 reason=e, xml=message_elt.toXml())) |
2754
3bea6b5ae972
plugin XEP-0380, XEP-0384: use C.EXTRA_INFO_DECR_ERR as info sub-type when a message can't be decrypted.
Goffi <goffi@goffi.org>
parents:
2750
diff
changeset
|
1000 user_msg = (D_(u"An OMEMO message from {sender} can't be decrypted: {reason}") |
3bea6b5ae972
plugin XEP-0380, XEP-0384: use C.EXTRA_INFO_DECR_ERR as info sub-type when a message can't be decrypted.
Goffi <goffi@goffi.org>
parents:
2750
diff
changeset
|
1001 .format(sender=from_jid.full(), reason=e)) |
3bea6b5ae972
plugin XEP-0380, XEP-0384: use C.EXTRA_INFO_DECR_ERR as info sub-type when a message can't be decrypted.
Goffi <goffi@goffi.org>
parents:
2750
diff
changeset
|
1002 extra = {C.MESS_EXTRA_INFO: C.EXTRA_INFO_DECR_ERR} |
3bea6b5ae972
plugin XEP-0380, XEP-0384: use C.EXTRA_INFO_DECR_ERR as info sub-type when a message can't be decrypted.
Goffi <goffi@goffi.org>
parents:
2750
diff
changeset
|
1003 client.feedback(from_jid, user_msg, extra) |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1004 defer.returnValue(False) |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
1005 if omemo_session.republish_bundle: |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1006 # we don't wait for the Deferred (i.e. no yield) on purpose |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1007 # there is no need to block the whole message workflow while |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1008 # updating the bundle |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
1009 self.setBundle(client, omemo_session.public_bundle, device_id) |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1010 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1011 message_elt.children.remove(encrypted_elt) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1012 if plaintext: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1013 message_elt.addElement("body", content=plaintext.decode('utf-8')) |
2662
0bef44f8e8ca
plugin XEP-0384: PEP handling + mark as encrypted:
Goffi <goffi@goffi.org>
parents:
2654
diff
changeset
|
1014 post_treat.addCallback(client.encryption.markAsEncrypted) |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1015 defer.returnValue(True) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1016 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1017 @defer.inlineCallbacks |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1018 def _sendMessageDataTrigger(self, client, mess_data): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1019 encryption = mess_data.get(C.MESS_KEY_ENCRYPTION) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1020 if encryption is None or encryption['plugin'].namespace != NS_OMEMO: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1021 return |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1022 message_elt = mess_data["xml"] |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1023 to_jid = mess_data["to"].userhostJID() |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1024 log.debug(u"encrypting message") |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1025 body = None |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1026 for child in list(message_elt.children): |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1027 if child.name == "body": |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1028 # we remove all unencrypted body, |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1029 # and will only encrypt the first one |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1030 if body is None: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1031 body = child |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1032 message_elt.children.remove(child) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1033 elif child.name == "html": |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1034 # we don't want any XHTML-IM element |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1035 message_elt.children.remove(child) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1036 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1037 if body is None: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1038 log.warning(u"No message found") |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1039 return |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1040 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1041 encryption_data = yield self.encryptMessage(client, to_jid, unicode(body)) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1042 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1043 encrypted_elt = message_elt.addElement((NS_OMEMO, 'encrypted')) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1044 header_elt = encrypted_elt.addElement('header') |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1045 header_elt['sid'] = unicode(encryption_data['sid']) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1046 bare_jid_s = to_jid.userhost() |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1047 |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
1048 for rid, data in encryption_data['keys'][bare_jid_s].iteritems(): |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1049 key_elt = header_elt.addElement( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1050 'key', |
2738
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
1051 content=b64enc(data['data'])) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
1052 key_elt['rid'] = unicode(rid) |
eb58f26ed236
plugin XEP-0384: update to last python-omemo + trust management:
Goffi <goffi@goffi.org>
parents:
2662
diff
changeset
|
1053 if data['pre_key']: |
2648
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1054 key_elt['prekey'] = 'true' |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1055 |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1056 header_elt.addElement( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1057 'iv', |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1058 content=b64enc(encryption_data['iv'])) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1059 try: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1060 encrypted_elt.addElement( |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1061 'payload', |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1062 content=b64enc(encryption_data['payload'])) |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1063 except KeyError: |
0f76813afc57
plugin XEP-0384: OMEMO implementation first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1064 pass |