5e17e7c8020c102fbdbc5777ae6959978cfe0c90
[feisty_meow.git] / huffware / huffotronic_scripts / email_notecards_v6.2.txt
1 
2 // huffware script: email notecards, by fred huffhines.
3 //
4 // this script allows us to export notecards out of the grids.  it reads every notecard
5 // that's dropped into the object, and then emails it to the specified recipient.
6 //
7 // this script is licensed by the GPL v3 which is documented at: http://www.gnu.org/licenses/gpl.html
8 // do not use it in objects without fully realizing you are implicitly accepting that license.
9
10
11 //hmmm: need a command to clean up the inventory...
12 //      among things that are not notecards, we need a way to clean up all inventory
13 //      except the huffotronic updater and this script itself.
14
15
16 // configure these for your own purposes:
17
18 // the list of emails that should receive the notecard content.
19 list EMAIL_LIST_FOR_NOTECARDS = [
20     "fred@gruntose.com"
21 ];
22
23 // global constants...
24
25 integer DEBUGGING = FALSE;  // if true, will make the run a lot noisier.
26
27 float TIMER_PERIOD = 2.0;  // period for the timer to check on any pending notecards.
28
29 // we'll keep our emails at or under this length.
30 integer MAXIMUM_STRING_BUFFER = 3200;
31   // older versions of opensim have email limit of 1024 for sum of subject and body length.
32   // the documented behavior for lsl email is a max of 4096 for sum of subject and body length.
33
34 vector TEXT_COLOR = <0.9, 0.7, 0.5>;  // the color the object's text will be painted with.
35
36 integer OWNER_ONLY = TRUE;  // if true, only the owner can drop things into the object.
37
38 integer PRIVATE_EMAILS = TRUE;  // if true, the names of the recipients are not shown.
39
40 // global variables....
41
42 key current_query_id = NULL_KEY;  // the query ID for the current notecard.
43
44 integer line_number;  // which line are we at in notecard?
45
46 string current_notecard_name;  // we are reading a notecard named this, possibly.
47
48 list pending_notecards;  // notecards that are going to be read eventually.
49
50 integer processing_a_notecard = FALSE;  // are we currently reading and emailing a notecard?
51
52 integer need_to_scan_notecards = FALSE;  // should the list of notecards be re-scanned?
53
54 /////
55 //hmmm: add to hufflets...
56
57 // basic email sending, with some extra info passed along.
58 send_email(string recipient, string subject, string content)
59 {
60     if (DEBUGGING) log_it("before sending an email: " + subject);
61     llEmail(recipient, subject + " [via " + llKey2Name(llGetOwner()) + "]", content);
62     if (DEBUGGING) log_it("after sending an email: " + subject);
63 }
64
65 list __email_buffer;  // contents to send in email should pile up here.
66
67 // sends out all the email that's pending in the __email_buffer variable.
68 send_buffered_email(string subject)
69 {
70     string temp_buffer;  // used to build up the email.
71     integer chunk_number = 1;  // tracks which part of the message this is.
72     integer i;
73     for (i = 0; i < llGetListLength(EMAIL_LIST_FOR_NOTECARDS); i++) {
74         string recip = llList2String(EMAIL_LIST_FOR_NOTECARDS, i);
75         temp_buffer = "";
76         integer j;
77         for (j = 0; j < llGetListLength(__email_buffer); j++) {
78             string line = llList2String(__email_buffer, j);        
79             if (llStringLength(temp_buffer) + llStringLength(line) >= MAXIMUM_STRING_BUFFER) {
80                 // we're over our limit per email.  send out what we have and
81                 // then freshen our buffer.
82                 if (DEBUGGING)
83                     log_it("part " + chunk_number + " is " + llStringLength(temp_buffer) + " bytes.");
84                 send_email(recip, subject + " (part " + chunk_number + ")", temp_buffer);
85                 temp_buffer = "";
86                 chunk_number++;
87             }
88             temp_buffer += llList2String(__email_buffer, j);
89         }
90         if (llStringLength(temp_buffer) > 0) {
91             // send the last piece of the email out.
92             if (DEBUGGING)
93                 log_it("part " + chunk_number + " is " + llStringLength(temp_buffer) + " bytes.");
94             send_email(recip, subject + " (part " + chunk_number + ")", temp_buffer);
95         }
96     }
97     __email_buffer = [];  // clear the buffer now that the email is flying out.
98 }
99 ////// add to hufflets end.
100 ////
101
102
103 // reset any important variables and set up our assets and timers anat.
104 initialize()
105 {
106     current_query_id = NULL_KEY;
107     __email_buffer = [];
108     line_number = 0;
109
110     if (OWNER_ONLY) llAllowInventoryDrop(FALSE);  // only owner can drop.
111     else llAllowInventoryDrop(TRUE);  // allow people to drop things into us.
112
113     // slap a title on the object.
114     string recip_list;
115     integer i;
116     if (!PRIVATE_EMAILS) {
117         for (i = 0; i < llGetListLength(EMAIL_LIST_FOR_NOTECARDS); i++) {
118             recip_list += "\n" + llList2String(EMAIL_LIST_FOR_NOTECARDS, i);
119         }
120     } else recip_list = "private list";
121     string prefix;
122     if (OWNER_ONLY) prefix = llKey2Name(llGetOwner()) + " can ";
123     llSetText(prefix + "drop notecards into me\nto email them to: " + recip_list, TEXT_COLOR, 1.0);
124         
125     // set the timer to processing notecards that are pending.
126     reset_timer(TIMER_PERIOD);
127 }
128
129 //hmmm: add to hufflets?
130 // safely reschedules the timer for a duration specified.
131 // this gets around a gnarly SL bug where the timer stops working if not stopped first.
132 reset_timer(float period)
133 {
134     llSetTimerEvent(0.0);
135     llSetTimerEvent(period);
136 }
137
138 // starts reading the current notecard name, which should have been set elsewhere.
139 consume_notecard()
140 {
141     if (current_notecard_name == "") {
142         log_it("somehow we do not have a notecard to read in consume_notecard.");
143         return;
144     }
145     line_number = 0;
146     __email_buffer = [];
147     current_query_id = llGetNotecardLine(current_notecard_name, 0);    
148 }
149
150 // when the timer goes off, this checks our ongoing processes and kicks them
151 // down the road a bit if they need it.
152 handle_timer_pong()
153 {
154     if (processing_a_notecard) return;  // already busy.
155     if (need_to_scan_notecards) {
156         queue_up_any_new_notecards();
157         need_to_scan_notecards = FALSE;
158     }
159
160     if (llGetListLength(pending_notecards) < 1) return;  // nothing to do.
161     processing_a_notecard = TRUE;
162     current_notecard_name = llList2String(pending_notecards, 0);
163     if (DEBUGGING) log_it("scheduling notecard: " + current_notecard_name);            
164     consume_notecard();
165 }
166
167 // processes the data chunks coming in from our notecard reading.
168 handle_data_arriving(key query_id, string data)
169 {
170     if (query_id != current_query_id) {
171         if (DEBUGGING) log_it("not our query id somehow?");
172         return;
173     }
174     // if we're not at the end of the notecard we're reading...
175     if (data != EOF) {
176         if (!line_number) {
177             if (DEBUGGING) log_it("starting to read notecard " + current_notecard_name + "...");    
178         }
179         // add the line to our destination list.
180         if (is_prefix(data, "From")) data = "#" + data;  // voodoo.
181         __email_buffer += [ data + "\n" ];
182         if (DEBUGGING) log_it("line " + (string)line_number + ": data=" + data);
183         line_number++;  // increase the line count.
184         // request the next line from the notecard.
185         current_query_id = llGetNotecardLine(current_notecard_name, line_number);
186     } else {
187         // no more data, so we're done with this card.
188         current_query_id = NULL_KEY;
189         // blast out the notecard's content in an email.
190         llSay(0, "Sending \"" + current_notecard_name + "\" with " + line_number + " lines.\n");
191         send_buffered_email(current_notecard_name);
192         // chop that name out of our current pending cards too.
193         integer where = llListFindList(pending_notecards, [current_notecard_name]);
194 //log_it("found the notecard to remove at index " + where);        
195         if (where >= 0) {
196             pending_notecards = llDeleteSubList(pending_notecards, where, where);
197         }
198         // done with it, so eat the current note card.
199         llRemoveInventory(current_notecard_name);
200         llSay(0, "Done with notecard \"" + current_notecard_name + "\"; now removing it.");
201         // reset our flag to signal that we're ready to eat another notecard.
202         processing_a_notecard = FALSE;        
203         current_notecard_name = "";
204         // make sure we have all current notecards queued.
205         need_to_scan_notecards = TRUE;
206         // push timer out.
207         reset_timer(TIMER_PERIOD);
208     }
209 }
210
211 // look through our inventory and if there are any notecards we don't know about,
212 // add them to the list for processing.
213 queue_up_any_new_notecards()
214 {
215     integer i;
216     for (i = 0; i < llGetInventoryNumber(INVENTORY_NOTECARD); i++) {
217         string note_name = llGetInventoryName(INVENTORY_NOTECARD, i);
218         // don't add the notecard if the name is already listed.
219         integer where = llListFindList(pending_notecards, [ note_name ]);
220         if (where >= 0) {
221 //            if (DEBUGGING) log_it("notecard already present; skipping: " + note_name);
222         } else {
223             // schedule notecard reading by adding to queue.
224             pending_notecards += [ note_name ];
225 //            if (DEBUGGING) log_it("notecard added to pending: " + note_name);
226         }
227     }
228 }
229
230 //////////////
231
232 // borrowed from hufflets...
233
234 integer debug_num = 0;
235
236 // a debugging output method.  can be disabled entirely in one place.
237 log_it(string to_say)
238 {
239     debug_num++;
240     // tell this to the owner.    
241     llOwnerSay(llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
242     // say this on an unusual channel for chat if it's not intended for general public.
243 //    llSay(108, llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
244     // say this on open chat that anyone can hear.  we take off the bling for this one.
245 //    llSay(0, to_say);
246 }
247
248 //////////////
249
250 // the string processing methods are not case sensitive.
251   
252 // returns TRUE if the "pattern" is found in the "full_string".
253 integer matches_substring(string full_string, string pattern)
254 { return (find_substring(full_string, pattern) >= 0); }
255
256 // returns the index of the first occurrence of "pattern" inside
257 // the "full_string".  if it is not found, then a negative number is returned.
258 integer find_substring(string full_string, string pattern)
259 { return llSubStringIndex(llToLower(full_string), llToLower(pattern)); }
260
261 // returns TRUE if the "prefix" string is the first part of "compare_with".
262 integer is_prefix(string compare_with, string prefix)
263 { return find_substring(compare_with, prefix) == 0; }
264
265 //////////////
266
267 //////////////
268 // huffware script: auto-retire, by fred huffhines, version 2.8.
269 // distributed under BSD-like license.
270 //   !!  keep in mind that this code must be *copied* into another
271 //   !!  script that you wish to add auto-retirement capability to.
272 // when a script has auto_retire in it, it can be dropped into an
273 // object and the most recent version of the script will destroy
274 // all older versions.
275 //
276 // the version numbers are embedded into the script names themselves.
277 // the notation for versions uses a letter 'v', followed by two numbers
278 // in the form "major.minor".
279 // major and minor versions are implicitly considered as a floating point
280 // number that increases with each newer version of the script.  thus,
281 // "hazmap v0.1" might be the first script in the "hazmap" script continuum,
282 // and "hazmap v3.2" is a more recent version.
283 //
284 // example usage of the auto-retirement script:
285 //     default {
286 //         state_entry() {
287 //            auto_retire();  // make sure newest addition is only version of script.
288 //        }
289 //     }
290 // this script is partly based on the self-upgrading scripts from markov brodsky
291 // and jippen faddoul.
292 //////////////
293 auto_retire() {
294     string self = llGetScriptName();  // the name of this script.
295     list split = compute_basename_and_version(self);
296     if (llGetListLength(split) != 2) return;  // nothing to do for this script.
297     string basename = llList2String(split, 0);  // script name with no version attached.
298     string version_string = llList2String(split, 1);  // the version found.
299     integer posn;
300     // find any scripts that match the basename.  they are variants of this script.
301     for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
302         string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
303         if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
304             // found a basic match at least.
305             list inv_split = compute_basename_and_version(curr_script);
306             if (llGetListLength(inv_split) == 2) {
307                 // see if this script is more ancient.
308                 string inv_version_string = llList2String(inv_split, 1);  // the version found.
309                 // must make sure that the retiring script is completely the identical basename;
310                 // just matching in the front doesn't make it a relative.
311                 if ( (llList2String(inv_split, 0) == basename)
312                     && ((float)inv_version_string < (float)version_string) ) {
313                     // remove script with same name from inventory that has inferior version.
314                     llRemoveInventory(curr_script);
315                 }
316             }
317         }
318     }
319 }
320 //
321 // separates the base script name and version number.  used by auto_retire.
322 list compute_basename_and_version(string to_chop_up)
323 {
324     // minimum script name is 2 characters plus a version.
325     integer space_v_posn;
326     // find the last useful space and 'v' combo.
327     for (space_v_posn = llStringLength(to_chop_up) - 3;
328         (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
329         space_v_posn--) {
330         // look for space and v but do nothing else.
331     }
332     if (space_v_posn < 2) return [];  // no space found.
333     // now we zoom through the stuff after our beloved v character and find any evil
334     // space characters, which are most likely from SL having found a duplicate item
335     // name and not so helpfully renamed it for us.
336     integer indy;
337     for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
338         if (llGetSubString(to_chop_up, indy, indy) == " ") {
339             // found one; zap it.  since we're going backwards we don't need to
340             // adjust the loop at all.
341             to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
342         }
343     }
344     string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
345     // ditch the space character for our numerical check.
346     string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
347     // strip out a 'v' if there is one.
348     if (llGetSubString(chop_suffix, 0, 0) == "v")
349         chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
350     // if valid floating point number and greater than zero, that works for our version.
351     string basename = to_chop_up;  // script name with no version attached.
352     if ((float)chop_suffix > 0.0) {
353         // this is a big success right here.
354         basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
355         return [ basename, chop_suffix ];
356     }
357     // seems like we found nothing useful.
358     return [];
359 }
360 //
361 //////////////
362
363 // end from hufflets.
364 //////////////
365
366 default {
367     state_entry() { if (llSubStringIndex(llGetObjectName(), "huffotronic") < 0) state real_default; }
368     on_rez(integer parm) { state rerun; }
369 }
370 state rerun { state_entry() { state default; } }
371
372 state real_default
373 {
374     state_entry() {
375         auto_retire();
376         initialize();
377     }
378     
379     changed(integer mask) {
380         if (mask & (CHANGED_ALLOWED_DROP | CHANGED_INVENTORY)) {
381             need_to_scan_notecards = TRUE;
382             pending_notecards = [];  // reset the list so we re-add all and drop any dead ones.
383             reset_timer(TIMER_PERIOD);
384         }
385     }
386     
387     timer() { handle_timer_pong(); }
388     
389     dataserver(key query_id, string data) { handle_data_arriving(query_id, data); }        
390 }