Mercurial > libervia-backend
comparison src/plugins/plugin_xep_0045.py @ 993:301b342c697a
core: use of the new core.log module:
/!\ this is a massive refactoring and was largely automated, it probably did bring some bugs /!\
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 19 Apr 2014 19:19:19 +0200 |
parents | 5e8e8a034411 |
children | 291eb8216f6e |
comparison
equal
deleted
inserted
replaced
992:f51a1895275c | 993:301b342c697a |
---|---|
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 _ |
21 from sat.core.constants import Const as C | 21 from sat.core.constants import Const as C |
22 from logging import debug, info, warning, error | 22 from sat.core.log import getLogger |
23 log = getLogger(__name__) | |
23 from twisted.internet import defer | 24 from twisted.internet import defer |
24 from twisted.words.protocols.jabber import jid | 25 from twisted.words.protocols.jabber import jid |
25 | 26 |
26 from sat.core import exceptions | 27 from sat.core import exceptions |
27 from sat.memory import memory | 28 from sat.memory import memory |
53 | 54 |
54 class XEP_0045(object): | 55 class XEP_0045(object): |
55 # TODO: this plugin is messy, need a big cleanup/refactoring | 56 # TODO: this plugin is messy, need a big cleanup/refactoring |
56 | 57 |
57 def __init__(self, host): | 58 def __init__(self, host): |
58 info(_("Plugin XEP_0045 initialization")) | 59 log.info(_("Plugin XEP_0045 initialization")) |
59 self.host = host | 60 self.host = host |
60 self.clients = {} | 61 self.clients = {} |
61 self._sessions = memory.Sessions() | 62 self._sessions = memory.Sessions() |
62 host.bridge.addMethod("joinMUC", ".plugin", in_sign='ssa{ss}s', out_sign='s', method=self._join) | 63 host.bridge.addMethod("joinMUC", ".plugin", in_sign='ssa{ss}s', out_sign='s', method=self._join) |
63 host.bridge.addMethod("mucNick", ".plugin", in_sign='sss', out_sign='', method=self.mucNick) | 64 host.bridge.addMethod("mucNick", ".plugin", in_sign='sss', out_sign='', method=self.mucNick) |
75 self.__submit_conf_id = host.registerCallback(self._submitConfiguration, with_data=True) | 76 self.__submit_conf_id = host.registerCallback(self._submitConfiguration, with_data=True) |
76 try: | 77 try: |
77 self.host.plugins[C.TEXT_CMDS].registerTextCommands(self) | 78 self.host.plugins[C.TEXT_CMDS].registerTextCommands(self) |
78 self.host.plugins[C.TEXT_CMDS].addWhoIsCb(self._whois, 100) | 79 self.host.plugins[C.TEXT_CMDS].addWhoIsCb(self._whois, 100) |
79 except KeyError: | 80 except KeyError: |
80 info(_("Text commands not available")) | 81 log.info(_("Text commands not available")) |
81 | 82 |
82 def profileConnected(self, profile): | 83 def profileConnected(self, profile): |
83 def assign_service(service): | 84 def assign_service(service): |
84 client = self.host.getClient(profile) | 85 client = self.host.getClient(profile) |
85 client.muc_service = service | 86 client.muc_service = service |
90 | 91 |
91 if profile known but disconnected, remove it from known profiles | 92 if profile known but disconnected, remove it from known profiles |
92 @param profile: profile to check | 93 @param profile: profile to check |
93 @return: True if the profile is known and connected, else False""" | 94 @return: True if the profile is known and connected, else False""" |
94 if not profile or profile not in self.clients or not self.host.isConnected(profile): | 95 if not profile or profile not in self.clients or not self.host.isConnected(profile): |
95 error(_('Unknown or disconnected profile (%s)') % profile) | 96 log.error(_('Unknown or disconnected profile (%s)') % profile) |
96 if profile in self.clients: | 97 if profile in self.clients: |
97 del self.clients[profile] | 98 del self.clients[profile] |
98 return False | 99 return False |
99 return True | 100 return True |
100 | 101 |
110 if room.locked: | 111 if room.locked: |
111 #FIXME: the current behaviour is to create an instant room | 112 #FIXME: the current behaviour is to create an instant room |
112 #and send the signal only when the room is unlocked | 113 #and send the signal only when the room is unlocked |
113 #a proper configuration management should be done | 114 #a proper configuration management should be done |
114 print "room locked !" | 115 print "room locked !" |
115 self.clients[profile].configure(room.roomJID, {}).addCallbacks(_sendBridgeSignal, lambda x: error(_('Error while configuring the room'))) | 116 self.clients[profile].configure(room.roomJID, {}).addCallbacks(_sendBridgeSignal, lambda x: log.error(_('Error while configuring the room'))) |
116 else: | 117 else: |
117 _sendBridgeSignal() | 118 _sendBridgeSignal() |
118 return room | 119 return room |
119 | 120 |
120 def __err_joining_room(self, failure, room_jid, nick, history_options, password, profile): | 121 def __err_joining_room(self, failure, room_jid, nick, history_options, password, profile): |
122 if hasattr(failure.value, "condition") and failure.value.condition == 'conflict': | 123 if hasattr(failure.value, "condition") and failure.value.condition == 'conflict': |
123 # we have a nickname conflict, we try again with "_" suffixed to current nickname | 124 # we have a nickname conflict, we try again with "_" suffixed to current nickname |
124 nick += '_' | 125 nick += '_' |
125 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]) | 126 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]) |
126 mess = _("Error while joining the room %s" % room_jid.userhost()) | 127 mess = _("Error while joining the room %s" % room_jid.userhost()) |
127 error(mess) | 128 log.error(mess) |
128 self.host.bridge.newAlert(mess, _("Group chat error"), "ERROR", profile) | 129 self.host.bridge.newAlert(mess, _("Group chat error"), "ERROR", profile) |
129 raise failure | 130 raise failure |
130 | 131 |
131 def getRoomsJoined(self, profile_key=C.PROF_KEY_NONE): | 132 def getRoomsJoined(self, profile_key=C.PROF_KEY_NONE): |
132 """Return room where user is""" | 133 """Return room where user is""" |
222 | 223 |
223 def _submitConfiguration(self, raw_data, profile): | 224 def _submitConfiguration(self, raw_data, profile): |
224 try: | 225 try: |
225 session_data = self._sessions.profileGet(raw_data["session_id"], profile) | 226 session_data = self._sessions.profileGet(raw_data["session_id"], profile) |
226 except KeyError: | 227 except KeyError: |
227 warning ("session id doesn't exist, session has probably expired") | 228 log.warning ("session id doesn't exist, session has probably expired") |
228 # TODO: send error dialog | 229 # TODO: send error dialog |
229 return defer.succeed({}) | 230 return defer.succeed({}) |
230 | 231 |
231 data = xml_tools.XMLUIResult2DataFormResult(raw_data) | 232 data = xml_tools.XMLUIResult2DataFormResult(raw_data) |
232 d = self.clients[profile].configure(session_data['room_jid'], data) | 233 d = self.clients[profile].configure(session_data['room_jid'], data) |
284 try: | 285 try: |
285 muc_service = client.muc_service | 286 muc_service = client.muc_service |
286 except AttributeError: | 287 except AttributeError: |
287 raise NotReadyYet("Main server MUC service has not been checked yet") | 288 raise NotReadyYet("Main server MUC service has not been checked yet") |
288 if muc_service is None: | 289 if muc_service is None: |
289 warning(_("No MUC service found on main server")) | 290 log.warning(_("No MUC service found on main server")) |
290 raise exceptions.FeatureNotFound | 291 raise exceptions.FeatureNotFound |
291 | 292 |
292 muc_service = muc_service.userhost() | 293 muc_service = muc_service.userhost() |
293 return jid.JID("%s@%s" % (room_name, muc_service)) | 294 return jid.JID("%s@%s" % (room_name, muc_service)) |
294 | 295 |
300 | 301 |
301 profile = self.host.memory.getProfileName(profile_key) | 302 profile = self.host.memory.getProfileName(profile_key) |
302 if not self.__check_profile(profile): | 303 if not self.__check_profile(profile): |
303 return _errDeferred() | 304 return _errDeferred() |
304 if room_jid.userhost() in self.clients[profile].joined_rooms: | 305 if room_jid.userhost() in self.clients[profile].joined_rooms: |
305 warning(_('%(profile)s is already in room %(room_jid)s') % {'profile': profile, 'room_jid': room_jid.userhost()}) | 306 log.warning(_('%(profile)s is already in room %(room_jid)s') % {'profile': profile, 'room_jid': room_jid.userhost()}) |
306 return _errDeferred() | 307 return _errDeferred() |
307 info(_("[%(profile)s] is joining room %(room)s with nick %(nick)s") % {'profile': profile, 'room': room_jid.userhost(), 'nick': nick}) | 308 log.info(_("[%(profile)s] is joining room %(room)s with nick %(nick)s") % {'profile': profile, 'room': room_jid.userhost(), 'nick': nick}) |
308 | 309 |
309 history_options = options["history"] == "True" if "history" in options else None | 310 history_options = options["history"] == "True" if "history" in options else None |
310 password = options["password"] if "password" in options else None | 311 password = options["password"] if "password" in options else None |
311 | 312 |
312 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]) | 313 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]) |
326 room_jid_s = self.getUniqueName(profile_key=profile_key) | 327 room_jid_s = self.getUniqueName(profile_key=profile_key) |
327 try: | 328 try: |
328 room_jid = jid.JID(room_jid_s) | 329 room_jid = jid.JID(room_jid_s) |
329 except: | 330 except: |
330 mess = _("Invalid room jid: %s") % room_jid_s | 331 mess = _("Invalid room jid: %s") % room_jid_s |
331 warning(mess) | 332 log.warning(mess) |
332 self.host.bridge.newAlert(mess, _("Group chat error"), "ERROR", profile) | 333 self.host.bridge.newAlert(mess, _("Group chat error"), "ERROR", profile) |
333 return | 334 return |
334 self.join(room_jid, nick, options, profile) | 335 self.join(room_jid, nick, options, profile) |
335 # TODO: error management + signal in bridge | 336 # TODO: error management + signal in bridge |
336 return room_jid_s | 337 return room_jid_s |
373 | 374 |
374 # Text commands # | 375 # Text commands # |
375 | 376 |
376 def cmd_nick(self, mess_data, profile): | 377 def cmd_nick(self, mess_data, profile): |
377 """change nickname""" | 378 """change nickname""" |
378 debug("Catched nick command") | 379 log.debug("Catched nick command") |
379 | 380 |
380 if mess_data['type'] != "groupchat": | 381 if mess_data['type'] != "groupchat": |
381 #/nick command does nothing if we are not on a group chat | 382 #/nick command does nothing if we are not on a group chat |
382 info("Ignoring /nick command on a non groupchat message") | 383 log.info("Ignoring /nick command on a non groupchat message") |
383 | 384 |
384 return True | 385 return True |
385 | 386 |
386 nick = mess_data["unparsed"].strip() | 387 nick = mess_data["unparsed"].strip() |
387 room = mess_data["to"] | 388 room = mess_data["to"] |
390 | 391 |
391 return False | 392 return False |
392 | 393 |
393 def cmd_join(self, mess_data, profile): | 394 def cmd_join(self, mess_data, profile): |
394 """join a new room (on the same service if full jid is not specified)""" | 395 """join a new room (on the same service if full jid is not specified)""" |
395 debug("Catched join command") | 396 log.debug("Catched join command") |
396 | 397 |
397 if mess_data['type'] != "groupchat": | 398 if mess_data['type'] != "groupchat": |
398 #/leave command does nothing if we are not on a group chat | 399 #/leave command does nothing if we are not on a group chat |
399 info("Ignoring /join command on a non groupchat message") | 400 log.info("Ignoring /join command on a non groupchat message") |
400 return True | 401 return True |
401 | 402 |
402 if mess_data["unparsed"].strip(): | 403 if mess_data["unparsed"].strip(): |
403 room = self.host.plugins[C.TEXT_CMDS].getRoomJID(mess_data["unparsed"].strip(), mess_data["to"].host) | 404 room = self.host.plugins[C.TEXT_CMDS].getRoomJID(mess_data["unparsed"].strip(), mess_data["to"].host) |
404 nick = (self.getRoomNick(mess_data["to"].userhost(), profile) or | 405 nick = (self.getRoomNick(mess_data["to"].userhost(), profile) or |
407 | 408 |
408 return False | 409 return False |
409 | 410 |
410 def cmd_leave(self, mess_data, profile): | 411 def cmd_leave(self, mess_data, profile): |
411 """quit a room""" | 412 """quit a room""" |
412 debug("Catched leave command") | 413 log.debug("Catched leave command") |
413 | 414 |
414 if mess_data['type'] != "groupchat": | 415 if mess_data['type'] != "groupchat": |
415 #/leave command does nothing if we are not on a group chat | 416 #/leave command does nothing if we are not on a group chat |
416 info("Ignoring /leave command on a non groupchat message") | 417 log.info("Ignoring /leave command on a non groupchat message") |
417 return True | 418 return True |
418 | 419 |
419 if mess_data["unparsed"].strip(): | 420 if mess_data["unparsed"].strip(): |
420 room = self.host.plugins[C.TEXT_CMDS].getRoomJID(mess_data["unparsed"].strip(), mess_data["to"].host) | 421 room = self.host.plugins[C.TEXT_CMDS].getRoomJID(mess_data["unparsed"].strip(), mess_data["to"].host) |
421 else: | 422 else: |
429 """just a synonym of /leave""" | 430 """just a synonym of /leave""" |
430 return self.cmd_leave(mess_data, profile) | 431 return self.cmd_leave(mess_data, profile) |
431 | 432 |
432 def cmd_title(self, mess_data, profile): | 433 def cmd_title(self, mess_data, profile): |
433 """change room's subject""" | 434 """change room's subject""" |
434 debug("Catched title command") | 435 log.debug("Catched title command") |
435 | 436 |
436 if mess_data['type'] != "groupchat": | 437 if mess_data['type'] != "groupchat": |
437 #/leave command does nothing if we are not on a group chat | 438 #/leave command does nothing if we are not on a group chat |
438 info("Ignoring /title command on a non groupchat message") | 439 log.info("Ignoring /title command on a non groupchat message") |
439 return True | 440 return True |
440 | 441 |
441 subject = mess_data["unparsed"].strip() | 442 subject = mess_data["unparsed"].strip() |
442 | 443 |
443 if subject: | 444 if subject: |
453 def _whois(self, whois_msg, mess_data, target_jid, profile): | 454 def _whois(self, whois_msg, mess_data, target_jid, profile): |
454 """ Add MUC user informations to whois """ | 455 """ Add MUC user informations to whois """ |
455 if mess_data['type'] != "groupchat": | 456 if mess_data['type'] != "groupchat": |
456 return | 457 return |
457 if target_jid.userhost() not in self.clients[profile].joined_rooms: | 458 if target_jid.userhost() not in self.clients[profile].joined_rooms: |
458 warning(_("This room has not been joined")) | 459 log.warning(_("This room has not been joined")) |
459 return | 460 return |
460 user = self.clients[profile].joined_rooms[target_jid.userhost()].getUser(target_jid.resource) | 461 user = self.clients[profile].joined_rooms[target_jid.userhost()].getUser(target_jid.resource) |
461 whois_msg.append(_("Nickname: %s") % user.nick) | 462 whois_msg.append(_("Nickname: %s") % user.nick) |
462 if user.entity: | 463 if user.entity: |
463 whois_msg.append(_("Entity: %s") % user.entity) | 464 whois_msg.append(_("Entity: %s") % user.entity) |
509 else: | 510 else: |
510 self.__changing_nicks.discard(presence.nick) | 511 self.__changing_nicks.discard(presence.nick) |
511 self.userLeftRoom(room, user) | 512 self.userLeftRoom(room, user) |
512 | 513 |
513 def receivedGroupChat(self, room, user, body): | 514 def receivedGroupChat(self, room, user, body): |
514 debug('receivedGroupChat: room=%s user=%s body=%s', room, user, body) | 515 log.debug('receivedGroupChat: room=%s user=%s body=%s', room, user, body) |
515 | 516 |
516 def userJoinedRoom(self, room, user): | 517 def userJoinedRoom(self, room, user): |
517 if user.nick in self.__changing_nicks: | 518 if user.nick in self.__changing_nicks: |
518 self.__changing_nicks.remove(user.nick) | 519 self.__changing_nicks.remove(user.nick) |
519 else: | 520 else: |
520 debug(_("user %(nick)s has joined room (%(room_id)s)") % {'nick': user.nick, 'room_id': room.occupantJID.userhost()}) | 521 log.debug(_("user %(nick)s has joined room (%(room_id)s)") % {'nick': user.nick, 'room_id': room.occupantJID.userhost()}) |
521 if not self.host.trigger.point("MUC user joined", room, user, self.parent.profile): | 522 if not self.host.trigger.point("MUC user joined", room, user, self.parent.profile): |
522 return | 523 return |
523 user_data = {'entity': user.entity.full() if user.entity else '', 'affiliation': user.affiliation, 'role': user.role} | 524 user_data = {'entity': user.entity.full() if user.entity else '', 'affiliation': user.affiliation, 'role': user.role} |
524 self.host.bridge.roomUserJoined(room.roomJID.userhost(), user.nick, user_data, self.parent.profile) | 525 self.host.bridge.roomUserJoined(room.roomJID.userhost(), user.nick, user_data, self.parent.profile) |
525 | 526 |
527 if not self.host.trigger.point("MUC user left", room, user, self.parent.profile): | 528 if not self.host.trigger.point("MUC user left", room, user, self.parent.profile): |
528 return | 529 return |
529 if user.nick == room.nick: | 530 if user.nick == room.nick: |
530 # we left the room | 531 # we left the room |
531 room_jid_s = room.roomJID.userhost() | 532 room_jid_s = room.roomJID.userhost() |
532 info(_("Room [%(room)s] left (%(profile)s))") % {"room": room_jid_s, | 533 log.info(_("Room [%(room)s] left (%(profile)s))") % {"room": room_jid_s, |
533 "profile": self.parent.profile}) | 534 "profile": self.parent.profile}) |
534 self.host.memory.delEntityCache(room.roomJID, self.parent.profile) | 535 self.host.memory.delEntityCache(room.roomJID, self.parent.profile) |
535 del self.plugin_parent.clients[self.parent.profile].joined_rooms[room_jid_s] | 536 del self.plugin_parent.clients[self.parent.profile].joined_rooms[room_jid_s] |
536 self.host.bridge.roomLeft(room.roomJID.userhost(), self.parent.profile) | 537 self.host.bridge.roomLeft(room.roomJID.userhost(), self.parent.profile) |
537 else: | 538 else: |
538 debug(_("user %(nick)s left room (%(room_id)s)") % {'nick': user.nick, 'room_id': room.occupantJID.userhost()}) | 539 log.debug(_("user %(nick)s left room (%(room_id)s)") % {'nick': user.nick, 'room_id': room.occupantJID.userhost()}) |
539 user_data = {'entity': user.entity.full() if user.entity else '', 'affiliation': user.affiliation, 'role': user.role} | 540 user_data = {'entity': user.entity.full() if user.entity else '', 'affiliation': user.affiliation, 'role': user.role} |
540 self.host.bridge.roomUserLeft(room.roomJID.userhost(), user.nick, user_data, self.parent.profile) | 541 self.host.bridge.roomUserLeft(room.roomJID.userhost(), user.nick, user_data, self.parent.profile) |
541 | 542 |
542 def userChangedNick(self, room, user, new_nick): | 543 def userChangedNick(self, room, user, new_nick): |
543 self.host.bridge.roomUserChangedNick(room.roomJID.userhost(), user.nick, new_nick, self.parent.profile) | 544 self.host.bridge.roomUserChangedNick(room.roomJID.userhost(), user.nick, new_nick, self.parent.profile) |
545 def userUpdatedStatus(self, room, user, show, status): | 546 def userUpdatedStatus(self, room, user, show, status): |
546 print("FIXME: MUC status not managed yet") | 547 print("FIXME: MUC status not managed yet") |
547 #FIXME: | 548 #FIXME: |
548 | 549 |
549 def receivedSubject(self, room, user, subject): | 550 def receivedSubject(self, room, user, subject): |
550 debug(_("New subject for room (%(room_id)s): %(subject)s") % {'room_id': room.roomJID.full(), 'subject': subject}) | 551 log.debug(_("New subject for room (%(room_id)s): %(subject)s") % {'room_id': room.roomJID.full(), 'subject': subject}) |
551 self.rec_subjects[room.roomJID.userhost()] = (room.roomJID.userhost(), subject) | 552 self.rec_subjects[room.roomJID.userhost()] = (room.roomJID.userhost(), subject) |
552 self.host.bridge.roomNewSubject(room.roomJID.userhost(), subject, self.parent.profile) | 553 self.host.bridge.roomNewSubject(room.roomJID.userhost(), subject, self.parent.profile) |