rev |
line source |
yuuji@0
|
1 /* ========================================================================
|
yuuji@0
|
2 * Copyright 1988-2007 University of Washington
|
yuuji@0
|
3 *
|
yuuji@0
|
4 * Licensed under the Apache License, Version 2.0 (the "License");
|
yuuji@0
|
5 * you may not use this file except in compliance with the License.
|
yuuji@0
|
6 * You may obtain a copy of the License at
|
yuuji@0
|
7 *
|
yuuji@0
|
8 * http://www.apache.org/licenses/LICENSE-2.0
|
yuuji@0
|
9 *
|
yuuji@0
|
10 *
|
yuuji@0
|
11 * ========================================================================
|
yuuji@0
|
12 */
|
yuuji@0
|
13
|
yuuji@0
|
14 /*
|
yuuji@0
|
15 * Program: MBX mail routines
|
yuuji@0
|
16 *
|
yuuji@0
|
17 * Author: Mark Crispin
|
yuuji@0
|
18 * Networks and Distributed Computing
|
yuuji@0
|
19 * Computing & Communications
|
yuuji@0
|
20 * University of Washington
|
yuuji@0
|
21 * Administration Building, AG-44
|
yuuji@0
|
22 * Seattle, WA 98195
|
yuuji@0
|
23 * Internet: MRC@CAC.Washington.EDU
|
yuuji@0
|
24 *
|
yuuji@0
|
25 * Date: 3 October 1995
|
yuuji@0
|
26 * Last Edited: 11 October 2007
|
yuuji@0
|
27 */
|
yuuji@0
|
28
|
yuuji@0
|
29
|
yuuji@0
|
30 /* FILE TIME SEMANTICS
|
yuuji@0
|
31 *
|
yuuji@0
|
32 * The atime is the last read time of the file.
|
yuuji@0
|
33 * The mtime is the last flags update time of the file.
|
yuuji@0
|
34 * The ctime is the last write time of the file.
|
yuuji@0
|
35 */
|
yuuji@0
|
36
|
yuuji@0
|
37 #include <stdio.h>
|
yuuji@0
|
38 #include <ctype.h>
|
yuuji@0
|
39 #include <errno.h>
|
yuuji@0
|
40 extern int errno; /* just in case */
|
yuuji@0
|
41 #include "mail.h"
|
yuuji@0
|
42 #include "osdep.h"
|
yuuji@0
|
43 #include <pwd.h>
|
yuuji@0
|
44 #include <sys/stat.h>
|
yuuji@0
|
45 #include <sys/time.h>
|
yuuji@0
|
46 #include "misc.h"
|
yuuji@0
|
47 #include "dummy.h"
|
yuuji@0
|
48 #include "fdstring.h"
|
yuuji@0
|
49
|
yuuji@0
|
50
|
yuuji@0
|
51 /* Build parameters */
|
yuuji@0
|
52
|
yuuji@0
|
53 #define HDRSIZE 2048
|
yuuji@0
|
54
|
yuuji@0
|
55
|
yuuji@0
|
56 /* Kludge to make Cygwin happy */
|
yuuji@0
|
57
|
yuuji@0
|
58 #ifndef O_BINARY
|
yuuji@0
|
59 #define O_BINARY 0
|
yuuji@0
|
60 #endif
|
yuuji@0
|
61
|
yuuji@0
|
62 /* MBX I/O stream local data */
|
yuuji@0
|
63
|
yuuji@0
|
64 typedef struct mbx_local {
|
yuuji@0
|
65 unsigned int flagcheck: 1; /* if ping should sweep for flags */
|
yuuji@0
|
66 unsigned int expok: 1; /* if expunging OK in ping */
|
yuuji@0
|
67 unsigned int expunged : 1; /* if one or more expunged messages */
|
yuuji@0
|
68 int fd; /* file descriptor for I/O */
|
yuuji@0
|
69 int ld; /* lock file descriptor */
|
yuuji@0
|
70 int ffuserflag; /* first free user flag */
|
yuuji@0
|
71 off_t filesize; /* file size parsed */
|
yuuji@0
|
72 time_t filetime; /* last file time */
|
yuuji@0
|
73 time_t lastsnarf; /* last snarf time */
|
yuuji@0
|
74 unsigned long lastpid; /* PID of last writer */
|
yuuji@0
|
75 unsigned char *buf; /* temporary buffer */
|
yuuji@0
|
76 unsigned long buflen; /* current size of temporary buffer */
|
yuuji@0
|
77 char lock[MAILTMPLEN]; /* buffer to write lock name */
|
yuuji@0
|
78 } MBXLOCAL;
|
yuuji@0
|
79
|
yuuji@0
|
80
|
yuuji@0
|
81 /* Convenient access to local data */
|
yuuji@0
|
82
|
yuuji@0
|
83 #define LOCAL ((MBXLOCAL *) stream->local)
|
yuuji@0
|
84
|
yuuji@0
|
85 /* Function prototypes */
|
yuuji@0
|
86
|
yuuji@0
|
87 DRIVER *mbx_valid (char *name);
|
yuuji@0
|
88 int mbx_isvalid (MAILSTREAM **stream,char *name,char *tmp,int *ld,char *lock,
|
yuuji@0
|
89 long flags);
|
yuuji@0
|
90 void *mbx_parameters (long function,void *value);
|
yuuji@0
|
91 void mbx_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents);
|
yuuji@0
|
92 void mbx_list (MAILSTREAM *stream,char *ref,char *pat);
|
yuuji@0
|
93 void mbx_lsub (MAILSTREAM *stream,char *ref,char *pat);
|
yuuji@0
|
94 long mbx_create (MAILSTREAM *stream,char *mailbox);
|
yuuji@0
|
95 long mbx_delete (MAILSTREAM *stream,char *mailbox);
|
yuuji@0
|
96 long mbx_rename (MAILSTREAM *stream,char *old,char *newname);
|
yuuji@0
|
97 long mbx_status (MAILSTREAM *stream,char *mbx,long flags);
|
yuuji@0
|
98 MAILSTREAM *mbx_open (MAILSTREAM *stream);
|
yuuji@0
|
99 void mbx_close (MAILSTREAM *stream,long options);
|
yuuji@0
|
100 void mbx_abort (MAILSTREAM *stream);
|
yuuji@0
|
101 void mbx_flags (MAILSTREAM *stream,char *sequence,long flags);
|
yuuji@0
|
102 char *mbx_header (MAILSTREAM *stream,unsigned long msgno,unsigned long *length,
|
yuuji@0
|
103 long flags);
|
yuuji@0
|
104 long mbx_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags);
|
yuuji@0
|
105 void mbx_flag (MAILSTREAM *stream,char *sequence,char *flag,long flags);
|
yuuji@0
|
106 void mbx_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt);
|
yuuji@0
|
107 long mbx_ping (MAILSTREAM *stream);
|
yuuji@0
|
108 void mbx_check (MAILSTREAM *stream);
|
yuuji@0
|
109 long mbx_expunge (MAILSTREAM *stream,char *sequence,long options);
|
yuuji@0
|
110 void mbx_snarf (MAILSTREAM *stream);
|
yuuji@0
|
111 long mbx_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options);
|
yuuji@0
|
112 long mbx_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data);
|
yuuji@0
|
113
|
yuuji@0
|
114 char *mbx_file (char *dst,char *name);
|
yuuji@0
|
115 long mbx_parse (MAILSTREAM *stream);
|
yuuji@0
|
116 MESSAGECACHE *mbx_elt (MAILSTREAM *stream,unsigned long msgno,long expok);
|
yuuji@0
|
117 unsigned long mbx_read_flags (MAILSTREAM *stream,MESSAGECACHE *elt);
|
yuuji@0
|
118 void mbx_update_header (MAILSTREAM *stream);
|
yuuji@0
|
119 void mbx_update_status (MAILSTREAM *stream,unsigned long msgno,long flags);
|
yuuji@0
|
120 unsigned long mbx_hdrpos (MAILSTREAM *stream,unsigned long msgno,
|
yuuji@0
|
121 unsigned long *size,char **hdr);
|
yuuji@0
|
122 unsigned long mbx_rewrite (MAILSTREAM *stream,unsigned long *reclaimed,
|
yuuji@0
|
123 long flags);
|
yuuji@0
|
124 long mbx_flaglock (MAILSTREAM *stream);
|
yuuji@0
|
125
|
yuuji@0
|
126 /* MBX mail routines */
|
yuuji@0
|
127
|
yuuji@0
|
128
|
yuuji@0
|
129 /* Driver dispatch used by MAIL */
|
yuuji@0
|
130
|
yuuji@0
|
131 DRIVER mbxdriver = {
|
yuuji@0
|
132 "mbx", /* driver name */
|
yuuji@0
|
133 DR_LOCAL|DR_MAIL|DR_CRLF|DR_LOCKING,
|
yuuji@0
|
134 /* driver flags */
|
yuuji@0
|
135 (DRIVER *) NIL, /* next driver */
|
yuuji@0
|
136 mbx_valid, /* mailbox is valid for us */
|
yuuji@0
|
137 mbx_parameters, /* manipulate parameters */
|
yuuji@0
|
138 mbx_scan, /* scan mailboxes */
|
yuuji@0
|
139 mbx_list, /* list mailboxes */
|
yuuji@0
|
140 mbx_lsub, /* list subscribed mailboxes */
|
yuuji@0
|
141 NIL, /* subscribe to mailbox */
|
yuuji@0
|
142 NIL, /* unsubscribe from mailbox */
|
yuuji@0
|
143 mbx_create, /* create mailbox */
|
yuuji@0
|
144 mbx_delete, /* delete mailbox */
|
yuuji@0
|
145 mbx_rename, /* rename mailbox */
|
yuuji@0
|
146 mbx_status, /* status of mailbox */
|
yuuji@0
|
147 mbx_open, /* open mailbox */
|
yuuji@0
|
148 mbx_close, /* close mailbox */
|
yuuji@0
|
149 mbx_flags, /* fetch message "fast" attributes */
|
yuuji@0
|
150 mbx_flags, /* fetch message flags */
|
yuuji@0
|
151 NIL, /* fetch overview */
|
yuuji@0
|
152 NIL, /* fetch message envelopes */
|
yuuji@0
|
153 mbx_header, /* fetch message header */
|
yuuji@0
|
154 mbx_text, /* fetch message body */
|
yuuji@0
|
155 NIL, /* fetch partial message text */
|
yuuji@0
|
156 NIL, /* unique identifier */
|
yuuji@0
|
157 NIL, /* message number */
|
yuuji@0
|
158 mbx_flag, /* modify flags */
|
yuuji@0
|
159 mbx_flagmsg, /* per-message modify flags */
|
yuuji@0
|
160 NIL, /* search for message based on criteria */
|
yuuji@0
|
161 NIL, /* sort messages */
|
yuuji@0
|
162 NIL, /* thread messages */
|
yuuji@0
|
163 mbx_ping, /* ping mailbox to see if still alive */
|
yuuji@0
|
164 mbx_check, /* check for new messages */
|
yuuji@0
|
165 mbx_expunge, /* expunge deleted messages */
|
yuuji@0
|
166 mbx_copy, /* copy messages to another mailbox */
|
yuuji@0
|
167 mbx_append, /* append string message to mailbox */
|
yuuji@0
|
168 NIL /* garbage collect stream */
|
yuuji@0
|
169 };
|
yuuji@0
|
170
|
yuuji@0
|
171 /* prototype stream */
|
yuuji@0
|
172 MAILSTREAM mbxproto = {&mbxdriver};
|
yuuji@0
|
173
|
yuuji@0
|
174 /* MBX mail validate mailbox
|
yuuji@0
|
175 * Accepts: mailbox name
|
yuuji@0
|
176 * Returns: our driver if name is valid, NIL otherwise
|
yuuji@0
|
177 */
|
yuuji@0
|
178
|
yuuji@0
|
179 DRIVER *mbx_valid (char *name)
|
yuuji@0
|
180 {
|
yuuji@0
|
181 char tmp[MAILTMPLEN];
|
yuuji@0
|
182 int fd = mbx_isvalid (NIL,name,tmp,NIL,NIL,NIL);
|
yuuji@0
|
183 if (fd < 0) return NIL;
|
yuuji@0
|
184 close (fd); /* don't need the fd now */
|
yuuji@0
|
185 return &mbxdriver;
|
yuuji@0
|
186 }
|
yuuji@0
|
187
|
yuuji@0
|
188
|
yuuji@0
|
189 /* MBX mail test for valid mailbox
|
yuuji@0
|
190 * Accepts: returned stream with valid mailbox keywords
|
yuuji@0
|
191 * mailbox name
|
yuuji@0
|
192 * scratch buffer
|
yuuji@0
|
193 * returned lock fd
|
yuuji@0
|
194 * returned lock name
|
yuuji@0
|
195 * RW flags or NIL for readonly
|
yuuji@0
|
196 * Returns: file descriptor if valid, NIL otherwise
|
yuuji@0
|
197 */
|
yuuji@0
|
198
|
yuuji@0
|
199 #define MBXISVALIDNOUID 0x1 /* RW, don't do UID action */
|
yuuji@0
|
200 #define MBXISVALIDUID 0x2 /* RW, do UID action */
|
yuuji@0
|
201
|
yuuji@0
|
202 int mbx_isvalid (MAILSTREAM **stream,char *name,char *tmp,int *ld,char *lock,
|
yuuji@0
|
203 long flags)
|
yuuji@0
|
204 {
|
yuuji@0
|
205 int fd,upd;
|
yuuji@0
|
206 int ret = -1;
|
yuuji@0
|
207 unsigned long i;
|
yuuji@0
|
208 long j,k;
|
yuuji@0
|
209 off_t pos;
|
yuuji@0
|
210 char c,*s,*t,hdr[HDRSIZE];
|
yuuji@0
|
211 struct stat sbuf;
|
yuuji@0
|
212 time_t tp[2];
|
yuuji@0
|
213 int error = EINVAL; /* assume invalid argument */
|
yuuji@0
|
214 if (ld) *ld = -1; /* initially no lock */
|
yuuji@0
|
215 if ((s = mbx_file (tmp,name)) && !stat (s,&sbuf) &&
|
yuuji@0
|
216 ((fd = open (tmp,(flags ? O_RDWR : O_RDONLY)|O_BINARY,NIL)) >= 0)) {
|
yuuji@0
|
217 error = -1; /* bogus format */
|
yuuji@0
|
218 /* I love cretinous C compilers -- don't you? */
|
yuuji@0
|
219 if (read (fd,hdr,HDRSIZE) == HDRSIZE)
|
yuuji@0
|
220 if ((hdr[0] == '*') && (hdr[1] == 'm') && (hdr[2] == 'b') &&
|
yuuji@0
|
221 (hdr[3] == 'x') && (hdr[4] == '*') && (hdr[5] == '\015') &&
|
yuuji@0
|
222 (hdr[6] == '\012') && isxdigit (hdr[7]) && isxdigit (hdr[8]))
|
yuuji@0
|
223 if (isxdigit (hdr[9]) && isxdigit (hdr[10]) && isxdigit (hdr[11]) &&
|
yuuji@0
|
224 isxdigit (hdr[12]) && isxdigit (hdr[13]) && isxdigit (hdr[14]) &&
|
yuuji@0
|
225 isxdigit (c = hdr[15]) && isxdigit (hdr[16]))
|
yuuji@0
|
226 if (isxdigit (hdr[17]) && isxdigit (hdr[18]) &&
|
yuuji@0
|
227 isxdigit (hdr[19]) && isxdigit (hdr[20]) &&
|
yuuji@0
|
228 isxdigit (hdr[21]) && isxdigit (hdr[22]) &&
|
yuuji@0
|
229 (hdr[23] == '\015') && (hdr[24] == '\012')) {
|
yuuji@0
|
230 ret = fd; /* mbx format */
|
yuuji@0
|
231
|
yuuji@0
|
232 if (stream) { /* lock if making mini-stream */
|
yuuji@0
|
233 if (flock (fd,LOCK_SH) ||
|
yuuji@0
|
234 (flags && ((*ld = lockfd (fd,lock,LOCK_EX)) < 0))) ret = -1;
|
yuuji@0
|
235 /* reread data now that locked */
|
yuuji@0
|
236 else if (lseek (fd,0,L_SET) ||
|
yuuji@0
|
237 (read (fd,hdr,HDRSIZE) != HDRSIZE)) ret = -1;
|
yuuji@0
|
238 else {
|
yuuji@0
|
239 *stream = (MAILSTREAM *) memset (fs_get (sizeof (MAILSTREAM)),
|
yuuji@0
|
240 0,sizeof (MAILSTREAM));
|
yuuji@0
|
241 hdr[15] = '\0'; /* tie off UIDVALIDITY */
|
yuuji@0
|
242 (*stream)->uid_validity = strtoul (hdr+7,NIL,16);
|
yuuji@0
|
243 hdr[15] = c; /* now get UIDLAST */
|
yuuji@0
|
244 (*stream)->uid_last = strtoul (hdr+15,NIL,16);
|
yuuji@0
|
245 /* parse user flags */
|
yuuji@0
|
246 for (i = 0, s = hdr + 25;
|
yuuji@0
|
247 (i < NUSERFLAGS) && (t = strchr (s,'\015')) && (t - s);
|
yuuji@0
|
248 i++, s = t + 2) {
|
yuuji@0
|
249 *t = '\0'; /* tie off flag */
|
yuuji@0
|
250 if (strlen (s) <= MAXUSERFLAG)
|
yuuji@0
|
251 (*stream)->user_flags[i] = cpystr (s);
|
yuuji@0
|
252 }
|
yuuji@0
|
253 /* make sure have true UIDLAST */
|
yuuji@0
|
254 if (flags & MBXISVALIDUID) {
|
yuuji@0
|
255 for (upd = NIL,pos = 2048, k = 0; pos < sbuf.st_size;
|
yuuji@0
|
256 pos += (j + k)) {
|
yuuji@0
|
257 /* read header for this message */
|
yuuji@0
|
258 lseek (fd,pos,L_SET);
|
yuuji@0
|
259 if ((j = read (fd,hdr,64)) >= 0) {
|
yuuji@0
|
260 hdr[j] = '\0';
|
yuuji@0
|
261 if ((s = strchr (hdr,'\015')) && (s[1] == '\012')) {
|
yuuji@0
|
262 *s = '\0';
|
yuuji@0
|
263 k = s + 2 - hdr;
|
yuuji@0
|
264 if ((s = strchr (hdr,',')) &&
|
yuuji@0
|
265 (j = strtol (s+1,&s,10)) && (*s == ';') &&
|
yuuji@0
|
266 (s = strchr (s+1,'-'))) {
|
yuuji@0
|
267 /* get UID if there is any */
|
yuuji@0
|
268 i = strtoul (++s,&t,16);
|
yuuji@0
|
269 if (!*t && (t == (s + 8)) &&
|
yuuji@0
|
270 (i <= (*stream)->uid_last)) {
|
yuuji@0
|
271 if (!i) {
|
yuuji@0
|
272 lseek (fd,pos + s - hdr,L_SET);
|
yuuji@0
|
273 sprintf (hdr,"%08lx",++(*stream)->uid_last);
|
yuuji@0
|
274 write (fd,hdr,8);
|
yuuji@0
|
275 upd = T;
|
yuuji@0
|
276 }
|
yuuji@0
|
277 continue;
|
yuuji@0
|
278 }
|
yuuji@0
|
279 }
|
yuuji@0
|
280 }
|
yuuji@0
|
281 ret = -1; /* error, give up */
|
yuuji@0
|
282 *stream = mail_close (*stream);
|
yuuji@0
|
283 pos = sbuf.st_size + 1;
|
yuuji@0
|
284 j = k = 0;
|
yuuji@0
|
285 }
|
yuuji@0
|
286 }
|
yuuji@0
|
287
|
yuuji@0
|
288 if (upd) { /* need to update hdr with new UIDLAST? */
|
yuuji@0
|
289 lseek (fd,15,L_SET);
|
yuuji@0
|
290 sprintf (hdr,"%08lx",(*stream)->uid_last);
|
yuuji@0
|
291 write (fd,hdr,8);
|
yuuji@0
|
292 }
|
yuuji@0
|
293 }
|
yuuji@0
|
294 }
|
yuuji@0
|
295 }
|
yuuji@0
|
296 }
|
yuuji@0
|
297 if (ret != fd) close (fd); /* close the file */
|
yuuji@0
|
298 else lseek (fd,0,L_SET); /* else rewind to start */
|
yuuji@0
|
299 /* \Marked status? */
|
yuuji@0
|
300 if (sbuf.st_ctime > sbuf.st_atime) {
|
yuuji@0
|
301 tp[0] = sbuf.st_atime; /* preserve atime and mtime */
|
yuuji@0
|
302 tp[1] = sbuf.st_mtime;
|
yuuji@0
|
303 utime (tmp,tp); /* set the times */
|
yuuji@0
|
304 }
|
yuuji@0
|
305 }
|
yuuji@0
|
306 /* in case INBOX but not mbx format */
|
yuuji@0
|
307 else if (((error = errno) == ENOENT) && !compare_cstring (name,"INBOX"))
|
yuuji@0
|
308 error = -1;
|
yuuji@0
|
309 if ((ret < 0) && ld && (*ld >= 0)) {
|
yuuji@0
|
310 unlockfd (*ld,lock);
|
yuuji@0
|
311 *ld = -1;
|
yuuji@0
|
312 }
|
yuuji@0
|
313 errno = error; /* return as last error */
|
yuuji@0
|
314 return ret; /* return what we should */
|
yuuji@0
|
315 }
|
yuuji@0
|
316
|
yuuji@0
|
317 /* MBX manipulate driver parameters
|
yuuji@0
|
318 * Accepts: function code
|
yuuji@0
|
319 * function-dependent value
|
yuuji@0
|
320 * Returns: function-dependent return value
|
yuuji@0
|
321 */
|
yuuji@0
|
322
|
yuuji@0
|
323 void *mbx_parameters (long function,void *value)
|
yuuji@0
|
324 {
|
yuuji@0
|
325 void *ret = NIL;
|
yuuji@0
|
326 switch ((int) function) {
|
yuuji@0
|
327 case GET_INBOXPATH:
|
yuuji@0
|
328 if (value) ret = mbx_file ((char *) value,"INBOX");
|
yuuji@0
|
329 break;
|
yuuji@0
|
330 case SET_ONETIMEEXPUNGEATPING:
|
yuuji@0
|
331 if (value) ((MBXLOCAL *) ((MAILSTREAM *) value)->local)->expok = T;
|
yuuji@0
|
332 case GET_ONETIMEEXPUNGEATPING:
|
yuuji@0
|
333 if (value) ret = (void *)
|
yuuji@0
|
334 (((MBXLOCAL *) ((MAILSTREAM *) value)->local)->expok ? VOIDT : NIL);
|
yuuji@0
|
335 break;
|
yuuji@0
|
336 }
|
yuuji@0
|
337 return ret;
|
yuuji@0
|
338 }
|
yuuji@0
|
339
|
yuuji@0
|
340
|
yuuji@0
|
341 /* MBX mail scan mailboxes
|
yuuji@0
|
342 * Accepts: mail stream
|
yuuji@0
|
343 * reference
|
yuuji@0
|
344 * pattern to search
|
yuuji@0
|
345 * string to scan
|
yuuji@0
|
346 */
|
yuuji@0
|
347
|
yuuji@0
|
348 void mbx_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents)
|
yuuji@0
|
349 {
|
yuuji@0
|
350 if (stream) dummy_scan (NIL,ref,pat,contents);
|
yuuji@0
|
351 }
|
yuuji@0
|
352
|
yuuji@0
|
353
|
yuuji@0
|
354 /* MBX mail list mailboxes
|
yuuji@0
|
355 * Accepts: mail stream
|
yuuji@0
|
356 * reference
|
yuuji@0
|
357 * pattern to search
|
yuuji@0
|
358 */
|
yuuji@0
|
359
|
yuuji@0
|
360 void mbx_list (MAILSTREAM *stream,char *ref,char *pat)
|
yuuji@0
|
361 {
|
yuuji@0
|
362 if (stream) dummy_list (NIL,ref,pat);
|
yuuji@0
|
363 }
|
yuuji@0
|
364
|
yuuji@0
|
365
|
yuuji@0
|
366 /* MBX mail list subscribed mailboxes
|
yuuji@0
|
367 * Accepts: mail stream
|
yuuji@0
|
368 * reference
|
yuuji@0
|
369 * pattern to search
|
yuuji@0
|
370 */
|
yuuji@0
|
371
|
yuuji@0
|
372 void mbx_lsub (MAILSTREAM *stream,char *ref,char *pat)
|
yuuji@0
|
373 {
|
yuuji@0
|
374 if (stream) dummy_lsub (NIL,ref,pat);
|
yuuji@0
|
375 }
|
yuuji@0
|
376
|
yuuji@0
|
377 /* MBX mail create mailbox
|
yuuji@0
|
378 * Accepts: MAIL stream
|
yuuji@0
|
379 * mailbox name to create
|
yuuji@0
|
380 * Returns: T on success, NIL on failure
|
yuuji@0
|
381 */
|
yuuji@0
|
382
|
yuuji@0
|
383 long mbx_create (MAILSTREAM *stream,char *mailbox)
|
yuuji@0
|
384 {
|
yuuji@0
|
385 char *s,*t,mbx[MAILTMPLEN],tmp[HDRSIZE];
|
yuuji@0
|
386 long ret = NIL;
|
yuuji@0
|
387 int i,fd;
|
yuuji@0
|
388 if (!(s = mbx_file (mbx,mailbox))) {
|
yuuji@0
|
389 sprintf (mbx,"Can't create %.80s: invalid name",mailbox);
|
yuuji@0
|
390 MM_LOG (mbx,ERROR);
|
yuuji@0
|
391 }
|
yuuji@0
|
392 /* create underlying file */
|
yuuji@0
|
393 else if (dummy_create_path (stream,s,get_dir_protection (mailbox))) {
|
yuuji@0
|
394 /* done if made directory */
|
yuuji@0
|
395 if ((s = strrchr (s,'/')) && !s[1]) return T;
|
yuuji@0
|
396 if ((fd = open (mbx,O_WRONLY|O_BINARY,NIL)) < 0) {
|
yuuji@0
|
397 sprintf (tmp,"Can't reopen mailbox node %.80s: %s",mbx,strerror (errno));
|
yuuji@0
|
398 MM_LOG (tmp,ERROR);
|
yuuji@0
|
399 unlink (mbx); /* delete the file */
|
yuuji@0
|
400 }
|
yuuji@0
|
401 else {
|
yuuji@0
|
402 memset (tmp,'\0',HDRSIZE);/* initialize header */
|
yuuji@0
|
403 sprintf (s = tmp,"*mbx*\015\012%08lx00000000\015\012",
|
yuuji@0
|
404 (unsigned long) time (0));
|
yuuji@0
|
405 for (i = 0; i < NUSERFLAGS; ++i) {
|
yuuji@0
|
406 t = (stream && stream->user_flags[i]) ? stream->user_flags[i] :
|
yuuji@0
|
407 ((t = default_user_flag (i)) ? t : "");
|
yuuji@0
|
408 sprintf (s += strlen (s),"%s\015\012",t);
|
yuuji@0
|
409 }
|
yuuji@0
|
410 if (write (fd,tmp,HDRSIZE) != HDRSIZE) {
|
yuuji@0
|
411 sprintf (tmp,"Can't initialize mailbox node %.80s: %s",
|
yuuji@0
|
412 mbx,strerror (errno));
|
yuuji@0
|
413 MM_LOG (tmp,ERROR);
|
yuuji@0
|
414 unlink (mbx); /* delete the file */
|
yuuji@0
|
415 }
|
yuuji@0
|
416 else ret = T; /* success */
|
yuuji@0
|
417 close (fd); /* close file */
|
yuuji@0
|
418 }
|
yuuji@0
|
419 }
|
yuuji@0
|
420 /* set proper protections */
|
yuuji@0
|
421 return ret ? set_mbx_protections (mailbox,mbx) : NIL;
|
yuuji@0
|
422 }
|
yuuji@0
|
423
|
yuuji@0
|
424
|
yuuji@0
|
425 /* MBX mail delete mailbox
|
yuuji@0
|
426 * Accepts: MAIL stream
|
yuuji@0
|
427 * mailbox name to delete
|
yuuji@0
|
428 * Returns: T on success, NIL on failure
|
yuuji@0
|
429 */
|
yuuji@0
|
430
|
yuuji@0
|
431 long mbx_delete (MAILSTREAM *stream,char *mailbox)
|
yuuji@0
|
432 {
|
yuuji@0
|
433 return mbx_rename (stream,mailbox,NIL);
|
yuuji@0
|
434 }
|
yuuji@0
|
435
|
yuuji@0
|
436 /* MBX mail rename mailbox
|
yuuji@0
|
437 * Accepts: MAIL stream
|
yuuji@0
|
438 * old mailbox name
|
yuuji@0
|
439 * new mailbox name (or NIL for delete)
|
yuuji@0
|
440 * Returns: T on success, NIL on failure
|
yuuji@0
|
441 */
|
yuuji@0
|
442
|
yuuji@0
|
443 long mbx_rename (MAILSTREAM *stream,char *old,char *newname)
|
yuuji@0
|
444 {
|
yuuji@0
|
445 long ret = LONGT;
|
yuuji@0
|
446 char c,*s,tmp[MAILTMPLEN],file[MAILTMPLEN],lock[MAILTMPLEN];
|
yuuji@0
|
447 int fd,ld;
|
yuuji@0
|
448 struct stat sbuf;
|
yuuji@0
|
449 if (!mbx_file (file,old) ||
|
yuuji@0
|
450 (newname && (!((s = mailboxfile (tmp,newname)) && *s) ||
|
yuuji@0
|
451 ((s = strrchr (tmp,'/')) && !s[1])))) {
|
yuuji@0
|
452 sprintf (tmp,newname ?
|
yuuji@0
|
453 "Can't rename mailbox %.80s to %.80s: invalid name" :
|
yuuji@0
|
454 "Can't delete mailbox %.80s: invalid name",
|
yuuji@0
|
455 old,newname);
|
yuuji@0
|
456 MM_LOG (tmp,ERROR);
|
yuuji@0
|
457 return NIL;
|
yuuji@0
|
458 }
|
yuuji@0
|
459 else if ((fd = open (file,O_RDWR|O_BINARY,NIL)) < 0) {
|
yuuji@0
|
460 sprintf (tmp,"Can't open mailbox %.80s: %s",old,strerror (errno));
|
yuuji@0
|
461 MM_LOG (tmp,ERROR);
|
yuuji@0
|
462 return NIL;
|
yuuji@0
|
463 }
|
yuuji@0
|
464 /* get parse/append permission */
|
yuuji@0
|
465 if ((ld = lockfd (fd,lock,LOCK_EX)) < 0) {
|
yuuji@0
|
466 MM_LOG ("Unable to lock rename mailbox",ERROR);
|
yuuji@0
|
467 return NIL;
|
yuuji@0
|
468 }
|
yuuji@0
|
469 /* lock out other users */
|
yuuji@0
|
470 if (flock (fd,LOCK_EX|LOCK_NB)) {
|
yuuji@0
|
471 close (fd); /* couldn't lock, give up on it then */
|
yuuji@0
|
472 sprintf (tmp,"Mailbox %.80s is in use by another process",old);
|
yuuji@0
|
473 MM_LOG (tmp,ERROR);
|
yuuji@0
|
474 unlockfd (ld,lock); /* release exclusive parse/append permission */
|
yuuji@0
|
475 return NIL;
|
yuuji@0
|
476 }
|
yuuji@0
|
477
|
yuuji@0
|
478 if (newname) { /* want rename? */
|
yuuji@0
|
479 /* found superior to destination name? */
|
yuuji@0
|
480 if (s = strrchr (tmp,'/')) {
|
yuuji@0
|
481 c = *++s; /* remember first character of inferior */
|
yuuji@0
|
482 *s = '\0'; /* tie off to get just superior */
|
yuuji@0
|
483 /* superior name doesn't exist, create it */
|
yuuji@0
|
484 if ((stat (tmp,&sbuf) || ((sbuf.st_mode & S_IFMT) != S_IFDIR)) &&
|
yuuji@0
|
485 !dummy_create_path (stream,tmp,get_dir_protection (newname)))
|
yuuji@0
|
486 ret = NIL;
|
yuuji@0
|
487 else *s = c; /* restore full name */
|
yuuji@0
|
488 }
|
yuuji@0
|
489 /* rename the file */
|
yuuji@0
|
490 if (ret && rename (file,tmp)) {
|
yuuji@0
|
491 sprintf (tmp,"Can't rename mailbox %.80s to %.80s: %s",old,newname,
|
yuuji@0
|
492 strerror (errno));
|
yuuji@0
|
493 MM_LOG (tmp,ERROR);
|
yuuji@0
|
494 ret = NIL; /* set failure */
|
yuuji@0
|
495 }
|
yuuji@0
|
496 }
|
yuuji@0
|
497 else if (unlink (file)) {
|
yuuji@0
|
498 sprintf (tmp,"Can't delete mailbox %.80s: %s",old,strerror (errno));
|
yuuji@0
|
499 MM_LOG (tmp,ERROR);
|
yuuji@0
|
500 ret = NIL; /* set failure */
|
yuuji@0
|
501 }
|
yuuji@0
|
502 flock (fd,LOCK_UN); /* release lock on the file */
|
yuuji@0
|
503 unlockfd (ld,lock); /* release exclusive parse/append permission */
|
yuuji@0
|
504 close (fd); /* close the file */
|
yuuji@0
|
505 /* recreate file if renamed INBOX */
|
yuuji@0
|
506 if (ret && !compare_cstring (old,"INBOX")) mbx_create (NIL,"INBOX");
|
yuuji@0
|
507 return ret; /* return success */
|
yuuji@0
|
508 }
|
yuuji@0
|
509
|
yuuji@0
|
510 /* MBX Mail status
|
yuuji@0
|
511 * Accepts: mail stream
|
yuuji@0
|
512 * mailbox name
|
yuuji@0
|
513 * status flags
|
yuuji@0
|
514 * Returns: T on success, NIL on failure
|
yuuji@0
|
515 */
|
yuuji@0
|
516
|
yuuji@0
|
517 long mbx_status (MAILSTREAM *stream,char *mbx,long flags)
|
yuuji@0
|
518 {
|
yuuji@0
|
519 MAILSTATUS status;
|
yuuji@0
|
520 unsigned long i;
|
yuuji@0
|
521 MAILSTREAM *tstream = NIL;
|
yuuji@0
|
522 MAILSTREAM *systream = NIL;
|
yuuji@0
|
523 /* make temporary stream (unless this mbx) */
|
yuuji@0
|
524 if (!stream && !(stream = tstream =
|
yuuji@0
|
525 mail_open (NIL,mbx,OP_READONLY|OP_SILENT)))
|
yuuji@0
|
526 return NIL;
|
yuuji@0
|
527 status.flags = flags; /* return status values */
|
yuuji@0
|
528 status.messages = stream->nmsgs;
|
yuuji@0
|
529 status.recent = stream->recent;
|
yuuji@0
|
530 if (flags & SA_UNSEEN) /* must search to get unseen messages */
|
yuuji@0
|
531 for (i = 1,status.unseen = 0; i <= stream->nmsgs; i++)
|
yuuji@0
|
532 if (!mail_elt (stream,i)->seen) status.unseen++;
|
yuuji@0
|
533 status.uidnext = stream->uid_last + 1;
|
yuuji@0
|
534 status.uidvalidity = stream->uid_validity;
|
yuuji@0
|
535 /* calculate post-snarf results */
|
yuuji@0
|
536 if (!status.recent && stream->inbox &&
|
yuuji@0
|
537 (systream = mail_open (NIL,sysinbox (),OP_READONLY|OP_SILENT))) {
|
yuuji@0
|
538 status.messages += systream->nmsgs;
|
yuuji@0
|
539 status.recent += systream->recent;
|
yuuji@0
|
540 if (flags & SA_UNSEEN) /* must search to get unseen messages */
|
yuuji@0
|
541 for (i = 1; i <= systream->nmsgs; i++)
|
yuuji@0
|
542 if (!mail_elt (systream,i)->seen) status.unseen++;
|
yuuji@0
|
543 /* kludge but probably good enough */
|
yuuji@0
|
544 status.uidnext += systream->nmsgs;
|
yuuji@0
|
545 }
|
yuuji@0
|
546 MM_STATUS(stream,mbx,&status);/* pass status to main program */
|
yuuji@0
|
547 if (tstream) mail_close (tstream);
|
yuuji@0
|
548 if (systream) mail_close (systream);
|
yuuji@0
|
549 return T; /* success */
|
yuuji@0
|
550 }
|
yuuji@0
|
551
|
yuuji@0
|
552 /* MBX mail open
|
yuuji@0
|
553 * Accepts: stream to open
|
yuuji@0
|
554 * Returns: stream on success, NIL on failure
|
yuuji@0
|
555 */
|
yuuji@0
|
556
|
yuuji@0
|
557 MAILSTREAM *mbx_open (MAILSTREAM *stream)
|
yuuji@0
|
558 {
|
yuuji@0
|
559 int fd,ld;
|
yuuji@0
|
560 short silent;
|
yuuji@0
|
561 char tmp[MAILTMPLEN];
|
yuuji@0
|
562 blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL);
|
yuuji@0
|
563 /* return prototype for OP_PROTOTYPE call */
|
yuuji@0
|
564 if (!stream) return user_flags (&mbxproto);
|
yuuji@0
|
565 if (stream->local) fatal ("mbx recycle stream");
|
yuuji@0
|
566 /* canonicalize the mailbox name */
|
yuuji@0
|
567 if (!mbx_file (tmp,stream->mailbox)) {
|
yuuji@0
|
568 sprintf (tmp,"Can't open - invalid name: %.80s",stream->mailbox);
|
yuuji@0
|
569 MM_LOG (tmp,ERROR);
|
yuuji@0
|
570 }
|
yuuji@0
|
571 if (stream->rdonly ||
|
yuuji@0
|
572 (fd = open (tmp,O_RDWR|O_BINARY,NIL)) < 0) {
|
yuuji@0
|
573 if ((fd = open (tmp,O_RDONLY|O_BINARY,NIL)) < 0) {
|
yuuji@0
|
574 sprintf (tmp,"Can't open mailbox: %s",strerror (errno));
|
yuuji@0
|
575 MM_LOG (tmp,ERROR);
|
yuuji@0
|
576 return NIL;
|
yuuji@0
|
577 }
|
yuuji@0
|
578 else if (!stream->rdonly) { /* got it, but readonly */
|
yuuji@0
|
579 MM_LOG ("Can't get write access to mailbox, access is readonly",WARN);
|
yuuji@0
|
580 stream->rdonly = T;
|
yuuji@0
|
581 }
|
yuuji@0
|
582 }
|
yuuji@0
|
583
|
yuuji@0
|
584 stream->local = memset (fs_get (sizeof (MBXLOCAL)),NIL,sizeof (MBXLOCAL));
|
yuuji@0
|
585 LOCAL->fd = fd; /* bind the file */
|
yuuji@0
|
586 LOCAL->ld = -1; /* no flaglock */
|
yuuji@0
|
587 LOCAL->buf = (char *) fs_get (CHUNKSIZE);
|
yuuji@0
|
588 LOCAL->buflen = CHUNKSIZE - 1;
|
yuuji@0
|
589 /* note if an INBOX or not */
|
yuuji@0
|
590 stream->inbox = !compare_cstring (stream->mailbox,"INBOX");
|
yuuji@0
|
591 fs_give ((void **) &stream->mailbox);
|
yuuji@0
|
592 stream->mailbox = cpystr (tmp);
|
yuuji@0
|
593 /* get parse/append permission */
|
yuuji@0
|
594 if ((ld = lockfd (LOCAL->fd,tmp,LOCK_EX)) < 0) {
|
yuuji@0
|
595 MM_LOG ("Unable to lock open mailbox",ERROR);
|
yuuji@0
|
596 return NIL;
|
yuuji@0
|
597 }
|
yuuji@0
|
598 (*bn) (BLOCK_FILELOCK,NIL);
|
yuuji@0
|
599 flock (LOCAL->fd,LOCK_SH); /* lock the file */
|
yuuji@0
|
600 (*bn) (BLOCK_NONE,NIL);
|
yuuji@0
|
601 unlockfd (ld,tmp); /* release shared parse permission */
|
yuuji@0
|
602 LOCAL->filesize = HDRSIZE; /* initialize parsed file size */
|
yuuji@0
|
603 /* time not set up yet */
|
yuuji@0
|
604 LOCAL->lastsnarf = LOCAL->filetime = 0;
|
yuuji@0
|
605 LOCAL->expok = LOCAL->flagcheck = NIL;
|
yuuji@0
|
606 stream->sequence++; /* bump sequence number */
|
yuuji@0
|
607 /* parse mailbox */
|
yuuji@0
|
608 stream->nmsgs = stream->recent = 0;
|
yuuji@0
|
609 silent = stream->silent; /* defer events */
|
yuuji@0
|
610 stream->silent = T;
|
yuuji@0
|
611 if (mbx_ping (stream) && !stream->nmsgs)
|
yuuji@0
|
612 MM_LOG ("Mailbox is empty",(long) NIL);
|
yuuji@0
|
613 stream->silent = silent; /* now notify upper level */
|
yuuji@0
|
614 mail_exists (stream,stream->nmsgs);
|
yuuji@0
|
615 mail_recent (stream,stream->recent);
|
yuuji@0
|
616 if (!LOCAL) return NIL; /* failure if stream died */
|
yuuji@0
|
617 stream->perm_seen = stream->perm_deleted = stream->perm_flagged =
|
yuuji@0
|
618 stream->perm_answered = stream->perm_draft = stream->rdonly ? NIL : T;
|
yuuji@0
|
619 stream->perm_user_flags = stream->rdonly ? NIL : 0xffffffff;
|
yuuji@0
|
620 stream->kwd_create = (stream->user_flags[NUSERFLAGS-1] || stream->rdonly) ?
|
yuuji@0
|
621 NIL : T; /* can we create new user flags? */
|
yuuji@0
|
622 return stream; /* return stream to caller */
|
yuuji@0
|
623 }
|
yuuji@0
|
624
|
yuuji@0
|
625 /* MBX mail close
|
yuuji@0
|
626 * Accepts: MAIL stream
|
yuuji@0
|
627 * close options
|
yuuji@0
|
628 */
|
yuuji@0
|
629
|
yuuji@0
|
630 void mbx_close (MAILSTREAM *stream,long options)
|
yuuji@0
|
631 {
|
yuuji@0
|
632 if (stream && LOCAL) { /* only if a file is open */
|
yuuji@0
|
633 int silent = stream->silent;
|
yuuji@0
|
634 stream->silent = T; /* note this stream is dying */
|
yuuji@0
|
635 /* do an expunge if requested */
|
yuuji@0
|
636 if (options & CL_EXPUNGE) mbx_expunge (stream,NIL,NIL);
|
yuuji@0
|
637 else { /* otherwise do a checkpoint to purge */
|
yuuji@0
|
638 LOCAL->expok = T; /* possible expunged messages */
|
yuuji@0
|
639 mbx_ping (stream);
|
yuuji@0
|
640 }
|
yuuji@0
|
641 stream->silent = silent; /* restore previous status */
|
yuuji@0
|
642 mbx_abort (stream);
|
yuuji@0
|
643 }
|
yuuji@0
|
644 }
|
yuuji@0
|
645
|
yuuji@0
|
646
|
yuuji@0
|
647 /* MBX mail abort stream
|
yuuji@0
|
648 * Accepts: MAIL stream
|
yuuji@0
|
649 */
|
yuuji@0
|
650
|
yuuji@0
|
651 void mbx_abort (MAILSTREAM *stream)
|
yuuji@0
|
652 {
|
yuuji@0
|
653 if (stream && LOCAL) { /* only if a file is open */
|
yuuji@0
|
654 flock (LOCAL->fd,LOCK_UN); /* unlock local file */
|
yuuji@0
|
655 close (LOCAL->fd); /* close the local file */
|
yuuji@0
|
656 /* free local text buffer */
|
yuuji@0
|
657 if (LOCAL->buf) fs_give ((void **) &LOCAL->buf);
|
yuuji@0
|
658 /* nuke the local data */
|
yuuji@0
|
659 fs_give ((void **) &stream->local);
|
yuuji@0
|
660 stream->dtb = NIL; /* log out the DTB */
|
yuuji@0
|
661 }
|
yuuji@0
|
662 }
|
yuuji@0
|
663
|
yuuji@0
|
664
|
yuuji@0
|
665 /* MBX mail fetch flags
|
yuuji@0
|
666 * Accepts: MAIL stream
|
yuuji@0
|
667 * sequence
|
yuuji@0
|
668 * option flags
|
yuuji@0
|
669 * Sniffs at file to see if some other process changed the flags
|
yuuji@0
|
670 */
|
yuuji@0
|
671
|
yuuji@0
|
672 void mbx_flags (MAILSTREAM *stream,char *sequence,long flags)
|
yuuji@0
|
673 {
|
yuuji@0
|
674 MESSAGECACHE *elt;
|
yuuji@0
|
675 unsigned long i;
|
yuuji@0
|
676 if (mbx_ping (stream) && /* ping mailbox, get new status for messages */
|
yuuji@0
|
677 ((flags & FT_UID) ? mail_uid_sequence (stream,sequence) :
|
yuuji@0
|
678 mail_sequence (stream,sequence)))
|
yuuji@0
|
679 for (i = 1; i <= stream->nmsgs; i++)
|
yuuji@0
|
680 if ((elt = mail_elt (stream,i))->sequence && !elt->valid)
|
yuuji@0
|
681 mbx_elt (stream,i,NIL);
|
yuuji@0
|
682 }
|
yuuji@0
|
683
|
yuuji@0
|
684 /* MBX mail fetch message header
|
yuuji@0
|
685 * Accepts: MAIL stream
|
yuuji@0
|
686 * message # to fetch
|
yuuji@0
|
687 * pointer to returned header text length
|
yuuji@0
|
688 * option flags
|
yuuji@0
|
689 * Returns: message header in RFC822 format
|
yuuji@0
|
690 */
|
yuuji@0
|
691
|
yuuji@0
|
692 char *mbx_header (MAILSTREAM *stream,unsigned long msgno,unsigned long *length,
|
yuuji@0
|
693 long flags)
|
yuuji@0
|
694 {
|
yuuji@0
|
695 unsigned long i;
|
yuuji@0
|
696 char *s;
|
yuuji@0
|
697 *length = 0; /* default to empty */
|
yuuji@0
|
698 if (flags & FT_UID) return "";/* UID call "impossible" */
|
yuuji@0
|
699 /* get header position, possibly header */
|
yuuji@0
|
700 i = mbx_hdrpos (stream,msgno,length,&s);
|
yuuji@0
|
701 if (!s) { /* mbx_hdrpos() returned header? */
|
yuuji@0
|
702 lseek (LOCAL->fd,i,L_SET); /* no, get to header position */
|
yuuji@0
|
703 /* is buffer big enough? */
|
yuuji@0
|
704 if (*length > LOCAL->buflen) {
|
yuuji@0
|
705 fs_give ((void **) &LOCAL->buf);
|
yuuji@0
|
706 LOCAL->buf = (char *) fs_get ((LOCAL->buflen = *length) + 1);
|
yuuji@0
|
707 }
|
yuuji@0
|
708 /* slurp the data */
|
yuuji@0
|
709 read (LOCAL->fd,s = LOCAL->buf,*length);
|
yuuji@0
|
710 }
|
yuuji@0
|
711 s[*length] = '\0'; /* tie off string */
|
yuuji@0
|
712 return s;
|
yuuji@0
|
713 }
|
yuuji@0
|
714
|
yuuji@0
|
715 /* MBX mail fetch message text (body only)
|
yuuji@0
|
716 * Accepts: MAIL stream
|
yuuji@0
|
717 * message # to fetch
|
yuuji@0
|
718 * pointer to returned header text length
|
yuuji@0
|
719 * option flags
|
yuuji@0
|
720 * Returns: T on success, NIL on failure
|
yuuji@0
|
721 */
|
yuuji@0
|
722
|
yuuji@0
|
723 long mbx_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags)
|
yuuji@0
|
724 {
|
yuuji@0
|
725 FDDATA d;
|
yuuji@0
|
726 unsigned long i,j;
|
yuuji@0
|
727 MESSAGECACHE *elt;
|
yuuji@0
|
728 /* UID call "impossible" */
|
yuuji@0
|
729 if (flags & FT_UID) return NIL;
|
yuuji@0
|
730 /* get message status */
|
yuuji@0
|
731 elt = mbx_elt (stream,msgno,NIL);
|
yuuji@0
|
732 /* if message not seen */
|
yuuji@0
|
733 if (!(flags & FT_PEEK) && !elt->seen && mbx_flaglock (stream)) {
|
yuuji@0
|
734 elt->seen = T; /* mark message as seen */
|
yuuji@0
|
735 /* recalculate status */
|
yuuji@0
|
736 mbx_update_status (stream,msgno,NIL);
|
yuuji@0
|
737 MM_FLAGS (stream,msgno);
|
yuuji@0
|
738 /* update flags */
|
yuuji@0
|
739 mbx_flag (stream,NIL,NIL,NIL);
|
yuuji@0
|
740 }
|
yuuji@0
|
741 if (!LOCAL) return NIL; /* mbx_flaglock() could have aborted */
|
yuuji@0
|
742 /* find header position */
|
yuuji@0
|
743 i = mbx_hdrpos (stream,msgno,&j,NIL);
|
yuuji@0
|
744 d.fd = LOCAL->fd; /* set up file descriptor */
|
yuuji@0
|
745 d.pos = i + j;
|
yuuji@0
|
746 d.chunk = LOCAL->buf; /* initial buffer chunk */
|
yuuji@0
|
747 d.chunksize = CHUNKSIZE;
|
yuuji@0
|
748 INIT (bs,fd_string,&d,elt->rfc822_size - j);
|
yuuji@0
|
749 return LONGT; /* success */
|
yuuji@0
|
750 }
|
yuuji@0
|
751
|
yuuji@0
|
752 /* MBX mail modify flags
|
yuuji@0
|
753 * Accepts: MAIL stream
|
yuuji@0
|
754 * sequence
|
yuuji@0
|
755 * flag(s)
|
yuuji@0
|
756 * option flags
|
yuuji@0
|
757 * Unlocks flag lock
|
yuuji@0
|
758 */
|
yuuji@0
|
759
|
yuuji@0
|
760 void mbx_flag (MAILSTREAM *stream,char *sequence,char *flag,long flags)
|
yuuji@0
|
761 {
|
yuuji@0
|
762 time_t tp[2];
|
yuuji@0
|
763 struct stat sbuf;
|
yuuji@0
|
764 unsigned long oldpid = LOCAL->lastpid;
|
yuuji@0
|
765 /* make sure the update takes */
|
yuuji@0
|
766 if (!stream->rdonly && LOCAL && (LOCAL->fd >= 0) && (LOCAL->ld >= 0)) {
|
yuuji@0
|
767 fsync (LOCAL->fd);
|
yuuji@0
|
768 fstat (LOCAL->fd,&sbuf); /* get current write time */
|
yuuji@0
|
769 tp[1] = LOCAL->filetime = sbuf.st_mtime;
|
yuuji@0
|
770 /* we are the last flag updater */
|
yuuji@0
|
771 LOCAL->lastpid = (unsigned long) getpid ();
|
yuuji@0
|
772 /* update header if needed */
|
yuuji@0
|
773 if (((LOCAL->ffuserflag < NUSERFLAGS) &&
|
yuuji@0
|
774 stream->user_flags[LOCAL->ffuserflag]) || (oldpid != LOCAL->lastpid))
|
yuuji@0
|
775 mbx_update_header (stream);
|
yuuji@0
|
776 tp[0] = time (0); /* make sure read comes after all that */
|
yuuji@0
|
777 utime (stream->mailbox,tp);
|
yuuji@0
|
778 }
|
yuuji@0
|
779 if (LOCAL->ld >= 0) { /* unlock now */
|
yuuji@0
|
780 unlockfd (LOCAL->ld,LOCAL->lock);
|
yuuji@0
|
781 LOCAL->ld = -1;
|
yuuji@0
|
782 }
|
yuuji@0
|
783 }
|
yuuji@0
|
784
|
yuuji@0
|
785
|
yuuji@0
|
786 /* MBX mail per-message modify flags
|
yuuji@0
|
787 * Accepts: MAIL stream
|
yuuji@0
|
788 * message cache element
|
yuuji@0
|
789 */
|
yuuji@0
|
790
|
yuuji@0
|
791 void mbx_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt)
|
yuuji@0
|
792 {
|
yuuji@0
|
793 if (mbx_flaglock (stream)) mbx_update_status (stream,elt->msgno,NIL);
|
yuuji@0
|
794 }
|
yuuji@0
|
795
|
yuuji@0
|
796 /* MBX mail ping mailbox
|
yuuji@0
|
797 * Accepts: MAIL stream
|
yuuji@0
|
798 * Returns: T if stream still alive, NIL if not
|
yuuji@0
|
799 */
|
yuuji@0
|
800
|
yuuji@0
|
801 long mbx_ping (MAILSTREAM *stream)
|
yuuji@0
|
802 {
|
yuuji@0
|
803 unsigned long i,pos;
|
yuuji@0
|
804 long ret = NIL;
|
yuuji@0
|
805 int ld;
|
yuuji@0
|
806 char lock[MAILTMPLEN];
|
yuuji@0
|
807 MESSAGECACHE *elt;
|
yuuji@0
|
808 struct stat sbuf;
|
yuuji@0
|
809 if (stream && LOCAL) { /* only if stream already open */
|
yuuji@0
|
810 int snarf = stream->inbox && !stream->rdonly;
|
yuuji@0
|
811 ret = LONGT; /* assume OK */
|
yuuji@0
|
812 fstat (LOCAL->fd,&sbuf); /* get current file poop */
|
yuuji@0
|
813 /* allow expunge if permitted at ping */
|
yuuji@0
|
814 if (mail_parameters (NIL,GET_EXPUNGEATPING,NIL)) LOCAL->expok = T;
|
yuuji@0
|
815 /* if external modification */
|
yuuji@0
|
816 if (LOCAL->filetime && (LOCAL->filetime < sbuf.st_mtime))
|
yuuji@0
|
817 LOCAL->flagcheck = T; /* upgrade to flag checking */
|
yuuji@0
|
818 /* new mail or flagcheck handling needed? */
|
yuuji@0
|
819 if (((sbuf.st_size - LOCAL->filesize) || LOCAL->flagcheck ||
|
yuuji@0
|
820 !stream->nmsgs || snarf) &&
|
yuuji@0
|
821 ((ld = lockfd (LOCAL->fd,lock,LOCK_EX)) >= 0)) {
|
yuuji@0
|
822 /* reparse header if not flagchecking */
|
yuuji@0
|
823 if (!LOCAL->flagcheck) ret = mbx_parse (stream);
|
yuuji@0
|
824 /* sweep mailbox for changed message status */
|
yuuji@0
|
825 else if (ret = mbx_parse (stream)) {
|
yuuji@0
|
826 unsigned long recent = 0;
|
yuuji@0
|
827 LOCAL->filetime = sbuf.st_mtime;
|
yuuji@0
|
828 for (i = 1; i <= stream->nmsgs; )
|
yuuji@0
|
829 if (elt = mbx_elt (stream,i,LOCAL->expok)) {
|
yuuji@0
|
830 if (elt->recent) ++recent;
|
yuuji@0
|
831 ++i;
|
yuuji@0
|
832 }
|
yuuji@0
|
833 mail_recent (stream,recent);
|
yuuji@0
|
834 LOCAL->flagcheck = NIL; /* got all the updates */
|
yuuji@0
|
835 }
|
yuuji@0
|
836 /* always reparse header at least */
|
yuuji@0
|
837 if (ret && snarf) { /* snarf new messages if still OK */
|
yuuji@0
|
838 mbx_snarf (stream);
|
yuuji@0
|
839 /* parse snarfed messages */
|
yuuji@0
|
840 ret = mbx_parse (stream);
|
yuuji@0
|
841 }
|
yuuji@0
|
842 unlockfd (ld,lock); /* release shared parse/append permission */
|
yuuji@0
|
843 }
|
yuuji@0
|
844 if (ret) { /* must still be alive */
|
yuuji@0
|
845 if (!LOCAL->expunged) /* look for holes if none known yet */
|
yuuji@0
|
846 for (i = 1, pos = HDRSIZE;
|
yuuji@0
|
847 !LOCAL->expunged && (i <= stream->nmsgs);
|
yuuji@0
|
848 i++, pos += elt->private.special.text.size + elt->rfc822_size)
|
yuuji@0
|
849 if ((elt = mail_elt (stream,i))->private.special.offset != pos)
|
yuuji@0
|
850 LOCAL->expunged = T;/* found a hole */
|
yuuji@0
|
851 /* burp any holes */
|
yuuji@0
|
852 if (LOCAL->expunged && !stream->rdonly) {
|
yuuji@0
|
853 if (mbx_rewrite (stream,&i,NIL)) fatal ("expunge on check");
|
yuuji@0
|
854 if (i) { /* any space reclaimed? */
|
yuuji@0
|
855 LOCAL->expunged = NIL;/* no more pending expunge */
|
yuuji@0
|
856 sprintf (LOCAL->buf,"Reclaimed %lu bytes of expunged space",i);
|
yuuji@0
|
857 MM_LOG (LOCAL->buf,(long) NIL);
|
yuuji@0
|
858 }
|
yuuji@0
|
859 }
|
yuuji@0
|
860 LOCAL->expok = NIL; /* no more expok */
|
yuuji@0
|
861 }
|
yuuji@0
|
862 }
|
yuuji@0
|
863 return ret; /* return result of the parse */
|
yuuji@0
|
864 }
|
yuuji@0
|
865
|
yuuji@0
|
866 /* MBX mail check mailbox (reparses status too)
|
yuuji@0
|
867 * Accepts: MAIL stream
|
yuuji@0
|
868 */
|
yuuji@0
|
869
|
yuuji@0
|
870 void mbx_check (MAILSTREAM *stream)
|
yuuji@0
|
871 {
|
yuuji@0
|
872 if (LOCAL) LOCAL->expok = T; /* mark that a check is desired */
|
yuuji@0
|
873 if (mbx_ping (stream)) MM_LOG ("Check completed",(long) NIL);
|
yuuji@0
|
874 }
|
yuuji@0
|
875
|
yuuji@0
|
876
|
yuuji@0
|
877 /* MBX mail expunge mailbox
|
yuuji@0
|
878 * Accepts: MAIL stream
|
yuuji@0
|
879 * sequence to expunge if non-NIL
|
yuuji@0
|
880 * expunge options
|
yuuji@0
|
881 * Returns: T if success, NIL if failure
|
yuuji@0
|
882 */
|
yuuji@0
|
883
|
yuuji@0
|
884 long mbx_expunge (MAILSTREAM *stream,char *sequence,long options)
|
yuuji@0
|
885 {
|
yuuji@0
|
886 long ret;
|
yuuji@0
|
887 unsigned long nexp,reclaimed;
|
yuuji@0
|
888 if (ret = sequence ? ((options & EX_UID) ?
|
yuuji@0
|
889 mail_uid_sequence (stream,sequence) :
|
yuuji@0
|
890 mail_sequence (stream,sequence)) : LONGT) {
|
yuuji@0
|
891 if (!mbx_ping (stream)); /* do nothing if stream dead */
|
yuuji@0
|
892 else if (stream->rdonly) /* won't do on readonly files! */
|
yuuji@0
|
893 MM_LOG ("Expunge ignored on readonly mailbox",WARN);
|
yuuji@0
|
894 /* if expunged any messages */
|
yuuji@0
|
895 else if (nexp = mbx_rewrite (stream,&reclaimed,sequence ? -1 : 1)) {
|
yuuji@0
|
896 sprintf (LOCAL->buf,"Expunged %lu messages",nexp);
|
yuuji@0
|
897 MM_LOG (LOCAL->buf,(long) NIL);
|
yuuji@0
|
898 }
|
yuuji@0
|
899 else if (reclaimed) { /* or if any prior expunged space reclaimed */
|
yuuji@0
|
900 sprintf (LOCAL->buf,"Reclaimed %lu bytes of expunged space",reclaimed);
|
yuuji@0
|
901 MM_LOG (LOCAL->buf,(long) NIL);
|
yuuji@0
|
902 }
|
yuuji@0
|
903 else MM_LOG ("No messages deleted, so no update needed",(long) NIL);
|
yuuji@0
|
904 }
|
yuuji@0
|
905 return ret;
|
yuuji@0
|
906 }
|
yuuji@0
|
907
|
yuuji@0
|
908 /* MBX mail snarf messages from system inbox
|
yuuji@0
|
909 * Accepts: MAIL stream, already locked
|
yuuji@0
|
910 */
|
yuuji@0
|
911
|
yuuji@0
|
912 void mbx_snarf (MAILSTREAM *stream)
|
yuuji@0
|
913 {
|
yuuji@0
|
914 unsigned long i = 0;
|
yuuji@0
|
915 unsigned long j,r,hdrlen,txtlen;
|
yuuji@0
|
916 struct stat sbuf;
|
yuuji@0
|
917 char *hdr,*txt,tmp[MAILTMPLEN];
|
yuuji@0
|
918 MESSAGECACHE *elt;
|
yuuji@0
|
919 MAILSTREAM *sysibx = NIL;
|
yuuji@0
|
920 /* give up if can't get exclusive permission */
|
yuuji@0
|
921 if ((time (0) >= (LOCAL->lastsnarf +
|
yuuji@0
|
922 (long) mail_parameters (NIL,GET_SNARFINTERVAL,NIL))) &&
|
yuuji@0
|
923 strcmp (sysinbox (),stream->mailbox)) {
|
yuuji@0
|
924 MM_CRITICAL (stream); /* go critical */
|
yuuji@0
|
925 /* sizes match and anything in sysinbox? */
|
yuuji@0
|
926 if (!stat (sysinbox (),&sbuf) && sbuf.st_size &&
|
yuuji@0
|
927 !fstat (LOCAL->fd,&sbuf) && (sbuf.st_size == LOCAL->filesize) &&
|
yuuji@0
|
928 (sysibx = mail_open (sysibx,sysinbox (),OP_SILENT)) &&
|
yuuji@0
|
929 (!sysibx->rdonly) && (r = sysibx->nmsgs)) {
|
yuuji@0
|
930 /* yes, go to end of file in our mailbox */
|
yuuji@0
|
931 lseek (LOCAL->fd,sbuf.st_size,L_SET);
|
yuuji@0
|
932 /* for each message in sysibx mailbox */
|
yuuji@0
|
933 while (r && (++i <= sysibx->nmsgs)) {
|
yuuji@0
|
934 /* snarf message from system INBOX */
|
yuuji@0
|
935 hdr = cpystr (mail_fetchheader_full (sysibx,i,NIL,&hdrlen,NIL));
|
yuuji@0
|
936 txt = mail_fetchtext_full (sysibx,i,&txtlen,FT_PEEK);
|
yuuji@0
|
937 /* if have a message */
|
yuuji@0
|
938 if (j = hdrlen + txtlen) {
|
yuuji@0
|
939 /* build header line */
|
yuuji@0
|
940 mail_date (LOCAL->buf,elt = mail_elt (sysibx,i));
|
yuuji@0
|
941 sprintf (LOCAL->buf + strlen (LOCAL->buf),
|
yuuji@0
|
942 ",%lu;00000000%04x-00000000\015\012",j,(unsigned)
|
yuuji@0
|
943 ((fSEEN * elt->seen) +
|
yuuji@0
|
944 (fDELETED * elt->deleted) + (fFLAGGED * elt->flagged) +
|
yuuji@0
|
945 (fANSWERED * elt->answered) + (fDRAFT * elt->draft)));
|
yuuji@0
|
946 /* copy message */
|
yuuji@0
|
947 if ((write (LOCAL->fd,LOCAL->buf,strlen (LOCAL->buf)) < 0) ||
|
yuuji@0
|
948 (write (LOCAL->fd,hdr,hdrlen) < 0) ||
|
yuuji@0
|
949 (write (LOCAL->fd,txt,txtlen) < 0)) r = 0;
|
yuuji@0
|
950 }
|
yuuji@0
|
951 fs_give ((void **) &hdr);
|
yuuji@0
|
952 }
|
yuuji@0
|
953
|
yuuji@0
|
954 /* make sure all the updates take */
|
yuuji@0
|
955 if (fsync (LOCAL->fd)) r = 0;
|
yuuji@0
|
956 if (r) { /* delete all the messages we copied */
|
yuuji@0
|
957 if (r == 1) strcpy (tmp,"1");
|
yuuji@0
|
958 else sprintf (tmp,"1:%lu",r);
|
yuuji@0
|
959 mail_setflag (sysibx,tmp,"\\Deleted");
|
yuuji@0
|
960 mail_expunge (sysibx); /* now expunge all those messages */
|
yuuji@0
|
961 }
|
yuuji@0
|
962 else {
|
yuuji@0
|
963 sprintf (LOCAL->buf,"Can't copy new mail: %s",strerror (errno));
|
yuuji@0
|
964 MM_LOG (LOCAL->buf,WARN);
|
yuuji@0
|
965 ftruncate (LOCAL->fd,sbuf.st_size);
|
yuuji@0
|
966 }
|
yuuji@0
|
967 fstat (LOCAL->fd,&sbuf); /* yes, get current file size */
|
yuuji@0
|
968 LOCAL->filetime = sbuf.st_mtime;
|
yuuji@0
|
969 }
|
yuuji@0
|
970 if (sysibx) mail_close (sysibx);
|
yuuji@0
|
971 MM_NOCRITICAL (stream); /* release critical */
|
yuuji@0
|
972 LOCAL->lastsnarf = time (0);/* note time of last snarf */
|
yuuji@0
|
973 }
|
yuuji@0
|
974 }
|
yuuji@0
|
975
|
yuuji@0
|
976 /* MBX mail copy message(s)
|
yuuji@0
|
977 * Accepts: MAIL stream
|
yuuji@0
|
978 * sequence
|
yuuji@0
|
979 * destination mailbox
|
yuuji@0
|
980 * copy options
|
yuuji@0
|
981 * Returns: T if success, NIL if failed
|
yuuji@0
|
982 */
|
yuuji@0
|
983
|
yuuji@0
|
984 long mbx_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options)
|
yuuji@0
|
985 {
|
yuuji@0
|
986 struct stat sbuf;
|
yuuji@0
|
987 time_t tp[2];
|
yuuji@0
|
988 MESSAGECACHE *elt;
|
yuuji@0
|
989 unsigned long i,j,k,m;
|
yuuji@0
|
990 long ret = LONGT;
|
yuuji@0
|
991 int fd,ld;
|
yuuji@0
|
992 char *s,*t,file[MAILTMPLEN],lock[MAILTMPLEN];
|
yuuji@0
|
993 mailproxycopy_t pc =
|
yuuji@0
|
994 (mailproxycopy_t) mail_parameters (stream,GET_MAILPROXYCOPY,NIL);
|
yuuji@0
|
995 copyuid_t cu = (copyuid_t) mail_parameters (NIL,GET_COPYUID,NIL);
|
yuuji@0
|
996 SEARCHSET *source = cu ? mail_newsearchset () : NIL;
|
yuuji@0
|
997 SEARCHSET *dest = cu ? mail_newsearchset () : NIL;
|
yuuji@0
|
998 MAILSTREAM *dstream = NIL;
|
yuuji@0
|
999 if (!((options & CP_UID) ? mail_uid_sequence (stream,sequence) :
|
yuuji@0
|
1000 mail_sequence (stream,sequence))) return NIL;
|
yuuji@0
|
1001 /* make sure valid mailbox */
|
yuuji@0
|
1002 if ((fd = mbx_isvalid (&dstream,mailbox,file,&ld,lock,
|
yuuji@0
|
1003 cu ? MBXISVALIDUID : MBXISVALIDNOUID)) < 0)
|
yuuji@0
|
1004 switch (errno) {
|
yuuji@0
|
1005 case ENOENT: /* no such file? */
|
yuuji@0
|
1006 MM_NOTIFY (stream,"[TRYCREATE] Must create mailbox before copy",NIL);
|
yuuji@0
|
1007 return NIL;
|
yuuji@0
|
1008 case EACCES: /* file protected */
|
yuuji@0
|
1009 sprintf (LOCAL->buf,"Can't access destination: %.80s",mailbox);
|
yuuji@0
|
1010 MM_LOG (LOCAL->buf,ERROR);
|
yuuji@0
|
1011 return NIL;
|
yuuji@0
|
1012 case EINVAL:
|
yuuji@0
|
1013 if (pc) return (*pc) (stream,sequence,mailbox,options);
|
yuuji@0
|
1014 sprintf (LOCAL->buf,"Invalid MBX-format mailbox name: %.80s",mailbox);
|
yuuji@0
|
1015 MM_LOG (LOCAL->buf,ERROR);
|
yuuji@0
|
1016 return NIL;
|
yuuji@0
|
1017 default:
|
yuuji@0
|
1018 if (pc) return (*pc) (stream,sequence,mailbox,options);
|
yuuji@0
|
1019 sprintf (LOCAL->buf,"Not a MBX-format mailbox: %.80s",mailbox);
|
yuuji@0
|
1020 MM_LOG (LOCAL->buf,ERROR);
|
yuuji@0
|
1021 return NIL;
|
yuuji@0
|
1022 }
|
yuuji@0
|
1023 MM_CRITICAL (stream); /* go critical */
|
yuuji@0
|
1024 fstat (fd,&sbuf); /* get current file size */
|
yuuji@0
|
1025 lseek (fd,sbuf.st_size,L_SET);/* move to end of file */
|
yuuji@0
|
1026
|
yuuji@0
|
1027 /* for each requested message */
|
yuuji@0
|
1028 for (i = 1; ret && (i <= stream->nmsgs); i++)
|
yuuji@0
|
1029 if ((elt = mail_elt (stream,i))->sequence) {
|
yuuji@0
|
1030 lseek (LOCAL->fd,elt->private.special.offset +
|
yuuji@0
|
1031 elt->private.special.text.size,L_SET);
|
yuuji@0
|
1032 mail_date(LOCAL->buf,elt);/* build target header */
|
yuuji@0
|
1033 /* get target keyword mask */
|
yuuji@0
|
1034 for (j = elt->user_flags, k = 0; j; )
|
yuuji@0
|
1035 if (s = stream->user_flags[find_rightmost_bit (&j)])
|
yuuji@0
|
1036 for (m = 0; (m < NUSERFLAGS) && (t = dstream->user_flags[m]); m++)
|
yuuji@0
|
1037 if (!compare_cstring (s,t) && (k |= 1 << m)) break;
|
yuuji@0
|
1038 sprintf (LOCAL->buf+strlen(LOCAL->buf),",%lu;%08lx%04x-%08lx\015\012",
|
yuuji@0
|
1039 elt->rfc822_size,k,(unsigned)
|
yuuji@0
|
1040 ((fSEEN * elt->seen) + (fDELETED * elt->deleted) +
|
yuuji@0
|
1041 (fFLAGGED * elt->flagged) + (fANSWERED * elt->answered) +
|
yuuji@0
|
1042 (fDRAFT * elt->draft)),cu ? ++dstream->uid_last : 0);
|
yuuji@0
|
1043 /* write target header */
|
yuuji@0
|
1044 if (ret = (write (fd,LOCAL->buf,strlen (LOCAL->buf)) > 0)) {
|
yuuji@0
|
1045 for (k = elt->rfc822_size; ret && (j = min (k,LOCAL->buflen)); k -= j){
|
yuuji@0
|
1046 read (LOCAL->fd,LOCAL->buf,j);
|
yuuji@0
|
1047 ret = write (fd,LOCAL->buf,j) >= 0;
|
yuuji@0
|
1048 }
|
yuuji@0
|
1049 if (cu) { /* need to pass back new UID? */
|
yuuji@0
|
1050 mail_append_set (source,mail_uid (stream,i));
|
yuuji@0
|
1051 mail_append_set (dest,dstream->uid_last);
|
yuuji@0
|
1052 }
|
yuuji@0
|
1053 }
|
yuuji@0
|
1054 }
|
yuuji@0
|
1055
|
yuuji@0
|
1056 /* make sure all the updates take */
|
yuuji@0
|
1057 if (!(ret && (ret = !fsync (fd)))) {
|
yuuji@0
|
1058 sprintf (LOCAL->buf,"Unable to write message: %s",strerror (errno));
|
yuuji@0
|
1059 MM_LOG (LOCAL->buf,ERROR);
|
yuuji@0
|
1060 ftruncate (fd,sbuf.st_size);
|
yuuji@0
|
1061 }
|
yuuji@0
|
1062 if (cu && ret) { /* return sets if doing COPYUID */
|
yuuji@0
|
1063 (*cu) (stream,mailbox,dstream->uid_validity,source,dest);
|
yuuji@0
|
1064 lseek (fd,15,L_SET); /* update UIDLAST */
|
yuuji@0
|
1065 sprintf (LOCAL->buf,"%08lx",dstream->uid_last);
|
yuuji@0
|
1066 write (fd,LOCAL->buf,8);
|
yuuji@0
|
1067 }
|
yuuji@0
|
1068 else { /* flush any sets we may have built */
|
yuuji@0
|
1069 mail_free_searchset (&source);
|
yuuji@0
|
1070 mail_free_searchset (&dest);
|
yuuji@0
|
1071 }
|
yuuji@0
|
1072 if (ret) tp[0] = time (0) - 1;/* set atime to now-1 if successful copy */
|
yuuji@0
|
1073 /* else preserve \Marked status */
|
yuuji@0
|
1074 else tp[0] = (sbuf.st_ctime > sbuf.st_atime) ? sbuf.st_atime : time(0);
|
yuuji@0
|
1075 tp[1] = sbuf.st_mtime; /* preserve mtime */
|
yuuji@0
|
1076 utime (file,tp); /* set the times */
|
yuuji@0
|
1077 close (fd); /* close the file */
|
yuuji@0
|
1078 MM_NOCRITICAL (stream); /* release critical */
|
yuuji@0
|
1079 unlockfd (ld,lock); /* release exclusive parse/append permission */
|
yuuji@0
|
1080 /* delete all requested messages */
|
yuuji@0
|
1081 if (ret && (options & CP_MOVE) && mbx_flaglock (stream)) {
|
yuuji@0
|
1082 for (i = 1; i <= stream->nmsgs; i++) if (mail_elt (stream,i)->sequence) {
|
yuuji@0
|
1083 /* mark message deleted */
|
yuuji@0
|
1084 mbx_elt (stream,i,NIL)->deleted = T;
|
yuuji@0
|
1085 /* recalculate status */
|
yuuji@0
|
1086 mbx_update_status (stream,i,NIL);
|
yuuji@0
|
1087 }
|
yuuji@0
|
1088 /* update flags */
|
yuuji@0
|
1089 mbx_flag (stream,NIL,NIL,NIL);
|
yuuji@0
|
1090 }
|
yuuji@0
|
1091 if (dstream != stream) mail_close (dstream);
|
yuuji@0
|
1092 return ret;
|
yuuji@0
|
1093 }
|
yuuji@0
|
1094
|
yuuji@0
|
1095 /* MBX mail append message from stringstruct
|
yuuji@0
|
1096 * Accepts: MAIL stream
|
yuuji@0
|
1097 * destination mailbox
|
yuuji@0
|
1098 * append callback
|
yuuji@0
|
1099 * data for callback
|
yuuji@0
|
1100 * Returns: T if append successful, else NIL
|
yuuji@0
|
1101 */
|
yuuji@0
|
1102
|
yuuji@0
|
1103 long mbx_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data)
|
yuuji@0
|
1104 {
|
yuuji@0
|
1105 struct stat sbuf;
|
yuuji@0
|
1106 int fd,ld;
|
yuuji@0
|
1107 char *flags,*date,tmp[MAILTMPLEN],file[MAILTMPLEN],lock[MAILTMPLEN];
|
yuuji@0
|
1108 time_t tp[2];
|
yuuji@0
|
1109 FILE *df;
|
yuuji@0
|
1110 MESSAGECACHE elt;
|
yuuji@0
|
1111 long f;
|
yuuji@0
|
1112 unsigned long i,uf;
|
yuuji@0
|
1113 STRING *message;
|
yuuji@0
|
1114 long ret = NIL;
|
yuuji@0
|
1115 MAILSTREAM *dstream = NIL;
|
yuuji@0
|
1116 appenduid_t au = (appenduid_t) mail_parameters (NIL,GET_APPENDUID,NIL);
|
yuuji@0
|
1117 SEARCHSET *dst = au ? mail_newsearchset () : NIL;
|
yuuji@0
|
1118 /* make sure valid mailbox */
|
yuuji@0
|
1119 if ((fd = mbx_isvalid (&dstream,mailbox,file,&ld,lock,
|
yuuji@0
|
1120 au ? MBXISVALIDUID : MBXISVALIDNOUID)) < 0)
|
yuuji@0
|
1121 switch (errno) {
|
yuuji@0
|
1122 case ENOENT: /* no such file? */
|
yuuji@0
|
1123 if (compare_cstring (mailbox,"INBOX")) {
|
yuuji@0
|
1124 MM_NOTIFY (stream,"[TRYCREATE] Must create mailbox before append",NIL);
|
yuuji@0
|
1125 return NIL;
|
yuuji@0
|
1126 }
|
yuuji@0
|
1127 /* can create INBOX here */
|
yuuji@0
|
1128 mbx_create (dstream = stream ? stream : user_flags (&mbxproto),"INBOX");
|
yuuji@0
|
1129 if ((fd = mbx_isvalid (&dstream,mailbox,file,&ld,lock,
|
yuuji@0
|
1130 au ? MBXISVALIDUID : MBXISVALIDNOUID)) >= 0)
|
yuuji@0
|
1131 break;
|
yuuji@0
|
1132 case EACCES: /* file protected */
|
yuuji@0
|
1133 sprintf (tmp,"Can't access destination: %.80s",mailbox);
|
yuuji@0
|
1134 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1135 return NIL;
|
yuuji@0
|
1136 case EINVAL:
|
yuuji@0
|
1137 sprintf (tmp,"Invalid MBX-format mailbox name: %.80s",mailbox);
|
yuuji@0
|
1138 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1139 return NIL;
|
yuuji@0
|
1140 default:
|
yuuji@0
|
1141 sprintf (tmp,"Not a MBX-format mailbox: %.80s",mailbox);
|
yuuji@0
|
1142 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1143 return NIL;
|
yuuji@0
|
1144 }
|
yuuji@0
|
1145
|
yuuji@0
|
1146 /* get first message */
|
yuuji@0
|
1147 if (!MM_APPEND (af) (dstream,data,&flags,&date,&message)) close (fd);
|
yuuji@0
|
1148 else if (!(df = fdopen (fd,"r+b"))) {
|
yuuji@0
|
1149 MM_LOG ("Unable to reopen append mailbox",ERROR);
|
yuuji@0
|
1150 close (fd);
|
yuuji@0
|
1151 }
|
yuuji@0
|
1152 else {
|
yuuji@0
|
1153 MM_CRITICAL (dstream); /* go critical */
|
yuuji@0
|
1154 fstat (fd,&sbuf); /* get current file size */
|
yuuji@0
|
1155 fseek (df,sbuf.st_size,SEEK_SET);
|
yuuji@0
|
1156 errno = 0;
|
yuuji@0
|
1157 for (ret = LONGT; ret && message; ) {
|
yuuji@0
|
1158 if (!SIZE (message)) { /* guard against zero-length */
|
yuuji@0
|
1159 MM_LOG ("Append of zero-length message",ERROR);
|
yuuji@0
|
1160 ret = NIL;
|
yuuji@0
|
1161 break;
|
yuuji@0
|
1162 }
|
yuuji@0
|
1163 f = mail_parse_flags (dstream,flags,&uf);
|
yuuji@0
|
1164 if (date) { /* parse date if given */
|
yuuji@0
|
1165 if (!mail_parse_date (&elt,date)) {
|
yuuji@0
|
1166 sprintf (tmp,"Bad date in append: %.80s",date);
|
yuuji@0
|
1167 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1168 ret = NIL; /* mark failure */
|
yuuji@0
|
1169 break;
|
yuuji@0
|
1170 }
|
yuuji@0
|
1171 mail_date (tmp,&elt); /* write preseved date */
|
yuuji@0
|
1172 }
|
yuuji@0
|
1173 else internal_date (tmp); /* get current date in IMAP format */
|
yuuji@0
|
1174 /* write header */
|
yuuji@0
|
1175 if (fprintf (df,"%s,%lu;%08lx%04lx-%08lx\015\012",tmp,i = SIZE (message),
|
yuuji@0
|
1176 uf,(unsigned long) f,au ? ++dstream->uid_last : 0) < 0)
|
yuuji@0
|
1177 ret = NIL;
|
yuuji@0
|
1178 else { /* write message */
|
yuuji@0
|
1179 size_t j;
|
yuuji@0
|
1180 if (!message->cursize) SETPOS (message,GETPOS (message));
|
yuuji@0
|
1181 while (i && (j = fwrite (message->curpos,1,message->cursize,df))) {
|
yuuji@0
|
1182 i -= j;
|
yuuji@0
|
1183 SETPOS (message,GETPOS (message) + j);
|
yuuji@0
|
1184 }
|
yuuji@0
|
1185 /* get next message */
|
yuuji@0
|
1186 if (i || !MM_APPEND (af) (dstream,data,&flags,&date,&message))
|
yuuji@0
|
1187 ret = NIL;
|
yuuji@0
|
1188 else if (au) mail_append_set (dst,dstream->uid_last);
|
yuuji@0
|
1189 }
|
yuuji@0
|
1190 }
|
yuuji@0
|
1191
|
yuuji@0
|
1192 /* if error... */
|
yuuji@0
|
1193 if (!ret || (fflush (df) == EOF)) {
|
yuuji@0
|
1194 /* revert file */
|
yuuji@0
|
1195 ftruncate (fd,sbuf.st_size);
|
yuuji@0
|
1196 close (fd); /* make sure fclose() doesn't corrupt us */
|
yuuji@0
|
1197 if (errno) {
|
yuuji@0
|
1198 sprintf (tmp,"Message append failed: %s",strerror (errno));
|
yuuji@0
|
1199 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1200 }
|
yuuji@0
|
1201 ret = NIL;
|
yuuji@0
|
1202 }
|
yuuji@0
|
1203 if (au && ret) { /* return sets if doing APPENDUID */
|
yuuji@0
|
1204 (*au) (mailbox,dstream->uid_validity,dst);
|
yuuji@0
|
1205 fseek (df,15,SEEK_SET); /* update UIDLAST */
|
yuuji@0
|
1206 fprintf (df,"%08lx",dstream->uid_last);
|
yuuji@0
|
1207 }
|
yuuji@0
|
1208 else mail_free_searchset (&dst);
|
yuuji@0
|
1209 /* set atime to now-1 if successful copy */
|
yuuji@0
|
1210 if (ret) tp[0] = time (0) - 1;
|
yuuji@0
|
1211 /* else preserve \Marked status */
|
yuuji@0
|
1212 else tp[0] = (sbuf.st_ctime > sbuf.st_atime) ? sbuf.st_atime : time(0);
|
yuuji@0
|
1213 tp[1] = sbuf.st_mtime; /* preserve mtime */
|
yuuji@0
|
1214 utime (file,tp); /* set the times */
|
yuuji@0
|
1215 fclose (df); /* close the file */
|
yuuji@0
|
1216 MM_NOCRITICAL (dstream); /* release critical */
|
yuuji@0
|
1217 }
|
yuuji@0
|
1218 unlockfd (ld,lock); /* release exclusive parse/append permission */
|
yuuji@0
|
1219 if (dstream != stream) mail_close (dstream);
|
yuuji@0
|
1220 return ret;
|
yuuji@0
|
1221 }
|
yuuji@0
|
1222
|
yuuji@0
|
1223 /* Internal routines */
|
yuuji@0
|
1224
|
yuuji@0
|
1225
|
yuuji@0
|
1226 /* MBX mail generate file string
|
yuuji@0
|
1227 * Accepts: temporary buffer to write into
|
yuuji@0
|
1228 * mailbox name string
|
yuuji@0
|
1229 * Returns: local file string or NIL if failure
|
yuuji@0
|
1230 */
|
yuuji@0
|
1231
|
yuuji@0
|
1232 char *mbx_file (char *dst,char *name)
|
yuuji@0
|
1233 {
|
yuuji@0
|
1234 char *s = mailboxfile (dst,name);
|
yuuji@0
|
1235 return (s && !*s) ? mailboxfile (dst,"~/INBOX") : s;
|
yuuji@0
|
1236 }
|
yuuji@0
|
1237
|
yuuji@0
|
1238 /* MBX mail parse mailbox
|
yuuji@0
|
1239 * Accepts: MAIL stream
|
yuuji@0
|
1240 * Returns: T if parse OK
|
yuuji@0
|
1241 * NIL if failure, stream aborted
|
yuuji@0
|
1242 */
|
yuuji@0
|
1243
|
yuuji@0
|
1244 long mbx_parse (MAILSTREAM *stream)
|
yuuji@0
|
1245 {
|
yuuji@0
|
1246 struct stat sbuf;
|
yuuji@0
|
1247 MESSAGECACHE *elt = NIL;
|
yuuji@0
|
1248 unsigned char c,*s,*t,*x;
|
yuuji@0
|
1249 char tmp[MAILTMPLEN];
|
yuuji@0
|
1250 unsigned long i,j,k,m;
|
yuuji@0
|
1251 off_t curpos = LOCAL->filesize;
|
yuuji@0
|
1252 unsigned long nmsgs = stream->nmsgs;
|
yuuji@0
|
1253 unsigned long recent = stream->recent;
|
yuuji@0
|
1254 unsigned long lastuid = 0;
|
yuuji@0
|
1255 short dirty = NIL;
|
yuuji@0
|
1256 short added = NIL;
|
yuuji@0
|
1257 short silent = stream->silent;
|
yuuji@0
|
1258 short uidwarn = T;
|
yuuji@0
|
1259 fstat (LOCAL->fd,&sbuf); /* get status */
|
yuuji@0
|
1260 if (sbuf.st_size < curpos) { /* sanity check */
|
yuuji@0
|
1261 sprintf (tmp,"Mailbox shrank from %lu to %lu!",
|
yuuji@0
|
1262 (unsigned long) curpos,(unsigned long) sbuf.st_size);
|
yuuji@0
|
1263 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1264 mbx_abort (stream);
|
yuuji@0
|
1265 return NIL;
|
yuuji@0
|
1266 }
|
yuuji@0
|
1267 lseek (LOCAL->fd,0,L_SET); /* rewind file */
|
yuuji@0
|
1268 /* read internal header */
|
yuuji@0
|
1269 read (LOCAL->fd,LOCAL->buf,HDRSIZE);
|
yuuji@0
|
1270 LOCAL->buf[HDRSIZE] = '\0'; /* tie off header */
|
yuuji@0
|
1271 c = LOCAL->buf[15]; /* save first character of last UID */
|
yuuji@0
|
1272 LOCAL->buf[15] = '\0';
|
yuuji@0
|
1273 /* parse UID validity */
|
yuuji@0
|
1274 stream->uid_validity = strtoul (LOCAL->buf + 7,NIL,16);
|
yuuji@0
|
1275 LOCAL->buf[15] = c; /* restore first character of last UID */
|
yuuji@0
|
1276 /* parse last UID */
|
yuuji@0
|
1277 i = strtoul (LOCAL->buf + 15,NIL,16);
|
yuuji@0
|
1278 stream->uid_last = stream->rdonly ? max (i,stream->uid_last) : i;
|
yuuji@0
|
1279 /* parse user flags */
|
yuuji@0
|
1280 for (i = 0, s = LOCAL->buf + 25;
|
yuuji@0
|
1281 (i < NUSERFLAGS) && (t = strchr (s,'\015')) && (t - s);
|
yuuji@0
|
1282 i++, s = t + 2) {
|
yuuji@0
|
1283 *t = '\0'; /* tie off flag */
|
yuuji@0
|
1284 if (!stream->user_flags[i] && (strlen (s) <= MAXUSERFLAG))
|
yuuji@0
|
1285 stream->user_flags[i] = cpystr (s);
|
yuuji@0
|
1286 }
|
yuuji@0
|
1287 LOCAL->ffuserflag = (int) i; /* first free user flag */
|
yuuji@0
|
1288
|
yuuji@0
|
1289 /* get current last flag updater PID */
|
yuuji@0
|
1290 i = (isxdigit (LOCAL->buf[HDRSIZE-10]) && isxdigit (LOCAL->buf[HDRSIZE-9]) &&
|
yuuji@0
|
1291 isxdigit (LOCAL->buf[HDRSIZE-8]) && isxdigit (LOCAL->buf[HDRSIZE-7]) &&
|
yuuji@0
|
1292 isxdigit (LOCAL->buf[HDRSIZE-6]) && isxdigit (LOCAL->buf[HDRSIZE-5]) &&
|
yuuji@0
|
1293 isxdigit (LOCAL->buf[HDRSIZE-4]) && isxdigit (LOCAL->buf[HDRSIZE-3]) &&
|
yuuji@0
|
1294 (LOCAL->buf[HDRSIZE-2] == '\015') && (LOCAL->buf[HDRSIZE-1] == '\012'))?
|
yuuji@0
|
1295 strtoul (LOCAL->buf + HDRSIZE - 8,NIL,16) : 0;
|
yuuji@0
|
1296 /* set flagcheck if lastpid changed */
|
yuuji@0
|
1297 if (LOCAL->lastpid && (LOCAL->lastpid != i)) LOCAL->flagcheck = T;
|
yuuji@0
|
1298 LOCAL->lastpid = i; /* set as last PID */
|
yuuji@0
|
1299 stream->silent = T; /* don't pass up exists events yet */
|
yuuji@0
|
1300 while (sbuf.st_size - curpos){/* while there is stuff to parse */
|
yuuji@0
|
1301 /* get to that position in the file */
|
yuuji@0
|
1302 lseek (LOCAL->fd,curpos,L_SET);
|
yuuji@0
|
1303 if ((i = read (LOCAL->fd,LOCAL->buf,64)) <= 0) {
|
yuuji@0
|
1304 sprintf (tmp,"Unable to read internal header at %lu, size = %lu: %s",
|
yuuji@0
|
1305 (unsigned long) curpos,(unsigned long) sbuf.st_size,
|
yuuji@0
|
1306 i ? strerror (errno) : "no data read");
|
yuuji@0
|
1307 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1308 mbx_abort (stream);
|
yuuji@0
|
1309 return NIL;
|
yuuji@0
|
1310 }
|
yuuji@0
|
1311 LOCAL->buf[i] = '\0'; /* tie off buffer just in case */
|
yuuji@0
|
1312 if (!((s = strchr (LOCAL->buf,'\015')) && (s[1] == '\012'))) {
|
yuuji@0
|
1313 sprintf (tmp,"Unable to find CRLF at %lu in %lu bytes, text: %.80s",
|
yuuji@0
|
1314 (unsigned long) curpos,i,(char *) LOCAL->buf);
|
yuuji@0
|
1315 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1316 mbx_abort (stream);
|
yuuji@0
|
1317 return NIL;
|
yuuji@0
|
1318 }
|
yuuji@0
|
1319 *s = '\0'; /* tie off header line */
|
yuuji@0
|
1320 i = (s + 2) - LOCAL->buf; /* note start of text offset */
|
yuuji@0
|
1321 if (!((s = strchr (LOCAL->buf,',')) && (t = strchr (s+1,';')))) {
|
yuuji@0
|
1322 sprintf (tmp,"Unable to parse internal header at %lu: %.80s",
|
yuuji@0
|
1323 (unsigned long) curpos,(char *) LOCAL->buf);
|
yuuji@0
|
1324 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1325 mbx_abort (stream);
|
yuuji@0
|
1326 return NIL;
|
yuuji@0
|
1327 }
|
yuuji@0
|
1328 if (!(isxdigit (t[1]) && isxdigit (t[2]) && isxdigit (t[3]) &&
|
yuuji@0
|
1329 isxdigit (t[4]) && isxdigit (t[5]) && isxdigit (t[6]) &&
|
yuuji@0
|
1330 isxdigit (t[7]) && isxdigit (t[8]) && isxdigit (t[9]) &&
|
yuuji@0
|
1331 isxdigit (t[10]) && isxdigit (t[11]) && isxdigit (t[12]))) {
|
yuuji@0
|
1332 sprintf (tmp,"Unable to parse message flags at %lu: %.80s",
|
yuuji@0
|
1333 (unsigned long) curpos,(char *) LOCAL->buf);
|
yuuji@0
|
1334 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1335 mbx_abort (stream);
|
yuuji@0
|
1336 return NIL;
|
yuuji@0
|
1337 }
|
yuuji@0
|
1338 if ((t[13] != '-') || t[22] ||
|
yuuji@0
|
1339 !(isxdigit (t[14]) && isxdigit (t[15]) && isxdigit (t[16]) &&
|
yuuji@0
|
1340 isxdigit (t[17]) && isxdigit (t[18]) && isxdigit (t[19]) &&
|
yuuji@0
|
1341 isxdigit (t[20]) && isxdigit (t[21]))) {
|
yuuji@0
|
1342 sprintf (tmp,"Unable to parse message UID at %lu: %.80s",
|
yuuji@0
|
1343 (unsigned long) curpos,(char *) LOCAL->buf);
|
yuuji@0
|
1344 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1345 mbx_abort (stream);
|
yuuji@0
|
1346 return NIL;
|
yuuji@0
|
1347 }
|
yuuji@0
|
1348
|
yuuji@0
|
1349 *s++ = '\0'; *t++ = '\0'; /* break up fields */
|
yuuji@0
|
1350 /* get message size */
|
yuuji@0
|
1351 if (!(j = strtoul (s,(char **) &x,10)) && (!(x && *x))) {
|
yuuji@0
|
1352 sprintf (tmp,"Unable to parse message size at %lu: %.80s,%.80s;%.80s",
|
yuuji@0
|
1353 (unsigned long) curpos,(char *) LOCAL->buf,(char *) s,
|
yuuji@0
|
1354 (char *) t);
|
yuuji@0
|
1355 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1356 mbx_abort (stream);
|
yuuji@0
|
1357 return NIL;
|
yuuji@0
|
1358 }
|
yuuji@0
|
1359 /* make sure didn't run off end of file */
|
yuuji@0
|
1360 if (((off_t) (curpos + i + j)) > sbuf.st_size) {
|
yuuji@0
|
1361 sprintf (tmp,"Last message (at %lu) runs past end of file (%lu > %lu)",
|
yuuji@0
|
1362 (unsigned long) curpos,(unsigned long) (curpos + i + j),
|
yuuji@0
|
1363 (unsigned long) sbuf.st_size);
|
yuuji@0
|
1364 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1365 mbx_abort (stream);
|
yuuji@0
|
1366 return NIL;
|
yuuji@0
|
1367 }
|
yuuji@0
|
1368 /* parse UID */
|
yuuji@0
|
1369 if ((m = strtoul (t+13,NIL,16)) &&
|
yuuji@0
|
1370 ((m <= lastuid) || (m > stream->uid_last))) {
|
yuuji@0
|
1371 if (uidwarn) {
|
yuuji@0
|
1372 sprintf (tmp,"Invalid UID %08lx in message %lu, rebuilding UIDs",
|
yuuji@0
|
1373 m,nmsgs+1);
|
yuuji@0
|
1374 MM_LOG (tmp,WARN);
|
yuuji@0
|
1375 uidwarn = NIL;
|
yuuji@0
|
1376 /* restart UID validity */
|
yuuji@0
|
1377 stream->uid_validity = time (0);
|
yuuji@0
|
1378 }
|
yuuji@0
|
1379 m = 0; /* lose this UID */
|
yuuji@0
|
1380 dirty = T; /* mark dirty, set new lastuid */
|
yuuji@0
|
1381 stream->uid_last = lastuid;
|
yuuji@0
|
1382 }
|
yuuji@0
|
1383
|
yuuji@0
|
1384 t[12] = '\0'; /* parse system flags */
|
yuuji@0
|
1385 if ((k = strtoul (t+8,NIL,16)) & fEXPUNGED) {
|
yuuji@0
|
1386 if (m) lastuid = m; /* expunge message, update last UID seen */
|
yuuji@0
|
1387 else { /* no UID assigned? */
|
yuuji@0
|
1388 lastuid = ++stream->uid_last;
|
yuuji@0
|
1389 dirty = T;
|
yuuji@0
|
1390 }
|
yuuji@0
|
1391 }
|
yuuji@0
|
1392 else { /* not expunged, swell the cache */
|
yuuji@0
|
1393 added = T; /* note that a new message was added */
|
yuuji@0
|
1394 mail_exists (stream,++nmsgs);
|
yuuji@0
|
1395 /* instantiate an elt for this message */
|
yuuji@0
|
1396 (elt = mail_elt (stream,nmsgs))->valid = T;
|
yuuji@0
|
1397 /* parse the date */
|
yuuji@0
|
1398 if (!mail_parse_date (elt,LOCAL->buf)) {
|
yuuji@0
|
1399 sprintf (tmp,"Unable to parse message date at %lu: %.80s",
|
yuuji@0
|
1400 (unsigned long) curpos,(char *) LOCAL->buf);
|
yuuji@0
|
1401 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1402 mbx_abort (stream);
|
yuuji@0
|
1403 return NIL;
|
yuuji@0
|
1404 }
|
yuuji@0
|
1405 /* note file offset of header */
|
yuuji@0
|
1406 elt->private.special.offset = curpos;
|
yuuji@0
|
1407 /* and internal header size */
|
yuuji@0
|
1408 elt->private.special.text.size = i;
|
yuuji@0
|
1409 /* header size not known yet */
|
yuuji@0
|
1410 elt->private.msg.header.text.size = 0;
|
yuuji@0
|
1411 elt->rfc822_size = j; /* note message size */
|
yuuji@0
|
1412 /* calculate system flags */
|
yuuji@0
|
1413 if (k & fSEEN) elt->seen = T;
|
yuuji@0
|
1414 if (k & fDELETED) elt->deleted = T;
|
yuuji@0
|
1415 if (k & fFLAGGED) elt->flagged = T;
|
yuuji@0
|
1416 if (k & fANSWERED) elt->answered = T;
|
yuuji@0
|
1417 if (k & fDRAFT) elt->draft = T;
|
yuuji@0
|
1418 t[8] = '\0'; /* get user flags value */
|
yuuji@0
|
1419 elt->user_flags = strtoul (t,NIL,16);
|
yuuji@0
|
1420 /* UID already assigned? */
|
yuuji@0
|
1421 if (!(elt->private.uid = m) || !(k & fOLD)) {
|
yuuji@0
|
1422 elt->recent = T; /* no, mark as recent */
|
yuuji@0
|
1423 ++recent; /* count up a new recent message */
|
yuuji@0
|
1424 dirty = T; /* and must rewrite header */
|
yuuji@0
|
1425 /* assign new UID */
|
yuuji@0
|
1426 if (!elt->private.uid) elt->private.uid = ++stream->uid_last;
|
yuuji@0
|
1427 mbx_update_status (stream,elt->msgno,NIL);
|
yuuji@0
|
1428 }
|
yuuji@0
|
1429 /* update last parsed UID */
|
yuuji@0
|
1430 lastuid = elt->private.uid;
|
yuuji@0
|
1431 }
|
yuuji@0
|
1432 curpos += i + j; /* update position */
|
yuuji@0
|
1433 }
|
yuuji@0
|
1434
|
yuuji@0
|
1435 if (dirty && !stream->rdonly){/* update header */
|
yuuji@0
|
1436 mbx_update_header (stream);
|
yuuji@0
|
1437 fsync (LOCAL->fd); /* make sure all the UID updates take */
|
yuuji@0
|
1438 }
|
yuuji@0
|
1439 /* update parsed file size and time */
|
yuuji@0
|
1440 LOCAL->filesize = sbuf.st_size;
|
yuuji@0
|
1441 fstat (LOCAL->fd,&sbuf); /* get status again to ensure time is right */
|
yuuji@0
|
1442 LOCAL->filetime = sbuf.st_mtime;
|
yuuji@0
|
1443 if (added && !stream->rdonly){/* make sure atime updated */
|
yuuji@0
|
1444 time_t tp[2];
|
yuuji@0
|
1445 tp[0] = time (0);
|
yuuji@0
|
1446 tp[1] = LOCAL->filetime;
|
yuuji@0
|
1447 utime (stream->mailbox,tp);
|
yuuji@0
|
1448 }
|
yuuji@0
|
1449 stream->silent = silent; /* can pass up events now */
|
yuuji@0
|
1450 mail_exists (stream,nmsgs); /* notify upper level of new mailbox size */
|
yuuji@0
|
1451 mail_recent (stream,recent); /* and of change in recent messages */
|
yuuji@0
|
1452 return LONGT; /* return the winnage */
|
yuuji@0
|
1453 }
|
yuuji@0
|
1454
|
yuuji@0
|
1455 /* MBX get cache element with status updating from file
|
yuuji@0
|
1456 * Accepts: MAIL stream
|
yuuji@0
|
1457 * message number
|
yuuji@0
|
1458 * expunge OK flag
|
yuuji@0
|
1459 * Returns: cache element
|
yuuji@0
|
1460 */
|
yuuji@0
|
1461
|
yuuji@0
|
1462 MESSAGECACHE *mbx_elt (MAILSTREAM *stream,unsigned long msgno,long expok)
|
yuuji@0
|
1463 {
|
yuuji@0
|
1464 MESSAGECACHE *elt = mail_elt (stream,msgno);
|
yuuji@0
|
1465 struct { /* old flags */
|
yuuji@0
|
1466 unsigned int seen : 1;
|
yuuji@0
|
1467 unsigned int deleted : 1;
|
yuuji@0
|
1468 unsigned int flagged : 1;
|
yuuji@0
|
1469 unsigned int answered : 1;
|
yuuji@0
|
1470 unsigned int draft : 1;
|
yuuji@0
|
1471 unsigned long user_flags;
|
yuuji@0
|
1472 } old;
|
yuuji@0
|
1473 old.seen = elt->seen; old.deleted = elt->deleted; old.flagged = elt->flagged;
|
yuuji@0
|
1474 old.answered = elt->answered; old.draft = elt->draft;
|
yuuji@0
|
1475 old.user_flags = elt->user_flags;
|
yuuji@0
|
1476 /* get new flags */
|
yuuji@0
|
1477 if (mbx_read_flags (stream,elt) && expok) {
|
yuuji@0
|
1478 mail_expunged (stream,elt->msgno);
|
yuuji@0
|
1479 return NIL; /* return this message was expunged */
|
yuuji@0
|
1480 }
|
yuuji@0
|
1481 if ((old.seen != elt->seen) || (old.deleted != elt->deleted) ||
|
yuuji@0
|
1482 (old.flagged != elt->flagged) || (old.answered != elt->answered) ||
|
yuuji@0
|
1483 (old.draft != elt->draft) || (old.user_flags != elt->user_flags))
|
yuuji@0
|
1484 MM_FLAGS (stream,msgno); /* let top level know */
|
yuuji@0
|
1485 return elt;
|
yuuji@0
|
1486 }
|
yuuji@0
|
1487
|
yuuji@0
|
1488 /* MBX read flags from file
|
yuuji@0
|
1489 * Accepts: MAIL stream
|
yuuji@0
|
1490 * cache element
|
yuuji@0
|
1491 * Returns: non-NIL if message expunged
|
yuuji@0
|
1492 */
|
yuuji@0
|
1493
|
yuuji@0
|
1494 unsigned long mbx_read_flags (MAILSTREAM *stream,MESSAGECACHE *elt)
|
yuuji@0
|
1495 {
|
yuuji@0
|
1496 unsigned long i;
|
yuuji@0
|
1497 struct stat sbuf;
|
yuuji@0
|
1498 fstat (LOCAL->fd,&sbuf); /* get status */
|
yuuji@0
|
1499 /* make sure file size is good */
|
yuuji@0
|
1500 if (sbuf.st_size < LOCAL->filesize) {
|
yuuji@0
|
1501 sprintf (LOCAL->buf,"Mailbox shrank from %lu to %lu in flag read!",
|
yuuji@0
|
1502 (unsigned long) LOCAL->filesize,(unsigned long) sbuf.st_size);
|
yuuji@0
|
1503 fatal (LOCAL->buf);
|
yuuji@0
|
1504 }
|
yuuji@0
|
1505 /* set the seek pointer */
|
yuuji@0
|
1506 lseek (LOCAL->fd,(off_t) elt->private.special.offset +
|
yuuji@0
|
1507 elt->private.special.text.size - 24,L_SET);
|
yuuji@0
|
1508 /* read the new flags */
|
yuuji@0
|
1509 if (read (LOCAL->fd,LOCAL->buf,14) < 0) {
|
yuuji@0
|
1510 sprintf (LOCAL->buf,"Unable to read new status: %s",strerror (errno));
|
yuuji@0
|
1511 fatal (LOCAL->buf);
|
yuuji@0
|
1512 }
|
yuuji@0
|
1513 if ((LOCAL->buf[0] != ';') || (LOCAL->buf[13] != '-')) {
|
yuuji@0
|
1514 LOCAL->buf[14] = '\0'; /* tie off buffer for error message */
|
yuuji@0
|
1515 sprintf (LOCAL->buf+50,"Invalid flags for message %lu (%lu %lu): %s",
|
yuuji@0
|
1516 elt->msgno,elt->private.special.offset,
|
yuuji@0
|
1517 elt->private.special.text.size,(char *) LOCAL->buf);
|
yuuji@0
|
1518 fatal (LOCAL->buf+50);
|
yuuji@0
|
1519 }
|
yuuji@0
|
1520 LOCAL->buf[13] = '\0'; /* tie off buffer */
|
yuuji@0
|
1521 /* calculate system flags */
|
yuuji@0
|
1522 i = strtoul (LOCAL->buf+9,NIL,16);
|
yuuji@0
|
1523 elt->seen = i & fSEEN ? T : NIL;
|
yuuji@0
|
1524 elt->deleted = i & fDELETED ? T : NIL;
|
yuuji@0
|
1525 elt->flagged = i & fFLAGGED ? T : NIL;
|
yuuji@0
|
1526 elt->answered = i & fANSWERED ? T : NIL;
|
yuuji@0
|
1527 elt->draft = i & fDRAFT ? T : NIL;
|
yuuji@0
|
1528 LOCAL->expunged |= i & fEXPUNGED ? T : NIL;
|
yuuji@0
|
1529 LOCAL->buf[9] = '\0'; /* tie off flags */
|
yuuji@0
|
1530 /* get user flags value */
|
yuuji@0
|
1531 elt->user_flags = strtoul (LOCAL->buf+1,NIL,16);
|
yuuji@0
|
1532 elt->valid = T; /* have valid flags now */
|
yuuji@0
|
1533 return i & fEXPUNGED;
|
yuuji@0
|
1534 }
|
yuuji@0
|
1535
|
yuuji@0
|
1536 /* MBX update header
|
yuuji@0
|
1537 * Accepts: MAIL stream
|
yuuji@0
|
1538 */
|
yuuji@0
|
1539
|
yuuji@0
|
1540 #ifndef CYGKLUDGEOFFSET
|
yuuji@0
|
1541 #define CYGKLUDGEOFFSET 0
|
yuuji@0
|
1542 #endif
|
yuuji@0
|
1543
|
yuuji@0
|
1544 void mbx_update_header (MAILSTREAM *stream)
|
yuuji@0
|
1545 {
|
yuuji@0
|
1546 int i;
|
yuuji@0
|
1547 char *s = LOCAL->buf;
|
yuuji@0
|
1548 memset (s,'\0',HDRSIZE); /* initialize header */
|
yuuji@0
|
1549 sprintf (s,"*mbx*\015\012%08lx%08lx\015\012",
|
yuuji@0
|
1550 stream->uid_validity,stream->uid_last);
|
yuuji@0
|
1551 for (i = 0; (i < NUSERFLAGS) && stream->user_flags[i]; ++i)
|
yuuji@0
|
1552 sprintf (s += strlen (s),"%s\015\012",stream->user_flags[i]);
|
yuuji@0
|
1553 LOCAL->ffuserflag = i; /* first free user flag */
|
yuuji@0
|
1554 /* can we create more user flags? */
|
yuuji@0
|
1555 stream->kwd_create = (i < NUSERFLAGS) ? T : NIL;
|
yuuji@0
|
1556 /* write reserved lines */
|
yuuji@0
|
1557 while (i++ < NUSERFLAGS) strcat (s,"\015\012");
|
yuuji@0
|
1558 sprintf (LOCAL->buf + HDRSIZE - 10,"%08lx\015\012",LOCAL->lastpid);
|
yuuji@0
|
1559 while (T) { /* rewind file */
|
yuuji@0
|
1560 lseek (LOCAL->fd,CYGKLUDGEOFFSET,L_SET);
|
yuuji@0
|
1561 /* write new header */
|
yuuji@0
|
1562 if (write (LOCAL->fd,LOCAL->buf + CYGKLUDGEOFFSET,
|
yuuji@0
|
1563 HDRSIZE - CYGKLUDGEOFFSET) > 0) break;
|
yuuji@0
|
1564 MM_NOTIFY (stream,strerror (errno),WARN);
|
yuuji@0
|
1565 MM_DISKERROR (stream,errno,T);
|
yuuji@0
|
1566 }
|
yuuji@0
|
1567 }
|
yuuji@0
|
1568
|
yuuji@0
|
1569 /* MBX update status string
|
yuuji@0
|
1570 * Accepts: MAIL stream
|
yuuji@0
|
1571 * message number
|
yuuji@0
|
1572 * flags
|
yuuji@0
|
1573 */
|
yuuji@0
|
1574
|
yuuji@0
|
1575 void mbx_update_status (MAILSTREAM *stream,unsigned long msgno,long flags)
|
yuuji@0
|
1576 {
|
yuuji@0
|
1577 struct stat sbuf;
|
yuuji@0
|
1578 MESSAGECACHE *elt = mail_elt (stream,msgno);
|
yuuji@0
|
1579 /* readonly */
|
yuuji@0
|
1580 if (stream->rdonly || !elt->valid) mbx_read_flags (stream,elt);
|
yuuji@0
|
1581 else { /* readwrite */
|
yuuji@0
|
1582 fstat (LOCAL->fd,&sbuf); /* get status */
|
yuuji@0
|
1583 /* make sure file size is good */
|
yuuji@0
|
1584 if (sbuf.st_size < LOCAL->filesize) {
|
yuuji@0
|
1585 sprintf (LOCAL->buf,"Mailbox shrank from %lu to %lu in flag update!",
|
yuuji@0
|
1586 (unsigned long) LOCAL->filesize,(unsigned long) sbuf.st_size);
|
yuuji@0
|
1587 fatal (LOCAL->buf);
|
yuuji@0
|
1588 }
|
yuuji@0
|
1589 /* set the seek pointer */
|
yuuji@0
|
1590 lseek (LOCAL->fd,(off_t) elt->private.special.offset +
|
yuuji@0
|
1591 elt->private.special.text.size - 24,L_SET);
|
yuuji@0
|
1592 /* read the new flags */
|
yuuji@0
|
1593 if (read (LOCAL->fd,LOCAL->buf,14) < 0) {
|
yuuji@0
|
1594 sprintf (LOCAL->buf,"Unable to read old status: %s",strerror (errno));
|
yuuji@0
|
1595 fatal (LOCAL->buf);
|
yuuji@0
|
1596 }
|
yuuji@0
|
1597 if ((LOCAL->buf[0] != ';') || (LOCAL->buf[13] != '-')) {
|
yuuji@0
|
1598 LOCAL->buf[14] = '\0'; /* tie off buffer for error message */
|
yuuji@0
|
1599 sprintf (LOCAL->buf+50,"Invalid flags for message %lu (%lu %lu): %s",
|
yuuji@0
|
1600 elt->msgno,elt->private.special.offset,
|
yuuji@0
|
1601 elt->private.special.text.size,(char *) LOCAL->buf);
|
yuuji@0
|
1602 fatal (LOCAL->buf+50);
|
yuuji@0
|
1603 }
|
yuuji@0
|
1604 /* print new flag string */
|
yuuji@0
|
1605 sprintf (LOCAL->buf,"%08lx%04x-%08lx",elt->user_flags,(unsigned)
|
yuuji@0
|
1606 (((elt->deleted && flags) ?
|
yuuji@0
|
1607 fEXPUNGED : (strtoul (LOCAL->buf+9,NIL,16)) & fEXPUNGED) +
|
yuuji@0
|
1608 (fSEEN * elt->seen) + (fDELETED * elt->deleted) +
|
yuuji@0
|
1609 (fFLAGGED * elt->flagged) + (fANSWERED * elt->answered) +
|
yuuji@0
|
1610 (fDRAFT * elt->draft) + fOLD),elt->private.uid);
|
yuuji@0
|
1611 while (T) { /* get to that place in the file */
|
yuuji@0
|
1612 lseek (LOCAL->fd,(off_t) elt->private.special.offset +
|
yuuji@0
|
1613 elt->private.special.text.size - 23,L_SET);
|
yuuji@0
|
1614 /* write new flags and UID */
|
yuuji@0
|
1615 if (write (LOCAL->fd,LOCAL->buf,21) > 0) break;
|
yuuji@0
|
1616 MM_NOTIFY (stream,strerror (errno),WARN);
|
yuuji@0
|
1617 MM_DISKERROR (stream,errno,T);
|
yuuji@0
|
1618 }
|
yuuji@0
|
1619 }
|
yuuji@0
|
1620 }
|
yuuji@0
|
1621
|
yuuji@0
|
1622 /* MBX locate header for a message
|
yuuji@0
|
1623 * Accepts: MAIL stream
|
yuuji@0
|
1624 * message number
|
yuuji@0
|
1625 * pointer to returned header size
|
yuuji@0
|
1626 * pointer to possible returned header
|
yuuji@0
|
1627 * Returns: position of header in file
|
yuuji@0
|
1628 */
|
yuuji@0
|
1629
|
yuuji@0
|
1630 #define HDRBUFLEN 16384 /* good enough for most headers */
|
yuuji@0
|
1631 #define SLOP 4 /* CR LF CR LF */
|
yuuji@0
|
1632
|
yuuji@0
|
1633 unsigned long mbx_hdrpos (MAILSTREAM *stream,unsigned long msgno,
|
yuuji@0
|
1634 unsigned long *size,char **hdr)
|
yuuji@0
|
1635 {
|
yuuji@0
|
1636 unsigned long siz,done;
|
yuuji@0
|
1637 long i;
|
yuuji@0
|
1638 unsigned char *s,*t,*te;
|
yuuji@0
|
1639 MESSAGECACHE *elt = mail_elt (stream,msgno);
|
yuuji@0
|
1640 unsigned long ret = elt->private.special.offset +
|
yuuji@0
|
1641 elt->private.special.text.size;
|
yuuji@0
|
1642 if (hdr) *hdr = NIL; /* assume no header returned */
|
yuuji@0
|
1643 /* is header size known? */
|
yuuji@0
|
1644 if (*size = elt->private.msg.header.text.size) return ret;
|
yuuji@0
|
1645 /* paranoia check */
|
yuuji@0
|
1646 if (LOCAL->buflen < (HDRBUFLEN + SLOP))
|
yuuji@0
|
1647 fatal ("LOCAL->buf smaller than HDRBUFLEN");
|
yuuji@0
|
1648 lseek (LOCAL->fd,ret,L_SET); /* get to header position */
|
yuuji@0
|
1649 /* read HDRBUFLEN chunks with 4 byte slop */
|
yuuji@0
|
1650 for (done = siz = 0, s = LOCAL->buf;
|
yuuji@0
|
1651 (i = min ((long) (elt->rfc822_size - done),(long) HDRBUFLEN)) &&
|
yuuji@0
|
1652 (read (LOCAL->fd,s,i) == i);
|
yuuji@0
|
1653 done += i, siz += (t - LOCAL->buf) - SLOP, s = LOCAL->buf + SLOP) {
|
yuuji@0
|
1654 te = (t = s + i) - 12; /* calculate end of fast scan */
|
yuuji@0
|
1655 /* fast scan for CR */
|
yuuji@0
|
1656 for (s = LOCAL->buf; s < te;)
|
yuuji@0
|
1657 if (((*s++ == '\015') || (*s++ == '\015') || (*s++ == '\015') ||
|
yuuji@0
|
1658 (*s++ == '\015') || (*s++ == '\015') || (*s++ == '\015') ||
|
yuuji@0
|
1659 (*s++ == '\015') || (*s++ == '\015') || (*s++ == '\015') ||
|
yuuji@0
|
1660 (*s++ == '\015') || (*s++ == '\015') || (*s++ == '\015')) &&
|
yuuji@0
|
1661 (*s == '\012') && (*++s == '\015') && (*++s == '\012')) {
|
yuuji@0
|
1662 *size = elt->private.msg.header.text.size = siz + (++s - LOCAL->buf);
|
yuuji@0
|
1663 if (hdr) *hdr = LOCAL->buf;
|
yuuji@0
|
1664 return ret;
|
yuuji@0
|
1665 }
|
yuuji@0
|
1666 for (te = t - 3; (s < te);) /* final character-at-a-time scan */
|
yuuji@0
|
1667 if ((*s++ == '\015') && (*s == '\012') && (*++s == '\015') &&
|
yuuji@0
|
1668 (*++s == '\012')) {
|
yuuji@0
|
1669 *size = elt->private.msg.header.text.size = siz + (++s - LOCAL->buf);
|
yuuji@0
|
1670 if (hdr) *hdr = LOCAL->buf;
|
yuuji@0
|
1671 return ret;
|
yuuji@0
|
1672 }
|
yuuji@0
|
1673 if (i <= SLOP) break; /* end of data */
|
yuuji@0
|
1674 /* slide over last 4 bytes */
|
yuuji@0
|
1675 memmove (LOCAL->buf,t - SLOP,SLOP);
|
yuuji@0
|
1676 hdr = NIL; /* can't return header this way */
|
yuuji@0
|
1677 }
|
yuuji@0
|
1678 /* not found: header consumes entire message */
|
yuuji@0
|
1679 elt->private.msg.header.text.size = *size = elt->rfc822_size;
|
yuuji@0
|
1680 if (hdr) *hdr = LOCAL->buf; /* possibly return header too */
|
yuuji@0
|
1681 return ret;
|
yuuji@0
|
1682 }
|
yuuji@0
|
1683
|
yuuji@0
|
1684 /* MBX mail rewrite mailbox
|
yuuji@0
|
1685 * Accepts: MAIL stream
|
yuuji@0
|
1686 * pointer to return reclaimed size
|
yuuji@0
|
1687 * flags (0 = no expunge, 1 = expunge deleted, -1 = expunge sequence)
|
yuuji@0
|
1688 * Returns: number of expunged messages
|
yuuji@0
|
1689 */
|
yuuji@0
|
1690
|
yuuji@0
|
1691 unsigned long mbx_rewrite (MAILSTREAM *stream,unsigned long *reclaimed,
|
yuuji@0
|
1692 long flags)
|
yuuji@0
|
1693 {
|
yuuji@0
|
1694 time_t tp[2];
|
yuuji@0
|
1695 struct stat sbuf;
|
yuuji@0
|
1696 off_t pos,ppos;
|
yuuji@0
|
1697 int ld;
|
yuuji@0
|
1698 unsigned long i,j,k,m,delta;
|
yuuji@0
|
1699 unsigned long n = *reclaimed = 0;
|
yuuji@0
|
1700 unsigned long recent = 0;
|
yuuji@0
|
1701 char lock[MAILTMPLEN];
|
yuuji@0
|
1702 MESSAGECACHE *elt;
|
yuuji@0
|
1703 blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL);
|
yuuji@0
|
1704 /* The cretins who designed flock() created a window of vulnerability in
|
yuuji@0
|
1705 * upgrading locks from shared to exclusive or downgrading from exclusive
|
yuuji@0
|
1706 * to shared. Rather than maintain the lock at shared status at a minimum,
|
yuuji@0
|
1707 * flock() actually *releases* the former lock. Obviously they never talked
|
yuuji@0
|
1708 * to any database guys. Fortunately, we have the parse/append permission
|
yuuji@0
|
1709 * lock. If we require this lock before going exclusive on the mailbox,
|
yuuji@0
|
1710 * another process can not sneak in and steal the exclusive mailbox lock on
|
yuuji@0
|
1711 * us, because it will block on trying to get parse/append permission first.
|
yuuji@0
|
1712 */
|
yuuji@0
|
1713 /* get parse/append permission */
|
yuuji@0
|
1714 if ((ld = lockfd (LOCAL->fd,lock,LOCK_EX)) < 0) {
|
yuuji@0
|
1715 MM_LOG ("Unable to lock mailbox for rewrite",ERROR);
|
yuuji@0
|
1716 return 0;
|
yuuji@0
|
1717 }
|
yuuji@0
|
1718 fstat (LOCAL->fd,&sbuf); /* get current write time */
|
yuuji@0
|
1719 if (LOCAL->filetime && !LOCAL->flagcheck &&
|
yuuji@0
|
1720 (LOCAL->filetime < sbuf.st_mtime)) LOCAL->flagcheck = T;
|
yuuji@0
|
1721 if (!mbx_parse (stream)) { /* make sure see any newly-arrived messages */
|
yuuji@0
|
1722 unlockfd (ld,lock); /* failed?? */
|
yuuji@0
|
1723 return 0;
|
yuuji@0
|
1724 }
|
yuuji@0
|
1725 if (LOCAL->flagcheck) { /* sweep flags if need flagcheck */
|
yuuji@0
|
1726 LOCAL->filetime = sbuf.st_mtime;
|
yuuji@0
|
1727 for (i = 1; i <= stream->nmsgs; ++i) mbx_elt (stream,i,NIL);
|
yuuji@0
|
1728 LOCAL->flagcheck = NIL;
|
yuuji@0
|
1729 }
|
yuuji@0
|
1730
|
yuuji@0
|
1731 /* get exclusive access */
|
yuuji@0
|
1732 if (!flock (LOCAL->fd,LOCK_EX|LOCK_NB)) {
|
yuuji@0
|
1733 MM_CRITICAL (stream); /* go critical */
|
yuuji@0
|
1734 for (i = 1,delta = 0,pos = ppos = HDRSIZE; i <= stream->nmsgs; ) {
|
yuuji@0
|
1735 /* note if message not at predicted location */
|
yuuji@0
|
1736 if (m = (elt = mbx_elt (stream,i,NIL))->private.special.offset - ppos) {
|
yuuji@0
|
1737 ppos = elt->private.special.offset;
|
yuuji@0
|
1738 *reclaimed += m; /* note reclaimed message space */
|
yuuji@0
|
1739 delta += m; /* and as expunge delta */
|
yuuji@0
|
1740 }
|
yuuji@0
|
1741 /* number of bytes to smash or preserve */
|
yuuji@0
|
1742 ppos += (k = elt->private.special.text.size + elt->rfc822_size);
|
yuuji@0
|
1743 /* if need to expunge this message*/
|
yuuji@0
|
1744 if (flags && elt->deleted && ((flags > 0) || elt->sequence)) {
|
yuuji@0
|
1745 delta += k; /* number of bytes to delete */
|
yuuji@0
|
1746 mail_expunged(stream,i);/* notify upper levels */
|
yuuji@0
|
1747 n++; /* count up one more expunged message */
|
yuuji@0
|
1748 }
|
yuuji@0
|
1749 else { /* preserved message */
|
yuuji@0
|
1750 i++; /* count this message */
|
yuuji@0
|
1751 if (elt->recent) ++recent;
|
yuuji@0
|
1752 if (delta) { /* moved, note first byte to preserve */
|
yuuji@0
|
1753 j = elt->private.special.offset;
|
yuuji@0
|
1754 do { /* read from source position */
|
yuuji@0
|
1755 m = min (k,LOCAL->buflen);
|
yuuji@0
|
1756 lseek (LOCAL->fd,j,L_SET);
|
yuuji@0
|
1757 read (LOCAL->fd,LOCAL->buf,m);
|
yuuji@0
|
1758 pos = j - delta; /* write to destination position */
|
yuuji@0
|
1759 while (T) {
|
yuuji@0
|
1760 lseek (LOCAL->fd,pos,L_SET);
|
yuuji@0
|
1761 if (write (LOCAL->fd,LOCAL->buf,m) > 0) break;
|
yuuji@0
|
1762 MM_NOTIFY (stream,strerror (errno),WARN);
|
yuuji@0
|
1763 MM_DISKERROR (stream,errno,T);
|
yuuji@0
|
1764 }
|
yuuji@0
|
1765 pos += m; /* new position */
|
yuuji@0
|
1766 j += m; /* next chunk, perhaps */
|
yuuji@0
|
1767 } while (k -= m); /* until done */
|
yuuji@0
|
1768 /* note the new address of this text */
|
yuuji@0
|
1769 elt->private.special.offset -= delta;
|
yuuji@0
|
1770 }
|
yuuji@0
|
1771 /* preserved but no deleted messages yet */
|
yuuji@0
|
1772 else pos = elt->private.special.offset + k;
|
yuuji@0
|
1773 }
|
yuuji@0
|
1774 }
|
yuuji@0
|
1775 /* deltaed file size match position? */
|
yuuji@0
|
1776 if (m = (LOCAL->filesize -= delta) - pos) {
|
yuuji@0
|
1777 *reclaimed += m; /* probably an fEXPUNGED msg */
|
yuuji@0
|
1778 LOCAL->filesize = pos; /* set correct size */
|
yuuji@0
|
1779 }
|
yuuji@0
|
1780 /* truncate file after last message */
|
yuuji@0
|
1781 ftruncate (LOCAL->fd,LOCAL->filesize);
|
yuuji@0
|
1782 fsync (LOCAL->fd); /* force disk update */
|
yuuji@0
|
1783 MM_NOCRITICAL (stream); /* release critical */
|
yuuji@0
|
1784 (*bn) (BLOCK_FILELOCK,NIL);
|
yuuji@0
|
1785 flock (LOCAL->fd,LOCK_SH); /* allow sharers again */
|
yuuji@0
|
1786 (*bn) (BLOCK_NONE,NIL);
|
yuuji@0
|
1787 }
|
yuuji@0
|
1788
|
yuuji@0
|
1789 else { /* can't get exclusive */
|
yuuji@0
|
1790 (*bn) (BLOCK_FILELOCK,NIL);
|
yuuji@0
|
1791 flock (LOCAL->fd,LOCK_SH); /* recover previous shared mailbox lock */
|
yuuji@0
|
1792 (*bn) (BLOCK_NONE,NIL);
|
yuuji@0
|
1793 /* do hide-expunge when shared */
|
yuuji@0
|
1794 if (flags) for (i = 1; i <= stream->nmsgs; ) {
|
yuuji@0
|
1795 if (elt = mbx_elt (stream,i,T)) {
|
yuuji@0
|
1796 /* make the message invisible */
|
yuuji@0
|
1797 if (elt->deleted && ((flags > 0) || elt->sequence)) {
|
yuuji@0
|
1798 mbx_update_status (stream,elt->msgno,LONGT);
|
yuuji@0
|
1799 /* notify upper levels */
|
yuuji@0
|
1800 mail_expunged (stream,i);
|
yuuji@0
|
1801 n++; /* count up one more expunged message */
|
yuuji@0
|
1802 }
|
yuuji@0
|
1803 else {
|
yuuji@0
|
1804 i++; /* preserved message */
|
yuuji@0
|
1805 if (elt->recent) ++recent;
|
yuuji@0
|
1806 }
|
yuuji@0
|
1807 }
|
yuuji@0
|
1808 else n++; /* count up one more expunged message */
|
yuuji@0
|
1809 }
|
yuuji@0
|
1810 fsync (LOCAL->fd); /* force disk update */
|
yuuji@0
|
1811 }
|
yuuji@0
|
1812 fstat (LOCAL->fd,&sbuf); /* get new write time */
|
yuuji@0
|
1813 tp[1] = LOCAL->filetime = sbuf.st_mtime;
|
yuuji@0
|
1814 tp[0] = time (0); /* reset atime to now */
|
yuuji@0
|
1815 utime (stream->mailbox,tp);
|
yuuji@0
|
1816 unlockfd (ld,lock); /* release exclusive parse/append permission */
|
yuuji@0
|
1817 /* notify upper level of new mailbox size */
|
yuuji@0
|
1818 mail_exists (stream,stream->nmsgs);
|
yuuji@0
|
1819 mail_recent (stream,recent);
|
yuuji@0
|
1820 return n; /* return number of expunged messages */
|
yuuji@0
|
1821 }
|
yuuji@0
|
1822
|
yuuji@0
|
1823 /* MBX mail lock for flag updating
|
yuuji@0
|
1824 * Accepts: stream
|
yuuji@0
|
1825 * Returns: T if successful, NIL if failure
|
yuuji@0
|
1826 */
|
yuuji@0
|
1827
|
yuuji@0
|
1828 long mbx_flaglock (MAILSTREAM *stream)
|
yuuji@0
|
1829 {
|
yuuji@0
|
1830 struct stat sbuf;
|
yuuji@0
|
1831 unsigned long i;
|
yuuji@0
|
1832 int ld;
|
yuuji@0
|
1833 char lock[MAILTMPLEN];
|
yuuji@0
|
1834 /* no-op if readonly or already locked */
|
yuuji@0
|
1835 if (!stream->rdonly && LOCAL && (LOCAL->fd >= 0) && (LOCAL->ld < 0)) {
|
yuuji@0
|
1836 /* lock now */
|
yuuji@0
|
1837 if ((ld = lockfd (LOCAL->fd,lock,LOCK_EX)) < 0) return NIL;
|
yuuji@0
|
1838 if (!LOCAL->flagcheck) { /* don't do this if flagcheck already needed */
|
yuuji@0
|
1839 if (LOCAL->filetime) { /* know previous time? */
|
yuuji@0
|
1840 fstat (LOCAL->fd,&sbuf);/* get current write time */
|
yuuji@0
|
1841 if (LOCAL->filetime < sbuf.st_mtime) LOCAL->flagcheck = T;
|
yuuji@0
|
1842 LOCAL->filetime = 0; /* don't do this test for any other messages */
|
yuuji@0
|
1843 }
|
yuuji@0
|
1844 if (!mbx_parse (stream)) {/* parse mailbox */
|
yuuji@0
|
1845 unlockfd (ld,lock); /* shouldn't happen */
|
yuuji@0
|
1846 return NIL;
|
yuuji@0
|
1847 }
|
yuuji@0
|
1848 if (LOCAL->flagcheck) /* invalidate cache if flagcheck */
|
yuuji@0
|
1849 for (i = 1; i <= stream->nmsgs; ++i) mail_elt (stream,i)->valid = NIL;
|
yuuji@0
|
1850 }
|
yuuji@0
|
1851 LOCAL->ld = ld; /* copy to stream for subsequent calls */
|
yuuji@0
|
1852 memcpy (LOCAL->lock,lock,MAILTMPLEN);
|
yuuji@0
|
1853 }
|
yuuji@0
|
1854 return LONGT;
|
yuuji@0
|
1855 }
|