From: gruban@oocities.com
To: tetractys@oocities.com
Subject: Mail Parser for UNIX
Thanks for your Java Post class. I am using it on my own pages and have written a Java class of my own to filter out the form-generated mail from my UNIX news feed.
Basically I put the line
Yes, it likely won't work on non-UNIX systems, but I thought you might be interested.
George Ruban, gruban@oocities.com
import java.io.*;
import java.util.StringTokenizer;
/** Class to pull selected mail messages out of a mail file
* to a separate file.
* It seems that the /var/mail/user-name file is separated by
* From a@b Date lines.
* The post mail example is at the end.
* A minimal valid mail is the next 2 lines.
* <PRE>
* From MAILER-DAEMON@cs.bu.edu Wed Aug 6 10:00:30 1997
*
* </PRE>
* The following file is treated as 2 valid mails.
* <PRE>
* From MAILER-DAEMON@cs.bu.edu Wed Aug 6 10:00:30 1997
*
* From MAILER-DAEMON@cs.bu.edu Wed Aug 6 10:00:30 1997
* </PRE>
* @author George Ruban
* @version 1.0 8/6/97
*/
public class ParseMail
{
/** Usage:
* java ParseMail /path/to/mail/file /path/to/new/file "selection string"
*/
public static void main(String args[]) throws IOException
{
if(args.length!= 3)
{
System.err.println("Usage: java ParseMail /path/to/mail/file "
+ "/path/to/new/file \"selection string\"");
System.exit(1);
}
File mailFile = new File(args[0]);
RandomAccessFile outFile = new RandomAccessFile(args[1], "rw");
String searchString = args[2];
DataInputStream in = new DataInputStream(new FileInputStream(mailFile));
// holds mails to be left in mail file
StringBuffer outBuf = new StringBuffer((int)mailFile.length());
// read mail, one line at a time.
String line = "";
while(line != null)
{
// build up a complete mail, one line at a time
StringBuffer mailBuf = new StringBuffer(1000);
mailBuf.append(line);
while(true)
{
line = in.readLine();
if(line == null) break;
if(line.startsWith("From "))
{
StringTokenizer tok = new StringTokenizer(line);
if(tok.countTokens() == 7)
{
try
{
tok.nextToken(); // From
if(tok.nextToken().indexOf('@') < 0) // MAILER-DAEMON@cs.bu.edu
throw new RuntimeException();
tok.nextToken(); // Wed
tok.nextToken(); // Aug
Integer.parseInt(tok.nextToken()); // 6
if(tok.nextToken().indexOf(':') != 2) // 10:00:30
throw new RuntimeException();
Integer.parseInt(tok.nextToken()); // 1997
break; // yup, it's a From - line
}
catch(RuntimeException notAFromLine) {}
}
}
mailBuf.append(line + '\n');
}
// complete mail built up in mailBuf
// decide whether to parse it out or not
if(mailBuf.toString().indexOf(searchString) >= 0)
{
outFile.seek(outFile.length());
outFile.writeBytes(mailBuf.toString());
}
else
{
outBuf.append(mailBuf);
}
} // end while(line != null)
// complete mail read, write back the unparsed parts
in.close();
in = null;
outFile.close();
FileOutputStream out = new FileOutputStream(mailFile);
byte [] buf = new byte[outBuf.length()];
outBuf.toString().getBytes(0, buf.length, buf, 0);
out.write(buf);
}
}
![]() |
In the next Issue Java GeoUtilities will be |
|---|