/** * mime tester * gdc -g -o/tmp/mime_test -fversion=dinc_mime_test *.d -I../../ -lgnutls -lgcrypt -lc -lpthread */ module dinc.mime.test; version (dinc_mime_test) { private import std.stdio; private import std.base64; private import std.c.stdlib; private import std.stream; private import std.cstream; private import dinc.mime.Part; private import dinc.mime.Source; private import dinc.mime.Header; private import dinc.mime.Document; private import dinc.mime.iconv; void main(char[][] args) { if (args.length < 2) { writefln("usage: Mime mimefile.txt"); exit(1); } for(int i =1; i < args.length;i++) { writefln("open %s", args[i]); //sourcetest(args[1]); //headertest(args[1]); /* lets test Source */ auto f = new File(args[i]); auto s = new dinc.mime.Source.Source(f); auto d = new dinc.mime.Document.Document(s); d.parseFull(); //auto dtest = new Dtest; d.dumpOut(); //d.dumpFunc(&(dtest.dumper)); f.close(); } } class Dtest{ void dumper(Part p) { //p.dump(); writefln("%s/%s (charset=%s) (name=%s) (len=%s)", p.maintype, p.subtype, p.charset, p.name, p.bodylength); auto ct = p.h.getFirstHeader("content-type"); if (p.maintype == "multipart") { return; } if (p.maintype == "message") { return; } if (p.maintype == "image") { // dont car.e return; } if (p.maintype == "text") { return; auto m = new MemoryStream(); p.printBody(m, 0); auto v = new Iconv("UTF-8//IGNORE", p.charset.length? p.charset : "ISO-8859-1"); writefln("%s", v.conv(m.toString())); } if (p.name.length) { ct = p.h.getFirstHeader("content-transfer-encoding"); auto m = new MemoryStream(); p.printBody(m, 0); if (!ct) { writefln("exracted file %s length(%d)", p.name, m.toString().length); return; } if (ct) { switch(std.string.tolower(std.string.strip(ct.value))) { case "base64": //writefln("%s", m.toString()); //throw new Exception("try decoding"); char[] bodystr = std.base64.decode( std.string.replace( std.string.replace( m.toString(), "\n", ""), "\r", "") ); writefln("exracted file %s length(%d)", p.name, bodystr.length); return; default: throw new Exception("unknown encoding"); } } } writefln("********* UNHANLED TYPE ********"); //writefln("%s", ct ? ct.toString() : "-- no type --"); } } void sourcetest(char[] fn) { auto f = new File(fn); auto s = new dinc.mime.Source.Source(f); char c; while(s.getChar(c)) { // dout.write(c); } writefln("now @%d",s.getOffset()); // reset and do it again? s.reset(); writefln("now @%d",s.getOffset()); while(s.getChar(c)) { //dout.write(c); } writefln("now @%d",s.getOffset()); /* OK... Source appears to work!!! */ // now let's check headers.. s.reset(); f.close(); } void headerTest(char[] fn) { auto f = new File(fn); auto s = new dinc.mime.Source.Source(f); auto h = new dinc.mime.Header.Header; uint nlines=0; h.parseHeader(s,nlines); h.dumpOut(); auto hd = h.analyzeHeader(); if (hd is null) { writefln("Nothing returned from anaylyzeHeader"); } else { hd.dumpOut(); } f.close(); } } // end version test..