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 return _('IM settings');
60 * Instructions for use
62 * @return instructions for use
65 function getInstructions()
67 return _('You can send and receive notices through '.
68 'instant messaging [instant messages](%%doc.im%%). '.
69 'Configure your addresses and settings below.');
73 * Content area of the page
75 * We make different sections of the form for the different kinds of
76 * functions, and have submit buttons with different names. These
77 * are muxed by handlePost() to see what the user really wants to do.
82 function showContent()
84 $transports = array();
85 Event::handle('GetImTransports', array(&$transports));
87 $this->element('div', array('class' => 'error'),
88 _('IM is not available.'));
92 $user = common_current_user();
94 $user_im_prefs_by_transport = array();
96 foreach($transports as $transport=>$transport_info)
98 $this->elementStart('form', array('method' => 'post',
99 'id' => 'form_settings_im',
100 'class' => 'form_settings',
102 common_local_url('imsettings')));
103 $this->elementStart('fieldset', array('id' => 'settings_im_address'));
104 $this->element('legend', null, $transport_info['display']);
105 $this->hidden('token', common_session_token());
106 $this->hidden('transport', $transport);
108 if ($user_im_prefs = User_im_prefs::pkeyGet( array('transport' => $transport, 'user_id' => $user->id) )) {
109 $user_im_prefs_by_transport[$transport] = $user_im_prefs;
110 $this->element('p', 'form_confirmed', $user_im_prefs->screenname);
111 $this->element('p', 'form_note',
112 sprintf(_('Current confirmed %s address.'),$transport_info['display']));
113 $this->hidden('screenname', $user_im_prefs->screenname);
114 $this->submit('remove', _('Remove'));
116 $confirm = $this->getConfirmation($transport);
118 $this->element('p', 'form_unconfirmed', $confirm->address);
119 $this->element('p', 'form_note',
120 sprintf(_('Awaiting confirmation on this address. '.
121 'Check your %s account for a '.
122 'message with further instructions.'),
123 $transport_info['display']));
124 $this->hidden('screenname', $confirm->address);
125 $this->submit('cancel', _('Cancel'));
127 $this->elementStart('ul', 'form_data');
128 $this->elementStart('li');
129 $this->input('screenname', _('IM address'),
130 ($this->arg('screenname')) ? $this->arg('screenname') : null,
131 sprintf(_('%s screenname.'),
132 $transport_info['display']));
133 $this->elementEnd('li');
134 $this->elementEnd('ul');
135 $this->submit('add', _('Add'));
138 $this->elementEnd('fieldset');
139 $this->elementEnd('form');
142 if($user_im_prefs_by_transport)
144 $this->elementStart('form', array('method' => 'post',
145 'id' => 'form_settings_im',
146 'class' => 'form_settings',
148 common_local_url('imsettings')));
149 $this->elementStart('fieldset', array('id' => 'settings_im_preferences'));
150 $this->element('legend', null, _('Preferences'));
151 $this->hidden('token', common_session_token());
152 $this->elementStart('table');
153 $this->elementStart('tr');
154 $this->element('th', null, _('Preferences'));
155 foreach($user_im_prefs_by_transport as $transport=>$user_im_prefs)
157 $this->element('th', null, $transports[$transport]['display']);
159 $this->elementEnd('tr');
160 $preferences = array(
161 array('name'=>'notify', 'description'=>_('Send me notices')),
162 array('name'=>'updatefrompresence', 'description'=>_('Post a notice when my status changes.')),
163 array('name'=>'replies', 'description'=>_('Send me replies '.
164 'from people I\'m not subscribed to.')),
165 array('name'=>'microid', 'description'=>_('Publish a MicroID'))
167 foreach($preferences as $preference)
169 $this->elementStart('tr');
170 foreach($user_im_prefs_by_transport as $transport=>$user_im_prefs)
172 $preference_name = $preference['name'];
173 $this->elementStart('td');
174 $this->checkbox($transport . '_' . $preference['name'],
175 $preference['description'],
176 $user_im_prefs->$preference_name);
177 $this->elementEnd('td');
179 $this->elementEnd('tr');
181 $this->elementEnd('table');
182 $this->submit('save', _('Save'));
183 $this->elementEnd('fieldset');
184 $this->elementEnd('form');
189 * Get a confirmation code for this user
191 * @return Confirm_address address object for this user
194 function getConfirmation($transport)
196 $user = common_current_user();
198 $confirm = new Confirm_address();
200 $confirm->user_id = $user->id;
201 $confirm->address_type = $transport;
203 if ($confirm->find(true)) {
211 * Handle posts to this form
213 * Based on the button that was pressed, muxes out to other functions
214 * to do the actual task requested.
216 * All sub-functions reload the form with a message -- success or failure.
221 function handlePost()
224 $token = $this->trimmed('token');
225 if (!$token || $token != common_session_token()) {
226 $this->showForm(_('There was a problem with your session token. '.
227 'Try again, please.'));
231 if ($this->arg('save')) {
232 $this->savePreferences();
233 } else if ($this->arg('add')) {
235 } else if ($this->arg('cancel')) {
236 $this->cancelConfirmation();
237 } else if ($this->arg('remove')) {
238 $this->removeAddress();
240 $this->showForm(_('Unexpected form submission.'));
245 * Save user's Jabber preferences
247 * These are the checkboxes at the bottom of the page. They're used to
248 * set different settings
253 function savePreferences()
255 $user = common_current_user();
257 $user_im_prefs = new User_im_prefs();
258 $user_im_prefs->user_id = $user->id;
259 if($user_im_prefs->find() && $user_im_prefs->fetch())
261 $preferences = array('notify', 'updatefrompresence', 'replies', 'microid');
262 $user_im_prefs->query('BEGIN');
265 $original = clone($user_im_prefs);
266 foreach($preferences as $preference)
268 $user_im_prefs->$preference = $this->boolean($user_im_prefs->transport . '_' . $preference);
270 $result = $user_im_prefs->update($original);
272 if ($result === false) {
273 common_log_db_error($user, 'UPDATE', __FILE__);
274 $this->serverError(_('Couldn\'t update IM preferences.'));
277 }while($user_im_prefs->fetch());
278 $user_im_prefs->query('COMMIT');
280 $this->showForm(_('Preferences saved.'), true);
284 * Sends a confirmation to the address given
286 * Stores a confirmation record and sends out a
287 * message with the confirmation info.
292 function addAddress()
294 $user = common_current_user();
296 $screenname = $this->trimmed('screenname');
297 $transport = $this->trimmed('transport');
302 $this->showForm(_('No screenname.'));
307 $this->showForm(_('No transport.'));
311 Event::handle('NormalizeImScreenname', array($transport, &$screenname));
314 $this->showForm(_('Cannot normalize that screenname'));
318 Event::handle('ValidateImScreenname', array($transport, $screenname, &$valid));
320 $this->showForm(_('Not a valid screenname'));
322 } else if ($this->screennameExists($transport, $screenname)) {
323 $this->showForm(_('Screenname already belongs to another user.'));
327 $confirm = new Confirm_address();
329 $confirm->address = $screenname;
330 $confirm->address_type = $transport;
331 $confirm->user_id = $user->id;
332 $confirm->code = common_confirmation_code(64);
333 $confirm->sent = common_sql_now();
334 $confirm->claimed = common_sql_now();
336 $result = $confirm->insert();
338 if ($result === false) {
339 common_log_db_error($confirm, 'INSERT', __FILE__);
340 $this->serverError(_('Couldn\'t insert confirmation code.'));
344 Event::handle('SendImConfirmationCode', array($transport, $screenname, $confirm->code, $user));
346 $msg = _('A confirmation code was sent '.
347 'to the IM address you added.');
349 $this->showForm($msg, true);
353 * Cancel a confirmation
355 * If a confirmation exists, cancel it.
360 function cancelConfirmation()
362 $screenname = $this->trimmed('screenname');
363 $transport = $this->trimmed('transport');
365 $confirm = $this->getConfirmation($transport);
368 $this->showForm(_('No pending confirmation to cancel.'));
371 if ($confirm->address != $screenname) {
372 $this->showForm(_('That is the wrong IM address.'));
376 $result = $confirm->delete();
379 common_log_db_error($confirm, 'DELETE', __FILE__);
380 $this->serverError(_('Couldn\'t delete confirmation.'));
384 $this->showForm(_('Confirmation cancelled.'), true);
390 * If the user has a confirmed address, remove it.
395 function removeAddress()
397 $user = common_current_user();
399 $screenname = $this->trimmed('screenname');
400 $transport = $this->trimmed('transport');
402 // Maybe an old tab open...?
404 $user_im_prefs = new User_im_prefs();
405 $user_im_prefs->user_id = $user->id;
406 if(! ($user_im_prefs->find() && $user_im_prefs->fetch())) {
407 $this->showForm(_('That is not your screenname.'));
411 $result = $user_im_prefs->delete();
414 common_log_db_error($user, 'UPDATE', __FILE__);
415 $this->serverError(_('Couldn\'t update user im prefs.'));
419 // XXX: unsubscribe to the old address
421 $this->showForm(_('The address was removed.'), true);
425 * Does this screenname exist?
427 * Checks if we already have another user with this address.
429 * @param string $transport Transport to check
430 * @param string $screenname Screenname to check
432 * @return boolean whether the screenname exists
435 function screennameExists($transport, $screenname)
437 $user = common_current_user();
439 $user_im_prefs = new User_im_prefs();
440 $user_im_prefs->transport = $transport;
441 $user_im_prefs->screenname = $screenname;
442 if($user_im_prefs->find() && $user_im_prefs->fetch()){