]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/xmppconfirmmanager.php
ee4e294fd4d723cf9a2fa3d233ae81807e3d4168
[quix0rs-gnu-social.git] / lib / xmppconfirmmanager.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008-2010 StatusNet, 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('STATUSNET') && !defined('LACONICA')) {
21     exit(1);
22 }
23
24 /**
25  * Event handler for pushing new confirmations to Jabber users.
26  * @fixme recommend redoing this on a queue-trigger model
27  * @fixme expiration of old items got dropped in the past, put it back?
28  */
29 class XmppConfirmManager extends IoManager
30 {
31
32     /**
33      * @return mixed XmppConfirmManager, or false if unneeded
34      */
35     public static function get()
36     {
37         if (common_config('xmpp', 'enabled')) {
38             $site = common_config('site', 'server');
39             return new XmppConfirmManager();
40         } else {
41             return false;
42         }
43     }
44
45     /**
46      * Tell the i/o master we need one instance for each supporting site
47      * being handled in this process.
48      */
49     public static function multiSite()
50     {
51         return IoManager::INSTANCE_PER_SITE;
52     }
53
54     function __construct()
55     {
56         $this->site = common_config('site', 'server');
57     }
58
59     /**
60      * 10 seconds? Really? That seems a bit frequent.
61      */
62     function pollInterval()
63     {
64         return 10;
65     }
66
67     /**
68      * Ping!
69      * @return boolean true if we found something
70      */
71     function poll()
72     {
73         $this->switchSite();
74         $confirm = $this->next_confirm();
75         if ($confirm) {
76             $this->handle_confirm($confirm);
77             return true;
78         } else {
79             return false;
80         }
81     }
82
83     protected function handle_confirm($confirm)
84     {
85         require_once INSTALLDIR . '/lib/jabber.php';
86
87         common_log(LOG_INFO, 'Sending confirmation for ' . $confirm->address);
88         $user = User::staticGet($confirm->user_id);
89         if (!$user) {
90             common_log(LOG_WARNING, 'Confirmation for unknown user ' . $confirm->user_id);
91             return;
92         }
93         $success = jabber_confirm_address($confirm->code,
94                                           $user->nickname,
95                                           $confirm->address);
96         if (!$success) {
97             common_log(LOG_ERR, 'Confirmation failed for ' . $confirm->address);
98             # Just let the claim age out; hopefully things work then
99             return;
100         } else {
101             common_log(LOG_INFO, 'Confirmation sent for ' . $confirm->address);
102             # Mark confirmation sent; need a dupe so we don't have the WHERE clause
103             $dupe = Confirm_address::staticGet('code', $confirm->code);
104             if (!$dupe) {
105                 common_log(LOG_WARNING, 'Could not refetch confirm', __FILE__);
106                 return;
107             }
108             $orig = clone($dupe);
109             $dupe->sent = $dupe->claimed;
110             $result = $dupe->update($orig);
111             if (!$result) {
112                 common_log_db_error($dupe, 'UPDATE', __FILE__);
113                 # Just let the claim age out; hopefully things work then
114                 return;
115             }
116         }
117         return true;
118     }
119
120     protected function next_confirm()
121     {
122         $confirm = new Confirm_address();
123         $confirm->whereAdd('claimed IS null');
124         $confirm->whereAdd('sent IS null');
125         # XXX: eventually we could do other confirmations in the queue, too
126         $confirm->address_type = 'jabber';
127         $confirm->orderBy('modified DESC');
128         $confirm->limit(1);
129         if ($confirm->find(true)) {
130             common_log(LOG_INFO, 'Claiming confirmation for ' . $confirm->address);
131                 # working around some weird DB_DataObject behaviour
132             $confirm->whereAdd(''); # clears where stuff
133             $original = clone($confirm);
134             $confirm->claimed = common_sql_now();
135             $result = $confirm->update($original);
136             if ($result) {
137                 common_log(LOG_INFO, 'Succeeded in claim! '. $result);
138                 return $confirm;
139             } else {
140                 common_log(LOG_INFO, 'Failed in claim!');
141                 return false;
142             }
143         }
144         return null;
145     }
146
147     protected function clear_old_confirm_claims()
148     {
149         $confirm = new Confirm();
150         $confirm->claimed = null;
151         $confirm->whereAdd('now() - claimed > '.CLAIM_TIMEOUT);
152         $confirm->update(DB_DATAOBJECT_WHEREADD_ONLY);
153         $confirm->free();
154         unset($confirm);
155     }
156
157     /**
158      * Make sure we're on the right site configuration
159      */
160     protected function switchSite()
161     {
162         if ($this->site != common_config('site', 'server')) {
163             common_log(LOG_DEBUG, __METHOD__ . ": switching to site $this->site");
164             $this->stats('switch');
165             StatusNet::init($this->site);
166         }
167     }
168 }