corrade-lsl-templates – Rev 42

Subversion Repositories:
Rev:
///////////////////////////////////////////////////////////////////////////
//  Copyright (C) Wizardry and Steamworks 2018 - License: GNU GPLv3      //
///////////////////////////////////////////////////////////////////////////
//
// A reminders module for Corrade Eggdrop.
//
///////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2015 Wizardry and Steamworks - License: CC BY 2.0    //
///////////////////////////////////////////////////////////////////////////
string wasKeyValueGet(string k, string data) {
    if(llStringLength(data) == 0) return "";
    if(llStringLength(k) == 0) return "";
    list a = llParseStringKeepNulls(data, ["&", "="], []);
    integer i = llListFindList(llList2ListStrided(a, 0, -1, 2), [ k ]);
    if(i != -1) return llList2String(a, 2*i+1);
    return "";
}

///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2013 Wizardry and Steamworks - License: GNU GPLv3    //
///////////////////////////////////////////////////////////////////////////
string wasKeyValueEncode(list data) {
    list k = llList2ListStrided(data, 0, -1, 2);
    list v = llList2ListStrided(llDeleteSubList(data, 0, 0), 0, -1, 2);
    data = [];
    do {
        data += llList2String(k, 0) + "=" + llList2String(v, 0);
        k = llDeleteSubList(k, 0, 0);
        v = llDeleteSubList(v, 0, 0);
    } while(llGetListLength(k) != 0);
    return llDumpList2String(data, "&");
}

///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2011 Wizardry and Steamworks - License: GNU GPLv3    //
///////////////////////////////////////////////////////////////////////////
// http://was.fm/secondlife/wanderer
vector wasCirclePoint(float radius) {
    float x = llPow(-1, 1 + (integer) llFrand(2)) * llFrand(radius*2);
    float y = llPow(-1, 1 + (integer) llFrand(2)) * llFrand(radius*2);
    if(llPow(x,2) + llPow(y,2) <= llPow(radius,2))
        return <x, y, 0>;
    return wasCirclePoint(radius);
}

///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3    //
///////////////////////////////////////////////////////////////////////////
// escapes a string in conformance with RFC1738
string wasURLEscape(string i) {
    string o = "";
    do {
        string c = llGetSubString(i, 0, 0);
        i = llDeleteSubString(i, 0, 0);
        if(c == "") jump continue;
        if(c == " ") {
            o += "+";
            jump continue;
        }
        if(c == "\n") {
            o += "%0D" + llEscapeURL(c);
            jump continue;
        }
        o += llEscapeURL(c);
@continue;
    } while(i != "");
    return o;
}

///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3    //
///////////////////////////////////////////////////////////////////////////
list wasCSVToList(string csv) {
    list l = [];
    list s = [];
    string m = "";
    do {
        string a = llGetSubString(csv, 0, 0);
        csv = llDeleteSubString(csv, 0, 0);
        if(a == ",") {
            if(llList2String(s, -1) != "\"") {
                l += m;
                m = "";
                jump continue;
            }
            m += a;
            jump continue;
        }
        if(a == "\"" && llGetSubString(csv, 0, 0) == a) {
            m += a;
            csv = llDeleteSubString(csv, 0, 0);
            jump continue;
        }
        if(a == "\"") {
            if(llList2String(s, -1) != a) {
                s += a;
                jump continue;
            }
            s = llDeleteSubList(s, -1, -1);
            jump continue;
        }
        m += a;
@continue;
    } while(csv != "");
    // postcondition: length(s) = 0
    return l + m;
}

///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3    //
///////////////////////////////////////////////////////////////////////////
string wasListToCSV(list l) {
    list v = [];
    do {
        string a = llDumpList2String(
            llParseStringKeepNulls(
                llList2String(
                    l,
                    0
                ),
                ["\""],
                []
            ),
            "\"\""
        );
        if(llParseStringKeepNulls(
            a,
            [" ", ",", "\n", "\""], []
            ) !=
            (list) a
        ) a = "\"" + a + "\"";
        v += a;
        l = llDeleteSubList(l, 0, 0);
    } while(l != []);
    return llDumpList2String(v, ",");
}

