9 use vars qw($VERSION %IRSSI);
10 use constant { DEBUG => 0 };
13 my ($REV) = '$Rev: 301 $' =~ /(\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 push @lines, $_ unless /^--friends:$/;
296 my ($f, $t) = split ' ', $_;
300 print "new last_poll = $last_poll" if DEBUG;
301 foreach my $line (@lines) {
303 $window->print( $line, MSGLEVEL_PUBLIC );
304 foreach ( $line =~ /\@([-\w]+)/ ) {
310 unlink $filename or warn "Failed to remove $filename: $!";
314 Irssi::timeout_add_once( 5000, 'monitor_child', [$filename] );
318 $window->print( "%R***%n @_", MSGLEVEL_PUBLIC );
322 my ( $complist, $window, $word, $linestart, $want_space ) = @_;
324 return unless $linestart =~ /^\/(?:tweet|dm)/;
325 return if $linestart eq '/tweet' and $word !~ s/^@//;
326 push @$complist, grep /^\Q$word/i,
327 sort { $nicks{$b} <=> $nicks{$a} } keys %nicks;
328 @$complist = map { "\@$_" } @$complist if $linestart eq '/tweet';
331 Irssi::settings_add_str( "twirssi", "twitter_window", "twitter" );
332 Irssi::settings_add_str( "twirssi", "bitlbee_server", "bitlbee" );
333 Irssi::settings_add_str( "twirssi", "short_url_provider", "TinyURL" );
334 Irssi::settings_add_bool( "twirssi", "tweet_to_away", 0 );
335 Irssi::settings_add_bool( "twirssi", "show_reply_context", 0 );
336 Irssi::settings_add_bool( "twirssi", "show_own_tweets", 1 );
337 $window = Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
339 Irssi::command_bind( "dm", "cmd_direct" );
340 Irssi::command_bind( "tweet", "cmd_tweet" );
341 Irssi::command_bind( "twitter_login", "cmd_login" );
346 "Twirssi v$VERSION (r$REV). See details at http://tinyurl.com/twirssi"
353 "/twitter_friend <username>",
355 sub { ¬ice("Following $_[0]"); $nicks{$_[0]} = time; }
361 "/twitter_unfriend <username>",
363 sub { ¬ice("Stopped following $_[0]"); delete $nicks{$_[0]}; }
366 Irssi::command_bind( "twitter_updates", "get_updates" );
367 Irssi::signal_add_last( 'complete word' => \&sig_complete );
369 ¬ice(" %Y<%C(%B^%C)%N TWIRSSI v%R$VERSION%N (r$REV)");
370 ¬ice(" %C(_(\\%N http://tinyurl.com/twirssi for full docs");
371 ¬ice( " %Y||%C `%N Log in with /twitter_login, send updates with /tweet");
373 if ( my $provider = Irssi::settings_get_str("short_url_provider") ) {
374 eval "use WWW::Shorten::$provider;";
378 "Failed to load WWW::Shorten::$provider - either clear short_url_provider or install the CPAN module"
384 ->print( "Create a window named "
385 . Irssi::settings_get_str('twitter_window')
386 . " or change the value of twitter_window. Then, reload twirssi." );