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('GNUSOCIAL')) { exit(1); }
33 * Settings for Jabber/XMPP integration
37 * @author Evan Prodromou <evan@status.net>
38 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
39 * @link http://status.net/
44 class ImsettingsAction extends SettingsAction
49 * @return string Title of the page
53 // TRANS: Title for Instant Messaging settings.
54 return _('IM settings');
58 * Instructions for use
60 * @return instructions for use
62 function getInstructions()
64 // TRANS: Instant messaging settings page instructions.
65 // TRANS: [instant messages] is link text, "(%%doc.im%%)" is the link.
66 // TRANS: the order and formatting of link text and link should remain unchanged.
67 return _('You can send and receive notices through '.
68 '[instant messaging](%%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.
81 function showContent()
83 $transports = array();
84 Event::handle('GetImTransports', array(&$transports));
86 $this->element('div', array('class' => 'error'),
87 // TRANS: Message given in the IM settings if IM is not enabled on the site.
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 // TRANS: Form legend for IM settings form.
105 $this->element('legend', null, $transport_info['display']);
106 $this->hidden('token', common_session_token());
107 $this->hidden('transport', $transport);
109 if ($user_im_prefs = User_im_prefs::pkeyGet( array('transport' => $transport, 'user_id' => $user->id) )) {
110 $user_im_prefs_by_transport[$transport] = $user_im_prefs;
111 $this->element('p', 'form_confirmed', $user_im_prefs->screenname);
112 $this->element('p', 'form_note',
113 // TRANS: Form note in IM settings form. %s is the type of IM address that was confirmed.
114 sprintf(_('Current confirmed %s address.'),$transport_info['display']));
115 $this->hidden('screenname', $user_im_prefs->screenname);
116 // TRANS: Button label to remove a confirmed IM address.
117 $this->submit('remove', _m('BUTTON','Remove'));
120 $confirm = $this->getConfirmation($transport);
121 $this->element('p', 'form_unconfirmed', $confirm->address);
122 // TRANS: Form note in IM settings form.
123 $this->element('p', 'form_note',
124 // TRANS: Form note in IM settings form.
125 // TRANS: %s is the IM service name, %2$s is the IM address set.
126 sprintf(_('Awaiting confirmation on this address. '.
127 'Check your %1$s account for a '.
128 'message with further instructions. '.
129 '(Did you add %2$s to your buddy list?)'),
130 $transport_info['display'],
131 $transport_info['daemonScreenname']));
132 $this->hidden('screenname', $confirm->address);
133 // TRANS: Button label to cancel an IM address confirmation procedure.
134 $this->submit('cancel', _m('BUTTON','Cancel'));
135 } catch (NoResultException $e) {
136 $this->elementStart('ul', 'form_data');
137 $this->elementStart('li');
138 // TRANS: Field label for IM address.
139 $this->input('screenname', _('IM address'),
140 ($this->arg('screenname')) ? $this->arg('screenname') : null,
141 // TRANS: Field title for IM address. %s is the IM service name.
142 sprintf(_('%s screenname.'),
143 $transport_info['display']));
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');
151 $this->elementEnd('form');
154 if($user_im_prefs_by_transport)
156 $this->elementStart('form', array('method' => 'post',
157 'id' => 'form_settings_im',
158 'class' => 'form_settings',
160 common_local_url('imsettings')));
161 $this->elementStart('fieldset', array('id' => 'settings_im_preferences'));
162 // TRANS: Header for IM preferences form.
163 $this->element('legend', null, _('IM Preferences'));
164 $this->hidden('token', common_session_token());
165 $this->elementStart('table');
166 $this->elementStart('tr');
167 foreach($user_im_prefs_by_transport as $transport=>$user_im_prefs)
169 $this->element('th', null, $transports[$transport]['display']);
171 $this->elementEnd('tr');
172 $preferences = array(
173 // TRANS: Checkbox label in IM preferences form.
174 array('name'=>'notify', 'description'=>_('Send me notices')),
175 // TRANS: Checkbox label in IM preferences form.
176 array('name'=>'updatefrompresence', 'description'=>_('Post a notice when my status changes.')),
177 // TRANS: Checkbox label in IM preferences form.
178 array('name'=>'replies', 'description'=>_('Send me replies '.
179 'from people I\'m not subscribed to.')),
181 foreach($preferences as $preference)
183 $this->elementStart('tr');
184 foreach($user_im_prefs_by_transport as $transport=>$user_im_prefs)
186 $preference_name = $preference['name'];
187 $this->elementStart('td');
188 $this->checkbox($transport . '_' . $preference['name'],
189 $preference['description'],
190 $user_im_prefs->$preference_name);
191 $this->elementEnd('td');
193 $this->elementEnd('tr');
195 $this->elementEnd('table');
196 // TRANS: Button label to save IM preferences.
197 $this->submit('save', _m('BUTTON','Save'));
198 $this->elementEnd('fieldset');
199 $this->elementEnd('form');
204 * Get a confirmation code for this user
206 * @return Confirm_address address object for this user
208 function getConfirmation($transport)
210 $confirm = new Confirm_address();
212 $confirm->user_id = $this->scoped->getID();
213 $confirm->address_type = $transport;
215 if ($confirm->find(true)) {
219 throw new NoResultException($confirm);
222 protected function doPost()
224 if ($this->arg('save')) {
225 return $this->savePreferences();
226 } else if ($this->arg('add')) {
227 return $this->addAddress();
228 } else if ($this->arg('cancel')) {
229 return $this->cancelConfirmation();
230 } else if ($this->arg('remove')) {
231 return $this->removeAddress();
233 // TRANS: Message given submitting a form with an unknown action in Instant Messaging settings.
234 throw new ClientException(_('Unexpected form submission.'));
238 * Save user's XMPP preferences
240 * These are the checkboxes at the bottom of the page. They're used to
241 * set different settings
245 function savePreferences()
247 $user_im_prefs = new User_im_prefs();
248 $user_im_prefs->query('BEGIN');
249 $user_im_prefs->user_id = $this->scoped->getID();
250 if($user_im_prefs->find() && $user_im_prefs->fetch())
252 $preferences = array('notify', 'updatefrompresence', 'replies');
255 $original = clone($user_im_prefs);
256 $new = clone($user_im_prefs);
257 foreach($preferences as $preference)
259 $new->$preference = $this->boolean($new->transport . '_' . $preference);
261 $result = $new->update($original);
263 if ($result === false) {
264 common_log_db_error($user_im_prefs, 'UPDATE', __FILE__);
265 // TRANS: Server error thrown on database error updating IM preferences.
266 throw new ServerException(_('Could not update IM preferences.'));
268 }while($user_im_prefs->fetch());
270 $user_im_prefs->query('COMMIT');
271 // TRANS: Confirmation message for successful IM preferences save.
272 return _('Preferences saved.');
276 * Sends a confirmation to the address given
278 * Stores a confirmation record and sends out a
279 * message with the confirmation info.
283 function addAddress()
285 $screenname = $this->trimmed('screenname');
286 $transport = $this->trimmed('transport');
290 if (empty($screenname)) {
291 // TRANS: Message given saving IM address without having provided one.
292 throw new ClientException(_('No screenname.'));
295 if (empty($transport)) {
296 // TRANS: Form validation error when no transport is available setting an IM address.
297 throw new ClientException(_('No transport.'));
300 Event::handle('NormalizeImScreenname', array($transport, &$screenname));
302 if (empty($screenname)) {
303 // TRANS: Message given saving IM address that cannot be normalised.
304 throw new ClientException(_('Cannot normalize that screenname.'));
307 Event::handle('ValidateImScreenname', array($transport, $screenname, &$valid));
309 // TRANS: Message given saving IM address that not valid.
310 throw new ClientException(_('Not a valid screenname.'));
311 } else if ($this->screennameExists($transport, $screenname)) {
312 // TRANS: Message given saving IM address that is already set for another user.
313 throw new ClientException(_('Screenname already belongs to another user.'));
316 $confirm = new Confirm_address();
318 $confirm->address = $screenname;
319 $confirm->address_type = $transport;
320 $confirm->user_id = $this->scoped->getID();
321 $confirm->code = common_confirmation_code(64);
322 $confirm->sent = common_sql_now();
323 $confirm->claimed = common_sql_now();
325 $result = $confirm->insert();
327 if ($result === false) {
328 common_log_db_error($confirm, 'INSERT', __FILE__);
329 // TRANS: Server error thrown on database error adding Instant Messaging confirmation code.
330 $this->serverError(_('Could not insert confirmation code.'));
333 Event::handle('SendImConfirmationCode', array($transport, $screenname, $confirm->code, $this->scoped));
335 // TRANS: Message given saving valid IM address that is to be confirmed.
336 return _('A confirmation code was sent to the IM address you added.');
340 * Cancel a confirmation
342 * If a confirmation exists, cancel it.
346 function cancelConfirmation()
348 $screenname = $this->trimmed('screenname');
349 $transport = $this->trimmed('transport');
352 $confirm = $this->getConfirmation($transport);
353 if ($confirm->address != $screenname) {
354 // TRANS: Message given canceling IM address confirmation for the wrong IM address.
355 throw new ClientException(_('That is the wrong IM address.'));
357 } catch (NoResultException $e) {
358 // TRANS: Message given canceling Instant Messaging address confirmation that is not pending.
359 throw new AlreadyFulfilledException(_('No pending confirmation to cancel.'));
364 // TRANS: Message given after successfully canceling IM address confirmation.
365 return _('IM confirmation cancelled.');
371 * If the user has a confirmed address, remove it.
375 function removeAddress()
377 $screenname = $this->trimmed('screenname');
378 $transport = $this->trimmed('transport');
380 // Maybe an old tab open...?
382 $user_im_prefs = new User_im_prefs();
383 $user_im_prefs->user_id = $this->scoped->getID();
384 $user_im_prefs->transport = $transport;
385 if (!$user_im_prefs->find(true)) {
386 // TRANS: Message given trying to remove an IM address that is not
387 // TRANS: registered for the active user.
388 throw new AlreadyFulfilledException(_('There were no preferences stored for this transport.'));
391 $result = $user_im_prefs->delete();
393 if ($result === false) {
394 common_log_db_error($user_im_prefs, 'UPDATE', __FILE__);
395 // TRANS: Server error thrown on database error removing a registered IM address.
396 throw new ServerException(_('Could not update user IM preferences.'));
399 // XXX: unsubscribe to the old address
401 // TRANS: Message given after successfully removing a registered Instant Messaging address.
402 return _('The IM address was removed.');
406 * Does this screenname exist?
408 * Checks if we already have another user with this address.
410 * @param string $transport Transport to check
411 * @param string $screenname Screenname to check
413 * @return boolean whether the screenname exists
416 function screennameExists($transport, $screenname)
418 $user_im_prefs = new User_im_prefs();
419 $user_im_prefs->transport = $transport;
420 $user_im_prefs->screenname = $screenname;
421 return $user_im_prefs->find(true) ? true : false;