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')) {
35 * Settings for Jabber/XMPP integration
39 * @author Evan Prodromou <evan@status.net>
40 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41 * @link http://status.net/
46 class ImsettingsAction extends ConnectSettingsAction
51 * @return string Title of the page
56 // TRANS: Title for instance messaging settings.
57 return _('IM settings');
61 * Instructions for use
63 * @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 'instant messaging [instant messages](%%doc.im%%). '.
73 'Configure your addresses 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.
86 function showContent()
88 $transports = array();
89 Event::handle('GetImTransports', array(&$transports));
91 $this->element('div', array('class' => 'error'),
92 // TRANS: Message given in the IM settings if IM is not enabled on the site.
93 _('IM is not available.'));
97 $user = common_current_user();
99 $user_im_prefs_by_transport = array();
101 foreach($transports as $transport=>$transport_info)
103 $this->elementStart('form', array('method' => 'post',
104 'id' => 'form_settings_im',
105 'class' => 'form_settings',
107 common_local_url('imsettings')));
108 $this->elementStart('fieldset', array('id' => 'settings_im_address'));
109 // TRANS: Form legend for IM settings form.
110 $this->element('legend', null, $transport_info['display']);
111 $this->hidden('token', common_session_token());
112 $this->hidden('transport', $transport);
114 if ($user_im_prefs = User_im_prefs::pkeyGet( array('transport' => $transport, 'user_id' => $user->id) )) {
115 $user_im_prefs_by_transport[$transport] = $user_im_prefs;
116 $this->element('p', 'form_confirmed', $user_im_prefs->screenname);
117 // TRANS: Form note in IM settings form.
118 $this->element('p', 'form_note',
119 sprintf(_('Current confirmed %s address.'),$transport_info['display']));
120 $this->hidden('screenname', $user_im_prefs->screenname);
121 // TRANS: Button label to remove a confirmed IM address.
122 $this->submit('remove', _m('BUTTON','Remove'));
124 $confirm = $this->getConfirmation($transport);
126 $this->element('p', 'form_unconfirmed', $confirm->address);
127 // TRANS: Form note in IM settings form.
128 $this->element('p', 'form_note',
129 // TRANS: Form note in IM settings form.
130 // TRANS: %s is the IM address set for the site.
131 sprintf(_('Awaiting confirmation on this address. '.
132 'Check your %s account for a '.
133 'message with further instructions. '.
134 '(Did you add %s to your buddy list?)'),
135 $transport_info['display'],
136 $transport_info['daemon_screenname']));
137 $this->hidden('screenname', $confirm->address);
138 // TRANS: Button label to cancel an IM address confirmation procedure.
139 $this->submit('cancel', _m('BUTTON','Cancel'));
141 $this->elementStart('ul', 'form_data');
142 $this->elementStart('li');
143 $this->input('screenname', _('IM address'),
144 ($this->arg('screenname')) ? $this->arg('screenname') : null,
145 sprintf(_('%s screenname.'),
146 $transport_info['display']));
147 $this->elementEnd('li');
148 $this->elementEnd('ul');
149 // TRANS: Button label for adding an IM address in IM settings form.
150 $this->submit('add', _m('BUTTON','Add'));
153 $this->elementEnd('fieldset');
154 $this->elementEnd('form');
157 if($user_im_prefs_by_transport)
159 $this->elementStart('form', array('method' => 'post',
160 'id' => 'form_settings_im',
161 'class' => 'form_settings',
163 common_local_url('imsettings')));
164 $this->elementStart('fieldset', array('id' => 'settings_im_preferences'));
165 // TRANS: Header for IM preferences form.
166 $this->element('legend', null, _('IM Preferences'));
167 $this->hidden('token', common_session_token());
168 $this->elementStart('table');
169 $this->elementStart('tr');
170 foreach($user_im_prefs_by_transport as $transport=>$user_im_prefs)
172 $this->element('th', null, $transports[$transport]['display']);
174 $this->elementEnd('tr');
175 $preferences = array(
176 // TRANS: Checkbox label in IM preferences form.
177 array('name'=>'notify', 'description'=>_('Send me notices')),
178 // TRANS: Checkbox label in IM preferences form.
179 array('name'=>'updatefrompresence', 'description'=>_('Post a notice when my status changes.')),
180 // TRANS: Checkbox label in IM preferences form.
181 array('name'=>'replies', 'description'=>_('Send me replies '.
182 'from people I\'m not subscribed to.')),
183 // TRANS: Checkbox label in IM preferences form.
184 array('name'=>'microid', 'description'=>_('Publish a MicroID'))
186 foreach($preferences as $preference)
188 $this->elementStart('tr');
189 foreach($user_im_prefs_by_transport as $transport=>$user_im_prefs)
191 $preference_name = $preference['name'];
192 $this->elementStart('td');
193 $this->checkbox($transport . '_' . $preference['name'],
194 $preference['description'],
195 $user_im_prefs->$preference_name);
196 $this->elementEnd('td');
198 $this->elementEnd('tr');
200 $this->elementEnd('table');
201 // TRANS: Button label to save IM preferences.
202 $this->submit('save', _m('BUTTON','Save'));
203 $this->elementEnd('fieldset');
204 $this->elementEnd('form');
209 * Get a confirmation code for this user
211 * @return Confirm_address address object for this user
214 function getConfirmation($transport)
216 $user = common_current_user();
218 $confirm = new Confirm_address();
220 $confirm->user_id = $user->id;
221 $confirm->address_type = $transport;
223 if ($confirm->find(true)) {
231 * Handle posts to this form
233 * Based on the button that was pressed, muxes out to other functions
234 * to do the actual task requested.
236 * All sub-functions reload the form with a message -- success or failure.
241 function handlePost()
244 $token = $this->trimmed('token');
245 if (!$token || $token != common_session_token()) {
246 $this->showForm(_('There was a problem with your session token. '.
247 'Try again, please.'));
251 if ($this->arg('save')) {
252 $this->savePreferences();
253 } else if ($this->arg('add')) {
255 } else if ($this->arg('cancel')) {
256 $this->cancelConfirmation();
257 } else if ($this->arg('remove')) {
258 $this->removeAddress();
260 // TRANS: Message given submitting a form with an unknown action in IM settings.
261 $this->showForm(_('Unexpected form submission.'));
266 * Save user's Jabber preferences
268 * These are the checkboxes at the bottom of the page. They're used to
269 * set different settings
274 function savePreferences()
276 $user = common_current_user();
278 $user_im_prefs = new User_im_prefs();
279 $user_im_prefs->query('BEGIN');
280 $user_im_prefs->user_id = $user->id;
281 if($user_im_prefs->find() && $user_im_prefs->fetch())
283 $preferences = array('notify', 'updatefrompresence', 'replies', 'microid');
286 $original = clone($user_im_prefs);
287 $new = clone($user_im_prefs);
288 foreach($preferences as $preference)
290 $new->$preference = $this->boolean($new->transport . '_' . $preference);
292 $result = $new->update($original);
294 if ($result === false) {
295 common_log_db_error($user, 'UPDATE', __FILE__);
296 // TRANS: Server error thrown on database error updating IM preferences.
297 $this->serverError(_('Couldn\'t update IM preferences.'));
300 }while($user_im_prefs->fetch());
302 $user_im_prefs->query('COMMIT');
303 // TRANS: Confirmation message for successful IM preferences save.
304 $this->showForm(_('Preferences saved.'), true);
308 * Sends a confirmation to the address given
310 * Stores a confirmation record and sends out a
311 * message with the confirmation info.
316 function addAddress()
318 $user = common_current_user();
320 $screenname = $this->trimmed('screenname');
321 $transport = $this->trimmed('transport');
326 // TRANS: Message given saving IM address without having provided one.
327 $this->showForm(_('No screenname.'));
332 $this->showForm(_('No transport.'));
336 Event::handle('NormalizeImScreenname', array($transport, &$screenname));
339 // TRANS: Message given saving IM address that cannot be normalised.
340 $this->showForm(_('Cannot normalize that screenname'));
344 Event::handle('ValidateImScreenname', array($transport, $screenname, &$valid));
346 // TRANS: Message given saving IM address that not valid.
347 $this->showForm(_('Not a valid screenname'));
349 } else if ($this->screennameExists($transport, $screenname)) {
350 // TRANS: Message given saving IM address that is already set for another user.
351 $this->showForm(_('Screenname already belongs to another user.'));
355 $confirm = new Confirm_address();
357 $confirm->address = $screenname;
358 $confirm->address_type = $transport;
359 $confirm->user_id = $user->id;
360 $confirm->code = common_confirmation_code(64);
361 $confirm->sent = common_sql_now();
362 $confirm->claimed = common_sql_now();
364 $result = $confirm->insert();
366 if ($result === false) {
367 common_log_db_error($confirm, 'INSERT', __FILE__);
368 // TRANS: Server error thrown on database error adding IM confirmation code.
369 $this->serverError(_('Couldn\'t insert confirmation code.'));
373 Event::handle('SendImConfirmationCode', array($transport, $screenname, $confirm->code, $user));
375 // TRANS: Message given saving valid IM address that is to be confirmed.
376 $msg = _('A confirmation code was sent '.
377 'to the IM address you added.');
379 $this->showForm($msg, true);
383 * Cancel a confirmation
385 * If a confirmation exists, cancel it.
390 function cancelConfirmation()
392 $screenname = $this->trimmed('screenname');
393 $transport = $this->trimmed('transport');
395 $confirm = $this->getConfirmation($transport);
398 // TRANS: Message given canceling IM address confirmation that is not pending.
399 $this->showForm(_('No pending confirmation to cancel.'));
402 if ($confirm->address != $screenname) {
403 // TRANS: Message given canceling IM address confirmation for the wrong IM address.
404 $this->showForm(_('That is the wrong IM address.'));
408 $result = $confirm->delete();
411 common_log_db_error($confirm, 'DELETE', __FILE__);
412 // TRANS: Server error thrown on database error canceling IM address confirmation.
413 $this->serverError(_('Couldn\'t delete confirmation.'));
417 // TRANS: Message given after successfully canceling IM address confirmation.
418 $this->showForm(_('IM confirmation cancelled.'), true);
424 * If the user has a confirmed address, remove it.
429 function removeAddress()
431 $user = common_current_user();
433 $screenname = $this->trimmed('screenname');
434 $transport = $this->trimmed('transport');
436 // Maybe an old tab open...?
438 $user_im_prefs = new User_im_prefs();
439 $user_im_prefs->user_id = $user->id;
440 if(! ($user_im_prefs->find() && $user_im_prefs->fetch())) {
441 // TRANS: Message given trying to remove an IM address that is not
442 // TRANS: registered for the active user.
443 $this->showForm(_('That is not your screenname.'));
447 $result = $user_im_prefs->delete();
450 common_log_db_error($user, 'UPDATE', __FILE__);
451 // TRANS: Server error thrown on database error removing a registered IM address.
452 $this->serverError(_('Couldn\'t update user im prefs.'));
453 $this->serverError(_('Couldn\'t update user.'));
457 // XXX: unsubscribe to the old address
459 // TRANS: Message given after successfully removing a registered IM address.
460 $this->showForm(_('The IM address was removed.'), true);
464 * Does this screenname exist?
466 * Checks if we already have another user with this address.
468 * @param string $transport Transport to check
469 * @param string $screenname Screenname to check
471 * @return boolean whether the screenname exists
474 function screennameExists($transport, $screenname)
476 $user = common_current_user();
478 $user_im_prefs = new User_im_prefs();
479 $user_im_prefs->transport = $transport;
480 $user_im_prefs->screenname = $screenname;
481 if($user_im_prefs->find() && $user_im_prefs->fetch()){