912e6a96-4835-6b8a-6aaf-428c61a130ed_script.lsl 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. //
  2. // The line above should be left blank to avoid script errors in OpenSim.
  3. // Sloodle object distributor.
  4. // Allows Sloodle objects to be distributed in-world to Second Life users,
  5. // either by an in-world user touching it and using a menu,
  6. // or via http-in from Moodle.
  7. // Part of the Sloodle project (www.sloodle.org)
  8. //
  9. // Copyright (c) 2007-8 Sloodle
  10. // Released under the GNU GPL
  11. //
  12. // Contributors:
  13. // Edmund Edgar
  14. // Peter R. Bloomfield
  15. //
  16. // When configured, reports its inventory list to the Moodle server.
  17. // Note that non-copyable items are NOT made available, and neither will scripts or items whose name is on the ignore list below.
  18. // Can send avatars inventory based on them touching and choosing, or based on an http-in message.
  19. // ***** IGNORE LIST *****
  20. //
  21. // This is a list of names of items which should NOT be handed out
  22. list ignorelist = ["sloodle_config","sloodle_object_distributor","sloodle_setup_notecard","sloodle_slave_object","sloodle_debug","awards_sloodle_config"];
  23. //
  24. // ***** ----------- *****
  25. // Returns number of Strides in a List
  26. integer SLOODLE_OBJECT_ACCESS_LEVEL_PUBLIC = 0;
  27. integer SLOODLE_OBJECT_ACCESS_LEVEL_OWNER = 1;
  28. integer SLOODLE_OBJECT_ACCESS_LEVEL_GROUP = 2;
  29. integer SLOODLE_CHANNEL_OPEN_IN_BROWSER= -1639277000;
  30. integer SLOODLE_CHANNEL_OBJECT_DIALOG = -3857343;
  31. integer SLOODLE_CHANNEL_HTTP_RESPONSE = -1639260101; // Tells the sloodle_rezzer_object script to send the contents as an http-in response, to the key specified as key, which should be waiting for a response.
  32. integer SLOODLE_CHANNEL_AVATAR_SETTING = 1;
  33. integer SLOODLE_CHANNEL_AVATAR_DIALOG = 1001;
  34. integer SLOODLE_CHANNEL_AVATAR_IGNORE = -1639279999;
  35. integer SLOODLE_CHANNEL_DISTRIBUTOR_REQUEST_GIVE_OBJECT = 1639271151; // start the process to give the specified object to the specified avatar, if they are allowed it.
  36. integer SLOODLE_CHANNEL_DISTRIBUTOR_DO_GIVE_OBJECT = 1639271152; // actually do give the specified object to the specified avatar
  37. string SLOODLE_DISTRIB_LINKER = "/mod/sloodle/mod/distributor-2.0/linker.php";
  38. string SLOODLE_EOF = "sloodleeof";
  39. string SLOODLE_OBJECT_TYPE = "distributor-2.0";
  40. string sloodleserverroot = "";
  41. integer sloodlecontrollerid = 0;
  42. string sloodlepwd = "";
  43. integer sloodlemoduleid = 0;
  44. integer sloodleobjectaccessleveluse = 0; // Who can use this object?
  45. integer sloodleobjectaccesslevelctrl = 0; // Who can control this object?
  46. integer sloodlerefreshtime = 0; // Number of seconds between each automated server update (doesn't need to be high-volume... every hour should suffice)
  47. integer lastrefresh = 0; // Timestamp of the last refresh
  48. integer isconfigured = FALSE;
  49. integer eof = FALSE;
  50. integer isconnected = FALSE;
  51. key ch = NULL_KEY; // UUID of the XMLRPC channel opened by this object
  52. key httpupdate = NULL_KEY; // UUID of the HTTP request to update the server
  53. list inventory = []; // Will contain the names of all available inventory items
  54. string inventorystr = ""; // Will contain a URL-escaped, pipe-delimited list of object names
  55. list cmddialog = []; // Alternating list of keys, timestamps and page numbers, indicating who activated a dialog, when, and which page they are on (where applicable)
  56. // Menu button texts
  57. string MENU_BUTTON_RECONNECT = "A";
  58. string MENU_BUTTON_RESET = "B";
  59. string MENU_BUTTON_PREVIOUS = "<<";
  60. string MENU_BUTTON_NEXT = ">>";
  61. string MENU_BUTTON_CMD = "cmd";
  62. string MENU_BUTTON_WEB = "web";
  63. ///// TRANSLATION /////
  64. // These items are standard... do not change them!
  65. // Link message channels
  66. integer SLOODLE_CHANNEL_TRANSLATION_REQUEST = -1928374651;
  67. integer SLOODLE_CHANNEL_TRANSLATION_RESPONSE = -1928374652;
  68. // Translation output methods
  69. string SLOODLE_TRANSLATE_LINK = "link"; // No output parameters - simply returns the translation on SLOODLE_TRANSLATION_RESPONSE link message channel
  70. string SLOODLE_TRANSLATE_SAY = "say"; // 1 output parameter: chat channel number
  71. string SLOODLE_TRANSLATE_WHISPER = "whisper"; // 1 output parameter: chat channel number
  72. string SLOODLE_TRANSLATE_SHOUT = "shout"; // 1 output parameter: chat channel number
  73. string SLOODLE_TRANSLATE_REGION_SAY = "regionsay"; // 1 output parameter: chat channel number
  74. string SLOODLE_TRANSLATE_OWNER_SAY = "ownersay"; // No output parameters
  75. 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.
  76. string SLOODLE_TRANSLATE_LOAD_URL = "loadurl"; // Recipient avatar should be identified in link message keyval.
  77. string SLOODLE_TRANSLATE_HOVER_TEXT = "hovertext"; // 2 output parameters: colour <r,g,b>, and alpha value
  78. string SLOODLE_TRANSLATE_IM = "instantmessage"; // Recipient avatar should be identified in link message keyval. No output parameters.
  79. integer DIALOG_CHANNEL;
  80. // Send a translation request link message
  81. // Parameter: output_method = should identify an output method, as given by the "SLOODLE_TRANSLATE_..." constants above
  82. // Parameter: output_params = a list of parameters which controls the output, such as chat channel or buttons for a dialog
  83. // Parameter: string_name = the name of the localization string to output
  84. // Parameter: string_params = a list of parameters which will be included in the translated string (or an empty list if none)
  85. // Parameter: keyval = a key to send in the link message
  86. // Parameter: batch = the name of the localization batch which should handle this request
  87. sloodle_translation_request(string output_method, list output_params, string string_name, list string_params, key keyval, string batch)
  88. {
  89. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_TRANSLATION_REQUEST, output_method + "|" + llList2CSV(output_params) + "|" + string_name + "|" + llList2CSV(string_params) + "|" + batch, keyval);
  90. }
  91. ///// ----------- /////
  92. ///// FUNCTIONS /////
  93. sloodle_debug(string msg)
  94. {
  95. llMessageLinked(LINK_THIS, DEBUG_CHANNEL, msg, NULL_KEY);
  96. }
  97. sloodle_reset()
  98. {
  99. llMessageLinked(LINK_SET, SLOODLE_CHANNEL_OBJECT_DIALOG, "do:reset", NULL_KEY);
  100. llResetScript();
  101. }
  102. // Configure by receiving a linked message from another script in the object
  103. // Returns TRUE if the object has all the data it needs
  104. integer sloodle_handle_command(string str)
  105. {
  106. list bits = llParseStringKeepNulls(str,["|"],[]);
  107. integer numbits = llGetListLength(bits);
  108. string name = llList2String(bits,0);
  109. string value1 = "";
  110. string value2 = "";
  111. if (numbits > 1) value1 = llList2String(bits,1);
  112. if (numbits > 2) value2 = llList2String(bits,2);
  113. if (name == "set:sloodleserverroot") sloodleserverroot = value1;
  114. else if (name == "set:sloodlepwd") {
  115. // The password may be a single prim password, or a UUID and a password
  116. if (value2 != "") sloodlepwd = value1 + "|" + value2;
  117. else sloodlepwd = value1;
  118. } else if (name == "set:sloodlecontrollerid") sloodlecontrollerid = (integer)value1;
  119. else if (name == "set:sloodlemoduleid") sloodlemoduleid = (integer)value1;
  120. else if (name == "set:sloodlerefreshtime") sloodlerefreshtime = (integer)value1;
  121. else if (name == "set:sloodleobjectaccessleveluse") sloodleobjectaccessleveluse = (integer)value1;
  122. else if (name == "set:sloodleobjectaccesslevelctrl") sloodleobjectaccesslevelctrl = (integer)value1;
  123. else if (name == SLOODLE_EOF) eof = TRUE;
  124. return (sloodleserverroot != "" && sloodlepwd != "" && sloodlecontrollerid > 0);
  125. }
  126. integer sloodle_initial_inventory_avatar_check(key avatartogiveto, string objecttogive) {
  127. // Make sure we have the named object
  128. if (llGetInventoryType(objecttogive) == INVENTORY_NONE) {
  129. sloodle_debug("Object \"" + objecttogive + "\" not found.");
  130. // llOwnerSay("object fail");
  131. return 0;
  132. }
  133. // Make sure we can find the identified avatar
  134. if (avatartogiveto == NULL_KEY || llGetOwnerKey(avatartogiveto) != avatartogiveto) {
  135. // llOwnerSay("avatar fail");
  136. sloodle_debug("Could not find identified avatar.");
  137. return 0;
  138. }
  139. return 1;
  140. }
  141. integer sloodle_kick_off_inventory_giving_or_say_error(key avatartogiveto, string objecttogive) {
  142. // llOwnerSay("plan to give "+objecttogive+" to avatar "+(string)avatartogiveto);
  143. if (sloodle_initial_inventory_avatar_check(avatartogiveto, objecttogive) != 1) {
  144. return 0;
  145. }
  146. string body = "sloodlecontrollerid=" + (string)sloodlecontrollerid;
  147. body += "&sloodleobjuuid=" + (string)llGetKey();
  148. body += "&sloodleuuid=" + (string)avatartogiveto;
  149. body += "&sloodleavname=" + llEscapeURL(llKey2Name(avatartogiveto));
  150. body += "&sloodlepwd=" + sloodlepwd;
  151. body += "&sloodlemoduleid=" + (string)sloodlemoduleid;
  152. body += "&sloodlegiveobject=" + objecttogive;
  153. llHTTPRequest(sloodleserverroot + SLOODLE_DISTRIB_LINKER, [HTTP_METHOD, "POST", HTTP_MIMETYPE, "application/x-www-form-urlencoded"], body);
  154. return 1;
  155. }
  156. // Checks if the given agent is permitted to control this object
  157. // Returns TRUE if so, or FALSE if not
  158. integer sloodle_check_access_ctrl(key id)
  159. {
  160. // Check the access mode
  161. if (sloodleobjectaccesslevelctrl == SLOODLE_OBJECT_ACCESS_LEVEL_GROUP) {
  162. return llSameGroup(id);
  163. } else if (sloodleobjectaccesslevelctrl == SLOODLE_OBJECT_ACCESS_LEVEL_PUBLIC) {
  164. return TRUE;
  165. }
  166. // Assume it's owner mode
  167. return (id == llGetOwner());
  168. }
  169. // Checks if the given agent is permitted to user this object
  170. // Returns TRUE if so, or FALSE if not
  171. integer sloodle_check_access_use(key id)
  172. {
  173. // Check the access mode
  174. if (sloodleobjectaccessleveluse == SLOODLE_OBJECT_ACCESS_LEVEL_GROUP) {
  175. return llSameGroup(id);
  176. } else if (sloodleobjectaccessleveluse == SLOODLE_OBJECT_ACCESS_LEVEL_PUBLIC) {
  177. return TRUE;
  178. }
  179. // Assume it's owner mode
  180. return (id == llGetOwner());
  181. }
  182. // Add the given agent to our command dialog list
  183. sloodle_add_cmd_dialog(key id, integer page)
  184. {
  185. // Does the person already exist?
  186. integer pos = llListFindList(cmddialog, [id]);
  187. if (pos < 0) {
  188. // No - add the agent to the end
  189. cmddialog += [id, llGetUnixTime(), page];
  190. } else {
  191. // Yes - update the time
  192. cmddialog = llListReplaceList(cmddialog, [llGetUnixTime(), page], pos + 1, pos + 2);
  193. }
  194. }
  195. // Get the number of the page the current user is on in the dialogs
  196. // (Returns 0 if they are not found)
  197. integer sloodle_get_cmd_dialog_page(key id)
  198. {
  199. // Does the person exist in the list?
  200. integer pos = llListFindList(cmddialog, [id]);
  201. if (pos >= 0) {
  202. // Yes - get the page number
  203. return llList2Integer(cmddialog, pos + 2);
  204. }
  205. return 0;
  206. }
  207. // Remove the given agent from our command dialog list
  208. sloodle_remove_cmd_dialog(key id)
  209. {
  210. // Is the person in the list?
  211. integer pos = llListFindList(cmddialog, [id]);
  212. if (pos >= 0) {
  213. // Yes - remove them and their timestamp
  214. cmddialog = llDeleteSubList(cmddialog, pos, pos + 2);
  215. }
  216. }
  217. // Purge the command dialog list of old activity
  218. sloodle_purge_cmd_dialog()
  219. {
  220. // Store the current timestamp
  221. integer curtime = llGetUnixTime();
  222. // Go through each command dialog
  223. integer i = 0;
  224. while (i < llGetListLength(cmddialog)) {
  225. // Is the current timestamp more than 12 seconds old?
  226. if ((curtime - llList2Integer(cmddialog, i + 1)) > 12) {
  227. // Yes - remove it
  228. cmddialog = llDeleteSubList(cmddialog, i, i + 2);
  229. } else {
  230. // No - advance to the next
  231. i += 3;
  232. }
  233. }
  234. }
  235. // Update our inventory list
  236. update_inventory()
  237. {
  238. // We're going to build a string of all copyable inventory items
  239. inventory = [];
  240. inventorystr = "";
  241. integer numitems = llGetInventoryNumber(INVENTORY_ALL);
  242. string itemname = "";
  243. integer numavailable = 0;
  244. // Go through each item
  245. integer i = 0;
  246. for (i = 0; i < numitems; i++) {
  247. // Get the name of this item
  248. itemname = llGetInventoryName(INVENTORY_ALL, i);
  249. // Make sure it's copyable, not a script, and not on the ignore list
  250. if((llGetInventoryPermMask(itemname, MASK_OWNER) & PERM_COPY) && llGetInventoryType(itemname) != INVENTORY_SCRIPT && llListFindList(ignorelist, [itemname]) == -1) {
  251. inventory += [itemname];
  252. if (numavailable > 0) inventorystr += "|";
  253. inventorystr += llEscapeURL(itemname);
  254. numavailable++;
  255. }
  256. }
  257. }
  258. integer Key2AppChan(key ID, integer App) {
  259. return 0x80000000 | ((integer)("0x"+(string)ID) ^ App);
  260. }
  261. // Update the server with our channel and inventory.
  262. // Returns the key of the HTTP request.
  263. key update_server()
  264. {
  265. string body = "sloodlecontrollerid=" + (string)sloodlecontrollerid;
  266. body += "&sloodlepwd=" + sloodlepwd;
  267. body += "&sloodlemoduleid=" + (string)sloodlemoduleid;
  268. body += "&sloodlechannel=" + (string)ch;
  269. body += "&sloodleinventory=" + inventorystr;
  270. return llHTTPRequest(sloodleserverroot + SLOODLE_DISTRIB_LINKER, [HTTP_METHOD, "POST", HTTP_MIMETYPE, "application/x-www-form-urlencoded"], body);
  271. }
  272. // Shows the given user a dialog of objects, starting at the specified page
  273. // If parameter "showcmd" is TRUE, then the "command" menu option will be shown.
  274. sloodle_show_object_dialog(key id, integer page, integer showcmd)
  275. {
  276. // Edmund Edgar, 2012-04-12: Turning off showcmd for now
  277. // We should replace it with the reset button and the open in browser button.
  278. showcmd = 0;
  279. // Each dialog can display 12 buttons, but we need 4 reserved for:
  280. // Previous page, next page, command menu, and web.
  281. // Check how many objects we have
  282. integer numobjects = llGetListLength(inventory);
  283. // How many pages are there?
  284. integer numpages = (integer)((float)numobjects / 9.0);
  285. if (numobjects%9 > 0) numpages+=1;
  286. // If the requested page number is invalid, then cap it
  287. if (page < 0) {
  288. page = 0;
  289. }
  290. else if (page >= numpages) page = numpages - 1;
  291. // Build our list of item buttons (up to a maximum of 9)
  292. list buttonlabels = [];
  293. string buttondef = ""; // Indicates which button does what
  294. integer numbuttons = 0;
  295. integer itemnum = 0;
  296. if (page <0) page=0;
  297. for (itemnum = page * 9; itemnum < numobjects && numbuttons < 9; itemnum++, numbuttons++) {
  298. // Add the button label (a number) and button definition
  299. buttonlabels += [(string)(itemnum + 1)]; // Button labels are 1-based
  300. buttondef += (string)(itemnum + 1) + " = " + llList2String(inventory, itemnum) + "\n";
  301. }
  302. // Add our page buttons if necessary
  303. if (page > 0) {
  304. buttonlabels += ["<<"];//MENU_BUTTON_PREVIOUS];
  305. }
  306. if (page < (numpages - 1)) buttonlabels += [MENU_BUTTON_NEXT];
  307. if (showcmd) {
  308. buttonlabels +=[MENU_BUTTON_CMD];
  309. }
  310. list box1=[];list box2=[];list box3=[];list box4=[];
  311. integer i;
  312. string lab="";
  313. buttonlabels = llListSort(buttonlabels,1,FALSE);
  314. /*
  315. * LSL Dialog buttons get printed on a max of 4 rows in the following order:
  316. *
  317. * 9 10 11 <--- consider this to be box 4
  318. * 6 7 8 <--- consider this to be box 3
  319. * 3 4 5 <--- consider this to be box 2
  320. * 0 1 2 <--- consider this to be box 1
  321. *
  322. * We want the higher numbers to be printed on the bottom row, not the top row, so put them into box1, and the lowest numbers in box 4
  323. * Do this by sorting the buttonlabels in decending order, and then place each label one by one into the boxes starting with box 1 first.
  324. */
  325. for (i=0; i<llGetListLength(buttonlabels);i++){
  326. lab = llList2String(buttonlabels,i);
  327. if (llGetListLength(box1)<3) box1+=lab; else
  328. if (llGetListLength(box2)<3) box2+=lab; else
  329. if (llGetListLength(box3)<3) box3+=lab; else
  330. if (llGetListLength(box4)<3) box4+=lab;
  331. }
  332. //now sort each box so the numbers on each row get printed in ascending order eg: 3,4,5 etc
  333. box1=llListSort(box1, 1, TRUE);
  334. box2=llListSort(box2, 1, TRUE);
  335. box3=llListSort(box3, 1, TRUE);
  336. box4=llListSort(box4, 1, TRUE);
  337. // now build our buttonlabel array, starting with the highest numbers first - this will allow us to display numbers correctly on the dialog
  338. buttonlabels = box1+box2+box3+box4;
  339. if (llGetListLength(buttonlabels)==0){
  340. sloodle_translation_request(SLOODLE_TRANSLATE_DIALOG, [DIALOG_CHANNEL] + "ok", "distributor:distributerisempty", [buttondef,MENU_BUTTON_WEB], id, "distributor");
  341. }else{
  342. sloodle_translation_request(SLOODLE_TRANSLATE_DIALOG, [DIALOG_CHANNEL] + buttonlabels, "dialog:distributorobjectmenu", [buttondef,MENU_BUTTON_WEB], id, "distributor");
  343. }
  344. }
  345. integer random_integer( integer min, integer max ){
  346. return min + (integer)( llFrand( max - min + 1 ) );
  347. }
  348. ///// STATES /////
  349. // In this state, we are uninitialised, waiting for configuration
  350. default
  351. {
  352. state_entry()
  353. {
  354. DIALOG_CHANNEL = Key2AppChan(llGetKey(),SLOODLE_CHANNEL_AVATAR_DIALOG);
  355. sloodle_debug("Distributor: default state");
  356. // Reset to empty settings
  357. sloodleserverroot = "";
  358. sloodlecontrollerid = 0;
  359. sloodlepwd = "";
  360. sloodlemoduleid = 0;
  361. sloodlerefreshtime = 0;
  362. ch = NULL_KEY;
  363. inventory = [];
  364. inventorystr = "";
  365. llSetText("", <0.0,0.0,0.0>, 0.0);
  366. isconfigured = FALSE;
  367. eof = FALSE;
  368. isconnected = FALSE;
  369. }
  370. link_message( integer sender_num, integer num, string str, key id)
  371. {
  372. // Check the channel
  373. if (num == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  374. // Split the message into lines
  375. list lines = llParseString2List(str, ["\n"], []);
  376. integer numlines = llGetListLength(lines);
  377. integer i = 0;
  378. for (i=0; i < numlines; i++) {
  379. isconfigured = sloodle_handle_command(llList2String(lines, i));
  380. }
  381. // If we've got all our data AND reached the end of the configuration data, then move on
  382. if (eof == TRUE) {
  383. if (isconfigured == TRUE) {
  384. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "configurationreceived", [], NULL_KEY, "");
  385. update_inventory();
  386. update_server();
  387. state ready;
  388. } else {
  389. // Got all configuration but, it's not complete... request reconfiguration
  390. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "configdatamissing", [], NULL_KEY, "");
  391. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_OBJECT_DIALOG, "do:reconfigure", NULL_KEY);
  392. eof = FALSE;
  393. }
  394. }
  395. }
  396. }
  397. touch_start(integer num_detected)
  398. {
  399. // Attempt to request a reconfiguration
  400. if (llDetectedKey(0) == llGetOwner()) {
  401. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_OBJECT_DIALOG, "do:requestconfig", NULL_KEY);
  402. }
  403. }
  404. }
  405. // Dummy state to jump straight back into "connecting"
  406. state reconnect
  407. {
  408. state_entry()
  409. {
  410. state ready;
  411. }
  412. }
  413. // Ready to receive http-in requests (if applicable) or user touches
  414. state ready
  415. {
  416. state_entry()
  417. {
  418. sloodle_debug("Distributor: ready state");
  419. // Display status text
  420. if (sloodlemoduleid > 0) {
  421. sloodle_translation_request(SLOODLE_TRANSLATE_HOVER_TEXT, [<0.1,0.9,0.1>, 0.9], "readyconnectedto", [sloodleserverroot], NULL_KEY, "");
  422. } else {
  423. sloodle_translation_request(SLOODLE_TRANSLATE_HOVER_TEXT, [<0.1,0.9,0.1>, 0.9], "readynotconnected", [sloodleserverroot], NULL_KEY, "");
  424. }
  425. // Make sure the refresh timer is not too often... no more than once per minute
  426. if (sloodlerefreshtime > 0 && sloodlerefreshtime < 60) sloodlerefreshtime = 60;
  427. else if (sloodlerefreshtime < 0) sloodlerefreshtime = 0;
  428. // Set a regular timer going
  429. llSetTimerEvent(12.0);
  430. lastrefresh = llGetUnixTime();
  431. // Listen for settings coming in on the avatar dialog channel
  432. llListen(DIALOG_CHANNEL, "", NULL_KEY, "");
  433. }
  434. on_rez(integer start_param)
  435. {
  436. llResetScript();
  437. }
  438. touch_start(integer num_detected)
  439. {
  440. // Go through each toucher
  441. integer i = 0;
  442. key id = NULL_KEY;
  443. for (i=0; i < num_detected; i++) {
  444. id = llDetectedKey(0);
  445. // Make sure the user is allowed to use this object
  446. if (sloodle_check_access_use(id) || sloodle_check_access_ctrl(id)) {
  447. // Show a menu of objects
  448. sloodle_show_object_dialog(id, 0, sloodle_check_access_ctrl(id));
  449. sloodle_add_cmd_dialog(id, 0);
  450. } else {
  451. // Inform the user of the problem
  452. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "nopermission:use", [llKey2Name(id)], NULL_KEY, "");
  453. }
  454. }
  455. }
  456. listen(integer channel, string name, key id, string msg)
  457. {
  458. // Check what channel it is
  459. if (channel == DIALOG_CHANNEL) {
  460. // Ignore the message if the user is not on our list
  461. if (llListFindList(cmddialog, [id]) == -1) {
  462. return;
  463. }
  464. // Find out what the current page number is
  465. integer page = sloodle_get_cmd_dialog_page(id);
  466. msg = llStringTrim(msg, STRING_TRIM);
  467. // Check what message is
  468. if (msg == MENU_BUTTON_NEXT) {
  469. // Show the next menu of objects
  470. sloodle_show_object_dialog(id, page + 1, sloodle_check_access_ctrl(id));
  471. sloodle_add_cmd_dialog(id, page + 1);
  472. } else if (msg == MENU_BUTTON_PREVIOUS) {
  473. // Show the previous menu of objects
  474. sloodle_show_object_dialog(id, page - 1, sloodle_check_access_ctrl(id));
  475. sloodle_add_cmd_dialog(id, page - 1);
  476. }else {
  477. // Treat the message as a number (objects are numbered from 1)
  478. integer objnum = (integer)msg;
  479. if (objnum > 0 && objnum <= llGetListLength(inventory)) {
  480. // Attempt to give the specified item
  481. sloodle_kick_off_inventory_giving_or_say_error(id, llList2String(inventory, objnum - 1));
  482. }
  483. sloodle_remove_cmd_dialog(id);
  484. }
  485. }
  486. }
  487. link_message( integer sender_num, integer num, string str, key id)
  488. {
  489. // llOwnerSay("got message on num"+(string)num);
  490. // llOwnerSay(str);
  491. // Check the channel
  492. if (num ==SLOODLE_CHANNEL_OPEN_IN_BROWSER){
  493. string urltoload = sloodleserverroot+"/mod/sloodle/view.php?id=" +( string)sloodlemoduleid; //the url to load
  494. string transLookup = "dialog:distributorobjectmenu:visitmoodle" ; //the translation lookup as defined in your translation script which will be displayed on the dialog
  495. key avuuid = id; // this is the avatar the dialog will be displayed to
  496. if (isconfigured&&sloodlemoduleid>0) {
  497. sloodle_translation_request(SLOODLE_TRANSLATE_LOAD_URL, [urltoload], transLookup , [], avuuid, "distributor" );
  498. }
  499. }
  500. if (num == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  501. // Split the message into lines
  502. list lines = llParseString2List(str, ["\n"], []);
  503. integer numlines = llGetListLength(lines);
  504. integer i = 0;
  505. for (i=0; i < numlines; i++) {
  506. isconfigured = sloodle_handle_command(llList2String(lines, i));
  507. }
  508. // If we've got all our data AND reached the end of the configuration data, then move on
  509. if (eof == TRUE) {
  510. if (isconfigured == TRUE) {
  511. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "configurationreceived", [], NULL_KEY, "");
  512. state ready;
  513. } else {
  514. // Got all configuration but, it's not complete... request reconfiguration
  515. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "configdatamissing", [], NULL_KEY, "");
  516. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_OBJECT_DIALOG, "do:reconfigure", NULL_KEY);
  517. eof = FALSE;
  518. }
  519. }
  520. } else if (num == SLOODLE_CHANNEL_DISTRIBUTOR_REQUEST_GIVE_OBJECT) {
  521. list lines = llParseString2List(str, ["\n"], []);
  522. key avatartogiveto = llList2Key(lines,1);
  523. string objecttogive = llList2String(lines, 2);
  524. string errors = "";
  525. string ok = "1";
  526. if (llGetInventoryType(objecttogive) == INVENTORY_NONE) {
  527. errors = "sloodleobjectnotfound"+"\n";
  528. ok = "0";
  529. }
  530. // Make sure we can find the identified avatar
  531. if (avatartogiveto == NULL_KEY || llKey2Name(avatartogiveto) == "") {
  532. errors += "sloodleavatarnotfound"+"\n";
  533. ok = "0";
  534. }
  535. // We don't get to find out if llGiveInventory actually works or not
  536. // ...so if the checks worked out, we'll consider it a "yes" and go ahead and send the response to the server.
  537. // That will get the user a faster response, avoiding the need to wait for llGiveInventory and its 2-second delay.
  538. llMessageLinked( sender_num, SLOODLE_CHANNEL_HTTP_RESPONSE, ok+"\n"+errors, id );
  539. llGiveInventory(avatartogiveto, objecttogive);
  540. return;
  541. }
  542. }
  543. changed(integer change)
  544. {
  545. // Was it an inventory change?
  546. if ((change & CHANGED_INVENTORY) == CHANGED_INVENTORY) {
  547. update_inventory();
  548. }
  549. }
  550. timer()
  551. {
  552. // Purge the list of expired dialog
  553. sloodle_purge_cmd_dialog();
  554. // Do we have a distributor module to connect to?
  555. if (sloodlemoduleid > 0) {
  556. // Has the timer period been exceeded?
  557. if ((llGetUnixTime() - lastrefresh) > sloodlerefreshtime) {
  558. // Yes - do an automatic refresh
  559. state reconnect;
  560. }
  561. }
  562. }
  563. http_response(key request_id, integer status, list metadata, string body) {
  564. if (status < 200) {
  565. return;
  566. }
  567. if (status >= 300) {
  568. return;
  569. }
  570. list lines = llParseString2List(body, ["\n"], []);
  571. list bits = llParseStringKeepNulls(llList2String(lines,0),["|"],[]);
  572. integer response_code = llList2Integer(bits,0);
  573. // llOwnerSay(body);
  574. if (response_code == SLOODLE_CHANNEL_DISTRIBUTOR_DO_GIVE_OBJECT){
  575. key avuuid = llList2Key(lines,1);
  576. string object_to_give= llList2String(lines,2);
  577. llDialog(avuuid, "An item called ("+object_to_give+")"+" has been delivered to your inventory, please open your recent items in your inventory", ["ok"], DIALOG_CHANNEL);
  578. llGiveInventory(avuuid,object_to_give);
  579. return;
  580. } else if (response_code < 0) {
  581. if (llGetListLength(bits) > 5) {
  582. key avuuid = llList2Key(bits,6);
  583. if (llGetListLength(lines) > 0) {
  584. if (llKey2Name(avuuid) != "") {
  585. // Report an error to the user, eg they didn't have enough money.
  586. llSay(0, llKey2Name(avuuid)+", "+llList2String(lines,1));
  587. return;
  588. }
  589. }
  590. }
  591. }
  592. return;
  593. }
  594. }
  595. // Please leave the following line intact to show where the script lives in Git:
  596. // SLOODLE LSL Script Git Location: mod/distributor-2.0/objects/default/assets/sloodle_vendor.lslp