]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/imdaemon.php
Merge branch '0.9.x' into 1.0.x
[quix0rs-gnu-social.git] / scripts / imdaemon.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_IM_HELP
27 Daemon script for receiving new notices from IM users.
28
29     -i --id           Identity (default none)
30     -f --foreground   Stay in the foreground (default background)
31
32 END_OF_IM_HELP;
33
34 require_once INSTALLDIR.'/scripts/commandline.inc';
35
36 class ImDaemon extends SpawningDaemon
37 {
38     function __construct($id=null, $daemonize=true, $threads=1)
39     {
40         if ($threads != 1) {
41             // This should never happen. :)
42             throw new Exception("IMDaemon can must run single-threaded");
43         }
44         parent::__construct($id, $daemonize, $threads);
45     }
46
47     function runThread()
48     {
49         common_log(LOG_INFO, 'Waiting to listen to IM connections and queues');
50
51         $master = new ImMaster($this->get_id());
52         $master->init();
53         $master->service();
54
55         common_log(LOG_INFO, 'terminating normally');
56
57         return $master->respawn ? self::EXIT_RESTART : self::EXIT_SHUTDOWN;
58     }
59
60 }
61
62 class ImMaster extends IoMaster
63 {
64     /**
65      * Initialize IoManagers for the currently configured site
66      * which are appropriate to this instance.
67      */
68     function initManagers()
69     {
70         $classes = array();
71         if (Event::handle('StartImDaemonIoManagers', array(&$classes))) {
72             $classes[] = 'QueueManager';
73         }
74         Event::handle('EndImDaemonIoManagers', array(&$classes));
75         foreach ($classes as $class) {
76             $this->instantiate($class);
77         }
78     }
79 }
80
81 if (have_option('i', 'id')) {
82     $id = get_option_value('i', 'id');
83 } else if (count($args) > 0) {
84     $id = $args[0];
85 } else {
86     $id = null;
87 }
88
89 $foreground = have_option('f', 'foreground');
90
91 $daemon = new ImDaemon($id, !$foreground);
92
93 $daemon->runOnce();