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
28 function get_instructions()
30 return _('You can send and receive notices through Jabber/GTalk [instant messages](%%doc.im%%). Configure your address and settings below.');
33 function show_form($msg=null, $success=false)
35 $user = common_current_user();
36 $this->form_header(_('IM Settings'), $msg, $success);
37 common_element_start('form', array('method' => 'post',
40 common_local_url('imsettings')));
41 common_hidden('token', common_session_token());
43 common_element('h2', null, _('Address'));
46 common_element_start('p');
47 common_element('span', 'address confirmed', $user->jabber);
48 common_element('span', 'input_instructions',
49 _('Current confirmed Jabber/GTalk address.'));
50 common_hidden('jabber', $user->jabber);
51 common_element_end('p');
52 common_submit('remove', _('Remove'));
54 $confirm = $this->get_confirmation();
56 common_element_start('p');
57 common_element('span', 'address unconfirmed', $confirm->address);
58 common_element('span', 'input_instructions',
59 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()));
60 common_hidden('jabber', $confirm->address);
61 common_element_end('p');
62 common_submit('cancel', _('Cancel'));
64 common_input('jabber', _('IM Address'),
65 ($this->arg('jabber')) ? $this->arg('jabber') : null,
66 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()));
67 common_submit('add', _('Add'));
71 common_element('h2', null, _('Preferences'));
73 common_checkbox('jabbernotify',
74 _('Send me notices through Jabber/GTalk.'),
76 common_checkbox('updatefrompresence',
77 _('Post a notice when my Jabber/GTalk status changes.'),
78 $user->updatefrompresence);
79 common_checkbox('jabberreplies',
80 _('Send me replies through Jabber/GTalk from people I\'m not subscribed to.'),
81 $user->jabberreplies);
82 common_checkbox('jabbermicroid',
83 _('Publish a MicroID for my Jabber/GTalk address.'),
84 $user->jabbermicroid);
85 common_submit('save', _('Save'));
87 common_element_end('form');
91 function get_confirmation()
93 $user = common_current_user();
94 $confirm = new Confirm_address();
95 $confirm->user_id = $user->id;
96 $confirm->address_type = 'jabber';
97 if ($confirm->find(true)) {
104 function handle_post()
108 $token = $this->trimmed('token');
109 if (!$token || $token != common_session_token()) {
110 $this->show_form(_('There was a problem with your session token. Try again, please.'));
114 if ($this->arg('save')) {
115 $this->save_preferences();
116 } else if ($this->arg('add')) {
117 $this->add_address();
118 } else if ($this->arg('cancel')) {
119 $this->cancel_confirmation();
120 } else if ($this->arg('remove')) {
121 $this->remove_address();
123 $this->show_form(_('Unexpected form submission.'));
127 function save_preferences()
130 $jabbernotify = $this->boolean('jabbernotify');
131 $updatefrompresence = $this->boolean('updatefrompresence');
132 $jabberreplies = $this->boolean('jabberreplies');
133 $jabbermicroid = $this->boolean('jabbermicroid');
135 $user = common_current_user();
137 assert(!is_null($user)); # should already be checked
139 $user->query('BEGIN');
141 $original = clone($user);
143 $user->jabbernotify = $jabbernotify;
144 $user->updatefrompresence = $updatefrompresence;
145 $user->jabberreplies = $jabberreplies;
146 $user->jabbermicroid = $jabbermicroid;
148 $result = $user->update($original);
150 if ($result === false) {
151 common_log_db_error($user, 'UPDATE', __FILE__);
152 common_server_error(_('Couldn\'t update user.'));
156 $user->query('COMMIT');
158 $this->show_form(_('Preferences saved.'), true);
161 function add_address()
164 $user = common_current_user();
166 $jabber = $this->trimmed('jabber');
171 $this->show_form(_('No Jabber ID.'));
175 $jabber = jabber_normalize_jid($jabber);
178 $this->show_form(_('Cannot normalize that Jabber ID'));
181 if (!jabber_valid_base_jid($jabber)) {
182 $this->show_form(_('Not a valid Jabber ID'));
184 } else if ($user->jabber == $jabber) {
185 $this->show_form(_('That is already your Jabber ID.'));
187 } else if ($this->jabber_exists($jabber)) {
188 $this->show_form(_('Jabber ID already belongs to another user.'));
192 $confirm = new Confirm_address();
193 $confirm->address = $jabber;
194 $confirm->address_type = 'jabber';
195 $confirm->user_id = $user->id;
196 $confirm->code = common_confirmation_code(64);
198 $result = $confirm->insert();
200 if ($result === false) {
201 common_log_db_error($confirm, 'INSERT', __FILE__);
202 common_server_error(_('Couldn\'t insert confirmation code.'));
206 if (!common_config('queue', 'enabled')) {
207 jabber_confirm_address($confirm->code,
212 $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());
214 $this->show_form($msg, true);
217 function cancel_confirmation()
219 $jabber = $this->arg('jabber');
220 $confirm = $this->get_confirmation();
222 $this->show_form(_('No pending confirmation to cancel.'));
225 if ($confirm->address != $jabber) {
226 $this->show_form(_('That is the wrong IM address.'));
230 $result = $confirm->delete();
233 common_log_db_error($confirm, 'DELETE', __FILE__);
234 $this->server_error(_('Couldn\'t delete email confirmation.'));
238 $this->show_form(_('Confirmation cancelled.'), true);
241 function remove_address()
244 $user = common_current_user();
245 $jabber = $this->arg('jabber');
247 # Maybe an old tab open...?
249 if ($user->jabber != $jabber) {
250 $this->show_form(_('That is not your Jabber ID.'));
254 $user->query('BEGIN');
255 $original = clone($user);
256 $user->jabber = null;
257 $result = $user->updateKeys($original);
259 common_log_db_error($user, 'UPDATE', __FILE__);
260 common_server_error(_('Couldn\'t update user.'));
263 $user->query('COMMIT');
265 # XXX: unsubscribe to the old address
267 $this->show_form(_('The address was removed.'), true);
270 function jabber_exists($jabber)
272 $user = common_current_user();
273 $other = User::staticGet('jabber', $jabber);
277 return $other->id != $user->id;