var info = {
    name        : 'Publish status to Twitter',
    description : 'When status message is changed, forward it to twitter@twitter.com',
    version     : '1.0.5',
    author      : 'Massimiliano Mirra <bard [at] hyperstruct [dot] net>',
    license     : 'GPL2',
    home        : 'http://sameplace.cc'
};


// CONFIGURATION
// ----------------------------------------------------------------------

var recipient = 'twitter@twitter.com';


// STATE
// ----------------------------------------------------------------------

var channel;


// DEFINITIONS
// ----------------------------------------------------------------------

var Cc = Components.classes;
var Ci = Components.interfaces;


function init() {
    channel = XMPP.createChannel();

    channel.on({
        event     : 'presence',
        direction : 'out',
        stanza    : function(s) {
                return s.status != undefined;
            }},
        function(myPresence) {
            if(!isMostRecentWindow())
                return;

            XMPP.send(myPresence.account,
                      <message to={recipient} type="chat">
                      <body>{myPresence.stanza.status.text()}</body>
                      </message>);
        });
}

function finish() {
    channel.release();
}

function isMostRecentWindow() {
    return top == Cc["@mozilla.org/appshell/window-mediator;1"]
        .getService(Ci.nsIWindowMediator)
        .getMostRecentWindow('navigator:browser');
}
