File Coverage

blib/lib/Mail/Salsa/Action/Personalize.pm
Criterion Covered Total %
statement 21 99 21.2
branch 0 32 0.0
condition 0 12 0.0
subroutine 7 12 58.3
pod 0 4 0.0
total 28 159 17.6


line stmt bran cond sub pod time code
1             #
2             # Mail/Salsa/Action/Personalize
3             # Last Modification: Wed Apr 6 16:09:58 WEST 2005
4             #
5             # Copyright (c) 2005 Henrique Dias . All rights reserved.
6             # This module is free software; you can redistribute it and/or modify
7             # it under the same terms as Perl itself.
8             #
9             package Mail::Salsa::Action::Personalize;
10              
11 1     1   51381 use 5.008000;
  1         4  
  1         82  
12 1     1   8 use strict;
  1         2  
  1         41  
13 1     1   5 use warnings;
  1         3  
  1         52  
14              
15             require Exporter;
16 1     1   917 use Mail::Salsa::Utils qw(file_path create_file generate_id email_components);
  1         3  
  1         137  
17 1     1   6 use Mail::Salsa::Logs qw(logs debug);
  1         1  
  1         53  
18 1     1   867 use Mail::Salsa::Archive qw(archive_msg);
  1         4  
  1         69  
19 1     1   973 use Mail::Salsa::Action::Post;
  1         4  
  1         1755  
