Genshi example

From OpenSimulator

Jump to: navigation, search


This script appears to be written in the OpenSim Script Language (OSL), which is similar to the LSL (Linden Scripting Language) used in Second Life. The script seems to be designed for use in a virtual world environment where avatars interact with terminals and perform various actions such as logging in, accessing settings, and managing inventory.

Here's a high-level overview of the script:

   Global Variables: The script defines various global variables to store settings, menu options, HTTP request IDs, and other necessary information.
   Initialization: The init() function initializes the settings by reading from a settings notecard. If the notecard is missing, it displays an error message.
   Settings Notecard Handling: The readSettings() function reads settings from a notecard and sets the corresponding variables.
   Utility Functions: The script includes utility functions like string2vector() to convert strings to vectors and generatePW() to generate passwords and hash them using MD5.
   HTTP Requests: The httpRequest() function handles various HTTP requests such as login, logoff, registration, and adding items.
   Login: The login() function handles the login process, including retrieving avatar data and initiating HTTP requests for authentication.
   Event Handlers: The script includes event handlers for various events like on_rez, state_entry, touch_start, dataserver, http_response, link_message, and changed.
   States: The script defines multiple states (default, restart_default, inuse) to manage different stages of operation.
   Menu Handling: The returnDialog() function displays different menus based on the user's profile status.
   Sensor and Listener Functions: The sensor() function handles sensor events to monitor avatar proximity, while the listen() function listens for user commands.
   Inventory Management: The changed() function manages inventory changes, such as allowing or denying item submissions.
   Error Handling and Feedback: The script includes error handling and feedback mechanisms to inform users about the status of their actions.

Overall, the script is a comprehensive system for managing terminals in a virtual world environment, handling user interactions, authentication, menu navigation, inventory management, and more. It demonstrates the flexibility and capabilities of scripting within virtual worlds.

//*********************
//****from notecard****
//*********************
integer intchannel;
integer paramchannel;
integer scanrate;
integer scanrange;
integer salt;
integer certificate;
float logrange; 
float lograngewarning; 
string url;
string sitetitle;
vector textcolor;
//*********************
 
 
//*********************
//*****avatar vars*****
//*********************
key av_key;
string username; 
string av_name; 
string av_surname; 
string av_created;
string av_payinfo;
//*********************
 
 
//*********************
//******menu vars******
//*********************
list menuAdmin = ["Settings", "My Account", "Website","Logoff"];
list menuRegistered = ["My Account", "Website", "Logoff"];
list menuInactive = ["Website", "About", "Logoff"];
list menuNewUser = ["Register", "Website", "About","Logoff"];
//*********************
 
 
//*********************
//*****http id's*******
//*********************
string item_request_id;
string authenticate_request_id;
string register_request_id;
string logoff_request_id;
string x_request_id;
string account_request_id;
//*********************
 
 
//*********************
//*****dataserver******
//*********************
key settingsDS; 
key payinfoDS; 
key bornDS; 
//*********************
 
 
//*********************
//******handlers*******
//*********************
string settingsFileName="jSLplus Settings";
integer settingsEntry;
integer defaultSalt=666;
integer online=-2;
string settingsNotify;
string sid;
string menu;
list objectSpecs;
string secretWord;
string status; 
string itemName;
//*********************
//Settings Notecard stuff
init() { 
    if (llGetInventoryType(settingsFileName) == INVENTORY_NOTECARD) {
    online=-1;    
    readSettings();    
    }
    else {
    llOwnerSay("===ERROR===\nNo settings-notecard found!");
    }
}
//Settings Notecard stuff
readSettings() {
llOwnerSay("Initializing settings. Please wait...");
settingsEntry = 0;
settingsNotify="===SETUP SETTINGS===";
settingsDS = llGetNotecardLine(settingsFileName, settingsEntry) ;
}
 
