maildirmessage.h (8962B)
1 /* -*- Mode: c++; -*- */ 2 /* -------------------------------------------------------------------- 3 * Filename: 4 * src/maildirmessage.h 5 * 6 * Description: 7 * Declaration of the MaildirMessage class. 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 maildirmessage_h_included 39 #define maildirmessage_h_included 40 #include <string> 41 #include <map> 42 #include <vector> 43 #include <exception> 44 #include <iostream> 45 #include <time.h> 46 47 #include <stdio.h> 48 #include <string.h> 49 50 #include "message.h" 51 #include "address.h" 52 #include "mime.h" 53 54 namespace Binc { 55 56 class Maildir; 57 58 /*! 59 \class MaildirMessage 60 \brief The MaildirMessage class provides an interface for 61 IMAP messages. 62 63 Mailbox independent operations and properties are available 64 through this interface. 65 66 \sa Message 67 */ 68 class MaildirMessage : public Message { 69 public: 70 /*! 71 Sets the UID of a message. 72 \param uid The UID that will be set. 73 */ 74 void setUID(unsigned int uid); 75 76 /*! 77 Returns the UID of a message. 78 */ 79 unsigned int getUID(void) const; 80 81 /*! 82 Sets the size of the message. This size must be consistent with 83 the size reported when fetching the full message. 84 85 \param size The size of the message in characters, after 86 any conversion to CRLF. 87 */ 88 void setSize(unsigned int size); 89 90 /*! 91 Returns the size of the message, optionally determining the size 92 if it is not yet known. 93 94 \param determine If render is true and the size is unknown, the 95 size will be calculated and stored implicitly. Otherwise if the 96 size is unknown, 0 is returned. 97 */ 98 unsigned int getSize(bool determine = false) const; 99 100 /*! 101 Adds one or more flags to a message. 102 103 \param flags This is a bitmask of flags from the Flags enum. 104 */ 105 void setStdFlag(unsigned char flags); 106 107 /*! 108 Resets all flags on a message. 109 */ 110 void resetStdFlags(void); 111 112 /*! 113 Returns the flags that are set on a message. 114 */ 115 unsigned char getStdFlags(void) const; 116 117 /*! 118 Sets the internal flags. 119 120 \param flags a bitmask of the Flags enum. 121 */ 122 void setInternalFlag(unsigned char flags); 123 124 /*! 125 Removes the internal flags. 126 127 \param flags a bitmask of the Flags enum. 128 */ 129 void clearInternalFlag(unsigned char flags); 130 131 /*! 132 Returns the internal flags. 133 */ 134 unsigned char getInternalFlags(void) const; 135 136 /*! 137 Sets a state in a message that indicates that no flags have been 138 changed. Used together with hasFlagsChanged() to check if the 139 flags in this message have been changed. 140 */ 141 void setFlagsUnchanged(void); 142 143 /*! 144 Returns true if flags have been added or reset since the last 145 call to setFlagsUnchanged(), otherwise returns false. 146 */ 147 bool hasFlagsChanged(void) const; 148 149 /*! 150 Sets the internal date of a message. This is usually the date in 151 which the message arrived in the mailbox. 152 153 \param internaldate The internal date of the message in seconds 154 since the epoch. 155 */ 156 void setInternalDate(time_t internaldate); 157 158 /*! 159 Returns the internal date of the message in seconds since the 160 epoch. 161 */ 162 time_t getInternalDate(void) const; 163 164 /*! 165 Resets the current read position to the first character of this 166 message. 167 */ 168 void rewind(void); 169 170 /*! 171 Reads a chunk of up to 4096 bytes from a message. Call close() 172 before readChunk() to read the first chunk from a message. 173 174 readChunk() is used for copying or appending a message to a 175 mailbox. 176 177 \param chunk The characters are stored in this string. 178 */ 179 int readChunk(std::string &chunk); 180 181 /*! 182 Appends a chunk of bytes to a message. appendChunk() is used for 183 copying or appending a message to a mailbox. 184 185 \param chunk The content of this string is appended to the 186 message. 187 */ 188 bool appendChunk(const std::string &chunk); 189 190 /*! 191 Resets a message and frees all allocated resources. 192 */ 193 void close(void); 194 195 /*! 196 Marks the message as expunged. Equivalent to calling 197 setStdFlag() with F_EXPUNGED. 198 */ 199 void setExpunged(void); 200 201 /*! 202 Removes the F_EXPUNGED flag from the message. 203 */ 204 void setUnExpunged(void); 205 206 /*! 207 Returns true if the message is marked as expunged, otherwise 208 returns false. 209 */ 210 bool isExpunged(void) const; 211 212 /*! 213 Returns the first occurrance of a MIME header in a message, 214 counting from the top of the message and downwards, or "" if no 215 such header is found. 216 \param header The name of the header to be fetched. 217 */ 218 const std::string &getHeader(const std::string &header); 219 220 bool headerContains(const std::string &header, 221 const std::string &text); 222 223 bool bodyContains(const std::string &text); 224 bool textContains(const std::string &text); 225 226 bool printBodyStructure(bool extended = true) const; 227 228 bool printEnvelope(void) const; 229 230 bool printHeader(const std::string §ion, 231 std::vector<std::string> headers, 232 bool includeHeaders = false, 233 unsigned int startOffset = 0, 234 unsigned int length = UINTMAX, 235 bool mime = false) const; 236 unsigned int getHeaderSize(const std::string §ion, 237 std::vector<std::string> headers, 238 bool includeHeaders = false, 239 unsigned int startOffset = 0, 240 unsigned int length = UINTMAX, 241 bool mime = false) const; 242 243 bool printBody(const std::string §ion = "", 244 unsigned int startOffset = 0, 245 unsigned int length = UINTMAX) const; 246 unsigned int getBodySize(const std::string §ion, 247 unsigned int startOffset = 0, 248 unsigned int length = UINTMAX) const; 249 250 bool printDoc(unsigned int startOffset = 0, 251 unsigned int length = UINTMAX, 252 bool onlyText = false) const; 253 unsigned int getDocSize(unsigned int startOffset = 0, 254 unsigned int length = UINTMAX, 255 bool onlyText = false) const; 256 257 void setUnique(const std::string &s_in); 258 const std::string &getUnique(void) const; 259 260 //-- 261 MaildirMessage(Maildir &home); 262 ~MaildirMessage(void); 263 264 friend class Maildir; 265 266 bool operator < (const MaildirMessage &a) const; 267 268 MaildirMessage(const MaildirMessage ©); 269 MaildirMessage &operator = (const MaildirMessage ©); 270 271 enum Flags { 272 None = 0x00, 273 Expunged = 0x01, 274 FlagsChanged = 0x02, 275 JustArrived = 0x04, 276 WasWrittenTo = 0x08, 277 Committed = 0x10 278 }; 279 280 protected: 281 bool parseFull(void) const; 282 bool parseHeaders(void) const; 283 284 std::string getFixedFilename(void) const; 285 std::string getFileName(void) const; 286 287 void setFile(int fd); 288 int getFile(void) const; 289 290 void setSafeName(const std::string &name); 291 const std::string &getSafeName(void) const; 292 293 private: 294 mutable int fd; 295 mutable MimeDocument *doc; 296 mutable unsigned char internalFlags; 297 mutable unsigned char stdflags; 298 mutable unsigned int uid; 299 mutable unsigned int size; 300 mutable std::string unique; 301 mutable std::string safeName; 302 time_t internaldate; 303 304 Maildir &home; 305 static std::string storage; 306 }; 307 308 //------------------------------------------------------------------------ 309 class MaildirMessageCache 310 { 311 public: 312 ~MaildirMessageCache(); 313 314 enum ParseStatus { 315 NotParsed, 316 HeaderParsed, 317 AllParsed 318 }; 319 320 static MaildirMessageCache &getInstance(void); 321 322 void removeStatus(const MaildirMessage *); 323 void addStatus(const MaildirMessage *, ParseStatus pstat); 324 ParseStatus getStatus(const MaildirMessage *) const; 325 void clear(void); 326 327 private: 328 MaildirMessageCache(); 329 330 mutable std::map<const MaildirMessage *, ParseStatus> statuses; 331 mutable std::deque<const MaildirMessage *> parsed; 332 }; 333 } 334 335 #endif