3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('LACONICA')) { exit(1); }
22 require_once(INSTALLDIR.'/lib/settingsaction.php');
23 require_once(INSTALLDIR.'/lib/jabber.php');
25 class ImsettingsAction extends SettingsAction {
27 function get_instructions() {
28 return _('You can send and receive notices through '.
29 'Jabber/GTalk [instant messages](%%doc.im%%). Configure '.
30 'your address and settings below.');
33 function show_form($msg=NULL, $success=false) {
34 $user = common_current_user();
35 $this->form_header(_('IM Settings'), $msg, $success);
36 common_element_start('form', array('method' => 'post',
39 common_local_url('imsettings')));
41 common_element('h2', NULL, _('Address'));
44 common_element_start('p');
45 common_element('span', 'address confirmed', $user->jabber);
46 common_element('span', 'input_instructions',
47 _('Current confirmed Jabber/GTalk address.'));
48 common_hidden('jabber', $user->jabber);
49 common_element_end('p');
50 common_submit('remove', _('Remove'));
52 $confirm = $this->get_confirmation();
54 common_element_start('p');
55 common_element('span', 'address unconfirmed', $confirm->address);
56 common_element('span', 'input_instructions',
57 sprintf(_('Awaiting confirmation on this address. Check your ' .
58 'Jabber/GTalk account for a message with further ' .
59 'instructions. (Did you add %s to your buddy list?)',
60 jabber_daemon_address())));
61 common_hidden('jabber', $confirm->address);
62 common_element_end('p');
63 common_submit('cancel', _('Cancel'));
65 common_input('jabber', _('IM Address'),
66 ($this->arg('jabber')) ? $this->arg('jabber') : NULL,
67 sprintf(_('Jabber or GTalk address, like "UserName@example.org". ' .
68 'First, make sure to add %s' .
69 ' to your buddy list in your IM client or on GTalk.'), jabber_daemon_address()));
70 common_submit('add', _('Add'));
74 common_element('h2', NULL, _('Preferences'));
76 common_checkbox('jabbernotify',
77 _('Send me notices through Jabber/GTalk.'),
79 common_checkbox('updatefrompresence',
80 _('Post a notice when my Jabber/GTalk status changes.'),
81 $user->updatefrompresence);
82 common_submit('save', _('Save'));
84 common_element_end('form');
88 function get_confirmation() {
89 $user = common_current_user();
90 $confirm = new Confirm_address();
91 $confirm->user_id = $user->id;
92 $confirm->address_type = 'jabber';
93 if ($confirm->find(TRUE)) {
100 function handle_post() {
102 if ($this->arg('save')) {
103 $this->save_preferences();
104 } else if ($this->arg('add')) {
105 $this->add_address();
106 } else if ($this->arg('cancel')) {
107 $this->cancel_confirmation();
108 } else if ($this->arg('remove')) {
109 $this->remove_address();
111 $this->show_form(_('Unexpected form submission.'));
115 function save_preferences() {
117 $jabbernotify = $this->boolean('jabbernotify');
118 $updatefrompresence = $this->boolean('updatefrompresence');
120 $user = common_current_user();
122 assert(!is_null($user)); # should already be checked
124 $user->query('BEGIN');
126 $original = clone($user);
128 $user->jabbernotify = $jabbernotify;
129 $user->updatefrompresence = $updatefrompresence;
131 $result = $user->update($original);
133 if ($result === FALSE) {
134 common_log_db_error($user, 'UPDATE', __FILE__);
135 common_server_error(_('Couldnt update user.'));
139 $user->query('COMMIT');
141 $this->show_form(_('Preferences saved.'), true);
144 function add_address() {
146 $user = common_current_user();
148 $jabber = $this->trimmed('jabber');
153 $this->show_form(_('No Jabber ID.'));
157 $jabber = jabber_normalize_jid($jabber);
160 $this->show_form(_('Cannot normalize that Jabber ID'));
163 if (!jabber_valid_base_jid($jabber)) {
164 $this->show_form(_('Not a valid Jabber ID'));
166 } else if ($user->jabber == $jabber) {
167 $this->show_form(_('That is already your Jabber ID.'));
169 } else if ($this->jabber_exists($jabber)) {
170 $this->show_form(_('Jabber ID already belongs to another user.'));
174 $confirm = new Confirm_address();
175 $confirm->address = $jabber;
176 $confirm->address_type = 'jabber';
177 $confirm->user_id = $user->id;
178 $confirm->code = common_confirmation_code(64);
180 $result = $confirm->insert();
182 if ($result === FALSE) {
183 common_log_db_error($confirm, 'INSERT', __FILE__);
184 common_server_error(_('Couldnt insert confirmation code.'));
188 if (!common_config('queue', 'enabled')) {
189 jabber_confirm_address($confirm->code,
194 $msg = sprintf(_('A confirmation code was sent to the IM address you added. ' .
195 'You must approve %s for sending messages to you.'), jabber_daemon_address());
197 $this->show_form($msg, TRUE);
200 function cancel_confirmation() {
201 $jabber = $this->arg('jabber');
202 $confirm = $this->get_confirmation();
204 $this->show_form(_('No pending confirmation to cancel.'));
207 if ($confirm->address != $jabber) {
208 $this->show_form(_('That is the wrong IM address.'));
212 $result = $confirm->delete();
215 common_log_db_error($confirm, 'DELETE', __FILE__);
216 $this->server_error(_('Couldn\'t delete email confirmation.'));
220 $this->show_form(_('Confirmation cancelled.'), TRUE);
223 function remove_address() {
225 $user = common_current_user();
226 $jabber = $this->arg('jabber');
228 # Maybe an old tab open...?
230 if ($user->jabber != $jabber) {
231 $this->show_form(_('That is not your Jabber ID.'));
235 $user->query('BEGIN');
236 $original = clone($user);
237 $user->jabber = NULL;
238 $result = $user->updateKeys($original);
240 common_log_db_error($user, 'UPDATE', __FILE__);
241 common_server_error(_('Couldnt update user.'));
244 $user->query('COMMIT');
246 # XXX: unsubscribe to the old address
248 $this->show_form(_('The address was removed.'), TRUE);
251 function jabber_exists($jabber) {
252 $user = common_current_user();
253 $other = User::staticGet('jabber', $jabber);
257 return $other->id != $user->id;