sloodle_mod_glossary-1.0.lslp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. //
  2. // The line above should be left blank to avoid script errors in OpenSim.
  3. // Sloodle Glossary (for Sloodle 0.3)
  4. // Allows users in-world to search a Moodle glossary.
  5. // Part of the Sloodle project (www.sloodle.org)
  6. //
  7. // Copyright (c) 2006-8 Sloodle
  8. // Released under the GNU GPL
  9. //
  10. // Contributors:
  11. // Jeremy Kemp
  12. // Peter R. Bloomfield
  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. integer SLOODLE_CHANNEL_AVATAR_DIALOG = 1001;
  17. string SLOODLE_GLOSSARY_LINKER = "/mod/sloodle/mod/glossary-1.0/linker.php";
  18. string SLOODLE_EOF = "sloodleeof";
  19. string SLOODLE_OBJECT_TYPE = "glossary-1.0";
  20. integer SLOODLE_OBJECT_ACCESS_LEVEL_PUBLIC = 0;
  21. integer SLOODLE_OBJECT_ACCESS_LEVEL_OWNER = 1;
  22. integer SLOODLE_OBJECT_ACCESS_LEVEL_GROUP = 2;
  23. string sloodleserverroot = "";
  24. string sloodlepwd = "";
  25. integer sloodlecontrollerid = 0;
  26. integer sloodlemoduleid = 0;
  27. integer sloodleobjectaccessleveluse = 0; // Who can use this object?
  28. integer sloodleserveraccesslevel = 0; // Who can use the server resource? (Value passed straight back to Moodle)
  29. integer sloodlepartialmatches = 1;
  30. integer sloodlesearchaliases = 0;
  31. integer sloodlesearchdefinitions = 0;
  32. integer sloodleidletimeout = 120; // How many seconds before automatic idle timeout? (0 means don't timeout)
  33. string sloodleglossaryname = ""; // Name of the glossary
  34. integer isconfigured = FALSE; // Do we have all the configuration data we need?
  35. integer eof = FALSE; // Have we reached the end of the configuration data?
  36. key httpcheck = NULL_KEY; // Request used to check the glossary
  37. key httpsearch = NULL_KEY; // Request used to search the glossary
  38. float HTTP_TIMEOUT = 10.0; // Period of time to wait for an HTTP response before giving up
  39. string SLOODLE_METAGLOSS_COMMAND = "/def "; // The command prefix for searching via chat message
  40. string searchterm = ""; // The term to be searched
  41. key searcheruuid = NULL_KEY; // Key of the avatar searching
  42. ///// TRANSLATION /////
  43. // Link message channels
  44. integer SLOODLE_CHANNEL_TRANSLATION_REQUEST = -1928374651;
  45. integer SLOODLE_CHANNEL_TRANSLATION_RESPONSE = -1928374652;
  46. // Translation output methods
  47. string SLOODLE_TRANSLATE_LINK = "link"; // No output parameters - simply returns the translation on SLOODLE_TRANSLATION_RESPONSE link message channel
  48. string SLOODLE_TRANSLATE_SAY = "say"; // 1 output parameter: chat channel number
  49. string SLOODLE_TRANSLATE_WHISPER = "whisper"; // 1 output parameter: chat channel number
  50. string SLOODLE_TRANSLATE_SHOUT = "shout"; // 1 output parameter: chat channel number
  51. string SLOODLE_TRANSLATE_REGION_SAY = "regionsay"; // 1 output parameter: chat channel number
  52. string SLOODLE_TRANSLATE_OWNER_SAY = "ownersay"; // No output parameters
  53. 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.
  54. string SLOODLE_TRANSLATE_LOAD_URL = "loadurl"; // Recipient avatar should be identified in link message keyval. 1 output parameter giving URL to load.
  55. string SLOODLE_TRANSLATE_HOVER_TEXT = "hovertext"; // 2 output parameters: colour <r,g,b>, and alpha value
  56. string SLOODLE_TRANSLATE_IM = "instantmessage"; // Recipient avatar should be identified in link message keyval. No output parameters.
  57. // Send a translation request link message
  58. sloodle_translation_request(string output_method, list output_params, string string_name, list string_params, key keyval, string batch)
  59. {
  60. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_TRANSLATION_REQUEST, output_method + "|" + llList2CSV(output_params) + "|" + string_name + "|" + llList2CSV(string_params) + "|" + batch, keyval);
  61. }
  62. ///// ----------- /////
  63. ///// FUNCTIONS /////
  64. /******************************************************************************************************************************
  65. * sloodle_error_code -
  66. * Author: Paul Preibisch
  67. * Description - This function sends a linked message on the SLOODLE_CHANNEL_ERROR_TRANSLATION_REQUEST channel
  68. * The error_messages script hears this, translates the status code and sends an instant message to the avuuid
  69. * Params: method - SLOODLE_TRANSLATE_SAY, SLOODLE_TRANSLATE_IM etc
  70. * Params: avuuid - this is the avatar UUID to that an instant message with the translated error code will be sent to
  71. * Params: status code - the status code of the error as on our wiki: http://slisweb.sjsu.edu/sl/index.php/Sloodle_status_codes
  72. *******************************************************************************************************************************/
  73. sloodle_error_code(string method, key avuuid,integer statuscode){
  74. llMessageLinked(LINK_SET, SLOODLE_CHANNEL_ERROR_TRANSLATION_REQUEST, method+"|"+(string)avuuid+"|"+(string)statuscode, NULL_KEY);
  75. }
  76. sloodle_debug(string msg)
  77. {
  78. llMessageLinked(LINK_THIS, DEBUG_CHANNEL, msg, NULL_KEY);
  79. }
  80. sloodle_reset()
  81. {
  82. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "resetting", [], NULL_KEY, "");
  83. llMessageLinked(LINK_SET, SLOODLE_CHANNEL_OBJECT_DIALOG, "do:reset", NULL_KEY);
  84. llResetScript();
  85. }
  86. // Configure by receiving a linked message from another script in the object
  87. // Returns TRUE if the object has all the data it needs
  88. integer sloodle_handle_command(string str)
  89. {
  90. list bits = llParseString2List(str,["|"],[]);
  91. integer numbits = llGetListLength(bits);
  92. string name = llList2String(bits,0);
  93. string value1 = "";
  94. string value2 = "";
  95. if (numbits > 1) value1 = llList2String(bits,1);
  96. if (numbits > 2) value2 = llList2String(bits,2);
  97. if (name == "set:sloodleserverroot") sloodleserverroot = value1;
  98. else if (name == "set:sloodlepwd") {
  99. // The password may be a single prim password, or a UUID and a password
  100. if (value2 != "") sloodlepwd = value1 + "|" + value2;
  101. else sloodlepwd = value1;
  102. } else if (name == "set:sloodlecontrollerid") sloodlecontrollerid = (integer)value1;
  103. else if (name == "set:sloodlemoduleid") sloodlemoduleid = (integer)value1;
  104. else if (name == "set:sloodleobjectaccessleveluse") sloodleobjectaccessleveluse = (integer)value1;
  105. else if (name == "set:sloodleserveraccesslevel") sloodleserveraccesslevel = (integer)value1;
  106. else if (name == "set:sloodlepartialmatches") sloodlepartialmatches = (integer)value1;
  107. else if (name == "set:sloodlesearchaliases") sloodlesearchaliases = (integer)value1;
  108. else if (name == "set:sloodlesearchdefinitions") sloodlesearchdefinitions = (integer)value1;
  109. else if (name == "set:sloodleidletimeout") sloodleidletimeout = (integer)value1;
  110. else if (name == SLOODLE_EOF) eof = TRUE;
  111. return (sloodleserverroot != "" && sloodlepwd != "" && sloodlecontrollerid > 0 && sloodlemoduleid > 0);
  112. }
  113. // Checks if the given agent is permitted to user this object
  114. // Returns TRUE if so, or FALSE if not
  115. integer sloodle_check_access_use(key id)
  116. {
  117. // Check the access mode
  118. if (sloodleobjectaccessleveluse == SLOODLE_OBJECT_ACCESS_LEVEL_GROUP) {
  119. return llSameGroup(id);
  120. } else if (sloodleobjectaccessleveluse == SLOODLE_OBJECT_ACCESS_LEVEL_PUBLIC) {
  121. return TRUE;
  122. }
  123. // Assume it's owner mode
  124. return (id == llGetOwner());
  125. }
  126. ///// STATES /////
  127. // Default state - waiting for configuration
  128. default
  129. {
  130. state_entry()
  131. {
  132. // Starting again with a new configuration
  133. isconfigured = FALSE;
  134. eof = FALSE;
  135. // Reset our data
  136. sloodleserverroot = "";
  137. sloodlepwd = "";
  138. sloodlecontrollerid = 0;
  139. sloodlemoduleid = 0;
  140. sloodleobjectaccessleveluse = 0;
  141. sloodleserveraccesslevel = 0;
  142. sloodlepartialmatches = 1;
  143. sloodlesearchaliases = 0;
  144. sloodlesearchdefinitions = 0;
  145. sloodleidletimeout = 120;
  146. sloodleglossaryname = "";
  147. }
  148. link_message( integer sender_num, integer num, string str, key id)
  149. {
  150. // Check the channel
  151. if (num == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  152. // Split the message into lines
  153. list lines = llParseString2List(str, ["\n"], []);
  154. integer numlines = llGetListLength(lines);
  155. integer i = 0;
  156. for (; i < numlines; i++) {
  157. isconfigured = sloodle_handle_command(llList2String(lines, i));
  158. }
  159. // If we've got all our data AND reached the end of the configuration data, then move on
  160. if (eof == TRUE) {
  161. if (isconfigured == TRUE) {
  162. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "configurationreceived", [], NULL_KEY, "");
  163. state check_glossary;
  164. } else {
  165. // Go all configuration but, it's not complete... request reconfiguration
  166. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "configdatamissing", [], NULL_KEY, "");
  167. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_OBJECT_DIALOG, "do:reconfigure", NULL_KEY);
  168. eof = FALSE;
  169. }
  170. }
  171. }
  172. }
  173. touch_start(integer num_detected)
  174. {
  175. // Attempt to request a reconfiguration
  176. if (llDetectedKey(0) == llGetOwner()) {
  177. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_OBJECT_DIALOG, "do:requestconfig", NULL_KEY);
  178. }
  179. }
  180. }
  181. // If necessary, check the name of the glossary
  182. state check_glossary
  183. {
  184. on_rez(integer par)
  185. {
  186. state default;
  187. }
  188. link_message( integer sender_num, integer num, string str, key id)
  189. {
  190. // Check the channel
  191. if (num == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  192. // Is it a reset message?
  193. if (str == "do:reset") llResetScript();
  194. }
  195. }
  196. state_entry()
  197. {
  198. // Lookup the glossary name
  199. sloodleglossaryname = "";
  200. sloodle_translation_request(SLOODLE_TRANSLATE_HOVER_TEXT, [<0.0,1.0,0.0>, 0.8], "metagloss:checking", [], NULL_KEY, "metagloss");
  201. httpcheck = llHTTPRequest(sloodleserverroot + SLOODLE_GLOSSARY_LINKER + "?sloodlecontrollerid=" + (string)sloodlecontrollerid + "&sloodlepwd=" + sloodlepwd + "&sloodlemoduleid=" + (string)sloodlemoduleid, [HTTP_METHOD, "GET"], "");
  202. llSetTimerEvent(0.0);
  203. llSetTimerEvent(HTTP_TIMEOUT);
  204. }
  205. state_exit()
  206. {
  207. llSetTimerEvent(0.0);
  208. }
  209. timer()
  210. {
  211. llSetTimerEvent(0.0);
  212. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "httptimeout", [], NULL_KEY, "");
  213. llSleep(0.1);
  214. sloodle_reset();
  215. }
  216. http_response(key id, integer status, list meta, string body)
  217. {
  218. // Make sure this is the response we're expecting
  219. if (id != httpcheck) return;
  220. if (status != 200) {
  221. sloodle_error_code(SLOODLE_TRANSLATE_SAY, NULL_KEY,status); //send message to error_message.lsl
  222. sloodle_reset();
  223. return;
  224. }
  225. // Split the response into lines
  226. list lines = llParseStringKeepNulls(body, ["\n"], []);
  227. integer numlines = llGetListLength(lines);
  228. list statusfields = llParseStringKeepNulls(llList2String(lines, 0), ["|"], []);
  229. integer statuscode = (integer)llList2String(statusfields, 0);
  230. // Check the statuscode
  231. if (statuscode <= 0) {
  232. //sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "servererror", [statuscode], NULL_KEY, "");
  233. sloodle_error_code(SLOODLE_TRANSLATE_SAY, NULL_KEY,statuscode); //send message to error_message.lsl
  234. sloodle_reset();
  235. return;
  236. }
  237. // Make sure we have enough data
  238. if (numlines < 2) {
  239. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "badresponseformat", [], NULL_KEY, "");
  240. sloodle_reset();
  241. return;
  242. }
  243. // Store the glossary name
  244. sloodleglossaryname = llList2String(lines, 1);
  245. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "metagloss:checkok", [sloodleglossaryname], NULL_KEY, "metagloss");
  246. state ready;
  247. }
  248. }
  249. // Ready for definition requests
  250. state ready
  251. {
  252. on_rez( integer param)
  253. {
  254. state default;
  255. }
  256. link_message( integer sender_num, integer num, string str, key id)
  257. {
  258. // Check the channel
  259. if (num == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  260. // Is it a reset message?
  261. if (str == "do:reset") llResetScript();
  262. }
  263. }
  264. state_entry()
  265. {
  266. // Update the hover text
  267. sloodle_translation_request(SLOODLE_TRANSLATE_HOVER_TEXT, [<1.0,0.0,0.0>, 0.9], "metagloss:ready", [sloodleglossaryname, SLOODLE_METAGLOSS_COMMAND], NULL_KEY, "metagloss");
  268. // Listen for chat messages
  269. llListen(0, "", NULL_KEY, "");
  270. // We may need to de-activate after a period of idle time
  271. llSetTimerEvent(0.0);
  272. if (sloodleidletimeout > 0) llSetTimerEvent((float)sloodleidletimeout);
  273. }
  274. state_exit()
  275. {
  276. llSetTimerEvent(0.0);
  277. }
  278. listen( integer channel, string name, key id, string message)
  279. {
  280. // Check the channel
  281. if (channel == 0) {
  282. // Check use of this object
  283. if (sloodle_check_access_use(id) == FALSE) return;
  284. // Is this a definition request?
  285. if (llSubStringIndex(message, SLOODLE_METAGLOSS_COMMAND) != 0) return;
  286. // Store the term to be searched and search it
  287. searchterm = llGetSubString(message, llStringLength(SLOODLE_METAGLOSS_COMMAND), -1);
  288. searcheruuid = id;
  289. state search;
  290. return;
  291. }
  292. }
  293. timer()
  294. {
  295. // Shutdown due to idle timeout
  296. state shutdown;
  297. }
  298. }
  299. state shutdown
  300. {
  301. on_rez(integer par)
  302. {
  303. state default;
  304. }
  305. link_message( integer sender_num, integer num, string str, key id)
  306. {
  307. // Check the channel
  308. if (num == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  309. // Is it a reset message?
  310. if (str == "do:reset") llResetScript();
  311. }
  312. }
  313. state_entry()
  314. {
  315. sloodle_translation_request(SLOODLE_TRANSLATE_HOVER_TEXT, [<0.5,0.1,0.1>, 0.6], "metagloss:idle", [sloodleglossaryname], NULL_KEY, "metagloss");
  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. // Does this user have permission to use this object?
  325. if (sloodle_check_access_use(id)) {
  326. state ready;
  327. } else {
  328. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "nopermission:use", [], NULL_KEY, "");
  329. }
  330. }
  331. }
  332. }
  333. state search
  334. {
  335. on_rez(integer par)
  336. {
  337. state default;
  338. }
  339. link_message( integer sender_num, integer num, string str, key id)
  340. {
  341. // Check the channel
  342. if (num == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  343. // Is it a reset message?
  344. if (str == "do:reset") llResetScript();
  345. }
  346. }
  347. state_entry()
  348. {
  349. // Search the specified term
  350. sloodle_translation_request(SLOODLE_TRANSLATE_HOVER_TEXT, [<1.0,0.5,0.0>, 0.9], "metagloss:searching", [sloodleglossaryname], NULL_KEY, "metagloss");
  351. string body = "sloodlecontrollerid=" + (string)sloodlecontrollerid;
  352. body += "&sloodlepwd=" + sloodlepwd;
  353. body += "&sloodlemoduleid=" + (string)sloodlemoduleid;
  354. body += "&sloodleuuid=" + (string)searcheruuid;
  355. body += "&sloodleavname=" + llEscapeURL(llKey2Name(searcheruuid));
  356. body += "&sloodleserveraccesslevel=" + (string)sloodleserveraccesslevel;
  357. body += "&sloodleterm=" + searchterm;
  358. body += "&sloodlepartialmatches=" + (string)sloodlepartialmatches;
  359. body += "&sloodlesearchaliases=" + (string)sloodlesearchaliases;
  360. body += "&sloodlesearchdefinitions=" + (string)sloodlesearchdefinitions;
  361. // Check if it's an object sending this message
  362. if (searcheruuid != llGetOwnerKey(searcheruuid)) {
  363. // This makes sure Moodle doesn't try to auto-register an object
  364. body += "&sloodleisobject=true";
  365. }
  366. // Send the request
  367. httpsearch = llHTTPRequest(sloodleserverroot + SLOODLE_GLOSSARY_LINKER, [HTTP_METHOD, "POST", HTTP_MIMETYPE, "application/x-www-form-urlencoded"], body);
  368. llSetTimerEvent(0.0);
  369. llSetTimerEvent(HTTP_TIMEOUT);
  370. }
  371. state_exit()
  372. {
  373. llSetTimerEvent(0.0);
  374. }
  375. timer()
  376. {
  377. llSetTimerEvent(0.0);
  378. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "httptimeout", [], NULL_KEY, "");
  379. state ready;
  380. }
  381. http_response(key id, integer status, list meta, string body)
  382. {
  383. // Make sure this is the response we're expecting
  384. if (id != httpsearch) return;
  385. if (status != 200) {
  386. sloodle_error_code(SLOODLE_TRANSLATE_SAY, NULL_KEY,status); //send message to error_message.lsl
  387. sloodle_reset();
  388. return;
  389. }
  390. // Split the response into lines
  391. list lines = llParseStringKeepNulls(body, ["\n"], []);
  392. integer numlines = llGetListLength(lines);
  393. list statusfields = llParseStringKeepNulls(llList2String(lines, 0), ["|"], []);
  394. integer statuscode = (integer)llList2String(statusfields, 0);
  395. // Check the statuscode
  396. if (statuscode <= 0) {
  397. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "servererror", [statuscode], NULL_KEY, "");
  398. sloodle_reset();
  399. return;
  400. }
  401. // Indicate how many definitions were found
  402. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "metagloss:numdefs", [searchterm, (numlines - 1)], NULL_KEY, "metagloss");
  403. // Go through each definition
  404. integer defnum = 1;
  405. list fields = [];
  406. for (; defnum < numlines; defnum++) {
  407. // Split this definition into fields
  408. fields = llParseStringKeepNulls(llList2String(lines, defnum), ["|"], []);
  409. if (llGetListLength(fields) >= 2) {
  410. llSay(0, llList2String(fields, 0) + " = " + llList2String(fields, 1));
  411. } else {
  412. llSay(0, llList2String(fields, 0));
  413. }
  414. }
  415. state ready;
  416. }
  417. }
  418. // Please leave the following line intact to show where the script lives in Git:
  419. // SLOODLE LSL Script Git Location: mod/glossary-1.0/objects/default/assets/sloodle_mod_glossary-1.0.lslp