From 4847f834bd8f6a1168851b0918c080fc0e6c13b8 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sun, 18 Sep 2016 17:49:52 +0900 Subject: Add a plugin for XEP-0380: Explicit Message Encryption. --- slixmpp/plugins/xep_0380/eme.py | 66 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 slixmpp/plugins/xep_0380/eme.py (limited to 'slixmpp/plugins/xep_0380/eme.py') diff --git a/slixmpp/plugins/xep_0380/eme.py b/slixmpp/plugins/xep_0380/eme.py new file mode 100644 index 00000000..7ff09f65 --- /dev/null +++ b/slixmpp/plugins/xep_0380/eme.py @@ -0,0 +1,66 @@ +""" + Slixmpp: The Slick XMPP Library + Copyright (C) 2016 Emmanuel Gil Peyrot + This file is part of Slixmpp. + + See the file LICENSE for copying permission. +""" + +import logging + +import slixmpp +from slixmpp.stanza import Message +from slixmpp.xmlstream.handler import Callback +from slixmpp.xmlstream.matcher import StanzaPath +from slixmpp.xmlstream import register_stanza_plugin, ElementBase, ET +from slixmpp.plugins import BasePlugin +from slixmpp.plugins.xep_0380 import stanza, Encryption + + +log = logging.getLogger(__name__) + + +class XEP_0380(BasePlugin): + + """ + XEP-0380: Explicit Message Encryption + """ + + name = 'xep_0380' + description = 'XEP-0380: Explicit Message Encryption' + dependencies = {'xep_0030'} + default_config = { + 'template': 'This message is encrypted with {name} ({namespace})', + } + + mechanisms = { + 'jabber:x:encrypted': 'Legacy OpenPGP', + 'urn:xmpp:ox:0': 'OpenPGP for XMPP', + 'urn:xmpp:otr:0': 'OTR', + 'eu.siacs.conversations.axolotl': 'Legacy OMEMO', + 'urn:xmpp:omemo:0': 'OMEMO', + } + + def plugin_init(self): + self.xmpp.register_handler( + Callback('Explicit Message Encryption', + StanzaPath('message/eme'), + self._handle_eme)) + + register_stanza_plugin(Message, Encryption) + + def plugin_end(self): + self.xmpp.remove_handler('Chat State') + + def session_bind(self, jid): + self.xmpp.plugin['xep_0030'].add_feature(Encryption.namespace) + + def replace_body_with_eme(self, msg): + eme = msg['eme'] + namespace = eme['namespace'] + name = self.mechanisms[namespace] if namespace in self.mechanisms else eme['name'] + body = self.config['template'].format(name=name, namespace=namespace) + msg['body'] = body + + def _handle_eme(self, msg): + self.xmpp.event('message_encryption', msg) -- cgit v1.2.3