WikiHUD (Source)

From Second Life Wiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

For WikiHUD by User:Lillie Yifu

// wiki reader 1.03 Lillie Yifu
// http://sexsecond.blogspot.com
// chat on channel 4 the name of an article to get the top of the entry text
// permission is granted to distribute this script free and open source with the header attached


// adding second channel to listen for mode changes
// adding category mode

list wikis = ["wikia","lindenlabs"];
list urls =  ["http://secondlife.wikia.com","http://wiki.secondlife.com"];
// need to find the api.php for wiki.secondlife, seems like we will need some kind of list for languages, and for where various installations put files
integer sources;
integer oldmode;
integer wiki = 0;

string redirect = "#REDIRECT";
integer section ;
integer follow= 0;
integer redirlen;
string qname;
list meta = [HTTP_MIMETYPE,"text/plain;charset=utf-8"];
key hid;
string name;
string str_replace(string src, string from, string to)
{//replaces all occurrences of 'from' with 'to' in 'src'.
    integer len = (~-(llStringLength(from)));
    if(~len)
    {
        string  buffer = src;
        integer b_pos = -1;
        integer to_len = (~-(llStringLength(to)));
        @loop; //instead of a while loop, saves 5 bytes (and run faster).
        integer to_pos = ~llSubStringIndex(buffer, from);
        if(to_pos)
        {
            buffer = llGetSubString(src = llInsertString(llDeleteSubString(src, b_pos -= to_pos, b_pos + len), b_pos, to), (-~(b_pos += to_len)), 0x8000);
            jump loop;
        }
    }
    return src;
}
key image = "f07b2853-6aa2-7819-cbde-a1bf4187091d";
vector large = <0.01,0.3,.3>;
vector small = <0.01,0.1,0.1>;
integer islarge =TRUE;
integer qmode = 0;//mode of query
integer mode = 0;//mode of command

