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 = $this->trimmed('jabber');
66 $jabbernotify = $this->boolean('jabbernotify');
67 $updatefrompresence = $this->boolean('updatefrompresence');
72 $jabber = jabber_normalize_jid($jabber);
74 $this->show_form(_('Cannot normalize that Jabber ID'));
77 if (!jabber_valid_base_jid($jabber)) {
78 $this->show_form(_('Not a valid Jabber ID'));
80 } else if ($this->jabber_exists($jabber)) {
81 $this->show_form(_('Jabber ID already belongs to another user.'));
86 $user = common_current_user();
88 assert(!is_null($user)); # should already be checked
90 $user->query('BEGIN');
92 $original = clone($user);
94 $user->jabbernotify = $jabbernotify;
95 $user->updatefrompresence = $updatefrompresence;
97 $result = $user->update($original); # For key columns
99 if ($result === FALSE) {
100 common_log_db_error($user, 'UPDATE', __FILE__);
101 common_server_error(_t('Couldnt update user.'));
105 $confirmation_sent = false;
107 if ($user->jabber != $jabber) {
110 $confirm = new Confirm_address();
111 $confirm->address = $jabber;
112 $confirm->address_type = 'jabber';
113 $confirm->user_id = $user->id;
114 $confirm->code = common_confirmation_code(64);
116 $result = $confirm->insert();
118 if ($result === FALSE) {
119 common_log_db_error($confirm, 'INSERT', __FILE__);
120 common_server_error(_t('Couldnt insert confirmation code.'));
124 # XXX: optionally queue for offline sending
126 jabber_confirm_address($confirm->code,
130 if ($result === FALSE) {
131 common_log_db_error($confirm, 'INSERT', __FILE__);
132 common_server_error(_t('Couldnt insert confirmation code.'));
136 $confirmation_sent = false;
138 # Clearing the ID is free
139 $user->jabber = NULL;
140 $result = $user->updateKeys($original);
141 if ($result === FALSE) {
142 common_log_db_error($user, 'UPDATE', __FILE__);
143 common_server_error(_t('Couldnt update user.'));
149 $user->query('COMMIT');
151 $msg = ($confirmation_sent) ?
152 _t('Settings saved. A confirmation code was ' .
153 ' sent to the IM address you added. ') :
154 _t('Settings saved.');
156 $this->show_form($msg, TRUE);
159 function jabber_exists($jabber) {
160 $user = common_current_user();
161 $other = User::staticGet('jabber', $jabber);
165 return $other->id != $user->id;