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')));
50 # too much common patterns here... abstractable?
51 common_input('jabber', _t('IM Address'),
52 ($this->arg('jabber')) ? $this->arg('jabber') : $user->jabber,
53 _t('Jabber or GTalk address, like "UserName@example.org"'));
54 common_checkbox('jabbernotify',
55 _t('Send me notices through Jabber/GTalk.'));
56 common_checkbox('updatefrompresence',
57 _t('Post a notice when my Jabber/GTalk status changes.'));
58 common_submit('submit', _t('Save'));
59 common_element_end('form');
63 function handle_post() {
65 $jabber = jabber_normalize_jid($this->trimmed('jabber'));
66 $jabbernotify = $this->boolean('jabbernotify');
67 $updatefrompresence = $this->boolean('updatefrompresence');
69 if (!jabber_valid_base_jid($jabber)) {
70 $this->show_form(_('Not a valid Jabber ID'));
72 } else if ($this->jabber_exists($jabber)) {
73 $this->show_form(_('Not a valid Jabber ID'));
79 $user = common_current_user();
81 assert(!is_null($user)); # should already be checked
83 $user->query('BEGIN');
85 $original = clone($user);
87 $user->jabbernotify = $jabbernotify;
88 $user->updatefrompresence = $updatefrompresence;
90 $result = $user->update($original); # For key columns
92 if ($result === FALSE) {
93 common_log_db_error($user, 'UPDATE', __FILE__);
94 common_server_error(_t('Couldnt update user.'));
98 $confirmation_sent = false;
100 if ($user->jabber != $jabber) {
103 $confirm = new Confirm_address();
104 $confirm->address = $jabber;
105 $confirm->address_type = 'jabber';
106 $confirm->user_id = $user->id;
107 $confirm->code = common_confirmation_code(64);
109 $result = $confirm->insert();
111 if ($result === FALSE) {
112 common_log_db_error($confirm, 'INSERT', __FILE__);
113 common_server_error(_t('Couldnt insert confirmation code.'));
117 # XXX: optionally queue for offline sending
119 jabber_confirm_address($confirm->code,
123 if ($result === FALSE) {
124 common_log_db_error($confirm, 'INSERT', __FILE__);
125 common_server_error(_t('Couldnt insert confirmation code.'));
129 $confirmation_sent = false;
131 # Clearing the ID is free
132 $user->jabber = NULL;
133 $result = $user->updateKeys($original);
134 if ($result === FALSE) {
135 common_log_db_error($user, 'UPDATE', __FILE__);
136 common_server_error(_t('Couldnt update user.'));
142 $user->query('COMMIT');
144 $msg = ($confirmation_sent) ?
145 _t('Settings saved. A confirmation code was ' .
146 ' sent to the IM address you added. ') :
147 _t('Settings saved.');
149 $this->show_form($msg, TRUE);
152 function jabber_exists($jabber) {
153 $user = common_current_user();
154 $other = User::staticGet('jabber', $jabber);
158 return $other->id != $user->id;