checkSettings(string entry) {
    if((entry != "") && (llGetSubString(entry, 0, 1) != "//")) {
    list Details = llParseString2List(entry, ["="], []);
    string keyname = llList2String(Details, 0);
    string contents = llList2String(Details, 1);
        if ((keyname != "") && (contents != "")) {
            if(keyname=="url") {url=contents;settingsNotify+="\nURL: "+contents;}
            if(keyname=="ssl") {if((integer)contents==1) {url="https://"+url;settingsNotify+="\nSSL: enabled";}
            else if((integer)contents==0){url="http://"+url;settingsNotify+="\nSSL: disabled (NOT recommended)";} 
            }  
            else if(keyname=="certificate") {
            certificate=(integer)contents;     
                if(certificate==0) {settingsNotify+="\nCertificate: not required";}
                else if(certificate==1) {settingsNotify+="\nCertificate: required";}
            }                            
            else if(keyname=="sitetitle") {sitetitle=contents;settingsNotify+="\nSite-title: "+contents;}
            else if(keyname=="intchannel") {intchannel=(integer)contents;settingsNotify+="\nInternal channel: "+contents;}
            else if(keyname=="paramchannel") {paramchannel=(integer)contents;settingsNotify+="\nParameter channel: "+contents;}            
            else if(keyname=="salt") {salt=(integer)contents;settingsNotify+="\nHash-Salt: "+contents;}       
            else if(keyname=="scanrate") {scanrate=(integer)contents;settingsNotify+="\nIdle Scan: "+contents+" seconds per scan";}   
            else if(keyname=="scanrange") {scanrange=(integer)contents;settingsNotify+="\nIdle Scanrange: "+contents+" meters";}  
            else if(keyname=="logrange") {logrange=(float)contents;settingsNotify+="\nLogs off: "+contents+" meters from console";}                       
            else if(keyname=="lograngewarning") {lograngewarning=(float)contents;settingsNotify+="\nLogoff warning: "+contents+" meters from console";}   
            else if(keyname=="textcolor") {textcolor=string2vector(contents);settingsNotify+="\nTextcolor: "+contents;}           
        }
    }
settingsEntry+=1; 
settingsDS = llGetNotecardLine(settingsFileName, settingsEntry) ;
}
 
vector string2vector(string s) {
list foo = llCSV2List(s);
vector v;
v.x = llList2Float(foo,0);
v.y = llList2Float(foo,1);
v.z = llList2Float(foo,2);
return v;
}
 
//Generates secretWord, and a MD5-hash from secretWord and Salt
string generatePW(float uniqueness) {
list primewords = ["alpha","beta","gamma","zulu","bravo","cappa","tango"];
string randomWord = llList2String(primewords,(integer)llFrand(llGetListLength(primewords))); 
string randomNumber = (string)llRound(llFrand(uniqueness)); 
secretWord = randomWord+randomNumber; 
return llMD5String(secretWord,salt); 
}
 
returnDialog(string returnMenu) {
    if(returnMenu == "MENU_UNREGISTERED") {
    llDialog( av_key, "Select an option...", menuNewUser, intchannel);   
    }
    else if(returnMenu == "MENU_PROFILE_ACTIVE") {
    llDialog( av_key, "Select an option...", menuRegistered, intchannel); 
    }    
    else if(returnMenu == "MENU_PROFILE_INACTIVE") {
    llDialog( av_key, "Select an option...", menuInactive, intchannel); 
    }            
    else if(returnMenu == "MENU_ADMIN") {
    llDialog( av_key, "Select an option...", menuAdmin, intchannel); 
    } 
}
 
httpRequest(string function,list params) {
    if(function=="login") {
    authenticate_request_id = llHTTPRequest(url+"index.php?option=com_jslplus&view=interface&controller=interface&task=login",
    [HTTP_METHOD,"POST",HTTP_VERIFY_CERT,certificate,HTTP_MIMETYPE,"application/x-www-form-urlencoded"],
    "&k="+llList2String(params,0)+"&n="+llList2String(params,1)+"&s="+llList2String(params,2)+"&p="+llList2String(params,3)+"&c="
    +llList2String(params,4)+"&h="+generatePW(1000)+"&o="+llList2String(params,5)+"&format=raw");
    } 
    if(function =="logoff") {
    logoff_request_id = llHTTPRequest(url+"index.php?option=com_jslplus&view=interface&controller=interface&task=logoff", 
    [HTTP_METHOD, "POST",HTTP_VERIFY_CERT,certificate,HTTP_MIMETYPE,"application/x-www-form-urlencoded"],
    "&sid="+llList2String(params,0)+"&format=raw");  
    }     
    if(function=="register") {
    register_request_id = llHTTPRequest(url+"index.php?option=com_jslplus&view=interface&controller=interface&task=register",
    [HTTP_METHOD, "POST",HTTP_VERIFY_CERT,certificate,HTTP_MIMETYPE,"application/x-www-form-urlencoded"],
    "&h="+llList2String(params,0)+"&e="+llList2String(params,1)+"&sid="+sid+"&format=raw");           
    }
    if(function=="additem") {
    item_request_id = llHTTPRequest(url + "index.php?option=com_jslplus&view=interface&controller=interface&task=additem",
    [HTTP_METHOD, "POST",HTTP_VERIFY_CERT,certificate,HTTP_MIMETYPE,"application/x-www-form-urlencoded"],
    "&k="+llList2String(params,0)+"&n="+llList2String(params,1)+"&t="+llList2String(params,2)+"&p="+llList2String(params,3)+llList2String(params,4)+
    llList2String(params,5)+"&sid="+sid+"&format=raw"); 
    }  
}
 
