+/* Checks that all nicknames have been synchronized to GoogleApps. Creates the
+ missing ones. */
+$res = XDB::iterator(
+ "SELECT g.l_userid AS id, s1.email AS username, s2.email AS nickname
+ FROM gapps_accounts AS g
+ INNER JOIN email_source_account AS s1 ON (s1.uid = g.l_userid AND s1.type = 'forlife')
+ INNER JOIN email_source_account AS s2 ON (s2.uid = g.l_userid AND s2.type = 'alias')
+ LEFT JOIN gapps_nicknames AS n ON (n.l_userid = g.l_userid AND n.g_nickname = s2.email)
+ WHERE g.g_status = 'active' AND n.g_nickname IS NULL AND g.l_userid IS NOT NULL");
+while ($nickname = $res->next()) {
+ // Checks that the requested nickname doesn't look like a regular forlife;
+ // we might run in troubler later if we don't keep the two repos. If we need
+ // to add a forlife-looking nickname at some point, we'll do it manually.
+ if (!preg_match('/^[-a-z]+\.[-a-z]+\.\d{4}$/', $nickname['nickname'])) {
+ $pending_tasks = XDB::fetchOneCell(
+ "SELECT COUNT(*)
+ FROM gapps_queue
+ WHERE q_recipient_id = {?} AND p_status = 'idle' AND j_type = 'n_create' AND j_parameters = {?}",
+ $nickname['id'], json_encode($nickname));
+ if ($pending_tasks == 0) {
+ XDB::execute(
+ "INSERT INTO gapps_queue
+ SET q_recipient_id = {?}, p_entry_date = NOW(), p_notbefore_date = NOW(),
+ p_priority = 'offline', j_type = 'n_create', j_parameters = {?}",
+ $nickname['id'], json_encode($nickname));
+ }
+ }
+}
+
+/* Checks that all nicknames in GoogleApps are also aliases on plat/al side.
+ Deletes the invalid ones. */
+$res = XDB::iterator(
+ "SELECT g.l_userid AS id, g.g_nickname AS nickname
+ FROM gapps_nicknames AS g
+ LEFT JOIN email_source_account AS s ON (s.uid = g.l_userid AND s.type = 'alias' AND s.email = g.g_nickname)
+ WHERE g.l_userid IS NOT NULL AND s.email IS NULL");
+while ($nickname = $res->next()) {
+ $pending_tasks = XDB::fetchOneCell(
+ "SELECT COUNT(*)
+ FROM gapps_queue
+ WHERE q_recipient_id = {?} AND p_status = 'idle' AND j_type = 'n_delete' AND j_parameters = {?}",
+ $nickname['id'], json_encode($nickname));
+ if ($pending_tasks == 0) {
+ XDB::execute(
+ "INSERT INTO gapps_queue
+ SET q_recipient_id = {?}, p_entry_date = NOW(), p_notbefore_date = NOW(),
+ p_priority = 'offline', j_type = 'n_delete', j_parameters = {?}",
+ $nickname['id'], json_encode($nickname));
+ }
+}
+
+/* Retrieves successful job queues for post-queue processing. */
+$res = XDB::iterator(
+ "SELECT q_id, q_recipient_id, j_type, j_parameters
+ FROM gapps_queue
+ WHERE p_status = 'success' AND q_recipient_id IS NOT NULL");
+while ($job = $res->next()) {
+ if ($job['j_type'] == 'u_create') {
+ post_queue_u_create($job);
+ } else if ($job['j_type'] == 'u_update') {
+ post_queue_u_update($job);
+ }
+}
+
+/* Removes successful jobs, and old failed jobs. */
+XDB::execute(
+ "DELETE FROM gapps_queue
+ WHERE p_status = 'success' OR
+ (p_status = 'hardfail' AND p_end_date < DATE_SUB(NOW(), INTERVAL 15 DAY))");
+
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8: