blib/lib/Qmail/Mysql.pm

filter: Statementstmt 15.3% 28 / 182 Branchbran 14.2% 6 / 42 Conditioncond 3.7% 1 / 27 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 24.1% 7 / 29 Podpod 91.6% 11 / 12 totaltotal 17.1% 53 / 310 CC 33 SCAR 32.0
CC
33
Cov
14%
_parse_control
90.0
mail_add
56.0
_check_mbox_base
20.0
CRAP 24.6
↑ Summary
1 package Qmail::Mysql;
2  
3  
4 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 1 $Qmail::Mysql::VERSION = '0.02';
14  
15  
16 # Fields that can be set in new method, with defaults
17 1 my %fields =(
18 sql_control_file => '/var/qmail/control/sqlserver',
19 mailbox_base => '/var/spool/pop/users',
20 password_type => 'Password',
21 multihosting => 0,
22 multihosting_join => '@',
23 );
24  
25 sub new
26 1 ▶{
Subroutine new: called CC=8 SCAR=24.5
Pod undocumented
27 1 my ($proto,%options) = @_;
28 1 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
29 1 my $self = {
30 %fields};
31 1 ▶ while (my ($key,$value) = each(%options)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %options) 5 1 6
32 5 ▶ if (exists($fields{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $fields{$key}) { } 5 0 5
33 5 ▶ $self->{$key} = $value if (defined $value);
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 5 0 5
34 } else {
35 0 die $class . "::new: invalid option '$key'\n";
36 }
37 }
38 1 foreach (keys(%fields)) {
39 die $class . "::new: must specify value for $_"
40 5 ▶ if (!defined $self->{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$_} 0 5 5
41 }
42 die $class . "::new: Unable to file sql control file at " .
43 1 ▶ $self->{sql_control_file} if (!-e $self->{sql_control_file});
Branch: 1 branch
BranchTrueFalseTotal
unless -e $self->{"sql_control_file"} 1 0 1
44 0 bless $self, $class;
45 0 $self->_init;
46 0 return $self;
47 }
48  
49 0 ▶sub _init {
Subroutine _init: not called CC=1 SCAR=6.9
50 0 my $self = shift;
51 0 $self->_parse_control();
52 }
53  
54 0 ▶sub _parse_control() {
Subroutine _parse_control: not called CC=9 SCAR=45.0
55 0 my $self = shift;
56 open(SQLCTL,$self->{sql_control_file})
57 0 ▶ or die "Unable to open $self->{sql_control_file}";
Branch: 1 branch
BranchTrueFalseTotal
open SQLCTL, $self->{"sql_control_file"} or die qq[Unable to open $self->{"sql_control_file"}] 0 0 0
58 0 my $line;
59 0 ▶ while (<SQLCTL>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline SQLCTL)) 0 0 0
60 0 $line++;
61 0 s/^\s*//;
62 0 s/\s*^//;
63 0 my ($key,$val) = split(/\s+/);
64 0 ▶ die "Invalid control file format in " .
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key and defined $val 0 0 0
Condition0%0/3
defined $key and defined $val
!ll&&!rl&&r
000
MC/DC0%0/2
defined $key and defined $val
defined $key defined $val
Truth table0%0/3
A and B
A= defined $keyB= defined $val
ABresult
0X0
100
111
65 "$self->{sql_control_file} at line $line"
66 if (!defined $key || !defined $val);
67 0 $self->{_db}->{$key} = $val;
68 }
69 0 close(SQLCTL);
70 # check minimum data
71 die "DB Host name not found in $self->{sql_control_file}"
72 0 ▶ if (!exists $self->{_db}->{server});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_db"}{"server"} 0 0 0
73 die "DB login name not found in $self->{sql_control_file}"
74 0 ▶ if (!exists $self->{_db}->{login});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_db"}{"login"} 0 0 0
75 die "DB password not found in $self->{sql_control_file}"
76 0 ▶ if (!exists $self->{_db}->{password});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_db"}{"password"} 0 0 0
77 die "DB name not found in $self->{sql_control_file}"
78 0 ▶ if (!exists $self->{_db}->{db});
Branch: 1 branch
BranchTrueFalseTotal
if not exists $self->{"_db"}{"db"} 0 0 0
79 }
80  
81 0 ▶sub connect {
Subroutine connect: not called CC=2 SCAR=17.9
Pod documented
82 0 my $self = shift;
83 0 my $database = $self->{_db}->{db};
84 0 my $hostname = $self->{_db}->{server};
85
86 0 my $dsn = "DBI:mysql:database=$database;host=$hostname";
87 $self->{dbh} = DBI->connect($dsn, $self->{_db}->{login},
88 0 ▶ $self->{_db}->{password}) or
Branch: 1 branch
BranchTrueFalseTotal
$self->{"dbh"} = "DBI"->connect($dsn, $self->{"_db"}{"login"}, $self->{"_db"}{"password"}) or die 'Unable to connect to qmail database using login ' . qq[information in $self->{"sql_control_file"}] 0 0 0
89 die "Unable to connect to qmail database using login " .
90 "information in $self->{sql_control_file}";
91 }
92  
93 0 ▶sub disconnect {
Subroutine disconnect: not called CC=2 SCAR=17.9
Pod documented
94 0 my $self = shift;
95 0 ▶ $self->{dbh}->disconnect if defined $self->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"dbh"} 0 0 0
96 }
97  
98 0 ▶sub rcpt_add {
Subroutine rcpt_add: not called CC=1 SCAR=6.9
Pod documented
99 0 my $self = shift;
100 0 my $rcpt_host = $self->_q(shift);
101 0 my $sql = qq|insert into rcpthosts (host) VALUES ($rcpt_host)|;
102 0 $self->_do($sql);
103 }
104  
105 0 ▶sub rcpt_exists {
Subroutine rcpt_exists: not called CC=1 SCAR=6.9
Pod documented
106 0 my $self = shift;
107 0 my $rcpt_host = $self->_q(shift);
108 0 my $sql = qq|select count(*) from rcpthosts where host = $rcpt_host|;
109 0 return $self->{dbh}->selectrow_arrayref($sql)->[0];
110 }
111  
112 0 ▶sub rcpt_del {
Subroutine rcpt_del: not called CC=1 SCAR=6.9
Pod documented
113 0 my $self = shift;
114 0 my $rcpt_host = $self->_q(shift);
115 0 my $sql = qq|delete from rcpthosts where host = $rcpt_host|;
116 0 $self->_do($sql);
117 }
118  
119 0 ▶sub mail_add {
Subroutine mail_add: not called CC=7 SCAR=40.3
Pod documented
120 0 my $self = shift;
121 0 my $vuser = shift;
122 0 my $vhost = shift;
123 0 my $pass = shift;
124 0 ▶ my $mbox = shift || $self->_mbox($vuser,$vhost);
Condition0%0/3
shift() || $self->_mbox($vuser, $vhost)
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->_mbox($vuser, $vhost)
shift() $self->_mbox($vuser, $vhost)
Truth table0%0/3
A || B
A= shift()B= $self->_mbox($vuser, $vhost)
ABresult
1X1
011
000
125 0 ▶ my $mbox_path = shift || $self->_mbox_path($vuser,$vhost);
Condition0%0/3
shift() || $self->_mbox_path($vuser, $vhost)
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->_mbox_path($vuser, $vhost)
shift() $self->_mbox_path($vuser, $vhost)
Truth table0%0/3
A || B
A= shift()B= $self->_mbox_path($vuser, $vhost)
ABresult
1X1
011
000
126 0 ▶ my $qmaild_id = shift || (getpwnam('qmaild'))[2];
Condition0%0/3
shift() || (getpwnam 'qmaild')[2]
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || (getpwnam 'qmaild')[2]
shift() (getpwnam 'qmaild')[2]
Truth table0%0/3
A || B
A= shift()B= (getpwnam 'qmaild')[2]
ABresult
1X1
011
000
127 0 ▶ my $qmail_id = shift || (getpwnam('qmailr'))[3];
Condition0%0/3
shift() || (getpwnam 'qmailr')[3]
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || (getpwnam 'qmailr')[3]
shift() (getpwnam 'qmailr')[3]
Truth table0%0/3
A || B
A= shift()B= (getpwnam 'qmailr')[3]
ABresult
1X1
011
000
128  
129 # check existence of mbox_base and virtual base path
130 0 $self->_check_mbox_base($vhost,$qmaild_id,$qmail_id);
131 # create user qmail dirs
132 0 foreach (('','Maildir','Maildir/cur','Maildir/new','Maildir/tmp')) {
133 0 ▶ my $mask = $_ eq '' ? 0700 : 0711;
Branch: 1 branch
BranchTrueFalseTotal
$_ eq '' ? : 0 0 0
134 0 $self->_make_qmail_dir($mbox_path ."/$_",$qmaild_id,$qmail_id,$mask);
135 }
136 # add user to db
137 # add user to virtual table
138 0 $self->_table_virtual_add($mbox,$vuser,$vhost);
139 # add user to mailbox table
140 0 $self->_table_mailbox_add($mbox,$qmaild_id,$qmail_id,$mbox_path,$pass);
141  
142 }
143  
144 0 ▶sub mail_exists {
Subroutine mail_exists: not called CC=2 SCAR=17.9
Pod documented
145 0 my $self = shift;
146 0 my $vuser = shift;
147 0 my $vhost = shift;
148 0 ▶ my $mbox = shift || $self->_mbox($vuser,$vhost);
Condition0%0/3
shift() || $self->_mbox($vuser, $vhost)
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->_mbox($vuser, $vhost)
shift() $self->_mbox($vuser, $vhost)
Truth table0%0/3
A || B
A= shift()B= $self->_mbox($vuser, $vhost)
ABresult
1X1
011
000
149 0 $mbox = $self->_q($mbox);
150 0 my $sql = qq|select count(*) from mailbox where username = $mbox|;
151 0 return $self->{dbh}->selectrow_arrayref($sql)->[0];
152 }
153  
154 0 ▶sub mail_del {
Subroutine mail_del: not called CC=2 SCAR=17.9
Pod documented
155 0 my $self = shift;
156 0 my $vuser = shift;
157 0 my $vhost = shift;
158 0 ▶ my $mbox = shift || $self->_mbox($vuser,$vhost);
Condition0%0/3
shift() || $self->_mbox($vuser, $vhost)
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->_mbox($vuser, $vhost)
shift() $self->_mbox($vuser, $vhost)
Truth table0%0/3
A || B
A= shift()B= $self->_mbox($vuser, $vhost)
ABresult
1X1
011
000
159
160 0 $mbox = $self->_q($mbox);
161 0 my $sql = qq|select home from mailbox where username = $mbox|;
162 0 my $home = $self->{dbh}->selectrow_arrayref($sql)->[0];
163 0 $sql = qq|delete from mailbox where username = $mbox|;
164 0 $self->_do($sql);
165 0 $sql = qq|delete from virtual where username = $mbox|;
166 0 $self->_do($sql);
167 0 rmtree($home);
168  
169
170 }
171  
172 0 ▶sub alias_add {
Subroutine alias_add: not called CC=1 SCAR=6.9
Pod documented
173 0 my $self = shift;
174 0 my $alias = shift;
175 0 my $mail = shift;
176 0 my ($au,$ah) = split(/\@/,$alias);
177 0 my ($mu,$mh) = split(/\@/,$mail);
178 0 $alias = $self->_q($alias);
179 0 $mu = $self->_q($mu);
180 0 $mh = $self->_q($mh);
181 # aggiunta utente alla tabella virtual
182 0 $self->_table_virtual_add($self->_mbox($au,$ah),$au,$ah);
183 0 my $sql = qq|insert into alias (username,alias,alias_username,
184 alias_host) VALUES ('alias',$alias,$mu,$mh)|;
185 0 $self->_do($sql);
186 }
187  
188 0 ▶sub alias_exists {
Subroutine alias_exists: not called CC=1 SCAR=6.9
Pod documented
189 0 my $self = shift;
190 0 my $alias = shift;
191 0 my $mail = shift;
192 0 my ($au,$ah) = split(/\@/,$mail);
193 0 $alias = $self->_q($alias);
194 0 $au = $self->_q($au);
195 0 $ah = $self->_q($ah);
196 0 my $sql = qq|select count(*) from alias where username = 'alias'
197 and alias = $alias and alias_username = $au
198 and alias_host = $ah|;
199 0 return $self->{dbh}->selectrow_arrayref($sql)->[0];
200 }
201  
202 0 ▶sub alias_del {
Subroutine alias_del: not called CC=1 SCAR=6.9
Pod documented
203 0 my $self = shift;
204 0 my $alias = shift;
205 0 my $mail = shift;
206 0 my ($au,$ah) = split(/\@/,$alias);
207 0 my ($mu,$mh) = split(/\@/,$mail);
208 0 $alias = $self->_q($alias);
209 0 $mu = $self->_q($mu);
210 0 $mh = $self->_q($mh);
211 0 my $mbox = $self->_q($self->_mbox($au,$ah));
212 0 my $sql = qq|delete from alias where username = 'alias'
213 and alias = $alias and alias_username = $mu
214 and alias_host = $mh|;
215 0 $self->_do($sql);
216 0 $sql = qq|delete from virtual where username = $mbox|;
217 0 $self->_do($sql);
218 }
219  
220 0 ▶sub _table_virtual_add {
Subroutine _table_virtual_add: not called CC=1 SCAR=6.9
221 0 my $self = shift;
222 0 my $mbox = $self->_q(shift);
223 0 my $vuser = $self->_q(shift);
224 0 my $vhost = $self->_q(shift);
225 0 my $sql = qq|insert into virtual
226 (username,virtual_username,virtual_host) VALUES
227 ($mbox,$vuser,$vhost)|;
228 0 $self->_do($sql);
229 }
230  
231 0 ▶sub _table_mailbox_add {
Subroutine _table_mailbox_add: not called CC=1 SCAR=6.9
232 0 my $self = shift;
233 0 my $mbox = $self->_q(shift);
234 0 my $qusr = $self->_q(shift);
235 0 my $qgrp = $self->_q(shift);
236 0 my $mbox_p = $self->_q(shift);
237 0 my $pass = $self->_q(shift);
238  
239 0 my $pass_t = $self->_q($self->{password_type});
240 0 my $sql = qq|insert into mailbox
241 (username,uid,gid,home,password,password_type) VALUES
242 ($mbox,$qusr,$qgrp,$mbox_p,$pass,$pass_t)|;
243 0 $self->_do($sql);
244 }
245  
246 0 ▶sub _mbox {
Subroutine _mbox: not called CC=2 SCAR=17.9
247 0 my $self = shift;
248 0 my $vuser = shift;
249 0 my $vhost = shift;
250 return $self->{multihosting}
251 0 ▶ ? $vuser . $self->{multihosting_join} . $vhost
Branch: 1 branch
BranchTrueFalseTotal
$self->{'multihosting'} ? : 0 0 0
252 : $vuser;
253 }
254  
255 0 ▶sub _mbox_path {
Subroutine _mbox_path: not called CC=2 SCAR=17.9
256 0 my $self = shift;
257 0 my $vuser = shift;
258 0 my $vhost = shift;
259 return $self->{multihosting}
260 ? $self->{mailbox_base} . "/$vhost/$vuser"
261 0 ▶ : $self->{mailbox_base} . "/$vuser";
Branch: 1 branch
BranchTrueFalseTotal
$self->{'multihosting'} ? : 0 0 0
262 }
263  
264 0 ▶sub _check_mbox_base {
Subroutine _check_mbox_base: not called CC=4 SCAR=30.0
265 0 my $self = shift;
266 0 my $vhost = shift;
267 0 my $quser = shift;
268 0 my $qgrp = shift;
269  
270 0 my $dir = $self->{mailbox_base};
271 0 ▶ $self->_make_qmail_dir( $dir,$quser,$qgrp,0700) if (!-e $dir);
Branch: 1 branch
BranchTrueFalseTotal
unless -e $dir 0 0 0
272 0 $dir .= "/$vhost";
273 $self->_make_qmail_dir( $dir,$quser,$qgrp,0700)
274 0 ▶ if ($self->{multihosting} && !-e $dir);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"multihosting"} and not -e $dir 0 0 0
Condition0%0/3
$self->{"multihosting"} and not -e $dir
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"multihosting"} and not -e $dir
$self->{"multihosting"} not -e $dir
Truth table0%0/3
A and not(B)
A= $self->{"multihosting"}B= not -e $dir
ABresult
0X0
100
111
275 }
276  
277 0 ▶sub _make_qmail_dir {
Subroutine _make_qmail_dir: not called CC=2 SCAR=17.9
278 0 my $self = shift;
279 0 my $dir = shift;
280 0 my $quser = shift;
281 0 my $qgrp = shift;
282 0 my $mask = shift;
283  
284 0 eval { mkpath($dir,0,$mask) };
285 0 ▶ if ($@) { die "Couldn't create $dir: $@"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
286 0 chown $quser,$qgrp,$dir;
287  
288 }
289  
290 0 ▶sub _q {
Subroutine _q: not called CC=1 SCAR=6.9
291 0 my $self = shift;
292 0 return $self->{dbh}->quote(shift);
293 }
294  
295  
296 0 ▶sub _do {
Subroutine _do: not called CC=2 SCAR=17.9
297 0 my $self = shift;
298 0 my $sql = shift;
299 0 ▶ $self->{dbh}->do($sql) or die "Unable to execute $sql";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"dbh"}->do($sql) or die "Unable to execute $sql" 0 0 0
300 }
301  
302 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
303 0 my $self = shift;
304 0 $self->disconnect;
305 # Enter here your code
306 }
↑ Summary