summaryrefslogtreecommitdiffstats
path: root/classes
diff options
context:
space:
mode:
authornico <nico@magicbroccoli.de>2019-01-09 19:38:04 +0100
committernico <nico@magicbroccoli.de>2019-01-09 19:38:04 +0100
commit134a674228b3d2b42cb7fe2a12f215a225e7544b (patch)
tree0643df4b2c3e0d0368eab6a2c01625fcb04d801f /classes
parent9c3f6761e5f7749125a0d4939cd1809aabdfd2be (diff)
initial rework of the implemented functionsdev
+ added __init__ import file for all functions + added new helper function + added strings.json file * some logic fixups * validator fixup * arg abbreviation moved to misc
Diffstat (limited to 'classes')
-rw-r--r--classes/__init__.py5
-rw-r--r--classes/help.py29
-rw-r--r--classes/servercontact.py26
-rw-r--r--classes/version.py11
-rw-r--r--classes/xep.py2
5 files changed, 46 insertions, 27 deletions
diff --git a/classes/__init__.py b/classes/__init__.py
new file mode 100644
index 0000000..838c8c2
--- /dev/null
+++ b/classes/__init__.py
@@ -0,0 +1,5 @@
+from classes.version import Version
+from classes.servercontact import ServerContact
+from classes.uptime import LastActivity
+from classes.xep import XEPRequest
+from classes.help import Helper
diff --git a/classes/help.py b/classes/help.py
new file mode 100644
index 0000000..c268520
--- /dev/null
+++ b/classes/help.py
@@ -0,0 +1,29 @@
+# -*- coding: utf-8 -*-
+from common.misc import arg_abbr
+from common.strings import StaticAnswers
+import json
+
+
+class Helper:
+ def __init__(self):
+ self.possible_vars = StaticAnswers().helpfile["help_advanced"].keys()
+
+ def receive(self, target):
+ # optional argument abbreviation
+ target = arg_abbr(target, self.possible_vars)
+
+ if target in self.possible_vars:
+ return StaticAnswers().help_doc(target)
+ else:
+ return StaticAnswers().help_doc()
+
+ def format(self, query, target, opt_arg):
+ doc = self.receive(target)
+
+ if target in self.possible_vars:
+ answer = json.dumps(StaticAnswers().help_doc(target), indent=4)
+
+ else:
+ answer = "\n".join(['%s' % value for (_, value) in doc.items()])
+
+ return answer
diff --git a/classes/servercontact.py b/classes/servercontact.py
index c2f4ad5..e0e871e 100644
--- a/classes/servercontact.py
+++ b/classes/servercontact.py
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
+from common.misc import arg_abbr
import defusedxml.ElementTree as Et
@@ -19,23 +20,6 @@ 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):
# get etree from base xml
iq = Et.fromstring(str(self.contact))
@@ -57,11 +41,11 @@ class ServerContact:
# 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:
- # check for possible abbreviations to the optional argument
- self.opt_arg_abbreviation()
+ # check for possible abbreviations to the optional argument
+ self.opt_arg = arg_abbr(self.opt_arg, self.possible_vars)
+ # if opt_arg is defined and valid 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()
diff --git a/classes/version.py b/classes/version.py
index 1e9ef7e..42b3e61 100644
--- a/classes/version.py
+++ b/classes/version.py
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
-
+from common.misc import arg_abbr
# XEP-0072: Server Version
class Version:
@@ -10,17 +10,18 @@ class Version:
# init all necessary variables
self.software_version = None
self.target, self.opt_arg = None, None
+ self.possible_vars = ["version", "os", "name"]
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']
+ # check for possible abbreviations to the optional argument
+ self.opt_arg = arg_abbr(self.opt_arg, self.possible_vars)
+
# if opt_arg is given member of possible_opt_args list return that element
- if self.opt_arg in possible_opt_args:
+ if self.opt_arg in self.possible_vars:
text = "%s: %s" % (self.opt_arg, self.software_version[self.opt_arg])
# otherwise return full version string
diff --git a/classes/xep.py b/classes/xep.py
index f5fae61..df401a7 100644
--- a/classes/xep.py
+++ b/classes/xep.py
@@ -47,10 +47,10 @@ class XEPRequest:
r = s.get("https://xmpp.org/extensions/xeplist.xml")
r.encoding = 'utf-8'
local_etag = head.headers['etag']
+ self.xeplist = Et.fromstring(r.content.decode())
with open("./common/xeplist.xml", "w") as file:
file.write(r.content.decode())
- self.xeplist = Et.fromstring(r.content.decode())
with open('./common/.etag', 'w') as string:
string.write(local_etag)