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 jabber_daemon_address()));
138 $this->hidden('screenname', $confirm->address);
139 // TRANS: Button label to cancel an IM address confirmation procedure.
140 $this->submit('cancel', _m('BUTTON','Cancel'));
142 $this->elementStart('ul', 'form_data');
143 $this->elementStart('li');
144 $this->input('screenname', _('IM address'),
145 ($this->arg('screenname')) ? $this->arg('screenname') : null,
146 sprintf(_('%s screenname.'),
147 $transport_info['display']));
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');
155 $this->elementEnd('form');
158 if($user_im_prefs_by_transport)
160 $this->elementStart('form', array('method' => 'post',
161 'id' => 'form_settings_im',
162 'class' => 'form_settings',
164 common_local_url('imsettings')));
165 $this->elementStart('fieldset', array('id' => 'settings_im_preferences'));
166 $this->element('legend', null, _('Preferences'));
167 $this->hidden('token', common_session_token());
168 $this->elementStart('table');
169 $this->elementStart('tr');
170 // TRANS: Header for IM preferences form.
171 $this->element('th', null, _('IM Preferences'));
172 foreach($user_im_prefs_by_transport as $transport=>$user_im_prefs)
174 $this->element('th', null, $transports[$transport]['display']);
176 $this->elementEnd('tr');
177 $preferences = array(
178 // TRANS: Checkbox label in IM preferences form.
179 array('name'=>'notify', 'description'=>_('Send me notices')),
180 // TRANS: Checkbox label in IM preferences form.
181 array('name'=>'updatefrompresence', 'description'=>_('Post a notice when my status changes.')),
182 // TRANS: Checkbox label in IM preferences form.
183 array('name'=>'replies', 'description'=>_('Send me replies '.
184 'from people I\'m not subscribed to.')),
185 // TRANS: Checkbox label in IM preferences form.
186 array('name'=>'microid', 'description'=>_('Publish a MicroID'))
188 foreach($preferences as $preference)
190 $this->elementStart('tr');
191 foreach($user_im_prefs_by_transport as $transport=>$user_im_prefs)
193 $preference_name = $preference['name'];
194 $this->elementStart('td');
195 $this->checkbox($transport . '_' . $preference['name'],
196 $preference['description'],
197 $user_im_prefs->$preference_name);
198 $this->elementEnd('td');
200 $this->elementEnd('tr');
202 $this->elementEnd('table');
203 // TRANS: Button label to save IM preferences.
204 $this->submit('save', _m('BUTTON','Save'));
205 $this->elementEnd('fieldset');
206 $this->elementEnd('form');
211 * Get a confirmation code for this user
213 * @return Confirm_address address object for this user
216 function getConfirmation($transport)
218 $user = common_current_user();
220 $confirm = new Confirm_address();
222 $confirm->user_id = $user->id;
223 $confirm->address_type = $transport;
225 if ($confirm->find(true)) {
233 * Handle posts to this form
235 * Based on the button that was pressed, muxes out to other functions
236 * to do the actual task requested.
238 * All sub-functions reload the form with a message -- success or failure.
243 function handlePost()
246 $token = $this->trimmed('token');
247 if (!$token || $token != common_session_token()) {
248 $this->showForm(_('There was a problem with your session token. '.
249 'Try again, please.'));
253 if ($this->arg('save')) {
254 $this->savePreferences();
255 } else if ($this->arg('add')) {
257 } else if ($this->arg('cancel')) {
258 $this->cancelConfirmation();
259 } else if ($this->arg('remove')) {
260 $this->removeAddress();
262 // TRANS: Message given submitting a form with an unknown action in IM settings.
263 $this->showForm(_('Unexpected form submission.'));
268 * Save user's Jabber preferences
270 * These are the checkboxes at the bottom of the page. They're used to
271 * set different settings
276 function savePreferences()
278 $user = common_current_user();
280 $user_im_prefs = new User_im_prefs();
281 $user_im_prefs->user_id = $user->id;
282 if($user_im_prefs->find() && $user_im_prefs->fetch())
284 $preferences = array('notify', 'updatefrompresence', 'replies', 'microid');
285 $user_im_prefs->query('BEGIN');
288 $original = clone($user_im_prefs);
289 foreach($preferences as $preference)
291 $user_im_prefs->$preference = $this->boolean($user_im_prefs->transport . '_' . $preference);
293 $result = $user_im_prefs->update($original);
295 if ($result === false) {
296 common_log_db_error($user, 'UPDATE', __FILE__);
297 // TRANS: Server error thrown on database error updating IM preferences.
298 $this->serverError(_('Couldn\'t update IM preferences.'));
301 }while($user_im_prefs->fetch());
302 $user_im_prefs->query('COMMIT');
304 // TRANS: Confirmation message for successful IM preferences save.
305 $this->showForm(_('Preferences saved.'), true);
309 * Sends a confirmation to the address given
311 * Stores a confirmation record and sends out a
312 * message with the confirmation info.
317 function addAddress()
319 $user = common_current_user();
321 $screenname = $this->trimmed('screenname');
322 $transport = $this->trimmed('transport');
327 // TRANS: Message given saving IM address without having provided one.
328 $this->showForm(_('No screenname.'));
333 $this->showForm(_('No transport.'));
337 Event::handle('NormalizeImScreenname', array($transport, &$screenname));
340 // TRANS: Message given saving IM address that cannot be normalised.
341 $this->showForm(_('Cannot normalize that screenname'));
345 Event::handle('ValidateImScreenname', array($transport, $screenname, &$valid));
347 // TRANS: Message given saving IM address that not valid.
348 $this->showForm(_('Not a valid screenname'));
350 } else if ($this->screennameExists($transport, $screenname)) {
351 // TRANS: Message given saving IM address that is already set for another user.
352 $this->showForm(_('Screenname already belongs to another user.'));
356 $confirm = new Confirm_address();
358 $confirm->address = $screenname;
359 $confirm->address_type = $transport;
360 $confirm->user_id = $user->id;
361 $confirm->code = common_confirmation_code(64);
362 $confirm->sent = common_sql_now();
363 $confirm->claimed = common_sql_now();
365 $result = $confirm->insert();
367 if ($result === false) {
368 common_log_db_error($confirm, 'INSERT', __FILE__);
369 // TRANS: Server error thrown on database error adding IM confirmation code.
370 $this->serverError(_('Couldn\'t insert confirmation code.'));
374 Event::handle('SendImConfirmationCode', array($transport, $screenname, $confirm->code, $user));
376 // TRANS: Message given saving valid IM address that is to be confirmed.
377 $msg = _('A confirmation code was sent '.
378 'to the IM address you added.');
380 $this->showForm($msg, true);
384 * Cancel a confirmation
386 * If a confirmation exists, cancel it.
391 function cancelConfirmation()
393 $screenname = $this->trimmed('screenname');
394 $transport = $this->trimmed('transport');
396 $confirm = $this->getConfirmation($transport);
399 // TRANS: Message given canceling IM address confirmation that is not pending.
400 $this->showForm(_('No pending confirmation to cancel.'));
403 if ($confirm->address != $screenname) {
404 // TRANS: Message given canceling IM address confirmation for the wrong IM address.
405 $this->showForm(_('That is the wrong IM address.'));
409 $result = $confirm->delete();
412 common_log_db_error($confirm, 'DELETE', __FILE__);
413 // TRANS: Server error thrown on database error canceling IM address confirmation.
414 $this->serverError(_('Couldn\'t delete confirmation.'));
418 // TRANS: Message given after successfully canceling IM address confirmation.
419 $this->showForm(_('IM confirmation cancelled.'), true);
425 * If the user has a confirmed address, remove it.
430 function removeAddress()
432 $user = common_current_user();
434 $screenname = $this->trimmed('screenname');
435 $transport = $this->trimmed('transport');
437 // Maybe an old tab open...?
439 $user_im_prefs = new User_im_prefs();
440 $user_im_prefs->user_id = $user->id;
441 if(! ($user_im_prefs->find() && $user_im_prefs->fetch())) {
442 // TRANS: Message given trying to remove an IM address that is not
443 // TRANS: registered for the active user.
444 $this->showForm(_('That is not your screenname.'));
448 $result = $user_im_prefs->delete();
451 common_log_db_error($user, 'UPDATE', __FILE__);
452 // TRANS: Server error thrown on database error removing a registered IM address.
453 $this->serverError(_('Couldn\'t update user im prefs.'));
454 $this->serverError(_('Couldn\'t update user.'));
458 // XXX: unsubscribe to the old address
460 // TRANS: Message given after successfully removing a registered IM address.
461 $this->showForm(_('The IM address was removed.'), true);
465 * Does this screenname exist?
467 * Checks if we already have another user with this address.
469 * @param string $transport Transport to check
470 * @param string $screenname Screenname to check
472 * @return boolean whether the screenname exists
475 function screennameExists($transport, $screenname)
477 $user = common_current_user();
479 $user_im_prefs = new User_im_prefs();
480 $user_im_prefs->transport = $transport;
481 $user_im_prefs->screenname = $screenname;
482 if($user_im_prefs->find() && $user_im_prefs->fetch()){