simplification and major rework

* updated gitignore file
* partly reworked servercontact implementation
* complete rework of uptime, version
* part rework of xep requests
+ added more comments to xep requests
+ added opt_arg to version, xep and contact
* complete rework of validate function
* updated HandleError function
* part rework of StaticStrings function
+ implemented data dictionary to hold all data in main bot
+ added message_ids
* complete rework of queue building and deduplication
This commit is contained in:
nico 2018-11-06 23:43:11 +01:00
parent 559ab280ca
commit 0c313565f2
No known key found for this signature in database
GPG key ID: EA7C31AAB1BDC1A2
9 changed files with 280 additions and 243 deletions

4
.gitignore vendored
View file

@ -60,6 +60,6 @@ target/
# .idea # .idea
.idea .idea
.etag
bot\.cfg bot\.cfg
xeplist.xml common/xeplist.xml
common/.etag

View file

@ -1,108 +0,0 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# XEP-0072: Server Version
class Version:
def __init__(self, version, msg, target):
self.version = version['software_version']['version']
self.os = version['software_version']['os']
self.name = version['software_version']['name']
self.nick = msg['mucnick']
self.message_type = msg['type']
self.target = target
def format_version(self):
if self.message_type == "groupchat":
text = "%s: %s is running %s version %s on %s" % (self.nick, self.target, self.name, self.version, self.os)
else:
text = "%s is running %s version %s on %s" % (self.target, self.name, self.version, self.os)
return text
# XEP-0012: Last Activity
class LastActivity:
""" query the server uptime of the specified domain, defined by XEP-0012 """
def __init__(self, last_activity, msg, target):
self.last_activity = last_activity
self.nick = msg['mucnick']
self.message_type = msg['type']
self.target = target
def format_values(self, granularity=4):
seconds = self.last_activity['last_activity']['seconds']
uptime = []
intervals = (
('years', 31536000), # 60 * 60 * 24 * 365
('weeks', 604800), # 60 * 60 * 24 * 7
('days', 86400), # 60 * 60 * 24
('hours', 3600), # 60 * 60
('minutes', 60),
('seconds', 1)
)
for name, count in intervals:
value = seconds // count
if value:
seconds -= value * count
if value == 1:
name = name.rstrip('s')
uptime.append("{} {}".format(value, name))
result = ' '.join(uptime[:granularity])
if self.message_type == "groupchat":
text = "%s: %s is running since %s" % (self.nick, self.target, result)
else:
text = "%s is running since %s" % (self.target, result)
return text
# XEP-0157: Contact Addresses for XMPP Services
class ContactInfo:
def __init__(self, contact, msg, target):
self.contact = contact
self.message = msg
self.target = target
def format_contact(self):
server_info = []
sep = ' , '
possible_vars = ['abuse-addresses',
'admin-addresses',
'feedback-addresses',
'sales-addresses',
'security-addresses',
'support-addresses']
for field in self.contact['disco_info']['form']:
var = field['var']
if var in possible_vars:
field_value = field.get_value(convert=False)
value = sep.join(field_value) if isinstance(field_value, list) else field_value
server_info.append(' - %s: %s' % (var, value))
if server_info:
text = "contact addresses for %s are" % self.target
for count in range(server_info.__len__()):
text += "\n" + server_info[count]
else:
text = "%s has no contact addresses configured." % self.target
return text
# class handeling XMPPError exeptions
class HandleError:
def __init__(self, error, msg, key, target="target missing"):
self.error = error
self.message = msg
self.key = key
self.target = target
def build_report(self):
condition = self.error.condition
keyword = self.key[1:]
text = "There was an error requesting " + self.target + '\'s ' + keyword + " : " + condition
return text

View file

