Changeset 43

Show
Ignore:
Timestamp:
02/07/06 18:42:50
Author:
miyagawa
Message:

hack more TLS connection persistency. Refs #15

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/plagger/lib/Plagger/Plugin/Publish/Gmail.pm

    r42 r43  
    1010use Encode::MIME::Header; 
    1111use MIME::Lite; 
     12 
     13our %TLSConn; 
    1214 
    1315sub register { 
     
    4951    my $route = $cfg->{mailroute} || { via => 'smtp', host => 'localhost' }; 
    5052    if ($route->{via} eq 'smtp_tls') { 
    51         $msg->send_by_smtp_tls( 
     53        $self->{tls_args} = [ 
    5254            $route->{host}, 
    5355            User     => $route->{username}, 
    5456            Password => $route->{password}, 
    5557            Port     => $route->{port} || 587, 
    56         ); 
     58        ]; 
     59        $msg->send_by_smtp_tls(@{ $self->{tls_args} }); 
    5760    } else { 
    5861        my @args  = $route->{host} ? ($route->{host}) : (); 
     
    7073} 
    7174 
     75sub DESTORY { 
     76    my $self = shift; 
     77    return unless $self->{tls_args}; 
     78 
     79    my $conn_key = join "|", @{ $self->{tls_args} }; 
     80    eval { 
     81        local $SIG{__WARN__} = sub { }; 
     82        $TLSConn{$conn_key} && $TLSConn{$conn_key}->quit; 
     83    }; 
     84 
     85    # known error from Gmail SMTP 
     86    if ($@ && $@ !~ /An error occurred disconnecting from the mail server/) { 
     87        warn $@; 
     88    } 
     89} 
    7290 
    7391# hack MIME::Lite to support TLS Authentication 
     
    96114    ### Create SMTP TLS client: 
    97115    require Net::SMTP::TLS; 
     116 
     117    my $conn_key = join "|", @args; 
    98118    my $smtp; 
    99     unless ($self->{__smtp}) { 
    100        $smtp = MIME::Lite::SMTP::TLS->new(@args) 
    101            or Carp::croak("Failed to connect to mail server: $!\n"); 
    102        $self->{__smtp} = $smtp; 
     119    unless ($smtp = $TLSConn{$conn_key}) { 
     120        $smtp = $TLSConn{$conn_key} = MIME::Lite::SMTP::TLS->new(@args) 
     121            or Carp::croak("Failed to connect to mail server: $!\n"); 
    103122    } 
    104123    $smtp->mail($from); 
     
    112131    1; 
    113132} 
    114 sub DESTORY { 
    115     my $self = shift; 
    116     eval { 
    117         local $SIG{__WARN__} = sub { }; 
    118         $self->{__smtp}->quit; 
    119     }; 
    120     # known error from Gmail SMTP 
    121     if ($@ && $@ !~ /An error occurred disconnecting from the mail server/) { 
    122         warn $@; 
    123     } 
    124 } 
    125133 
    126134package MIME::Lite::SMTP::TLS;