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')));
38 common_hidden('token', common_session_token());
40 common_element('h2', NULL, _('Address'));
43 common_element_start('p');
44 common_element('span', 'address confirmed', $user->jabber);
45 common_element('span', 'input_instructions',
46 _('Current confirmed Jabber/GTalk address.'));
47 common_hidden('jabber', $user->jabber);
48 common_element_end('p');
49 common_submit('remove', _('Remove'));
51 $confirm = $this->get_confirmation();
53 common_element_start('p');
54 common_element('span', 'address unconfirmed', $confirm->address);
55 common_element('span', 'input_instructions',
56 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()));
57 common_hidden('jabber', $confirm->address);
58 common_element_end('p');
59 common_submit('cancel', _('Cancel'));
61 common_input('jabber', _('IM Address'),
62 ($this->arg('jabber')) ? $this->arg('jabber') : NULL,
63 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()));
64 common_submit('add', _('Add'));
68 common_element('h2', NULL, _('Preferences'));
70 common_checkbox('jabbernotify',
71 _('Send me notices through Jabber/GTalk.'),
73 common_checkbox('updatefrompresence',
74 _('Post a notice when my Jabber/GTalk status changes.'),
75 $user->updatefrompresence);
76 common_checkbox('jabberreplies',
77 _('Send me replies through Jabber/GTalk from people I\'m not subscribed to.'),
78 $user->jabberreplies);
79 common_checkbox('jabbermicroid',
80 _('Publish a MicroID for my Jabber/GTalk address.'),
81 $user->jabbermicroid);
82 common_submit('save', _('Save'));
84 common_element_end('form');
88 function get_confirmation() {
89 $user = common_current_user();
90 $confirm = new Confirm_address();
91 $confirm->user_id = $user->id;
92 $confirm->address_type = 'jabber';
93 if ($confirm->find(TRUE)) {
100 function handle_post() {
103 $token = $this->trimmed('token');
104 if (!$token || $token != common_session_token()) {
105 $this->show_form(_('There was a problem with your session token. Try again, please.'));
109 if ($this->arg('save')) {
110 $this->save_preferences();
111 } else if ($this->arg('add')) {
112 $this->add_address();
113 } else if ($this->arg('cancel')) {
114 $this->cancel_confirmation();
115 } else if ($this->arg('remove')) {
116 $this->remove_address();
118 $this->show_form(_('Unexpected form submission.'));
122 function save_preferences() {
124 $jabbernotify = $this->boolean('jabbernotify');
125 $updatefrompresence = $this->boolean('updatefrompresence');
126 $jabberreplies = $this->boolean('jabberreplies');
127 $jabbermicroid = $this->boolean('jabbermicroid');
129 $user = common_current_user();
131 assert(!is_null($user)); # should already be checked
133 $user->query('BEGIN');
135 $original = clone($user);
137 $user->jabbernotify = $jabbernotify;
138 $user->updatefrompresence = $updatefrompresence;
139 $user->jabberreplies = $jabberreplies;
140 $user->jabbermicroid = $jabbermicroid;
142 $result = $user->update($original);
144 if ($result === FALSE) {
145 common_log_db_error($user, 'UPDATE', __FILE__);
146 common_server_error(_('Couldn\'t update user.'));
150 $user->query('COMMIT');
152 $this->show_form(_('Preferences saved.'), true);
155 function add_address() {
157 $user = common_current_user();
159 $jabber = $this->trimmed('jabber');
164 $this->show_form(_('No Jabber ID.'));
168 $jabber = jabber_normalize_jid($jabber);
171 $this->show_form(_('Cannot normalize that Jabber ID'));
174 if (!jabber_valid_base_jid($jabber)) {
175 $this->show_form(_('Not a valid Jabber ID'));
177 } else if ($user->jabber == $jabber) {
178 $this->show_form(_('That is already your Jabber ID.'));
180 } else if ($this->jabber_exists($jabber)) {
181 $this->show_form(_('Jabber ID already belongs to another user.'));
185 $confirm = new Confirm_address();
186 $confirm->address = $jabber;
187 $confirm->address_type = 'jabber';
188 $confirm->user_id = $user->id;
189 $confirm->code = common_confirmation_code(64);
191 $result = $confirm->insert();
193 if ($result === FALSE) {
194 common_log_db_error($confirm, 'INSERT', __FILE__);
195 common_server_error(_('Couldn\'t insert confirmation code.'));
199 if (!common_config('queue', 'enabled')) {
200 jabber_confirm_address($confirm->code,
205 $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());
207 $this->show_form($msg, TRUE);
210 function cancel_confirmation() {
211 $jabber = $this->arg('jabber');
212 $confirm = $this->get_confirmation();
214 $this->show_form(_('No pending confirmation to cancel.'));
217 if ($confirm->address != $jabber) {
218 $this->show_form(_('That is the wrong IM address.'));
222 $result = $confirm->delete();
225 common_log_db_error($confirm, 'DELETE', __FILE__);
226 $this->server_error(_('Couldn\'t delete email confirmation.'));
230 $this->show_form(_('Confirmation cancelled.'), TRUE);
233 function remove_address() {
235 $user = common_current_user();
236 $jabber = $this->arg('jabber');
238 # Maybe an old tab open...?
240 if ($user->jabber != $jabber) {
241 $this->show_form(_('That is not your Jabber ID.'));
245 $user->query('BEGIN');
246 $original = clone($user);
247 $user->jabber = NULL;
248 $result = $user->updateKeys($original);
250 common_log_db_error($user, 'UPDATE', __FILE__);
251 common_server_error(_('Couldn\'t update user.'));
254 $user->query('COMMIT');
256 # XXX: unsubscribe to the old address
258 $this->show_form(_('The address was removed.'), TRUE);
261 function jabber_exists($jabber) {
262 $user = common_current_user();
263 $other = User::staticGet('jabber', $jabber);
267 return $other->id != $user->id;