Handle hostnames with upper-case letters
[webmin.git] / sendmail / mailers-lib.pl
1 # mailers-lib.pl
2 # Functions for editing the mailertable database
3
4 # mailers_dbm(&config)
5 # Returns the filename of the mailertable database and type, or undef if none
6 sub mailers_dbm
7 {
8 foreach $f (&find_type("K", $_[0])) {
9         if ($f->{'value'} =~ /^mailertable\s+(\S+)[^\/]+(\/\S+)$/) {
10                 return ($2, $1);
11                 }
12         }
13 return undef;
14 }
15
16 # mailers_file(&config)
17 # Returns the filename of the text mailertable file, or undef if none
18 sub mailers_file
19 {
20 return &find_textfile($config{'mailers_file'}, &mailers_dbm($_[0]));
21 }
22
23 # list_mailers(textfile)
24 sub list_mailers
25 {
26 if (!scalar(@list_mailers_cache)) {
27         local $lnum = 0;
28         @list_mailers_cache = ( );
29         local $cmt;
30         open(MAILER, $_[0]);
31         while(<MAILER>) {
32                 s/\r|\n//g;     # remove newlines
33                 if (/^\s*#+\s*(.*)/) {
34                         # A comment line
35                         $cmt = &is_table_comment($_);
36                         }
37                 elsif (/^(\S+)\s+([^: ]+):(.*)/) {
38                         # An actual mailer line
39                         local(%virt);
40                         $virt{'domain'} = $1;
41                         $virt{'mailer'} = $2;
42                         $virt{'dest'} = $3;
43                         $virt{'line'} = $cmt ? $lnum-1 : $lnum;
44                         $virt{'eline'} = $lnum;
45                         $virt{'num'} = scalar(@list_mailers_cache);
46                         $virt{'cmt'} = $cmt;
47                         push(@list_mailers_cache, \%virt);
48                         $cmt = undef;
49                         }
50                 else {
51                         $cmt = undef;
52                         }
53                 $lnum++;
54                 }
55         close(MAILER);
56         }
57 return @list_mailers_cache;
58 }
59
60 # create_mailer(&details, textfile, dbmfile, dbmtype)
61 sub create_mailer
62 {
63 local(%mailer);
64 &list_mailers($_[1]);   # force cache init
65
66 # Write to the file
67 local $lref = &read_file_lines($_[1]);
68 $_[0]->{'line'} = scalar(@$lref);
69 push(@$lref, &make_table_comment($_[0]->{'cmt'}));
70 push(@$lref, "$_[0]->{'domain'}\t$_[0]->{'mailer'}:$_[0]->{'dest'}");
71 $_[0]->{'eline'} = scalar(@$lref)-1;
72 &flush_file_lines();
73
74 # Write to the DBM
75 if (!&rebuild_map_cmd($_[1])) {
76         if ($_[3] eq "dbm") {
77                 dbmopen(%mailer, $_[2], 0644);
78                 $mailer{$_[0]->{'domain'}} = "$_[0]->{'mailer'}:$_[0]->{'dest'}";
79                 dbmclose(%mailer);
80                 }
81         else { &run_makemap($_[1], $_[2], $_[3]); }
82         }
83
84 # Update the cache
85 $_[0]->{'num'} = scalar(@list_mailers_cache);
86 $_[0]->{'file'} = $_[1];
87 push(@list_mailers_cache, $_[0]);
88 }
89
90 # modify_mailer(&old, &details, textfile, dbmfile, dbmtype)
91 sub modify_mailer
92 {
93 local(@mailer, %mailer);
94
95 # Update the file
96 local $lref = &read_file_lines($_[2]);
97 local $oldlen = $_[0]->{'eline'} - $_[0]->{'line'} + 1;
98 local @newlines;
99 push(@newlines, &make_table_comment($_[1]->{'cmt'}));
100 push(@newlines, "$_[1]->{'domain'}\t$_[1]->{'mailer'}:$_[1]->{'dest'}");
101 splice(@$lref, $_[0]->{'line'}, $oldlen, @newlines);
102 &flush_file_lines($_[2]);
103
104 # Update the DBM
105 if (!&rebuild_map_cmd($_[2])) {
106         if ($_[3] eq "dbm") {
107                 dbmopen(%mailer, $_[3], 0644);
108                 delete($mailer{$_[0]->{'domain'}});
109                 $mailer{$_[1]->{'domain'}} = "$_[1]->{'mailer'}:$_[1]->{'dest'}";
110                 dbmclose(%mailer);
111                 }
112         else { &run_makemap($_[2], $_[3], $_[4]); }
113         }
114
115 local $idx = &indexof($_[0], @list_mailers_cache);
116 $_[1]->{'line'} = $_[0]->{'line'};
117 $_[1]->{'eline'} = $_[1]->{'cmt'} ? $_[0]->{'line'}+1 : $_[0]->{'line'};
118 $list_mailers_cache[$idx] = $_[1] if ($idx != -1);
119 &renumber_list(\@list_mailers_cache, $_[0], scalar(@newlines)-$oldlen);
120 }
121
122 # delete_mailer(&old, textfile, dbmfile, dbmtype)
123 sub delete_mailer
124 {
125 local(@mailer, %mailer);
126
127 # Delete from the file
128 local $len = $_[0]->{'eline'} - $_[0]->{'line'} + 1;
129 local $lref = &read_file_lines($_[1]);
130 splice(@$lref, $_[0]->{'line'}, $len);
131 &flush_file_lines($_[1]);
132
133 # Delete f rom the DBM
134 if (!&rebuild_map_cmd($_[1])) {
135         if ($_[3] eq "dbm") {
136                 dbmopen(%mailer, $_[2], 0644);
137                 delete($mailer{$_[0]->{'domain'}});
138                 dbmclose(%mailer);
139                 }
140         else { &run_makemap($_[1], $_[2], $_[3]); }
141         }
142
143 # Update the cache
144 local $idx = &indexof($_[0], @list_mailers_cache);
145 splice(@list_mailers_cache, $idx, 1) if ($idx != -1);
146 &renumber_list(\@list_mailers_cache, $_[0], -$len);
147 }
148
149 # mailer_form([&details])
150 sub mailer_form
151 {
152 local ($m) = @_;
153 local ($mode, $addr, $conf, $ml, $dest, $nomx);
154
155 print &ui_form_start("save_mailer.cgi", "post");
156 if ($m) {
157         print &ui_hidden("num", $m->{'num'});
158         }
159 else {
160         print &ui_hidden("new", 1);
161         }
162 print &ui_table_start($m ? $text{'mform_edit'} : $text{'mform_create'},
163                       undef, 2);
164
165 # Description
166 print &ui_table_row($text{'vform_cmt'},
167                     &ui_textbox("cmt", $m ? $m->{'cmt'} : undef, 50));
168
169 # Show 'mail for' input
170 local $dom = $m && $m->{'domain'} =~ /^\.(\S+)$/ ? $1 : undef;
171 print &ui_table_row($text{'mform_for'},
172     &ui_radio_table("from_type", $dom ? 1 : 0,
173         [ [ 0, $text{'mform_host2'},
174                &ui_textbox("from_host", $dom || !$m ? "" : $m->{'domain'},
175                            20) ],
176           [ 1, $text{'mform_domain2'},
177                &ui_textbox("from_dom", $dom, 20) ],
178           $m ? ( ) : ( [ 2, $text{'mform_domain3'},
179                             &ui_textbox("from_all", undef ,20) ] ) ]));
180
181 # Show delivery input
182 $conf = &get_sendmailcf();
183 local @mailers = ( { 'value' => 'error' }, &find_type("M", $conf) );
184 print &ui_table_row($text{'mform_delivery'},
185     &ui_select("mailer",
186                $m ? $m->{'mailer'} : "smtp",
187                [ map { $_->{'value'} =~ /^([^ ,]+)/;
188                        [ $1, $text{"mform_$1"} ] } @mailers ]));
189
190 # Show send to input
191 $dest = $m->{'dest'};
192 if ($dest =~ s/\[([^\]:]+)\]/$1/g) {
193         $nomx = 1;
194         }
195 else {
196         $dest = $m->{'dest'};
197         }
198 print &ui_table_row($text{'mform_to'},
199     &ui_textbox("dest", $dest, 30)."<br>".
200     &ui_checkbox("nomx", 1, $text{'mform_ignore'}, $nomx));
201
202 print &ui_table_end();
203 print &ui_form_end($_[0] ? [ [ "save", $text{'save'} ],
204                              [ "delete", $text{'delete'} ] ]
205                          : [ [ "create", $text{'create'} ] ]);
206 }
207
208 1;
209