login(key id) {
av_key = id; 
username = llKey2Name(av_key);
av_surname = llGetSubString(username, llSubStringIndex(username, " ") + 1, -1);
av_name = llGetSubString(username, 0, llSubStringIndex(username, " "));
llSetText("Controller: "+username, textcolor, 1);
llInstantMessage(av_key, "Contacting server. Please wait..."); 
bornDS = llRequestAgentData(av_key, DATA_BORN); 
payinfoDS = llRequestAgentData(av_key, DATA_PAYINFO); 
}
 
default {
    on_rez(integer startparam){
    llResetScript();   
    }
 
    state_entry() {
        if(online==-2) {
        init(); 
        }
    llAllowInventoryDrop(FALSE); 
        if(online==TRUE || online==FALSE){
        llSetText("Terminal online...",textcolor,1);    
        }
        else {
        llSetText("Terminal offline...",<0,0,0>,1);     
        }
    }
 
    touch_start( integer num ) {
 
        if(llDetectedKey(0)!=av_key) {
        return;
        }
        else {
            if(online==-1) {
            init();  
            }   
            else {
            login(llDetectedKey(0));    
            }
        }
    } 
 
    dataserver(key query_id, string data) { 
        if(query_id == bornDS) {    
        av_created=data;
        }
        else if (query_id == payinfoDS) {
        av_payinfo=data;
        httpRequest("login",[av_key,av_name,av_surname,av_payinfo,av_created,(string)llGetLinkKey(2)]); 
        }
        else if (query_id == settingsDS) {
            if (data != EOF) {
            checkSettings(data) ;
            return;
            }
            else {
            llOwnerSay("Settings are loaded. Checking content...");
                if(salt==defaultSalt) {
                llOwnerSay("You didn't setup a unique hash-salt. Please correct the problem...");
                state restart_default; 
                }
                else {
                llOwnerSay(settingsNotify+"\nSettings ok...\n");
                settingsNotify="";
                login(llGetOwner());   
                }
            }
        }        
    }
 
    //RESPONSE FROM INITIAL HTTP-REQUEST
    http_response(key request_id, integer httpStatus, list metadata, string body) {
        if (authenticate_request_id == request_id) { 
        body = llStringTrim(body, STRING_TRIM); 
        list parameters; 
        parameters = llCSV2List(body);
        status = llList2String(parameters,0); 
        sid = llList2String(parameters,1);
        string visited = llList2String(parameters,2);
        //Send link-message to feedback-module(3)
        llMessageLinked(3,0,status+","+username+","+visited+","+secretWord,av_key);
        }
    }
 
    //Receives link-messages from feedback-module(3)
    link_message(integer sender_num, integer lineResponse, string str, key id) { 
        if (sender_num==3) {
        list input = llCSV2List(str); 
        menu = llList2String(input,0); 
            //Positive message, proceed to logged-in state  
            if(lineResponse==TRUE) {  
            state inuse; 
            }
            //Reboot when an error-message comes back...
            else {
            //Critical error(lineResponse=-1) or non-critical error(lineResponse=0/FALSE)
            online=lineResponse;
            state restart_default;   
            }
        }
    }
 
    changed( integer change ) { 
        if ( change == CHANGED_INVENTORY ) {
        integer itemCount = llGetInventoryNumber(INVENTORY_ALL); 
        integer i=0; 
            if(itemCount>3) { 
                for(i=0;i<itemCount;i++) {
                string itemName = llGetInventoryName(INVENTORY_ALL, i);   
                    if (itemName != "About" && itemName!="jSLplus Interface" && itemName!="jSLplus Visitor-Tracker" && itemName!="jSLplus Settings") {
                    llSay(0,"You must be logged in, before you may upload items to the ODS!");
                    llRemoveInventory(itemName);
                    }
                }
            }
            else {
            llOwnerSay("Settings have been changed. Please wait while the terminal reboots...");    
            online=-2;
            state restart_default;
            }
        }
    }    
}
 
 
state restart_default {
    state_entry() {
    state default;
    }
}
 
 
state inuse { 
    state_entry() { 
    llSensorRepeat( "", "", AGENT, scanrange, TWO_PI, scanrate); 
    llListen(intchannel, "", av_key, "" );  
    llListen(paramchannel,"",av_key,"");
        if(menu=="MENU_PROFILE_ACTIVE" || status=="MENU_ADMIN") {
        llAllowInventoryDrop(TRUE);
        string folder = llGetObjectName();
        }
    returnDialog(menu);    
    }
 
    touch_start( integer number_detected ) {
    integer i;
        for( i = 0; i < number_detected; i++ ) {
            if (llDetectedKey(i) == av_key) {
            returnDialog(menu);     
            } 
            else { 
            llSay(0,username+" is currently logged into this terminal. Please wait until this user logged off...");    
            }                 
        }
 
 
    }
 
    sensor( integer number_detected ) {   
    integer i; 
        for( i = 0; i < number_detected; i++ ) {
        key detected_key = llDetectedKey(i);     
            if(av_key==detected_key) {
            vector agentPos = llDetectedPos(i);    
            vector terminalPos = llGetPos();
            float distance = llVecDist(agentPos, terminalPos);
                if (distance > logrange) {
                llInstantMessage(av_key, "===NOTICE===\nYou are out of range. logging off now...");  
                httpRequest("logoff",[]);
                llSensorRemove();
                }
                else if(distance > lograngewarning){
                llInstantMessage(av_key, "===NOTICE===\nYou are "+(string)(logrange-distance)+" meters from being logged out."); 
                }
            }
        }
    }    
 
    listen( integer chan, string name, key id, string msg ) {
        if(id == av_key) {
        msg = llStringTrim(msg, STRING_TRIM);
        list paramList;
        string command;
            if(msg!="") {
            paramList = llCSV2List(msg); 
            command = llList2String(paramList,0);  
            }                
            if (command == "Website") {
            llLoadURL(av_key, sitetitle, url); 
            returnDialog(menu); 
            }    
            else if (command == "About") {
            llGiveInventory(av_key,"About");
            returnDialog(menu); 
            } 
            else if (command == "Request") {
            x_request_id = llHTTPRequest(url + "index.php?option=com_jslplus&view=interface&controller=interface&task=request&sid="+sid+
            "&format=raw", [HTTP_METHOD, "GET"],"");  
            } 
            else if (command == "Logoff") {
            llSetText(username+" logs out...",textcolor,1);   
            httpRequest("logoff",[sid]);  
            llInstantMessage(av_key,"Preparing to log off..."); 
            }   
 
            if(menu=="MENU_UNREGISTERED") {
                if (command == "Register") {
                llInstantMessage(av_key,"===NOTICE===\nYou must provide a valid emailaddress.\nType: /"+(string)paramchannel+
                " registermail,myemail@address.org' in order to finish the registration process."); 
                }  
                if (command == "registermail") {
                httpRequest("register",[generatePW(1000),llStringTrim(llList2String(paramList,1), STRING_TRIM)]);
                } 
            }
            else if(menu=="MENU_PROFILE_ACTIVE") {  
                if (command == "My Account") {
                string myAccount = "===MY ACCOUNT===\n";
                myAccount += "Avatar-key: "+(string)av_key;
                llInstantMessage(av_key,myAccount);
                returnDialog(menu); 
                } 
            }                   
            else if(menu=="MENU_ADMIN") {
                if (command == "Settings") {
                string terminalSettings = "===SETTINGS===\n";    
                terminalSettings += "terminal-key: "+(string)llGetKey()+"\n";
                terminalSettings += "heap: "+(string)llGetFreeMemory() + " bytes\n";
                llInstantMessage(av_key, terminalSettings);   
                llMessageLinked(2, 0, "settings",av_key); 
                returnDialog(menu);  
                }  
                else if (command == "My Account") {
                string myAccount = "===MY ACCOUNT===\n";
                account_request_id = llHTTPRequest(url + "index.php?option=com_jslplus&view=interface&controller=interface&task=getaccount&sid="+sid+
                "&format=raw", [HTTP_METHOD, "GET"],"");    
                myAccount += "Avatar-key: "+(string)av_key;
                llInstantMessage(av_key,myAccount);
                returnDialog(menu); 
                } 
            }  
        }
    }
 
    http_response(key request_id, integer httpStatus, list metadata, string body) {
    list parameters;
    body = llStringTrim(body, STRING_TRIM);  
    parameters = llCSV2List(body);
    status = llList2String(parameters,0); 
        if (request_id == register_request_id) { 
        llMessageLinked(3,0,status+","+username+","+secretWord,av_key);
        }
        if (request_id == item_request_id) {
        llMessageLinked(3,0,status+","+username,av_key);  
        }
 
        if (request_id == logoff_request_id) {
        llMessageLinked(3,0,status+","+username,av_key);
        }
 
        if (request_id == x_request_id) { 
        llMessageLinked(3,0,status+","+username,av_key);  
         } 
 
        if (request_id == account_request_id) { 
        llMessageLinked(3,0,status+","+username,av_key);  
        } 
    }
 
    link_message(integer sender_num, integer lineResponse, string str, key id) { 
    online = lineResponse; 
        if(lineResponse==TRUE) {
        list input = llCSV2List(str); 
        menu = llList2String(input,0);  
        returnDialog(menu);
        }
        else {
        state default;  
        }
    } 
 
    changed( integer change ) {
        if (change & CHANGED_ALLOWED_DROP) {
        llOwnerSay("The inventory has changed as a result of a user without mod permissions dropping an item on the prim"+
        "and it being allowed by the script.");
        }
 
        if ( change == CHANGED_INVENTORY ) {
        integer itemCount = llGetInventoryNumber(INVENTORY_ALL);   
            if(itemCount>3) {
            llSay(0,"A new item was submitted!"); 
            integer i=0;
            string newItem;
            for(i=0;i<itemCount;i++) {
            string itemName = llGetInventoryName(INVENTORY_ALL, i);   
                if (itemName != "About" && itemName!="jSLplus Interface" && itemName!="jSLplus Visitor-Tracker" && itemName!="jSLplus Settings") {
                newItem = itemName;
                }
            }
            key itemKey = llGetInventoryKey(newItem);            
            key itemCreatorKey = llGetInventoryCreator(newItem);
                if(av_key != itemCreatorKey) {
                llSay(0,"Item denied! You must be the creator of the submitted item!");
                llRemoveInventory(newItem);
                return;   
                }
                else {
                    if (llStringLength(newItem) <=30) {
                    llSay(0,"Item accepted. You have been recognised as the creator of this item..."); 
                    }
                    else {
                    llSay(0,"Item denied! Please shorten the objectname to a maximum of 30 characters...");    
                    llRemoveInventory(newItem);
                    return; 
                    }
                }
            integer itemType = llGetInventoryType(newItem);
            integer permCopy; integer permMod; integer permTransfer;
            string itemSpecs = "\n===Item specifications===\n";
            itemSpecs+="item-Key: "+(string)itemKey+"\n";
            itemSpecs+="item-Name: "+newItem+"\n";
            itemSpecs+="=Permissions=\n";
                if(llGetInventoryPermMask(newItem, MASK_NEXT) & PERM_COPY) {permCopy=1;itemSpecs+="Copy:Yes\n";}
                else {permCopy=0;itemSpecs+="Copy:No\n";}
                if(llGetInventoryPermMask(newItem, MASK_NEXT) & PERM_MODIFY) {permMod=1;itemSpecs+="Modify:Yes\n";}
                else {permMod=0;itemSpecs+="Modify:No\n";}
                if(llGetInventoryPermMask(newItem, MASK_NEXT) & PERM_TRANSFER) {permTransfer=1;itemSpecs+="Transfer:Yes\n";}
                else {permTransfer=0;itemSpecs+="Transfer:No\n";}
            llSay(0,itemSpecs);       
            llGiveInventory(llGetLinkKey(2), newItem);
            llRemoveInventory(newItem);
            llSay(0,"Sending request to ODS-Site...");
            newItem = llEscapeURL(newItem);
            httpRequest("additem",[itemKey,newItem,itemType,permCopy,permMod,permTransfer]);
            }
        }
    }          
}
Personal tools
General
About This Wiki