var irc = require("irc");
var repl = require("repl");
var fs = require("fs");

var config = JSON.parse(fs.readFileSync("config.json"));
var defcfg = {
	autoRejoin: false,
	channels: [],
	messageSplit: 300,
};
config.__proto__ = defcfg;

var bot = new irc.Client(config.server, config.nick, config);

bot.conn.setTimeout(180 * 1000);
bot.conn.on("timeout", function() {
	bot.conn.destroy();
	//bot.connect();
});

bot.on("error", function(message) {
	console.log(message);
});

bot.on("registered", function(message) {
	bot.send("UMODE2", "+B");
});

bot.plugins = require("./plugins");

fs.readFile("autoload.json", function(err, data) {
	var autoload = JSON.parse(data).autoload;
	for (var i = 0; i < autoload.length; i++) {
		console.log(autoload[i]);
		bot.plugins.load(autoload[i]);
	}
});

bot.saver = setInterval(function() {
	for (var name in bot.plugins) {
		if (bot.plugins[name].name) {
			bot.plugins.save(name);
		}
	}
}, 10 * 60 * 1000);

process.on("SIGINT", function() {
	for (var name in bot.plugins) {
		if (bot.plugins[name].name) {
			bot.plugins.unload(name);
		}
	}
	process.exit(0);
});

process.on("uncaughtException", function(e) {
	console.trace("ERROR process uncaught: " + e);
});

repl.start({
	useGlobal: true
}).context.bot = bot;


@@ -784,24 +784,52 @@
 %5D);%0A%09%7D%0A%7D);%0A%0A
+if (config.saveinterval) %7B%0A%09
 bot.saver = 
@@ -845,32 +845,33 @@
 al(function() %7B%0A
+%09
 %09for (var name i
@@ -881,32 +881,33 @@
 ot.plugins) %7B%0A%09%09
+%09
 if (bot.plugins%5B
@@ -915,32 +915,33 @@
 ame%5D.name) %7B%0A%09%09%09
+%09
 bot.plugins.save
@@ -954,18 +954,38 @@
 ;%0A%09%09
+%09
 %7D%0A
+%09
 %09%7D%0A
+%09
 %7D, 
-10
+config.saveinterval
  * 6
@@ -994,16 +994,18 @@
 * 1000);
+%0A%7D
 %0A%0Aproces

Read save interval length from global config