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 _t('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(_t('IM Settings'), $msg, $success);
36 common_element_start('form', array('method' => 'post',
39 common_local_url('imsettings')));
41 common_element('h2', NULL, _t('Address'));
44 common_element_start('p');
45 common_element('span', 'address confirmed', $user->jabber);
46 common_element('span', 'input_instructions',
47 _t('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 _t('Awaiting confirmation on this address. Check your ' .
58 'Jabber/GTalk account for a message with further ' .
59 'instructions. (Did you add ' . jabber_daemon_address() .
60 ' to your buddy list?)'));
61 common_hidden('jabber', $confirm->address);
62 common_element_end('p');
63 common_submit('cancel', _t('Cancel'));
65 common_input('jabber', _t('IM Address'),
66 ($this->arg('jabber')) ? $this->arg('jabber') : NULL,
67 _t('Jabber or GTalk address, like "UserName@example.org". ' .
68 'First, make sure to add ' . jabber_daemon_address() .
69 ' to your buddy list in your IM client or on GTalk.'));
70 common_submit('add', 'Add');
74 common_element('h2', NULL, _t('Preferences'));
76 common_checkbox('jabbernotify',
77 _t('Send me notices through Jabber/GTalk.'),
79 common_checkbox('updatefrompresence',
80 _t('Post a notice when my Jabber/GTalk status changes.'),
81 $user->updatefrompresence);
82 common_submit('save', _t('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(_t('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(_t('Couldnt update user.'));
139 $user->query('COMMIT');
141 $this->show_form(_t('Preferences saved.'), true);
144 function add_address() {
146 $user = common_current_user();
148 $jabber = $this->trimmed('jabber');
153 $this->show_form(_t('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(_t('Couldnt insert confirmation code.'));
188 if (!common_config('queue', 'enabled')) {
189 jabber_confirm_address($confirm->code,
196 $msg = 'A confirmation code was sent to the IM address you added. ' .
197 ' You must approve ' . jabber_daemon_address() .
198 ' for sending messages to you.';
200 $this->show_form($msg, TRUE);
203 function cancel_confirmation() {
204 $jabber = $this->arg('jabber');
205 $confirm = $this->get_confirmation();
207 $this->show_form(_t('No pending confirmation to cancel.'));
210 if ($confirm->address != $jabber) {
211 $this->show_form(_t('That is the wrong IM address.'));
215 $result = $confirm->delete();
218 common_log_db_error($confirm, 'DELETE', __FILE__);
219 $this->server_error(_t('Couldn\'t delete email confirmation.'));
223 $this->show_form(_t('Confirmation cancelled.'), TRUE);
226 function remove_address() {
228 $user = common_current_user();
229 $jabber = $this->arg('jabber');
231 # Maybe an old tab open...?
233 if ($user->jabber != $jabber) {
234 $this->show_form(_t('That is not your Jabber ID.'));
238 $user->query('BEGIN');
239 $original = clone($user);
240 $user->jabber = NULL;
241 $result = $user->updateKeys($original);
243 common_log_db_error($user, 'UPDATE', __FILE__);
244 common_server_error(_t('Couldnt update user.'));
247 $user->query('COMMIT');
249 # XXX: unsubscribe to the old address
251 $this->show_form(_t('The address was removed.'), TRUE);
254 function jabber_exists($jabber) {
255 $user = common_current_user();
256 $other = User::staticGet('jabber', $jabber);
260 return $other->id != $user->id;