Commit | Line | Data |
---|---|---|
a8bf3ea4 | 1 | var fs = require('fs'), |
f9ff7686 | 2 | _ = require('lodash'), |
1360a454 | 3 | WebListener = require('./weblistener.js'), |
1286229a D |
4 | config = require('./configuration.js'), |
5 | rehash = require('./rehash.js'); | |
fd779420 | 6 | |
186531ed D |
7 | |
8 | ||
11dbb00f D |
9 | process.chdir(__dirname + '/../'); |
10 | config.loadConfig(); | |
11 | ||
12 | ||
13 | // If we're not running in the forground and we have a log file.. switch | |
14 | // console.log to output to a file | |
b737610b | 15 | if (process.argv.indexOf('-f') === -1 && global.config.log) { |
11dbb00f | 16 | (function () { |
b737610b | 17 | var log_file_name = global.config.log; |
11dbb00f D |
18 | |
19 | if (log_file_name[0] !== '/') { | |
20 | log_file_name = __dirname + '/../' + log_file_name; | |
21 | } | |
22 | ||
23 | ||
24 | ||
25 | console.log = function() { | |
26 | var logfile = fs.openSync(log_file_name, 'a'), | |
27 | out; | |
28 | ||
29 | out = Array.prototype.join.apply(arguments, [' ']); | |
30 | ||
31 | // Make sure we out somthing to log and we have an open file | |
32 | if (!out || !logfile) return; | |
33 | ||
34 | out += '\n'; | |
35 | fs.writeSync(logfile, out, null); | |
36 | ||
37 | fs.closeSync(logfile); | |
38 | }; | |
39 | })(); | |
40 | } | |
4a30a583 | 41 | |
ab15f618 | 42 | |
ab15f618 | 43 | |
186531ed | 44 | // Make sure we have a valid config file and at least 1 server |
1f49a029 D |
45 | if (!global.config || Object.keys(global.config).length === 0) { |
46 | console.log('Couldn\'t find a valid config.js file (Did you copy the config.example.js file yet?)'); | |
a8bf3ea4 | 47 | process.exit(1); |
fd779420 D |
48 | } |
49 | ||
b737610b | 50 | if ((!global.config.servers) || (global.config.servers.length < 1)) { |
a8bf3ea4 JA |
51 | console.log('No servers defined in config file'); |
52 | process.exit(2); | |
fd779420 D |
53 | } |
54 | ||
55 | ||
186531ed D |
56 | |
57 | ||
58 | ||
186531ed | 59 | // Holder for all the connected clients |
26322e8f D |
60 | global.clients = { |
61 | clients: Object.create(null), | |
c36ed4eb | 62 | addresses: Object.create(null), |
26322e8f | 63 | |
c36ed4eb JA |
64 | add: function (client) { |
65 | this.clients[client.hash] = client; | |
66 | if (typeof this.addresses[client.real_address] === 'undefined') { | |
67 | this.addresses[client.real_address] = Object.create(null); | |
68 | } | |
69 | this.addresses[client.real_address][client.hash] = client; | |
70 | }, | |
26322e8f | 71 | |
c36ed4eb JA |
72 | remove: function (client) { |
73 | if (typeof this.clients[client.hash] !== 'undefined') { | |
74 | delete this.clients[client.hash]; | |
75 | delete this.addresses[client.real_address][client.hash]; | |
76 | if (Object.keys(this.addresses[client.real_address]).length < 1) { | |
77 | delete this.addresses[client.real_address]; | |
78 | } | |
79 | } | |
80 | }, | |
26322e8f | 81 | |
c36ed4eb JA |
82 | numOnAddress: function (addr) { |
83 | if (typeof this.addresses[addr] !== 'undefined') { | |
84 | return Object.keys(this.addresses[addr]).length; | |
85 | } else { | |
86 | return 0; | |
87 | } | |
88 | } | |
89 | }; | |
186531ed | 90 | |
26322e8f D |
91 | |
92 | ||
93 | ||
94 | /* | |
95 | * Web listeners | |
96 | */ | |
97 | ||
98 | ||
186531ed | 99 | // Start up a weblistener for each found in the config |
b737610b JA |
100 | _.each(global.config.servers, function (server) { |
101 | var wl = new WebListener(server, global.config.transports); | |
170a3d85 | 102 | |
a8bf3ea4 | 103 | wl.on('connection', function (client) { |
c36ed4eb | 104 | clients.add(client); |
a8bf3ea4 | 105 | }); |
26322e8f | 106 | |
a8bf3ea4 | 107 | wl.on('destroy', function (client) { |
c36ed4eb | 108 | clients.remove(client); |
a8bf3ea4 | 109 | }); |
e380bc9e D |
110 | |
111 | wl.on('listening', webListenerRunning); | |
a8bf3ea4 | 112 | }); |
68ad40c6 | 113 | |
e380bc9e D |
114 | // Once all the listeners are listening, set the processes UID/GID |
115 | var num_listening = 0; | |
116 | function webListenerRunning() { | |
117 | num_listening++; | |
118 | if (num_listening === global.config.servers.length) { | |
119 | setProcessUid(); | |
120 | } | |
121 | } | |
b0ad9f0a | 122 | |
f52d8543 | 123 | |
186531ed D |
124 | |
125 | ||
126 | /* | |
127 | * Process settings | |
128 | */ | |
129 | ||
130 | // Set process title | |
131 | process.title = 'kiwiirc'; | |
132 | ||
133 | // Change UID/GID | |
e380bc9e D |
134 | function setProcessUid() { |
135 | if ((global.config.group) && (global.config.group !== '')) { | |
136 | process.setgid(global.config.group); | |
137 | } | |
138 | if ((global.config.user) && (global.config.user !== '')) { | |
139 | process.setuid(global.config.user); | |
140 | } | |
fd779420 | 141 | } |
709031df | 142 | |
186531ed | 143 | |
0bb21ab3 D |
144 | // Make sure Kiwi doesn't simply quit on an exception |
145 | process.on('uncaughtException', function (e) { | |
146 | console.log('[Uncaught exception] ' + e); | |
147 | }); | |
148 | ||
149 | ||
88f14637 D |
150 | process.on('SIGUSR1', function() { |
151 | if (config.loadConfig()) { | |
152 | console.log('New config file loaded'); | |
153 | } else { | |
154 | console.log("No new config file was loaded"); | |
155 | } | |
156 | }); | |
157 | ||
158 | ||
159 | ||
186531ed D |
160 | |
161 | /* | |
162 | * Listen for runtime commands | |
163 | */ | |
164 | ||
87a6abbe | 165 | process.stdin.resume(); |
186531ed D |
166 | process.stdin.on('data', function (buffered) { |
167 | var data = buffered.toString().trim(); | |
168 | ||
169 | switch (data) { | |
170 | case 'stats': | |
170a3d85 D |
171 | console.log('Connected clients: ' + _.size(global.clients.clients).toString()); |
172 | console.log('Num. remote hosts: ' + _.size(global.clients.addresses).toString()); | |
186531ed D |
173 | break; |
174 | ||
ab15f618 | 175 | case 'reconfig': |
88f14637 D |
176 | if (config.loadConfig()) { |
177 | console.log('New config file loaded'); | |
178 | } else { | |
179 | console.log("No new config file was loaded"); | |
180 | } | |
ab15f618 D |
181 | |
182 | break; | |
183 | ||
1286229a D |
184 | |
185 | case 'rehash': | |
186 | (function () { | |
187 | rehash.rehashAll(); | |
188 | console.log('Rehashed'); | |
189 | })(); | |
190 | ||
191 | break; | |
192 | ||
186531ed D |
193 | default: |
194 | console.log('Unrecognised command: ' + data); | |
195 | } | |
a8bf3ea4 | 196 | }); |