def cmd_choicecmd(msg): if not len(msg.args): raise IMException("indicate some command to pick!") choice = shlex.split(random.choice(msg.args)) return [x for x in context.subtreat(context.subparse(msg, choice))]
def cmd_smscmd(msg): if not len(msg.args): raise IMException("À qui veux-tu envoyer ce SMS ?") cur_epoch = time.mktime(time.localtime()) dests = msg.args[0].split(",") frm = msg.frm if msg.to_response[0] == msg.frm else msg.frm + "@" + msg.to[0] cmd = " ".join(msg.args[1:]) content = None for r in context.subtreat(context.subparse(msg, cmd)): if isinstance(r, Response): for m in r.messages: if isinstance(m, list): for n in m: content = n break if content is not None: break elif isinstance(m, str): content = m break elif isinstance(r, Text): content = r.message if content is None: raise IMException("Aucun SMS envoyé : le résultat de la commande n'a pas retourné de contenu.") check_sms_dests(dests, cur_epoch) return send_sms_to_list(msg, frm, dests, content, cur_epoch)
def cmd_alias(msg): if not len(msg.args): raise IMException("!alias takes as argument an alias to extend.") alias = context.subparse(msg, msg.args[0]) if alias is None or not isinstance(alias, Command): raise IMException("%s is not a valid alias" % msg.args[0]) if alias.cmd in context.data.getNode("aliases").index: return Response( "%s corresponds to %s" % (alias.cmd, context.data.getNode("aliases").index[alias.cmd]["origin"]), channel=msg.channel, nick=msg.frm) elif len(msg.args) > 1: create_alias(alias.cmd, " ".join(msg.args[1:]), channel=msg.channel, creator=msg.frm) return Response("New alias %s successfully registered." % alias.cmd, channel=msg.channel) else: wym = [ m for m in guess(alias.cmd, context.data.getNode("aliases").index) ] raise IMException(msg.args[0] + " is not an alias." + (" Would you mean: %s?" % ", ".join(wym) if len(wym) else ""))
def cmd_smscmd(msg): if not len(msg.args): raise IMException("À qui veux-tu envoyer ce SMS ?") cur_epoch = time.mktime(time.localtime()) dests = msg.args[0].split(",") frm = msg.frm if msg.to_response[ 0] == msg.frm else msg.frm + "@" + msg.to[0] cmd = " ".join(msg.args[1:]) content = None for r in context.subtreat(context.subparse(msg, cmd)): if isinstance(r, Response): for m in r.messages: if isinstance(m, list): for n in m: content = n break if content is not None: break elif isinstance(m, str): content = m break elif isinstance(r, Text): content = r.message if content is None: raise IMException( "Aucun SMS envoyé : le résultat de la commande n'a pas retourné de contenu." ) check_sms_dests(dests, cur_epoch) return send_sms_to_list(msg, frm, dests, content, cur_epoch)
def treat_alias(msg): if context.data.getNode("aliases") is not None and msg.cmd in context.data.getNode("aliases").index: origin = context.data.getNode("aliases").index[msg.cmd]["origin"] rpl_msg = context.subparse(msg, origin) if isinstance(rpl_msg, Command): rpl_msg.args = replace_variables(rpl_msg.args, msg) rpl_msg.args += msg.args rpl_msg.kwargs.update(msg.kwargs) elif len(msg.args) or len(msg.kwargs): raise IMException("This kind of alias doesn't take any argument (haven't you forgotten the '!'?).") # Avoid infinite recursion if not isinstance(rpl_msg, Command) or msg.cmd != rpl_msg.cmd: return rpl_msg return msg
def cmd_choiceres(msg): if not len(msg.args): raise IMException("indicate some command to pick a message from!") rl = [x for x in context.subtreat(context.subparse(msg, " ".join(msg.args)))] if len(rl) <= 0: return rl r = random.choice(rl) if isinstance(r, Response): for i in range(len(r.messages) - 1, -1, -1): if isinstance(r.messages[i], list): r.messages = [ random.choice(random.choice(r.messages)) ] elif isinstance(r.messages[i], str): r.messages = [ random.choice(r.messages) ] return r
def grep(fltr, cmd, msg, icase=False, only=False): """Perform a grep like on known nemubot structures Arguments: fltr -- The filter regexp cmd -- The subcommand to execute msg -- The original message icase -- like the --ignore-case parameter of grep only -- like the --only-matching parameter of grep """ fltr = re.compile(fltr, re.I if icase else 0) for r in context.subtreat(context.subparse(msg, cmd)): if isinstance(r, Response): for i in range(len(r.messages) - 1, -1, -1): if isinstance(r.messages[i], list): for j in range(len(r.messages[i]) - 1, -1, -1): res = fltr.match(r.messages[i][j]) if not res: r.messages[i].pop(j) elif only: r.messages[i][j] = res.group(1) if fltr.groups else res.group(0) if len(r.messages[i]) <= 0: r.messages.pop(i) elif isinstance(r.messages[i], str): res = fltr.match(r.messages[i]) if not res: r.messages.pop(i) elif only: r.messages[i] = res.group(1) if fltr.groups else res.group(0) yield r elif isinstance(r, Text): res = fltr.match(r.message) if res: if only: r.message = res.group(1) if fltr.groups else res.group(0) yield r else: yield r
def treat_alias(msg): if context.data.getNode( "aliases") is not None and msg.cmd in context.data.getNode( "aliases").index: origin = context.data.getNode("aliases").index[msg.cmd]["origin"] rpl_msg = context.subparse(msg, origin) if isinstance(rpl_msg, Command): rpl_msg.args = replace_variables(rpl_msg.args, msg) rpl_msg.args += msg.args rpl_msg.kwargs.update(msg.kwargs) elif len(msg.args) or len(msg.kwargs): raise IMException( "This kind of alias doesn't take any argument (haven't you forgotten the '!'?)." ) # Avoid infinite recursion if not isinstance(rpl_msg, Command) or msg.cmd != rpl_msg.cmd: return rpl_msg return msg
def cat(msg, *terms): res = Response(channel=msg.to_response, server=msg.server) for term in terms: m = context.subparse(msg, term) if isinstance(m, Command) or isinstance(m, DirectAsk): for r in context.subtreat(m): if isinstance(r, Response): for t in range(len(r.messages)): res.append_message(r.messages[t], title=r.rawtitle if not isinstance(r.rawtitle, list) else r.rawtitle[t]) elif isinstance(r, Text): res.append_message(r.message) elif isinstance(r, str): res.append_message(r) else: res.append_message(term) return res
def cmd_alias(msg): if not len(msg.args): raise IMException("!alias takes as argument an alias to extend.") alias = context.subparse(msg, msg.args[0]) if alias is None or not isinstance(alias, Command): raise IMException("%s is not a valid alias" % msg.args[0]) if alias.cmd in context.data.getNode("aliases").index: return Response("%s corresponds to %s" % (alias.cmd, context.data.getNode("aliases").index[alias.cmd]["origin"]), channel=msg.channel, nick=msg.frm) elif len(msg.args) > 1: create_alias(alias.cmd, " ".join(msg.args[1:]), channel=msg.channel, creator=msg.frm) return Response("New alias %s successfully registered." % alias.cmd, channel=msg.channel) else: wym = [m for m in guess(alias.cmd, context.data.getNode("aliases").index)] raise IMException(msg.args[0] + " is not an alias." + (" Would you mean: %s?" % ", ".join(wym) if len(wym) else ""))
def cmd_smmry(msg): if not len(msg.args): global LAST_URLS if msg.channel in LAST_URLS and len(LAST_URLS[msg.channel]) > 0: msg.args.append(LAST_URLS[msg.channel].pop()) else: raise IMException("I have no more URL to sum up.") URL = URL_API if "length" in msg.kwargs: if int(msg.kwargs["length"]) > 0 : URL += "&SM_LENGTH=" + msg.kwargs["length"] else: msg.kwargs["ignore_length"] = True if "break" in msg.kwargs: URL += "&SM_WITH_BREAK" if "ignore_length" in msg.kwargs: URL += "&SM_IGNORE_LENGTH" if "quote_avoid" in msg.kwargs: URL += "&SM_QUOTE_AVOID" if "question_avoid" in msg.kwargs: URL += "&SM_QUESTION_AVOID" if "exclamation_avoid" in msg.kwargs: URL += "&SM_EXCLAMATION_AVOID" if "keywords" in msg.kwargs and msg.kwargs["keywords"] is not None and int(msg.kwargs["keywords"]) > 0: URL += "&SM_KEYWORD_COUNT=" + msg.kwargs["keywords"] res = Response(channel=msg.channel) if web.isURL(" ".join(msg.args)): smmry = web.getJSON(URL + "&SM_URL=" + quote(" ".join(msg.args)), timeout=23) else: cnt = "" for r in context.subtreat(context.subparse(msg, " ".join(msg.args))): if isinstance(r, Response): for i in range(len(r.messages) - 1, -1, -1): if isinstance(r.messages[i], list): for j in range(len(r.messages[i]) - 1, -1, -1): cnt += r.messages[i][j] + "\n" elif isinstance(r.messages[i], str): cnt += r.messages[i] + "\n" else: cnt += str(r.messages) + "\n" elif isinstance(r, Text): cnt += r.message + "\n" else: cnt += str(r) + "\n" smmry = web.getJSON(URL, body="sm_api_input=" + quote(cnt), timeout=23) if "sm_api_error" in smmry: if smmry["sm_api_error"] == 0: title = "Internal server problem (not your fault)" elif smmry["sm_api_error"] == 1: title = "Incorrect submission variables" elif smmry["sm_api_error"] == 2: title = "Intentional restriction (low credits?)" elif smmry["sm_api_error"] == 3: title = "Summarization error" else: title = "Unknown error" raise IMException(title + ": " + smmry['sm_api_message'].lower()) if "keywords" in msg.kwargs: smmry["sm_api_content"] = ", ".join(smmry["sm_api_keyword_array"]) if "sm_api_title" in smmry and smmry["sm_api_title"] != "": res.append_message(smmry["sm_api_content"], title=smmry["sm_api_title"]) else: res.append_message(smmry["sm_api_content"]) return res
def cmd_smmry(msg): if not len(msg.args): global LAST_URLS if msg.channel in LAST_URLS and len(LAST_URLS[msg.channel]) > 0: msg.args.append(LAST_URLS[msg.channel].pop()) else: raise IMException("I have no more URL to sum up.") URL = URL_API if "length" in msg.kwargs: if int(msg.kwargs["length"]) > 0: URL += "&SM_LENGTH=" + msg.kwargs["length"] else: msg.kwargs["ignore_length"] = True if "break" in msg.kwargs: URL += "&SM_WITH_BREAK" if "ignore_length" in msg.kwargs: URL += "&SM_IGNORE_LENGTH" if "quote_avoid" in msg.kwargs: URL += "&SM_QUOTE_AVOID" if "question_avoid" in msg.kwargs: URL += "&SM_QUESTION_AVOID" if "exclamation_avoid" in msg.kwargs: URL += "&SM_EXCLAMATION_AVOID" if "keywords" in msg.kwargs and msg.kwargs["keywords"] is not None and int( msg.kwargs["keywords"]) > 0: URL += "&SM_KEYWORD_COUNT=" + msg.kwargs["keywords"] res = Response(channel=msg.channel) if web.isURL(" ".join(msg.args)): smmry = web.getJSON(URL + "&SM_URL=" + quote(" ".join(msg.args)), timeout=23) else: cnt = "" for r in context.subtreat(context.subparse(msg, " ".join(msg.args))): if isinstance(r, Response): for i in range(len(r.messages) - 1, -1, -1): if isinstance(r.messages[i], list): for j in range(len(r.messages[i]) - 1, -1, -1): cnt += r.messages[i][j] + "\n" elif isinstance(r.messages[i], str): cnt += r.messages[i] + "\n" else: cnt += str(r.messages) + "\n" elif isinstance(r, Text): cnt += r.message + "\n" else: cnt += str(r) + "\n" smmry = web.getJSON(URL, body="sm_api_input=" + quote(cnt), timeout=23) if "sm_api_error" in smmry: if smmry["sm_api_error"] == 0: title = "Internal server problem (not your fault)" elif smmry["sm_api_error"] == 1: title = "Incorrect submission variables" elif smmry["sm_api_error"] == 2: title = "Intentional restriction (low credits?)" elif smmry["sm_api_error"] == 3: title = "Summarization error" else: title = "Unknown error" raise IMException(title + ": " + smmry['sm_api_message'].lower()) if "keywords" in msg.kwargs: smmry["sm_api_content"] = ", ".join(smmry["sm_api_keyword_array"]) if "sm_api_title" in smmry and smmry["sm_api_title"] != "": res.append_message(smmry["sm_api_content"], title=smmry["sm_api_title"]) else: res.append_message(smmry["sm_api_content"]) return res