mime.h (4881B)
1 /* -*- Mode: c++; -*- */ 2 /* -------------------------------------------------------------------- 3 * Filename: 4 * src/parsers/mime/mime.h 5 * 6 * Description: 7 * Declaration of main mime parser components 8 * 9 * Authors: 10 * Andreas Aardal Hanssen <andreas-binc curly bincimap spot org> 11 * 12 * Bugs: 13 * 14 * ChangeLog: 15 * 16 * -------------------------------------------------------------------- 17 * Copyright 2002-2005 Andreas Aardal Hanssen 18 * 19 * This program is free software; you can redistribute it and/or modify 20 * it under the terms of the GNU General Public License as published by 21 * the Free Software Foundation; either version 2 of the License, or 22 * (at your option) any later version. 23 * 24 * This program is distributed in the hope that it will be useful, 25 * but WITHOUT ANY WARRANTY; without even the implied warranty of 26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 27 * GNU General Public License for more details. 28 * 29 * You should have received a copy of the GNU General Public License 30 * along with this program; if not, write to the Free Software 31 * Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA. 32 * -------------------------------------------------------------------- 33 */ 34 #ifdef HAVE_CONFIG_H 35 #include <config.h> 36 #endif 37 38 #ifndef mime_h_included 39 #define mime_h_included 40 #include <string> 41 #include <vector> 42 #include <map> 43 #include <stdio.h> 44 #include "io.h" 45 46 namespace Binc { 47 48 //---------------------------------------------------------------------- 49 class HeaderItem { 50 private: 51 mutable std::string key; 52 mutable std::string value; 53 54 public: 55 inline const std::string &getKey(void) const { return key; } 56 inline const std::string &getValue(void) const { return value; } 57 58 //-- 59 HeaderItem(void); 60 HeaderItem(const std::string &key, const std::string &value); 61 }; 62 63 //---------------------------------------------------------------------- 64 class Header { 65 private: 66 mutable std::vector<HeaderItem> content; 67 68 public: 69 bool getFirstHeader(const std::string &key, HeaderItem &dest) const; 70 bool getAllHeaders(const std::string &key, std::vector<HeaderItem> &dest) const; 71 void add(const std::string &name, const std::string &content); 72 void print(void) const; 73 void clear(void) const; 74 75 //-- 76 Header(void); 77 ~Header(void); 78 }; 79 80 //---------------------------------------------------------------------- 81 class MimeDocument; 82 class MimePart { 83 protected: 84 public: 85 mutable bool multipart; 86 mutable bool messagerfc822; 87 mutable std::string subtype; 88 mutable std::string boundary; 89 90 mutable unsigned int headerstartoffsetcrlf; 91 mutable unsigned int headerlength; 92 93 mutable unsigned int bodystartoffsetcrlf; 94 mutable unsigned int bodylength; 95 mutable unsigned int nlines; 96 mutable unsigned int nbodylines; 97 mutable unsigned int size; 98 99 public: 100 enum FetchType { 101 FetchBody, 102 FetchHeader, 103 FetchMime 104 }; 105 106 mutable Header h; 107 108 mutable std::vector<MimePart> members; 109 110 inline const std::string &getSubType(void) const { return subtype; } 111 inline bool isMultipart(void) const { return multipart; } 112 inline bool isMessageRFC822(void) const { return messagerfc822; } 113 inline unsigned int getSize(void) const { return bodylength; } 114 inline unsigned int getNofLines(void) const { return nlines; } 115 inline unsigned int getNofBodyLines(void) const { return nbodylines; } 116 inline unsigned int getBodyLength(void) const { return bodylength; } 117 inline unsigned int getBodyStartOffset(void) const { return bodystartoffsetcrlf; } 118 119 void printBody(int fd, Binc::IO &output, unsigned int startoffset, unsigned int length) const; 120 void printHeader(int fd, Binc::IO &output, std::vector<std::string> headers, bool includeheaders, unsigned int startoffset, unsigned int length, std::string &storage) const; 121 void printDoc(int fd, Binc::IO &output, unsigned int startoffset, unsigned int length) const; 122 virtual void clear(void) const; 123 124 const MimePart *getPart(const std::string &findpart, std::string genpart, FetchType fetchType = FetchBody) const; 125 virtual int parseOnlyHeader(const std::string &toboundary) const; 126 virtual int parseFull(const std::string &toboundary, int &boundarysize) const; 127 128 MimePart(void); 129 virtual ~MimePart(void); 130 }; 131 132 //---------------------------------------------------------------------- 133 class MimeDocument : public MimePart { 134 private: 135 mutable bool headerIsParsed; 136 mutable bool allIsParsed; 137 138 public: 139 void parseOnlyHeader(int fd) const; 140 void parseFull(int fd) const; 141 void clear(void) const; 142 143 inline bool isHeaderParsed(void) { return headerIsParsed; } 144 inline bool isAllParsed(void) { return allIsParsed; } 145 146 //-- 147 MimeDocument(void); 148 ~MimeDocument(void); 149 }; 150 151 }; 152 153 #endif