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 )
8 * Last Updated : $Date: 2004/03/28 00:28:58 $
18 #include <sys/types.h>
28 #define PCM (pc->u).maildir
30 static int count_msgs(char *path
)
39 "Error opening directory '%s': %s\n", path
, strerror(errno
));
43 while ((de
= readdir(D
)) != NULL
) {
44 if ((strcmp(de
->d_name
, ".") & strcmp(de
->d_name
, "..")) != 0) {
53 int maildirCheckHistory(Pop3 pc
)
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 */
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) {
80 "Can't create dircache flush file '%s': %s\n",
81 path_newtmp
, strerror(errno
));
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 */
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 */
140 int maildirCreate(Pop3 pc
, const char *str
)
144 /* Maildir format: maildir:fullpathname */
149 pc
->OldUnreadMsgs
= -1;
150 pc
->checkMail
= maildirCheckHistory
;
151 pc
->u
.maildir
.dircache_flush
= 0;
154 if (*(str
+ 8) == ':') { /* path is of the format maildir::flags:path */
156 for (i
= 1; c
!= ':' && c
!= '\0'; i
++) {
160 pc
->u
.maildir
.dircache_flush
= 1;
161 DM(pc
, DEBUG_INFO
, "maildir: dircache_flush enabled\n");
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
);
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
);