Mercurial > libervia-backend
comparison sat/plugins/plugin_xep_0249.py @ 2624:56f94936df1e
code style reformatting using black
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 27 Jun 2018 20:14:46 +0200 |
parents | 26edcf3a30eb |
children | 003b8b4b56a7 |
comparison
equal
deleted
inserted
replaced
2623:49533de4540b | 2624:56f94936df1e |
---|---|
19 | 19 |
20 from sat.core.i18n import _, D_ | 20 from sat.core.i18n import _, D_ |
21 from sat.core.constants import Const as C | 21 from sat.core.constants import Const as C |
22 from sat.core import exceptions | 22 from sat.core import exceptions |
23 from sat.core.log import getLogger | 23 from sat.core.log import getLogger |
24 | |
24 log = getLogger(__name__) | 25 log = getLogger(__name__) |
25 from sat.tools import xml_tools | 26 from sat.tools import xml_tools |
26 from twisted.words.xish import domish | 27 from twisted.words.xish import domish |
27 from twisted.words.protocols.jabber import jid | 28 from twisted.words.protocols.jabber import jid |
28 | 29 |
34 try: | 35 try: |
35 from twisted.words.protocols.xmlstream import XMPPHandler | 36 from twisted.words.protocols.xmlstream import XMPPHandler |
36 except ImportError: | 37 except ImportError: |
37 from wokkel.subprotocols import XMPPHandler | 38 from wokkel.subprotocols import XMPPHandler |
38 | 39 |
39 MESSAGE = '/message' | 40 MESSAGE = "/message" |
40 NS_DIRECT_MUC_INVITATION = 'jabber:x:conference' | 41 NS_DIRECT_MUC_INVITATION = "jabber:x:conference" |
41 DIRECT_MUC_INVITATION_REQUEST = MESSAGE + '/x[@xmlns="' + NS_DIRECT_MUC_INVITATION + '"]' | 42 DIRECT_MUC_INVITATION_REQUEST = MESSAGE + '/x[@xmlns="' + NS_DIRECT_MUC_INVITATION + '"]' |
42 AUTOJOIN_KEY = "Misc" | 43 AUTOJOIN_KEY = "Misc" |
43 AUTOJOIN_NAME = "Auto-join MUC on invitation" | 44 AUTOJOIN_NAME = "Auto-join MUC on invitation" |
44 AUTOJOIN_VALUES = ["ask", "always", "never"] | 45 AUTOJOIN_VALUES = ["ask", "always", "never"] |
45 | 46 |
50 C.PI_PROTOCOLS: ["XEP-0249"], | 51 C.PI_PROTOCOLS: ["XEP-0249"], |
51 C.PI_DEPENDENCIES: ["XEP-0045"], | 52 C.PI_DEPENDENCIES: ["XEP-0045"], |
52 C.PI_RECOMMENDATIONS: [C.TEXT_CMDS], | 53 C.PI_RECOMMENDATIONS: [C.TEXT_CMDS], |
53 C.PI_MAIN: "XEP_0249", | 54 C.PI_MAIN: "XEP_0249", |
54 C.PI_HANDLER: "yes", | 55 C.PI_HANDLER: "yes", |
55 C.PI_DESCRIPTION: _("""Implementation of Direct MUC Invitations""") | 56 C.PI_DESCRIPTION: _("""Implementation of Direct MUC Invitations"""), |
56 } | 57 } |
57 | 58 |
58 | 59 |
59 class XEP_0249(object): | 60 class XEP_0249(object): |
60 | 61 |
67 </param> | 68 </param> |
68 </category> | 69 </category> |
69 </individual> | 70 </individual> |
70 </params> | 71 </params> |
71 """ % { | 72 """ % { |
72 'category_name': AUTOJOIN_KEY, | 73 "category_name": AUTOJOIN_KEY, |
73 'category_label': _("Misc"), | 74 "category_label": _("Misc"), |
74 'param_name': AUTOJOIN_NAME, | 75 "param_name": AUTOJOIN_NAME, |
75 'param_label': _("Auto-join MUC on invitation"), | 76 "param_label": _("Auto-join MUC on invitation"), |
76 'param_options': '\n'.join(['<option value="%s" %s/>' % \ | 77 "param_options": "\n".join( |
77 (value, 'selected="true"' if value == AUTOJOIN_VALUES[0] else '') \ | 78 [ |
78 for value in AUTOJOIN_VALUES]) | 79 '<option value="%s" %s/>' |
80 % (value, 'selected="true"' if value == AUTOJOIN_VALUES[0] else "") | |
81 for value in AUTOJOIN_VALUES | |
82 ] | |
83 ), | |
79 } | 84 } |
80 | 85 |
81 def __init__(self, host): | 86 def __init__(self, host): |
82 log.info(_("Plugin XEP_0249 initialization")) | 87 log.info(_("Plugin XEP_0249 initialization")) |
83 self.host = host | 88 self.host = host |
84 host.memory.updateParams(self.params) | 89 host.memory.updateParams(self.params) |
85 host.bridge.addMethod("inviteMUC", ".plugin", in_sign='ssa{ss}s', out_sign='', method=self._invite) | 90 host.bridge.addMethod( |
91 "inviteMUC", ".plugin", in_sign="ssa{ss}s", out_sign="", method=self._invite | |
92 ) | |
86 try: | 93 try: |
87 self.host.plugins[C.TEXT_CMDS].registerTextCommands(self) | 94 self.host.plugins[C.TEXT_CMDS].registerTextCommands(self) |
88 except KeyError: | 95 except KeyError: |
89 log.info(_("Text commands not available")) | 96 log.info(_("Text commands not available")) |
90 | 97 |
97 @param guest_jid_s: jid of the user to invite | 104 @param guest_jid_s: jid of the user to invite |
98 @param service: jid of the MUC service | 105 @param service: jid of the MUC service |
99 @param roomId: name of the room | 106 @param roomId: name of the room |
100 @param profile_key: %(doc_profile_key)s | 107 @param profile_key: %(doc_profile_key)s |
101 """ | 108 """ |
102 #TODO: check parameters validity | 109 # TODO: check parameters validity |
103 client = self.host.getClient(profile_key) | 110 client = self.host.getClient(profile_key) |
104 self.invite(client, jid.JID(guest_jid_s), jid.JID(room_jid_s, options)) | 111 self.invite(client, jid.JID(guest_jid_s), jid.JID(room_jid_s, options)) |
105 | 112 |
106 def invite(self, client, guest, room, options={}): | 113 def invite(self, client, guest, room, options={}): |
107 """Invite a user to a room | 114 """Invite a user to a room |
108 | 115 |
109 @param guest(jid.JID): jid of the user to invite | 116 @param guest(jid.JID): jid of the user to invite |
110 @param room(jid.JID): jid of the room where the user is invited | 117 @param room(jid.JID): jid of the room where the user is invited |
111 @param options(dict): attribute with extra info (reason, password) as in #XEP-0249 | 118 @param options(dict): attribute with extra info (reason, password) as in #XEP-0249 |
112 """ | 119 """ |
113 message = domish.Element((None, 'message')) | 120 message = domish.Element((None, "message")) |
114 message["to"] = guest.full() | 121 message["to"] = guest.full() |
115 x_elt = message.addElement((NS_DIRECT_MUC_INVITATION, 'x')) | 122 x_elt = message.addElement((NS_DIRECT_MUC_INVITATION, "x")) |
116 x_elt['jid'] = room.userhost() | 123 x_elt["jid"] = room.userhost() |
117 for key, value in options.iteritems(): | 124 for key, value in options.iteritems(): |
118 if key not in ('password', 'reason', 'thread'): | 125 if key not in ("password", "reason", "thread"): |
119 log.warning(u"Ignoring invalid invite option: {}".format(key)) | 126 log.warning(u"Ignoring invalid invite option: {}".format(key)) |
120 continue | 127 continue |
121 x_elt[key] = value | 128 x_elt[key] = value |
122 # there is not body in this message, so we can use directly send() | 129 # there is not body in this message, so we can use directly send() |
123 client.send(message) | 130 client.send(message) |
124 | 131 |
125 def _accept(self, room_jid, profile_key=C.PROF_KEY_NONE): | 132 def _accept(self, room_jid, profile_key=C.PROF_KEY_NONE): |
126 """Accept the invitation to join a MUC. | 133 """Accept the invitation to join a MUC. |
127 | 134 |
128 @param room (jid.JID): JID of the room | 135 @param room (jid.JID): JID of the room |
129 """ | 136 """ |
130 client = self.host.getClient(profile_key) | 137 client = self.host.getClient(profile_key) |
131 log.info(_(u'Invitation accepted for room %(room)s [%(profile)s]') % {'room': room_jid.userhost(), 'profile': client.profile}) | 138 log.info( |
139 _(u"Invitation accepted for room %(room)s [%(profile)s]") | |
140 % {"room": room_jid.userhost(), "profile": client.profile} | |
141 ) | |
132 d = self.host.plugins["XEP-0045"].join(client, room_jid, client.jid.user, {}) | 142 d = self.host.plugins["XEP-0045"].join(client, room_jid, client.jid.user, {}) |
133 return d | 143 return d |
134 | 144 |
135 def onInvitation(self, message, profile): | 145 def onInvitation(self, message, profile): |
136 """ | 146 """ |
138 @param message: message element | 148 @param message: message element |
139 @profile: %(doc_profile)s | 149 @profile: %(doc_profile)s |
140 """ | 150 """ |
141 client = self.host.getClient(profile) | 151 client = self.host.getClient(profile) |
142 try: | 152 try: |
143 room_jid_s = message.firstChildElement()['jid'] | 153 room_jid_s = message.firstChildElement()["jid"] |
144 log.info(_(u'Invitation received for room %(room)s [%(profile)s]') % {'room': room_jid_s, 'profile': profile}) | 154 log.info( |
155 _(u"Invitation received for room %(room)s [%(profile)s]") | |
156 % {"room": room_jid_s, "profile": profile} | |
157 ) | |
145 except: | 158 except: |
146 log.error(_('Error while parsing invitation')) | 159 log.error(_("Error while parsing invitation")) |
147 return | 160 return |
148 from_jid_s = message["from"] | 161 from_jid_s = message["from"] |
149 room_jid = jid.JID(room_jid_s) | 162 room_jid = jid.JID(room_jid_s) |
150 try: | 163 try: |
151 self.host.plugins["XEP-0045"].checkRoomJoined(client, room_jid) | 164 self.host.plugins["XEP-0045"].checkRoomJoined(client, room_jid) |
152 except exceptions.NotFound: | 165 except exceptions.NotFound: |
153 pass | 166 pass |
154 else: | 167 else: |
155 log.info(_(u"Invitation silently discarded because user is already in the room.")) | 168 log.info( |
169 _(u"Invitation silently discarded because user is already in the room.") | |
170 ) | |
156 return | 171 return |
157 | 172 |
158 autojoin = self.host.memory.getParamA(AUTOJOIN_NAME, AUTOJOIN_KEY, profile_key=profile) | 173 autojoin = self.host.memory.getParamA( |
174 AUTOJOIN_NAME, AUTOJOIN_KEY, profile_key=profile | |
175 ) | |
159 | 176 |
160 if autojoin == "always": | 177 if autojoin == "always": |
161 self._accept(room_jid, profile) | 178 self._accept(room_jid, profile) |
162 elif autojoin == "never": | 179 elif autojoin == "never": |
163 msg = D_("An invitation from %(user)s to join the room %(room)s has been declined according to your personal settings.") % {'user': from_jid_s, 'room': room_jid_s} | 180 msg = D_( |
181 "An invitation from %(user)s to join the room %(room)s has been declined according to your personal settings." | |
182 ) % {"user": from_jid_s, "room": room_jid_s} | |
164 title = D_("MUC invitation") | 183 title = D_("MUC invitation") |
165 xml_tools.quickNote(self.host, client, msg, title, C.XMLUI_DATA_LVL_INFO) | 184 xml_tools.quickNote(self.host, client, msg, title, C.XMLUI_DATA_LVL_INFO) |
166 else: # leave the default value here | 185 else: # leave the default value here |
167 confirm_msg = D_("You have been invited by %(user)s to join the room %(room)s. Do you accept?") % {'user': from_jid_s, 'room': room_jid_s} | 186 confirm_msg = D_( |
187 "You have been invited by %(user)s to join the room %(room)s. Do you accept?" | |
188 ) % {"user": from_jid_s, "room": room_jid_s} | |
168 confirm_title = D_("MUC invitation") | 189 confirm_title = D_("MUC invitation") |
169 d = xml_tools.deferConfirm(self.host, confirm_msg, confirm_title, profile=profile) | 190 d = xml_tools.deferConfirm( |
191 self.host, confirm_msg, confirm_title, profile=profile | |
192 ) | |
193 | |
170 def accept_cb(accepted): | 194 def accept_cb(accepted): |
171 if accepted: | 195 if accepted: |
172 self._accept(room_jid, profile) | 196 self._accept(room_jid, profile) |
173 | 197 |
174 d.addCallback(accept_cb) | 198 d.addCallback(accept_cb) |
182 contact_jid_s = mess_data["unparsed"].strip() | 206 contact_jid_s = mess_data["unparsed"].strip() |
183 my_host = client.jid.host | 207 my_host = client.jid.host |
184 try: | 208 try: |
185 contact_jid = jid.JID(contact_jid_s) | 209 contact_jid = jid.JID(contact_jid_s) |
186 except (RuntimeError, jid.InvalidFormat, AttributeError): | 210 except (RuntimeError, jid.InvalidFormat, AttributeError): |
187 feedback = _(u"You must provide a valid JID to invite, like in '/invite contact@{host}'").format(host=my_host) | 211 feedback = _( |
212 u"You must provide a valid JID to invite, like in '/invite contact@{host}'" | |
213 ).format(host=my_host) | |
188 self.host.plugins[C.TEXT_CMDS].feedBack(client, feedback, mess_data) | 214 self.host.plugins[C.TEXT_CMDS].feedBack(client, feedback, mess_data) |
189 return False | 215 return False |
190 if not contact_jid.user: | 216 if not contact_jid.user: |
191 contact_jid.user, contact_jid.host = contact_jid.host, my_host | 217 contact_jid.user, contact_jid.host = contact_jid.host, my_host |
192 self.invite(client, contact_jid, mess_data["to"]) | 218 self.invite(client, contact_jid, mess_data["to"]) |
199 def __init__(self, plugin_parent): | 225 def __init__(self, plugin_parent): |
200 self.plugin_parent = plugin_parent | 226 self.plugin_parent = plugin_parent |
201 self.host = plugin_parent.host | 227 self.host = plugin_parent.host |
202 | 228 |
203 def connectionInitialized(self): | 229 def connectionInitialized(self): |
204 self.xmlstream.addObserver(DIRECT_MUC_INVITATION_REQUEST, self.plugin_parent.onInvitation, profile=self.parent.profile) | 230 self.xmlstream.addObserver( |
205 | 231 DIRECT_MUC_INVITATION_REQUEST, |
206 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): | 232 self.plugin_parent.onInvitation, |
233 profile=self.parent.profile, | |
234 ) | |
235 | |
236 def getDiscoInfo(self, requestor, target, nodeIdentifier=""): | |
207 return [disco.DiscoFeature(NS_DIRECT_MUC_INVITATION)] | 237 return [disco.DiscoFeature(NS_DIRECT_MUC_INVITATION)] |
208 | 238 |
209 def getDiscoItems(self, requestor, target, nodeIdentifier=''): | 239 def getDiscoItems(self, requestor, target, nodeIdentifier=""): |
210 return [] | 240 return [] |