File: maildir.cc

package info (click to toggle)
libbuffy 1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 3,796 kB
  • ctags: 380
  • sloc: sh: 8,946; cpp: 2,636; makefile: 75
file content (348 lines) | stat: -rw-r--r-- 8,271 bytes parent folder | download
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
 * Maildir folder access
 *
 * Copyright (C) 2004--2008  Enrico Zini <enrico@debian.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 */

#include <buffy/mailfolder/maildir.h>
#include <wibble/exception.h>
#include <buffy/utils/system.h>

#include <sys/types.h>  // stat, opendir, readdir, utimes
#include <sys/stat.h>   // stat
#include <dirent.h>		// opendir, readdir
#include <sys/time.h>	// utimes

#include <cerrno>
#include <cstring>

using namespace std;
using namespace wibble;

namespace buffy {
namespace mailfolder {

static string concat(const std::string& str1, char sep, const std::string& str2)
{
	// Avoid adding a separator if str1 or str are empty, if str1 ends with the
	// separator or if str2 starts with the separator
	if (!str1.empty() && str1[str1.size() - 1] != sep && ! str2.empty() && str2[0] != sep)
		return str1 + sep + str2;
	else
		return str1 + str2;
}

Maildir::Maildir(const std::string& path) throw ()
	: _path(path), _stat_total(-1), _stat_unread(-1), _stat_new(-1), _stat_flagged(-1),
	  _deleted(false), _new_mtime(0), _cur_mtime(0)
{
	_name = _path;

	/// Normalize the folder name

	// Remove trailing '/'
	while (_name[_name.size() - 1] == '/')
		_name.resize(_name.size() - 1);

	// Remove leading path
	size_t lastslash = _name.find_last_of('/');
	if (lastslash != string::npos)
		_name = _name.substr(lastslash + 1);

	// Remove leading dot
	if (_name[0] == '.')
		_name = _name.substr(1);
}

Maildir::Maildir(const std::string& name, const std::string& path) throw ()
	: _name(name), _path(path), _stat_total(-1), _stat_unread(-1), _stat_new(-1), _stat_flagged(-1),
	  _deleted(false), _new_mtime(0), _cur_mtime(0) {}


bool Maildir::changed()
{
	// Compute 'new' and 'cur' directory names
	string path_new = _path + "/new";
	string path_cur = _path + "/cur";

	struct stat st_new;
	if (!statIfFound(path_new, &st_new))
		if (! _deleted)
		{
			_deleted = true;
			return true;
		}

	struct stat st_cur;
	if (!statIfFound(path_cur, &st_cur))
		if (! _deleted)
		{
			_deleted = true;
			return true;
		}

	if (_deleted)
	{
		_deleted = false;
		return true;
	}

	return st_new.st_mtime > _new_mtime || st_cur.st_mtime > _cur_mtime;
}

void Maildir::updateStatistics()
{
	int res_total = 0;
	int res_unread = 0;
	int res_new = 0;
	int res_flagged = 0;

	// Compute 'new' and 'cur' directory names
	string path_new = _path + "/new";
	string path_cur = _path + "/cur";

	// Perform consistency checks on the 'new' directory
	struct stat st_new;
	if (!statIfFound(path_new, &st_new))
	{
		_stat_total = 0;
		_stat_unread = 0;
		_stat_new = 0;
		_stat_flagged = 0;
		_deleted = true;
		return;
	}
	if (S_ISDIR(st_new.st_mode) == 0)
		throw wibble::exception::Consistency(path_new + " is not a directory");

	// Perform consistency checks on the 'cur' directory
	struct stat st_cur;
	if (!statIfFound(path_cur, &st_cur))
	{
		_stat_total = 0;
		_stat_unread = 0;
		_stat_new = 0;
		_stat_flagged = 0;
		_deleted = true;
		return;
	}
	if (S_ISDIR(st_cur.st_mode) == 0)
		throw wibble::exception::Consistency(path_cur + " is not a directory");

	if (_deleted)
		_deleted = false;

	_new_mtime = st_new.st_mtime;
	_cur_mtime = st_cur.st_mtime;

	/// Count messages in the 'new' directory

	// Count the files in the 'new' directory
	{
		Directory dir(path_new);
		while (struct dirent* d = dir.read())
		{
			if (d->d_name[0] == '.') 
				continue;
			res_total++;
			res_new++;
		}
	}

	// Restore the access time of the mailbox for other checking programs
	struct timeval t[2];
	t[0].tv_sec = st_new.st_atime;
	t[0].tv_usec = 0;
	t[1].tv_sec = st_new.st_mtime;
	t[1].tv_usec = 0;
	utimes(path_new.c_str(), t);


	/// Count messages in the 'cur' directory

	// Count the files in the 'cur' directory
	{
		Directory dir(path_cur);
		while (struct dirent *d = dir.read())
		{
			if (d->d_name[0] == '.') 
				continue;

			res_total++;

			// Look for an `info' block in the name
			char* info = strrchr(d->d_name, ':');
			if (info == 0)
				continue;

			// Ensure that the info block is in the right format
			if (strncmp(info, ":2,", 3) != 0)
				continue;

			// Look for the 'S' flag (it should not be there)
			info += 3;
			if (strchr(info, 'S') == 0)
				res_unread++;
			if (strchr(info, 'F') != 0)
				res_flagged++;
		}
	}

	// Restore the access time of the mailbox for other checking programs
	t[0].tv_sec = st_cur.st_atime;
	t[0].tv_usec = 0;
	t[1].tv_sec = st_cur.st_mtime;
	t[1].tv_usec = 0;
	utimes(path_cur.c_str(), t);


	// Return the values
	_stat_total = res_total;
	_stat_unread = res_unread + res_new;
	_stat_new = res_new;
	_stat_flagged = res_flagged;
}


static bool isMaildir(const std::string& pathname)
{
	try {
		// It must be a directory
		struct stat st;
		stat(pathname, &st);
		if (S_ISDIR(st.st_mode) == 0)
			return false;

		// It must contain cur, new and tmp subdirectories
		const char* subdirs[3] = { "cur", "new", "tmp" };
		for (int i = 0; i < 3; i++)
		{
			string subdir = concat(pathname, '/', subdirs[i]);

			struct stat st;
			if (stat(subdir.c_str(), &st) != 0)
				return false;
			if (S_ISDIR(st.st_mode) == 0)
				return false;
		}

		// It appears to be a maildir directory
		return true;
	} catch (wibble::exception::File& e) {
		// If we can't even stat() it, then it's not a maildir
		return false;
	}
}

MailFolder Maildir::accessFolder(const std::string& path)
{
	try {
		if (isMaildir(path))
			return MailFolder(new Maildir(path));
	} catch (wibble::exception::System& e) {
		// FIXME: cerr << e.type() << ": " << e.fullInfo() << endl;
	}
	return MailFolder();
}

static void enumerateSubfolders(
		const std::string& parent,
		const std::string& name,
		MailFolderConsumer& cons,
		InodeSet seen = InodeSet())
{
	try {
		struct stat st;
		try {
			stat(parent, &st);
		} catch (wibble::exception::File& e) {
			// If we can't even stat() it, then we don't try to enumerate its subfolders
			// FIXME: cerr << e.type() << ": " << e.fullInfo() << endl;
			return;
		}
		// It must be a directory
		if (S_ISDIR(st.st_mode) == 0)
			return;

		// Check that we aren't looping
		if (seen.has(st.st_ino))
			return;

		if (isMaildir(parent))
		{
			MailFolder f(new Maildir(name, parent));
			cons.consume(f);
		}

		// Recursively enumerate the Maildirs in the directory
		Directory dir(parent);
		while (struct dirent *d = dir.read())
		{
			if (strcmp(d->d_name, ".") == 0)
				continue;
			if (strcmp(d->d_name, "..") == 0)
				continue;
			if (strcmp(d->d_name, "tmp") == 0)
				continue;
			if (strcmp(d->d_name, "cur") == 0)
				continue;
			if (strcmp(d->d_name, "new") == 0)
				continue;

			enumerateSubfolders(
					concat(parent, '/', d->d_name),
					concat(name, '.', d->d_name),
					cons, seen + st.st_ino);
		}
	} catch (wibble::exception::Generic& e) {
		// FIXME: cerr << e.type() << ": " << e.fullInfo() << endl;
	}
}

void Maildir::enumerateFolders(const std::string& parent, MailFolderConsumer& cons)
{
	// Remove trailing slash from the parent directory
	// The root name is empty if parent is not a maildir
	//   else, it is the last component of parent's path
	string root;
	string rootName;

	size_t pos = parent.rfind('/');
	if (pos == string::npos)
		root = rootName = parent;
	else if (pos == parent.size() - 1)
	{
		pos = parent.rfind('/', pos - 1);
		root = parent.substr(0, parent.size() - 1);
		rootName = parent.substr(pos+1, parent.size() - pos - 2);
	}
	else
	{
		root = parent;
		rootName = parent.substr(pos + 1);
	}

	if (!isMaildir(parent))
		rootName = string();

	enumerateSubfolders(root, rootName, cons);
}

}
}

// vim:set ts=4 sw=4: