1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.email;
18
19import android.content.ContentUris;
20import android.content.ContentValues;
21import android.content.Context;
22import android.database.Cursor;
23import android.net.Uri;
24
25import com.android.emailcommon.Logging;
26import com.android.emailcommon.internet.MimeBodyPart;
27import com.android.emailcommon.internet.MimeHeader;
28import com.android.emailcommon.internet.MimeMessage;
29import com.android.emailcommon.internet.MimeMultipart;
30import com.android.emailcommon.internet.MimeUtility;
31import com.android.emailcommon.internet.TextBody;
32import com.android.emailcommon.mail.Address;
33import com.android.emailcommon.mail.Flag;
34import com.android.emailcommon.mail.Message;
35import com.android.emailcommon.mail.Message.RecipientType;
36import com.android.emailcommon.mail.MessagingException;
37import com.android.emailcommon.mail.Part;
38import com.android.emailcommon.provider.EmailContent;
39import com.android.emailcommon.provider.EmailContent.Attachment;
40import com.android.emailcommon.provider.EmailContent.AttachmentColumns;
41import com.android.emailcommon.provider.Mailbox;
42import com.android.emailcommon.utility.AttachmentUtilities;
43import com.android.mail.providers.UIProvider;
44import com.android.mail.utils.LogUtils;
45
46import org.apache.commons.io.IOUtils;
47
48import java.io.File;
49import java.io.FileOutputStream;
50import java.io.IOException;
51import java.io.InputStream;
52import java.util.ArrayList;
53import java.util.Date;
54import java.util.HashMap;
55
56public class LegacyConversions {
57
58    /** DO NOT CHECK IN "TRUE" */
59    private static final boolean DEBUG_ATTACHMENTS = false;
60
61    /** Used for mapping folder names to type codes (e.g. inbox, drafts, trash) */
62    private static final HashMap<String, Integer>
63            sServerMailboxNames = new HashMap<String, Integer>();
64
65    /**
66     * Values for HEADER_ANDROID_BODY_QUOTED_PART to tag body parts
67     */
68    /* package */ static final String BODY_QUOTED_PART_REPLY = "quoted-reply";
69    /* package */ static final String BODY_QUOTED_PART_FORWARD = "quoted-forward";
70    /* package */ static final String BODY_QUOTED_PART_INTRO = "quoted-intro";
71
72    /**
73     * Copy field-by-field from a "store" message to a "provider" message
74     * @param message The message we've just downloaded (must be a MimeMessage)
75     * @param localMessage The message we'd like to write into the DB
76     * @result true if dirty (changes were made)
77     */
78    public static boolean updateMessageFields(EmailContent.Message localMessage, Message message,
79                long accountId, long mailboxId) throws MessagingException {
80
81        Address[] from = message.getFrom();
82        Address[] to = message.getRecipients(Message.RecipientType.TO);
83        Address[] cc = message.getRecipients(Message.RecipientType.CC);
84        Address[] bcc = message.getRecipients(Message.RecipientType.BCC);
85        Address[] replyTo = message.getReplyTo();
86        String subject = message.getSubject();
87        Date sentDate = message.getSentDate();
88        Date internalDate = message.getInternalDate();
89
90        if (from != null && from.length > 0) {
91            localMessage.mDisplayName = from[0].toFriendly();
92        }
93        if (sentDate != null) {
94            localMessage.mTimeStamp = sentDate.getTime();
95        } else if (internalDate != null) {
96            LogUtils.w(Logging.LOG_TAG, "No sentDate, falling back to internalDate");
97            localMessage.mTimeStamp = internalDate.getTime();
98        }
99        if (subject != null) {
100            localMessage.mSubject = subject;
101        }
102        localMessage.mFlagRead = message.isSet(Flag.SEEN);
103        if (message.isSet(Flag.ANSWERED)) {
104            localMessage.mFlags |= EmailContent.Message.FLAG_REPLIED_TO;
105        }
106
107        // Keep the message in the "unloaded" state until it has (at least) a display name.
108        // This prevents early flickering of empty messages in POP download.
109        if (localMessage.mFlagLoaded != EmailContent.Message.FLAG_LOADED_COMPLETE) {
110            if (localMessage.mDisplayName == null || "".equals(localMessage.mDisplayName)) {
111                localMessage.mFlagLoaded = EmailContent.Message.FLAG_LOADED_UNLOADED;
112            } else {
113                localMessage.mFlagLoaded = EmailContent.Message.FLAG_LOADED_PARTIAL;
114            }
115        }
116        localMessage.mFlagFavorite = message.isSet(Flag.FLAGGED);
117//        public boolean mFlagAttachment = false;
118//        public int mFlags = 0;
119
120        localMessage.mServerId = message.getUid();
121        if (internalDate != null) {
122            localMessage.mServerTimeStamp = internalDate.getTime();
123        }
124//        public String mClientId;
125
126        // Only replace the local message-id if a new one was found.  This is seen in some ISP's
127        // which may deliver messages w/o a message-id header.
128        String messageId = ((MimeMessage)message).getMessageId();
129        if (messageId != null) {
130            localMessage.mMessageId = messageId;
131        }
132
133//        public long mBodyKey;
134        localMessage.mMailboxKey = mailboxId;
135        localMessage.mAccountKey = accountId;
136
137        if (from != null && from.length > 0) {
138            localMessage.mFrom = Address.pack(from);
139        }
140
141        localMessage.mTo = Address.pack(to);
142        localMessage.mCc = Address.pack(cc);
143        localMessage.mBcc = Address.pack(bcc);
144        localMessage.mReplyTo = Address.pack(replyTo);
145
146//        public String mText;
147//        public String mHtml;
148//        public String mTextReply;
149//        public String mHtmlReply;
150
151//        // Can be used while building messages, but is NOT saved by the Provider
152//        transient public ArrayList<Attachment> mAttachments = null;
153
154        return true;
155    }
156
157    /**
158     * Copy attachments from MimeMessage to provider Message.
159     *
160     * @param context a context for file operations
161     * @param localMessage the attachments will be built against this message
162     * @param attachments the attachments to add
163     * @throws IOException
164     */
165    public static void updateAttachments(Context context, EmailContent.Message localMessage,
166            ArrayList<Part> attachments) throws MessagingException, IOException {
167        localMessage.mAttachments = null;
168        for (Part attachmentPart : attachments) {
169            addOneAttachment(context, localMessage, attachmentPart);
170        }
171    }
172
173    /**
174     * Add a single attachment part to the message
175     *
176     * This will skip adding attachments if they are already found in the attachments table.
177     * The heuristic for this will fail (false-positive) if two identical attachments are
178     * included in a single POP3 message.
179     * TODO: Fix that, by (elsewhere) simulating an mLocation value based on the attachments
180     * position within the list of multipart/mixed elements.  This would make every POP3 attachment
181     * unique, and might also simplify the code (since we could just look at the positions, and
182     * ignore the filename, etc.)
183     *
184     * TODO: Take a closer look at encoding and deal with it if necessary.
185     *
186     * @param context a context for file operations
187     * @param localMessage the attachments will be built against this message
188     * @param part a single attachment part from POP or IMAP
189     * @throws IOException
190     */
191    public static void addOneAttachment(Context context, EmailContent.Message localMessage,
192            Part part) throws MessagingException, IOException {
193
194        Attachment localAttachment = new Attachment();
195
196        // Transfer fields from mime format to provider format
197        String contentType = MimeUtility.unfoldAndDecode(part.getContentType());
198        String name = MimeUtility.getHeaderParameter(contentType, "name");
199        if (name == null) {
200            String contentDisposition = MimeUtility.unfoldAndDecode(part.getDisposition());
201            name = MimeUtility.getHeaderParameter(contentDisposition, "filename");
202        }
203
204        // Incoming attachment: Try to pull size from disposition (if not downloaded yet)
205        long size = 0;
206        String disposition = part.getDisposition();
207        if (disposition != null) {
208            String s = MimeUtility.getHeaderParameter(disposition, "size");
209            if (s != null) {
210                size = Long.parseLong(s);
211            }
212        }
213
214        // Get partId for unloaded IMAP attachments (if any)
215        // This is only provided (and used) when we have structure but not the actual attachment
216        String[] partIds = part.getHeader(MimeHeader.HEADER_ANDROID_ATTACHMENT_STORE_DATA);
217        String partId = partIds != null ? partIds[0] : null;
218
219        // Run the mime type through inferMimeType in case we have something generic and can do
220        // better using the filename extension
221        String mimeType = AttachmentUtilities.inferMimeType(name, part.getMimeType());
222        localAttachment.mMimeType = mimeType;
223        localAttachment.mFileName = name;
224        localAttachment.mSize = size;           // May be reset below if file handled
225        localAttachment.mContentId = part.getContentId();
226        localAttachment.setContentUri(null);     // Will be rewritten by saveAttachmentBody
227        localAttachment.mMessageKey = localMessage.mId;
228        localAttachment.mLocation = partId;
229        localAttachment.mEncoding = "B";        // TODO - convert other known encodings
230        localAttachment.mAccountKey = localMessage.mAccountKey;
231
232        if (DEBUG_ATTACHMENTS) {
233            LogUtils.d(Logging.LOG_TAG, "Add attachment " + localAttachment);
234        }
235
236        // To prevent duplication - do we already have a matching attachment?
237        // The fields we'll check for equality are:
238        //  mFileName, mMimeType, mContentId, mMessageKey, mLocation
239        // NOTE:  This will false-positive if you attach the exact same file, twice, to a POP3
240        // message.  We can live with that - you'll get one of the copies.
241        Uri uri = ContentUris.withAppendedId(Attachment.MESSAGE_ID_URI, localMessage.mId);
242        Cursor cursor = context.getContentResolver().query(uri, Attachment.CONTENT_PROJECTION,
243                null, null, null);
244        boolean attachmentFoundInDb = false;
245        try {
246            while (cursor.moveToNext()) {
247                Attachment dbAttachment = new Attachment();
248                dbAttachment.restore(cursor);
249                // We test each of the fields here (instead of in SQL) because they may be
250                // null, or may be strings.
251                if (stringNotEqual(dbAttachment.mFileName, localAttachment.mFileName)) continue;
252                if (stringNotEqual(dbAttachment.mMimeType, localAttachment.mMimeType)) continue;
253                if (stringNotEqual(dbAttachment.mContentId, localAttachment.mContentId)) continue;
254                if (stringNotEqual(dbAttachment.mLocation, localAttachment.mLocation)) continue;
255                // We found a match, so use the existing attachment id, and stop looking/looping
256                attachmentFoundInDb = true;
257                localAttachment.mId = dbAttachment.mId;
258                if (DEBUG_ATTACHMENTS) {
259                    LogUtils.d(Logging.LOG_TAG, "Skipped, found db attachment " + dbAttachment);
260                }
261                break;
262            }
263        } finally {
264            cursor.close();
265        }
266
267        // Save the attachment (so far) in order to obtain an id
268        if (!attachmentFoundInDb) {
269            localAttachment.save(context);
270        }
271
272        // If an attachment body was actually provided, we need to write the file now
273        saveAttachmentBody(context, part, localAttachment, localMessage.mAccountKey);
274
275        if (localMessage.mAttachments == null) {
276            localMessage.mAttachments = new ArrayList<Attachment>();
277        }
278        localMessage.mAttachments.add(localAttachment);
279        localMessage.mFlagAttachment = true;
280    }
281
282    /**
283     * Helper for addOneAttachment that compares two strings, deals with nulls, and treats
284     * nulls and empty strings as equal.
285     */
286    /* package */ static boolean stringNotEqual(String a, String b) {
287        if (a == null && b == null) return false;       // fast exit for two null strings
288        if (a == null) a = "";
289        if (b == null) b = "";
290        return !a.equals(b);
291    }
292
293    /**
294     * Save the body part of a single attachment, to a file in the attachments directory.
295     */
296    public static void saveAttachmentBody(Context context, Part part, Attachment localAttachment,
297            long accountId) throws MessagingException, IOException {
298        if (part.getBody() != null) {
299            long attachmentId = localAttachment.mId;
300
301            InputStream in = part.getBody().getInputStream();
302
303            File saveIn = AttachmentUtilities.getAttachmentDirectory(context, accountId);
304            if (!saveIn.exists()) {
305                saveIn.mkdirs();
306            }
307            File saveAs = AttachmentUtilities.getAttachmentFilename(context, accountId,
308                    attachmentId);
309            saveAs.createNewFile();
310            FileOutputStream out = new FileOutputStream(saveAs);
311            long copySize = IOUtils.copy(in, out);
312            in.close();
313            out.close();
314
315            // update the attachment with the extra information we now know
316            String contentUriString = AttachmentUtilities.getAttachmentUri(
317                    accountId, attachmentId).toString();
318
319            localAttachment.mSize = copySize;
320            localAttachment.setContentUri(contentUriString);
321
322            // update the attachment in the database as well
323            ContentValues cv = new ContentValues();
324            cv.put(AttachmentColumns.SIZE, copySize);
325            cv.put(AttachmentColumns.CONTENT_URI, contentUriString);
326            cv.put(AttachmentColumns.UI_STATE, UIProvider.AttachmentState.SAVED);
327            Uri uri = ContentUris.withAppendedId(Attachment.CONTENT_URI, attachmentId);
328            context.getContentResolver().update(uri, cv, null, null);
329        }
330    }
331
332    /**
333     * Read a complete Provider message into a legacy message (for IMAP upload).  This
334     * is basically the equivalent of LocalFolder.getMessages() + LocalFolder.fetch().
335     */
336    public static Message makeMessage(Context context, EmailContent.Message localMessage)
337            throws MessagingException {
338        MimeMessage message = new MimeMessage();
339
340        // LocalFolder.getMessages() equivalent:  Copy message fields
341        message.setSubject(localMessage.mSubject == null ? "" : localMessage.mSubject);
342        Address[] from = Address.unpack(localMessage.mFrom);
343        if (from.length > 0) {
344            message.setFrom(from[0]);
345        }
346        message.setSentDate(new Date(localMessage.mTimeStamp));
347        message.setUid(localMessage.mServerId);
348        message.setFlag(Flag.DELETED,
349                localMessage.mFlagLoaded == EmailContent.Message.FLAG_LOADED_DELETED);
350        message.setFlag(Flag.SEEN, localMessage.mFlagRead);
351        message.setFlag(Flag.FLAGGED, localMessage.mFlagFavorite);
352//      message.setFlag(Flag.DRAFT, localMessage.mMailboxKey == draftMailboxKey);
353        message.setRecipients(RecipientType.TO, Address.unpack(localMessage.mTo));
354        message.setRecipients(RecipientType.CC, Address.unpack(localMessage.mCc));
355        message.setRecipients(RecipientType.BCC, Address.unpack(localMessage.mBcc));
356        message.setReplyTo(Address.unpack(localMessage.mReplyTo));
357        message.setInternalDate(new Date(localMessage.mServerTimeStamp));
358        message.setMessageId(localMessage.mMessageId);
359
360        // LocalFolder.fetch() equivalent: build body parts
361        message.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "multipart/mixed");
362        MimeMultipart mp = new MimeMultipart();
363        mp.setSubType("mixed");
364        message.setBody(mp);
365
366        try {
367            addTextBodyPart(mp, "text/html", null,
368                    EmailContent.Body.restoreBodyHtmlWithMessageId(context, localMessage.mId));
369        } catch (RuntimeException rte) {
370            LogUtils.d(Logging.LOG_TAG, "Exception while reading html body " + rte.toString());
371        }
372
373        try {
374            addTextBodyPart(mp, "text/plain", null,
375                    EmailContent.Body.restoreBodyTextWithMessageId(context, localMessage.mId));
376        } catch (RuntimeException rte) {
377            LogUtils.d(Logging.LOG_TAG, "Exception while reading text body " + rte.toString());
378        }
379
380        boolean isReply = (localMessage.mFlags & EmailContent.Message.FLAG_TYPE_REPLY) != 0;
381        boolean isForward = (localMessage.mFlags & EmailContent.Message.FLAG_TYPE_FORWARD) != 0;
382
383        // If there is a quoted part (forwarding or reply), add the intro first, and then the
384        // rest of it.  If it is opened in some other viewer, it will (hopefully) be displayed in
385        // the same order as we've just set up the blocks:  composed text, intro, replied text
386        if (isReply || isForward) {
387            try {
388                addTextBodyPart(mp, "text/plain", BODY_QUOTED_PART_INTRO,
389                        EmailContent.Body.restoreIntroTextWithMessageId(context, localMessage.mId));
390            } catch (RuntimeException rte) {
391                LogUtils.d(Logging.LOG_TAG, "Exception while reading text reply " + rte.toString());
392            }
393
394            String replyTag = isReply ? BODY_QUOTED_PART_REPLY : BODY_QUOTED_PART_FORWARD;
395            try {
396                addTextBodyPart(mp, "text/html", replyTag,
397                        EmailContent.Body.restoreReplyHtmlWithMessageId(context, localMessage.mId));
398            } catch (RuntimeException rte) {
399                LogUtils.d(Logging.LOG_TAG, "Exception while reading html reply " + rte.toString());
400            }
401
402            try {
403                addTextBodyPart(mp, "text/plain", replyTag,
404                        EmailContent.Body.restoreReplyTextWithMessageId(context, localMessage.mId));
405            } catch (RuntimeException rte) {
406                LogUtils.d(Logging.LOG_TAG, "Exception while reading text reply " + rte.toString());
407            }
408        }
409
410        // Attachments
411        // TODO: Make sure we deal with these as structures and don't accidentally upload files
412//        Uri uri = ContentUris.withAppendedId(Attachment.MESSAGE_ID_URI, localMessage.mId);
413//        Cursor attachments = context.getContentResolver().query(uri, Attachment.CONTENT_PROJECTION,
414//                null, null, null);
415//        try {
416//
417//        } finally {
418//            attachments.close();
419//        }
420
421        return message;
422    }
423
424    /**
425     * Helper method to add a body part for a given type of text, if found
426     *
427     * @param mp The text body part will be added to this multipart
428     * @param contentType The content-type of the text being added
429     * @param quotedPartTag If non-null, HEADER_ANDROID_BODY_QUOTED_PART will be set to this value
430     * @param partText The text to add.  If null, nothing happens
431     */
432    private static void addTextBodyPart(MimeMultipart mp, String contentType, String quotedPartTag,
433            String partText) throws MessagingException {
434        if (partText == null) {
435            return;
436        }
437        TextBody body = new TextBody(partText);
438        MimeBodyPart bp = new MimeBodyPart(body, contentType);
439        if (quotedPartTag != null) {
440            bp.addHeader(MimeHeader.HEADER_ANDROID_BODY_QUOTED_PART, quotedPartTag);
441        }
442        mp.addBodyPart(bp);
443    }
444
445
446    /**
447     * Infer mailbox type from mailbox name.  Used by MessagingController (for live folder sync).
448     */
449    public static synchronized int inferMailboxTypeFromName(Context context, String mailboxName) {
450        if (sServerMailboxNames.size() == 0) {
451            // preload the hashmap, one time only
452            sServerMailboxNames.put(
453                    context.getString(R.string.mailbox_name_server_inbox).toLowerCase(),
454                    Mailbox.TYPE_INBOX);
455            sServerMailboxNames.put(
456                    context.getString(R.string.mailbox_name_server_outbox).toLowerCase(),
457                    Mailbox.TYPE_OUTBOX);
458            sServerMailboxNames.put(
459                    context.getString(R.string.mailbox_name_server_drafts).toLowerCase(),
460                    Mailbox.TYPE_DRAFTS);
461            sServerMailboxNames.put(
462                    context.getString(R.string.mailbox_name_server_trash).toLowerCase(),
463                    Mailbox.TYPE_TRASH);
464            sServerMailboxNames.put(
465                    context.getString(R.string.mailbox_name_server_sent).toLowerCase(),
466                    Mailbox.TYPE_SENT);
467            sServerMailboxNames.put(
468                    context.getString(R.string.mailbox_name_server_junk).toLowerCase(),
469                    Mailbox.TYPE_JUNK);
470        }
471        if (mailboxName == null || mailboxName.length() == 0) {
472            return Mailbox.TYPE_MAIL;
473        }
474        String lowerCaseName = mailboxName.toLowerCase();
475        Integer type = sServerMailboxNames.get(lowerCaseName);
476        if (type != null) {
477            return type;
478        }
479        return Mailbox.TYPE_MAIL;
480    }
481}
482