WWW-Sitebase

 view release on metacpan or  search on metacpan

lib/WWW/Sitebase/Navigator.pm  view on Meta::CPAN

# _account_verified
# Returns true if we've verified that the current account and password
# are valid (by successfully logging in with them)
sub _account_verified {

    ( ( $self->{_account_verified}->{ $self->account_name } ) &&
      ( $self->password = $self->{_account_verified}->{ $self->account_name } )
    )

}

# _init_account
# Initialize basic account/login-specific settings after login
sub _init_account {
    
    # Get our friend ID from our profile page (which happens to
    # be the page we go to after logging in).
    $self->_get_friend_id( $self->current_page );

    # If for some reason we couldn't set this, fail login.
    unless ( $self->my_friend_id ) { $self->logged_in(0) ; return }
    
    # Set the user_name and friend_count fields.
    $self->user_name( $self->current_page );
    $self->friend_count( $self->current_page );
    
    # Cache whether or not we're a band.
    $self->is_band;

    # Note that we've verified this account/password
    $self->{_account_verified}->{ $self->account_name } = $self->password;

}

sub _new_mech {

    # Set up our web browser (WWW::Mechanize object)
    $self->mech( new WWW::Mechanize(
                 onerror => undef,
                 # We'll say we're Safari running on MacOS 10.9.1
                 agent => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1)'
                    . ' AppleWebKit/537.73.11 (KHTML, like Gecko) Version/7.0.1'
                    . ' Safari/537.73.11',
                 stack_depth => 1,
                 quiet => 1,
               ) );

    # We need to follow redirects for POST too.
    push @{ $self->mech->requests_redirectable }, 'POST';

}

#---------------------------------------------------------------------
# _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

sub logout {

    # If you change this to just log out instead of making a new Mech
    # object, be sure you change site_login too.
    $self->_new_mech;
    
    # Clear anything login-specific
    $self->logged_in(0);
    $self->error(0);

    # Do NOT clear options that are set by the user!
#   $self->{account_name} = undef;
#   $self->{password} = undef;

}

#---------------------------------------------------------------------
# Value return methods
# These methods return internal data that is of use to outsiders

sub ____CHECK_STATUS____ {}

=head1 CHECK STATUS

=head2 logged_in

Returns true if login was successful. When you call the new method
of WWW::Sitebase::Navigator, the class logs in using the username and password
you provided (or that it prompted for).  It then retreives your "home"
page (the one you see when you click the "Home" button that's set up in your
site_info field), and checks it against an RE.  If the page matches the RE,
logged_in is set to a true value. Otherwise it's set to a false value.

 Notes:
 - This method is only set on login. If you're logged out somehow,
   this method won't tell you that (yet - I may add that later).
 - The internal login method calls this method to set the value.
   You can (currently) call logged_in with a value, and it'll set
   it, but that would be stupid, and it might not work later
   anyway, so don't.

 Examples, pretending we have a subclass named WWW::Bebo to navigate a site
 named bebo.com:

 my $bebo = new WWW::Bebo;
 unless ( $site->logged_in ) {
    die "Login failed\n";
 }
 



( run in 1.657 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )