#!/usr/bin/perl =head1 NAME check_cookie - check for X-Cookie header =head1 DESCRIPTION Example plugin for cf_wrapper. This plugin checks if the mail has an X-Cookie header specified by the recipient. =head1 NOTES =over 4 =item 'recipient_options'->{check_cookie} The cookie to be checked. =back =head1 COPYRIGHT AND LICENSE Copyright (c) 2005-2006 Peter J. Holzer This plugin is licensed under the same terms as the qpsmtpd package itself. Please see the LICENSE file included with qpsmtpd for details. =cut sub register { my ($self, $qp, @args) = @_; $self->register_hook("rcpt", "rcpt_handler"); $self->register_hook("data_post", "data_post_handler"); } sub rcpt_handler { my ($self, $transaction, $rcpt) = @_; my $ro = $rcpt->notes('options'); if ($ro && $ro->{check_cookie}) { my $c = $transaction->notes('check_cookie'); $self->log(LOGINFO, "setting cookie for recipient ", $rcpt->address, " to ", $ro->{check_cookie}); $c->{$rcpt->address} = $ro->{check_cookie}; $transaction->notes('check_cookie', $c); } return DECLINED; } sub data_post_handler { my ($self, $transaction) = @_; my $cookie = $transaction->header->get('X-Cookie') || ''; $cookie =~ s/^\s*(.*?)\s*$/$1/s; $self->log(LOGINFO, "X-Cookie $cookie found in message"); my $results = $transaction->notes('cf_wrapper_results'); for ($transaction->recipients()) { my $r = $_->address; $self->log(LOGINFO, "checking cookie for recipient $r"); my $rc = $results->{$r}; if (!defined($rc) || $rc == DECLINED) { my $msg = "Ok"; if (defined($transaction->notes('check_cookie'))) { $self->log(LOGINFO, "check_cookie note exists"); my $c = ($transaction->notes('check_cookie')->{$r}); if (defined($c)) { $self->log(LOGINFO, "check_cookie note for $r is $c"); if ($c eq $cookie) { $rc = DECLINED; } else { $rc = DENY; $msg = "Please include 'X-Cookie: $c' in your message"; } } else { return DECLINED; } } else { $rc = DECLINED } $self->log(LOGINFO, "setting result for $r to $rc"); $results->{$r} = [$rc, $msg]; $transaction->notes('cf_wrapper_results', $results); } else { $self->log(LOGINFO, "$r already has result $rc, skipping"); } } return DECLINED; } #vim: sw=2 ts=8