// Twitterbox v0.1 // Post to Twitter and receive updates from within SL // Ordinal Malaprop // 2007-02-26 // Free for distribution and use, but if you use it in something else // I would like at least a mention. //------------------------------------------------------------------ // Edit these to your own specifications // The email address you signed up to Twitter with string EMAIL = ""; // Your Twitter password string PASS = ""; // This is the URL of the intermediary script. Don't change it unless // you are using an intermediary of your own. string TWITTERPING = "http://ordinalmalaprop.com/twitter/control.php"; // Leave these alone. integer gTime = 0; integer gCode = 0; string gScreenName = ""; integer gManual = FALSE; //------------------------------------------------------------------ key twitter_send(string action, string subject) { if (EMAIL == "" || PASS == "") { llOwnerSay("No email/password set - edit script and try again"); return NULL_KEY; } else return llHTTPRequest(TWITTERPING, [HTTP_METHOD, "POST"], EMAIL + "\n" + PASS + "\n" + action + "\n" + subject); } menu() { llDialog(llGetOwner(), "Please select an option", ["Check Now", "Web", "Help", "About", "Cancel"], 282); } help() { if (EMAIL == "" || PASS == "") { llOwnerSay( "BEWARE! You have not configured an email and password! " + "Open the object, open the TwitterBox script, and fill in " + "the variables at the top."); } llOwnerSay( "To send a tweet, say '/282 ', or touch the TwitterBox " + "HUD for more options."); } about() { llOwnerSay("A simple SL client for Twitter - http://twitter.com/ " + "- by Ordinal Malaprop"); llOwnerSay( "TwitterBox will automatically check for new tweets from your " + "friends every minute, or when you tell it to manually from " + "the menu, obtainable by touching the HUD."); llOwnerSay( "To use, you need to have registered with Twitter, and edited " + "the script to include your email address and password."); llOwnerSay( "For more information or the latest version, visit " + "http://ordinalmalaprop.com/twitter/"); } //------------------------------------------------------------------ default { state_entry() { llOwnerSay("Initialising..."); // At the start, get the user's screen name and ID number twitter_send("get id", ""); // reset the clock to now gTime = llGetUnixTime(); // and set up a regular check llSetTimerEvent(60.0); // and also start listening for commands llListen(282, "", llGetOwner(), ""); llOwnerSay("Done."); help(); } changed(integer change) { if (change & CHANGED_OWNER) { llResetScript(); } } attach(key id) { // Don't do anything if taking it off! if (id == NULL_KEY) return; // Get screen name again whenever it is attached, // as this may change twitter_send("get id", ""); // Also display the help message help(); } timer() { // The regular check for new tweets twitter_send("check", ""); } touch_start(integer n) { // On owner touch, launch a control menu if (llDetectedKey(0) != llGetOwner()) return; menu(); } listen(integer c, string name, key id, string msg) { if (msg == "Check Now") { llOwnerSay("Checking latest entries..."); gManual = TRUE; twitter_send("check", ""); llSetTimerEvent(60.0); } else if (msg == "Web") { llLoadURL(id, "Visit your own Twitter page", "http://twitter.com/" + gScreenName); } else if (msg == "Help") { help(); } else if (msg == "About") { about(); } else if (msg != "Cancel") { llOwnerSay("Twittering..."); twitter_send("update", msg); } } http_response(key id, integer status, list metadata, string body) { if (llGetSubString(body, 0, 1) == "OK") { // A success list lines = llParseString2List(body, ["\n"], []); string firstLine = llList2String(lines, 0); if (llGetListLength(lines) == 1) { // A successful update, or an ID check list bits = llParseString2List(firstLine, [","], []); gCode = llList2Integer(bits, 1); gScreenName = llList2String(bits, 2); if (llList2String(bits, 3) == "posted") { llOwnerSay("Successfully Twittered!"); llPlaySound("c923f3d9-83a6-99dc-9b7d-bbcdb3c30789", 1.0); } } else { integer f = 1; integer maxTime = gTime; do { string user = llList2String(lines, f); integer time = llList2Integer(lines, f + 2); if (user != gScreenName && time > gTime) { if (maxTime == gTime) { // First new tweet, play sound llPlaySound("c923f3d9-83a6-99dc-9b7d-bbcdb3c30789", 1.0); } llOwnerSay(llList2String(lines, f) + ": " + llList2String(lines, f + 1)); maxTime = time; } f += 3; } while (f < llGetListLength(lines)); if (maxTime == gTime) { if (gManual) { llOwnerSay("No new entries"); gManual = FALSE; } } else { gTime = maxTime; } } } else { if (body == "") { // SL sends back a blank entry if it can't get in touch body = "Could not contact intermediary server " + TWITTERPING; } else llOwnerSay("Error " + (string)status + " - " + body); } } }