comparison src/plugins/plugin_xep_0045.py @ 1989:757c512fe06c

plugin XEP-0045: various fixes/improvments: - use self.txt_cmds instead of self.host.plugins["XEP-0045"] - nick and options in join have now default values - directly raise ConflictError when room is already joined instead of using defer.fail - removed self.host.getClient where it's now useless because client is used instead of profile
author Goffi <goffi@goffi.org>
date Fri, 01 Jul 2016 00:00:23 +0200
parents fbd313cfd40b
children dfbe0bb056dc
comparison
equal deleted inserted replaced
1988:3f0d22565684 1989:757c512fe06c
82 host.bridge.addSignal("mucRoomUserChangedNick", ".plugin", signature='ssss') # args: room_jid, old_nick, new_nick, profile 82 host.bridge.addSignal("mucRoomUserChangedNick", ".plugin", signature='ssss') # args: room_jid, old_nick, new_nick, profile
83 host.bridge.addSignal("mucRoomNewSubject", ".plugin", signature='sss') # args: room_jid, subject, profile 83 host.bridge.addSignal("mucRoomNewSubject", ".plugin", signature='sss') # args: room_jid, subject, profile
84 self.__submit_conf_id = host.registerCallback(self._submitConfiguration, with_data=True) 84 self.__submit_conf_id = host.registerCallback(self._submitConfiguration, with_data=True)
85 host.importMenu((D_("MUC"), D_("configure")), self._configureRoomMenu, security_limit=0, help_string=D_("Configure Multi-User Chat room"), type_=C.MENU_ROOM) 85 host.importMenu((D_("MUC"), D_("configure")), self._configureRoomMenu, security_limit=0, help_string=D_("Configure Multi-User Chat room"), type_=C.MENU_ROOM)
86 try: 86 try:
87 txt_cmds = self.host.plugins[C.TEXT_CMDS] 87 self.txt_cmds = self.host.plugins[C.TEXT_CMDS]
88 except KeyError: 88 except KeyError:
89 log.info(_(u"Text commands not available")) 89 log.info(_(u"Text commands not available"))
90 else: 90 else:
91 txt_cmds.registerTextCommands(self) 91 self.txt_cmds.registerTextCommands(self)
92 txt_cmds.addWhoIsCb(self._whois, 100) 92 self.txt_cmds.addWhoIsCb(self._whois, 100)
93 93
94 host.trigger.add("presence_available", self.presenceTrigger) 94 host.trigger.add("presence_available", self.presenceTrigger)
95 host.trigger.add("MessageReceived", self.MessageReceivedTrigger, priority=1000000) 95 host.trigger.add("MessageReceived", self.MessageReceivedTrigger, priority=1000000)
96 96
97 def profileConnected(self, profile): 97 def profileConnected(self, profile):
374 """join method used by bridge 374 """join method used by bridge
375 375
376 @return: unicode (the room bare) 376 @return: unicode (the room bare)
377 """ 377 """
378 client = self.host.getClient(profile_key) 378 client = self.host.getClient(profile_key)
379 if options is None:
380 options = {}
381 if room_jid_s: 379 if room_jid_s:
382 muc_service = self.host.getClient(client.profile).muc_service 380 muc_service = client.muc_service
383 try: 381 try:
384 room_jid = jid.JID(room_jid_s) 382 room_jid = jid.JID(room_jid_s)
385 except (RuntimeError, jid.InvalidFormat, AttributeError): 383 except (RuntimeError, jid.InvalidFormat, AttributeError):
386 return defer.fail(jid.InvalidFormat(_(u"Invalid room identifier: '%s'. Please give a room short or full identifier like 'room' or 'room@%s'.") % (room_jid_s, unicode(muc_service)))) 384 return defer.fail(jid.InvalidFormat(_(u"Invalid room identifier: '%s'. Please give a room short or full identifier like 'room' or 'room@%s'.") % (room_jid_s, unicode(muc_service))))
387 if not room_jid.user: 385 if not room_jid.user:
390 room_jid = self.getUniqueName(profile_key=client.profile) 388 room_jid = self.getUniqueName(profile_key=client.profile)
391 # TODO: error management + signal in bridge 389 # TODO: error management + signal in bridge
392 d = self.join(client, room_jid, nick, options) 390 d = self.join(client, room_jid, nick, options)
393 return d.addCallback(lambda room: room.roomJID.userhost()) 391 return d.addCallback(lambda room: room.roomJID.userhost())
394 392
395 def join(self, client, room_jid, nick, options): 393 def join(self, client, room_jid, nick=None, options=None):
394 if not nick:
395 nick = client.jid.user
396 if options is None:
397 options = {}
396 def _errDeferred(exc_obj=Exception, txt=u'Error while joining room'): 398 def _errDeferred(exc_obj=Exception, txt=u'Error while joining room'):
397 d = defer.Deferred() 399 d = defer.Deferred()
398 d.errback(exc_obj(txt)) 400 d.errback(exc_obj(txt))
399 return d 401 return d
400 402
401 if room_jid in client._muc_client.joined_rooms: 403 if room_jid in client._muc_client.joined_rooms:
402 log.warning(_(u'{profile} is already in room {room_jid}').format(profile=client.profile, room_jid = room_jid.userhost())) 404 log.warning(_(u'{profile} is already in room {room_jid}').format(profile=client.profile, room_jid = room_jid.userhost()))
403 return defer.fail(exceptions.ConflictError(_(u"The room has already been joined"))) 405 raise failure.Failure(exceptions.ConflictError(_(u"The room has already been joined")))
404 log.info(_(u"[{profile}] is joining room {room} with nick {nick}").format(profile=client.profile, room=room_jid.userhost(), nick=nick)) 406 log.info(_(u"[{profile}] is joining room {room} with nick {nick}").format(profile=client.profile, room=room_jid.userhost(), nick=nick))
405 407
406 password = options["password"] if "password" in options else None 408 password = options["password"] if "password" in options else None
407 409
408 return client._muc_client.join(room_jid, nick, password).addCallbacks(self._joinCb, self._joinEb, (client, room_jid, nick), errbackArgs=(client, room_jid, nick, password)) 410 return client._muc_client.join(room_jid, nick, password).addCallbacks(self._joinCb, self._joinEb, (client, room_jid, nick), errbackArgs=(client, room_jid, nick, password))
494 496
495 @command (all): JID 497 @command (all): JID
496 - JID: room to join (on the same service if full jid is not specified) 498 - JID: room to join (on the same service if full jid is not specified)
497 """ 499 """
498 if mess_data["unparsed"].strip(): 500 if mess_data["unparsed"].strip():
499 room_jid = self.host.plugins[C.TEXT_CMDS].getRoomJID(mess_data["unparsed"].strip(), mess_data["to"].host) 501 room_jid = self.text_cmds.getRoomJID(mess_data["unparsed"].strip(), mess_data["to"].host)
500 nick = (self.getRoomNick(client, room_jid) or 502 nick = (self.getRoomNick(client, room_jid) or
501 self.host.getClient(client.profile).jid.user) 503 client.jid.user)
502 self.join(client, room_jid, nick, {}) 504 self.join(client, room_jid, nick, {})
503 505
504 return False 506 return False
505 507
506 def cmd_leave(self, client, mess_data): 508 def cmd_leave(self, client, mess_data):
508 510
509 @command (group): [ROOM_JID] 511 @command (group): [ROOM_JID]
510 - ROOM_JID: jid of the room to live (current room if not specified) 512 - ROOM_JID: jid of the room to live (current room if not specified)
511 """ 513 """
512 if mess_data["unparsed"].strip(): 514 if mess_data["unparsed"].strip():
513 room = self.host.plugins[C.TEXT_CMDS].getRoomJID(mess_data["unparsed"].strip(), mess_data["to"].host) 515 room = self.text_cmds.getRoomJID(mess_data["unparsed"].strip(), mess_data["to"].host)
514 else: 516 else:
515 room = mess_data["to"] 517 room = mess_data["to"]
516 518
517 self.leave(client, room) 519 self.leave(client, room)
518 520
536 try: 538 try:
537 nick = options[0] 539 nick = options[0]
538 assert self.isNickInRoom(client, mess_data["to"], nick) 540 assert self.isNickInRoom(client, mess_data["to"], nick)
539 except (IndexError, AssertionError): 541 except (IndexError, AssertionError):
540 feedback = _(u"You must provide a member's nick to kick.") 542 feedback = _(u"You must provide a member's nick to kick.")
541 self.host.plugins[C.TEXT_CMDS].feedBack(client, feedback, mess_data) 543 self.text_cmds.feedBack(client, feedback, mess_data)
542 return False 544 return False
543 545
544 d = self.kick(client, nick, mess_data["to"], {} if len(options) == 1 else {'reason': options[1]}) 546 d = self.kick(client, nick, mess_data["to"], {} if len(options) == 1 else {'reason': options[1]})
545 547
546 def cb(dummy): 548 def cb(dummy):
547 feedback_msg = _(u'You have kicked {}').format(nick) 549 feedback_msg = _(u'You have kicked {}').format(nick)
548 if len(options) > 1: 550 if len(options) > 1:
549 feedback_msg += _(u' for the following reason: {}').format(options[1]) 551 feedback_msg += _(u' for the following reason: {}').format(options[1])
550 self.host.plugins[C.TEXT_CMDS].feedBack(client, feedback_msg, mess_data) 552 self.text_cmds.feedBack(client, feedback_msg, mess_data)
551 return True 553 return True
552 d.addCallback(cb) 554 d.addCallback(cb)
553 return d 555 return d
554 556
555 def cmd_ban(self, client, mess_data): 557 def cmd_ban(self, client, mess_data):
565 entity_jid = jid.JID(jid_s).userhostJID() 567 entity_jid = jid.JID(jid_s).userhostJID()
566 assert(entity_jid.user) 568 assert(entity_jid.user)
567 assert(entity_jid.host) 569 assert(entity_jid.host)
568 except (RuntimeError, jid.InvalidFormat, AttributeError, IndexError, AssertionError): 570 except (RuntimeError, jid.InvalidFormat, AttributeError, IndexError, AssertionError):
569 feedback = _(u"You must provide a valid JID to ban, like in '/ban contact@example.net'") 571 feedback = _(u"You must provide a valid JID to ban, like in '/ban contact@example.net'")
570 self.host.plugins[C.TEXT_CMDS].feedBack(client, feedback, mess_data) 572 self.text_cmds.feedBack(client, feedback, mess_data)
571 return False 573 return False
572 574
573 d = self.ban(client, entity_jid, mess_data["to"], {} if len(options) == 1 else {'reason': options[1]}) 575 d = self.ban(client, entity_jid, mess_data["to"], {} if len(options) == 1 else {'reason': options[1]})
574 576
575 def cb(dummy): 577 def cb(dummy):
576 feedback_msg = _(u'You have banned {}').format(entity_jid) 578 feedback_msg = _(u'You have banned {}').format(entity_jid)
577 if len(options) > 1: 579 if len(options) > 1:
578 feedback_msg += _(u' for the following reason: {}').format(options[1]) 580 feedback_msg += _(u' for the following reason: {}').format(options[1])
579 self.host.plugins[C.TEXT_CMDS].feedBack(client, feedback_msg, mess_data) 581 self.text_cmds.feedBack(client, feedback_msg, mess_data)
580 return True 582 return True
581 d.addCallback(cb) 583 d.addCallback(cb)
582 return d 584 return d
583 585
584 def cmd_affiliate(self, client, mess_data): 586 def cmd_affiliate(self, client, mess_data):
598 entity_jid = jid.JID(jid_s).userhostJID() 600 entity_jid = jid.JID(jid_s).userhostJID()
599 assert(entity_jid.user) 601 assert(entity_jid.user)
600 assert(entity_jid.host) 602 assert(entity_jid.host)
601 except (RuntimeError, jid.InvalidFormat, AttributeError, IndexError, AssertionError): 603 except (RuntimeError, jid.InvalidFormat, AttributeError, IndexError, AssertionError):
602 feedback = _(u"You must provide a valid JID to affiliate, like in '/affiliate contact@example.net member'") 604 feedback = _(u"You must provide a valid JID to affiliate, like in '/affiliate contact@example.net member'")
603 self.host.plugins[C.TEXT_CMDS].feedBack(client, feedback, mess_data) 605 self.text_cmds.feedBack(client, feedback, mess_data)
604 return False 606 return False
605 607
606 affiliation = options[1] if len(options) > 1 else 'none' 608 affiliation = options[1] if len(options) > 1 else 'none'
607 if affiliation not in AFFILIATIONS: 609 if affiliation not in AFFILIATIONS:
608 feedback = _(u"You must provide a valid affiliation: %s") % ' '.join(AFFILIATIONS) 610 feedback = _(u"You must provide a valid affiliation: %s") % ' '.join(AFFILIATIONS)
609 self.host.plugins[C.TEXT_CMDS].feedBack(client, feedback, mess_data) 611 self.text_cmds.feedBack(client, feedback, mess_data)
610 return False 612 return False
611 613
612 d = self.affiliate(client, entity_jid, mess_data["to"], {'affiliation': affiliation}) 614 d = self.affiliate(client, entity_jid, mess_data["to"], {'affiliation': affiliation})
613 615
614 def cb(dummy): 616 def cb(dummy):
615 feedback_msg = _(u'New affiliation for %(entity)s: %(affiliation)s').format(entity=entity_jid, affiliation=affiliation) 617 feedback_msg = _(u'New affiliation for %(entity)s: %(affiliation)s').format(entity=entity_jid, affiliation=affiliation)
616 self.host.plugins[C.TEXT_CMDS].feedBack(client, feedback_msg, mess_data) 618 self.text_cmds.feedBack(client, feedback_msg, mess_data)
617 return True 619 return True
618 d.addCallback(cb) 620 d.addCallback(cb)
619 return d 621 return d
620 622
621 def cmd_title(self, client, mess_data): 623 def cmd_title(self, client, mess_data):