comparison src/plugins/plugin_sec_otr.py @ 1136:ea2bbdf5b541

plugin OTR: added start/refresh and end session menus
author Goffi <goffi@goffi.org>
date Mon, 25 Aug 2014 21:32:23 +0200
parents 3158f9e08760
children 768f1f1ef12c
comparison
equal deleted inserted replaced
1135:3158f9e08760 1136:ea2bbdf5b541
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 # XXX: thanks to Darrik L Mazey for his documentation (https://blog.darmasoft.net/2013/06/30/using-pure-python-otr.html) 20 # XXX: thanks to Darrik L Mazey for his documentation (https://blog.darmasoft.net/2013/06/30/using-pure-python-otr.html)
21 # this implentation is based on it 21 # this implentation is based on it
22 22
23 from sat.core.i18n import _ 23 from sat.core.i18n import _, D_
24 from sat.core.constants import Const as C
24 from sat.core.log import getLogger 25 from sat.core.log import getLogger
25 from sat.core import exceptions 26 from sat.core import exceptions
26 log = getLogger(__name__) 27 log = getLogger(__name__)
27 from twisted.words.protocols.jabber import jid 28 from twisted.words.protocols.jabber import jid
28 from twisted.python import failure 29 from twisted.python import failure
30 import potr 31 import potr
31 from sat.memory import persistent 32 from sat.memory import persistent
32 33
33 NS_OTR = "otr_plugin" 34 NS_OTR = "otr_plugin"
34 PRIVATE_KEY = "PRIVATE KEY" 35 PRIVATE_KEY = "PRIVATE KEY"
36 MAIN_MENU = D_('OTR')
35 37
36 DEFAULT_POLICY_FLAGS = { 38 DEFAULT_POLICY_FLAGS = {
37 'ALLOW_V1':False, 39 'ALLOW_V1':False,
38 'ALLOW_V2':True, 40 'ALLOW_V2':True,
39 'REQUIRE_ENCRYPTION':True, 41 'REQUIRE_ENCRYPTION':True,
161 self._fixPotr() # FIXME: to be removed when potr will be fixed 163 self._fixPotr() # FIXME: to be removed when potr will be fixed
162 self.host = host 164 self.host = host
163 self.context_managers = {} 165 self.context_managers = {}
164 host.trigger.add("MessageReceived", self.MessageReceivedTrigger, priority=100000) 166 host.trigger.add("MessageReceived", self.MessageReceivedTrigger, priority=100000)
165 host.trigger.add("sendMessage", self.sendMessageTrigger, priority=100000) 167 host.trigger.add("sendMessage", self.sendMessageTrigger, priority=100000)
168 host.importMenu((MAIN_MENU, D_("Start/Refresh")), self._startRefresh, security_limit=0, help_string=D_("Start or refresh an OTR session"), type_=C.MENU_SINGLE)
169 host.importMenu((MAIN_MENU, D_("End session")), self._endSession, security_limit=0, help_string=D_("Finish an OTR session"), type_=C.MENU_SINGLE)
166 170
167 def _fixPotr(self): 171 def _fixPotr(self):
168 # FIXME: potr fix for bad unicode handling 172 # FIXME: potr fix for bad unicode handling
169 # this method monkeypatch it, must be removed when potr 173 # this method monkeypatch it, must be removed when potr
170 # is fixed 174 # is fixed
190 if encrypted_priv_key is not None: 194 if encrypted_priv_key is not None:
191 priv_key = yield self.host.memory.decryptValue(encrypted_priv_key, profile) 195 priv_key = yield self.host.memory.decryptValue(encrypted_priv_key, profile)
192 client.otr_priv_key = potr.crypt.PK.parsePrivateKey(priv_key)[0] 196 client.otr_priv_key = potr.crypt.PK.parsePrivateKey(priv_key)[0]
193 else: 197 else:
194 client.otr_priv_key = None 198 client.otr_priv_key = None
199
200 def _startRefresh(self, menu_data, profile):
201 """Start or refresh an OTR session
202
203 @param menu_data: %(menu_data)s
204 @param profile: %(doc_profile)s
205 """
206 try:
207 to_jid = jid.JID(menu_data['jid'])
208 if not to_jid.resource:
209 to_jid.resource = self.host.memory.getLastResource(to_jid, profile) # FIXME: temporary and unsecure, must be changed when frontends are refactored
210 except KeyError:
211 log.error(_("jid key is not present !"))
212 return defer.fail(exceptions.DataError)
213 otrctx = self.context_managers[profile].getContextForUser(to_jid)
214 query = otrctx.sendMessage(0, '?OTRv?')
215 otrctx.inject(query)
216 return {}
217
218 def _endSession(self, menu_data, profile):
219 """End an OTR session
220
221 @param menu_data: %(menu_data)s
222 @param profile: %(doc_profile)s
223 """
224 try:
225 to_jid = jid.JID(menu_data['jid'])
226 if not to_jid.resource:
227 to_jid.resource = self.host.memory.getLastResource(to_jid, profile) # FIXME: temporary and unsecure, must be changed when frontends are refactored
228 except KeyError:
229 log.error(_("jid key is not present !"))
230 return defer.fail(exceptions.DataError)
231 otrctx = self.context_managers[profile].getContextForUser(to_jid)
232 otrctx.disconnect()
233 return {}
195 234
196 def _receivedTreatment(self, data, profile): 235 def _receivedTreatment(self, data, profile):
197 from_jid = jid.JID(data['from']) 236 from_jid = jid.JID(data['from'])
198 log.debug(u"_receivedTreatment [from_jid = %s]" % from_jid) 237 log.debug(u"_receivedTreatment [from_jid = %s]" % from_jid)
199 otrctx = self.context_managers[profile].getContextForUser(from_jid) 238 otrctx = self.context_managers[profile].getContextForUser(from_jid)