sloodle_mod_set-1.0.lslp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. //
  2. // The line above should be left blank to avoid script errors in OpenSim.
  3. // Sloodle Set manager script (for Sloodle 0.3).
  4. // Performs the overall management of a Sloodle Set's configuration.
  5. //
  6. // Part of the Sloodle project (www.sloodle.org).
  7. // Copyright (c) 2007-8 Sloodle
  8. // Released under the GNU GPL v3
  9. //
  10. // Contributors:
  11. // Edmund Edgar
  12. // Peter R. Bloomfield
  13. ///// DATA /////
  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_OBJECT_CREATOR_TYPE_BASIC_SET = 0;
  16. integer SLOODLE_OBJECT_CREATOR_TYPE_MOTHERSHIP = 1;
  17. integer SLOODLE_THIS_OBJECT_TYPE = 0;
  18. integer SLOODLE_CHANNEL_OBJECT_DIALOG = -3857343;
  19. integer SLOODLE_CHANNEL_AVATAR_DIALOG = 1001;
  20. string SLOODLE_SET_LINKER = "/mod/sloodle/mod/set-1.0/linker.php";
  21. string SLOODLE_COURSEINFO_LINKER = "/mod/sloodle/classroom/course_info_linker.php";
  22. string SLOODLE_EOF = "sloodleeof";
  23. integer SLOODLE_OBJECT_ACCESS_LEVEL_PUBLIC = 0;
  24. integer SLOODLE_OBJECT_ACCESS_LEVEL_OWNER = 1;
  25. integer SLOODLE_OBJECT_ACCESS_LEVEL_GROUP = 2;
  26. string SLOODLE_OBJECT_TYPE = "set-1.0";
  27. string sloodleserverroot = "";
  28. string sloodlepwd = "";
  29. integer sloodlecontrollerid = 0;
  30. string sloodlecoursename_full = "";
  31. integer sloodleobjectaccessleveluse = 0;
  32. integer isconfigured = FALSE; // Do we have all the configuration data we need?
  33. integer eof = FALSE; // Have we reached the end of the configuration data?
  34. key httpcheckcourse = NULL_KEY;
  35. list cmddialog = []; // Alternating list of keys and timestamps, indicating who activated a dialog, and when
  36. string MENU_BUTTON_RESET = "0";
  37. string MENU_BUTTON_OPEN_REZ_DIALOG = "1"; // The same as touching the cargo bay
  38. string MENU_BUTTON_OPEN_LAYOUT_DIALOG = "2"; // The same as touching the layout button
  39. string MENU_BUTTON_KILL_ALL = "3"; // The same as touching the kill button
  40. integer TEX_SIDE = 2; // The side to apply our texture to for "ready" or otherwise
  41. ///// TRANSLATION /////
  42. // Link message channels
  43. integer SLOODLE_CHANNEL_TRANSLATION_REQUEST = -1928374651;
  44. integer SLOODLE_CHANNEL_TRANSLATION_RESPONSE = -1928374652;
  45. integer SLOODLE_CHANNEL_SET_CONFIGURED = -1639270091;
  46. integer SLOODLE_CHANNEL_SET_RESET = -1639270092;
  47. integer SLOODLE_CHANNEL_SET_MENU_BUTTON_OPEN_REZ_DIALOG = -1639270093;
  48. integer SLOODLE_CHANNEL_SET_MENU_BUTTON_OPEN_LAYOUT_DIALOG = -1639270094;
  49. integer SLOODLE_CHANNEL_SET_MENU_BUTTON_KILL_ALL = -1639270095;
  50. // Translation output methods
  51. string SLOODLE_TRANSLATE_LINK = "link"; // No output parameters - simply returns the translation on SLOODLE_TRANSLATION_RESPONSE link message channel
  52. string SLOODLE_TRANSLATE_SAY = "say"; // 1 output parameter: chat channel number
  53. string SLOODLE_TRANSLATE_WHISPER = "whisper"; // 1 output parameter: chat channel number
  54. string SLOODLE_TRANSLATE_SHOUT = "shout"; // 1 output parameter: chat channel number
  55. string SLOODLE_TRANSLATE_REGION_SAY = "regionsay"; // 1 output parameter: chat channel number
  56. string SLOODLE_TRANSLATE_OWNER_SAY = "ownersay"; // No output parameters
  57. 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.
  58. string SLOODLE_TRANSLATE_LOAD_URL = "loadurl"; // Recipient avatar should be identified in link message keyval. 1 output parameter giving URL to load.
  59. string SLOODLE_TRANSLATE_HOVER_TEXT = "hovertext"; // 2 output parameters: colour <r,g,b>, and alpha value
  60. string SLOODLE_TRANSLATE_IM = "instantmessage"; // Recipient avatar should be identified in link message keyval. No output parameters.
  61. // Send a translation request link message
  62. sloodle_translation_request(string output_method, list output_params, string string_name, list string_params, key keyval, string batch)
  63. {
  64. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_TRANSLATION_REQUEST, output_method + "|" + llList2CSV(output_params) + "|" + string_name + "|" + llList2CSV(string_params) + "|" + batch, keyval);
  65. }
  66. ///// ----------- /////
  67. ///// FUNCTIONS /////
  68. /******************************************************************************************************************************
  69. * sloodle_error_code -
  70. * Author: Paul Preibisch
  71. * Description - This function sends a linked message on the SLOODLE_CHANNEL_ERROR_TRANSLATION_REQUEST channel
  72. * The error_messages script hears this, translates the status code and sends an instant message to the avuuid
  73. * Params: method - SLOODLE_TRANSLATE_SAY, SLOODLE_TRANSLATE_IM etc
  74. * Params: avuuid - this is the avatar UUID to that an instant message with the translated error code will be sent to
  75. * Params: status code - the status code of the error as on our wiki: http://slisweb.sjsu.edu/sl/index.php/Sloodle_status_codes
  76. *******************************************************************************************************************************/
  77. sloodle_error_code(string method, key avuuid,integer statuscode){
  78. llMessageLinked(LINK_SET, SLOODLE_CHANNEL_ERROR_TRANSLATION_REQUEST, method+"|"+(string)avuuid+"|"+(string)statuscode, NULL_KEY);
  79. }
  80. // Send debug info
  81. sloodle_debug(string msg)
  82. {
  83. llMessageLinked(LINK_THIS, DEBUG_CHANNEL, msg, NULL_KEY);
  84. }
  85. // Reset the whole object
  86. sloodle_reset()
  87. {
  88. llMessageLinked(LINK_SET, SLOODLE_CHANNEL_OBJECT_DIALOG, "do:reset", NULL_KEY);
  89. llResetScript();
  90. }
  91. // Configure by receiving a linked message from another script in the object
  92. // Returns TRUE if the object has all the data it needs
  93. integer sloodle_handle_command(string str)
  94. {
  95. sloodle_debug("Handling command: " + str);
  96. list bits = llParseString2List(str,["|"],[]);
  97. integer numbits = llGetListLength(bits);
  98. string name = llList2String(bits,0);
  99. string value1 = "";
  100. string value2 = "";
  101. if (numbits > 1) value1 = llList2String(bits,1);
  102. if (numbits > 2) value2 = llList2String(bits,2);
  103. if (name == "set:sloodleserverroot") sloodleserverroot = value1;
  104. if (name == "set:sloodlecoursename_full") sloodlecoursename_full = value1;
  105. else if (name == "set:sloodlepwd") {
  106. // The password may be a single prim password, or a UUID and a password
  107. if (value2 != "") sloodlepwd = value1 + "|" + value2;
  108. else sloodlepwd = value1;
  109. } else if (name == "set:sloodlecontrollerid") sloodlecontrollerid = (integer)value1;
  110. else if (name == "set:sloodleobjectaccessleveluse") sloodleobjectaccessleveluse = (integer)value1;
  111. else if (name == SLOODLE_EOF) eof = TRUE;
  112. return (sloodleserverroot != "" && sloodlepwd != "" && sloodlecontrollerid > 0);
  113. }
  114. // Checks if the given agent is permitted to control this object
  115. // Returns TRUE if so, or FALSE if not
  116. integer sloodle_check_access_ctrl(key id)
  117. {
  118. // Only the owner can control this
  119. return (id == llGetOwner());
  120. }
  121. // Checks if the given agent is permitted to user this object
  122. // Returns TRUE if so, or FALSE if not
  123. integer sloodle_check_access_use(key id)
  124. {
  125. // The owner can always use this
  126. if (id == llGetOwner()) return TRUE;
  127. // Check the access mode
  128. if (sloodleobjectaccessleveluse == SLOODLE_OBJECT_ACCESS_LEVEL_GROUP) {
  129. return llSameGroup(id);
  130. } else if (sloodleobjectaccessleveluse == SLOODLE_OBJECT_ACCESS_LEVEL_PUBLIC) {
  131. return TRUE;
  132. }
  133. return FALSE;
  134. }
  135. // Show a command dialog to the specified user
  136. sloodle_show_command_dialog(key id)
  137. {
  138. if (SLOODLE_THIS_OBJECT_TYPE == SLOODLE_OBJECT_CREATOR_TYPE_BASIC_SET) {
  139. sloodle_translation_request(SLOODLE_TRANSLATE_DIALOG, [SLOODLE_CHANNEL_AVATAR_DIALOG, MENU_BUTTON_RESET, MENU_BUTTON_OPEN_REZ_DIALOG], "sloodleset:cmddialog_simple", [MENU_BUTTON_RESET, MENU_BUTTON_OPEN_REZ_DIALOG], id, "set");
  140. } else {
  141. sloodle_translation_request(SLOODLE_TRANSLATE_DIALOG, [SLOODLE_CHANNEL_AVATAR_DIALOG, MENU_BUTTON_RESET, MENU_BUTTON_OPEN_REZ_DIALOG , MENU_BUTTON_OPEN_LAYOUT_DIALOG , MENU_BUTTON_KILL_ALL ], "sloodleset:cmddialog_mothership", [MENU_BUTTON_RESET, MENU_BUTTON_OPEN_REZ_DIALOG , MENU_BUTTON_OPEN_LAYOUT_DIALOG , MENU_BUTTON_KILL_ALL], id, "set");
  142. }
  143. }
  144. // Add the given agent to our command dialog list
  145. sloodle_add_cmd_dialog(key id)
  146. {
  147. // Does the person already exist?
  148. integer pos = llListFindList(cmddialog, [id]);
  149. if (pos < 0) {
  150. // No - add the agent to the end
  151. cmddialog += [id, llGetUnixTime()];
  152. } else {
  153. // Yes - update the time
  154. cmddialog = llListReplaceList(cmddialog, [llGetUnixTime()], pos + 1, pos + 1);
  155. }
  156. }
  157. // Remove the given agent from our command dialog list
  158. sloodle_remove_cmd_dialog(key id)
  159. {
  160. // Is the person in the list?
  161. integer pos = llListFindList(cmddialog, [id]);
  162. if (pos >= 0) {
  163. // Yes - remove them and their timestamp
  164. cmddialog = llDeleteSubList(cmddialog, pos, pos + 1);
  165. }
  166. }
  167. // Purge the command dialog list of old activity
  168. sloodle_purge_cmd_dialog()
  169. {
  170. // Store the current timestamp
  171. integer curtime = llGetUnixTime();
  172. // Go through each command dialog
  173. integer i = 0;
  174. while (i < llGetListLength(cmddialog)) {
  175. // Is the current timestamp more than 12 seconds old?
  176. if ((curtime - llList2Integer(cmddialog, i + 1)) > 12) {
  177. // Yes - remove it
  178. cmddialog = llDeleteSubList(cmddialog, i, i + 1);
  179. } else {
  180. // No - advance to the next
  181. i += 2;
  182. }
  183. }
  184. }
  185. default
  186. {
  187. state_entry()
  188. {
  189. // Starting again with a new configuration
  190. // llSetTexture("touch_to_set_moodle", TEX_SIDE);
  191. llMessageLinked(LINK_SET, SLOODLE_CHANNEL_SET_RESET, "", NULL_KEY);
  192. llSetText("", <0.0,0.0,0.0>, 0.0);
  193. isconfigured = FALSE;
  194. eof = FALSE;
  195. // Reset our data
  196. sloodleserverroot = "";
  197. sloodlepwd = "";
  198. sloodlecontrollerid = 0;
  199. sloodlecoursename_full = "";
  200. }
  201. link_message(integer sender_num, integer num, string str, key id)
  202. {
  203. // Check the channel
  204. if (num == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  205. // Split the message into lines
  206. list lines = llParseString2List(str, ["\n"], []);
  207. integer numlines = llGetListLength(lines);
  208. integer i = 0;
  209. for (; i < numlines; i++) {
  210. isconfigured = sloodle_handle_command(llList2String(lines, i));
  211. }
  212. // If we've got all our data AND reached the end of the configuration data, then move on
  213. if (eof == TRUE) {
  214. if (isconfigured == TRUE) {
  215. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "configurationreceived", [], NULL_KEY, "");
  216. if (sloodlecoursename_full == "") state check_course;
  217. else state ready;
  218. } else {
  219. // No more data, but we're not configured yet...
  220. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "configdatamissing", [], NULL_KEY, "");
  221. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_OBJECT_DIALOG, "do:reconfigure", NULL_KEY);
  222. eof = FALSE;
  223. }
  224. }
  225. }
  226. }
  227. touch_start(integer num_detected)
  228. {
  229. // Attempt to request a reconfiguration
  230. if (llDetectedKey(0) == llGetOwner()) {
  231. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_OBJECT_DIALOG, "do:requestconfig", NULL_KEY);
  232. }
  233. }
  234. on_rez(integer par)
  235. {
  236. llResetScript();
  237. }
  238. }
  239. // This state will check which course we are connecting to (only necessary for notecard setups)
  240. state check_course
  241. {
  242. state_entry()
  243. {
  244. sloodle_translation_request(SLOODLE_TRANSLATE_HOVER_TEXT, [<0.0,1.0,0.0>, 1.0], "checkingcourse", [], NULL_KEY, "");
  245. // Send our course check request
  246. httpcheckcourse = llHTTPRequest(sloodleserverroot + SLOODLE_COURSEINFO_LINKER + "?sloodlecontrollerid=" + (string)sloodlecontrollerid + "&sloodlepwd=" + sloodlepwd, [HTTP_METHOD, "GET"], "");
  247. }
  248. http_response(key id, integer status, list meta, string body)
  249. {
  250. // Make sure this is the data we expect
  251. if (id != httpcheckcourse) return;
  252. httpcheckcourse = NULL_KEY;
  253. // Check that we got a proper response
  254. if (status != 200) {
  255. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "httperror:code", [status], NULL_KEY, "");
  256. sloodle_translation_request(SLOODLE_TRANSLATE_HOVER_TEXT, [<1.0,0.0,0.0>, 1.0], "errortouchtoreset", [], NULL_KEY, "");
  257. return;
  258. }
  259. if (body == "") {
  260. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "httpempty", [], NULL_KEY, "");
  261. sloodle_translation_request(SLOODLE_TRANSLATE_HOVER_TEXT, [<1.0,0.0,0.0>, 1.0], "errortouchtoreset", [], NULL_KEY, "");
  262. return;
  263. }
  264. // Split the response at each line, then at each field
  265. list lines = llParseStringKeepNulls(body, ["\n"], []);
  266. integer numlines = llGetListLength(lines);
  267. // Make sure there were enough lines
  268. if (numlines < 2) {
  269. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "badresponseformat", [], NULL_KEY, "");
  270. sloodle_translation_request(SLOODLE_TRANSLATE_HOVER_TEXT, [<1.0,0.0,0.0>, 1.0], "errortouchtoreset", [], NULL_KEY, "");
  271. return;
  272. }
  273. // The first item should be the status code
  274. list statusfields = llParseStringKeepNulls(llList2String(lines, 0), ["|"], []);
  275. integer statuscode = llList2Integer(statusfields, 0);
  276. // The status could should be positive if successful
  277. if (statuscode == -106) {
  278. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "sloodlenotinstalled", [], NULL_KEY, "");
  279. sloodle_translation_request(SLOODLE_TRANSLATE_HOVER_TEXT, [<1.0,0.0,0.0>, 1.0], "errortouchtoreset", [], NULL_KEY, "");
  280. return;
  281. } else if (statuscode <= 0) {
  282. // Get the error message if one was given
  283. if (llGetListLength(lines) > 1) {
  284. string errmsg = llList2String(lines, 1);
  285. sloodle_debug("ERROR " + (string)statuscode + ": " + errmsg);
  286. }
  287. //sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "servererror", [statuscode], NULL_KEY, "");
  288. sloodle_error_code(SLOODLE_TRANSLATE_SAY, NULL_KEY,statuscode); //send message to error_message.lsl
  289. sloodle_translation_request(SLOODLE_TRANSLATE_HOVER_TEXT, [<1.0,0.0,0.0>, 1.0], "errortouchtoreset", [], NULL_KEY, "");
  290. return;
  291. }
  292. // Everything seems fine - extract the data
  293. list coursenames = llParseStringKeepNulls(llList2String(lines, 1), ["|"], []);
  294. if (llGetListLength(coursenames) > 1) sloodlecoursename_full = llList2String(coursenames, 1);
  295. else sloodlecoursename_full = "???";
  296. state ready;
  297. }
  298. on_rez(integer param)
  299. {
  300. state default;
  301. }
  302. }
  303. // The Set has been configured
  304. state ready
  305. {
  306. state_entry()
  307. {
  308. // Display the site and course info in the hover text
  309. // llSetTexture("sloodle_ready", TEX_SIDE);
  310. llMessageLinked(LINK_SET, SLOODLE_CHANNEL_SET_CONFIGURED, "", NULL_KEY);
  311. sloodle_translation_request(SLOODLE_TRANSLATE_HOVER_TEXT, [<0.0,1.0,0.0>, 1.0], "readyconnectedto:sitecourse", [sloodleserverroot, sloodlecoursename_full], NULL_KEY, "");
  312. // Run a regular timer to cancel expired dialogs
  313. llSetTimerEvent(12.0);
  314. // Listen for dialog input
  315. llListen(SLOODLE_CHANNEL_AVATAR_DIALOG, "", NULL_KEY, "");
  316. }
  317. touch_start(integer num_detected)
  318. {
  319. // Go through each toucher
  320. integer i = 0;
  321. key id = NULL_KEY;
  322. for (; i < num_detected; i++) {
  323. id = llDetectedKey(i);
  324. // Check the use access level here
  325. if (sloodle_check_access_use(id)) {
  326. // Show the command dialog to the user
  327. sloodle_show_command_dialog(id);
  328. sloodle_add_cmd_dialog(id);
  329. } else {
  330. // Inform the user that they do not have permission
  331. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "nopermission:use", [llKey2Name(id)], NULL_KEY, "");
  332. }
  333. }
  334. }
  335. listen(integer channel, string name, key id, string msg)
  336. {
  337. // Check the channel
  338. if (channel == SLOODLE_CHANNEL_AVATAR_DIALOG) {
  339. // Make sure we are actually listening to this user
  340. if (llListFindList(cmddialog, [id]) < 0) return;
  341. // Check the use access level here
  342. if (!sloodle_check_access_use(id)) return;
  343. // The user no longer needs the dialog
  344. sloodle_remove_cmd_dialog(id);
  345. // Check the command
  346. if (msg == MENU_BUTTON_RESET) {
  347. // Reset the object
  348. sloodle_reset();
  349. return;
  350. } else if (msg == MENU_BUTTON_OPEN_REZ_DIALOG) {
  351. llMessageLinked(LINK_SET, SLOODLE_CHANNEL_SET_MENU_BUTTON_OPEN_REZ_DIALOG , "", id);
  352. return;
  353. } else if (msg == MENU_BUTTON_OPEN_LAYOUT_DIALOG) {
  354. llMessageLinked(LINK_SET, SLOODLE_CHANNEL_SET_MENU_BUTTON_OPEN_LAYOUT_DIALOG , "", id);
  355. return;
  356. } else if (msg == MENU_BUTTON_KILL_ALL) {
  357. llMessageLinked(LINK_SET, SLOODLE_CHANNEL_SET_MENU_BUTTON_KILL_ALL , "", id);
  358. return;
  359. }
  360. }
  361. }
  362. timer()
  363. {
  364. sloodle_purge_cmd_dialog();
  365. }
  366. on_rez(integer param)
  367. {
  368. state default;
  369. }
  370. }
  371. // Please leave the following line intact to show where the script lives in Git:
  372. // SLOODLE LSL Script Git Location: mod/set-1.0/objects/default/assets/sloodle_mod_set-1.0.lslp