comparison src/plugins/plugin_xep_0045.py @ 594:e629371a28d3

Fix pep8 support in src/plugins.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 18 Jan 2013 17:55:35 +0100
parents beaf6bec2fcd
children 98a962d0b23c
comparison
equal deleted inserted replaced
593:70bae685d05c 594:e629371a28d3
34 from twisted.words.protocols.xmlstream import XMPPHandler 34 from twisted.words.protocols.xmlstream import XMPPHandler
35 except ImportError: 35 except ImportError:
36 from wokkel.subprotocols import XMPPHandler 36 from wokkel.subprotocols import XMPPHandler
37 37
38 PLUGIN_INFO = { 38 PLUGIN_INFO = {
39 "name": "XEP 0045 Plugin", 39 "name": "XEP 0045 Plugin",
40 "import_name": "XEP-0045", 40 "import_name": "XEP-0045",
41 "type": "XEP", 41 "type": "XEP",
42 "protocols": ["XEP-0045"], 42 "protocols": ["XEP-0045"],
43 "dependencies": [], 43 "dependencies": [],
44 "main": "XEP_0045", 44 "main": "XEP_0045",
45 "handler": "yes", 45 "handler": "yes",
46 "description": _("""Implementation of Multi-User Chat""") 46 "description": _("""Implementation of Multi-User Chat""")
47 } 47 }
48
48 49
49 class UnknownRoom(Exception): 50 class UnknownRoom(Exception):
50 pass 51 pass
52
51 53
52 class XEP_0045(object): 54 class XEP_0045(object):
53 55
54 def __init__(self, host): 56 def __init__(self, host):
55 info(_("Plugin XEP_0045 initialization")) 57 info(_("Plugin XEP_0045 initialization"))
56 self.host = host 58 self.host = host
57 self.clients={} 59 self.clients = {}
58 host.bridge.addMethod("joinMUC", ".plugin", in_sign='ssa{ss}s', out_sign='', method=self._join) 60 host.bridge.addMethod("joinMUC", ".plugin", in_sign='ssa{ss}s', out_sign='', method=self._join)
59 host.bridge.addMethod("mucNick", ".plugin", in_sign='sss', out_sign='', method=self.mucNick) 61 host.bridge.addMethod("mucNick", ".plugin", in_sign='sss', out_sign='', method=self.mucNick)
60 host.bridge.addMethod("mucLeave", ".plugin", in_sign='ss', out_sign='', method=self.leave) 62 host.bridge.addMethod("mucLeave", ".plugin", in_sign='ss', out_sign='', method=self.leave)
61 host.bridge.addMethod("getRoomsJoined", ".plugin", in_sign='s', out_sign='a(sass)', method=self.getRoomsJoined) 63 host.bridge.addMethod("getRoomsJoined", ".plugin", in_sign='s', out_sign='a(sass)', method=self.getRoomsJoined)
62 host.bridge.addMethod("getRoomsSubjects", ".plugin", in_sign='s', out_sign='a(ss)', method=self.getRoomsSubjects) 64 host.bridge.addMethod("getRoomsSubjects", ".plugin", in_sign='s', out_sign='a(ss)', method=self.getRoomsSubjects)
63 host.bridge.addMethod("getUniqueRoomName", ".plugin", in_sign='s', out_sign='s', method=self.getUniqueName) 65 host.bridge.addMethod("getUniqueRoomName", ".plugin", in_sign='s', out_sign='s', method=self.getUniqueName)
64 host.bridge.addSignal("roomJoined", ".plugin", signature='sasss') #args: room_jid, room_nicks, user_nick, profile 66 host.bridge.addSignal("roomJoined", ".plugin", signature='sasss') # args: room_jid, room_nicks, user_nick, profile
65 host.bridge.addSignal("roomLeft", ".plugin", signature='ss') #args: room_jid, profile 67 host.bridge.addSignal("roomLeft", ".plugin", signature='ss') # args: room_jid, profile
66 host.bridge.addSignal("roomUserJoined", ".plugin", signature='ssa{ss}s') #args: room_jid, user_nick, user_data, profile 68 host.bridge.addSignal("roomUserJoined", ".plugin", signature='ssa{ss}s') # args: room_jid, user_nick, user_data, profile
67 host.bridge.addSignal("roomUserLeft", ".plugin", signature='ssa{ss}s') #args: room_jid, user_nick, user_data, profile 69 host.bridge.addSignal("roomUserLeft", ".plugin", signature='ssa{ss}s') # args: room_jid, user_nick, user_data, profile
68 host.bridge.addSignal("roomUserChangedNick", ".plugin", signature='ssss') #args: room_jid, old_nick, new_nick, profile 70 host.bridge.addSignal("roomUserChangedNick", ".plugin", signature='ssss') # args: room_jid, old_nick, new_nick, profile
69 host.bridge.addSignal("roomNewSubject", ".plugin", signature='sss') #args: room_jid, subject, profile 71 host.bridge.addSignal("roomNewSubject", ".plugin", signature='sss') # args: room_jid, subject, profile
70
71 72
72 def __check_profile(self, profile): 73 def __check_profile(self, profile):
73 """check if profile is used and connected 74 """check if profile is used and connected
74 if profile known but disconnected, remove it from known profiles 75 if profile known but disconnected, remove it from known profiles
75 @param profile: profile to check 76 @param profile: profile to check
76 @return: True if the profile is known and connected, else False""" 77 @return: True if the profile is known and connected, else False"""
77 if not profile or not self.clients.has_key(profile) or not self.host.isConnected(profile): 78 if not profile or profile not in self.clients or not self.host.isConnected(profile):
78 error (_('Unknown or disconnected profile (%s)') % profile) 79 error(_('Unknown or disconnected profile (%s)') % profile)
79 if self.clients.has_key(profile): 80 if profile in self.clients:
80 del self.clients[profile] 81 del self.clients[profile]
81 return False 82 return False
82 return True 83 return True
83 84
84 def __room_joined(self, room, profile): 85 def __room_joined(self, room, profile):
85 """Called when the user is in the requested room""" 86 """Called when the user is in the requested room"""
87
86 def _sendBridgeSignal(ignore=None): 88 def _sendBridgeSignal(ignore=None):
87 self.host.bridge.roomJoined(room.roomJID.userhost(), [user.nick for user in room.roster.values()], room.nick, profile) 89 self.host.bridge.roomJoined(room.roomJID.userhost(), [user.nick for user in room.roster.values()], room.nick, profile)
88 90
89 room_jid_s = room.roomJID.userhost() 91 room_jid_s = room.roomJID.userhost()
90 self.host.memory.updateEntityData(room.roomJID, "type", "chatroom", profile) 92 self.host.memory.updateEntityData(room.roomJID, "type", "chatroom", profile)
97 self.clients[profile].configure(room.roomJID, {}).addCallbacks(_sendBridgeSignal, lambda x: error(_('Error while configuring the room'))) 99 self.clients[profile].configure(room.roomJID, {}).addCallbacks(_sendBridgeSignal, lambda x: error(_('Error while configuring the room')))
98 else: 100 else:
99 _sendBridgeSignal() 101 _sendBridgeSignal()
100 return room 102 return room
101 103
102
103 def __err_joining_room(self, failure, room_jid, nick, history_options, password, profile): 104 def __err_joining_room(self, failure, room_jid, nick, history_options, password, profile):
104 """Called when something is going wrong when joining the room""" 105 """Called when something is going wrong when joining the room"""
105 if failure.value.condition == 'conflict': 106 if failure.value.condition == 'conflict':
106 # we have a nickname conflict, we try again with "_" suffixed to current nickname 107 # we have a nickname conflict, we try again with "_" suffixed to current nickname
107 nick += '_' 108 nick += '_'
108 return self.clients[profile].join(room_jid, nick, history_options, password).addCallbacks(self.__room_joined, self.__err_joining_room, callbackKeywords={'profile':profile}, errbackArgs=[room_jid, nick, history_options, password, profile]) 109 return self.clients[profile].join(room_jid, nick, history_options, password).addCallbacks(self.__room_joined, self.__err_joining_room, callbackKeywords={'profile': profile}, errbackArgs=[room_jid, nick, history_options, password, profile])
109 110
110 mess = _("Error when joining the room") 111 mess = _("Error when joining the room")
111 error (mess) 112 error(mess)
112 self.host.bridge.newAlert(mess, _("Group chat error"), "ERROR", profile) 113 self.host.bridge.newAlert(mess, _("Group chat error"), "ERROR", profile)
113 raise failure 114 raise failure
114 115
115 def getRoomsJoined(self, profile_key='@DEFAULT@'): 116 def getRoomsJoined(self, profile_key='@DEFAULT@'):
116 """Return room where user is""" 117 """Return room where user is"""
126 """return nick used in room by user 127 """return nick used in room by user
127 @param room_jid_s: unicode room id 128 @param room_jid_s: unicode room id
128 @profile_key: profile 129 @profile_key: profile
129 @return: nick or empty string in case of error""" 130 @return: nick or empty string in case of error"""
130 profile = self.host.memory.getProfileName(profile_key) 131 profile = self.host.memory.getProfileName(profile_key)
131 if not self.__check_profile(profile) or not self.clients[profile].joined_rooms.has_key(room_jid_s): 132 if not self.__check_profile(profile) or room_jid_s not in self.clients[profile].joined_rooms:
132 return '' 133 return ''
133 return self.clients[profile].joined_rooms[room_jid_s].nick 134 return self.clients[profile].joined_rooms[room_jid_s].nick
134 135
135 def isNickInRoom(self, room_jid, nick, profile): 136 def isNickInRoom(self, room_jid, nick, profile):
136 """Tell if a nick is currently present in a room""" 137 """Tell if a nick is currently present in a room"""
137 profile = self.host.memory.getProfileName(profile) 138 profile = self.host.memory.getProfileName(profile)
138 if not self.__check_profile(profile): 139 if not self.__check_profile(profile):
139 raise exceptions.UnknownProfileError("Unknown or disconnected profile") 140 raise exceptions.UnknownProfileError("Unknown or disconnected profile")
140 if not self.clients[profile].joined_rooms.has_key(room_jid.userhost()): 141 if room_jid.userhost() not in self.clients[profile].joined_rooms:
141 raise UnknownRoom("This room has not been joined") 142 raise UnknownRoom("This room has not been joined")
142 return self.clients[profile].joined_rooms[room_jid.userhost()].inRoster(nick) 143 return self.clients[profile].joined_rooms[room_jid.userhost()].inRoster(nick)
143 144
144 def getRoomsSubjects(self, profile_key='@DEFAULT@'): 145 def getRoomsSubjects(self, profile_key='@DEFAULT@'):
145 """Return received subjects of rooms""" 146 """Return received subjects of rooms"""
153 #TODO: we should use #RFC-0045 10.1.4 when available here 154 #TODO: we should use #RFC-0045 10.1.4 when available here
154 #TODO: we should be able to select the MUC service here 155 #TODO: we should be able to select the MUC service here
155 return uuid.uuid1() 156 return uuid.uuid1()
156 157
157 def join(self, room_jid, nick, options, profile_key='@DEFAULT@'): 158 def join(self, room_jid, nick, options, profile_key='@DEFAULT@'):
158 def _errDeferred(exc_obj = Exception, txt='Error while joining room'): 159 def _errDeferred(exc_obj=Exception, txt='Error while joining room'):
159 d = defer.Deferred() 160 d = defer.Deferred()
160 d.errback(exc_obj(txt)) 161 d.errback(exc_obj(txt))
161 return d 162 return d
162 163
163 profile = self.host.memory.getProfileName(profile_key) 164 profile = self.host.memory.getProfileName(profile_key)
164 if not self.__check_profile(profile): 165 if not self.__check_profile(profile):
165 return _errDeferred() 166 return _errDeferred()
166 if self.clients[profile].joined_rooms.has_key(room_jid.userhost()): 167 if room_jid.userhost() in self.clients[profile].joined_rooms:
167 warning(_('%(profile)s is already in room %(room_jid)s') % {'profile':profile, 'room_jid':room_jid.userhost()}) 168 warning(_('%(profile)s is already in room %(room_jid)s') % {'profile': profile, 'room_jid': room_jid.userhost()})
168 return _errDeferred() 169 return _errDeferred()
169 info (_("[%(profile)s] is joining room %(room)s with nick %(nick)s") % {'profile':profile,'room':room_jid.userhost(), 'nick':nick}) 170 info(_("[%(profile)s] is joining room %(room)s with nick %(nick)s") % {'profile': profile, 'room': room_jid.userhost(), 'nick': nick})
170 171
171 history_options = options["history"] == "True" if options.has_key("history") else None 172 history_options = options["history"] == "True" if "history" in options else None
172 password = options["password"] if options.has_key("password") else None 173 password = options["password"] if "password" in options else None
173 174
174 return self.clients[profile].join(room_jid, nick, history_options, password).addCallbacks(self.__room_joined, self.__err_joining_room, callbackKeywords={'profile':profile}, errbackArgs=[room_jid, nick, history_options, password, profile]) 175 return self.clients[profile].join(room_jid, nick, history_options, password).addCallbacks(self.__room_joined, self.__err_joining_room, callbackKeywords={'profile': profile}, errbackArgs=[room_jid, nick, history_options, password, profile])
175 176
176 def _join(self, room_jid_s, nick, options={}, profile_key='@DEFAULT@'): 177 def _join(self, room_jid_s, nick, options={}, profile_key='@DEFAULT@'):
177 """join method used by bridge: use the _join method, but doesn't return any deferred""" 178 """join method used by bridge: use the _join method, but doesn't return any deferred"""
178 profile = self.host.memory.getProfileName(profile_key) 179 profile = self.host.memory.getProfileName(profile_key)
179 if not self.__check_profile(profile): 180 if not self.__check_profile(profile):
184 mess = _("Invalid room jid: %s") % room_jid_s 185 mess = _("Invalid room jid: %s") % room_jid_s
185 warning(mess) 186 warning(mess)
186 self.host.bridge.newAlert(mess, _("Group chat error"), "ERROR", profile) 187 self.host.bridge.newAlert(mess, _("Group chat error"), "ERROR", profile)
187 return 188 return
188 d = self.join(room_jid, nick, options, profile) 189 d = self.join(room_jid, nick, options, profile)
189 d.addErrback(lambda x: warning(_('Error while joining room'))) #TODO: error management + signal in bridge 190 d.addErrback(lambda x: warning(_('Error while joining room'))) # TODO: error management + signal in bridge
190 191
191 def nick(self, room_jid, nick, profile_key): 192 def nick(self, room_jid, nick, profile_key):
192 profile = self.host.memory.getProfileName(profile_key) 193 profile = self.host.memory.getProfileName(profile_key)
193 if not self.__check_profile(profile): 194 if not self.__check_profile(profile):
194 raise exceptions.UnknownProfileError("Unknown or disconnected profile") 195 raise exceptions.UnknownProfileError("Unknown or disconnected profile")
195 if not self.clients[profile].joined_rooms.has_key(room_jid.userhost()): 196 if room_jid.userhost() not in self.clients[profile].joined_rooms:
196 raise UnknownRoom("This room has not been joined") 197 raise UnknownRoom("This room has not been joined")
197 return self.clients[profile].nick(room_jid, nick) 198 return self.clients[profile].nick(room_jid, nick)
198 199
199 def leave(self, room_jid, profile_key): 200 def leave(self, room_jid, profile_key):
200 profile = self.host.memory.getProfileName(profile_key) 201 profile = self.host.memory.getProfileName(profile_key)
201 if not self.__check_profile(profile): 202 if not self.__check_profile(profile):
202 raise exceptions.UnknownProfileError("Unknown or disconnected profile") 203 raise exceptions.UnknownProfileError("Unknown or disconnected profile")
203 if not self.clients[profile].joined_rooms.has_key(room_jid.userhost()): 204 if room_jid.userhost() not in self.clients[profile].joined_rooms:
204 raise UnknownRoom("This room has not been joined") 205 raise UnknownRoom("This room has not been joined")
205 return self.clients[profile].leave(room_jid) 206 return self.clients[profile].leave(room_jid)
206 207
207 def subject(self, room_jid, subject, profile_key): 208 def subject(self, room_jid, subject, profile_key):
208 profile = self.host.memory.getProfileName(profile_key) 209 profile = self.host.memory.getProfileName(profile_key)
209 if not self.__check_profile(profile): 210 if not self.__check_profile(profile):
210 raise exceptions.UnknownProfileError("Unknown or disconnected profile") 211 raise exceptions.UnknownProfileError("Unknown or disconnected profile")
211 if not self.clients[profile].joined_rooms.has_key(room_jid.userhost()): 212 if room_jid.userhost() not in self.clients[profile].joined_rooms:
212 raise UnknownRoom("This room has not been joined") 213 raise UnknownRoom("This room has not been joined")
213 return self.clients[profile].subject(room_jid, subject) 214 return self.clients[profile].subject(room_jid, subject)
214 215
215 def mucNick(self, room_jid_s, nick, profile_key='@DEFAULT@'): 216 def mucNick(self, room_jid_s, nick, profile_key='@DEFAULT@'):
216 """Change nickname in a room""" 217 """Change nickname in a room"""
230 231
231 def __init__(self, plugin_parent): 232 def __init__(self, plugin_parent):
232 self.plugin_parent = plugin_parent 233 self.plugin_parent = plugin_parent
233 self.host = plugin_parent.host 234 self.host = plugin_parent.host
234 muc.MUCClient.__init__(self) 235 muc.MUCClient.__init__(self)
235 self.joined_rooms = {} #FIXME: seem to do the same thing as MUCClient's _rooms attribute, must be removed 236 self.joined_rooms = {} # FIXME: seem to do the same thing as MUCClient's _rooms attribute, must be removed
236 self.rec_subjects = {} 237 self.rec_subjects = {}
237 self.__changing_nicks = set() # used to keep trace of who is changing nick, 238 self.__changing_nicks = set() # used to keep trace of who is changing nick,
238 # and to discard userJoinedRoom signal in this case 239 # and to discard userJoinedRoom signal in this case
239 print "init SatMUCClient OK" 240 print "init SatMUCClient OK"
240 241
241 def subject(self, room, subject): 242 def subject(self, room, subject):
242 return muc.MUCClientProtocol.subject(self, room, subject) 243 return muc.MUCClientProtocol.subject(self, room, subject)
269 270
270 def userJoinedRoom(self, room, user): 271 def userJoinedRoom(self, room, user):
271 if user.nick in self.__changing_nicks: 272 if user.nick in self.__changing_nicks:
272 self.__changing_nicks.remove(user.nick) 273 self.__changing_nicks.remove(user.nick)
273 else: 274 else:
274 debug (_("user %(nick)s has joined room (%(room_id)s)") % {'nick':user.nick, 'room_id':room.occupantJID.userhost()}) 275 debug(_("user %(nick)s has joined room (%(room_id)s)") % {'nick': user.nick, 'room_id': room.occupantJID.userhost()})
275 if not self.host.trigger.point("MUC user joined", room, user, self.parent.profile): 276 if not self.host.trigger.point("MUC user joined", room, user, self.parent.profile):
276 return 277 return
277 user_data={'entity':user.entity.full() if user.entity else '', 'affiliation':user.affiliation, 'role':user.role} 278 user_data = {'entity': user.entity.full() if user.entity else '', 'affiliation': user.affiliation, 'role': user.role}
278 self.host.bridge.roomUserJoined(room.roomJID.userhost(), user.nick, user_data, self.parent.profile) 279 self.host.bridge.roomUserJoined(room.roomJID.userhost(), user.nick, user_data, self.parent.profile)
279 280
280 def userLeftRoom(self, room, user): 281 def userLeftRoom(self, room, user):
281 if user.nick == room.nick: 282 if user.nick == room.nick:
282 # we left the room 283 # we left the room
283 room_jid_s = room.roomJID.userhost() 284 room_jid_s = room.roomJID.userhost()
284 info (_("Room [%(room)s] left (%(profile)s))") % { "room": room_jid_s, 285 info(_("Room [%(room)s] left (%(profile)s))") % {"room": room_jid_s,
285 "profile": self.parent.profile }) 286 "profile": self.parent.profile})
286 self.host.memory.delEntityCache(room.roomJID, self.parent.profile) 287 self.host.memory.delEntityCache(room.roomJID, self.parent.profile)
287 del self.plugin_parent.clients[self.parent.profile].joined_rooms[room_jid_s] 288 del self.plugin_parent.clients[self.parent.profile].joined_rooms[room_jid_s]
288 self.host.bridge.roomLeft(room.roomJID.userhost(), self.parent.profile) 289 self.host.bridge.roomLeft(room.roomJID.userhost(), self.parent.profile)
289 else: 290 else:
290 debug (_("user %(nick)s left room (%(room_id)s)") % {'nick':user.nick, 'room_id':room.occupantJID.userhost()}) 291 debug(_("user %(nick)s left room (%(room_id)s)") % {'nick': user.nick, 'room_id': room.occupantJID.userhost()})
291 user_data={'entity':user.entity.full() if user.entity else '', 'affiliation':user.affiliation, 'role':user.role} 292 user_data = {'entity': user.entity.full() if user.entity else '', 'affiliation': user.affiliation, 'role': user.role}
292 self.host.bridge.roomUserLeft(room.roomJID.userhost(), user.nick, user_data, self.parent.profile) 293 self.host.bridge.roomUserLeft(room.roomJID.userhost(), user.nick, user_data, self.parent.profile)
293 294
294 def userChangedNick(self, room, user, new_nick): 295 def userChangedNick(self, room, user, new_nick):
295 self.host.bridge.roomUserChangedNick(room.roomJID.userhost(), user.nick, new_nick, self.parent.profile) 296 self.host.bridge.roomUserChangedNick(room.roomJID.userhost(), user.nick, new_nick, self.parent.profile)
296 297
297 def userUpdatedStatus(self, room, user, show, status): 298 def userUpdatedStatus(self, room, user, show, status):
298 print("FIXME: MUC status not managed yet") 299 print("FIXME: MUC status not managed yet")
299 #FIXME: 300 #FIXME:
300 301
301 def receivedSubject(self, room, user, subject): 302 def receivedSubject(self, room, user, subject):
302 debug (_("New subject for room (%(room_id)s): %(subject)s") % {'room_id':room.roomJID.full(),'subject':subject}) 303 debug(_("New subject for room (%(room_id)s): %(subject)s") % {'room_id': room.roomJID.full(), 'subject': subject})
303 self.rec_subjects[room.roomJID.userhost()] = (room.roomJID.userhost(), subject) 304 self.rec_subjects[room.roomJID.userhost()] = (room.roomJID.userhost(), subject)
304 self.host.bridge.roomNewSubject(room.roomJID.userhost(), subject, self.parent.profile) 305 self.host.bridge.roomNewSubject(room.roomJID.userhost(), subject, self.parent.profile)
305 306
306 #def connectionInitialized(self): 307 #def connectionInitialized(self):
307 #pass 308 #pass
309 #def getDiscoInfo(self, requestor, target, nodeIdentifier=''): 310 #def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
310 #return [disco.DiscoFeature(NS_VCARD)] 311 #return [disco.DiscoFeature(NS_VCARD)]
311 312
312 #def getDiscoItems(self, requestor, target, nodeIdentifier=''): 313 #def getDiscoItems(self, requestor, target, nodeIdentifier=''):
313 #return [] 314 #return []
314