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 _ = require("lodash");
var Chan = require("./models/chan"); var Chan = require("./models/chan");
var crypto = require("crypto"); var crypto = require("crypto");
var fs = require("fs");
var identd = require("./identd"); var identd = require("./identd");
var log = require("./log"); var log = require("./log");
var net = require("net"); var net = require("net");
@ -133,7 +134,7 @@ Client.prototype.connect = function(args) {
server.socket = socket; server.socket = socket;
} }
} }
var stream = args.tls ? tls.connect(server) : net.connect(server); var stream = args.tls ? tls.connect(server) : net.connect(server);
(stream.socket || stream).on("error", function(e) { (stream.socket || stream).on("error", function(e) {
@ -167,7 +168,7 @@ Client.prototype.connect = function(args) {
name: server.name, name: server.name,
host: server.host, host: server.host,
port: server.port, port: server.port,
tls: args.tls, tls: !!args.tls,
password: args.password, password: args.password,
username: username, username: username,
realname: realname, realname: realname,
@ -320,5 +321,39 @@ Client.prototype.quit = function() {
}; };
Client.prototype.save = 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)); }, attr));
this.name = attr.name || prettify(attr.host); this.name = attr.name || prettify(attr.host);
this.channels.unshift( 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() { Network.prototype.export = function() {
var network = _.pick( var network = _.pick(this, [
this, "name",
["name", "host", "port", "tls", "password", "username", "realname"] "host",
); "port",
"tls",
"password",
"username",
"realname"
]);
network.nick = (this.irc || {}).me; network.nick = (this.irc || {}).me;
network.join = _.pluck( network.join = _.pluck(
_.where(this.channels, {type: "channel"}), _.where(this.channels, {type: "channel"}),
"name" "name"
); ).join(",");
return network; return network;
}; };