Commit | Line | Data |
---|---|---|
a8bf3ea4 | 1 | var fs = require('fs'), |
bd299b17 | 2 | _ = require('underscore'), |
1360a454 | 3 | WebListener = require('./weblistener.js'), |
1286229a D |
4 | config = require('./configuration.js'), |
5 | rehash = require('./rehash.js'); | |
fd779420 | 6 | |
186531ed D |
7 | |
8 | ||
11dbb00f D |
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 | } | |
4a30a583 | 41 | |
ab15f618 | 42 | |
ab15f618 | 43 | |
186531ed | 44 | // Make sure we have a valid config file and at least 1 server |
1360a454 | 45 | if (Object.keys(config.get()).length === 0) { |
a8bf3ea4 JA |
46 | console.log('Couldn\'t find a valid config file!'); |
47 | process.exit(1); | |
fd779420 D |
48 | } |
49 | ||
1360a454 | 50 | if ((!config.get().servers) || (config.get().servers.length < 1)) { |
a8bf3ea4 JA |
51 | console.log('No servers defined in config file'); |
52 | process.exit(2); | |
fd779420 D |
53 | } |
54 | ||
55 | ||
186531ed D |
56 | |
57 | ||
58 | ||
59 | /* | |
60 | * Web listeners | |
61 | */ | |
62 | ||
63 | // Holder for all the connected clients | |
c36ed4eb JA |
64 | global.clients = { clients: Object.create(null), |
65 | addresses: Object.create(null), | |
66 | add: function (client) { | |
67 | this.clients[client.hash] = client; | |
68 | if (typeof this.addresses[client.real_address] === 'undefined') { | |
69 | this.addresses[client.real_address] = Object.create(null); | |
70 | } | |
71 | this.addresses[client.real_address][client.hash] = client; | |
72 | }, | |
73 | remove: function (client) { | |
74 | if (typeof this.clients[client.hash] !== 'undefined') { | |
75 | delete this.clients[client.hash]; | |
76 | delete this.addresses[client.real_address][client.hash]; | |
77 | if (Object.keys(this.addresses[client.real_address]).length < 1) { | |
78 | delete this.addresses[client.real_address]; | |
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 | }; | |
186531ed D |
90 | |
91 | // Start up a weblistener for each found in the config | |
1360a454 D |
92 | _.each(config.get().servers, function (server) { |
93 | var wl = new WebListener(server, config.get().transports); | |
a8bf3ea4 | 94 | wl.on('connection', function (client) { |
c36ed4eb JA |
95 | clients.add(client); |
96 | console.log(clients); | |
a8bf3ea4 JA |
97 | }); |
98 | wl.on('destroy', function (client) { | |
c36ed4eb JA |
99 | clients.remove(client); |
100 | //client.dispose(); | |
101 | console.log(clients); | |
a8bf3ea4 JA |
102 | }); |
103 | }); | |
68ad40c6 | 104 | |
b0ad9f0a | 105 | |
f52d8543 | 106 | |
186531ed D |
107 | |
108 | ||
109 | /* | |
110 | * Process settings | |
111 | */ | |
112 | ||
113 | // Set process title | |
114 | process.title = 'kiwiirc'; | |
115 | ||
116 | // Change UID/GID | |
1360a454 | 117 | if ((config.get().group) && (config.get().group !== '')) { |
6b586d2e D |
118 | process.setgid(config.get().group); |
119 | } | |
120 | if ((config.get().user) && (config.get().user !== '')) { | |
121 | process.setuid(config.get().user); | |
fd779420 | 122 | } |
709031df | 123 | |
186531ed D |
124 | |
125 | ||
126 | /* | |
127 | * Listen for runtime commands | |
128 | */ | |
129 | ||
87a6abbe | 130 | process.stdin.resume(); |
186531ed D |
131 | process.stdin.on('data', function (buffered) { |
132 | var data = buffered.toString().trim(); | |
133 | ||
134 | switch (data) { | |
135 | case 'stats': | |
136 | console.log('Connected clients: ' + _.size(clients).toString()); | |
137 | break; | |
138 | ||
ab15f618 D |
139 | case 'reconfig': |
140 | (function () { | |
1360a454 | 141 | if (config.loadConfig()) { |
ab15f618 D |
142 | console.log('New config file loaded'); |
143 | } else { | |
144 | console.log("No new config file was loaded"); | |
145 | } | |
146 | })(); | |
147 | ||
148 | break; | |
149 | ||
1286229a D |
150 | |
151 | case 'rehash': | |
152 | (function () { | |
153 | rehash.rehashAll(); | |
154 | console.log('Rehashed'); | |
155 | })(); | |
156 | ||
157 | break; | |
158 | ||
186531ed D |
159 | default: |
160 | console.log('Unrecognised command: ' + data); | |
161 | } | |
a8bf3ea4 | 162 | }); |