Beispiel #1
0
 def test_command_update_no_new_comics(self):
     response = xkcd.get_url(xkcd.api_url % "")
     max_comic = json.loads(response.decode("utf-8"))["num"]
     xkcd.cur_max_comic = max_comic
     output = xkcd.command_update()
     expected_output = "No new comics.\n"
     self.assertEqual(output, expected_output)
Beispiel #2
0
 def test_command_update_1_comic(self):
     response = xkcd.get_url(xkcd.api_url % "")
     new_max_comic = json.loads(response.decode("utf-8"))["num"] - 1
     xkcd.cur_max_comic = new_max_comic
     output = xkcd.command_update()
     expected_output = "1 new comic!\n"
     self.assertEqual(output, expected_output)
async def xkcd(ctx, number=None):
    """Grabs an XKCD comic, random if no number is supplied"""
    valid = True
    if number:
        valid = validate(number)
        if not valid:
            await ctx.send("Please give a valid number")
            return
    post = get_url(number) if number else get_random_url()
    await ctx.send(post)
Beispiel #4
0
def process_command(line, sender, channel):
    # !help -- get help about TuxBot's commads
    match = re.match(r'help$', line)
    if match:
        irc.send_private_notice(commandref, sender)
        if sender in channel_ops[channel] + channel_voices[channel]:
            irc.send_private_notice(opcommandref, sender)
        return True

    # !help <key> -- get help
    match = re.match(r'help\s+(.*)$', line)
    if match:
        key = clean_string(match.group(1))
        text = config.get_help(key)
        if text:
            irc.send_message("%s" % (text), channel)
        else:
            irc.send_message("I don't have an answer for \"%s\". You can set it using the \"!sethelp question: answer\" command." % (key), channel)
        return True

    # !man <section> <name> -- get the URL to an online man page
    match = re.match(r'man\s+(\w+)\s+([-A-Za-z0-9_]+)$', line)
    if match:
        irc.send_message(man.get(match.group(1), match.group(2)), channel)
        return True
    # !synopsis <section> <name> -- print the "SYNOPSIS" section of the specified man page
    match = re.match(r'synopsis\s+(\w+)\s+(\w+)$', line)
    if match:
        text = man.synopsis(match.group(1), match.group(2))
        if not text:
            irc.send_message("Failed to get man page for \"%s\" in section \"%s\"" % (match.group(2), match.group(1)), channel)
            return True
        irc.send_message(text, channel)
        return True
    # !man <criteria> -- search for an online man page
    match = re.match(r'man\s+(\w+)$', line)
    if match:
        irc.send_message(man.search(match.group(1)), channel)
        return True

    # !xkcd -- get a random xkcd comic
    match = re.match(r'xkcd$', line)
    if match:
        irc.send_message(xkcd.get_random(), channel)
        return True
    # !xkcd <index> -- get an xkcd comic by index
    match = re.match(r'xkcd\s+([0-9]+)$', line)
    if match:
        irc.send_message(xkcd.get_url(int(match.group(1))), channel)
        return True

    # !xkcd-linux and !xkcd-geek -- get linux-related and geeky xkcd comics
    match = re.match(r'xkcd-linux$', line)
    if match:
        l = config.get_linux_xkcds()
        if not l:
            irc.send_message("No linux-related comics in list.", channel)
        else:
            irc.send_message(xkcd.get_url(int(l[random.randint(0, len(l)-1)])), channel)
        return True
    match = re.match(r'xkcd-geek$', line)
    if match:
        l = config.get_geek_xkcds()
        if not l:
            irc.send_message("No linux-related comics in list.", channel)
        else:
            irc.send_message(xkcd.get_url(int(l[random.randint(0, len(l)-1)])), channel)
        return True

    # !google <criteria> -- get the URL for a Google search
    match = re.match(r'google\s+([^\s].+)$', line)
    if match:
        irc.send_message("https://encrypted.google.com/#q=" + clean_string(match.group(1)).replace(" ", "+"), channel)
        return True

    # !wikipedia -- get a random wikipedia article
    match = re.match(r'wikipedia$', line)
    if match:
        irc.send_message("http://en.wikipedia.org/wiki/Special:Random", channel)
        return True
    # !wikipedia <article> -- get a link to wikipedia article
    match = re.match(r'wikipedia\s+([^\s].+)$', line)
    if match:
        irc.send_message("http://en.wikipedia.org/wiki/Special:Search?search=" + clean_string(match.group(1)).replace(" ", "+"), channel)
        return True
    # !wikipedia-<lang> -- get a random wikipedia article in a certain language
    match = re.match(r'wikipedia-(\w+)$', line)
    if match:
        irc.send_message("http://"+match.group(1)+".wikipedia.org/wiki/Special:Random", channel)
        return True
    # !wikipedia-<lang> <article> -- get a link to wikipedia article in a certain language
    match = re.match(r'wikipedia-(\w+)\s+([^\s].+)$', line)
    if match:
        irc.send_message("http://"+match.group(1)+".wikipedia.org/wiki/Special:Search?search=" + clean_string(match.group(2)).replace(" ", "+"), channel)
        return True

    # !time and !date -- get the current time
    match = re.match(r'(time|date)$', line)
    if match:
        irc.send_message(time.strftime("%A %Y-%m-%d %H:%M:%S %Z"), channel)
        return True
    match = re.match(r'(time|date)\s+([^\s].*)$', line)
    if match:
        irc.send_message(time.strftime(match.group(2)), channel)
        return True

    # !tr[anslate]-<fromlang> <text> -- translate some text to English
    match = re.match(r'tr(anslate)?-([a-z]+)\s+([^\s].*)', line)
    if match:
        irc.send_message(translator.translate(match.group(2), "en", match.group(3)), channel)
        return True;

    # !tr[anslate]-<fromlang>-<tolang> <text> -- translate some text
    match = re.match(r'tr(anslate)?-([a-z]+)-([a-z]+)\s+([^\s].*)', line)
    if match:
        irc.send_message(translator.translate(match.group(2), match.group(3), match.group(4)), channel)
        return True;

    # !license or !authors or !credits -- display license information and the names of the people who made TuxBot
    match = re.match(r'credits|authors|license$', line)
    if match:
        irc.send_private_notice(license, sender)
        return True

    # !user -- display the username which this python script is running under
    match = re.match(r'user$', line)
    if match:
        irc.send_message(getpass.getuser(), channel)
        return True

    # !version -- get TuxBot's version. Assumes that TuxBot is run from its git repository directory
    match = re.match(r'version$', line)
    if match:
        irc.send_message(version, channel)
        return True

    # !quit -- make TuxBot quit
    match = re.match(r'quit$', line)
    if match:
        if sender not in channel_ops[channel] + channel_voices[channel]:
            irc.send_message(sender + ": Permission denied. You must be +o or +v.", channel)
        else:
            irc.quit(quitmessage)
            sys.exit(0)
        return True

    response = config.get_command_response(clean_string(line))
    if response:
        irc.send_message(response.replace("\\s", sender), channel)
        return
    response = config.get_command_response_command(clean_string(line))
    if response:
        process_command(response, sender, channel)
        return

    return False