1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
<?php
namespace callnotifier;
class CLI
{
protected $cliParser;
public function __construct()
{
$this->setupCli();
}
public function run()
{
$config = new Config();
try {
$result = $this->cliParser->parse();
} catch (\Exception $exc) {
$this->cliParser->displayError($exc->getMessage());
}
$this->fillConfig($config, $result);
$log = new Log();
if ($result->options['debug'] || $result->options['debugEdss1']) {
$debugLogger = new Logger_Debug();
$log->addLogger($debugLogger, '*');
if ($result->options['debugEdss1']) {
$debugLogger->edss1MsgOnly = true;
}
}
$callMonitor = new CallMonitor($config, $log);
$configFile = $this->getConfigFile();
if ($configFile !== null) {
include $configFile;
}
$handler = new MessageHandler($config, $log, $callMonitor);
if ($config->replayFile !== null) {
$sourceClass = 'callnotifier\Source_File';
} else {
$sourceClass = 'callnotifier\Source_Remote';
}
try {
$source = new $sourceClass($config, $handler);
$source->run();
} catch (\Exception $e) {
$msg = 'Callnotifier error!' . "\n"
. 'Code: ' . $e->getCode() . "\n"
. 'Message: ' . $e->getMessage() . "\n";
file_put_contents('php://stderr', $msg);
exit(1);
}
}
public function setupCli()
{
$p = new \Console_CommandLine();
$p->description = 'Notifies about incoming calls on an Auerswald COMpact 3000';
$p->version = '0.0.1';
$p->addOption(
'host',
array(
'short_name' => '-h',
'long_name' => '--host',
'description' => 'IP of COMpact 3000',
'help_name' => 'IP',
'action' => 'StoreString'
)
);
$p->addOption(
'dumpFile',
array(
'long_name' => '--dump',
'description' => 'Dump messages into file for later replay',
'help_name' => 'FILE',
'action' => 'StoreString'
)
);
$p->addOption(
'replayFile',
array(
'long_name' => '--replay',
'description' => "Replay messages from file instead from network",
'help_name' => 'FILE',
'action' => 'StoreString'
)
);
$p->addOption(
'debug',
array(
'short_name' => '-d',
'long_name' => '--debug',
'description' => "Debug mode: Echo all received messages and events",
'action' => 'StoreTrue'
)
);
$p->addOption(
'debugEdss1',
array(
'short_name' => '-e',
'long_name' => '--debug-edss1',
'description' => "Debug mode: Show EDSS1 messages only",
'action' => 'StoreTrue'
)
);
$this->cliParser = $p;
}
protected function fillConfig($config, $result)
{
$config->setIfNotEmpty('host', $result->options['host']);
$config->setIfNotEmpty('dumpFile', $result->options['dumpFile']);
$config->setIfNotEmpty('replayFile', $result->options['replayFile']);
}
/**
* Finds the path to the configuration file.
*
* The following locations are tried:
* - Git checkout: data/callnotifier.config.php
* - ~/.config/callnotifier.config.php
* - /etc/callnotifier.config.php
*
* @return string Full path of config file or NULL if no file found
*/
public function getConfigFile()
{
if (basename(dirname(__DIR__)) == 'src'
&& file_exists(__DIR__ . '/../../data/callnotifier.config.php')
) {
return __DIR__ . '/../../data/callnotifier.config.php';
}
if (isset($_ENV['HOME'])) {
$file = $_ENV['HOME'] . '/.config/callnotifier.config.php';
if (file_exists($file)) {
return $file;
}
}
$file = '/etc/callnotifier.config.php';
if (file_exists($file)) {
return $file;
}
return null;
}
}
?>
|