summaryrefslogtreecommitdiff
path: root/src/include/maildir.h
blob: 6ba1df314b48695524bbf068805e3b5e1f926193 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
 *  @file  maildir.h
 *  @brief Declaration of the Maildir class.
 *  @author Andreas Aardal Hanssen
 *  @date 2002-2005
 */

#ifndef maildir_h_included
#define maildir_h_included

#include "mailbox.h"
#include "maildirmessage.h"

#include <map>
#include <string>
#include <vector>

namespace Binc {
  static const std::string CACHEFILEVERSION = "1.0.5";
  static const std::string UIDVALFILEVERSION = "1.0.5";

  class Lock {
  public:
    Lock(const std::string &path);
    ~Lock();

  private:
    std::string lock;
  };

  class MaildirIndexItem {
  public:
    unsigned int uid;
    std::string fileName;
  };

  class MaildirIndex {
  private:
    std::map<std::string, MaildirIndexItem> idx;

  public:
    void insert(const std::string &unique, unsigned int uid, const std::string &fileName = "");
    void remove(const std::string &unique);
    void clear();
    void clearFileNames();
    void clearUids();
    unsigned int getSize() const;
    MaildirIndexItem *find(const std::string &unique);
  };

  class Maildir : public Mailbox {
  public:
    typedef std::map<unsigned int, MaildirMessage> MessageMap;

    class iterator : public BaseIterator {
    public:
      iterator();
      iterator(Maildir *home,
               MessageMap::iterator i,
               const SequenceSet &bset,
               unsigned int mod = INCLUDE_EXPUNGED | SQNR_MODE);
      iterator(const iterator &copy);
      ~iterator();

      Message &operator*();
      void operator++();
      bool operator==(const BaseIterator &) const;
      bool operator!=(const BaseIterator &) const;

      iterator &operator=(const iterator &copy);

      void erase();

      friend class Maildir;

    protected:
      void reposition();
      MaildirMessage &curMessage();

    private:
      Maildir *mailbox;
      SequenceSet bset;
      int mod;

      MessageMap::iterator i;
      unsigned int uidmax;
      unsigned int sqnrmax;

      iterator(iterator &external);
    };

    const std::string getTypeName() const;

    Mailbox::iterator begin(const SequenceSet &bset,
                            unsigned int mod = INCLUDE_EXPUNGED | SQNR_MODE) const;
    Mailbox::iterator end() const;

    unsigned int getMaxUid() const;
    unsigned int getMaxSqnr() const;
    unsigned int getUidValidity() const;
    unsigned int getUidNext() const;

    bool getUpdates(bool doscan, unsigned int type, PendingUpdates &updates, bool forceScan);

    const std::string &getPath() const;
    void setPath(const std::string &path_in);

    void bumpUidValidity(const std::string &) const;

    unsigned int getStatusID(const std::string &) const;
    bool getStatus(const std::string &, Status &) const;
    void updateFlags();

    bool isMailbox(const std::string &) const;
    bool isMarked(const std::string &) const;
    bool selectMailbox(const std::string &name, const std::string &s_in);
    void closeMailbox();
    void expungeMailbox();
    bool createMailbox(const std::string &s,
                       mode_t mode,
                       uid_t owner = 0,
                       gid_t group = 0,
                       bool root = false);
    bool deleteMailbox(const std::string &s);

    Message *createMessage(const std::string &mbox, time_t idate = 0);
    bool commitNewMessages(const std::string &mbox);
    bool rollBackNewMessages();

    bool fastCopy(Message &source, Mailbox &desttype, const std::string &destname);

    Maildir();
    ~Maildir();

    friend class Maildir::iterator;
    friend class MaildirMessage;

  protected:
    enum ReadCacheResult { Ok, NoCache, Error };

    ReadCacheResult readCache();
    bool writeCache();
    bool scanFileNames() const;

    enum ScanResult { Success = 0, TemporaryError = 1, PermanentError = 2 };

    ScanResult scan(bool forceScan = false);

    MaildirMessage *get(const std::string &id);
    void add(MaildirMessage &m);

  private:
    std::vector<MaildirMessage> newMessages;

    unsigned int uidvalidity;
    unsigned int uidnext;
    bool selected;
    std::string path;

    mutable iterator beginIterator;
    mutable iterator endIterator;

    mutable bool firstscan;
    mutable bool cacheRead;
    mutable MaildirIndex index;
    mutable MessageMap messages;

    mutable unsigned int oldrecent;
    mutable unsigned int oldexists;

    mutable time_t old_bincimap_cache_st_mtime;
    mutable time_t old_bincimap_cache_st_ctime;
    mutable time_t old_cur_st_mtime;
    mutable time_t old_cur_st_ctime;
    mutable time_t old_new_st_mtime;
    mutable time_t old_new_st_ctime;

    mutable bool mailboxchanged;
  };
}

#endif