]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/xmppdaemon.php
fd7cf055b485cedaae3f327952c16baf4f634456
[quix0rs-gnu-social.git] / scripts / xmppdaemon.php
1 #!/usr/bin/env php
2 <?php
3 /*
4  * StatusNet - the distributed open-source microblogging tool
5  * Copyright (C) 2008, 2009, StatusNet, Inc.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
22
23 $shortoptions = 'fi::';
24 $longoptions = array('id::', 'foreground');
25
26 $helptext = <<<END_OF_XMPP_HELP
27 Daemon script for receiving new notices from Jabber users.
28
29     -i --id           Identity (default none)
30     -f --foreground   Stay in the foreground (default background)
31
32 END_OF_XMPP_HELP;
33
34 require_once INSTALLDIR.'/scripts/commandline.inc';
35
36 require_once INSTALLDIR . '/lib/jabber.php';
37
38 class XMPPDaemon extends SpawningDaemon
39 {
40     function __construct($id=null, $daemonize=true, $threads=1)
41     {
42         if ($threads != 1) {
43             // This should never happen. :)
44             throw new Exception("XMPPDaemon can must run single-threaded");
45         }
46         parent::__construct($id, $daemonize, $threads);
47     }
48
49     function runThread()
50     {
51         common_log(LOG_INFO, 'Waiting to listen to XMPP and queues');
52
53         $master = new XmppMaster($this->get_id());
54         $master->init();
55         $master->service();
56
57         common_log(LOG_INFO, 'terminating normally');
58
59         return true;
60     }
61
62 }
63
64 class XmppMaster extends IoMaster
65 {
66     /**
67      * Initialize IoManagers for the currently configured site
68      * which are appropriate to this instance.
69      */
70     function initManagers()
71     {
72         // @fixme right now there's a hack in QueueManager to determine
73         // which queues to subscribe to based on the master class.
74         $this->instantiate('QueueManager');
75         $this->instantiate('XmppManager');
76     }
77 }
78
79 // Abort immediately if xmpp is not enabled, otherwise the daemon chews up
80 // lots of CPU trying to connect to unconfigured servers
81 if (common_config('xmpp','enabled')==false) {
82     print "Aborting daemon - xmpp is disabled\n";
83     exit();
84 }
85
86 if (have_option('i', 'id')) {
87     $id = get_option_value('i', 'id');
88 } else if (count($args) > 0) {
89     $id = $args[0];
90 } else {
91     $id = null;
92 }
93
94 $foreground = have_option('f', 'foreground');
95
96 $daemon = new XMPPDaemon($id, !$foreground);
97
98 $daemon->runOnce();