summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: f659c03)
raw | patch | inline | side by side (parent: f659c03)
author | cajus <cajus@594d385d-05f5-0310-b6e9-bd551577e9d8> | |
Mon, 3 Dec 2007 15:25:53 +0000 (15:25 +0000) | ||
committer | cajus <cajus@594d385d-05f5-0310-b6e9-bd551577e9d8> | |
Mon, 3 Dec 2007 15:25:53 +0000 (15:25 +0000) |
git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@7986 594d385d-05f5-0310-b6e9-bd551577e9d8
contrib/scripts/sieve_vacation/update-vacation.pl | patch | blob | history |
diff --git a/contrib/scripts/sieve_vacation/update-vacation.pl b/contrib/scripts/sieve_vacation/update-vacation.pl
index 0c4d4e3d0c1a0f242d6a3c07e9822f0769c429a7..aeef9649501077610c51ad8525a0a2e52681f241 100755 (executable)
-#!/usr/bin/perl -w -I.
+#!/usr/bin/perl -w -I/usr/local/lib/perl
#
# This code is part of GOsa (https://gosa.gonicus.de)
# Copyright (C) 2007 Frank Moeller
use XML::Simple;
use Data::Dumper;
use Net::LDAP;
+use URI;
use utf8;
use Getopt::Std;
+use Date::Format;
use vars qw/ %opt /;
#
# Definitions
#
my $gosa_config = "/etc/gosa/gosa.conf";
-my $opt_string = 'l:h';
+my $opt_string = 'l:hs';
my $location = "";
-my $today = time ();
-# default mailMethod = kolab
-my $server_attribute = "kolabHomeServer";
-my $alternate_address_attribute = "alias";
+my $today_gmt = time ();
+my $today = $today_gmt + 3600;
+my $server_attribute = "";
+my $alternate_address_attribute = "";
my $gosa_sieve_script_name = "gosa";
my $simple_bind_dn = "";
my $simple_bind_dn_pwd = "";
my $gosa_sieve_script_status = "FALSE";
-my $sieve_vacation = "";
my $gosa_sieve_spam_header = "Sort mails with higher spam level";
+my ($ss,$mm,$hh,$day,$month,$year,$zone);
#
# Templates
@@ -50,11 +52,22 @@ my $gosa_sieve_header = "\#\#\#GOSA\nrequire\ \[\"fileinto\",\ \"reject\",\ \"va
my $vacation_header_template = "\# Begin vacation message";
my $vacation_footer_template = "\# End vacation message";
+#
+# Placeholder
+#
+my $start_date_ph = "##STARTDATE##";
+my $stop_date_ph = "##STOPDATE##";
+
#
# Usage
#
sub usage {
- die "Usage:\nperl $0 [option]\n\n\tOptions:\n\t\t-l <\"location name\">\tuse special location\n\t\t-h\t\t\tthis help \n";
+ die "Usage:\nperl $0 [option]\n
+ \twithout any option $0 uses the default location\n
+ \tOptions:
+ \t\t-l <\"location name\">\tuse special location
+ \t\t-s\t\t\tshow all locations
+ \t\t-h\t\t\tthis help \n";
}
#
my $xml = new XML::Simple ();
my $c_data = $xml -> XMLin( $xmldata);
my $config = {};
- my $server;
my $config_base;
my $ldap_admin;
my $ldap_admin_pwd;
+ my $url;
my $mailMethod;
- if ( ! ( ref($c_data->{main}->{location}->{server}) ) ) {
- $server = $c_data->{main}->{location}->{server};
+ #print Dumper ($c_data->{main}->{location}->{config});
+ if ( $c_data->{main}->{location}->{config} ) {
+ #print "IF\n";
$config_base = $c_data->{main}->{location}->{config};
+ $url = $c_data->{main}->{location}->{referral}->{url};
$ldap_admin = $c_data->{main}->{location}->{referral}->{admin};
$ldap_admin_pwd = $c_data->{main}->{location}->{referral}->{password};
$mailMethod = $c_data->{main}->{location}->{mailMethod};
} else {
- $server = $c_data->{main}->{location}->{$c_location}->{server};
+ #print "ELSE\n";
$config_base = $c_data->{main}->{location}->{$c_location}->{config};
+ $url = $c_data->{main}->{location}->{$c_location}->{referral}->{url};
$ldap_admin = $c_data->{main}->{location}->{$c_location}->{referral}->{admin};
$ldap_admin_pwd = $c_data->{main}->{location}->{$c_location}->{referral}->{password};
$mailMethod = $c_data->{main}->{location}->{$c_location}->{mailMethod};
}
- $config->{server} = $server;
+ print "$config_base -- $url -- $ldap_admin -- $ldap_admin_pwd -- $mailMethod\n";
$config->{config_base} = $config_base;
+ $config->{url} = $url;
$config->{mailMethod} = $mailMethod;
$config->{ldap_admin} = $ldap_admin;
$config->{ldap_admin_pwd} = $ldap_admin_pwd;
return $default;
}
+#
+# List all location
+#
+sub list_locations {
+ my $xmldata = shift;
+ my $xml = new XML::Simple ( RootName=>'conf' );
+ my $c_data = $xml -> XMLin( $xmldata );
+ my $default = get_default_location ( $xmldata );
+ $default = $default . " (default)";
+ my @locations = ( $default );
+ my $data_ref = $c_data->{main}->{location};
+ my @keys = keys ( %{$data_ref} );
+ @locations = (@locations, @keys);
+
+ return @locations;
+}
+
#
# LDAP error handling
#
return $result;
}
+#
+# Retrieve LDAP server
+#
+sub get_ldap_server {
+ my $url = shift;
+
+ my $uri = URI->new($url);
+
+ my $scheme = $uri->scheme;
+ my $host = $uri->host;
+ my $port = $uri->port;
+ #print "$scheme - $host - $port\n";
+ my $server = $scheme . "://" . $host . ":" . $port;
+
+ return $server;
+}
+
#
# Retrieve LDAP base
#
my @scripts = $sieve->listscripts;
my $script_list = join("\n",@scripts)."\n";
+ #print $script_list;
return $script_list;
}
} elsif ( $opt{h} ) {
usage ();
exit (0);
+} elsif ( $opt{s} ) {
+ my $loc;
+ my $counter = 1;
+ my @locations = list_locations ( $input_stream );
+ print "\nConfigured Locations: \n";
+ print "---------------------\n";
+ foreach $loc ( @locations ) {
+ print $counter . ". " . $loc . "\n";
+ $counter++;
+ }
+ print "\n\n";
+ exit (0);
} else {
$location = get_default_location ( $input_stream );
}
-#print "$location\n";
# parse config
my $config = parseconfig ( $location, $input_stream );
-my $ldap_url = $config->{server};
+my $ldap_url = get_ldap_server ( $config->{url} );
my $gosa_config_base = $config->{config_base};
my $bind_dn = $config->{ldap_admin};
my $bind_dn_pwd = $config->{ldap_admin_pwd};
utf8::encode($gosa_config_base);
utf8::encode($mailMethod);
-if ( $mailMethod =~ m/cyrus/i ) {
- my $server_attribute = "gosaMailServer";
- my $alternate_address_attribute = "gosaMailAlternateAddress";
+# default mailMethod = kolab
+if ( $mailMethod =~ m/kolab/i ) {
+ $server_attribute = "kolabHomeServer";
+ $alternate_address_attribute = "alias";
+} elsif ( $mailMethod =~ m/cyrus/i ) {
+ $server_attribute = "gosaMailServer";
+ $alternate_address_attribute = "gosaMailAlternateAddress";
+} else {
+ exit (0);
}
# determine LDAP base
my $result = ldap_search ( $ldap_url, $filter, $search_scope, $ldap_base, $list_of_attributes, $simple_bind_dn, $simple_bind_dn_pwd );
my @entries = $result->entries;
+my $noe = @entries;
+#print "NOE = $noe\n";
my $entry = {};
foreach $entry ( @entries ) {
+ # INITIALISATIONS
+ $gosa_sieve_script_status = "FALSE";
+ my @sieve_scripts = "";
+ my $script_name = "";
+ my $sieve_script = "";
+ my $sieve_vacation = "";
+ # END INITIALISATIONS
my $uid_v = $entry->get_value ( 'uid' );
+ #print "$uid_v\n";
my $mail_v = $entry->get_value ( 'mail' );
my @mailalternate = $entry->get_value ( $alternate_address_attribute );
my $vacation = $entry->get_value ( 'gosaVacationMessage' );
my $start_v = $entry->get_value ( 'gosaVacationStart' );
my $stop_v = $entry->get_value ( 'gosaVacationStop' );
my $server_v = $entry->get_value ( $server_attribute );
+
+ # temp. hack to compensate old gosa server name style
+ #if ( $server_v =~ m/^imap\:\/\//i ) {
+ # $server_v =~ s/^imap\:\/\///;
+ #}
if ( ! ( $uid_v ) ) {
$uid_v = "";
}
if ( ! ( $vacation ) ) {
$vacation = "";
}
+
if ( ! ( $start_v ) ) {
$start_v = 0;
+ next;
}
+ #print time2str("%d.%m.%Y", $start_v)."\n";
+ my $start_date_string = time2str("%d.%m.%Y", $start_v)."\n";
+
if ( ! ( $stop_v ) ) {
$stop_v = 0;
+ next;
}
+ #print time2str("%d.%m.%Y", $stop_v)."\n";
+ my $stop_date_string = time2str("%d.%m.%Y", $stop_v)."\n";
+
+ chomp $start_date_string;
+ chomp $stop_date_string;
+ $vacation =~ s/$start_date_ph/$start_date_string/g;
+ $vacation =~ s/$stop_date_ph/$stop_date_string/g;
+
if ( ! ( $server_v ) ) {
$server_v = "";
next;
my ($sieve_user, $tmp) = split ( /\@/, $mail_v );
- if ( ( $today >= $start_v ) && ( $today < $stop_v ) ) {
+ print "today = $today\nstart = $start_v\nstop = $stop_v\n";
+ my $real_stop = $stop_v + 86400;
+ if ( ( $today >= $start_v ) && ( $today < $real_stop ) ) {
print "activating vacation for user $uid_v\n";
my $srv_filter = "(&(goImapName=$server_v)(objectClass=goImapServer))";
my $goImapAdmin = $srv_entries[0]->get_value ( 'goImapAdmin' );
my $goImapPassword = $srv_entries[0]->get_value ( 'goImapPassword' );
if ( ( $goImapSieveServer ) && ( $goImapSievePort ) && ( $goImapAdmin ) && ( $goImapPassword ) ) {
- my $sieve = opensieve ( $goImapAdmin, $goImapPassword, $sieve_user, $goImapSieveServer, $goImapSievePort);
- my @sieve_scripts = listscripts ( $sieve );
- my $script_name = "";
+# if ( ! ( $sieve_user = $uid_v ) ) {
+# $sieve_user = $uid_v;
+# }
+ #my $sieve = opensieve ( $goImapAdmin, $goImapPassword, $sieve_user, $goImapSieveServer, $goImapSievePort);
+ my $sieve = opensieve ( $goImapAdmin, $goImapPassword, $uid_v, $goImapSieveServer, $goImapSievePort);
+ @sieve_scripts = listscripts ( $sieve );
+ #print Dumper (@sieve_scripts);
+ $script_name = "";
if ( @sieve_scripts ) {
foreach $script_name ( @sieve_scripts ) {
if ( $script_name =~ m/$gosa_sieve_script_name/ ) {
}
}
if ( $gosa_sieve_script_status eq "TRUE" ) {
- print "retrieving and modifying gosa sieve script\n";
+ print "retrieving and modifying gosa sieve script for user $uid_v\n";
# requirements
- my $sieve_script = getscript( $sieve, $gosa_sieve_script_name );
+ $sieve_script = getscript( $sieve, $gosa_sieve_script_name );
#print "$sieve_script\n";
if ( ! ( $sieve_script ) ) {
print "No Sieve Script! Creating New One!\n";
#print "MATCH\n";
$sieve_script =~ s/($gosa_sieve_spam_header[^{}]*{[^{}]*})/$1\n\n$sieve_vacation/;
} else {
- $sieve_script =~ s/require(.*\[.*\])/require$1\n\n$sieve_vacation/;
+ $sieve_script =~ s/require(.*\[.*\]\;)/require$1\n\n$sieve_vacation/;
}
#print ( "START SIEVE $sieve_script\nSTOP SIEVE" );
# uploading new sieve script
# activating new sieve script
setactive( $sieve, $gosa_sieve_script_name );
} else {
- print "no gosa script available, creating new one";
- my $sieve_script = $gosa_sieve_header . "\n\n" . $sieve_vacation;
+ print "no gosa script available for user $uid_v, creating new one";
+ $sieve_script = $gosa_sieve_header . "\n\n" . $sieve_vacation;
# uploading new sieve script
putscript( $sieve, $gosa_sieve_script_name, $sieve_script );
# activating new sieve script
setactive( $sieve, $gosa_sieve_script_name );
}
}
+ closesieve ( $sieve );
}
}
- } elsif ( $today >= $stop_v ) {
+ } elsif ( $today >= $real_stop ) {
print "deactivating vacation for user $uid_v\n";
my $srv_filter = "(&(goImapName=$server_v)(objectClass=goImapServer))";
my $goImapAdmin = $srv_entries[0]->get_value ( 'goImapAdmin' );
my $goImapPassword = $srv_entries[0]->get_value ( 'goImapPassword' );
if ( ( $goImapSieveServer ) && ( $goImapSievePort ) && ( $goImapAdmin ) && ( $goImapPassword ) ) {
- my $sieve = opensieve ( $goImapAdmin, $goImapPassword, $sieve_user, $goImapSieveServer, $goImapSievePort);
- my @sieve_scripts = listscripts ( $sieve );
- my $script_name = "";
+ #my $sieve = opensieve ( $goImapAdmin, $goImapPassword, $sieve_user, $goImapSieveServer, $goImapSievePort);
+ my $sieve = opensieve ( $goImapAdmin, $goImapPassword, $uid_v, $goImapSieveServer, $goImapSievePort);
+ @sieve_scripts = listscripts ( $sieve );
+ $script_name = "";
if ( @sieve_scripts ) {
foreach $script_name ( @sieve_scripts ) {
if ( $script_name =~ m/$gosa_sieve_script_name/ ) {
}
if ( $gosa_sieve_script_status eq "TRUE" ) {
# removing vacation part
- my $sieve_script = getscript( $sieve, $gosa_sieve_script_name );
+ $sieve_script = getscript( $sieve, $gosa_sieve_script_name );
if ( $sieve_script ) {
#print "OLD SIEVE SCRIPT:\n$sieve_script\n\n";
$sieve_script =~ s/$vacation_header_template[^#]*$vacation_footer_template//;
}
}
}
+ closesieve ( $sieve );
}
}
} else {
- print "no vacation process necessary\n";
+ print "no vacation process necessary for user $uid_v\n";
}
}