Handle hostnames with upper-case letters
[webmin.git] / sendmail / features-lib.pl
1 # features-lib.pl
2
3 # list_features()
4 # Returns a list of entries in the sendmail.mc file, each of which may be a
5 # feature or some other unrecognized line
6 sub list_features
7 {
8 local (@rv, $lnum = 0);
9 open(MC, $config{'sendmail_mc'});
10 while(<MC>) {
11         s/\r|\n//g;
12         local $f;
13         if (/^FEATURE\((.*)\)/i) {
14                 local ($name, @v) = &split_m4_params($1);
15                 $f = { 'type' => 1,
16                        'name' => $name,
17                        'values' => \@v };
18                 }
19         elsif (/^(define|undefine)\((.*)\)/i) {
20                 local @v = &split_m4_params($2);
21                 $f = { 'type' => ($1 eq 'define' ? 2 : 3),
22                        'name' => $v[0],
23                        'value' => $v[1] };
24                 }
25         elsif (/^MAILER\((.*)\)/i) {
26                 local ($mailer) = &split_m4_params($1);
27                 $f = { 'type' => 4,
28                        'mailer' => $mailer };
29                 }
30         elsif (/^OSTYPE\((.*)\)/i) {
31                 local ($ostype) = &split_m4_params($1);
32                 $f = { 'type' => 5,
33                        'line' => $lnum,
34                        'index' => scalar(@rv),
35                        'ostype' => $ostype };
36                 }
37         else {
38                 # Unrecognized line
39                 $f = { 'type' => 0 };
40                 }
41         if ($f) {
42                 $f->{'line'} = $lnum;
43                 $f->{'index'} = scalar(@rv);
44                 $f->{'text'} = $_;
45                 push(@rv, $f);
46                 }
47         $lnum++;
48         }
49 close(MC);
50 return @rv;
51 }
52
53 # split_m4_params(string)
54 sub split_m4_params
55 {
56 local @p;
57 local $str = $_[0];
58 while($str =~ /^`([^']*)'\s*,?\s*(.*)$/ ||
59       $str =~ /^([^\s,]+)\s*,?\s*(.*)$/) {
60         push(@p, $1);
61         $str = $2;
62         }
63 return @p;
64 }
65
66 # list_feature_types()
67 sub list_feature_types
68 {
69 local (@rv, $f);
70 opendir(DIR, "$config{'sendmail_features'}/feature");
71 while($f = readdir(DIR)) {
72         if ($f =~ /^(\S+)\.m4$/) {
73                 local $t = $text{'feat_'.lc($1)};
74                 push(@rv, [ $1, $t ? "$1 ($t)" : $1 ] );
75                 }
76         }
77 close(DIR);
78 return @rv;
79 }
80
81 # list_define_types()
82 # Returns a list of known define types. Some (but not all) will have human-
83 # readable descriptions
84 sub list_define_types
85 {
86 local (@rv, $d);
87 open(DEFINES, "$module_root_directory/defines");
88 while($d = <DEFINES>) {
89         $d =~ s/\r|\n//g;
90         local $t = $text{'def_'.lc($d)};
91         push(@rv, [ $d, $t ? "$d ($t)" : $d ]);
92         }
93 close(DEFINES);
94 return @rv;
95 }
96
97 # list_mailer_types()
98 sub list_mailer_types
99 {
100 local (@rv, $f);
101 opendir(DIR, "$config{'sendmail_features'}/mailer");
102 while($f = readdir(DIR)) {
103         if ($f =~ /^(\S+)\.m4$/) {
104                 local $t = $text{'mailer_'.lc($1)};
105                 push(@rv, [ $1, $t ? "$1 ($t)" : $1 ] );
106                 }
107         }
108 close(DIR);
109 return @rv;
110 }
111
112 # list_ostype_types()
113 sub list_ostype_types
114 {
115 local (@rv, $f);
116 opendir(DIR, "$config{'sendmail_features'}/ostype");
117 while($f = readdir(DIR)) {
118         if ($f =~ /^(\S+)\.m4$/) {
119                 local $t = $text{'ostype_'.lc($1)};
120                 push(@rv, [ $1, $t ? "$1 ($t)" : $1 ] );
121                 }
122         }
123 close(DIR);
124 return @rv;
125 }
126
127 # create_feature(&feature)
128 # Adds an entry to the end of the M4 config file
129 sub create_feature
130 {
131 &open_tempfile(MC, ">>$config{'sendmail_mc'}");
132 &print_tempfile(MC, &feature_line($_[0]),"\n");
133 &close_tempfile(MC);
134 $_[0]->{'text'} = &feature_line($_[0]);
135 }
136
137 # delete_feature(&feature)
138 # Deletes one entry from the M4 config file
139 sub delete_feature
140 {
141 local $lref = &read_file_lines($config{'sendmail_mc'});
142 splice(@$lref, $_[0]->{'line'}, 1);
143 &flush_file_lines();
144 }
145
146 # modify_feature(&feature)
147 # Updates an entry in the M4 config file
148 sub modify_feature
149 {
150 local $lref = &read_file_lines($config{'sendmail_mc'});
151 splice(@$lref, $_[0]->{'line'}, 1, &feature_line($_[0]));
152 &flush_file_lines();
153 $_[0]->{'text'} = &feature_line($_[0]);
154 }
155
156 # swap_features(&feature1, &feature2)
157 sub swap_features
158 {
159 local $lref = &read_file_lines($config{'sendmail_mc'});
160 splice(@$lref, $_[0]->{'line'}, 1, $_[1]->{'text'});
161 splice(@$lref, $_[1]->{'line'}, 1, $_[0]->{'text'});
162 &flush_file_lines();
163 }
164
165 # feature_line(&feature)
166 sub feature_line
167 {
168 if ($_[0]->{'type'} == 0) {
169         return $_[0]->{'text'};
170         }
171 elsif ($_[0]->{'type'} == 1) {
172         return "FEATURE(".join_m4_params($_[0]->{'name'}, @{$_[0]->{'values'}}).")";
173         }
174 elsif ($_[0]->{'type'} == 2) {
175         if ($_[0]->{'value'} eq '') {
176                 return "define(".join_m4_params($_[0]->{'name'}).")";
177                 }
178         else {
179                 return "define(".join_m4_params($_[0]->{'name'},
180                                                 $_[0]->{'value'}).")";
181                 }
182         }
183 elsif ($_[0]->{'type'} == 3) {
184         return "undefine(".join_m4_params($_[0]->{'name'}).")";
185         }
186 elsif ($_[0]->{'type'} == 4) {
187         return "MAILER(".join_m4_params($_[0]->{'mailer'}).")";
188         }
189 elsif ($_[0]->{'type'} == 5) {
190         return "OSTYPE(".join_m4_params($_[0]->{'ostype'}).")";
191         }
192 }
193
194 sub join_m4_params
195 {
196 local @rv = map { $_ =~ /^\d+$/ ? $_ : "`$_'" } @_;
197 return join(",", @rv);
198 }
199
200 1;
201