Mail-Webmail-Gmail

 view release on metacpan or  search on metacpan

lib/Mail/Webmail/Gmail.pm  view on Meta::CPAN

    }

    my $final_url;
    ( $final_url = $2 ) =~ s/\\u003d/=/;

    $req = HTTP::Request->new( GET => $final_url );
    $req->header( 'Cookie' => $self->{_cookie} );
    $res = $self->{_ua}->request( $req );
#    if ( ( $res->content() !~ /<a href="http:\/\/mail\.google\.com\/mail\?view=(?:.*?)&amp;fs=(?:.*?)" target="_blank"> (?:.*?) <\/a>/ ) &&
#       ( $res->content() !~ /<a href="http:\/\/mail\.google\.com\/mail">(?:.*?)<\/a>/ ) ) {
#        $self->{_error} = 1;
#        $self->{_err_str} .= "Error: Could not login with those credentials - could not find Gmail account.\n";
#        $self->{_err_str} .= "  Additionally, HTTP error: " . $res->status_line . "\n";
#        return;
#    }
    update_tokens( $self, $res );

    $req = HTTP::Request->new( GET => 'http://mail.google.com/mail?view=pr&amp;fs=1' );
    $req->header( 'Cookie' => $self->{_cookie} );
    $res = $self->{_ua}->request( $req );
    if ( $res->content() !~ /top.location="(.*?)"/ ) {
        $self->{_error} = 1;
        $self->{_err_str} .= "Error: Could not login with those credentials - could not find 'top.location'\n";
        $self->{_err_str} .= "  Additionally, HTTP error: " . $res->status_line . "\n";
        return;
    }
    update_tokens( $self, $res );

    $final_url = ( URI::Escape::uri_unescape( $1 ) );
    if ( $self->{_proxy_enable} ) {
        if ( $self->{_proxy_enable} >= 1 ) {
            $self->{_ua}->proxy( 'http', $self->{_proxy_name} );
            delete ( $ENV{HTTPS_PROXY} );
        }
        if ( $self->{_proxy_enable} >= 2 ) {
            delete ( $ENV{HTTPS_PROXY_USERNAME} );
            delete ( $ENV{HTTPS_PROXY_PASSWORD} );
        }
    }

    $self->{_logged_in} = 1;
    $res = get_page( $self, search => '', start => '', view => '', req_url => $final_url );
    $res = get_page( $self, search => 'inbox' );

    return ( 1 );
}

sub check_login {
    my ( $self ) = @_;

    if ( !$self->{_logged_in} ) {
        unless ( $self->login() ) {
            $self->{_error} = 1;
            $self->{_err_str} .= "Error: Could not Login.\n";
            return;
        }
    }
    return ( $self->{_logged_in} );
}

sub update_tokens {
    my ( $self, $res ) = @_;

    my $previous = $res->previous();
    if ( $previous ) {
        update_tokens( $self, $previous );
    }
    my $header = $res->header( 'Set-Cookie' );
    if ( defined( $header ) ) {
        my ( @cookies ) = split( ',', $header );
        foreach( @cookies ) {
            $_ =~ s/^\s*//;
            if ( $_ =~ /(.*?)=(.*?);/ ) {
                if ( $2 eq '' ) {
                    delete( $self->{_cookies}->{$1} );
                } else {
                    unless ( $1 =~ /\s/ ) {
                        if ( $1 ne '' ) {
                            $self->{_cookies}->{$1} = $2;
                        } else {
                            $self->{_cookies}->{'Session'} = $2;
                        }
                    }
                }
            }
        }
        $self->{_cookie} = join( '; ', map{ "$_=$self->{_cookies}->{$_}"; }( sort keys %{ $self->{_cookies} } ) );
    }
}

