9 use vars qw($VERSION %IRSSI);
10 use constant { DEBUG => 0 };
13 my ($REV) = '$Rev: 310 $' =~ /(\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',
32 my $last_poll = time - 300;
35 my ( $data, $server, $win ) = @_;
38 ¬ice("Not logged in! Use /twitter_login username pass!");
42 my ( $target, $text ) = split ' ', $data, 2;
43 unless ( $target and $text ) {
44 ¬ice("Usage: /dm <nick> <message>");
48 &cmd_direct_as( "$user $data", $server, $win );
52 my ( $data, $server, $win ) = @_;
55 ¬ice("Not logged in! Use /twitter_login username pass!");
59 my ( $username, $target, $text ) = split ' ', $data, 3;
60 unless ( $username and $target and $text ) {
61 ¬ice("Usage: /dm_as <username> <nick> <message>");
65 unless ( exists $twits{$username} ) {
66 ¬ice("Unknown username $username");
70 unless ( $twits{$username}
71 ->new_direct_message( { user => $target, text => $text } ) )
73 ¬ice("DM to $target failed");
77 ¬ice("DM sent to $target");
78 $nicks{$target} = time;
82 my ( $data, $server, $win ) = @_;
85 ¬ice("Not logged in! Use /twitter_login username pass!");
89 $data =~ s/^\s+|\s+$//;
91 ¬ice("Usage: /tweet <update>");
95 &cmd_tweet_as( "$user $data", $server, $win );
99 my ( $data, $server, $win ) = @_;
102 ¬ice("Not logged in! Use /twitter_login username pass!");
106 $data =~ s/^\s+|\s+$//;
107 my ( $username, $data ) = split ' ', $data, 2;
109 unless ( $username and $data ) {
110 ¬ice("Usage: /tweet_as <username> <update>");
114 unless ( exists $twits{$username} ) {
115 ¬ice("Unknown username $username");
119 foreach my $url ( $data =~ /(https?:\/\/\S+[\w\/])/g ) {
120 eval { my $short = makeashorterlink($url); $data =~ s/\Q$url/$short/g; };
123 unless ( $twits{$username}->update($data) ) {
124 ¬ice("Update failed");
128 foreach ( $data =~ /@([-\w]+)/ ) {
133 if ( Irssi::settings_get_bool("tweet_to_away")
135 and $data !~ /^[dD] / )
138 Irssi::server_find_tag( Irssi::settings_get_str("bitlbee_server") );
140 $server->send_raw("away :$data");
143 ¬ice( "Can't find bitlbee server.",
144 "Update bitlbee_server or disalbe tweet_to_away" );
148 ¬ice( "Update sent" . ( $away ? " (and away msg set)" : "" ) );
152 my ( $usage_str, $api_name, $post_ref ) = @_;
155 my ( $data, $server, $win ) = @_;
158 ¬ice("Not logged in! Use /twitter_login username pass!");
162 $data =~ s/^\s+|\s+$//;
164 ¬ice("Usage: $usage_str");
168 unless ( $twit->$api_name($data) ) {
169 ¬ice("$api_name failed");
173 &$post_ref($data) if $post_ref;
178 my ( $data, $server, $win ) = @_;
180 $data =~ s/^\s+|\s+$//g;
181 if ( exists $twits{$data} ) {
182 ¬ice("Switching to $data");
183 $twit = $twits{$data};
186 ¬ice("Unknown user $data");
191 my ( $data, $server, $win ) = @_;
193 $data =~ s/^\s+|\s+$//g;
194 if ( $data and exists $twits{$data} ) {
195 ¬ice("Logging out $data...");
196 $twits{$data}->end_session();
197 delete $twits{$data};
199 ¬ice("Unknown username '$data'");
201 ¬ice("Logging out $user...");
202 $twit->end_session();
204 delete $twits{$user};
206 &cmd_switch( ( keys %twits )[0], $server, $win );
208 Irssi::timeout_remove($poll) if $poll;
215 my ( $data, $server, $win ) = @_;
217 ( $user, $pass ) = split ' ', $data, 2;
219 %friends = %nicks = ();
221 $twit = Net::Twitter->new(
227 unless ( $twit->verify_credentials() ) {
228 ¬ice("Login failed");
234 my $rate_limit = $twit->rate_limit_status();
235 if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
236 ¬ice("Rate limit exceeded, try again later");
241 $twits{$user} = $twit;
242 Irssi::timeout_remove($poll) if $poll;
243 $poll = Irssi::timeout_add( 300 * 1000, \&get_updates, "" );
244 ¬ice("Logged in as $user, loading friends list...");
246 ¬ice( "loaded friends: ", scalar keys %friends );
251 ¬ice("Login failed");
259 my $friends = $twit->friends( { page => $page } );
260 last unless $friends;
261 $new_friends{ $_->{screen_name} } = time foreach @$friends;
263 last if @$friends == 0 or $page == 10;
264 $friends = $twit->friends( page => $page );
267 foreach ( keys %new_friends ) {
268 next if exists $friends{$_};
272 foreach ( keys %friends ) {
273 delete $friends{$_} unless exists $new_friends{$_};
279 Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
282 ->print( "Can't find a window named '"
283 . Irssi::settings_get_str('twitter_window')
284 . "'. Create it or change the value of twitter_window" );
287 ¬ice("Not logged in! Use /twitter_login username pass!");
291 my ( $fh, $filename ) = File::Temp::tempfile();
295 Irssi::timeout_add_once( 5000, 'monitor_child', [$filename] );
296 } elsif ( defined $pid ) { # child
303 &do_updates( $fh, $user, $twit );
304 foreach ( keys %twits ) {
306 &do_updates( $fh, $_, $twits{$_} );
309 print $fh "__friends__\n";
311 foreach ( sort keys %friends ) {
312 print $fh "$_ $friends{$_}\n";
321 my ( $fh, $username, $obj ) = @_;
323 print scalar localtime, " - Polling for updates for $username" if DEBUG;
325 $obj->friends_timeline( { since => HTTP::Date::time2str($last_poll) } )
327 foreach my $t ( reverse @$tweets ) {
328 my $text = decode_entities( $t->{text} );
330 $text =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/g;
332 if ( Irssi::settings_get_bool("show_reply_context")
333 and $t->{in_reply_to_screen_name} ne $username
334 and $t->{in_reply_to_screen_name}
335 and not exists $friends{ $t->{in_reply_to_screen_name} } )
337 $nicks{ $t->{in_reply_to_screen_name} } = time;
338 my $context = $obj->show_status( $t->{in_reply_to_status_id} );
340 my $ctext = decode_entities( $context->{text} );
342 $ctext =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/g;
343 printf $fh "[%s%%B\@%s%%n] %s\n",
344 ( $username ne $user ? "$username: " : "" ),
345 $context->{user}{screen_name}, $ctext;
350 if $t->{user}{screen_name} eq $username
351 and not Irssi::settings_get_bool("show_own_tweets");
352 printf $fh "%s[%s%%B\@%s%%n] %s\n",
354 ( $username ne $user ? "$username: " : "" ),
355 $t->{user}{screen_name},
359 print scalar localtime, " - Polling for replies" if DEBUG;
360 $tweets = $obj->replies( { since => HTTP::Date::time2str($last_poll) } )
362 foreach my $t ( reverse @$tweets ) {
364 if exists $friends{ $t->{user}{screen_name} };
366 my $text = decode_entities( $t->{text} );
368 $text =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/g;
369 printf $fh "[%s%%B\@%s%%n] %s\n",
370 ( $username ne $user ? "$username: " : "" ),
371 $t->{user}{screen_name},
375 print scalar localtime, " - Polling for DMs" if DEBUG;
377 $obj->direct_messages( { since => HTTP::Date::time2str($last_poll) } )
379 foreach my $t ( reverse @$tweets ) {
380 my $text = decode_entities( $t->{text} );
382 $text =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/g;
383 printf $fh "[%s%%B\@%s%%n (%%WDM%%n)] %s\n",
384 ( $username ne $user ? "$username: " : "" ),
385 $t->{sender_screen_name},
388 print scalar localtime, " - Done" if DEBUG;
393 my $filename = $data->[0];
395 print scalar localtime, " - checking child log at $filename" if DEBUG;
396 if ( open FILE, $filename ) {
400 last if /^__friends__/;
401 push @lines, $_ unless /^__friends__/;
410 my ( $f, $t ) = split ' ', $_;
411 $nicks{$f} = $friends{$f} = $t;
414 print "new last_poll = $last_poll" if DEBUG;
415 foreach my $line (@lines) {
417 $window->print( $line, MSGLEVEL_PUBLIC );
418 foreach ( $line =~ /\@([-\w]+)/ ) {
424 unlink $filename or warn "Failed to remove $filename: $!";
428 Irssi::timeout_add_once( 5000, 'monitor_child', [$filename] );
432 $window->print( "%R***%n @_", MSGLEVEL_PUBLIC );
436 my ( $complist, $window, $word, $linestart, $want_space ) = @_;
438 return unless $linestart =~ /^\/(?:tweet|dm)/;
439 return if $linestart eq '/tweet' and $word !~ s/^@//;
440 push @$complist, grep /^\Q$word/i,
441 sort { $nicks{$b} <=> $nicks{$a} } keys %nicks;
442 @$complist = map { "\@$_" } @$complist if $linestart eq '/tweet';
445 Irssi::settings_add_str( "twirssi", "twitter_window", "twitter" );
446 Irssi::settings_add_str( "twirssi", "bitlbee_server", "bitlbee" );
447 Irssi::settings_add_str( "twirssi", "short_url_provider", "TinyURL" );
448 Irssi::settings_add_bool( "twirssi", "tweet_to_away", 0 );
449 Irssi::settings_add_bool( "twirssi", "show_reply_context", 0 );
450 Irssi::settings_add_bool( "twirssi", "show_own_tweets", 1 );
451 $window = Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
453 Irssi::command_bind( "dm", "cmd_direct" );
454 Irssi::command_bind( "tweet", "cmd_tweet" );
455 Irssi::command_bind( "dm_as", "cmd_direct_as" );
456 Irssi::command_bind( "tweet_as", "cmd_tweet_as" );
457 Irssi::command_bind( "twitter_login", "cmd_login" );
458 Irssi::command_bind( "twitter_logout", "cmd_logout" );
459 Irssi::command_bind( "twitter_switch", "cmd_switch" );
464 "Twirssi v$VERSION (r$REV). See details at http://tinyurl.com/twirssi"
471 "/twitter_friend <username>",
473 sub { ¬ice("Following $_[0]"); $nicks{ $_[0] } = time; }
479 "/twitter_unfriend <username>",
481 sub { ¬ice("Stopped following $_[0]"); delete $nicks{ $_[0] }; }
484 Irssi::command_bind( "twitter_updates", "get_updates" );
485 Irssi::signal_add_last( 'complete word' => \&sig_complete );
487 ¬ice(" %Y<%C(%B^%C)%N TWIRSSI v%R$VERSION%N (r$REV)");
488 ¬ice(" %C(_(\\%N http://tinyurl.com/twirssi for full docs");
490 " %Y||%C `%N Log in with /twitter_login, send updates with /tweet");
492 if ( my $provider = Irssi::settings_get_str("short_url_provider") ) {
493 eval "use WWW::Shorten::$provider;";
497 "Failed to load WWW::Shorten::$provider - either clear short_url_provider or install the CPAN module"
503 ->print( "Create a window named "
504 . Irssi::settings_get_str('twitter_window')
505 . " or change the value of twitter_window. Then, reload twirssi." );