annotate src/browser/sat_browser/otrjs_wrapper.py @ 546:ff03fdb5215d SàT v0.5.0

SàT 0.5.0 released
author Goffi <goffi@goffi.org>
date Tue, 09 Sep 2014 08:28:13 +0200
parents cd492c18b366
children cd56f2b0e85b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
522
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
1 #!/usr/bin/python
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
3
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
4 # Libervia wrapper for otr.js
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Jérôme Poisson (goffi@goffi.org)
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
6 # Copyright (C) 2013, 2014 Adrien Cossa (souliane@mailoo.org)
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
7
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
8 # This program is free software: you can redistribute it and/or modify
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
9 # it under the terms of the GNU Affero General Public License as published by
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
10 # the Free Software Foundation, either version 3 of the License, or
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
11 # (at your option) any later version.
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
12
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
13 # This program is distributed in the hope that it will be useful,
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
16 # GNU Affero General Public License for more details.
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
17
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
18 # You should have received a copy of the GNU Affero General Public License
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
20
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
21 """This file is a wrapper for otr.js. It partially reproduces the usage
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
22 (modules, classes and attributes names) and behavior of potr, so you
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
23 can easily adapt some code based on potr to Pyjamas applications.
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
24
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
25 potr is released under the GNU LESSER GENERAL PUBLIC LICENSE Version 3
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
26 - https://github.com/python-otr/pure-python-otr/blob/master/LICENSE
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
27
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
28 otr.js is released under the Mozilla Public Licence Version 2.0
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
29 - https://github.com/arlolra/otr/blob/master/license
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
30 """
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
31
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
32 from __pyjamas__ import JS
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
33
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
34 # should you re-use this class outside SàT, you can import __pyjamas__.console as log instead
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
35 from sat.core.log import getLogger
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
36 log = getLogger(__name__)
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
37
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
38
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
39 # XXX: pyjamas can't probably import more than one JS file, it messes the order.
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
40 # XXX: pyjamas needs the file to be in the compilation directory - no submodule.
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
41 # XXX: pyjamas needs the imported file to end with a empty line or semi-column.
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
42 # FIXME: fix these bugs upstream in Pyjamas
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
43 import otr.min.js
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
44
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
45
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
46 class context(object):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
47
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
48 # Pre-declare these attributes to avoid the pylint "undefined variable" errors
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
49 STATUS_SEND_QUERY = None
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
50 STATUS_AKE_INIT = None
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
51 STATUS_AKE_SUCCESS = None
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
52 STATUS_END_OTR = None
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
53 STATE_PLAINTEXT = None
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
54 STATE_ENCRYPTED = None
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
55 STATE_FINISHED = None
537
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
56 OTR_TAG = None
522
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
57 OTR_VERSION_2 = None
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
58 OTR_VERSION_3 = None
537
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
59 WHITESPACE_TAG = None
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
60 WHITESPACE_TAG_V2 = None
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
61 WHITESPACE_TAG_V3 = None
522
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
62
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
63 JS("""
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
64 $cls_definition['STATUS_SEND_QUERY'] = OTR.CONST.STATUS_SEND_QUERY;
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
65 $cls_definition['STATUS_AKE_INIT'] = OTR.CONST.STATUS_AKE_INIT;
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
66 $cls_definition['STATUS_AKE_SUCCESS'] = OTR.CONST.STATUS_AKE_SUCCESS;
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
67 $cls_definition['STATUS_END_OTR'] = OTR.CONST.STATUS_END_OTR;
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
68 $cls_definition['STATE_PLAINTEXT'] = OTR.CONST.MSGSTATE_PLAINTEXT;
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
69 $cls_definition['STATE_ENCRYPTED'] = OTR.CONST.MSGSTATE_ENCRYPTED;
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
70 $cls_definition['STATE_FINISHED'] = OTR.CONST.MSGSTATE_FINISHED;
537
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
71 $cls_definition['OTR_TAG'] = OTR.CONST.OTR_TAG;
522
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
72 $cls_definition['OTR_VERSION_2'] = OTR.CONST.OTR_VERSION_2;
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
73 $cls_definition['OTR_VERSION_3'] = OTR.CONST.OTR_VERSION_3;
537
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
74 $cls_definition['WHITESPACE_TAG'] = OTR.CONST.WHITESPACE_TAG;
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
75 $cls_definition['WHITESPACE_TAG_V2'] = OTR.CONST.WHITESPACE_TAG_V2;
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
76 $cls_definition['WHITESPACE_TAG_V3'] = OTR.CONST.WHITESPACE_TAG_V3;
522
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
77 """)
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
78
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
79 class UnencryptedMessage(Exception):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
80 pass
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
81
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
82 class Context(object):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
83
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
84 def __init__(self, account, peername):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
85 self.user = account
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
86 self.peer = peername
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
87 self.trustName = self.peer
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
88 options = {'fragment_size': 140,
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
89 'send_interval': 200,
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
90 'priv': account.getPrivkey(), # this would generate the account key if it hasn't been done yet
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
91 'debug': False,
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
92 }
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
93 JS("""self.otr = new OTR(options);""")
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
94
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
95 for policy in ('ALLOW_V2', 'ALLOW_V3', 'REQUIRE_ENCRYPTION'):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
96 setattr(self.otr, policy, self.getPolicy(policy))
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
97
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
98 self.otr.on('ui', self.receiveMessageCb)
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
99 self.otr.on('io', self.sendMessageCb)
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
100 self.otr.on('error', self.messageErrorCb)
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
101 self.otr.on('status', lambda status: self.setStateCb(self.otr.msgstate, status))
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
102 self.otr.on('smp', self.smpAuthCb)
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
103
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
104 @property
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
105 def state(self):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
106 return self.otr.msgstate
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
107
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
108 @state.setter
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
109 def state(self, state):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
110 self.otr.msgstate = state
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
111
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
112 def getCurrentKey(self):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
113 return self.otr.their_priv_pk
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
114
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
115 def setTrust(self, fingerprint, trustLevel):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
116 self.user.setTrust(self.trustName, fingerprint, trustLevel)
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
117
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
118 def setCurrentTrust(self, trustLevel):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
119 self.setTrust(self.otr.their_priv_pk.fingerprint(), trustLevel)
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
120
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
121 def getTrust(self, fingerprint, default=None):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
122 return self.user.getTrust(self.trustName, fingerprint, default)
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
123
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
124 def getCurrentTrust(self):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
125 # XXX: the docstring of potr for the return value of this method is incorrect
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
126 if self.otr.their_priv_pk is None:
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
127 return None
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
128 return self.getTrust(self.otr.their_priv_pk.fingerprint(), None)
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
129
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
130 def getUsedVersion(self):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
131 """Return the otr version that is beeing used"""
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
132 # this method doesn't exist in potr, it has been added for convenience
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
133 try:
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
134 return self.otr.ake.otr_version
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
135 except AttributeError:
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
136 return None
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
137
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
138 def disconnect(self):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
139 self.otr.endOtr()
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
140
530
1735aaeac652 plugin OTR: forces FINISHED state if we are in ENCRYPTED state on contact disconnection
souliane <souliane@mailoo.org>
parents: 523
diff changeset
141 def finish(self):
1735aaeac652 plugin OTR: forces FINISHED state if we are in ENCRYPTED state on contact disconnection
souliane <souliane@mailoo.org>
parents: 523
diff changeset
142 """Finish the session - avoid to send any message and the user has to manually disconnect"""
532
1fa173107fde browser_side (wrapper otr.js): fixes the method to finish the conversation
souliane <souliane@mailoo.org>
parents: 530
diff changeset
143 # it means TLV of type 1 (two first bytes), message length 0 (2 last bytes)
1fa173107fde browser_side (wrapper otr.js): fixes the method to finish the conversation
souliane <souliane@mailoo.org>
parents: 530
diff changeset
144 self.otr.handleTLVs('\x00\x01\x00\x00')
530
1735aaeac652 plugin OTR: forces FINISHED state if we are in ENCRYPTED state on contact disconnection
souliane <souliane@mailoo.org>
parents: 523
diff changeset
145
522
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
146 def receiveMessage(self, msg):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
147 """Received a message, ask otr.js to (try to) decrypt it"""
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
148 self.otr.receiveMsg(msg)
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
149
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
150 def sendMessage(self, msg):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
151 """Ask otr.js to encrypt a message for sending"""
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
152 self.otr.sendMsg(msg)
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
153
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
154 def sendQueryMessage(self):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
155 """Start or refresh an encryption communication"""
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
156 # otr.js offers this method, with potr you have to build the query message yourself
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
157 self.otr.sendQueryMsg()
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
158
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
159 def inject(self, msg, appdata=None):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
160 return self.sendMessageCb(msg, appdata)
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
161
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
162 def getPolicy(self, key):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
163 raise NotImplementedError
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
164
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
165 def smpAuthSecret(self, secret, question=None):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
166 return self.otr.smpSecret(secret, question)
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
167
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
168 def smpAuthAbort(self, act=None):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
169 # XXX: dirty hack to let the triggered method know who aborted the
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
170 # authentication. We need it to display the proper feedback and,
523
5add182e7dd5 browser_side (plugin OTR): rename menus and replace 'buddy' with 'correspondent'
souliane <souliane@mailoo.org>
parents: 522
diff changeset
171 # if the correspondent aborted, set the conversation 'unverified'.
522
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
172 self.otr.sm.init()
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
173 JS("""self.otr.sm.sendMsg(OTR.HLP.packTLV(6, ''))""")
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
174 self.smpAuthCb('abort', '', act)
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
175
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
176 def sendMessageCb(self, msg, meta):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
177 """Actually send the message after it's been encrypted"""
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
178 raise NotImplementedError
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
179
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
180 def receiveMessageCb(self, msg, encrypted):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
181 """Display the message after it's been eventually decrypted"""
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
182 raise NotImplementedError
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
183
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
184 def messageErrorCb(self, error):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
185 """Message error callback"""
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
186 raise NotImplementedError
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
187
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
188 def setStateCb(self, newstate):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
189 raise NotImplementedError
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
190
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
191 def smpAuthCb(self, newstate):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
192 raise NotImplementedError
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
193
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
194 class Account(object):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
195
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
196 def __init__(self, host):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
197 self.host = host
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
198 self.privkey = None
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
199 self.trusts = {}
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
200
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
201 def getPrivkey(self):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
202 # the return value must have a method serializePrivateKey()
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
203 # if the key is not saved yet, call savePrivkey to generate it
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
204 if self.privkey is None:
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
205 self.privkey = self.loadPrivkey()
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
206 if self.privkey is None:
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
207 JS("""self.privkey = new DSA();""")
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
208 self.savePrivkey()
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
209 return self.privkey
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
210
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
211 def setTrust(self, key, fingerprint, trustLevel):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
212 if key not in self.trusts:
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
213 self.trusts[key] = {}
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
214 self.trusts[key][fingerprint] = trustLevel
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
215 self.saveTrusts()
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
216
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
217 def getTrust(self, key, fingerprint, default=None):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
218 if key not in self.trusts:
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
219 return default
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
220 return self.trusts[key].get(fingerprint, default)
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
221
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
222 def loadPrivkey(self):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
223 raise NotImplementedError
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
224
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
225 def savePrivkey(self):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
226 raise NotImplementedError
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
227
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
228 def saveTrusts(self):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
229 raise NotImplementedError
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
230
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
231
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
232 class crypt(object):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
233
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
234 class PK(object):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
235
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
236 def parsePrivateKey(self, key):
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
237 JS("""return DSA.parsePrivate(key);""")
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
238
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
239
537
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
240 class proto(object):
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
241
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
242 @classmethod
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
243 def checkForOTR(cls, body):
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
244 """Helper method to check if the message contains OTR starting tag or whitespace
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
245
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
246 @return:
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
247 - context.OTR_TAG if the message starts with it
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
248 - context.WHITESPACE_TAG if the message contains OTR whitespaces
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
249 - None otherwise
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
250 """
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
251 if body.startswith(context.OTR_TAG):
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
252 return context.OTR_TAG
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
253 index = body.find(context.WHITESPACE_TAG)
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
254 if index < 0:
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
255 return False
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
256 tags = [body[i:i + 8] for i in range(index, len(body), 8)]
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
257 if [True for tag in tags if tag in (context.WHITESPACE_TAG_V2, context.WHITESPACE_TAG_V3)]:
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
258 return context.WHITESPACE_TAG
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
259 return None
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
260
cd492c18b366 browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
souliane <souliane@mailoo.org>
parents: 532
diff changeset
261
522
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
262 # serialazePrivateKey is the method name in potr
0de69fec24e9 browser and server sides: OTR plugin, first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
263 JS("""DSA.serializePrivateKey = DSA.packPrivate;""")