1 // this converts old chiroptera mailbox CHMT database to new SQLite database
2 // this only puts message data into "messages" table; you need to rebuild
3 // other tables with the separate utility
4 module zsq_convert
is aliced
;
17 ////////////////////////////////////////////////////////////////////////////////
18 void main (string
[] args
) {
21 if (args
.length
> 1) {
22 foreach (string s
; args
[1..$]) {
23 if (s
.strEquCI("--nocompress")) docompress
= 0;
27 writeln("reading text database...");
30 auto fl
= VFile("index.chmt");
32 if (sz
> 0x3fffffffU
) throw new Exception("text database too big");
34 alltext
= new char[cast(uint)sz
];
35 fl
.rawReadExact(alltext
[]);
39 writeln("creating db...");
40 auto db = chiroRecreateDB();
42 auto stInsMsg
= db.statement("INSERT INTO messages (uid,headers,body) VALUES(NULL,ChiroPack(ChiroNormHeaders(:hdrs),:dopack),ChiroPack(:body,:dopack));");
43 //db.execute("begin transaction;");
45 usize allsize
= alltext
.length
;
48 while (alltext
.length
) {
49 usize end
= findMessageEnd(alltext
);
50 const(char)[] msg
= alltext
[0..end
];
51 usize hdrend
= findHeadersEnd(msg
);
52 const(char)[] hdrs
= msg
[0..hdrend
];
53 hdrend
= skipOneLine(msg
, hdrend
);
54 const(char)[] body = msg
[hdrend
..$];
56 // nobody needs final blanks; no, really
57 // it is forbidden to use binary encoding in emails anyway, so don't bother keeping them intact
58 hdrs
= hdrs
.xstripright
;
59 body = body.xstripright
;
62 .bindBlob(":hdrs", hdrs
)
63 .bindBlob(":body", body)
64 .bind(":dopack", docompress
)
68 auto msgid
= findHeaderField(hdrs
, "Message-ID");
69 if (msgid
.length
) writeln("MSGID: <", msgid
, ">");
74 auto subj
= findHeaderField(hdrs
, "Subject").decodeSubj
.subjRemoveRe
;
75 auto from
= findHeaderField(hdrs
, "From").decodeSubj
;
76 writeln(" FROM: {", from
.recodeToKOI8
, "}");
77 writeln(" SUBJ: {", subj
.recodeToKOI8
, "}");
80 alltext
= cutTopMessage(alltext
);
84 usize cpos
= allsize
-alltext
.length
;
85 write(count
, " ", cast(ulong)cpos
*100u/cast(ulong)allsize
, "%\r");
86 //db.execute("commit transaction;");
87 //db.execute("begin transaction;");
91 writeln(count
, " messages found.");
92 //writeln("commiting final transaction...");
93 //db.execute("commit transaction;");
95 writeln("closing the db");