1 var _
= require('lodash'),
2 winston
= require('winston'),
3 WebListener
= require('./weblistener.js'),
4 config
= require('./configuration.js'),
5 modules
= require('./modules.js'),
6 Identd
= require('./identd.js'),
7 Proxy
= require('./proxy.js'),
8 ControlInterface
= require('./controlinterface.js');
12 process
.chdir(__dirname
+ '/../');
14 // Get our own version from package.json
15 global
.build_version
= require('../package.json').version
;
17 // Load the configuration
18 require('./helpers/configloader.js')();
21 // If we're not running in the forground and we have a log file.. switch
22 // console.log to output to a file
23 if (process
.argv
.indexOf('-f') === -1 && global
.config
&& global
.config
.log
) {
25 var log_file_name
= global
.config
.log
;
27 if (log_file_name
[0] !== '/') {
28 log_file_name
= __dirname
+ '/../' + log_file_name
;
31 winston
.add(winston
.transports
.File
, {
32 filename
: log_file_name
,
34 timestamp: function() {
35 var year
, month
, day
, time_string
,
38 year
= String(d
.getFullYear());
39 month
= String(d
.getMonth() + 1);
40 if (month
.length
=== 1) {
44 day
= String(d
.getDate());
45 if (day
.length
=== 1) {
49 // Take the time from the existing toTimeString() format
50 time_string
= (new Date()).toTimeString().replace(/.*(\d{2}:\d{2}:\d{2}).*/, "$1");
52 return year
+ "-" + month
+ "-" + day
+ ' ' + time_string
;
56 winston
.remove(winston
.transports
.Console
);
62 // Make sure we have a valid config file and at least 1 server
63 if (!global
.config
|| Object
.keys(global
.config
).length
=== 0) {
64 winston
.error('Couldn\'t find a valid config.js file (Did you copy the config.example.js file yet?)');
68 if ((!global
.config
.servers
) || (global
.config
.servers
.length
< 1)) {
69 winston
.error('No servers defined in config file');
76 // Create a plugin interface
77 global
.modules
= new modules
.Publisher();
79 // Register as the active interface
80 modules
.registerPublisher(global
.modules
);
82 // Load any modules in the config
83 if (global
.config
.module_dir
) {
84 (global
.config
.modules
|| []).forEach(function (module_name
) {
85 if (modules
.load(module_name
)) {
86 winston
.info('Module %s loaded successfully', module_name
);
88 winston
.warn('Module %s failed to load', module_name
);
96 // Holder for all the connected clients
98 clients
: Object
.create(null),
99 addresses
: Object
.create(null),
101 // Local and foriegn port pairs for identd lookups
102 // {'65483_6667': client_obj, '54356_6697': client_obj}
105 add: function (client
) {
106 this.clients
[client
.hash
] = client
;
107 if (typeof this.addresses
[client
.real_address
] === 'undefined') {
108 this.addresses
[client
.real_address
] = Object
.create(null);
110 this.addresses
[client
.real_address
][client
.hash
] = client
;
113 remove: function (client
) {
114 if (typeof this.clients
[client
.hash
] !== 'undefined') {
115 delete this.clients
[client
.hash
];
116 delete this.addresses
[client
.real_address
][client
.hash
];
117 if (Object
.keys(this.addresses
[client
.real_address
]).length
< 1) {
118 delete this.addresses
[client
.real_address
];
123 numOnAddress: function (addr
) {
124 if (typeof this.addresses
[addr
] !== 'undefined') {
125 return Object
.keys(this.addresses
[addr
]).length
;
131 broadcastKiwiCommand: function (command
, data
, callback
) {
134 // Get an array of clients for us to work with
135 for (var client
in global
.clients
.clients
) {
136 clients
.push(global
.clients
.clients
[client
]);
140 // Sending of the command in batches
141 var sendCommandBatch = function (list
) {
142 var batch_size
= 100,
145 if (list
.length
>= batch_size
) {
146 // If we have more clients than our batch size, call ourself with the next batch
147 setTimeout(function () {
148 sendCommandBatch(list
.slice(batch_size
));
154 cutoff
= list
.length
;
157 list
.slice(0, cutoff
).forEach(function (client
) {
158 if (!client
.disposed
) {
159 client
.sendKiwiCommand(command
, data
);
163 if (cutoff
=== list
.length
&& typeof callback
=== 'function') {
168 sendCommandBatch(clients
);
173 servers
: Object
.create(null),
175 addConnection: function (connection
) {
176 var host
= connection
.irc_host
.hostname
;
177 if (!this.servers
[host
]) {
178 this.servers
[host
] = [];
180 this.servers
[host
].push(connection
);
183 removeConnection: function (connection
) {
184 var host
= connection
.irc_host
.hostname
185 if (this.servers
[host
]) {
186 this.servers
[host
] = _
.without(this.servers
[host
], connection
);
187 if (this.servers
[host
].length
=== 0) {
188 delete this.servers
[host
];
193 numOnHost: function (host
) {
194 if (this.servers
[host
]) {
195 return this.servers
[host
].length
;
205 * When a new config is loaded, send out an alert to the clients so
206 * so they can reload it
208 config
.on('loaded', function () {
209 global
.clients
.broadcastKiwiCommand('reconfig');
217 if (global
.config
.identd
&& global
.config
.identd
.enabled
) {
218 var identd_resolve_user = function(port_here
, port_there
) {
219 var key
= port_here
.toString() + '_' + port_there
.toString();
221 if (typeof global
.clients
.port_pairs
[key
] == 'undefined') {
225 return global
.clients
.port_pairs
[key
].username
;
228 var identd_server
= new Identd({
229 bind_addr
: global
.config
.identd
.address
,
230 bind_port
: global
.config
.identd
.port
,
231 user_id
: identd_resolve_user
234 identd_server
.start();
245 // Start up a weblistener for each found in the config
246 _
.each(global
.config
.servers
, function (server
) {
247 if (server
.type
== 'proxy') {
248 // Start up a kiwi proxy server
249 var serv
= new Proxy
.ProxyServer();
250 serv
.listen(server
.port
, server
.address
, server
);
252 serv
.on('listening', function() {
253 winston
.info('Kiwi proxy listening on %s:%s %s SSL', server
.address
, server
.port
, (server
.ssl
? 'with' : 'without'));
256 serv
.on('socket_connected', function(pipe
) {
257 // SSL connections have the raw socket as a property
258 var socket
= pipe
.irc_socket
.socket
?
259 pipe
.irc_socket
.socket
:
262 pipe
.identd_pair
= socket
.localPort
.toString() + '_' + socket
.remotePort
.toString();
263 global
.clients
.port_pairs
[pipe
.identd_pair
] = pipe
.meta
;
266 serv
.on('connection_close', function(pipe
) {
267 delete global
.clients
.port_pairs
[pipe
.identd_pair
];
271 // Start up a kiwi web server
272 var wl
= new WebListener(server
, global
.config
.transports
);
274 wl
.on('connection', function (client
) {
278 wl
.on('client_dispose', function (client
) {
279 clients
.remove(client
);
282 wl
.on('listening', function () {
283 winston
.info('Listening on %s:%s %s SSL', server
.address
, server
.port
, (server
.ssl
? 'with' : 'without'));
284 webListenerRunning();
287 wl
.on('error', function (err
) {
288 winston
.info('Error listening on %s:%s: %s', server
.address
, server
.port
, err
.code
);
289 // TODO: This should probably be refactored. ^JA
290 webListenerRunning();
295 // Once all the listeners are listening, set the processes UID/GID
296 var num_listening
= 0;
297 function webListenerRunning() {
299 if (num_listening
=== global
.config
.servers
.length
) {
312 process
.title
= 'kiwiirc';
315 function setProcessUid() {
316 if ((global
.config
.group
) && (global
.config
.group
!== '')) {
317 process
.setgid(global
.config
.group
);
319 if ((global
.config
.user
) && (global
.config
.user
!== '')) {
320 process
.setuid(global
.config
.user
);
325 // Make sure Kiwi doesn't simply quit on an exception
326 process
.on('uncaughtException', function (e
) {
327 winston
.error('[Uncaught exception] %s', e
, {stack
: e
.stack
});
331 process
.on('SIGUSR1', function() {
332 if (config
.loadConfig()) {
333 winston
.info('New config file loaded');
335 winston
.info('No new config file was loaded');
340 process
.on('SIGUSR2', function() {
341 winston
.info('Connected clients: %s', _
.size(global
.clients
.clients
));
342 winston
.info('Num. remote hosts: %s', _
.size(global
.clients
.addresses
));
347 * Listen for runtime commands
349 process
.stdin
.resume();
350 new ControlInterface(process
.stdin
, process
.stdout
, {prompt
: ''});