line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# Mail/Salsa/Utils.pm |
3
|
|
|
|
|
|
|
# Last Modification: Thu Nov 13 15:09:09 WET 2008 |
4
|
|
|
|
|
|
|
# |
5
|
|
|
|
|
|
|
# Copyright (c) 2008 Henrique Dias . |
6
|
|
|
|
|
|
|
# All rights reserved. |
7
|
|
|
|
|
|
|
# This module is free software; you can redistribute it and/or modify |
8
|
|
|
|
|
|
|
# it under the same terms as Perl itself. |
9
|
|
|
|
|
|
|
# |
10
|
|
|
|
|
|
|
package Mail::Salsa::Utils; |
11
|
|
|
|
|
|
|
|
12
|
10
|
|
|
10
|
|
33785
|
use 5.008000; |
|
10
|
|
|
|
|
32
|
|
|
10
|
|
|
|
|
396
|
|
13
|
10
|
|
|
10
|
|
65
|
use strict; |
|
10
|
|
|
|
|
16
|
|
|
10
|
|
|
|
|
280
|
|
14
|
10
|
|
|
10
|
|
49
|
use warnings; |
|
10
|
|
|
|
|
15
|
|
|
10
|
|
|
|
|
354
|
|
15
|
10
|
|
|
10
|
|
70
|
use Digest::MD5 qw(md5_hex); |
|
10
|
|
|
|
|
17
|
|
|
10
|
|
|
|
|
836
|
|
16
|
10
|
|
|
10
|
|
2943
|
use Mail::Salsa::Logs qw(logs); |
|
10
|
|
|
|
|
30
|
|
|
10
|
|
|
|
|
594
|
|
17
|
10
|
|
|
10
|
|
5820
|
use Mail::Salsa::Sendmail; |
|
10
|
|
|
|
|
35
|
|
|
10
|
|
|
|
|
535
|
|
18
|
10
|
|
|
10
|
|
6368
|
use Mail::Salsa::Template; |
|
10
|
|
|
|
|
28
|
|
|
10
|
|
|
|
|
501
|
|
19
|
10
|
|
|
10
|
|
9441
|
use Sys::Hostname; |
|
10
|
|
|
|
|
15799
|
|
|
10
|
|
|
|
|
594
|
|
20
|
10
|
|
|
10
|
|
73
|
use Socket; |
|
10
|
|
|
|
|
18
|
|
|
10
|
|
|
|
|
8479
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
require Exporter; |
23
|
10
|
|
|
10
|
|
267
|
use AutoLoader qw(AUTOLOAD); |
|
10
|
|
|
|
|
21
|
|
|
10
|
|
|
|
|
87
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# Items to export into callers namespace by default. Note: do not export |
28
|
|
|
|
|
|
|
# names by default without a very good reason. Use EXPORT_OK instead. |
29
|
|
|
|
|
|
|
# Do not simply export all your public functions/methods/constants. |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# This allows declaration use Mail::Salsa::Utils ':all'; |
32
|
|
|
|
|
|
|
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK |
33
|
|
|
|
|
|
|
# will save memory. |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( 'all' => [ qw() ] ); |
36
|
|
|
|
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
37
|
|
|
|
|
|
|
our @EXPORT = qw(&file_path &generate_id &string_date &host_addresses &create_file &email_components &make_dir_rec); |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
our $VERSION = '0.05'; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
my @patterns = ( |
42
|
|
|
|
|
|
|
'[^\<\>\@\(\)]+', |
43
|
|
|
|
|
|
|
'[^\000-\037\300-\377\@<>(),;:\s]+\@([\w\-]+\.)+[a-zA-Z]{2,4}' |
44
|
|
|
|
|
|
|
); |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub create_file { |
47
|
0
|
|
|
0
|
0
|
|
my $file = shift; |
48
|
0
|
|
|
|
|
|
my $data = shift; |
49
|
0
|
|
0
|
|
|
|
my $mode = shift || 0644; |
50
|
|
|
|
|
|
|
|
51
|
0
|
0
|
|
|
|
|
open(FILE, ">", $file) or die("$!"); |
52
|
0
|
|
|
|
|
|
print FILE $data; |
53
|
0
|
|
|
|
|
|
close(FILE); |
54
|
|
|
|
|
|
|
|
55
|
0
|
|
|
|
|
|
chmod($mode, $file); |
56
|
0
|
|
|
|
|
|
return(); |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub lookup4key { |
60
|
0
|
|
|
0
|
0
|
|
my $filename = shift; |
61
|
0
|
|
|
|
|
|
my $key = shift; |
62
|
|
|
|
|
|
|
|
63
|
0
|
|
|
|
|
|
my $exist = 0; |
64
|
0
|
0
|
|
|
|
|
open(FILE, "<", $filename) or die("$!"); |
65
|
0
|
0
|
|
|
|
|
while() { if(/\b$key\b/) { $exist = 1; last; } } |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
|
close(FILE); |
67
|
|
|
|
|
|
|
|
68
|
0
|
|
|
|
|
|
return($exist); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub host_addresses { |
72
|
0
|
|
|
0
|
0
|
|
my $hostname = hostname(); |
73
|
0
|
|
|
|
|
|
my $iaddr = gethostbyname($hostname); |
74
|
0
|
|
|
|
|
|
my $ip_addrs = inet_ntoa($iaddr); |
75
|
0
|
|
|
|
|
|
$hostname = gethostbyaddr($iaddr, AF_INET); |
76
|
0
|
|
|
|
|
|
return($ip_addrs, $hostname); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub clean_dir { |
80
|
0
|
|
|
0
|
0
|
|
my $dir = shift; |
81
|
|
|
|
|
|
|
|
82
|
0
|
|
|
|
|
|
my @files = (); |
83
|
0
|
0
|
|
|
|
|
opendir(DIRECTORY, $dir) or return("Can't opendir $dir: $!\n"); |
84
|
0
|
|
|
|
|
|
while(defined(my $file = readdir(DIRECTORY))) { |
85
|
0
|
0
|
|
|
|
|
next if($file =~ /^\.\.?$/); |
86
|
0
|
|
|
|
|
|
push(@files, "$dir/$file"); |
87
|
|
|
|
|
|
|
} |
88
|
0
|
|
|
|
|
|
closedir(DIRECTORY); |
89
|
0
|
|
|
|
|
|
for my $file (@files) { |
90
|
0
|
0
|
|
|
|
|
if(my ($f) = ($file =~ /^(.+)$/)) { |
91
|
0
|
0
|
|
|
|
|
unlink($f) or return("Could not unlink $f: $!"); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
} |
94
|
0
|
0
|
|
|
|
|
rmdir($dir) or return("Couldn't remove dir $dir: $!"); |
95
|
0
|
|
|
|
|
|
return(); |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub make_dir_rec { |
99
|
0
|
|
|
0
|
0
|
|
my $path = shift; |
100
|
0
|
|
0
|
|
|
|
my $mode = shift || 0755; |
101
|
|
|
|
|
|
|
|
102
|
0
|
0
|
|
|
|
|
!index($path, "/") or die("Not full path to directory \"$path\""); |
103
|
0
|
|
|
|
|
|
my $tmp = ""; |
104
|
0
|
|
|
|
|
|
for my $dir (split(/\//, $path)) { |
105
|
0
|
0
|
|
|
|
|
$dir or next; |
106
|
0
|
|
|
|
|
|
$tmp = join("/", $tmp, $dir); |
107
|
0
|
0
|
|
|
|
|
(-d $tmp) or &make_dir($tmp, $mode); |
108
|
|
|
|
|
|
|
} |
109
|
0
|
|
|
|
|
|
return(); |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub make_dir { |
113
|
0
|
|
|
0
|
0
|
|
my $dir = shift; |
114
|
0
|
|
0
|
|
|
|
my $mode = shift || 0755; |
115
|
|
|
|
|
|
|
|
116
|
0
|
|
|
|
|
|
umask(0); |
117
|
0
|
0
|
|
|
|
|
mkdir($dir, $mode) or die("Failed to create directory \"$dir\" $!"); |
118
|
0
|
|
|
|
|
|
return(); |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub string_date { |
122
|
0
|
|
|
0
|
0
|
|
my ($sec,$min,$hour,$mday,$mon,$year) = localtime(); |
123
|
0
|
|
|
|
|
|
return sprintf("%04d/%02d/%02d %02d:%02d:%02d", |
124
|
|
|
|
|
|
|
$year + 1900, $mon + 1, $mday, $hour, $min, $sec); |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
sub email_components { |
128
|
0
|
|
|
0
|
0
|
|
local $_ = shift; |
129
|
|
|
|
|
|
|
|
130
|
0
|
0
|
|
|
|
|
/^($patterns[0]) +<($patterns[1])>\s+/ and return({'username' => $1, 'address' => $2}); |
131
|
0
|
0
|
|
|
|
|
/^($patterns[1])>?\s+/ and return({'address' => $1}); |
132
|
0
|
|
|
|
|
|
return({}); |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
sub only_addresses { |
136
|
0
|
|
|
0
|
0
|
|
for(my $i=0; $i < scalar(@{$_[0]}); $i++) { |
|
0
|
|
|
|
|
|
|
137
|
0
|
|
|
|
|
|
$_[0]->[$i] =~ /\($patterns[1])\>?/; |
138
|
0
|
|
|
|
|
|
$_[0]->[$i] = $1; |
139
|
|
|
|
|
|
|
} |
140
|
0
|
|
|
|
|
|
return(); |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub check4email { |
144
|
0
|
|
|
0
|
0
|
|
my $array = shift; |
145
|
0
|
|
|
|
|
|
my $file = shift; |
146
|
|
|
|
|
|
|
|
147
|
0
|
|
|
|
|
|
my @emexist = (); |
148
|
0
|
|
|
|
|
|
my %hash = (); |
149
|
0
|
|
|
|
|
|
@hash{@{$array}} = (0 .. $#{$array}); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
150
|
0
|
0
|
|
|
|
|
open(LIST, "<", $file) or die("$!"); |
151
|
0
|
|
|
|
|
|
while() { |
152
|
0
|
0
|
|
|
|
|
next if(/^\#/); |
153
|
0
|
|
|
|
|
|
s/[\r\n]+$//; |
154
|
0
|
|
|
|
|
|
/\($patterns[1])\>?/o; |
155
|
0
|
0
|
|
|
|
|
$1 or next; |
156
|
0
|
0
|
|
|
|
|
push(@emexist, $1) if(exists($hash{$1})); |
157
|
|
|
|
|
|
|
} |
158
|
0
|
|
|
|
|
|
close(LIST); |
159
|
0
|
|
|
|
|
|
return(\@emexist); |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
sub get_key { |
163
|
0
|
|
|
0
|
0
|
|
my $file = shift; |
164
|
|
|
|
|
|
|
|
165
|
0
|
0
|
|
|
|
|
open(KEY, "<", $file) or return(""); |
166
|
0
|
|
|
|
|
|
chomp(my $key = ); |
167
|
0
|
|
|
|
|
|
close(KEY); |
168
|
0
|
|
|
|
|
|
return($key); |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
sub file_path { |
172
|
0
|
|
|
0
|
0
|
|
my $list = shift; |
173
|
0
|
|
|
|
|
|
my $list_dir = shift; |
174
|
0
|
|
|
|
|
|
my $file = shift; |
175
|
|
|
|
|
|
|
|
176
|
0
|
|
|
|
|
|
my ($name, $domain) = split(/\@/, $list); |
177
|
0
|
|
|
|
|
|
return(join("/", $list_dir, $domain, $name, $file)); |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
sub generate_id { |
181
|
0
|
|
0
|
0
|
0
|
|
my $size = shift || 16; |
182
|
0
|
|
|
|
|
|
return(substr(md5_hex(time(). {}. rand(). $$. 'prelin'), 0, $size)); |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
sub tplsendmail { |
186
|
0
|
|
|
0
|
0
|
|
my $param = { |
187
|
|
|
|
|
|
|
smtp_server => ["localhost"], |
188
|
|
|
|
|
|
|
label => undef, |
189
|
|
|
|
|
|
|
lang => "en", |
190
|
|
|
|
|
|
|
vars => {}, |
191
|
|
|
|
|
|
|
@_, |
192
|
|
|
|
|
|
|
}; |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
my $refsub = sub { |
195
|
0
|
|
|
0
|
|
|
my $handle = shift; |
196
|
0
|
|
|
|
|
|
my $tpl = Mail::Salsa::Template->new( |
197
|
|
|
|
|
|
|
lang => $param->{'lang'}, |
198
|
|
|
|
|
|
|
label => $param->{'label'}, |
199
|
|
|
|
|
|
|
outfh => $handle, |
200
|
|
|
|
|
|
|
); |
201
|
0
|
|
|
|
|
|
$tpl->replace(%{$param->{'vars'}}); |
|
0
|
|
|
|
|
|
|
202
|
0
|
|
|
|
|
|
}; |
203
|
|
|
|
|
|
|
|
204
|
0
|
|
0
|
|
|
|
my $sm = Mail::Salsa::Sendmail->new( |
205
|
|
|
|
|
|
|
smtp_server => $param->{'smtp_server'} || ["localhost"], |
206
|
|
|
|
|
|
|
timeout => $param->{'timeout'} |
207
|
|
|
|
|
|
|
); |
208
|
0
|
|
|
|
|
|
$sm->everything( |
209
|
|
|
|
|
|
|
mail_from => $param->{'vars'}->{'from'}, |
210
|
|
|
|
|
|
|
rcpt_to => [$param->{'vars'}->{'to'}], |
211
|
|
|
|
|
|
|
data => $refsub |
212
|
|
|
|
|
|
|
); |
213
|
0
|
|
|
|
|
|
return(); |
214
|
|
|
|
|
|
|
} |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
# Autoload methods go after =cut, and are processed by the autosplit program. |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
1; |
219
|
|
|
|
|
|
|
__END__ |