4 * Update Majordomo mailinglists with data from egroupware
5 * Each mailinglist is filled with users from one egroupware group.
7 * Copyright 2010 Philipp Wagner <mail@philipp-wagner.com>
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 require_once 'lib/Config.php';
23 require_once 'lib/RpcService.php';
24 require_once 'lib/WhoJob.php';
25 require_once 'lib/SubscribeJob.php';
26 require_once 'lib/UnsubscribeJob.php';
29 $config = &Config::getInstance();
30 $config->setConfigFile('config.ini');
33 * Wait max. 120s for a result of $job
39 function wait_for_results(Job &$job)
42 echo '(I) Waiting for response ';
44 $res = $job->checkResults();
46 if ($res['status'] === Job::STATUS_NOT_READY) {
49 } while ($cnt++ < 120 && $res['status'] === Job::STATUS_NOT_READY);
54 function p_mysql_query($query)
56 if (!($res = mysql_query($query))) {
57 echo "Query failed! Query was: $query\n".
58 "MySQL error message was: ".mysql_error()."\n";
63 if (!mysql_connect($config->getValue('egroupware/db_server'),
64 $config->getValue('egroupware/db_user'),
65 $config->getValue('egroupware/db_password'))) {
66 echo 'DB connection failed.';
70 if (!mysql_select_db($config->getValue('egroupware/db_database'))) {
71 echo 'Unable to select DB.';
75 $lists = $config->getValue(('egroupware-lists'));
76 foreach ($lists as $listName => $groupId) {
77 echo "(I) Processing list $listName\n";
78 $q = "SELECT account_lid, n_family, n_given, contact_email
80 LEFT JOIN egw_accounts ON acl_account = account_id
81 LEFT JOIN egw_addressbook USING(account_id)
82 WHERE acl_appname = 'phpgw_group' AND
83 `acl_location` = '-$groupId'";
84 ($res = p_mysql_query($q)) || exit(1);
87 while ($user = mysql_fetch_array($res, MYSQL_ASSOC)) {
88 $users[] = $user['contact_email'];
91 // get existing users on list (3 tries)
95 echo "(I) Getting existing users on the list (try $cnt)\n";
97 $uuid = $wj->go(array('list' => $listName));
98 echo "(I) Executing job with ID $uuid.\n";
99 $res = wait_for_results($wj);
100 if ($res['status'] !== Job::STATUS_SUCCESS) {
102 echo "(E) Getting all users from list $listName failed.\n";
104 $existingUsers = $res['result'];
106 } while ($cnt++ < 4 && !$success);
109 echo "(E) Unable to get existing users from list in 3 tries, giving up.\n";
113 // unsubscribe existing users
114 if (empty($existingUsers)) {
115 echo "(I) No existing users to unsubscribe.\n";
120 echo "(I) Unsubscribing existing users (try $cnt)\n";
121 $uj = new UnsubscribeJob;
124 'users' => $existingUsers
126 $uuid = $uj->go($params);
127 echo "(I) Executing job with ID $uuid.\n";
128 $res = wait_for_results($uj);
129 if ($res['status'] !== Job::STATUS_SUCCESS) {
131 echo "(E) Unsubscribing all users from $listName failed.\n";
134 } while ($cnt++ < 4 && !$success);
137 echo "(E) Unable to unsubscribe users from list in 3 tries, giving up.\n";
142 // subscribe new users
144 echo "(I) No new users to subscribe.\n";
149 echo "(I) Subscribing new users\n";
150 $sj = new SubscribeJob;
155 $uuid = $sj->go($params);
156 echo "(I) Executing job with ID $uuid.\n";
157 $res = wait_for_results($sj);
158 if ($res['status'] !== Job::STATUS_SUCCESS) {
160 echo "(E) Subscribing new users to $listName failed.\n";
163 } while ($cnt++ < 4 && !$success);
166 echo "(E) Houston, we have a problem. All users unsubscribed, ".
167 "but unable to subscribe the new ones. List empty?! ".
168 "Stopping update! Manual intervention required.\n";
175 echo "(I) All lists updated!\n";