///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3    //
///////////////////////////////////////////////////////////////////////////
// unescapes a string in conformance with RFC1738
string wasURLUnescape(string i) {
    return llUnescapeURL(
        llDumpList2String(
            llParseString2List(
                llDumpList2String(
                    llParseString2List(
                        i,
                        ["+"],
                        []
                    ),
                    " "
                ),
                ["%0D%0A"],
                []
            ),
            "\n"
        )
    );
}

// configuration data
string configuration = "";
// store message over state.
string src_firstname = "";
string src_lastname = "";
string dst_firstname = "";
string dst_lastname = "";
string text = "";
string group = "";
string data = "";

// recent list of avatars
list recent_firstnames = [];
list recent_lastnames = [];

// the action to execute
string action = "";
// holds SQL statements and parameters
string statement = "";
string parameters = "";

default {
    state_entry() {
        llOwnerSay("[Reminders] Starting module...");
        llSetTimerEvent(10);
    }
    link_message(integer sender, integer num, string message, key id) {
        if(id != "configuration") return;
        llOwnerSay("[Reminders] Got configuration...");
        configuration = message;
        action = "create";
        state trampoline;
    }
    timer() {
        llOwnerSay("[Reminders] Requesting configuration...");
        llMessageLinked(LINK_THIS, 0, "configuration", NULL_KEY);
    }
    on_rez(integer num) {
        llResetScript();
    }
    changed(integer change) {
        if((change & CHANGED_INVENTORY) ||
            (change & CHANGED_REGION_START) ||
            (change & CHANGED_OWNER)) {
            llResetScript();
        }
    }
    state_exit() {
        llSetTimerEvent(0);
    }
}


state trampoline {
    state_entry() {
        if(action == "create") {
            statement = wasURLEscape("CREATE TABLE IF NOT EXISTS \"" +
                wasKeyValueGet("reminders table", configuration) +
            "\" (message TEXT(1023), name TEXT(35), src_firstname TEXT(31), src_lastname TEXT(31), dst_firstname TEXT(31), dst_lastname TEXT(31))");
            state query;
        }

        if(action == "search") {
            statement = wasURLEscape("SELECT * FROM \"" +
                wasKeyValueGet("reminders table", configuration) +
            "\" WHERE name=:group AND dst_firstname=:firstname AND dst_lastname=:lastname LIMIT 1");
            parameters = wasURLEscape(
                wasListToCSV(
                    [
                        "group",
                        wasURLEscape(
                            wasKeyValueGet(
                                "group",
                                configuration
                            )
                        ),
                        "firstname",
                        wasURLEscape(dst_firstname),
                        "lastname",
                        wasURLEscape(dst_lastname)
                    ]
                )
            );

            state query;
        }

        if(action == "commit") {
            statement = wasURLEscape("INSERT INTO \"" +
                wasKeyValueGet("reminders table", configuration) +
            "\" (name, dst_firstname, dst_lastname, src_firstname, src_lastname, message) VALUES (:name, :dst_firstname, :dst_lastname, :src_firstname, :src_lastname, :message)");
            parameters = wasURLEscape(
                wasListToCSV(
                    [
                        "name",
                        wasURLEscape(
                            wasKeyValueGet(
                                "group",
                                configuration
                            )
                        ),
                        "dst_firstname",
                        wasURLEscape(dst_firstname),
                        "dst_lastname",
                        wasURLEscape(dst_lastname),
                        "src_firstname",
                        wasURLEscape(src_firstname),
                        "src_lastname",
                        wasURLEscape(src_lastname),
                        "message", wasURLEscape(text)
                    ]
                )
            );

            state query;
        }

        if(action == "remove") {
            statement = wasURLEscape("DELETE FROM \"" +
                wasKeyValueGet("reminders table", configuration) +
            "\" WHERE name=:name AND src_firstname=:src_firstname AND src_lastname=:src_lastname AND dst_firstname=:dst_firstname AND dst_lastname=:dst_lastname AND message=:message");
            parameters = wasURLEscape(
                wasListToCSV(
                    [
                        "name",
                        wasURLEscape(
                            wasKeyValueGet(
                                "group",
                                configuration
                            )
                        ),
                        "src_firstname",
                        wasURLEscape(src_firstname),
                        "src_lastname",
                        wasURLEscape(src_lastname),
                        "dst_firstname",
                        wasURLEscape(dst_firstname),
                        "dst_lastname",
                        wasURLEscape(dst_lastname),
                        "message",
                        wasURLEscape(text)
                    ]
                )
            );
            state query;
        }

        // DEBUG
        llOwnerSay("[Reminders] Jump table corrupted, please contact vendor...");
        llResetScript();
    }
    on_rez(integer num) {
        llResetScript();
    }
    changed(integer change) {
        if((change & CHANGED_INVENTORY) ||
            (change & CHANGED_REGION_START) ||
            (change & CHANGED_OWNER)) {
            llResetScript();
        }
    }
}

