comparison sat/plugins/plugin_xep_0334.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 26edcf3a30eb
children baccc27d5c5c
comparison
equal deleted inserted replaced
2623:49533de4540b 2624:56f94936df1e
18 # You should have received a copy of the GNU Affero General Public License 18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>. 19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 20
21 from sat.core.i18n import _, D_ 21 from sat.core.i18n import _, D_
22 from sat.core.log import getLogger 22 from sat.core.log import getLogger
23
23 log = getLogger(__name__) 24 log = getLogger(__name__)
24 from sat.core.constants import Const as C 25 from sat.core.constants import Const as C
25 26
26 from sat.tools.common import data_format 27 from sat.tools.common import data_format
27 28
28 from wokkel import disco, iwokkel 29 from wokkel import disco, iwokkel
30
29 try: 31 try:
30 from twisted.words.protocols.xmlstream import XMPPHandler 32 from twisted.words.protocols.xmlstream import XMPPHandler
31 except ImportError: 33 except ImportError:
32 from wokkel.subprotocols import XMPPHandler 34 from wokkel.subprotocols import XMPPHandler
33 from zope.interface import implements 35 from zope.interface import implements
40 C.PI_TYPE: u"XEP", 42 C.PI_TYPE: u"XEP",
41 C.PI_PROTOCOLS: [u"XEP-0334"], 43 C.PI_PROTOCOLS: [u"XEP-0334"],
42 C.PI_MAIN: "XEP_0334", 44 C.PI_MAIN: "XEP_0334",
43 C.PI_HANDLER: u"yes", 45 C.PI_HANDLER: u"yes",
44 C.PI_DESCRIPTION: D_(u"""Implementation of Message Processing Hints"""), 46 C.PI_DESCRIPTION: D_(u"""Implementation of Message Processing Hints"""),
45 C.PI_USAGE: dedent(D_(u"""\ 47 C.PI_USAGE: dedent(
48 D_(
49 u"""\
46 Frontends can use HINT_* constants in mess_data['extra'] in a serialized 'hints' dict. 50 Frontends can use HINT_* constants in mess_data['extra'] in a serialized 'hints' dict.
47 Internal plugins can use directly addHint([HINT_* constant]). 51 Internal plugins can use directly addHint([HINT_* constant]).
48 Will set mess_data['extra']['history'] to 'skipped' when no store is requested and message is not saved in history.""")) 52 Will set mess_data['extra']['history'] to 'skipped' when no store is requested and message is not saved in history."""
49 53 )
54 ),
50 } 55 }
51 56
52 NS_HINTS = u'urn:xmpp:hints' 57 NS_HINTS = u"urn:xmpp:hints"
53 58
54 59
55 class XEP_0334(object): 60 class XEP_0334(object):
56 HINT_NO_PERMANENT_STORE = u'no-permanent-store' 61 HINT_NO_PERMANENT_STORE = u"no-permanent-store"
57 HINT_NO_STORE = u'no-store' 62 HINT_NO_STORE = u"no-store"
58 HINT_NO_COPY = u'no-copy' 63 HINT_NO_COPY = u"no-copy"
59 HINT_STORE = u'store' 64 HINT_STORE = u"store"
60 HINTS = (HINT_NO_PERMANENT_STORE, HINT_NO_STORE, HINT_NO_COPY, HINT_STORE) 65 HINTS = (HINT_NO_PERMANENT_STORE, HINT_NO_STORE, HINT_NO_COPY, HINT_STORE)
61 66
62 def __init__(self, host): 67 def __init__(self, host):
63 log.info(_("Message Processing Hints plugin initialization")) 68 log.info(_("Message Processing Hints plugin initialization"))
64 self.host = host 69 self.host = host
67 72
68 def getHandler(self, client): 73 def getHandler(self, client):
69 return XEP_0334_handler() 74 return XEP_0334_handler()
70 75
71 def addHint(self, mess_data, hint): 76 def addHint(self, mess_data, hint):
72 if hint == self.HINT_NO_COPY and not mess_data['to'].resource: 77 if hint == self.HINT_NO_COPY and not mess_data["to"].resource:
73 log.error(u"{hint} can only be used with full jids! Ignoring it.".format(hint=hint)) 78 log.error(
79 u"{hint} can only be used with full jids! Ignoring it.".format(hint=hint)
80 )
74 return 81 return
75 hints = mess_data.setdefault('hints', set()) 82 hints = mess_data.setdefault("hints", set())
76 if hint in self.HINTS: 83 if hint in self.HINTS:
77 hints.add(hint) 84 hints.add(hint)
78 else: 85 else:
79 log.error(u"Unknown hint: {}".format(hint)) 86 log.error(u"Unknown hint: {}".format(hint))
80 87
81 def _sendPostXmlTreatment(self, mess_data): 88 def _sendPostXmlTreatment(self, mess_data):
82 if 'hints' in mess_data: 89 if "hints" in mess_data:
83 for hint in mess_data['hints']: 90 for hint in mess_data["hints"]:
84 mess_data[u'xml'].addElement((NS_HINTS, hint)) 91 mess_data[u"xml"].addElement((NS_HINTS, hint))
85 return mess_data 92 return mess_data
86 93
87 def sendMessageTrigger(self, client, mess_data, pre_xml_treatments, post_xml_treatments): 94 def sendMessageTrigger(
95 self, client, mess_data, pre_xml_treatments, post_xml_treatments
96 ):
88 """Add the hints element to the message to be sent""" 97 """Add the hints element to the message to be sent"""
89 if u'hints' in mess_data[u'extra']: 98 if u"hints" in mess_data[u"extra"]:
90 for hint in data_format.dict2iter(u'hints', mess_data[u'extra'], pop=True): 99 for hint in data_format.dict2iter(u"hints", mess_data[u"extra"], pop=True):
91 self.addHint(hint) 100 self.addHint(hint)
92 101
93 post_xml_treatments.addCallback(self._sendPostXmlTreatment) 102 post_xml_treatments.addCallback(self._sendPostXmlTreatment)
94 return True 103 return True
95 104
96 def _receivedSkipHistory(self, mess_data): 105 def _receivedSkipHistory(self, mess_data):
97 mess_data[u'history'] = C.HISTORY_SKIP 106 mess_data[u"history"] = C.HISTORY_SKIP
98 return mess_data 107 return mess_data
99 108
100 def messageReceivedTrigger(self, client, message_elt, post_treat): 109 def messageReceivedTrigger(self, client, message_elt, post_treat):
101 """Check for hints in the received message""" 110 """Check for hints in the received message"""
102 for elt in message_elt.elements(): 111 for elt in message_elt.elements():
103 if elt.uri == NS_HINTS and elt.name in (self.HINT_NO_PERMANENT_STORE, self.HINT_NO_STORE): 112 if elt.uri == NS_HINTS and elt.name in (
113 self.HINT_NO_PERMANENT_STORE,
114 self.HINT_NO_STORE,
115 ):
104 log.debug(u"history will be skipped for this message, as requested") 116 log.debug(u"history will be skipped for this message, as requested")
105 post_treat.addCallback(self._receivedSkipHistory) 117 post_treat.addCallback(self._receivedSkipHistory)
106 break 118 break
107 return True 119 return True
108 120
109 121
110 class XEP_0334_handler(XMPPHandler): 122 class XEP_0334_handler(XMPPHandler):
111 implements(iwokkel.IDisco) 123 implements(iwokkel.IDisco)
112 124
113 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): 125 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
114 return [disco.DiscoFeature(NS_HINTS)] 126 return [disco.DiscoFeature(NS_HINTS)]
115 127
116 def getDiscoItems(self, requestor, target, nodeIdentifier=''): 128 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
117 return [] 129 return []