sloodle_layout_object.lslp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. //
  2. // The line above should be left blank to avoid script errors in OpenSim.
  3. // Sloodle object layout script.
  4. // Allows individual objects to store themselves in a Sloodle layout.
  5. //
  6. // Part of the Sloodle project (www.sloodle.org)
  7. // Copyright (c) 2008 Sloodle
  8. // Released under the GNU GPL v3
  9. //
  10. // Contributors:
  11. // Peter R. Bloomfield
  12. // Edmund Edgar
  13. // Paul Preibisch - Fire Centaur in SL
  14. //
  15. ///// DATA /////
  16. integer SLOODLE_CHANNEL_ERROR_TRANSLATION_REQUEST=-1828374651; // this channel is used to send status codes for translation to the error_messages lsl script
  17. integer SLOODLE_CHANNEL_OBJECT_DIALOG = -3857343;
  18. integer SLOODLE_CHANNEL_AVATAR_DIALOG = 1001;
  19. string SLOODLE_LAYOUT_LINKER = "/mod/sloodle/mod/set-1.0/layout_linker.php";
  20. string SLOODLE_EOF = "sloodleeof";
  21. string sloodleserverroot = "";
  22. string sloodlepwd = "";
  23. integer sloodlecontrollerid = 0;
  24. key sloodlemyrezzer = NULL_KEY;
  25. integer isconfigured = FALSE; // Do we have all the configuration data we need?
  26. integer eof = FALSE; // Have we reached the end of the configuration data?
  27. key httpstore = NULL_KEY; // Request to store this object's location
  28. float HTTP_TIMEOUT = 8.0; // Time to wait for an HTTP response before failing
  29. integer attemptnum = 0; // Which attempt number is this?
  30. integer attemptmax = 2; // How many attempts should be allowed [0,max)
  31. float DELAY_MIN = 0.0; // Minimum delay before sending an HTTP request (seconds)
  32. float DELAY_RANGE = 3.5; // Added to DELAY_MIN gives the maximum delay time before sending HTTP request (seconds)
  33. key useruuid = NULL_KEY; // User agent requesting profile storage
  34. string layoutname = ""; // Name of the layout to save to
  35. vector layoutpos = <0.0,0.0,0.0>; // Relative position from the rezzer to this object
  36. ///// TRANSLATION /////
  37. // Link message channels
  38. integer SLOODLE_CHANNEL_TRANSLATION_REQUEST = -1928374651;
  39. integer SLOODLE_CHANNEL_TRANSLATION_RESPONSE = -1928374652;
  40. // Translation output methods
  41. string SLOODLE_TRANSLATE_LINK = "link"; // No output parameters - simply returns the translation on SLOODLE_TRANSLATION_RESPONSE link message channel
  42. string SLOODLE_TRANSLATE_SAY = "say"; // 1 output parameter: chat channel number
  43. string SLOODLE_TRANSLATE_WHISPER = "whisper"; // 1 output parameter: chat channel number
  44. string SLOODLE_TRANSLATE_SHOUT = "shout"; // 1 output parameter: chat channel number
  45. string SLOODLE_TRANSLATE_REGION_SAY = "regionsay"; // 1 output parameter: chat channel number
  46. string SLOODLE_TRANSLATE_OWNER_SAY = "ownersay"; // No output parameters
  47. 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.
  48. string SLOODLE_TRANSLATE_LOAD_URL = "loadurl"; // Recipient avatar should be identified in link message keyval. 1 output parameter giving URL to load.
  49. string SLOODLE_TRANSLATE_HOVER_TEXT = "hovertext"; // 2 output parameters: colour <r,g,b>, and alpha value
  50. string SLOODLE_TRANSLATE_IM = "instantmessage"; // Recipient avatar should be identified in link message keyval. No output parameters.
  51. // Send a translation request link message
  52. sloodle_translation_request(string output_method, list output_params, string string_name, list string_params, key keyval, string batch)
  53. {
  54. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_TRANSLATION_REQUEST, output_method + "|" + llList2CSV(output_params) + "|" + string_name + "|" + llList2CSV(string_params) + "|" + batch, keyval);
  55. }
  56. ///// ----------- /////
  57. ///// FUNCTIONS /////
  58. /******************************************************************************************************************************
  59. * sloodle_error_code -
  60. * Author: Paul Preibisch
  61. * Description - This function sends a linked message on the SLOODLE_CHANNEL_ERROR_TRANSLATION_REQUEST channel
  62. * The error_messages script hears this, translates the status code and sends an instant message to the avuuid
  63. * Params: method - SLOODLE_TRANSLATE_SAY, SLOODLE_TRANSLATE_IM etc
  64. * Params: avuuid - this is the avatar UUID to that an instant message with the translated error code will be sent to
  65. * Params: status code - the status code of the error as on our wiki: http://slisweb.sjsu.edu/sl/index.php/Sloodle_status_codes
  66. *******************************************************************************************************************************/
  67. sloodle_error_code(string method, key avuuid,integer statuscode){
  68. llMessageLinked(LINK_SET, SLOODLE_CHANNEL_ERROR_TRANSLATION_REQUEST, method+"|"+(string)avuuid+"|"+(string)statuscode, NULL_KEY);
  69. }
  70. // Send debug info
  71. sloodle_debug(string msg)
  72. {
  73. llMessageLinked(LINK_THIS, DEBUG_CHANNEL, msg, NULL_KEY);
  74. }
  75. // Configure by receiving a linked message from another script in the object
  76. // Returns TRUE if the object has all the data it needs
  77. integer sloodle_handle_command(string str)
  78. {
  79. list bits = llParseString2List(str,["|"],[]);
  80. integer numbits = llGetListLength(bits);
  81. string name = llList2String(bits,0);
  82. string value1 = "";
  83. string value2 = "";
  84. if (numbits > 1) value1 = llList2String(bits,1);
  85. if (numbits > 2) value2 = llList2String(bits,2);
  86. if (name == "set:sloodleserverroot") sloodleserverroot = value1;
  87. else if (name == "set:sloodlepwd") {
  88. // The password may be a single prim password, or a UUID and a password
  89. if (value2 != "") sloodlepwd = value1 + "|" + value2;
  90. else sloodlepwd = value1;
  91. } else if (name == "set:sloodlecontrollerid") sloodlecontrollerid = (integer)value1;
  92. else if (name == "set:sloodlemyrezzer") sloodlemyrezzer = (key)value1;
  93. else if (name == SLOODLE_EOF) eof = TRUE;
  94. else if (name == "do:reset") llResetScript();
  95. return (sloodleserverroot != "" && sloodlepwd != "" && sloodlecontrollerid > 0);
  96. }
  97. // Generate a random delay time
  98. float random_delay()
  99. {
  100. return (DELAY_MIN + llFrand(DELAY_RANGE));
  101. }
  102. ///// STATES /////
  103. default
  104. {
  105. state_entry()
  106. {
  107. // Starting again with a new configuration
  108. isconfigured = FALSE;
  109. eof = FALSE;
  110. sloodleserverroot = "";
  111. sloodlepwd = "";
  112. sloodlecontrollerid = 0;
  113. }
  114. link_message(integer sender_num, integer num, string str, key id)
  115. {
  116. // Check the channel
  117. if (num == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  118. // Split the message into lines
  119. list lines = llParseString2List(str, ["\n"], []);
  120. integer numlines = llGetListLength(lines);
  121. integer i;
  122. for (i=0; i < numlines; i++) {
  123. isconfigured = sloodle_handle_command(llList2String(lines, i));
  124. }
  125. // If we've got all our data AND reached the end of the configuration data, then move on
  126. if (eof == TRUE && isconfigured == TRUE) {
  127. state ready;
  128. }
  129. }
  130. }
  131. }
  132. state ready
  133. {
  134. state_entry()
  135. {
  136. // Reset our data
  137. useruuid = NULL_KEY;
  138. layoutname = "";
  139. // Listen for chat messages on the object dialog channel
  140. llListen(SLOODLE_CHANNEL_OBJECT_DIALOG, "", sloodlemyrezzer, "");
  141. }
  142. listen(integer channel, string name, key id, string msg)
  143. {
  144. // Ignore anything if we don't know who our rezzer is
  145. if (sloodlemyrezzer == NULL_KEY) return;
  146. // Ignore anything but object chat
  147. if (channel != SLOODLE_CHANNEL_OBJECT_DIALOG) return;
  148. // Ignore anything owned by a different agent
  149. if (llGetOwnerKey(id) != llGetOwner()) return;
  150. // Parse the message
  151. // We are expecting: cmd|rezzer|uuid|pos|layoutname
  152. // "cmd" should be "do:storelayout"
  153. // "rezzer" is the UUID of the Set whose items should be stored
  154. // "uuid" is the UUID of the user agent storing the layout
  155. // "pos" is the vector giving the position of the root of the rezzer
  156. // "layoutname" should be the name of the layout to save to
  157. list fields = llParseStringKeepNulls(msg, ["|"], []);
  158. integer numfields = llGetListLength(fields);
  159. if (numfields < 4) return;
  160. // Get the command and UUID
  161. string cmd = llList2String(fields, 0);
  162. key rezzer = (key)llList2String(fields, 1);
  163. useruuid = (key)llList2String(fields, 2);
  164. vector rezzerpos = (vector)llList2String(fields, 3);
  165. layoutname = llList2String(fields, 4);
  166. // Check that everything looks OK
  167. if (cmd != "do:storelayout") return;
  168. if (rezzer != sloodlemyrezzer || sloodlemyrezzer == NULL_KEY) return;
  169. if (useruuid == NULL_KEY) return;
  170. if (layoutname == "") return;
  171. // Calculate the relative position of us compared to the rezzer
  172. layoutpos = llGetPos() - rezzerpos;
  173. // If the distance is more than can be rezzed later on, then ignore it
  174. if (llVecMag(layoutpos) > 10.0) {
  175. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "layout:toofar", [], NULL_KEY, "");
  176. return;
  177. }
  178. // Attempt to store the layout
  179. attemptnum = 0;
  180. state store_layout;
  181. }
  182. link_message(integer sender_num, integer num, string sval, key kval)
  183. {
  184. // Check the channel
  185. if (num == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  186. // Is it a reset command?
  187. if (sval == "do:reset") {
  188. llResetScript();
  189. return;
  190. }
  191. }
  192. }
  193. }
  194. // This is a dummy state... it can be used to start the store process again
  195. state store_layout
  196. {
  197. state_entry()
  198. {
  199. // Move on immediately...
  200. state delay;
  201. }
  202. link_message(integer sender_num, integer num, string sval, key kval)
  203. {
  204. // Check the channel
  205. if (num == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  206. // Is it a reset command?
  207. if (sval == "do:reset") {
  208. llResetScript();
  209. return;
  210. }
  211. }
  212. }
  213. }
  214. // Wait for a delay before actually sending the request
  215. state delay
  216. {
  217. state_entry()
  218. {
  219. // Generate a random delay
  220. llSetTimerEvent(random_delay());
  221. }
  222. state_exit()
  223. {
  224. llSetTimerEvent(0.0);
  225. }
  226. timer()
  227. {
  228. llSetTimerEvent(0.0);
  229. // Send the storage request
  230. attemptnum += 1;
  231. state request;
  232. }
  233. link_message(integer sender_num, integer num, string sval, key kval)
  234. {
  235. // Check the channel
  236. if (num == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  237. // Is it a reset command?
  238. if (sval == "do:reset") {
  239. llResetScript();
  240. return;
  241. }
  242. }
  243. }
  244. }
  245. // Send a request to store the object data
  246. state request
  247. {
  248. state_entry()
  249. {
  250. // Construct our request
  251. string body = "sloodlecontrollerid=" + (string)sloodlecontrollerid;
  252. body += "&sloodlepwd=" + sloodlepwd;
  253. body += "&sloodlelayoutname=" + layoutname;
  254. body += "&sloodleuuid=" + (string)useruuid;
  255. body += "&sloodlelayoutentries=" + llGetObjectName() + "|" + (string)layoutpos + "|" + (string)llGetRot();
  256. body += "&sloodleadd=true";
  257. httpstore = llHTTPRequest(sloodleserverroot + SLOODLE_LAYOUT_LINKER, [HTTP_METHOD, "POST", HTTP_MIMETYPE, "application/x-www-form-urlencoded"], body);
  258. }
  259. state_exit()
  260. {
  261. llSetTimerEvent(0.0);
  262. }
  263. timer()
  264. {
  265. llSetTimerEvent(0.0);
  266. httpstore = NULL_KEY;
  267. state failed;
  268. }
  269. http_response(key id, integer status, list meta, string body)
  270. {
  271. // Ignore this response if it is not expected
  272. if (id != httpstore) return;
  273. llSetTimerEvent(0.0);
  274. httpstore = NULL_KEY;
  275. // Check the HTTP status
  276. if (status != 200) {
  277. sloodle_error_code(SLOODLE_TRANSLATE_SAY, NULL_KEY,status); //send message to error_message.lsl
  278. state failed;
  279. return;
  280. }
  281. // Check the body of the response
  282. if (body == "") {
  283. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "httpempty", [], NULL_KEY, "");
  284. state failed;
  285. return;
  286. }
  287. // Parse the response
  288. list lines = llParseStringKeepNulls(body, ["\n"], []);
  289. integer numlines = llGetListLength(lines);
  290. list statusfields = llParseStringKeepNulls(llList2String(lines, 0), ["|"], []);
  291. integer statuscode = (integer)llList2String(statusfields, 0);
  292. // Did an error occur?
  293. if (statuscode <= 0) {
  294. //sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "servererror", [statuscode], NULL_KEY, "");
  295. sloodle_error_code(SLOODLE_TRANSLATE_SAY, NULL_KEY,statuscode); //send message to error_message.lsl
  296. sloodle_debug("HTTP response: " + body);
  297. state failed;
  298. return;
  299. }
  300. // Everything must be OK... send the data to the object. Format:
  301. state success;
  302. }
  303. link_message(integer sender_num, integer num, string sval, key kval)
  304. {
  305. // Check the channel
  306. if (num == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  307. // Is it a reset command?
  308. if (sval == "do:reset") {
  309. llResetScript();
  310. return;
  311. }
  312. }
  313. }
  314. }
  315. // Update failed... try again?
  316. state failed
  317. {
  318. state_entry()
  319. {
  320. // Do we have any attempts left?
  321. if (attemptnum < attemptmax) {
  322. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "layout:failedretrying", [], NULL_KEY, "");
  323. state store_layout;
  324. } else {
  325. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "layout:failedaborting", [], NULL_KEY, "");
  326. state ready;
  327. }
  328. }
  329. link_message(integer sender_num, integer num, string sval, key kval)
  330. {
  331. // Check the channel
  332. if (num == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  333. // Is it a reset command?
  334. if (sval == "do:reset") {
  335. llResetScript();
  336. return;
  337. }
  338. }
  339. }
  340. }
  341. // Update successful
  342. state success
  343. {
  344. state_entry()
  345. {
  346. // Done!
  347. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "layout:stored", [], NULL_KEY, "");
  348. state ready;
  349. }
  350. link_message(integer sender_num, integer num, string sval, key kval)
  351. {
  352. // Check the channel
  353. if (num == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  354. // Is it a reset command?
  355. if (sval == "do:reset") {
  356. llResetScript();
  357. return;
  358. }
  359. }
  360. }
  361. }
  362. // Please leave the following line intact to show where the script lives in Git:
  363. // SLOODLE LSL Script Git Location: assets/sloodle_layout_object.lslp