]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/imsettings.php
some UI changes to the IM settings
[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('Current confirmed Jabber/GTalk address.'));
68                                 common_hidden('jabber', $confirm->address);
69                                 common_element_end('p');
70                                 common_submit('cancel', _t('Cancel'));
71                         } else {
72                                 common_input('jabber', _t('IM Address'),
73                                                         ($this->arg('jabber')) ? $this->arg('jabber') : NULL,
74                                                  _t('Jabber or GTalk address, like "UserName@example.org"'));
75                                 common_submit('add', 'Add');
76                         }
77                 }
78
79                 common_element('h2', NULL, _t('Preferences'));
80
81                 common_checkbox('jabbernotify',
82                                 _t('Send me notices through Jabber/GTalk.'));
83                 common_checkbox('updatefrompresence',
84                                 _t('Post a notice when my Jabber/GTalk status changes.'));
85                 common_submit('save', _t('Save'));
86
87                 common_element_end('form');
88                 common_show_footer();
89         }
90
91         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)) {
97                         return $confirm;
98                 } else {
99                         return NULL;
100                 }
101         }
102
103         function handle_post() {
104
105                 if ($this->arg('save')) {
106                         $this->save_preferences();
107                 } else if ($this->arg('add')) {
108                         $this->add_address();
109                 } else if ($this->arg('cancel')) {
110                         $this->cancel_confirmation();
111                 } else if ($this->arg('remove')) {
112                         $this->remove_address();
113                 } else {
114                         $this->show_form(_t('Unexpected form submission.'));
115                 }
116         }
117
118         function save_preferences() {
119
120                 $jabbernotify = $this->boolean('jabbernotify');
121                 $updatefrompresence = $this->boolean('updatefrompresence');
122
123                 $user = common_current_user();
124
125                 assert(!is_null($user)); # should already be checked
126
127                 $user->query('BEGIN');
128
129                 $original = clone($user);
130
131                 $user->jabbernotify = $jabbernotify;
132                 $user->updatefrompresence = $updatefrompresence;
133
134                 $result = $user->update($original);
135
136                 if ($result === FALSE) {
137                         common_log_db_error($user, 'UPDATE', __FILE__);
138                         common_server_error(_t('Couldnt update user.'));
139                         return;
140                 }
141
142                 $user->query('COMMIT');
143
144                 $this->show_form(_t('Preferences saved.'), true);
145         }
146
147         function add_address() {
148
149                 $jabber = $this->trimmed('jabber');
150
151                 # Some validation
152
153                 if (!$jabber) {
154                         $this->show_form(_t('No Jabber ID.'));
155                         return;
156                 }
157
158                 $jabber = jabber_normalize_jid($jabber);
159
160                 if (!$jabber) {
161                     $this->show_form(_('Cannot normalize that Jabber ID'));
162                     return;
163                 }
164                 if (!jabber_valid_base_jid($jabber)) {
165                     $this->show_form(_('Not a valid Jabber ID'));
166                     return;
167                 } else if ($user->jabber == $jabber) {
168                     $this->show_form(_('That is already your Jabber ID.'));
169                     return;
170                 } else if ($this->jabber_exists($jabber)) {
171                     $this->show_form(_('Jabber ID already belongs to another user.'));
172                     return;
173                 }
174
175                 $confirm = new Confirm_address();
176                 $confirm->address = $jabber;
177                 $confirm->address_type = 'jabber';
178                 $confirm->user_id = $user->id;
179                 $confirm->code = common_confirmation_code(64);
180
181                 $result = $confirm->insert();
182
183                 if ($result === FALSE) {
184                         common_log_db_error($confirm, 'INSERT', __FILE__);
185                         common_server_error(_t('Couldnt insert confirmation code.'));
186                         return;
187                 }
188
189                 # XXX: optionally queue for offline sending
190
191                 if (!jabber_is_subscribed($jabber)) {
192                         jabber_special_presence('subscribe', $address);
193                 } else {
194                         jabber_confirm_address($confirm->code,
195                                                                    $user->nickname,
196                                                                    $jabber);
197                 }
198
199                 $this->show_form(_t('A confirmation code was ' .
200                                     ' sent to the IM address you added. ' .
201                                     ' You must approve ' . jabber_daemon_address() .
202                                     ' for sending messages to you.'),
203                                     TRUE);
204         }
205
206         function cancel_confirmation() {
207                 $jabber = $this->arg('jabber');
208                 $confirm = $this->get_confirmation();
209                 if (!$confirm) {
210                         $this->show_form(_t('No pending confirmation to cancel.'));
211                         return;
212                 }
213                 if ($confirm->address != $jabber) {
214                         $this->show_form(_t('That is the wrong IM address.'));
215                         return;
216                 }
217
218         $result = $confirm->delete();
219
220         if (!$result) {
221                         common_log_db_error($confirm, 'DELETE', __FILE__);
222             $this->server_error(_t('Couldn\'t delete email confirmation.'));
223             return;
224         }
225
226         $this->show_form(_t('Confirmation cancelled.'), TRUE);
227         }
228
229         function remove_address() {
230
231                 $user = common_current_user();
232                 $jabber = $this->arg('jabber');
233
234                 # Maybe an old tab open...?
235
236                 if ($user->jabber != $jabber) {
237                     $this->show_form(_t('That is not your Jabber ID.'));
238                     return;
239                 }
240
241                 $user->query('BEGIN');
242                 $user->jabber = NULL;
243                 $result = $user->updateKeys($original);
244                 if ($result === FALSE) {
245                         common_log_db_error($user, 'UPDATE', __FILE__);
246                         common_server_error(_t('Couldnt update user.'));
247                         return;
248                 }
249                 $user->query('COMMIT');
250
251                 # Unsubscribe to the old address
252
253                 jabber_special_presence('unsubscribe', $jabber);
254
255                 $this->show_form(_t('The address was removed.'), TRUE);
256         }
257
258         function jabber_exists($jabber) {
259                 $user = common_current_user();
260                 $other = User::staticGet('jabber', $jabber);
261                 if (!$other) {
262                         return false;
263                 } else {
264                         return $other->id != $user->id;
265                 }
266         }
267 }