From aa02b0eaa72e8942ac33ecd506ed98817e2eca5d Mon Sep 17 00:00:00 2001 From: Mattias Erming Date: Sun, 12 Oct 2014 00:47:24 +0200 Subject: [PATCH] Added client save --- src/client.js | 41 ++++++++++++++++++++++++++++++++++++++--- src/models/network.js | 20 ++++++++++++++------ 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/src/client.js b/src/client.js index df8eb12..48a9464 100644 --- a/src/client.js +++ b/src/client.js @@ -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); + } + } + ); + }); }; diff --git a/src/models/network.js b/src/models/network.js index 88d4680..efd302c 100644 --- a/src/models/network.js +++ b/src/models/network.js @@ -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; };