@ -2,12 +2,13 @@
import defusedxml.ElementTree as Et import defusedxml.ElementTree as Et
# XEP-0157: Contact Addresses for XMPP Services
class ServerContact: class ServerContact:
def __init__(self, contact, msg, target): """
self.contact = contact plugin to process the server contact addresses from a disco query
self.message = msg """
self.target = target def __init__(self):
# init all necessary variables
self.possible_vars = ['abuse-addresses', self.possible_vars = ['abuse-addresses',
'admin-addresses', 'admin-addresses',
'feedback-addresses', 'feedback-addresses',
@ -15,6 +16,9 @@ class ServerContact:
'security-addresses', 'security-addresses',
'support-addresses'] 'support-addresses']
self.contact = None
self.target, self.opt_arg = None, None
def process(self): def process(self):
# get etree from base xml # get etree from base xml
iq = Et.fromstring(str(self.contact)) iq = Et.fromstring(str(self.contact))
@ -36,8 +40,16 @@ class ServerContact:
# iterate over all x nodes # iterate over all x nodes
for child in xdata[x]: for child in xdata[x]:
# if one opt_arg is defined return just that one
if self.opt_arg in self.possible_vars:
if child.attrib['var'] == self.opt_arg:
# add section to result dict and append info
result[child.attrib['var']] = list()
for value in child:
result[child.attrib['var']].append(value.text)
# if node has a var attribute that matches our list process # if node has a var attribute that matches our list process
if child.attrib['var'] in self.possible_vars: elif child.attrib['var'] in self.possible_vars:
# add section to result dict and append info # add section to result dict and append info
result[child.attrib['var']] = list() result[child.attrib['var']] = list()
for value in child: for value in child:
@ -45,17 +57,30 @@ class ServerContact:
return result return result
def format_contact(self): def format(self, query, target, opt_arg):
self.contact = query
self.target = target
self.opt_arg = opt_arg
result = self.process() result = self.process()
# if result is present continue
if result: if result:
text = "contact addresses for %s are\n" % self.target text = "contact addresses for %s are\n" % self.target
# if opt_arg is present and member of possible_vars change text line
if opt_arg in self.possible_vars:
text = "%s for %s are\n" % (self.opt_arg, self.target)
for key in result.keys(): for key in result.keys():
if result[key]:
addr = ' , '.join(result[key]) addr = ' , '.join(result[key])
text += "- %s : %s\n" % (key, addr) text += "- %s : %s\n" % (key, addr)
else: else:
text = "%s has no contact addresses configured." % self.target text = "%s has no contact addresses configured." % self.target
# if opt_arg is present and member of possible_vars but the key is empty change text line
if opt_arg in self.possible_vars:
text = "%s for %s are not defined." % (self.opt_arg, self.target)
return text return text

View file

@ -3,23 +3,19 @@
# XEP-0012: Last Activity # XEP-0012: Last Activity
class LastActivity: class LastActivity:
""" query the server uptime of the specified domain, defined by XEP-0012 """ """
def __init__(self, session, msg, target): query the server uptime of the specified domain, defined by XEP-0012
self.session = session """
self.nick = msg['mucnick'] def __init__(self):
self.message_type = msg['type'] # init all necessary variables
self.target = target self.last_activity = None
self.target, self.opt_arg = None, None
async def query(self): def process(self, granularity=4):
last_activity = await self.session['xep_0012'].get_last_activity(jid=self.target) seconds = self.last_activity['last_activity']['seconds']
seconds = await last_activity['last_activity']['seconds']
return seconds
async def format_values(self, granularity=4):
seconds = await self.query()
#seconds = last_activity['last_activity']['seconds']
uptime = [] uptime = []
# touple with displayable time sections
intervals = ( intervals = (
('years', 31536000), # 60 * 60 * 24 * 365 ('years', 31536000), # 60 * 60 * 24 * 365
('weeks', 604800), # 60 * 60 * 24 * 7 ('weeks', 604800), # 60 * 60 * 24 * 7
@ -28,6 +24,8 @@ class LastActivity:
('minutes', 60), ('minutes', 60),
('seconds', 1) ('seconds', 1)
) )
# for every element in possible time section process the seconds
for name, count in intervals: for name, count in intervals:
value = seconds // count value = seconds // count
if value: if value:
@ -37,9 +35,16 @@ class LastActivity:
uptime.append("{} {}".format(value, name)) uptime.append("{} {}".format(value, name))
result = ' '.join(uptime[:granularity]) result = ' '.join(uptime[:granularity])
if self.message_type == "groupchat": # insert values into result string
text = "%s: %s is running since %s" % (self.nick, self.target, result)
else:
text = "%s is running since %s" % (self.target, result) text = "%s is running since %s" % (self.target, result)
return text return text
def format(self, query, target, opt_arg):
self.last_activity = query
self.target = target
self.opt_arg = opt_arg
reply = self.process()
return reply

