File Coverage

blib/lib/Mail/Vmailmgr.pm
Criterion Covered Total %
statement 20 175 11.4
branch 0 90 0.0
condition n/a
subroutine 6 23 26.0
pod 0 17 0.0
total 26 305 8.5


line stmt bran cond sub pod time code
1             package Mail::Vmailmgr;
2              
3             ##Copyright (C) 2000 Martin Langhoff
4             ##
5             ##Most of this code is based on a PHP version written by
6             ##Mike Bell . This Perl Module is mostly
7             ##a quick translation of Mikes PHP code, so it doesn't look
8             ##nice, but we certainly hope it works.
9             ##
10             ##This program is free software; you can redistribute it and/or modify
11             ##it under the terms of the GNU General Public License as published by
12             ##the Free Software Foundation; either version 2 of the License, or
13             ##(at your option) any later version.
14             ##
15             ##This program is distributed in the hope that it will be useful,
16             ##but WITHOUT ANY WARRANTY; without even the implied warranty of
17             ##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18             ##GNU General Public License for more details.
19             ##
20             ##You should have received a copy of the GNU General Public License
21             ##along with this program; if not, write to the Free Software
22             ##Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23              
24             # define globals
25             my ($debug);
26              
27             BEGIN {
28 1     1   743 use strict;
  1         2  
  1         38  
29 1     1   4 use Exporter ();
  1         2  
  1         21  
30 1     1   5 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  1         5  
  1         143  
31              
32 1     1   3 $debug = 0;
33            
34 1         2 $VERSION = 0.4;
35            
36 1         16 @ISA = qw(Exporter);
37            
38             # symbols to export by default
39 1         4 @EXPORT = qw(
40             &vlistdomain
41             &vlookup
42             &vadduser
43             &vaddalias
44             &vdeluser
45             &vchpass
46             &vchforward
47             &vchattr
48             &vwriteautoresponse
49             &vreadautoresponse
50             &vdisableautoresponse
51             &venableautoresponse
52             &vautoresponsestatus
53             );
54            
55             # symbols to export on request
56             #@EXPORT_OK = qw($Var1 %Hashit &func3);
57              
58             # define names for sets of symbols
59             #%EXPORT_TAGS = ;# not used here, yet...
60              
61              
62 1         17 return 1;
63             };
64              
65 1     1   4 use strict;
  1         2  
  1         33  
66 1     1   1190 use IO::Socket;
  1         29922  
  1         5  
