Config files now as a nodejs module
[KiwiIRC.git] / server / kiwi.js
1 var fs = require('fs'),
2 _ = require('underscore'),
3 WebListener = require('./weblistener.js');
4
5
6
7 /*
8 * Config loading
9 */
10
11 var config_filename = 'config.js',
12 config_dirs = ['/etc/kiwiirc/', __dirname + '/'];
13
14 var config;
15
16 function loadConfig() {
17 var new_config,
18 conf_filepath;
19
20 // Loop through the possible config paths and find a usable one
21 for (var i in config_dirs) {
22 conf_filepath = config_dirs[i] + config_filename;
23
24 try {
25 if (fs.lstatSync(conf_filepath).isFile() === true) {
26 // Clear the loaded config cache
27 delete require.cache[require.resolve(conf_filepath)];
28
29 // Try load the new config file
30 new_config = require(conf_filepath).production;
31 console.log('Loaded config file ' + config_dirs[i] + config_filename);
32 break;
33 }
34 } catch (e) {
35 switch (e.code) {
36 case 'ENOENT': // No file/dir
37 break;
38 default:
39 console.log('An error occured parsing the config file ' + config_dirs[i] + config_filename + ': ' + e.message);
40 return false;
41 }
42 continue;
43 }
44 }
45
46 return new_config;
47 }
48
49
50 config = loadConfig() || Object.create(null);
51
52 // Make sure we have a valid config file and at least 1 server
53 if (Object.keys(config).length === 0) {
54 console.log('Couldn\'t find a valid config file!');
55 process.exit(1);
56 }
57
58 if ((!config.servers) || (config.servers.length < 1)) {
59 console.log('No servers defined in config file');
60 process.exit(2);
61 }
62
63
64
65
66
67 /*
68 * Web listeners
69 */
70
71 // Holder for all the connected clients
72 // TODO: Change from an array to an object. Generate sha1 hash within the client
73 // and use that as the key. (Much less work involved in removing a client)
74 var clients = [];
75
76 // Start up a weblistener for each found in the config
77 _.each(config.servers, function (server) {
78 var wl = new WebListener(server, config.transports);
79 wl.on('connection', function (client) {
80 clients.push(client);
81 });
82 wl.on('destroy', function (client) {
83 clients = _.reject(clients, function (c) {
84 return client === c;
85 });
86 });
87 });
88
89
90
91
92
93 /*
94 * Process settings
95 */
96
97 // Set process title
98 process.title = 'kiwiirc';
99
100 // Change UID/GID
101 if ((config.user) && (config.user !== '')) {
102 process.setuid(config.user);
103 }
104 if ((config.group) && (config.group !== '')) {
105 process.setgid(config.group);
106 }
107
108
109
110 /*
111 * Listen for runtime commands
112 */
113
114 process.stdin.resume();
115 process.stdin.on('data', function (buffered) {
116 var data = buffered.toString().trim();
117
118 switch (data) {
119 case 'stats':
120 console.log('Connected clients: ' + _.size(clients).toString());
121 break;
122
123 case 'reconfig':
124 (function () {
125 var new_conf = loadConfig();
126 if (new_conf) {
127 config = new_conf;
128 console.log('New config file loaded');
129 } else {
130 console.log("No new config file was loaded");
131 }
132 })();
133
134 break;
135
136 default:
137 console.log('Unrecognised command: ' + data);
138 }
139 });