1 var fs
= require('fs'),
2 _
= require('underscore'),
3 WebListener
= require('./weblistener.js');
11 var config_filename
= 'config.js',
12 config_dirs
= ['/etc/kiwiirc/', __dirname
+ '/'];
16 function loadConfig() {
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
;
25 if (fs
.lstatSync(conf_filepath
).isFile() === true) {
26 // Clear the loaded config cache
27 delete require
.cache
[require
.resolve(conf_filepath
)];
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
);
36 case 'ENOENT': // No file/dir
39 console
.log('An error occured parsing the config file ' + config_dirs
[i
] + config_filename
+ ': ' + e
.message
);
50 config
= loadConfig() || Object
.create(null);
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!');
58 if ((!config
.servers
) || (config
.servers
.length
< 1)) {
59 console
.log('No servers defined in config file');
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)
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
) {
82 wl
.on('destroy', function (client
) {
83 clients
= _
.reject(clients
, function (c
) {
98 process
.title
= 'kiwiirc';
101 if ((config
.user
) && (config
.user
!== '')) {
102 process
.setuid(config
.user
);
104 if ((config
.group
) && (config
.group
!== '')) {
105 process
.setgid(config
.group
);
111 * Listen for runtime commands
114 process
.stdin
.resume();
115 process
.stdin
.on('data', function (buffered
) {
116 var data
= buffered
.toString().trim();
120 console
.log('Connected clients: ' + _
.size(clients
).toString());
125 var new_conf
= loadConfig();
128 console
.log('New config file loaded');
130 console
.log("No new config file was loaded");
137 console
.log('Unrecognised command: ' + data
);