Пример #1
0
def get_factoid(bot, msg, key):
    """
    Get a factoid. Invoked with `foo?` or `foo!` for a factoid named foo.
    """
    key = util.flatten_incoming_text(bot, key)
    if key in factoids:
        bot.reply(factoids[key])
Пример #2
0
def get_factoid(bot, msg, key):
    """
    Get a factoid. Invoked with `foo?` or `foo!` for a factoid named foo.
    """
    key = util.flatten_incoming_text(bot, key)
    if key in factoids:
        bot.reply(factoids[key])
Пример #3
0
def code(bot, msg, language, _, code):
    """
    Run arbitrary code of the specified language.

    Usage:
      @you: @bot python `print [x ** 2 for x in xrange(10) if x % 2]`
      @bot: [1, 9, 25, 49, 81]

    Valid languages include python, py3, ruby, coffeescript, gcc (C),
    and php.
    """
    uri = 'https://eval.in/'
    data = {
        "utf8": "\xce\xbb",
        "execute": "on",
        "private": "on",
        "lang": supported_languages[language],
        "input": "",
        "code": util.flatten_incoming_text(bot, code).encode('utf-8'),
    }
    response = requests.post(uri, data)
    bot.debug(response.url)
    _, html = response.content.split("<h2>Program Output</h2>", 1)
    html = html.lstrip()
    html = html[5: html.index("</pre>")]
    output = util.unescape(html).rstrip().decode('utf-8')
    if output:
        try:
            bot.reply(u"```{}```".format(output))
        except exception.MessageTooLongException:
            bot.reply(response.url)
    else:
        bot.reply("No output...")
Пример #4
0
def set_factoid(bot, msg, key, verb, value):
    """
    Set a factoid. Invoked with `@bot: [key] is [value]` or
    `@bot: [key] are value`. You can also use the form
    `@bot: [key] is <reply>[value]` to have the bot respond with only the
    value instead of repeating "[key] is".
    """
    key = util.flatten_incoming_text(bot, key)
    value = util.flatten_incoming_text(bot, value)

    factoid = Factoid(key=key, verb=verb, value=value, reply=False)

    if value.startswith('<reply>'):
        factoid.value = re.sub(r'^<reply>\s*', '', value)
        factoid.reply = True

    factoids[key] = factoid
    bot.reply('ok')
Пример #5
0
def set_factoid(bot, msg, key, verb, value):
    """
    Set a factoid. Invoked with `@bot: [key] is [value]` or
    `@bot: [key] are value`. You can also use the form
    `@bot: [key] is <reply>[value]` to have the bot respond with only the
    value instead of repeating "[key] is".
    """
    key = util.flatten_incoming_text(bot, key)
    value = util.flatten_incoming_text(bot, value)

    factoid = Factoid(key=key, verb=verb, value=value, reply=False)

    if value.startswith('<reply>'):
        factoid.value = re.sub(r'^<reply>\s*', '', value)
        factoid.reply = True

    factoids[key] = factoid
    bot.reply('ok')
Пример #6
0
def forget_factoid(bot, msg, key):
    """
    Forget a factoid. Invoked with `@bot: forget foo` for a factoid named foo.
    """
    key = util.flatten_incoming_text(bot, key)
    try:
        del factoids[key]
        bot.reply('ok')
    except KeyError:
        bot.reply('no factoid named {key}'.format(key))
Пример #7
0
def forget_factoid(bot, msg, key):
    """
    Forget a factoid. Invoked with `@bot: forget foo` for a factoid named foo.
    """
    key = util.flatten_incoming_text(bot, key)
    try:
        del factoids[key]
        bot.reply('ok')
    except KeyError:
        bot.reply('no factoid named {key}'.format(key))
Пример #8
0
def choose(bot, msg, choose_string):
    """
    Given a sequence definition, return an element at random from that
    sequence.

    Sequence definition syntax is detailed in `shuffle`.
    """
    choose_string = util.flatten_incoming_text(bot, choose_string, flatten_notice=False)
    success, choose_list = _parse_shuffle_set(bot, choose_string)
    if not success:
        bot.reply('\n'.join(choose_list))
        return
    bot.reply(" {}".format(random.choice(choose_list)))
Пример #9
0
def shuffle(bot, msg, shuffle_string):
    """
    Given a sequence definition, return the elements of that sequence
    in a random order.

    Note that if the sequence is over 200 elements, only the first 200
    will be shown.

    A sequence is generated by the following rules:
    1. If the sequence definition contains spaces the spaces are
       considered a delimiter and each of the tokens is considered
       literally.

       Example:
       @you: @bot shuffle four score and seven years ago
       @bot: years, seven, four, ago, score, and

    2. Otherwise, commas are used as the delimiter. Each comma delimited
       element is interpreted as follows:
       i. Two integers of the format ``a:b`` are a range from ``a``
           to ``b``, inclusive.
       ii. The @channel mention, if in a channel, is expanded as the
           nick of every person in the channel. Note that since spaces
           are disallowed the @channel mention must come first.
       iii. A literal string. If a literal string is the only
            comma-delimited element it is interpreted as its characters.

       Example:
       @you: @bot shuffle @channel,4:6,word
       @bot: 5, @you, 6, @bot, word, 4

       @you: @bot shuffle exemplify
       @bot: l, f, i, m, x, p, y, e, e
    """
    CUTOFF = 200
    shuffle_string = util.flatten_incoming_text(bot,
                                                shuffle_string,
                                                flatten_notice=False)
    success, shuffle_list = _parse_shuffle_set(bot, shuffle_string)
    if not success:
        bot.reply('\n'.join(shuffle_list))
        return
    shuffle_list = random.sample(shuffle_list, min(len(shuffle_list), CUTOFF))
    bot.reply(" " + ", ".join(map(str, shuffle_list)))
Пример #10
0
def shuffle(bot, msg, shuffle_string):
    """
    Given a sequence definition, return the elements of that sequence
    in a random order.

    Note that if the sequence is over 200 elements, only the first 200
    will be shown.

    A sequence is generated by the following rules:
    1. If the sequence definition contains spaces the spaces are
       considered a delimiter and each of the tokens is considered
       literally.

       Example:
       @you: @bot shuffle four score and seven years ago
       @bot: years, seven, four, ago, score, and

    2. Otherwise, commas are used as the delimiter. Each comma delimited
       element is interpreted as follows:
       i. Two integers of the format ``a:b`` are a range from ``a``
           to ``b``, inclusive.
       ii. The @channel mention, if in a channel, is expanded as the
           nick of every person in the channel. Note that since spaces
           are disallowed the @channel mention must come first.
       iii. A literal string. If a literal string is the only
            comma-delimited element it is interpreted as its characters.

       Example:
       @you: @bot shuffle @channel,4:6,word
       @bot: 5, @you, 6, @bot, word, 4

       @you: @bot shuffle exemplify
       @bot: l, f, i, m, x, p, y, e, e
    """
    CUTOFF = 200
    shuffle_string = util.flatten_incoming_text(bot, shuffle_string, flatten_notice=False)
    success, shuffle_list = _parse_shuffle_set(bot, shuffle_string)
    if not success:
        bot.reply('\n'.join(shuffle_list))
        return
    shuffle_list = random.sample(shuffle_list, min(len(shuffle_list), CUTOFF))
    bot.reply(" " + ", ".join(map(str, shuffle_list)))