Sloodle Chat Mirror Code

From SLIS Second Life Wiki

This Sloodle article is deprecated. It is probably out of date and will not be updated. It continues to exist only for reference purposes.
For up-to-date information, see: Sloodle WebIntercom.

Setting notecard example:


// Settings for SLoodle Chat, make sure same version of card and code
// MOODLE_NAME | URL_BASE | CHAT_ID | USER_ID
SL Chat Bot
http://www.sloodle.com/
13
158

// Can include additional data on lower lines... for future extensions


The script itself:


//---------------------------
/////// SLOODLE CHAT MIRROR
/////// Version .74
/////// Connects a Second Life object to a chat room in Moodle.
/////// Texture is configured for a cube prim
/////// Touch the object to give permission to log chat to Moodle.
/////// Requires a Setting notecard

//----------
//VERSIONS
// 0.74 Added Moodle chat URL to starting note, cleaned up chatter, cleaned SLURL maker
// 0.72 reset when notecard is changed, to auto reload Moodle and chatroom data. - DL
// 0.7 added?
//0.6 changes - changed the /SLURL to /slurl to avoid clashes with other gestures. - PA

//----------
// NOTECARD SETTINGS
// edit these to put in your moodle name as it appears in moodle chat room,
// and place your moodle ID number at the end of line 2
// you also need to log into the moodle chat room with your moodle account for this 
// to work currently!

//----------
// Declare vars w/out values
string MOODLE_NAME;
string URL_BASE;
string URL; // this will be constructed out of URL_BASE, CHAT_ID, USER_ID, and some other hard coding
string CHAT_ID;
string USER_ID;


//----------
//SLURL maker variables
vector Where;
string Name;
string SLURL;
integer X;
integer Y;
integer Z;
string RevisedMessage;
integer location;

//-----------
//Sloodle chat variables
list menu1=["YES"];
list menu2=["STOP","Continue"];
list menu_other=["Accept","Cancel"];
list unique_names;
list keys;
integer listenID;
integer part;
integer active;
integer CHANNEL = -67999;
string text;
string COL_START = "";
string COL_END = "";
string CODE_END = "";
integer total_len = 0;

key httprequest;
integer message_id = 0; // last message cc'd from moodle

// FOR NOTECARD CONFIGURATION
string gName;   // name of a notecard in the object's inventory
integer gLine = 2;  // current line number, skips first commented line
key gQueryID;   // id used to identify dataserver queries


default
{
    state_entry()
    {
        //  READ THE NOTECARD TO GET SETTINGS
        gName = llGetInventoryName(INVENTORY_NOTECARD, 0); // select the first notecard in the object's inventory
        gQueryID = llGetNotecardLine(gName, gLine);    // request data line 2

        llSetTimerEvent(0); // clear timer in case
        llSetTexture("059eb6eb-9eef-c1b5-7e95-a4c6b3e5ed9a",ALL_SIDES);
        unique_names = [];
        part = 0;
        llSetText("",<1,1,1>,1);
    }
    
    // If there has been an inventory change, then it was probably the notecard with
    // chatroom settings... so reset and reload.
    changed(integer change) { 
        if (change & CHANGED_INVENTORY) // and it was inventory
            llResetScript(); // reload the URL from the notecard
   }

    
    // USE DATASERVER TO GET BACK THE NOTECARD SETTINGS
    dataserver(key query_id, string data) {
        if (query_id == gQueryID) {
            if (data != EOF) {    // not at the end of the notecard
                // llSay(0, (string)gLine+": "+data);    // output the line
    
                if (gLine == 2) { // MOODLE_NAME
                MOODLE_NAME = data;
                // llSay(0, MOODLE_NAME);
                }
                else if (gLine == 3) { // URL_BASE
                URL_BASE = data;
                // llSay(0, URL_BASE);
                }
                else if (gLine == 4) { // CHAT_ID
                CHAT_ID = data;
                // llSay(0, CHAT_ID);
                }
                else if (gLine == 5) { // USER_ID
                USER_ID = data;
                // llSay(0, USER_ID);
                }
                ++gLine;                // increase line count
                
                if (gLine <= 5) { // still reading the notecard
                    gQueryID = llGetNotecardLine(gName, gLine);    // request next line
                }
                else {
                    URL = URL_BASE+"/mod/sloodle/mod/chat/sl_chat_linker_noauth.php?chat_id="+CHAT_ID+"&user_id="+USER_ID;
                    // should equal something like http://www.sloodle.com/SL_insert.php?chat_id=6&user_id=158
                    //llSay(0, URL); //DEBUG ECHO
                }
            }
        }
    }
        
    touch_start(integer total_number)   {
        if (llDetectedKey(0) == llGetOwner() )  {
            llListenRemove(listenID);
            listenID = llListen(CHANNEL,"",llGetOwner(),"");
            llDialog(llGetOwner(),"\nRecord this chat?",menu1,CHANNEL);
            llSetTimerEvent(10);
        }
    }
    
    listen(integer channel, string name, key id, string message)    {
        //llOwnerSay(message);
        if (message == "YES") {
            llSay(0,"Chat logging is on!");
            llSay(0,"Join this Moodle chat at "+URL_BASE+"mod/chat/gui_header_js/index.php?id="+CHAT_ID);
            llSay(0,"Touch logger to record your chat.");
            llSetText("Chat logging is on!",<0,0,0>,1.0);
            llMessageLinked(LINK_THIS,part,"START",NULL_KEY);
            llSleep(0.1);
            llSetTimerEvent(0);
            state logging;
        }
    }

    timer() {
        llSetTimerEvent(0);
        llListenRemove(listenID);
        llOwnerSay("Dialog has timed out. Touch logger again to use.");
    }
    
}

