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