diff options
author | nico <nico@magicbroccoli.de> | 2019-01-09 19:38:04 +0100 |
---|---|---|
committer | nico <nico@magicbroccoli.de> | 2019-01-09 19:38:04 +0100 |
commit | 134a674228b3d2b42cb7fe2a12f215a225e7544b (patch) | |
tree | 0643df4b2c3e0d0368eab6a2c01625fcb04d801f /main.py | |
parent | 9c3f6761e5f7749125a0d4939cd1809aabdfd2be (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 'main.py')
-rwxr-xr-x | main.py | 73 |
1 files changed, 40 insertions, 33 deletions
@@ -17,10 +17,7 @@ from slixmpp.exceptions import XMPPError import common.misc as misc from common.strings import StaticAnswers -from classes.servercontact import ServerContact -from classes.version import Version -from classes.uptime import LastActivity -from classes.xep import XEPRequest +from classes import * class QueryBot(slixmpp.ClientXMPP): @@ -35,7 +32,8 @@ class QueryBot(slixmpp.ClientXMPP): "!uptime": LastActivity(), "!contact": ServerContact(), "!version": Version(), - "!xep": XEPRequest() + "!xep": XEPRequest(), + "!help": Helper() } # session start event, starting point for the presence and roster requests @@ -72,8 +70,15 @@ class QueryBot(slixmpp.ClientXMPP): return elif self.nick in msg['body']: + if msg["type"] == "groupchat": + # discover role first + affiliation = self['xep_0045'].get_jid_property(msg["mucroom"], self.nick, "role") + if affiliation in ["moderator", "owner"]: + # discover real jid + realjid = self['xep_0045'].get_jid_property(msg["mucroom"], msg["mucnick"], "jid") + # add pre predefined text to reply list - data['reply'].append(StaticAnswers(msg['mucnick']).gen_answer()) + data['reply'].append(StaticAnswers().answers(msg['mucnick'])) data = self.build_queue(data, msg) @@ -86,10 +91,6 @@ class QueryBot(slixmpp.ClientXMPP): opt_arg = job[keyword][1] query = None - if keyword == '!help': - data['reply'].append(StaticAnswers().gen_help()) - continue - try: if keyword == "!uptime": query = await self['xep_0012'].get_last_activity(jid=target) @@ -101,7 +102,7 @@ class QueryBot(slixmpp.ClientXMPP): query = await self['xep_0030'].get_info(jid=target, cached=False) except XMPPError as error: - logging.info(misc.HandleError(error, keyword, target).report()) + logging.error(misc.HandleError(error, keyword, target).report()) data['reply'].append(misc.HandleError(error, keyword, target).report()) continue @@ -110,13 +111,15 @@ class QueryBot(slixmpp.ClientXMPP): # remove None type from list and send all elements if list(filter(None.__ne__, data['reply'])) and data['reply']: - # if msg type is groupchat prepend mucnick + # if msg type is groupchat if msg["type"] == "groupchat": - data["reply"][0] = "%s: " % msg["mucnick"] + data["reply"][0] - # reply = misc.deduplicate(data['reply']) - reply = data["reply"] - self.send_message(mto=msg['from'].bare, mbody="\n".join(reply), mtype=msg['type']) + # check if mucnick is present to tag the user + if msg["mucnick"] not in data["reply"][0]: + data["reply"][0] = "%s: " % msg["mucnick"] + data["reply"][0] + + # send the answer + self.send_message(mto=msg['from'].bare, mbody="\n".join(data["reply"]), mtype=msg['type']) def build_queue(self, data, msg): # building the queue @@ -125,35 +128,39 @@ class QueryBot(slixmpp.ClientXMPP): wordcount = len(data["words"]) # check all words in side the message for possible hits - for x in enumerate(data['words']): - # check for valid keywords - index = x[0] - keyword = x[1] - - # match all words starting with ! and member of no_arg_keywords - if keyword.startswith("!") and keyword in StaticAnswers().keys("no_arg_keywords"): - data['queue'].append({keyword: [None, None]}) - - # matching all words starting with ! and member of keywords - elif keyword.startswith("!") and keyword in StaticAnswers().keys("keywords"): + for index, keyword in enumerate(data['words']): + if keyword.startswith("!") and keyword in StaticAnswers().keys("all"): # init variables to circumvent IndexErrors target, opt_arg = None, None # compare to wordcount if assignment is possible if index + 1 < wordcount: - target = data["words"][index + 1] + if not data["words"][index + 1].startswith("!"): + target = data["words"][index + 1] if index + 2 < wordcount: if not data["words"][index + 2].startswith("!"): opt_arg = data["words"][index + 2] - # only add job to queue if domain is valid + # only add job to queue if target is valid if misc.validate(keyword, target): - logging.debug("Item added to queue %s" % {str(keyword): [target, opt_arg]}) - data['queue'].append({str(keyword): [target, opt_arg]}) + + # if keyword is a key in expand category expand it + if keyword in StaticAnswers().keys("expand").keys(): + # expand the keyword with the keyword list attached to it + for key in StaticAnswers().keys("expand")[keyword]: + data['queue'].append({key: [target, None]}) + + logging.debug("expanding keyword %s" % keyword) + + else: + data['queue'].append({keyword: [target, opt_arg]}) + logging.warning("Item added to queue %s" % {keyword: [target, opt_arg]}) # deduplicate queue elements + logging.warning("before dedup %s" %data["queue"]) data["queue"] = misc.deduplicate(data["queue"]) + logging.warning("after dedup %s" %data["queue"]) return data @@ -164,8 +171,8 @@ if __name__ == '__main__': parser.add_argument('-q', '--quiet', help='set logging to ERROR', action='store_const', dest='loglevel', const=logging.ERROR, default=logging.INFO) parser.add_argument('-d', '--debug', help='set logging to DEBUG', action='store_const', dest='loglevel', - const=logging.DEBUG, default=logging.INFO) - parser.add_argument('-D', '--dev', help='set logging to console', action='store_const', dest='logfile', + const=logging.WARNING, default=logging.INFO) + parser.add_argument('-D', '--dev', help='set log output to console', action='store_const', dest='logfile', const="", default='bot.log') args = parser.parse_args() |