Remove trailing whitespace.
[dockapps.git] / wmbiff / wmbiff / maildirClient.c
blob5858e465e575ca90e711f74668e84e8700b7dac0
1 /* $Id: maildirClient.c,v 1.15 2004/03/28 00:28:58 bluehal Exp $ */
2 /* Author : Yong-iL Joh ( tolkien@mizi.com )
3 Modified : Jorge GarcĂ­a ( Jorge.Garcia@uv.es )
4 Modified : Dwayne C. Litzenberger ( dlitz@dlitz.net )
6 * Maildir checker.
8 * Last Updated : $Date: 2004/03/28 00:28:58 $
12 #ifdef HAVE_CONFIG_H
13 #include <config.h>
14 #endif
16 #include "Client.h"
17 #include <sys/stat.h>
18 #include <sys/types.h>
19 #include <dirent.h>
20 #include <errno.h>
21 #include <utime.h>
22 #include <unistd.h>
23 #ifdef USE_DMALLOC
24 #include <dmalloc.h>
25 #endif
28 #define PCM (pc->u).maildir
30 static int count_msgs(char *path)
32 DIR *D;
33 struct dirent *de;
34 int count = 0;
36 D = opendir(path);
37 if (D == NULL) {
38 DMA(DEBUG_ERROR,
39 "Error opening directory '%s': %s\n", path, strerror(errno));
40 return -1;
43 while ((de = readdir(D)) != NULL) {
44 if ((strcmp(de->d_name, ".") & strcmp(de->d_name, "..")) != 0) {
45 count++;
48 closedir(D);
50 return count;
53 int maildirCheckHistory(Pop3 pc)
55 struct stat st_new;
56 struct stat st_cur;
57 struct utimbuf ut;
58 char path_new[BUF_BIG * 2], path_cur[BUF_BIG * 2];
60 int count_new = 0, count_cur = 0;
62 DM(pc, DEBUG_INFO, ">Maildir: '%s'\n", pc->path);
64 strcpy(path_new, pc->path);
65 strcat(path_new, "/new/");
66 strcpy(path_cur, pc->path);
67 strcat(path_cur, "/cur/");
69 if (pc->u.maildir.dircache_flush) {
70 /* hack to clear directory cache for network-mounted maildirs */
71 int fd;
72 char path_newtmp[BUF_BIG * 2 + 32];
73 strcpy(path_newtmp, path_new);
74 strcat(path_newtmp, ".wmbiff.dircache_flush.XXXXXX");
75 if ((fd = mkstemp(path_newtmp)) >= 0) {
76 close(fd);
77 unlink(path_newtmp);
78 } else {
79 DM(pc, DEBUG_ERROR,
80 "Can't create dircache flush file '%s': %s\n",
81 path_newtmp, strerror(errno));
85 /* maildir */
86 if (stat(path_new, &st_new)) {
87 DM(pc, DEBUG_ERROR, "Can't stat mailbox '%s': %s\n",
88 path_new, strerror(errno));
89 return -1; /* Error stating mailbox */
91 if (stat(path_cur, &st_cur)) {
92 DM(pc, DEBUG_ERROR, "Can't stat mailbox '%s': %s\n",
93 path_cur, strerror(errno));
94 return -1; /* Error stating mailbox */
98 /* file was changed OR initially read */
99 if (st_new.st_mtime != PCM.mtime_new
100 || st_new.st_size != PCM.size_new
101 || st_cur.st_mtime != PCM.mtime_cur
102 || st_cur.st_size != PCM.size_cur || pc->OldMsgs < 0) {
103 DM(pc, DEBUG_INFO, " was changed,\n"
104 " TIME(new): old %lu, new %lu"
105 " SIZE(new): old %lu, new %lu\n"
106 " TIME(cur): old %lu, new %lu"
107 " SIZE(cur): old %lu, new %lu\n",
108 PCM.mtime_new, (unsigned long) st_new.st_mtime,
109 (unsigned long) PCM.size_new, (unsigned long) st_new.st_size,
110 PCM.mtime_cur, (unsigned long) st_cur.st_mtime,
111 (unsigned long) PCM.size_cur, (unsigned long) st_cur.st_size);
113 count_new = count_msgs(path_new);
114 count_cur = count_msgs(path_cur);
115 if ((count_new | count_cur) == -1) { /* errors occurred */
116 return -1;
119 pc->TotalMsgs = count_cur + count_new;
120 pc->UnreadMsgs = count_new;
122 /* Reset atime for MUTT and something others work correctly */
123 ut.actime = st_new.st_atime;
124 ut.modtime = st_new.st_mtime;
125 utime(path_new, &ut);
126 ut.actime = st_cur.st_atime;
127 ut.modtime = st_cur.st_mtime;
128 utime(path_cur, &ut);
130 /* Store new values */
131 PCM.mtime_new = st_new.st_mtime; /* Store new mtime_new */
132 PCM.size_new = st_new.st_size; /* Store new size_new */
133 PCM.mtime_cur = st_cur.st_mtime; /* Store new mtime_cur */
134 PCM.size_cur = st_cur.st_size; /* Store new size_cur */
137 return 0;
140 int maildirCreate(Pop3 pc, const char *str)
142 int i;
143 char c;
144 /* Maildir format: maildir:fullpathname */
146 pc->TotalMsgs = 0;
147 pc->UnreadMsgs = 0;
148 pc->OldMsgs = -1;
149 pc->OldUnreadMsgs = -1;
150 pc->checkMail = maildirCheckHistory;
151 pc->u.maildir.dircache_flush = 0;
153 /* special flags */
154 if (*(str + 8) == ':') { /* path is of the format maildir::flags:path */
155 c = ' ';
156 for (i = 1; c != ':' && c != '\0'; i++) {
157 c = *(str + 8 + i);
158 switch (c) {
159 case 'F':
160 pc->u.maildir.dircache_flush = 1;
161 DM(pc, DEBUG_INFO, "maildir: dircache_flush enabled\n");
164 } else {
165 i = 0;
167 if (strlen(str + 8 + i) + 1 > BUF_BIG) {
168 DM(pc, DEBUG_ERROR, "maildir '%s' is too long.\n", str + 8 + i);
169 memset(pc->path, 0, BUF_BIG);
170 } else {
171 strncpy(pc->path, str + 8 + i, BUF_BIG - 1); /* cut off ``maildir:'' */
174 DM(pc, DEBUG_INFO, "maildir: str = '%s'\n", str);
175 DM(pc, DEBUG_INFO, "maildir: path= '%s'\n", pc->path);
177 return 0;
180 /* vim:set ts=4: */
182 * Local Variables:
183 * tab-width: 4
184 * c-indent-level: 4
185 * c-basic-offset: 4
186 * End: