From d305f8adf3fa7c4f154f003bdf16ce42b1895ffd Mon Sep 17 00:00:00 2001 From: nico Date: Sat, 6 Oct 2018 13:16:27 +0200 Subject: small improvements * leading 0 fix * validation function improvements * moved strings.py and misc files to /common/ --- classes/strings.py | 54 ------------------------------------------------------ classes/xep.py | 8 ++++---- common/strings.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.py | 23 +++++++++++------------ 4 files changed, 69 insertions(+), 70 deletions(-) delete mode 100644 classes/strings.py create mode 100644 common/strings.py diff --git a/classes/strings.py b/classes/strings.py deleted file mode 100644 index 6866a31..0000000 --- a/classes/strings.py +++ /dev/null @@ -1,54 +0,0 @@ -# -*- coding: utf-8 -*- -from random import randint - - -class StaticAnswers: - """ - collection of callable static/ semi-static strings - """ - def __init__(self, nick=""): - self.nickname = nick - self.helpfile = { - 'help': '!help -- display this text', - 'version': '!version domain.tld -- receive XMPP server version', - 'uptime': '!uptime domain.tld -- receive XMPP server uptime', - 'contact': '!contact domain.tld -- receive XMPP server contact address info', - 'xep': '!xep XEP Number -- recieve information about the specified XEP'} - self.possible_answers = { - '1': 'I heard that, %s.', - '2': 'I am sorry for that %s.', - '3': '%s did you try turning it off and on again?'} - self.error_messages = { - '1': 'not reachable', - '2': 'not a valid target' - } - self.keywords = { - "keywords": ["!help", "!uptime", "!version", "!contact", "!xep"], - "domain_keywords": ["!uptime", "!version", "!contact"], - "no_arg_keywords": ["!help"], - "number_keywords": ["!xep"] - } - - def keys(self, arg="", keyword='keywords'): - if arg == 'list': - try: - return self.keywords[keyword] - except KeyError: - return self.keywords['keywords'] - else: - return self.keywords - - def gen_help(self): - helpdoc = "\n".join(['%s' % value for (_, value) in self.helpfile.items()]) - return helpdoc - - def gen_answer(self): - possible_answers = self.possible_answers - return possible_answers[str(randint(1, possible_answers.__len__()))] % self.nickname - - def error(self,code): - try: - text = self.error_messages[str(code)] - except KeyError: - return 'unknown error' - return text diff --git a/classes/xep.py b/classes/xep.py index 9e4f61f..9e8414c 100644 --- a/classes/xep.py +++ b/classes/xep.py @@ -13,7 +13,7 @@ class XEPRequest: self.message_type = msg['type'] self.muc_nick = msg['mucnick'] - self.reqxep = str(xepnumber) + self.reqxep = int(xepnumber) self.xeplist = None self.acceptedxeps = list() @@ -33,14 +33,14 @@ class XEPRequest: etag = head.headers['etag'] if local_etag == etag: - with open("xeplist.xml", "r") as file: + with open("./common/xeplist.xml", "r") as file: self.xeplist = ET.fromstring(file.read()) else: r = s.get("https://xmpp.org/extensions/xeplist.xml") r.encoding = 'utf-8' local_etag = head.headers['etag'] - with open("xeplist.xml", "w") as file: + with open("./common/xeplist.xml", "w") as file: file.write(r.content.decode()) self.xeplist = ET.fromstring(r.content.decode()) @@ -61,7 +61,7 @@ class XEPRequest: result = list() # if requested number is inside acceptedxeps continou - if self.reqxep in self.acceptedxeps: + if str(self.reqxep) in self.acceptedxeps: searchstring = ".//*[@accepted='true']/[number='%s']" % self.reqxep for item in self.xeplist.findall(searchstring): diff --git a/common/strings.py b/common/strings.py new file mode 100644 index 0000000..6866a31 --- /dev/null +++ b/common/strings.py @@ -0,0 +1,54 @@ +# -*- coding: utf-8 -*- +from random import randint + + +class StaticAnswers: + """ + collection of callable static/ semi-static strings + """ + def __init__(self, nick=""): + self.nickname = nick + self.helpfile = { + 'help': '!help -- display this text', + 'version': '!version domain.tld -- receive XMPP server version', + 'uptime': '!uptime domain.tld -- receive XMPP server uptime', + 'contact': '!contact domain.tld -- receive XMPP server contact address info', + 'xep': '!xep XEP Number -- recieve information about the specified XEP'} + self.possible_answers = { + '1': 'I heard that, %s.', + '2': 'I am sorry for that %s.', + '3': '%s did you try turning it off and on again?'} + self.error_messages = { + '1': 'not reachable', + '2': 'not a valid target' + } + self.keywords = { + "keywords": ["!help", "!uptime", "!version", "!contact", "!xep"], + "domain_keywords": ["!uptime", "!version", "!contact"], + "no_arg_keywords": ["!help"], + "number_keywords": ["!xep"] + } + + def keys(self, arg="", keyword='keywords'): + if arg == 'list': + try: + return self.keywords[keyword] + except KeyError: + return self.keywords['keywords'] + else: + return self.keywords + + def gen_help(self): + helpdoc = "\n".join(['%s' % value for (_, value) in self.helpfile.items()]) + return helpdoc + + def gen_answer(self): + possible_answers = self.possible_answers + return possible_answers[str(randint(1, possible_answers.__len__()))] % self.nickname + + def error(self,code): + try: + text = self.error_messages[str(code)] + except KeyError: + return 'unknown error' + return text diff --git a/main.py b/main.py index 2625728..cc0d282 100644 --- a/main.py +++ b/main.py @@ -17,7 +17,7 @@ import logging from argparse import ArgumentParser from slixmpp.exceptions import XMPPError -from classes.strings import StaticAnswers +from common.strings import StaticAnswers from classes.functions import Version, LastActivity, ContactInfo, HandleError from classes.xep import XEPRequest @@ -46,7 +46,7 @@ class QueryBot(slixmpp.ClientXMPP): for rooms in self.room.split(sep=","): self.plugin['xep_0045'].join_muc(rooms, self.nick, wait=True) - def validate_domain(self, wordlist, index): + def validate(self, wordlist, index): """ validation method to reduce malformed querys and unnecessary connection attempts :param wordlist: words separated by " " from the message @@ -71,8 +71,8 @@ class QueryBot(slixmpp.ClientXMPP): # check if number keyword is used if true check if target is assignable elif argument in StaticAnswers().keys(arg='list', keyword='number_keywords'): try: - target = wordlist[index + 1] - return True + if wordlist[index + 1]: + return True except IndexError: # except an IndexError if target is not assignable return False @@ -124,27 +124,26 @@ class QueryBot(slixmpp.ClientXMPP): # if so queue the keyword and the postion in the string if x[1] == y: # only add job to queue if domain is valid - if self.validate_domain(words, x[0]): + if self.validate(words, x[0]): queue.append({str(y): x[0]}) # queue for job in queue: - for key in job: - keyword = key - index = job[key] + for keyword in job: + index = job[keyword] if keyword == '!help': reply.append(StaticAnswers().gen_help()) continue + target = words[index + 1] try: - target = words[index + 1] if keyword == '!uptime': - last_activity = yield from self['xep_0012'].get_last_activity(target) + last_activity = yield from self['xep_0012'].get_last_activity(jid=target, cached=False) reply.append(LastActivity(last_activity, msg, target).format_values()) elif keyword == "!version": - version = yield from self['xep_0092'].get_version(target) + version = yield from self['xep_0092'].get_version(jid=target, cached=False) reply.append(Version(version, msg, target).format_version()) elif keyword == "!contact": @@ -155,7 +154,7 @@ class QueryBot(slixmpp.ClientXMPP): reply.append(XEPRequest(msg, target).format()) except XMPPError as error: - reply.append(HandleError(error, msg, key, target).build_report()) + reply.append(HandleError(error, msg, keyword, target).build_report()) # remove None type from list and send all elements if list(filter(None.__ne__, reply)) and reply: -- cgit v1.2.3-54-g00ecf