sub get_page {
    my ( $self ) = shift;
    my ( %args ) = (
        search  => 'all',
        view    => 'tl',
        start   => 0,
        method  => '',
        req_url => $self->{_mail_url},
        @_, );
    my ( $res, $req, $req_url, @tees );

    unless ( check_login( $self ) ) { return };

    if ( defined( $args{ 'label' } ) ) {
        $args{ 'label' } = validate_label( $self, $args{ 'label' } );
        if ( $self->error ) {
            return;
        } else {
            $args{ 'cat' } = $args{ 'label' };
            delete( $args{ 'label' } );
            $args{ 'search' } = 'cat';
        }
    }

    if ( defined( $args{ 't' } ) ) {
        if ( ref( $args{ 't' } ) eq 'ARRAY' ) {
            foreach ( @{ $args{ 't' } } ) {
                push( @tees, 't' );
                push( @tees, $_ );
            }
            delete( $args{ 't' } );

lib/Mail/Webmail/Gmail.pm  view on Meta::CPAN

        @_,
    );

    if ( $args{ 'url' } ) {
        $args{ 'url' } =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack( "C", hex( $1 ) )/eg;
        return( $args{ 'url' } );
    } else {
        $self->{_error} = 1;
        $self->{_err_str} .= "Error: Must supply URL to unencode.";
        return;
    }
}

sub get_attachment {
    my ( $self ) = shift;
    my ( %args ) = (
        view   => 'att',
        disp   => 'attd',
        search => '',
        @_, );

    if ( defined( $args{ 'attid' } ) && defined( $args{ 'msgid' } ) ) {
        $args{ 'th' } = $args{ 'msgid' };
        delete( $args{ 'msgid' } );
    } elsif ( defined( $args{ 'attachment' } ) ) {
        if ( defined( $args{ 'attachment' }->{ 'id' } ) ) {
            $args{ 'attid' } = $args{ 'attachment' }->{ 'id' };
        } else {
            $self->{_error} = 1;
            $self->{_err_str} .= "Error: Not a valid attachment.1\n";
            return;
        }
        if ( defined( $args{ 'attachment' }->{ 'th' } ) ) {
            $args{ 'th' } = $args{ 'attachment' }->{ 'th' };
        } else {
            $self->{_error} = 1;
            $self->{_err_str} .= "Error: Not a valid attachment.2\n";
            return;
        }
        delete( $args{ 'attachment' } );        
    } else {
        $self->{_error} = 1;
        $self->{_err_str} .= "Error: Must supply attid and msgid or a reference to an attachment through 'attachment'.\n";
        return;
    }

    unless ( check_login( $self ) ) { return };
    
    my $res = get_page( $self, %args );

    if ( $res->is_success() ) {
        my $attachment = $res->content();
        return( \$attachment );
    } else {
        $self->{_error} = 1;
        $self->{_err_str} .= "Error: While requesting attachment: '$res->{_request}->{_uri}'.\n";
        return;
    }
}

sub update_prefs {
    my ( $self ) = shift;
    my ( %args ) = (
        view         => 'tl',
        act          => 'prefs',
        search       => 'inbox',
        @_, );

    unless ( check_login( $self ) ) { return };

    $args{ 'at' } = $self->{_cookies}->{GMAIL_AT};

    my ( %pref_mappings ) = (
        bx_hs => 'keyboard_shortcuts',
        ix_nt => 'max_page_size',
        bx_sc => 'indicators',
        sx_dn => 'display_name',
        bx_ns => 'snippets',
        sx_rt => 'reply_to',
        sx_sg => 'signature', );

    my ( %pref_args ) = (
        view    => 'pr',
        pnl     => 'g',
        search  => '',
        start   => '',
        method  => '',
    );

    my $pref_res = get_page( $self, %pref_args );

    if ( $pref_res->is_success() ) {
        my %functions = %{ parse_page( $self, $pref_res ) };

        if ( $self->{_error} ) {
            return;
        }

        unless ( defined( $functions{ 'p' } ) ) {
            return;
        }

        ### Delete if equal to the string '' ###
        foreach ( 'signature', 'reply_to', 'display_name' ) {
            if ( defined( $args{ $_ } ) ) {
                if ( $args{ $_ } eq '' ) {
                    $args{ $_ } = '%0A%0D';
                }
            }
        }

        ### Load Prefs if not redefined ###
        foreach ( @{ $functions{ 'p' } } ) {
            my ( @setting ) = @{ extract_fields( $_ ) };
            foreach ( @setting ) {
                $_ = remove_quotes( $_ );
            }
            unless ( defined( $args{ $pref_mappings{ $setting[0] } } ) ) {
                $args{ 'p_' . $setting[0] } = $setting[1];
            } else {
                $args{ 'p_' . $setting[0] } = $args{ $pref_mappings{ $setting[0] } };



( run in 3.301 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )