sloodle_setup_web.lslp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. //
  2. // The line above should be left blank to avoid script errors in OpenSim.
  3. // Sloodle web-configuration script
  4. // In the absence of a configuration notecard, allows an object to be authorised/configured via the web
  5. // Part of the Sloodle project (www.sloodle.org)
  6. //
  7. // Copyright (c) 2008 Sloodle
  8. // Released under the GNU GPL
  9. //
  10. // Contributors:
  11. // Peter R. Bloomfield
  12. // Edmund Edgar
  13. // Paul Preibisch - Fire Centaur in SL
  14. integer SLOODLE_CHANNEL_ERROR_TRANSLATION_REQUEST=-1828374651; // this channel is used to send status codes for translation to the error_messages lsl script
  15. integer SLOODLE_CHANNEL_OBJECT_DIALOG = -3857343;
  16. string SLOODLE_CONFIG_NOTECARD = "sloodle_config";
  17. string SLOODLE_EOF = "sloodleeof";
  18. integer SLOODLE_CHANNEL_AVATAR_DIALOG = 1001;
  19. string SLOODLE_VERSION_LINKER = "/mod/sloodle/version_linker.php";
  20. string SLOODLE_AUTH_LINKER = "/mod/sloodle/classroom/auth_object_linker.php";
  21. string SLOODLE_CONFIG_INTERFACE = "/mod/sloodle/classroom/configure_object.php";
  22. string SLOODLE_CONFIG_LINKER = "/mod/sloodle/classroom/object_config_linker.php";
  23. string SLOODLE_PING_LINKER = "/mod/sloodle/classroom/active_object_ping_linker.php";
  24. float PING_DELAY = 10800.0; // Number of seconds between pings (does not need to be very frequent)
  25. string SLOODLE_SCRIPT_PREFIX = "sloodle_mod_";
  26. float SLOODLE_VERSION_MIN = 0.3; // Minimum required version of Sloodle
  27. key httpcheckmoodle = NULL_KEY;
  28. key httpauthobject = NULL_KEY;
  29. key httpconfig = NULL_KEY;
  30. string sloodleserverroot = "";
  31. string sloodlepwd = ""; // stores the object-specific session key (UUID|pwd)
  32. string sloodleauthid = ""; // The ID which is passed to Moodle in the URL for the user authorisation step
  33. key sloodlemyrezzer = NULL_KEY; // Stores the UUID of the object that rezzed this one (if applicable)
  34. integer sloodlecontrollerid = 0; // So we know which Controller to access when PING-in the server later
  35. string password = ""; // stores the self-generated part of the password
  36. integer request_config = FALSE; // This is used when jumping from the idle state back to a configuration request
  37. integer show_config_url = TRUE; // By default, we will automatically show the config URL to the owner when config starts
  38. integer url_shown = FALSE; // At first we simply want to show the URL, but thereafter we can show the menu
  39. string SLOODLE_OBJECT_TYPE = "";
  40. ///// TRANSLATION /////
  41. // Link message channels
  42. integer SLOODLE_CHANNEL_TRANSLATION_REQUEST = -1928374651;
  43. integer SLOODLE_CHANNEL_TRANSLATION_RESPONSE = -1928374652;
  44. // Translation output methods
  45. string SLOODLE_TRANSLATE_LINK = "link"; // No output parameters - simply returns the translation on SLOODLE_TRANSLATION_RESPONSE link message channel
  46. string SLOODLE_TRANSLATE_SAY = "say"; // 1 output parameter: chat channel number
  47. string SLOODLE_TRANSLATE_WHISPER = "whisper"; // 1 output parameter: chat channel number
  48. string SLOODLE_TRANSLATE_SHOUT = "shout"; // 1 output parameter: chat channel number
  49. string SLOODLE_TRANSLATE_REGION_SAY = "regionsay"; // 1 output parameter: chat channel number
  50. string SLOODLE_TRANSLATE_OWNER_SAY = "ownersay"; // No output parameters
  51. string SLOODLE_TRANSLATE_DIALOG = "dialog"; // Recipient avatar should be identified in link message keyval. At least 2 output parameters: first the channel number for the dialog, and then 1 to 12 button label strings.
  52. string SLOODLE_TRANSLATE_LOAD_URL = "loadurl"; // Recipient avatar should be identified in link message keyval. 1 output parameter, containing the URL.
  53. string SLOODLE_TRANSLATE_HOVER_TEXT = "hovertext"; // 2 output parameters: colour <r,g,b>, and alpha value
  54. //FUNCTIONS
  55. /******************************************************************************************************************************
  56. * sloodle_error_code -
  57. * Author: Paul Preibisch
  58. * Description - This function sends a linked message on the SLOODLE_CHANNEL_ERROR_TRANSLATION_REQUEST channel
  59. * The error_messages script hears this, translates the status code and sends an instant message to the avuuid
  60. * Params: method - SLOODLE_TRANSLATE_SAY, SLOODLE_TRANSLATE_IM etc
  61. * Params: avuuid - this is the avatar UUID to that an instant message with the translated error code will be sent to
  62. * Params: status code - the status code of the error as on our wiki: http://slisweb.sjsu.edu/sl/index.php/Sloodle_status_codes
  63. *******************************************************************************************************************************/
  64. sloodle_error_code(string method, key avuuid,integer statuscode){
  65. llMessageLinked(LINK_SET, SLOODLE_CHANNEL_ERROR_TRANSLATION_REQUEST, method+"|"+(string)avuuid+"|"+(string)statuscode, NULL_KEY);
  66. }
  67. // Send a translation request link message
  68. sloodle_translation_request(string output_method, list output_params, string string_name, list string_params, key keyval, string batch)
  69. {
  70. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_TRANSLATION_REQUEST, output_method + "|" + llList2CSV(output_params) + "|" + string_name + "|" + llList2CSV(string_params) + "|" + batch, keyval);
  71. }
  72. ///// ----------- /////
  73. sloodle_tell_other_scripts(string msg)
  74. {
  75. sloodle_debug("Web configuration sending message to other scripts: "+msg);
  76. llMessageLinked(LINK_SET, SLOODLE_CHANNEL_OBJECT_DIALOG, msg, NULL_KEY);
  77. }
  78. sloodle_debug(string msg)
  79. {
  80. //llWhisper(0,msg);
  81. }
  82. // Determines if the objet has a configuration notecard
  83. // Returns true if so, or false otherwise
  84. integer sloodle_has_config_notecard()
  85. {
  86. return (llGetInventoryType(SLOODLE_CONFIG_NOTECARD) == INVENTORY_NOTECARD);
  87. }
  88. // Generate a random password string
  89. string sloodle_random_object_password()
  90. {
  91. return (string)(10000 + (integer)llFrand(999989999)); // Gets a random integer between 10000 and 999999999
  92. }
  93. // Show a menu letting the user choose between configuring the object, and downloading the configuration into SL
  94. sloodle_show_config_menu(key av)
  95. {
  96. sloodle_translation_request(SLOODLE_TRANSLATE_DIALOG, [SLOODLE_CHANNEL_AVATAR_DIALOG, "0", "1"], "webconfigmenu", ["0", "1"], av, "");
  97. llListen(SLOODLE_CHANNEL_AVATAR_DIALOG, "", av, "0");
  98. llListen(SLOODLE_CHANNEL_AVATAR_DIALOG, "", av, "1");
  99. }
  100. // Load the configuration URL
  101. sloodle_load_config_url(key av)
  102. {
  103. string url = sloodleserverroot + SLOODLE_CONFIG_INTERFACE + "?sloodleauthid=" + sloodleauthid + "&sloodleobjtype=" + SLOODLE_OBJECT_TYPE;
  104. sloodle_translation_request(SLOODLE_TRANSLATE_LOAD_URL, [url], "configlink", [], av, "");
  105. }
  106. // Check to see what type this script is to use
  107. // Returns the type/version identifier as a string, e.g. "chat-1.0"
  108. string sloodle_check_type()
  109. {
  110. // Find out how many scripts there are
  111. integer numscripts = llGetInventoryNumber(INVENTORY_SCRIPT);
  112. string type = "";
  113. string itemname = "";
  114. // Go through each item
  115. integer i = 0;
  116. for (i = 0; i < numscripts; i++) {
  117. // Get the name of this item
  118. itemname = llGetInventoryName(INVENTORY_SCRIPT, i);
  119. // Does this have the necessary prefix?
  120. if (llSubStringIndex(itemname, SLOODLE_SCRIPT_PREFIX) == 0) {
  121. // Ignore the script if it's not running... unless we don't already have a type identified
  122. if (llGetScriptState(itemname) == TRUE || type == "") {
  123. // Looks like this is our type
  124. type = llGetSubString(itemname, llStringLength(SLOODLE_SCRIPT_PREFIX), -1);
  125. }
  126. }
  127. }
  128. return type;
  129. }
  130. default
  131. {
  132. state_entry()
  133. {
  134. // Pause for a moment, in case all scripts were reset at the same time
  135. llSleep(0.2);
  136. sloodleserverroot = "";
  137. // Attempt to get the object type if we don't already have it
  138. if (SLOODLE_OBJECT_TYPE == "") {
  139. SLOODLE_OBJECT_TYPE = sloodle_check_type();
  140. if (SLOODLE_OBJECT_TYPE == "") {
  141. sloodle_translation_request(SLOODLE_TRANSLATE_WHISPER, [0], "notypeid", [], NULL_KEY, "");
  142. } else {
  143. sloodle_translation_request(SLOODLE_TRANSLATE_WHISPER, [0], "gottype", [SLOODLE_OBJECT_TYPE], NULL_KEY, "");
  144. }
  145. }
  146. // Listen for anything on the object dialog channel
  147. llListen(SLOODLE_CHANNEL_OBJECT_DIALOG, "", NULL_KEY, "");
  148. request_config = FALSE;
  149. // Do we have a configuration notecard?
  150. if (!sloodle_has_config_notecard()) {
  151. // No - if we have no starting parameter, then invite the user to use web-configuration
  152. if (llGetStartParameter() == 0) {
  153. sloodle_translation_request(SLOODLE_TRANSLATE_HOVER_TEXT, [<0.0,1.0,0.0>, 1.0], "touchforwebconfig", [], NULL_KEY, "");
  154. }
  155. }
  156. }
  157. state_exit()
  158. {
  159. llSetText("", <0.0,0.0,0.0>, 0.0);
  160. }
  161. listen(integer channel, string name, key id, string msg)
  162. {
  163. // Check the channel
  164. if (channel == 0 || channel == 1) {
  165. // Ignore anybody but the owner
  166. if (id != llGetOwner()) return;
  167. show_config_url = TRUE;
  168. // If the message starts with "http" then store it as the Moodle address
  169. msg = llStringTrim(msg, STRING_TRIM);
  170. if (llSubStringIndex(msg, "http") == 0) {
  171. // If the message ends with a slash, remove it.
  172. if ( llGetSubString(msg, -1, -1) == "/" ) {
  173. msg = llGetSubString(msg, 0, -2);
  174. }
  175. sloodleserverroot = msg;
  176. state check_moodle;
  177. return;
  178. }
  179. } else if (channel == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  180. // Check for standard messages, then for anything else
  181. if (msg == "CLEANUP") {
  182. // Did it come from the rezzer?
  183. if (id == sloodlemyrezzer && id != NULL_KEY) {
  184. // Delete this object
  185. sloodle_debug("Received CLEANUP command from object \"" + name + "\".");
  186. llDie();
  187. }
  188. } else {
  189. // Ignore anything not owned by the same person
  190. if (llGetOwnerKey(id) != llGetOwner()) return;
  191. // This should be a Sloodle initialisation message:
  192. // sloodle_init|<rezzer>|<target-uuid>|<moodle-address>|<authid>
  193. // (the last part is optional)
  194. list parts = llParseStringKeepNulls(msg, ["|"], []);
  195. integer numparts = llGetListLength(parts);
  196. string cmd = llList2String(parts, 0);
  197. // Check what the command is
  198. if (cmd == "sloodle_init") {
  199. // Make sure we have enough parts in the message
  200. if (llGetListLength(parts) < 4) return;
  201. key rezzer = (key)llList2String(parts, 1);
  202. key target = (key)llList2String(parts, 2);
  203. string url = llList2String(parts, 3);
  204. string auth = "";
  205. string isconfigured = "0";
  206. if (numparts >= 5) auth = llList2String(parts, 4);
  207. if (numparts >= 6) isconfigured = llList2String(parts, 5);
  208. // Make sure the command is correct, the UUIDs are OK, and that the URL looks valid
  209. if (rezzer == NULL_KEY) return;
  210. if (target != llGetKey()) return;
  211. url = llStringTrim(url, STRING_TRIM);
  212. if (llSubStringIndex(url, "http") != 0) return;
  213. // Store the settings
  214. sloodleserverroot = url;
  215. sloodleauthid = auth;
  216. sloodlemyrezzer = rezzer;
  217. // Do we have a password *and* an authorisation ID?
  218. if (llGetStartParameter() != 0 && (integer)auth > 0) {
  219. // Store the password
  220. password = (string)llGetStartParameter();
  221. sloodlepwd = (string)llGetKey() + "|" + password;
  222. if (isconfigured == "1") {
  223. request_config = TRUE; // we're already configured by a layout, so we can go right ahead and get our config off the server
  224. }
  225. // Allow the user to configure the object
  226. show_config_url = FALSE;
  227. state configure_object;
  228. return;
  229. }
  230. // Begin self-authorisation
  231. state check_moodle;
  232. }
  233. }
  234. }
  235. }
  236. touch_start(integer num_detected)
  237. {
  238. // Only pay attention to the object owner
  239. if (llDetectedKey(0) != llGetOwner()) return;
  240. // Do nothing if there is a configuration script present
  241. if (sloodle_has_config_notecard()) return;
  242. // We can do nothing without a server root
  243. if (sloodleserverroot == "") {
  244. llListen(0, "", llGetOwner(), "");
  245. llListen(1, "", llGetOwner(), "");
  246. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "chatserveraddress", [], NULL_KEY, "");
  247. sloodle_translation_request(SLOODLE_TRANSLATE_HOVER_TEXT, [<0.0,1.0,0.0>, 0.8], "waitingforserveraddress", [], NULL_KEY, "");
  248. return;
  249. }
  250. // Start the configuration process
  251. state check_moodle;
  252. }
  253. link_message(integer sender_num, integer num, string sval, key kval)
  254. {
  255. // Check the channel
  256. if (num == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  257. // Is it a reset command?
  258. if (sval == "do:reset") {
  259. llResetScript();
  260. return;
  261. }
  262. }
  263. }
  264. }
  265. // Check that the Moodle site is valid
  266. state check_moodle
  267. {
  268. state_entry()
  269. {
  270. sloodle_translation_request(SLOODLE_TRANSLATE_HOVER_TEXT, [<0.0,1.0,0.0>, 0.8], "checkingserverat", [sloodleserverroot], NULL_KEY, "");
  271. httpcheckmoodle = llHTTPRequest(sloodleserverroot + SLOODLE_VERSION_LINKER, [HTTP_METHOD, "GET"], "");
  272. // Listen for chat messages from the rezzer
  273. if (sloodlemyrezzer != NULL_KEY) llListen(SLOODLE_CHANNEL_OBJECT_DIALOG, "", NULL_KEY, "");
  274. }
  275. state_exit()
  276. {
  277. llSetText("", <0.0,0.0,0.0>, 0.0);
  278. httpcheckmoodle = NULL_KEY;
  279. }
  280. http_response(key id, integer status, list meta, string body)
  281. {
  282. // Make sure it's the response we're expecting
  283. if (id != httpcheckmoodle) return;
  284. httpcheckmoodle = NULL_KEY;
  285. // Check the status code
  286. if (status != 200) {
  287. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "httperror:code", [status], NULL_KEY, "");
  288. return;
  289. }
  290. // Split the response into lines and get the status line info
  291. list lines = llParseStringKeepNulls(body, ["\n"], []);
  292. integer numlines = llGetListLength(lines);
  293. list statusfields = llParseStringKeepNulls(llList2String(lines, 0), ["|"], []);
  294. integer statuscode = (integer)llList2String(statusfields, 0);
  295. // Make sure the status code was OK
  296. if (statuscode == -106) {
  297. sloodle_debug("ERROR -106: the Sloodle module is not installed on the specified Moodle site (" + sloodleserverroot + ")");
  298. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "sloodlenotinstalled", [], NULL_KEY, "");
  299. return;
  300. } else if (statuscode <= 0) {
  301. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "failedcheckcompatibility", [], NULL_KEY, "");
  302. sloodle_error_code(SLOODLE_TRANSLATE_SAY, NULL_KEY,statuscode); //send message to error_message.lsl
  303. return;
  304. }
  305. // Make sure we have enough other data
  306. if (numlines < 2) {
  307. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "badresponseformat", [], NULL_KEY, "");
  308. return;
  309. }
  310. // Extract the Sloodle version number
  311. list datafields = llParseStringKeepNulls(llList2String(lines, 1), ["|"], []);
  312. float installedversion = (float)llList2String(datafields, 0);
  313. // Check compatibility
  314. if (installedversion < SLOODLE_VERSION_MIN) {
  315. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "sloodleversioninstalled", [installedversion], NULL_KEY, "");
  316. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "sloodleversionrequired", [SLOODLE_VERSION_MIN], NULL_KEY, "");
  317. return;
  318. }
  319. // Initiate object authorisation
  320. state auth_object_initial;
  321. }
  322. touch_start(integer num_detected)
  323. {
  324. // Revert to the default state if the owner touched
  325. if (llDetectedKey(0) != llGetOwner()) return;
  326. state default;
  327. }
  328. link_message(integer sender_num, integer num, string sval, key kval)
  329. {
  330. // Check the channel
  331. if (num == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  332. // Is it a reset command?
  333. if (sval == "do:reset") {
  334. llResetScript();
  335. return;
  336. }
  337. }
  338. }
  339. listen(integer channel, string name, key id, string msg)
  340. {
  341. // Check the channel
  342. if (channel == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  343. // Parse the message
  344. list parts = llParseStringKeepNulls(msg, ["|"], []);
  345. if (llGetListLength(parts) < 2) return;
  346. string cmd = llList2String(parts, 0);
  347. string val = llList2String(parts, 1);
  348. // Is it a recognised command?
  349. if (cmd == "do:cleanup") {
  350. // Did it come from our rezzer?
  351. key kval = (key)val;
  352. if (kval == sloodlemyrezzer && kval != NULL_KEY) {
  353. // Delete this object
  354. sloodle_debug("Received CLEANUP command from object \"" + name + "\".");
  355. llDie();
  356. return;
  357. }
  358. }
  359. }
  360. }
  361. on_rez(integer start_param)
  362. {
  363. llResetScript();
  364. }
  365. }
  366. // Initial object authorisation (stores details in the database)
  367. state auth_object_initial
  368. {
  369. state_entry()
  370. {
  371. sloodle_translation_request(SLOODLE_TRANSLATE_HOVER_TEXT, [<0.0, 1.0, 0.0>, 0.8], "initobjectauth", [], NULL_KEY, "");
  372. // Generate a random password
  373. password = sloodle_random_object_password();
  374. sloodlepwd = (string)llGetKey() + "|" + password;
  375. // Initiate the object authorisation
  376. string body = "sloodleobjuuid="+(string)llGetKey()+"&sloodleobjname="+llGetObjectName()+"&sloodleobjpwd="+password+"&sloodleobjtype="+SLOODLE_OBJECT_TYPE;
  377. httpauthobject = llHTTPRequest(sloodleserverroot + SLOODLE_AUTH_LINKER, [HTTP_METHOD, "POST", HTTP_MIMETYPE, "application/x-www-form-urlencoded"], body);
  378. // Listen for chat messages from the rezzer
  379. if (sloodlemyrezzer != NULL_KEY) llListen(SLOODLE_CHANNEL_OBJECT_DIALOG, "", NULL_KEY, "");
  380. }
  381. state_exit()
  382. {
  383. llSetText("", <0.0,0.0,0.0>, 0.0);
  384. httpauthobject = NULL_KEY;
  385. }
  386. http_response(key id, integer status, list meta, string body)
  387. {
  388. // Make sure this is the response we're expecting
  389. if (id != httpauthobject) return;
  390. if (status != 200) {
  391. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "httperror:code", [status], NULL_KEY, "");
  392. return;
  393. }
  394. // Split the response into lines
  395. list lines = llParseStringKeepNulls(body, ["\n"], []);
  396. integer numlines = llGetListLength(lines);
  397. list statusfields = llParseStringKeepNulls(llList2String(lines, 0), ["|"], []);
  398. integer statuscode = (integer)llList2String(statusfields, 0);
  399. // Check the statuscode
  400. if (statuscode <= 0) {
  401. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "objectauthfailed:code", [statuscode], NULL_KEY, "");
  402. sloodle_error_code(SLOODLE_TRANSLATE_SAY, NULL_KEY,statuscode); //send message to error_message.lsl
  403. return;
  404. }
  405. // Attempt to get the auth ID
  406. if (numlines < 2) {
  407. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "badresponseformat", [], NULL_KEY, "");
  408. return;
  409. }
  410. sloodleauthid = llList2String(lines, 1);
  411. // Start the configuration
  412. state configure_object;
  413. }
  414. touch_start(integer num_detected)
  415. {
  416. // Revert to the default state if the owner touched
  417. if (llDetectedKey(0) != llGetOwner()) return;
  418. state default;
  419. }
  420. link_message(integer sender_num, integer num, string sval, key kval)
  421. {
  422. // Check the channel
  423. if (num == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  424. // Is it a reset command?
  425. if (sval == "do:reset") {
  426. llResetScript();
  427. return;
  428. }
  429. }
  430. }
  431. on_rez(integer start_param)
  432. {
  433. llResetScript();
  434. }
  435. listen(integer channel, string name, key id, string msg)
  436. {
  437. // Check the channel
  438. if (channel == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  439. // Parse the message
  440. list parts = llParseStringKeepNulls(msg, ["|"], []);
  441. if (llGetListLength(parts) < 2) return;
  442. string cmd = llList2String(parts, 0);
  443. string val = llList2String(parts, 1);
  444. // Is it a recognised command?
  445. if (cmd == "do:cleanup") {
  446. // Did it come from our rezzer?
  447. key kval = (key)val;
  448. if (kval == sloodlemyrezzer && kval != NULL_KEY) {
  449. // Delete this object
  450. sloodle_debug("Received CLEANUP command from object \"" + name + "\".");
  451. llDie();
  452. return;
  453. }
  454. }
  455. }
  456. }
  457. }
  458. // Send the user to the configuration page on the Moodle site.
  459. // (That page will present authorisation options as necessary).
  460. // If touched, ask the user if they want the URL again, or to download the configuration.
  461. state configure_object
  462. {
  463. state_entry()
  464. {
  465. url_shown = FALSE;
  466. sloodlecontrollerid = 0;
  467. // Has object configuration been requested?
  468. if (request_config) {
  469. request_config = FALSE;
  470. llSetText("Requesting configuration...", <0.0,1.0,0.0>, 0.8);
  471. httpconfig = llHTTPRequest(sloodleserverroot + SLOODLE_CONFIG_LINKER + "?sloodlepwd="+sloodlepwd+"&sloodleauthid="+sloodleauthid, [HTTP_METHOD, "GET"], "");
  472. } else {
  473. llSetText("Waiting for configuration.\nTouch me for a URL, or to download the configuration.", <0.0,1.0,0.0>, 0.8);
  474. // Load the URL immediately
  475. if (show_config_url) {
  476. sloodle_load_config_url(llGetOwner());
  477. url_shown = TRUE;
  478. }
  479. }
  480. // Listen for chat messages from the rezzer
  481. if (sloodlemyrezzer != NULL_KEY) llListen(SLOODLE_CHANNEL_OBJECT_DIALOG, "", NULL_KEY, "");
  482. }
  483. state_exit()
  484. {
  485. llSetText("", <0.0,0.0,0.0>, 0.0);
  486. httpconfig = NULL_KEY;
  487. }
  488. touch_start(integer num_detected)
  489. {
  490. // Ignore anybody but the owner
  491. if (llDetectedKey(0) != llGetOwner()) return;
  492. // If the URL has already been shown, then display the menu.
  493. // Otherwise, offer the URL
  494. if (url_shown) sloodle_show_config_menu(llGetOwner());
  495. else {
  496. sloodle_load_config_url(llGetOwner());
  497. url_shown = TRUE;
  498. }
  499. }
  500. listen(integer channel, string name, key id, string msg)
  501. {
  502. // Check the channel
  503. if (channel == SLOODLE_CHANNEL_AVATAR_DIALOG)
  504. {
  505. // Check it's the owner talking to us
  506. if (id != llGetOwner()) return;
  507. // What was the message?
  508. if (msg == "0") {
  509. // Load the configuration URL
  510. sloodle_load_config_url(llGetOwner());
  511. } else if (msg == "1") {
  512. // Download the configuration from the site
  513. httpconfig = llHTTPRequest(sloodleserverroot + SLOODLE_CONFIG_LINKER + "?sloodlepwd="+sloodlepwd+"&sloodleauthid="+sloodleauthid, [HTTP_METHOD, "GET"], "");
  514. }
  515. } else if (channel == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  516. // Parse the message
  517. list parts = llParseStringKeepNulls(msg, ["|"], []);
  518. if (llGetListLength(parts) < 2) return;
  519. string cmd = llList2String(parts, 0);
  520. string val = llList2String(parts, 1);
  521. // Is it a recognised command?
  522. if (cmd == "do:cleanup") {
  523. // Did it come from our rezzer?
  524. key kval = (key)val;
  525. if (kval == sloodlemyrezzer && kval != NULL_KEY) {
  526. // Delete this object
  527. sloodle_debug("Received CLEANUP command from object \"" + name + "\".");
  528. llDie();
  529. return;
  530. }
  531. }
  532. }
  533. }
  534. http_response(key id, integer status, list meta, string body)
  535. {
  536. // Is this the response we're expecting?
  537. if (id == httpconfig) {
  538. httpconfig = NULL_KEY;
  539. // Check the return code
  540. if (status != 200) {
  541. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "httperror:code", [status], NULL_KEY, "");
  542. return;
  543. }
  544. // Split the response into lines
  545. list lines = llParseStringKeepNulls(body, ["\n"], []);
  546. integer numlines = llGetListLength(lines);
  547. // Fetch the status line
  548. list statusfields = llParseStringKeepNulls(llList2String(lines, 0), ["|"], []);
  549. integer statuscode = (integer)llList2String(statusfields, 0);
  550. if (statuscode == -103) {
  551. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "noconfigavailable", [], NULL_KEY, "");
  552. return;
  553. } else if (statuscode <= 0) {
  554. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "objectconfigfailed:code", [statuscode], NULL_KEY, "");
  555. sloodle_error_code(SLOODLE_TRANSLATE_SAY, NULL_KEY,statuscode); //send message to error_message.lsl
  556. state default;
  557. return;
  558. }
  559. // Indicate that we are sending configuration data
  560. sloodle_translation_request(SLOODLE_TRANSLATE_HOVER_TEXT, [<0.0, 1.0, 0.0>, 0.8], "sendingconfig", [], NULL_KEY, "");
  561. // This will be our buffer of configuration commands
  562. string cmdbuffer = "";
  563. integer maxbufferlength = 300;
  564. integer cmdbufferlength = 0;
  565. // Add the server address and password in as the first commands. Also add the rezzer key if we have one
  566. cmdbuffer = "set:sloodleserverroot|"+sloodleserverroot+"\nset:sloodlepwd|" + sloodlepwd + "\n";
  567. if (sloodlemyrezzer != NULL_KEY) cmdbuffer += "set:sloodlemyrezzer|" + (string)sloodlemyrezzer + "\n";
  568. cmdbufferlength = llStringLength(cmdbuffer);
  569. // Go through each data line
  570. integer linenum = 1;
  571. string cmd = "";
  572. integer cmdlen = 0;
  573. string curline = "";
  574. for (linenum=0; linenum < numlines; linenum++) {
  575. curline = llList2String(lines, linenum);
  576. // If this is a controller ID, then store the value.
  577. // (Don't bother checking if we already have a controller id)
  578. if (sloodlecontrollerid == 0 && llSubStringIndex(curline, "sloodlecontrollerid") == 0) {
  579. list parts = llParseStringKeepNulls(curline, ["|"], []);
  580. if (llGetListLength(parts) > 1) sloodlecontrollerid = (integer)llList2String(parts, 1);
  581. }
  582. // This should be "name|value" format, so just prefix it with "set:"
  583. cmd = "set:" + curline + "\n";
  584. cmdlen = llStringLength(cmd);
  585. // Ignore lengths of less than 5
  586. if (cmdlen >= 5) {
  587. // If the addition of this command will overflow the buffer, then send the buffer first
  588. if ((cmdbufferlength + cmdlen) > maxbufferlength) {
  589. sloodle_tell_other_scripts(cmdbuffer);
  590. cmdbuffer = "";
  591. cmdbufferlength = 0;
  592. }
  593. // Add the current command to the buffer
  594. cmdbuffer += cmd;
  595. cmdbufferlength += cmdlen;
  596. }
  597. }
  598. // If there is anything left in the buffer, then send it
  599. if (cmdbufferlength > 0) {
  600. sloodle_tell_other_scripts(cmdbuffer);
  601. cmdbuffer = "";
  602. cmdbufferlength = 0;
  603. }
  604. // After a brief pause, send the EOF command
  605. llSleep(0.15);
  606. sloodle_tell_other_scripts(SLOODLE_EOF);
  607. // We're now finished
  608. state idle;
  609. }
  610. }
  611. link_message(integer sender_num, integer num, string sval, key kval)
  612. {
  613. // Ignore anything from this script
  614. if (sender_num == llGetLinkNumber()) return;
  615. // Check the channel
  616. if (num == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  617. // Is it a reset command?
  618. if (sval == "do:reset") {
  619. llResetScript();
  620. return;
  621. }
  622. }
  623. }
  624. on_rez(integer start_param)
  625. {
  626. llResetScript();
  627. }
  628. }
  629. // In this state, the script has either finished, or been instructed not to execute
  630. // It will only respond to a reset command
  631. state idle
  632. {
  633. state_entry()
  634. {
  635. // Listen for chat messages from the rezzer
  636. if (sloodlemyrezzer != NULL_KEY) llListen(SLOODLE_CHANNEL_OBJECT_DIALOG, "", NULL_KEY, "");
  637. // Regularly ping the server to notify it that this object is still 'alive'.
  638. // (But only if we have a valid controller id)
  639. llSetTimerEvent(0.0);
  640. if (sloodlecontrollerid > 0) llSetTimerEvent(PING_DELAY);
  641. }
  642. timer()
  643. {
  644. // Send our ping request and ignore the response
  645. string body = "sloodlecontrollerid=" + (string)sloodlecontrollerid;
  646. body += "&sloodlepwd=" + sloodlepwd;
  647. body += "&sloodleobjuuid=" + (string)llGetKey();
  648. llHTTPRequest(sloodleserverroot + SLOODLE_PING_LINKER, [HTTP_METHOD, "POST", HTTP_MIMETYPE, "application/x-www-form-urlencoded"], body);
  649. }
  650. listen(integer channel, string name, key id, string msg)
  651. {
  652. // Check the channel
  653. if (channel == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  654. // Parse the message
  655. list parts = llParseStringKeepNulls(msg, ["|"], []);
  656. if (llGetListLength(parts) < 2) return;
  657. string cmd = llList2String(parts, 0);
  658. string val = llList2String(parts, 1);
  659. // Is it a recognised command?
  660. if (cmd == "do:cleanup") {
  661. // Did it come from our rezzer?
  662. key kval = (key)val;
  663. if (kval == sloodlemyrezzer && kval != NULL_KEY) {
  664. // Delete this object
  665. sloodle_debug("Received CLEANUP command from object \"" + name + "\".");
  666. llDie();
  667. return;
  668. }
  669. }
  670. }
  671. }
  672. link_message(integer sender_num, integer num, string sval, key kval)
  673. {
  674. // Check the channel
  675. if (num == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  676. // Check the command type
  677. if (sval == "do:reset") {
  678. llResetScript();
  679. return;
  680. } else if (sval == "do:requestconfig") {
  681. // Send the configuration data again, so long as there isn't a notecard
  682. if (sloodle_has_config_notecard() == FALSE) {
  683. request_config = TRUE;
  684. state configure_object;
  685. } else {
  686. state default;
  687. }
  688. return;
  689. } else if (sval == "do:reconfigure") {
  690. // Let the user re-configure from the start
  691. show_config_url = TRUE;
  692. state configure_object;
  693. }
  694. }
  695. }
  696. on_rez(integer param)
  697. {
  698. // Temporary measure: request the existing configuration.
  699. // (this allows a pre-configured object to be rezzed from inventory, assuming the active object entry has not yet expired)
  700. request_config = TRUE;
  701. state configure_object;
  702. }
  703. }
  704. // Please leave the following line intact to show where the script lives in Git:
  705. // SLOODLE LSL Script Git Location: assets/sloodle_setup_web.lslp