]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/imsettings.php
56fb1475883b723999dfb8b4ea7a0631ea3c68e4
[quix0rs-gnu-social.git] / actions / imsettings.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/settingsaction.php');
23 require_once(INSTALLDIR.'/lib/jabber.php');
24
25 class ImsettingsAction extends SettingsAction {
26
27     function get_instructions()
28     {
29         return _('You can send and receive notices through Jabber/GTalk [instant messages](%%doc.im%%). Configure your address and settings below.');
30     }
31
32     function show_form($msg=null, $success=false)
33     {
34         $user = common_current_user();
35         $this->form_header(_('IM Settings'), $msg, $success);
36         common_element_start('form', array('method' => 'post',
37                                            'id' => 'imsettings',
38                                            'action' =>
39                                            common_local_url('imsettings')));
40         common_hidden('token', common_session_token());
41
42         common_element('h2', null, _('Address'));
43
44         if ($user->jabber) {
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'));
52         } else {
53             $confirm = $this->get_confirmation();
54             if ($confirm) {
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'));
62             } else {
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'));
67             }
68         }
69
70         common_element('h2', null, _('Preferences'));
71
72         common_checkbox('jabbernotify',
73                         _('Send me notices through Jabber/GTalk.'),
74                         $user->jabbernotify);
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'));
85
86         common_element_end('form');
87         common_show_footer();
88     }
89
90     function get_confirmation()
91     {
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)) {
97             return $confirm;
98         } else {
99             return null;
100         }
101     }
102
103     function handle_post()
104     {
105
106         # CSRF protection
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.'));
110             return;
111         }
112
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();
121         } else {
122             $this->show_form(_('Unexpected form submission.'));
123         }
124     }
125
126     function save_preferences()
127     {
128
129         $jabbernotify = $this->boolean('jabbernotify');
130         $updatefrompresence = $this->boolean('updatefrompresence');
131         $jabberreplies = $this->boolean('jabberreplies');
132         $jabbermicroid = $this->boolean('jabbermicroid');
133
134         $user = common_current_user();
135
136         assert(!is_null($user)); # should already be checked
137
138         $user->query('BEGIN');
139
140         $original = clone($user);
141
142         $user->jabbernotify = $jabbernotify;
143         $user->updatefrompresence = $updatefrompresence;
144         $user->jabberreplies = $jabberreplies;
145         $user->jabbermicroid = $jabbermicroid;
146
147         $result = $user->update($original);
148
149         if ($result === false) {
150             common_log_db_error($user, 'UPDATE', __FILE__);
151             common_server_error(_('Couldn\'t update user.'));
152             return;
153         }
154
155         $user->query('COMMIT');
156
157         $this->show_form(_('Preferences saved.'), true);
158     }
159
160     function add_address()
161     {
162
163         $user = common_current_user();
164
165         $jabber = $this->trimmed('jabber');
166
167         # Some validation
168
169         if (!$jabber) {
170             $this->show_form(_('No Jabber ID.'));
171             return;
172         }
173
174         $jabber = jabber_normalize_jid($jabber);
175
176         if (!$jabber) {
177             $this->show_form(_('Cannot normalize that Jabber ID'));
178             return;
179         }
180         if (!jabber_valid_base_jid($jabber)) {
181             $this->show_form(_('Not a valid Jabber ID'));
182             return;
183         } else if ($user->jabber == $jabber) {
184             $this->show_form(_('That is already your Jabber ID.'));
185             return;
186         } else if ($this->jabber_exists($jabber)) {
187             $this->show_form(_('Jabber ID already belongs to another user.'));
188             return;
189         }
190
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);
196
197         $result = $confirm->insert();
198
199         if ($result === false) {
200             common_log_db_error($confirm, 'INSERT', __FILE__);
201             common_server_error(_('Couldn\'t insert confirmation code.'));
202             return;
203         }
204
205         if (!common_config('queue', 'enabled')) {
206             jabber_confirm_address($confirm->code,
207                                    $user->nickname,
208                                    $jabber);
209         }
210
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());
212
213         $this->show_form($msg, true);
214     }
215
216     function cancel_confirmation()
217     {
218         $jabber = $this->arg('jabber');
219         $confirm = $this->get_confirmation();
220         if (!$confirm) {
221             $this->show_form(_('No pending confirmation to cancel.'));
222             return;
223         }
224         if ($confirm->address != $jabber) {
225             $this->show_form(_('That is the wrong IM address.'));
226             return;
227         }
228
229         $result = $confirm->delete();
230
231         if (!$result) {
232             common_log_db_error($confirm, 'DELETE', __FILE__);
233             $this->server_error(_('Couldn\'t delete email confirmation.'));
234             return;
235         }
236
237         $this->show_form(_('Confirmation cancelled.'), true);
238     }
239
240     function remove_address()
241     {
242
243         $user = common_current_user();
244         $jabber = $this->arg('jabber');
245
246         # Maybe an old tab open...?
247
248         if ($user->jabber != $jabber) {
249             $this->show_form(_('That is not your Jabber ID.'));
250             return;
251         }
252
253         $user->query('BEGIN');
254         $original = clone($user);
255         $user->jabber = null;
256         $result = $user->updateKeys($original);
257         if (!$result) {
258             common_log_db_error($user, 'UPDATE', __FILE__);
259             common_server_error(_('Couldn\'t update user.'));
260             return;
261         }
262         $user->query('COMMIT');
263
264         # XXX: unsubscribe to the old address
265
266         $this->show_form(_('The address was removed.'), true);
267     }
268
269     function jabber_exists($jabber)
270     {
271         $user = common_current_user();
272         $other = User::staticGet('jabber', $jabber);
273         if (!$other) {
274             return false;
275         } else {
276             return $other->id != $user->id;
277         }
278     }
279 }