3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008-2010 StatusNet, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('STATUSNET') && !defined('LACONICA')) {
25 * Event handler for pushing new confirmations to Jabber users.
26 * @fixme recommend redoing this on a queue-trigger model
27 * @fixme expiration of old items got dropped in the past, put it back?
29 class XmppConfirmManager extends IoManager
33 * @return mixed XmppConfirmManager, or false if unneeded
35 public static function get()
37 if (common_config('xmpp', 'enabled')) {
38 $site = common_config('site', 'server');
39 return new XmppConfirmManager();
46 * Tell the i/o master we need one instance for each supporting site
47 * being handled in this process.
49 public static function multiSite()
51 return IoManager::INSTANCE_PER_SITE;
54 function __construct()
56 $this->site = common_config('site', 'server');
60 * 10 seconds? Really? That seems a bit frequent.
62 function pollInterval()
69 * @return boolean true if we found something
74 $confirm = $this->next_confirm();
76 $this->handle_confirm($confirm);
83 protected function handle_confirm($confirm)
85 require_once INSTALLDIR . '/lib/jabber.php';
87 common_log(LOG_INFO, 'Sending confirmation for ' . $confirm->address);
88 $user = User::staticGet($confirm->user_id);
90 common_log(LOG_WARNING, 'Confirmation for unknown user ' . $confirm->user_id);
93 $success = jabber_confirm_address($confirm->code,
97 common_log(LOG_ERR, 'Confirmation failed for ' . $confirm->address);
98 # Just let the claim age out; hopefully things work then
101 common_log(LOG_INFO, 'Confirmation sent for ' . $confirm->address);
102 # Mark confirmation sent; need a dupe so we don't have the WHERE clause
103 $dupe = Confirm_address::staticGet('code', $confirm->code);
105 common_log(LOG_WARNING, 'Could not refetch confirm', __FILE__);
108 $orig = clone($dupe);
109 $dupe->sent = $dupe->claimed;
110 $result = $dupe->update($orig);
112 common_log_db_error($dupe, 'UPDATE', __FILE__);
113 # Just let the claim age out; hopefully things work then
120 protected function next_confirm()
122 $confirm = new Confirm_address();
123 $confirm->whereAdd('claimed IS null');
124 $confirm->whereAdd('sent IS null');
125 # XXX: eventually we could do other confirmations in the queue, too
126 $confirm->address_type = 'jabber';
127 $confirm->orderBy('modified DESC');
129 if ($confirm->find(true)) {
130 common_log(LOG_INFO, 'Claiming confirmation for ' . $confirm->address);
131 # working around some weird DB_DataObject behaviour
132 $confirm->whereAdd(''); # clears where stuff
133 $original = clone($confirm);
134 $confirm->claimed = common_sql_now();
135 $result = $confirm->update($original);
137 common_log(LOG_INFO, 'Succeeded in claim! '. $result);
140 common_log(LOG_INFO, 'Failed in claim!');
147 protected function clear_old_confirm_claims()
149 $confirm = new Confirm();
150 $confirm->claimed = null;
151 $confirm->whereAdd('now() - claimed > '.CLAIM_TIMEOUT);
152 $confirm->update(DB_DATAOBJECT_WHEREADD_ONLY);
158 * Make sure we're on the right site configuration
160 protected function switchSite()
162 if ($this->site != common_config('site', 'server')) {
163 common_log(LOG_DEBUG, __METHOD__ . ": switching to site $this->site");
164 $this->stats('switch');
165 StatusNet::init($this->site);