From 9d452717786908d5a1e72e392d8c20239e415adf Mon Sep 17 00:00:00 2001 From: nico Date: Wed, 10 Oct 2018 17:43:01 +0200 Subject: + added etree implementation to grab contact addresses from bare xml --- classes/servercontact.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 classes/servercontact.py (limited to 'classes/servercontact.py') diff --git a/classes/servercontact.py b/classes/servercontact.py new file mode 100644 index 0000000..27b72f0 --- /dev/null +++ b/classes/servercontact.py @@ -0,0 +1,61 @@ +# -*- coding: utf-8 -*- +import defusedxml.ElementTree as Et + + +class ServerContact: + def __init__(self, contact, msg, target): + self.contact = contact + self.message = msg + self.target = target + + self.possible_vars = ['abuse-addresses', + 'admin-addresses', + 'feedback-addresses', + 'sales-addresses', + 'security-addresses', + 'support-addresses'] + + def process(self): + # get etree from base xml + iq = Et.fromstring(str(self.contact)) + + # check if query is a valid result query + if iq.find('{http://jabber.org/protocol/disco#info}query'): + # only init result dict if result query is present + result = dict() + + # extract query from iq + query = iq.find('{http://jabber.org/protocol/disco#info}query') + + # extract jabber:x:data from query + xdata = query.findall('{jabber:x:data}x') + + # check for multiple x nodes + for x in range(len(xdata)): + + # iterate over all x nodes + for child in xdata[x]: + + # if node has a var attribute that matches our list process + if child.attrib['var'] in self.possible_vars: + # add section to result dict and append info + result[child.attrib['var']] = list() + for value in child: + result[child.attrib['var']].append(value.text) + + return result + + def format_contact(self): + result = self.process() + + if result: + text = "contact addresses for %s are\n" % self.target + + for key in result.keys(): + if result[key]: + addr = ' , '.join(result[key]) + text += "- %s : %s\n" % (key, addr) + else: + text = "%s has no contact addresses configured." % self.target + + return text -- cgit v1.2.3-18-g5258 From 0c313565f2b649366f7382dc1b3f28a3e80f4ffc Mon Sep 17 00:00:00 2001 From: nico Date: Tue, 6 Nov 2018 23:43:11 +0100 Subject: 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 --- classes/servercontact.py | 45 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 10 deletions(-) (limited to 'classes/servercontact.py') diff --git a/classes/servercontact.py b/classes/servercontact.py index 27b72f0..ea7216d 100644 --- a/classes/servercontact.py +++ b/classes/servercontact.py @@ -2,12 +2,13 @@ import defusedxml.ElementTree as Et +# XEP-0157: Contact Addresses for XMPP Services class ServerContact: - def __init__(self, contact, msg, target): - self.contact = contact - self.message = msg - self.target = target - + """ + plugin to process the server contact addresses from a disco query + """ + def __init__(self): + # init all necessary variables self.possible_vars = ['abuse-addresses', 'admin-addresses', 'feedback-addresses', @@ -15,6 +16,9 @@ class ServerContact: 'security-addresses', 'support-addresses'] + self.contact = None + self.target, self.opt_arg = None, None + def process(self): # get etree from base xml iq = Et.fromstring(str(self.contact)) @@ -36,8 +40,16 @@ class ServerContact: # iterate over all x nodes 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 child.attrib['var'] in self.possible_vars: + elif child.attrib['var'] in self.possible_vars: # add section to result dict and append info result[child.attrib['var']] = list() for value in child: @@ -45,17 +57,30 @@ class ServerContact: 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() + # if result is present continue if result: 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(): - if result[key]: - addr = ' , '.join(result[key]) - text += "- %s : %s\n" % (key, addr) + addr = ' , '.join(result[key]) + text += "- %s : %s\n" % (key, addr) else: 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 -- cgit v1.2.3-18-g5258 From cd1442e216abf564daceaef5fe45555587eef69b Mon Sep 17 00:00:00 2001 From: nico Date: Fri, 9 Nov 2018 19:42:21 +0100 Subject: code quality improvements - remove unused variable * better iteration of xdata nodes - removed unnecessary else --- classes/servercontact.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'classes/servercontact.py') diff --git a/classes/servercontact.py b/classes/servercontact.py index ea7216d..749a2c3 100644 --- a/classes/servercontact.py +++ b/classes/servercontact.py @@ -34,11 +34,11 @@ class ServerContact: # extract jabber:x:data from query xdata = query.findall('{jabber:x:data}x') - # check for multiple x nodes - for x in range(len(xdata)): + # iterate over all nodes with the xdata tag + for node in xdata: - # iterate over all x nodes - for child in xdata[x]: + # iterate over all child elements in node + for child in node: # if one opt_arg is defined return just that one if self.opt_arg in self.possible_vars: -- cgit v1.2.3-18-g5258 From b6b84108ed24939a78ed4f6240b830860967136f Mon Sep 17 00:00:00 2001 From: nico Date: Fri, 9 Nov 2018 20:52:31 +0100 Subject: quality of life improvement to servercontact + added opt_arg abbreviation --- classes/servercontact.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'classes/servercontact.py') diff --git a/classes/servercontact.py b/classes/servercontact.py index 749a2c3..031ef67 100644 --- a/classes/servercontact.py +++ b/classes/servercontact.py @@ -19,7 +19,27 @@ class ServerContact: self.contact = None self.target, self.opt_arg = None, None + def opt_arg_abbreviation(self): + """ + optional argument abbreviation function + if the provided string > 2 characters the most likely key will be chosen + :return: completes the opt_arg to the most likely one + """ + # if opt_argument is smaller then 2 pass to prohibit multiple answers + if len(self.opt_arg) < 2: + pass + + abbr = str(self.opt_arg) + possible_abbr = ["abuse-addresses", "admin-addresses", "feedback-addresses", "sales-addresses", + "security-addresses", "support-addresses"] + + # searches the best match in the list of possible_abbr and completes the opt_arg to that + self.opt_arg = [s for s in possible_abbr if s.startswith(abbr)][0] + def process(self): + # optional argument abbreviation + self.opt_arg_abbreviation() + # get etree from base xml iq = Et.fromstring(str(self.contact)) @@ -70,7 +90,7 @@ class ServerContact: 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: + if self.opt_arg in self.possible_vars: text = "%s for %s are\n" % (self.opt_arg, self.target) for key in result.keys(): @@ -80,7 +100,7 @@ class ServerContact: 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: + if self.opt_arg in self.possible_vars: text = "%s for %s are not defined." % (self.opt_arg, self.target) return text -- cgit v1.2.3-18-g5258