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