pongSmtp.pl

pongSmtp.pl Git Source Tree

Root/pongSmtp.pl

1#!/usr/bin/perl
2
3use strict;
4use warnings "all";
5use Getopt::Long;
6use DBI;
7
8# Version du programme
9my $version = '0.2'; my $dateVersion = 'Mars 2013';
10
11# Parametres
12my $params = {
13 'version' => $version,
14 'dateversion' => $dateVersion,
15 'programme' => 'pongSmtp',
16 'help' => 0,
17 'debug' => '',
18 'from' => 'ping@zici.fr',
19 'subject' => '[pongSmtp] ',
20 'sendmailBin' => '/usr/sbin/sendmail',
21 'sendmailOpt' => '-t',
22 'limitDb' => '/tmp/pongSmtp.db',
23 'limitNb' => 10,
24 'limitTime' => '-1 hour',
25 'limitTimeTxt' => '1 heures'
26};
27
28# Lecture des options ligne de commande
29GetOptions(
30 'help!' => \$params->{help},
31 'debug:s' => \$params->{debug},
32 'from:s' => \$params->{from},
33 'subject:s' => \$params->{subject},
34 'sendmailBin:s' => \$params->{sendmailBin},
35 'sendmailOpt:s' => \$params->{sendmailOpt},
36 'limitDb:s' => \$params->{limitDb},
37 'limitNb:n' => \$params->{limitNb},
38 'limitTime:s' => \$params->{limitTime},
39 'limitTimeTxt:s' => \$params->{limitTime}
40);
41
42if ($params->{help} > 0) {
43 print <<TEXTHELP;
44Programme : $params->{programme}.pl V$params->{version} - ($params->{dateversion})
45 Test un serveur SMTP en envoyant un ping\@hostname.
46
47
48Perl version : $]
49
50Usage : $params->{programme}.pl [Option ...]
51
52 Option :
53 -help : Afficher l'aide
54 -debug= : Log de debug
55
56 -subject= : Modifier le sujet du message de retour
57 -sendmailBin= : Bin de la commande sendmail (ex : /usr/sbin/sendmail)
58 -sendmailOpt= : Modifier le sujet du message de retour (ex : -t)
59
60 Attention aux permissions sur la destination de la base
61
62
63
64
65Installation :
66 * Editer votre fichier (exemple : /etc/aliases)
67 * Ajouter la ligne :
68 ping: "| perl /chemin/pongSmtp.pl --from=ping\@zici.fr -limitNb=35"
69 * Lancer la commande "newaliases"
70
71TEXTHELP
72 exit();
73}
74
75
76
77my $stdin;
78my $entetes="";
79my $subject="";
80my $received="";
81while ($stdin = <STDIN>) {
82 if ($stdin =~ /^Subject/) {
83
84 my ($subjectLabel, $subjectData) = split('Subject: ', $stdin);
85 $subject=$subjectData;
86} elsif ($stdin =~ /^Received/) {
87
88my ($receivedLabel, $receivedBy, $receivedIp) = split(' ', $stdin);
89 $received=$receivedIp;
90 } elsif ($stdin =~ /^Content-Type/) {
91
92 last;
93 }
94 $entetes=$entetes.$stdin;
95}
96
97# Si la limitation est active
98my $pongSmtpLogNb=0;
99if ($params->{limitDb}) {
100 my $db = DBI->connect("dbi:SQLite:".$params->{limitDb}, "", "", {RaiseError => 1, AutoCommit => 1});
101 $db->do("CREATE TABLE IF NOT EXISTS pongSmtpLog (id INTEGER PRIMARY KEY, date DATETIME, sender TEXT, received TEXT)");
102 $db->do("INSERT INTO pongSmtpLog VALUES (NULL, datetime('now'), '".$ENV{SENDER}."', '".$received."')");
103 my $req1 = $db->selectall_arrayref("SELECT id FROM pongSmtpLog WHERE date > datetime('now','".$params->{limitTime}."') AND (sender = '".$ENV{SENDER}."' OR received = '".$received."')");
104 $pongSmtpLogNb = scalar(@$req1);
105 if ($pongSmtpLogNb > $params->{limitNb}) {
106 exit();
107 } else {
108 if ($pongSmtpLogNb == $params->{limitNb}) {
109 $params->{subject}=$params->{subject}." [LIMIT EXCEEDED] ";
110 }
111 }
112
113$db->do("DELETE FROM pongSmtpLog WHERE date < datetime('now','".$params->{limitTime}."')");
114}
115
116
117open(PONG, "|$params->{sendmailBin} $params->{sendmailOpt} -f$params->{from}");
118print PONG "To: $ENV{SENDER}\n";
119print PONG "From: $params->{from}\n";
120print PONG "Subject: $params->{subject}$subject\n\n";
121# Si la limitation est active
122if ($params->{limitDb}) {
123
124}
125
126print PONG $entetes;
127print PONG "\n\npongSmtp projet : http://pongsmtp.zici.fr";
128close(PONG);
129

Archive Download this file

Branches