File: http://svn.grimore.org/corrade-lsl-templates/source/eggdrop/unban.lsl -

///////////////////////////////////////////////////////////////////////////
//  Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3      //
///////////////////////////////////////////////////////////////////////////
//
// A module that unbans group members using fuzzy name matching.
//
///////////////////////////////////////////////////////////////////////////
 
///////////////////////////////////////////////////////////////////////////
//    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"
        )
    );
}
 
///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3    //
///////////////////////////////////////////////////////////////////////////
list wasSetIntersect(list a, list b) {
    if(llGetListLength(a) == 0) return [];
    string i = llList2String(a, 0);
    a = llDeleteSubList(a, 0, 0);
    if(llListFindList(b, (list)i) == -1)
        return wasSetIntersect(a, b);
    return i + wasSetIntersect(a, b);
}
 
// configuration data
string configuration = "";
// store message over state.
string data = "";
// banee
string firstname = "";
string lastname = "";
string soft = "True";
 
default {
    state_entry() {
        llOwnerSay("[Unban] Starting...");
        llSetTimerEvent(10);
    }
    link_message(integer sender, integer num, string message, key id) {
        if(id != "configuration") return;
        llOwnerSay("[Unban] Got configuration...");
        configuration = message;
        state listen_group;
    }
    timer() {
        llOwnerSay("[Unban] 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 listen_group {
    state_entry() {
        // DEBUG
        llOwnerSay("[Unban] Waiting for group messages...");
    }
    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 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) + "unban")
            return;
 
        // Remove command.
        command = llDeleteSubList(command, 0, 0);
 
        firstname = wasKeyValueGet("firstname", message);
        lastname = wasKeyValueGet("lastname", message);
 
        if(firstname == "" || lastname == "") {
            data = "And who would yarr be?";
            state tell;
        }
 
        // Dump the rest of the message.
        data = llDumpList2String(command, " ");
 
        // Get roles of caller.
        state get_caller_roles;
    }
    on_rez(integer num) {
        llResetScript();
    }
    changed(integer change) {
        if((change & CHANGED_INVENTORY) ||
            (change & CHANGED_REGION_START) ||
            (change & CHANGED_OWNER)) {
            llResetScript();
        }
    }
}
 
state get_caller_roles {
    state_entry() {
        // DEBUG
        llOwnerSay("[Unban] Searching for caller...");
        llInstantMessage(
            wasKeyValueGet(
                "corrade",
                configuration
            ),
            wasKeyValueEncode(
                [
                    "command", "getmemberroles",
                    "group", wasURLEscape(
                        wasKeyValueGet(
                            "group",
                            configuration
                        )
                    ),
                    "password", wasURLEscape(
                        wasKeyValueGet(
                            "password",
                            configuration
                        )
                    ),
                    "firstname", firstname,
                    "lastname", 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) != "getmemberroles")
            return;
 
        if(wasKeyValueGet("success", body) != "True") {
            // DEBUG
            llOwnerSay("[Unban] Unable to get member roles: " +
                wasURLUnescape(
                    wasKeyValueGet("error", body)
                )
            );
            state listen_group;
        }
 
        // Dump the roles to a list.
        list roles = wasCSVToList(
            wasURLUnescape(
                wasKeyValueGet("data", body)
            )
        );
 
        if(llGetListLength(
            wasSetIntersect(roles,
                wasCSVToList(
                    wasKeyValueGet(
                        "admin roles", configuration
                    )
                )
            )
        ) == 0) {
            data = "You ain't got the cojones!";
            state tell;
        }
 
        list banee = llParseString2List(data, [" "], []);
 
        firstname = llList2String(banee, 0);
        banee = llDeleteSubList(banee, 0, 0);
        lastname = llList2String(banee, 0);
        banee = llDeleteSubList(banee, 0, 0);
 
        if(firstname == "" || lastname == "") {
            data = "Full name required.";
            state tell;
        }
 
        if(llGetListLength(banee) != 0 &&
            llToLower(llList2String(banee, 0)) == "nosoft") {
            soft = "False";
            banee = llDeleteSubList(banee, 0, 0);
        }
 
        // GC
        banee = [];
        state get_banee_roles;
    }
    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 get_banee_roles {
    state_entry() {
        // DEBUG
        llOwnerSay("[Unban] Searching for banee...");
        llInstantMessage(
            wasKeyValueGet(
                "corrade",
                configuration
            ),
            wasKeyValueEncode(
                [
                    "command", "getmemberroles",
                    "group", wasURLEscape(
                        wasKeyValueGet(
                            "group",
                            configuration
                        )
                    ),
                    "password", wasURLEscape(
                        wasKeyValueGet(
                            "password",
                            configuration
                        )
                    ),
                    "firstname", firstname,
                    "lastname", 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) != "getmemberroles")
            return;
 
        if(wasKeyValueGet("success", body) != "True") {
            if(wasKeyValueGet("status", body) == "19862") {
                // DEBUG
                llOwnerSay("[Unban] User not in group, but proceeding anyway...");
                jump continue;
            }
            // DEBUG
            llOwnerSay("[Unban] Unable to get member roles: " +
                wasURLUnescape(
                    wasKeyValueGet("error", body)
                )
            );
 
            state listen_group;
        }
 
        string result = wasURLUnescape(
            wasKeyValueGet("data", body)
        );
 
        if(result != "" && llListFindList(wasCSVToList(result), (list)"Owners") != -1) {
            data = "Ejectee is an owner. I'm not gonna open the pod bay doors.";
            state tell;
        }
 
@continue;
 
        state unban;
    }
    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 unban {
    state_entry() {
        // DEBUG
        llOwnerSay("[Unban] Unbanning...");
        llInstantMessage(
            wasKeyValueGet(
                "corrade",
                configuration
            ),
            wasKeyValueEncode(
                [
                    "command", "ban",
                    "group", wasURLEscape(
                        wasKeyValueGet(
                            "group",
                            configuration
                        )
                    ),
                    "password", wasURLEscape(
                        wasKeyValueGet(
                            "password",
                            configuration
                        )
                    ),
                    "soft", soft,
                    "action", "unban",
                    "avatars", wasURLEscape(
                        wasListToCSV(
                            [
                                firstname + " " + 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) != "ban")
            return;
 
        if(wasKeyValueGet("success", body) != "True") {
            // DEBUG
            llOwnerSay("[Unban] Unable to ban member: " +
                wasURLUnescape(
                    wasKeyValueGet("error", body)
                )
            );
            state listen_group;
        }
 
        data = "They'll be bak!";
 
        state tell;
    }
    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 tell {
    state_entry() {
        // DEBUG
        llOwnerSay("[Unban] 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)
                ]
            )
        );
 
        // reset variables.
        soft = "True";
 
        state listen_group;
    }
}