string url;
integer modes;
//action=query & list=search & srsearch=wikipedia & srlimit=10
list modenames = ["summary","categories","word","full","source","wikis"];
list phps = [ 
"/index.php?action=raw&title=",
"/api.php?format=xml&prop=categories&action=query&titles=",
"/api.php?format=xml&action=query&list=search&srlimit=20&srsearch=",
"/index.php?action=raw&title="
];
//string url = "http://en.wikipedia.org/index.php?action=raw&title=";
integer achan = 4;
integer mchan = 5;
default
{
    state_entry()
    {
        integer i = 0;
        url = llList2String(wikis,wiki);
        sources = llGetListLength(wikis);
        modes = llGetListLength(modenames);
        llSetTexture(image,ALL_SIDES);
        llOwnerSay("Chat on channel "+(string) achan+" the name of an article to reference the Second Life Wiki.");
        llOwnerSay("Chat on channel "+(string) mchan+" to change query mode.");
        for(;i<modes;++i) {
            llOwnerSay(llList2String(modenames, i));
        }
        
        llListen(achan,"",llGetOwner(),"");
        llListen(mchan,"",llGetOwner(),"");
    }

    listen(integer c,string n,key id,string m)
    {
        string article = str_replace(m," ","_");
       
        if(achan==c) {
            if(0==mode|| 3==mode) {
                qname=url+llList2String(phps,mode)+article;
                name = qname +"&section=0"; // section postpend
                llOwnerSay(name+" "+ llList2String(modenames,mode));
                hid = llHTTPRequest(name, meta, "");
                qmode = mode;
                section =-1;
                
            } else if(1==mode) {
                name = url+llList2String(phps,mode)+article;
                llOwnerSay(name+" "+ llList2String(modenames,mode));
                hid = llHTTPRequest(name, meta, "");
                qmode = mode;
            } else if(2==mode) {// will need a loop here
                name = url+llList2String(phps,mode)+article;
                llOwnerSay(name+" "+ llList2String(modenames,mode));
                hid = llHTTPRequest(name, meta, "");
                qmode = mode;
            }
                
        } else if (mchan ==c) { 
        
            integer i =0;
             oldmode = mode;
            for(;i<modes;++i) {
                if(llGetSubString(m,0,2) == llGetSubString(llList2String(modenames,i),0,2) ){
                    mode = i;
                    i=modes;
                    }
                }// end look for mode
                if(4 == mode) { // set source
                    list parse = llParseString2List(m,[" "],[]);
                    string newsource = llGetSubString(llList2String(parse,1),0,3);
                    for(i=0;i<sources;++i) { // I should sort and search... 
                        if(llGetSubString(llList2String(wikis,i),0,3)==newsource) {
                            wiki = i;
                            url = llList2String(urls,wiki);
                            llOwnerSay("Setting source to " + url);
                            i=sources;
                            }
                        }
                    mode = oldmode; // set mode to default, we should never stay here.
                } else if(5 ==mode) {
                        llOwnerSay("To change source chat  /" + (string)mchan + " source <wiki name>");
                        for(i=0;i<sources;++i) {
                            llOwnerSay(llList2String(wikis,i) + "\t\t @ " + llList2String(urls,i));
                        }
                        mode = oldmode; // set mode  back, we should never stay here
                    } // end of mode processing
                if(mode!= oldmode) {
                    llOwnerSay("Setting mode to "+llList2String(modenames,mode));
                }
            }// end channel 5
        }
            
            
            
    
    on_rez(integer p)
    {
        llResetScript();
    }
    touch_start(integer p)
    {
        if(1==p) {
        if(islarge) {
                llSetScale(small);
                islarge = FALSE;
                llSetAlpha(.3,ALL_SIDES);
            } else {
                llSetScale(large);
                islarge = TRUE;
                llSetAlpha(1.,ALL_SIDES);
            }
        } 
    }
        
        timer()
        {
            if(hid){
                hid = NULL_KEY;
                follow = 0;
                llOwnerSay("Query timed out.");
            }
            llSetTimerEvent(0.);
        }

    http_response(key request_id, integer status, list metadata, string body)
    {
        if(0==qmode || 3==qmode) { // article or full
            key image;
            if (request_id == hid) {
                    image = (key)llGetSubString(body,5,40); // image key if available
                    llSetTexture(image,ALL_SIDES);
                    follow = TRUE;
                if(llSubStringIndex(body,redirect)!=0) { // no redirect
                    if( qmode == 0) { // if summary mode, or multipart query after first 
                        llOwnerSay(body); 
                        return;
                    } else {
                        if(0 == section && llSubStringIndex(body,"=")!=0) {
                            llOwnerSay(llGetSubString(body,0,llSubStringIndex(body,"\n="))); //truncate at first line that starts with = 
                        } else if(section >0)
                            llOwnerSay(body);
                        }
                        if(body !="") {    
                            ++section ; // increment section, will now be 0
                            name = qname + "&templates=expand&section=" + (string) section; // query name
                            hid = llHTTPRequest(name, meta, "");
                        }
                    }
                        
                        

                } else if(follow< 3) {// if redirect and less than 3 jumps
                    
                    integer start = llSubStringIndex(body,"[[") + 2;
                    integer end = llSubStringIndex(body,"]]") -1;
                    string m = llGetSubString(body,start, end);
                    ++follow ;
                    llSetTimerEvent(180.);
                    name=url+str_replace(m," ","_");
                    llOwnerSay("Following Redirect to \n"+name);
                    list meta = [HTTP_MIMETYPE,"text/plain;charset=utf-8"];
                    hid = llHTTPRequest(name, meta, "");
                } else {
                    hid = NULL_KEY;
                    follow=0;
                    llOwnerSay("Too many redirects");
                }
            } else if(1 == qmode) {// xml categories
                list catraw = llParseString2List(body,["Category:"],[]);
                integer i = 1;
                string temp;
                for(;i<llGetListLength(catraw);++i){
                    temp = llList2String(catraw,i);
                    llOwnerSay(llGetSubString(temp,0,llSubStringIndex(temp,"\"")-1));
                }
            } else if(2 == qmode) {
                 list catraw = llParseString2List(body,["title=\""],[]);
                integer i = 1;
                string temp;
                for(;i<llGetListLength(catraw);++i){
                    temp = llList2String(catraw,i);
                    llOwnerSay(llGetSubString(temp,0,llSubStringIndex(temp,"\"")-1));
                }
            }
    }
        
}