summaryrefslogtreecommitdiff
blob: b0acb569f2e73bffef9bf103f2ce5d85efca6127 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/perl

use Date::Calc qw(:all);
use Net::SMTP;

($year,$month,$day) = Today();
($hour,$min,$sec) = Now();

my $server = $ARGV[0]; 
my $ldapdc = $ARGV[1]; 
my $ldpass = $ARGV[2]; 

system("ldapsearch -x -LL -H $server -b $ldapdc -s sub -D cn=admin,$ldapdc -w $ldpass \"(birthDate=*)\" cn mail birthDate > /tmp/birthday.tmp");

open BDAY, "</tmp/birthday.tmp";

while ($line = <BDAY>)
{
    if($line =~ /^dn:/)
    {
	$user = $line;
    }
    if($line =~ /^cn:/)
    {
	($bday{$user}{"NAME"}) = ($line =~ /cn: (.*)/);
    }
    if($line =~ /^mail:/)
    {
	($bday{$user}{"MAIL"}) = ($line =~ /mail: (.*)/);
    }
    if($line =~ /^birthDate:/)
    {
	($bday{$user}{"BDAY"}{"YEAR"}, $bday{$user}{"BDAY"}{"MONTH"}, $bday{$user}{"BDAY"}{"DAY"}) = ($line =~ /birthDate: (\d\d\d\d)-(\d\d)-(\d\d).*/);
	$bday{$user}{"AGE"} = ($year - $bday{$user}{"BDAY"}{"YEAR"});
	$bday{$user}{"TLEFT"} = Delta_Days($year, $month, $day, $year, $bday{$user}{"BDAY"}{"MONTH"}, $bday{$user}{"BDAY"}{"DAY"});
    }
}

foreach $user (keys %bday)
{
    if ($bday{$user}{"TLEFT"}==7 || $bday{$user}{"TLEFT"}==-358)
    {
	&bdayMail($bday{$user}{"NAME"},$bday{$user}{"MAIL"},$bday{$user}{"AGE"},"Der Geburtstag von " . $bday{$user}{"NAME"} . " naht!", $bday{$user}{"BDAY"}{"DAY"} . "." . $bday{$user}{"BDAY"}{"MONTH"});
    }
    if ($bday{$user}{"TLEFT"}==0)
    {
	&bdayMail($bday{$user}{"NAME"},$bday{$user}{"MAIL"},$bday{$user}{"AGE"},"Heute hat " . $bday{$user}{"NAME"} . " Geburtstag!", $bday{$user}{"BDAY"}{"DAY"} . "." . $bday{$user}{"BDAY"}{"MONTH"});
    }
} 

system("rm /tmp/birthday.tmp");

sub bdayMail {
    $smtp = Net::SMTP->new('localhost')
	or die "Can't connect SMTP localhost!\n";

    $recp{'post@gunnarwrobel.de'}{"NAME"} = "Gunnar";

    $date = sprintf("%.3s, %02d %.3s %d %d:%d:%d +0200",
      Day_of_Week_to_Text(Day_of_Week($year,$month,$day)),
      $day,
      Month_to_Text($month),
		   $year,$hour,$min,$sec);

    foreach $localname (keys %recp)
    {
	$mail  = 'From: Birthday Skript <system@gunnarwrobel.de>' . "\n";
	$mail .= "To: " . $recp{$localname}{"NAME"} . " <" . $localname .">\n";
	$mail .= "Date: " . $date . "\n";
	$mail .= "Subject: " . $_[3] . "\n\n";
	$mail .= "Am " . $_[4] . " wird " . $_[0] . " " . $_[2] . " Jahre alt.\n\n";
	if (!$_[1] eq "")
	{
	    $mail .= "Falls Du eine Mail schreiben moechtest: " . $_[1] . "\n";
	}
	$smtp->mail('system@gunnarwrobel.de');
	$smtp->to($localname);
	$smtp->data();
	$smtp->datasend($mail);
	$smtp->dataend();
	$smtp->quit;
    }
}