state logging   {
    on_rez(integer param)   {
        // goto state default - TO FINISH
        llOwnerSay("Chat logging is off");
        state default;
    }
    
    state_entry()   {
        llSetTexture("d3c9180a-1703-3a84-8dcd-e3aa6306a343",ALL_SIDES);
        listenID = llListen(0,"",NULL_KEY,"");
        llListen(CHANNEL,"",NULL_KEY,"");  // listen for commands
        keys = [llGetOwner()];
        unique_names = [llKey2Name(llGetOwner())];
        text = "Recording: " + llList2String(unique_names,0);
        llSetText(text, <1,0.2,0.2>,1);
        total_len = 0;
        llSetTimerEvent(12);
    }
    
    touch_start(integer total_number)   {
        integer i;
        
        for (i=0; i < total_number; i++)    {

            if (llDetectedKey(i) == llGetOwner())  {
                llDialog(llGetOwner(),"\nStop logging?",menu2,CHANNEL);
            }
            else    {
                // Add name of avatar touching to the list
                llDialog(llDetectedKey(i),"\nMay I record your chat?",menu_other,CHANNEL);
                llLoadURL(llDetectedKey(0), "\n\n\nClick 'Go To Page' to join the Moodle chatroom:", URL_BASE+"mod/chat/gui_header_js/index.php?id="+CHAT_ID);

            }
        }
    }
    
    listen(integer channel, string name, key id, string message)    {

        if (llGetOwnerKey(id) != id)    { // only true for avatars! Will ignore all object chat
            //llOwnerSay("Attempted spoofing");
            return;
        }
        
        if (channel == CHANNEL)     {
            if (message == "STOP" && id == llGetOwner() )   {    
                llSay(0,"Chat logging is now off!");
                llMessageLinked(LINK_THIS,part,"SEND",NULL_KEY);
                state default;
            }
            // double check that this message is genuinely from avatar not spoofed
            if (message == "Accept" && name == llKey2Name(id) )     {
                llSetTimerEvent(10);
                
                if(llListFindList(unique_names,[name]) == -1)    { 
                    unique_names += (unique_names=[]) + unique_names + name; // hack for faster code on wiki
                    keys += (keys=[]) + keys + id;
                    llSay(0,"Now recording " + name);
                    text = text + "\n " + name;
                    llSetText(text, <1,0.2,0.2>,1);
                }
            }
        }
        else    {
            integer i;
            string speech;
            i = llListFindList(unique_names,[name]  );
            
            if (i != -1)   {

/////////////SLURL MAKER

                if(llSubStringIndex(llToUpper(message),"/SLURL")>-1) {    
                    Name = llGetRegionName();
                    Where = llGetPos();
                    X = (integer)Where.x;
                    Y = (integer)Where.y;
                    Z = (integer)Where.z;
                    SLURL = "http://slurl.com/secondlife/" + Name + "/" + (string)X + "/" + (string)Y + "/" + (string)Z + "/?title=" + Name;  
                    message=llInsertString(message,llSubStringIndex(llToUpper(message),"/SLURL"), SLURL);
                    //llSay(0,(string)llSubStringIndex(llToUpper(message),"/SLURL"));
                }
///////////////SLURL MAKER  

                httprequest = llHTTPRequest(URL+"&chat_message="+llEscapeURL(" <FONT color=#009999>" +name +"</font>: "+ message+" <FONT size=1 color=gray><a href="+SLURL+">"+llGetRegionName()+"</a></font>"),[HTTP_METHOD,"GET"],"");

             
            }
        }
    }    

    timer()     {
        httprequest = llHTTPRequest(URL,[HTTP_METHOD,"GET"],"");
        // llSay(0, httprequest); // DEBUG PURPOSES
        // llSay(0, URL); // DEBUG PURPOSES
        // FINDINGS:  the timer is working, the request is being properly constructed, and the SLoodle server does send back the proper info from the PHP script. Look further below...
    }
    
    http_response(key id,integer status, list meta, string body)
    {
        if (httprequest == id)
        {
            if (llStringLength(body) == 0)
                return;
            list message_list = llParseString2List(body,["Line: "],[]);
            integer i= llGetListLength(message_list);

            string text;
            string line;
            integer lno;
                          
            while ( i > 0 ) //I did this kind of back to front, becuase this is the result of hackery
                            // writing from scratch Id use a for loop, but this is kind of the way
                            // it is.
            {
                i--;
                text = llList2String(message_list,i);
                line = llGetSubString(text,0,4);
                lno = (integer)line;
                if ( lno > message_id && llSubStringIndex(text,MOODLE_NAME + " (SL)") == -1)
                    llSay(0,llGetSubString(text,5,-1));
            }
            message_id = lno;
        }
    }
}

Toolbox
LANGUAGES