001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo
013: // Stich.
014: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
015: //
016: //All Rights Reserved.
017: package org.columba.mail.spam.command;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.InputStream;
021: import java.io.SequenceInputStream;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Vector;
025:
026: import org.columba.mail.config.AccountItem;
027: import org.columba.mail.config.MailConfig;
028: import org.columba.mail.folder.IMailbox;
029: import org.columba.ristretto.coder.Base64DecoderInputStream;
030: import org.columba.ristretto.coder.QuotedPrintableDecoderInputStream;
031: import org.columba.ristretto.message.Header;
032: import org.columba.ristretto.message.MimeHeader;
033: import org.columba.ristretto.message.MimePart;
034: import org.columba.ristretto.message.MimeTree;
035:
036: /**
037: * Helper class provides methods for preparing email messages before getting
038: * passed along to the spam filter.
039: *
040: * @author fdietz
041: */
042: public final class CommandHelper {
043:
044: /**
045: * Return bodypart of message as inputstream.
046: * <p>
047: * Note, that this depends on wether the user prefers HTML or text messages.
048: * <p>
049: * Bodypart is decoded if necessary.
050: *
051: * @param folder
052: * selected folder containing the message
053: * @param uid
054: * ID of message
055: * @return inputstream of message bodypart
056: * @throws Exception
057: */
058: public static InputStream getBodyPart(IMailbox folder, Object uid)
059: throws Exception {
060: MimeTree mimePartTree = folder.getMimePartTree(uid);
061:
062: List l = mimePartTree.getLeafsWithContentType(mimePartTree
063: .getRootMimeNode(), "text");
064: if (l.size() > 1) {
065: Vector streamList = new Vector();
066: Iterator it = l.iterator();
067: while (it.hasNext()) {
068: MimePart mp = (MimePart) it.next();
069:
070: InputStream s = getBodyPartStream(folder, uid, mp);
071: streamList.add(s);
072: }
073:
074: SequenceInputStream stream = new SequenceInputStream(
075: streamList.elements());
076: return stream;
077: } else if (l.size() == 1) {
078: MimePart mp = (MimePart) l.get(0);
079: InputStream s = getBodyPartStream(folder, uid, mp);
080: return s;
081: } else {
082: return new ByteArrayInputStream(new byte[0]);
083:
084: }
085:
086: }
087:
088: /**
089: * Get inputstream of bodypart.
090: * <p>
091: * Additionally decode inputstream.
092: *
093: * @param folder
094: * selected folder
095: * @param uid
096: * selected message UID
097: * @param mp
098: * selected Mimepart
099: * @return decoded inputstream
100: * @throws Exception
101: */
102: private static InputStream getBodyPartStream(IMailbox folder,
103: Object uid, MimePart mp) throws Exception {
104: InputStream bodyStream = folder.getMimePartBodyStream(uid, mp
105: .getAddress());
106:
107: int encoding = mp.getHeader().getContentTransferEncoding();
108:
109: switch (encoding) {
110: case MimeHeader.QUOTED_PRINTABLE: {
111: bodyStream = new QuotedPrintableDecoderInputStream(
112: bodyStream);
113:
114: break;
115: }
116:
117: case MimeHeader.BASE64: {
118: bodyStream = new Base64DecoderInputStream(bodyStream);
119:
120: break;
121: }
122: }
123:
124: return bodyStream;
125: }
126:
127: /**
128: * Retrieve account this message is associated to.
129: *
130: * @param folder
131: * selected folder
132: * @param uid
133: * selected message
134: * @return account item
135: * @throws Exception
136: */
137: public static AccountItem retrieveAccountItem(IMailbox folder,
138: Object uid) throws Exception {
139: AccountItem item = null;
140:
141: Object accountUid = folder.getAttribute(uid,
142: "columba.accountuid");
143: if (accountUid != null) {
144: // try to get account using the account ID
145: item = MailConfig.getInstance().getAccountList().uidGet(
146: ((Integer) accountUid).intValue());
147:
148: }
149:
150: if (item == null) {
151: // try to get the account using the email address
152: Header header = folder.getHeaderFields(uid,
153: new String[] { "To" });
154:
155: item = MailConfig.getInstance().getAccountList()
156: .getAccount(header.get("To"));
157:
158: }
159:
160: if (item == null) {
161: // use default account as fallback
162:
163: item = MailConfig.getInstance().getAccountList()
164: .getDefaultAccount();
165: }
166:
167: return item;
168: }
169: }
|