39
classes/version.py Normal file
View file

@ -0,0 +1,39 @@
# -*- coding: utf-8 -*-
# XEP-0072: Server Version
class Version:
"""
process and format a version query
"""
def __init__(self):
# init all necessary variables
self.software_version = None
self.target, self.opt_arg = None, None
def format_result(self):
# list of all possible opt_arg
possible_opt_args = ["version", "os", "name"]
name = self.software_version['name']
version = self.software_version['version']
os = self.software_version['os']
# if opt_arg is given member of possible_opt_args list return that element
if self.opt_arg in possible_opt_args:
text = "%s: %s" % (self.opt_arg, self.software_version[self.opt_arg])
# otherwise return full version string
else:
text = "%s is running %s version %s on %s" % (self.target, name, version, os)
return text
def format(self, query, target, opt_arg):
self.software_version = query['software_version']
self.target = target
self.opt_arg = opt_arg
reply = self.format_result()
return reply

View file

@ -1,19 +1,18 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import os
import requests import requests
import defusedxml.ElementTree as ET import defusedxml.ElementTree as et
class XEPRequest: class XEPRequest:
def __init__(self, msg, xepnumber):
""" """
class which requests the header of the referenced xep class which requests the header of the referenced xep
:param xepnumber: number int or str to request the xep for
""" """
self.message_type = msg['type'] def __init__(self):
self.muc_nick = msg['mucnick'] # init all necessary variables
self.reqxep, self.opt_arg = None, None
self.reqxep = int(xepnumber)
self.xeplist = None self.xeplist = None
self.acceptedxeps = list() self.acceptedxeps = list()
@ -21,20 +20,30 @@ class XEPRequest:
""" """
query and save the current xep list to reduce network bandwidth query and save the current xep list to reduce network bandwidth
""" """
try: # check if etag header is present if not set local_etag to ""
with open(".etag") as file: if os.path.isfile("./common/.etag"):
with open("./common/.etag") as file:
local_etag = file.read() local_etag = file.read()
except FileNotFoundError: else:
local_etag = "" local_etag = ""
with requests.Session() as s: with requests.Session() as s:
# head request the xeplist.xml
s.headers.update({'Accept': 'application/xml'}) s.headers.update({'Accept': 'application/xml'})
head = s.head("https://xmpp.org/extensions/xeplist.xml") head = s.head("https://xmpp.org/extensions/xeplist.xml")
etag = head.headers['etag'] etag = head.headers['etag']
# compare etag with local_etag if they match up no request is made
if local_etag == etag: if local_etag == etag:
with open("./common/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())
# if the connection is not possible use cached xml if present
elif os.path.isfile("./common/xeplist.xml") and head.status_code != 200:
with open("./common/xeplist.xml", "r") as file:
self.xeplist = et.fromstring(file.read())
# in any other case request the latest xml
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'
@ -42,9 +51,9 @@ class XEPRequest:
with open("./common/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())
with open('.etag', 'w') as string: with open('./common/.etag', 'w') as string:
string.write(local_etag) string.write(local_etag)
# populate xep comparison list # populate xep comparison list
@ -54,34 +63,52 @@ class XEPRequest:
def get(self): def get(self):
""" """
function to query the xep entry if xepnumber is present in xeplist function to query the xep entry if xepnumber is present in xeplist
:return: nicely formatted xep header information :return: formatted xep header information
""" """
# all possible subtags grouped by location
last_revision_tags = ["date", "version", "initials", "remark"]
xep_tags = ["number", "title", "abstract", "type", "status", "approver", "shortname", "sig", "lastcall"]
# check if xeplist is accurate # check if xeplist is accurate
self.req_xeplist() self.req_xeplist()
result = list() result = list()
# if requested number is inside acceptedxeps continou # if requested number is member of acceptedxeps continue
if str(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):
for x in range(1,5): # if the opt_arg references is member of xeptag return only that tag
result.append(item[x].tag + " : " + item[x].text) if self.opt_arg in xep_tags:
query = item.find(self.opt_arg)
result.append("%s : %s" % (query.tag, query.text))
# if the opt_arg references is member of last-revision_tags return only that tag
elif self.opt_arg in last_revision_tags:
query = item.find("last-revision").find(self.opt_arg)
result.append("%s : %s" % (query.tag, query.text))
# in any other case return the general answer
else: else:
if self.message_type == "groupchat": result_opts = ["title", "type", "abstract", "status"]
result.append(self.muc_nick + " : " + "XEP-" + str(self.reqxep) + " : is not available.") for tag in result_opts:
result.append(item.find(tag).text)
# if the requested number is no member of acceptedxeps and/or not accepted return error.
else: else:
result.append("XEP-" + str(self.reqxep) + " : is not available.") result.append("XEP-%s : is not available." % self.reqxep)
return result return result
def format(self): def format(self, query, target, opt_arg):
"""
:param target: number int or str to request the xep for
:return:
"""
self.reqxep = int(target)
self.opt_arg = opt_arg
reply = self.get() reply = self.get()
if self.message_type == "groupchat":
text = "%s: " % self.muc_nick
reply[0] = text + reply[0]
text = '\n'.join(reply) text = '\n'.join(reply)
return text return text

View file

@ -17,43 +17,53 @@ def deduplicate(reply):
return reply_dedup return reply_dedup
def validate(wordlist, index): def validate(keyword, target):
""" """
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 keyword: used keyword
:param index: keyword index inside the message :param target: provided target
:return: true if valid :return: true if valid
""" """
# keyword inside the message # check if keyword is in the argument list
argument = wordlist[index] if keyword in StaticAnswers().keys():
# check if argument is in the argument list # if keyword in domain_keywords list
if argument in StaticAnswers().keys(arg='list'): if keyword in StaticAnswers().keys('domain_keywords'):
# if argument uses a domain check for occurrence in list and check domain # if target is a domain / email return True
if argument in StaticAnswers().keys(arg='list', keyword='domain_keywords'):
try:
target = wordlist[index + 1]
if validators.domain(target): if validators.domain(target):
return True return True
elif validators.email(target): elif validators.email(target):
return True return True
except IndexError: # check if keyword is in number_keyword list
# except an IndexError if a keywords is the last word in the message elif keyword in StaticAnswers().keys('number_keywords'):
# if target only consists of digits return True
if target.isdigit():
return True
# if keyword is in no_arg_keywords list return True
elif keyword in StaticAnswers().keys("no_arg_keywords"):
return True
# if the target could not be validated until this return False
else:
return False return False
# check if number keyword is used if true check if target is assignable
elif argument in StaticAnswers().keys(arg='list', keyword='number_keywords'): #
try: class HandleError:
if wordlist[index + 1]: """
return True simple XMPP error / exception class formating the error condition
except IndexError: """
# except an IndexError if target is not assignable def __init__(self, error, key, target):
return False # init all necessary variables
# check if argument is inside no_arg list self.error = error
elif argument in StaticAnswers().keys(arg='list', keyword="no_arg_keywords"): self.key = key
return True self.target = target
else:
return False def report(self):
else: # return the formatted result string to the user
return False condition = self.error.condition
text = "There was an error requesting %s's %s : %s" % (self.target, self.key, condition)
return text

View file

@ -29,14 +29,13 @@ class StaticAnswers:
"number_keywords": ["!xep"] "number_keywords": ["!xep"]
} }
def keys(self, arg="", keyword='keywords'): def keys(self, key=""):
if arg == 'list': # if specific keyword in referenced return that
try: if key in self.keywords.keys():
return self.keywords[keyword] return self.keywords[key]
except KeyError: # in any other case return the whole dict
return self.keywords['keywords']
else: else:
return self.keywords return self.keywords["keywords"]
def gen_help(self): def gen_help(self):
helpdoc = "\n".join(['%s' % value for (_, value) in self.helpfile.items()]) helpdoc = "\n".join(['%s' % value for (_, value) in self.helpfile.items()])

