Mercurial > psgxs
comparison backends/directory.js @ 56:99bd1d1ac071
Migration to node-xmpp, done!
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Wed, 10 Aug 2011 15:11:22 -0700 |
parents | fd69d35cf2e6 |
children |
comparison
equal
deleted
inserted
replaced
55:fd69d35cf2e6 | 56:99bd1d1ac071 |
---|---|
72 }); | 72 }); |
73 }); | 73 }); |
74 } | 74 } |
75 | 75 |
76 backend.load = function(dir) { | 76 backend.load = function(dir) { |
77 var parse = require('ltx').parse; | |
77 if (!dir) | 78 if (!dir) |
78 dir = 'data'; | 79 dir = 'data'; |
79 | 80 |
80 fs.stat(dir, function(err, stats) { | 81 fs.stat(dir, function(err, stats) { |
81 if (err) | 82 if (err) |
84 console.log('\x1b[1;41mThe file “' + dir + '” isn’t a directory. Exiting.\x1b[0m'); | 85 console.log('\x1b[1;41mThe file “' + dir + '” isn’t a directory. Exiting.\x1b[0m'); |
85 process.exit(1); | 86 process.exit(1); |
86 } | 87 } |
87 | 88 |
88 fs.readdir(dir, function(err, files) { | 89 fs.readdir(dir, function(err, files) { |
89 var xmpp = require('xmpp'); | |
90 function parseStanza(path, content) { | |
91 var stanza = null; | |
92 var stream = new xmpp.Stream({ | |
93 stanza: function (stanza) { | |
94 path[content] = stanza; | |
95 } | |
96 }); | |
97 stream.opened = true; | |
98 stream.data(path[content]); | |
99 } | |
100 | |
101 function endParsing(o) { | 90 function endParsing(o) { |
102 var regexp = /\d{4}-\d\d-\d\dT\d\d:\d\d:\d\dZ/; | 91 var regexp = /\d{4}-\d\d-\d\dT\d\d:\d\d:\d\dZ/; |
103 for (var i in o) { | 92 for (var i in o) { |
104 if (typeof o[i] == 'string' && i == 'content') | 93 if (typeof o[i] == 'string' && i == 'content') |
105 parseStanza(o, i); | 94 o[i] = parse(o[i]); |
106 else if (typeof o[i] == 'string' && regexp(o[i])) { | 95 else if (typeof o[i] == 'string' && regexp(o[i])) { |
107 var today = new Date(); | 96 var today = new Date(); |
108 today.setFromISO8601(o[i]); | 97 today.setFromISO8601(o[i]); |
109 o[i] = today; | 98 o[i] = today; |
110 } else if (typeof o[i] == 'object') | 99 } else if (typeof o[i] == 'object') |
118 for (var i in files) { | 107 for (var i in files) { |
119 var file = files[i]; | 108 var file = files[i]; |
120 if (/^\./.test(file)) | 109 if (/^\./.test(file)) |
121 continue; | 110 continue; |
122 | 111 |
112 if (!/\.json$/.test(file)) | |
113 continue; | |
114 | |
123 var nodeID = decodeURI(file.replace(/\.json$/, '').replace('%2f', '/')); | 115 var nodeID = decodeURI(file.replace(/\.json$/, '').replace('%2f', '/')); |
124 | 116 |
125 var data = fs.readFileSync(dir+'/'+file).toString(); | 117 var data = fs.readFileSync(dir+'/'+file).toString(); |
126 | 118 |
127 var obj = eval('('+data+')'); | 119 var obj = eval('('+data+')'); |