corrade-lsl-templates

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 41  →  ?path2? @ 42
/source/eggdrop/joke-database.lsl
@@ -5,7 +5,7 @@
// A database-based joke module for Corrade Eggdrop.
//
///////////////////////////////////////////////////////////////////////////
 
///////////////////////////////////////////////////////////////////////////
// Copyright (C) 2015 Wizardry and Steamworks - License: CC BY 2.0 //
///////////////////////////////////////////////////////////////////////////
@@ -17,7 +17,7 @@
if(i != -1) return llList2String(a, 2*i+1);
return "";
}
 
///////////////////////////////////////////////////////////////////////////
// Copyright (C) 2013 Wizardry and Steamworks - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
@@ -32,7 +32,7 @@
} while(llGetListLength(k) != 0);
return llDumpList2String(data, "&");
}
 
///////////////////////////////////////////////////////////////////////////
// Copyright (C) 2011 Wizardry and Steamworks - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
@@ -44,7 +44,7 @@
return <x, y, 0>;
return wasCirclePoint(radius);
}
 
///////////////////////////////////////////////////////////////////////////
// Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
@@ -68,7 +68,7 @@
} while(i != "");
return o;
}
 
///////////////////////////////////////////////////////////////////////////
// Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
@@ -107,7 +107,7 @@
// postcondition: length(s) = 0
return l + m;
}
 
///////////////////////////////////////////////////////////////////////////
// Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
@@ -117,18 +117,18 @@
string a = llDumpList2String(
llParseStringKeepNulls(
llList2String(
l,
l,
0
),
["\""],
),
["\""],
[]
),
"\"\""
);
if(llParseStringKeepNulls(
a,
a,
[" ", ",", "\n", "\""], []
) !=
) !=
(list) a
) a = "\"" + a + "\"";
v += a;
@@ -136,7 +136,7 @@
} while(l != []);
return llDumpList2String(v, ",");
}
 
///////////////////////////////////////////////////////////////////////////
// Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
@@ -147,32 +147,32 @@
llParseString2List(
llDumpList2String(
llParseString2List(
i,
["+"],
i,
["+"],
[]
),
),
" "
),
["%0D%0A"],
),
["%0D%0A"],
[]
),
),
"\n"
)
);
}
 
// configuration data
string configuration = "";
// callback URL
string URL = "";
// store message over state.
string firstname = "";
string lastname = "";
string group = "";
string data = "";
string jump_state = "";
integer joke_counter = 0;
string action = "";
 
string statement = "";
string parameters = "";
 
