comparison src/plugins/plugin_xep_0045.py @ 801:02ee9ef95277

plugin XEP-0045, primitivus: added MUC configuration
author Goffi <goffi@goffi.org>
date Tue, 04 Feb 2014 18:06:12 +0100
parents 23b0c949b86c
children 1fe00f0c9a91
comparison
equal deleted inserted replaced
800:e0770d977d58 801:02ee9ef95277
21 from logging import debug, info, warning, error 21 from logging import debug, info, warning, error
22 from twisted.internet import defer 22 from twisted.internet import defer
23 from twisted.words.protocols.jabber import jid 23 from twisted.words.protocols.jabber import jid
24 24
25 from sat.core import exceptions 25 from sat.core import exceptions
26 from sat.memory import memory
26 27
27 import uuid 28 import uuid
28 29
29 from wokkel import muc 30 from wokkel import muc
31 from sat.tools import xml_tools
30 32
31 33
32 try: 34 try:
33 from twisted.words.protocols.xmlstream import XMPPHandler 35 from twisted.words.protocols.xmlstream import XMPPHandler
34 except ImportError: 36 except ImportError:
54 56
55 def __init__(self, host): 57 def __init__(self, host):
56 info(_("Plugin XEP_0045 initialization")) 58 info(_("Plugin XEP_0045 initialization"))
57 self.host = host 59 self.host = host
58 self.clients = {} 60 self.clients = {}
61 self._sessions = memory.Sessions()
59 host.bridge.addMethod("joinMUC", ".plugin", in_sign='ssa{ss}s', out_sign='s', method=self._join) 62 host.bridge.addMethod("joinMUC", ".plugin", in_sign='ssa{ss}s', out_sign='s', method=self._join)
60 host.bridge.addMethod("mucNick", ".plugin", in_sign='sss', out_sign='', method=self.mucNick) 63 host.bridge.addMethod("mucNick", ".plugin", in_sign='sss', out_sign='', method=self.mucNick)
61 host.bridge.addMethod("mucLeave", ".plugin", in_sign='ss', out_sign='', method=self.mucLeave, async = True) 64 host.bridge.addMethod("mucLeave", ".plugin", in_sign='ss', out_sign='', method=self.mucLeave, async=True)
62 host.bridge.addMethod("getRoomsJoined", ".plugin", in_sign='s', out_sign='a(sass)', method=self.getRoomsJoined) 65 host.bridge.addMethod("getRoomsJoined", ".plugin", in_sign='s', out_sign='a(sass)', method=self.getRoomsJoined)
63 host.bridge.addMethod("getRoomsSubjects", ".plugin", in_sign='s', out_sign='a(ss)', method=self.getRoomsSubjects) 66 host.bridge.addMethod("getRoomsSubjects", ".plugin", in_sign='s', out_sign='a(ss)', method=self.getRoomsSubjects)
64 host.bridge.addMethod("getUniqueRoomName", ".plugin", in_sign='ss', out_sign='s', method=self.getUniqueName) 67 host.bridge.addMethod("getUniqueRoomName", ".plugin", in_sign='ss', out_sign='s', method=self.getUniqueName)
68 host.bridge.addMethod("configureRoom", ".plugin", in_sign='ss', out_sign='s', method=self._configureRoom, async=True)
65 host.bridge.addSignal("roomJoined", ".plugin", signature='sasss') # args: room_jid, room_nicks, user_nick, profile 69 host.bridge.addSignal("roomJoined", ".plugin", signature='sasss') # args: room_jid, room_nicks, user_nick, profile
66 host.bridge.addSignal("roomLeft", ".plugin", signature='ss') # args: room_jid, profile 70 host.bridge.addSignal("roomLeft", ".plugin", signature='ss') # args: room_jid, profile
67 host.bridge.addSignal("roomUserJoined", ".plugin", signature='ssa{ss}s') # args: room_jid, user_nick, user_data, profile 71 host.bridge.addSignal("roomUserJoined", ".plugin", signature='ssa{ss}s') # args: room_jid, user_nick, user_data, profile
68 host.bridge.addSignal("roomUserLeft", ".plugin", signature='ssa{ss}s') # args: room_jid, user_nick, user_data, profile 72 host.bridge.addSignal("roomUserLeft", ".plugin", signature='ssa{ss}s') # args: room_jid, user_nick, user_data, profile
69 host.bridge.addSignal("roomUserChangedNick", ".plugin", signature='ssss') # args: room_jid, old_nick, new_nick, profile 73 host.bridge.addSignal("roomUserChangedNick", ".plugin", signature='ssss') # args: room_jid, old_nick, new_nick, profile
70 host.bridge.addSignal("roomNewSubject", ".plugin", signature='sss') # args: room_jid, subject, profile 74 host.bridge.addSignal("roomNewSubject", ".plugin", signature='sss') # args: room_jid, subject, profile
75 self.__submit_conf_id = host.registerCallback(self._submitConfiguration, with_data=True)
71 76
72 def __check_profile(self, profile): 77 def __check_profile(self, profile):
73 """check if profile is used and connected 78 """check if profile is used and connected
74 if profile known but disconnected, remove it from known profiles 79 if profile known but disconnected, remove it from known profiles
75 @param profile: profile to check 80 @param profile: profile to check
166 if nick is None: 171 if nick is None:
167 missing.append(user) 172 missing.append(user)
168 else: 173 else:
169 nicks.append(nick) 174 nicks.append(nick)
170 return nicks, missing 175 return nicks, missing
176
177 def _configureRoom(self, room_jid_s, profile_key='@NONE@'):
178 d = self.configureRoom(jid.JID(room_jid_s), profile_key)
179 d.addCallback(lambda xmlui: xmlui.toXml())
180 return d
181
182 def configureRoom(self, room_jid, profile_key='@NONE@'):
183 """ return the room configuration form
184 @param room: jid of the room to configure
185 @param profile_key: %(doc_profile_key)s
186 @return: configuration form as XMLUI
187
188 """
189 profile = self.host.memory.getProfileName(profile_key)
190 if not self.__check_profile(profile):
191 raise exceptions.ProfileUnknownError("Unknown or disconnected profile")
192 if room_jid.userhost() not in self.clients[profile].joined_rooms:
193 raise UnknownRoom("This room has not been joined")
194
195 def config2XMLUI(result):
196 if not result:
197 return ""
198 session_id, session_data = self._sessions.newSession(profile=profile)
199 session_data["room_jid"] = room_jid
200 xmlui = xml_tools.dataForm2XMLUI(result, submit_id=self.__submit_conf_id)
201 xmlui.session_id = session_id
202 return xmlui
203
204 d = self.clients[profile].getConfiguration(room_jid)
205 d.addCallback(config2XMLUI)
206 return d
207
208 def _submitConfiguration(self, raw_data, profile):
209 try:
210 session_data = self._sessions.profileGet(raw_data["session_id"], profile)
211 except KeyError:
212 warning ("session id doesn't exist, session has probably expired")
213 # TODO: send error dialog
214 return defer.succeed({})
215
216 data = xml_tools.XMLUIResult2DataFormResult(raw_data)
217 d = self.clients[profile].configure(session_data['room_jid'], data)
218 d.addCallback(lambda ignore: {})
219 del self._sessions[raw_data["session_id"]]
220 return d
171 221
172 def isNickInRoom(self, room_jid, nick, profile): 222 def isNickInRoom(self, room_jid, nick, profile):
173 """Tell if a nick is currently present in a room""" 223 """Tell if a nick is currently present in a room"""
174 profile = self.host.memory.getProfileName(profile) 224 profile = self.host.memory.getProfileName(profile)
175 if not self.__check_profile(profile): 225 if not self.__check_profile(profile):