Skip to content
This repository has been archived by the owner on May 2, 2021. It is now read-only.

Commit

Permalink
Support for : config, syslog, lock and tempfile
Browse files Browse the repository at this point in the history
 * load configuration from a dedicated file
 * support a --config option
 * use syslog to log activities and errors
 * use a lock file to avoid several instances
 * write fetchmail configuration into a temp file
 * execute fetchmail with a specified configuration and pid file
  • Loading branch information
albanpeignier committed Jan 15, 2009
1 parent 6f1b038 commit fa4667a
Showing 1 changed file with 52 additions and 13 deletions.
65 changes: 52 additions & 13 deletions ADDITIONS/fetchmail.pl
Expand Up @@ -3,19 +3,53 @@
use DBI;
use MIME::Base64;
# use Data::Dumper;
use File::Temp qw/ mkstemp /;
use Sys::Syslog;
# require liblockfile-simple-perl
use LockFile::Simple qw(lock trylock unlock);

# the home dir of vmail user:
$vmail_dir="/home/maildirs";
openlog("fetchmail-all", "pid", "mail");

sub log_and_die {
my($message) = @_;
syslog("err", $message);
die $message;
}

# read options and arguments

$configfile = "/etc/fetchmail-all/config";

@ARGS1 = @ARGV;

while ($_ = shift @ARGS1) {
if (/^-/) {
if (/^--config$/) {
$configfile = shift @ARGS1
}
}
}

# mysql settings
$database="mailadmin";
$hostname="127.0.0.1";
$user="mail";
$password="*****";

$run_dir="/var/run/fetchmail";

# use specified config file
if (-e $configfile) {
do $configfile;
}

$dsn = "DBI:mysql:database=$database;host=$hostname";
$lock_file=$run_dir . "/fetchmail-all.lock";

$lockmgr = LockFile::Simple->make(-autoclean => 1, -max => 1);
$lockmgr->lock($lock_file) || log_and_die "can't lock ${lock_file}";

#mysql connect
$dbh = DBI->connect($dsn, $user, $password) || die "cannot connect the database";
$dbh = DBI->connect($dsn, $user, $password) || log_and_die "cannot connect the database";

$sql=<<SQL;
SELECT id,mailbox,src_server,src_auth,src_user,src_password,src_folder,fetchall,keep,protocol,mda,extra_options,usessl
Expand All @@ -26,6 +60,8 @@
my (%config);
map{
my ($id,$mailbox,$src_server,$src_auth,$src_user,$src_password,$src_folder,$fetchall,$keep,$protocol,$mda,$extra_options,$usessl)=@$_;

syslog("info","fetch ${src_user}@${src_server} for ${mailbox}");

$cmd="user '${src_user}' there with password '".decode_base64($src_password)."'";
$cmd.=" folder '${src_folder}'" if ($src_folder);
Expand All @@ -44,21 +80,24 @@
set nobouncemail
set no spambounce
set properties ""
set syslog
poll ${src_server} with proto ${protocol}
$cmd
TXT

open X,"> ${vmail_dir}/.fetchmailrc" || die "cannot open/create ${vmail_dir}/.fetchmailrc";
print X $text;
close X;
chmod 0600,"${vmail_dir}/.fetchmailrc";
$ret=`/usr/bin/fetchmail`;
$sql="UPDATE fetchmail SET returned_text=".$dbh->quote($ret).", date=now() WHERE id=".$id;
$dbh->do($sql);
($file_handler, $filename) = mkstemp( "/tmp/fetchmail-all-XXXXX" ) or log_and_die "cannot open/create fetchmail temp file";
print $file_handler $text;
close $file_handler;

}@{$dbh->selectall_arrayref($sql)};
$ret=`/usr/bin/fetchmail -f $filename -i $run_dir/fetchmail.pid`;

unlink $filename;

unlink "${vmail_dir}/.fetchmailrc";
$sql="UPDATE fetchmail SET returned_text=".$dbh->quote($ret).", date=now() WHERE id=".$id;
$dbh->do($sql);
}@{$dbh->selectall_arrayref($sql)};

$lockmgr->unlock($lock_file);
closelog();

0 comments on commit fa4667a

Please sign in to comment.