1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #ifndef __nsMsgBodyHandler_h
6 #define __nsMsgBodyHandler_h
7 
8 #include "nsIMsgSearchScopeTerm.h"
9 #include "nsILineInputStream.h"
10 #include "nsIMsgDatabase.h"
11 
12 //---------------------------------------------------------------------------
13 // nsMsgBodyHandler: used to retrieve lines from POP and IMAP offline messages.
14 // This is a helper class used by nsMsgSearchTerm::MatchBody
15 //---------------------------------------------------------------------------
16 class nsMsgBodyHandler {
17  public:
18   nsMsgBodyHandler(nsIMsgSearchScopeTerm*, uint32_t length, nsIMsgDBHdr* msg,
19                    nsIMsgDatabase* db);
20 
21   // we can also create a body handler when doing arbitrary header
22   // filtering...we need the list of headers and the header size as well
23   // if we are doing filtering...if ForFilters is false, headers and
24   // headersSize is ignored!!!
25   nsMsgBodyHandler(nsIMsgSearchScopeTerm*, uint32_t length, nsIMsgDBHdr* msg,
26                    nsIMsgDatabase* db,
27                    const char* headers /* NULL terminated list of headers */,
28                    uint32_t headersSize, bool ForFilters);
29 
30   virtual ~nsMsgBodyHandler();
31 
32   // Returns next message line in buf and the applicable charset, if found.
33   // The return value is the length of 'buf' or -1 for EOF.
34   int32_t GetNextLine(nsCString& buf, nsCString& charset);
IsQP()35   bool IsQP() { return m_partIsQP; }
36 
37   // Transformations
SetStripHeaders(bool strip)38   void SetStripHeaders(bool strip) { m_stripHeaders = strip; }
39 
40  protected:
41   void Initialize();  // common initialization code
42 
43   // filter related methods. For filtering we always use the headers
44   // list instead of the database...
45   bool m_Filtering;
46   int32_t GetNextFilterLine(nsCString& buf);
47   // pointer into the headers list in the original message hdr db...
48   const char* m_headers;
49   uint32_t m_headersSize;
50   uint32_t m_headerBytesRead;
51 
52   // local / POP related methods
53   void OpenLocalFolder();
54 
55   // goes through the mail folder
56   int32_t GetNextLocalLine(nsCString& buf);
57 
58   nsIMsgSearchScopeTerm* m_scope;
59   nsCOMPtr<nsILineInputStream> m_fileLineStream;
60   nsCOMPtr<nsIFile> m_localFile;
61 
62   /**
63    * The number of lines in the message.  If |m_lineCountInBodyLines| then this
64    * is the number of body lines, otherwise this is the entire number of lines
65    * in the message.  This is important so we know when to stop reading the file
66    * without accidentally reading part of the next message.
67    */
68   uint32_t m_numLocalLines;
69   /**
70    * When true, |m_numLocalLines| is the number of body lines in the message,
71    * when false it is the entire number of lines in the message.
72    *
73    * When a message is an offline IMAP or news message, then the number of lines
74    * will be the entire number of lines, so this should be false.  When the
75    * message is a local message, the number of lines will be the number of body
76    * lines.
77    */
78   bool m_lineCountInBodyLines;
79 
80   // Offline IMAP related methods & state
81 
82   nsCOMPtr<nsIMsgDBHdr> m_msgHdr;
83   nsCOMPtr<nsIMsgDatabase> m_db;
84 
85   // Transformations
86   // With the exception of m_isMultipart, these all apply to the various parts
87   bool m_stripHeaders;     // true if we're supposed to strip of message headers
88   bool m_pastMsgHeaders;   // true if we've already skipped over the message
89                            // headers
90   bool m_pastPartHeaders;  // true if we've already skipped over the part
91                            // headers
92   bool m_partIsQP;     // true if the Content-Transfer-Encoding header claims
93                        // quoted-printable
94   bool m_partIsHtml;   // true if the Content-type header claims text/html
95   bool m_base64part;   // true if the current part is in base64
96   bool m_isMultipart;  // true if the message is a multipart/* message
97   bool m_partIsText;   // true if the current part is text/*
98   bool m_inMessageAttachment;  // true if current part is message/*
99 
100   nsTArray<nsCString> m_boundaries;  // The boundary strings to look for
101   nsCString m_partCharset;           // The charset found in the part
102 
103   // See implementation for comments
104   int32_t ApplyTransformations(const nsCString& line, int32_t length,
105                                bool& returnThisLine, nsCString& buf);
106   void SniffPossibleMIMEHeader(const nsCString& line);
107   static void StripHtml(nsCString& buf);
108   static void Base64Decode(nsCString& buf);
109 };
110 #endif
111