4d9d75f2-767e-9d59-15b0-ec9b9330328f_script.lsl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. //
  2. // The line above should be left blank to avoid script errors in OpenSim.
  3. // Sloodle Password Reset object
  4. // Allows avatar with auto-registered Moodle accounts to reset their Moodle password from in-world.
  5. // Part of the Sloodle project (www.sloodle.org)
  6. //
  7. // Copyright (c) 2008 Sloodle
  8. // Released under the GNU GPL v3
  9. //
  10. // Contributors:
  11. // Peter R. Bloomfield
  12. //
  13. integer SLOODLE_CHANNEL_OBJECT_DIALOG = -3857343;
  14. integer SLOODLE_CHANNEL_AVATAR_DIALOG = 1001;
  15. string SLOODLE_PWRESET_LINKER = "/mod/sloodle/mod/pwreset-1.0/linker.php";
  16. string SLOODLE_EOF = "sloodleeof";
  17. string SLOODLE_OBJECT_TYPE = "pwreset-1.0";
  18. integer SLOODLE_OBJECT_ACCESS_LEVEL_PUBLIC = 0;
  19. integer SLOODLE_OBJECT_ACCESS_LEVEL_OWNER = 1;
  20. integer SLOODLE_OBJECT_ACCESS_LEVEL_GROUP = 2;
  21. string sloodleserverroot = "";
  22. string sloodlecoursename_full = "";
  23. string sloodlepwd = "";
  24. integer sloodlecontrollerid = 0;
  25. integer sloodleobjectaccessleveluse = 0; // Who can use this object?
  26. integer sloodleobjectaccesslevelctrl = 0; // Who can control this object?
  27. integer sloodleserveraccesslevel = 0; // Who can use the server resource? (Value passed straight back to Moodle)
  28. integer isconfigured = FALSE; // Do we have all the configuration data we need?
  29. integer eof = FALSE; // Have we reached the end of the configuration data?
  30. list avatars = []; // A list of avatars with pending requests
  31. list httprequests = []; // A list of http request keys, corresponding to the "avatars" list
  32. ///// TRANSLATION /////
  33. // Link message channels
  34. integer SLOODLE_CHANNEL_ERROR_TRANSLATION_REQUEST=-1828374651; // this channel is used to send status codes for translation to the error_messages lsl script
  35. integer SLOODLE_CHANNEL_TRANSLATION_REQUEST = -1928374651;
  36. integer SLOODLE_CHANNEL_TRANSLATION_RESPONSE = -1928374652;
  37. // Translation output methods
  38. string SLOODLE_TRANSLATE_LINK = "link"; // No output parameters - simply returns the translation on SLOODLE_TRANSLATION_RESPONSE link message channel
  39. string SLOODLE_TRANSLATE_SAY = "say"; // 1 output parameter: chat channel number
  40. string SLOODLE_TRANSLATE_WHISPER = "whisper"; // 1 output parameter: chat channel number
  41. string SLOODLE_TRANSLATE_SHOUT = "shout"; // 1 output parameter: chat channel number
  42. string SLOODLE_TRANSLATE_REGION_SAY = "regionsay"; // 1 output parameter: chat channel number
  43. string SLOODLE_TRANSLATE_OWNER_SAY = "ownersay"; // No output parameters
  44. 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.
  45. string SLOODLE_TRANSLATE_LOAD_URL = "loadurl"; // Recipient avatar should be identified in link message keyval. 1 output parameter giving URL to load.
  46. string SLOODLE_TRANSLATE_HOVER_TEXT = "hovertext"; // 2 output parameters: colour <r,g,b>, and alpha value
  47. string SLOODLE_TRANSLATE_IM = "instantmessage"; // Recipient avatar should be identified in link message keyval. No output parameters.
  48. // Send a translation request link message
  49. sloodle_translation_request(string output_method, list output_params, string string_name, list string_params, key keyval, string batch)
  50. {
  51. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_TRANSLATION_REQUEST, output_method + "|" + llList2CSV(output_params) + "|" + string_name + "|" + llList2CSV(string_params) + "|" + batch, keyval);
  52. }
  53. ///// ----------- /////
  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. sloodle_debug(string msg)
  68. {
  69. llMessageLinked(LINK_THIS, DEBUG_CHANNEL, msg, NULL_KEY);
  70. }
  71. // Configure by receiving a linked message from another script in the object
  72. // Returns TRUE if the object has all the data it needs
  73. integer sloodle_handle_command(string str)
  74. {
  75. list bits = llParseString2List(str,["|"],[]);
  76. integer numbits = llGetListLength(bits);
  77. string name = llList2String(bits,0);
  78. string value1 = "";
  79. string value2 = "";
  80. if (numbits > 1) value1 = llList2String(bits,1);
  81. if (numbits > 2) value2 = llList2String(bits,2);
  82. if (name == "set:sloodleserverroot") sloodleserverroot = value1;
  83. if (name == "set:sloodlecoursename_full") sloodlecoursename_full = value1;
  84. else if (name == "set:sloodlepwd") {
  85. // The password may be a single prim password, or a UUID and a password
  86. if (value2 != "") sloodlepwd = value1 + "|" + value2;
  87. else sloodlepwd = value1;
  88. } else if (name == "set:sloodlecontrollerid") sloodlecontrollerid = (integer)value1;
  89. else if (name == "set:sloodleobjectaccessleveluse") sloodleobjectaccessleveluse = (integer)value1;
  90. else if (name == "set:sloodleobjectaccesslevelctrl") sloodleobjectaccesslevelctrl = (integer)value1;
  91. else if (name == "set:sloodleserveraccesslevel") sloodleserveraccesslevel = (integer)value1;
  92. else if (name == SLOODLE_EOF) eof = TRUE;
  93. return (sloodleserverroot != "" && sloodlepwd != "" && sloodlecontrollerid > 0);
  94. }
  95. // Checks if the given agent is permitted to control this object
  96. // Returns TRUE if so, or FALSE if not
  97. integer sloodle_check_access_ctrl(key id)
  98. {
  99. // Check the access mode
  100. if (sloodleobjectaccesslevelctrl == SLOODLE_OBJECT_ACCESS_LEVEL_GROUP) {
  101. return llSameGroup(id);
  102. } else if (sloodleobjectaccesslevelctrl == SLOODLE_OBJECT_ACCESS_LEVEL_PUBLIC) {
  103. return TRUE;
  104. }
  105. // Assume it's owner mode
  106. return (id == llGetOwner());
  107. }
  108. // Checks if the given agent is permitted to user this object
  109. // Returns TRUE if so, or FALSE if not
  110. integer sloodle_check_access_use(key id)
  111. {
  112. // Check the access mode
  113. if (sloodleobjectaccessleveluse == SLOODLE_OBJECT_ACCESS_LEVEL_GROUP) {
  114. return llSameGroup(id);
  115. } else if (sloodleobjectaccessleveluse == SLOODLE_OBJECT_ACCESS_LEVEL_PUBLIC) {
  116. return TRUE;
  117. }
  118. // Assume it's owner mode
  119. return (id == llGetOwner());
  120. }
  121. // Initiate a password reset
  122. sloodle_password_reset(key av)
  123. {
  124. // Start the request
  125. string body = "sloodlecontrollerid=" + (string)sloodlecontrollerid;
  126. body += "&sloodlepwd=" + sloodlepwd;
  127. body += "&sloodleuuid=" + (string)av;
  128. body += "&sloodleavname=" + llEscapeURL(llKey2Name(av));
  129. body += "&sloodleserveraccesslevel=" + (string)sloodleserveraccesslevel;
  130. key newhttp = llHTTPRequest(sloodleserverroot + SLOODLE_PWRESET_LINKER, [HTTP_METHOD, "POST", HTTP_MIMETYPE, "application/x-www-form-urlencoded"], body);
  131. // Is the avatar already in the list?
  132. integer pos = llListFindList(avatars, [av]);
  133. if (pos >= 0) {
  134. // Yes - replace the entry
  135. httprequests = llListReplaceList(httprequests, [newhttp], pos, pos);
  136. } else {
  137. // No - add a new entry
  138. avatars += [av];
  139. httprequests += [newhttp];
  140. }
  141. }
  142. // Remove an avatar from the lists
  143. sloodle_remove_password_reset(key av)
  144. {
  145. // Check for the entry in the list
  146. integer pos = llListFindList(avatars, [av]);
  147. if (pos >= 0) {
  148. avatars = llDeleteSubList(avatars, pos, pos);
  149. httprequests = llDeleteSubList(httprequests, pos, pos);
  150. }
  151. }
  152. // Default state - waiting for configuration
  153. default
  154. {
  155. state_entry()
  156. {
  157. // Starting again with a new configuration
  158. llSetText("", <0.0,0.0,0.0>, 0.0);
  159. isconfigured = FALSE;
  160. eof = FALSE;
  161. // Reset our data
  162. sloodleserverroot = "";
  163. sloodlepwd = "";
  164. sloodlecontrollerid = 0;
  165. sloodleobjectaccessleveluse = 0;
  166. sloodleobjectaccesslevelctrl = 0;
  167. sloodleserveraccesslevel = 0;
  168. }
  169. link_message( integer sender_num, integer num, string str, key id)
  170. {
  171. // Check the channel
  172. if (num == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  173. // Split the message into lines
  174. list lines = llParseString2List(str, ["\n"], []);
  175. integer numlines = llGetListLength(lines);
  176. integer i = 0;
  177. for (; i < numlines; i++) {
  178. isconfigured = sloodle_handle_command(llList2String(lines, i));
  179. }
  180. // If we've got all our data AND reached the end of the configuration data, then move on
  181. if (eof == TRUE) {
  182. if (isconfigured == TRUE) {
  183. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "configurationreceived", [], NULL_KEY, "");
  184. state ready;
  185. } else {
  186. // Got all configuration but, it's not complete... request reconfiguration
  187. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "configdatamissing", [], NULL_KEY, "");
  188. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_OBJECT_DIALOG, "do:reconfigure", NULL_KEY);
  189. eof = FALSE;
  190. }
  191. }
  192. }
  193. }
  194. touch_start(integer num_detected)
  195. {
  196. // Attempt to request a reconfiguration
  197. if (llDetectedKey(0) == llGetOwner()) {
  198. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_OBJECT_DIALOG, "do:requestconfig", NULL_KEY);
  199. }
  200. }
  201. }
  202. // Ready to receive requests for password resets
  203. state ready
  204. {
  205. on_rez( integer param)
  206. {
  207. state default;
  208. }
  209. state_entry()
  210. {
  211. // Display status
  212. if (sloodleserveraccesslevel == 2) {
  213. sloodle_translation_request(SLOODLE_TRANSLATE_HOVER_TEXT, [<0.0,1.0,0.0>, 0.9], "pwresetready:course", [sloodleserverroot, sloodlecoursename_full], NULL_KEY, "pwreset");
  214. } else if (sloodleserveraccesslevel == 3) {
  215. sloodle_translation_request(SLOODLE_TRANSLATE_HOVER_TEXT, [<0.0,1.0,0.0>, 0.9], "pwresetready:staff", [sloodleserverroot, sloodlecoursename_full], NULL_KEY, "pwreset");
  216. } else {
  217. sloodle_translation_request(SLOODLE_TRANSLATE_HOVER_TEXT, [<0.0,1.0,0.0>, 0.9], "pwresetready:site", [sloodleserverroot], NULL_KEY, "pwreset");
  218. }
  219. }
  220. state_exit()
  221. {
  222. // Clear status tet
  223. llSetText("", <0.0,0.0,0.0>, 0.0);
  224. }
  225. touch_start( integer total_number)
  226. {
  227. // Activating this requires access permission
  228. if (sloodle_check_access_use(llDetectedKey(0)) == FALSE) {
  229. llWhisper(0, "Sorry " + llDetectedName(0) + ". You do not have permission to control this object.");
  230. return;
  231. }
  232. // Do the password reset
  233. sloodle_password_reset(llDetectedKey(0));
  234. }
  235. http_response(key id, integer status, list meta, string body)
  236. {
  237. // Ignore any unexpected responses
  238. integer pos = llListFindList(httprequests, [id]);
  239. if (pos < 0) return;
  240. // Extract the data
  241. key av = llList2Key(avatars, pos);
  242. string name = llKey2Name(av);
  243. sloodle_remove_password_reset(av);
  244. // Check the response status
  245. if (status != 200) {
  246. sloodle_error_code(SLOODLE_TRANSLATE_SAY, NULL_KEY,status); //send message to error_message.lsl
  247. return;
  248. }
  249. // Check the response body
  250. if (body == "") {
  251. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "httpempty", [], NULL_KEY, "");
  252. return;
  253. }
  254. // Parse the response
  255. list lines = llParseStringKeepNulls(body, ["\n"], []);
  256. integer numlines = llGetListLength(lines);
  257. list statusfields = llParseStringKeepNulls(llList2String(lines, 0), ["|"], []);
  258. integer statuscode = (integer)llList2String(statusfields, 0);
  259. // Check the status code
  260. if (statuscode == -341) {
  261. // User cannot have their password reset in-world
  262. sloodle_translation_request(SLOODLE_TRANSLATE_IM, [], "pwreseterror:hasemail", [name, sloodleserverroot], av, "pwreset");
  263. sloodle_debug("ERROR reported in response: " + body);
  264. return;
  265. } else if (statuscode == -331) {
  266. // User cannot use this device
  267. sloodle_translation_request(SLOODLE_TRANSLATE_IM, [], "nopermission:use", [name], av, "");
  268. return;
  269. } else if (statuscode <= 0) {
  270. // Error occurred
  271. //sloodle_translation_request(SLOODLE_TRANSLATE_IM, [], "pwreseterror:failed:code", [name, statuscode], av, "pwreset");
  272. sloodle_error_code(SLOODLE_TRANSLATE_IM, av,statuscode); //send message to error_message.lsl
  273. sloodle_debug("ERROR reported in response: " + body);
  274. return;
  275. }
  276. // Check that there are enough lines
  277. if (numlines < 2) {
  278. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "badresponseformat", [], NULL_KEY, "");
  279. sloodle_debug("ERROR: not enough lines in response: " + body);
  280. return;
  281. }
  282. // Attempt to parse the data line
  283. list datafields = llParseStringKeepNulls(llList2String(lines, 1), ["|"], []);
  284. if (llGetListLength(datafields) < 2) {
  285. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "badresponseformat", [], NULL_KEY, "");
  286. sloodle_debug("ERROR: not enough data fields in response: " + body);
  287. return;
  288. }
  289. // Extract the data items
  290. string username = llList2String(datafields, 0);
  291. string password = llList2String(datafields, 1);
  292. // Inform the user of their new password
  293. sloodle_translation_request(SLOODLE_TRANSLATE_IM, [], "pwreset:success", [name, sloodleserverroot, username, password], av, "pwreset");
  294. }
  295. }
  296. // Please leave the following line intact to show where the script lives in Git:
  297. // SLOODLE LSL Script Git Location: mod/pwreset-1.0/objects/default/assets/sloodle_mod_pwreset-1.0.lslp