Feed bogo depending on mailing-list moderation
[platal.git] / bin / cron / cron_ml_moderate.php
1 #!/usr/bin/php5 -q
2 <?php
3 /***************************************************************************
4 * Copyright (C) 2003-2008 Polytechnique.org *
5 * http://opensource.polytechnique.org/ *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the Free Software *
19 * Foundation, Inc., *
20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
21 ***************************************************************************/
22
23 require('./connect.db.inc.php');
24 ini_set('max_execution_time', '75');
25 $sent_mails = 0;
26 $handler = time();
27
28 while ($sent_mails < $globals->lists->max_mail_per_min
29 && time() - $handler < 60) {
30 // take a lock on a mail
31 XDB::execute("UPDATE ml_moderate
32 SET handler = {?}
33 WHERE handler IS NULL
34 ORDER BY ts
35 LIMIT 1", $handler);
36 if (XDB::affectedRows() == 0) {
37 break;
38 }
39 $query = XDB::query("SELECT nom, prenom, user_id, password,
40 ml, domain, mid, action, message
41 FROM auth_user_md5 AS u
42 INNER JOIN ml_moderate AS ml ON (u.user_id = ml.uid)
43 WHERE ml.handler = {?}", $handler);
44 list($nom, $prenom, $uid, $password, $list, $domain, $mid, $action, $reason) = $query->fetchOneRow();
45
46 // build the client
47 $client = new MMList($uid, $password, $domain);
48
49 // send the mail
50 $mail = $client->get_pending_mail($list, $mid);
51 list($det,$mem,$own) = $client->get_members($list);
52 $count = 0;
53 switch ($action) {
54 case 'accept':
55 $action = 1; /** 1 = ACCEPT **/
56 $subject = "Message accepté";
57 $append = "a été accepté par $prenom $nom.\n";
58 $type = 'nonspam';
59 $count += count($mem) + count($own);
60 break;
61 case 'refuse':
62 $action = 2; /** 2 = REJECT **/
63 $subject = "Message refusé";
64 $append = "a été refusé par $prenom $nom avec la raison :\n\n" . $reason;
65 $type = 'nonspam';
66 $count += count($own) + 1;
67 break;
68 case 'delete':
69 $action = 3; /** 3 = DISCARD **/
70 $subject = "Message supprimé";
71 $append = "a été supprimé par $prenom $nom.\n\n"
72 . "Rappel: il ne faut utiliser cette opération "
73 . "que dans le cas de spams ou de virus !\n";
74 $type = 'spam';
75 $count += count($own);
76 break;
77 }
78
79 if ($client->handle_request($list, $mid, $action, $reason)) {
80 $sent_mails += $count;
81 $texte = "le message suivant :\n\n"
82 . " Auteur: {$mail['sender']}\n"
83 . " Sujet : « {$mail['subj']} »\n"
84 . " Date : ".strftime("le %d %b %Y à %H:%M:%S", (int)$mail['stamp'])."\n\n"
85 . $append;
86 $mailer = new PlMailer();
87 $mailer->addTo("$list-owner@{$domain}");
88 $mailer->setFrom("$list-bounces@{$domain}");
89 $mailer->addHeader('Reply-To', "$list-owner@{$domain}");
90 $mailer->setSubject($subject);
91 $mailer->setTxtBody($texte);
92 $mailer->send();
93 }
94
95 // if the mail was classified as Unsure, feed bogo
96 $raw_mail = html_entity_decode($client->get_pending_mail($list, $mid, 1));
97 // search for the X-Spam-Flag header
98 $end_of_headers = strpos($raw_mail, "\r\n\r\n");
99 if ($end_of_headers === false) // sometimes headers are separated by \n
100 $end_of_headers = strpos($raw_mail, "\n\n");
101 $x_spam_flag = '';
102 if (preg_match('/^X-Spam-Flag: ([a-zA-Z]+), tests=bogofilter/m', substr($raw_mail, 0, $end_of_headers + 1), $matches))
103 $x_spam_flag = $matches[1];
104 if ($x_spam_flag == 'Unsure') {
105 $mailer = new PlMailer();
106 $mailer->addTo($type . '@' . $globals->mail->domain);
107 $mailer->setFrom('"' . $prenom . ' ' . $nom . '" <web@' . $globals->mail->domain . '>');
108 $mailer->setTxtBody($type . ' soumis par ' . $prenom . ' ' . $nom . ' via la modération de la liste ' . $list . '@' . $domain);
109 $mailer->addAttachment($raw_mail, 'message/rfc822', $type . '.mail', false);
110 $mailer->send();
111 }
112
113 // release the lock
114 XDB::execute("DELETE FROM ml_moderate WHERE handler = {?}",
115 $handler);
116 sleep(60 * $count / $globals->lists->max_mail_per_min);
117 }
118
119 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
120 ?>