comparison sat/plugins/plugin_xep_0249.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 32d714a8ea51
children c23cad65ae99
comparison
equal deleted inserted replaced
4036:c4464d7ae97b 4037:524856bd7b19
84 } 84 }
85 85
86 def __init__(self, host): 86 def __init__(self, host):
87 log.info(_("Plugin XEP_0249 initialization")) 87 log.info(_("Plugin XEP_0249 initialization"))
88 self.host = host 88 self.host = host
89 host.memory.updateParams(self.params) 89 host.memory.update_params(self.params)
90 host.bridge.addMethod( 90 host.bridge.add_method(
91 "inviteMUC", ".plugin", in_sign="ssa{ss}s", out_sign="", method=self._invite 91 "muc_invite", ".plugin", in_sign="ssa{ss}s", out_sign="", method=self._invite
92 ) 92 )
93 try: 93 try:
94 self.host.plugins[C.TEXT_CMDS].registerTextCommands(self) 94 self.host.plugins[C.TEXT_CMDS].register_text_commands(self)
95 except KeyError: 95 except KeyError:
96 log.info(_("Text commands not available")) 96 log.info(_("Text commands not available"))
97 host.registerNamespace('x-conference', NS_X_CONFERENCE) 97 host.register_namespace('x-conference', NS_X_CONFERENCE)
98 host.trigger.add("messageReceived", self._messageReceivedTrigger) 98 host.trigger.add("messageReceived", self._message_received_trigger)
99 99
100 def getHandler(self, client): 100 def get_handler(self, client):
101 return XEP_0249_handler() 101 return XEP_0249_handler()
102 102
103 def _invite(self, guest_jid_s, room_jid_s, options, profile_key): 103 def _invite(self, guest_jid_s, room_jid_s, options, profile_key):
104 """Invite an user to a room 104 """Invite an user to a room
105 105
107 @param service: jid of the MUC service 107 @param service: jid of the MUC service
108 @param roomId: name of the room 108 @param roomId: name of the room
109 @param profile_key: %(doc_profile_key)s 109 @param profile_key: %(doc_profile_key)s
110 """ 110 """
111 # TODO: check parameters validity 111 # TODO: check parameters validity
112 client = self.host.getClient(profile_key) 112 client = self.host.get_client(profile_key)
113 self.invite(client, jid.JID(guest_jid_s), jid.JID(room_jid_s, options)) 113 self.invite(client, jid.JID(guest_jid_s), jid.JID(room_jid_s, options))
114 114
115 def invite(self, client, guest, room, options={}): 115 def invite(self, client, guest, room, options={}):
116 """Invite a user to a room 116 """Invite a user to a room
117 117
134 def _accept(self, room_jid, profile_key=C.PROF_KEY_NONE): 134 def _accept(self, room_jid, profile_key=C.PROF_KEY_NONE):
135 """Accept the invitation to join a MUC. 135 """Accept the invitation to join a MUC.
136 136
137 @param room (jid.JID): JID of the room 137 @param room (jid.JID): JID of the room
138 """ 138 """
139 client = self.host.getClient(profile_key) 139 client = self.host.get_client(profile_key)
140 log.info( 140 log.info(
141 _("Invitation accepted for room %(room)s [%(profile)s]") 141 _("Invitation accepted for room %(room)s [%(profile)s]")
142 % {"room": room_jid.userhost(), "profile": client.profile} 142 % {"room": room_jid.userhost(), "profile": client.profile}
143 ) 143 )
144 d = defer.ensureDeferred( 144 d = defer.ensureDeferred(
145 self.host.plugins["XEP-0045"].join(client, room_jid, client.jid.user, {}) 145 self.host.plugins["XEP-0045"].join(client, room_jid, client.jid.user, {})
146 ) 146 )
147 return d 147 return d
148 148
149 def _messageReceivedTrigger(self, client, message_elt, post_treat): 149 def _message_received_trigger(self, client, message_elt, post_treat):
150 """Check if a direct invitation is in the message, and handle it""" 150 """Check if a direct invitation is in the message, and handle it"""
151 x_elt = next(message_elt.elements(NS_X_CONFERENCE, 'x'), None) 151 x_elt = next(message_elt.elements(NS_X_CONFERENCE, 'x'), None)
152 if x_elt is None: 152 if x_elt is None:
153 return True 153 return True
154 154
163 % {"room": room_jid_s, "profile": client.profile} 163 % {"room": room_jid_s, "profile": client.profile}
164 ) 164 )
165 from_jid_s = message_elt["from"] 165 from_jid_s = message_elt["from"]
166 room_jid = jid.JID(room_jid_s) 166 room_jid = jid.JID(room_jid_s)
167 try: 167 try:
168 self.host.plugins["XEP-0045"].checkRoomJoined(client, room_jid) 168 self.host.plugins["XEP-0045"].check_room_joined(client, room_jid)
169 except exceptions.NotFound: 169 except exceptions.NotFound:
170 pass 170 pass
171 else: 171 else:
172 log.info( 172 log.info(
173 _("Invitation silently discarded because user is already in the room.") 173 _("Invitation silently discarded because user is already in the room.")
174 ) 174 )
175 return 175 return
176 176
177 autojoin = self.host.memory.getParamA( 177 autojoin = self.host.memory.param_get_a(
178 AUTOJOIN_NAME, AUTOJOIN_KEY, profile_key=client.profile 178 AUTOJOIN_NAME, AUTOJOIN_KEY, profile_key=client.profile
179 ) 179 )
180 180
181 if autojoin == "always": 181 if autojoin == "always":
182 self._accept(room_jid, client.profile) 182 self._accept(room_jid, client.profile)
184 msg = D_( 184 msg = D_(
185 "An invitation from %(user)s to join the room %(room)s has been " 185 "An invitation from %(user)s to join the room %(room)s has been "
186 "declined according to your personal settings." 186 "declined according to your personal settings."
187 ) % {"user": from_jid_s, "room": room_jid_s} 187 ) % {"user": from_jid_s, "room": room_jid_s}
188 title = D_("MUC invitation") 188 title = D_("MUC invitation")
189 xml_tools.quickNote(self.host, client, msg, title, C.XMLUI_DATA_LVL_INFO) 189 xml_tools.quick_note(self.host, client, msg, title, C.XMLUI_DATA_LVL_INFO)
190 else: # leave the default value here 190 else: # leave the default value here
191 confirm_msg = D_( 191 confirm_msg = D_(
192 "You have been invited by %(user)s to join the room %(room)s. " 192 "You have been invited by %(user)s to join the room %(room)s. "
193 "Do you accept?" 193 "Do you accept?"
194 ) % {"user": from_jid_s, "room": room_jid_s} 194 ) % {"user": from_jid_s, "room": room_jid_s}
195 confirm_title = D_("MUC invitation") 195 confirm_title = D_("MUC invitation")
196 d = xml_tools.deferConfirm( 196 d = xml_tools.defer_confirm(
197 self.host, confirm_msg, confirm_title, profile=client.profile 197 self.host, confirm_msg, confirm_title, profile=client.profile
198 ) 198 )
199 199
200 def accept_cb(accepted): 200 def accept_cb(accepted):
201 if accepted: 201 if accepted:
217 except (RuntimeError, jid.InvalidFormat, AttributeError): 217 except (RuntimeError, jid.InvalidFormat, AttributeError):
218 feedback = _( 218 feedback = _(
219 "You must provide a valid JID to invite, like in '/invite " 219 "You must provide a valid JID to invite, like in '/invite "
220 "contact@{host}'" 220 "contact@{host}'"
221 ).format(host=my_host) 221 ).format(host=my_host)
222 self.host.plugins[C.TEXT_CMDS].feedBack(client, feedback, mess_data) 222 self.host.plugins[C.TEXT_CMDS].feed_back(client, feedback, mess_data)
223 return False 223 return False
224 if not contact_jid.user: 224 if not contact_jid.user:
225 contact_jid.user, contact_jid.host = contact_jid.host, my_host 225 contact_jid.user, contact_jid.host = contact_jid.host, my_host
226 self.invite(client, contact_jid, mess_data["to"]) 226 self.invite(client, contact_jid, mess_data["to"])
227 return False 227 return False