3 * StatusNet, the distributed open-source microblogging tool
5 * Settings for Jabber/XMPP integration
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero 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 Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Evan Prodromou <evan@status.net>
25 * @copyright 2008-2009 StatusNet, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://status.net/
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
34 require_once INSTALLDIR.'/lib/connectsettingsaction.php';
35 require_once INSTALLDIR.'/lib/jabber.php';
38 * Settings for Jabber/XMPP integration
42 * @author Evan Prodromou <evan@status.net>
43 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44 * @link http://status.net/
49 class ImsettingsAction extends ConnectSettingsAction
54 * @return string Title of the page
59 return _('IM Settings');
63 * Instructions for use
65 * @return instructions for use
68 function getInstructions()
70 return _('You can send and receive notices through '.
71 'Jabber/GTalk [instant messages](%%doc.im%%). '.
72 'Configure your address and settings below.');
76 * Content area of the page
78 * We make different sections of the form for the different kinds of
79 * functions, and have submit buttons with different names. These
80 * are muxed by handlePost() to see what the user really wants to do.
85 function showContent()
87 if (!common_config('xmpp', 'enabled')) {
88 $this->element('div', array('class' => 'error'),
89 _('IM is not available.'));
93 $user = common_current_user();
94 $this->elementStart('form', array('method' => 'post',
95 'id' => 'form_settings_im',
96 'class' => 'form_settings',
98 common_local_url('imsettings')));
99 $this->elementStart('fieldset', array('id' => 'settings_im_address'));
100 $this->element('legend', null, _('Address'));
101 $this->hidden('token', common_session_token());
104 $this->element('p', 'form_confirmed', $user->jabber);
105 $this->element('p', 'form_note',
106 _('Current confirmed Jabber/GTalk address.'));
107 $this->hidden('jabber', $user->jabber);
108 $this->submit('remove', _('Remove'));
110 $confirm = $this->getConfirmation();
112 $this->element('p', 'form_unconfirmed', $confirm->address);
113 $this->element('p', 'form_note',
114 sprintf(_('Awaiting confirmation on this address. '.
115 'Check your Jabber/GTalk account for a '.
116 'message with further instructions. '.
117 '(Did you add %s to your buddy list?)'),
118 jabber_daemon_address()));
119 $this->hidden('jabber', $confirm->address);
120 $this->submit('cancel', _('Cancel'));
122 $this->elementStart('ul', 'form_data');
123 $this->elementStart('li');
124 $this->input('jabber', _('IM Address'),
125 ($this->arg('jabber')) ? $this->arg('jabber') : null,
126 sprintf(_('Jabber or GTalk address, '.
127 'like "UserName@example.org". '.
128 'First, make sure to add %s to your '.
129 'buddy list in your IM client or on GTalk.'),
130 jabber_daemon_address()));
131 $this->elementEnd('li');
132 $this->elementEnd('ul');
133 $this->submit('add', _('Add'));
136 $this->elementEnd('fieldset');
138 $this->elementStart('fieldset', array('id' => 'settings_im_preferences'));
139 $this->element('legend', null, _('Preferences'));
140 $this->elementStart('ul', 'form_data');
141 $this->elementStart('li');
142 $this->checkbox('jabbernotify',
143 _('Send me notices through Jabber/GTalk.'),
144 $user->jabbernotify);
145 $this->elementEnd('li');
146 $this->elementStart('li');
147 $this->checkbox('updatefrompresence',
148 _('Post a notice when my Jabber/GTalk status changes.'),
149 $user->updatefrompresence);
150 $this->elementEnd('li');
151 $this->elementStart('li');
152 $this->checkbox('jabberreplies',
153 _('Send me replies through Jabber/GTalk '.
154 'from people I\'m not subscribed to.'),
155 $user->jabberreplies);
156 $this->elementEnd('li');
157 $this->elementStart('li');
158 $this->checkbox('jabbermicroid',
159 _('Publish a MicroID for my Jabber/GTalk address.'),
160 $user->jabbermicroid);
161 $this->elementEnd('li');
162 $this->elementEnd('ul');
163 $this->submit('save', _('Save'));
164 $this->elementEnd('fieldset');
165 $this->elementEnd('form');
169 * Get a confirmation code for this user
171 * @return Confirm_address address object for this user
174 function getConfirmation()
176 $user = common_current_user();
178 $confirm = new Confirm_address();
180 $confirm->user_id = $user->id;
181 $confirm->address_type = 'jabber';
183 if ($confirm->find(true)) {
191 * Handle posts to this form
193 * Based on the button that was pressed, muxes out to other functions
194 * to do the actual task requested.
196 * All sub-functions reload the form with a message -- success or failure.
201 function handlePost()
204 $token = $this->trimmed('token');
205 if (!$token || $token != common_session_token()) {
206 $this->showForm(_('There was a problem with your session token. '.
207 'Try again, please.'));
211 if ($this->arg('save')) {
212 $this->savePreferences();
213 } else if ($this->arg('add')) {
215 } else if ($this->arg('cancel')) {
216 $this->cancelConfirmation();
217 } else if ($this->arg('remove')) {
218 $this->removeAddress();
220 $this->showForm(_('Unexpected form submission.'));
225 * Save user's Jabber preferences
227 * These are the checkboxes at the bottom of the page. They're used to
228 * set different settings
233 function savePreferences()
236 $jabbernotify = $this->boolean('jabbernotify');
237 $updatefrompresence = $this->boolean('updatefrompresence');
238 $jabberreplies = $this->boolean('jabberreplies');
239 $jabbermicroid = $this->boolean('jabbermicroid');
241 $user = common_current_user();
243 assert(!is_null($user)); // should already be checked
245 $user->query('BEGIN');
247 $original = clone($user);
249 $user->jabbernotify = $jabbernotify;
250 $user->updatefrompresence = $updatefrompresence;
251 $user->jabberreplies = $jabberreplies;
252 $user->jabbermicroid = $jabbermicroid;
254 $result = $user->update($original);
256 if ($result === false) {
257 common_log_db_error($user, 'UPDATE', __FILE__);
258 $this->serverError(_('Couldn\'t update user.'));
262 $user->query('COMMIT');
264 $this->showForm(_('Preferences saved.'), true);
268 * Sends a confirmation to the address given
270 * Stores a confirmation record and sends out a
271 * Jabber message with the confirmation info.
276 function addAddress()
278 $user = common_current_user();
280 $jabber = $this->trimmed('jabber');
285 $this->showForm(_('No Jabber ID.'));
289 $jabber = jabber_normalize_jid($jabber);
292 $this->showForm(_('Cannot normalize that Jabber ID'));
295 if (!jabber_valid_base_jid($jabber)) {
296 $this->showForm(_('Not a valid Jabber ID'));
298 } else if ($user->jabber == $jabber) {
299 $this->showForm(_('That is already your Jabber ID.'));
301 } else if ($this->jabberExists($jabber)) {
302 $this->showForm(_('Jabber ID already belongs to another user.'));
306 $confirm = new Confirm_address();
308 $confirm->address = $jabber;
309 $confirm->address_type = 'jabber';
310 $confirm->user_id = $user->id;
311 $confirm->code = common_confirmation_code(64);
313 $result = $confirm->insert();
315 if ($result === false) {
316 common_log_db_error($confirm, 'INSERT', __FILE__);
317 $this->serverError(_('Couldn\'t insert confirmation code.'));
321 if (!common_config('queue', 'enabled')) {
322 jabber_confirm_address($confirm->code,
327 $msg = sprintf(_('A confirmation code was sent '.
328 'to the IM address you added. '.
329 'You must approve %s for '.
330 'sending messages to you.'),
331 jabber_daemon_address());
333 $this->showForm($msg, true);
337 * Cancel a confirmation
339 * If a confirmation exists, cancel it.
344 function cancelConfirmation()
346 $jabber = $this->arg('jabber');
348 $confirm = $this->getConfirmation();
351 $this->showForm(_('No pending confirmation to cancel.'));
354 if ($confirm->address != $jabber) {
355 $this->showForm(_('That is the wrong IM address.'));
359 $result = $confirm->delete();
362 common_log_db_error($confirm, 'DELETE', __FILE__);
363 $this->serverError(_('Couldn\'t delete email confirmation.'));
367 $this->showForm(_('Confirmation cancelled.'), true);
373 * If the user has a confirmed address, remove it.
378 function removeAddress()
380 $user = common_current_user();
382 $jabber = $this->arg('jabber');
384 // Maybe an old tab open...?
386 if ($user->jabber != $jabber) {
387 $this->showForm(_('That is not your Jabber ID.'));
391 $user->query('BEGIN');
393 $original = clone($user);
395 $user->jabber = null;
397 $result = $user->updateKeys($original);
400 common_log_db_error($user, 'UPDATE', __FILE__);
401 $this->serverError(_('Couldn\'t update user.'));
404 $user->query('COMMIT');
406 // XXX: unsubscribe to the old address
408 $this->showForm(_('The address was removed.'), true);
412 * Does this Jabber ID exist?
414 * Checks if we already have another user with this address.
416 * @param string $jabber Address to check
418 * @return boolean whether the Jabber ID exists
421 function jabberExists($jabber)
423 $user = common_current_user();
425 $other = User::staticGet('jabber', $jabber);
430 return $other->id != $user->id;