SecondLifeのTwitterクライアントを書いた

使い方は、以下のような感じ。
/1 id password で設定。1回だけ設定すればよい。
/2 statusでtwitterのstatusの更新

クライアントサイド

//----------------------------------------------
// Constants/Global variables
//----------------------------------------------
key requestId;
string API_BASE = "http://xxx/";

//----------------------------------------------
// Functions
//----------------------------------------------
update_status(string status)
{
   string url =  API_BASE + "update" + "?status=" + llEscapeURL(status);
   llWhisper(0, url);
   requestId = llHTTPRequest(url,[HTTP_METHOD,"GET"],"");
}

setup_account(string id, string password) {
    string url =  API_BASE + "setup" +"?id=" + id + "&password=" + password;
    llWhisper(0, url);
    requestId = llHTTPRequest(url,[HTTP_METHOD,"GET"],"");
}

help()
{
    llWhisper(0,"Type '/1 id password'");
    llWhisper(0,"Type '/2 status'");
}

//----------------------------------------------
// Main
//----------------------------------------------
default
{
    state_entry()
    {
        llSetText("Twitter Client",<255,255,255>,1);
        llListen(1,"",NULL_KEY,"");
        llListen(2,"",NULL_KEY,"");
    }
   
    listen(integer channel, string name, key id, string message)
    {
            
        if(channel == 1) {
           list id_and_password = llParseString2List(message,[" "],[]);
           string id = llList2String(id_and_password,0);
           string passsword = llList2String(id_and_password,1);
           setup_account(id, passsword);

        } else if (channel == 2) {
            update_status(message);

        }
    }

    http_response(key request_id, integer status, list metadata, string body)
    {
        if (request_id == requestId)
        {
            // XXX
        }

    }
}

サーバーサイド

Perl+Catalyst

package SecondLife2Twitter::Controller::Root;

use strict;
use warnings;
use base 'Catalyst::Controller';

use YAML;
use Net::Twitter;
use File::Spec;
use File::HomeDir;


__PACKAGE__->config->{namespace} = '';

sub default : Private {
    my ( $self, $c ) = @_;

    # Hello World
    $c->response->body( $c->welcome_message );
}

sub setup : Local {
    my ( $self, $c ) = @_;

    my $id = $c->req->param('id');
    my $password = $c->req->param('password');
    setup_config($id, $password);

}

sub update : Local {
    my ( $self, $c ) = @_;

    my $status = $c->req->param('status');
    update_status($status);
}

sub setup_config {
    my ($id, $password) = @_;
    my $path   = File::Spec->catfile(File::HomeDir->my_home, ".twitter");
    my $config = eval { YAML::LoadFile($path) } || {};

    $config->{username} = $id;
    $config->{password} = $password;

    save_config($path, $config);
}

sub save_config {
    my ($path, $config ) =@_;
    YAML::DumpFile($path, $config);
    chmod 0600, $path;
}

sub update_status {
    my ($status) = @_;

    my $path   = File::Spec->catfile(File::HomeDir->my_home, ".twitter");
    my $config = eval { YAML::LoadFile($path) } || {};
    my $twitter   =  Net::Twitter->new(
        username  => $config->{username},
        password  => $config->{password},
    );

    $twitter->update($status);
}