corrade-lsl-templates – Blame information for rev 42

Subversion Repositories:
Rev:
Rev Author Line No. Line
42 office 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 //
3 ///////////////////////////////////////////////////////////////////////////
4 //
5 // A module that pulls information from an encyclopedia.
6 //
7 ///////////////////////////////////////////////////////////////////////////
8  
9 ///////////////////////////////////////////////////////////////////////////
10 // Copyright (C) 2015 Wizardry and Steamworks - License: CC BY 2.0 //
11 ///////////////////////////////////////////////////////////////////////////
12 string wasKeyValueGet(string k, string data) {
13 if(llStringLength(data) == 0) return "";
14 if(llStringLength(k) == 0) return "";
15 list a = llParseStringKeepNulls(data, ["&", "="], []);
16 integer i = llListFindList(llList2ListStrided(a, 0, -1, 2), [ k ]);
17 if(i != -1) return llList2String(a, 2*i+1);
18 return "";
19 }
20  
21 ///////////////////////////////////////////////////////////////////////////
22 // Copyright (C) 2013 Wizardry and Steamworks - License: GNU GPLv3 //
23 ///////////////////////////////////////////////////////////////////////////
24 string wasKeyValueEncode(list data) {
25 list k = llList2ListStrided(data, 0, -1, 2);
26 list v = llList2ListStrided(llDeleteSubList(data, 0, 0), 0, -1, 2);
27 data = [];
28 do {
29 data += llList2String(k, 0) + "=" + llList2String(v, 0);
30 k = llDeleteSubList(k, 0, 0);
31 v = llDeleteSubList(v, 0, 0);
32 } while(llGetListLength(k) != 0);
33 return llDumpList2String(data, "&");
34 }
35  
36 ///////////////////////////////////////////////////////////////////////////
37 // Copyright (C) 2011 Wizardry and Steamworks - License: GNU GPLv3 //
38 ///////////////////////////////////////////////////////////////////////////
39 // http://was.fm/secondlife/wanderer
40 vector wasCirclePoint(float radius) {
41 float x = llPow(-1, 1 + (integer) llFrand(2)) * llFrand(radius*2);
42 float y = llPow(-1, 1 + (integer) llFrand(2)) * llFrand(radius*2);
43 if(llPow(x,2) + llPow(y,2) <= llPow(radius,2))
44 return <x, y, 0>;
45 return wasCirclePoint(radius);
46 }
47  
48 ///////////////////////////////////////////////////////////////////////////
49 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
50 ///////////////////////////////////////////////////////////////////////////
51 // escapes a string in conformance with RFC1738
52 string wasURLEscape(string i) {
53 string o = "";
54 do {
55 string c = llGetSubString(i, 0, 0);
56 i = llDeleteSubString(i, 0, 0);
57 if(c == "") jump continue;
58 if(c == " ") {
59 o += "+";
60 jump continue;
61 }
62 if(c == "\n") {
63 o += "%0D" + llEscapeURL(c);
64 jump continue;
65 }
66 o += llEscapeURL(c);
67 @continue;
68 } while(i != "");
69 return o;
70 }
71  
72 ///////////////////////////////////////////////////////////////////////////
73 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
74 ///////////////////////////////////////////////////////////////////////////
75 list wasCSVToList(string csv) {
76 list l = [];
77 list s = [];
78 string m = "";
79 do {
80 string a = llGetSubString(csv, 0, 0);
81 csv = llDeleteSubString(csv, 0, 0);
82 if(a == ",") {
83 if(llList2String(s, -1) != "\"") {
84 l += m;
85 m = "";
86 jump continue;
87 }
88 m += a;
89 jump continue;
90 }
91 if(a == "\"" && llGetSubString(csv, 0, 0) == a) {
92 m += a;
93 csv = llDeleteSubString(csv, 0, 0);
94 jump continue;
95 }
96 if(a == "\"") {
97 if(llList2String(s, -1) != a) {
98 s += a;
99 jump continue;
100 }
101 s = llDeleteSubList(s, -1, -1);
102 jump continue;
103 }
104 m += a;
105 @continue;
106 } while(csv != "");
107 // postcondition: length(s) = 0
108 return l + m;
109 }
110  
111 ///////////////////////////////////////////////////////////////////////////
112 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
113 ///////////////////////////////////////////////////////////////////////////
114 string wasListToCSV(list l) {
115 list v = [];
116 do {
117 string a = llDumpList2String(
118 llParseStringKeepNulls(
119 llList2String(
120 l,
121  
122 ),
123 ["\""],
124 []
125 ),
126 "\"\""
127 );
128 if(llParseStringKeepNulls(
129 a,
130 [" ", ",", "\n", "\""], []
131 ) !=
132 (list) a
133 ) a = "\"" + a + "\"";
134 v += a;
135 l = llDeleteSubList(l, 0, 0);
136 } while(l != []);
137 return llDumpList2String(v, ",");
138 }
139  
140 ///////////////////////////////////////////////////////////////////////////
141 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
142 ///////////////////////////////////////////////////////////////////////////
143 // unescapes a string in conformance with RFC1738
144 string wasURLUnescape(string i) {
145 return llUnescapeURL(
146 llDumpList2String(
147 llParseString2List(
148 llDumpList2String(
149 llParseString2List(
150 i,
151 ["+"],
152 []
153 ),
154 " "
155 ),
156 ["%0D%0A"],
157 []
158 ),
159 "\n"
160 )
161 );
162 }
163  
164 // configuration data
165 string configuration = "";
166 // store message over state.
167 string data = "";
168 string query = "";
169  
170 default {
171 state_entry() {
172 llOwnerSay("[Urban Dictionary] Starting module...");
173 llSetTimerEvent(10);
174 }
175 link_message(integer sender, integer num, string message, key id) {
176 if(id != "configuration") return;
177 llOwnerSay("[Urban Dictionary] Got configuration...");
178 configuration = message;
179 state listen_group;
180 }
181 timer() {
182 llOwnerSay("[Urban Dictionary] Requesting configuration...");
183 llMessageLinked(LINK_THIS, 0, "configuration", NULL_KEY);
184 }
185 on_rez(integer num) {
186 llResetScript();
187 }
188 changed(integer change) {
189 if((change & CHANGED_INVENTORY) ||
190 (change & CHANGED_REGION_START) ||
191 (change & CHANGED_OWNER)) {
192 llResetScript();
193 }
194 }
195 state_exit() {
196 llSetTimerEvent(0);
197 }
198 }
199  
200 state listen_group {
201 state_entry() {
202 // DEBUG
203 llOwnerSay("[Urban Dictionary] Waiting for group messages...");
204 }
205 link_message(integer sender, integer num, string message, key id) {
206 // We only care about notifications now.
207 if(id != "notification")
208 return;
209  
210 // This script only processes group notifications.
211 if(wasKeyValueGet("type", message) != "group" ||
212 (wasKeyValueGet("type", message) == "group" &&
213 wasURLUnescape(wasKeyValueGet("group", message)) !=
214 wasKeyValueGet("group", configuration)))
215 return;
216  
217 // Get the sent message.
218 data = wasURLUnescape(
219 wasKeyValueGet(
220 "message",
221 message
222 )
223 );
224  
225 // Check if this is an eggdrop command.
226 if(llGetSubString(data, 0, 0) !=
227 wasKeyValueGet("command", configuration))
228 return;
229  
230 // Check if the command matches the current module.
231 list command = llParseString2List(data, [" "], []);
232 if(llList2String(command, 0) !=
233 wasKeyValueGet("command", configuration) + "urbandictionary")
234 return;
235  
236 // Remove command.
237 command = llDeleteSubList(command, 0, 0);
238  
239 query = llDumpList2String(command, " ");
240  
241 state query_wikipedia;
242 }
243 on_rez(integer num) {
244 llResetScript();
245 }
246 changed(integer change) {
247 if((change & CHANGED_INVENTORY) ||
248 (change & CHANGED_REGION_START) ||
249 (change & CHANGED_OWNER)) {
250 llResetScript();
251 }
252 }
253 }
254  
255 state query_wikipedia {
256 state_entry() {
257 // DEBUG
258 llOwnerSay("[Urban Dictionary] Querying...");
259 llInstantMessage(
260 wasKeyValueGet(
261 "corrade",
262 configuration
263 ),
264 wasKeyValueEncode(
265 [
266 "command", "HTTP",
267 "group", wasURLEscape(
268 wasKeyValueGet(
269 "group",
270 configuration
271 )
272 ),
273 "password", wasURLEscape(
274 wasKeyValueGet(
275 "password",
276 configuration
277 )
278 ),
279 "action", "get",
280 "type", "text",
281 "URL", wasURLEscape("http://api.urbandictionary.com/v0/define?term=" + query),
282 "sift", wasURLEscape(
283 wasListToCSV(
284 [
285 "select",
286 "body",
287 "JSONPath",
288 wasURLEscape("$..list[*].definition"),
289 "random",
290 (string)llFloor(llFrand(2147483647))
291 ]
292 )
293 ),
294 "callback", wasURLEscape(
295 wasKeyValueGet(
296 "URL",
297 configuration
298 )
299 )
300 ]
301 )
302 );
303  
304 llSetTimerEvent(60);
305 }
306 link_message(integer sender, integer num, string body, key id) {
307 // Only process callbacks for the database command.
308 if(id != "callback" || wasKeyValueGet("command", body) != "HTTP")
309 return;
310  
311 if(wasKeyValueGet("success", body) != "True") {
312 // DEBUG
313 llOwnerSay("[Urban Dictionary] Unable to query: " +
314 wasURLUnescape(
315 wasKeyValueGet("error", body)
316 )
317 );
318 state listen_group;
319 }
320  
321 data = llDumpList2String(
322 llParseString2List(
323 wasKeyValueGet(
324 "data",
325 wasURLUnescape(body)
326 ),
327 ["\\r", "\n", "\\n", "[", "]", "\\\""],
328 []
329 ),
330 ""
331 );
332  
333  
334 if(llStringLength(data) == 0)
335 data = "No data returned, perhaps the site is down!";
336  
337 state tell;
338 }
339 timer() {
340 state listen_group;
341 }
342 on_rez(integer num) {
343 llResetScript();
344 }
345 changed(integer change) {
346 if((change & CHANGED_INVENTORY) ||
347 (change & CHANGED_REGION_START) ||
348 (change & CHANGED_OWNER)) {
349 llResetScript();
350 }
351 }
352 state_exit() {
353 llSetTimerEvent(0);
354 }
355 }
356  
357 state tell {
358 state_entry() {
359 // DEBUG
360 llOwnerSay("[Urban Dictionary] Sending to group...");
361 llInstantMessage(
362 wasKeyValueGet(
363 "corrade",
364 configuration
365 ),
366 wasKeyValueEncode(
367 [
368 "command", "tell",
369 "group", wasURLEscape(
370 wasKeyValueGet(
371 "group",
372 configuration
373 )
374 ),
375 "password", wasURLEscape(
376 wasKeyValueGet(
377 "password",
378 configuration
379 )
380 ),
381 "entity", "group",
382 "message", wasURLEscape(data)
383 ]
384 )
385 );
386 state listen_group;
387 }
388 }