108
main.py
View file

@ -17,8 +17,8 @@ from slixmpp.exceptions import XMPPError
import common.misc as misc import common.misc as misc
from common.strings import StaticAnswers from common.strings import StaticAnswers
from classes.functions import Version, ContactInfo, HandleError
from classes.servercontact import ServerContact from classes.servercontact import ServerContact
from classes.version import Version
from classes.uptime import LastActivity from classes.uptime import LastActivity
from classes.xep import XEPRequest from classes.xep import XEPRequest
@ -29,6 +29,14 @@ class QueryBot(slixmpp.ClientXMPP):
self.ssl_version = ssl.PROTOCOL_TLSv1_2 self.ssl_version = ssl.PROTOCOL_TLSv1_2
self.room = room self.room = room
self.nick = nick self.nick = nick
self.use_message_ids = True
self.functions = {
"!uptime": LastActivity(),
"!contact": ServerContact(),
"!version": Version(),
"!xep": XEPRequest()
}
self.data = { self.data = {
'words': list(), 'words': list(),
@ -72,56 +80,88 @@ class QueryBot(slixmpp.ClientXMPP):
# add pre predefined text to reply list # add pre predefined text to reply list
data['reply'].append(StaticAnswers(msg['mucnick']).gen_answer()) data['reply'].append(StaticAnswers(msg['mucnick']).gen_answer())
# building the queue data = self.build_queue(data, msg)
# double splitting to exclude whitespaces
data['words'] = " ".join(msg['body'].split()).split(sep=" ")
# check all words in side the message for possible hits
for x in enumerate(data['words']):
# check word for match in keywords list
for y in StaticAnswers().keys(arg='list'):
# if so queue the keyword and the position in the string
if x[1] == y:
# only add job to queue if domain is valid
if misc.validate(data['words'], x[0]):
data['queue'].append({str(y): x[0]})
# queue # queue
for job in data['queue']: for job in data['queue']:
for keyword in job: keys = list(job.keys())
index = job[keyword] keyword = keys[0]
target = job[keyword][0]
opt_arg = job[keyword][1]
query = None
if keyword == '!help': if keyword == '!help':
data['reply'].append(StaticAnswers().gen_help()) data['reply'].append(StaticAnswers().gen_help())
continue continue
target = data['words'][index + 1]
try: try:
if keyword == '!uptime': if keyword == "!uptime":
data['reply'].append(LastActivity(self, msg, target).format_values()) query = await self['xep_0012'].get_last_activity(jid=target)
#last_activity = await self['xep_0012'].get_last_activity(jid=target)
#data['reply'].append(LastActivity(last_activity, msg, target).format_values())
elif keyword == "!version": elif keyword == "!version":
version = await self['xep_0092'].get_version(jid=target) query = await self['xep_0092'].get_version(jid=target)
data['reply'].append(Version(version, msg, target).format_version())
elif keyword == "!contact": elif keyword == "!contact":
contact = await self['xep_0030'].get_info(jid=target, cached=False) query = await self['xep_0030'].get_info(jid=target, cached=False)
data['reply'].append(ServerContact(contact, msg, target).format_contact())
elif keyword == "!xep":
data['reply'].append(XEPRequest(msg, target).format())
except XMPPError as error: except XMPPError as error:
data['reply'].append(HandleError(error, msg, keyword, target).build_report()) logging.INFO(misc.HandleError(error, keyword, target).report())
data['reply'].append(misc.HandleError(error, keyword, target).report())
continue
data["reply"].append(self.functions[keyword].format(query=query, target=target, opt_arg=opt_arg))
# remove None type from list and send all elements # remove None type from list and send all elements
if list(filter(None.__ne__, data['reply'])) and data['reply']: if list(filter(None.__ne__, data['reply'])) and data['reply']:
reply = misc.deduplicate(data['reply'])
# if msg type is groupchat prepend mucnick
if msg["type"] == "groupchat":
data["reply"][0] = "%s: " % msg["mucnick"] + data["reply"][0]
# reply = misc.deduplicate(data['reply'])
reply = data["reply"]
self.send_message(mto=msg['from'].bare, mbody="\n".join(reply), mtype=msg['type']) self.send_message(mto=msg['from'].bare, mbody="\n".join(reply), mtype=msg['type'])
def build_queue(self, data, msg):
# building the queue
# double splitting to exclude whitespaces
data['words'] = " ".join(msg['body'].split()).split(sep=" ")
wordcount = len(data["words"])
# check all words in side the message for possible hits
for x in enumerate(data['words']):
# check for valid keywords
index = x[0]
keyword = x[1]
# match all words starting with ! and member of no_arg_keywords
if keyword.startswith("!") and keyword in StaticAnswers().keys("no_arg_keywords"):
data['queue'].append({keyword: [None, None]})
# matching all words starting with ! and member of keywords
elif keyword.startswith("!") and keyword in StaticAnswers().keys("keywords"):
# init variables to circumvent IndexErrors
target, opt_arg = None, None
# compare to wordcount if assignment is possible
if index + 1 < wordcount:
target = data["words"][index + 1]
if index + 2 < wordcount:
if not data["words"][index + 2].startswith("!"):
opt_arg = data["words"][index + 2]
# only add job to queue if domain is valid
if misc.validate(keyword, target):
logging.debug("Item added to queue %s" % {str(keyword): [target, opt_arg]})
data['queue'].append({str(keyword): [target, opt_arg]})
# deduplicate queue elements
data["queue"] = misc.deduplicate(data["queue"])
return data
if __name__ == '__main__': if __name__ == '__main__':
# command line arguments. # command line arguments.
@ -130,12 +170,12 @@ if __name__ == '__main__':
const=logging.ERROR, default=logging.INFO) const=logging.ERROR, default=logging.INFO)
parser.add_argument('-d', '--debug', help='set logging to DEBUG', action='store_const', dest='loglevel', parser.add_argument('-d', '--debug', help='set logging to DEBUG', action='store_const', dest='loglevel',
const=logging.DEBUG, default=logging.INFO) const=logging.DEBUG, default=logging.INFO)
parser.add_argument('-D', '--dev', help='set logging to console', action='store_const', dest='logfile', const="", parser.add_argument('-D', '--dev', help='set logging to console', action='store_const', dest='logfile',
default='bot.log') const="", default='bot.log')
args = parser.parse_args() args = parser.parse_args()
# logging # logging
logging.basicConfig(filename=args.logfile, level=args.loglevel, format='%(levelname)-8s %(message)s') logging.basicConfig(filename=args.logfile, level=logging.INFO, format='%(levelname)-8s %(message)s')
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
# configfile # configfile