Mercurial > libervia-backend
diff sat/plugins/plugin_xep_0085.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 | 94708a7d3ecf |
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0085.py Wed Jun 27 07:51:29 2018 +0200 +++ b/sat/plugins/plugin_xep_0085.py Wed Jun 27 20:14:46 2018 +0200 @@ -21,10 +21,12 @@ from sat.core.constants import Const as C from sat.core import exceptions from sat.core.log import getLogger + log = getLogger(__name__) from wokkel import disco, iwokkel from zope.interface import implements from twisted.words.protocols.jabber.jid import JID + try: from twisted.words.protocols.xmlstream import XMPPHandler except ImportError: @@ -50,7 +52,7 @@ C.PI_DEPENDENCIES: [], C.PI_MAIN: "XEP_0085", C.PI_HANDLER: "yes", - C.PI_DESCRIPTION: _("""Implementation of Chat State Notifications Protocol""") + C.PI_DESCRIPTION: _("""Implementation of Chat State Notifications Protocol"""), } @@ -63,7 +65,7 @@ "inactive": {"next_state": "gone", "delay": 480}, "gone": {"next_state": "", "delay": 0}, "composing": {"next_state": "paused", "delay": 30}, - "paused": {"next_state": "inactive", "delay": 450} + "paused": {"next_state": "inactive", "delay": 450}, } @@ -71,6 +73,7 @@ """ This error is raised when an unknown chat state is used. """ + pass @@ -78,6 +81,7 @@ """ Implementation for XEP 0085 """ + params = """ <params> <individual> @@ -87,10 +91,10 @@ </individual> </params> """ % { - 'category_name': PARAM_KEY, - 'category_label': _(PARAM_KEY), - 'param_name': PARAM_NAME, - 'param_label': _('Enable chat state notifications') + "category_name": PARAM_KEY, + "category_label": _(PARAM_KEY), + "param_name": PARAM_NAME, + "param_label": _("Enable chat state notifications"), } def __init__(self, host): @@ -107,11 +111,16 @@ host.trigger.add("paramUpdateTrigger", self.paramUpdateTrigger) # args: to_s (jid as string), profile - host.bridge.addMethod("chatStateComposing", ".plugin", in_sign='ss', - out_sign='', method=self.chatStateComposing) + host.bridge.addMethod( + "chatStateComposing", + ".plugin", + in_sign="ss", + out_sign="", + method=self.chatStateComposing, + ) # args: from (jid as string), state in CHAT_STATES, profile - host.bridge.addSignal("chatStateReceived", ".plugin", signature='sss') + host.bridge.addSignal("chatStateReceived", ".plugin", signature="sss") def getHandler(self, client): return XEP_0085_handler(self, client.profile) @@ -125,7 +134,7 @@ # FIXME: the "unavailable" presence stanza is received by to_jid # before the chat state, so it will be ignored... find a way to # actually defer the disconnection - self.map[profile][to_jid]._onEvent('gone') + self.map[profile][to_jid]._onEvent("gone") del self.map[profile] def updateCache(self, entity_jid, value, profile): @@ -139,7 +148,9 @@ if value == DELETE_VALUE: self.host.memory.delEntityDatum(entity_jid, ENTITY_KEY, profile) else: - self.host.memory.updateEntityData(entity_jid, ENTITY_KEY, value, profile_key=profile) + self.host.memory.updateEntityData( + entity_jid, ENTITY_KEY, value, profile_key=profile + ) if not value or value == DELETE_VALUE: # reinit chat state UI for this or these contact(s) self.host.bridge.chatStateReceived(entity_jid.full(), "", profile) @@ -153,7 +164,9 @@ @param type_: parameter type """ if (category, name) == (PARAM_KEY, PARAM_NAME): - self.updateCache(C.ENTITY_ALL, True if C.bool(value) else DELETE_VALUE, profile=profile) + self.updateCache( + C.ENTITY_ALL, True if C.bool(value) else DELETE_VALUE, profile=profile + ) return False return True @@ -178,7 +191,7 @@ # contact enabled Chat State Notifications self.updateCache(from_jid, True, profile=profile) except StopIteration: - if message.getAttribute('type') == 'chat': + if message.getAttribute("type") == "chat": # contact didn't enable Chat State Notifications self.updateCache(from_jid, False, profile=profile) return True @@ -188,32 +201,40 @@ # send our next "composing" states to any MUC and to the contacts who enabled the feature self._chatStateInit(from_jid, message.getAttribute("type"), profile) - state_list = [child.name for child in message.elements() if - message.getAttribute("type") in MESSAGE_TYPES - and child.name in CHAT_STATES - and child.defaultUri == NS_CHAT_STATES] + state_list = [ + child.name + for child in message.elements() + if message.getAttribute("type") in MESSAGE_TYPES + and child.name in CHAT_STATES + and child.defaultUri == NS_CHAT_STATES + ] for state in state_list: # there must be only one state according to the XEP - if state != 'gone' or message.getAttribute('type') != 'groupchat': - self.host.bridge.chatStateReceived(message.getAttribute("from"), state, profile) + if state != "gone" or message.getAttribute("type") != "groupchat": + self.host.bridge.chatStateReceived( + message.getAttribute("from"), state, profile + ) break return True - def sendMessageTrigger(self, client, mess_data, pre_xml_treatments, post_xml_treatments): + def sendMessageTrigger( + self, client, mess_data, pre_xml_treatments, post_xml_treatments + ): """ Eventually add the chat state to the message and initiate the state machine when sending an "active" state. """ profile = client.profile + def treatment(mess_data): - message = mess_data['xml'] + message = mess_data["xml"] to_jid = JID(message.getAttribute("to")) if not self._checkActivation(to_jid, forceEntityData=True, profile=profile): return mess_data try: # message with a body always mean active state domish.generateElementsNamed(message.elements(), name="body").next() - message.addElement('active', NS_CHAT_STATES) + message.addElement("active", NS_CHAT_STATES) # launch the chat state machine (init the timer) if self._isMUC(to_jid, profile): to_jid = to_jid.userhostJID() @@ -236,8 +257,8 @@ @return: bool """ try: - type_ = self.host.memory.getEntityDatum(to_jid.userhostJID(), 'type', profile) - if type_ == 'chatroom': # FIXME: should not use disco instead ? + type_ = self.host.memory.getEntityDatum(to_jid.userhostJID(), "type", profile) + if type_ == "chatroom": # FIXME: should not use disco instead ? return True except (exceptions.UnknownEntityError, KeyError): pass @@ -281,8 +302,7 @@ return profile_map = self.map.setdefault(profile, {}) if to_jid not in profile_map: - machine = ChatStateMachine(self.host, to_jid, - mess_type, profile) + machine = ChatStateMachine(self.host, to_jid, mess_type, profile) self.map[profile][to_jid] = machine def _chatStateActive(self, to_jid, mess_type, profile_key): @@ -316,7 +336,9 @@ to_jid = to_jid.userhostJID() elif not to_jid.resource: to_jid.resource = self.host.memory.getMainResource(client, to_jid) - if not self._checkActivation(to_jid, forceEntityData=False, profile=client.profile): + if not self._checkActivation( + to_jid, forceEntityData=False, profile=client.profile + ): return try: self.map[client.profile][to_jid]._onEvent("composing") @@ -359,22 +381,26 @@ assert "next_state" in transition and "delay" in transition if state != self.state and state != "active": - if state != 'gone' or self.mess_type != 'groupchat': + if state != "gone" or self.mess_type != "groupchat": # send a new message without body - log.debug(u"sending state '{state}' to {jid}".format(state=state, jid=self.to_jid.full())) + log.debug( + u"sending state '{state}' to {jid}".format( + state=state, jid=self.to_jid.full() + ) + ) client = self.host.getClient(self.profile) mess_data = { - 'from': client.jid, - 'to': self.to_jid, - 'uid': '', - 'message': {}, - 'type': self.mess_type, - 'subject': {}, - 'extra': {}, - } + "from": client.jid, + "to": self.to_jid, + "uid": "", + "message": {}, + "type": self.mess_type, + "subject": {}, + "extra": {}, + } client.generateMessageXML(mess_data) - mess_data['xml'].addElement(state, NS_CHAT_STATES) - client.send(mess_data['xml']) + mess_data["xml"].addElement(state, NS_CHAT_STATES) + client.send(mess_data["xml"]) self.state = state try: @@ -383,7 +409,9 @@ pass if transition["next_state"] and transition["delay"] > 0: - self.timer = reactor.callLater(transition["delay"], self._onEvent, transition["next_state"]) + self.timer = reactor.callLater( + transition["delay"], self._onEvent, transition["next_state"] + ) class XEP_0085_handler(XMPPHandler): @@ -394,8 +422,8 @@ self.host = plugin_parent.host self.profile = profile - def getDiscoInfo(self, requestor, target, nodeIdentifier=''): + def getDiscoInfo(self, requestor, target, nodeIdentifier=""): return [disco.DiscoFeature(NS_CHAT_STATES)] - def getDiscoItems(self, requestor, target, nodeIdentifier=''): + def getDiscoItems(self, requestor, target, nodeIdentifier=""): return []