Server: Updated stats command to new client store
[KiwiIRC.git] / server / kiwi.js
1 var fs = require('fs'),
2 _ = require('underscore'),
3 WebListener = require('./weblistener.js'),
4 config = require('./configuration.js'),
5 rehash = require('./rehash.js');
6
7
8
9 process.chdir(__dirname + '/../');
10 config.loadConfig();
11
12
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) {
16 (function () {
17 var log_file_name = config.get().log;
18
19 if (log_file_name[0] !== '/') {
20 log_file_name = __dirname + '/../' + log_file_name;
21 }
22
23
24
25 console.log = function() {
26 var logfile = fs.openSync(log_file_name, 'a'),
27 out;
28
29 out = Array.prototype.join.apply(arguments, [' ']);
30
31 // Make sure we out somthing to log and we have an open file
32 if (!out || !logfile) return;
33
34 out += '\n';
35 fs.writeSync(logfile, out, null);
36
37 fs.closeSync(logfile);
38 };
39 })();
40 }
41
42
43
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!');
47 process.exit(1);
48 }
49
50 if ((!config.get().servers) || (config.get().servers.length < 1)) {
51 console.log('No servers defined in config file');
52 process.exit(2);
53 }
54
55
56
57
58
59 // Holder for all the connected clients
60 global.clients = {
61 clients: Object.create(null),
62 addresses: Object.create(null),
63
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);
68 }
69 this.addresses[client.real_address][client.hash] = client;
70 },
71
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];
78 }
79 }
80 },
81
82 numOnAddress: function (addr) {
83 if (typeof this.addresses[addr] !== 'undefined') {
84 return Object.keys(this.addresses[addr]).length;
85 } else {
86 return 0;
87 }
88 }
89 };
90
91
92
93
94 /*
95 * Web listeners
96 */
97
98
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);
102
103 wl.on('connection', function (client) {
104 clients.add(client);
105 });
106
107 wl.on('destroy', function (client) {
108 clients.remove(client);
109 });
110 });
111
112
113
114
115
116 /*
117 * Process settings
118 */
119
120 // Set process title
121 process.title = 'kiwiirc';
122
123 // Change UID/GID
124 if ((config.get().group) && (config.get().group !== '')) {
125 process.setgid(config.get().group);
126 }
127 if ((config.get().user) && (config.get().user !== '')) {
128 process.setuid(config.get().user);
129 }
130
131
132
133 /*
134 * Listen for runtime commands
135 */
136
137 process.stdin.resume();
138 process.stdin.on('data', function (buffered) {
139 var data = buffered.toString().trim();
140
141 switch (data) {
142 case 'stats':
143 console.log('Connected clients: ' + _.size(global.clients.clients).toString());
144 console.log('Num. remote hosts: ' + _.size(global.clients.addresses).toString());
145 break;
146
147 case 'reconfig':
148 (function () {
149 if (config.loadConfig()) {
150 console.log('New config file loaded');
151 } else {
152 console.log("No new config file was loaded");
153 }
154 })();
155
156 break;
157
158
159 case 'rehash':
160 (function () {
161 rehash.rehashAll();
162 console.log('Rehashed');
163 })();
164
165 break;
166
167 default:
168 console.log('Unrecognised command: ' + data);
169 }
170 });