Mercurial > psgxs
comparison backends/file.js @ 27:a36a514e8be8
Learned that “hg addremove” can be good, sometimes.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Mon, 01 Nov 2010 02:41:04 +0100 |
parents | |
children | 7cfcd7d5796c |
comparison
equal
deleted
inserted
replaced
26:f2e9365bc8ec | 27:a36a514e8be8 |
---|---|
1 /* | |
2 * Copyright (C) 2010 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> | |
3 * | |
4 * This file is part of PSĜS, a PubSub server written in JavaScript. | |
5 * | |
6 * PSĜS is free software: you can redistribute it and/or modify | |
7 * it under the terms of the GNU Affero General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License. | |
10 * | |
11 * PSĜS is distributed in the hope that it will be useful, | |
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 * GNU Affero General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU Affero General Public License | |
17 * along with PSĜS. If not, see <http://www.gnu.org/licenses/>. | |
18 */ | |
19 | |
20 var fs = require('fs'); | |
21 var backend = exports; | |
22 | |
23 backend.save = function(file) { | |
24 function sanitize(o) { | |
25 var n = {}; | |
26 for (var i in o) { | |
27 if (i == 'content' || o[i].setISO8601) | |
28 n[i] = o[i].toString(); | |
29 else if (o[i] instanceof Array) | |
30 n[i] = o[i]; | |
31 else if (typeof o[i] == 'object') | |
32 n[i] = sanitize(o[i]); | |
33 else if (typeof o[i] == 'function') | |
34 ; | |
35 else | |
36 n[i] = o[i]; | |
37 } | |
38 return n; | |
39 } | |
40 | |
41 var data = sanitize(backend.list); | |
42 | |
43 if (!file) | |
44 file = 'save.json'; | |
45 | |
46 fs.writeFile(file, require('sys').inspect(data, null, null)); | |
47 } | |
48 | |
49 backend.load = function(file) { | |
50 var xmpp = require('xmpp'); | |
51 function parseStanza(path, content) { | |
52 var stanza = null; | |
53 var stream = new xmpp.Stream({ | |
54 stanza: function (stanza) { | |
55 path[content] = stanza; | |
56 } | |
57 }); | |
58 stream.opened = true; | |
59 stream.data(path[content]); | |
60 } | |
61 | |
62 function endParsing(o) { | |
63 var regexp = /\d{4}-\d\d-\d\dT\d\d:\d\d:\d\dZ/; | |
64 for (var i in o) { | |
65 if (typeof o[i] == 'string' && i == 'content') | |
66 parseStanza(o, i); | |
67 else if (typeof o[i] == 'string' && regexp(o[i])) { | |
68 var today = new Date(); | |
69 today.setISO8601(o[i]); | |
70 o[i] = today; | |
71 } else if (typeof o[i] == 'object') | |
72 endParsing(o[i]); | |
73 } | |
74 return o; | |
75 } | |
76 | |
77 if (!file) | |
78 file = 'save.json'; | |
79 | |
80 fs.stat(file, function(err, stats) { | |
81 if (err) | |
82 fs.writeFileSync(file, '{}'); | |
83 else if (!stats.isFile()) { | |
84 console.log('\033[1;41mThe file “' + file + '” isn’t a regular file. Exiting.\033[0m'); | |
85 process.exit(1); | |
86 } | |
87 | |
88 var data = fs.readFileSync(file); | |
89 var obj = eval('('+data+')'); | |
90 backend.list = endParsing(obj); | |
91 | |
92 for (var i in backend.list) | |
93 for (var j in Node.prototype) | |
94 backend.list[i][j] = Node.prototype[j]; | |
95 }); | |
96 } |