- Joined
- Sep 20, 2018
- Messages
- 3,555
This is a bit more complex than necessary because I'm tossing the notecard processing into a separate, callable function in order to keep the the http script clean, and because I've developed something of a protocol for link message handling. It does keep things "sane" for me.
Place the two scripts, http and notecard, as well as a notecard into a prim, and touch when completed. Follow the prompts to view the web page.
http
notecard
Place the two scripts, http and notecard, as well as a notecard into a prim, and touch when completed. Follow the prompts to view the web page.
http
Code:
string _me = "http";
key hURL;
string myCode;
list lScript;
init()
{
llSetText("touch for script",<1,1,1>,1);
}
on_load()
{
hURL = llRequestURL();
llWhisper(0,"click on the link below while within a chat window to view the notecard");
}
default
{
state_entry()
{
init();
}
touch_end(integer num_detected)
{
string notecard = llGetInventoryName(INVENTORY_NOTECARD,0);
llMessageLinked(LINK_SET,0,_me+"|notecard|nc_req_name|"+notecard,NULL_KEY);
llMessageLinked(LINK_SET,0,_me+"|notecard|nc_req_process|passthrough",NULL_KEY);
}
http_request(key id, string method, string body)
{
if (method == URL_REQUEST_GRANTED) {
llSay(0,"URL: " + body);
}
else if (method == URL_REQUEST_DENIED) {
llSay(0, "Something went wrong, no url. " + body);
}
else if (method == "GET") {
llHTTPResponse(id,200,llDumpList2String(lScript,"\n"));
}
else {
llHTTPResponse(id,405,"Unsupported Method");
}
}
link_message(integer source, integer num, string msg, key id)
{
list lMsg = llParseString2List(msg, ["|"], []);
string src = llList2String(lMsg,0);
string tgt = llList2String(lMsg,1);
string cmd = llList2String(lMsg,2);
string arg = llList2String(lMsg,3);
if (tgt == _me) {
if (cmd == "nc_req_data") {
if (num > 0) {
lScript += arg;
}
else if (num == -1) {
on_load();
}
}
}
}
}
Code:
// notecard reader with wait code
string _me = "notecard";
string _notecard_name;
integer _notecard_read = FALSE;
key _notecard_key;
integer _notecard_idx;
list _notecard_text;
string _nc_req_process;
string _nc_req_src;
init()
{
_notecard_name = "";
_notecard_read = FALSE;
_notecard_key = NULL_KEY;
_notecard_idx = -1;
_notecard_text = [];
_nc_req_process = "";
_nc_req_src = "";
}
load_notecard(string note)
{
_notecard_name = note;
_notecard_text = [];
llSay(0, "Reading notecard "+_notecard_name+ "... please wait");
if (llGetInventoryType(note) == INVENTORY_NONE) {
llSay(0, "Missing notecard: "+_notecard_name+", cannot proceed");
}
else {
_notecard_idx = 0;
_notecard_key = llGetNotecardLine(note,_notecard_idx++);
}
}
default
{
state_entry()
{
init();
}
on_rez(integer parameter)
{
llResetScript();
init();
}
changed(integer change)
{
if (change & CHANGED_OWNER) {
llResetScript();
}
if (change & CHANGED_INVENTORY) {
_notecard_read = FALSE;
init();
// load_notecard("Test Script");
}
}
dataserver(key query_id, string data)
{
if (_notecard_key == query_id) {
if (data == EOF) {
_notecard_read = TRUE;
llSay(0, "Notecard "+_notecard_name+" read");
if (_nc_req_process == "load") {
llMessageLinked(LINK_SET,0,_me+"|all|user_controls|resume",NULL_KEY);
}
llSleep(0.2); // pad in enough dalay to ensure resume is sent in the right order
llMessageLinked(LINK_SET,-1,_me+"|"+_nc_req_src+"|nc_req_data|",NULL_KEY);
// state default;
}
else {
if (_nc_req_process == "load") {
_notecard_text += [data];
// llOwnerSay("read "+data);
}
else if (_nc_req_process == "passthrough") {
llMessageLinked(LINK_SET,_notecard_idx,_me+"|"+_nc_req_src+"|nc_req_data|"+data,NULL_KEY);
// llOwnerSay("sent: "+_me+"|"+_nc_req_src+"|nc_req_data|"+data);
}
else {
llOwnerSay("cmd "+_nc_req_process+" not recognized");
}
_notecard_key = llGetNotecardLine(_notecard_name,_notecard_idx++);
}
}
}
link_message(integer source, integer num, string msg, key id)
{
string fn = _me+" link_message("+(string)source+","+(string)num+","+msg; // +","+(string)id;
// llOwnerSay(fn);
list lMsg = llParseString2List(msg, ["|"], []);
string src = llList2String(lMsg,0);
// _src = src;
string tgt = llList2String(lMsg,1);
string cmd = llList2String(lMsg,2);
if (llListFindList(["load","passthrough"],[cmd]) != -1) {
_nc_req_process = cmd;
}
string arg = llList2String(lMsg,3);
if (tgt == _me) {
integer handled = FALSE;
if (cmd == "nc_req_name") {
_nc_req_src = src;
_notecard_name = arg;
handled = TRUE;
}
else if (cmd == "nc_req_process") {
if (arg == "load") {
llMessageLinked(LINK_SET,0,_me+"|all|user_controls|suspend",NULL_KEY);
_nc_req_process = arg;
handled = TRUE;
load_notecard(_notecard_name);
}
else if (arg == "passthrough") {
_nc_req_process = arg;
handled = TRUE;
load_notecard(_notecard_name);
}
}
if (!handled) {
llOwnerSay("No handler found for "+_me+" cmd "+cmd);
}
}
}
}
Last edited: