Re: Downloading messages
- Posted by Derek Parnell <ddparnell at bigpond.com> May 20, 2005
- 424 views
cklester wrote: > > Derek Parnell wrote: > > > > I wrote a tool to convert EUForum archive files into the .MBX format. > > Many news readers and email clients can import messages stored > > in that format. > > Derek, what does that same program look like in D? (just curious; send via > email if you want, but i think comparisons are always on topic...). :) Note: D's arrays start at zero and not 1. // --------CODE STARTS ------------- import std.stdio; import std.stream; import std.string; alias char[] string; void Usage(string pName) { writefln("(c) 2005 Derek Parnell. " "Convert a EUforum archive to MBX format"); writefln("Usage:- %s filename ...", pName); writefln(" For each input file, " "a new file of type '.mbx' is created."); } int begins(string pString, string pSubStr) { if (pString.length < pSubStr.length) return -1; if (pSubStr.length == 0) return -1; if (pString[0..pSubStr.length] == pSubStr) return pSubStr.length; else return -1; } string[] format_date(string pText) { // Returns an array of strings of YYYY,MMM,dd,hh,mm,ss // Input is in the form YYYY MMM dd hh:mm string[] lRetValue; int lPos; bool lWS; lRetValue.length = 6; lPos = 0; foreach(char c; pText) { if (c != ' ' && c != ':') { lRetValue[lPos] ~= c; lWS = true; } else { if (lWS) { lPos += 1; lWS = false; } } } for (int i = 3; i < 6; i++) { if (lRetValue[i].length == 1) lRetValue[i] = "0" ~ lRetValue[i]; } return lRetValue; } const string[] kSpecHdrs = ["X-EUFORUM", "Date", "From"]; const int kMessageId = 0; const int kDate = 1; const int kFrom = 2; void process_file(string pFileName) { BufferedFile lInputFH = new BufferedFile; BufferedFile lOutputFH = new BufferedFile; string lLine; int lLineCnt; string lOutName; int lPos; string[] lHeaders; bool lDoingHdrs; string lTemp; string[] lDate; writef("Processing '%s' ... ", pFileName); lInputFH.open(pFileName, FileMode.In); lPos = pFileName.length-1; // Copy the input file name rather than just reference it. lOutName = pFileName.dup; while (lPos >= 0) { if (lOutName[lPos] == '.') { // Trim off everything from the last dot. lOutName.length = lPos-1; break; } lPos--; } lOutName ~= ".mbx"; lOutputFH.create(lOutName); lLineCnt = 0; lDoingHdrs = true; while (lInputFH.eof() == false) { lLine = lInputFH.readLine(); toASCII(lLine); // Remove any non-ASCII chars (bad UTF-8) lLineCnt++; if (lLine == "-= B E G I N =-") { lHeaders.length = kSpecHdrs.length; lDoingHdrs = true; } else if (lLine.length == 0) { if (lDoingHdrs) { lDoingHdrs = false; // Process the headers. -- lDate = format_date(lHeaders[kDate]); // The 'MBX message start line' lPos = find(lHeaders[kFrom],"<"); lTemp = lHeaders[kFrom][lPos+1 .. $-1]; foreach (inout char c; lTemp) { if (c == ' ') c = '.'; } lOutputFH.writefln("From %s %s %s %s:%s:00 %s", lTemp, lDate[1], lDate[2], lDate[3], lDate[4], lDate[0] ); // The message id lOutputFH.writefln("Message-ID: <%s>", lHeaders[kMessageId]); lOutputFH.writefln("From: %s", lHeaders[kFrom]); lOutputFH.writefln("Date: Mon, %s %s %s %s:%s:00 +0000", lDate[2], lDate[1], lDate[0], lDate[3], lDate[4] ); for (int j = kSpecHdrs.length; j < lHeaders.length; j++) { lOutputFH.writeLine(lHeaders[j]); } lOutputFH.writeLine(""); lHeaders.length = kSpecHdrs.length; } else { lOutputFH.writeLine(lLine); } } else { if (lDoingHdrs) { foreach( int idx, string lHdr; kSpecHdrs) { lPos = begins(lLine, lHdr ~ ": "); if (lPos >= 0) { lHeaders[idx] = lLine[lPos .. $]; break; } if (idx == kSpecHdrs.length-1) { // Not a special header. lHeaders ~= lLine; } } } else { lOutputFH.writeLine(lLine); } } } writefln("( %d lines)", lLineCnt); lInputFH.close(); lOutputFH.close(); } int main(string[] pArgs) { if (pArgs.length < 2) { Usage(pArgs[0]); return 0; } foreach (int idx, string lArg; pArgs) { if (idx > 0) process_file(pArgs[idx]); } return 0; } void toASCII(string pText) { foreach(inout char c; pText) { if (c > 127) c = '?'; } } // --------CODE ENDS ------------- -- Derek Parnell Melbourne, Australia irc://irc.sorcery.net:9000/euphoria