20              
21             # Items to export into callers namespace by default. Note: do not export
22             # names by default without a very good reason. Use EXPORT_OK instead.
23             # Do not simply export all your public functions/methods/constants.
24              
25             # This allows declaration use Mail::Salsa ':all';
26             # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
27             # will save memory.
28              
29             our %EXPORT_TAGS = ( 'all' => [ qw() ] );
30             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
31             our @EXPORT = qw();
32             our $VERSION = '0.02';
33              
34             sub new {
35 0     0 0   my $proto = shift;
36 0   0       my $class = ref($proto) || $proto;
37 0           my $self = {@_};
38 0           bless ($self, $class);
39 0           $self->process_msg();
40 0           return($self);
41             }
42              
43             sub check_restrict {
44 0     0 0   my $self = shift;
45 0           $self->Mail::Salsa::Action::Post::check_restrict(@_);
46             }
47              
48             sub process_msg {
49 0     0 0   my $self = shift;
50              
51 0           my ($name, $domain) = split(/\@/, $self->{'list'});
52 0 0         unless($self->Mail::Salsa::Action::Post::check_restrict('restrict.txt', $self->{'headers'}->{'0.0'}->{'received'}, [])) {
53 0           Mail::Salsa::Utils::tplsendmail(
54             smtp_server => $self->{'smtp_server'},
55             timeout => $self->{'timeout'},
56             label => "PERMISSION_DENY",
57             lang => $self->{'config'}->{'language'},
58             vars => {
59             from => "$name\-owner\@$domain",
60             to => $self->{'from'},
61             list => $self->{'list'},
62             }
63             );
64 0           $self->logs(join("", "[permission deny] from: ", $self->{'from'}), "list");
65 0           return();
66             }
67              
68 0 0 0       if($self->{'config'}->{'max_message_size'} && (((-s $self->{'message'})/1024) > $self->{'config'}->{'max_message_size'})) {
69 0           Mail::Salsa::Utils::tplsendmail(
70             smtp_server => $self->{'smtp_server'},
71             timeout => $self->{'timeout'},
72             label => "MAX_MESSAGE_SIZE",
73             lang => $self->{'config'}->{'language'},
74             vars => {
75             from => "$name\-owner\@$domain",
76             to => $self->{'from'},
77             list => $self->{'list'},
78             size => $self->{'config'}->{'max_message_size'},
79             }
80             );
81 0           $self->logs(join("", "[max message size exceed] from: ", $self->{'from'}), "list");
82 0           return();
83             }
84 0 0         if(scalar(keys(%{$self->{'headers'}})) > 1) {
  0            
85 0           my $attachfile = file_path($self->{'list'}, $self->{'list_dir'}, "attachments.txt");
86 0           my $listfile = file_path($self->{'list'}, $self->{'list_dir'}, "list.txt");
87 0           my ($code, $access, $mime_type) = ("", "allow", "");
88 0 0         if(my $error = Mail::Salsa::Action::Post::generate_code($listfile, $attachfile, $self->{'from'}, $code)) {
89 0           $self->logs("[file] $error", "errors");
90 0           return();
91             }
92 0           eval($code);
93 0 0         if($@) {
94 0           $self->logs("[eval] $@", "errors");
95 0           return();
96             }
97 0 0         if($access eq "deny") {
98 0           Mail::Salsa::Utils::tplsendmail(
99             smtp_server => $self->{'smtp_server'},
100             timeout => $self->{'timeout'},
101             label => "NO_ATTACHMENTS",
102             lang => $self->{'config'}->{'language'},
103             vars => {
104             from => "$name\-owner\@$domain",
105             to => $self->{'from'},
106             list => $self->{'list'},
107             mime_type => $mime_type,
108             }
109             );
110 0           $self->logs(join("", "[deny attachment] mime-type: $mime_type from: ", $self->{'from'}), "list");
111 0           return();
112             }
113             }
114 0           $self->{'headers'}->{'0.0'}->{'to'} =~ s/\-personalize(?=\@)//;
115 0 0         $self->Mail::Salsa::Action::Post::setup_stamp() if($self->{'config'}->{'stamp'} eq "y");
116 0 0         my $reply = ($self->{'headers'}->{'0.0'}->{'subject'}->{'value'} =~ /^Re: /i) ? 1 : 0;
117 0           my $bounce = $self->Mail::Salsa::Action::Post::check4bounces();
118 0           my $human = $self->Mail::Salsa::Action::Post::setup_msg();
119 0 0         $human = 0 unless($self->{'stamp'});
120              
121 0 0 0       if($bounce == 2) {
    0 0        
    0          
122             # debug and test
123 0           $self->logs(join("", "[been-there] from: ", $self->{'from'}), "list");
124             } elsif($bounce && $self->{'config'}->{'accept_bounces'} eq "n") {
125 0           Mail::Salsa::Utils::tplsendmail(
126             smtp_server => $self->{'smtp_server'},
127             timeout => $self->{'timeout'},
128             label => "DONT_BOUNCE",
129             lang => $self->{'config'}->{'language'},
130             vars => {
131             from => "$name\-owner\@$domain",
132             to => $self->{'from'},
133             list => $self->{'list'},
134             }
135             );
136 0           $self->logs(join("", "[bounce] from: ", $self->{'from'}), "list");
137             } elsif($self->{'stamp'} && !$human) {
138 0           Mail::Salsa::Utils::tplsendmail(
139             smtp_server => $self->{'smtp_server'},
140             timeout => $self->{'timeout'},
141             label => "MAILSTAMP",
142             lang => $self->{'config'}->{'language'},
143             vars => {
144             from => "$name\-owner\@$domain",
145             to => $self->{'from'},
146             list => $self->{'list'},
147             stamp => $self->{'stamp'}
148             }
149             );
150 0           $self->logs(join("", "[sent stamp] to: ", $self->{'from'}), "list");
151             } else {
152 0           $self->sendmail4all();
153 0           $self->logs(join("", "[personalize message] from: ", $self->{'from'}), "list");
154 0 0         $self->archive_msg() if($self->{'config'}->{'archive'} eq "y");
155             }
156 0           unlink($self->{'message'});
157 0           return();
158             }
159              
160             sub sendmail4all {
161 0     0 0   my $self = shift;
162              
163 0           my ($name, $domain) = split(/\@/, $self->{'list'});
164 0           my $listfile = file_path($self->{'list'}, $self->{'list_dir'}, "list.txt");
165              
166 0           (my $outfile = $self->{'message'}) =~ s/\.msg$/\.out/;
167 0           open(LIST, "<", $listfile);
168 0           while() {
169 0           my $fullemail = email_components($_);
170 0 0         exists($fullemail->{'address'}) or next;
171 0 0         $fullemail->{'username'} = "Mailing List Subscriber" unless(exists($fullemail->{'username'}));
172 0           my ($username, $email) = ($fullemail->{'username'}, $fullemail->{'address'});
173              
174 0           my $sm = Mail::Salsa::Sendmail->new(
175             'smtp_server' => $self->{'smtp_server'},
176             'smtp_port' => 25,
177             'timeout' => $self->{'timeout'},
178             );
179 0           $sm->helo();
180 0           $sm->mail_from("$name\-return\@$domain");
181 0           $sm->rcpt_to('addresses' => [$email]);
182             $sm->data(sub {
183 0     0     my $handle = shift;
184 0 0         open(SENDFILE, "<", $outfile) or die("$!");
185 0           while() {
186 0           s/^To: +[^\n\r]+/To: $username \<$email\>/io;
187 0           s/\$FULLNAME\b/$username/o;
188 0           s/\$EMAIL\b/$email/o;
189 0           print $handle $_;
190             }
191 0           close(SENDFILE);
192 0           });
193 0           $sm->quit();
194             }
195 0           close(LIST);
196 0           unlink($outfile);
197              
198 0           return();
199             }
200              
201             # Autoload methods go after =cut, and are processed by the autosplit program.
202              
203             1;
204              
205             __END__