Commit | Line | Data |
---|---|---|
4926321f JA |
1 | /*jslint continue: true, forin: true, regexp: true, undef: false, node: true, nomen: true, plusplus: true, maxerr: 50, indent: 4 */ |
2 | "use strict"; | |
68ad40c6 | 3 | var tls = require('tls'), |
c6b5e668 | 4 | net = require('net'), |
68ad40c6 | 5 | http = require('http'), |
09365139 | 6 | https = require('https'), |
1eaf214f | 7 | fs = require('fs'), |
7d1c3ee9 | 8 | url = require('url'), |
36cc7c0e | 9 | dns = require('dns'), |
02b2c80e | 10 | crypto = require('crypto'), |
c6b5e668 | 11 | ws = require('socket.io'), |
02b2c80e JA |
12 | jsp = require("uglify-js").parser, |
13 | pro = require("uglify-js").uglify, | |
4136eb38 | 14 | _ = require('./lib/underscore.min.js'), |
fd779420 D |
15 | starttls = require('./lib/starttls.js'), |
16 | app = require(__dirname + '/app.js'); | |
17 | ||
18 | ||
19 | ||
20 | ||
c6b5e668 | 21 | |
a3364605 JA |
22 | // Libraries may need to know kiwi.js path as __dirname |
23 | // only gives that librarys path. Set it here for usage later. | |
fd779420 D |
24 | this.kiwi_root = __dirname; |
25 | ||
c6b5e668 | 26 | |
4a30a583 | 27 | |
28 | /* | |
fd779420 | 29 | * Configuration and rehashing routines |
4a30a583 | 30 | */ |
fd779420 | 31 | var config_filename = 'config.json', |
4f06269b | 32 | config_dirs = ['/etc/kiwiirc/', this.kiwi_root + '/']; |
a3364605 | 33 | |
fd779420 D |
34 | this.config = {}; |
35 | this.loadConfig = function () { | |
4926321f JA |
36 | var i, j, |
37 | nconf = {}, | |
38 | cconf = {}, | |
39 | found_config = false; | |
fd779420 | 40 | |
a3364605 JA |
41 | for (i in config_dirs) { |
42 | try { | |
43 | if (fs.lstatSync(config_dirs[i] + config_filename).isDirectory() === false) { | |
a7f1f3d6 | 44 | found_config = true; |
709031df JA |
45 | nconf = JSON.parse(fs.readFileSync(config_dirs[i] + config_filename, 'ascii')); |
46 | for (j in nconf) { | |
a7f1f3d6 | 47 | // If this has changed from the previous config, mark it as changed |
fd779420 | 48 | if (!_.isEqual(this.config[j], nconf[j])) { |
709031df JA |
49 | cconf[j] = nconf[j]; |
50 | } | |
a7f1f3d6 | 51 | |
fd779420 | 52 | this.config[j] = nconf[j]; |
709031df | 53 | } |
a7f1f3d6 | 54 | |
709031df | 55 | console.log('Loaded config file ' + config_dirs[i] + config_filename); |
a3364605 JA |
56 | break; |
57 | } | |
58 | } catch (e) { | |
4926321f | 59 | switch (e.code) { |
a7f1f3d6 D |
60 | case 'ENOENT': // No file/dir |
61 | break; | |
62 | default: | |
63 | console.log('An error occured parsing the config file ' + config_dirs[i] + config_filename + ': ' + e.message); | |
64 | return false; | |
65 | } | |
a3364605 | 66 | continue; |
4a30a583 | 67 | } |
4a30a583 | 68 | } |
fd779420 | 69 | if (Object.keys(this.config).length === 0) { |
4926321f JA |
70 | if (!found_config) { |
71 | console.log('Couldn\'t find a config file!'); | |
72 | } | |
a7f1f3d6 | 73 | return false; |
a3364605 | 74 | } |
709031df JA |
75 | return [nconf, cconf]; |
76 | }; | |
77 | ||
fd779420 D |
78 | |
79 | // Reloads the config during runtime | |
80 | this.rehash = function () { | |
81 | return app.rehash(); | |
82 | } | |
83 | ||
84 | // Reloads app.js during runtime for any recoding | |
85 | this.recode = function () { | |
4f06269b D |
86 | if (typeof require.cache[this.kiwi_root + '/app.js'] !== 'undefined'){ |
87 | delete require.cache[this.kiwi_root + '/app.js']; | |
fd779420 D |
88 | } |
89 | ||
90 | app = null; | |
91 | app = require(__dirname + '/app.js'); | |
92 | ||
93 | var objs = {tls:tls, net:net, http:http, https:https, fs:fs, url:url, dns:dns, crypto:crypto, ws:ws, jsp:jsp, pro:pro, _:_, starttls:starttls}; | |
94 | app.init(objs); | |
95 | ||
96 | return true; | |
4926321f | 97 | } |
4a30a583 | 98 | |
99 | ||
4a30a583 | 100 | |
101 | ||
102 | ||
103 | ||
104 | /* | |
fd779420 | 105 | * Before we continue we need the config loaded |
4a30a583 | 106 | */ |
fd779420 D |
107 | if (!this.loadConfig()) { |
108 | process.exit(0); | |
109 | } | |
110 | ||
111 | ||
4a30a583 | 112 | |
4a30a583 | 113 | |
114 | ||
a7f1f3d6 D |
115 | |
116 | ||
4a30a583 | 117 | /* |
fd779420 | 118 | * HTTP file serving |
4a30a583 | 119 | */ |
fd779420 D |
120 | if (this.config.handle_http) { |
121 | this.fileServer = new (require('node-static').Server)(__dirname + this.config.public_http); | |
122 | this.jade = require('jade'); | |
123 | this.cache = {alljs: '', html: []}; | |
124 | } | |
125 | this.httpServer = null; | |
126 | this.httpHandler = function (request, response) { | |
127 | return app.httpHandler(request, response); | |
128 | } | |
1eaf214f | 129 | |
c6b5e668 JA |
130 | |
131 | ||
1d90c3f1 D |
132 | |
133 | ||
b0ad9f0a | 134 | |
fd779420 D |
135 | /* |
136 | * Websocket handling | |
137 | */ | |
138 | this.connections = {}; | |
139 | this.io = null; | |
140 | this.websocketListen = function (port, host, handler, secure, key, cert) { | |
141 | return app.websocketListen(port, host, handler, secure, key, cert); | |
142 | } | |
143 | this.websocketConnection = function (websocket) { | |
144 | return app.websocketConnection(websocket); | |
145 | } | |
146 | this.websocketDisconnect = function () { | |
277b3040 | 147 | return app.websocketDisconnect(this); |
fd779420 D |
148 | } |
149 | this.websocketMessage = function (msg, callback) { | |
150 | return app.websocketMessage(this, msg, callback); | |
151 | } | |
152 | this.websocketIRCConnect = function (nick, host, port, ssl, callback) { | |
153 | return app.websocketIRCConnect(this, nick, host, port, ssl, callback); | |
154 | } | |
155 | ||
156 | ||
68ad40c6 | 157 | |
b0ad9f0a D |
158 | |
159 | /* | |
fd779420 | 160 | * IRC handling |
b0ad9f0a | 161 | */ |
fd779420 D |
162 | this.parseIRCMessage = function (websocket, ircSocket, data) { |
163 | return app.parseIRCMessage(websocket, ircSocket, data); | |
164 | } | |
165 | this.ircSocketDataHandler = function (data, websocket, ircSocket) { | |
166 | return app.ircSocketDataHandler(data, websocket, ircSocket); | |
167 | } | |
a7f1f3d6 | 168 | |
4136eb38 | 169 | |
7d1c3ee9 | 170 | |
7d1c3ee9 | 171 | |
726d3e03 | 172 | |
43bdc759 | 173 | |
43bdc759 | 174 | |
709031df | 175 | |
709031df | 176 | |
fd779420 D |
177 | /* |
178 | * Load up main application source | |
179 | */ | |
180 | if (!this.recode()) { | |
181 | process.exit(0); | |
182 | } | |
709031df | 183 | |
709031df | 184 | |
a7f1f3d6 | 185 | |
fd779420 D |
186 | // Set the process title |
187 | app.setTitle(); | |
a7f1f3d6 D |
188 | |
189 | ||
a7f1f3d6 | 190 | |
fd779420 D |
191 | /* |
192 | * Load the modules as set in the config and print them out | |
193 | */ | |
194 | this.kiwi_mod = require('./lib/kiwi_mod.js'); | |
195 | this.kiwi_mod.loadModules(this.kiwi_root, this.config); | |
196 | this.kiwi_mod.printMods(); | |
a7f1f3d6 | 197 | |
938a6630 | 198 | |
709031df | 199 | |
a7f1f3d6 | 200 | |
fd779420 D |
201 | // Start the server up |
202 | this.websocketListen(this.config.port, this.config.bind_address, this.httpHandler, this.config.listen_ssl, this.config.ssl_key, this.config.ssl_cert); | |
a7f1f3d6 | 203 | |
fd779420 D |
204 | // Now we're listening on the network, set our UID/GIDs if required |
205 | app.changeUser(); | |
c6b5e668 | 206 | |
fd779420 | 207 | // Listen for controll messages |
87a6abbe D |
208 | process.stdin.resume(); |
209 | process.stdin.on('data', function (data) { app.manageControll(data); }); | |
210 | ||
709031df JA |
211 | |
212 | ||
213 |