Commit f7546060 authored by Côme Chilliet's avatar Côme Chilliet
Browse files

Merge branch '5723-add-a-log-of-mail-send-to-argonaut-reminder' into '1.3-dev'

Resolve "Add a log of emails sent by argonaut-user-reminder"

See merge request argonaut/argonaut!120
parents d00a1d2e 108448fa
......@@ -48,12 +48,13 @@ $config->{'fd_rdn'} = 'ou=fusiondirectory';
my $verbose = 0;
my $posix = 1;
my $ppolicy = 1;
my $maillog = '';
my $maillogfile;
sub print_usage
{
my ($help) = @_;
print "Usage : argonaut-user-reminder [--help] [--verbose] [--posix|--ppolicy|--both (default)]\n";
print "Usage : argonaut-user-reminder [--help] [--verbose] [--posix|--ppolicy|--both (default)] [--mail-log LOGFILE]\n";
if ($help) {
print << "EOF";
......@@ -62,6 +63,7 @@ sub print_usage
--posix : check POSIX account expiration
--ppolicy : check ppolicy password expiration
--both : check both (default)
--mail-log: store log of sent emails to LOGFILE
EOF
exit(0);
} else {
......@@ -70,7 +72,9 @@ EOF
}
foreach my $arg ( @ARGV ) {
if (lc($arg) eq "--verbose") {
if (not defined $maillog) {
$maillog = $arg;
} elsif (lc($arg) eq "--verbose") {
$verbose = 1;
} elsif (lc($arg) eq "--posix") {
$posix = 1;
......@@ -81,6 +85,8 @@ foreach my $arg ( @ARGV ) {
} elsif (lc($arg) eq "--both") {
$posix = 1;
$ppolicy = 1;
} elsif (lc($arg) eq "--mail-log") {
undef $maillog;
} elsif ((lc($arg) eq "--help") || (lc($arg) eq "-h")) {
print_usage(1);
} else {
......@@ -88,6 +94,15 @@ foreach my $arg ( @ARGV ) {
}
}
if (not defined $maillog) {
# --mail-log used with no filename after
print_usage(0);
}
if ($maillog ne '') {
open ($maillogfile, q{>}, $maillog) or die "Cannot open mail log file ".$maillog;
}
check_expired_users();
exit 0;
......@@ -298,6 +313,8 @@ sub alert_user_if_needed
return;
}
my $first_email = 1;
# Check if we already sent an email.
my ($token_hash, $token_datetime) = get_ldap_token($ldap, $entry->get_value('uid'));
if ((defined $token_datetime) && ($token_datetime + ($config->{'resend_delay'} * 86400) > $now)) {
......@@ -306,6 +323,7 @@ sub alert_user_if_needed
} elsif ((defined $token_hash) || (defined $token_datetime)) {
# Delete obsolete token so we may create it again
delete_ldap_token($ldap, $entry->get_value('uid'));
$first_email = 0;
}
my ($manager_cn, $manager_mail);
......@@ -337,12 +355,12 @@ sub alert_user_if_needed
$manager_mail = get_mail_from_entry(($manager_mesg->entries)[0]);
}
}
send_alert_mail($ldap, $entry->get_value('uid'), $now, $cn, $mail_address, $manager_cn, $manager_mail, $ppolicy_mode);
send_alert_mail($ldap, $entry->get_value('uid'), $now, $cn, $mail_address, $manager_cn, $manager_mail, $ppolicy_mode, $first_email);
}
sub send_alert_mail
{
my ($ldap, $uid, $datetime, $user_cn, $user_mail, $manager_cn, $manager_mail, $ppolicy_mode) = @_;
my ($ldap, $uid, $datetime, $user_cn, $user_mail, $manager_cn, $manager_mail, $ppolicy_mode, $first_email) = @_;
my ($alert_mailsubject, $alert_mailbody, $token);
if ($ppolicy_mode) {
$alert_mailsubject = $config->{'ppolicy_mailsubject'};
......@@ -358,13 +376,30 @@ sub send_alert_mail
return;
}
print "Sending mail to $user_cn<$user_mail>" if $verbose;
if (defined $maillogfile) {
print $maillogfile, scalar(localtime)." $user_cn<$user_mail>";
}
my $cc = "";
if (defined $manager_mail) {
print ", copy to $manager_cn<$manager_mail>" if $verbose;
$cc = encode_mimewords($manager_cn, Charset => 'utf-8', Encoding => 'B')." <$manager_mail>";
if (defined $maillogfile) {
print $maillogfile, " $manager_cn<$manager_mail>";
}
}
print " with token $token" if ($verbose and (not $ppolicy_mode));
print "\n" if $verbose;
if (defined $maillogfile) {
if ($first_email) {
print $maillogfile, " (first email)";
} else {
print $maillogfile, " (resent email)";
}
if ($ppolicy_mode) {
print $maillogfile, " (from ppolicy)";
}
print $maillogfile, "\n";
}
my $body = sprintf($alert_mailbody,$user_cn,$uid,$token);
my %message = (
'From' => $config->{'alert_mailaddress'},
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment