WWW-Sitebase
view release on metacpan or search on metacpan
lib/WWW/Sitebase/Navigator.pm view on Meta::CPAN
}
#---------------------------------------------------------------------
# _get_acct()
# Get and store the login and password. We check the user's preference
# file for defaults, then prompt them.
sub _get_acct {
# Initialize
my $prefs = {};
my $ref = "";
my ( $pref, $value, $res );
my $cache_filepath = catfile( $self->cache_dir, $self->cache_file);
# Read what we got last time.
if ( open ( PREFS, "< ", $cache_filepath ) ) {
while (<PREFS>) {
chomp;
( $pref, $value ) = split( ":" );
$prefs->{"$pref"} = $value;
}
close PREFS;
}
# If we have a username and password, and they asked us to use the
# cached defaults, then skip the login prompts. Otherwise, prompt
# the user for login info.
unless ( $self->use_defaults && $prefs->{'email'} && $prefs->{'password'} ) {
$prefs = $self->_prompt_for_login( $prefs );
}
# Store the account info.
$self->{account_name}=$prefs->{"email"};
$self->{password}=$prefs->{"password"};
}
# _prompt_for_login( { email => $email, password => $password } )
#
# Given an optional email and password, prompt the user, displaying the
# existing email and password as defaults (well, passwords are displayed as
# "*****"). Returns the email and password entered, or defaulted to,
# by the user.
#
sub _prompt_for_login {
my ( $prefs ) = @_;
# Prompt them for current values
unless ( defined $prefs->{"email"} ) { $prefs->{"email"} = "" }
print "Email [" . $prefs->{"email"} . "]: ";
my $res = ReadLine 0; chomp $res;
if ( $res ) {
$prefs->{"email"} = $res;
}
unless ( defined $prefs->{"password"} ) { $prefs->{"password"} = "" }
my $password_indicator = $prefs->{'password'} ? '*****' : '';
print "Password [". $password_indicator . "]: ";
ReadMode 'noecho'; # From Term::ReadKey. Make password not echo.
$res = ReadLine 0;
chomp $res;
ReadMode 'normal';
print "\n"; # Because ReadLine won't output a new line when they hit return
if ( $res ) {
$prefs->{"password"} = $res;
}
# Make the cache directory if it doesn't exist.
$self->make_cache_dir;
# Store the new values. We clobber the file, set it r/w by the user,
# *then* write.
my $cache_filepath = catfile( $self->cache_dir, $self->cache_file);
open ( PREFS, ">", $cache_filepath ) or croak $!;
chmod 0600, $cache_filepath;
print PREFS "email:" . $prefs->{"email"} . "\n" .
"password:" . $prefs->{"password"} . "\n";
close PREFS || croak "Error closing file when writing username/password: $!";
return $prefs;
}
=head2 logout
Clears the current web browsing object and resets any login-specific
internal values. Currently this drops and creates a new WWW::Mechanize
object. This may change in the future to actually clicking "logout"
or something.
=cut
( run in 0.521 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )