Skip to content

Instantly share code, notes, and snippets.

@jmora
Created August 22, 2009 22:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmora/173013 to your computer and use it in GitHub Desktop.
Save jmora/173013 to your computer and use it in GitHub Desktop.
This is a Ubiquity (firefox extension) command to post to identi.ca, it's based (it's pretty much the same) in the twitter command available in Ubiquity by default.
const IDENTICA_STATUS_MAXLEN = 140;
var noun_type_identica_user = {
label: "user",
rankLast: true,
noExternalCalls: true,
suggest: function nt_idcauser_suggest(text, html, cb, selected) {
// reject text from selection.
if (!text || selected)
return [];
var foundAt = (text[0] == '@');
if (foundAt) text = text.slice(1); // strip off the @
var suggs = CmdUtils.grepSuggs(text, this.logins);
// only letters and numbers, up to 64 characters are allowed as identica
// usernames.
if (/^[a-z0-9]{1,64}$/.test(text))
suggs.push(CmdUtils.makeSugg(text, text, {}, 0.5));
if (foundAt)
suggs = [{__proto__:s,
text: '@' + s.text,
html: '@' + s.html,
summary: '@' + s.summary,
score: Math.pow(s.score,0.8)}
for each (s in suggs)];
return suggs;
},
logins: function nt_idcauser_logins(reload) {
// TODO: figure out how often to clear this list cache.
if (this._list && !reload) return this._list;
var list = [];
var token = (Cc["@mozilla.org/security/pk11tokendb;1"]
.getService(Ci.nsIPK11TokenDB)
.getInternalKeyToken());
if (!token.needsLogin() || token.isLoggedIn()) {
// Look for identi.ca usernames stored in password manager
var usersFound = {};
var passwordManager = (Cc["@mozilla.org/login-manager;1"]
.getService(Ci.nsILoginManager));
var urls = ["https://identi.ca", "http://identi.ca"];
for (url in urls) {
var logins = passwordManager.findLogins({}, url, "", "");
for (login in logins) {
var x = logins[login];
var {username} = login;
if (username in usersFound) continue;
usersFound[username] = true;
list.push(CmdUtils.makeSugg(username, null, login));
}
}
}
return this._list = list;
},
_list: null,
};
// based on twitter functionality from social.js
// using: http://laconi.ca/trac/wiki/API http://laconi.ca/trac/wiki/TwitterCompatibleAPI
// TODO should there also be a "share" overlord verb with
// providers "using twitter", "using digg", etc.
CmdUtils.CreateCommand({
names: ["identi.ca", "identica", "share using identi.ca"],
arguments: [
{role: "object", label: 'status', nountype: noun_arb_text},
{role: "alias", nountype: noun_type_identica_user}
],
icon: "http://identi.ca/favicon.ico",
description:
"Sets your Identi.ca status to a message of at most 160 characters.",
help: ("You'll need a <a href=\"http://identi.ca\">Identi.ca account</a>," +
" obviously. If you're not already logged in" +
" you'll be asked to log in."),
author: {name:"jmora"},
homepage: "http://identi.ca/jmora",
license: "MPL1.1+ || GPL2.0+ || LGPL2.1+",
preview: function(previewBlock, args) {
var statusText = (args.object ? args.object.text : '');
var usernameText = "";
if (args.alias) {
usernameText = args.alias.text;
} else if (args.as) {
usernameText = args.as.text;
}
if (usernameText[0] == '@')
usernameText = usernameText.slice(1);
var previewTemplate = (
"<div class='identica'>"+_("Updates your Identi.ca status ${username} to:")+"<br/>" +
"<b class='status'>${status}</b><br/><br/>" +
_("Characters remaining: <b>${chars}</b>") +
"<p><small>"+_("tip: If you know how to use the twitter command this is the same.")+"</small></p></div>");
var truncateTemplate = (
"<strong>"+_("The last <b>${truncate}</b> characters will be truncated!")+"</strong>");
var previewData = {
status: <>{statusText}</>.toXMLString(),
username: usernameText && _("(For user <b>${usernameText}</b>)"),
chars: IDENTICA_STATUS_MAXLEN - statusText.length
};
var previewHTML = CmdUtils.renderTemplate(
CmdUtils.renderTemplate(previewTemplate, previewData),
{usernameText:usernameText});
if (previewData.chars < 0) {
var truncateData = {
truncate: 0 - previewData.chars
};
previewHTML += CmdUtils.renderTemplate(truncateTemplate, truncateData);
}
previewBlock.innerHTML = previewHTML;
},
execute: function(args) {
var statusText = args.object.text;
if(statusText.length < 1) {
this._show(_("requires a status to be entered"));
return;
}
var updateUrl = "https://identi.ca/api/statuses/update.json";
var updateParams = {
source: "ubiquity",
status: statusText
//dont cut the input since sometimes, the user selects a big url,
//and the total lenght is more than 140, but tinyurl takes care of that
};
var me = this;
function sendMessage() {
jQuery.ajax({
type: "POST",
url: updateUrl,
data: updateParams,
dataType: "json",
error: function() {
me._show(_("error - status not updated"));
},
success: function() {
me._show(/^d /.test(statusText)
? _("direct message sent")
: _("status updated"));
},
username: login.username,
password: login.password
});
}
var login;
var alias = args.alias;
if (alias && alias.text && alias.data) {
login = alias.data;
sendMessage();
} else {
login = {username: null,
password: null};
if (alias && alias.text)
login.username = alias.text;
sendMessage();
}
},
_show: function(txt){
displayMessage({icon: this.icon, title: this.name, text: txt});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment