f3ce6ca716c471bc182ec8dcf02622453dc1f8b0
1 var fs
= require('fs'),
2 _
= require('underscore'),
3 WebListener
= require('./weblistener.js'),
4 config
= require('./configuration.js'),
5 rehash
= require('./rehash.js');
9 process
.chdir(__dirname
+ '/../');
13 // If we're not running in the forground and we have a log file.. switch
14 // console.log to output to a file
15 if (process
.argv
.indexOf('-f') === -1 && config
.get().log
) {
17 var log_file_name
= config
.get().log
;
19 if (log_file_name
[0] !== '/') {
20 log_file_name
= __dirname
+ '/../' + log_file_name
;
25 console
.log = function() {
26 var logfile
= fs
.openSync(log_file_name
, 'a'),
29 out
= Array
.prototype.join
.apply(arguments
, [' ']);
31 // Make sure we out somthing to log and we have an open file
32 if (!out
|| !logfile
) return;
35 fs
.writeSync(logfile
, out
, null);
37 fs
.closeSync(logfile
);
44 // Make sure we have a valid config file and at least 1 server
45 if (Object
.keys(config
.get()).length
=== 0) {
46 console
.log('Couldn\'t find a valid config file!');
50 if ((!config
.get().servers
) || (config
.get().servers
.length
< 1)) {
51 console
.log('No servers defined in config file');
59 // Holder for all the connected clients
61 clients
: Object
.create(null),
62 addresses
: Object
.create(null),
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);
69 this.addresses
[client
.real_address
][client
.hash
] = client
;
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
];
82 numOnAddress: function (addr
) {
83 if (typeof this.addresses
[addr
] !== 'undefined') {
84 return Object
.keys(this.addresses
[addr
]).length
;
99 // Start up a weblistener for each found in the config
100 _
.each(config
.get().servers
, function (server
) {
101 var wl
= new WebListener(server
, config
.get().transports
);
103 wl
.on('connection', function (client
) {
107 wl
.on('destroy', function (client
) {
108 clients
.remove(client
);
121 process
.title
= 'kiwiirc';
124 if ((config
.get().group
) && (config
.get().group
!== '')) {
125 process
.setgid(config
.get().group
);
127 if ((config
.get().user
) && (config
.get().user
!== '')) {
128 process
.setuid(config
.get().user
);
132 process
.on('SIGUSR1', function() {
133 if (config
.loadConfig()) {
134 console
.log('New config file loaded');
136 console
.log("No new config file was loaded");
144 * Listen for runtime commands
147 process
.stdin
.resume();
148 process
.stdin
.on('data', function (buffered
) {
149 var data
= buffered
.toString().trim();
153 console
.log('Connected clients: ' + _
.size(global
.clients
.clients
).toString());
154 console
.log('Num. remote hosts: ' + _
.size(global
.clients
.addresses
).toString());
158 if (config
.loadConfig()) {
159 console
.log('New config file loaded');
161 console
.log("No new config file was loaded");
170 console
.log('Rehashed');
176 console
.log('Unrecognised command: ' + data
);