comparison src/plugins/plugin_xep_0045.py @ 1977:bdc6a5b07922

plugin XEP-0045: fixed calls to join, nick, kick, ban and affiliate
author Goffi <goffi@goffi.org>
date Tue, 28 Jun 2016 18:26:21 +0200
parents 200cd707a46d
children de4fac507dc4
comparison
equal deleted inserted replaced
1975:da6d1988dfcb 1977:bdc6a5b07922
155 msg_suffix = '' 155 msg_suffix = ''
156 else: 156 else:
157 if condition == 'conflict': 157 if condition == 'conflict':
158 # we have a nickname conflict, we try again with "_" suffixed to current nickname 158 # we have a nickname conflict, we try again with "_" suffixed to current nickname
159 nick += '_' 159 nick += '_'
160 return client.join(room_jid, nick, password).addCallbacks(self._joinCb, self._joinEb, callbackKeywords={client: client}, errbackArgs=[client, room_jid, nick, password]) 160 return client._muc_client.join(room_jid, nick, password).addCallbacks(self._joinCb, self._joinEb, callbackKeywords={client: client}, errbackArgs=[client, room_jid, nick, password])
161 msg_suffix = ' with condition "{}"'.format(failure.value.condition) 161 msg_suffix = ' with condition "{}"'.format(failure.value.condition)
162 162
163 mess = D_(u"Error while joining the room {room}{suffix}".format( 163 mess = D_(u"Error while joining the room {room}{suffix}".format(
164 room = room_jid.userhost(), suffix = msg_suffix)) 164 room = room_jid.userhost(), suffix = msg_suffix))
165 log.error(mess) 165 log.error(mess)
350 350
351 @return: unicode 351 @return: unicode
352 """ 352 """
353 return self.host.memory.getConfig(CONFIG_SECTION, 'default_muc', default_conf['default_muc']) 353 return self.host.memory.getConfig(CONFIG_SECTION, 'default_muc', default_conf['default_muc'])
354 354
355 def join(self, client, room_jid, nick, options):
356 def _errDeferred(exc_obj=Exception, txt=u'Error while joining room'):
357 d = defer.Deferred()
358 d.errback(exc_obj(txt))
359 return d
360
361 if room_jid in client._muc_client.joined_rooms:
362 log.warning(_(u'{profile} is already in room {room_jid}').format(profile=client.profile, room_jid = room_jid.userhost()))
363 return defer.fail(exceptions.ConflictError(_(u"The room has already been joined")))
364 log.info(_(u"[{profile}] is joining room {room} with nick {nick}").format(profile=client.profile, room=room_jid.userhost(), nick=nick))
365
366 password = options["password"] if "password" in options else None
367
368 return client._muc_client.join(room_jid, nick, password).addCallbacks(self._joinCb, self._joinEb, callbackKeywords={'client': client}, errbackArgs=[client, room_jid, nick, password])
369
370 def _join(self, room_jid_s, nick, options=None, profile_key=C.PROF_KEY_NONE): 355 def _join(self, room_jid_s, nick, options=None, profile_key=C.PROF_KEY_NONE):
371 """join method used by bridge 356 """join method used by bridge
372 357
373 @return: unicode (the room bare) 358 @return: unicode (the room bare)
374 """ 359 """
387 room_jid = self.getUniqueName(profile_key=client.profile) 372 room_jid = self.getUniqueName(profile_key=client.profile)
388 # TODO: error management + signal in bridge 373 # TODO: error management + signal in bridge
389 d = self.join(client, room_jid, nick, options) 374 d = self.join(client, room_jid, nick, options)
390 return d.addCallback(lambda room: room.roomJID.userhost()) 375 return d.addCallback(lambda room: room.roomJID.userhost())
391 376
377 def join(self, client, room_jid, nick, options):
378 def _errDeferred(exc_obj=Exception, txt=u'Error while joining room'):
379 d = defer.Deferred()
380 d.errback(exc_obj(txt))
381 return d
382
383 if room_jid in client._muc_client.joined_rooms:
384 log.warning(_(u'{profile} is already in room {room_jid}').format(profile=client.profile, room_jid = room_jid.userhost()))
385 return defer.fail(exceptions.ConflictError(_(u"The room has already been joined")))
386 log.info(_(u"[{profile}] is joining room {room} with nick {nick}").format(profile=client.profile, room=room_jid.userhost(), nick=nick))
387
388 password = options["password"] if "password" in options else None
389
390 return client._muc_client.join(room_jid, nick, password).addCallbacks(self._joinCb, self._joinEb, callbackKeywords={'client': client}, errbackArgs=[client, room_jid, nick, password])
391
392 def _nick(self, room_jid_s, nick, profile_key=C.PROF_KEY_NONE): 392 def _nick(self, room_jid_s, nick, profile_key=C.PROF_KEY_NONE):
393 client = self.host.getClient(profile_key) 393 client = self.host.getClient(profile_key)
394 return self.nick(client, jid.JID(room_jid_s), nick) 394 return self.nick(client, jid.JID(room_jid_s), nick)
395 395
396 def nick(self, client, room_jid, nick): 396 def nick(self, client, room_jid, nick):
465 - new_nick: new nick to use 465 - new_nick: new nick to use
466 """ 466 """
467 nick = mess_data["unparsed"].strip() 467 nick = mess_data["unparsed"].strip()
468 if nick: 468 if nick:
469 room = mess_data["to"] 469 room = mess_data["to"]
470 self.nick(room, nick, client.profile) 470 self.nick(client, room, nick)
471 471
472 return False 472 return False
473 473
474 def cmd_join(self, client, mess_data): 474 def cmd_join(self, client, mess_data):
475 """join a new room 475 """join a new room
494 if mess_data["unparsed"].strip(): 494 if mess_data["unparsed"].strip():
495 room = self.host.plugins[C.TEXT_CMDS].getRoomJID(mess_data["unparsed"].strip(), mess_data["to"].host) 495 room = self.host.plugins[C.TEXT_CMDS].getRoomJID(mess_data["unparsed"].strip(), mess_data["to"].host)
496 else: 496 else:
497 room = mess_data["to"] 497 room = mess_data["to"]
498 498
499 self.leave(room, client.profile) 499 self.leave(client, room)
500 500
501 return False 501 return False
502 502
503 def cmd_part(self, client, mess_data): 503 def cmd_part(self, client, mess_data):
504 """just a synonym of /leave 504 """just a synonym of /leave
521 except (IndexError, AssertionError): 521 except (IndexError, AssertionError):
522 feedback = _(u"You must provide a member's nick to kick.") 522 feedback = _(u"You must provide a member's nick to kick.")
523 self.host.plugins[C.TEXT_CMDS].feedBack(client, feedback, mess_data) 523 self.host.plugins[C.TEXT_CMDS].feedBack(client, feedback, mess_data)
524 return False 524 return False
525 525
526 d = self.kick(nick, mess_data["to"], {} if len(options) == 1 else {'reason': options[1]}, client.profile) 526 d = self.kick(client, nick, mess_data["to"], {} if len(options) == 1 else {'reason': options[1]})
527 527
528 def cb(dummy): 528 def cb(dummy):
529 feedback_msg = _(u'You have kicked {}').format(nick) 529 feedback_msg = _(u'You have kicked {}').format(nick)
530 if len(options) > 1: 530 if len(options) > 1:
531 feedback_msg += _(u' for the following reason: {}').format(options[1]) 531 feedback_msg += _(u' for the following reason: {}').format(options[1])
550 except (RuntimeError, jid.InvalidFormat, AttributeError, IndexError, AssertionError): 550 except (RuntimeError, jid.InvalidFormat, AttributeError, IndexError, AssertionError):
551 feedback = _(u"You must provide a valid JID to ban, like in '/ban contact@example.net'") 551 feedback = _(u"You must provide a valid JID to ban, like in '/ban contact@example.net'")
552 self.host.plugins[C.TEXT_CMDS].feedBack(client, feedback, mess_data) 552 self.host.plugins[C.TEXT_CMDS].feedBack(client, feedback, mess_data)
553 return False 553 return False
554 554
555 d = self.ban(entity_jid, mess_data["to"], {} if len(options) == 1 else {'reason': options[1]}, client.profile) 555 d = self.ban(client, entity_jid, mess_data["to"], {} if len(options) == 1 else {'reason': options[1]})
556 556
557 def cb(dummy): 557 def cb(dummy):
558 feedback_msg = _(u'You have banned {}').format(entity_jid) 558 feedback_msg = _(u'You have banned {}').format(entity_jid)
559 if len(options) > 1: 559 if len(options) > 1:
560 feedback_msg += _(u' for the following reason: {}').format(options[1]) 560 feedback_msg += _(u' for the following reason: {}').format(options[1])
589 if affiliation not in AFFILIATIONS: 589 if affiliation not in AFFILIATIONS:
590 feedback = _(u"You must provide a valid affiliation: %s") % ' '.join(AFFILIATIONS) 590 feedback = _(u"You must provide a valid affiliation: %s") % ' '.join(AFFILIATIONS)
591 self.host.plugins[C.TEXT_CMDS].feedBack(client, feedback, mess_data) 591 self.host.plugins[C.TEXT_CMDS].feedBack(client, feedback, mess_data)
592 return False 592 return False
593 593
594 d = self.affiliate(entity_jid, mess_data["to"], {'affiliation': affiliation}, client.profile) 594 d = self.affiliate(client, entity_jid, mess_data["to"], {'affiliation': affiliation})
595 595
596 def cb(dummy): 596 def cb(dummy):
597 feedback_msg = _(u'New affiliation for %(entity)s: %(affiliation)s').format(entity=entity_jid, affiliation=affiliation) 597 feedback_msg = _(u'New affiliation for %(entity)s: %(affiliation)s').format(entity=entity_jid, affiliation=affiliation)
598 self.host.plugins[C.TEXT_CMDS].feedBack(client, feedback_msg, mess_data) 598 self.host.plugins[C.TEXT_CMDS].feedBack(client, feedback_msg, mess_data)
599 return True 599 return True