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 // TRANS: Title for instance messaging settings.
60 return _('IM settings');
64 * Instructions for use
66 * @return instructions for use
69 function getInstructions()
71 // TRANS: Instant messaging settings page instructions.
72 // TRANS: [instant messages] is link text, "(%%doc.im%%)" is the link.
73 // TRANS: the order and formatting of link text and link should remain unchanged.
74 return _('You can send and receive notices through '.
75 'Jabber/GTalk [instant messages](%%doc.im%%). '.
76 'Configure your address and settings below.');
80 * Content area of the page
82 * We make different sections of the form for the different kinds of
83 * functions, and have submit buttons with different names. These
84 * are muxed by handlePost() to see what the user really wants to do.
89 function showContent()
91 if (!common_config('xmpp', 'enabled')) {
92 $this->element('div', array('class' => 'error'),
93 // TRANS: Message given in the IM settings if XMPP is not enabled on the site.
94 _('IM is not available.'));
98 $user = common_current_user();
99 $this->elementStart('form', array('method' => 'post',
100 'id' => 'form_settings_im',
101 'class' => 'form_settings',
103 common_local_url('imsettings')));
104 $this->elementStart('fieldset', array('id' => 'settings_im_address'));
105 // TRANS: Form legend for IM settings form.
106 $this->element('legend', null, _('IM address'));
107 $this->hidden('token', common_session_token());
110 $this->element('p', 'form_confirmed', $user->jabber);
111 // TRANS: Form note in IM settings form.
112 $this->element('p', 'form_note',
113 _('Current confirmed Jabber/GTalk address.'));
114 $this->hidden('jabber', $user->jabber);
115 // TRANS: Button label to remove a confirmed IM address.
116 $this->submit('remove', _m('BUTTON','Remove'));
118 $confirm = $this->getConfirmation();
120 $this->element('p', 'form_unconfirmed', $confirm->address);
121 $this->element('p', 'form_note',
122 // TRANS: Form note in IM settings form.
123 // TRANS: %s is the IM address set for the site.
124 sprintf(_('Awaiting confirmation on this address. '.
125 'Check your Jabber/GTalk account for a '.
126 'message with further instructions. '.
127 '(Did you add %s to your buddy list?)'),
128 jabber_daemon_address()));
129 $this->hidden('jabber', $confirm->address);
130 // TRANS: Button label to cancel an IM address confirmation procedure.
131 $this->submit('cancel', _m('BUTTON','Cancel'));
133 $this->elementStart('ul', 'form_data');
134 $this->elementStart('li');
135 // TRANS: Field label for IM address input in IM settings form.
136 $this->input('jabber', _('IM address'),
137 ($this->arg('jabber')) ? $this->arg('jabber') : null,
138 // TRANS: IM address input field instructions in IM settings form.
139 // TRANS: %s is the IM address set for the site.
140 sprintf(_('Jabber or GTalk address, '.
141 'like "UserName@example.org". '.
142 'First, make sure to add %s to your '.
143 'buddy list in your IM client or on GTalk.'),
144 jabber_daemon_address()));
145 $this->elementEnd('li');
146 $this->elementEnd('ul');
147 // TRANS: Button label for adding an IM address in IM settings form.
148 $this->submit('add', _m('BUTTON','Add'));
151 $this->elementEnd('fieldset');
153 $this->elementStart('fieldset', array('id' => 'settings_im_preferences'));
154 // TRANS: Form legend for IM preferences form.
155 $this->element('legend', null, _('IM preferences'));
156 $this->elementStart('ul', 'form_data');
157 $this->elementStart('li');
158 $this->checkbox('jabbernotify',
159 // TRANS: Checkbox label in IM preferences form.
160 _('Send me notices through Jabber/GTalk.'),
161 $user->jabbernotify);
162 $this->elementEnd('li');
163 $this->elementStart('li');
164 $this->checkbox('updatefrompresence',
165 // TRANS: Checkbox label in IM preferences form.
166 _('Post a notice when my Jabber/GTalk status changes.'),
167 $user->updatefrompresence);
168 $this->elementEnd('li');
169 $this->elementStart('li');
170 $this->checkbox('jabberreplies',
171 // TRANS: Checkbox label in IM preferences form.
172 _('Send me replies through Jabber/GTalk '.
173 'from people I\'m not subscribed to.'),
174 $user->jabberreplies);
175 $this->elementEnd('li');
176 $this->elementStart('li');
177 $this->checkbox('jabbermicroid',
178 // TRANS: Checkbox label in IM preferences form.
179 _('Publish a MicroID for my Jabber/GTalk address.'),
180 $user->jabbermicroid);
181 $this->elementEnd('li');
182 $this->elementEnd('ul');
183 // TRANS: Button label to save IM preferences.
184 $this->submit('save', _m('BUTTON','Save'));
185 $this->elementEnd('fieldset');
186 $this->elementEnd('form');
190 * Get a confirmation code for this user
192 * @return Confirm_address address object for this user
195 function getConfirmation()
197 $user = common_current_user();
199 $confirm = new Confirm_address();
201 $confirm->user_id = $user->id;
202 $confirm->address_type = 'jabber';
204 if ($confirm->find(true)) {
212 * Handle posts to this form
214 * Based on the button that was pressed, muxes out to other functions
215 * to do the actual task requested.
217 * All sub-functions reload the form with a message -- success or failure.
222 function handlePost()
225 $token = $this->trimmed('token');
226 if (!$token || $token != common_session_token()) {
227 $this->showForm(_('There was a problem with your session token. '.
228 'Try again, please.'));
232 if ($this->arg('save')) {
233 $this->savePreferences();
234 } else if ($this->arg('add')) {
236 } else if ($this->arg('cancel')) {
237 $this->cancelConfirmation();
238 } else if ($this->arg('remove')) {
239 $this->removeAddress();
241 // TRANS: Message given submitting a form with an unknown action in IM settings.
242 $this->showForm(_('Unexpected form submission.'));
247 * Save user's Jabber preferences
249 * These are the checkboxes at the bottom of the page. They're used to
250 * set different settings
255 function savePreferences()
257 $jabbernotify = $this->boolean('jabbernotify');
258 $updatefrompresence = $this->boolean('updatefrompresence');
259 $jabberreplies = $this->boolean('jabberreplies');
260 $jabbermicroid = $this->boolean('jabbermicroid');
262 $user = common_current_user();
264 assert(!is_null($user)); // should already be checked
266 $user->query('BEGIN');
268 $original = clone($user);
270 $user->jabbernotify = $jabbernotify;
271 $user->updatefrompresence = $updatefrompresence;
272 $user->jabberreplies = $jabberreplies;
273 $user->jabbermicroid = $jabbermicroid;
275 $result = $user->update($original);
277 if ($result === false) {
278 common_log_db_error($user, 'UPDATE', __FILE__);
279 // TRANS: Server error thrown on database error updating IM preferences.
280 $this->serverError(_('Couldn\'t update user.'));
284 $user->query('COMMIT');
286 // TRANS: Confirmation message for successful IM preferences save.
287 $this->showForm(_('Preferences saved.'), true);
291 * Sends a confirmation to the address given
293 * Stores a confirmation record and sends out a
294 * Jabber message with the confirmation info.
299 function addAddress()
301 $user = common_current_user();
303 $jabber = $this->trimmed('jabber');
308 // TRANS: Message given saving IM address without having provided one.
309 $this->showForm(_('No Jabber ID.'));
313 $jabber = jabber_normalize_jid($jabber);
316 // TRANS: Message given saving IM address that cannot be normalised.
317 $this->showForm(_('Cannot normalize that Jabber ID'));
320 if (!jabber_valid_base_jid($jabber, common_config('email', 'domain_check'))) {
321 // TRANS: Message given saving IM address that not valid.
322 $this->showForm(_('Not a valid Jabber ID'));
324 } else if ($user->jabber == $jabber) {
325 // TRANS: Message given saving IM address that is already set.
326 $this->showForm(_('That is already your Jabber ID.'));
328 } else if ($this->jabberExists($jabber)) {
329 // TRANS: Message given saving IM address that is already set for another user.
330 $this->showForm(_('Jabber ID already belongs to another user.'));
334 $confirm = new Confirm_address();
336 $confirm->address = $jabber;
337 $confirm->address_type = 'jabber';
338 $confirm->user_id = $user->id;
339 $confirm->code = common_confirmation_code(64);
340 $confirm->sent = common_sql_now();
341 $confirm->claimed = common_sql_now();
343 $result = $confirm->insert();
345 if ($result === false) {
346 common_log_db_error($confirm, 'INSERT', __FILE__);
347 // TRANS: Server error thrown on database error adding IM confirmation code.
348 $this->serverError(_('Couldn\'t insert confirmation code.'));
352 jabber_confirm_address($confirm->code,
356 // TRANS: Message given saving valid IM address that is to be confirmed.
357 // TRANS: %s is the IM address set for the site.
358 $msg = sprintf(_('A confirmation code was sent '.
359 'to the IM address you added. '.
360 'You must approve %s for '.
361 'sending messages to you.'),
362 jabber_daemon_address());
364 $this->showForm($msg, true);
368 * Cancel a confirmation
370 * If a confirmation exists, cancel it.
375 function cancelConfirmation()
377 $jabber = $this->arg('jabber');
379 $confirm = $this->getConfirmation();
382 // TRANS: Message given canceling IM address confirmation that is not pending.
383 $this->showForm(_('No pending confirmation to cancel.'));
386 if ($confirm->address != $jabber) {
387 // TRANS: Message given canceling IM address confirmation for the wrong IM address.
388 $this->showForm(_('That is the wrong IM address.'));
392 $result = $confirm->delete();
395 common_log_db_error($confirm, 'DELETE', __FILE__);
396 // TRANS: Server error thrown on database error canceling IM address confirmation.
397 $this->serverError(_('Couldn\'t delete IM confirmation.'));
401 // TRANS: Message given after successfully canceling IM address confirmation.
402 $this->showForm(_('IM confirmation cancelled.'), true);
408 * If the user has a confirmed address, remove it.
413 function removeAddress()
415 $user = common_current_user();
417 $jabber = $this->arg('jabber');
419 // Maybe an old tab open...?
421 if ($user->jabber != $jabber) {
422 // TRANS: Message given trying to remove an IM address that is not
423 // TRANS: registered for the active user.
424 $this->showForm(_('That is not your Jabber ID.'));
428 $user->query('BEGIN');
430 $original = clone($user);
432 $user->jabber = null;
434 $result = $user->updateKeys($original);
437 common_log_db_error($user, 'UPDATE', __FILE__);
438 // TRANS: Server error thrown on database error removing a registered IM address.
439 $this->serverError(_('Couldn\'t update user.'));
442 $user->query('COMMIT');
444 // XXX: unsubscribe to the old address
446 // TRANS: Message given after successfully removing a registered IM address.
447 $this->showForm(_('The IM address was removed.'), true);
451 * Does this Jabber ID exist?
453 * Checks if we already have another user with this address.
455 * @param string $jabber Address to check
457 * @return boolean whether the Jabber ID exists
460 function jabberExists($jabber)
462 $user = common_current_user();
464 $other = User::staticGet('jabber', $jabber);
469 return $other->id != $user->id;