comparison src/plugins/plugin_sec_otr.py @ 1149:652cd93dfdb4

plugin OTR: add bridge method skipOTR to desactivate OTR handling for a given profile
author souliane <souliane@mailoo.org>
date Mon, 01 Sep 2014 15:45:35 +0200
parents 736f1dd6e142
children 39572f9d5249
comparison
equal deleted inserted replaced
1148:8cdb97e89d9b 1149:652cd93dfdb4
188 def __init__(self, host): 188 def __init__(self, host):
189 log.info(_(u"OTR plugin initialization")) 189 log.info(_(u"OTR plugin initialization"))
190 self._fixPotr() # FIXME: to be removed when potr will be fixed 190 self._fixPotr() # FIXME: to be removed when potr will be fixed
191 self.host = host 191 self.host = host
192 self.context_managers = {} 192 self.context_managers = {}
193 self.skipped_profiles = set()
193 host.trigger.add("MessageReceived", self.MessageReceivedTrigger, priority=100000) 194 host.trigger.add("MessageReceived", self.MessageReceivedTrigger, priority=100000)
194 host.trigger.add("sendMessage", self.sendMessageTrigger, priority=100000) 195 host.trigger.add("sendMessage", self.sendMessageTrigger, priority=100000)
196 host.bridge.addMethod("skipOTR", ".plugin", in_sign='s', out_sign='', method=self._skipOTR)
195 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) 197 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)
196 host.importMenu((MAIN_MENU, D_("End session")), self._endSession, security_limit=0, help_string=D_("Finish an OTR session"), type_=C.MENU_SINGLE) 198 host.importMenu((MAIN_MENU, D_("End session")), self._endSession, security_limit=0, help_string=D_("Finish an OTR session"), type_=C.MENU_SINGLE)
197 host.importMenu((MAIN_MENU, D_("Authenticate")), self._authenticate, security_limit=0, help_string=D_("Authenticate user/see your fingerprint"), type_=C.MENU_SINGLE) 199 host.importMenu((MAIN_MENU, D_("Authenticate")), self._authenticate, security_limit=0, help_string=D_("Authenticate user/see your fingerprint"), type_=C.MENU_SINGLE)
198 host.importMenu((MAIN_MENU, D_("Drop private key")), self._dropPrivKey, security_limit=0, type_=C.MENU_SINGLE) 200 host.importMenu((MAIN_MENU, D_("Drop private key")), self._dropPrivKey, security_limit=0, type_=C.MENU_SINGLE)
199 201
211 msg = defaultQuery.format(versions=v) 213 msg = defaultQuery.format(versions=v)
212 return msg.encode('ascii') 214 return msg.encode('ascii')
213 215
214 potr.context.Account.getDefaultQueryMessage = getDefaultQueryMessage 216 potr.context.Account.getDefaultQueryMessage = getDefaultQueryMessage
215 217
218 def _skipOTR(self, profile):
219 """Tell the backend to not handle OTR for this profile.
220
221 @param profile (str): %(doc_profile)s
222 """
223 self.skipped_profiles.add(profile)
224
216 @defer.inlineCallbacks 225 @defer.inlineCallbacks
217 def profileConnected(self, profile): 226 def profileConnected(self, profile):
227 if profile in self.skipped_profiles:
228 return
218 client = self.host.getClient(profile) 229 client = self.host.getClient(profile)
219 ctxMng = self.context_managers[profile] = ContextManager(self.host, client) 230 ctxMng = self.context_managers[profile] = ContextManager(self.host, client)
220 client.otr_data = persistent.PersistentBinaryDict(NS_OTR, profile) 231 client.otr_data = persistent.PersistentBinaryDict(NS_OTR, profile)
221 yield client.otr_data.load() 232 yield client.otr_data.load()
222 encrypted_priv_key = client.otr_data.get(PRIVATE_KEY, None) 233 encrypted_priv_key = client.otr_data.get(PRIVATE_KEY, None)
224 priv_key = yield self.host.memory.decryptValue(encrypted_priv_key, profile) 235 priv_key = yield self.host.memory.decryptValue(encrypted_priv_key, profile)
225 ctxMng.account.privkey = potr.crypt.PK.parsePrivateKey(priv_key.decode('hex'))[0] 236 ctxMng.account.privkey = potr.crypt.PK.parsePrivateKey(priv_key.decode('hex'))[0]
226 else: 237 else:
227 ctxMng.account.privkey = None 238 ctxMng.account.privkey = None
228 ctxMng.account.loadTrusts() 239 ctxMng.account.loadTrusts()
240
241 def profileDisconnected(self, profile):
242 try:
243 self.skipped_profiles.remove(profile)
244 except KeyError:
245 pass
229 246
230 def _startRefresh(self, menu_data, profile): 247 def _startRefresh(self, menu_data, profile):
231 """Start or refresh an OTR session 248 """Start or refresh an OTR session
232 249
233 @param menu_data: %(menu_data)s 250 @param menu_data: %(menu_data)s
396 raise failure.Failure(exceptions.SkipHistory()) # we send the decrypted message to frontends, but we don't want it in history 413 raise failure.Failure(exceptions.SkipHistory()) # we send the decrypted message to frontends, but we don't want it in history
397 else: 414 else:
398 raise failure.Failure(exceptions.CancelError()) # no message at all (no history, no signal) 415 raise failure.Failure(exceptions.CancelError()) # no message at all (no history, no signal)
399 416
400 def MessageReceivedTrigger(self, message, post_treat, profile): 417 def MessageReceivedTrigger(self, message, post_treat, profile):
418 if profile in self.skipped_profiles:
419 return True
401 post_treat.addCallback(self._receivedTreatment, profile) 420 post_treat.addCallback(self._receivedTreatment, profile)
402 return True 421 return True
403 422
404 def sendMessageTrigger(self, mess_data, pre_xml_treatments, post_xml_treatments, profile): 423 def sendMessageTrigger(self, mess_data, pre_xml_treatments, post_xml_treatments, profile):
424 if profile in self.skipped_profiles:
425 return True
405 to_jid = mess_data['to'] 426 to_jid = mess_data['to']
406 if mess_data['type'] != 'groupchat' and not to_jid.resource: 427 if mess_data['type'] != 'groupchat' and not to_jid.resource:
407 to_jid.resource = self.host.memory.getLastResource(to_jid, profile) # FIXME: it's dirty, but frontends don't manage resources correctly now, refactoring is planed 428 to_jid.resource = self.host.memory.getLastResource(to_jid, profile) # FIXME: it's dirty, but frontends don't manage resources correctly now, refactoring is planed
408 otrctx = self.context_managers[profile].getContextForUser(to_jid) 429 otrctx = self.context_managers[profile].getContextForUser(to_jid)
409 if mess_data['type'] != 'groupchat' and otrctx.state == potr.context.STATE_ENCRYPTED: 430 if mess_data['type'] != 'groupchat' and otrctx.state == potr.context.STATE_ENCRYPTED: