3 * Laconica, 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@controlyourself.ca>
25 * @copyright 2008-2009 Control Yourself, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://laconi.ca/
30 if (!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@controlyourself.ca>
43 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44 * @link http://laconi.ca/
49 class ImsettingsAction extends ConnectSettingsAction
54 * @return string Title of the page
59 return _('IM Settings');
63 * Instructions for use
65 * @return instructions for use
68 function getInstructions()
70 return _('You can send and receive notices through '.
71 'Jabber/GTalk [instant messages](%%doc.im%%). '.
72 'Configure your address and settings below.');
76 * Content area of the page
78 * We make different sections of the form for the different kinds of
79 * functions, and have submit buttons with different names. These
80 * are muxed by handlePost() to see what the user really wants to do.
85 function showContent()
87 $user = common_current_user();
88 $this->elementStart('form', array('method' => 'post',
89 'id' => 'form_settings_im',
90 'class' => 'form_settings',
92 common_local_url('imsettings')));
93 $this->elementStart('fieldset', array('id' => 'settings_im_address'));
94 $this->element('legend', null, _('Address'));
95 $this->hidden('token', common_session_token());
98 $this->element('p', 'form_confirmed', $user->jabber);
99 $this->element('p', 'form_note',
100 _('Current confirmed Jabber/GTalk address.'));
101 $this->hidden('jabber', $user->jabber);
102 $this->submit('remove', _('Remove'));
104 $confirm = $this->getConfirmation();
106 $this->element('p', 'form_unconfirmed', $confirm->address);
107 $this->element('p', 'form_note',
108 sprintf(_('Awaiting confirmation on this address. '.
109 'Check your Jabber/GTalk account for a '.
110 'message with further instructions. '.
111 '(Did you add %s to your buddy list?)'),
112 jabber_daemon_address()));
113 $this->hidden('jabber', $confirm->address);
114 $this->submit('cancel', _('Cancel'));
116 $this->elementStart('ul', 'form_data');
117 $this->elementStart('li');
118 $this->input('jabber', _('IM Address'),
119 ($this->arg('jabber')) ? $this->arg('jabber') : null,
120 sprintf(_('Jabber or GTalk address, '.
121 'like "UserName@example.org". '.
122 'First, make sure to add %s to your '.
123 'buddy list in your IM client or on GTalk.'),
124 jabber_daemon_address()));
125 $this->elementEnd('li');
126 $this->elementEnd('ul');
127 $this->submit('add', _('Add'));
130 $this->elementEnd('fieldset');
132 $this->elementStart('fieldset', array('id' => 'settings_im_preferences'));
133 $this->element('legend', null, _('Preferences'));
134 $this->elementStart('ul', 'form_data');
135 $this->elementStart('li');
136 $this->checkbox('jabbernotify',
137 _('Send me notices through Jabber/GTalk.'),
138 $user->jabbernotify);
139 $this->elementEnd('li');
140 $this->elementStart('li');
141 $this->checkbox('updatefrompresence',
142 _('Post a notice when my Jabber/GTalk status changes.'),
143 $user->updatefrompresence);
144 $this->elementEnd('li');
145 $this->elementStart('li');
146 $this->checkbox('jabberreplies',
147 _('Send me replies through Jabber/GTalk '.
148 'from people I\'m not subscribed to.'),
149 $user->jabberreplies);
150 $this->elementEnd('li');
151 $this->elementStart('li');
152 $this->checkbox('jabbermicroid',
153 _('Publish a MicroID for my Jabber/GTalk address.'),
154 $user->jabbermicroid);
155 $this->elementEnd('li');
156 $this->elementEnd('ul');
157 $this->submit('save', _('Save'));
158 $this->elementEnd('fieldset');
159 $this->elementEnd('form');
163 * Get a confirmation code for this user
165 * @return Confirm_address address object for this user
168 function getConfirmation()
170 $user = common_current_user();
172 $confirm = new Confirm_address();
174 $confirm->user_id = $user->id;
175 $confirm->address_type = 'jabber';
177 if ($confirm->find(true)) {
185 * Handle posts to this form
187 * Based on the button that was pressed, muxes out to other functions
188 * to do the actual task requested.
190 * All sub-functions reload the form with a message -- success or failure.
195 function handlePost()
198 $token = $this->trimmed('token');
199 if (!$token || $token != common_session_token()) {
200 $this->showForm(_('There was a problem with your session token. '.
201 'Try again, please.'));
205 if ($this->arg('save')) {
206 $this->savePreferences();
207 } else if ($this->arg('add')) {
209 } else if ($this->arg('cancel')) {
210 $this->cancelConfirmation();
211 } else if ($this->arg('remove')) {
212 $this->removeAddress();
214 $this->showForm(_('Unexpected form submission.'));
219 * Save user's Jabber preferences
221 * These are the checkboxes at the bottom of the page. They're used to
222 * set different settings
227 function savePreferences()
230 $jabbernotify = $this->boolean('jabbernotify');
231 $updatefrompresence = $this->boolean('updatefrompresence');
232 $jabberreplies = $this->boolean('jabberreplies');
233 $jabbermicroid = $this->boolean('jabbermicroid');
235 $user = common_current_user();
237 assert(!is_null($user)); // should already be checked
239 $user->query('BEGIN');
241 $original = clone($user);
243 $user->jabbernotify = $jabbernotify;
244 $user->updatefrompresence = $updatefrompresence;
245 $user->jabberreplies = $jabberreplies;
246 $user->jabbermicroid = $jabbermicroid;
248 $result = $user->update($original);
250 if ($result === false) {
251 common_log_db_error($user, 'UPDATE', __FILE__);
252 $this->serverError(_('Couldn\'t update user.'));
256 $user->query('COMMIT');
258 $this->showForm(_('Preferences saved.'), true);
262 * Sends a confirmation to the address given
264 * Stores a confirmation record and sends out a
265 * Jabber message with the confirmation info.
270 function addAddress()
272 $user = common_current_user();
274 $jabber = $this->trimmed('jabber');
279 $this->showForm(_('No Jabber ID.'));
283 $jabber = jabber_normalize_jid($jabber);
286 $this->showForm(_('Cannot normalize that Jabber ID'));
289 if (!jabber_valid_base_jid($jabber)) {
290 $this->showForm(_('Not a valid Jabber ID'));
292 } else if ($user->jabber == $jabber) {
293 $this->showForm(_('That is already your Jabber ID.'));
295 } else if ($this->jabberExists($jabber)) {
296 $this->showForm(_('Jabber ID already belongs to another user.'));
300 $confirm = new Confirm_address();
302 $confirm->address = $jabber;
303 $confirm->address_type = 'jabber';
304 $confirm->user_id = $user->id;
305 $confirm->code = common_confirmation_code(64);
307 $result = $confirm->insert();
309 if ($result === false) {
310 common_log_db_error($confirm, 'INSERT', __FILE__);
311 $this->serverError(_('Couldn\'t insert confirmation code.'));
315 if (!common_config('queue', 'enabled')) {
316 jabber_confirm_address($confirm->code,
321 $msg = sprintf(_('A confirmation code was sent '.
322 'to the IM address you added. '.
323 'You must approve %s for '.
324 'sending messages to you.'),
325 jabber_daemon_address());
327 $this->showForm($msg, true);
331 * Cancel a confirmation
333 * If a confirmation exists, cancel it.
338 function cancelConfirmation()
340 $jabber = $this->arg('jabber');
342 $confirm = $this->getConfirmation();
345 $this->showForm(_('No pending confirmation to cancel.'));
348 if ($confirm->address != $jabber) {
349 $this->showForm(_('That is the wrong IM address.'));
353 $result = $confirm->delete();
356 common_log_db_error($confirm, 'DELETE', __FILE__);
357 $this->serverError(_('Couldn\'t delete email confirmation.'));
361 $this->showForm(_('Confirmation cancelled.'), true);
367 * If the user has a confirmed address, remove it.
372 function removeAddress()
374 $user = common_current_user();
376 $jabber = $this->arg('jabber');
378 // Maybe an old tab open...?
380 if ($user->jabber != $jabber) {
381 $this->showForm(_('That is not your Jabber ID.'));
385 $user->query('BEGIN');
387 $original = clone($user);
389 $user->jabber = null;
391 $result = $user->updateKeys($original);
394 common_log_db_error($user, 'UPDATE', __FILE__);
395 $this->serverError(_('Couldn\'t update user.'));
398 $user->query('COMMIT');
400 // XXX: unsubscribe to the old address
402 $this->showForm(_('The address was removed.'), true);
406 * Does this Jabber ID exist?
408 * Checks if we already have another user with this address.
410 * @param string $jabber Address to check
412 * @return boolean whether the Jabber ID exists
415 function jabberExists($jabber)
417 $user = common_current_user();
419 $other = User::staticGet('jabber', $jabber);
424 return $other->id != $user->id;