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 ' .
69 'instructions. (Did you add ' . jabber_daemon_address() .
70 'to your buddy list?)'));
71 common_hidden('jabber', $confirm->address);
72 common_element_end('p');
73 common_submit('cancel', _t('Cancel'));
75 common_input('jabber', _t('IM Address'),
76 ($this->arg('jabber')) ? $this->arg('jabber') : NULL,
77 _t('Jabber or GTalk address, like "UserName@example.org". ' .
78 'First, make sure to add ' . jabber_daemon_address() .
79 ' to your buddy list in your IM client or on GTalk.'));
80 common_submit('add', 'Add');
84 common_element('h2', NULL, _t('Preferences'));
86 common_checkbox('jabbernotify',
87 _t('Send me notices through Jabber/GTalk.'),
89 common_checkbox('updatefrompresence',
90 _t('Post a notice when my Jabber/GTalk status changes.'),
91 $user->updatefrompresence);
92 common_submit('save', _t('Save'));
94 common_element_end('form');
98 function get_confirmation() {
99 $user = common_current_user();
100 $confirm = new Confirm_address();
101 $confirm->user_id = $user->id;
102 $confirm->address_type = 'jabber';
103 if ($confirm->find(TRUE)) {
110 function handle_post() {
112 if ($this->arg('save')) {
113 $this->save_preferences();
114 } else if ($this->arg('add')) {
115 $this->add_address();
116 } else if ($this->arg('cancel')) {
117 $this->cancel_confirmation();
118 } else if ($this->arg('remove')) {
119 $this->remove_address();
121 $this->show_form(_t('Unexpected form submission.'));
125 function save_preferences() {
127 $jabbernotify = $this->boolean('jabbernotify');
128 $updatefrompresence = $this->boolean('updatefrompresence');
130 $user = common_current_user();
132 assert(!is_null($user)); # should already be checked
134 $user->query('BEGIN');
136 $original = clone($user);
138 $user->jabbernotify = $jabbernotify;
139 $user->updatefrompresence = $updatefrompresence;
141 $result = $user->update($original);
143 if ($result === FALSE) {
144 common_log_db_error($user, 'UPDATE', __FILE__);
145 common_server_error(_t('Couldnt update user.'));
149 $user->query('COMMIT');
151 $this->show_form(_t('Preferences saved.'), true);
154 function add_address() {
156 $user = common_current_user();
158 $jabber = $this->trimmed('jabber');
163 $this->show_form(_t('No Jabber ID.'));
167 $jabber = jabber_normalize_jid($jabber);
170 $this->show_form(_('Cannot normalize that Jabber ID'));
173 if (!jabber_valid_base_jid($jabber)) {
174 $this->show_form(_('Not a valid Jabber ID'));
176 } else if ($user->jabber == $jabber) {
177 $this->show_form(_('That is already your Jabber ID.'));
179 } else if ($this->jabber_exists($jabber)) {
180 $this->show_form(_('Jabber ID already belongs to another user.'));
184 $confirm = new Confirm_address();
185 $confirm->address = $jabber;
186 $confirm->address_type = 'jabber';
187 $confirm->user_id = $user->id;
188 $confirm->code = common_confirmation_code(64);
190 $result = $confirm->insert();
192 if ($result === FALSE) {
193 common_log_db_error($confirm, 'INSERT', __FILE__);
194 common_server_error(_t('Couldnt insert confirmation code.'));
198 # XXX: queue for offline sending
200 jabber_confirm_address($confirm->code,
206 $msg = 'A confirmation code was sent to the IM address you added. ' .
207 ' You must approve ' . jabber_daemon_address() .
208 ' for sending messages to you.';
210 $this->show_form($msg, TRUE);
213 function cancel_confirmation() {
214 $jabber = $this->arg('jabber');
215 $confirm = $this->get_confirmation();
217 $this->show_form(_t('No pending confirmation to cancel.'));
220 if ($confirm->address != $jabber) {
221 $this->show_form(_t('That is the wrong IM address.'));
225 $result = $confirm->delete();
228 common_log_db_error($confirm, 'DELETE', __FILE__);
229 $this->server_error(_t('Couldn\'t delete email confirmation.'));
233 $this->show_form(_t('Confirmation cancelled.'), TRUE);
236 function remove_address() {
238 $user = common_current_user();
239 $jabber = $this->arg('jabber');
241 # Maybe an old tab open...?
243 if ($user->jabber != $jabber) {
244 $this->show_form(_t('That is not your Jabber ID.'));
248 $user->query('BEGIN');
249 $original = clone($user);
250 $user->jabber = NULL;
251 $result = $user->updateKeys($original);
253 common_log_db_error($user, 'UPDATE', __FILE__);
254 common_server_error(_t('Couldnt update user.'));
257 $user->query('COMMIT');
259 # Unsubscribe to the old address
261 jabber_special_presence('unsubscribe', $jabber);
263 $this->show_form(_t('The address was removed.'), TRUE);
266 function jabber_exists($jabber) {
267 $user = common_current_user();
268 $other = User::staticGet('jabber', $jabber);
272 return $other->id != $user->id;