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/
48 class ImsettingsAction extends ConnectSettingsAction
53 * @return string Title of the page
57 // TRANS: Title for instance messaging settings.
58 return _('IM settings');
62 * Instructions for use
64 * @return instructions for use
66 function getInstructions()
68 // TRANS: Instant messaging settings page instructions.
69 // TRANS: [instant messages] is link text, "(%%doc.im%%)" is the link.
70 // TRANS: the order and formatting of link text and link should remain unchanged.
71 return _('You can send and receive notices through '.
72 'Jabber/GTalk [instant messages](%%doc.im%%). '.
73 'Configure your address and settings below.');
77 * Content area of the page
79 * We make different sections of the form for the different kinds of
80 * functions, and have submit buttons with different names. These
81 * 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 // TRANS: Message given in the IM settings if XMPP is not enabled on the site.
90 _('IM is not available.'));
94 $user = common_current_user();
95 $this->elementStart('form', array('method' => 'post',
96 'id' => 'form_settings_im',
97 'class' => 'form_settings',
99 common_local_url('imsettings')));
100 $this->elementStart('fieldset', array('id' => 'settings_im_address'));
101 // TRANS: Form legend for IM settings form.
102 $this->element('legend', null, _('IM address'));
103 $this->hidden('token', common_session_token());
106 $this->element('p', 'form_confirmed', $user->jabber);
107 // TRANS: Form note in IM settings form.
108 $this->element('p', 'form_note',
109 _('Current confirmed Jabber/GTalk address.'));
110 $this->hidden('jabber', $user->jabber);
111 // TRANS: Button label to remove a confirmed IM address.
112 $this->submit('remove', _m('BUTTON','Remove'));
114 $confirm = $this->getConfirmation();
116 $this->element('p', 'form_unconfirmed', $confirm->address);
117 $this->element('p', 'form_note',
118 // TRANS: Form note in IM settings form.
119 // TRANS: %s is the IM address set for the site.
120 sprintf(_('Awaiting confirmation on this address. '.
121 'Check your Jabber/GTalk account for a '.
122 'message with further instructions. '.
123 '(Did you add %s to your buddy list?)'),
124 jabber_daemon_address()));
125 $this->hidden('jabber', $confirm->address);
126 // TRANS: Button label to cancel an IM address confirmation procedure.
127 $this->submit('cancel', _m('BUTTON','Cancel'));
129 $this->elementStart('ul', 'form_data');
130 $this->elementStart('li');
131 // TRANS: Field label for IM address input in IM settings form.
132 $this->input('jabber', _('IM address'),
133 ($this->arg('jabber')) ? $this->arg('jabber') : null,
134 // TRANS: IM address input field instructions in IM settings form.
135 // TRANS: %s is the IM address set for the site.
136 // TRANS: Do not translate "example.org". It is one of the domain names reserved for use in examples by
137 // TRANS: http://www.rfc-editor.org/rfc/rfc2606.txt. Any other domain may be owned by a legitimate
138 // TRANS: person or organization.
139 sprintf(_('Jabber or GTalk address, '.
140 'like "UserName@example.org". '.
141 'First, make sure to add %s to your '.
142 'buddy list in your IM client or on GTalk.'),
143 jabber_daemon_address()));
144 $this->elementEnd('li');
145 $this->elementEnd('ul');
146 // TRANS: Button label for adding an IM address in IM settings form.
147 $this->submit('add', _m('BUTTON','Add'));
150 $this->elementEnd('fieldset');
152 $this->elementStart('fieldset', array('id' => 'settings_im_preferences'));
153 // TRANS: Form legend for IM preferences form.
154 $this->element('legend', null, _('IM preferences'));
155 $this->elementStart('ul', 'form_data');
156 $this->elementStart('li');
157 $this->checkbox('jabbernotify',
158 // TRANS: Checkbox label in IM preferences form.
159 _('Send me notices through Jabber/GTalk.'),
160 $user->jabbernotify);
161 $this->elementEnd('li');
162 $this->elementStart('li');
163 $this->checkbox('updatefrompresence',
164 // TRANS: Checkbox label in IM preferences form.
165 _('Post a notice when my Jabber/GTalk status changes.'),
166 $user->updatefrompresence);
167 $this->elementEnd('li');
168 $this->elementStart('li');
169 $this->checkbox('jabberreplies',
170 // TRANS: Checkbox label in IM preferences form.
171 _('Send me replies through Jabber/GTalk '.
172 'from people I\'m not subscribed to.'),
173 $user->jabberreplies);
174 $this->elementEnd('li');
175 $this->elementStart('li');
176 $this->checkbox('jabbermicroid',
177 // TRANS: Checkbox label in IM preferences form.
178 _('Publish a MicroID for my Jabber/GTalk address.'),
179 $user->jabbermicroid);
180 $this->elementEnd('li');
181 $this->elementEnd('ul');
182 // TRANS: Button label to save IM preferences.
183 $this->submit('save', _m('BUTTON','Save'));
184 $this->elementEnd('fieldset');
185 $this->elementEnd('form');
189 * Get a confirmation code for this user
191 * @return Confirm_address address object for this user
193 function getConfirmation()
195 $user = common_current_user();
197 $confirm = new Confirm_address();
199 $confirm->user_id = $user->id;
200 $confirm->address_type = 'jabber';
202 if ($confirm->find(true)) {
210 * Handle posts to this form
212 * Based on the button that was pressed, muxes out to other functions
213 * to do the actual task requested.
215 * All sub-functions reload the form with a message -- success or failure.
219 function handlePost()
222 $token = $this->trimmed('token');
223 if (!$token || $token != common_session_token()) {
224 $this->showForm(_('There was a problem with your session token. '.
225 'Try again, please.'));
229 if ($this->arg('save')) {
230 $this->savePreferences();
231 } else if ($this->arg('add')) {
233 } else if ($this->arg('cancel')) {
234 $this->cancelConfirmation();
235 } else if ($this->arg('remove')) {
236 $this->removeAddress();
238 // TRANS: Message given submitting a form with an unknown action in IM settings.
239 $this->showForm(_('Unexpected form submission.'));
244 * Save user's Jabber preferences
246 * These are the checkboxes at the bottom of the page. They're used to
247 * set different settings
251 function savePreferences()
253 $jabbernotify = $this->boolean('jabbernotify');
254 $updatefrompresence = $this->boolean('updatefrompresence');
255 $jabberreplies = $this->boolean('jabberreplies');
256 $jabbermicroid = $this->boolean('jabbermicroid');
258 $user = common_current_user();
260 assert(!is_null($user)); // should already be checked
262 $user->query('BEGIN');
264 $original = clone($user);
266 $user->jabbernotify = $jabbernotify;
267 $user->updatefrompresence = $updatefrompresence;
268 $user->jabberreplies = $jabberreplies;
269 $user->jabbermicroid = $jabbermicroid;
271 $result = $user->update($original);
273 if ($result === false) {
274 common_log_db_error($user, 'UPDATE', __FILE__);
275 // TRANS: Server error thrown on database error updating IM preferences.
276 $this->serverError(_('Could not update user.'));
280 $user->query('COMMIT');
282 // TRANS: Confirmation message for successful IM preferences save.
283 $this->showForm(_('Preferences saved.'), true);
287 * Sends a confirmation to the address given
289 * Stores a confirmation record and sends out a
290 * Jabber message with the confirmation info.
294 function addAddress()
296 $user = common_current_user();
298 $jabber = $this->trimmed('jabber');
303 // TRANS: Message given saving IM address without having provided one.
304 $this->showForm(_('No Jabber ID.'));
308 $jabber = jabber_normalize_jid($jabber);
311 // TRANS: Message given saving IM address that cannot be normalised.
312 $this->showForm(_('Cannot normalize that Jabber ID'));
315 if (!jabber_valid_base_jid($jabber, common_config('email', 'domain_check'))) {
316 // TRANS: Message given saving IM address that not valid.
317 $this->showForm(_('Not a valid Jabber ID'));
319 } else if ($user->jabber == $jabber) {
320 // TRANS: Message given saving IM address that is already set.
321 $this->showForm(_('That is already your Jabber ID.'));
323 } else if ($this->jabberExists($jabber)) {
324 // TRANS: Message given saving IM address that is already set for another user.
325 $this->showForm(_('Jabber ID already belongs to another user.'));
329 $confirm = new Confirm_address();
331 $confirm->address = $jabber;
332 $confirm->address_type = 'jabber';
333 $confirm->user_id = $user->id;
334 $confirm->code = common_confirmation_code(64);
335 $confirm->sent = common_sql_now();
336 $confirm->claimed = common_sql_now();
338 $result = $confirm->insert();
340 if ($result === false) {
341 common_log_db_error($confirm, 'INSERT', __FILE__);
342 // TRANS: Server error thrown on database error adding IM confirmation code.
343 $this->serverError(_('Could not insert confirmation code.'));
347 jabber_confirm_address($confirm->code,
351 // TRANS: Message given saving valid IM address that is to be confirmed.
352 // TRANS: %s is the IM address set for the site.
353 $msg = sprintf(_('A confirmation code was sent '.
354 'to the IM address you added. '.
355 'You must approve %s for '.
356 'sending messages to you.'),
357 jabber_daemon_address());
359 $this->showForm($msg, true);
363 * Cancel a confirmation
365 * If a confirmation exists, cancel it.
369 function cancelConfirmation()
371 $jabber = $this->arg('jabber');
373 $confirm = $this->getConfirmation();
376 // TRANS: Message given canceling IM address confirmation that is not pending.
377 $this->showForm(_('No pending confirmation to cancel.'));
380 if ($confirm->address != $jabber) {
381 // TRANS: Message given canceling IM address confirmation for the wrong IM address.
382 $this->showForm(_('That is the wrong IM address.'));
386 $result = $confirm->delete();
389 common_log_db_error($confirm, 'DELETE', __FILE__);
390 // TRANS: Server error thrown on database error canceling IM address confirmation.
391 $this->serverError(_('Could not delete IM confirmation.'));
395 // TRANS: Message given after successfully canceling IM address confirmation.
396 $this->showForm(_('IM confirmation cancelled.'), true);
402 * If the user has a confirmed address, remove it.
406 function removeAddress()
408 $user = common_current_user();
410 $jabber = $this->arg('jabber');
412 // Maybe an old tab open...?
414 if ($user->jabber != $jabber) {
415 // TRANS: Message given trying to remove an IM address that is not
416 // TRANS: registered for the active user.
417 $this->showForm(_('That is not your Jabber ID.'));
421 $user->query('BEGIN');
423 $original = clone($user);
425 $user->jabber = null;
427 $result = $user->updateKeys($original);
430 common_log_db_error($user, 'UPDATE', __FILE__);
431 // TRANS: Server error thrown on database error removing a registered IM address.
432 $this->serverError(_('Could not update user.'));
435 $user->query('COMMIT');
437 // XXX: unsubscribe to the old address
439 // TRANS: Message given after successfully removing a registered IM address.
440 $this->showForm(_('The IM address was removed.'), true);
444 * Does this Jabber ID exist?
446 * Checks if we already have another user with this address.
448 * @param string $jabber Address to check
450 * @return boolean whether the Jabber ID exists
452 function jabberExists($jabber)
454 $user = common_current_user();
456 $other = User::staticGet('jabber', $jabber);
461 return $other->id != $user->id;