mailing_list_email.lsl
///////////////////////////////////////////////////////////////////////////
//  Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3      //
//  Please see: http://www.gnu.org/licenses/gpl.html for legal details,  //
//  rights of fair usage, the disclaimer and warranty conditions.        //
///////////////////////////////////////////////////////////////////////////
 
///////////////////////////////////////////////////////////////////////////
//                               CONFIGURATION                           //
///////////////////////////////////////////////////////////////////////////
 
string CLIENT_MAIL_FILTER = "VIBE Gridmasters";
 
///////////////////////////////////////////////////////////////////////////
//                                 INTERNALS                             //
///////////////////////////////////////////////////////////////////////////
key nQuery = NULL_KEY;
integer nLine = 0;
list maillist = [];
 
string mailcontent = "";
string mailsubject = "";
 
default
{
    state_entry() {
        if(llGetInventoryType("Subscribers") != INVENTORY_NOTECARD) {
            llSay(DEBUG_CHANNEL, "Failed to find a notecard named Subscribers in the primitive's inventory.");
            return;
        }
        nQuery = llGetNotecardLine("Subscribers", nLine);
    }
    dataserver(key id, string data) {
        if(id != nQuery) return;
        if(data == EOF) state process_queue;
        if(data == "") jump continue;
        maillist += data;
@continue;
        nQuery = llGetNotecardLine("Subscribers", ++nLine);
    }
    changed(integer change) {
        if(change & CHANGED_INVENTORY) llResetScript();
    }
    on_rez(integer num) {
        llResetScript();
    }
}
 
state process_queue {
    state_entry() {
        llSetTimerEvent(1);
    }
 
    email(string time, string address, string subject, string message, integer num_left) {
        if (llGetSubString(address, -19, -1) == "@lsl.secondlife.com") return;
        mailcontent = message;
        mailsubject = subject;
        state post;
    }
    timer() {
        llGetNextEmail("", "");
    }
    changed(integer change) {
        if(change & CHANGED_INVENTORY) llResetScript();
    }
    on_rez(integer num) {
        llResetScript();
    }
}
 
state post {
    state_entry() {
        llMessageLinked(LINK_THIS, llGetListLength(maillist)*20, "progress", NULL_KEY);
        integer i = llGetListLength(maillist)-1;
        do {
            string address = llList2String(maillist, i);
            llEmail(address, "[" + CLIENT_MAIL_FILTER + "]: " + mailsubject, mailcontent);
        } while(--i>-1);
        llMessageLinked(LINK_THIS, 0, "reset", NULL_KEY);
        state process_queue;
    }
    changed(integer change) {
        if(change & CHANGED_INVENTORY) llResetScript();
    }
    on_rez(integer num) {
        llResetScript();
    }
}