e77643dcc4b31f6683d6b0225ee946259120b858
[platal.git] / bin / cron / cron_ml_moderate.php
1 #!/usr/bin/php5 -q
2 <?php
3 /***************************************************************************
4 * Copyright (C) 2003-2011 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 ini_set('memory_limit', '128M');
26 $sent_mails = 0;
27 $handler = time();
28
29 /* Cleanup dead locks */
30 XDB::execute('UPDATE email_list_moderate
31 SET handler = NULL
32 WHERE handler < NOW() - 300');
33
34 while ($sent_mails < $globals->lists->max_mail_per_min
35 && time() - $handler < 60) {
36 // take a lock on a mail
37 XDB::execute('UPDATE email_list_moderate
38 SET handler = {?}
39 WHERE handler IS NULL
40 ORDER BY ts
41 LIMIT 1', $handler);
42 if (XDB::affectedRows() == 0) {
43 break;
44 }
45 $query = XDB::query('SELECT a.full_name, a.uid, a.password,
46 ml.ml, ml.domain, ml.mid, ml.action, ml.message
47 FROM accounts AS a
48 INNER JOIN email_list_moderate AS ml ON (a.uid = ml.uid)
49 WHERE ml.handler = {?}', $handler);
50 list($fullname, $uid, $password, $list, $domain, $mid, $action, $reason) = $query->fetchOneRow();
51
52 // build the client
53 $client = new MMList($uid, $password, $domain);
54
55 // send the mail
56 $mail = $client->get_pending_mail($list, $mid);
57 list($det,$mem,$own) = $client->get_members($list);
58 $count = 0;
59 switch ($action) {
60 case 'accept':
61 $action = 1; /** 1 = ACCEPT **/
62 $subject = "Message accepté";
63 $append = "a été accepté par $fullname.\n";
64 $type = 'nonspam';
65 $count += count($mem) + count($own);
66 break;
67 case 'refuse':
68 $action = 2; /** 2 = REJECT **/
69 $subject = "Message refusé";
70 $append = "a été refusé par $fullname avec la raison :\n\n" . $reason;
71 $type = 'nonspam';
72 $count += count($own) + 1;
73 break;
74 case 'delete':
75 $action = 3; /** 3 = DISCARD **/
76 $subject = "Message supprimé";
77 $append = "a été supprimé par $fullname.\n\n"
78 . "Rappel : il ne faut utiliser cette opération "
79 . "que dans le cas de spams ou de virus !\n";
80 $type = 'spam';
81 $count += count($own);
82 break;
83 }
84
85 // if the mail was classified as Unsure, feed bogo
86 $raw_mail = html_entity_decode($client->get_pending_mail($list, $mid, 1));
87 // search for the X-Spam-Flag header
88 $end_of_headers = strpos($raw_mail, "\r\n\r\n");
89 if ($end_of_headers === false) { // sometimes headers are separated by \n
90 $end_of_headers = strpos($raw_mail, "\n\n");
91 }
92 $x_spam_flag = '';
93 if (preg_match('/^X-Spam-Flag: ([a-zA-Z]+), tests=bogofilter/m', substr($raw_mail, 0, $end_of_headers + 1), $matches)) {
94 $x_spam_flag = $matches[1];
95 }
96 if ($x_spam_flag == 'Unsure') {
97 $mailer = new PlMailer();
98 $mailer->addTo($type . '@' . $globals->mail->domain);
99 $mailer->setFrom('"' . $fullname . '" <web@' . $globals->mail->domain . '>');
100 $mailer->setTxtBody($type . ' soumis par ' . $fullname . ' via la modération de la liste ' . $list . '@' . $domain);
101 $mailer->addAttachment($raw_mail, 'message/rfc822', $type . '.mail', false);
102 $mailer->send();
103 }
104
105 // send feedback to the mailing list owners
106 if ($client->handle_request($list, $mid, $action, utf8_decode($reason))) {
107 $sent_mails += $count;
108 $texte = "Le message suivant :\n\n"
109 . " Auteur: {$mail['sender']}\n"
110 . " Sujet : « {$mail['subj']} »\n"
111 . " Date : ".strftime("le %d %b %Y à %H:%M:%S", (int)$mail['stamp'])."\n\n"
112 . $append;
113 $mailer = new PlMailer();
114 $mailer->addTo("$list-owner@{$domain}");
115 $mailer->setFrom("$list-bounces@{$domain}");
116 $mailer->addHeader('Reply-To', "$list-owner@{$domain}");
117 $mailer->setSubject($subject);
118 $mailer->setTxtBody($texte);
119 $mailer->send();
120 }
121
122 // release the lock
123 XDB::execute('DELETE FROM email_list_moderate
124 WHERE handler = {?}',
125 $handler);
126 sleep(60 * $count / $globals->lists->max_mail_per_min);
127 }
128
129 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
130 ?>