comparison sat/plugins/plugin_xep_0033.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 944f51f9c2b4
children c23cad65ae99
comparison
equal deleted inserted replaced
4036:c4464d7ae97b 4037:524856bd7b19
75 def __init__(self, host): 75 def __init__(self, host):
76 log.info(_("Extended Stanza Addressing plugin initialization")) 76 log.info(_("Extended Stanza Addressing plugin initialization"))
77 self.host = host 77 self.host = host
78 self.internal_data = {} 78 self.internal_data = {}
79 host.trigger.add( 79 host.trigger.add(
80 "sendMessage", self.sendMessageTrigger, trigger.TriggerManager.MIN_PRIORITY 80 "sendMessage", self.send_message_trigger, trigger.TriggerManager.MIN_PRIORITY
81 ) 81 )
82 host.trigger.add("messageReceived", self.messageReceivedTrigger) 82 host.trigger.add("messageReceived", self.message_received_trigger)
83 83
84 def sendMessageTrigger( 84 def send_message_trigger(
85 self, client, mess_data, pre_xml_treatments, post_xml_treatments 85 self, client, mess_data, pre_xml_treatments, post_xml_treatments
86 ): 86 ):
87 """Process the XEP-0033 related data to be sent""" 87 """Process the XEP-0033 related data to be sent"""
88 profile = client.profile 88 profile = client.profile
89 89
90 def treatment(mess_data): 90 def treatment(mess_data):
91 if not "address" in mess_data["extra"]: 91 if not "address" in mess_data["extra"]:
92 return mess_data 92 return mess_data
93 93
94 def discoCallback(entities): 94 def disco_callback(entities):
95 if not entities: 95 if not entities:
96 log.warning( 96 log.warning(
97 _("XEP-0033 is being used but the server doesn't support it!") 97 _("XEP-0033 is being used but the server doesn't support it!")
98 ) 98 )
99 raise failure.Failure( 99 raise failure.Failure(
124 domish.Element( 124 domish.Element(
125 (None, "address"), None, {"type": type_, "jid": jid_} 125 (None, "address"), None, {"type": type_, "jid": jid_}
126 ) 126 )
127 ) 127 )
128 # when the prosody plugin is completed, we can immediately return mess_data from here 128 # when the prosody plugin is completed, we can immediately return mess_data from here
129 self.sendAndStoreMessage(mess_data, entries, profile) 129 self.send_and_store_message(mess_data, entries, profile)
130 log.debug("XEP-0033 took over") 130 log.debug("XEP-0033 took over")
131 raise failure.Failure(exceptions.CancelError("Cancelled by XEP-0033")) 131 raise failure.Failure(exceptions.CancelError("Cancelled by XEP-0033"))
132 132
133 d = self.host.findFeaturesSet(client, [NS_ADDRESS]) 133 d = self.host.find_features_set(client, [NS_ADDRESS])
134 d.addCallbacks(discoCallback, lambda __: discoCallback(None)) 134 d.addCallbacks(disco_callback, lambda __: disco_callback(None))
135 return d 135 return d
136 136
137 post_xml_treatments.addCallback(treatment) 137 post_xml_treatments.addCallback(treatment)
138 return True 138 return True
139 139
140 def sendAndStoreMessage(self, mess_data, entries, profile): 140 def send_and_store_message(self, mess_data, entries, profile):
141 """Check if target servers support XEP-0033, send and store the messages 141 """Check if target servers support XEP-0033, send and store the messages
142 @return: a friendly failure to let the core know that we sent the message already 142 @return: a friendly failure to let the core know that we sent the message already
143 143
144 Later we should be able to remove this method because: 144 Later we should be able to remove this method because:
145 # XXX: sending the messages should be done by the local server 145 # XXX: sending the messages should be done by the local server
146 # FIXME: for now we duplicate the messages in the history for each recipient, this should change 146 # FIXME: for now we duplicate the messages in the history for each recipient, this should change
147 # FIXME: for now we duplicate the echoes to the sender, this should also change 147 # FIXME: for now we duplicate the echoes to the sender, this should also change
148 Ideas: 148 Ideas:
149 - fix Prosody plugin to check if target server support the feature 149 - fix Prosody plugin to check if target server support the feature
150 - redesign the database to save only one entry to the database 150 - redesign the database to save only one entry to the database
151 - change the messageNew signal to eventually pass more than one recipient 151 - change the message_new signal to eventually pass more than one recipient
152 """ 152 """
153 client = self.host.getClient(profile) 153 client = self.host.get_client(profile)
154 154
155 def send(mess_data, skip_send=False): 155 def send(mess_data, skip_send=False):
156 d = defer.Deferred() 156 d = defer.Deferred()
157 if not skip_send: 157 if not skip_send:
158 d.addCallback( 158 d.addCallback(
159 lambda ret: defer.ensureDeferred(client.sendMessageData(ret)) 159 lambda ret: defer.ensureDeferred(client.send_message_data(ret))
160 ) 160 )
161 d.addCallback( 161 d.addCallback(
162 lambda ret: defer.ensureDeferred(client.messageAddToHistory(ret)) 162 lambda ret: defer.ensureDeferred(client.message_add_to_history(ret))
163 ) 163 )
164 d.addCallback(client.messageSendToBridge) 164 d.addCallback(client.message_send_to_bridge)
165 d.addErrback(lambda failure: failure.trap(exceptions.CancelError)) 165 d.addErrback(lambda failure: failure.trap(exceptions.CancelError))
166 return d.callback(mess_data) 166 return d.callback(mess_data)
167 167
168 def discoCallback(entities, to_jid_s): 168 def disco_callback(entities, to_jid_s):
169 history_data = copy.deepcopy(mess_data) 169 history_data = copy.deepcopy(mess_data)
170 history_data["to"] = JID(to_jid_s) 170 history_data["to"] = JID(to_jid_s)
171 history_data["xml"]["to"] = to_jid_s 171 history_data["xml"]["to"] = to_jid_s
172 if entities: 172 if entities:
173 if entities not in self.internal_data[timestamp]: 173 if entities not in self.internal_data[timestamp]:
181 else: 181 else:
182 # target server misses the addressing feature 182 # target server misses the addressing feature
183 send(history_data) 183 send(history_data)
184 184
185 def errback(failure, to_jid): 185 def errback(failure, to_jid):
186 discoCallback(None, to_jid) 186 disco_callback(None, to_jid)
187 187
188 timestamp = time() 188 timestamp = time()
189 self.internal_data[timestamp] = [] 189 self.internal_data[timestamp] = []
190 defer_list = [] 190 defer_list = []
191 for type_, jid_ in entries: 191 for type_, jid_ in entries:
192 d = defer.Deferred() 192 d = defer.Deferred()
193 d.addCallback( 193 d.addCallback(
194 self.host.findFeaturesSet, client=client, jid_=JID(JID(jid_).host) 194 self.host.find_features_set, client=client, jid_=JID(JID(jid_).host)
195 ) 195 )
196 d.addCallbacks( 196 d.addCallbacks(
197 discoCallback, errback, callbackArgs=[jid_], errbackArgs=[jid_] 197 disco_callback, errback, callbackArgs=[jid_], errbackArgs=[jid_]
198 ) 198 )
199 d.callback([NS_ADDRESS]) 199 d.callback([NS_ADDRESS])
200 defer_list.append(d) 200 defer_list.append(d)
201 d = defer.Deferred().addCallback(lambda __: self.internal_data.pop(timestamp)) 201 d = defer.Deferred().addCallback(lambda __: self.internal_data.pop(timestamp))
202 defer.DeferredList(defer_list).chainDeferred(d) 202 defer.DeferredList(defer_list).chainDeferred(d)
203 203
204 def messageReceivedTrigger(self, client, message, post_treat): 204 def message_received_trigger(self, client, message, post_treat):
205 """In order to save the addressing information in the history""" 205 """In order to save the addressing information in the history"""
206 206
207 def post_treat_addr(data, addresses): 207 def post_treat_addr(data, addresses):
208 data["extra"]["addresses"] = "" 208 data["extra"]["addresses"] = ""
209 for address in addresses: 209 for address in addresses:
222 pass # no addresses 222 pass # no addresses
223 else: 223 else:
224 post_treat.addCallback(post_treat_addr, addresses.children) 224 post_treat.addCallback(post_treat_addr, addresses.children)
225 return True 225 return True
226 226
227 def getHandler(self, client): 227 def get_handler(self, client):
228 return XEP_0033_handler(self, client.profile) 228 return XEP_0033_handler(self, client.profile)
229 229
230 230
231 @implementer(iwokkel.IDisco) 231 @implementer(iwokkel.IDisco)
232 class XEP_0033_handler(XMPPHandler): 232 class XEP_0033_handler(XMPPHandler):