PageRenderTime 64ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Qpsmtpd/Transaction.pm

http://github.com/smtpd/qpsmtpd
Perl | 424 lines | 349 code | 59 blank | 16 comment | 34 complexity | 3df4406d10b085a1ef8e0f08e29eee1c MD5 | raw file
  1. package Qpsmtpd::Transaction;
  2. use strict;
  3. use warnings;
  4. use parent 'Qpsmtpd';
  5. use Qpsmtpd::Constants;
  6. use IO::File qw(O_RDWR O_CREAT);
  7. use Socket qw(inet_aton);
  8. use Sys::Hostname;
  9. use Time::HiRes qw(gettimeofday);
  10. sub new { start(@_) }
  11. sub start {
  12. my $proto = shift;
  13. my $class = ref($proto) || $proto;
  14. my %args = @_;
  15. my $self = {_rcpt => [], started => time,};
  16. bless($self, $class);
  17. return $self;
  18. }
  19. sub add_recipient {
  20. my ($self, $rcpt) = @_;
  21. push @{$self->{_recipients}}, $rcpt if $rcpt;
  22. }
  23. sub remove_recipient {
  24. my ($self, $rcpt) = @_;
  25. $self->{_recipients} =
  26. [grep { $_->address ne $rcpt->address } @{$self->{_recipients} || []}]
  27. if $rcpt;
  28. }
  29. sub recipients {
  30. my $self = shift;
  31. @_ and $self->{_recipients} = [@_];
  32. ($self->{_recipients} ? @{$self->{_recipients}} : ());
  33. }
  34. sub sender {
  35. my $self = shift;
  36. @_ and $self->{_sender} = shift;
  37. $self->{_sender};
  38. }
  39. sub header {
  40. my $self = shift;
  41. @_ and $self->{_header} = shift;
  42. $self->{_header};
  43. }
  44. # blocked() will return when we actually can do something useful with it...
  45. #sub blocked {
  46. # my $self = shift;
  47. # carp 'Use of transaction->blocked is deprecated;'
  48. # . 'tell ask@develooper.com if you have a reason to use it';
  49. # @_ and $self->{_blocked} = shift;
  50. # $self->{_blocked};
  51. #}
  52. sub notes {
  53. my ($self, $key) = (shift, shift);
  54. # Check for any additional arguments passed by the caller -- including undef
  55. return $self->{_notes}->{$key} unless @_;
  56. return $self->{_notes}->{$key} = shift;
  57. }
  58. sub set_body_start {
  59. my $self = shift;
  60. $self->{_body_start} = $self->body_current_pos;
  61. if ($self->{_body_file}) {
  62. $self->{_header_size} = $self->{_body_start};
  63. }
  64. else {
  65. $self->{_header_size} = 0;
  66. if ($self->{_body_array}) {
  67. foreach my $line (@{$self->{_body_array}}) {
  68. $self->{_header_size} += length($line);
  69. }
  70. }
  71. }
  72. }
  73. sub body_start {
  74. my $self = shift;
  75. @_ and die "body_start now read only";
  76. $self->{_body_start};
  77. }
  78. sub body_current_pos {
  79. my $self = shift;
  80. if ($self->{_body_file}) {
  81. return tell($self->{_body_file});
  82. }
  83. return $self->{_body_current_pos} || 0;
  84. }
  85. sub body_filename {
  86. my $self = shift;
  87. $self->body_spool() unless $self->{_filename};
  88. $self->{_body_file}->flush(); # so contents won't be cached
  89. return $self->{_filename};
  90. }
  91. sub body_spool {
  92. my $self = shift;
  93. $self->log(LOGINFO, "spooling message to disk");
  94. $self->{_filename} = $self->temp_file();
  95. $self->{_body_file} =
  96. IO::File->new($self->{_filename}, O_RDWR | O_CREAT, 0600)
  97. or die "Could not open file $self->{_filename} - $! "
  98. ; # . $self->{_body_file}->error;
  99. if ($self->{_body_array}) {
  100. foreach my $line (@{$self->{_body_array}}) {
  101. $self->{_body_file}->print($line)
  102. or die "Cannot print to temp file: $!";
  103. }
  104. $self->{_body_start} = $self->{_header_size};
  105. }
  106. else {
  107. $self->log(LOGERROR, "no message body");
  108. }
  109. $self->{_body_array} = undef;
  110. }
  111. sub body_write {
  112. my $self = shift;
  113. my $data = shift;
  114. if ($self->{_body_file}) {
  115. #warn("body_write to file\n");
  116. # go to the end of the file
  117. seek($self->{_body_file}, 0, 2)
  118. unless $self->{_body_file_writing};
  119. $self->{_body_file_writing} = 1;
  120. $self->{_body_file}->print(ref $data eq "SCALAR" ? $$data : $data)
  121. and $self->{_body_size} +=
  122. length(ref $data eq "SCALAR" ? $$data : $data);
  123. }
  124. else {
  125. #warn("body_write to array\n");
  126. $self->{_body_array} ||= [];
  127. my $ref = ref($data) eq "SCALAR" ? $data : \$data;
  128. pos($$ref) = 0;
  129. while ($$ref =~ m/\G(.*?\n)/gc) {
  130. push @{$self->{_body_array}}, $1;
  131. $self->{_body_size} += length($1);
  132. ++$self->{_body_current_pos};
  133. }
  134. if ($$ref =~ m/\G(.+)\z/gc) {
  135. push @{$self->{_body_array}}, $1;
  136. $self->{_body_size} += length($1);
  137. ++$self->{_body_current_pos};
  138. }
  139. $self->body_spool if ($self->{_body_size} >= $self->size_threshold());
  140. }
  141. }
  142. sub body_size { # deprecated, use data_size() instead
  143. my $self = shift;
  144. $self->log(LOGWARN,
  145. "WARNING: body_size() is deprecated, use data_size() instead");
  146. $self->{_body_size} || 0;
  147. }
  148. sub data_size {
  149. shift->{_body_size} || 0;
  150. }
  151. sub body_length {
  152. my $self = shift;
  153. $self->{_body_size} or return 0;
  154. $self->{_header_size} or return 0;
  155. return $self->{_body_size} - $self->{_header_size};
  156. }
  157. sub body_resetpos {
  158. my $self = shift;
  159. if ($self->{_body_file}) {
  160. my $start = $self->{_body_start} || 0;
  161. seek($self->{_body_file}, $start, 0);
  162. $self->{_body_file_writing} = 0;
  163. }
  164. else {
  165. $self->{_body_current_pos} = $self->{_body_start};
  166. }
  167. 1;
  168. }
  169. sub body_getline {
  170. my $self = shift;
  171. if ($self->{_body_file}) {
  172. my $start = $self->{_body_start} || 0;
  173. seek($self->{_body_file}, $start, 0)
  174. if $self->{_body_file_writing};
  175. $self->{_body_file_writing} = 0;
  176. my $line = $self->{_body_file}->getline;
  177. return $line;
  178. }
  179. else {
  180. return unless $self->{_body_array};
  181. $self->{_body_current_pos} ||= 0;
  182. my $line = $self->{_body_array}->[$self->{_body_current_pos}];
  183. $self->{_body_current_pos}++;
  184. return $line;
  185. }
  186. }
  187. sub body_as_string {
  188. my $self = shift;
  189. $self->body_resetpos;
  190. local $/;
  191. my $str = '';
  192. while (defined(my $line = $self->body_getline)) {
  193. $str .= $line;
  194. }
  195. return $str;
  196. }
  197. sub body_fh {
  198. return shift->{_body_file};
  199. }
  200. sub dup_body_fh {
  201. my ($self) = @_;
  202. open(my $fh, '<&=', $self->body_fh);
  203. return $fh;
  204. }
  205. sub DESTROY {
  206. my $self = shift;
  207. # would we save some disk flushing if we unlinked the file before
  208. # closing it?
  209. $self->log(LOGDEBUG, sprintf("DESTROY called by %s, %s, %s", (caller)));
  210. if ($self->{_body_file}) {
  211. undef $self->{_body_file};
  212. }
  213. if ($self->{_filename} and -e $self->{_filename}) {
  214. if (unlink $self->{_filename}) {
  215. $self->log(LOGDEBUG, "unlinked ", $self->{_filename});
  216. }
  217. else {
  218. $self->log(LOGERROR, "Could not unlink ",
  219. $self->{_filename}, ": $!");
  220. }
  221. }
  222. # These may not exist
  223. if ($self->{_temp_files}) {
  224. $self->log(LOGDEBUG, "Cleaning up temporary transaction files");
  225. foreach my $file (@{$self->{_temp_files}}) {
  226. next unless -e $file;
  227. unlink $file
  228. or $self->log(LOGERROR, "Could not unlink temporary file",
  229. $file, ": $!");
  230. }
  231. }
  232. # Ditto
  233. if ($self->{_temp_dirs}) {
  234. eval { use File::Path };
  235. $self->log(LOGDEBUG, "Cleaning up temporary directories");
  236. foreach my $dir (@{$self->{_temp_dirs}}) {
  237. rmtree($dir)
  238. or $self->log(LOGERROR, "Could not unlink temporary dir",
  239. $dir, ": $!");
  240. }
  241. }
  242. }
  243. 1;
  244. __END__
  245. =head1 NAME
  246. Qpsmtpd::Transaction - single SMTP session transaction data
  247. =head1 SYNOPSIS
  248. foreach my $recip ($transaction->recipients) {
  249. print "T", $recip->address, "\0";
  250. }
  251. =head1 DESCRIPTION
  252. Qpsmtpd::Transaction maintains a single SMTP session's data, including
  253. the envelope details and the mail header and body.
  254. The docs below cover using the C<$transaction> object from within plugins
  255. rather than constructing a C<Qpsmtpd::Transaction> object, because the
  256. latter is done for you by qpsmtpd.
  257. =head1 API
  258. =head2 add_recipient($recipient)
  259. This adds a new recipient (as in RCPT TO) to the envelope of the mail.
  260. The C<$recipient> is a C<Qpsmtpd::Address> object. See L<Qpsmtpd::Address>
  261. for more details.
  262. =head2 remove_recipient($recipient)
  263. This removes a recipient (as in RCPT TO) from the envelope of the mail.
  264. The C<$recipient> is a C<Qpsmtpd::Address> object. See L<Qpsmtpd::Address>
  265. for more details.
  266. =head2 recipients( )
  267. This returns a list of the current recipients in the envelope.
  268. Each recipient returned is a C<Qpsmtpd::Address> object.
  269. This method is also a setter. Pass in a list of recipients to change
  270. the recipient list to an entirely new list. Note that the recipients
  271. you pass in B<MUST> be C<Qpsmtpd::Address> objects.
  272. =head2 sender( [ ADDRESS ] )
  273. Get or set the sender (MAIL FROM) address in the envelope.
  274. The sender is a C<Qpsmtpd::Address> object.
  275. =head2 header( [ HEADER ] )
  276. Get or set the header of the email.
  277. The header is a <Mail::Header> object, which gives you access to all
  278. the individual headers using a simple API. e.g.:
  279. my $headers = $transaction->header();
  280. my $msgid = $headers->get('Message-Id');
  281. my $subject = $headers->get('Subject');
  282. =head2 notes( $key [, $value ] )
  283. Get or set a note on the transaction. This is a piece of data that you wish
  284. to attach to the transaction and read somewhere else. For example you can
  285. use this to pass data between plugins.
  286. Note though that these notes will be lost when a transaction ends, for
  287. example on a C<RSET> or after C<DATA> completes, so you might want to
  288. use the notes field in the C<Qpsmtpd::Connection> object instead.
  289. =head2 body_filename ( )
  290. Returns the temporary filename used to store the message contents; useful for
  291. virus scanners so that an additional copy doesn't need to be made.
  292. Calling C<body_filename()> also forces spooling to disk. A message is not
  293. spooled to disk if it's size is smaller than
  294. I<$self-E<gt>config("size_threshold")>, default threshold is 0, the sample
  295. config file sets this to 10000.
  296. =head2 body_write( $data )
  297. Write data to the end of the email.
  298. C<$data> can be either a plain scalar, or a reference to a scalar.
  299. =head2 body_size( )
  300. B<Deprecated>, Use I<data_size()> instead.
  301. =head2 data_size( )
  302. Get the current size of the email. Note that this is not the size of the
  303. message that will be queued, it is the size of what the client sent after
  304. the C<DATA> command. If you need the size that will be queued, use
  305. my $msg_len = length($transaction->header->as_string)
  306. + $transaction->body_length;
  307. The line above is of course only valid in I<hook_queue( )>, as other plugins
  308. may add headers and qpsmtpd will add it's I<Received:> header.
  309. =head2 body_length( )
  310. Get the current length of the body of the email. This length includes the
  311. empty line between the headers and the body. Until the client has sent
  312. some data of the body of the message (i.e. headers are finished and client
  313. sent the empty line) this will return 0.
  314. =head2 body_resetpos( )
  315. Resets the body filehandle to the start of the file (via C<seek()>).
  316. Use this function before every time you wish to process the entire
  317. body of the email to ensure that some other plugin has not moved the
  318. file pointer.
  319. =head2 body_getline( )
  320. Returns a single line of data from the body of the email.
  321. =head2 body_fh( )
  322. Returns the file handle to the temporary file of the email. This will return
  323. undef if the file is not opened (yet). In I<hook_data( )> or later you can
  324. force spooling to disk by calling I<$transaction-E<gt>body_filename>.
  325. =head2 dup_body_fh( )
  326. Returns a dup()'d file handle to the temporary file of the email. This can be
  327. useful if an external module may call close() on the filehandle that is passed
  328. to it. This should only be used for reads, as writing to a dup'd filehandle
  329. may have unintended consequences.
  330. =head1 SEE ALSO
  331. L<Mail::Header>, L<Qpsmtpd::Address>, L<Qpsmtpd::Connection>
  332. =cut