]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/imdaemon.php
Make attachment fit better in notice: drop text and link
[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', dirname(__DIR__));
22 define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
23
24 $shortoptions = 'fi::a';
25 $longoptions = array('id::', 'foreground', 'all');
26
27 $helptext = <<<END_OF_IM_HELP
28 Daemon script for receiving new notices from IM users.
29
30     -i --id           Identity (default none)
31     -a --all          Handle XMPP for all local sites
32                       (requires Stomp queue handler, status_network setup)
33     -f --foreground   Stay in the foreground (default background)
34
35 END_OF_IM_HELP;
36
37 require_once INSTALLDIR.'/scripts/commandline.inc';
38
39 class ImDaemon extends SpawningDaemon
40 {
41     protected $allsites = false;
42
43     function __construct($id=null, $daemonize=true, $threads=1, $allsites=false)
44     {
45         if ($threads != 1) {
46             // This should never happen. :)
47             throw new Exception("IMDaemon can must run single-threaded");
48         }
49         parent::__construct($id, $daemonize, $threads);
50         $this->allsites = $allsites;
51     }
52
53     function runThread()
54     {
55         common_log(LOG_INFO, 'Waiting to listen to IM connections and queues');
56
57         $master = new ImMaster($this->get_id(), $this->processManager());
58         $master->init($this->allsites);
59         $master->service();
60
61         common_log(LOG_INFO, 'terminating normally');
62
63         return $master->respawn ? self::EXIT_RESTART : self::EXIT_SHUTDOWN;
64     }
65
66 }
67
68 class ImMaster extends IoMaster
69 {
70     protected $processManager;
71
72     function __construct($id, $processManager)
73     {
74         parent::__construct($id);
75         $this->processManager = $processManager;
76     }
77
78     /**
79      * Initialize IoManagers for the currently configured site
80      * which are appropriate to this instance.
81      */
82     function initManagers()
83     {
84         $classes = array();
85         if (Event::handle('StartImDaemonIoManagers', array(&$classes))) {
86             $qm = QueueManager::get();
87             $qm->setActiveGroup('im');
88             $classes[] = $qm;
89             $classes[] = $this->processManager;
90         }
91         Event::handle('EndImDaemonIoManagers', array(&$classes));
92         foreach ($classes as $class) {
93             $this->instantiate($class);
94         }
95     }
96 }
97
98 if (have_option('i', 'id')) {
99     $id = get_option_value('i', 'id');
100 } else if (count($args) > 0) {
101     $id = $args[0];
102 } else {
103     $id = null;
104 }
105
106 $foreground = have_option('f', 'foreground');
107 $all = have_option('a') || have_option('--all');
108
109 $daemon = new ImDaemon($id, !$foreground, 1, $all);
110
111 $daemon->runOnce();