67             #use Data::Dumper;
68              
69             sub vm_daemon_raw {
70            
71 0 0   0 0   $debug && warn "vm_daemon_raw called with params: \n " . Dumper(\@_);
72            
73 0           my @arg = @_;
74 0           my $vmailfile = "/tmp/.vmailmgrd";
75 0           my $socketfile = "/etc/vmailmgr/socket-file";
76            
77             # override $vmailfile witth the contents of $socketfile, if it's there
78 0 0         if (stat $socketfile){
79 0 0         open SOCKETFILE, $socketfile or die "can't open $socketfile: $!";
80 0           my $socket = ;
81 0           chomp $socket;
82 0 0         if (stat $socket){ # the daemon seems to be running ok!
83 0           $vmailfile = $socket;
84             }
85 0           close SOCKETFILE;
86             }
87            
88 0 0         $debug && warn "vm_daemon_raw ->about to connect to $vmailfile";
89            
90 0           socket (DAEMON, PF_UNIX, SOCK_STREAM, 0);
91 0 0         connect(DAEMON, sockaddr_un($vmailfile))
92             or die "Can't connect to $vmailfile : $! ... is the daemon running?";
93              
94 0 0         $debug && warn "vm_daemon_raw ->connected!";
95              
96             # parse @arg into a glob... I don't seem to understand Mike's code well
97             # hope bit-operators work similarly between perl and php...
98 0           my $command;
99 0           for (my $n=0; $n<@arg; $n++){
100 0           my $commandlength = length $arg[$n];
101 0           my $high = (($commandlength & (0xFF << 8)) >> 8);
102 0           my $low = ($commandlength & 0xFF);
103 0           $command .= sprintf('%c%c%s', $high, $low, $arg[$n]);
104             }
105            
106             # Create the header, which consists of another two byte length
107             # representation, the number of arguments being passed, and the
108             # command string created above.
109 0           my $commandlength = length $command;
110 0           my $high = (($commandlength & (255 << 8)) >> 8);
111 0           my $low = ($commandlength & 255);
112 0           my $commandstr = sprintf("\002%c%c%c", $high, $low+1, scalar @arg -1) . $command;
113            
114             # pass it to the daemon
115 0 0         $debug && warn "vm_daemon_raw ->sending command with length ". length $commandstr;
116              
117 0 0         send (DAEMON, $commandstr,0) == length($commandstr) or die "cant send!";
118            
119             #
120             # now catch the answer
121             #
122 0 0         $debug && warn "vm_daemon_raw ->reading answer";
123            
124             # catch the 1 char $value
125 0           my $value;
126 0           read(DAEMON, $value, 1);
127 0           $value = ord $value;
128            
129             #catch the 2 char length ...
130 0           my $length;
131 0           read(DAEMON, $length, 2);
132 0           $length = "$length";
133 0           $length = ( ord(substr($length,0,1) ) << 8 + ord(substr($length,1,1)));
134            
135             ;
136            
137             # now read the damned message!
138 0           my $message;
139 0 0         if ($value == 0){
140 0           my $buffer;
141 0           $message .= $buffer while read(DAEMON, $buffer, 65535);
142             # don't really know why ....
143 0           close DAEMON;
144 0           return $message;
145             }
146            
147 0           read(DAEMON, $message, $length);
148            
149             # and close the socket
150 0           close DAEMON;
151            
152            
153 0           return [$value, $message];
154            
155             }
156              
157             ##=for martin listdomain_parse_userdata($line, $username)
158             ##
159             ##Parses the lines from listdomain into fields. All fields after aliases are ignored, but this is easy to fix if anybody cared about them.
160             ##
161             ##=cut
162              
163             sub listdomain_parse_userdata {
164              
165 0 0   0 0   $debug && warn "listdomain_parse_userdata called with params: \n " . Dumper(\@_);
166              
167 0           my $line = shift;
168 0           my $username = shift;
169            
170             # grab the protocol version
171 0           my $ver = ord(substr($line,0,1));
172 0 0         if ($ver ne "2") { die "Protocol version is $ver. This module expects protocol version 2."};
  0            
173            
174             # chop off the version
175 0           $line = substr($line,1);
176            
177            
178             # process flags (???) according to Mike's code, they seem to be pairs,
179             # but I don't quite get it
180 0           my @flags;
181             {
182             # need to scope $n a bit farther
183 0           my $n;
  0            
184 0           for ($n=0; $n
185             # flags come in pairs. and null is a valid value.
186 0           my $flagname = substr($line, $n,1);
187 0           my $flagvalue = substr($line, $n+1,1);
188             # according to mike,
189             # if the las flag name/identifier is a null (\0)
190             # then that means flags are over...
191 0 0         last if $flagname =~ /\0/;
192 0           $flags[ord($flagname)] = ord($flagvalue);
193             }
194             #remove the already processed flags + the trailing \0...
195 0           $line = substr($line, $n+1);
196             }
197            
198             # split the fields on NULLS
199 0           my @fields = split(/\0/, $line);
200            
201 0           (my $password, my $mailbox, @fields) = @fields;
202            
203             #$password = 'Set' if $password ne '*' ;
204            
205 0           my @aliases;
206 0           while($fields[0]){
207 0           push (@aliases, shift @fields);
208             }
209 0           shift @fields;
210            
211 0           my ( $PersonalInfo,
212             $HardQuota,
213             $SoftQuota,
214             $SizeLimit,
215             $CountLimit,
216             $CreationTime,
217             $ExpiryTime ) = @fields;
218            
219             return [
220 0           $username, $password,
221             $mailbox, \@aliases,
222             $PersonalInfo, $HardQuota,
223             $SoftQuota, $SizeLimit,
224             $CountLimit, $CreationTime,
225             $ExpiryTime, \@flags];
226             }
227              
228             ##=for martin list_domain_parse_line($line)
229             ##
230             ##Parses the lines from listdomain into fields.
231             ##
232             ##=cut
233             sub listdomain_parse_line {
234              
235 0 0   0 0   $debug && warn "listdomain_parse_line called with param of " . length $_[0] . 'length';
236            
237 0           my $line = shift;
238            
239             # find the first null.
240 0 0         $line =~ m/\0/ or warn "no nulls in string??";
241            
242             # grab the user data
243 0           my $username= $`;
244 0 0         $debug && warn "user found->$username";
245            
246             # Send that user's data to be parsed.
247 0           return &listdomain_parse_userdata( $', $username);
248             }
249              
250             ##=for martin listdomain_parse($output)
251             ##
252             ##Does the ugly stuff for listdomain, and calls listdomain_parse_line once
253             ##for each user
254             ##
255             ##=cut
256              
257             sub listdomain_parse {
258              
259 0 0   0 0   $debug && warn "listdomain_parse called with param of " . length $_[0]. " chars";
260              
261 0           my $output = shift;
262 0           my @array;
263 0           my $cur=1;
264 0           while (1){
265 0           my $linelength=(ord(substr($output, $cur++, 1)) << 8 ) + ord(substr($output, $cur++, 1));
266 0 0         last unless $linelength;
267 0           push @array, listdomain_parse_line(substr($output, $cur, $linelength));
268 0           $cur += $linelength + 1;
269             } ;
270            
271 0           return \@array;
272             }
273              
274             sub vlistdomain{
275              
276 0 0   0 0   $debug && warn "listdomain called with params: \n " . Dumper(\@_);
277              
278 0           my ($domain, $password) = @_;
279            
280 0 0         return [1, "Empty domain"] unless $domain;
281 0 0         return [1, "Empty domain password"] unless $password;
282            
283            
284 0           my $temp = vm_daemon_raw("listdomain", $domain, $password);
285              
286 0 0         if (ref($temp) eq 'ARRAY') {return $temp};
  0            
287 0           return listdomain_parse($temp);
288             }
289              
290             sub vlookup {
291 0     0 0   my ($domain, $user, $password) = @_;
292 0           my $tmp = vm_daemon_raw("lookup", $domain, $user, $password);
293            
294 0 0         if (ref $tmp eq 'ARRAY'){
295              
296 0           return $tmp;
297             } else {
298 0           return listdomain_parse_userdata($tmp, $user);
299             }
300             }
301              
302             sub vadduser {
303 0     0 0   my ($domain, $password, $username, $userpass, @forwards) = @_;
304            
305 0 0         return [1, "Empty domain"] unless $domain;
306 0 0         return [1, "Empty domain password"] unless $password;
307 0 0         return [1, "Empty username"] unless $username;
308 0 0         return [1, "No user password supplied"] unless $userpass;
309            
310 0           my @command = ("adduser2", $domain, $username, $password,
311             $userpass, $username);
312 0           foreach my $fw (@forwards){
313 0 0         push (@command, $fw) if $fw;
314             }
315 0           return vm_daemon_raw(@command);
316             }
317              
318             sub vaddalias {
319 0     0 0   my ($domain, $password, $username, $userpass, @forwards) = @_;
320            
321 0 0         return [1, "Empty domain"] unless $domain;
322 0 0         return [1, "Empty domain password"] unless $password;
323 0 0         return [1, "Empty username"] unless $username;
324            
325 0           my @command = ("adduser2", $domain, $username, $password,
326             $userpass, "");
327              
328 0           foreach my $fw (@forwards){
329 0 0         push (@command, $fw) if $fw;
330             }
331            
332 0           return vm_daemon_raw(@command);
333             }
334              
335             sub vdeluser {
336 0     0 0   my ($domain, $password, $username) = @_;
337            
338 0 0         return [1, "Empty domain"] unless $domain;
339 0 0         return [1, "Empty domain password"] unless $password;
340 0 0         return [1, "Empty username"] unless $username;
341            
342 0           my @command=("deluser", $domain, $username,$password, );
343 0           return vm_daemon_raw(@command);
344             }
345              
346             sub vchpass {
347 0     0 0   my ($domain, $password, $username, $newpass) = @_;
348              
349 0 0         return [1, "Empty domain"] unless $domain;
350 0 0         return [1, "Empty domain password"] unless $password;
351 0 0         return [1, "Empty username"] unless $username;
352 0 0         return [1, "No new password supplied"] unless $newpass;
353              
354 0           my @command=("chattr", $domain, $username, $password, "1", $newpass);
355 0           return vm_daemon_raw(@command);
356            
357             }
358              
359             sub vchforward {
360 0     0 0   my ($domain, $password, $username, @forwards) = @_;
361            
362 0 0         return [1, "Empty domain"] unless $domain;
363 0 0         return [1, "Empty domain password"] unless $password;
364 0 0         return [1, "Empty username"] unless $username;
365            
366 0           my @command=("chattr", $domain, $username, $password, "2");
367 0           foreach my $fw (@forwards){
368 0 0         push (@command, $fw) if $fw;
369             }
370 0 0         push (@command, "") unless @forwards;
371            
372 0           return vm_daemon_raw(@command);
373             }
374              
375             sub vchattr {
376 0     0 0   my ($domain, $password, $username, $attr, $value) = @_;
377 0           my %ATTR = (
378             "PASS" => "1",
379             "DEST" => "2",
380             "HARDQUOTA" => "3",
381             "SOFTQUOTA" => "4",
382             "MSGSIZE" => "5",
383             "MSGCOUNT" => "6",
384             "EXPIRY" => "7",
385             "MAILBOX_ENABLED" => "8",
386             "PERSONAL" => "9",
387             );
388 0           my @command = ("chattr", $domain, $username, $password, $ATTR{$attr}, $value);
389            
390 0           return vm_daemon_raw(@command);
391             }
392              
393             sub vwriteautoresponse {
394 0     0 0   my ($domain, $password, $username, $message) = @_;
395 0           my @command = ("autoresponse", $domain, $username, $password, "write", $message);
396 0           return vm_daemon_raw(@command);
397             }
398              
399             sub vreadautoresponse{
400 0     0 0   my ($domain, $password, $username) = @_;
401 0           my @command = ("autoresponse", $domain, $username, $password, "read");
402 0           return vm_daemon_raw(@command);
403             }
404              
405             sub vdisableautoresponse{
406 0     0 0   my ($domain, $password, $username) = @_;
407 0           my @command = ("autoresponse", $domain, $username, $password, "disable");
408 0           return vm_daemon_raw(@command);
409             }
410              
411             sub venableautoresponse{
412 0     0 0   my ($domain, $password, $username) = @_;
413 0           my @command = ("autoresponse", $domain, $username, $password, "enable");
414 0           return vm_daemon_raw(@command);
415             }
416              
417             sub vautoresponsestatus{
418 0     0 0   my ($domain, $password, $username) = @_;
419 0           my @command = ("autoresponse", $domain, $username, $password, "status");
420 0           return vm_daemon_raw(@command);
421             }
422              
423             1; # yes, we compiled gracefully.
424              
425             __END__