summaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authornico <nico@magicbroccoli.de>2018-11-11 03:12:11 +0100
committernico <nico@magicbroccoli.de>2018-11-11 03:13:33 +0100
commit69951bba37a85cf7527d08861ab1f2715576df49 (patch)
tree3798538b14cc67e7deed4adc44957b4cc4607019 /common
parent6bb9f1d5b87537ed6bccf0dd6efb7b80c6a81395 (diff)
parent1b13bdfd926e394cab2a2edd15ecabf0afcc4cf2 (diff)
Merge branch 'dev'
+ added more comments to xep requests + added opt_arg to version, xep and contact + implemented data dictionary to hold all data in main bot + added message_ids * updated gitignore file * partly reworked servercontact implementation * complete rework of uptime, version * part rework of xep requests * complete rework of validate function * updated HandleError function * part rework of StaticStrings function * complete rework of queue building and deduplication * logging parameter fix
Diffstat (limited to 'common')
-rwxr-xr-xcommon/misc.py62
-rw-r--r--common/strings.py55
2 files changed, 117 insertions, 0 deletions
diff --git a/common/misc.py b/common/misc.py
new file mode 100755
index 0000000..86798b8
--- /dev/null
+++ b/common/misc.py
@@ -0,0 +1,62 @@
+# -*- coding: utf-8 -*-
+import validators
+from common.strings import StaticAnswers
+
+
+def deduplicate(reply):
+ """
+ list deduplication method
+ :param list reply: list containing non unique items
+ :return: list containing unique items
+ """
+ reply_dedup = list()
+ for item in reply:
+ if item not in reply_dedup:
+ reply_dedup.append(item)
+
+ return reply_dedup
+
+
+def validate(keyword, target):
+ """
+ validation method to reduce malformed querys and unnecessary connection attempts
+ :param keyword: used keyword
+ :param target: provided target
+ :return: true if valid
+ """
+ # if keyword in domain_keywords list
+ if keyword in StaticAnswers().keys('domain_keywords'):
+ # if target is a domain / email return True
+ if validators.domain(target) or validators.email(target):
+ return True
+
+ # check if keyword is in number_keyword list
+ elif keyword in StaticAnswers().keys('number_keywords'):
+ # if target only consists of digits return True
+ return target.isdigit()
+
+ # 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
+ return False
+
+
+#
+class HandleError:
+ """
+ simple XMPP error / exception class formating the error condition
+ """
+ def __init__(self, error, key, target):
+ # init all necessary variables
+ self.text = error.text
+ self.condition = error.condition
+ self.key = key
+ self.target = target
+
+ def report(self):
+ # return the formatted result string to the user
+ text = "%s. %s %s resulted in: %s" % (self.text, self.key, self.target, self.condition)
+
+ return text
diff --git a/common/strings.py b/common/strings.py
new file mode 100644
index 0000000..6f1b629
--- /dev/null
+++ b/common/strings.py
@@ -0,0 +1,55 @@
+# -*- coding: utf-8 -*-
+from random import randint
+
+
+class StaticAnswers:
+ """
+ collection of callable static/ semi-static strings
+ """
+ def __init__(self, nick=""):
+ self.nickname = nick
+ self.helpfile = {
+ 'help': '!help -- display this text',
+ 'version': '!version domain.tld -- receive XMPP server version',
+ 'uptime': '!uptime domain.tld -- receive XMPP server uptime',
+ 'contact': '!contact domain.tld -- receive XMPP server contact address info',
+ 'xep': '!xep XEP Number -- recieve information about the specified XEP'
+ }
+ self.possible_answers = {
+ '1': 'I heard that, %s.',
+ '2': 'I am sorry for that %s.',
+ '3': '%s did you try turning it off and on again?'
+ }
+ self.error_messages = {
+ '1': 'not reachable',
+ '2': 'not a valid target'
+ }
+ self.keywords = {
+ "keywords": ["!help", "!uptime", "!version", "!contact", "!xep"],
+ "domain_keywords": ["!uptime", "!version", "!contact"],
+ "no_arg_keywords": ["!help"],
+ "number_keywords": ["!xep"]
+ }
+
+ def keys(self, key=""):
+ # if specific keyword in referenced return that
+ if key in self.keywords.keys():
+ return self.keywords[key]
+
+ # in any other case return the whole dict
+ return self.keywords["keywords"]
+
+ def gen_help(self):
+ helpdoc = "\n".join(['%s' % value for (_, value) in self.helpfile.items()])
+ return helpdoc
+
+ def gen_answer(self):
+ possible_answers = self.possible_answers
+ return possible_answers[str(randint(1, possible_answers.__len__()))] % self.nickname
+
+ def error(self,code):
+ try:
+ text = self.error_messages[str(code)]
+ except KeyError:
+ return 'unknown error'
+ return text