state query {
    state_entry() {
        // Check messge length.
        string message = wasKeyValueEncode(
            [
                "command", "database",
                "group", wasURLEscape(
                    wasKeyValueGet(
                        "group",
                        configuration
                    )
                ),
                "password", wasURLEscape(
                    wasKeyValueGet(
                        "password",
                        configuration
                    )
                ),
                "SQL", statement,
                "data", parameters,
                "callback", wasURLEscape(
                    wasKeyValueGet(
                        "URL",
                        configuration
                    )
                )
            ]
        );

        // GC - none of these are needed anymore.
        statement = "";
        parameters = "";

        // DEBUG
        llOwnerSay("[Reminders] Executing action: " + action);
        llInstantMessage(
            wasKeyValueGet(
                "corrade",
                configuration
            ),
            message
        );
        // GC
        message = "";
        llSetTimerEvent(60);
    }
    link_message(integer sender, integer num, string body, key id) {
        // Only process callbacks for the database command.
        if(id != "callback" || wasKeyValueGet("command", body) != "database")
            return;

        if(wasKeyValueGet("success", body) != "True") {
            // DEBUG
            llOwnerSay("[Reminders] Unable query database: " +
                wasURLUnescape(
                    wasKeyValueGet("error", body)
                )
            );
            state listen_group;
        }

        // Process actions.
        if(action == "create") {
            llOwnerSay("[Reminders] Database created!");
            state listen_group;
        }

        if(action == "search") {
            list result = wasCSVToList(
                wasURLUnescape(
                    wasKeyValueGet("data", body)
                )
            );

            // DEBUG
            //llOwnerSay(llDumpList2String(result, ","));

            // 12 CSV cells, 6 fields + 6 results
            if(llGetListLength(result) != 12) {
                // DEBUG
                llOwnerSay("[Reminders] No reminders for: " + dst_firstname + " " + dst_lastname);
                state search_trampoline;
            }

            data = llList2String(
                result,
                llListFindList(result, ["data"]) + 1
            );

            src_firstname = llList2String(
                result,
                llListFindList(result, ["src_firstname"]) + 1
            );

            src_lastname = llList2String(
                result,
                llListFindList(result, ["src_lastname"]) + 1
            );

            dst_firstname = llList2String(
                result,
                llListFindList(result, ["dst_firstname"]) + 1
            );

            dst_lastname = llList2String(
                result,
                llListFindList(result, ["dst_lastname"]) + 1
            );

            text = llList2String(
                result,
                llListFindList(result, ["message"]) + 1
            );

            // Build data to be sent.
            data = dst_firstname +
                " " +
                dst_lastname +
                ": " +
                text +
                " "  +
                "[" +
                src_firstname +
                " " +
                src_lastname +
                "]";

            // DEBUG
            //llOwnerSay("BODY: " + wasURLUnescape(body));
            //llOwnerSay("DATA: " + wasURLUnescape(data));

            state remind;
        }

        if(action == "commit") {
            data = "Reminder for " + dst_firstname + " " + dst_lastname + " stored.";
            state tell;
        }

        if(action == "remove") {
            // DEBUG
            llOwnerSay("[Reminders] Reminder removed for " + dst_firstname + " " + dst_lastname);

            state search_trampoline;
        }

        // Don't announce creating table.
        if(action == "create")
            state listen_group;

        // DEBUG
        llOwnerSay("[Wiki] Jump table corrupted, please contact creator...");
        state listen_group;
    }
    timer() {
        // DEBUG
        llOwnerSay("[Reminders] Query timed out...");
        state listen_group;
    }
    on_rez(integer num) {
        llResetScript();
    }
    changed(integer change) {
        if((change & CHANGED_INVENTORY) ||
            (change & CHANGED_REGION_START) ||
            (change & CHANGED_OWNER)) {
            llResetScript();
        }
    }
    state_exit() {
        llSetTimerEvent(0);
    }
}

