summaryrefslogtreecommitdiff
path: root/src/maildir-updateflags.cc
blob: be37234a504e35e509a23f783289f2fbc9c990d9 (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
/**  --------------------------------------------------------------------
 *  @file maildir-updateflags.cc
 *  @brief  Implementation of the Maildir class.
 *  @author Andreas Aardal Hanssen
 *  @date 2002-2005 
 *  -----------------------------------------------------------------  **/
#include "maildir.h"

#include <dirent.h>
#include <unistd.h>
#include <errno.h>

#include "iodevice.h"
#include "iofactory.h"

using namespace ::std;

//------------------------------------------------------------------------
void Binc::Maildir::updateFlags(void)
{
  // don't update a read-only mailbox.
  if (readOnly) return;

  // open the cur/ directory
  string curpath = path + "/cur/";
  DIR *pdir = opendir(curpath.c_str());
  if (pdir == 0) {
    bincError << "failed to open " << curpath << ": " 
	      << strerror(errno) << endl;
    return;
  }

  // read all entries in the directory
  vector<string> entries;  
  struct dirent *pdirent;
  while ((pdirent = readdir(pdir)) != 0) {
    string filename = pdirent->d_name;
    if (filename[0] == '.')
      continue;
    entries.push_back(filename);
  }
  closedir(pdir);

  bool customFlagsChanged = false;

  // loop through all messages in cur/, and update the flags that have
  // changed.
  for (vector<string>::const_iterator it = entries.begin(); it != entries.end(); ++it) {
    string filename = *it;

    // separate the unique name from the flags. accept messages that do not
    // contain the flags section.
    string uniquename;
    string::size_type pos;
    if ((pos = filename.find(":2,")) != string::npos)
      uniquename = filename.substr(0, pos);
    else
      uniquename = filename;    

    // only update flags for messages that are known.
    MaildirMessage *message = get(uniquename);
    if (!message)
      continue;

    // check if custom flags have changed; if so, we need to regenerate the
    // cache file.
    if (message->internalFlags & MaildirMessage::CustomFlagsChanged)
      customFlagsChanged = true;

    // generate the flag string.
    string flags;
    int mflags = message->getStdFlags();
    if (mflags & Message::F_DRAFT) flags += "D";
    if (mflags & Message::F_FLAGGED) flags += "F";
    if (mflags & Message::F_PASSED) flags += "P";
    if (mflags & Message::F_ANSWERED) flags += "R";
    if (mflags & Message::F_SEEN) flags += "S";
    if (mflags & Message::F_DELETED) flags += "T";

    // prepare the old and new message name. if equal, skip to the next
    // message.
    string srcname = curpath + filename;
    string destname = curpath + uniquename + ":2," + flags;
    if (srcname == destname)
      continue;

    // rename the file
    if (rename(srcname.c_str(), destname.c_str()) != 0) {
      if (errno == ENOENT) {
        closedir(pdir);
        if ((pdir = opendir(curpath.c_str())) == 0) {
          bincError << "failed to open " << curpath << ": " 
                    << strerror(errno) << endl;
          return;
        }

    // restart scan. this catches the race condition where concurrent
    // clients may have set a flag or removed the message.
        continue;
      }

      bincError << "failed to rename  " << srcname
                << " to " << destname << ": " 
                << strerror(errno) << endl;
    } else {
      index.insert(uniquename, 0,  uniquename + ":2," + flags);
    }
  }

  // finally, regenerate the cache file if the custom flags have changed.
  if (customFlagsChanged) {
    Lock lock(path);
    writeCache();
  }
}