| 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 | /* Number of consecutive month of bounce before deactivating a redirection |
| 24 | */ |
| 25 | $panne_level = 3; |
| 26 | |
| 27 | require './connect.db.inc.php'; |
| 28 | require 'Console/Getopt.php'; |
| 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 | } |
| 46 | |
| 47 | /* |
| 48 | * Check duplicated addresses |
| 49 | */ |
| 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"); |
| 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) { |
| 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"; |
| 71 | |
| 72 | echo "\n\n"; |
| 73 | $sql = "INSERT IGNORE INTO email_watch (email, state, detection, last) |
| 74 | VALUES " . join(", ", $insert); |
| 75 | XDB::execute($sql); |
| 76 | if (XDB::errno() != 0) { |
| 77 | echo 'Error : ' . XDB::error() . "\n$sql"; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * Check dead emails |
| 83 | */ |
| 84 | if ($panne_level > 0) { |
| 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); |
| 91 | |
| 92 | if ($res) { |
| 93 | echo "Nouvelles adresses en panne detectees :\n"; |
| 94 | foreach ($res as $assoc) { |
| 95 | echo '* ' . $assoc['redirect'] . ' pour ' . $assoc['hruid'] . "\n"; |
| 96 | } |
| 97 | echo "\n\n"; |
| 98 | |
| 99 | Xdb::execute("UPDATE email_redirect_account |
| 100 | SET flags = 'broken' |
| 101 | WHERE broken_level = 3"); |
| 102 | } |
| 103 | |
| 104 | Xdb::execute('UPDATE email_redirect_account |
| 105 | SET broken_level = {?} |
| 106 | WHERE broken_level > {?}', |
| 107 | $panne_level, $panne_level); |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * Retrieve the users with no active redirection, but still one working |
| 112 | * inactive redirection. |
| 113 | */ |
| 114 | if ($opt_verbose) { |
| 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"); |
| 122 | |
| 123 | if ($res) { |
| 124 | echo "Camarades n'ayant plus d'adresses actives, mais ayant une adresse inactive :\n"; |
| 125 | foreach ($res as $user) { |
| 126 | echo '* ' . $user['redirect'] . ' pour ' . $user['hruid'] . "\n"; |
| 127 | } |
| 128 | } |
| 129 | echo "\n"; |
| 130 | } |
| 131 | |
| 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 | |
| 142 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 143 | ?> |