state listen_group {
    state_entry() {
        // DEBUG
        llOwnerSay("[Reminders] Waiting for group messages.");
    }
    timer() {
        if(!llGetListLength(recent_firstnames) ||
            !llGetListLength(recent_lastnames))
            return;

        // DEBUG
        llOwnerSay("[Reminders] Starting reminders sweep...");

        // jump to process the recents list
        state search_trampoline;
    }
    link_message(integer sender, integer num, string message, key id) {
        // We only care about notifications now.
        if(id != "notification")
            return;

        // This script only processes group notifications.
        if(wasKeyValueGet("type", message) != "group" ||
            (wasKeyValueGet("type", message) == "group" &&
            wasURLUnescape(wasKeyValueGet("group", message)) !=
            wasKeyValueGet("group", configuration)))
            return;

        // Get the message sender.
        src_firstname = wasURLUnescape(
            wasKeyValueGet(
                "firstname",
                message
            )
        );

        src_lastname = wasURLUnescape(
            wasKeyValueGet(
                "lastname",
                message
            )
        );

        // Add the agent to the recents list.
        if(llListFindList(recent_firstnames, (list)src_firstname) == -1 &&
            llListFindList(recent_lastnames, (list)src_lastname) == -1) {
            recent_firstnames += src_firstname;
            recent_lastnames += src_lastname;
        }

        // alarm 5
        llSetTimerEvent(10);

        // Get the sent message.
        data = wasURLUnescape(
            wasKeyValueGet(
                "message",
                message
            )
        );

        // Check if this is an eggdrop command.
        if(llGetSubString(data, 0, 0) !=
            wasKeyValueGet("command", configuration))
            return;

        // Check if the command matches the current module.
        list command = llParseString2List(data, [" "], []);
        if(llList2String(command, 0) !=
            wasKeyValueGet("command", configuration) + "remind")
            return;

        // Remove command.
        command = llDeleteSubList(command, 0, 0);

        // Retrieve the first and last name to remind.
        dst_firstname = llList2String(command, 0);
        command = llDeleteSubList(command, 0, 0);
        dst_lastname = llList2String(command, 0);
        command = llDeleteSubList(command, 0, 0);

        if(llStringLength(dst_firstname) == 0 ||
            llStringLength(dst_lastname) == 0) {
            data = "Invalid avatar name, please use the full username!";
            state tell;
        }

        // Retrieve the message to remind of.
        text = llDumpList2String(command, " ");
        if(llStringLength(message) == 0) {
            data = "No message to remind of supplied. . .";
            state tell;
        }

        state validate;
    }
    on_rez(integer num) {
        llResetScript();
    }
    changed(integer change) {
        if((change & CHANGED_INVENTORY) ||
            (change & CHANGED_REGION_START) ||
            (change & CHANGED_OWNER)) {
            llResetScript();
        }
    }
    state_exit() {
        llSetTimerEvent(0);
    }
}

