| 
									
										
										
										
											2008-06-26 11:03:36 -04:00
										 |  |  | #!/usr/bin/env php
 | 
					
						
							| 
									
										
										
										
											2008-06-22 22:27:10 -04:00
										 |  |  | <?php | 
					
						
							|  |  |  | /* | 
					
						
							| 
									
										
										
										
											2009-08-25 18:14:12 -04:00
										 |  |  |  * StatusNet - the distributed open-source microblogging tool | 
					
						
							| 
									
										
										
										
											2009-08-25 18:12:20 -04:00
										 |  |  |  * Copyright (C) 2008, 2009, StatusNet, Inc. | 
					
						
							| 
									
										
										
										
											2008-06-22 22:27:10 -04:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU Affero General Public License as published by | 
					
						
							|  |  |  |  * the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  |  * (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU Affero General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  |  * along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-13 11:46:03 -04:00
										 |  |  | define('INSTALLDIR', realpath(dirname(__FILE__) . '/..')); | 
					
						
							| 
									
										
										
										
											2008-08-27 10:23:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-28 16:13:08 -04:00
										 |  |  | $shortoptions = 'fi::'; | 
					
						
							|  |  |  | $longoptions = array('id::', 'foreground'); | 
					
						
							| 
									
										
										
										
											2009-06-22 17:07:14 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | $helptext = <<<END_OF_XMPP_HELP | 
					
						
							|  |  |  | Daemon script for receiving new notices from Jabber users. | 
					
						
							| 
									
										
										
										
											2009-06-20 14:58:47 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-24 19:31:12 -07:00
										 |  |  |     -i --id           Identity (default none) | 
					
						
							| 
									
										
										
										
											2009-06-28 16:13:08 -04:00
										 |  |  |     -f --foreground   Stay in the foreground (default background) | 
					
						
							| 
									
										
										
										
											2009-06-20 14:58:47 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-22 17:07:14 -07:00
										 |  |  | END_OF_XMPP_HELP; | 
					
						
							| 
									
										
										
										
											2008-06-22 22:27:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-22 17:07:14 -07:00
										 |  |  | require_once INSTALLDIR.'/scripts/commandline.inc'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require_once INSTALLDIR . '/lib/jabber.php'; | 
					
						
							| 
									
										
										
										
											2008-08-27 16:54:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-21 16:42:50 -08:00
										 |  |  | class XMPPDaemon extends SpawningDaemon | 
					
						
							| 
									
										
										
										
											2008-12-23 14:49:23 -05:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-01-21 16:42:50 -08:00
										 |  |  |     function __construct($id=null, $daemonize=true, $threads=1) | 
					
						
							| 
									
										
										
										
											2008-12-23 14:33:23 -05:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2010-01-21 16:42:50 -08:00
										 |  |  |         if ($threads != 1) { | 
					
						
							|  |  |  |             // This should never happen. :)
 | 
					
						
							|  |  |  |             throw new Exception("XMPPDaemon can must run single-threaded"); | 
					
						
							| 
									
										
										
										
											2009-07-09 09:31:10 -04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2010-01-21 16:42:50 -08:00
										 |  |  |         parent::__construct($id, $daemonize, $threads); | 
					
						
							| 
									
										
										
										
											2009-07-09 09:31:10 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-21 16:42:50 -08:00
										 |  |  |     function runThread() | 
					
						
							| 
									
										
										
										
											2009-07-09 09:31:10 -04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2010-01-21 16:42:50 -08:00
										 |  |  |         common_log(LOG_INFO, 'Waiting to listen to XMPP and queues'); | 
					
						
							| 
									
										
										
										
											2009-07-09 09:31:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-21 16:42:50 -08:00
										 |  |  |         $master = new XmppMaster($this->get_id()); | 
					
						
							|  |  |  |         $master->init(); | 
					
						
							|  |  |  |         $master->service(); | 
					
						
							| 
									
										
										
										
											2009-07-09 09:31:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-21 16:42:50 -08:00
										 |  |  |         common_log(LOG_INFO, 'terminating normally'); | 
					
						
							| 
									
										
										
										
											2008-12-23 14:19:07 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-21 16:42:50 -08:00
										 |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2008-12-23 14:19:07 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-21 16:42:50 -08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-12-23 14:19:07 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-21 16:42:50 -08:00
										 |  |  | class XmppMaster extends IoMaster | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Initialize IoManagers for the currently configured site | 
					
						
							|  |  |  |      * which are appropriate to this instance. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     function initManagers() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // @fixme right now there's a hack in QueueManager to determine
 | 
					
						
							|  |  |  |         // which queues to subscribe to based on the master class.
 | 
					
						
							|  |  |  |         $this->instantiate('QueueManager'); | 
					
						
							|  |  |  |         $this->instantiate('XmppManager'); | 
					
						
							| 
									
										
										
										
											2008-12-23 14:19:07 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2008-06-22 22:27:10 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-16 21:07:59 +01:00
										 |  |  | // Abort immediately if xmpp is not enabled, otherwise the daemon chews up
 | 
					
						
							|  |  |  | // lots of CPU trying to connect to unconfigured servers
 | 
					
						
							|  |  |  | if (common_config('xmpp','enabled')==false) { | 
					
						
							|  |  |  |     print "Aborting daemon - xmpp is disabled\n"; | 
					
						
							|  |  |  |     exit(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-28 16:13:08 -04:00
										 |  |  | if (have_option('i', 'id')) { | 
					
						
							|  |  |  |     $id = get_option_value('i', 'id'); | 
					
						
							| 
									
										
										
										
											2009-06-22 17:07:14 -07:00
										 |  |  | } else if (count($args) > 0) { | 
					
						
							| 
									
										
										
										
											2009-06-24 19:31:12 -07:00
										 |  |  |     $id = $args[0]; | 
					
						
							| 
									
										
										
										
											2009-06-22 17:07:14 -07:00
										 |  |  | } else { | 
					
						
							| 
									
										
										
										
											2009-06-24 19:31:12 -07:00
										 |  |  |     $id = null; | 
					
						
							| 
									
										
										
										
											2009-06-22 17:07:14 -07:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-06-26 11:03:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-28 16:13:08 -04:00
										 |  |  | $foreground = have_option('f', 'foreground'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-28 16:33:08 -04:00
										 |  |  | $daemon = new XMPPDaemon($id, !$foreground); | 
					
						
							| 
									
										
										
										
											2008-06-22 22:27:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-16 17:08:13 -04:00
										 |  |  | $daemon->runOnce(); |