Code

Initial commit.
[identica.pl.git] / identica.pl
1 #! /usr/bin/perl
2 #
3 # Copyright © 2009 Sebastian "tokkee" Harl
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 # 1. Redistributions of source code must retain the above copyright notice,
9 #    this list of conditions and the following disclaimer.
10 # 2. Redistributions in binary form must reproduce the above copyright notice,
11 #    this list of conditions and the following disclaimer in the documentation
12 #    and/or other materials provided with the distribution.
13 # 3. Neither the name of the copyright holders nor the names of their
14 #    contributors may be used to endorse or promote products derived from this
15 #    software without specific prior written permission.
16
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
18 # IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19 # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
21 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 # OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #
29 # Author:
30 #   Sebastian "tokkee" Harl <sh@tokkee.org>
32 use strict;
33 use warnings;
35 use utf8;
37 use Term::ANSIColor;
39 use Net::Twitter::Lite;
41 my $conf_file = "$ENV{'HOME'}/.identica.conf";
43 my ($fh, $nt);
44 my %id;
46 # This file should fill the %id hash.
47 #
48 # See <http://search.cpan.org/~mmims/Net-Twitter-Lite-0.08000/lib/Net/
49 # Twitter/Lite.pm#METHODS_AND_ARGUMENTS> for details about which parameters
50 # are available.
51 if (! open($fh, '<', $conf_file)) {
52         print_err("Failed to open config file ($conf_file): $!\n");
53         exit 1;
54 }
56 {
57         my @conf = <$fh>;
58         eval join '', @conf;
59 }
61 close($fh);
63 if (! $id{'identica'}) {
64         print_err('id{identica} has not been set - '
65                 . 'Twitter is not currently supported!' . "\n");
66         exit 1;
67 }
69 $nt = Net::Twitter::Lite->new(%id);
71 if (! $nt) {
72         print_err("Failed to create Net::Twitter::Lite object: $!\n");
73         exit 1;
74 }
76 while (42) {
77         my $msg = "";
78         my $status;
79         my $err;
81         print "identi.ca> ", ' ' x 140, '←', "\r";
82         print color 'bold';
83         print "identi.ca> ";
84         print color 'reset';
86         $msg = <STDIN>;
88         if (! defined($msg)) {
89                 last;
90         }
91         elsif ($msg =~ m/^\?(\?)?(\d+)?$/) {
92                 my $friends = $1;
93                 my $count = $2;
94                 my $posts;
96                 if (! defined($count)) {
97                         $count = 5;
98                 }
99                         
100                 eval {
101                         if (defined $friends) {
102                                 $posts = $nt->friends_timeline({count => $count});
103                         }
104                         else {
105                                 $posts = $nt->user_timeline({count => $count});
106                         }
107                 };
109                 if ($err = $@) {
110                         print_err("Failed to get ``tweeds'': $err\n");
111                         next;
112                 }
114                 foreach my $post (@$posts) {
115                         my $text   = $post->{'text'};
116                         my $source = $post->{'source'};
118                         utf8::upgrade($text);
120                         if ($source =~ m/^<a href="[^"]+">(.*)<\/a>$/) {
121                                 $source = $1;
122                         }
124                         print $post->{'created_at'} . ' (';
125                         print color 'bold blue';
126                         print $post->{'user'}->{'screen_name'};
127                         print color 'reset';
128                         print ", $source): ";
129                         print color 'dark yellow';
130                         print "$text\n";
131                         print color 'reset';
132                 }
133                 next;
134         }
136         eval {
137                 $status = $nt->update({ status => $msg });
138         };
140         if ($err = $@) {
141                 print_err("Failed to send message: $err\n");
142                 next;
143         }
145         print color 'bold green';
146         print 'http://identi.ca/notice/' . $status->{'id'} . "\n";
147         print color 'reset';
150 print color 'bold';
151 print "\nBye!\n";
152 print color 'reset';
154 sub print_err {
155         print STDERR color 'bold red';
156         print STDERR join(' ', @_);
157         print STDERR color 'reset';
160 # vim: set noexpandtab tw=78 sw=4 ts=4 :