Handle hostnames with upper-case letters
[webmin.git] / fetchmail / check.pl
1 #!/usr/local/bin/perl
2 # check.pl
3 # Run fetchmail, and send the output somewhere
4
5 $no_acl_check++;
6 $ENV{'REMOTE_USER'} = getpwuid($<);
7 require './fetchmail-lib.pl';
8
9 # Parse command-line args
10 while(@ARGV > 0) {
11         local $a = shift(@ARGV);
12         if ($a eq "--mail") {
13                 $mail = shift(@ARGV);
14                 }
15         elsif ($a eq "--file") {
16                 $file = shift(@ARGV);
17                 }
18         elsif ($a eq "--output") {
19                 $output = 1;
20                 }
21         elsif ($a eq "--user") {
22                 $user = shift(@ARGV);
23                 }
24         elsif ($a eq "--errors") {
25                 $errors = 1;
26                 }
27         elsif ($a eq "--owner") {
28                 $owner = 1;
29                 }
30         }
31
32 if ($fetchmail_config) {
33         # Just run once for a single config file
34         &run_fetchmail($fetchmail_config, $user);
35         }
36 else {
37         # Run for all users
38         setpwent();
39         while(@uinfo = getpwent()) {
40                 next if ($donehome{$uinfo[7]}++);
41                 @conf = &parse_config_file("$uinfo[7]/.fetchmailrc");
42                 @conf = grep { $_->{'poll'} } @conf;
43                 if (@conf) {
44                         &run_fetchmail("$uinfo[7]/.fetchmailrc", $uinfo[0]);
45                         }
46                 }
47         endpwent();
48         }
49
50 # run_fetchmail(config, user)
51 sub run_fetchmail
52 {
53 local ($config, $user) = @_;
54
55 # Check if we have anything to do
56 local @conf = &parse_config_file($config);
57 @conf = grep { $_->{'poll'} } @conf;
58 return if (!@conf);
59
60 # Build the command
61 local $cmd = "$config{'fetchmail_path'} -v -f ".quotemeta($config);
62 if ($config{'mda_command'}) {
63         $cmd .= " -m ".quotemeta($config{'mda_command'});
64         }
65 if ($user && $user ne "root") {
66         $cmd = &command_as_user($user, 0, $cmd);
67         }
68
69 # Run it
70 local $out = &backquote_command("($cmd) 2>&1 </dev/null");
71 $ex = $? / 256;
72
73 # Handle the output
74 if ($owner) {
75         # Force mailing to user
76         $mail = $user."\@".&get_system_hostname();
77         }
78 if ($errors && $ex <= 1) {
79         # No error occurred, so do nothing
80         }
81 elsif ($file) {
82         # Just write to a file
83         open(FILE, ">$file");
84         print FILE $out;
85         close(FILE);
86         }
87 elsif ($mail) {
88         # Capture output and email
89         $mm = $module_info{'usermin'} ? "mailbox" : "mailboxes";
90         if (&foreign_check($mm)) {
91                 &foreign_require($mm, "$mm-lib.pl");
92                 &foreign_require($mm, "boxes-lib.pl");
93                 if ($module_info{'usermin'}) {
94                         ($froms, $doms) =
95                                 &foreign_call($mm, "list_from_addresses");
96                         $fr = $froms->[0];
97                         }
98                 else {
99                         $fr = &foreign_call($mm, "get_from_address");
100                         }
101                 &foreign_call($mm, "send_text_mail", $fr, $mail, undef,
102                               $ex <= 1 ? $text{'email_ok'}
103                                        : $text{'email_failed'},
104                               $out);
105                 }
106         else {
107                 print "$mm module not installed - could not email the following output :\n";
108                 print $out;
109                 }
110         }
111 elsif ($output) {
112         # Output goes to cron
113         print STDERR $out;
114         }
115 else {
116         # Just throw away output
117         }
118 }
119