-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpiring-aliases.pl
72 lines (59 loc) · 2.17 KB
/
expiring-aliases.pl
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
#!/usr/bin/env perl
use strict;
use warnings;
use DBI;
my $dbuser = "user";
my $dbpass = "pass";
my $dbname = "mail";
my $today = date(0);
my $yesterday = date(-1);
my $dsn = "DBI:mysql:$dbname";
my $dbh = DBI->connect( $dsn, $dbuser, $dbpass, { RaiseError => 1 } )
or die "Could not connect to database: " . DBI->errstr;
my $sth = $dbh->prepare( "SELECT username, userrealm FROM virtual_users" );
$sth->execute();
while ( my ( $username, $userrealm ) = $sth->fetchrow_array() ) {
my $alias_today = "$username-EXPIRES-$today\@$userrealm";
my $alias_yesterday = "$username-EXPIRES-$yesterday\@$userrealm";
my $recipient = "$username\@$userrealm";
update_alias( $alias_today, $alias_yesterday )
or insert_alias( $alias_today, $recipient );
}
$sth->finish;
delete_alias("%-EXPIRES-$yesterday\@%");
$dbh->disconnect()
or warn "Could not disconnect from database: " . $dbh->errstr;
###############################################################################
sub update_alias {
###############################################################################
my ( $alias_today, $alias_yesterday ) = @_;
return $dbh->do( "
UPDATE virtual_aliases SET alias = $alias_today
WHERE alias = $alias_yesterday
" );
}
###############################################################################
sub insert_alias {
###############################################################################
my ( $alias_today, $recipient ) = @_;
return $dbh->do( "
INSERT INTO virtual_aliases ( alias, recipients )
VALUES( $alias_today, $recipient )
" );
}
###############################################################################
sub delete_alias {
###############################################################################
my ($expired_aliases) = @_;
return $dbh->do( "
DELETE FROM virtual_aliases
WHERE alias LIKE $expired_aliases
" );
}
###############################################################################
sub date {
###############################################################################
my ($day) = @_;
my ( $year, $mon, $mday ) = ( localtime( time + $day * 86400 ) )[ 5, 4, 3 ];
return sprintf( "%04d%02d%02d", $year + 1900, $mon + 1, $mday );
}