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
;
28 // How to handle log output
29 this.log = function(str
, level
) {
36 * Configuration and rehashing routines
38 var config_filename
= 'config.json',
39 config_dirs
= ['/etc/kiwiirc/', this.kiwi_root
+ '/'];
42 this.loadConfig = function () {
48 for (i
in config_dirs
) {
50 if (fs
.lstatSync(config_dirs
[i
] + config_filename
).isDirectory() === false) {
52 nconf
= JSON
.parse(fs
.readFileSync(config_dirs
[i
] + config_filename
, 'ascii'));
54 // If this has changed from the previous config, mark it as changed
55 if (!_
.isEqual(this.config
[j
], nconf
[j
])) {
59 this.config
[j
] = nconf
[j
];
62 this.log('Loaded config file ' + config_dirs
[i
] + config_filename
);
67 case 'ENOENT': // No file/dir
70 this.log('An error occured parsing the config file ' + config_dirs
[i
] + config_filename
+ ': ' + e
.message
);
76 if (Object
.keys(this.config
).length
=== 0) {
78 this.log('Couldn\'t find a config file!');
82 return [nconf
, cconf
];
86 // Reloads the config during runtime
87 this.rehash = function () {
91 // Reloads app.js during runtime for any recoding
92 this.recode = function () {
93 if (typeof require
.cache
[this.kiwi_root
+ '/app.js'] !== 'undefined'){
94 delete require
.cache
[this.kiwi_root
+ '/app.js'];
98 app
= require(__dirname
+ '/app.js');
100 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
};
112 * Before we continue we need the config loaded
114 if (!this.loadConfig()) {
127 if (this.config
.handle_http
) {
128 this.fileServer
= new (require('node-static').Server
)(__dirname
+ this.config
.public_http
);
129 this.jade
= require('jade');
130 this.cache
= {alljs
: '', html
: []};
132 this.httpServers
= [];
133 this.httpHandler = function (request
, response
) {
134 return app
.httpHandler(request
, response
);
145 this.connections
= {};
147 this.websocketListen = function (servers
, handler
) {
148 return app
.websocketListen(servers
, handler
);
150 this.websocketConnection = function (websocket
) {
151 return app
.websocketConnection(websocket
);
153 this.websocketDisconnect = function () {
154 return app
.websocketDisconnect(this);
156 this.websocketMessage = function (msg
, callback
) {
157 return app
.websocketMessage(this, msg
, callback
);
159 this.websocketIRCConnect = function (nick
, host
, port
, ssl
, callback
) {
160 return app
.websocketIRCConnect(this, nick
, host
, port
, ssl
, callback
);
169 this.parseIRCMessage = function (websocket
, ircSocket
, data
) {
170 return app
.parseIRCMessage(websocket
, ircSocket
, data
);
172 this.ircSocketDataHandler = function (data
, websocket
, ircSocket
) {
173 return app
.ircSocketDataHandler(data
, websocket
, ircSocket
);
185 * Load up main application source
187 if (!this.recode()) {
193 // Set the process title
199 * Load the modules as set in the config and print them out
201 this.kiwi_mod
= require('./lib/kiwi_mod.js');
202 this.kiwi_mod
.loadModules(this.kiwi_root
, this.config
);
203 this.kiwi_mod
.printMods();
206 // Make sure Kiwi doesn't simply quit on an exception
207 process
.on('uncaughtException', function (e
) {
208 this.log('[Uncaught exception] ' + e
);
212 // Start the server up
213 this.websocketListen(this.config
.servers
, this.httpHandler
);
215 // Now we're listening on the network, set our UID/GIDs if required
218 // Listen for controll messages
219 process
.stdin
.resume();
220 process
.stdin
.on('data', function (data
) { app
.manageControll(data
); });