Creative Commons License Machine/Badge Script

float gTouchTimeout = 30.0; // constant
integer gDialogChannel = 85021; // constant
integer gKioskChannel;
integer gKioskListener;
key gOriginalKey = NULL_KEY;
list gParams;
 // user parameters, stride two: [ <param>, <value> ... ]
string gLicenseImage;
string gLicenseURL;
string gLicenseName;
string gLicenseDescription;
string gFullTitle;
string gShortTitle;
integer gListener;
integer gCatalog = FALSE;
float gCatalogRate = 86400.0; // once a day
float gCatalogTime;
addParam(string param, string value)
{
 gParams += [ param, value ];
}
string
getParam(string param)
{
 integer i = llListFindList(gParams, [ param ]);
 if (i < 0) return "";
 
 return llList2String(gParams, i + 1);
}
reciteParam(string param, string prompt)
{
 string value = getParam(param);
 if (value == "") return;
 
 llSay(0, prompt + value);
}
aboutWork()
{
 llSetObjectName(gShortTitle);
 reciteParam("title", "Title: ");
 reciteParam("description", "Description: ");
 reciteParam("tags", "Tags: ");
 reciteParam("format", "Format: ");
 reciteParam("creator", "Creator: ");
 reciteParam("holder", "Copyright Holder: ");
 reciteParam("year", "Copyright Year: ");
 reciteParam("source", "Source URL: ");
 llSetObjectName(gFullTitle);
}
emailCatalog()
{
 string message;
 
 message += "original-key: " + (string)gOriginalKey + "\n";
 message += "current-key: " + (string)llGetKey() + "\n";
 integer n = llGetListLength(gParams);
 integer i;
 for (i = 0; i < n; i += 2) {
 string param = llList2String(gParams, i);
 string value = llList2String(gParams, i + 1);
 message += param + ": " + value + "\n";
 }
 llEmail("ccsl-object@notabene-sl.com", "list", message);
 
 gCatalogTime = llGetGMTclock(); 
}
addToCatalog()
{
 llInstantMessage(llGetOwner(), "Listing in catalog (takes about 20 seconds)...");
 gCatalog = TRUE;
 emailCatalog();
 llInstantMessage(llGetOwner(), "...done.");
}
removeFromCatalog()
{
 llInstantMessage(llGetOwner(), "Removing from catalog (takes about 20 seconds)...");
 gCatalog = FALSE;
 string message;
 
 message += "original-key: " + (string)gOriginalKey + "\n";
 message += "current-key: " + (string)llGetKey() + "\n";
 llEmail("ccsl-object@notabene-sl.com", "unlist", message); 
 llInstantMessage(llGetOwner(), "...done.");
}
setUp()
{
 string commercialUse = getParam("commercial-use");
 string modifications = getParam("modifications");
 
 string license = "by";
 if (commercialUse == "no") { license += "-nc"; }
 if (modifications == "no") { license += "-nd"; }
 if (modifications == "share") { license += "-sa"; }
 
 gLicenseURL = "http://creativecommons.org/licenses/" + license + "/2.5/";
 gLicenseName = "Creative Commons Attribution";
 if (commercialUse == "no") { gLicenseName += "-NonCommercial"; }
 if (modifications == "no") { gLicenseName += "-NoDerivs"; }
 if (modifications == "share") { gLicenseName += "-ShareAlike"; }
 gLicenseName += " 2.5 License";
 
 gLicenseDescription = "This work is licensed under a " + gLicenseName;
 
 gLicenseImage = getParam("image-key");
 gShortTitle = "CC Notice";
 gFullTitle = "Creative Commons Notice";
 string title = getParam("title");
 if (title != "") {
 gFullTitle += " for \"" + title + "\"";
 }
 
 llSetObjectName(gFullTitle);
 llSetObjectDesc(gLicenseDescription);
}
showLicenseImage()
{
 llSetPrimitiveParams([
 PRIM_TEXTURE, 2, gLicenseImage, <1.0, 0.8, 1.0>, <0.0, 0.075, 0.0>, 0.0,
 PRIM_TEXTURE, 4, gLicenseImage, <1.0, 0.8, 1.0>, <0.0, 0.075, 0.0>, 0.0
 ]);
}
showBadgeImage()
{
 llSetPrimitiveParams([
 PRIM_TEXTURE, 2, "badge", <1.0, 1.0, 1.0>, <0.0, 0.0, 0.0>, 0.0,
 PRIM_TEXTURE, 4, "badge", <1.0, 1.0, 1.0>, <0.0, 0.0, 0.0>, 0.0
 ]);
}
default
{
 on_rez(integer ch) {
 if (ch == 0) {
 if (gOriginalKey != NULL_KEY) {
 llOwnerSay("You took a copy of this cc license before it was ready, . Please generate a new one.");
 llDie();
 }
 else {
 // user rez from inventory of prototype
 }
 }
 else {
 // rez from kiosk
 gOriginalKey = llGetKey();
 gKioskChannel = ch; 
 llListen(gKioskChannel, "", NULL_KEY, "");
 llSay(gKioskChannel, "go");
 }
 }
 
 listen(integer ch, string who, key id, string what) {
 integer i = llSubStringIndex(what, ":");
 if (i > 0) {
 string param = llGetSubString(what, 0, i - 1);
 string value = llGetSubString(what, i + 1, -1);
 addParam(param, value);
 }
 else {
 if (llGetSubString(what, 0, 3) == "done") {
 vector p = (vector)llGetSubString(what, 5, -1);
 vector s = <0.1, 0.512, 0.180>;
 llSetPrimitiveParams([
 PRIM_SIZE, s,
 PRIM_POSITION, p,
 PRIM_TEXTURE, 2, "badge", <1, 1, 1>, <0, 0, 0>, 0.0,
 PRIM_TEXTURE, 4, "badge", <1, 1, 1>, <0, 0, 0>, 0.0
 ]);
 setUp();
 state ready;
 }
 }
 }
 
 touch_start(integer n) { }
}
state ready
{
 state_entry() {
 llSay(0, "Take a copy of this badge, then attach it to the work.");
 if (gKioskChannel != 0) {
 gKioskListener = llListen(gKioskChannel, "", NULL_KEY, "");
 }
 }
 on_rez(integer n) {
 if (gKioskChannel != 0) {
 llListenRemove(gKioskListener);
 gKioskChannel = 0;
 }
 } 
 
 state_exit() {
 llSetTimerEvent(0.0);
 }
 
 touch_start(integer n) {
 showLicenseImage();
 gListener = llListen(gDialogChannel, "", NULL_KEY, "");
 
 integer i;
 for (i = 0; i < n; ++i) {
 key id = llDetectedKey(i);
 
 string message = 
 gLicenseDescription
 + "\n\nDeed: go to deed page on web"
 + "\nAbout: speak details about the work";
 list buttons = [ "Deed", "About" ];
 
 if (id == llGetOwner()) {
 if (!gCatalog) {
 message += "\nList: add object to public catalog at http://www.notabene-sl.com/cc";
 buttons += [ "List" ];
 }
 else {
 message += "\nUnlist: remove item from public catalog at http://www.notabene-sl.com/cc";
 buttons += [ "Unlist" ];
 }
 }
 
 llDialog(id, message, buttons, gDialogChannel);
 }
 llSetTimerEvent(gTouchTimeout);
 }
 
 listen(integer ch, string who, key id, string what) {
 if (what == "Deed") {
 llLoadURL(id, gLicenseName, gLicenseURL);
 }
 else if (what == "About") {
 aboutWork();
 }
 else if (what == "die") {
 llDie();
 }
 else if (id == llGetOwner()) {
 if (what == "List") {
 addToCatalog();
 }
 else if (what == "Unlist") {
 removeFromCatalog();
 }
 }
 }
 
 timer() {
 showBadgeImage();
 llListenRemove(gListener);
 
 if (gCatalog) {
 if ((llGetGMTclock() - gCatalogTime) >= gCatalogRate) {
 emailCatalog();
 }
 llSetTimerEvent(gCatalogRate);
 }
 else {
 llSetTimerEvent(0.0);
 }
 }
}