]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/imsettings.php
6e2c3c11b27197917b01e8dd2ee0d49bcb9a60b0
[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 show_top($arr) {
28                 $msg = $arr[0];
29                 $success = $arr[1];
30                 if ($msg) {
31                         $this->message($msg, $success);
32                 } else {
33                         common_element('div', 'instructions',
34                                                    _t('You can send and receive notices through '.
35                                                           'Jabber/GTalk instant messages. Configure '.
36                                                           'your address and settings below.'));
37                 }
38                 $this->settings_menu();
39         }
40
41         function show_form($msg=NULL, $success=false) {
42                 $user = common_current_user();
43                 common_show_header(_t('IM settings'), NULL, array($msg, $success),
44                                                    array($this, 'show_top'));
45
46                 common_element_start('form', array('method' => 'POST',
47                                                                                    'id' => 'imsettings',
48                                                                                    'action' =>
49                                                                                    common_local_url('imsettings')));
50
51                 common_element('h2', NULL, _t('Address'));
52
53                 if ($user->jabber) {
54                         common_element_start('p');
55                         common_element('span', 'address confirmed', $user->jabber);
56                         common_element('span', 'input_instructions',
57                                        _t('Current confirmed Jabber/GTalk address.'));
58                         common_hidden('jabber', $user->jabber);
59                         common_element_end('p');
60                         common_submit('remove', 'Remove');
61                 } else {
62                         $confirm = $this->get_confirmation();
63                         if ($confirm) {
64                                 common_element_start('p');
65                                 common_element('span', 'address unconfirmed', $confirm->address);
66                                 common_element('span', 'input_instructions',
67                                              _t('Awaiting confirmation on this address. Check your ' .
68                                                 'Jabber/GTalk account for a message with further ' .
69                                                 'instructions. (Did you add '  . jabber_daemon_address() . 
70                                                                 'to your buddy list?)'));
71                                 common_hidden('jabber', $confirm->address);
72                                 common_element_end('p');
73                                 common_submit('cancel', _t('Cancel'));
74                         } else {
75                                 common_input('jabber', _t('IM Address'),
76                                                         ($this->arg('jabber')) ? $this->arg('jabber') : NULL,
77                                                  _t('Jabber or GTalk address, like "UserName@example.org". ' .
78                                                     'First, make sure to add ' . jabber_daemon_address() .
79                                                     ' to your buddy list in your IM client or on GTalk.'));
80                                 common_submit('add', 'Add');
81                         }
82                 }
83
84                 common_element('h2', NULL, _t('Preferences'));
85
86                 common_checkbox('jabbernotify',
87                                 _t('Send me notices through Jabber/GTalk.'),
88                                 $user->jabbernotify);
89                 common_checkbox('updatefrompresence',
90                                 _t('Post a notice when my Jabber/GTalk status changes.'),
91                                 $user->updatefrompresence);
92                 common_submit('save', _t('Save'));
93
94                 common_element_end('form');
95                 common_show_footer();
96         }
97
98         function get_confirmation() {
99                 $user = common_current_user();
100                 $confirm = new Confirm_address();
101                 $confirm->user_id = $user->id;
102                 $confirm->address_type = 'jabber';
103                 if ($confirm->find(TRUE)) {
104                         return $confirm;
105                 } else {
106                         return NULL;
107                 }
108         }
109
110         function handle_post() {
111
112                 if ($this->arg('save')) {
113                         $this->save_preferences();
114                 } else if ($this->arg('add')) {
115                         $this->add_address();
116                 } else if ($this->arg('cancel')) {
117                         $this->cancel_confirmation();
118                 } else if ($this->arg('remove')) {
119                         $this->remove_address();
120                 } else {
121                         $this->show_form(_t('Unexpected form submission.'));
122                 }
123         }
124
125         function save_preferences() {
126
127                 $jabbernotify = $this->boolean('jabbernotify');
128                 $updatefrompresence = $this->boolean('updatefrompresence');
129
130                 $user = common_current_user();
131
132                 assert(!is_null($user)); # should already be checked
133
134                 $user->query('BEGIN');
135
136                 $original = clone($user);
137
138                 $user->jabbernotify = $jabbernotify;
139                 $user->updatefrompresence = $updatefrompresence;
140
141                 $result = $user->update($original);
142
143                 if ($result === FALSE) {
144                         common_log_db_error($user, 'UPDATE', __FILE__);
145                         common_server_error(_t('Couldnt update user.'));
146                         return;
147                 }
148
149                 $user->query('COMMIT');
150
151                 $this->show_form(_t('Preferences saved.'), true);
152         }
153
154         function add_address() {
155
156                 $user = common_current_user();
157
158                 $jabber = $this->trimmed('jabber');
159
160                 # Some validation
161
162                 if (!$jabber) {
163                         $this->show_form(_t('No Jabber ID.'));
164                         return;
165                 }
166
167                 $jabber = jabber_normalize_jid($jabber);
168
169                 if (!$jabber) {
170                     $this->show_form(_('Cannot normalize that Jabber ID'));
171                     return;
172                 }
173                 if (!jabber_valid_base_jid($jabber)) {
174                     $this->show_form(_('Not a valid Jabber ID'));
175                     return;
176                 } else if ($user->jabber == $jabber) {
177                     $this->show_form(_('That is already your Jabber ID.'));
178                     return;
179                 } else if ($this->jabber_exists($jabber)) {
180                     $this->show_form(_('Jabber ID already belongs to another user.'));
181                     return;
182                 }
183
184                 $confirm = new Confirm_address();
185                 $confirm->address = $jabber;
186                 $confirm->address_type = 'jabber';
187                 $confirm->user_id = $user->id;
188                 $confirm->code = common_confirmation_code(64);
189
190                 $result = $confirm->insert();
191
192                 if ($result === FALSE) {
193                         common_log_db_error($confirm, 'INSERT', __FILE__);
194                         common_server_error(_t('Couldnt insert confirmation code.'));
195                         return;
196                 }
197
198                 # XXX: queue for offline sending
199
200                 jabber_confirm_address($confirm->code,
201                                                            $user->nickname,
202                                                            $jabber);
203
204                 # XXX: I18N
205
206                 $msg = 'A confirmation code was sent to the IM address you added. ' .
207                         ' You must approve ' . jabber_daemon_address() .
208                         ' for sending messages to you.';
209
210                 $this->show_form($msg, TRUE);
211         }
212
213         function cancel_confirmation() {
214                 $jabber = $this->arg('jabber');
215                 $confirm = $this->get_confirmation();
216                 if (!$confirm) {
217                         $this->show_form(_t('No pending confirmation to cancel.'));
218                         return;
219                 }
220                 if ($confirm->address != $jabber) {
221                         $this->show_form(_t('That is the wrong IM address.'));
222                         return;
223                 }
224
225         $result = $confirm->delete();
226
227         if (!$result) {
228                         common_log_db_error($confirm, 'DELETE', __FILE__);
229             $this->server_error(_t('Couldn\'t delete email confirmation.'));
230             return;
231         }
232
233         $this->show_form(_t('Confirmation cancelled.'), TRUE);
234         }
235
236         function remove_address() {
237
238                 $user = common_current_user();
239                 $jabber = $this->arg('jabber');
240
241                 # Maybe an old tab open...?
242
243                 if ($user->jabber != $jabber) {
244                     $this->show_form(_t('That is not your Jabber ID.'));
245                     return;
246                 }
247
248                 $user->query('BEGIN');
249                 $original = clone($user);
250                 $user->jabber = NULL;
251                 $result = $user->updateKeys($original);
252                 if (!$result) {
253                         common_log_db_error($user, 'UPDATE', __FILE__);
254                         common_server_error(_t('Couldnt update user.'));
255                         return;
256                 }
257                 $user->query('COMMIT');
258
259                 # Unsubscribe to the old address
260
261                 jabber_special_presence('unsubscribe', $jabber);
262
263                 $this->show_form(_t('The address was removed.'), TRUE);
264         }
265
266         function jabber_exists($jabber) {
267                 $user = common_current_user();
268                 $other = User::staticGet('jabber', $jabber);
269                 if (!$other) {
270                         return false;
271                 } else {
272                         return $other->id != $user->id;
273                 }
274         }
275 }