Convert cron_ml_moderate to MailingList.
[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 uid, ml, domain, mid, action, message
46 FROM email_list_moderate
47 WHERE handler = {?}', $handler);
48 list($uid, $list, $domain, $mid, $action, $reason) = $query->fetchOneRow();
49 $user = User::get($uid);
50 $fullname = $user->fullName();
51 $mlist = new MailingList($list, $domain, $user);
52
53 // send the mail
54 $mail = $mlist->getPendingMail($mid);
55 list($det,$mem,$own) = $mlist->getMembers();
56 $count = 0;
57 switch ($action) {
58 case 'accept':
59 $action = MailingList::REQ_ACCEPT;
60 $subject = "Message accepté";
61 $append = "a été accepté par $fullname.\n";
62 $type = 'nonspam';
63 $count += count($mem) + count($own);
64 break;
65 case 'refuse':
66 $action = MailingList::REQ_REJECT;
67 $subject = "Message refusé";
68 $append = "a été refusé par $fullname avec la raison :\n\n" . $reason;
69 $type = 'nonspam';
70 $count += count($own) + 1;
71 break;
72 case 'delete':
73 $action = MailingList::REQ_DISCARD;
74 $subject = "Message supprimé";
75 $append = "a été supprimé par $fullname.\n\n"
76 . "Rappel : il ne faut utiliser cette opération "
77 . "que dans le cas de spams ou de virus !\n";
78 $type = 'spam';
79 $count += count($own);
80 break;
81 }
82
83 // if the mail was classified as Unsure, feed bogo
84 $raw_mail = html_entity_decode($mlist->getPendingMail($mid, 1));
85 // search for the X-Spam-Flag header
86 $end_of_headers = strpos($raw_mail, "\r\n\r\n");
87 if ($end_of_headers === false) { // sometimes headers are separated by \n
88 $end_of_headers = strpos($raw_mail, "\n\n");
89 }
90 $x_spam_flag = '';
91 if (preg_match('/^X-Spam-Flag: ([a-zA-Z]+), tests=bogofilter/m', substr($raw_mail, 0, $end_of_headers + 1), $matches)) {
92 $x_spam_flag = $matches[1];
93 }
94 if ($x_spam_flag == 'Unsure') {
95 $mailer = new PlMailer();
96 $mailer->addTo($type . '@' . $globals->mail->domain);
97 $mailer->setFrom('"' . $fullname . '" <web@' . $globals->mail->domain . '>');
98 $mailer->setTxtBody($type . ' soumis par ' . $fullname . ' via la modération de la liste ' . $list . '@' . $domain);
99 $mailer->addAttachment($raw_mail, 'message/rfc822', $type . '.mail', false);
100 $mailer->send();
101 }
102
103 // send feedback to the mailing list owners
104 if ($mlist->handleRequest($action, $mid, $reason)) {
105 $sent_mails += $count;
106 $texte = "Le message suivant :\n\n"
107 . " Auteur: {$mail['sender']}\n"
108 . " Sujet : « {$mail['subj']} »\n"
109 . " Date : ".strftime("le %d %b %Y à %H:%M:%S", (int)$mail['stamp'])."\n\n"
110 . $append;
111 $mailer = new PlMailer();
112 $mailer->addTo("$list-owner@{$domain}");
113 $mailer->setFrom("$list-bounces@{$domain}");
114 $mailer->addHeader('Reply-To', "$list-owner@{$domain}");
115 $mailer->setSubject($subject);
116 $mailer->setTxtBody($texte);
117 $mailer->send();
118 }
119
120 // release the lock
121 XDB::execute('DELETE FROM email_list_moderate
122 WHERE handler = {?}',
123 $handler);
124 sleep(60 * $count / $globals->lists->max_mail_per_min);
125 }
126
127 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
128 ?>