<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl
# check.pl
# Run fetchmail, and send the output somewhere

$no_acl_check++;
$ENV{'REMOTE_USER'} = getpwuid($&lt;);
require './fetchmail-lib.pl';

# Parse command-line args
while(@ARGV &gt; 0) {
	local $a = shift(@ARGV);
	if ($a eq "--mail") {
		$mail = shift(@ARGV);
		}
	elsif ($a eq "--file") {
		$file = shift(@ARGV);
		}
	elsif ($a eq "--output") {
		$output = 1;
		}
	elsif ($a eq "--user") {
		$user = shift(@ARGV);
		}
	elsif ($a eq "--errors") {
		$errors = 1;
		}
	elsif ($a eq "--owner") {
		$owner = 1;
		}
	}

if ($fetchmail_config) {
	# Just run once for a single config file
	&amp;run_fetchmail($fetchmail_config, $user);
	}
else {
	# Run for all users
	setpwent();
	while(@uinfo = getpwent()) {
		next if ($donehome{$uinfo[7]}++);
		@conf = &amp;parse_config_file("$uinfo[7]/.fetchmailrc");
		@conf = grep { $_-&gt;{'poll'} } @conf;
		if (@conf) {
			&amp;run_fetchmail("$uinfo[7]/.fetchmailrc", $uinfo[0]);
			}
		}
	endpwent();
	}

# run_fetchmail(config, user)
sub run_fetchmail
{
local ($config, $user) = @_;

# Check if we have anything to do
local @conf = &amp;parse_config_file($config);
@conf = grep { $_-&gt;{'poll'} } @conf;
return if (!@conf);

# Build the command
local $cmd = "$config{'fetchmail_path'} -v -f ".quotemeta($config);
if ($config{'mda_command'}) {
	$cmd .= " -m ".quotemeta($config{'mda_command'});
	}
if ($user &amp;&amp; $user ne "root") {
	$cmd = &amp;command_as_user($user, 0, $cmd);
	}

# Run it
local $out = &amp;backquote_command("($cmd) 2&gt;&amp;1 &lt;/dev/null");
$ex = $? / 256;

# Handle the output
if ($owner) {
	# Force mailing to user
	$mail = $user."\@".&amp;get_system_hostname();
	}
if ($errors &amp;&amp; $ex &lt;= 1) {
	# No error occurred, so do nothing
	}
elsif ($file) {
	# Just write to a file
	open(FILE, "&gt;$file");
	print FILE $out;
	close(FILE);
	}
elsif ($mail) {
	# Capture output and email
	$mm = $module_info{'usermin'} ? "mailbox" : "mailboxes";
	if (&amp;foreign_check($mm)) {
		&amp;foreign_require($mm, "$mm-lib.pl");
		&amp;foreign_require($mm, "boxes-lib.pl");
		if ($module_info{'usermin'}) {
			($froms, $doms) =
				&amp;foreign_call($mm, "list_from_addresses");
			$fr = $froms-&gt;[0];
			}
		else {
			$fr = &amp;foreign_call($mm, "get_from_address");
			}
		&amp;foreign_call($mm, "send_text_mail", $fr, $mail, undef,
			      $ex &lt;= 1 ? $text{'email_ok'}
				       : $text{'email_failed'},
			      $out);
		}
	else {
		print "$mm module not installed - could not email the following output :\n";
		print $out;
		}
	}
elsif ($output) {
	# Output goes to cron
	print STDERR $out;
	}
else {
	# Just throw away output
	}
}

</pre></body></html>