Added client save

This commit is contained in:
Mattias Erming 2014-10-12 00:47:24 +02:00
parent 81401cec0f
commit aa02b0eaa7
2 changed files with 52 additions and 9 deletions

View File

@ -1,6 +1,7 @@
var _ = require("lodash");
var Chan = require("./models/chan");
var crypto = require("crypto");
var fs = require("fs");
var identd = require("./identd");
var log = require("./log");
var net = require("net");
@ -133,7 +134,7 @@ Client.prototype.connect = function(args) {
server.socket = socket;
}
}
var stream = args.tls ? tls.connect(server) : net.connect(server);
(stream.socket || stream).on("error", function(e) {
@ -167,7 +168,7 @@ Client.prototype.connect = function(args) {
name: server.name,
host: server.host,
port: server.port,
tls: args.tls,
tls: !!args.tls,
password: args.password,
username: username,
realname: realname,
@ -320,5 +321,39 @@ Client.prototype.quit = function() {
};
Client.prototype.save = function() {
var networks = _.map(this.networks, function(n) { return n.export(); });
var name = this.name;
var path = Helper.HOME + "/users/" + name + "/user.json";
var networks = _.map(
this.networks,
function(n) {
return n.export();
}
);
var json = {};
fs.readFile(path, "utf-8", function(err, data) {
if (err) {
console.log(err);
return;
}
try {
json = JSON.parse(data);
json.networks = networks;
} catch(e) {
console.log(e);
}
fs.writeFile(
path,
JSON.stringify(json, null, " "),
{mode: "0777"},
function(err) {
if (err) {
console.log(err);
}
}
);
});
};

View File

@ -21,7 +21,10 @@ function Network(attr) {
}, attr));
this.name = attr.name || prettify(attr.host);
this.channels.unshift(
new Chan({name: this.name, type: Chan.Type.LOBBY})
new Chan({
name: this.name,
type: Chan.Type.LOBBY
})
);
}
@ -31,15 +34,20 @@ Network.prototype.toJSON = function() {
};
Network.prototype.export = function() {
var network = _.pick(
this,
["name", "host", "port", "tls", "password", "username", "realname"]
);
var network = _.pick(this, [
"name",
"host",
"port",
"tls",
"password",
"username",
"realname"
]);
network.nick = (this.irc || {}).me;
network.join = _.pluck(
_.where(this.channels, {type: "channel"}),
"name"
);
).join(",");
return network;
};