comparison sat/plugins/plugin_xep_0359.py @ 4037:524856bd7b19

massive refactoring to switch from camelCase to snake_case: historically, Libervia (SàT before) was using camelCase as allowed by PEP8 when using a pre-PEP8 code, to use the same coding style as in Twisted. However, snake_case is more readable and it's better to follow PEP8 best practices, so it has been decided to move on full snake_case. Because Libervia has a huge codebase, this ended with a ugly mix of camelCase and snake_case. To fix that, this patch does a big refactoring by renaming every function and method (including bridge) that are not coming from Twisted or Wokkel, to use fully snake_case. This is a massive change, and may result in some bugs.
author Goffi <goffi@goffi.org>
date Sat, 08 Apr 2023 13:54:42 +0200
parents 8289ac1b34f4
children
comparison
equal deleted inserted replaced
4036:c4464d7ae97b 4037:524856bd7b19
48 class XEP_0359(object): 48 class XEP_0359(object):
49 49
50 def __init__(self, host): 50 def __init__(self, host):
51 log.info(_("Unique and Stable Stanza IDs plugin initialization")) 51 log.info(_("Unique and Stable Stanza IDs plugin initialization"))
52 self.host = host 52 self.host = host
53 host.registerNamespace("stanza_id", NS_SID) 53 host.register_namespace("stanza_id", NS_SID)
54 host.trigger.add("message_parse", self._message_parseTrigger) 54 host.trigger.add("message_parse", self._message_parse_trigger)
55 host.trigger.add("sendMessageData", self._sendMessageDataTrigger) 55 host.trigger.add("send_message_data", self._send_message_data_trigger)
56 56
57 def _message_parseTrigger(self, client, message_elt, mess_data): 57 def _message_parse_trigger(self, client, message_elt, mess_data):
58 """Check if message has a stanza-id""" 58 """Check if message has a stanza-id"""
59 stanza_id = self.getStanzaId(message_elt, client.jid.userhostJID()) 59 stanza_id = self.get_stanza_id(message_elt, client.jid.userhostJID())
60 if stanza_id is not None: 60 if stanza_id is not None:
61 mess_data['extra']['stanza_id'] = stanza_id 61 mess_data['extra']['stanza_id'] = stanza_id
62 origin_id = self.getOriginId(message_elt) 62 origin_id = self.get_origin_id(message_elt)
63 if origin_id is not None: 63 if origin_id is not None:
64 mess_data['extra']['origin_id'] = origin_id 64 mess_data['extra']['origin_id'] = origin_id
65 return True 65 return True
66 66
67 def _sendMessageDataTrigger(self, client, mess_data): 67 def _send_message_data_trigger(self, client, mess_data):
68 origin_id = mess_data["extra"].get("origin_id") 68 origin_id = mess_data["extra"].get("origin_id")
69 if not origin_id: 69 if not origin_id:
70 origin_id = str(uuid.uuid4()) 70 origin_id = str(uuid.uuid4())
71 mess_data["extra"]["origin_id"] = origin_id 71 mess_data["extra"]["origin_id"] = origin_id
72 message_elt = mess_data["xml"] 72 message_elt = mess_data["xml"]
73 self.addOriginId(message_elt, origin_id) 73 self.add_origin_id(message_elt, origin_id)
74 74
75 def getStanzaId(self, element, by): 75 def get_stanza_id(self, element, by):
76 """Return stanza-id if found in element 76 """Return stanza-id if found in element
77 77
78 @param element(domish.Element): element to parse 78 @param element(domish.Element): element to parse
79 @param by(jid.JID): entity which should have set a stanza-id 79 @param by(jid.JID): entity which should have set a stanza-id
80 @return (unicode, None): stanza-id if found 80 @return (unicode, None): stanza-id if found
90 # we don't break to be sure that there is no more than one element 90 # we don't break to be sure that there is no more than one element
91 # with this "by" attribute 91 # with this "by" attribute
92 92
93 return stanza_id 93 return stanza_id
94 94
95 def addStanzaId(self, client, element, stanza_id, by=None): 95 def add_stanza_id(self, client, element, stanza_id, by=None):
96 """Add a <stanza-id/> to a stanza 96 """Add a <stanza-id/> to a stanza
97 97
98 @param element(domish.Element): stanza where the <stanza-id/> must be added 98 @param element(domish.Element): stanza where the <stanza-id/> must be added
99 @param stanza_id(unicode): id to use 99 @param stanza_id(unicode): id to use
100 @param by(jid.JID, None): jid to use or None to use client.jid 100 @param by(jid.JID, None): jid to use or None to use client.jid
101 """ 101 """
102 sid_elt = element.addElement((NS_SID, "stanza-id")) 102 sid_elt = element.addElement((NS_SID, "stanza-id"))
103 sid_elt["by"] = client.jid.userhost() if by is None else by.userhost() 103 sid_elt["by"] = client.jid.userhost() if by is None else by.userhost()
104 sid_elt["id"] = stanza_id 104 sid_elt["id"] = stanza_id
105 105
106 def getOriginId(self, element: domish.Element) -> Optional[str]: 106 def get_origin_id(self, element: domish.Element) -> Optional[str]:
107 """Return origin-id if found in element 107 """Return origin-id if found in element
108 108
109 @param element: element to parse 109 @param element: element to parse
110 @return: origin-id if found 110 @return: origin-id if found
111 """ 111 """
114 except StopIteration: 114 except StopIteration:
115 return None 115 return None
116 else: 116 else:
117 return origin_elt.getAttribute("id") 117 return origin_elt.getAttribute("id")
118 118
119 def addOriginId(self, element, origin_id=None): 119 def add_origin_id(self, element, origin_id=None):
120 """Add a <origin-id/> to a stanza 120 """Add a <origin-id/> to a stanza
121 121
122 @param element(domish.Element): stanza where the <origin-id/> must be added 122 @param element(domish.Element): stanza where the <origin-id/> must be added
123 @param origin_id(str): id to use, None to automatically generate 123 @param origin_id(str): id to use, None to automatically generate
124 @return (str): origin_id 124 @return (str): origin_id
127 origin_id = str(uuid.uuid4()) 127 origin_id = str(uuid.uuid4())
128 sid_elt = element.addElement((NS_SID, "origin-id")) 128 sid_elt = element.addElement((NS_SID, "origin-id"))
129 sid_elt["id"] = origin_id 129 sid_elt["id"] = origin_id
130 return origin_id 130 return origin_id
131 131
132 def getHandler(self, client): 132 def get_handler(self, client):
133 return XEP_0359_handler() 133 return XEP_0359_handler()
134 134
135 135
136 @implementer(disco.IDisco) 136 @implementer(disco.IDisco)
137 class XEP_0359_handler(xmlstream.XMPPHandler): 137 class XEP_0359_handler(xmlstream.XMPPHandler):