view plugins/ask/ask.js @ 733:bd12709b1975

Irccd: rework server to be simpler Server no longer has signals, now user is responsible of calling connect(), recv() and pass a completion handler. The recv function will complete with a std::variant of all possible events. The server does not manage itself anymore, the reconnection system has been moved to server_service instead. To simplify reconnection, the limit has been removed now you can only enable indefinite reconnection or disable it at all. closes #893 closes #892
author David Demelier <markand@malikania.fr>
date Tue, 24 Jul 2018 21:30:00 +0200
parents 3e816cebed2c
children 8c44bbcbbab9
line wrap: on
line source

/*
 * ask.js -- crazy module for asking a medium
 *
 * Copyright (c) 2013-2018 David Demelier <markand@malikania.fr>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

// Plugin information.
info = {
    name: "ask",
    author: "David Demelier <markand@malikania.fr>",
    license: "ISC",
    summary: "Crazy module for asking a medium",
    version: "@IRCCD_VERSION@"
};

// Modules.
var File = Irccd.File;
var Logger = Irccd.Logger;
var Plugin = Irccd.Plugin;
var Util = Irccd.Util;

/* List of answers */
var answers = [
    "Yes",
    "No"
];

function onLoad()
{
    try {
        // User specified file?
        if (Plugin.config["file"])
            path = Plugin.config["file"];
        else
            path = Plugin.paths.config + "/answers.conf";

        var file = new File(path, "r");
        var line;

        // Reset.
        answers = [];

        while ((line = file.readline()))
            // Skip empty lines.
            if (line.length > 0)
                answers.push(line);
    } catch (e) {
        Logger.warning(path + ": " + e.message);
        Logger.warning("using default answers");
    }
}

function onCommand(server, origin, channel)
{
    var target = Util.splituser(origin);
    var response = answers[Math.floor(Math.random() * answers.length)];

    server.message(channel, target + ", " + response);
}

onReload = onLoad;