-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions-lib.pl
215 lines (196 loc) · 5.08 KB
/
functions-lib.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/perl
do 'cyrus-lib.pl';
use Encode qw/encode decode/;
use HTML::Entities;
@ACLS=('l','r','s','w','i','p','c','d','a');
@aHost=split /\./,$HOSTNAME; $ext=pop @aHost; $domain=pop @aHost;
$DEFDOMAIN="$domain.$ext";
sub check_mailbox{
# checks if a mailbox exists
my $path=shift;
my @list = $cyrus->list($path);
&cyrus_error();
if ($#list!=0) {
print '<SCRIPT> alert("', $text{unknown_mbox}, ': ', name_decode($path), '"); history.back(); </SCRIPT>';
}
}
sub mbox_info
{
# get mailbox information
# arguments: mailbox name, return mode
# mode=0 or null: returns $name, $supmbox, $domain;
# mode=1: returns $name, $supmbox, $partition;
# mode=2: returns $name, $supmbox, $messages, $size, $partition
my $mbox=shift; my $mode=shift; my @paths=(), $n=0, $i=0;
while ($i>-1)
{
$i=index($mbox,$SEP,$n);
if ($i>-1)
{
push @paths, substr($mbox,$n,$i-$n);
$n=$i+1;
}
}
my ($name, $domain)=split /\@/,$mbox;
$name = name_decode(substr($mbox,$n));
my $supmbox = join($SEP,@paths);
return ($name, $supmbox, $domain) if $mode==0;
get_partitions() unless %imapPartitions;
my $partition="";
my $infocmd=$config{lib_path}."/mbpath";
if (-e $infocmd)
{
my $res=`su -c '$infocmd \"$mbox\"' cyrus`;
while (($k, $v)=each %imapPartitions)
{
if (index($res,$v)>=0) { $partition=$v; last; }
}
return ($name, $supmbox, $partition) if $mode==1;
}
else
{
print "<BR><BR>$infocmd ", $text{not_found}, "<BR>", $text{go_to_config};
exit;
}
my $messages=0, $size=0;
$infocmd=$config{lib_path}."/mbexamine";
if (-e $infocmd)
{
@res=split /\n/, `su -c '$infocmd \"$mbox\"' cyrus`;
foreach (@res)
{
if (/Number of Messages:\s+(\d+)\s+Mailbox Size:\s+(\d+)\s+bytes/)
{
$messages+=$1;
$size+=$2;
}
}
}
else
{
print "<BR><BR>$infocmd ", $text{not_found}, "<BR>", $text{go_to_config};
exit;
}
return ($name, $supmbox, $messages, $size, $partition);
}
sub get_services
{
# get the list of services managed by Cyrus
@cyrusServs=();
open(MYFILE,$config{cyrus_conf_path})
or die "<BR><BR>".$config{cyrus_conf_path}." ".$text{not_found}."<BR>".$text{go_to_config};
while(<MYFILE> !~ /^SERVICES \{/) {}
while(<MYFILE>)
{
last if /^\}/;
if (/^(#?)\s*(\w+)\s+cmd=/)
{ push @cyrusServs, "$1:$2"; }
}
close MYFILE;
}
sub get_partitions
{
# get the imap partitions
get_config() unless %imapParams;
%imapPartitions=(); $defaultPartition="default";
while (($k,$v)=each %imapParams)
{
if ($k =~ /^partition-(.+)/) { $imapPartitions{$1}=$v; }
$defaultPartition=$v if $k eq "defaultpartition";
}
}
# function to select the default partition
sub defaultpartition
{
my $value=shift;
get_partitions();
$value=$defaultPartition unless $value;
$value="default" unless $value;
print "<SELECT NAME=defaultpartition>\n";
while (($k,$v)=each %imapPartitions)
{
print "<OPTION VALUE=$k";
print " SELECTED" if $value eq $k || $value eq $v;
print ">$k: $v</OPTION>\n";
}
print "</SELECT>\n";
}
sub getquota
{
# get used and quota amount, and perc.used, if it's a root folder
my $mbox=shift;
my %quotas, $elems;
($mbox, %quotas)=$cyrus->quotaroot($mbox);
$elems=$quotas{'STORAGE'};
return ($elems->[1]*1024, trim(sprintf("%9.2f %", $elems->[0]/$elems->[1]*100))) if $elems->[1];
return (0, 0) unless $elems->[1];
}
# functions to encode-decode international
# characters in mailbox names
sub name_encode
{
my $name=shift;
$name=encode("UTF-7", $name);
$name =~ s/\+/&/g;
return $name;
}
sub name_decode
{
my $name=shift;
$name =~ s/&/\+/g;
$name=decode("UTF-7", $name);
return encode_entities($name);
}
sub byteFormat
{
my $size=shift;
Case: {
if ($size < 1024) { $size.=" byte"; last Case; }
if ($size < 1024*1024) { $size=sprintf("%9.2f Kb",$size/1024); last Case; }
if ($size < 1024*1024*1024) { $size=sprintf("%9.2f Mb",$size/1024/1024); last Case; }
$size=sprintf("%9.2f Gb", $size/1024/1024/1024);
}
my ($val,$unit)=split / /,trim($size); $val =~ s/\.*0+$// unless $unit eq 'byte';
return "$val $unit";
}
sub msglog
{
my $msg=shift;
&webmin_log($msg);
print "$msg: OK<BR>\n";
}
sub iframe_header
{
print "Content-type: text/html
<HTML><BODY>
<SCRIPT>
if (top.document.styleSheets.length>0)
{
document.write('<LINK REL=stylesheet HREF='+top.document.styleSheets[0].href+'>');\n";
if ($_[0])
{
print "\t}\ndocument.write(\"<body $_[0]>\");\n";
}
else
{
print "\tdocument.write('<body>');
}
else if (top.document.body.bgColor)
document.write('<body bgcolor=' + top.document.body.bgColor +
' topmargin=1 leftmargin=1 marginwidth=1 marginheight=1>');\n";
}
print "</SCRIPT>\n<P>\n";
}
sub iframe_footer
{
my ($name, $err, $reload)=@_;
print "<BR><FONT COLOR=red><B>$err</B></FONT><BR>\n" if $err;
print "<SCRIPT> parent.frames[0].location.reload(); </SCRIPT>" if $reload;
print "<FORM><INPUT TYPE=BUTTON VALUE='";
if (!$name || $name eq 'user')
{ print &text('main_return', '/'), "' onClick=\"location.href='mbox_info.cgi'\">"; }
else
{ print &text('main_return', name_decode($name)), "' onClick=\"location.href='mbox_info.cgi?mbox='+escape('$name')\">"; }
print "\n </FORM>\n</BODY></HTML>";
}
1;