Skip to content

Instantly share code, notes, and snippets.

/x.js

Created August 20, 2009 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/171102 to your computer and use it in GitHub Desktop.
Save anonymous/171102 to your computer and use it in GitHub Desktop.
/* This is a template command. */
CmdUtils.CreateCommand({
names: ["message"],
icon: "http://developer.pidgin.im/static/pidgin.ico",
description: "Send a message to one of your pidgin contacts",
help: "Supported formats are:<br> (empty) - just text<br/> as link - hyperlink using the document title<br/> as quote - hyperlink using the current selection / text",
author: {name: "Rene Koecher", email: "shirk@bitspin.org"},
license: "GPL",
homepage: "http://labs.mozilla.com/",
arguments: [
{ role : 'object', nountype: noun_arb_text , label: 'message'},
{ role : 'goal' , nountype: noun_type_contact, label: 'screenname' },
{ role : 'instrument', nountype: noun_arb_text, label: 'protocol'},
{ role : 'alias', nountype: noun_arb_text, label: '[(empty), link, quote]' }],
/*! shared nsILocalFile instance holding the path to purple-remote */
_purple_remote : '',
/**
* Try to locate a usable purple-remote helper.
* On success this will set this._purple_remote.
*/
_findPurpleRemote : function() {
var purple_locations = '/usr/bin /usr/local/bin'.split(' ');
var executable = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
executable.followLinks = true;
if (this._purple_remote) {
return this._purple_remote;
}
/* FIXME: provide at least some cross platform support */
if (Utils.OS == 'WINNT') {
displayMessage(_('Sorry, but your OS is not supported in this version.'));
return NULL;
}
for (path in purple_locations) {
//Utils.log('Trying path: ' + purple_locations[path]);
executable.initWithPath(purple_locations[path] + '/purple-remote');
if (executable.exists() && executable.isExecutable()) {
displayMessage(_('Found purple-helper in ') + purple_locations[path]);
this._purple_remote = executable;
return this._purple_remote;
}
}
return false;
},
/**
* Run a purple command (no output parsing right now)
* @param command - a valid libpurple command
* @param args - a key -> value array containing option arguments
*/
_runPurpleCommand : function(command, args) {
if (!this._purple_remote && !this._findPurpleRemote())
return NULL;
var process = Components.classes["@mozilla.org/process/util;1"]
.createInstance(Components.interfaces.nsIProcess);
var arg_str = '';
var arg_array = new Array('');
if (args)
arg_str = Utils.paramsToString(args);
arg_array[0] = command + arg_str;
process.init(this._purple_remote);
//Utils.log('purple-call: ' + arg_array);
return process.run(false, arg_array, arg_array.length);
},
/**
* Try to send a message to one of your buddies
* @param protocol - messaging protocol to use (e.g. jabber)
* @param buddy - qualified buddy id
* @param message - the message body
*/
_sendPurpleMessage : function(protocol, buddy, message) {
var purple_args = { 'screenname' : buddy, 'message' : message };
var purple_cmd = protocol + ':goim';
if (!this._runPurpleCommand(purple_cmd, purple_args)) {
displayMessage(_('Sorry, something went wrong!'));
}
},
preview: function preview(pblock, args) {
var preview = '';
if (args.instrument.text) {
preview += '[' + args.instrument.text + '] ';
} else {
preview += '[jabber] ';
}
if (args.goal.text) {
preview += ' to ' + args.goal.text + ' ';
} else {
preview += ' to ??? ';
}
if (args.alias.text) {
if (args.alias.text == 'link') {
preview += '<a href="' + CmdUtils.getWindow().location + '">' + CmdUtils.getDocument().title + '</a>';
} else if (args.alias.text == 'quote') {
preview += '<a href="' + CmdUtils.getWindow().location + '">' + args.object.html + '</a>';
} else {
preview += '(Don\'t know how to format as "' + args.alias.text + '")';
}
} else {
preview += args.object.html;
}
//pblock.innerHTML = '[' + args.instrument.text + '] to <i>' + args.goal.text + '</i> : <b>' + args.object.html + '</b>';
pblock.innerHTML = preview;
},
execute: function execute(args) {
var protocol = 'jabber';
var message = '';
if (args.instrument.text)
protocol = args.instrument.text;
if (!args.goal.text) {
displayMessage(_('I don\'t know who to send this message to!'));
return;
}
if (args.alias.text) {
if (args.alias.text == 'link') {
message = '<a href="' + CmdUtils.getWindow().location + '">' + CmdUtils.getDocument().title + '</a>';
} else if (args.alias.text == 'quote') {
message = '<a href="' + CmdUtils.getWindow().location + '">' + args.object.html + '</a>';
} else {
displayMessage(_('(Don\'t know how to format as "' + args.alias.text + '")'));
return;
}
} else {
message = args.object.html;
}
this._sendPurpleMessage(protocol, args.goal.text, message);
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment