2018-10-10 01:34:56 +02:00
|
|
|
# -*- 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
|
|
|
|
|
|
|
|
|
2018-11-06 23:43:11 +01:00
|
|
|
def validate(keyword, target):
|
2018-10-10 01:34:56 +02:00
|
|
|
"""
|
|
|
|
validation method to reduce malformed querys and unnecessary connection attempts
|
2018-11-06 23:43:11 +01:00
|
|
|
:param keyword: used keyword
|
|
|
|
:param target: provided target
|
2018-10-10 01:34:56 +02:00
|
|
|
:return: true if valid
|
|
|
|
"""
|
2018-11-09 19:56:09 +01:00
|
|
|
# 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
|
2018-11-06 23:43:11 +01:00
|
|
|
|
2018-11-09 19:56:09 +01:00
|
|
|
# check if keyword is in number_keyword list
|
|
|
|
elif keyword in StaticAnswers().keys('number_keywords'):
|
2018-11-11 04:00:16 +01:00
|
|
|
# prevent AttributeError if target is NoneType
|
|
|
|
if target is not None:
|
|
|
|
# if target only consists of digits return True
|
|
|
|
return target.isdigit()
|
2018-11-06 23:43:11 +01:00
|
|
|
|
2018-11-09 19:56:09 +01:00
|
|
|
# if keyword is in no_arg_keywords list return True
|
|
|
|
elif keyword in StaticAnswers().keys("no_arg_keywords"):
|
|
|
|
return True
|
2018-11-06 23:43:11 +01:00
|
|
|
|
|
|
|
# if the target could not be validated until this return False
|
2018-11-09 19:42:21 +01:00
|
|
|
return False
|
2018-11-06 23:43:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
class HandleError:
|
|
|
|
"""
|
|
|
|
simple XMPP error / exception class formating the error condition
|
|
|
|
"""
|
|
|
|
def __init__(self, error, key, target):
|
|
|
|
# init all necessary variables
|
2018-11-07 00:37:24 +01:00
|
|
|
self.text = error.text
|
|
|
|
self.condition = error.condition
|
2018-11-06 23:43:11 +01:00
|
|
|
self.key = key
|
|
|
|
self.target = target
|
|
|
|
|
|
|
|
def report(self):
|
|
|
|
# return the formatted result string to the user
|
2018-11-07 00:49:32 +01:00
|
|
|
text = "%s. %s %s resulted in: %s" % (self.text, self.key, self.target, self.condition)
|
2018-11-06 23:43:11 +01:00
|
|
|
|
|
|
|
return text
|