9 use vars qw($VERSION %IRSSI);
10 use constant { DEBUG => 0 };
13 my ($REV) = '$Rev: 303 $' =~ /(\d+)/;
15 authors => 'Dan Boger',
16 contact => 'zigdon@gmail.com',
18 description => 'Send twitter updates using /tweet. '
19 . 'Can optionally set your bitlbee /away message to same',
20 license => 'GNU GPL v2',
21 url => 'http://tinyurl.com/twirssi',
22 changed => 'Mon Dec 1 15:36:01 PST 2008',
31 my $last_poll = time - 300;
34 my ( $data, $server, $win ) = @_;
37 ¬ice("Not logged in! Use /twitter_login username pass!");
41 my ( $target, $text ) = split ' ', $data, 2;
42 unless ( $target and $text ) {
43 ¬ice("Usage: /dm <nick> <message>");
47 unless ( $twit->new_direct_message( { user => $target, text => $text } ) ) {
48 ¬ice("DM to $target failed");
52 ¬ice("DM sent to $target");
53 $nicks{$target} = time;
57 my ( $data, $server, $win ) = @_;
60 ¬ice("Not logged in! Use /twitter_login username pass!");
64 $data =~ s/^\s+|\s+$//;
66 ¬ice("Usage: /tweet <update>");
70 foreach my $url ( $data =~ /(https?:\/\/\S+[\w\/])/g ) {
71 eval { my $short = makeashorterlink($url); $data =~ s/\Q$url/$short/g; };
74 unless ( $twit->update($data) ) {
75 ¬ice("Update failed");
79 foreach ( $data =~ /@([-\w]+)/ ) {
84 if ( Irssi::settings_get_bool("tweet_to_away")
86 and $data !~ /^[dD] / )
89 Irssi::server_find_tag( Irssi::settings_get_str("bitlbee_server") );
91 $server->send_raw("away :$data");
94 ¬ice( "Can't find bitlbee server.",
95 "Update bitlbee_server or disalbe tweet_to_away" );
99 ¬ice( "Update sent" . ( $away ? " (and away msg set)" : "" ) );
103 my ( $usage_str, $api_name, $post_ref ) = @_;
106 my ( $data, $server, $win ) = @_;
109 ¬ice("Not logged in! Use /twitter_login username pass!");
113 $data =~ s/^\s+|\s+$//;
115 ¬ice("Usage: $usage_str");
119 unless ( $twit->$api_name($data) ) {
120 ¬ice("$api_name failed");
124 &$post_ref($data) if $post_ref;
129 my ( $data, $server, $win ) = @_;
131 ( $user, $pass ) = split ' ', $data, 2;
133 %friends = %nicks = ();
135 $twit = Net::Twitter->new(
141 unless ( $twit->verify_credentials() ) {
142 ¬ice("Login failed");
148 Irssi::timeout_remove($poll) if $poll;
149 $poll = Irssi::timeout_add( 300 * 1000, \&get_updates, "" );
150 ¬ice("Logged in as $user, loading friends list...");
152 ¬ice( "loaded friends: ", scalar keys %nicks );
157 ¬ice("Login failed");
165 my $friends = $twit->friends( { page => $page } );
166 last unless $friends;
167 $new_friends{ $_->{screen_name} } = time
170 last if @$friends == 0 or $page == 10;
171 $friends = $twit->friends( page => $page );
174 foreach (keys %new_friends) {
175 next if exists $friends{$_};
179 foreach (keys %friends) {
180 delete $friends{$_} unless exists $new_friends{$_};
186 Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
189 ->print( "Can't find a window named '"
190 . Irssi::settings_get_str('twitter_window')
191 . "'. Create it or change the value of twitter_window" );
194 ¬ice("Not logged in! Use /twitter_login username pass!");
198 my ( $fh, $filename ) = File::Temp::tempfile();
202 Irssi::timeout_add_once( 5000, 'monitor_child', [$filename] );
203 } elsif ( defined $pid ) { # child
210 print scalar localtime, " - Polling for updates" if DEBUG;
211 my $tweets = $twit->friends_timeline(
212 { since => HTTP::Date::time2str($last_poll) } )
214 foreach my $t ( reverse @$tweets ) {
215 my $text = decode_entities( $t->{text} );
217 $text =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/g;
219 if ( Irssi::settings_get_bool("show_reply_context")
220 and $t->{in_reply_to_screen_name} ne $user
221 and $t->{in_reply_to_screen_name}
222 and not exists $friends{ $t->{in_reply_to_screen_name} } )
224 $nicks{ $t->{in_reply_to_screen_name} } = time;
225 my $context = $twit->show_status( $t->{in_reply_to_status_id} );
227 my $ctext = decode_entities( $context->{text} );
229 $ctext =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/g;
230 printf $fh "[%%B\@%s%%n] %s\n",
231 $context->{user}{screen_name}, $ctext;
236 if $t->{user}{screen_name} eq $user
237 and not Irssi::settings_get_bool("show_own_tweets");
238 printf $fh "%s[%%B\@%s%%n] %s\n", $prefix, $t->{user}{screen_name},
242 print scalar localtime, " - Polling for replies" if DEBUG;
244 $twit->replies( { since => HTTP::Date::time2str($last_poll) } )
246 foreach my $t ( reverse @$tweets ) {
248 if exists $friends{ $t->{user}{screen_name} };
250 my $text = decode_entities( $t->{text} );
252 $text =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/g;
253 printf $fh "[%%B\@%s%%n] %s\n", $t->{user}{screen_name}, $text;
256 print scalar localtime, " - Polling for DMs" if DEBUG;
257 $tweets = $twit->direct_messages(
258 { since => HTTP::Date::time2str($last_poll) } )
260 foreach my $t ( reverse @$tweets ) {
261 my $text = decode_entities( $t->{text} );
263 $text =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/g;
264 printf $fh "[%%B\@%s%%n (%%WDM%%n)] %s\n", $t->{sender_screen_name},
267 print scalar localtime, " - Done" if DEBUG;
268 print $fh "__friends__\n";
270 foreach (sort keys %friends) {
271 print $fh "$_ $friends{$_}\n";
281 my $filename = $data->[0];
283 print scalar localtime, " - checking child log at $filename" if DEBUG;
284 if ( open FILE, $filename ) {
288 last if /^__friends__/;
289 push @lines, $_ unless /^__friends__/;
298 my ($f, $t) = split ' ', $_;
299 $nicks{$f} = $friends{$f} = $t;
302 print "new last_poll = $last_poll" if DEBUG;
303 foreach my $line (@lines) {
305 $window->print( $line, MSGLEVEL_PUBLIC );
306 foreach ( $line =~ /\@([-\w]+)/ ) {
312 unlink $filename or warn "Failed to remove $filename: $!";
316 Irssi::timeout_add_once( 5000, 'monitor_child', [$filename] );
320 $window->print( "%R***%n @_", MSGLEVEL_PUBLIC );
324 my ( $complist, $window, $word, $linestart, $want_space ) = @_;
326 return unless $linestart =~ /^\/(?:tweet|dm)/;
327 return if $linestart eq '/tweet' and $word !~ s/^@//;
328 push @$complist, grep /^\Q$word/i,
329 sort { $nicks{$b} <=> $nicks{$a} } keys %nicks;
330 @$complist = map { "\@$_" } @$complist if $linestart eq '/tweet';
333 Irssi::settings_add_str( "twirssi", "twitter_window", "twitter" );
334 Irssi::settings_add_str( "twirssi", "bitlbee_server", "bitlbee" );
335 Irssi::settings_add_str( "twirssi", "short_url_provider", "TinyURL" );
336 Irssi::settings_add_bool( "twirssi", "tweet_to_away", 0 );
337 Irssi::settings_add_bool( "twirssi", "show_reply_context", 0 );
338 Irssi::settings_add_bool( "twirssi", "show_own_tweets", 1 );
339 $window = Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
341 Irssi::command_bind( "dm", "cmd_direct" );
342 Irssi::command_bind( "tweet", "cmd_tweet" );
343 Irssi::command_bind( "twitter_login", "cmd_login" );
348 "Twirssi v$VERSION (r$REV). See details at http://tinyurl.com/twirssi"
355 "/twitter_friend <username>",
357 sub { ¬ice("Following $_[0]"); $nicks{$_[0]} = time; }
363 "/twitter_unfriend <username>",
365 sub { ¬ice("Stopped following $_[0]"); delete $nicks{$_[0]}; }
368 Irssi::command_bind( "twitter_updates", "get_updates" );
369 Irssi::signal_add_last( 'complete word' => \&sig_complete );
371 ¬ice(" %Y<%C(%B^%C)%N TWIRSSI v%R$VERSION%N (r$REV)");
372 ¬ice(" %C(_(\\%N http://tinyurl.com/twirssi for full docs");
373 ¬ice( " %Y||%C `%N Log in with /twitter_login, send updates with /tweet");
375 if ( my $provider = Irssi::settings_get_str("short_url_provider") ) {
376 eval "use WWW::Shorten::$provider;";
380 "Failed to load WWW::Shorten::$provider - either clear short_url_provider or install the CPAN module"
386 ->print( "Create a window named "
387 . Irssi::settings_get_str('twitter_window')
388 . " or change the value of twitter_window. Then, reload twirssi." );