]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/imsettings.php
type -> address_type
[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                 # too much common patterns here... abstractable?
51                 common_input('jabber', _t('IM Address'),
52                                          ($this->arg('jabber')) ? $this->arg('jabber') : $user->jabber,
53                                          _t('Jabber or GTalk address, like "UserName@example.org"'));
54                 common_checkbox('jabbernotify',
55                                 _t('Send me notices through Jabber/GTalk.'));
56                 common_checkbox('updatefrompresence',
57                                 _t('Post a notice when my Jabber/GTalk status changes.'));
58                 common_submit('submit', _t('Save'));
59                 common_element_end('form');
60                 common_show_footer();
61         }
62
63         function handle_post() {
64
65                 $jabber = $this->trimmed('jabber');
66                 $jabbernotify = $this->boolean('jabbernotify');
67                 $updatefrompresence = $this->boolean('updatefrompresence');
68
69                 # Some validation
70                 
71                 if ($jabber) {
72                         $jabber = jabber_normalize_jid($jabber);
73                         if (!$jabber) {
74                             $this->show_form(_('Cannot normalize that Jabber ID'));
75                             return;
76                         }
77                         if (!jabber_valid_base_jid($jabber)) {
78                             $this->show_form(_('Not a valid Jabber ID'));
79                             return;
80                     } else if ($this->jabber_exists($jabber)) {
81                             $this->show_form(_('Jabber ID already belongs to another user.'));
82                             return;
83                         }
84                 }
85
86                 $user = common_current_user();
87
88                 assert(!is_null($user)); # should already be checked
89
90                 $user->query('BEGIN');
91
92                 $original = clone($user);
93                 
94                 $user->jabbernotify = $jabbernotify;
95                 $user->updatefrompresence = $updatefrompresence;
96
97                 $result = $user->update($original); # For key columns
98
99                 if ($result === FALSE) {
100                         common_log_db_error($user, 'UPDATE', __FILE__);
101                         common_server_error(_t('Couldnt update user.'));
102                         return;
103                 }
104
105                 $confirmation_sent = false;
106                 
107                 if ($user->jabber != $jabber) {
108                         
109                         if ($jabber) {
110                         $confirm = new Confirm_address();
111                         $confirm->address = $jabber;
112                         $confirm->address_type = 'jabber';
113                         $confirm->user_id = $user->id;
114                         $confirm->code = common_confirmation_code(64);
115             
116                                 $result = $confirm->insert();
117
118                                 if ($result === FALSE) {
119                                         common_log_db_error($confirm, 'INSERT', __FILE__);
120                                         common_server_error(_t('Couldnt insert confirmation code.'));
121                                         return;
122                                 }
123                                 
124                                 # XXX: optionally queue for offline sending
125                                 
126                                 jabber_confirm_address($confirm->code,
127                                                                            $user->nickname,
128                                                                            $jabber);
129                                                                            
130                                 if ($result === FALSE) {
131                                         common_log_db_error($confirm, 'INSERT', __FILE__);
132                                         common_server_error(_t('Couldnt insert confirmation code.'));
133                                         return;
134                                 }
135                                 
136                                 $confirmation_sent = false;
137                         } else {
138                                 # Clearing the ID is free
139                                 $user->jabber = NULL;
140                                 $result = $user->updateKeys($original);
141                                 if ($result === FALSE) {
142                                         common_log_db_error($user, 'UPDATE', __FILE__);
143                                         common_server_error(_t('Couldnt update user.'));
144                                         return;
145                                 }
146                         }
147                 }
148                 
149                 $user->query('COMMIT');
150
151         $msg = ($confirmation_sent) ? 
152                                   _t('Settings saved. A confirmation code was ' .
153                                      ' sent to the IM address you added. ') :
154                                   _t('Settings saved.');
155                                   
156                 $this->show_form($msg, TRUE);
157         }
158
159         function jabber_exists($jabber) {
160                 $user = common_current_user();
161                 $other = User::staticGet('jabber', $jabber);
162                 if (!$other) {
163                         return false;
164                 } else {
165                         return $other->id != $user->id;
166                 }
167         }
168 }