summaryrefslogtreecommitdiff
path: root/frontend/delta/js/Clipperz/KeePassExportProcessor.js
Unidiff
Diffstat (limited to 'frontend/delta/js/Clipperz/KeePassExportProcessor.js') (more/less context) (ignore whitespace changes)
-rw-r--r--frontend/delta/js/Clipperz/KeePassExportProcessor.js191
1 files changed, 191 insertions, 0 deletions
diff --git a/frontend/delta/js/Clipperz/KeePassExportProcessor.js b/frontend/delta/js/Clipperz/KeePassExportProcessor.js
new file mode 100644
index 0000000..e35d729
--- a/dev/null
+++ b/frontend/delta/js/Clipperz/KeePassExportProcessor.js
@@ -0,0 +1,191 @@
1/*
2
3Copyright 2008-2013 Clipperz Srl
4
5This file is part of Clipperz, the online password manager.
6For further information about its features and functionalities please
7refer to http://www.clipperz.com.
8
9* Clipperz is free software: you can redistribute it and/or modify it
10 under the terms of the GNU Affero General Public License as published
11 by the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14* Clipperz is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 See the GNU Affero General Public License for more details.
18
19* You should have received a copy of the GNU Affero General Public
20 License along with Clipperz. If not, see http://www.gnu.org/licenses/.
21
22*/
23
24if (typeof(Clipperz) == 'undefined') { Clipperz = {}; }
25
26
27Clipperz.KeePassExportProcessor = function(args) {
28 args = args || {};
29
30 return this;
31}
32
33//=============================================================================
34
35Clipperz.KeePassExportProcessor.prototype = MochiKit.Base.update(null, {
36
37 //-------------------------------------------------------------------------
38
39 'deferredParse_core': function(aContext) {
40 var deferredResult;
41
42 if (aContext.line == "") {
43 deferredResult = MochiKit.Async.succeed(aContext.result);
44 } else {
45 var record;
46
47 record = this.parseRecord(aContext);
48 if (record != null) {
49 aContext.result.push(record);
50 }
51
52 aContext.line = aContext.line.replace(/^\n*/g, "").replace(/\n$/g, "");
53
54 deferredResult = new Clipperz.Async.Deferred("KeePassExportProcessor.deferredParse_core");
55 deferredResult.addCallbackPass(MochiKit.Signal.signal, this, 'importProcessorProgressUpdate', {status:'processing', size:aContext.size, progress:(aContext.size - aContext.line.length)});
56 deferredResult.addCallback(MochiKit.Async.wait, 0.2);
57 deferredResult.addMethod(this, 'deferredParse_core');
58 deferredResult.callback(aContext);
59 }
60
61 return deferredResult;
62 },
63
64 //.........................................................................
65
66 'deferredParse': function(aValue) {
67 var deferredResult;
68 var lines;
69 var context;
70
71 lines = aValue.replace(/\r?\n/g, "\n");
72 context = {
73 line: lines,
74 size: lines.length,
75 result: []
76 }
77
78 deferredResult = new Clipperz.Async.Deferred("KeePassExportProcessor.deferredResult");
79 deferredResult.addMethod(this, 'deferredParse_core');
80 deferredResult.callback(context);
81
82 return deferredResult;
83 },
84
85 //-------------------------------------------------------------------------
86
87 'parseRecord': function(aContext) {
88 var result;
89 var recordLabelRegexp;
90 varfieldLabelRegexp;
91 var fieldValueRegexp;
92 var fullLineRegexp;
93/*
94[Record name]
95Group Tree:
96UserName:
97URL:
98Password:
99Notes: test
100UUID: 525f62430079bae48b79ed2961924b05
101Icon: 0
102Creation Time: 2007-06-26 17:56:03
103Last Access: 2007-10-25 16:23:51
104Last Modification: 2007-10-25 16:23:51
105Expires: 2999-12-28 23:59:59
106
107 [Record name] ==> Title
108 Group: General ==> Group
109 Group Tree: ==> Group Tree
110 UserName: ==> UserName
111 URL: ==>URL
112 Password: ==>Password
113 Notes: test ==>Notes
114 UUID: 525f62430079bae48b79ed2961924b05 ==>UUID
115 Icon: 0 ==>Icon
116 Creation Time: 2007-06-26 17:56:03 ==>Creation Time
117 Last Access: 2007-10-25 16:23:51 ==>Last Access
118 Last Modification: 2007-10-25 16:23:51 ==>Last Modification
119 Expires: 2999-12-28 23:59:59 ==> Expires
120 Attachment Description: ==> Attachment Description
121 Attachment: ==> Attachment
122*/
123 // recordLabelRegexp = new RegExp("(^\\[(.*)\\]\\n|^Title:\s*(.*)\\n)");
124 recordLabelRegexp = new RegExp("^\\[(.*)\\]\\n|^Title:\s*(.*)\\n");
125 fieldLabelRegexp = new RegExp("^\s?(Group|Group Tree|Username|UserName|User Name|Url|URL|Password|Notes|Comment|UUID|Icon|Creation Time|Last Access|Last Modification|Expires|Attachment Description|Attachment|Valid until): ");
126 fieldValueRegexp = new RegExp("(.*)(\\n|$)");
127 fullLineRegexp = new RegExp("^(.*\\n)");
128
129 if (recordLabelRegexp.test(aContext.line) == true) {
130 var line;
131
132 line = aContext.line;
133
134 result = {};
135 result['Title'] = line.match(recordLabelRegexp)[1];
136 line = line.replace(/^.*\n/, "");
137 while (fieldLabelRegexp.test(line) == true) {
138 var fieldName;
139 var fieldValue;
140
141 fieldName = RegExp.$1;
142 line = RegExp.rightContext;
143
144 fieldValue = line.match(fieldValueRegexp)[1];
145 line = RegExp.rightContext;
146
147 if (fieldName == 'Notes') {
148 var isMultiline;
149
150 isMultiline = false;
151
152 if ((line != "") && (fieldLabelRegexp.test(line) == false) && (recordLabelRegexp.test(line) == false)) {
153 fieldValue += '\n';
154 }
155
156 while ((line != "") && (fieldLabelRegexp.test(line) == false) && (recordLabelRegexp.test(line) == false)) {
157 var newLineValue;
158
159 newLineValue = line.match(fullLineRegexp)[1];
160 if (newLineValue != "\n") {
161 isMultiline = true;
162 }
163 fieldValue += newLineValue;
164 line = RegExp.rightContext;
165 }
166
167 if (isMultiline) {
168 fieldValue = fieldValue.replace(/\n$/g, "");
169 } else {
170 fieldValue = fieldValue.replace(/\n\n$/g, "");
171 }
172
173 line = line.replace(/^\n/, '');
174 }
175
176 result[fieldName] = fieldValue;
177 }
178 } else {
179 result = null;
180 }
181
182 aContext.line = line;
183
184 return result;
185 },
186
187 //-------------------------------------------------------------------------
188 __syntaxFix__: "syntax fix"
189});
190
191