Commit | Line | Data |
---|---|---|
ee68ddc1 | 1 | #!/usr/bin/php5 -q |
866f1333 | 2 | <?php |
3 | /*************************************************************************** | |
5e1513f6 | 4 | * Copyright (C) 2003-2011 Polytechnique.org * |
866f1333 | 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 | ||
dc557110 | 23 | /* Number of consecutive month of bounce before deactivating a redirection |
24 | */ | |
25 | $panne_level = 3; | |
26 | ||
5ecaa68e SJ |
27 | require './connect.db.inc.php'; |
28 | require 'Console/Getopt.php'; | |
4f970ab0 SJ |
29 | |
30 | /* | |
31 | * Parse the command-line options. | |
32 | */ | |
33 | $opts = Console_GetOpt::getopt($argv, 'v'); | |
34 | $opt_verbose = false; | |
35 | ||
36 | if (PEAR::isError($opts)) { | |
37 | echo $opts->getMessage(); | |
38 | } else { | |
39 | $opts = $opts[0]; | |
40 | foreach ($opts as $opt) { | |
41 | if ($opt[0] == 'v') { | |
42 | $opt_verbose = true; | |
43 | } | |
44 | } | |
45 | } | |
866f1333 | 46 | |
eaf30d86 | 47 | /* |
dc557110 | 48 | * Check duplicated addresses |
49 | */ | |
337a6acf SJ |
50 | $it = Xdb::iterRow("SELECT s1.email, s2.email, r1.redirect |
51 | FROM email_redirect_account AS r1 | |
52 | INNER JOIN email_redirect_account AS r2 ON (r1.redirect = r2.redirect AND r1.uid != r2.uid) | |
53 | INNER JOIN email_source_account AS s1 ON (s1.uid = r1.uid AND s1.type = 'forlife') | |
54 | INNER JOIN email_source_account AS s2 ON (s2.uid = r2.uid AND s2.type = 'forlife') | |
55 | LEFT JOIN email_watch AS w ON (r1.redirect = w.email) | |
56 | WHERE w.email IS NULL | |
57 | GROUP BY r1.redirect | |
58 | ORDER BY r1.redirect, s1.email"); | |
866f1333 | 59 | |
60 | $insert = array(); | |
61 | $conflits = array(); | |
62 | while (list($alias1, $alias2, $mail) = $it->next()) { | |
63 | $insert[] = "('$mail', 'pending', CURDATE(), NOW())"; | |
64 | $conflits[] = "* $mail sur $alias1 et $alias2"; | |
65 | } | |
66 | ||
67 | if (count($conflits) > 0) { | |
1707967a SJ |
68 | echo "Nouvelles adresses en doublon détectées :\n" . join("\n", $conflits) |
69 | . "\n\nVous pouvez entrer les informations collectées à ce sujet sur la page :\n" | |
70 | . "https://www.polytechnique.org/admin/emails/duplicated"; | |
866f1333 | 71 | |
72 | echo "\n\n"; | |
06f4daf9 | 73 | $sql = "INSERT IGNORE INTO email_watch (email, state, detection, last) |
4f970ab0 | 74 | VALUES " . join(", ", $insert); |
0380bf85 | 75 | XDB::execute($sql); |
76 | if (XDB::errno() != 0) { | |
77 | echo 'Error : ' . XDB::error() . "\n$sql"; | |
866f1333 | 78 | } |
79 | } | |
80 | ||
dc557110 | 81 | /* |
82 | * Check dead emails | |
83 | */ | |
84 | if ($panne_level > 0) { | |
337a6acf SJ |
85 | $res = XDB::fetchAllAssoc("SELECT r.redirect, a.hruid |
86 | FROM email_redirect_account AS r | |
87 | INNER JOIN accounts AS a ON (a.uid = r.uid) | |
88 | WHERE r.broken_level = {?} AND r.flags != 'broken' | |
89 | ORDER BY a.hruid", | |
90 | $panne_level); | |
dc557110 | 91 | |
337a6acf | 92 | if ($res) { |
d20d604f | 93 | echo "Nouvelles adresses en panne detectees :\n"; |
337a6acf SJ |
94 | foreach ($res as $assoc) { |
95 | echo '* ' . $assoc['redirect'] . ' pour ' . $assoc['hruid'] . "\n"; | |
dc557110 | 96 | } |
97 | echo "\n\n"; | |
866f1333 | 98 | |
337a6acf SJ |
99 | Xdb::execute("UPDATE email_redirect_account |
100 | SET flags = 'broken' | |
101 | WHERE broken_level = 3"); | |
dc557110 | 102 | } |
103 | ||
337a6acf SJ |
104 | Xdb::execute('UPDATE email_redirect_account |
105 | SET broken_level = {?} | |
106 | WHERE broken_level > {?}', | |
fe2a133d | 107 | $panne_level, $panne_level); |
dc557110 | 108 | } |
866f1333 | 109 | |
4f970ab0 SJ |
110 | /* |
111 | * Retrieve the users with no active redirection, but still one working | |
112 | * inactive redirection. | |
113 | */ | |
114 | if ($opt_verbose) { | |
337a6acf SJ |
115 | $res = XDB::fetchAllAssoc("SELECT a.hruid, r2.redirect |
116 | FROM accounts AS a | |
117 | LEFT JOIN email_redirect_account AS r1 ON (a.uid = r1.uid AND r1.flags = 'active') | |
118 | INNER JOIN email_redirect_account AS r2 ON (a.uid = r2.uid AND r2.flags = 'inactive' | |
119 | AND r2.type != 'imap' AND r2.type != 'homonym') | |
120 | WHERE r1.uid IS NULL | |
121 | GROUP BY a.uid"); | |
4f970ab0 | 122 | |
337a6acf | 123 | if ($res) { |
4f970ab0 | 124 | echo "Camarades n'ayant plus d'adresses actives, mais ayant une adresse inactive :\n"; |
337a6acf SJ |
125 | foreach ($res as $user) { |
126 | echo '* ' . $user['redirect'] . ' pour ' . $user['hruid'] . "\n"; | |
4f970ab0 | 127 | } |
c952e3ea SJ |
128 | } |
129 | echo "\n"; | |
4f970ab0 SJ |
130 | } |
131 | ||
24e8f53b SJ |
132 | /* |
133 | * Updates imap settings for users with no active redirection. Their emails | |
134 | * must go to imap and bounce. | |
135 | */ | |
136 | XDB::execute("UPDATE email_redirect_account AS r | |
137 | LEFT JOIN email_redirect_account AS a ON (r.uid = a.uid AND a.flags = 'active' AND a.type != 'imap') | |
138 | SET r.action = 'imap_and_bounce' | |
139 | WHERE r.type = 'imap' AND a.redirect IS NULL"); | |
140 | ||
141 | ||
a7de4ef7 | 142 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
866f1333 | 143 | ?> |