+ my $service;
+ if ( $user =~ /^(.*)@(twitter|identica)$/ ) {
+ ( $user, $service ) = ( $1, $2 );
+ } else {
+ $service = Irssi::settings_get_str("twirssi_default_service");
+ }
+ $defservice = $service = ucfirst lc $service;
+
+ if ( $service eq 'Twitter'
+ and Irssi::settings_get_bool("twirssi_use_oauth") )
+ {
+ print "Attempting OAuth for $user\@$service" if &debug;
+ eval {
+ if ( $service eq 'Identica' )
+ {
+ $twit = Net::Twitter->new(
+ identica => 1,
+ traits => [ 'API::REST', 'API::Search' ],
+ source => "twirssi",
+ ssl => !Irssi::settings_get_bool("twirssi_avoid_ssl"),
+ );
+ } else {
+ $twit = Net::Twitter->new(
+ traits => [ 'API::REST', 'OAuth', 'API::Search' ],
+ consumer_key => 'BZVAvBma4GxdiRwXIvbnw',
+ consumer_secret =>
+ '0T5kahwLyb34vciGZsgkA9lsjtGCQ05vxVE2APXM',
+ source => "twirssi",
+ ssl => !Irssi::settings_get_bool("twirssi_avoid_ssl"),
+ );
+ }
+ };
+
+ if ($twit) {
+ if ( open( OAUTH, Irssi::settings_get_str("twirssi_oauth_store") ) )
+ {
+ while (<OAUTH>) {
+ chomp;
+ next unless m/$user\@$service (\S+) (\S+)/i;
+ print "Trying cached oauth creds for $user\@$service"
+ if &debug;
+ $twit->access_token($1);
+ $twit->access_token_secret($2);
+ last;
+ }
+ close OAUTH;
+ }
+
+ unless ( $twit->authorized ) {
+ my $url;
+ eval { $url = $twit->get_authorization_url; };
+
+ if ($@) {
+ ¬ice(
+"ERROR: Failed to get OAuth authorization_url. Try again later."
+ );
+ return;
+ }
+
+ ¬ice("Twirssi not autorized to access $service for $user.");
+ ¬ice(
+ "Please authorize at the following url, then enter the pin "
+ );
+ ¬ice("supplied with /twirssi_oauth $user\@$service <pin>");
+ ¬ice($url);
+
+ $oauth{pending}{"$user\@$service"} = $twit;
+ return;
+ }
+ }
+ } else {
+ $twit = Net::Twitter->new(
+ $service eq 'Identica' ? ( identica => 1 ) : (),
+ username => $user,
+ password => $pass,
+ source => "twirssi",
+ ssl => Irssi::settings_get_bool("twirssi_avoid_ssl") ? 0 : 1,
+ );
+ }
+
+ unless ($twit) {
+ ¬ice("Failed to create object! Aborting.");
+ return;
+ }
+
+ return &verify_twitter_object( $server, $win, $user, $service, $twit );
+}
+
+sub cmd_oauth {
+ my ( $data, $server, $win ) = @_;
+ my ( $key, $pin ) = split ' ', $data;
+ my ( $user, $service );
+ $key = &normalize_username($key);
+ if ( $key =~ /^(.*)@(Twitter|Identica)$/ ) {
+ ( $user, $service ) = ( $1, $2 );
+ }
+ $pin =~ s/\D//g;
+ print "Applying pin to $key" if &debug;
+
+ unless ( exists $oauth{pending}{$key} ) {
+ ¬ice("There isn't a pending oauth request for $key. "
+ . "Try /twitter_login first" );
+ return;
+ }
+
+ my $twit = $oauth{pending}{$key};
+ my ( $access_token, $access_token_secret );
+ eval {
+ ( $access_token, $access_token_secret ) =
+ $twit->request_access_token( verifier => $pin );
+ };
+
+ if ($@) {
+ ¬ice("Invalid pin, try again.");
+ return;
+ }
+
+ delete $oauth{pending}{$key};
+
+ my $store_file = Irssi::settings_get_str("twirssi_oauth_store");
+ if ($store_file) {
+ my @store;
+ if ( open( OAUTH, $store_file ) ) {
+ while (<OAUTH>) {
+ chomp;
+ next if /$key/i;
+ push @store, $_;
+ }
+ close OAUTH;
+
+ }
+
+ push @store, "$key $access_token $access_token_secret";
+
+ if ( open( OAUTH, ">$store_file.new" ) ) {
+ print OAUTH "$_\n" foreach @store;
+ close OAUTH;
+ rename "$store_file.new", $store_file
+ or ¬ice("Failed to rename $store_file.new: $!");
+ } else {
+ ¬ice("Failed to write $store_file.new: $!");
+ }
+ } else {
+ ¬ice("No persistant storage set for OAuth. "
+ . "Please /set twirssi_oauth_store to a writable filename." );
+ }
+
+ return &verify_twitter_object( $server, $win, $user, $service, $twit );
+}
+
+sub verify_twitter_object {
+ my ( $server, $win, $user, $service, $twit ) = @_;
+
+ if ( my $timeout = Irssi::settings_get_int("twitter_timeout")
+ and $twit->can('ua') )
+ {
+ $twit->ua->timeout($timeout);
+ ¬ice("Twitter timeout set to $timeout");
+ }