9 use vars qw($VERSION %IRSSI);
10 use constant { DEBUG => 0 };
13 my ($REV) = '$Rev: 302 $' =~ /(\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 );
156 ¬ice("Login failed");
164 my $friends = $twit->friends( { page => $page } );
165 last unless $friends;
166 $new_friends{ $_->{screen_name} } = $nicks{ $_->{screen_name} } = time
169 last if @$friends == 0 or $page == 10;
170 $friends = $twit->friends( page => $page );
173 foreach (keys %new_friends) {
174 next if exists $friends{$_};
178 foreach (keys %friends) {
179 delete $friends{$_} unless exists $new_friends{$_};
185 Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
188 ->print( "Can't find a window named '"
189 . Irssi::settings_get_str('twitter_window')
190 . "'. Create it or change the value of twitter_window" );
193 ¬ice("Not logged in! Use /twitter_login username pass!");
197 my ( $fh, $filename ) = File::Temp::tempfile();
201 Irssi::timeout_add_once( 5000, 'monitor_child', [$filename] );
202 } elsif ( defined $pid ) { # child
209 print scalar localtime, " - Polling for updates" if DEBUG;
210 my $tweets = $twit->friends_timeline(
211 { since => HTTP::Date::time2str($last_poll) } )
213 foreach my $t ( reverse @$tweets ) {
214 my $text = decode_entities( $t->{text} );
216 $text =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/g;
218 if ( Irssi::settings_get_bool("show_reply_context")
219 and $t->{in_reply_to_screen_name} ne $user
220 and $t->{in_reply_to_screen_name}
221 and not exists $friends{ $t->{in_reply_to_screen_name} } )
223 $nicks{ $t->{in_reply_to_screen_name} } = time;
224 my $context = $twit->show_status( $t->{in_reply_to_status_id} );
226 my $ctext = decode_entities( $context->{text} );
228 $ctext =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/g;
229 printf $fh "[%%B\@%s%%n] %s\n",
230 $context->{user}{screen_name}, $ctext;
235 if $t->{user}{screen_name} eq $user
236 and not Irssi::settings_get_bool("show_own_tweets");
237 printf $fh "%s[%%B\@%s%%n] %s\n", $prefix, $t->{user}{screen_name},
241 print scalar localtime, " - Polling for replies" if DEBUG;
243 $twit->replies( { since => HTTP::Date::time2str($last_poll) } )
245 foreach my $t ( reverse @$tweets ) {
247 if exists $friends{ $t->{user}{screen_name} };
249 my $text = decode_entities( $t->{text} );
251 $text =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/g;
252 printf $fh "[%%B\@%s%%n] %s\n", $t->{user}{screen_name}, $text;
255 print scalar localtime, " - Polling for DMs" if DEBUG;
256 $tweets = $twit->direct_messages(
257 { since => HTTP::Date::time2str($last_poll) } )
259 foreach my $t ( reverse @$tweets ) {
260 my $text = decode_entities( $t->{text} );
262 $text =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/g;
263 printf $fh "[%%B\@%s%%n (%%WDM%%n)] %s\n", $t->{sender_screen_name},
266 print scalar localtime, " - Done" if DEBUG;
267 print $fh "__friends__\n";
269 foreach (sort keys %friends) {
270 print $fh "$_ $friends{$_}\n";
280 my $filename = $data->[0];
282 print scalar localtime, " - checking child log at $filename" if DEBUG;
283 if ( open FILE, $filename ) {
287 last if /^__friends__/;
288 push @lines, $_ unless /^__friends__/;
297 my ($f, $t) = split ' ', $_;
301 print "new last_poll = $last_poll" if DEBUG;
302 foreach my $line (@lines) {
304 $window->print( $line, MSGLEVEL_PUBLIC );
305 foreach ( $line =~ /\@([-\w]+)/ ) {
311 unlink $filename or warn "Failed to remove $filename: $!";
315 Irssi::timeout_add_once( 5000, 'monitor_child', [$filename] );
319 $window->print( "%R***%n @_", MSGLEVEL_PUBLIC );
323 my ( $complist, $window, $word, $linestart, $want_space ) = @_;
325 return unless $linestart =~ /^\/(?:tweet|dm)/;
326 return if $linestart eq '/tweet' and $word !~ s/^@//;
327 push @$complist, grep /^\Q$word/i,
328 sort { $nicks{$b} <=> $nicks{$a} } keys %nicks;
329 @$complist = map { "\@$_" } @$complist if $linestart eq '/tweet';
332 Irssi::settings_add_str( "twirssi", "twitter_window", "twitter" );
333 Irssi::settings_add_str( "twirssi", "bitlbee_server", "bitlbee" );
334 Irssi::settings_add_str( "twirssi", "short_url_provider", "TinyURL" );
335 Irssi::settings_add_bool( "twirssi", "tweet_to_away", 0 );
336 Irssi::settings_add_bool( "twirssi", "show_reply_context", 0 );
337 Irssi::settings_add_bool( "twirssi", "show_own_tweets", 1 );
338 $window = Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
340 Irssi::command_bind( "dm", "cmd_direct" );
341 Irssi::command_bind( "tweet", "cmd_tweet" );
342 Irssi::command_bind( "twitter_login", "cmd_login" );
347 "Twirssi v$VERSION (r$REV). See details at http://tinyurl.com/twirssi"
354 "/twitter_friend <username>",
356 sub { ¬ice("Following $_[0]"); $nicks{$_[0]} = time; }
362 "/twitter_unfriend <username>",
364 sub { ¬ice("Stopped following $_[0]"); delete $nicks{$_[0]}; }
367 Irssi::command_bind( "twitter_updates", "get_updates" );
368 Irssi::signal_add_last( 'complete word' => \&sig_complete );
370 ¬ice(" %Y<%C(%B^%C)%N TWIRSSI v%R$VERSION%N (r$REV)");
371 ¬ice(" %C(_(\\%N http://tinyurl.com/twirssi for full docs");
372 ¬ice( " %Y||%C `%N Log in with /twitter_login, send updates with /tweet");
374 if ( my $provider = Irssi::settings_get_str("short_url_provider") ) {
375 eval "use WWW::Shorten::$provider;";
379 "Failed to load WWW::Shorten::$provider - either clear short_url_provider or install the CPAN module"
385 ->print( "Create a window named "
386 . Irssi::settings_get_str('twitter_window')
387 . " or change the value of twitter_window. Then, reload twirssi." );