Mercurial > libervia-backend
comparison src/plugins/plugin_xep_0249.py @ 2045:48536a22b599
plugin XEP-0249: some modernisation, don't use anymore deprecated methods, invite bridge method use a single param for room jid
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 28 Aug 2016 18:18:10 +0200 |
parents | a5eb6f6b8db2 |
children | 6a66c8c5a567 |
comparison
equal
deleted
inserted
replaced
2044:4de202bdde05 | 2045:48536a22b599 |
---|---|
15 # GNU Affero General Public License for more details. | 15 # GNU Affero General Public License for more details. |
16 | 16 |
17 # You should have received a copy of the GNU Affero General Public License | 17 # You should have received a copy of the GNU Affero General Public License |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | 19 |
20 from sat.core.i18n import _ | 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 log = getLogger(__name__) | 24 log = getLogger(__name__) |
25 from sat.tools import xml_tools | |
25 from twisted.words.xish import domish | 26 from twisted.words.xish import domish |
26 from twisted.words.protocols.jabber import jid | 27 from twisted.words.protocols.jabber import jid |
27 | 28 |
28 from zope.interface import implements | 29 from zope.interface import implements |
29 | 30 |
79 | 80 |
80 def __init__(self, host): | 81 def __init__(self, host): |
81 log.info(_("Plugin XEP_0249 initialization")) | 82 log.info(_("Plugin XEP_0249 initialization")) |
82 self.host = host | 83 self.host = host |
83 host.memory.updateParams(self.params) | 84 host.memory.updateParams(self.params) |
84 host.bridge.addMethod("inviteMUC", ".plugin", in_sign='sssa{ss}s', out_sign='', method=self._invite) | 85 host.bridge.addMethod("inviteMUC", ".plugin", in_sign='ssa{ss}s', out_sign='', method=self._invite) |
85 try: | 86 try: |
86 self.host.plugins[C.TEXT_CMDS].registerTextCommands(self) | 87 self.host.plugins[C.TEXT_CMDS].registerTextCommands(self) |
87 except KeyError: | 88 except KeyError: |
88 log.info(_("Text commands not available")) | 89 log.info(_("Text commands not available")) |
89 | 90 |
90 def getHandler(self, profile): | 91 def getHandler(self, profile): |
91 return XEP_0249_handler(self) | 92 return XEP_0249_handler(self) |
92 | 93 |
93 def invite(self, target, room, options={}, profile_key=C.PROF_KEY_NONE): | 94 def _invite(self, guest_jid_s, room_jid_s, options, profile_key): |
94 """ | 95 """Invite an user to a room |
95 Invite a user to a room | 96 |
96 @param target: jid of the user to invite | 97 @param guest_jid_s: jid of the user to invite |
97 @param room: jid of the room where the user is invited | |
98 @options: attribute with extra info (reason, password) as in #XEP-0249 | |
99 @profile_key: %(doc_profile_key)s | |
100 """ | |
101 profile = self.host.memory.getProfileName(profile_key) | |
102 if not profile: | |
103 log.error(_("Profile doesn't exists !")) | |
104 return | |
105 message = domish.Element((None, 'message')) | |
106 message["to"] = target.full() | |
107 x_elt = message.addElement((NS_DIRECT_MUC_INVITATION, 'x')) | |
108 x_elt['jid'] = room.userhost() | |
109 for opt in options: | |
110 x_elt[opt] = options[opt] | |
111 self.host.profiles[profile].xmlstream.send(message) | |
112 | |
113 def _invite(self, target, service, roomId, options={}, profile_key=C.PROF_KEY_NONE): | |
114 """ | |
115 Invite an user to a room | |
116 @param target: jid of the user to invite | |
117 @param service: jid of the MUC service | 98 @param service: jid of the MUC service |
118 @param roomId: name of the room | 99 @param roomId: name of the room |
119 @param profile_key: %(doc_profile_key)s | 100 @param profile_key: %(doc_profile_key)s |
120 """ | 101 """ |
121 #TODO: check parameters validity | 102 #TODO: check parameters validity |
122 self.invite(jid.JID(target), jid.JID("%s@%s" % (roomId, service)), options, profile_key) | 103 client = self.host.getClient(profile_key) |
104 self.invite(client, jid.JID(guest_jid_s), jid.JID(room_jid_s, options)) | |
105 | |
106 def invite(self, client, guest, room, options={}): | |
107 """Invite a user to a room | |
108 | |
109 @param guest(jid.JID): jid of the user to invite | |
110 @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 | |
112 """ | |
113 message = domish.Element((None, 'message')) | |
114 message["to"] = guest.full() | |
115 x_elt = message.addElement((NS_DIRECT_MUC_INVITATION, 'x')) | |
116 x_elt['jid'] = room.userhost() | |
117 for key, value in options.iteritems(): | |
118 if key not in ('password', 'reason', 'thread'): | |
119 log.warning(u"Ignoring invalid invite option: {}".format(key)) | |
120 continue | |
121 x_elt[key] = value | |
122 client.xmlstream.send(message) | |
123 | 123 |
124 def _accept(self, room_jid, profile_key=C.PROF_KEY_NONE): | 124 def _accept(self, room_jid, profile_key=C.PROF_KEY_NONE): |
125 """Accept the invitation to join a MUC. | 125 """Accept the invitation to join a MUC. |
126 | 126 |
127 @param room (jid.JID): JID of the room | 127 @param room (jid.JID): JID of the room |
154 log.info(_(u"Invitation silently discarded because user is already in the room.")) | 154 log.info(_(u"Invitation silently discarded because user is already in the room.")) |
155 return | 155 return |
156 | 156 |
157 autojoin = self.host.memory.getParamA(AUTOJOIN_NAME, AUTOJOIN_KEY, profile_key=profile) | 157 autojoin = self.host.memory.getParamA(AUTOJOIN_NAME, AUTOJOIN_KEY, profile_key=profile) |
158 | 158 |
159 def accept_cb(conf_id, accepted, data, profile): | |
160 if conf_id == room_jid_s and accepted: | |
161 self._accept(room_jid, profile) | |
162 | |
163 if autojoin == "always": | 159 if autojoin == "always": |
164 self._accept(room_jid, profile) | 160 self._accept(room_jid, profile) |
165 elif autojoin == "never": | 161 elif autojoin == "never": |
166 self.host.bridge.newAlert(_("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}, _("MUC invitation"), "INFO", profile) | 162 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} |
163 title = D_("MUC invitation") | |
164 xml_tools.quickNote(self.host, client, msg, title, C.XMLUI_DATA_LVL_INFO) | |
167 else: # leave the default value here | 165 else: # leave the default value here |
168 data = {"message": _("You have been invited by %(user)s to join the room %(room)s. Do you accept?") % {'user': from_jid_s, 'room': room_jid_s}, "title": _("MUC invitation")} | 166 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} |
169 self.host.askConfirmation(room_jid_s, "YES/NO", data, accept_cb, profile) | 167 confirm_title = D_("MUC invitation") |
168 d = xml_tools.deferConfirm(self.host, confirm_msg, confirm_title, profile=profile) | |
169 def accept_cb(accepted): | |
170 if accepted: | |
171 self._accept(room_jid, profile) | |
172 | |
173 d.addCallback(accept_cb) | |
170 | 174 |
171 def cmd_invite(self, client, mess_data): | 175 def cmd_invite(self, client, mess_data): |
172 """invite someone in the room | 176 """invite someone in the room |
173 | 177 |
174 @command (group): JID | 178 @command (group): JID |
182 feedback = _(u"You must provide a valid JID to invite, like in '/invite contact@{host}'").format(host=my_host) | 186 feedback = _(u"You must provide a valid JID to invite, like in '/invite contact@{host}'").format(host=my_host) |
183 self.host.plugins[C.TEXT_CMDS].feedBack(client, feedback, mess_data) | 187 self.host.plugins[C.TEXT_CMDS].feedBack(client, feedback, mess_data) |
184 return False | 188 return False |
185 if not contact_jid.user: | 189 if not contact_jid.user: |
186 contact_jid.user, contact_jid.host = contact_jid.host, my_host | 190 contact_jid.user, contact_jid.host = contact_jid.host, my_host |
187 self.invite(contact_jid, mess_data["to"], {}, client.profile) | 191 self.invite(client, contact_jid, mess_data["to"]) |
188 return False | 192 return False |
189 | 193 |
190 | 194 |
191 class XEP_0249_handler(XMPPHandler): | 195 class XEP_0249_handler(XMPPHandler): |
192 implements(iwokkel.IDisco) | 196 implements(iwokkel.IDisco) |