From eb46c860d4a776185091b74e2c1614ef488f48b9 Mon Sep 17 00:00:00 2001 From: Yannik Enss Date: Wed, 27 Feb 2019 00:57:03 +0100 Subject: [PATCH] test --- xmpp_tg/xmpp.py | 143 ++++++++++++++++++++++++------------------------ 1 file changed, 72 insertions(+), 71 deletions(-) diff --git a/xmpp_tg/xmpp.py b/xmpp_tg/xmpp.py index 5e2ae3a..503ad13 100644 --- a/xmpp_tg/xmpp.py +++ b/xmpp_tg/xmpp.py @@ -27,6 +27,78 @@ import xmpp_tg.monkey # monkeypatch # modified by adnidor +class MessageHandler(): + _on_connect = lambda: None + + def _unknown_command_handler(self, *args, **kwargs): + return "Unknown command, for a list send !help" + + class WrongNumberOfArgsError(Exception): + pass + + def _min_args(self, num_args): + if len(self.arguments) < num_args: + raise self.WrongNumberOfArgsError("!{} needs at least {} arguments".format(self._command, num_args)) + + def __init__(self, msg): + self._command = msg["body"].split(" ")[0][1:] + self._handler = getattr(self, self._command, self._unknown_command_handler) + self.type = "groupchat" if msg["type"] == "groupchat" else "chat" + self.sender = msg["from"] + self.jid = msg["from"].bare + self.replyto = self.sender.full if self.type == "chat" else self.sender.bare + self.arguments = msg["body"].split(" ")[1:] + self.msg = msg + + def _update(self, text): + xmpp.send_message(mto=self.replyto, mtype=self.type, mbody=text) + + def debug(self, *args, **kwargs): + """Show debug info""" + return pprint.pformat(self.__dict__) + + def help(self, *args, **kwargs): + """List available commands""" + #taken from https://www.python.org/dev/peps/pep-0257/#handling-docstring-indentation + def trim(docstring): + if not docstring: + return '' + # Convert tabs to spaces (following the normal Python rules) + # and split into a list of lines: + lines = docstring.expandtabs().splitlines() + # Determine minimum indentation (first line doesn't count): + indent = 500 + for line in lines[1:]: + stripped = line.lstrip() + if stripped: + indent = min(indent, len(line) - len(stripped)) + # Remove indentation (first line is special): + trimmed = [lines[0].strip()] + if indent < 500: + for line in lines[1:]: + trimmed.append(line[indent:].rstrip()) + # Strip off trailing and leading blank lines: + while trimmed and not trimmed[-1]: + trimmed.pop() + while trimmed and not trimmed[0]: + trimmed.pop(0) + # Return a single string: + return '\n'.join(trimmed) + + if len(self.arguments) == 0: + methods = [func for func in dir(self) if not func.startswith("_") and callable(getattr(self, func))] + reply = "Available commands:" + for method in methods: + docstring = getattr(self, method).__doc__ + if docstring is None: + docstring = "No description available" + reply += "\n"+method+" ("+docstring.split("\n")[0]+")" + return reply + else: + method = getattr(self,self.arguments[0]) + reply = trim(method.__doc__) + return reply + class XMPPTelegram(ComponentXMPP): """ Main XMPPTelegram class. @@ -965,74 +1037,3 @@ class XMPPTelegram(ComponentXMPP): return conn -class MessageHandler(): - _on_connect = lambda: None - - def _unknown_command_handler(self, *args, **kwargs): - return "Unknown command, for a list send !help" - - class WrongNumberOfArgsError(Exception): - pass - - def _min_args(self, num_args): - if len(self.arguments) < num_args: - raise self.WrongNumberOfArgsError("!{} needs at least {} arguments".format(self._command, num_args)) - - def __init__(self, msg): - self._command = msg["body"].split(" ")[0][1:] - self._handler = getattr(self, self._command, self._unknown_command_handler) - self.type = "groupchat" if msg["type"] == "groupchat" else "chat" - self.sender = msg["from"] - self.jid = msg["from"].bare - self.replyto = self.sender.full if self.type == "chat" else self.sender.bare - self.arguments = msg["body"].split(" ")[1:] - self.msg = msg - - def _update(self, text): - xmpp.send_message(mto=self.replyto, mtype=self.type, mbody=text) - - def debug(self, *args, **kwargs): - """Show debug info""" - return pprint.pformat(self.__dict__) - - def help(self, *args, **kwargs): - """List available commands""" - #taken from https://www.python.org/dev/peps/pep-0257/#handling-docstring-indentation - def trim(docstring): - if not docstring: - return '' - # Convert tabs to spaces (following the normal Python rules) - # and split into a list of lines: - lines = docstring.expandtabs().splitlines() - # Determine minimum indentation (first line doesn't count): - indent = 500 - for line in lines[1:]: - stripped = line.lstrip() - if stripped: - indent = min(indent, len(line) - len(stripped)) - # Remove indentation (first line is special): - trimmed = [lines[0].strip()] - if indent < 500: - for line in lines[1:]: - trimmed.append(line[indent:].rstrip()) - # Strip off trailing and leading blank lines: - while trimmed and not trimmed[-1]: - trimmed.pop() - while trimmed and not trimmed[0]: - trimmed.pop(0) - # Return a single string: - return '\n'.join(trimmed) - - if len(self.arguments) == 0: - methods = [func for func in dir(self) if not func.startswith("_") and callable(getattr(self, func))] - reply = "Available commands:" - for method in methods: - docstring = getattr(self, method).__doc__ - if docstring is None: - docstring = "No description available" - reply += "\n"+method+" ("+docstring.split("\n")[0]+")" - return reply - else: - method = getattr(self,self.arguments[0]) - reply = trim(method.__doc__) - return reply