1 /*jslint continue: true, forin: true, regexp: true, undef: false, node: true, nomen: true, plusplus: true, maxerr: 50, indent: 4 */
3 var tls
= require('tls'),
5 http
= require('http'),
6 https
= require('https'),
10 crypto
= require('crypto'),
11 ws
= require('socket.io'),
12 jsp
= require("uglify-js").parser
,
13 pro
= require("uglify-js").uglify
,
14 _
= require('./lib/underscore.min.js'),
15 starttls
= require('./lib/starttls.js'),
16 app
= require(__dirname
+ '/app.js');
22 // Libraries may need to know kiwi.js path as __dirname
23 // only gives that librarys path. Set it here for usage later.
24 this.kiwi_root
= __dirname
;
29 * Configuration and rehashing routines
31 var config_filename
= 'config.json',
32 config_dirs
= ['/etc/kiwiirc/', __dirname
+ '/'];
35 this.loadConfig = function () {
41 for (i
in config_dirs
) {
43 if (fs
.lstatSync(config_dirs
[i
] + config_filename
).isDirectory() === false) {
45 nconf
= JSON
.parse(fs
.readFileSync(config_dirs
[i
] + config_filename
, 'ascii'));
47 // If this has changed from the previous config, mark it as changed
48 if (!_
.isEqual(this.config
[j
], nconf
[j
])) {
52 this.config
[j
] = nconf
[j
];
55 console
.log('Loaded config file ' + config_dirs
[i
] + config_filename
);
60 case 'ENOENT': // No file/dir
63 console
.log('An error occured parsing the config file ' + config_dirs
[i
] + config_filename
+ ': ' + e
.message
);
69 if (Object
.keys(this.config
).length
=== 0) {
71 console
.log('Couldn\'t find a config file!');
75 return [nconf
, cconf
];
79 // Reloads the config during runtime
80 this.rehash = function () {
84 // Reloads app.js during runtime for any recoding
85 this.recode = function () {
86 if (typeof require
.cache
[__dirname
+ '/app.js'] !== 'undefined'){
87 delete require
.cache
[__dirname
+ '/app.js'];
91 app
= require(__dirname
+ '/app.js');
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
};
105 * Before we continue we need the config loaded
107 if (!this.loadConfig()) {
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
: []};
125 this.httpServer
= null;
126 this.httpHandler = function (request
, response
) {
127 return app
.httpHandler(request
, response
);
138 this.connections
= {};
140 this.websocketListen = function (port
, host
, handler
, secure
, key
, cert
) {
141 return app
.websocketListen(port
, host
, handler
, secure
, key
, cert
);
143 this.websocketConnection = function (websocket
) {
144 return app
.websocketConnection(websocket
);
146 this.websocketDisconnect = function () {
147 return app
.websocketDisconnect();
149 this.websocketMessage = function (msg
, callback
) {
150 return app
.websocketMessage(this, msg
, callback
);
152 this.websocketIRCConnect = function (nick
, host
, port
, ssl
, callback
) {
153 return app
.websocketIRCConnect(this, nick
, host
, port
, ssl
, callback
);
162 this.parseIRCMessage = function (websocket
, ircSocket
, data
) {
163 return app
.parseIRCMessage(websocket
, ircSocket
, data
);
165 this.ircSocketDataHandler = function (data
, websocket
, ircSocket
) {
166 return app
.ircSocketDataHandler(data
, websocket
, ircSocket
);
178 * Load up main application source
180 if (!this.recode()) {
186 // Set the process title
192 * Load the modules as set in the config and print them out
194 this.kiwi_mod
= require('./lib/kiwi_mod.js');
195 this.kiwi_mod
.loadModules(this.kiwi_root
, this.config
);
196 this.kiwi_mod
.printMods();
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
);
204 // Now we're listening on the network, set our UID/GIDs if required
207 // Listen for controll messages