state validate {
    state_entry() {
        // DEBUG
        llOwnerSay("[Reminders] Searching for agent.");
        llInstantMessage(
            wasKeyValueGet(
                "corrade",
                configuration
            ),
            wasKeyValueEncode(
                [
                    "command", "getmembers",
                    "group", wasURLEscape(
                        wasKeyValueGet(
                            "group",
                            configuration
                        )
                    ),
                    "password", wasURLEscape(
                        wasKeyValueGet(
                            "password",
                            configuration
                        )
                    ),
                    "sift", wasURLEscape(
                        wasListToCSV(
                            [
                                "match",
                                wasURLEscape("(?i),?\"([^\",$]*" + dst_firstname + " " + dst_lastname  + "[^\",$]*)\",?")
                            ]
                        )
                    ),
                    "callback", wasURLEscape(
                        wasKeyValueGet(
                            "URL",
                            configuration
                        )
                    )
                ]
            )
        );
        llSetTimerEvent(60);
    }
    link_message(integer sender, integer num, string body, key id) {
        // Only process callbacks for the database command.
        if(id != "callback" || wasKeyValueGet("command", body) != "getmembers")
            return;

        if(wasKeyValueGet("success", body) != "True") {
            // DEBUG
            llOwnerSay("[Reminders] Unable to get members: " +
                wasURLUnescape(
                    wasKeyValueGet("error", body)
                )
            );
            state listen_group;
        }

        // Dump the members to a list.
        list members = wasCSVToList(
            wasURLUnescape(
                wasKeyValueGet("data", body)
            )
        );

        list name = llParseString2List(
            llList2String(members, 0),
            [" "],
            []
        );

        dst_firstname = llList2String(name, 0);
        dst_lastname = llList2String(name, 1);

        if(llStringLength(dst_firstname) == 0 ||
            llStringLength(dst_lastname) == 0) {
            data = "Invalid avatar name, please use the full username!";
            state tell;
        }

        action = "commit";
        state trampoline;
    }
    timer() {
        state listen_group;
    }
    on_rez(integer num) {
        llResetScript();
    }
    changed(integer change) {
        if((change & CHANGED_INVENTORY) ||
            (change & CHANGED_REGION_START) ||
            (change & CHANGED_OWNER)) {
            llResetScript();
        }
    }
    state_exit() {
        llSetTimerEvent(0);
    }
}

state search_trampoline {
    state_entry() {
        // pop
        if(!llGetListLength(recent_firstnames) ||
            !llGetListLength(recent_lastnames)) {
            state listen_group;
        }
        dst_firstname = llList2String(recent_firstnames, 0);
        recent_firstnames = llDeleteSubList(recent_firstnames, 0, 0);
        dst_lastname = llList2String(recent_lastnames, 0);
        recent_lastnames = llDeleteSubList(recent_lastnames, 0, 0);

        // wend
        action = "search";
        state trampoline;
    }
}

state remind {
    state_entry() {
        // DEBUG
        llOwnerSay("[Reminders] Reminding...");
        llInstantMessage(
            wasKeyValueGet(
                "corrade",
                configuration
            ),
            wasKeyValueEncode(
                [
                    "command", "tell",
                    "group", wasURLEscape(
                        wasKeyValueGet(
                            "group",
                            configuration
                        )
                    ),
                    "password", wasURLEscape(
                        wasKeyValueGet(
                            "password",
                            configuration
                        )
                    ),
                    "entity", "group",
                    "message", wasURLEscape(data)
                ]
            )
        );

        action = "remove";
        state trampoline;
    }
}

state tell {
    state_entry() {
        // DEBUG
        llOwnerSay("[Reminders] Sending to group.");
        llInstantMessage(
            wasKeyValueGet(
                "corrade",
                configuration
            ),
            wasKeyValueEncode(
                [
                    "command", "tell",
                    "group", wasURLEscape(
                        wasKeyValueGet(
                            "group",
                            configuration
                        )
                    ),
                    "password", wasURLEscape(
                        wasKeyValueGet(
                            "password",
                            configuration
                        )
                    ),
                    "entity", "group",
                    "message", wasURLEscape(data)
                ]
            )
        );
        state listen_group;
    }
}