diff --git a/xmpp_tg/message_handlers.py b/xmpp_tg/message_handlers.py index 82cef95..48a6138 100644 --- a/xmpp_tg/message_handlers.py +++ b/xmpp_tg/message_handlers.py @@ -75,7 +75,7 @@ class MessageHandler(): docstring = "No description available" if docstring.startswith("nolist "): continue - reply += "\n!{command} - {doc}".format(method, docstring.split("\n")[0]) + reply += "\n!{} - {}".format(method, docstring.split("\n")[0]) return reply else: method = getattr(self,self.arguments[0]) diff --git a/xmpp_tg/xmpp.py b/xmpp_tg/xmpp.py index d5a55dc..4832fd9 100644 --- a/xmpp_tg/xmpp.py +++ b/xmpp_tg/xmpp.py @@ -323,88 +323,32 @@ class XMPPTelegram(ComponentXMPP): self.gate_reply_message(msg, reply) #msg.reply(reply).send() - if False: - self.gate_reply_message(iq, '=== Available dialog commands ===:\n\n' - - '!help - Displays this text\n' - '!s/find/replace - Edit last message. Use empty `find` to edit whole message and empty `replace` to delete it.\n' - '!block - Blacklists current user\n' - '!unblock - Unblacklists current user\n' - '!remove - Removes history and contact from your contact list\n' ) - def process_chat_group_command(self, iq): - parsed = iq['body'].split(' ') - jid = iq['from'].bare + logging.info("received command "+str(msg["body"])+" from "+str(msg["from"])+" for "+str(msg["to"])) + is_command = msg["body"].startswith("!") and msg["body"][1] != "_" - if parsed[0] == '!help': - self.gate_reply_message(iq, '=== Available chat commands ===:\n\n' - '!help - Displays this text\n' - '!s/find/replace - Edit last message. Use empty `find` to edit whole message and empty `replace` to delete it.\n' - '!leave - Leaves current group or supergroup\n' - '!invite - Invites user to group\n' - '!kick - Kicks user to group\n' - ) - elif parsed[0] == '!leave': - tg_id = int(iq['to'].node[1:]) - if tg_id in self.tg_dialogs[jid]['supergroups']: - peer = InputPeerChannel(tg_id, self.tg_dialogs[jid]['supergroups'][tg_id].access_hash) - self.tg_connections[jid].invoke( LeaveChannelRequest(peer) ) - self.tg_connections[jid].invoke( DeleteHistoryRequest( peer, max_id = 0, just_clear = None ) ) - c_jid = get_contact_jid(self.tg_dialogs[jid]['supergroups'][tg_id], self.boundjid.bare) - self.send_presence(pto = jid, pfrom = c_jid, ptype = 'unavailable') - self.send_presence(pto = jid, pfrom = c_jid, ptype = 'unsubscribed') - self.send_presence(pto = jid, pfrom = c_jid, ptype = 'unsubscribe') - if tg_id in self.tg_dialogs[jid]['groups']: - self.tg_connections[jid].invoke( DeleteChatUserRequest(tg_id, self.tg_connections[jid].me) ) - self.tg_connections[jid].invoke( DeleteHistoryRequest( InputPeerChat(tg_id), max_id = 0, just_clear = None ) ) - c_jid = get_contact_jid(self.tg_dialogs[jid]['groups'][tg_id], self.boundjid.bare) - self.send_presence(pto = jid, pfrom = c_jid, ptype = 'unavailable') - self.send_presence(pto = jid, pfrom = c_jid, ptype = 'unsubscribed') - self.send_presence(pto = jid, pfrom = c_jid, ptype = 'unsubscribe') - - elif parsed[0] == '!invite': - tg_id = int(iq['to'].node[1:]) - if tg_id in self.tg_dialogs[jid]['supergroups']: - invited_user = self.tg_connections[jid].get_entity(parsed[1]) - if type(invited_user) == User: - self.tg_connections[jid].invoke(EditBannedRequest( InputPeerChannel(tg_id, self.tg_dialogs[jid]['supergroups'][tg_id].access_hash), invited_user, ChannelBannedRights(until_date=None,view_messages=False) ) ) - self.tg_connections[jid].invoke(InviteToChannelRequest( InputPeerChannel(tg_id, self.tg_dialogs[jid]['supergroups'][tg_id].access_hash), [invited_user] ) ) - if tg_id in self.tg_dialogs[jid]['groups']: - invited_user = self.tg_connections[jid].get_entity(parsed[1]) - if type(invited_user) == User: - self.tg_connections[jid].invoke( AddChatUserRequest(tg_id, invited_user, 0) ) - - elif parsed[0] == '!kick': - tg_id = int(iq['to'].node[1:]) - if tg_id in self.tg_dialogs[jid]['supergroups']: - kicked_user = self.tg_connections[jid].get_entity(parsed[1]) - if type(kicked_user) == User: - self.tg_connections[jid].invoke(EditBannedRequest( InputPeerChannel(tg_id, self.tg_dialogs[jid]['supergroups'][tg_id].access_hash), kicked_user, ChannelBannedRights(until_date=None,view_messages=True) ) ) - if tg_id in self.tg_dialogs[jid]['groups']: - kicked_user = self.tg_connections[jid].get_entity(parsed[1]) - if type(kicked_user) == User: - self.tg_connections[jid].invoke( DeleteChatUserRequest(tg_id, kicked_user) ) - - elif iq['body'].startswith('!s/'): - tg_id = int(iq['to'].node[1:]) - peer = InputPeerChannel(tg_id, self.tg_dialogs[jid]['supergroups'][tg_id].access_hash) if tg_id in self.tg_dialogs[jid]['supergroups'] else InputPeerChat(tg_id) - - msg_id, edited = self.edit_message(jid, tg_id, iq['body']) - if not edited: return - - # and send it - if edited != '' and edited != ' ': - self.tg_dialogs[jid]['messages'][tg_id]["body"] = edited - self.tg_connections[jid].invoke( EditMessageRequest(peer, msg_id, message = edited) ) - else: - del(self.tg_dialogs[jid]['messages'][tg_id]) - if isinstance(peer, InputPeerChannel): - self.tg_connections[jid].invoke( DeleteMessagesChannel(peer, [msg_id]) ) + if is_command: + command = msg["body"].split(" ")[0][1:] + handler = GroupchatCommandHandler(msg)._handler + try: + reply = str(handler(self)) + except Exception as e: + if self.config["debug"]: + reply = "******* DEBUG MODE ACTIVE *********\n" + reply += "An Exception occured while executing this command:\n" + reply += traceback.format_exc() else: - self.tg_connections[jid].invoke( DeleteMessagesRequest([msg_id], revoke = True) ) - - - + if isinstance(e, NotAuthorizedError): + reply = str(e) + elif isinstance(e, MessageHandler.WrongNumberOfArgsError): + reply = str(e) + else: + logging.error("Exception in command from {}, command was '{}'".format(msg["from"],msg["body"])) + traceback.print_exc() + reply = "Internal error, please contact Sysadmin" + if reply is not None: + self.gate_reply_message(msg, reply) + #msg.reply(reply).send() def spawn_tg_client(self, jid, phone): """