| 1 |
package Qpsmtpd::Connection; |
| 2 |
use strict; |
| 3 |
|
| 4 |
# All of these parameters depend only on the physical connection, |
| 5 |
# i.e. not on anything sent from the remote machine. Hence, they |
| 6 |
# are an appropriate set to use for either start() or clone(). Do |
| 7 |
# not add parameters here unless they also meet that criteria. |
| 8 |
my @parameters = qw( |
| 9 |
remote_host |
| 10 |
remote_ip |
| 11 |
remote_info |
| 12 |
remote_port |
| 13 |
local_ip |
| 14 |
local_port |
| 15 |
relay_client |
| 16 |
); |
| 17 |
|
| 18 |
sub new { |
| 19 |
my $proto = shift; |
| 20 |
my $class = ref($proto) || $proto; |
| 21 |
my $self = {}; |
| 22 |
bless($self, $class); |
| 23 |
} |
| 24 |
|
| 25 |
sub start { |
| 26 |
my $self = shift; |
| 27 |
$self = $self->new(@_) unless ref $self; |
| 28 |
|
| 29 |
my %args = @_; |
| 30 |
|
| 31 |
foreach my $f (@parameters) { |
| 32 |
$self->$f($args{$f}) if $args{$f}; |
| 33 |
} |
| 34 |
|
| 35 |
return $self; |
| 36 |
} |
| 37 |
|
| 38 |
sub clone { |
| 39 |
my $self = shift; |
| 40 |
my %args = @_; |
| 41 |
my $new = $self->new(); |
| 42 |
foreach my $f (@parameters) { |
| 43 |
$new->$f($self->$f()) if $self->$f(); |
| 44 |
} |
| 45 |
$new->{_notes} = $self->{_notes} if defined $self->{_notes}; |
| 46 |
|
| 47 |
# reset the old connection object like it's done at the end of a connection |
| 48 |
# to prevent leaks (like prefork/tls problem with the old SSL file handle |
| 49 |
# still around) |
| 50 |
$self->reset unless $args{no_reset}; |
| 51 |
|
| 52 |
# should we generate a new id here? |
| 53 |
return $new; |
| 54 |
} |
| 55 |
|
| 56 |
sub remote_host { |
| 57 |
my $self = shift; |
| 58 |
@_ and $self->{_remote_host} = shift; |
| 59 |
$self->{_remote_host}; |
| 60 |
} |
| 61 |
|
| 62 |
sub remote_ip { |
| 63 |
my $self = shift; |
| 64 |
@_ and $self->{_remote_ip} = shift; |
| 65 |
$self->{_remote_ip}; |
| 66 |
} |
| 67 |
|
| 68 |
sub remote_port { |
| 69 |
my $self = shift; |
| 70 |
@_ and $self->{_remote_port} = shift; |
| 71 |
$self->{_remote_port}; |
| 72 |
} |
| 73 |
|
| 74 |
sub local_ip { |
| 75 |
my $self = shift; |
| 76 |
@_ and $self->{_local_ip} = shift; |
| 77 |
$self->{_local_ip}; |
| 78 |
} |
| 79 |
|
| 80 |
sub local_port { |
| 81 |
my $self = shift; |
| 82 |
@_ and $self->{_local_port} = shift; |
| 83 |
$self->{_local_port}; |
| 84 |
} |
| 85 |
|
| 86 |
sub remote_info { |
| 87 |
my $self = shift; |
| 88 |
@_ and $self->{_remote_info} = shift; |
| 89 |
$self->{_remote_info}; |
| 90 |
} |
| 91 |
|
| 92 |
sub relay_client { |
| 93 |
my $self = shift; |
| 94 |
@_ and $self->{_relay_client} = shift; |
| 95 |
$self->{_relay_client}; |
| 96 |
} |
| 97 |
|
| 98 |
sub hello { |
| 99 |
my $self = shift; |
| 100 |
@_ and $self->{_hello} = shift; |
| 101 |
$self->{_hello}; |
| 102 |
} |
| 103 |
|
| 104 |
sub hello_host { |
| 105 |
my $self = shift; |
| 106 |
@_ and $self->{_hello_host} = shift; |
| 107 |
$self->{_hello_host}; |
| 108 |
} |
| 109 |
|
| 110 |
sub notes { |
| 111 |
my ($self, $key) = (shift, shift); |
| 112 |
|
| 113 |
# Check for any additional arguments passed by the caller -- including undef |
| 114 |
return $self->{_notes}->{$key} unless @_; |
| 115 |
return $self->{_notes}->{$key} = shift; |
| 116 |
} |
| 117 |
|
| 118 |
sub reset { |
| 119 |
my $self = shift; |
| 120 |
$self->{_notes} = undef; |
| 121 |
$self = $self->new; |
| 122 |
} |
| 123 |
|
| 124 |
1; |
| 125 |
|
| 126 |
__END__ |
| 127 |
|
| 128 |
=head1 NAME |
| 129 |
|
| 130 |
Qpsmtpd::Connection - A single SMTP connection |
| 131 |
|
| 132 |
=head1 SYNOPSIS |
| 133 |
|
| 134 |
my $rdns = $qp->connection->remote_host; |
| 135 |
my $ip = $qp->connection->remote_ip; |
| 136 |
|
| 137 |
=head1 DESCRIPTION |
| 138 |
|
| 139 |
This class contains details about an individual SMTP connection. A |
| 140 |
connection lasts the lifetime of a TCP connection to the SMTP server. |
| 141 |
|
| 142 |
See also L<Qpsmtpd::Transaction> which is a class containing details |
| 143 |
about an individual SMTP transaction. A transaction lasts from |
| 144 |
C<MAIL FROM> to the end of the C<DATA> marker, or a C<RSET> command, |
| 145 |
whichever comes first, whereas a connection lasts until the client |
| 146 |
disconnects. |
| 147 |
|
| 148 |
=head1 API |
| 149 |
|
| 150 |
These API docs assume you already have a connection object. See the |
| 151 |
source code if you need to construct one. You can access the connection |
| 152 |
object via the C<Qpsmtpd> object's C<< $qp->connection >> method. |
| 153 |
|
| 154 |
=head2 new ( ) |
| 155 |
|
| 156 |
Instantiates a new Qpsmtpd::Connection object. |
| 157 |
|
| 158 |
=head2 start ( %args ) |
| 159 |
|
| 160 |
Initializes the connection object with %args attribute data. |
| 161 |
|
| 162 |
=head2 remote_host( ) |
| 163 |
|
| 164 |
The remote host connecting to the server as looked up via reverse dns. |
| 165 |
|
| 166 |
=head2 remote_ip( ) |
| 167 |
|
| 168 |
The remote IP address of the connecting host. |
| 169 |
|
| 170 |
=head2 remote_port( ) |
| 171 |
|
| 172 |
The remote port. |
| 173 |
|
| 174 |
=head2 remote_info( ) |
| 175 |
|
| 176 |
If your server does an ident lookup on the remote host, this is the |
| 177 |
identity of the remote client. |
| 178 |
|
| 179 |
=head2 local_ip( ) |
| 180 |
|
| 181 |
The local ip. |
| 182 |
|
| 183 |
=head2 local_port( ) |
| 184 |
|
| 185 |
The local port. |
| 186 |
|
| 187 |
=head2 hello( ) |
| 188 |
|
| 189 |
Either C<"helo"> or C<"ehlo"> depending on how the remote client |
| 190 |
greeted your server. |
| 191 |
|
| 192 |
NOTE: This field is empty during the helo or ehlo hooks, it is only |
| 193 |
set after a successful return from those hooks. |
| 194 |
|
| 195 |
=head2 hello_host( ) |
| 196 |
|
| 197 |
The host name specified in the C<HELO> or C<EHLO> command. |
| 198 |
|
| 199 |
NOTE: This field is empty during the helo or ehlo hooks, it is only |
| 200 |
set after a successful return from those hooks. |
| 201 |
|
| 202 |
=head2 notes($key [, $value]) |
| 203 |
|
| 204 |
Get or set a note on the connection. This is a piece of data that you wish |
| 205 |
to attach to the connection and read somewhere else. For example you can |
| 206 |
use this to pass data between plugins. |
| 207 |
|
| 208 |
=head2 clone([%args]) |
| 209 |
|
| 210 |
Returns a copy of the Qpsmtpd::Connection object. The optional args parameter |
| 211 |
may contain: |
| 212 |
|
| 213 |
=over 4 |
| 214 |
|
| 215 |
=item no_reset (1|0) |
| 216 |
|
| 217 |
If true, do not reset the original connection object, the author has to care |
| 218 |
about that: only the cloned connection object is reset at the end of the |
| 219 |
connection |
| 220 |
|
| 221 |
=back |
| 222 |
|
| 223 |
=cut |
| 224 |
|
| 225 |
=head2 relay_client( ) |
| 226 |
|
| 227 |
True if the client is allowed to relay messages. |
| 228 |
|
| 229 |
=cut |