diff options
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() |