Mercurial > libervia-backend
comparison sat/plugins/plugin_exp_parrot.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 | be6d91572633 |
children | c23cad65ae99 |
comparison
equal
deleted
inserted
replaced
4036:c4464d7ae97b | 4037:524856bd7b19 |
---|---|
46 class Exp_Parrot(object): | 46 class Exp_Parrot(object): |
47 """Parrot mode plugin: repeat messages from one entity or MUC room to another one""" | 47 """Parrot mode plugin: repeat messages from one entity or MUC room to another one""" |
48 | 48 |
49 # XXX: This plugin can be potentially dangerous if we don't trust entities linked | 49 # XXX: This plugin can be potentially dangerous if we don't trust entities linked |
50 # this is specially true if we have other triggers. | 50 # this is specially true if we have other triggers. |
51 # sendMessageTrigger avoid other triggers execution, it's deactivated to allow | 51 # send_message_trigger avoid other triggers execution, it's deactivated to allow |
52 # /unparrot command in text commands plugin. | 52 # /unparrot command in text commands plugin. |
53 # FIXME: potentially unsecure, specially with e2e encryption | 53 # FIXME: potentially unsecure, specially with e2e encryption |
54 | 54 |
55 def __init__(self, host): | 55 def __init__(self, host): |
56 log.info(_("Plugin Parrot initialization")) | 56 log.info(_("Plugin Parrot initialization")) |
57 self.host = host | 57 self.host = host |
58 host.trigger.add("messageReceived", self.messageReceivedTrigger, priority=100) | 58 host.trigger.add("messageReceived", self.message_received_trigger, priority=100) |
59 # host.trigger.add("sendMessage", self.sendMessageTrigger, priority=100) | 59 # host.trigger.add("sendMessage", self.send_message_trigger, priority=100) |
60 try: | 60 try: |
61 self.host.plugins[C.TEXT_CMDS].registerTextCommands(self) | 61 self.host.plugins[C.TEXT_CMDS].register_text_commands(self) |
62 except KeyError: | 62 except KeyError: |
63 log.info(_("Text commands not available")) | 63 log.info(_("Text commands not available")) |
64 | 64 |
65 # def sendMessageTrigger(self, client, mess_data, treatments): | 65 # def send_message_trigger(self, client, mess_data, treatments): |
66 # """ Deactivate other triggers if recipient is in parrot links """ | 66 # """ Deactivate other triggers if recipient is in parrot links """ |
67 # try: | 67 # try: |
68 # _links = client.parrot_links | 68 # _links = client.parrot_links |
69 # except AttributeError: | 69 # except AttributeError: |
70 # return True | 70 # return True |
71 # | 71 # |
72 # if mess_data['to'].userhostJID() in _links.values(): | 72 # if mess_data['to'].userhostJID() in _links.values(): |
73 # log.debug("Parrot link detected, skipping other triggers") | 73 # log.debug("Parrot link detected, skipping other triggers") |
74 # raise trigger.SkipOtherTriggers | 74 # raise trigger.SkipOtherTriggers |
75 | 75 |
76 def messageReceivedTrigger(self, client, message_elt, post_treat): | 76 def message_received_trigger(self, client, message_elt, post_treat): |
77 """ Check if source is linked and repeat message, else do nothing """ | 77 """ Check if source is linked and repeat message, else do nothing """ |
78 # TODO: many things are not repeated (subject, thread, etc) | 78 # TODO: many things are not repeated (subject, thread, etc) |
79 from_jid = message_elt["from"] | 79 from_jid = message_elt["from"] |
80 | 80 |
81 try: | 81 try: |
90 for e in message_elt.elements(C.NS_CLIENT, "body"): | 90 for e in message_elt.elements(C.NS_CLIENT, "body"): |
91 body = str(e) | 91 body = str(e) |
92 lang = e.getAttribute("lang") or "" | 92 lang = e.getAttribute("lang") or "" |
93 | 93 |
94 try: | 94 try: |
95 entity_type = self.host.memory.getEntityData( | 95 entity_type = self.host.memory.entity_data_get( |
96 client, from_jid, [C.ENTITY_TYPE])[C.ENTITY_TYPE] | 96 client, from_jid, [C.ENTITY_TYPE])[C.ENTITY_TYPE] |
97 except (UnknownEntityError, KeyError): | 97 except (UnknownEntityError, KeyError): |
98 entity_type = "contact" | 98 entity_type = "contact" |
99 if entity_type == C.ENTITY_TYPE_MUC: | 99 if entity_type == C.ENTITY_TYPE_MUC: |
100 src_txt = from_jid.resource | 100 src_txt = from_jid.resource |
101 if src_txt == self.host.plugins["XEP-0045"].getRoomNick( | 101 if src_txt == self.host.plugins["XEP-0045"].get_room_nick( |
102 client, from_jid.userhostJID() | 102 client, from_jid.userhostJID() |
103 ): | 103 ): |
104 # we won't repeat our own messages | 104 # we won't repeat our own messages |
105 return True | 105 return True |
106 else: | 106 else: |
113 jid.JID(str(linked)), message, None, "auto", no_trigger=True | 113 jid.JID(str(linked)), message, None, "auto", no_trigger=True |
114 ) | 114 ) |
115 | 115 |
116 return True | 116 return True |
117 | 117 |
118 def addParrot(self, client, source_jid, dest_jid): | 118 def add_parrot(self, client, source_jid, dest_jid): |
119 """Add a parrot link from one entity to another one | 119 """Add a parrot link from one entity to another one |
120 | 120 |
121 @param source_jid: entity from who messages will be repeated | 121 @param source_jid: entity from who messages will be repeated |
122 @param dest_jid: entity where the messages will be repeated | 122 @param dest_jid: entity where the messages will be repeated |
123 """ | 123 """ |
130 log.info( | 130 log.info( |
131 "Parrot mode: %s will be repeated to %s" | 131 "Parrot mode: %s will be repeated to %s" |
132 % (source_jid.userhost(), str(dest_jid)) | 132 % (source_jid.userhost(), str(dest_jid)) |
133 ) | 133 ) |
134 | 134 |
135 def removeParrot(self, client, source_jid): | 135 def remove_parrot(self, client, source_jid): |
136 """Remove parrot link | 136 """Remove parrot link |
137 | 137 |
138 @param source_jid: this entity will no more be repeated | 138 @param source_jid: this entity will no more be repeated |
139 """ | 139 """ |
140 try: | 140 try: |
150 try: | 150 try: |
151 link_left_jid = jid.JID(mess_data["unparsed"].strip()) | 151 link_left_jid = jid.JID(mess_data["unparsed"].strip()) |
152 if not link_left_jid.user or not link_left_jid.host: | 152 if not link_left_jid.user or not link_left_jid.host: |
153 raise jid.InvalidFormat | 153 raise jid.InvalidFormat |
154 except (RuntimeError, jid.InvalidFormat, AttributeError): | 154 except (RuntimeError, jid.InvalidFormat, AttributeError): |
155 txt_cmd.feedBack( | 155 txt_cmd.feed_back( |
156 client, "Can't activate Parrot mode for invalid jid", mess_data | 156 client, "Can't activate Parrot mode for invalid jid", mess_data |
157 ) | 157 ) |
158 return False | 158 return False |
159 | 159 |
160 link_right_jid = mess_data["to"] | 160 link_right_jid = mess_data["to"] |
161 | 161 |
162 self.addParrot(client, link_left_jid, link_right_jid) | 162 self.add_parrot(client, link_left_jid, link_right_jid) |
163 self.addParrot(client, link_right_jid, link_left_jid) | 163 self.add_parrot(client, link_right_jid, link_left_jid) |
164 | 164 |
165 txt_cmd.feedBack( | 165 txt_cmd.feed_back( |
166 client, | 166 client, |
167 "Parrot mode activated for {}".format(str(link_left_jid)), | 167 "Parrot mode activated for {}".format(str(link_left_jid)), |
168 mess_data, | 168 mess_data, |
169 ) | 169 ) |
170 | 170 |
178 try: | 178 try: |
179 link_left_jid = jid.JID(mess_data["unparsed"].strip()) | 179 link_left_jid = jid.JID(mess_data["unparsed"].strip()) |
180 if not link_left_jid.user or not link_left_jid.host: | 180 if not link_left_jid.user or not link_left_jid.host: |
181 raise jid.InvalidFormat | 181 raise jid.InvalidFormat |
182 except jid.InvalidFormat: | 182 except jid.InvalidFormat: |
183 txt_cmd.feedBack( | 183 txt_cmd.feed_back( |
184 client, "Can't deactivate Parrot mode for invalid jid", mess_data | 184 client, "Can't deactivate Parrot mode for invalid jid", mess_data |
185 ) | 185 ) |
186 return False | 186 return False |
187 | 187 |
188 link_right_jid = mess_data["to"] | 188 link_right_jid = mess_data["to"] |
189 | 189 |
190 self.removeParrot(client, link_left_jid) | 190 self.remove_parrot(client, link_left_jid) |
191 self.removeParrot(client, link_right_jid) | 191 self.remove_parrot(client, link_right_jid) |
192 | 192 |
193 txt_cmd.feedBack( | 193 txt_cmd.feed_back( |
194 client, | 194 client, |
195 "Parrot mode deactivated for {} and {}".format( | 195 "Parrot mode deactivated for {} and {}".format( |
196 str(link_left_jid), str(link_right_jid) | 196 str(link_left_jid), str(link_right_jid) |
197 ), | 197 ), |
198 mess_data, | 198 mess_data, |