1 36 package MessageDecomposer; 37 38 import java.io.File ; 39 import java.io.FileOutputStream ; 40 import java.io.IOException ; 41 import java.io.InputStream ; 42 import java.nio.charset.Charset ; 43 import java.text.DateFormat ; 44 import java.util.Iterator ; 45 import java.util.List ; 46 47 import org.columba.ristretto.coder.Base64DecoderInputStream; 48 import org.columba.ristretto.coder.CharsetDecoderInputStream; 49 import org.columba.ristretto.coder.QuotedPrintableDecoderInputStream; 50 import org.columba.ristretto.io.FileSource; 51 import org.columba.ristretto.io.StreamUtils; 52 import org.columba.ristretto.message.BasicHeader; 53 import org.columba.ristretto.message.Header; 54 import org.columba.ristretto.message.LocalMimePart; 55 import org.columba.ristretto.message.Message; 56 import org.columba.ristretto.message.MimeHeader; 57 import org.columba.ristretto.message.MimeTree; 58 import org.columba.ristretto.message.MimeType; 59 import org.columba.ristretto.parser.MessageParser; 60 import org.columba.ristretto.parser.ParserException; 61 62 public class MessageDecomposer { 63 64 private static final String helpMessage = 65 "Usage : MessageDecomposer mail-file <outdir> (optional)\n\n" 66 + "Example: POPStats mail.txt result_dir\n\n"; 67 68 71 public static void main(String [] args) { 72 if( args.length < 1) { 73 System.out.println(helpMessage); 74 return; 75 } 76 77 File messageFile = new File (args[0]); 78 File outDir; 79 80 if( args.length > 1) { 81 outDir = new File (args[1]); 82 } else { 83 outDir = new File ("."); 84 } 85 86 87 if( !messageFile.exists() ) { 88 System.out.println("File not found " + messageFile.toString()); 89 return; 90 } 91 92 if( !outDir.exists() || !outDir.isDirectory()) { 93 if( !outDir.mkdir() ) { 94 System.out.println("Could not create directory" + outDir.toString()); 95 return; 96 } 97 } 98 99 try { 100 Message message = MessageParser.parse(new FileSource(messageFile)); 101 102 writeStream(message.getHeader().getInputStream(), new File (outDir, "header.txt")); 103 printHeader(message.getHeader()); 104 105 MimeTree mimeTree = message.getMimePartTree(); 106 List mimeParts = mimeTree.getAllLeafs(); 107 108 Iterator it = mimeParts.iterator(); 109 110 int counter = 1; 111 while(it.hasNext()) { 112 LocalMimePart part = (LocalMimePart)it.next(); 113 MimeHeader h = part.getHeader(); 114 String filename = h.getFileName(); 115 if( filename == null) { 116 MimeType type = h.getMimeType(); 117 if( type.getSubtype().equals("html")) { 118 filename = "part"+ counter + ".html"; 119 } else if (type.getSubtype().equals("plain")) { 120 filename = "part"+ counter + ".txt"; 121 } else { 122 filename = "part"+counter; 123 } 124 } 125 126 InputStream stream = part.getInputStream(); 127 if( h.getContentTransferEncoding() == MimeHeader.BASE64) { 128 stream = new Base64DecoderInputStream(stream); 129 } else if( h.getContentTransferEncoding() == MimeHeader.QUOTED_PRINTABLE) { 130 stream = new QuotedPrintableDecoderInputStream(stream); 131 } 132 133 if( h.getContentParameter("charset") != null) { 134 stream = new CharsetDecoderInputStream(stream, Charset.forName(h.getContentParameter("charset"))); 135 } 136 137 writeStream(stream, new File (outDir, filename)); 138 139 counter++; 140 } 141 142 } catch (ParserException e) { 143 System.out.println("Parser Exception: " + e.getLocalizedMessage()); 144 } catch (IOException e) { 145 System.out.println("IO Exception: " + e.getLocalizedMessage()); 146 } 147 148 System.exit(0); 149 } 150 151 private static void writeStream(InputStream in, File file) throws IOException { 152 FileOutputStream out = new FileOutputStream (file); 153 StreamUtils.streamCopy(in, out); 154 in.close(); 155 out.close(); 156 } 157 158 private static void printHeader(Header rawHeader) { 159 BasicHeader header = new BasicHeader(rawHeader); 160 161 System.out.println(header.getFrom().toString()); 162 System.out.println(DateFormat.getInstance().format(header.getDate())); 163 System.out.println(header.getSubject()); 164 } 165 166 167 } 168 | Popular Tags |