| 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::a'; |
| 24 |
$longoptions = array('id::', 'foreground', 'all'); |
| 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 |
-a --all Handle XMPP for all local sites |
| 31 |
(requires Stomp queue handler, status_network setup) |
| 32 |
-f --foreground Stay in the foreground (default background) |
| 33 |
|
| 34 |
END_OF_XMPP_HELP; |
| 35 |
|
| 36 |
require_once INSTALLDIR.'/scripts/commandline.inc'; |
| 37 |
|
| 38 |
require_once INSTALLDIR . '/lib/jabber.php'; |
| 39 |
|
| 40 |
class XMPPDaemon extends SpawningDaemon |
| 41 |
{ |
| 42 |
protected $allsites = false; |
| 43 |
|
| 44 |
function __construct($id=null, $daemonize=true, $threads=1, $allsites=false) |
| 45 |
{ |
| 46 |
if ($threads != 1) { |
| 47 |
// This should never happen. :) |
| 48 |
throw new Exception("XMPPDaemon can must run single-threaded"); |
| 49 |
} |
| 50 |
parent::__construct($id, $daemonize, $threads); |
| 51 |
$this->allsites = $allsites; |
| 52 |
} |
| 53 |
|
| 54 |
function runThread() |
| 55 |
{ |
| 56 |
common_log(LOG_INFO, 'Waiting to listen to XMPP and queues'); |
| 57 |
|
| 58 |
$master = new XmppMaster($this->get_id(), $this->processManager()); |
| 59 |
$master->init($this->allsites); |
| 60 |
$master->service(); |
| 61 |
|
| 62 |
common_log(LOG_INFO, 'terminating normally'); |
| 63 |
|
| 64 |
return $master->respawn ? self::EXIT_RESTART : self::EXIT_SHUTDOWN; |
| 65 |
} |
| 66 |
|
| 67 |
} |
| 68 |
|
| 69 |
class XmppMaster extends IoMaster |
| 70 |
{ |
| 71 |
protected $processManager; |
| 72 |
|
| 73 |
function __construct($id, $processManager) |
| 74 |
{ |
| 75 |
parent::__construct($id); |
| 76 |
$this->processManager = $processManager; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Initialize IoManagers for the currently configured site |
| 81 |
* which are appropriate to this instance. |
| 82 |
*/ |
| 83 |
function initManagers() |
| 84 |
{ |
| 85 |
if (common_config('xmpp', 'enabled')) { |
| 86 |
$qm = QueueManager::get(); |
| 87 |
$qm->setActiveGroup('xmpp'); |
| 88 |
$this->instantiate($qm); |
| 89 |
$this->instantiate(XmppManager::get()); |
| 90 |
$this->instantiate($this->processManager); |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
// Abort immediately if xmpp is not enabled, otherwise the daemon chews up |
| 96 |
// lots of CPU trying to connect to unconfigured servers |
| 97 |
// @fixme do this check after we've run through the site list so we |
| 98 |
// don't have to find an XMPP site to start up when using --all mode. |
| 99 |
if (common_config('xmpp','enabled')==false) { |
| 100 |
print "Aborting daemon - xmpp is disabled\n"; |
| 101 |
exit(1); |
| 102 |
} |
| 103 |
|
| 104 |
if (version_compare(PHP_VERSION, '5.2.6', '<')) { |
| 105 |
$arch = php_uname('m'); |
| 106 |
if ($arch == 'x86_64' || $arch == 'amd64') { |
| 107 |
print "Aborting daemon - 64-bit PHP prior to 5.2.6 has known bugs in stream_select; you are running " . PHP_VERSION . " on $arch.\n"; |
| 108 |
exit(1); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
if (have_option('i', 'id')) { |
| 113 |
$id = get_option_value('i', 'id'); |
| 114 |
} else if (count($args) > 0) { |
| 115 |
$id = $args[0]; |
| 116 |
} else { |
| 117 |
$id = null; |
| 118 |
} |
| 119 |
|
| 120 |
$foreground = have_option('f', 'foreground'); |
| 121 |
$all = have_option('a') || have_option('--all'); |
| 122 |
|
| 123 |
$daemon = new XMPPDaemon($id, !$foreground, 1, $all); |
| 124 |
|
| 125 |
$daemon->runOnce(); |