Mercurial > libervia-backend
comparison src/plugins/plugin_exp_parrot.py @ 1963:a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
/!\ several features are temporarily disabled, like notifications in frontends
next step in refactoring, with the following changes:
- jp: updated jp message to follow changes in backend/bridge
- jp: added --lang, --subject, --subject_lang, and --type options to jp message + fixed unicode handling for jid
- quick_frontend (QuickApp, QuickChat):
- follow backend changes
- refactored chat, message are now handled in OrderedDict and uid are kept so they can be updated
- Message and Occupant classes handle metadata, so frontend just have to display them
- Primitivus (Chat):
- follow backend/QuickFrontend changes
- info & standard messages are handled in the same MessageWidget class
- improved/simplified handling of messages, removed update() method
- user joined/left messages are merged when next to each other
- a separator is shown when message is received while widget is out of focus, so user can quickly see the new messages
- affiliation/role are shown (in a basic way for now) in occupants panel
- removed "/me" messages handling, as it will be done by a backend plugin
- message language is displayed when available (only one language per message for now)
- fixed :history and :search commands
- core (constants): new constants for messages type, XML namespace, entity type
- core: *Message methods renamed to follow new code sytle (e.g. sendMessageToBridge => messageSendToBridge)
- core (messages handling): fixed handling of language
- core (messages handling): mes_data['from'] and ['to'] are now jid.JID
- core (core.xmpp): reorganised message methods, added getNick() method to client.roster
- plugin text commands: fixed plugin and adapted to new messages behaviour. client is now used in arguments instead of profile
- plugins: added information for cancellation reason in CancelError calls
- plugin XEP-0045: various improvments, but this plugin still need work:
- trigger is used to avoid message already handled by the plugin to be handled a second time
- changed the way to handle history, the last message from DB is checked and we request only messages since this one, in seconds (thanks Poezio folks :))
- subject reception is waited before sending the roomJoined signal, this way we are sure that everything including history is ready
- cmd_* method now follow the new convention with client instead of profile
- roomUserJoined and roomUserLeft messages are removed, the events are now handled with info message with a "ROOM_USER_JOINED" info subtype
- probably other forgotten stuffs :p
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 20 Jun 2016 18:41:53 +0200 |
parents | 633b5c21aefd |
children | 200cd707a46d |
comparison
equal
deleted
inserted
replaced
1962:a45235d8dc93 | 1963:a2bc5089c2eb |
---|---|
65 # | 65 # |
66 # if mess_data['to'].userhostJID() in _links.values(): | 66 # if mess_data['to'].userhostJID() in _links.values(): |
67 # log.debug("Parrot link detected, skipping other triggers") | 67 # log.debug("Parrot link detected, skipping other triggers") |
68 # raise trigger.SkipOtherTriggers | 68 # raise trigger.SkipOtherTriggers |
69 | 69 |
70 def MessageReceivedTrigger(self, client, message, post_treat): | 70 def MessageReceivedTrigger(self, client, message_elt, post_treat): |
71 """ Check if source is linked and repeat message, else do nothing """ | 71 """ Check if source is linked and repeat message, else do nothing """ |
72 # TODO: many things are not repeated (subject, thread, etc) | 72 # TODO: many things are not repeated (subject, thread, etc) |
73 profile = client.profile | 73 profile = client.profile |
74 client = self.host.getClient(profile) | 74 client = self.host.getClient(profile) |
75 from_jid = jid.JID(message["from"]) | 75 from_jid = message_elt["from"] |
76 | 76 |
77 try: | 77 try: |
78 _links = client.parrot_links | 78 _links = client.parrot_links |
79 except AttributeError: | 79 except AttributeError: |
80 return True | 80 return True |
81 | 81 |
82 if not from_jid.userhostJID() in _links: | 82 if not from_jid.userhostJID() in _links: |
83 return True | 83 return True |
84 | 84 |
85 message = {} | 85 message = {} |
86 for e in message.elements(C.NS_CLIENT, 'body'): | 86 for e in message_elt.elements(C.NS_CLIENT, 'body'): |
87 body = unicode(e) | 87 body = unicode(e) |
88 lang = e.getAttribute('lang') or '' | 88 lang = e.getAttribute('lang') or '' |
89 | 89 |
90 try: | 90 try: |
91 entity_type = self.host.memory.getEntityData(from_jid, ['type'], profile)["type"] | 91 entity_type = self.host.memory.getEntityData(from_jid, ['type'], profile)["type"] |
104 | 104 |
105 self.host.messageSend(jid.JID(unicode(linked)), message, None, "auto", no_trigger=True, profile_key=profile) | 105 self.host.messageSend(jid.JID(unicode(linked)), message, None, "auto", no_trigger=True, profile_key=profile) |
106 | 106 |
107 return True | 107 return True |
108 | 108 |
109 def addParrot(self, source_jid, dest_jid, profile): | 109 def addParrot(self, client, source_jid, dest_jid): |
110 """Add a parrot link from one entity to another one | 110 """Add a parrot link from one entity to another one |
111 | |
111 @param source_jid: entity from who messages will be repeated | 112 @param source_jid: entity from who messages will be repeated |
112 @param dest_jid: entity where the messages will be repeated | 113 @param dest_jid: entity where the messages will be repeated |
113 @param profile: %(doc_profile_key)s""" | 114 """ |
114 client = self.host.getClient(profile) | |
115 try: | 115 try: |
116 _links = client.parrot_links | 116 _links = client.parrot_links |
117 except AttributeError: | 117 except AttributeError: |
118 _links = client.parrot_links = {} | 118 _links = client.parrot_links = {} |
119 | 119 |
120 _links[source_jid.userhostJID()] = dest_jid | 120 _links[source_jid.userhostJID()] = dest_jid |
121 log.info(u"Parrot mode: %s will be repeated to %s" % (source_jid.userhost(), unicode(dest_jid))) | 121 log.info(u"Parrot mode: %s will be repeated to %s" % (source_jid.userhost(), unicode(dest_jid))) |
122 | 122 |
123 def removeParrot(self, source_jid, profile): | 123 def removeParrot(self, client, source_jid): |
124 """Remove parrot link | 124 """Remove parrot link |
125 | |
125 @param source_jid: this entity will no more be repeated | 126 @param source_jid: this entity will no more be repeated |
126 @param profile: %(doc_profile_key)s""" | 127 """ |
127 client = self.host.getClient(profile) | |
128 try: | 128 try: |
129 del client.parrot_links[source_jid.userhostJID()] | 129 del client.parrot_links[source_jid.userhostJID()] |
130 except (AttributeError, KeyError): | 130 except (AttributeError, KeyError): |
131 pass | 131 pass |
132 | 132 |
133 def cmd_parrot(self, mess_data, profile): | 133 def cmd_parrot(self, client, mess_data): |
134 """activate Parrot mode between 2 entities, in both directions.""" | 134 """activate Parrot mode between 2 entities, in both directions.""" |
135 log.debug("Catched parrot command") | 135 log.debug("Catched parrot command") |
136 txt_cmd = self.host.plugins[C.TEXT_CMDS] | 136 txt_cmd = self.host.plugins[C.TEXT_CMDS] |
137 | 137 |
138 try: | 138 try: |
139 link_left_jid = jid.JID(mess_data["unparsed"].strip()) | 139 link_left_jid = jid.JID(mess_data["unparsed"].strip()) |
140 if not link_left_jid.user or not link_left_jid.host: | 140 if not link_left_jid.user or not link_left_jid.host: |
141 raise jid.InvalidFormat | 141 raise jid.InvalidFormat |
142 except (RuntimeError, jid.InvalidFormat, AttributeError): | 142 except (RuntimeError, jid.InvalidFormat, AttributeError): |
143 txt_cmd.feedBack("Can't activate Parrot mode for invalid jid", mess_data, profile) | 143 txt_cmd.feedBack(client, "Can't activate Parrot mode for invalid jid", mess_data) |
144 return False | 144 return False |
145 | 145 |
146 link_right_jid = mess_data['to'] | 146 link_right_jid = mess_data['to'] |
147 | 147 |
148 self.addParrot(link_left_jid, link_right_jid, profile) | 148 self.addParrot(client, link_left_jid, link_right_jid) |
149 self.addParrot(link_right_jid, link_left_jid, profile) | 149 self.addParrot(client, link_right_jid, link_left_jid) |
150 | 150 |
151 txt_cmd.feedBack("Parrot mode activated for %s" % (unicode(link_left_jid), ), mess_data, profile) | 151 txt_cmd.feedBack(client, "Parrot mode activated for {}".format(unicode(link_left_jid)), mess_data) |
152 | 152 |
153 return False | 153 return False |
154 | 154 |
155 def cmd_unparrot(self, mess_data, profile): | 155 def cmd_unparrot(self, client, mess_data): |
156 """remove Parrot mode between 2 entities, in both directions.""" | 156 """remove Parrot mode between 2 entities, in both directions.""" |
157 log.debug("Catched unparrot command") | 157 log.debug("Catched unparrot command") |
158 txt_cmd = self.host.plugins[C.TEXT_CMDS] | 158 txt_cmd = self.host.plugins[C.TEXT_CMDS] |
159 | 159 |
160 try: | 160 try: |
161 link_left_jid = jid.JID(mess_data["unparsed"].strip()) | 161 link_left_jid = jid.JID(mess_data["unparsed"].strip()) |
162 if not link_left_jid.user or not link_left_jid.host: | 162 if not link_left_jid.user or not link_left_jid.host: |
163 raise jid.InvalidFormat | 163 raise jid.InvalidFormat |
164 except jid.InvalidFormat: | 164 except jid.InvalidFormat: |
165 txt_cmd.feedBack("Can't deactivate Parrot mode for invalid jid", mess_data, profile) | 165 txt_cmd.feedBack(client, u"Can't deactivate Parrot mode for invalid jid", mess_data) |
166 return False | 166 return False |
167 | 167 |
168 link_right_jid = mess_data['to'] | 168 link_right_jid = mess_data['to'] |
169 | 169 |
170 self.removeParrot(link_left_jid, profile) | 170 self.removeParrot(client, link_left_jid) |
171 self.removeParrot(link_right_jid, profile) | 171 self.removeParrot(client, link_right_jid) |
172 | 172 |
173 txt_cmd.feedBack("Parrot mode deactivated for %s and %s" % (unicode(link_left_jid), unicode(link_right_jid)), mess_data, profile) | 173 txt_cmd.feedBack(client, u"Parrot mode deactivated for {} and {}".format(unicode(link_left_jid), unicode(link_right_jid)), mess_data) |
174 | 174 |
175 return False | 175 return False |