Playing around with the TinyPaste.com API, I found a way to store and retrieve information from TinyPaste.com using Linden Scripting Language (LSL) in Second Life(r)
[tpcode]// Title: TinyPaste API Calls via LSL
// Author: Lewis Moten (inSL: Dedric Mauriac)
// Blog: http://dedricmauriac.wordpress.com
//
// This script permits you to store and retrieve
// information from tinypaste.com
// It will also give you a link to the information
// to view in your web browser
//
// API: http://tinypaste.com/api/
// Terms/Privacy: http://tinypaste.com/other.php
//
// NOTE: LSL does not have methods for encoding/decoding HTML.
// While this script attempts to encode the most common symbols,
// tinypaste.com appears to strip them out
//
key getPaste_RequestId;
key setPaste_RequestId;
getPaste_Begin(string id)
{
getPaste_RequestId = executeMethod("getPaste", [id]);
}
setPaste_Begin(string text)
{
setPaste_RequestId = executeMethod("setPaste", [text]);
}
string getPaste_End(string xml)
{
return getFirstStringResponse(xml);
}
string setPaste_End(string xml)
{
return getFirstStringResponse(xml);
}
string escapeHTML(string text)
{
// escape ampersand
text = llDumpList2String(llParseStringKeepNulls(text, ["&"], []), "&");
// escape less than
text = llDumpList2String(llParseStringKeepNulls(text, ["<"], []), "<");
// escape greater than
text = llDumpList2String(llParseStringKeepNulls(text, [">"], []), ">");
// escape double-quote
text = llDumpList2String(llParseStringKeepNulls(text, ["\""], []), """);
return text;
}
string unescapeHTML(string text)
{
// unescape double-quote
text = llDumpList2String(llParseStringKeepNulls(text, ["""], []), "\"");
// unescape greater than
text = llDumpList2String(llParseStringKeepNulls(text, [">"], []), ">");
// unescape less than
text = llDumpList2String(llParseStringKeepNulls(text, ["<"], []), "<");
// unescape ampersand
text = llDumpList2String(llParseStringKeepNulls(text, ["&"], []), "&");
return text;
}
key executeMethod(string methodName, list params)
{
integer i;
// count parameters
integer count = llGetListLength(params);
// define xml-rpc call
string xml = "<?xml version=\"1.0\"?><methodCall><methodName>tinypaste." + methodName + "</methodName><params>";
// loop through each parameter
for(i = 0; i < count; i++)
// add parameter to xml-rpc call
xml += "<param><value><string>"
+ escapeHTML(llList2String(params, i))
+ "</string></value></param>";
// close xml-rpc call
xml += "</params></methodCall>";
// setup xml-rpc end-point
string url = "http://tinypaste.com/api/xml-rpc/xml-rpc-server.php";
// execute the xml-rpc call
return llHTTPRequest(url, [HTTP_METHOD, "POST"], xml);
}
string getFirstStringResponse(string xml)
{
// find where text begins
integer i = llSubStringIndex(xml, "<string>");
if(i == -1) return "Error";
// cut off xml before paste begins
xml = llGetSubString(xml, i + 8, -1);
// find where text ends
i = llSubStringIndex(xml, "</string>");
if(i == -1) return "Error";
// cut off xml after paste ends
xml = llGetSubString(xml, 0, --i);
// decode HTML characters and return results
return unescapeHTML(xml);
}
default
{
state_entry()
{
// paste the text, "Hello World"
setPaste_Begin("Hello World");
}
http_response(key request_id, integer status, list metadata, string body)
{
if(request_id == getPaste_RequestId)
{
// parse and write the text received
llSay(PUBLIC_CHANNEL, getPaste_End(body));
}
else if(request_id == setPaste_RequestId)
{
// parse the id from the response
string id = setPaste_End(body);
// show link where text was pasted
llSay(PUBLIC_CHANNEL, "Your paste is located at http://tinypaste.com/" + id);
// fetch the text
getPaste_Begin(id);
}
}
}
[/tpcode]