corrade-lsl-templates – Blame information for rev 41
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
8 | office | 1 | /////////////////////////////////////////////////////////////////////////// |
29 | office | 2 | // Copyright (C) Wizardry and Steamworks 2016 - License: CC BY 2.0 // |
8 | office | 3 | /////////////////////////////////////////////////////////////////////////// |
4 | // |
||
5 | // A fortune module for Corrade Eggdrop. |
||
6 | // |
||
7 | /////////////////////////////////////////////////////////////////////////// |
||
8 | |||
9 | /////////////////////////////////////////////////////////////////////////// |
||
41 | office | 10 | // Copyright (C) 2015 Wizardry and Steamworks - License: CC BY 2.0 // |
8 | office | 11 | /////////////////////////////////////////////////////////////////////////// |
12 | string wasKeyValueGet(string k, string data) { |
||
13 | if(llStringLength(data) == 0) return ""; |
||
14 | if(llStringLength(k) == 0) return ""; |
||
41 | office | 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); |
||
8 | office | 18 | return ""; |
19 | } |
||
20 | |||
21 | /////////////////////////////////////////////////////////////////////////// |
||
29 | office | 22 | // Copyright (C) 2013 Wizardry and Steamworks - License: CC BY 2.0 // |
8 | office | 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 | /////////////////////////////////////////////////////////////////////////// |
||
29 | office | 37 | // Copyright (C) 2011 Wizardry and Steamworks - License: CC BY 2.0 // |
8 | office | 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 | /////////////////////////////////////////////////////////////////////////// |
||
29 | office | 49 | // Copyright (C) 2015 Wizardry and Steamworks - License: CC BY 2.0 // |
8 | office | 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 | /////////////////////////////////////////////////////////////////////////// |
||
29 | office | 73 | // Copyright (C) 2015 Wizardry and Steamworks - License: CC BY 2.0 // |
8 | office | 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 | /////////////////////////////////////////////////////////////////////////// |
||
29 | office | 112 | // Copyright (C) 2015 Wizardry and Steamworks - License: CC BY 2.0 // |
8 | office | 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 | /////////////////////////////////////////////////////////////////////////// |
||
29 | office | 141 | // Copyright (C) 2015 Wizardry and Steamworks - License: CC BY 2.0 // |
8 | office | 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 | // callback URL |
||
167 | string URL = ""; |
||
168 | // store message over state. |
||
169 | string data = ""; |
||
170 | |||
171 | // Notecard reading. |
||
172 | key nQuery = NULL_KEY; |
||
173 | integer nLine = 0; |
||
174 | list nList = []; |
||
175 | |||
176 | default { |
||
177 | state_entry() { |
||
178 | llOwnerSay("[Fortune] Starting module..."); |
||
179 | llSetTimerEvent(10); |
||
180 | } |
||
181 | link_message(integer sender, integer num, string message, key id) { |
||
182 | if(id != "configuration") return; |
||
183 | llOwnerSay("[Fortune] Got configuration..."); |
||
184 | configuration = message; |
||
185 | state read_fortunes; |
||
186 | } |
||
187 | timer() { |
||
188 | llOwnerSay("[Fortune] Requesting configuration..."); |
||
189 | llMessageLinked(LINK_THIS, 0, "configuration", NULL_KEY); |
||
190 | } |
||
191 | on_rez(integer num) { |
||
192 | llResetScript(); |
||
193 | } |
||
194 | changed(integer change) { |
||
195 | if((change & CHANGED_INVENTORY) || |
||
196 | (change & CHANGED_REGION_START) || |
||
197 | (change & CHANGED_OWNER)) { |
||
198 | llResetScript(); |
||
199 | } |
||
200 | } |
||
201 | state_exit() { |
||
202 | llSetTimerEvent(0); |
||
203 | } |
||
204 | } |
||
205 | |||
206 | state read_fortunes { |
||
207 | state_entry() { |
||
208 | // DEBUG |
||
209 | llOwnerSay("[Fortune] Reading fortunes..."); |
||
210 | if(llGetInventoryType("Fortune") != INVENTORY_NOTECARD) { |
||
211 | llOwnerSay("[Fortune] Failed to find a notecard named fortune in the primitive's inventory."); |
||
212 | return; |
||
213 | } |
||
214 | nQuery = llGetNotecardLine("Fortune", nLine); |
||
215 | } |
||
216 | dataserver(key id, string data) { |
||
217 | if(id != nQuery) return; |
||
218 | if(data == EOF) { |
||
219 | // DEBUG |
||
220 | llOwnerSay("[Fortune] Read fortunes..."); |
||
221 | state listen_group; |
||
222 | } |
||
223 | if(data == "") jump continue; |
||
224 | nList += data; |
||
225 | @continue; |
||
226 | nQuery = llGetNotecardLine("Fortune", ++nLine); |
||
227 | } |
||
228 | on_rez(integer num) { |
||
229 | llResetScript(); |
||
230 | } |
||
231 | changed(integer change) { |
||
232 | if((change & CHANGED_INVENTORY) || |
||
233 | (change & CHANGED_REGION_START) || |
||
234 | (change & CHANGED_OWNER)) { |
||
235 | llResetScript(); |
||
236 | } |
||
237 | } |
||
238 | } |
||
239 | |||
240 | state listen_group { |
||
241 | state_entry() { |
||
242 | // DEBUG |
||
243 | llOwnerSay("[Fortune] Waiting for group messages..."); |
||
244 | } |
||
245 | link_message(integer sender, integer num, string message, key id) { |
||
246 | // We only care about notifications now. |
||
247 | if(id != "notification") |
||
248 | return; |
||
249 | |||
250 | // This script only processes group notifications. |
||
251 | if(wasKeyValueGet("type", message) != "group") |
||
252 | return; |
||
253 | |||
254 | // Get the sent message. |
||
255 | data = wasURLUnescape( |
||
256 | wasKeyValueGet( |
||
257 | "message", |
||
258 | message |
||
259 | ) |
||
260 | ); |
||
261 | |||
11 | office | 262 | // Check if this is an eggdrop command. |
8 | office | 263 | if(llGetSubString(data, 0, 0) != |
264 | wasKeyValueGet("command", configuration)) |
||
265 | return; |
||
11 | office | 266 | |
267 | // Check if the command matches the current module. |
||
15 | office | 268 | list command = llParseString2List(data, [" "], []); |
269 | if(llList2String(command, 0) != |
||
270 | wasKeyValueGet("command", configuration) + "fortune") |
||
8 | office | 271 | return; |
15 | office | 272 | |
273 | // Remove command. |
||
274 | command = llDeleteSubList(command, 0, 0); |
||
8 | office | 275 | |
276 | data = llList2String( |
||
277 | nList, |
||
278 | (integer) |
||
279 | llFrand( |
||
280 | llGetListLength(nList) |
||
281 | ) |
||
282 | ); |
||
283 | |||
284 | state tell; |
||
285 | } |
||
286 | on_rez(integer num) { |
||
287 | llResetScript(); |
||
288 | } |
||
289 | changed(integer change) { |
||
290 | if((change & CHANGED_INVENTORY) || |
||
291 | (change & CHANGED_REGION_START) || |
||
292 | (change & CHANGED_OWNER)) { |
||
293 | llResetScript(); |
||
294 | } |
||
295 | } |
||
296 | } |
||
297 | |||
298 | state tell { |
||
299 | state_entry() { |
||
300 | // DEBUG |
||
301 | llOwnerSay("[Fortune] Sending to group."); |
||
302 | llInstantMessage( |
||
303 | wasKeyValueGet( |
||
304 | "corrade", |
||
305 | configuration |
||
306 | ), |
||
307 | wasKeyValueEncode( |
||
308 | [ |
||
309 | "command", "tell", |
||
310 | "group", wasURLEscape( |
||
311 | wasKeyValueGet( |
||
312 | "group", |
||
313 | configuration |
||
314 | ) |
||
315 | ), |
||
316 | "password", wasURLEscape( |
||
317 | wasKeyValueGet( |
||
318 | "password", |
||
319 | configuration |
||
320 | ) |
||
321 | ), |
||
322 | "entity", "group", |
||
323 | "message", wasURLEscape(data) |
||
324 | ] |
||
325 | ) |
||
326 | ); |
||
327 | state listen_group; |
||
328 | } |
||
329 | } |