#! /usr/bin/perl
#
# Copyright © 2009 Sebastian "tokkee" Harl
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holders nor the names of their
#    contributors may be used to endorse or promote products derived from this
#    software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
# IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Author:
#   Sebastian "tokkee" Harl <sh@tokkee.org>

use strict;
use warnings;

use utf8;
use open ':locale';

use Term::ANSIColor;

use Net::Twitter::Lite;

my $conf_file = "$ENV{'HOME'}/.identica.conf";

my ($fh, $nt);
my %id;

# This file should fill the %id hash.
#
# See <http://search.cpan.org/~mmims/Net-Twitter-Lite-0.08000/lib/Net/
# Twitter/Lite.pm#METHODS_AND_ARGUMENTS> for details about which parameters
# are available.
if (! open($fh, '<', $conf_file)) {
	print_err("Failed to open config file ($conf_file): $!\n");
	exit 1;
}

{
	my @conf = <$fh>;
	eval join '', @conf;
}

close($fh);

if (! $id{'identica'}) {
	print_err('id{identica} has not been set - '
		. 'Twitter is not currently supported!' . "\n");
	exit 1;
}

$nt = Net::Twitter::Lite->new(%id);

if (! $nt) {
	print_err("Failed to create Net::Twitter::Lite object: $!\n");
	exit 1;
}

while (42) {
	my $msg = "";
	my $status;
	my $err;

	print "identi.ca> ", ' ' x 140, '←', "\r";
	print color 'bold';
	print "identi.ca> ";
	print color 'reset';

	$msg = <STDIN>;

	if (! defined($msg)) {
		last;
	}
	elsif ($msg =~ m/^\?(\?)?(\d+)?$/) {
		my $friends = $1;
		my $count = $2;
		my $posts;

		if (! defined($count)) {
			$count = 5;
		}
			
		eval {
			if (defined $friends) {
				$posts = $nt->friends_timeline({count => $count});
			}
			else {
				$posts = $nt->user_timeline({count => $count});
			}
		};

		if ($err = $@) {
			print_err("Failed to get ``tweeds'': $err\n");
			next;
		}

		foreach my $post (@$posts) {
			my $text   = $post->{'text'};
			my $source = $post->{'source'};

			utf8::upgrade($text);

			if ($source =~ m/^<a href="[^"]+">(.*)<\/a>$/) {
				$source = $1;
			}

			print $post->{'created_at'} . ' (';
			print color 'bold blue';
			print $post->{'user'}->{'screen_name'};
			print color 'reset';
			print ", $source): ";
			print color 'dark yellow';
			print "$text\n";
			print color 'reset';
		}
		next;
	}

	eval {
		$status = $nt->update({ status => $msg });
	};

	if ($err = $@) {
		print_err("Failed to send message: $err\n");
		next;
	}

	print color 'bold green';
	print 'http://identi.ca/notice/' . $status->{'id'} . "\n";
	print color 'reset';
}

print color 'bold';
print "\nBye!\n";
print color 'reset';

sub print_err {
	print STDERR color 'bold red';
	print STDERR join(' ', @_);
	print STDERR color 'reset';
}

# vim: set noexpandtab tw=78 sw=4 ts=4 :

