small improvements

* leading 0 fix
* validation function improvements
* moved strings.py and misc files to /common/
This commit is contained in:
nico 2018-10-06 13:16:27 +02:00
parent 6bb9f1d5b8
commit d305f8adf3
3 changed files with 15 additions and 16 deletions

View file

@ -13,7 +13,7 @@ class XEPRequest:
self.message_type = msg['type'] self.message_type = msg['type']
self.muc_nick = msg['mucnick'] self.muc_nick = msg['mucnick']
self.reqxep = str(xepnumber) self.reqxep = int(xepnumber)
self.xeplist = None self.xeplist = None
self.acceptedxeps = list() self.acceptedxeps = list()
@ -33,14 +33,14 @@ class XEPRequest:
etag = head.headers['etag'] etag = head.headers['etag']
if local_etag == 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()) self.xeplist = ET.fromstring(file.read())
else: else:
r = s.get("https://xmpp.org/extensions/xeplist.xml") r = s.get("https://xmpp.org/extensions/xeplist.xml")
r.encoding = 'utf-8' r.encoding = 'utf-8'
local_etag = head.headers['etag'] 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()) file.write(r.content.decode())
self.xeplist = ET.fromstring(r.content.decode()) self.xeplist = ET.fromstring(r.content.decode())
@ -61,7 +61,7 @@ class XEPRequest:
result = list() result = list()
# if requested number is inside acceptedxeps continou # 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 searchstring = ".//*[@accepted='true']/[number='%s']" % self.reqxep
for item in self.xeplist.findall(searchstring): for item in self.xeplist.findall(searchstring):

23
main.py
View file

@ -17,7 +17,7 @@ import logging
from argparse import ArgumentParser from argparse import ArgumentParser
from slixmpp.exceptions import XMPPError 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.functions import Version, LastActivity, ContactInfo, HandleError
from classes.xep import XEPRequest from classes.xep import XEPRequest
@ -46,7 +46,7 @@ class QueryBot(slixmpp.ClientXMPP):
for rooms in self.room.split(sep=","): for rooms in self.room.split(sep=","):
self.plugin['xep_0045'].join_muc(rooms, self.nick, wait=True) 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 validation method to reduce malformed querys and unnecessary connection attempts
:param wordlist: words separated by " " from the message :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 # check if number keyword is used if true check if target is assignable
elif argument in StaticAnswers().keys(arg='list', keyword='number_keywords'): elif argument in StaticAnswers().keys(arg='list', keyword='number_keywords'):
try: try:
target = wordlist[index + 1] if wordlist[index + 1]:
return True return True
except IndexError: except IndexError:
# except an IndexError if target is not assignable # except an IndexError if target is not assignable
return False return False
@ -124,27 +124,26 @@ class QueryBot(slixmpp.ClientXMPP):
# if so queue the keyword and the postion in the string # if so queue the keyword and the postion in the string
if x[1] == y: if x[1] == y:
# only add job to queue if domain is valid # 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.append({str(y): x[0]})
# queue # queue
for job in queue: for job in queue:
for key in job: for keyword in job:
keyword = key index = job[keyword]
index = job[key]
if keyword == '!help': if keyword == '!help':
reply.append(StaticAnswers().gen_help()) reply.append(StaticAnswers().gen_help())
continue continue
target = words[index + 1]
try: try:
target = words[index + 1]
if keyword == '!uptime': 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()) reply.append(LastActivity(last_activity, msg, target).format_values())
elif keyword == "!version": 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()) reply.append(Version(version, msg, target).format_version())
elif keyword == "!contact": elif keyword == "!contact":
@ -155,7 +154,7 @@ class QueryBot(slixmpp.ClientXMPP):
reply.append(XEPRequest(msg, target).format()) reply.append(XEPRequest(msg, target).format())
except XMPPError as error: 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 # remove None type from list and send all elements
if list(filter(None.__ne__, reply)) and reply: if list(filter(None.__ne__, reply)) and reply: