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