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