plugins TEXT-COMMANDS, XEP-0045, XEP-0048, XEP-0249:

- give a feedback instead of sending the message when the command is invalid or used in a wrong context
    - add command /join for XEP-0249
This commit is contained in:
souliane 2014-04-30 16:34:09 +02:00
commit 42b1b6c4ed
4 changed files with 62 additions and 21 deletions

View file

@ -43,6 +43,8 @@ class TextCommands(object):
# plugins need a dynamic translation system (translation
# should be downloadable independently)
HELP_SUGGESTION = _("Type '/help' to get a list of the available commands. If you didn't want to use a command, please start your message with '//' to escape the slash.")
def __init__(self, host):
log.info(_("Text commands initialization"))
self.host = host
@ -133,9 +135,10 @@ class TextCommands(object):
d = defer.maybeDeferred(self._commands[command], mess_data, profile)
d.addCallback(retHandling)
except KeyError:
pass
self.feedBack(_("Unknown command /%s. ") % command + self.HELP_SUGGESTION, mess_data, profile)
return Failure(MessageSentAndStored("text commands took over", mess_data))
return d or mess_data # if a command is detected, we should have a deferred, else be send the message normally
return d or mess_data # if a command is detected, we should have a deferred, else we send the message normally
def getRoomJID(self, arg, service_jid):
"""Return a room jid with a shortcut
@ -159,6 +162,19 @@ class TextCommands(object):
self.host.bridge.newMessage(unicode(mess_data["to"]), message, mess_data['type'], unicode(_from), {}, profile=profile)
def feedBackWrongContext(self, command, types, mess_data, profile):
"""Give a generic message to the user when a command has been used in a wrong context.
@param command (string): the command name (without the slash)
@param types (string, list): the message types to which the command applies.
@param mess_data (dict): original message data
@param profile: %(doc_profile)s
"""
if not isinstance(types, str):
types = _(' or ').join(types)
feedback = _("/%(command)s command only applies on %(type)s messages. ") % {'command': command, 'type': types}
self.host.plugins[C.TEXT_CMDS].feedBack(feedback + self.HELP_SUGGESTION, mess_data, profile)
def cmd_whois(self, mess_data, profile):
"""show informations on entity"""
log.debug("Catched whois command")

View file

@ -379,10 +379,8 @@ class XEP_0045(object):
log.debug("Catched nick command")
if mess_data['type'] != "groupchat":
#/nick command does nothing if we are not on a group chat
log.info("Ignoring /nick command on a non groupchat message")
return True
self.host.plugins[C.TEXT_CMDS].feedBackWrongContext('nick', 'groupchat', mess_data, profile)
return False
nick = mess_data["unparsed"].strip()
room = mess_data["to"]
@ -396,9 +394,8 @@ class XEP_0045(object):
log.debug("Catched join command")
if mess_data['type'] != "groupchat":
#/leave command does nothing if we are not on a group chat
log.info("Ignoring /join command on a non groupchat message")
return True
self.host.plugins[C.TEXT_CMDS].feedBackWrongContext('join', 'groupchat', mess_data, profile)
return False
if mess_data["unparsed"].strip():
room = self.host.plugins[C.TEXT_CMDS].getRoomJID(mess_data["unparsed"].strip(), mess_data["to"].host)
@ -413,9 +410,8 @@ class XEP_0045(object):
log.debug("Catched leave command")
if mess_data['type'] != "groupchat":
#/leave command does nothing if we are not on a group chat
log.info("Ignoring /leave command on a non groupchat message")
return True
self.host.plugins[C.TEXT_CMDS].feedBackWrongContext('leave', 'groupchat', mess_data, profile)
return False
if mess_data["unparsed"].strip():
room = self.host.plugins[C.TEXT_CMDS].getRoomJID(mess_data["unparsed"].strip(), mess_data["to"].host)
@ -435,9 +431,8 @@ class XEP_0045(object):
log.debug("Catched title command")
if mess_data['type'] != "groupchat":
#/leave command does nothing if we are not on a group chat
log.info("Ignoring /title command on a non groupchat message")
return True
self.host.plugins[C.TEXT_CMDS].feedBackWrongContext('title', 'groupchat', mess_data, profile)
return False
subject = mess_data["unparsed"].strip()

View file

@ -428,9 +428,8 @@ class XEP_0048(object):
txt_cmd = self.host.plugins[C.TEXT_CMDS]
if mess_data['type'] != "groupchat":
#/bookmark command does nothing if we are not on a group chat
log.info("Ignoring /bookmark command on a non groupchat message")
return True
self.host.plugins[C.TEXT_CMDS].feedBackWrongContext('bookmark', 'groupchat', mess_data, profile)
return False
options = mess_data["unparsed"].strip().split()
if options and options[0] not in ('autojoin', 'remove'):

View file

@ -47,6 +47,7 @@ PLUGIN_INFO = {
"type": "XEP",
"protocols": ["XEP-0249"],
"dependencies": ["XEP-0045"],
"recommendations": [C.TEXT_CMDS],
"main": "XEP_0249",
"handler": "yes",
"description": _("""Implementation of Direct MUC Invitations""")
@ -80,6 +81,10 @@ class XEP_0249(object):
self.host = host
host.memory.updateParams(self.params)
host.bridge.addMethod("inviteMUC", ".plugin", in_sign='sssa{ss}s', out_sign='', method=self._invite)
try:
self.host.plugins[C.TEXT_CMDS].registerTextCommands(self)
except KeyError:
log.info(_("Text commands not available"))
def getHandler(self, profile):
return XEP_0249_handler(self)
@ -153,11 +158,37 @@ class XEP_0249(object):
if autojoin == "always":
self._accept(room, profile)
elif autojoin == "ask":
elif autojoin == "never":
self.host.bridge.newAlert(_("An invitation from %(user)s to join the room %(room)s has been declined according to your personal settings.") % {'user': from_, 'room': room}, _("MUC invitation"), "INFO", profile)
else: # leave the default value here
data = {"message": _("You have been invited by %(user)s to join the room %(room)s. Do you accept?") % {'user': from_, 'room': room}, "title": _("MUC invitation")}
self.host.askConfirmation(room, "YES/NO", data, accept_cb, profile)
else:
self.host.bridge.newAlert(_("An invitation from %(user)s to join the room %(room)s has been declined according to your personal settings.") % {'user': from_, 'room': room}, _("MUC invitation"), "INFO", profile)
def cmd_invite(self, mess_data, profile):
"""invite someone in the room
@command (group): (JID)
- JID: the JID of the person to invite
"""
log.debug("Catched invite command")
if mess_data['type'] != "groupchat":
self.host.plugins[C.TEXT_CMDS].feedBackWrongContext('invite', 'groupchat', mess_data, profile)
return False
jid_s = mess_data["unparsed"].strip()
try:
assert(jid_s)
jid_ = jid.JID(jid_s)
assert(jid_.user)
assert(jid_.host)
except (jid.InvalidFormat, AssertionError):
feedback = _(u"You must provide a valid JID to invite, like in '/invite contact@example.net'")
self.host.plugins[C.TEXT_CMDS].feedBack(feedback, mess_data, profile)
return False
self.invite(jid_, mess_data["to"], {}, profile)
return False
class XEP_0249_handler(XMPPHandler):