corrade-lsl-templates – Blame information for rev 4

Subversion Repositories:
Rev:
Rev Author Line No. Line
4 office 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 //
3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
4 // rights of fair usage, the disclaimer and warranty conditions. //
5 ///////////////////////////////////////////////////////////////////////////
6  
7 ///////////////////////////////////////////////////////////////////////////
8 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
9 ///////////////////////////////////////////////////////////////////////////
10 // escapes a string in conformance with RFC1738
11 string wasURLEscape(string i) {
12 string o = "";
13 do {
14 string c = llGetSubString(i, 0, 0);
15 i = llDeleteSubString(i, 0, 0);
16 if(c == "") jump continue;
17 if(c == " ") {
18 o += "+";
19 jump continue;
20 }
21 if(c == "\n") {
22 o += "%0D" + llEscapeURL(c);
23 jump continue;
24 }
25 o += llEscapeURL(c);
26 @continue;
27 } while(i != "");
28 return o;
29 }
30  
31 ///////////////////////////////////////////////////////////////////////////
32 // Copyright (C) 2013 Wizardry and Steamworks - License: GNU GPLv3 //
33 ///////////////////////////////////////////////////////////////////////////
34 string wasKeyValueEncode(list data) {
35 list k = llList2ListStrided(data, 0, -1, 2);
36 list v = llList2ListStrided(llDeleteSubList(data, 0, 0), 0, -1, 2);
37 data = [];
38 do {
39 data += llList2String(k, 0) + "=" + llList2String(v, 0);
40 k = llDeleteSubList(k, 0, 0);
41 v = llDeleteSubList(v, 0, 0);
42 } while(llGetListLength(k) != 0);
43 return llDumpList2String(data, "&");
44 }
45  
46 // Temporary storage for notecard lines.
47 integer f;
48 string say;
49  
50 // Corrade data
51 string CORRADE = "";
52 string GROUP = "";
53 string PASSWORD = "";
54 integer INTERVAL = 21600;
55  
56 // For notecard reading
57 integer line = 0;
58  
59 // Key-value data will be read into this list
60 list tuples = [];
61  
62 default {
63 state_entry() {
64 if(llGetInventoryType("configuration") != INVENTORY_NOTECARD) {
65 llOwnerSay("Sorry, could not find an inventory notecard.");
66 return;
67 }
68 // DEBUG
69 llOwnerSay("Reading configuration file...");
70 llGetNotecardLine("configuration", line);
71 }
72 dataserver(key id, string data) {
73 if(data == EOF) {
74 // invariant, length(tuples) % 2 == 0
75 if(llGetListLength(tuples) % 2 != 0) {
76 llOwnerSay("Error in configuration notecard.");
77 return;
78 }
79 CORRADE = llList2String(
80 tuples,
81 llListFindList(
82 tuples,
83 [
84 "corrade"
85 ]
86 )
87 +1);
88 if(CORRADE == "") {
89 llOwnerSay("Error in configuration notecard: corrade");
90 return;
91 }
92 GROUP = llList2String(
93 tuples,
94 llListFindList(
95 tuples,
96 [
97 "group"
98 ]
99 )
100 +1);
101 if(GROUP == "") {
102 llOwnerSay("Error in configuration notecard: group");
103 return;
104 }
105 PASSWORD = llList2String(
106 tuples,
107 llListFindList(
108 tuples,
109 [
110 "password"
111 ]
112 )
113 +1);
114 if(PASSWORD == "") {
115 llOwnerSay("Error in configuration notecard: password");
116 return;
117 }
118 INTERVAL = llList2Integer(
119 tuples,
120 llListFindList(
121 tuples,
122 [
123 "interval"
124 ]
125 )
126 +1);
127 if(INTERVAL == 0) {
128 llOwnerSay("Error in configuration notecard: interval");
129 return;
130 }
131 // DEBUG
132 llOwnerSay("Read configuration file...");
133 state fortune;
134 }
135 if(data == "") jump continue;
136 integer i = llSubStringIndex(data, "#");
137 if(i != -1) data = llDeleteSubString(data, i, -1);
138 list o = llParseString2List(data, ["="], []);
139 // get rid of starting and ending quotes
140 string k = llDumpList2String(
141 llParseString2List(
142 llStringTrim(
143 llList2String(
144 o,
145  
146 ),
147 STRING_TRIM),
148 ["\""], []
149 ), "\"");
150 string v = llDumpList2String(
151 llParseString2List(
152 llStringTrim(
153 llList2String(
154 o,
155 1
156 ),
157 STRING_TRIM),
158 ["\""], []
159 ), "\"");
160 if(k == "" || v == "") jump continue;
161 tuples += k;
162 tuples += v;
163 @continue;
164 llGetNotecardLine("configuration", ++line);
165 }
166 on_rez(integer num) {
167 llResetScript();
168 }
169 changed(integer change) {
170 if((change & CHANGED_INVENTORY) || (change & CHANGED_REGION_START)) {
171 llResetScript();
172 }
173 }
174 }
175  
176 state fortune {
177 state_entry() {
178 if(llGetInventoryType("Fortunes") == INVENTORY_NOTECARD) jump schedule;
179 llSay(DEBUG_CHANNEL, "Fortunes notecard not found.");
180 return;
181 @schedule;
182 llGetNumberOfNotecardLines("Fortunes");
183 }
184 dataserver(key requested, string data) {
185 f=(integer)data;
186 llOwnerSay("Ready, sleeping...");
187 state quote;
188 }
189 on_rez(integer num) {
190 llResetScript();
191 }
192 changed(integer change) {
193 if((change & CHANGED_INVENTORY) || (change & CHANGED_REGION_START)) {
194 llResetScript();
195 }
196 }
197 }
198  
199 state quote {
200 state_entry() {
201 llGetNotecardLine("Fortunes", (integer)llFrand(f));
202 }
203 timer() {
204 state speak;
205 }
206 dataserver(key requested, string data) {
207 say = data;
208 llSetTimerEvent(1 + llFrand(INTERVAL));
209  
210 }
211 on_rez(integer num) {
212 llResetScript();
213 }
214 changed(integer change) {
215 if((change & CHANGED_INVENTORY) || (change & CHANGED_REGION_START)) {
216 llResetScript();
217 }
218 }
219 state_exit() {
220 llSetTimerEvent(0);
221 }
222 }
223  
224 state speak {
225 state_entry() {
226 llInstantMessage(CORRADE,
227 wasKeyValueEncode(
228 [
229 "command", "tell",
230 "group", wasURLEscape(GROUP),
231 "password", wasURLEscape(PASSWORD),
232 "entity", "group",
233 "message", wasURLEscape(say)
234 ]
235 )
236 );
237 llSetTimerEvent(1);
238 }
239 timer() {
240 state quote;
241 }
242 on_rez(integer num) {
243 llResetScript();
244 }
245 changed(integer change) {
246 if((change & CHANGED_INVENTORY) || (change & CHANGED_REGION_START)) {
247 llResetScript();
248 }
249 }
250 state_exit() {
251 llSetTimerEvent(0);
252 }
253 }