UID/GID changing in the correct order?
[KiwiIRC.git] / server / kiwi.js
CommitLineData
a8bf3ea4 1var fs = require('fs'),
bd299b17 2 _ = require('underscore'),
1360a454 3 WebListener = require('./weblistener.js'),
1286229a
D
4 config = require('./configuration.js'),
5 rehash = require('./rehash.js');
fd779420 6
186531ed
D
7
8
4a30a583 9
ab15f618 10
1360a454 11config.loadConfig();
ab15f618 12
186531ed 13// Make sure we have a valid config file and at least 1 server
1360a454 14if (Object.keys(config.get()).length === 0) {
a8bf3ea4
JA
15 console.log('Couldn\'t find a valid config file!');
16 process.exit(1);
fd779420
D
17}
18
1360a454 19if ((!config.get().servers) || (config.get().servers.length < 1)) {
a8bf3ea4
JA
20 console.log('No servers defined in config file');
21 process.exit(2);
fd779420
D
22}
23
24
186531ed
D
25
26
27
28/*
29 * Web listeners
30 */
31
32// Holder for all the connected clients
33// TODO: Change from an array to an object. Generate sha1 hash within the client
34// and use that as the key. (Much less work involved in removing a client)
a8bf3ea4 35var clients = [];
186531ed
D
36
37// Start up a weblistener for each found in the config
1360a454
D
38_.each(config.get().servers, function (server) {
39 var wl = new WebListener(server, config.get().transports);
a8bf3ea4
JA
40 wl.on('connection', function (client) {
41 clients.push(client);
42 });
43 wl.on('destroy', function (client) {
44 clients = _.reject(clients, function (c) {
c08717da
D
45 if (client === c) {
46 c.dispose();
47 return true;
48 }
49
50 return false;
a8bf3ea4
JA
51 });
52 });
53});
68ad40c6 54
b0ad9f0a 55
f52d8543 56
186531ed
D
57
58
59/*
60 * Process settings
61 */
62
63// Set process title
64process.title = 'kiwiirc';
65
66// Change UID/GID
1360a454 67if ((config.get().group) && (config.get().group !== '')) {
6b586d2e
D
68 process.setgid(config.get().group);
69}
70if ((config.get().user) && (config.get().user !== '')) {
71 process.setuid(config.get().user);
fd779420 72}
709031df 73
186531ed
D
74
75
76/*
77 * Listen for runtime commands
78 */
79
87a6abbe 80process.stdin.resume();
186531ed
D
81process.stdin.on('data', function (buffered) {
82 var data = buffered.toString().trim();
83
84 switch (data) {
85 case 'stats':
86 console.log('Connected clients: ' + _.size(clients).toString());
87 break;
88
ab15f618
D
89 case 'reconfig':
90 (function () {
1360a454 91 if (config.loadConfig()) {
ab15f618
D
92 console.log('New config file loaded');
93 } else {
94 console.log("No new config file was loaded");
95 }
96 })();
97
98 break;
99
1286229a
D
100
101 case 'rehash':
102 (function () {
103 rehash.rehashAll();
104 console.log('Rehashed');
105 })();
106
107 break;
108
186531ed
D
109 default:
110 console.log('Unrecognised command: ' + data);
111 }
a8bf3ea4 112});