default {
state_entry() {
llOwnerSay("[Joke] Starting module...");
@@ -182,8 +182,8 @@
if(id != "configuration") return;
llOwnerSay("[Joke] Got configuration...");
configuration = message;
jump_state = "create_database";
state url;
action = "create_database";
state trampoline;
}
timer() {
llOwnerSay("[Joke] Requesting configuration...");
@@ -193,8 +193,8 @@
llResetScript();
}
changed(integer change) {
if((change & CHANGED_INVENTORY) ||
(change & CHANGED_REGION_START) ||
if((change & CHANGED_INVENTORY) ||
(change & CHANGED_REGION_START) ||
(change & CHANGED_OWNER)) {
llResetScript();
}
@@ -203,32 +203,161 @@
llSetTimerEvent(0);
}
}
state url {
 
state trampoline {
state_entry() {
if(action == "create_database") {
statement = wasURLEscape("CREATE TABLE IF NOT EXISTS \"" +
wasKeyValueGet("joke table", configuration) +
"\" (data text(1023), name text(35), firstname text(31), lastname text(31), id integer NOT NULL PRIMARY KEY)");
state query;
}
 
if(action == "random") {
statement = wasURLEscape("SELECT * FROM \"" +
wasKeyValueGet("joke table", configuration) +
"\" WHERE name=:group ORDER BY RANDOM() LIMIT 1");
parameters = wasURLEscape(
wasListToCSV(
[
"group",
wasURLEscape(
wasKeyValueGet(
"group",
configuration
)
)
]
)
);
 
state query;
}
 
if(action == "id") {
statement = wasURLEscape("SELECT * FROM \"" +
wasKeyValueGet("joke table", configuration) +
"\" WHERE name=:group AND id=:id");
parameters = wasURLEscape(
wasListToCSV(
[
"group",
wasURLEscape(
wasKeyValueGet(
"group",
configuration
)
),
"id",
data
]
)
);
state query;
}
 
if(action == "search") {
statement = wasURLEscape("SELECT * FROM \"" +
wasKeyValueGet("joke table", configuration) +
"\" WHERE name=:group AND data LIKE :data COLLATE NOCASE ORDER BY RANDOM() LIMIT 1");
parameters = wasURLEscape(
wasListToCSV(
[
"group",
wasURLEscape(
wasKeyValueGet(
"group",
configuration
)
),
"data",
wasURLEscape("%" + llDumpList2String(llParseString2List(data, [" "], []), "%") + "%")
]
)
);
state query;
}
 
if(action == "remove") {
statement = wasURLEscape("DELETE FROM \"" +
wasKeyValueGet("joke table", configuration) +
"\" WHERE name=:name AND id=:id");
parameters = wasURLEscape(
wasListToCSV(
[
"name",
wasURLEscape(
wasKeyValueGet(
"group",
configuration
)
),
"id",
data
]
)
);
state query;
}
 
if(action == "add") {
statement = wasURLEscape("INSERT INTO \"" +
wasKeyValueGet("joke table", configuration) +
"\" (name, data, firstname, lastname) VALUES (:name, :data, :firstname, :lastname)");
parameters = wasURLEscape(
wasListToCSV(
[
"name",
wasURLEscape(
wasKeyValueGet(
"group",
configuration
)
),
"data",
wasURLEscape(data),
"firstname",
wasURLEscape(firstname),
"lastname",
wasURLEscape(lastname)
]
)
);
state query;
}
 
if(action == "get_joke_id") {
statement = wasURLEscape("SELECT MAX(id) AS LAST FROM \"" +
wasKeyValueGet("joke table", configuration) + "\"");
state query;
}
 
if(action == "by") {
statement = wasURLEscape("SELECT * FROM \"" +
wasKeyValueGet("joke table", configuration) +
"\" WHERE name=:group AND firstname=:firstname AND lastname=:lastname ORDER BY RANDOM() LIMIT 1");
parameters = wasURLEscape(
wasListToCSV(
[
"group",
wasURLEscape(
wasKeyValueGet(
"group",
configuration
)
),
"firstname",
wasURLEscape(firstname),
"lastname",
wasURLEscape(lastname)
]
)
);
 
state query;
}
 
// DEBUG
llOwnerSay("[Joke] Requesting URL...");
llRequestURL();
}
http_request(key id, string method, string body) {
if(method != URL_REQUEST_GRANTED) return;
URL = body;
// DEBUG
llOwnerSay("[Joke] Got URL...");
if(jump_state == "create_database")
state create_database;
if(jump_state == "get")
state get;
if(jump_state == "add")
state add;
if(jump_state == "remove")
state remove;
if(jump_state == "count_jokes")
state count_jokes;
if(jump_state == "listen_group")
state listen_group;
// DEBUG
llOwnerSay("[Joke] Jump table corrupted, please contact creator...");
llResetScript();
}
@@ -236,161 +365,164 @@
llResetScript();
}
changed(integer change) {
if((change & CHANGED_INVENTORY) ||
(change & CHANGED_REGION_START) ||
if((change & CHANGED_INVENTORY) ||
(change & CHANGED_REGION_START) ||
(change & CHANGED_OWNER)) {
llResetScript();
}
}
}
state create_database {
 
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 = "";
 
// Message length check.
if(llStringLength(message) > 1023) {
data = "Message length exceeded 1023 characters.";
state tell;
}
 
// DEBUG
llOwnerSay("[Joke] Creating database: " + wasKeyValueGet("joke table", configuration));
llOwnerSay("[Joke] Executing action: " + action);
llInstantMessage(
wasKeyValueGet(
"corrade",
"corrade",
configuration
),
wasKeyValueEncode(
[
"command", "database",
"group", wasURLEscape(
wasKeyValueGet(
"group",
configuration
)
),
"password", wasURLEscape(
wasKeyValueGet(
"password",
configuration
)
),
"SQL", wasURLEscape("CREATE TABLE IF NOT EXISTS \"" +
wasKeyValueGet("joke table", configuration) +
"\" (data text(1023), name text(35), firstname text(31), lastname text(31), id integer NOT NULL PRIMARY KEY)"),
"callback", wasURLEscape(URL)
]
)
),
message
);
// GC
message = "";
llSetTimerEvent(60);
}
http_request(key id, string method, string body) {
llHTTPResponse(id, 200, "OK");
llReleaseURL(URL);
if(wasKeyValueGet("command", body) != "database" ||
wasKeyValueGet("success", body) != "True") {
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("[Joke] Unable modify database: " +
llOwnerSay("[Joke] Unable query database: " +
wasURLUnescape(
wasKeyValueGet("error", body)
) +
" " +
)
);
state listen_group;
}
 
if(action == "create_database") {
llOwnerSay("[Joke] Database created!");
state listen_group;
}
 
if(action == "random" || action == "id" || action == "search" || action == "by") {
list result = wasCSVToList(
wasURLUnescape(
wasKeyValueGet("data", body)
)
);
llResetScript();
 
if(llGetListLength(result) != 10) {
data = "No joke found. . .";
state tell;
}
 
data = llList2String(
result,
llListFindList(result, ["data"]) + 1
);
 
firstname = llList2String(
result,
llListFindList(result, ["firstname"]) + 1
);
 
lastname = llList2String(
result,
llListFindList(result, ["lastname"]) + 1
);
 
string id = llList2String(
result,
llListFindList(result, ["id"]) + 1
);
 
// Build data to be sent.
data += " " + "[" + firstname + " " + lastname + "/" + id + "]";
 
state tell;
}
llOwnerSay("[Joke] Database created!");
jump_state = "count_jokes";
state url;
}
timer() {
llResetScript();
}
on_rez(integer num) {
llResetScript();
}
changed(integer change) {
if((change & CHANGED_INVENTORY) ||
(change & CHANGED_REGION_START) ||
(change & CHANGED_OWNER)) {
llResetScript();
 
if(action == "remove") {
data = "Joke " + data + " has been removed.";
state tell;
}
}
state_exit() {
llSetTimerEvent(0);
}
}
 
state count_jokes {
state_entry() {
// DEBUG
llOwnerSay("[Joke] Counting jokes in database: " + wasKeyValueGet("joke table", configuration));
llInstantMessage(
wasKeyValueGet(
"corrade",
configuration
),
wasKeyValueEncode(
[
"command", "database",
"group", wasURLEscape(
wasKeyValueGet(
"group",
configuration
)
),
"password", wasURLEscape(
wasKeyValueGet(
"password",
configuration
)
),
"SQL", wasURLEscape("SELECT COUNT(*) AS count FROM \"" +
wasKeyValueGet("joke table", configuration) + "\""),
"callback", wasURLEscape(URL)
]
)
);
llSetTimerEvent(60);
}
http_request(key id, string method, string body) {
llHTTPResponse(id, 200, "OK");
llReleaseURL(URL);
if(wasKeyValueGet("command", body) != "database" ||
wasKeyValueGet("success", body) != "True") {
// DEBUG
llOwnerSay("[Joke] Unable modify database: " +
if(action == "add") {
action = "get_joke_id";
data = body;
state trampoline;
}
 
if(action == "get_joke_id") {
list result = wasCSVToList(
wasURLUnescape(
wasKeyValueGet("error", body)
) +
" " +
wasURLUnescape(
wasKeyValueGet("data", body)
)
);
llResetScript();
 
data = llList2String(
result,
llListFindList(result, ["LAST"]) + 1
);
 
data = "Joke " + data + " has been stored.";
state tell;
}
list result = wasCSVToList(
wasURLUnescape(
wasKeyValueGet("data", body)
)
);
joke_counter = llList2Integer(
result,
llListFindList(result, ["count"]) + 1
) + 1;
llOwnerSay("[Joke] There are " + (string)joke_counter + " jokes in the database.");
 
// DEBUG
llOwnerSay("[Joke] Jump table corrupted, please contact creator...");
state listen_group;
}
timer() {
llResetScript();
state listen_group;
}
on_rez(integer num) {
llResetScript();
}
changed(integer change) {
if((change & CHANGED_INVENTORY) ||
(change & CHANGED_REGION_START) ||
if((change & CHANGED_INVENTORY) ||
(change & CHANGED_REGION_START) ||
(change & CHANGED_OWNER)) {
llResetScript();
}
@@ -399,7 +531,7 @@
llSetTimerEvent(0);
}
}
 
state listen_group {
state_entry() {
// DEBUG
@@ -409,54 +541,54 @@
// 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)) !=
wasURLUnescape(wasKeyValueGet("group", message)) !=
wasKeyValueGet("group", configuration)))
return;
 
// Get the message sender.
firstname = wasURLUnescape(
wasKeyValueGet(
"firstname",
"firstname",
message
)
);
 
lastname = wasURLUnescape(
wasKeyValueGet(
"lastname",
"lastname",
message
)
);
 
// Get the sent message.
data = wasURLUnescape(
wasKeyValueGet(
"message",
"message",
message
)
);
 
// Check if this is an eggdrop command.
if(llGetSubString(data, 0, 0) !=
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) !=
if(llList2String(command, 0) !=
wasKeyValueGet("command", configuration) + "joke")
return;
 
// Remove command.
command = llDeleteSubList(command, 0, 0);
 
// Remove action.
string action = llList2String(command, 0);
// Jump to the "add" state for adding
action = llList2String(command, 0);
// Jump to the "add" state for adding
if(action == "add") {
command = llDeleteSubList(command, 0, 0);
data = llDumpList2String(command, " ");
@@ -464,11 +596,11 @@
data = "The joke's too short to be funny.";
state tell;
}
jump_state = "add";
state url;
action = "add";
state trampoline;
}
// Jump to the "remove" state for removing
 
// Jump to the "remove" state for removing
if(action == "remove") {
command = llDeleteSubList(command, 0, 0);
data = llDumpList2String(command, " ");
@@ -476,369 +608,59 @@
data = "Which one though? Please provide a joke id.";
state tell;
}
jump_state = "remove";
state url;
action = "remove";
state trampoline;
}
 
if(action == "by") {
command = llDeleteSubList(command, 0, 0);
firstname = llList2String(command, 0);
command = llDeleteSubList(command, 0, 0);
lastname = llList2String(command, 0);
command = llDeleteSubList(command, 0, 0);
data = llDumpList2String(command, " ");
 
if(llStringLength(firstname) == 0 ||
llStringLength(lastname) == 0) {
data = "First and Last name is required.";
state tell;
}
 
action = "by";
state trampoline;
}
 
string index = llList2String(command, 0);
command = llDeleteSubList(command, 0, 0);
 
data = llDumpList2String(command, " ");
if((integer)data <= 0)
data = "";
jump_state = "get";
state url;
}
on_rez(integer num) {
llResetScript();
}
changed(integer change) {
if((change & CHANGED_INVENTORY) ||
(change & CHANGED_REGION_START) ||
(change & CHANGED_OWNER)) {
llResetScript();
 
if(index == "search") {
action = "search";
state trampoline;
}
}
}
state get {
state_entry() {
// DEBUG
llOwnerSay("[Joke] Retrieving from database.");
if(data == "") {
llInstantMessage(
wasKeyValueGet(
"corrade",
configuration
),
wasKeyValueEncode(
[
"command", "database",
"group", wasURLEscape(
wasKeyValueGet(
"group",
configuration
)
),
"password", wasURLEscape(
wasKeyValueGet(
"password",
configuration
)
),
"SQL", wasURLEscape("SELECT * FROM \"" +
wasKeyValueGet("joke table", configuration) +
"\" WHERE name=:group LIMIT 1 OFFSET :id"),
"data", wasURLEscape(
wasListToCSV(
[
"group",
wasURLEscape(
wasKeyValueGet(
"group",
configuration
)
),
"id",
(string)(
(integer)llFrand(
joke_counter
) + 1
)
]
)
),
"callback", wasURLEscape(URL)
]
)
);
llSetTimerEvent(60);
return;
 
if((integer)index <= 0) {
action = "random";
state trampoline;
}
llInstantMessage(
wasKeyValueGet(
"corrade",
configuration
),
wasKeyValueEncode(
[
"command", "database",
"group", wasURLEscape(
wasKeyValueGet(
"group",
configuration
)
),
"password", wasURLEscape(
wasKeyValueGet(
"password",
configuration
)
),
"SQL", wasURLEscape("SELECT * FROM \"" +
wasKeyValueGet("joke table", configuration) +
"\" WHERE name=:group AND id=:id"),
"data", wasURLEscape(
wasListToCSV(
[
"group",
wasURLEscape(
wasKeyValueGet(
"group",
configuration
)
),
"id",
data
]
)
),
"callback", wasURLEscape(URL)
]
)
);
llSetTimerEvent(60);
 
data = index;
action = "id";
state trampoline;
}
http_request(key id, string method, string body) {
llHTTPResponse(id, 200, "OK");
llReleaseURL(URL);
if(wasKeyValueGet("command", body) != "database" ||
wasKeyValueGet("success", body) != "True") {
// DEBUG
llOwnerSay("[Joke] Unable retrieve from database: " +
wasURLUnescape(
wasKeyValueGet("error", body)
)
);
state listen_group;
}
list result = wasCSVToList(
wasURLUnescape(
wasKeyValueGet("data", body)
)
);
if(llGetListLength(result) != 10) {
data = "No joke found. . .";
state tell;
}
data = llList2String(
result,
llListFindList(result, ["data"]) + 1
);
string firstname = llList2String(
result,
llListFindList(result, ["firstname"]) + 1
);
string lastname = llList2String(
result,
llListFindList(result, ["lastname"]) + 1
);
string id = llList2String(
result,
llListFindList(result, ["id"]) + 1
);
// Build data to be sent.
data += " " + "[" + firstname + " " + lastname + "/" + id + "]";
state tell;
}
timer() {
llReleaseURL(URL);
state listen_group;
}
on_rez(integer num) {
llResetScript();
}
changed(integer change) {
if((change & CHANGED_INVENTORY) ||
(change & CHANGED_REGION_START) ||
if((change & CHANGED_INVENTORY) ||
(change & CHANGED_REGION_START) ||
(change & CHANGED_OWNER)) {
llResetScript();
}
}
state_exit() {
llSetTimerEvent(0);
}
}
state add {
state_entry() {
// DEBUG
llOwnerSay("[Joke] Adding to database: " + (string)(joke_counter + 1));
llInstantMessage(
wasKeyValueGet(
"corrade",
configuration
),
wasKeyValueEncode(
[
"command", "database",
"group", wasURLEscape(
wasKeyValueGet(
"group",
configuration
)
),
"password", wasURLEscape(
wasKeyValueGet(
"password",
configuration
)
),
"SQL", wasURLEscape("INSERT INTO \"" +
wasKeyValueGet("joke table", configuration) +
"\" (name, data, firstname, lastname, id) VALUES (:name, :data, :firstname, :lastname, :id)"),
"data", wasURLEscape(
wasListToCSV(
[
"name",
wasURLEscape(
wasKeyValueGet(
"group",
configuration
)
),
"data",
wasURLEscape(data),
"firstname",
wasURLEscape(firstname),
"lastname",
wasURLEscape(lastname),
"id",
(string)(joke_counter + 1)
]
)
),
"callback", wasURLEscape(URL)
]
)
);
llSetTimerEvent(60);
}
http_request(key id, string method, string body) {
llHTTPResponse(id, 200, "OK");
llReleaseURL(URL);
if(wasKeyValueGet("command", body) != "database" ||
wasKeyValueGet("success", body) != "True") {
// DEBUG
llOwnerSay("[Joke] Unable modify database: " +
wasURLUnescape(
wasKeyValueGet("data", body)
)
);
state listen_group;
}
++joke_counter;
data = "Joke " + (string)joke_counter + " has been stored.";
state tell;
}
timer() {
llReleaseURL(URL);
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 remove {
state_entry() {
// DEBUG
llOwnerSay("[Joke] Removing from database.");
llInstantMessage(
wasKeyValueGet(
"corrade",
configuration
),
wasKeyValueEncode(
[
"command", "database",
"group", wasURLEscape(
wasKeyValueGet(
"group",
configuration
)
),
"password", wasURLEscape(
wasKeyValueGet(
"password",
configuration
)
),
"SQL", wasURLEscape("DELETE FROM \"" +
wasKeyValueGet("joke table", configuration) +
"\" WHERE name=:name AND id=:id"),
"data", wasURLEscape(
wasListToCSV(
[
"name",
wasURLEscape(
wasKeyValueGet(
"group",
configuration
)
),
"id",
data
]
)
),
"callback", wasURLEscape(URL)
]
)
);
llSetTimerEvent(60);
}
http_request(key id, string method, string body) {
llHTTPResponse(id, 200, "OK");
llReleaseURL(URL);
if(wasKeyValueGet("command", body) != "database" ||
wasKeyValueGet("success", body) != "True") {
// DEBUG
llOwnerSay("[Joke] Unable modify database: " +
wasURLUnescape(
wasKeyValueGet("error", body)
)
);
state listen_group;
}
--joke_counter;
data = "Joke " + data + " has been removed.";
state tell;
}
timer() {
llReleaseURL(URL);
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
@@ -845,21 +667,21 @@
llOwnerSay("[Joke] Sending to group.");
llInstantMessage(
wasKeyValueGet(
"corrade",
"corrade",
configuration
),
),
wasKeyValueEncode(
[
"command", "tell",
"group", wasURLEscape(
wasKeyValueGet(
"group",
"group",
configuration
)
),
"password", wasURLEscape(
wasKeyValueGet(
"password",
"password",
configuration
)
),