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 show_top($arr) {
31 $this->message($msg, $success);
33 common_element('div', 'instructions',
34 _t('You can send and receive notices through '.
35 'Jabber/GTalk instant messages. Configure '.
36 'your address and settings below.'));
38 $this->settings_menu();
41 function show_form($msg=NULL, $success=false) {
42 $user = common_current_user();
43 common_show_header(_t('IM settings'), NULL, array($msg, $success),
44 array($this, 'show_top'));
46 common_element_start('form', array('method' => 'POST',
49 common_local_url('imsettings')));
51 common_element('h2', NULL, _t('Address'));
54 common_element_start('p');
55 common_element('span', 'address confirmed', $user->jabber);
56 common_element('span', 'input_instructions',
57 _t('Current confirmed Jabber/GTalk address.'));
58 common_hidden('jabber', $user->jabber);
59 common_element_end('p');
60 common_submit('remove', 'Remove');
62 $confirm = $this->get_confirmation();
64 common_element_start('p');
65 common_element('span', 'address unconfirmed', $confirm->address);
66 common_element('span', 'input_instructions',
67 _t('Awaiting confirmation on this address. Check your ' .
68 'Jabber/GTalk account for a message with further ' .
70 common_hidden('jabber', $confirm->address);
71 common_element_end('p');
72 common_submit('cancel', _t('Cancel'));
74 common_input('jabber', _t('IM Address'),
75 ($this->arg('jabber')) ? $this->arg('jabber') : NULL,
76 _t('Jabber or GTalk address, like "UserName@example.org"'));
77 common_submit('add', 'Add');
81 common_element('h2', NULL, _t('Preferences'));
83 common_checkbox('jabbernotify',
84 _t('Send me notices through Jabber/GTalk.'),
86 common_checkbox('updatefrompresence',
87 _t('Post a notice when my Jabber/GTalk status changes.'),
88 $user->updatefrompresence);
89 common_submit('save', _t('Save'));
91 common_element_end('form');
95 function get_confirmation() {
96 $user = common_current_user();
97 $confirm = new Confirm_address();
98 $confirm->user_id = $user->id;
99 $confirm->address_type = 'jabber';
100 if ($confirm->find(TRUE)) {
107 function handle_post() {
109 if ($this->arg('save')) {
110 $this->save_preferences();
111 } else if ($this->arg('add')) {
112 $this->add_address();
113 } else if ($this->arg('cancel')) {
114 $this->cancel_confirmation();
115 } else if ($this->arg('remove')) {
116 $this->remove_address();
118 $this->show_form(_t('Unexpected form submission.'));
122 function save_preferences() {
124 $jabbernotify = $this->boolean('jabbernotify');
125 $updatefrompresence = $this->boolean('updatefrompresence');
127 $user = common_current_user();
129 assert(!is_null($user)); # should already be checked
131 $user->query('BEGIN');
133 $original = clone($user);
135 $user->jabbernotify = $jabbernotify;
136 $user->updatefrompresence = $updatefrompresence;
138 $result = $user->update($original);
140 if ($result === FALSE) {
141 common_log_db_error($user, 'UPDATE', __FILE__);
142 common_server_error(_t('Couldnt update user.'));
146 $user->query('COMMIT');
148 $this->show_form(_t('Preferences saved.'), true);
151 function add_address() {
153 $user = common_current_user();
155 $jabber = $this->trimmed('jabber');
160 $this->show_form(_t('No Jabber ID.'));
164 $jabber = jabber_normalize_jid($jabber);
167 $this->show_form(_('Cannot normalize that Jabber ID'));
170 if (!jabber_valid_base_jid($jabber)) {
171 $this->show_form(_('Not a valid Jabber ID'));
173 } else if ($user->jabber == $jabber) {
174 $this->show_form(_('That is already your Jabber ID.'));
176 } else if ($this->jabber_exists($jabber)) {
177 $this->show_form(_('Jabber ID already belongs to another user.'));
181 $confirm = new Confirm_address();
182 $confirm->address = $jabber;
183 $confirm->address_type = 'jabber';
184 $confirm->user_id = $user->id;
185 $confirm->code = common_confirmation_code(64);
187 $result = $confirm->insert();
189 if ($result === FALSE) {
190 common_log_db_error($confirm, 'INSERT', __FILE__);
191 common_server_error(_t('Couldnt insert confirmation code.'));
195 # XXX: optionally queue for offline sending
197 if (!jabber_is_subscribed($jabber)) {
198 jabber_special_presence('subscribe', $jabber);
200 jabber_confirm_address($confirm->code,
205 $this->show_form(_t('A confirmation code was ' .
206 ' sent to the IM address you added. ' .
207 ' You must approve ' . jabber_daemon_address() .
208 ' for sending messages to you.'),
212 function cancel_confirmation() {
213 $jabber = $this->arg('jabber');
214 $confirm = $this->get_confirmation();
216 $this->show_form(_t('No pending confirmation to cancel.'));
219 if ($confirm->address != $jabber) {
220 $this->show_form(_t('That is the wrong IM address.'));
224 $result = $confirm->delete();
227 common_log_db_error($confirm, 'DELETE', __FILE__);
228 $this->server_error(_t('Couldn\'t delete email confirmation.'));
232 $this->show_form(_t('Confirmation cancelled.'), TRUE);
235 function remove_address() {
237 $user = common_current_user();
238 $jabber = $this->arg('jabber');
240 # Maybe an old tab open...?
242 if ($user->jabber != $jabber) {
243 $this->show_form(_t('That is not your Jabber ID.'));
247 $user->query('BEGIN');
248 $original = clone($user);
249 $user->jabber = NULL;
250 $result = $user->updateKeys($original);
252 common_log_db_error($user, 'UPDATE', __FILE__);
253 common_server_error(_t('Couldnt update user.'));
256 $user->query('COMMIT');
258 # Unsubscribe to the old address
260 jabber_special_presence('unsubscribe', $jabber);
262 $this->show_form(_t('The address was removed.'), TRUE);
265 function jabber_exists($jabber) {
266 $user = common_current_user();
267 $other = User::staticGet('jabber', $jabber);
271 return $other->id != $user->id;