Advertisement
Gayngel

Persistent_URL_Greeter_Client

Oct 22nd, 2021 (edited)
1,864
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Script by Gayngel of The Script Yard.
  2.  
  3. //Join The Script Yard Group: secondlife:///app/group/4921b4f0-c21e-7866-c0c2-791d10bb5502/about
  4. //Visit The Script Yard Marketplace: https://marketplace.secondlife.com/stores/149734
  5.  
  6. // This is a barebones script to store the scripts url persistently in a Google Sheet.
  7.  
  8. // The server will send a greeting when requested by a client terminal.
  9.  
  10.  
  11.  
  12. // The server saves it's url to a Google Sheet via Google App Script. The client will request the url of the server from the spreadsheet and then request the server to greet the avatar who touched the object and requested a greeting from the menu.
  13.  
  14.  
  15. // Get the Google App Script from https://pastebin.com/Cc9ZbYyj and follow the instructions in the app script to setup.
  16.  
  17. // Once set up paste the app url below.
  18.  
  19. string app = "https://script.google.com/macros/s/AKfycbz-rAtEqm5clysDSL3l3-9kYqfwrDjSpFg7u4PuF9208F-ON3g/exec"; // place the url of Google App Script here.
  20. string url;
  21. key reqURL;
  22. key server_req;
  23. key app_req;
  24. integer allow =FALSE; // only allow a user to touch the object if a URL has been granted to the script.
  25. integer wait = FALSE;  // if someone is using the terminal the next user must wait their turn
  26. integer listen_chan;
  27. integer listen_handle;
  28. key recipient;
  29.  
  30. default
  31. {
  32.    
  33.     on_rez(integer start_param)
  34.     {
  35.        
  36.      llResetScript();  
  37.        
  38.     }
  39.    
  40.    
  41.     state_entry()
  42.     {
  43.       allow = FALSE;                  // verbosity just in case
  44.       llReleaseURL(url);             // verbosity just in case
  45.       reqURL =llRequestURL();
  46.    
  47.     }
  48.    
  49.    
  50.    http_request(key id, string method, string body)
  51.     {
  52.     if (id == reqURL)
  53.     {
  54.         if (method == URL_REQUEST_DENIED)
  55.         llOwnerSay("The following error occurred while attempting to get a free URL for this device:\n \n" + body);
  56.  
  57.         else if (method == URL_REQUEST_GRANTED)
  58.         {
  59.         url = body;
  60.         if(app != "")
  61.         {
  62.         llOwnerSay("This terminal can now request greetings from the server");
  63.         allow = TRUE;
  64.          
  65.          }
  66.        
  67.        
  68.         else
  69.         llOwnerSay("Please add the URL of the Google App Script at the top of the script");
  70.        
  71.        
  72.         }
  73.         }
  74.        
  75.        
  76.         else if(id == server_req)
  77.         {
  78.          
  79.          if(body == "redelivery_ok")
  80.          {
  81.          
  82.           wait = TRUE;  // verbosity just in case
  83.           allow = TRUE; // verbosity just in case
  84.           llListenRemove(listen_handle); // verbosity just in case
  85.          
  86.          }
  87.            
  88.            
  89.         }
  90.        
  91.        
  92.         else if(method == "POST")
  93.         {
  94.             list tmp = llParseString2List(body,["="],[""]);
  95.             string cmd = llToLower(llList2String(tmp,0));
  96.             string ref = llList2String(tmp,1);
  97.            
  98.            
  99.             if(cmd == "server_url")
  100.             {
  101.                
  102.                
  103.              
  104.                server_req = llHTTPRequest(llUnescapeURL(ref),[HTTP_METHOD,"POST"],"request_greeting="+(string)recipient); // instruct server to send a greeting
  105.              
  106.             }
  107.            
  108.            
  109.            
  110.         }
  111.        
  112.        
  113.        
  114.     }
  115.    
  116.     touch_end(integer num)
  117.     {
  118.          key toucher = llDetectedKey(0);
  119.        if(allow == TRUE) // only allow a user to touch the object if a URL has been granted to the script.
  120.        {
  121.        
  122.        if(wait == FALSE) // if someone is using the terminal the next user to touch must wait their turn
  123.        {
  124.         wait = TRUE;  // other users must wait their turn
  125.         llListenRemove(listen_handle);
  126.         listen_chan = ((integer)("0x"+llGetSubString((string)toucher,-8,-1)) - 723) | 0x8000000;
  127.         listen_handle = llListen(listen_chan,"",toucher,"");
  128.        
  129.         llDialog(toucher,"\nSelect Greet to get a greeting from the server.",["Greet"],listen_chan);
  130.         }
  131.        
  132.         else  // other users must wait their turn
  133.         {
  134.            llRegionSayTo(toucher,0,"Someone is already using this terminal, please wait.");    
  135.            
  136.         }
  137.        }
  138.        
  139.        else
  140.        {
  141.            
  142.          llRegionSayTo(toucher,0,"This server is currently offline.");
  143.            
  144.         }
  145.        
  146.        
  147.        
  148.        
  149.     }
  150.    
  151.    
  152.     listen(integer chan, string name, key id, string msg)
  153.     {
  154.        if(chan == listen_chan)
  155.        {
  156.            
  157.           if(msg == "Greet")
  158.           {
  159.              
  160.              
  161.              
  162.               llListenRemove(listen_handle);
  163.               wait = FALSE;  //allow next user to use the terminal
  164.               allow = TRUE; // only allow a user to touch the object if a URL has been granted to the script.
  165.              
  166.              //http request to google app script to get url of drop box
  167.              recipient = id;
  168.              app_req = llHTTPRequest(app+"?client_url="+url,[HTTP_METHOD,"GET"],"");  // request url of server from spreadsheet database
  169.           }
  170.            
  171.         }
  172.        
  173.        
  174.        
  175.     }
  176.    
  177.    
  178.    
  179.    
  180.    
  181.    
  182.     changed(integer change)
  183.     {
  184.        
  185.       if(change & CHANGED_REGION_START || change & CHANGED_REGION || change & CHANGED_INVENTORY || change & CHANGED_TELEPORT)
  186.       {
  187.        llReleaseURL(url);    // verbosity just in case
  188.       llResetScript();
  189.       }
  190.        
  191.        
  192.     }
  193.    
  194. }
  195.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement