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 // TRANS: Do not translate "example.org". It is one of the domain names reserved for use in examples by
141 // TRANS: http://www.rfc-editor.org/rfc/rfc2606.txt. Any other domain may be owned by a legitimate
142 // TRANS: person or organization.
143 sprintf(_('Jabber or GTalk address, '.
144 'like "UserName@example.org". '.
145 'First, make sure to add %s to your '.
146 'buddy list in your IM client or on GTalk.'),
147 jabber_daemon_address()));
148 $this->elementEnd('li');
149 $this->elementEnd('ul');
150 // TRANS: Button label for adding an IM address in IM settings form.
151 $this->submit('add', _m('BUTTON','Add'));
154 $this->elementEnd('fieldset');
156 $this->elementStart('fieldset', array('id' => 'settings_im_preferences'));
157 // TRANS: Form legend for IM preferences form.
158 $this->element('legend', null, _('IM preferences'));
159 $this->elementStart('ul', 'form_data');
160 $this->elementStart('li');
161 $this->checkbox('jabbernotify',
162 // TRANS: Checkbox label in IM preferences form.
163 _('Send me notices through Jabber/GTalk.'),
164 $user->jabbernotify);
165 $this->elementEnd('li');
166 $this->elementStart('li');
167 $this->checkbox('updatefrompresence',
168 // TRANS: Checkbox label in IM preferences form.
169 _('Post a notice when my Jabber/GTalk status changes.'),
170 $user->updatefrompresence);
171 $this->elementEnd('li');
172 $this->elementStart('li');
173 $this->checkbox('jabberreplies',
174 // TRANS: Checkbox label in IM preferences form.
175 _('Send me replies through Jabber/GTalk '.
176 'from people I\'m not subscribed to.'),
177 $user->jabberreplies);
178 $this->elementEnd('li');
179 $this->elementStart('li');
180 $this->checkbox('jabbermicroid',
181 // TRANS: Checkbox label in IM preferences form.
182 _('Publish a MicroID for my Jabber/GTalk address.'),
183 $user->jabbermicroid);
184 $this->elementEnd('li');
185 $this->elementEnd('ul');
186 // TRANS: Button label to save IM preferences.
187 $this->submit('save', _m('BUTTON','Save'));
188 $this->elementEnd('fieldset');
189 $this->elementEnd('form');
193 * Get a confirmation code for this user
195 * @return Confirm_address address object for this user
198 function getConfirmation()
200 $user = common_current_user();
202 $confirm = new Confirm_address();
204 $confirm->user_id = $user->id;
205 $confirm->address_type = 'jabber';
207 if ($confirm->find(true)) {
215 * Handle posts to this form
217 * Based on the button that was pressed, muxes out to other functions
218 * to do the actual task requested.
220 * All sub-functions reload the form with a message -- success or failure.
225 function handlePost()
228 $token = $this->trimmed('token');
229 if (!$token || $token != common_session_token()) {
230 $this->showForm(_('There was a problem with your session token. '.
231 'Try again, please.'));
235 if ($this->arg('save')) {
236 $this->savePreferences();
237 } else if ($this->arg('add')) {
239 } else if ($this->arg('cancel')) {
240 $this->cancelConfirmation();
241 } else if ($this->arg('remove')) {
242 $this->removeAddress();
244 // TRANS: Message given submitting a form with an unknown action in IM settings.
245 $this->showForm(_('Unexpected form submission.'));
250 * Save user's Jabber preferences
252 * These are the checkboxes at the bottom of the page. They're used to
253 * set different settings
258 function savePreferences()
260 $jabbernotify = $this->boolean('jabbernotify');
261 $updatefrompresence = $this->boolean('updatefrompresence');
262 $jabberreplies = $this->boolean('jabberreplies');
263 $jabbermicroid = $this->boolean('jabbermicroid');
265 $user = common_current_user();
267 assert(!is_null($user)); // should already be checked
269 $user->query('BEGIN');
271 $original = clone($user);
273 $user->jabbernotify = $jabbernotify;
274 $user->updatefrompresence = $updatefrompresence;
275 $user->jabberreplies = $jabberreplies;
276 $user->jabbermicroid = $jabbermicroid;
278 $result = $user->update($original);
280 if ($result === false) {
281 common_log_db_error($user, 'UPDATE', __FILE__);
282 // TRANS: Server error thrown on database error updating IM preferences.
283 $this->serverError(_('Couldn\'t update user.'));
287 $user->query('COMMIT');
289 // TRANS: Confirmation message for successful IM preferences save.
290 $this->showForm(_('Preferences saved.'), true);
294 * Sends a confirmation to the address given
296 * Stores a confirmation record and sends out a
297 * Jabber message with the confirmation info.
302 function addAddress()
304 $user = common_current_user();
306 $jabber = $this->trimmed('jabber');
311 // TRANS: Message given saving IM address without having provided one.
312 $this->showForm(_('No Jabber ID.'));
316 $jabber = jabber_normalize_jid($jabber);
319 // TRANS: Message given saving IM address that cannot be normalised.
320 $this->showForm(_('Cannot normalize that Jabber ID'));
323 if (!jabber_valid_base_jid($jabber, common_config('email', 'domain_check'))) {
324 // TRANS: Message given saving IM address that not valid.
325 $this->showForm(_('Not a valid Jabber ID'));
327 } else if ($user->jabber == $jabber) {
328 // TRANS: Message given saving IM address that is already set.
329 $this->showForm(_('That is already your Jabber ID.'));
331 } else if ($this->jabberExists($jabber)) {
332 // TRANS: Message given saving IM address that is already set for another user.
333 $this->showForm(_('Jabber ID already belongs to another user.'));
337 $confirm = new Confirm_address();
339 $confirm->address = $jabber;
340 $confirm->address_type = 'jabber';
341 $confirm->user_id = $user->id;
342 $confirm->code = common_confirmation_code(64);
343 $confirm->sent = common_sql_now();
344 $confirm->claimed = common_sql_now();
346 $result = $confirm->insert();
348 if ($result === false) {
349 common_log_db_error($confirm, 'INSERT', __FILE__);
350 // TRANS: Server error thrown on database error adding IM confirmation code.
351 $this->serverError(_('Couldn\'t insert confirmation code.'));
355 jabber_confirm_address($confirm->code,
359 // TRANS: Message given saving valid IM address that is to be confirmed.
360 // TRANS: %s is the IM address set for the site.
361 $msg = sprintf(_('A confirmation code was sent '.
362 'to the IM address you added. '.
363 'You must approve %s for '.
364 'sending messages to you.'),
365 jabber_daemon_address());
367 $this->showForm($msg, true);
371 * Cancel a confirmation
373 * If a confirmation exists, cancel it.
378 function cancelConfirmation()
380 $jabber = $this->arg('jabber');
382 $confirm = $this->getConfirmation();
385 // TRANS: Message given canceling IM address confirmation that is not pending.
386 $this->showForm(_('No pending confirmation to cancel.'));
389 if ($confirm->address != $jabber) {
390 // TRANS: Message given canceling IM address confirmation for the wrong IM address.
391 $this->showForm(_('That is the wrong IM address.'));
395 $result = $confirm->delete();
398 common_log_db_error($confirm, 'DELETE', __FILE__);
399 // TRANS: Server error thrown on database error canceling IM address confirmation.
400 $this->serverError(_('Couldn\'t delete IM confirmation.'));
404 // TRANS: Message given after successfully canceling IM address confirmation.
405 $this->showForm(_('IM confirmation cancelled.'), true);
411 * If the user has a confirmed address, remove it.
416 function removeAddress()
418 $user = common_current_user();
420 $jabber = $this->arg('jabber');
422 // Maybe an old tab open...?
424 if ($user->jabber != $jabber) {
425 // TRANS: Message given trying to remove an IM address that is not
426 // TRANS: registered for the active user.
427 $this->showForm(_('That is not your Jabber ID.'));
431 $user->query('BEGIN');
433 $original = clone($user);
435 $user->jabber = null;
437 $result = $user->updateKeys($original);
440 common_log_db_error($user, 'UPDATE', __FILE__);
441 // TRANS: Server error thrown on database error removing a registered IM address.
442 $this->serverError(_('Couldn\'t update user.'));
445 $user->query('COMMIT');
447 // XXX: unsubscribe to the old address
449 // TRANS: Message given after successfully removing a registered IM address.
450 $this->showForm(_('The IM address was removed.'), true);
454 * Does this Jabber ID exist?
456 * Checks if we already have another user with this address.
458 * @param string $jabber Address to check
460 * @return boolean whether the Jabber ID exists
463 function jabberExists($jabber)
465 $user = common_current_user();
467 $other = User::staticGet('jabber', $jabber);
472 return $other->id != $user->id;