forked from webmin/usermin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mailbox-lib.pl
executable file
·2302 lines (2166 loc) · 63.9 KB
/
mailbox-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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# mailbox-lib.pl
use strict;
use warnings;
our (%text, %config, %gconfig, %userconfig);
our $remote_user;
our @remote_user_info;
our $user_module_config_directory;
our $module_root_directory;
our $module_name;
BEGIN { push(@INC, ".."); };
use WebminCore;
use Socket;
&init_config();
&switch_to_remote_user();
&create_user_config_dirs();
do "$module_root_directory/boxes-lib.pl";
do "$module_root_directory/folders-lib.pl";
#open(DEBUG, ">>/tmp/mailbox.debug");
our $qmail_maildir;
if ($config{'mail_qmail'}) {
$qmail_maildir = &mail_file_style($remote_user, $config{'mail_qmail'},
$config{'mail_style'});
}
else {
$qmail_maildir = "$remote_user_info[7]/$config{'mail_dir_qmail'}";
}
our $address_book = "$user_module_config_directory/address_book";
our $address_group_book = "$user_module_config_directory/address_group_book";
our $folders_dir = "$remote_user_info[7]/$userconfig{'mailbox_dir'}";
our %folder_types = map { $_, 1 } (split(/,/, $config{'folder_types'}),
split(/,/, $config{'folder_virts'}));
our $search_folder_id = 1;
our $special_folder_id = 2;
our $auto_cmd = "$user_module_config_directory/auto.pl";
our $last_folder_file = "$user_module_config_directory/lastfolder";
# mailbox_file()
sub mailbox_file
{
if ($config{'mail_system'} == 0) {
return &user_mail_file(@remote_user_info);
}
else {
return "$qmail_maildir/";
}
}
# supports_gpg()
# Returns 1 if GPG is installed and the module is available
my $supports_gpg_cache;
sub supports_gpg
{
if (!defined($supports_gpg_cache)) {
$supports_gpg_cache = &has_command("gpg") &&
&foreign_check("gnupg") &&
&foreign_available("gnupg") ? 1 : 0;
}
return $supports_gpg_cache;
}
# decrypt_attachments(&mail)
# If the attachments on a mail are encrypted, converts them into unencrypted
# form. Returns a code and message, valid codes being: 0 = not encrypted,
# 1 = encrypted but cannot decrypt, 2 = failed to decrypt, 3 = decrypted OK
sub decrypt_attachments
{
# Check requirements for decryption
my $first = $_[0]->{'attach'}->[0];
my ($body) = grep { $_->{'type'} eq 'text/plain' || $_->{'type'} eq 'text' }
@{$_[0]->{'attach'}};
my $hasgpg = &has_command("gpg") && &foreign_check("gnupg") &&
&foreign_available("gnupg");
if ($_[0]->{'header'}->{'content-type'} =~ /^multipart\/encrypted/ &&
$first->{'type'} =~ /^application\/pgp-encrypted/ &&
$first->{'data'} =~ /Version:\s+1/i) {
# RFC 2015 PGP encryption of entire message
return (1) if (!&supports_gpg());
&foreign_require("gnupg", "gnupg-lib.pl");
my $plain;
my $enc = $_[0]->{'attach'}->[1];
my $rv = &foreign_call("gnupg", "decrypt_data", $enc->{'data'}, \$plain);
return (2, $rv) if ($rv);
$plain =~ s/\r//g;
my $amail = &extract_mail($plain);
&parse_mail($amail);
$_[0]->{'attach'} = $amail->{'attach'};
return (3);
}
# Check individual attachments for text-only encryption
my $a;
my $cc = 0;
my $ok = 3;
foreach my $a (@{$_[0]->{'attach'}}) {
if ($a->{'type'} =~ /^(text|application\/pgp-encrypted)/i &&
$a->{'data'} =~ /BEGIN PGP MESSAGE/ &&
$a->{'data'} =~ /([\000-\377]*)(-----BEGIN PGP MESSAGE-+\r?\n([\000-\377]+)-+END PGP MESSAGE-+\r?\n)([\000-\377]*)/i) {
my ($before, $enc, $after) = ($1, $2, $4);
return (1) if (!&supports_gpg());
&foreign_require("gnupg", "gnupg-lib.pl");
$cc++;
my $pass = &gnupg::get_passphrase();
my $plain;
my $rv = &gnupg::decrypt_data($enc, \$plain, $pass);
return (2, $rv) if ($rv);
$ok = 4 if ($before =~ /\S/ || $after =~ /\S/);
if ($a->{'type'} !~ /^text/) {
$a->{'type'} = "text/plain";
}
$a->{'data'} = $before.$plain.$after;
}
}
return $cc ? ( $ok ) : ( 0 );
}
# check_signature_attachments(&attach, &textbody-attach)
# Checks for a signature attachment, and verifies it. Returns the signature
# status code and message.
sub check_signature_attachments
{
my ($attach, $textbody) = @_;
my ($sigcode, $sigmessage, $sindex);
if (&has_command("gpg") && &foreign_check("gnupg") && &foreign_available("gnupg")) {
# Check for GnuPG signatures
my $sig;
my $sindex;
foreach my $a (@$attach) {
$sig = $a if ($a->{'type'} =~ /^application\/pgp-signature/);
}
if ($sig) {
# Verify the signature against the rest of the attachment
&foreign_require("gnupg", "gnupg-lib.pl");
my $rest = $sig->{'parent'}->{'attach'}->[0];
$rest->{'raw'} =~ s/\r//g;
$rest->{'raw'} =~ s/\n/\r\n/g;
($sigcode, $sigmessage) =
&gnupg::verify_data($rest->{'raw'}, $sig->{'data'});
@$attach = grep { $_ ne $sig } @$attach;
$sindex = $sig->{'idx'};
}
elsif ($textbody && $textbody->{'data'} =~ /(-+BEGIN PGP SIGNED MESSAGE-+\r?\n(Hash:\s+(\S+)\r?\n\r?\n)?([\000-\377]+\r?\n)-+BEGIN PGP SIGNATURE-+\r?\n([\000-\377]+)-+END PGP SIGNATURE-+\r?\n)/i) {
# Signature is in body text!
my $sig = $1;
my $text = $4;
&foreign_require("gnupg", "gnupg-lib.pl");
($sigcode, $sigmessage) = &gnupg::verify_data($sig);
if ($sigcode == 0 || $sigcode == 1) {
$textbody->{'data'} = $text;
}
}
}
return ($sigcode, $sigmessage, $sindex);
}
# list_addresses()
# Returns a list of address book entries, each an array reference containing
# the email address, real name, index (if editable) and From: flag
sub list_addresses
{
my @rv;
my $i = 0;
open(my $ADDRESS, "<", $address_book);
while(<$ADDRESS>) {
s/\r|\n//g;
my @sp = split(/\t/, $_);
if (@sp >= 1) {
push(@rv, [ $sp[0], $sp[1], $i, $sp[2] ]);
}
$i++;
}
close($ADDRESS);
if ($config{'global_address'}) {
my $gab = &group_subs($config{'global_address'});
open(my $ADDRESS, "<", $gab);
while(<$ADDRESS>) {
s/\r|\n//g;
my @sp = split(/\t+/, $_);
if (@sp >= 2) {
push(@rv, [ $sp[0], $sp[1] ]);
}
}
close($ADDRESS);
}
if ($userconfig{'sort_addrs'} == 2) {
return sort { lc($a->[0]) cmp lc($b->[0]) } @rv;
}
elsif ($userconfig{'sort_addrs'} == 1) {
return sort { lc($a->[1]) cmp lc($b->[1]) } @rv;
}
else {
return @rv;
}
}
# create_address(email, real name, forfrom)
# Adds an entry to the address book
sub create_address
{
no strict "subs";
&open_tempfile(ADDRESS, ">>$address_book");
&print_tempfile(ADDRESS, "$_[0]\t$_[1]\t$_[2]\n");
&close_tempfile(ADDRESS);
use strict "subs";
}
# modify_address(index, email, real name, forfrom)
# Updates some entry in the address book
sub modify_address
{
&replace_file_line($address_book, $_[0], "$_[1]\t$_[2]\t$_[3]\n");
}
# delete_address(index)
# Deletes some entry from the address book
sub delete_address
{
&replace_file_line($address_book, $_[0]);
}
# address_button(field, [form], [frommode], [realfield], [nogroups])
# Returns HTML for an address-book popup button
sub address_button
{
if (defined(&theme_address_button)) {
return &theme_address_button(@_);
}
my $form = @_ > 1 ? $_[1] : 0;
my $mode = @_ > 2 ? $_[2] : 0;
my $nogroups = @_ > 4 ? $_[4] : 0;
my ($rfield1, $rfield2);
if ($_[3]) {
return "<input type=button onClick='ifield = document.forms[$form].$_[0]; rfield = document.forms[$form].$_[3]; chooser = window.open(\"../$module_name/address_chooser.cgi?addr=\"+escape(ifield.value)+\"&mode=$mode&nogroups=$nogroups\", \"chooser\", \"toolbar=no,menubar=no,scrollbars=yes,width=500,height=500\"); chooser.ifield = ifield; window.ifield = ifield; chooser.rfield = rfield; window.rfield = rfield' value=\"...\">\n";
}
else {
return "<input type=button onClick='ifield = document.forms[$form].$_[0]; chooser = window.open(\"../$module_name/address_chooser.cgi?addr=\"+escape(ifield.value)+\"&mode=$mode\", \"chooser\", \"toolbar=no,menubar=no,scrollbars=yes,width=500,height=500\"); chooser.ifield = ifield; window.ifield = ifield' value=\"...\">\n";
}
}
# list_folders()
# Returns a list of all folders for this user
# folder types: 0 = mbox, 1 = maildir, 2 = pop3, 3 = mh, 4 = imap, 5 = combined,
# 6 = virtual
# folder modes: 0 = ~/mail, 1 = external folder, 2 = sent mail,
# 3 = inbox/drafts/trash
my @list_folders_cache;
sub list_folders
{
if (@list_folders_cache) {
return @list_folders_cache;
}
my (@rv, $f, $o, %done);
# Read the module's config directory, to find folders files
opendir(DIR, $user_module_config_directory);
my @folderfiles = readdir(DIR);
closedir(DIR);
if ($config{'mail_system'} == 2) {
# POP3 inbox
push(@rv, { 'name' => $text{'folder_inbox'},
'type' => 2,
'server' => $config{'pop3_server'} || "localhost",
'mode' => 3,
'remote' => 1,
'nowrite' => 1,
'inbox' => 1,
'index' => 0 });
&read_file("$user_module_config_directory/inbox.pop3", $rv[$#rv]);
}
elsif ($config{'mail_system'} == 4) {
# IMAP inbox
my $imapserver = $config{'pop3_server'} || "localhost";
push(@rv, { 'name' => $text{'folder_inbox'},
'id' => 'INBOX',
'type' => 4,
'server' => $imapserver,
'mode' => 3,
'remote' => 1,
'flags' => 1,
'inbox' => 1,
'index' => 0 });
&read_file("$user_module_config_directory/inbox.imap", $rv[$#rv]);
# Use HTTP username and password, if available and if logging in to
# a local IMAP server.
if ($remote_user && $main::remote_pass &&
(&to_ipaddress($rv[0]->{'server'}) eq '127.0.0.1' ||
&to_ipaddress($rv[0]->{'server'}) eq
&to_ipaddress(&get_system_hostname()))) {
$rv[0]->{'user'} = $remote_user;
$rv[0]->{'pass'} = $main::remote_pass;
$rv[0]->{'autouser'} = 1;
}
# Get other IMAP folders (if we can)
my ($ok, $ih) = &imap_login($rv[0]);
if ($ok == 1) {
my @irv = &imap_command($ih, "list \"\" \"*\"");
if ($irv[0]) {
foreach my $l (@{$irv[1]}) {
if ($l =~ /LIST\s+\(.*\)\s+("(.*)"|\S+)\s+("(.*)"|\S+)/) {
# Found a folder line
my $fn = $4 || $3;
next if ($fn eq "INBOX");
push(@rv,
{ 'name' => $fn,
'id' => $fn,
'type' => 4,
'server' => $imapserver,
'user' => $rv[0]->{'user'},
'pass' => $rv[0]->{'pass'},
'mode' => 0,
'remote' => 1,
'flags' => 1,
'imapauto' => 1,
'mailbox' => $fn,
'nologout' => $config{'nologout'},
'index' => scalar(@rv) });
&read_file("$user_module_config_directory/$fn.imap", $rv[$#rv]);
}
}
$rv[0]->{'nologout'} = $config{'nologout'};
}
}
# Find or create the IMAP sent mail folder
my $sf;
my $sent;
if ($userconfig{'sent_name'}) {
($sent) = grep { lc($_->{'name'}) eq lc($sf) } @rv;
}
else {
($sent) = grep { lc($_->{'name'}) eq 'sent' } @rv;
if (!$sent) {
($sent) = grep { $_->{'name'} =~ /sent/i } @rv;
}
}
if (!$sent && $ok == 1) {
my @irv = &imap_command($ih, "create \"$sf\"");
if ($irv[0]) {
$sent = { 'id' => $sf,
'type' => 4,
'server' => $imapserver,
'user' => $rv[0]->{'user'},
'pass' => $rv[0]->{'pass'},
'mode' => 2,
'remote' => 1,
'flags' => 1,
'imapauto' => 1,
'mailbox' => $sf,
'index' => scalar(@rv) };
push(@rv, $sent);
&read_file("$user_module_config_directory/$sf.imap",
$sent);
}
}
if ($sent) {
$sent->{'name'} = $text{'folder_sent'};
$sent->{'perpage'} = $userconfig{'perpage_sent_mail'};
$sent->{'fromaddr'} = $userconfig{'fromaddr_sent_mail'};
$sent->{'sent'} = 1;
$sent->{'mode'} = 2;
}
# Find or create the IMAP drafts folder
my $df = $userconfig{'drafts_name'} || 'drafts';
my ($drafts) = grep { lc($_->{'name'}) eq lc($df) } @rv;
if (!$drafts && $ok == 1) {
my @irv = &imap_command($ih, "create \"$df\"");
if ($irv[0]) {
$drafts = { 'id' => $df,
'type' => 4,
'server' => $imapserver,
'user' => $rv[0]->{'user'},
'pass' => $rv[0]->{'pass'},
'mode' => 3,
'remote' => 1,
'flags' => 1,
'imapauto' => 1,
'mailbox' => $df,
'index' => scalar(@rv) };
push(@rv, $drafts);
&read_file("$user_module_config_directory/$df.imap",
$drafts);
}
}
if ($drafts) {
$drafts->{'name'} = $text{'folder_drafts'};
$drafts->{'drafts'} = 1;
$drafts->{'mode'} = 3;
}
# Find or create the IMAP trash folder
if ($userconfig{'delete_mode'} == 1) {
my $tf = $userconfig{'trash_name'} || 'trash';
my ($trash) = grep { lc($_->{'name'}) eq lc($tf) } @rv;
if (!$trash && $ok == 1) {
my @irv = &imap_command($ih, "create \"$tf\"");
if ($irv[0]) {
$trash = { 'id' => $tf,
'type' => 4,
'server' => $imapserver,
'user' => $rv[0]->{'user'},
'pass' => $rv[0]->{'pass'},
'mode' => 3,
'remote' => 1,
'flags' => 1,
'imapauto' => 1,
'mailbox' => $tf,
'index' => scalar(@rv) };
push(@rv, $trash);
&read_file(
"$user_module_config_directory/$tf.imap",
$trash);
}
}
if ($trash) {
$trash->{'name'} = $text{'folder_trash'};
$trash->{'trash'} = 1;
$trash->{'mode'} = 3;
}
}
# For each IMAP folder, guess the underlying file
foreach my $f (@rv) {
if ($f->{'inbox'}) {
# Use the configured inbox location
my $path = $config{'mail_system'} == 0 ?
&user_mail_file(@remote_user_info) :
$qmail_maildir;
$f->{'file'} = $path if (-e $path);
}
else {
# Look in configured folders directory
my $path = "$folders_dir/$f->{'id'}";
if (-e $path) {
$f->{'file'} = $path;
}
else {
# Try . at start of folder names
my $n = $f->{'id'};
$n =~ s/^\.//;
if ($n =~ /\//) {
# Turn foo/bar to foo/.bar
$n =~ s/\//\/\./;
}
else {
# Turn foo to .foo
$n = ".".$n;
}
$path = "$folders_dir/$n";
$f->{'file'} = $path if (-e $path);
}
}
}
goto IMAPONLY;
}
else {
# Local mail file inbox
push(@rv, { 'name' => $text{'folder_inbox'},
'type' => $config{'mail_system'},
'mode' => 3,
'inbox' => 1,
'file' => $config{'mail_system'} == 0 ?
&user_mail_file(@remote_user_info) :
$qmail_maildir,
'index' => 0 });
$done{$rv[$#rv]->{'file'}}++;
}
my $inbox = $rv[$#rv];
# Add sent mail file
my $sf;
if ($folder_types{'ext'} && $userconfig{'sent_mail'}) {
$sf = $userconfig{'sent_mail'};
$done{$userconfig{'sent_mail'}}++;
}
else {
my $sfn = $userconfig{'sent_name'} || 'sentmail';
$sf = "$folders_dir/$sfn";
if (!-e $sf && $userconfig{'mailbox_dir'} eq "Maildir") {
# For Maildir++ , use .sentmail
$sf = "$folders_dir/.$sfn";
}
}
$done{$sf}++;
my $sft = -e $sf ? &folder_type($sf) :
$userconfig{'mailbox_dir'} eq "Maildir" ? 1 : 0;
push(@rv, { 'name' => $text{'folder_sent'},
'type' => $sft,
'file' => $sf,
'perpage' => $userconfig{'perpage_sent_mail'},
'fromaddr' => $userconfig{'fromaddr_sent_mail'},
'hide' => $userconfig{'hide_sent_mail'},
'mode' => 2,
'sent' => 1,
'index' => scalar(@rv) });
# Add drafts file
my $dn = $userconfig{'drafts_name'};
if ($dn && $userconfig{'mailbox_dir'} eq "Maildir" && $dn !~ /^\./) {
# Maildir++ folders always start with .
$dn = ".".$dn;
}
my $df = $dn ? "$folders_dir/$dn" :
-r "$folders_dir/Drafts" ? "$folders_dir/Drafts" :
-r "$folders_dir/.Drafts" ? "$folders_dir/.Drafts" :
-r "$folders_dir/.drafts" ? "$folders_dir/.drafts" :
$userconfig{'mailbox_dir'} eq "Maildir" ? "$folders_dir/.drafts" :
"$folders_dir/drafts";
$done{$df}++;
my $dft = -e $df ? &folder_type($df) :
$userconfig{'mailbox_dir'} eq "Maildir" ? 1 : 0;
push(@rv, { 'name' => $text{'folder_drafts'},
'type' => $dft,
'file' => $df,
'mode' => 3,
'drafts' => 1,
'index' => scalar(@rv) });
# If using a trash folder, add it
if ($userconfig{'delete_mode'} == 1) {
my $tn = $userconfig{'trash_name'};
if ($tn && $userconfig{'mailbox_dir'} eq "Maildir" && $tn !~ /^\./) {
# Maildir++ folders always start with .
$tn = ".".$tn;
}
my $tf = $tn ? "$folders_dir/$tn" :
-r "$folders_dir/Trash" ? "$folders_dir/Trash" :
-r "$folders_dir/.Trash" ? "$folders_dir/.Trash" :
-r "$folders_dir/.trash" ? "$folders_dir/.trash" :
$userconfig{'mailbox_dir'} eq "Maildir" ?
"$folders_dir/.trash" : "$folders_dir/trash";
$done{$tf}++;
my $tft = -e $tf ? &folder_type($tf) :
$userconfig{'mailbox_dir'} eq "Maildir" ? 1 : 0;
push(@rv, { 'name' => $text{'folder_trash'},
'type' => $tft,
'file' => $tf,
'mode' => 3,
'trash' => 1,
'index' => scalar(@rv) });
}
# Add local folders, usually under ~/mail
if ($folder_types{'local'}) {
foreach my $p (&recursive_files($folders_dir,
$userconfig{'mailbox_recur'})) {
my $f = $p;
$f =~ s/^\Q$folders_dir\E\///;
my $name = $f;
if ($folders_dir eq "$remote_user_info[7]/Maildir") {
# A sub-folder under Maildir .. remove the . at the
# start of the sub-folder name
$name =~ s/^\.// || $name =~ s/\/\./\// || next;
# When in Maildir++ mode, any non-subdirectory
# is ignored
next if (!-d $p);
}
push(@rv, { 'name' => $name,
'file' => $p,
'type' => &folder_type($p),
'perpage' => $userconfig{"perpage_$f"},
'fromaddr' => $userconfig{"fromaddr_$f"},
'show_to' => $userconfig{"show_to_$f"},
'sent' => $userconfig{"sent_$f"},
'hide' => $userconfig{"hide_$f"},
'mode' => 0,
'index' => scalar(@rv) } ) if (!$done{$p});
$done{$p}++;
}
}
# Add sub-folders in ~/Maildir/ , as used by Courier
if ($inbox->{'type'} == 1 && $userconfig{'mailbox_dir'} ne "Maildir") {
foreach my $p (&recursive_files($inbox->{'file'}, 0)) {
my $f = $p;
$f =~ s/^\Q$inbox->{'file'}\E\///;
my $name = $f;
$name =~ s/^\.// || $name =~ s/\/\./\//;
push(@rv, { 'name' => $name,
'file' => $p,
'type' => &folder_type($p),
'perpage' => $userconfig{"perpage_$f"},
'fromaddr' => $userconfig{"fromaddr_$f"},
'show_to' => $userconfig{"show_to_$f"},
'sent' => $userconfig{"sent_$f"},
'hide' => $userconfig{"hide_$f"},
'mode' => 0,
'index' => scalar(@rv) } ) if (!$done{$p});
$done{$p}++;
}
}
# Add user-defined external mail file folders
if ($folder_types{'ext'}) {
foreach my $o (split(/\t+/, $userconfig{'mailboxes'})) {
$o =~ /\/([^\/]+)$/ || next;
push(@rv, { 'name' => $userconfig{"folder_$o"} || $1,
'file' => $o,
'perpage' => $userconfig{"perpage_$o"},
'fromaddr' => $userconfig{"fromaddr_$o"},
'show_to' => $userconfig{"show_to_$o"},
'sent' => $userconfig{"sent_$o"},
'hide' => $userconfig{"hide_$o"},
'type' => &folder_type($o),
'mode' => 1,
'index' => scalar(@rv) } ) if (!$done{$o});
$done{$o}++;
}
}
# Add user-defined POP3 and IMAP folders
foreach my $f (@folderfiles) {
if ($f =~ /^(\d+)\.pop3$/ && $folder_types{'pop3'}) {
my %pop3 = ( 'id' => $1 );
&read_file("$user_module_config_directory/$f", \%pop3);
$pop3{'type'} = 2;
$pop3{'mode'} = 0;
$pop3{'remote'} = 1;
$pop3{'nowrite'} = 1;
$pop3{'index'} = scalar(@rv);
push(@rv, \%pop3);
}
elsif ($f =~ /^(\d+)\.imap$/ && $folder_types{'imap'}) {
my %imap = ( 'id' => $1 );
&read_file("$user_module_config_directory/$f", \%imap);
$imap{'type'} = 4;
$imap{'mode'} = 0;
$imap{'remote'} = 1;
$imap{'flags'} = 1;
$imap{'index'} = scalar(@rv);
push(@rv, \%imap);
}
}
# When in IMAP inbox mode, we goto this label to skip all my folders
IMAPONLY:
# Add user-defined composite folders
my %fcache;
foreach my $f (@folderfiles) {
if ($f =~ /^(\d+)\.comp$/) {
my %comp = ( 'id' => $1 );
&read_file("$user_module_config_directory/$f", \%comp);
$comp{'folderfile'} = "$user_module_config_directory/$f";
$comp{'type'} = 5;
$comp{'mode'} = 0;
$comp{'index'} = scalar(@rv);
my $sfn;
foreach my $sfn (split(/\t+/, $comp{'subfoldernames'})) {
my $sf = &find_named_folder($sfn, \@rv, \%fcache);
push(@{$comp{'subfolders'}}, $sf) if ($sf);
}
push(@rv, \%comp);
}
}
# Add spam folder as specified in spamassassin module, in case it is
# outside of the folders we scan
if (&foreign_check("spam")) {
my %suserconfig = &foreign_config("spam", 1);
my $file = $suserconfig{'spam_file'};
$file =~ s/\.$//;
$file =~ s/\/$//;
$file = "$remote_user_info[7]/$file" if ($file && $file !~ /^\//);
$file =~ s/\~/$remote_user_info[7]/;
if ($file) {
if ($config{'mail_system'} == 4) {
# In IMAP mode, the first folder named spam is marked
my ($sf) = grep { lc($_->{'name'}) eq 'spam' } @rv;
if ($sf) {
$sf->{'spam'} = 1;
}
}
elsif (!$done{$file}) {
# Need to add
push(@rv, { 'name' => "Spam",
'file' => $file,
'type' => &folder_type($file),
'perpage' => $userconfig{"perpage_$file"},
'fromaddr' => $userconfig{"fromaddr_$file"},
'sent' => $userconfig{"sent_$file"},
'hide' => 0,
'mode' => 0,
'spam' => 1,
'index' => scalar(@rv) } );
$done{$file}++;
}
else {
# Mark as spam folder
my ($sf) = grep { $_->{'file'} eq $file } @rv;
if ($sf) {
$sf->{'spam'} = 1;
}
}
}
}
# Add virtual folders. This has to be last, so that other folders can be found
# based on virtual/composite indexes.
foreach my $f (@folderfiles) {
if ($f =~ /^(\d+)\.virt$/) {
my %virt = ( 'id' => $1 );
&read_file("$user_module_config_directory/$f", \%virt);
$virt{'folderfile'} = "$user_module_config_directory/$f";
$virt{'type'} = 6;
$virt{'mode'} = 0;
$virt{'index'} = scalar(@rv);
$virt{'noadd'} = 1;
$virt{'members'} = [ ];
push(@rv, \%virt);
}
}
# Expand virtual folder sub-folders
foreach my $virt (grep { $_->{'type'} == 6 } @rv) {
foreach my $k (keys %$virt) {
next if ($k !~ /^\d+$/);
next if ($virt->{$k} !~ /\t/); # Old format
my ($sfn, $id) = split(/\t+/, $virt->{$k}, 2);
my $sf = &find_named_folder($sfn, \@rv, \%fcache);
$virt->{'members'}->[$k] = [ $sf, $id ] if ($sf);
delete($virt->{$k});
}
}
# Work out last-modified time of all folders, and set sortable flag
&set_folder_lastmodified(\@rv);
# Set searchable flag
foreach my $f (@rv) {
$f->{'searchable'} = 1 if ($f->{'type'} != 6 ||
$f->{'id'} != $search_folder_id);
}
# Set show to/from flags
foreach my $f (@rv) {
if (!defined($f->{'show_to'})) {
$f->{'show_to'} = $f->{'sent'} || $f->{'drafts'} ||
$userconfig{'show_to'};
}
if (!defined($f->{'show_from'})) {
$f->{'show_from'} = !($f->{'sent'} || $f->{'drafts'}) ||
$userconfig{'show_to'};
}
}
# For Maildir folders, check if we can get the read flag from the folder files
foreach my $f (@rv) {
if ($f->{'type'} == 1) {
$f->{'flags'} = 2;
}
}
@list_folders_cache = @rv;
return @rv;
}
# get_spam_inbox_folder()
# Returns the folder to which spam should be moved
sub get_spam_inbox_folder
{
my ($inbox) = grep { $_->{'inbox'} } &list_folders();
return $inbox;
}
# save_folder(&folder, [&old])
# Creates or updates a folder
sub save_folder
{
my ($folder, $old) = @_;
mkdir($folders_dir, 0700) if (!-d $folders_dir);
if ($folder->{'type'} == 2) {
# A POP3 folder
$folder->{'id'} ||= time();
my %pop3;
foreach my $k (keys %$folder) {
if ($k ne "type" && $k ne "mode" && $k ne "remote" &&
$k ne "nowrite" && $k ne "index") {
$pop3{$k} = $folder->{$k};
}
}
&write_file("$user_module_config_directory/$folder->{'id'}.pop3",
\%pop3);
chmod(0700, "$user_module_config_directory/$folder->{'id'}.pop3");
}
elsif ($folder->{'type'} == 4) {
# An IMAP folder
my @exclude;
if ($folder->{'imapauto'}) {
# This type of folder needs to be really created or updated
# on the server
if (!$folder->{'id'}) {
# Need to create
my ($ok, $ih) = &imap_login($folder);
my @irv = &imap_command($ih,
"create \"$folder->{'name'}\"");
$irv[0] || &error($irv[2]);
$folder->{'id'} = $folder->{'mailbox'} =
$folder->{'name'};
}
elsif ($folder->{'mailbox'} ne $folder->{'name'}) {
# Need to rename
my ($ok, $ih) = &imap_login($folder);
my @irv = &imap_command($ih,
"rename \"$folder->{'mailbox'}\" \"$folder->{'name'}\"");
$irv[0] || &error($irv[2]);
$folder->{'id'} = $folder->{'name'};
$folder->{'id'} = $folder->{'mailbox'} =
$folder->{'name'};
}
@exclude = ( "type", "mode", "remote", "nowrite", "index",
"id", "mailbox", "server", "user", "pass" );
}
else {
# Just save details of IMAP folder in a file
$folder->{'id'} ||= time();
@exclude = ( "type", "mode", "remote", "nowrite", "index" );
}
my %imap;
foreach my $k (keys %$folder) {
if (&indexof($k, @exclude) == -1) {
$imap{$k} = $folder->{$k};
}
}
&write_file("$user_module_config_directory/$folder->{'id'}.imap",
\%imap);
chmod(0700, "$user_module_config_directory/$folder->{'id'}.imap");
}
elsif ($folder->{'type'} == 5) {
# A composite
$folder->{'id'} ||= time();
my %comp;
foreach my $k (keys %$folder) {
if ($k ne "type" && $k ne "mode" && $k ne "index" &&
$k ne "subfolders") {
$comp{$k} = $folder->{$k};
}
}
my ($sf, @sfns);
foreach my $sf (@{$folder->{'subfolders'}}) {
my $sfn = &folder_name($sf);
push(@sfns, $sfn);
}
$comp{'subfoldernames'} = join("\t", @sfns);
&write_file("$user_module_config_directory/$folder->{'id'}.comp",
\%comp);
chmod(0700, "$user_module_config_directory/$folder->{'id'}.comp");
}
elsif ($folder->{'type'} == 6) {
# A virtual folder
$folder->{'id'} ||= time();
my %virt;
foreach my $k (keys %$folder) {
if ($k ne "type" && $k ne "mode" && $k ne "index" &&
$k ne "members") {
$virt{$k} = $folder->{$k};
}
}
my $i;
my $mems = $folder->{'members'};
for($i=0; $i<@$mems; $i++) {
$virt{$i} = &folder_name($mems->[$i]->[0])."\t".
$mems->[$i]->[1];
}
&write_file("$user_module_config_directory/$folder->{'id'}.virt",
\%virt);
chmod(0700, "$user_module_config_directory/$folder->{'id'}.virt");
}
elsif ($folder->{'mode'} == 0) {
# Updating a folder in ~/mail .. need to manage file, and config options
my $path = "$folders_dir/$folder->{'name'}";
if ($folders_dir eq "$remote_user_info[7]/Maildir") {
# Maildir sub-folder .. put a . in the name
$path =~ s/([^\/]+)$/.$1/;
}
if ($folder->{'name'} =~ /\//) {
my $pp = $path;
$pp =~ s/\/[^\/]+$//;
system("mkdir -p ".quotemeta($pp));
}
if (!$old) {
# Create the mailbox/maildir/MH dir
if ($folder->{'type'} == 0) {
open(my $FOLDER, ">>", "$path");
close($FOLDER);
chmod(0700, $path);
}
elsif ($folder->{'type'} == 1) {
mkdir($path, 0700);
mkdir("$path/cur", 0700);
mkdir("$path/new", 0700);
mkdir("$path/tmp", 0700);
}
elsif ($folder->{'type'} == 3) {
mkdir($path, 0700);
}
}
elsif ($old->{'name'} ne $folder->{'name'}) {
# Just rename
rename($old->{'file'}, $path);
}
if ($old) {
delete($userconfig{'perpage_'.$old->{'name'}});
delete($userconfig{'sent_'.$old->{'name'}});
delete($userconfig{'hide_'.$old->{'name'}});
delete($userconfig{'fromaddr_'.$old->{'name'}});
}
$userconfig{'perpage_'.$folder->{'name'}} = $folder->{'perpage'}
if ($folder->{'perpage'});
$userconfig{'sent_'.$folder->{'name'}} = $folder->{'sent'}
if ($folder->{'sent'});
$userconfig{'hide_'.$folder->{'name'}} = $folder->{'hide'}
if ($folder->{'hide'});
$userconfig{'fromaddr_'.$folder->{'name'}} = $folder->{'fromaddr'}
if ($folder->{'fromaddr'});
$userconfig{'show_to_'.$folder->{'name'}} = $folder->{'show_to'};
&save_user_module_config();
$folder->{'file'} = $path;
}
elsif ($folder->{'mode'} == 1) {
# Updating or adding an external file folder
my @mailboxes = split(/\t+/, $userconfig{'mailboxes'});
if (!$old) {
push(@mailboxes, $folder->{'file'});
}
else {
delete($userconfig{'folder_'.$folder->{'file'}});
delete($userconfig{'perpage_'.$folder->{'file'}});
delete($userconfig{'sent_'.$folder->{'file'}});
delete($userconfig{'hide_'.$folder->{'file'}});
delete($userconfig{'fromaddr_'.$folder->{'file'}});
my $idx = &indexof($folder->{'file'}, @mailboxes);
$mailboxes[$idx] = $folder->{'file'};
}
$userconfig{'folder_'.$folder->{'file'}} = $folder->{'name'};
$userconfig{'perpage_'.$folder->{'file'}} = $folder->{'perpage'}
if ($folder->{'perpage'});
$userconfig{'sent_'.$folder->{'file'}} = $folder->{'sent'};
$userconfig{'hide_'.$folder->{'file'}} = $folder->{'hide'}
if ($folder->{'hide'});
$userconfig{'fromaddr_'.$folder->{'file'}} = $folder->{'fromaddr'}
if ($folder->{'fromaddr'});
$userconfig{'show_to_'.$folder->{'file'}} = $folder->{'show_to'};
$userconfig{'mailboxes'} = join("\t", @mailboxes);
&save_user_module_config();
}
elsif ($folder->{'mode'} == 2) {
# The sent mail folder
delete($userconfig{'perpage_sent_mail'});
delete($userconfig{'hide_sent_mail'});
delete($userconfig{'fromaddr_sent_mail'});
my $sf = "$folders_dir/sentmail";
if ($folder->{'file'} eq $sf) {
delete($userconfig{'sent_mail'});
}
else {
$userconfig{'sent_mail'} = $folder->{'file'};
}
$userconfig{'perpage_sent_mail'} = $folder->{'perpage'}
if ($folder->{'perpage'});
$userconfig{'hide_sent_mail'} = $folder->{'hide'}
if ($folder->{'hide'});
$userconfig{'fromaddr_sent_mail'} = $folder->{'fromaddr'}
if ($folder->{'fromaddr'});
&save_user_module_config();
}
# Add to or update cache
if (@list_folders_cache) {
if ($old) {
my $idx = &indexof($old, @list_folders_cache);
if ($idx >= 0) {
$list_folders_cache[$idx] = $folder;
}
}
else {
push(@list_folders_cache, $folder);
}
}
}
# delete_folder(&folder)
# Removes some folder
sub delete_folder
{
my ($folder) = @_;
if ($folder->{'type'} == 2) {
# A POP3 folder
unlink("$user_module_config_directory/$folder->{'id'}.pop3");
system("rm -rf $user_module_config_directory/$folder->{'id'}.cache");
}
elsif ($folder->{'type'} == 4) {
# An IMAP folder
unlink("$user_module_config_directory/$folder->{'id'}.imap");