Rename MessageHandler to CommandHandler and put Error at top level
This commit is contained in:
@@ -7,19 +7,19 @@ from xmpp_tg.utils import var_dump, display_tg_name, get_contact_jid, localtime
|
|||||||
|
|
||||||
# modified by adnidor
|
# modified by adnidor
|
||||||
|
|
||||||
class MessageHandler():
|
class WrongNumberOfArgsError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class CommandHandler():
|
||||||
_on_connect = lambda: None
|
_on_connect = lambda: None
|
||||||
_helptext = "Available Commands:"
|
_helptext = "Available Commands:"
|
||||||
|
|
||||||
def _unknown_command_handler(self, *args, **kwargs):
|
def _unknown_command_handler(self, *args, **kwargs):
|
||||||
return "Unknown command, for a list send !help"
|
return "Unknown command, for a list send !help"
|
||||||
|
|
||||||
class WrongNumberOfArgsError(Exception):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def _min_args(self, num_args):
|
def _min_args(self, num_args):
|
||||||
if len(self.arguments) < num_args:
|
if len(self.arguments) < num_args:
|
||||||
raise self.WrongNumberOfArgsError("!{} needs at least {} arguments".format(self._command, num_args))
|
raise WrongNumberOfArgsError("!{} needs at least {} arguments".format(self._command, num_args))
|
||||||
|
|
||||||
def __init__(self, msg):
|
def __init__(self, msg):
|
||||||
self._command = msg["body"].split(" ")[0][1:]
|
self._command = msg["body"].split(" ")[0][1:]
|
||||||
@@ -82,7 +82,7 @@ class MessageHandler():
|
|||||||
reply = trim(method.__doc__)
|
reply = trim(method.__doc__)
|
||||||
return reply
|
return reply
|
||||||
|
|
||||||
class GateMessageHandler(MessageHandler):
|
class GateCommandHandler(CommandHandler):
|
||||||
_helptext = "Available Gateway commands:"
|
_helptext = "Available Gateway commands:"
|
||||||
|
|
||||||
def configure(hndl, self):
|
def configure(hndl, self):
|
||||||
@@ -316,7 +316,7 @@ class GateMessageHandler(MessageHandler):
|
|||||||
|
|
||||||
'!roster - Lists yout TG roster\n')
|
'!roster - Lists yout TG roster\n')
|
||||||
|
|
||||||
class ChatCommandHandler(MessageHandler):
|
class ChatCommandHandler(CommandHandler):
|
||||||
_helptext = "Available Userchat commands:"
|
_helptext = "Available Userchat commands:"
|
||||||
|
|
||||||
def __init__(self, msg):
|
def __init__(self, msg):
|
||||||
@@ -363,7 +363,7 @@ class ChatCommandHandler(MessageHandler):
|
|||||||
del(self.tg_dialogs[hndl.jid]['messages'][hndl.tg_id])
|
del(self.tg_dialogs[hndl.jid]['messages'][hndl.tg_id])
|
||||||
self.tg_connections[hndl.jid].invoke( DeleteMessagesRequest([msg_id], revoke = True) )
|
self.tg_connections[hndl.jid].invoke( DeleteMessagesRequest([msg_id], revoke = True) )
|
||||||
|
|
||||||
class GroupchatCommandHandler(MessageHandler):
|
class GroupchatCommandHandler(CommandHandler):
|
||||||
_helptext = "Available Groupchat commands:"
|
_helptext = "Available Groupchat commands:"
|
||||||
|
|
||||||
def __init__(self, msg):
|
def __init__(self, msg):
|
||||||
|
|||||||
@@ -279,7 +279,7 @@ class XMPPTelegram(ComponentXMPP):
|
|||||||
|
|
||||||
if is_command:
|
if is_command:
|
||||||
command = msg["body"].split(" ")[0][1:]
|
command = msg["body"].split(" ")[0][1:]
|
||||||
handler = GateMessageHandler(msg)._handler
|
handler = GateCommandHandler(msg)._handler
|
||||||
try:
|
try:
|
||||||
reply = str(handler(self))
|
reply = str(handler(self))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -290,7 +290,7 @@ class XMPPTelegram(ComponentXMPP):
|
|||||||
else:
|
else:
|
||||||
if isinstance(e, NotAuthorizedError):
|
if isinstance(e, NotAuthorizedError):
|
||||||
reply = str(e)
|
reply = str(e)
|
||||||
elif isinstance(e, MessageHandler.WrongNumberOfArgsError):
|
elif isinstance(e, WrongNumberOfArgsError):
|
||||||
reply = str(e)
|
reply = str(e)
|
||||||
else:
|
else:
|
||||||
logging.error("Exception in command from {}, command was '{}'".format(msg["from"],msg["body"]))
|
logging.error("Exception in command from {}, command was '{}'".format(msg["from"],msg["body"]))
|
||||||
@@ -317,7 +317,7 @@ class XMPPTelegram(ComponentXMPP):
|
|||||||
else:
|
else:
|
||||||
if isinstance(e, NotAuthorizedError):
|
if isinstance(e, NotAuthorizedError):
|
||||||
reply = str(e)
|
reply = str(e)
|
||||||
elif isinstance(e, MessageHandler.WrongNumberOfArgsError):
|
elif isinstance(e, WrongNumberOfArgsError):
|
||||||
reply = str(e)
|
reply = str(e)
|
||||||
else:
|
else:
|
||||||
logging.error("Exception in command from {}, command was '{}'".format(msg["from"],msg["body"]))
|
logging.error("Exception in command from {}, command was '{}'".format(msg["from"],msg["body"]))
|
||||||
@@ -344,7 +344,7 @@ class XMPPTelegram(ComponentXMPP):
|
|||||||
else:
|
else:
|
||||||
if isinstance(e, NotAuthorizedError):
|
if isinstance(e, NotAuthorizedError):
|
||||||
reply = str(e)
|
reply = str(e)
|
||||||
elif isinstance(e, MessageHandler.WrongNumberOfArgsError):
|
elif isinstance(e, WrongNumberOfArgsError):
|
||||||
reply = str(e)
|
reply = str(e)
|
||||||
else:
|
else:
|
||||||
logging.error("Exception in command from {}, command was '{}'".format(msg["from"],msg["body"]))
|
logging.error("Exception in command from {}, command was '{}'".format(msg["from"],msg["body"]))
|
||||||
|
|||||||
Reference in New Issue
Block a user