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.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):

21
main.py
View file

@ -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,7 +71,7 @@ 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]
if wordlist[index + 1]:
return True
except IndexError:
# except an IndexError if target is not assignable
@ -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
try:
target = words[index + 1]
try:
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: