Twitter on irssi

Update: Twitter changed its authentication scheme so this plugin no longer works.

A while ago, I wondered why I had a Twitter account. I didn't use it, and everything interesting I was doing I put in my away message in irssi (with, in addition to IRC, my IM accounts, thanks to BitlBee).

So I thought, why not Twitter my away messages? I made a Perl script for irssi to do just that.

use strict;
use warnings;
use vars qw($VERSION %IRSSI);
use Irssi;
use WWW::Curl::Easy;
 
$VERSION = '1.00';
%IRSSI = (
	authors     => 'Strager Neds',
	contact     => 'strager.nds@gmail.com',
	name        => '/away twitter',
	description => 'Fuck descriptions',
	license     => 'WTFPL',
);
 
sub setTwitterStatus {
	# $_ is the message ... dunno how to do a substitute without $_.  Perl's gay.
	$_ = shift;
 
	# Taken from http://support.internetconnection.net/CODE_LIBRARY/Perl_URL_Encode_and_Decode.shtml
	s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
 
	my $newStatus = $_;
 
	$SIG{CHLD} = 'IGNORE';
 
	# We fork so we don't freeze irssi.
	if(fork() == 0) {
		close(STDOUT);
		close(STDIN);
		close(STDERR);
 
		my $curl = new WWW::Curl::Easy;
 
		open(my $curlOutput, '>', '/dev/null');
		open(my $curlInput, '<', '/dev/null');
		open(my $curlStderr, '>', '/dev/null');
 
		$curl->setopt(CURLOPT_WRITEDATA, $curlOutput);
		$curl->setopt(CURLOPT_READDATA, $curlInput);
		$curl->setopt(CURLOPT_STDERR, $curlStderr);
 
		$curl->setopt(CURLOPT_NOPROGRESS, 1);
 
		$curl->setopt(CURLOPT_URL, 'http://twitter.com/statuses/update.xml');
		$curl->setopt(CURLOPT_USERPWD, Irssi::settings_get_str('twitter_username') . ':' . Irssi::settings_get_str('twitter_password'));
		$curl->setopt(CURLOPT_POST, 1);
		$curl->setopt(CURLOPT_POSTFIELDS, 'status=' . $newStatus);
 
		my $curlStatus = $curl->perform;
 
		close($curlOutput);
		close($curlInput);
		close($curlStderr);
 
		if($curlStatus != 0) {
			# Do something here.
		}
 
		exit(0);
	}
}
 
Irssi::command_bind 'away' => sub {
	my($data, $server, $windowItem) = @_;
 
	if(!($data =~ /./)) {
		$data = Irssi::settings_get_str('twitter_notawaytext');
	}
 
	if(!($data =~ /./)) {
		return;
	}
 
	setTwitterStatus($data);
};
 
Irssi::settings_add_str('twitter', 'twitter_username', '');
Irssi::settings_add_str('twitter', 'twitter_password', '');
Irssi::settings_add_str('twitter', 'twitter_notawaytext', '');
 
Irssi::theme_register([
	'away_hilight_notice_loaded', '%R>>%n %_Scriptinfo:%_ Loaded $0 version $1 by $2.'
]);

Dump the code into awaytwit.pl in ~/irssi/plugins/. In irssi, type /script load awaytwit. Configure using /set twitter.

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
By submitting this form, you accept the Mollom privacy policy.