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/settingsaction.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 SettingsAction
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',
91 common_local_url('imsettings')));
92 $this->hidden('token', common_session_token());
94 $this->element('h2', null, _('Address'));
97 $this->elementStart('p');
98 $this->element('span', 'address confirmed', $user->jabber);
99 $this->element('span', 'input_instructions',
100 _('Current confirmed Jabber/GTalk address.'));
101 $this->hidden('jabber', $user->jabber);
102 $this->elementEnd('p');
103 $this->submit('remove', _('Remove'));
105 $confirm = $this->getConfirmation();
107 $this->elementStart('p');
108 $this->element('span', 'address unconfirmed', $confirm->address);
109 $this->element('span', 'input_instructions',
110 sprintf(_('Awaiting confirmation on this address. '.
111 'Check your Jabber/GTalk account for a '.
112 'message with further instructions. '.
113 '(Did you add %s to your buddy list?)'),
114 jabber_daemon_address()));
115 $this->hidden('jabber', $confirm->address);
116 $this->elementEnd('p');
117 $this->submit('cancel', _('Cancel'));
119 $this->input('jabber', _('IM Address'),
120 ($this->arg('jabber')) ? $this->arg('jabber') : null,
121 sprintf(_('Jabber or GTalk address, '.
122 'like "UserName@example.org". '.
123 'First, make sure to add %s to your '.
124 'buddy list in your IM client or on GTalk.'),
125 jabber_daemon_address()));
126 $this->submit('add', _('Add'));
130 $this->element('h2', null, _('Preferences'));
132 $this->checkbox('jabbernotify',
133 _('Send me notices through Jabber/GTalk.'),
134 $user->jabbernotify);
135 $this->checkbox('updatefrompresence',
136 _('Post a notice when my Jabber/GTalk status changes.'),
137 $user->updatefrompresence);
138 $this->checkbox('jabberreplies',
139 _('Send me replies through Jabber/GTalk '.
140 'from people I\'m not subscribed to.'),
141 $user->jabberreplies);
142 $this->checkbox('jabbermicroid',
143 _('Publish a MicroID for my Jabber/GTalk address.'),
144 $user->jabbermicroid);
145 $this->submit('save', _('Save'));
147 $this->elementEnd('form');
151 * Get a confirmation code for this user
153 * @return Confirm_address address object for this user
156 function getConfirmation()
158 $user = common_current_user();
160 $confirm = new Confirm_address();
162 $confirm->user_id = $user->id;
163 $confirm->address_type = 'jabber';
165 if ($confirm->find(true)) {
173 * Handle posts to this form
175 * Based on the button that was pressed, muxes out to other functions
176 * to do the actual task requested.
178 * All sub-functions reload the form with a message -- success or failure.
183 function handlePost()
186 $token = $this->trimmed('token');
187 if (!$token || $token != common_session_token()) {
188 $this->showForm(_('There was a problem with your session token. '.
189 'Try again, please.'));
193 if ($this->arg('save')) {
194 $this->savePreferences();
195 } else if ($this->arg('add')) {
197 } else if ($this->arg('cancel')) {
198 $this->cancelConfirmation();
199 } else if ($this->arg('remove')) {
200 $this->removeAddress();
202 $this->showForm(_('Unexpected form submission.'));
207 * Save user's Jabber preferences
209 * These are the checkboxes at the bottom of the page. They're used to
210 * set different settings
215 function savePreferences()
218 $jabbernotify = $this->boolean('jabbernotify');
219 $updatefrompresence = $this->boolean('updatefrompresence');
220 $jabberreplies = $this->boolean('jabberreplies');
221 $jabbermicroid = $this->boolean('jabbermicroid');
223 $user = common_current_user();
225 assert(!is_null($user)); // should already be checked
227 $user->query('BEGIN');
229 $original = clone($user);
231 $user->jabbernotify = $jabbernotify;
232 $user->updatefrompresence = $updatefrompresence;
233 $user->jabberreplies = $jabberreplies;
234 $user->jabbermicroid = $jabbermicroid;
236 $result = $user->update($original);
238 if ($result === false) {
239 common_log_db_error($user, 'UPDATE', __FILE__);
240 $this->serverError(_('Couldn\'t update user.'));
244 $user->query('COMMIT');
246 $this->showForm(_('Preferences saved.'), true);
250 * Sends a confirmation to the address given
252 * Stores a confirmation record and sends out a
253 * Jabber message with the confirmation info.
258 function addAddress()
260 $user = common_current_user();
262 $jabber = $this->trimmed('jabber');
267 $this->showForm(_('No Jabber ID.'));
271 $jabber = jabber_normalize_jid($jabber);
274 $this->showForm(_('Cannot normalize that Jabber ID'));
277 if (!jabber_valid_base_jid($jabber)) {
278 $this->showForm(_('Not a valid Jabber ID'));
280 } else if ($user->jabber == $jabber) {
281 $this->showForm(_('That is already your Jabber ID.'));
283 } else if ($this->jabberExists($jabber)) {
284 $this->showForm(_('Jabber ID already belongs to another user.'));
288 $confirm = new Confirm_address();
290 $confirm->address = $jabber;
291 $confirm->address_type = 'jabber';
292 $confirm->user_id = $user->id;
293 $confirm->code = common_confirmation_code(64);
295 $result = $confirm->insert();
297 if ($result === false) {
298 common_log_db_error($confirm, 'INSERT', __FILE__);
299 $this->serverError(_('Couldn\'t insert confirmation code.'));
303 if (!common_config('queue', 'enabled')) {
304 jabber_confirm_address($confirm->code,
309 $msg = sprintf(_('A confirmation code was sent '.
310 'to the IM address you added. '.
311 'You must approve %s for '.
312 'sending messages to you.'),
313 jabber_daemon_address());
315 $this->showForm($msg, true);
319 * Cancel a confirmation
321 * If a confirmation exists, cancel it.
326 function cancelConfirmation()
328 $jabber = $this->arg('jabber');
330 $confirm = $this->getConfirmation();
333 $this->showForm(_('No pending confirmation to cancel.'));
336 if ($confirm->address != $jabber) {
337 $this->showForm(_('That is the wrong IM address.'));
341 $result = $confirm->delete();
344 common_log_db_error($confirm, 'DELETE', __FILE__);
345 $this->serverError(_('Couldn\'t delete email confirmation.'));
349 $this->showForm(_('Confirmation cancelled.'), true);
355 * If the user has a confirmed address, remove it.
360 function removeAddress()
362 $user = common_current_user();
364 $jabber = $this->arg('jabber');
366 // Maybe an old tab open...?
368 if ($user->jabber != $jabber) {
369 $this->showForm(_('That is not your Jabber ID.'));
373 $user->query('BEGIN');
375 $original = clone($user);
377 $user->jabber = null;
379 $result = $user->updateKeys($original);
382 common_log_db_error($user, 'UPDATE', __FILE__);
383 $this->serverError(_('Couldn\'t update user.'));
386 $user->query('COMMIT');
388 // XXX: unsubscribe to the old address
390 $this->showForm(_('The address was removed.'), true);
394 * Does this Jabber ID exist?
396 * Checks if we already have another user with this address.
398 * @param string $jabber Address to check
400 * @return boolean whether the Jabber ID exists
403 function jabberExists($jabber)
405 $user = common_current_user();
407 $other = User::staticGet('jabber', $jabber);
412 return $other->id != $user->id;