xmpp-chatbot/common/misc.py
nico 0c313565f2
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
2018-11-06 23:43:11 +01:00

69 lines
1.7 KiB
Python
Executable file

# -*- 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
"""
# check if keyword is in the argument list
if keyword in StaticAnswers().keys():
# 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):
return True
elif 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
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
#
class HandleError:
"""
simple XMPP error / exception class formating the error condition
"""
def __init__(self, error, key, target):
# init all necessary variables
self.error = error
self.key = key
self.target = target
def report(self):
# return the formatted result string to the user
condition = self.error.condition
text = "There was an error requesting %s's %s : %s" % (self.target, self.key, condition)
return text