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 _('You can send and receive notices through Jabber/GTalk [instant messages](%%doc.im%%). Configure your address and settings below.');
31 function show_form($msg=NULL, $success=false) {
32 $user = common_current_user();
33 $this->form_header(_('IM Settings'), $msg, $success);
34 common_element_start('form', array('method' => 'post',
37 common_local_url('imsettings')));
39 common_element('h2', NULL, _('Address'));
42 common_element_start('p');
43 common_element('span', 'address confirmed', $user->jabber);
44 common_element('span', 'input_instructions',
45 _('Current confirmed Jabber/GTalk address.'));
46 common_hidden('jabber', $user->jabber);
47 common_element_end('p');
48 common_submit('remove', _('Remove'));
50 $confirm = $this->get_confirmation();
52 common_element_start('p');
53 common_element('span', 'address unconfirmed', $confirm->address);
54 common_element('span', 'input_instructions',
55 sprintf(_('Awaiting confirmation on this address. Check your Jabber/GTalk account for a message with further instructions. (Did you add %s to your buddy list?)'), jabber_daemon_address()));
56 common_hidden('jabber', $confirm->address);
57 common_element_end('p');
58 common_submit('cancel', _('Cancel'));
60 common_input('jabber', _('IM Address'),
61 ($this->arg('jabber')) ? $this->arg('jabber') : NULL,
62 sprintf(_('Jabber or GTalk address, like "UserName@example.org". First, make sure to add %s to your buddy list in your IM client or on GTalk.'), jabber_daemon_address()));
63 common_submit('add', _('Add'));
67 common_element('h2', NULL, _('Preferences'));
69 common_checkbox('jabbernotify',
70 _('Send me notices through Jabber/GTalk.'),
72 common_checkbox('updatefrompresence',
73 _('Post a notice when my Jabber/GTalk status changes.'),
74 $user->updatefrompresence);
75 common_checkbox('jabberreplies',
76 _('Send me replies through Jabber/GTalk from people I\'m not subscribed to.'),
77 $user->jabberreplies);
78 common_submit('save', _('Save'));
80 common_element_end('form');
84 function get_confirmation() {
85 $user = common_current_user();
86 $confirm = new Confirm_address();
87 $confirm->user_id = $user->id;
88 $confirm->address_type = 'jabber';
89 if ($confirm->find(TRUE)) {
96 function handle_post() {
98 if ($this->arg('save')) {
99 $this->save_preferences();
100 } else if ($this->arg('add')) {
101 $this->add_address();
102 } else if ($this->arg('cancel')) {
103 $this->cancel_confirmation();
104 } else if ($this->arg('remove')) {
105 $this->remove_address();
107 $this->show_form(_('Unexpected form submission.'));
111 function save_preferences() {
113 $jabbernotify = $this->boolean('jabbernotify');
114 $updatefrompresence = $this->boolean('updatefrompresence');
115 $jabberreplies = $this->boolean('jabberreplies');
117 $user = common_current_user();
119 assert(!is_null($user)); # should already be checked
121 $user->query('BEGIN');
123 $original = clone($user);
125 $user->jabbernotify = $jabbernotify;
126 $user->updatefrompresence = $updatefrompresence;
127 $user->jabberreplies = $jabberreplies;
129 $result = $user->update($original);
131 if ($result === FALSE) {
132 common_log_db_error($user, 'UPDATE', __FILE__);
133 common_server_error(_('Couldn\'t update user.'));
137 $user->query('COMMIT');
139 $this->show_form(_('Preferences saved.'), true);
142 function add_address() {
144 $user = common_current_user();
146 $jabber = $this->trimmed('jabber');
151 $this->show_form(_('No Jabber ID.'));
155 $jabber = jabber_normalize_jid($jabber);
158 $this->show_form(_('Cannot normalize that Jabber ID'));
161 if (!jabber_valid_base_jid($jabber)) {
162 $this->show_form(_('Not a valid Jabber ID'));
164 } else if ($user->jabber == $jabber) {
165 $this->show_form(_('That is already your Jabber ID.'));
167 } else if ($this->jabber_exists($jabber)) {
168 $this->show_form(_('Jabber ID already belongs to another user.'));
172 $confirm = new Confirm_address();
173 $confirm->address = $jabber;
174 $confirm->address_type = 'jabber';
175 $confirm->user_id = $user->id;
176 $confirm->code = common_confirmation_code(64);
178 $result = $confirm->insert();
180 if ($result === FALSE) {
181 common_log_db_error($confirm, 'INSERT', __FILE__);
182 common_server_error(_('Couldn\'t insert confirmation code.'));
186 if (!common_config('queue', 'enabled')) {
187 jabber_confirm_address($confirm->code,
192 $msg = sprintf(_('A confirmation code was sent to the IM address you added. You must approve %s for sending messages to you.'), jabber_daemon_address());
194 $this->show_form($msg, TRUE);
197 function cancel_confirmation() {
198 $jabber = $this->arg('jabber');
199 $confirm = $this->get_confirmation();
201 $this->show_form(_('No pending confirmation to cancel.'));
204 if ($confirm->address != $jabber) {
205 $this->show_form(_('That is the wrong IM address.'));
209 $result = $confirm->delete();
212 common_log_db_error($confirm, 'DELETE', __FILE__);
213 $this->server_error(_('Couldn\'t delete email confirmation.'));
217 $this->show_form(_('Confirmation cancelled.'), TRUE);
220 function remove_address() {
222 $user = common_current_user();
223 $jabber = $this->arg('jabber');
225 # Maybe an old tab open...?
227 if ($user->jabber != $jabber) {
228 $this->show_form(_('That is not your Jabber ID.'));
232 $user->query('BEGIN');
233 $original = clone($user);
234 $user->jabber = NULL;
235 $result = $user->updateKeys($original);
237 common_log_db_error($user, 'UPDATE', __FILE__);
238 common_server_error(_('Couldn\'t update user.'));
241 $user->query('COMMIT');
243 # XXX: unsubscribe to the old address
245 $this->show_form(_('The address was removed.'), TRUE);
248 function jabber_exists($jabber) {
249 $user = common_current_user();
250 $other = User::staticGet('jabber', $jabber);
254 return $other->id != $user->id;