コード例 #1
0
ファイル: sms.py プロジェクト: nbr23/nemubot
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)
コード例 #2
0
ファイル: sms.py プロジェクト: pombredanne/nemubot
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)
コード例 #3
0
ファイル: rnd.py プロジェクト: nbr23/nemubot
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))]
コード例 #4
0
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(Command(choice[0][1:],
                                                choice[1:],
                                                to_response=msg.to_response,
                                                frm=msg.frm,
                                                server=msg.server))]
コード例 #5
0
ファイル: rnd.py プロジェクト: nbr23/nemubot
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
コード例 #6
0
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
コード例 #7
0
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
コード例 #8
0
ファイル: cat.py プロジェクト: pombredanne/nemubot
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
コード例 #9
0
ファイル: smmry.py プロジェクト: nbr23/nemubot
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
コード例 #10
0
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