| 1 | # | ||||||||||||||
| 2 | # Mail/Salsa/Utils.pm | ||||||||||||||
| 3 | # Last Modification: Thu Nov 13 15:09:09 WET 2008 | ||||||||||||||
| 4 | # | ||||||||||||||
| 5 | # Copyright (c) 2008 Henrique Dias <henrique.ribeiro.dias@gmail.com>. | ||||||||||||||
| 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 | ▶ | use 5.008000; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 10 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 10 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | 10 | ▶ | use Digest::MD5 qw(md5_hex); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | 10 | ▶ | use Mail::Salsa::Logs qw(logs); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | 10 | ▶ | use Mail::Salsa::Sendmail; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 18 | 10 | ▶ | use Mail::Salsa::Template; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 19 | 10 | ▶ | use Sys::Hostname; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 20 | 10 | ▶ | use Socket; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 21 | |||||||||||||||
| 22 | 10 | require Exporter; | |||||||||||||
| 23 | 10 | ▶ | use AutoLoader qw(AUTOLOAD); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 24 | |||||||||||||||
| 25 | 10 | 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 | 10 | our %EXPORT_TAGS = ( 'all' => [ qw() ] ); | |||||||||||||
| 36 | 10 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||||||
| 37 | 10 | our @EXPORT = qw(&file_path &generate_id &string_date &host_addresses &create_file &email_components &make_dir_rec); | |||||||||||||
| 38 | |||||||||||||||
| 39 | 10 | our $VERSION = '0.05'; | |||||||||||||
| 40 | |||||||||||||||
| 41 | 10 | my @patterns = ( | |||||||||||||
| 42 | '[^\<\>\@\(\)]+', | ||||||||||||||
| 43 | '[^\000-\037\300-\377\@<>(),;:\s]+\@([\w\-]+\.)+[a-zA-Z]{2,4}' | ||||||||||||||
| 44 | ); | ||||||||||||||
| 45 | |||||||||||||||
| 46 | 0 | ▶ | sub create_file { | ||||||||||||
|
Subroutine
create_file: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||
| 47 | 0 | my $file = shift; | |||||||||||||
| 48 | 0 | my $data = shift; | |||||||||||||
| 49 | 0 | ▶ | my $mode = shift || 0644; | ||||||||||||
|
Condition0%0/2
shift() || 420
MC/DC0%0/1
shift() || 420
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||
| 50 | |||||||||||||||
| 51 | 0 | ▶ | open(FILE, ">", $file) or die("$!"); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 52 | 0 | print FILE $data; | |||||||||||||
| 53 | 0 | close(FILE); | |||||||||||||
| 54 | |||||||||||||||
| 55 | 0 | chmod($mode, $file); | |||||||||||||
| 56 | 0 | return(); | |||||||||||||
| 57 | } | ||||||||||||||
| 58 | |||||||||||||||
| 59 | 0 | ▶ | sub lookup4key { | ||||||||||||
|
Subroutine
lookup4key: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||
| 60 | 0 | my $filename = shift; | |||||||||||||
| 61 | 0 | my $key = shift; | |||||||||||||
| 62 | |||||||||||||||
| 63 | 0 | my $exist = 0; | |||||||||||||
| 64 | 0 | ▶ | open(FILE, "<", $filename) or die("$!"); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 65 | 0 | ▶ | while(<FILE>) { if(/\b$key\b/) { $exist = 1; last; } } | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 66 | 0 | close(FILE); | |||||||||||||
| 67 | |||||||||||||||
| 68 | 0 | return($exist); | |||||||||||||
| 69 | } | ||||||||||||||
| 70 | |||||||||||||||
| 71 | 0 | ▶ | sub host_addresses { | ||||||||||||
|
Subroutine
host_addresses: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 72 | 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 | 0 | ▶ | sub clean_dir { | ||||||||||||
|
Subroutine
clean_dir: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||
| 80 | 0 | my $dir = shift; | |||||||||||||
| 81 | |||||||||||||||
| 82 | 0 | my @files = (); | |||||||||||||
| 83 | 0 | ▶ | opendir(DIRECTORY, $dir) or return("Can't opendir $dir: $!\n"); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 84 | 0 | ▶ | while(defined(my $file = readdir(DIRECTORY))) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 85 | 0 | ▶ | next if($file =~ /^\.\.?$/); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 86 | 0 | push(@files, "$dir/$file"); | |||||||||||||
| 87 | } | ||||||||||||||
| 88 | 0 | closedir(DIRECTORY); | |||||||||||||
| 89 | 0 | for my $file (@files) { | |||||||||||||
| 90 | 0 | ▶ | if(my ($f) = ($file =~ /^(.+)$/)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 91 | 0 | ▶ | unlink($f) or return("Could not unlink $f: $!"); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 92 | } | ||||||||||||||
| 93 | } | ||||||||||||||
| 94 | 0 | ▶ | rmdir($dir) or return("Couldn't remove dir $dir: $!"); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 95 | 0 | return(); | |||||||||||||
| 96 | } | ||||||||||||||
| 97 | |||||||||||||||
| 98 | 0 | ▶ | sub make_dir_rec { | ||||||||||||
|
Subroutine
make_dir_rec: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||
| 99 | 0 | my $path = shift; | |||||||||||||
| 100 | 0 | ▶ | my $mode = shift || 0755; | ||||||||||||
|
Condition0%0/2
shift() || 493
MC/DC0%0/1
shift() || 493
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||
| 101 | |||||||||||||||
| 102 | 0 | ▶ | !index($path, "/") or die("Not full path to directory \"$path\""); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 103 | 0 | my $tmp = ""; | |||||||||||||
| 104 | 0 | for my $dir (split(/\//, $path)) { | |||||||||||||
| 105 | 0 | ▶ | $dir or next; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 106 | 0 | $tmp = join("/", $tmp, $dir); | |||||||||||||
| 107 | 0 | ▶ | (-d $tmp) or &make_dir($tmp, $mode); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 108 | } | ||||||||||||||
| 109 | 0 | return(); | |||||||||||||
| 110 | } | ||||||||||||||
| 111 | |||||||||||||||
| 112 | 0 | ▶ | sub make_dir { | ||||||||||||
|
Subroutine
make_dir: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||
| 113 | 0 | my $dir = shift; | |||||||||||||
| 114 | 0 | ▶ | my $mode = shift || 0755; | ||||||||||||
|
Condition0%0/2
shift() || 493
MC/DC0%0/1
shift() || 493
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||
| 115 | |||||||||||||||
| 116 | 0 | umask(0); | |||||||||||||
| 117 | 0 | ▶ | mkdir($dir, $mode) or die("Failed to create directory \"$dir\" $!"); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 118 | 0 | return(); | |||||||||||||
| 119 | } | ||||||||||||||
| 120 | |||||||||||||||
| 121 | 0 | ▶ | sub string_date { | ||||||||||||
|
Subroutine
string_date: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 122 | 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 | 0 | ▶ | sub email_components { | ||||||||||||
|
Subroutine
email_components: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||
| 128 | 0 | local $_ = shift; | |||||||||||||
| 129 | |||||||||||||||
| 130 | 0 | ▶ | /^($patterns[0]) +<($patterns[1])>\s+/ and return({'username' => $1, 'address' => $2}); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 131 | 0 | ▶ | /^<?($patterns[1])>?\s+/ and return({'address' => $1}); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 132 | 0 | return({}); | |||||||||||||
| 133 | } | ||||||||||||||
| 134 | |||||||||||||||
| 135 | 0 | ▶ | sub only_addresses { | ||||||||||||
|
Subroutine
only_addresses: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 136 | 0 | ▶ | for(my $i=0; $i < scalar(@{$_[0]}); $i++) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 137 | 0 | $_[0]->[$i] =~ /\<?($patterns[1])\>?/; | |||||||||||||
| 138 | 0 | $_[0]->[$i] = $1; | |||||||||||||
| 139 | } | ||||||||||||||
| 140 | 0 | return(); | |||||||||||||
| 141 | } | ||||||||||||||
| 142 | |||||||||||||||
| 143 | 0 | ▶ | sub check4email { | ||||||||||||
|
Subroutine
check4email: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||
| 144 | 0 | my $array = shift; | |||||||||||||
| 145 | 0 | my $file = shift; | |||||||||||||
| 146 | |||||||||||||||
| 147 | 0 | my @emexist = (); | |||||||||||||
| 148 | 0 | my %hash = (); | |||||||||||||
| 149 | 0 | @hash{@{$array}} = (0 .. $#{$array}); | |||||||||||||
| 150 | 0 | ▶ | open(LIST, "<", $file) or die("$!"); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 151 | 0 | ▶ | while(<LIST>) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 152 | 0 | ▶ | next if(/^\#/); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 153 | 0 | s/[\r\n]+$//; | |||||||||||||
| 154 | 0 | /\<?($patterns[1])\>?/o; | |||||||||||||
| 155 | 0 | ▶ | $1 or next; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 156 | 0 | ▶ | push(@emexist, $1) if(exists($hash{$1})); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 157 | } | ||||||||||||||
| 158 | 0 | close(LIST); | |||||||||||||
| 159 | 0 | return(\@emexist); | |||||||||||||
| 160 | } | ||||||||||||||
| 161 | |||||||||||||||
| 162 | 0 | ▶ | sub get_key { | ||||||||||||
|
Subroutine
get_key: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 163 | 0 | my $file = shift; | |||||||||||||
| 164 | |||||||||||||||
| 165 | 0 | ▶ | open(KEY, "<", $file) or return(""); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 166 | 0 | chomp(my $key = <KEY>); | |||||||||||||
| 167 | 0 | close(KEY); | |||||||||||||
| 168 | 0 | return($key); | |||||||||||||
| 169 | } | ||||||||||||||
| 170 | |||||||||||||||
| 171 | 0 | ▶ | sub file_path { | ||||||||||||
|
Subroutine
file_path: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 172 | 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 | 0 | ▶ | sub generate_id { | ||||||||||||
|
Subroutine
generate_id: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 181 | 0 | ▶ | my $size = shift || 16; | ||||||||||||
|
Condition0%0/2
shift() || 16
MC/DC0%0/1
shift() || 16
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||
| 182 | 0 | return(substr(md5_hex(time(). {}. rand(). $$. 'prelin'), 0, $size)); | |||||||||||||
| 183 | } | ||||||||||||||
| 184 | |||||||||||||||
| 185 | 0 | ▶ | sub tplsendmail { | ||||||||||||
|
Subroutine
tplsendmail: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 186 | 0 | my $param = { | |||||||||||||
| 187 | smtp_server => ["localhost"], | ||||||||||||||
| 188 | label => undef, | ||||||||||||||
| 189 | lang => "en", | ||||||||||||||
| 190 | vars => {}, | ||||||||||||||
| 191 | @_, | ||||||||||||||
| 192 | }; | ||||||||||||||
| 193 | |||||||||||||||
| 194 | 0 | ▶ | my $refsub = sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 195 | 0 | my $handle = shift; | |||||||||||||
| 196 | my $tpl = Mail::Salsa::Template->new( | ||||||||||||||
| 197 | lang => $param->{'lang'}, | ||||||||||||||
| 198 | 0 | label => $param->{'label'}, | |||||||||||||
| 199 | outfh => $handle, | ||||||||||||||
| 200 | ); | ||||||||||||||
| 201 | 0 | $tpl->replace(%{$param->{'vars'}}); | |||||||||||||
| 202 | 0 | }; | |||||||||||||
| 203 | |||||||||||||||
| 204 | my $sm = Mail::Salsa::Sendmail->new( | ||||||||||||||
| 205 | smtp_server => $param->{'smtp_server'} || ["localhost"], | ||||||||||||||
| 206 | 0 | ▶ | timeout => $param->{'timeout'} | ||||||||||||
|
Condition0%0/2
$param->{'smtp_server'} || ['localhost']
MC/DC0%0/1
$param->{'smtp_server'} || ['localhost']
$param->{'smtp_server'}
Truth table0%0/2
A
A= $param->{'smtp_server'}
| |||||||||||||||
| 207 | ); | ||||||||||||||
| 208 | $sm->everything( | ||||||||||||||
| 209 | mail_from => $param->{'vars'}->{'from'}, | ||||||||||||||
| 210 | 0 | 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 | 10 | 1; | |||||||||||||
| 219 | __END__ | ||||||||||||||