From 31ddc8aeb47a19ff038678fb55992338aad0b6b1 Mon Sep 17 00:00:00 2001 From: nico Date: Wed, 10 Oct 2018 01:34:56 +0200 Subject: new async logic --- common/misc.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100755 common/misc.py (limited to 'common') diff --git a/common/misc.py b/common/misc.py new file mode 100755 index 0000000..3961b8d --- /dev/null +++ b/common/misc.py @@ -0,0 +1,59 @@ +# -*- 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(wordlist, index): + """ + validation method to reduce malformed querys and unnecessary connection attempts + :param wordlist: words separated by " " from the message + :param index: keyword index inside the message + :return: true if valid + """ + # keyword inside the message + argument = wordlist[index] + + # check if argument is in the argument list + if argument in StaticAnswers().keys(arg='list'): + # if argument uses a domain check for occurrence in list and check domain + if argument in StaticAnswers().keys(arg='list', keyword='domain_keywords'): + try: + target = wordlist[index + 1] + if validators.domain(target): + return True + elif validators.email(target): + return True + + except IndexError: + # except an IndexError if a keywords is the last word in the message + return False + + # check if number keyword is used if true check if target is assignable + elif argument in StaticAnswers().keys(arg='list', keyword='number_keywords'): + try: + if wordlist[index + 1]: + return True + except IndexError: + # except an IndexError if target is not assignable + return False + # check if argument is inside no_arg list + elif argument in StaticAnswers().keys(arg='list', keyword="no_arg_keywords"): + return True + else: + return False + else: + return False -- cgit v1.2.3-18-g5258