sloodle_mod_demo-1.0.lslp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. //
  2. // The line above should be left blank to avoid script errors in OpenSim.
  3. // Sloodle Demo object (for Sloodle 0.4)
  4. // Part of the SLOODLE project (www.sloodle.org)
  5. //
  6. // Note: all language output in this script SHOULD use translation requests, but it doesn't yet!
  7. //
  8. // Copyright (c) 2009 Sloodle
  9. // Released under the GNU GPL
  10. //
  11. // Contributors:
  12. // Peter R. Bloomfield
  13. //
  14. // These are common constants
  15. integer SLOODLE_CHANNEL_OBJECT_DIALOG = -3857343;
  16. integer SLOODLE_CHANNEL_AVATAR_DIALOG = 1001;
  17. string SLOODLE_EOF = "sloodleeof";
  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. // This variable identifies the type of object this is
  22. string SLOODLE_OBJECT_TYPE = "demo-1.0";
  23. // This string identifies the location of the linker script relative to this Moodle root
  24. string SLOODLE_DEMO_LINKER = "/mod/sloodle/mod/demo-1.0/linker.php";
  25. // These are common configuration settings
  26. string sloodleserverroot = "";
  27. string sloodlepwd = "";
  28. integer sloodlecontrollerid = 0;
  29. integer sloodlemoduleid = 0;
  30. integer sloodleobjectaccessleveluse = 0; // Who can use this object?
  31. integer sloodleobjectaccesslevelctrl = 0; // Who can control this object?
  32. integer sloodleserveraccesslevel = 0; // Who can use the server resource? (Value passed straight back to Moodle)
  33. integer sloodleautodeactivate = 1; // Should the WebIntercom auto-deactivate when not in use?
  34. // These are object-specific configuration settings
  35. string sloodlerandomtext = "";
  36. integer sloodleshowhovertext = 1;
  37. // TODO: add other object-specific configuration settings...
  38. // These are used when receiving configuration so we know when to move on
  39. integer isconfigured = FALSE; // Do we have all the configuration data we need?
  40. integer eof = FALSE; // Have we reached the end of the configuration data?
  41. // TODO: add other data your script needs
  42. key http = NULL_KEY;
  43. ///// TRANSLATION /////
  44. // This is common translation code.
  45. // Leave it alone! But feel free to copy it to other scripts.
  46. // Link message channels
  47. integer SLOODLE_CHANNEL_TRANSLATION_REQUEST = -1928374651;
  48. integer SLOODLE_CHANNEL_TRANSLATION_RESPONSE = -1928374652;
  49. // Translation output methods
  50. string SLOODLE_TRANSLATE_LINK = "link"; // No output parameters - simply returns the translation on SLOODLE_TRANSLATION_RESPONSE link message channel
  51. string SLOODLE_TRANSLATE_SAY = "say"; // 1 output parameter: chat channel number
  52. string SLOODLE_TRANSLATE_WHISPER = "whisper"; // 1 output parameter: chat channel number
  53. string SLOODLE_TRANSLATE_SHOUT = "shout"; // 1 output parameter: chat channel number
  54. string SLOODLE_TRANSLATE_REGION_SAY = "regionsay"; // 1 output parameter: chat channel number
  55. string SLOODLE_TRANSLATE_OWNER_SAY = "ownersay"; // No output parameters
  56. 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.
  57. string SLOODLE_TRANSLATE_LOAD_URL = "loadurl"; // Recipient avatar should be identified in link message keyval. 1 output parameter giving URL to load.
  58. string SLOODLE_TRANSLATE_HOVER_TEXT = "hovertext"; // 2 output parameters: colour <r,g,b>, and alpha value
  59. string SLOODLE_TRANSLATE_IM = "instantmessage"; // Recipient avatar should be identified in link message keyval. No output parameters.
  60. // Send a translation request link message
  61. sloodle_translation_request(string output_method, list output_params, string string_name, list string_params, key keyval, string batch)
  62. {
  63. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_TRANSLATION_REQUEST, output_method + "|" + llList2CSV(output_params) + "|" + string_name + "|" + llList2CSV(string_params) + "|" + batch, keyval);
  64. }
  65. ///// ----------- /////
  66. ///// FUNCTIONS /////
  67. // Common SLOODLE debug output
  68. sloodle_debug(string msg)
  69. {
  70. llMessageLinked(LINK_THIS, DEBUG_CHANNEL, msg, NULL_KEY);
  71. }
  72. // Configure by receiving a linked message from another script in the object.
  73. // Returns TRUE if the object has all the data it needs.
  74. // Copy the basic structure for other objects, but add/remove specific configuration settings as necessary.
  75. integer sloodle_handle_command(string str)
  76. {
  77. list bits = llParseString2List(str,["|"],[]);
  78. integer numbits = llGetListLength(bits);
  79. string name = llList2String(bits,0);
  80. string value1 = "";
  81. string value2 = "";
  82. if (numbits > 1) value1 = llList2String(bits,1);
  83. if (numbits > 2) value2 = llList2String(bits,2);
  84. if (name == "set:sloodleserverroot") sloodleserverroot = value1;
  85. else if (name == "set:sloodlepwd") {
  86. // The password may be a single prim password, or a UUID and a password
  87. if (value2 != "") sloodlepwd = value1 + "|" + value2;
  88. else sloodlepwd = value1;
  89. } else if (name == "set:sloodlecontrollerid") sloodlecontrollerid = (integer)value1;
  90. else if (name == "set:sloodlemoduleid") sloodlemoduleid = (integer)value1;
  91. else if (name == "set:sloodleobjectaccessleveluse") sloodleobjectaccessleveluse = (integer)value1;
  92. else if (name == "set:sloodleobjectaccesslevelctrl") sloodleobjectaccesslevelctrl = (integer)value1;
  93. else if (name == "set:sloodleserveraccesslevel") sloodleserveraccesslevel = (integer)value1;
  94. else if (name == "set:sloodlerandomtext") sloodlerandomtext = value1;
  95. else if (name == "set:sloodleshowhovertext") sloodleshowhovertext = (integer)value1;
  96. // TODO: Add additional configuration parameters here
  97. else if (name == SLOODLE_EOF) eof = TRUE;
  98. // This line figures out if we have all the core data we need.
  99. // TODO: If you absolutely need any other core data in the configuration, then add it to this condition.
  100. return (sloodleserverroot != "" && sloodlepwd != "" && sloodlecontrollerid > 0 && sloodlemoduleid > 0);
  101. }
  102. // Checks if the given agent is permitted to control this object.
  103. // Returns TRUE if so, or FALSE if not.
  104. // You can leave this out if you don't need to check for control authority.
  105. integer sloodle_check_access_ctrl(key id)
  106. {
  107. // Check the access mode
  108. if (sloodleobjectaccesslevelctrl == SLOODLE_OBJECT_ACCESS_LEVEL_GROUP) {
  109. return llSameGroup(id);
  110. } else if (sloodleobjectaccesslevelctrl == SLOODLE_OBJECT_ACCESS_LEVEL_PUBLIC) {
  111. return TRUE;
  112. }
  113. // Assume it's owner mode
  114. return (id == llGetOwner());
  115. }
  116. // Checks if the given agent is permitted to user this object.
  117. // Returns TRUE if so, or FALSE if not.
  118. // You can leave this out if you don't need to check for usage authority.
  119. integer sloodle_check_access_use(key id)
  120. {
  121. // Check the access mode
  122. if (sloodleobjectaccessleveluse == SLOODLE_OBJECT_ACCESS_LEVEL_GROUP) {
  123. return llSameGroup(id);
  124. } else if (sloodleobjectaccessleveluse == SLOODLE_OBJECT_ACCESS_LEVEL_PUBLIC) {
  125. return TRUE;
  126. }
  127. // Assume it's owner mode
  128. return (id == llGetOwner());
  129. }
  130. // TODO: add other functions your script might need
  131. ///// STATES /////
  132. // Default state - waiting for configuration.
  133. // The first state of your main script should ALWAYS be something like this.
  134. // However, you'll need to tweak it for each object.
  135. default
  136. {
  137. state_entry()
  138. {
  139. // Starting again with a new configuration
  140. llSetText("", <0.0,0.0,0.0>, 0.0);
  141. isconfigured = FALSE;
  142. eof = FALSE;
  143. // Reset our data
  144. sloodleserverroot = "";
  145. sloodlepwd = "";
  146. sloodlecontrollerid = 0;
  147. sloodlemoduleid = 0;
  148. sloodleobjectaccessleveluse = 0;
  149. sloodleobjectaccesslevelctrl = 0;
  150. sloodleserveraccesslevel = 0;
  151. sloodlerandomtext = "";
  152. sloodleshowhovertext = 0;
  153. // TODO: Add other custom reset stuff here...
  154. }
  155. link_message( integer sender_num, integer num, string str, key id)
  156. {
  157. // Received a link message possibly containing configuration data.
  158. // Split it up and process it.
  159. // Check the channel
  160. if (num == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  161. // Split the message into lines
  162. list lines = llParseString2List(str, ["\n"], []);
  163. integer numlines = llGetListLength(lines);
  164. integer i = 0;
  165. for (i=0; i < numlines; i++) {
  166. isconfigured = sloodle_handle_command(llList2String(lines, i));
  167. }
  168. // If we've got all our data AND reached the end of the configuration data, then move on
  169. if (eof == TRUE) {
  170. if (isconfigured == TRUE) {
  171. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "configurationreceived", [], NULL_KEY, "");
  172. // TODO: customize the state change if you need to
  173. state ready;
  174. return;
  175. } else {
  176. // Go all configuration but, it's not complete... request reconfiguration
  177. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "configdatamissing", [], NULL_KEY, "");
  178. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_OBJECT_DIALOG, "do:reconfigure", NULL_KEY);
  179. eof = FALSE;
  180. }
  181. }
  182. }
  183. }
  184. touch_start(integer num_detected)
  185. {
  186. // Attempt to request a reconfiguration.
  187. if (llDetectedKey(0) == llGetOwner()) {
  188. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_OBJECT_DIALOG, "do:requestconfig", NULL_KEY);
  189. }
  190. }
  191. }
  192. // TODO: from this point on, the structure of the script is largely up to you!
  193. // Common name for the primary state in which the tool operates.
  194. state ready
  195. {
  196. on_rez( integer param)
  197. {
  198. state default;
  199. }
  200. state_entry()
  201. {
  202. // Should we show hover text?
  203. // (This is set in object configuration)
  204. if (sloodleshowhovertext) {
  205. llSetText("Random text:\n" + sloodlerandomtext, <1.0, 0.0, 0.0>, 1.0);
  206. } else {
  207. llSay(0, "");
  208. }
  209. }
  210. state_exit()
  211. {
  212. llSetTimerEvent(0.0);
  213. }
  214. touch_start( integer total_number)
  215. {
  216. // Check to see if the user has permission to control or use this object.
  217. if (sloodle_check_access_ctrl(llDetectedKey(0))) {
  218. llSay(0, llDetectedName(0) + " has permission to control this object.");
  219. } else if (sloodle_check_access_use(llDetectedKey(0))) {
  220. llSay(0, llDetectedName(0) + " has permission to use this object.");
  221. } else {
  222. // No permission
  223. llSay(0, llDetectedName(0) + " does not have permission to control or use this object.");
  224. }
  225. // We want to communicate with the linker script.
  226. // Construct the body of the request, and then send it all as POST parameters.
  227. // First, we will pass the configuration data that needs to be passed:
  228. string body = "sloodlecontrollerid=" + (string)sloodlecontrollerid;
  229. body += "&sloodlepwd=" + (string)sloodlepwd;
  230. body += "&sloodlemoduleid=" + (string)sloodlemoduleid;
  231. body += "&sloodleserveraccesslevel=" + (string)sloodleserveraccesslevel;
  232. // Add our other data
  233. body += "&sloodleavname=" + llEscapeURL(llDetectedName(0));
  234. body += "&sloodleuuid=" + (string)llDetectedKey(0);
  235. body += "&message=testing";
  236. body += "&anothermessage=testing_some_more";
  237. // Now send the data
  238. http = llHTTPRequest(sloodleserverroot + SLOODLE_DEMO_LINKER, [HTTP_METHOD, "POST", HTTP_MIMETYPE, "application/x-www-form-urlencoded"], body);
  239. llSetTimerEvent(10.0);
  240. }
  241. http_response(key id, integer status, list meta, string body)
  242. {
  243. // An HTTP response has been received.
  244. // Ignore anything that's not expected.
  245. if (id != http) return;
  246. http = NULL_KEY;
  247. llSetTimerEvent(0.0);
  248. // Check the status code
  249. if (status != 200) {
  250. // An error occurred
  251. llSay(0, "HTTP request failed with status code " + (string)status);
  252. return;
  253. }
  254. // Output the whole response
  255. llSay(0, "HTTP response:\n\n" + body);
  256. }
  257. timer()
  258. {
  259. llSetTimerEvent(0.0);
  260. llSay(0, "HTTP Timeout");
  261. }
  262. }
  263. // Please leave the following line intact to show where the script lives in Git:
  264. // SLOODLE LSL Script Git Location: mod/demo-1.0/objects/default/assets/sloodle_mod_demo-1.0.lslp