+ 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;
+ my ( $k, $v ) = split ' ', 2;
+ $store{$k} = $v;
+ }
+ close OAUTH;
+
+ }
+
+ $store{$key} = "$access_token $access_token_secret";
+
+ if ( open( OAUTH, ">$store_file.new" ) ) {
+ print OAUTH "$_ $store{$_}\n" foreach keys %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) = @_;
+