mailing_list_notecard.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 mailcard = "";
string mailcontent = "";
 
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 wait;
        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 wait {
    state_entry() {
        llAllowInventoryDrop(TRUE);
    }
    changed(integer change) {
        if(change & CHANGED_INVENTORY) {
            llAllowInventoryDrop(FALSE);
            state notecard_drop;
        }
    }
    on_rez(integer num) {
        llResetScript();
    }
}
 
state notecard_drop {
    state_entry() {
        integer i = llGetInventoryNumber(INVENTORY_NOTECARD)-1;
        do {
            string name = llGetInventoryName(INVENTORY_NOTECARD, i);
            if(name == "Subscribers") jump continue;
            mailcard = name;
            nLine = 0;
            nQuery = llGetNotecardLine(mailcard, nLine);
            return;
@continue;
        } while(--i>-1);
    }
    dataserver(key id, string data) {
        if(id != nQuery) return;
        if(data == EOF) {
            llRemoveInventory(mailcard);
            state mail;
        }
        if(data == "") jump continue;
        mailcontent += data;
@continue;
        nQuery = llGetNotecardLine(mailcard, ++nLine);
    }
    changed(integer change) {
        if(change & CHANGED_INVENTORY) llResetScript();
    }
    on_rez(integer num) {
        llResetScript();
    }
}
 
state mail {
    state_entry() {
        llMessageLinked(LINK_THIS, llGetListLength(maillist)*20, "progress", NULL_KEY);
        do {
            string address = llList2String(maillist, 0);
            llEmail(address, "[" + CLIENT_MAIL_FILTER + "]: " + mailcard, mailcontent);
            maillist = llDeleteSubList(maillist, 0, 0);
        } while(llGetListLength(maillist));
        llMessageLinked(LINK_THIS, 0, "reset", NULL_KEY);
        llResetScript();
    }
    changed(integer change) {
        if(change & CHANGED_INVENTORY) llResetScript();
    }
    on_rez(integer num) {
        llResetScript();
    }
}