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()
29 return _('You can send and receive notices through Jabber/GTalk [instant messages](%%doc.im%%). Configure your address and settings below.');
32 function show_form($msg=null, $success=false)
34 $user = common_current_user();
35 $this->form_header(_('IM Settings'), $msg, $success);
36 common_element_start('form', array('method' => 'post',
39 common_local_url('imsettings')));
40 common_hidden('token', common_session_token());
42 common_element('h2', null, _('Address'));
45 common_element_start('p');
46 common_element('span', 'address confirmed', $user->jabber);
47 common_element('span', 'input_instructions',
48 _('Current confirmed Jabber/GTalk address.'));
49 common_hidden('jabber', $user->jabber);
50 common_element_end('p');
51 common_submit('remove', _('Remove'));
53 $confirm = $this->get_confirmation();
55 common_element_start('p');
56 common_element('span', 'address unconfirmed', $confirm->address);
57 common_element('span', 'input_instructions',
58 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()));
59 common_hidden('jabber', $confirm->address);
60 common_element_end('p');
61 common_submit('cancel', _('Cancel'));
63 common_input('jabber', _('IM Address'),
64 ($this->arg('jabber')) ? $this->arg('jabber') : null,
65 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()));
66 common_submit('add', _('Add'));
70 common_element('h2', null, _('Preferences'));
72 common_checkbox('jabbernotify',
73 _('Send me notices through Jabber/GTalk.'),
75 common_checkbox('updatefrompresence',
76 _('Post a notice when my Jabber/GTalk status changes.'),
77 $user->updatefrompresence);
78 common_checkbox('jabberreplies',
79 _('Send me replies through Jabber/GTalk from people I\'m not subscribed to.'),
80 $user->jabberreplies);
81 common_checkbox('jabbermicroid',
82 _('Publish a MicroID for my Jabber/GTalk address.'),
83 $user->jabbermicroid);
84 common_submit('save', _('Save'));
86 common_element_end('form');
90 function get_confirmation()
92 $user = common_current_user();
93 $confirm = new Confirm_address();
94 $confirm->user_id = $user->id;
95 $confirm->address_type = 'jabber';
96 if ($confirm->find(TRUE)) {
103 function handle_post()
107 $token = $this->trimmed('token');
108 if (!$token || $token != common_session_token()) {
109 $this->show_form(_('There was a problem with your session token. Try again, please.'));
113 if ($this->arg('save')) {
114 $this->save_preferences();
115 } else if ($this->arg('add')) {
116 $this->add_address();
117 } else if ($this->arg('cancel')) {
118 $this->cancel_confirmation();
119 } else if ($this->arg('remove')) {
120 $this->remove_address();
122 $this->show_form(_('Unexpected form submission.'));
126 function save_preferences()
129 $jabbernotify = $this->boolean('jabbernotify');
130 $updatefrompresence = $this->boolean('updatefrompresence');
131 $jabberreplies = $this->boolean('jabberreplies');
132 $jabbermicroid = $this->boolean('jabbermicroid');
134 $user = common_current_user();
136 assert(!is_null($user)); # should already be checked
138 $user->query('BEGIN');
140 $original = clone($user);
142 $user->jabbernotify = $jabbernotify;
143 $user->updatefrompresence = $updatefrompresence;
144 $user->jabberreplies = $jabberreplies;
145 $user->jabbermicroid = $jabbermicroid;
147 $result = $user->update($original);
149 if ($result === FALSE) {
150 common_log_db_error($user, 'UPDATE', __FILE__);
151 common_server_error(_('Couldn\'t update user.'));
155 $user->query('COMMIT');
157 $this->show_form(_('Preferences saved.'), true);
160 function add_address()
163 $user = common_current_user();
165 $jabber = $this->trimmed('jabber');
170 $this->show_form(_('No Jabber ID.'));
174 $jabber = jabber_normalize_jid($jabber);
177 $this->show_form(_('Cannot normalize that Jabber ID'));
180 if (!jabber_valid_base_jid($jabber)) {
181 $this->show_form(_('Not a valid Jabber ID'));
183 } else if ($user->jabber == $jabber) {
184 $this->show_form(_('That is already your Jabber ID.'));
186 } else if ($this->jabber_exists($jabber)) {
187 $this->show_form(_('Jabber ID already belongs to another user.'));
191 $confirm = new Confirm_address();
192 $confirm->address = $jabber;
193 $confirm->address_type = 'jabber';
194 $confirm->user_id = $user->id;
195 $confirm->code = common_confirmation_code(64);
197 $result = $confirm->insert();
199 if ($result === FALSE) {
200 common_log_db_error($confirm, 'INSERT', __FILE__);
201 common_server_error(_('Couldn\'t insert confirmation code.'));
205 if (!common_config('queue', 'enabled')) {
206 jabber_confirm_address($confirm->code,
211 $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());
213 $this->show_form($msg, TRUE);
216 function cancel_confirmation()
218 $jabber = $this->arg('jabber');
219 $confirm = $this->get_confirmation();
221 $this->show_form(_('No pending confirmation to cancel.'));
224 if ($confirm->address != $jabber) {
225 $this->show_form(_('That is the wrong IM address.'));
229 $result = $confirm->delete();
232 common_log_db_error($confirm, 'DELETE', __FILE__);
233 $this->server_error(_('Couldn\'t delete email confirmation.'));
237 $this->show_form(_('Confirmation cancelled.'), TRUE);
240 function remove_address()
243 $user = common_current_user();
244 $jabber = $this->arg('jabber');
246 # Maybe an old tab open...?
248 if ($user->jabber != $jabber) {
249 $this->show_form(_('That is not your Jabber ID.'));
253 $user->query('BEGIN');
254 $original = clone($user);
255 $user->jabber = null;
256 $result = $user->updateKeys($original);
258 common_log_db_error($user, 'UPDATE', __FILE__);
259 common_server_error(_('Couldn\'t update user.'));
262 $user->query('COMMIT');
264 # XXX: unsubscribe to the old address
266 $this->show_form(_('The address was removed.'), TRUE);
269 function jabber_exists($jabber)
271 $user = common_current_user();
272 $other = User::staticGet('jabber', $jabber);
276 return $other->id != $user->id;