rev |
line source |
yuuji@0
|
1 /* ========================================================================
|
yuuji@0
|
2 * Copyright 1988-2008 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: MMDF mail routines
|
yuuji@0
|
16 *
|
yuuji@0
|
17 * Author: Mark Crispin
|
yuuji@0
|
18 * UW Technology
|
yuuji@0
|
19 * University of Washington
|
yuuji@0
|
20 * Seattle, WA 98195
|
yuuji@0
|
21 * Internet: MRC@Washington.EDU
|
yuuji@0
|
22 *
|
yuuji@0
|
23 * Date: 20 December 1989
|
yuuji@0
|
24 * Last Edited: 27 March 2008
|
yuuji@0
|
25 */
|
yuuji@0
|
26
|
yuuji@0
|
27
|
yuuji@0
|
28 #include <stdio.h>
|
yuuji@0
|
29 #include <ctype.h>
|
yuuji@0
|
30 #include <errno.h>
|
yuuji@0
|
31 extern int errno; /* just in case */
|
yuuji@0
|
32 #include <signal.h>
|
yuuji@0
|
33 #include "mail.h"
|
yuuji@0
|
34 #include "osdep.h"
|
yuuji@0
|
35 #include <time.h>
|
yuuji@0
|
36 #include <sys/stat.h>
|
yuuji@0
|
37 #include "pseudo.h"
|
yuuji@0
|
38 #include "fdstring.h"
|
yuuji@0
|
39 #include "misc.h"
|
yuuji@0
|
40 #include "dummy.h"
|
yuuji@0
|
41
|
yuuji@0
|
42 /* Supposedly, this page has everything the MMDF driver needs to know about
|
yuuji@0
|
43 * the MMDF delimiter. By changing these macros, the MMDF driver should
|
yuuji@0
|
44 * change with it. Note that if you change the length of MMDFHDRTXT you
|
yuuji@0
|
45 * also need to change the ISMMDF and RETIFMMDFWRD macros to reflect the new
|
yuuji@0
|
46 * size.
|
yuuji@0
|
47 */
|
yuuji@0
|
48
|
yuuji@0
|
49
|
yuuji@0
|
50 /* Useful MMDF constants */
|
yuuji@0
|
51
|
yuuji@0
|
52 #define MMDFCHR '\01' /* MMDF character */
|
yuuji@0
|
53 #define MMDFCHRS 0x01010101 /* MMDF header character spread in a word */
|
yuuji@0
|
54 /* MMDF header text */
|
yuuji@0
|
55 #define MMDFHDRTXT "\01\01\01\01\n"
|
yuuji@0
|
56 /* length of MMDF header text */
|
yuuji@0
|
57 #define MMDFHDRLEN (sizeof (MMDFHDRTXT) - 1)
|
yuuji@0
|
58
|
yuuji@0
|
59
|
yuuji@0
|
60 /* Validate MMDF header
|
yuuji@0
|
61 * Accepts: pointer to candidate string to validate as an MMDF header
|
yuuji@0
|
62 * Returns: T if valid; else NIL
|
yuuji@0
|
63 */
|
yuuji@0
|
64
|
yuuji@0
|
65 #define ISMMDF(s) \
|
yuuji@0
|
66 ((*(s) == MMDFCHR) && ((s)[1] == MMDFCHR) && ((s)[2] == MMDFCHR) && \
|
yuuji@0
|
67 ((s)[3] == MMDFCHR) && ((s)[4] == '\n'))
|
yuuji@0
|
68
|
yuuji@0
|
69
|
yuuji@0
|
70 /* Return if a 32-bit word has the start of an MMDF header
|
yuuji@0
|
71 * Accepts: pointer to word of four bytes to validate as an MMDF header
|
yuuji@0
|
72 * Returns: pointer to MMDF header, else proceeds
|
yuuji@0
|
73 */
|
yuuji@0
|
74
|
yuuji@0
|
75 #define RETIFMMDFWRD(s) { \
|
yuuji@0
|
76 if (s[3] == MMDFCHR) { \
|
yuuji@0
|
77 if ((s[4] == MMDFCHR) && (s[5] == MMDFCHR) && (s[6] == MMDFCHR) && \
|
yuuji@0
|
78 (s[7] == '\n')) return s + 3; \
|
yuuji@0
|
79 else if (s[2] == MMDFCHR) { \
|
yuuji@0
|
80 if ((s[4] == MMDFCHR) && (s[5] == MMDFCHR) && (s[6] == '\n')) \
|
yuuji@0
|
81 return s + 2; \
|
yuuji@0
|
82 else if (s[1] == MMDFCHR) { \
|
yuuji@0
|
83 if ((s[4] == MMDFCHR) && (s[5] == '\n')) return s + 1; \
|
yuuji@0
|
84 else if ((*s == MMDFCHR) && (s[4] == '\n')) return s; \
|
yuuji@0
|
85 } \
|
yuuji@0
|
86 } \
|
yuuji@0
|
87 } \
|
yuuji@0
|
88 }
|
yuuji@0
|
89
|
yuuji@0
|
90 /* Validate line
|
yuuji@0
|
91 * Accepts: pointer to candidate string to validate as a From header
|
yuuji@0
|
92 * return pointer to end of date/time field
|
yuuji@0
|
93 * return pointer to offset from t of time (hours of ``mmm dd hh:mm'')
|
yuuji@0
|
94 * return pointer to offset from t of time zone (if non-zero)
|
yuuji@0
|
95 * Returns: t,ti,zn set if valid From string, else ti is NIL
|
yuuji@0
|
96 */
|
yuuji@0
|
97
|
yuuji@0
|
98 #define VALID(s,x,ti,zn) { \
|
yuuji@0
|
99 ti = 0; \
|
yuuji@0
|
100 if ((*s == 'F') && (s[1] == 'r') && (s[2] == 'o') && (s[3] == 'm') && \
|
yuuji@0
|
101 (s[4] == ' ')) { \
|
yuuji@0
|
102 for (x = s + 5; *x && *x != '\n'; x++); \
|
yuuji@0
|
103 if (*x) { \
|
yuuji@0
|
104 if (x - s >= 41) { \
|
yuuji@0
|
105 for (zn = -1; x[zn] != ' '; zn--); \
|
yuuji@0
|
106 if ((x[zn-1] == 'm') && (x[zn-2] == 'o') && (x[zn-3] == 'r') && \
|
yuuji@0
|
107 (x[zn-4] == 'f') && (x[zn-5] == ' ') && (x[zn-6] == 'e') && \
|
yuuji@0
|
108 (x[zn-7] == 't') && (x[zn-8] == 'o') && (x[zn-9] == 'm') && \
|
yuuji@0
|
109 (x[zn-10] == 'e') && (x[zn-11] == 'r') && (x[zn-12] == ' '))\
|
yuuji@0
|
110 x += zn - 12; \
|
yuuji@0
|
111 } \
|
yuuji@0
|
112 if (x - s >= 27) { \
|
yuuji@0
|
113 if (x[-5] == ' ') { \
|
yuuji@0
|
114 if (x[-8] == ':') zn = 0,ti = -5; \
|
yuuji@0
|
115 else if (x[-9] == ' ') ti = zn = -9; \
|
yuuji@0
|
116 else if ((x[-11] == ' ') && ((x[-10]=='+') || (x[-10]=='-'))) \
|
yuuji@0
|
117 ti = zn = -11; \
|
yuuji@0
|
118 } \
|
yuuji@0
|
119 else if (x[-4] == ' ') { \
|
yuuji@0
|
120 if (x[-9] == ' ') zn = -4,ti = -9; \
|
yuuji@0
|
121 } \
|
yuuji@0
|
122 else if (x[-6] == ' ') { \
|
yuuji@0
|
123 if ((x[-11] == ' ') && ((x[-5] == '+') || (x[-5] == '-'))) \
|
yuuji@0
|
124 zn = -6,ti = -11; \
|
yuuji@0
|
125 } \
|
yuuji@0
|
126 if (ti && !((x[ti - 3] == ':') && \
|
yuuji@0
|
127 (x[ti -= ((x[ti - 6] == ':') ? 9 : 6)] == ' ') && \
|
yuuji@0
|
128 (x[ti - 3] == ' ') && (x[ti - 7] == ' ') && \
|
yuuji@0
|
129 (x[ti - 11] == ' '))) ti = 0; \
|
yuuji@0
|
130 } \
|
yuuji@0
|
131 } \
|
yuuji@0
|
132 } \
|
yuuji@0
|
133 }
|
yuuji@0
|
134
|
yuuji@0
|
135 /* You are not expected to understand this macro, but read the next page if
|
yuuji@0
|
136 * you are not faint of heart.
|
yuuji@0
|
137 *
|
yuuji@0
|
138 * Known formats to the VALID macro are:
|
yuuji@0
|
139 * From user Wed Dec 2 05:53 1992
|
yuuji@0
|
140 * BSD From user Wed Dec 2 05:53:22 1992
|
yuuji@0
|
141 * SysV From user Wed Dec 2 05:53 PST 1992
|
yuuji@0
|
142 * rn From user Wed Dec 2 05:53:22 PST 1992
|
yuuji@0
|
143 * From user Wed Dec 2 05:53 -0700 1992
|
yuuji@0
|
144 * emacs From user Wed Dec 2 05:53:22 -0700 1992
|
yuuji@0
|
145 * From user Wed Dec 2 05:53 1992 PST
|
yuuji@0
|
146 * From user Wed Dec 2 05:53:22 1992 PST
|
yuuji@0
|
147 * From user Wed Dec 2 05:53 1992 -0700
|
yuuji@0
|
148 * Solaris From user Wed Dec 2 05:53:22 1992 -0700
|
yuuji@0
|
149 *
|
yuuji@0
|
150 * Plus all of the above with `` remote from xxx'' after it. Thank you very
|
yuuji@0
|
151 * much, smail and Solaris, for making my life considerably more complicated.
|
yuuji@0
|
152 */
|
yuuji@0
|
153
|
yuuji@0
|
154 /*
|
yuuji@0
|
155 * What? You want to understand the VALID macro anyway? Alright, since you
|
yuuji@0
|
156 * insist. Actually, it isn't really all that difficult, provided that you
|
yuuji@0
|
157 * take it step by step.
|
yuuji@0
|
158 *
|
yuuji@0
|
159 * Line 1 Initializes the return ti value to failure (0);
|
yuuji@0
|
160 * Lines 2-3 Validates that the 1st-5th characters are ``From ''.
|
yuuji@0
|
161 * Lines 4-5 Validates that there is an end of line and points x at it.
|
yuuji@0
|
162 * Lines 6-13 First checks to see if the line is at least 41 characters long.
|
yuuji@0
|
163 * If so, it scans backwards to find the rightmost space. From
|
yuuji@0
|
164 * that point, it scans backwards to see if the string matches
|
yuuji@0
|
165 * `` remote from''. If so, it sets x to point to the space at
|
yuuji@0
|
166 * the start of the string.
|
yuuji@0
|
167 * Line 14 Makes sure that there are at least 27 characters in the line.
|
yuuji@0
|
168 * Lines 15-20 Checks if the date/time ends with the year (there is a space
|
yuuji@0
|
169 * five characters back). If there is a colon three characters
|
yuuji@0
|
170 * further back, there is no timezone field, so zn is set to 0
|
yuuji@0
|
171 * and ti is set in front of the year. Otherwise, there must
|
yuuji@0
|
172 * either to be a space four characters back for a three-letter
|
yuuji@0
|
173 * timezone, or a space six characters back followed by a + or -
|
yuuji@0
|
174 * for a numeric timezone; in either case, zn and ti become the
|
yuuji@0
|
175 * offset of the space immediately before it.
|
yuuji@0
|
176 * Lines 21-23 Are the failure case for line 14. If there is a space four
|
yuuji@0
|
177 * characters back, it is a three-letter timezone; there must be a
|
yuuji@0
|
178 * space for the year nine characters back. zn is the zone
|
yuuji@0
|
179 * offset; ti is the offset of the space.
|
yuuji@0
|
180 * Lines 24-27 Are the failure case for line 20. If there is a space six
|
yuuji@0
|
181 * characters back, it is a numeric timezone; there must be a
|
yuuji@0
|
182 * space eleven characters back and a + or - five characters back.
|
yuuji@0
|
183 * zn is the zone offset; ti is the offset of the space.
|
yuuji@0
|
184 * Line 28-31 If ti is valid, make sure that the string before ti is of the
|
yuuji@0
|
185 * form www mmm dd hh:mm or www mmm dd hh:mm:ss, otherwise
|
yuuji@0
|
186 * invalidate ti. There must be a colon three characters back
|
yuuji@0
|
187 * and a space six or nine characters back (depending upon
|
yuuji@0
|
188 * whether or not the character six characters back is a colon).
|
yuuji@0
|
189 * There must be a space three characters further back (in front
|
yuuji@0
|
190 * of the day), one seven characters back (in front of the month),
|
yuuji@0
|
191 * and one eleven characters back (in front of the day of week).
|
yuuji@0
|
192 * ti is set to be the offset of the space before the time.
|
yuuji@0
|
193 *
|
yuuji@0
|
194 * Why a macro? It gets invoked a *lot* in a tight loop. On some of the
|
yuuji@0
|
195 * newer pipelined machines it is faster being open-coded than it would be if
|
yuuji@0
|
196 * subroutines are called.
|
yuuji@0
|
197 *
|
yuuji@0
|
198 * Why does it scan backwards from the end of the line, instead of doing the
|
yuuji@0
|
199 * much easier forward scan? There is no deterministic way to parse the
|
yuuji@0
|
200 * ``user'' field, because it may contain unquoted spaces! Yes, I tested it to
|
yuuji@0
|
201 * see if unquoted spaces were possible. They are, and I've encountered enough
|
yuuji@0
|
202 * evil mail to be totally unwilling to trust that ``it will never happen''.
|
yuuji@0
|
203 */
|
yuuji@0
|
204
|
yuuji@0
|
205 /* Build parameters */
|
yuuji@0
|
206
|
yuuji@0
|
207 #define KODRETRY 15 /* kiss-of-death retry in seconds */
|
yuuji@0
|
208 #define LOCKTIMEOUT 5 /* lock timeout in minutes */
|
yuuji@0
|
209
|
yuuji@0
|
210
|
yuuji@0
|
211 /* MMDF I/O stream local data */
|
yuuji@0
|
212
|
yuuji@0
|
213 typedef struct mmdf_local {
|
yuuji@0
|
214 unsigned int dirty : 1; /* disk copy needs updating */
|
yuuji@0
|
215 unsigned int ddirty : 1; /* double-dirty, ping becomes checkpoint */
|
yuuji@0
|
216 unsigned int pseudo : 1; /* uses a pseudo message */
|
yuuji@0
|
217 unsigned int appending : 1; /* don't mark new messages as old */
|
yuuji@0
|
218 int fd; /* mailbox file descriptor */
|
yuuji@0
|
219 int ld; /* lock file descriptor */
|
yuuji@0
|
220 char *lname; /* lock file name */
|
yuuji@0
|
221 off_t filesize; /* file size parsed */
|
yuuji@0
|
222 time_t filetime; /* last file time */
|
yuuji@0
|
223 unsigned char *buf; /* temporary buffer */
|
yuuji@0
|
224 unsigned long buflen; /* current size of temporary buffer */
|
yuuji@0
|
225 unsigned long uid; /* current text uid */
|
yuuji@0
|
226 SIZEDTEXT text; /* current text */
|
yuuji@0
|
227 unsigned long textlen; /* current text length */
|
yuuji@0
|
228 char *line; /* returned line */
|
yuuji@0
|
229 char *linebuf; /* line readin buffer */
|
yuuji@0
|
230 unsigned long linebuflen; /* current line readin buffer length */
|
yuuji@0
|
231 } MMDFLOCAL;
|
yuuji@0
|
232
|
yuuji@0
|
233
|
yuuji@0
|
234 /* Convenient access to local data */
|
yuuji@0
|
235
|
yuuji@0
|
236 #define LOCAL ((MMDFLOCAL *) stream->local)
|
yuuji@0
|
237
|
yuuji@0
|
238
|
yuuji@0
|
239 /* MMDF protected file structure */
|
yuuji@0
|
240
|
yuuji@0
|
241 typedef struct mmdf_file {
|
yuuji@0
|
242 MAILSTREAM *stream; /* current stream */
|
yuuji@0
|
243 off_t curpos; /* current file position */
|
yuuji@0
|
244 off_t protect; /* protected position */
|
yuuji@0
|
245 off_t filepos; /* current last written file position */
|
yuuji@0
|
246 char *buf; /* overflow buffer */
|
yuuji@0
|
247 size_t buflen; /* current overflow buffer length */
|
yuuji@0
|
248 char *bufpos; /* current buffer position */
|
yuuji@0
|
249 } MMDFFILE;
|
yuuji@0
|
250
|
yuuji@0
|
251 /* Function prototypes */
|
yuuji@0
|
252
|
yuuji@0
|
253 DRIVER *mmdf_valid (char *name);
|
yuuji@0
|
254 long mmdf_isvalid (char *name,char *tmp);
|
yuuji@0
|
255 long mmdf_isvalid_fd (int fd,char *tmp);
|
yuuji@0
|
256 void *mmdf_parameters (long function,void *value);
|
yuuji@0
|
257 void mmdf_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents);
|
yuuji@0
|
258 void mmdf_list (MAILSTREAM *stream,char *ref,char *pat);
|
yuuji@0
|
259 void mmdf_lsub (MAILSTREAM *stream,char *ref,char *pat);
|
yuuji@0
|
260 long mmdf_create (MAILSTREAM *stream,char *mailbox);
|
yuuji@0
|
261 long mmdf_delete (MAILSTREAM *stream,char *mailbox);
|
yuuji@0
|
262 long mmdf_rename (MAILSTREAM *stream,char *old,char *newname);
|
yuuji@0
|
263 MAILSTREAM *mmdf_open (MAILSTREAM *stream);
|
yuuji@0
|
264 void mmdf_close (MAILSTREAM *stream,long options);
|
yuuji@0
|
265 char *mmdf_header (MAILSTREAM *stream,unsigned long msgno,
|
yuuji@0
|
266 unsigned long *length,long flags);
|
yuuji@0
|
267 long mmdf_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags);
|
yuuji@0
|
268 char *mmdf_text_work (MAILSTREAM *stream,MESSAGECACHE *elt,
|
yuuji@0
|
269 unsigned long *length,long flags);
|
yuuji@0
|
270 void mmdf_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt);
|
yuuji@0
|
271 long mmdf_ping (MAILSTREAM *stream);
|
yuuji@0
|
272 void mmdf_check (MAILSTREAM *stream);
|
yuuji@0
|
273 long mmdf_expunge (MAILSTREAM *stream,char *sequence,long options);
|
yuuji@0
|
274 long mmdf_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options);
|
yuuji@0
|
275 long mmdf_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data);
|
yuuji@0
|
276 int mmdf_collect_msg (MAILSTREAM *stream,FILE *sf,char *flags,char *date,
|
yuuji@0
|
277 STRING *msg);
|
yuuji@0
|
278 int mmdf_append_msgs (MAILSTREAM *stream,FILE *sf,FILE *df,SEARCHSET *set);
|
yuuji@0
|
279
|
yuuji@0
|
280 void mmdf_abort (MAILSTREAM *stream);
|
yuuji@0
|
281 char *mmdf_file (char *dst,char *name);
|
yuuji@0
|
282 int mmdf_lock (char *file,int flags,int mode,DOTLOCK *lock,int op);
|
yuuji@0
|
283 void mmdf_unlock (int fd,MAILSTREAM *stream,DOTLOCK *lock);
|
yuuji@0
|
284 int mmdf_parse (MAILSTREAM *stream,DOTLOCK *lock,int op);
|
yuuji@0
|
285 char *mmdf_mbxline (MAILSTREAM *stream,STRING *bs,unsigned long *size);
|
yuuji@0
|
286 unsigned long mmdf_pseudo (MAILSTREAM *stream,char *hdr);
|
yuuji@0
|
287 unsigned long mmdf_xstatus (MAILSTREAM *stream,char *status,MESSAGECACHE *elt,
|
yuuji@0
|
288 unsigned long uid,long flag);
|
yuuji@0
|
289 long mmdf_rewrite (MAILSTREAM *stream,unsigned long *nexp,DOTLOCK *lock,
|
yuuji@0
|
290 long flags);
|
yuuji@0
|
291 long mmdf_extend (MAILSTREAM *stream,unsigned long size);
|
yuuji@0
|
292 void mmdf_write (MMDFFILE *f,char *s,unsigned long i);
|
yuuji@0
|
293 void mmdf_phys_write (MMDFFILE *f,char *buf,size_t size);
|
yuuji@0
|
294
|
yuuji@0
|
295 /* MMDF mail routines */
|
yuuji@0
|
296
|
yuuji@0
|
297
|
yuuji@0
|
298 /* Driver dispatch used by MAIL */
|
yuuji@0
|
299
|
yuuji@0
|
300 DRIVER mmdfdriver = {
|
yuuji@0
|
301 "mmdf", /* driver name */
|
yuuji@0
|
302 /* driver flags */
|
yuuji@0
|
303 DR_LOCAL|DR_MAIL|DR_LOCKING|DR_NONEWMAILRONLY|DR_XPOINT,
|
yuuji@0
|
304 (DRIVER *) NIL, /* next driver */
|
yuuji@0
|
305 mmdf_valid, /* mailbox is valid for us */
|
yuuji@0
|
306 mmdf_parameters, /* manipulate parameters */
|
yuuji@0
|
307 mmdf_scan, /* scan mailboxes */
|
yuuji@0
|
308 mmdf_list, /* list mailboxes */
|
yuuji@0
|
309 mmdf_lsub, /* list subscribed mailboxes */
|
yuuji@0
|
310 NIL, /* subscribe to mailbox */
|
yuuji@0
|
311 NIL, /* unsubscribe from mailbox */
|
yuuji@0
|
312 mmdf_create, /* create mailbox */
|
yuuji@0
|
313 mmdf_delete, /* delete mailbox */
|
yuuji@0
|
314 mmdf_rename, /* rename mailbox */
|
yuuji@0
|
315 mail_status_default, /* status of mailbox */
|
yuuji@0
|
316 mmdf_open, /* open mailbox */
|
yuuji@0
|
317 mmdf_close, /* close mailbox */
|
yuuji@0
|
318 NIL, /* fetch message "fast" attributes */
|
yuuji@0
|
319 NIL, /* fetch message flags */
|
yuuji@0
|
320 NIL, /* fetch overview */
|
yuuji@0
|
321 NIL, /* fetch message envelopes */
|
yuuji@0
|
322 mmdf_header, /* fetch message header */
|
yuuji@0
|
323 mmdf_text, /* fetch message text */
|
yuuji@0
|
324 NIL, /* fetch partial message text */
|
yuuji@0
|
325 NIL, /* unique identifier */
|
yuuji@0
|
326 NIL, /* message number */
|
yuuji@0
|
327 NIL, /* modify flags */
|
yuuji@0
|
328 mmdf_flagmsg, /* per-message modify flags */
|
yuuji@0
|
329 NIL, /* search for message based on criteria */
|
yuuji@0
|
330 NIL, /* sort messages */
|
yuuji@0
|
331 NIL, /* thread messages */
|
yuuji@0
|
332 mmdf_ping, /* ping mailbox to see if still alive */
|
yuuji@0
|
333 mmdf_check, /* check for new messages */
|
yuuji@0
|
334 mmdf_expunge, /* expunge deleted messages */
|
yuuji@0
|
335 mmdf_copy, /* copy messages to another mailbox */
|
yuuji@0
|
336 mmdf_append, /* append string message to mailbox */
|
yuuji@0
|
337 NIL /* garbage collect stream */
|
yuuji@0
|
338 };
|
yuuji@0
|
339
|
yuuji@0
|
340 /* prototype stream */
|
yuuji@0
|
341 MAILSTREAM mmdfproto = {&mmdfdriver};
|
yuuji@0
|
342
|
yuuji@0
|
343 char *mmdfhdr = MMDFHDRTXT; /* MMDF header */
|
yuuji@0
|
344
|
yuuji@0
|
345 /* MMDF mail validate mailbox
|
yuuji@0
|
346 * Accepts: mailbox name
|
yuuji@0
|
347 * Returns: our driver if name is valid, NIL otherwise
|
yuuji@0
|
348 */
|
yuuji@0
|
349
|
yuuji@0
|
350 DRIVER *mmdf_valid (char *name)
|
yuuji@0
|
351 {
|
yuuji@0
|
352 char tmp[MAILTMPLEN];
|
yuuji@0
|
353 return mmdf_isvalid (name,tmp) ? &mmdfdriver : NIL;
|
yuuji@0
|
354 }
|
yuuji@0
|
355
|
yuuji@0
|
356
|
yuuji@0
|
357 /* MMDF mail test for valid mailbox name
|
yuuji@0
|
358 * Accepts: mailbox name
|
yuuji@0
|
359 * scratch buffer
|
yuuji@0
|
360 * Returns: T if valid, NIL otherwise
|
yuuji@0
|
361 */
|
yuuji@0
|
362
|
yuuji@0
|
363 long mmdf_isvalid (char *name,char *tmp)
|
yuuji@0
|
364 {
|
yuuji@0
|
365 int fd;
|
yuuji@0
|
366 int ret = NIL;
|
yuuji@0
|
367 char *t,file[MAILTMPLEN];
|
yuuji@0
|
368 struct stat sbuf;
|
yuuji@0
|
369 time_t tp[2];
|
yuuji@0
|
370 errno = EINVAL; /* assume invalid argument */
|
yuuji@0
|
371 /* must be non-empty file */
|
yuuji@0
|
372 if ((t = dummy_file (file,name)) && !stat (t,&sbuf)) {
|
yuuji@0
|
373 if (!sbuf.st_size)errno = 0;/* empty file */
|
yuuji@0
|
374 else if ((fd = open (file,O_RDONLY,NIL)) >= 0) {
|
yuuji@0
|
375 /* error -1 for invalid format */
|
yuuji@0
|
376 if (!(ret = mmdf_isvalid_fd (fd,tmp))) errno = -1;
|
yuuji@0
|
377 close (fd); /* close the file */
|
yuuji@0
|
378 /* \Marked status? */
|
yuuji@0
|
379 if ((sbuf.st_ctime > sbuf.st_atime) || (sbuf.st_mtime > sbuf.st_atime)) {
|
yuuji@0
|
380 tp[0] = sbuf.st_atime; /* preserve atime and mtime */
|
yuuji@0
|
381 tp[1] = sbuf.st_mtime;
|
yuuji@0
|
382 utime (file,tp); /* set the times */
|
yuuji@0
|
383 }
|
yuuji@0
|
384 }
|
yuuji@0
|
385 }
|
yuuji@0
|
386 return ret; /* return what we should */
|
yuuji@0
|
387 }
|
yuuji@0
|
388
|
yuuji@0
|
389 /* MMDF mail test for valid mailbox
|
yuuji@0
|
390 * Accepts: file descriptor
|
yuuji@0
|
391 * scratch buffer
|
yuuji@0
|
392 * Returns: T if valid, NIL otherwise
|
yuuji@0
|
393 */
|
yuuji@0
|
394
|
yuuji@0
|
395 long mmdf_isvalid_fd (int fd,char *tmp)
|
yuuji@0
|
396 {
|
yuuji@0
|
397 int ret = NIL;
|
yuuji@0
|
398 memset (tmp,'\0',MAILTMPLEN);
|
yuuji@0
|
399 if (read (fd,tmp,MAILTMPLEN-1) >= 0) ret = ISMMDF (tmp) ? T : NIL;
|
yuuji@0
|
400 return ret; /* return what we should */
|
yuuji@0
|
401 }
|
yuuji@0
|
402
|
yuuji@0
|
403
|
yuuji@0
|
404 /* MMDF manipulate driver parameters
|
yuuji@0
|
405 * Accepts: function code
|
yuuji@0
|
406 * function-dependent value
|
yuuji@0
|
407 * Returns: function-dependent return value
|
yuuji@0
|
408 */
|
yuuji@0
|
409
|
yuuji@0
|
410 void *mmdf_parameters (long function,void *value)
|
yuuji@0
|
411 {
|
yuuji@0
|
412 void *ret = NIL;
|
yuuji@0
|
413 switch ((int) function) {
|
yuuji@0
|
414 case GET_INBOXPATH:
|
yuuji@0
|
415 if (value) ret = dummy_file ((char *) value,"INBOX");
|
yuuji@0
|
416 break;
|
yuuji@0
|
417 }
|
yuuji@0
|
418 return ret;
|
yuuji@0
|
419 }
|
yuuji@0
|
420
|
yuuji@0
|
421 /* MMDF mail scan mailboxes
|
yuuji@0
|
422 * Accepts: mail stream
|
yuuji@0
|
423 * reference
|
yuuji@0
|
424 * pattern to search
|
yuuji@0
|
425 * string to scan
|
yuuji@0
|
426 */
|
yuuji@0
|
427
|
yuuji@0
|
428 void mmdf_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents)
|
yuuji@0
|
429 {
|
yuuji@0
|
430 if (stream) dummy_scan (NIL,ref,pat,contents);
|
yuuji@0
|
431 }
|
yuuji@0
|
432
|
yuuji@0
|
433
|
yuuji@0
|
434 /* MMDF mail list mailboxes
|
yuuji@0
|
435 * Accepts: mail stream
|
yuuji@0
|
436 * reference
|
yuuji@0
|
437 * pattern to search
|
yuuji@0
|
438 */
|
yuuji@0
|
439
|
yuuji@0
|
440 void mmdf_list (MAILSTREAM *stream,char *ref,char *pat)
|
yuuji@0
|
441 {
|
yuuji@0
|
442 if (stream) dummy_list (NIL,ref,pat);
|
yuuji@0
|
443 }
|
yuuji@0
|
444
|
yuuji@0
|
445
|
yuuji@0
|
446 /* MMDF mail list subscribed mailboxes
|
yuuji@0
|
447 * Accepts: mail stream
|
yuuji@0
|
448 * reference
|
yuuji@0
|
449 * pattern to search
|
yuuji@0
|
450 */
|
yuuji@0
|
451
|
yuuji@0
|
452 void mmdf_lsub (MAILSTREAM *stream,char *ref,char *pat)
|
yuuji@0
|
453 {
|
yuuji@0
|
454 if (stream) dummy_lsub (NIL,ref,pat);
|
yuuji@0
|
455 }
|
yuuji@0
|
456
|
yuuji@0
|
457 /* MMDF mail create mailbox
|
yuuji@0
|
458 * Accepts: MAIL stream
|
yuuji@0
|
459 * mailbox name to create
|
yuuji@0
|
460 * Returns: T on success, NIL on failure
|
yuuji@0
|
461 */
|
yuuji@0
|
462
|
yuuji@0
|
463 long mmdf_create (MAILSTREAM *stream,char *mailbox)
|
yuuji@0
|
464 {
|
yuuji@0
|
465 char *s,mbx[MAILTMPLEN],tmp[MAILTMPLEN];
|
yuuji@0
|
466 long ret = NIL;
|
yuuji@0
|
467 int i,fd;
|
yuuji@0
|
468 time_t ti = time (0);
|
yuuji@0
|
469 if (!(s = dummy_file (mbx,mailbox))) {
|
yuuji@0
|
470 sprintf (tmp,"Can't create %.80s: invalid name",mailbox);
|
yuuji@0
|
471 MM_LOG (tmp,ERROR);
|
yuuji@0
|
472 }
|
yuuji@0
|
473 /* create underlying file */
|
yuuji@0
|
474 else if (dummy_create_path (stream,s,get_dir_protection (mailbox))) {
|
yuuji@0
|
475 /* done if dir-only or whiner */
|
yuuji@0
|
476 if (((s = strrchr (s,'/')) && !s[1]) ||
|
yuuji@0
|
477 mail_parameters (NIL,GET_USERHASNOLIFE,NIL)) ret = T;
|
yuuji@0
|
478 else if ((fd = open (mbx,O_WRONLY,
|
yuuji@0
|
479 (long) mail_parameters (NIL,GET_MBXPROTECTION,NIL))) < 0) {
|
yuuji@0
|
480 sprintf (tmp,"Can't reopen mailbox node %.80s: %s",mbx,strerror (errno));
|
yuuji@0
|
481 MM_LOG (tmp,ERROR);
|
yuuji@0
|
482 unlink (mbx); /* delete the file */
|
yuuji@0
|
483 }
|
yuuji@0
|
484 else { /* initialize header */
|
yuuji@0
|
485 memset (tmp,'\0',MAILTMPLEN);
|
yuuji@0
|
486 sprintf (tmp,"%sFrom %s %sDate: ",mmdfhdr,pseudo_from,ctime (&ti));
|
yuuji@0
|
487 rfc822_date (s = tmp + strlen (tmp));
|
yuuji@0
|
488 sprintf (s += strlen (s), /* write the pseudo-header */
|
yuuji@0
|
489 "\nFrom: %s <%s@%s>\nSubject: %s\nX-IMAP: %010lu 0000000000",
|
yuuji@0
|
490 pseudo_name,pseudo_from,mylocalhost (),pseudo_subject,
|
yuuji@0
|
491 (unsigned long) ti);
|
yuuji@0
|
492 for (i = 0; i < NUSERFLAGS; ++i) if (default_user_flag (i))
|
yuuji@0
|
493 sprintf (s += strlen (s)," %s",default_user_flag (i));
|
yuuji@0
|
494 sprintf (s += strlen (s),"\nStatus: RO\n\n%s\n%s",pseudo_msg,mmdfhdr);
|
yuuji@0
|
495 if (write (fd,tmp,strlen (tmp)) > 0) ret = T;
|
yuuji@0
|
496 else {
|
yuuji@0
|
497 sprintf (tmp,"Can't initialize mailbox node %.80s: %s",mbx,
|
yuuji@0
|
498 strerror (errno));
|
yuuji@0
|
499 MM_LOG (tmp,ERROR);
|
yuuji@0
|
500 unlink (mbx); /* delete the file */
|
yuuji@0
|
501 }
|
yuuji@0
|
502 close (fd); /* close file */
|
yuuji@0
|
503 }
|
yuuji@0
|
504 }
|
yuuji@0
|
505 /* set proper protections */
|
yuuji@0
|
506 return ret ? set_mbx_protections (mailbox,mbx) : NIL;
|
yuuji@0
|
507 }
|
yuuji@0
|
508
|
yuuji@0
|
509 /* MMDF mail delete mailbox
|
yuuji@0
|
510 * Accepts: MAIL stream
|
yuuji@0
|
511 * mailbox name to delete
|
yuuji@0
|
512 * Returns: T on success, NIL on failure
|
yuuji@0
|
513 */
|
yuuji@0
|
514
|
yuuji@0
|
515 long mmdf_delete (MAILSTREAM *stream,char *mailbox)
|
yuuji@0
|
516 {
|
yuuji@0
|
517 return mmdf_rename (stream,mailbox,NIL);
|
yuuji@0
|
518 }
|
yuuji@0
|
519
|
yuuji@0
|
520
|
yuuji@0
|
521 /* MMDF mail rename mailbox
|
yuuji@0
|
522 * Accepts: MAIL stream
|
yuuji@0
|
523 * old mailbox name
|
yuuji@0
|
524 * new mailbox name (or NIL for delete)
|
yuuji@0
|
525 * Returns: T on success, NIL on failure
|
yuuji@0
|
526 */
|
yuuji@0
|
527
|
yuuji@0
|
528 long mmdf_rename (MAILSTREAM *stream,char *old,char *newname)
|
yuuji@0
|
529 {
|
yuuji@0
|
530 long ret = NIL;
|
yuuji@0
|
531 char c,*s = NIL;
|
yuuji@0
|
532 char tmp[MAILTMPLEN],file[MAILTMPLEN],lock[MAILTMPLEN];
|
yuuji@0
|
533 DOTLOCK lockx;
|
yuuji@0
|
534 int fd,ld;
|
yuuji@0
|
535 long i;
|
yuuji@0
|
536 struct stat sbuf;
|
yuuji@0
|
537 MM_CRITICAL (stream); /* get the c-client lock */
|
yuuji@0
|
538 if (!dummy_file (file,old) ||
|
yuuji@0
|
539 (newname && (!((s = mailboxfile (tmp,newname)) && *s) ||
|
yuuji@0
|
540 ((s = strrchr (tmp,'/')) && !s[1]))))
|
yuuji@0
|
541 sprintf (tmp,newname ?
|
yuuji@0
|
542 "Can't rename mailbox %.80s to %.80s: invalid name" :
|
yuuji@0
|
543 "Can't delete mailbox %.80s: invalid name",
|
yuuji@0
|
544 old,newname);
|
yuuji@0
|
545 /* lock out other c-clients */
|
yuuji@0
|
546 else if ((ld = lockname (lock,file,LOCK_EX|LOCK_NB,&i)) < 0)
|
yuuji@0
|
547 sprintf (tmp,"Mailbox %.80s is in use by another process",old);
|
yuuji@0
|
548
|
yuuji@0
|
549 else {
|
yuuji@0
|
550 if ((fd = mmdf_lock (file,O_RDWR,
|
yuuji@0
|
551 (long) mail_parameters (NIL,GET_MBXPROTECTION,NIL),
|
yuuji@0
|
552 &lockx,LOCK_EX)) < 0)
|
yuuji@0
|
553 sprintf (tmp,"Can't lock mailbox %.80s: %s",old,strerror (errno));
|
yuuji@0
|
554 else {
|
yuuji@0
|
555 if (newname) { /* want rename? */
|
yuuji@0
|
556 /* found superior to destination name? */
|
yuuji@0
|
557 if (s = strrchr (s,'/')) {
|
yuuji@0
|
558 c = *++s; /* remember first character of inferior */
|
yuuji@0
|
559 *s = '\0'; /* tie off to get just superior */
|
yuuji@0
|
560 /* name doesn't exist, create it */
|
yuuji@0
|
561 if ((stat (tmp,&sbuf) || ((sbuf.st_mode & S_IFMT) != S_IFDIR)) &&
|
yuuji@0
|
562 !dummy_create_path (stream,tmp,get_dir_protection (newname))) {
|
yuuji@0
|
563 mmdf_unlock (fd,NIL,&lockx);
|
yuuji@0
|
564 mmdf_unlock (ld,NIL,NIL);
|
yuuji@0
|
565 unlink (lock);
|
yuuji@0
|
566 MM_NOCRITICAL (stream);
|
yuuji@0
|
567 return ret; /* return success or failure */
|
yuuji@0
|
568 }
|
yuuji@0
|
569 *s = c; /* restore full name */
|
yuuji@0
|
570 }
|
yuuji@0
|
571 if (rename (file,tmp))
|
yuuji@0
|
572 sprintf (tmp,"Can't rename mailbox %.80s to %.80s: %s",old,newname,
|
yuuji@0
|
573 strerror (errno));
|
yuuji@0
|
574 else ret = T; /* set success */
|
yuuji@0
|
575 }
|
yuuji@0
|
576 else if (unlink (file))
|
yuuji@0
|
577 sprintf (tmp,"Can't delete mailbox %.80s: %s",old,strerror (errno));
|
yuuji@0
|
578 else ret = T; /* set success */
|
yuuji@0
|
579 mmdf_unlock (fd,NIL,&lockx);
|
yuuji@0
|
580 }
|
yuuji@0
|
581 mmdf_unlock (ld,NIL,NIL); /* flush the lock */
|
yuuji@0
|
582 unlink (lock);
|
yuuji@0
|
583 }
|
yuuji@0
|
584 MM_NOCRITICAL (stream); /* no longer critical */
|
yuuji@0
|
585 if (!ret) MM_LOG (tmp,ERROR); /* log error */
|
yuuji@0
|
586 return ret; /* return success or failure */
|
yuuji@0
|
587 }
|
yuuji@0
|
588
|
yuuji@0
|
589 /* MMDF mail open
|
yuuji@0
|
590 * Accepts: Stream to open
|
yuuji@0
|
591 * Returns: Stream on success, NIL on failure
|
yuuji@0
|
592 */
|
yuuji@0
|
593
|
yuuji@0
|
594 MAILSTREAM *mmdf_open (MAILSTREAM *stream)
|
yuuji@0
|
595 {
|
yuuji@0
|
596 long i;
|
yuuji@0
|
597 int fd;
|
yuuji@0
|
598 char tmp[MAILTMPLEN];
|
yuuji@0
|
599 DOTLOCK lock;
|
yuuji@0
|
600 long retry;
|
yuuji@0
|
601 /* return prototype for OP_PROTOTYPE call */
|
yuuji@0
|
602 if (!stream) return user_flags (&mmdfproto);
|
yuuji@0
|
603 retry = stream->silent ? 1 : KODRETRY;
|
yuuji@0
|
604 if (stream->local) fatal ("mmdf recycle stream");
|
yuuji@0
|
605 stream->local = memset (fs_get (sizeof (MMDFLOCAL)),0,sizeof (MMDFLOCAL));
|
yuuji@0
|
606 /* note if an INBOX or not */
|
yuuji@0
|
607 stream->inbox = !compare_cstring (stream->mailbox,"INBOX");
|
yuuji@0
|
608 /* canonicalize the stream mailbox name */
|
yuuji@0
|
609 if (!dummy_file (tmp,stream->mailbox)) {
|
yuuji@0
|
610 sprintf (tmp,"Can't open - invalid name: %.80s",stream->mailbox);
|
yuuji@0
|
611 MM_LOG (tmp,ERROR);
|
yuuji@0
|
612 return NIL;
|
yuuji@0
|
613 }
|
yuuji@0
|
614 /* flush old name */
|
yuuji@0
|
615 fs_give ((void **) &stream->mailbox);
|
yuuji@0
|
616 /* save canonical name */
|
yuuji@0
|
617 stream->mailbox = cpystr (tmp);
|
yuuji@0
|
618 LOCAL->fd = LOCAL->ld = -1; /* no file or state locking yet */
|
yuuji@0
|
619 LOCAL->buf = (char *) fs_get (CHUNKSIZE);
|
yuuji@0
|
620 LOCAL->buflen = CHUNKSIZE - 1;
|
yuuji@0
|
621 LOCAL->text.data = (unsigned char *) fs_get (CHUNKSIZE);
|
yuuji@0
|
622 LOCAL->text.size = CHUNKSIZE - 1;
|
yuuji@0
|
623 LOCAL->linebuf = (char *) fs_get (CHUNKSIZE);
|
yuuji@0
|
624 LOCAL->linebuflen = CHUNKSIZE - 1;
|
yuuji@0
|
625 stream->sequence++; /* bump sequence number */
|
yuuji@0
|
626
|
yuuji@0
|
627 /* make lock for read/write access */
|
yuuji@0
|
628 if (!stream->rdonly) while (retry) {
|
yuuji@0
|
629 /* try to lock file */
|
yuuji@0
|
630 if ((fd = lockname (tmp,stream->mailbox,LOCK_EX|LOCK_NB,&i)) < 0) {
|
yuuji@0
|
631 /* suppressing kiss-of-death? */
|
yuuji@0
|
632 if (stream->nokod) retry = 0;
|
yuuji@0
|
633 /* no, first time through? */
|
yuuji@0
|
634 else if (retry-- == KODRETRY) {
|
yuuji@0
|
635 /* learned other guy's PID and can signal? */
|
yuuji@0
|
636 if (i && !kill ((int) i,SIGUSR2)) {
|
yuuji@0
|
637 sprintf (tmp,"Trying to get mailbox lock from process %ld",i);
|
yuuji@0
|
638 MM_LOG (tmp,WARN);
|
yuuji@0
|
639 }
|
yuuji@0
|
640 else retry = 0; /* give up */
|
yuuji@0
|
641 }
|
yuuji@0
|
642 if (!stream->silent) { /* nothing if silent stream */
|
yuuji@0
|
643 if (retry) sleep (1); /* wait a second before trying again */
|
yuuji@0
|
644 else MM_LOG ("Mailbox is open by another process, access is readonly",
|
yuuji@0
|
645 WARN);
|
yuuji@0
|
646 }
|
yuuji@0
|
647 }
|
yuuji@0
|
648 else { /* got the lock, nobody else can alter state */
|
yuuji@0
|
649 LOCAL->ld = fd; /* note lock's fd and name */
|
yuuji@0
|
650 LOCAL->lname = cpystr (tmp);
|
yuuji@0
|
651 /* make sure mode OK (don't use fchmod()) */
|
yuuji@0
|
652 chmod (LOCAL->lname,(long) mail_parameters (NIL,GET_LOCKPROTECTION,NIL));
|
yuuji@0
|
653 if (stream->silent) i = 0;/* silent streams won't accept KOD */
|
yuuji@0
|
654 else { /* note our PID in the lock */
|
yuuji@0
|
655 sprintf (tmp,"%d",getpid ());
|
yuuji@0
|
656 write (fd,tmp,(i = strlen (tmp))+1);
|
yuuji@0
|
657 }
|
yuuji@0
|
658 ftruncate (fd,i); /* make sure tied off */
|
yuuji@0
|
659 fsync (fd); /* make sure it's available */
|
yuuji@0
|
660 retry = 0; /* no more need to try */
|
yuuji@0
|
661 }
|
yuuji@0
|
662 }
|
yuuji@0
|
663
|
yuuji@0
|
664 /* parse mailbox */
|
yuuji@0
|
665 stream->nmsgs = stream->recent = 0;
|
yuuji@0
|
666 /* will we be able to get write access? */
|
yuuji@0
|
667 if ((LOCAL->ld >= 0) && access (stream->mailbox,W_OK) && (errno == EACCES)) {
|
yuuji@0
|
668 MM_LOG ("Can't get write access to mailbox, access is readonly",WARN);
|
yuuji@0
|
669 flock (LOCAL->ld,LOCK_UN); /* release the lock */
|
yuuji@0
|
670 close (LOCAL->ld); /* close the lock file */
|
yuuji@0
|
671 LOCAL->ld = -1; /* no more lock fd */
|
yuuji@0
|
672 unlink (LOCAL->lname); /* delete it */
|
yuuji@0
|
673 }
|
yuuji@0
|
674 /* reset UID validity */
|
yuuji@0
|
675 stream->uid_validity = stream->uid_last = 0;
|
yuuji@0
|
676 if (stream->silent && !stream->rdonly && (LOCAL->ld < 0))
|
yuuji@0
|
677 mmdf_abort (stream); /* abort if can't get RW silent stream */
|
yuuji@0
|
678 /* parse mailbox */
|
yuuji@0
|
679 else if (mmdf_parse (stream,&lock,LOCK_SH)) {
|
yuuji@0
|
680 mmdf_unlock (LOCAL->fd,stream,&lock);
|
yuuji@0
|
681 mail_unlock (stream);
|
yuuji@0
|
682 MM_NOCRITICAL (stream); /* done with critical */
|
yuuji@0
|
683 }
|
yuuji@0
|
684 if (!LOCAL) return NIL; /* failure if stream died */
|
yuuji@0
|
685 /* make sure upper level knows readonly */
|
yuuji@0
|
686 stream->rdonly = (LOCAL->ld < 0);
|
yuuji@0
|
687 /* notify about empty mailbox */
|
yuuji@0
|
688 if (!(stream->nmsgs || stream->silent)) MM_LOG ("Mailbox is empty",NIL);
|
yuuji@0
|
689 if (!stream->rdonly) { /* flags stick if readwrite */
|
yuuji@0
|
690 stream->perm_seen = stream->perm_deleted =
|
yuuji@0
|
691 stream->perm_flagged = stream->perm_answered = stream->perm_draft = T;
|
yuuji@0
|
692 if (!stream->uid_nosticky) {/* users with lives get permanent keywords */
|
yuuji@0
|
693 stream->perm_user_flags = 0xffffffff;
|
yuuji@0
|
694 /* and maybe can create them too! */
|
yuuji@0
|
695 stream->kwd_create = stream->user_flags[NUSERFLAGS-1] ? NIL : T;
|
yuuji@0
|
696 }
|
yuuji@0
|
697 }
|
yuuji@0
|
698 return stream; /* return stream alive to caller */
|
yuuji@0
|
699 }
|
yuuji@0
|
700
|
yuuji@0
|
701
|
yuuji@0
|
702 /* MMDF mail close
|
yuuji@0
|
703 * Accepts: MAIL stream
|
yuuji@0
|
704 * close options
|
yuuji@0
|
705 */
|
yuuji@0
|
706
|
yuuji@0
|
707 void mmdf_close (MAILSTREAM *stream,long options)
|
yuuji@0
|
708 {
|
yuuji@0
|
709 int silent = stream->silent;
|
yuuji@0
|
710 stream->silent = T; /* go silent */
|
yuuji@0
|
711 /* expunge if requested */
|
yuuji@0
|
712 if (options & CL_EXPUNGE) mmdf_expunge (stream,NIL,NIL);
|
yuuji@0
|
713 /* else dump final checkpoint */
|
yuuji@0
|
714 else if (LOCAL->dirty) mmdf_check (stream);
|
yuuji@0
|
715 stream->silent = silent; /* restore old silence state */
|
yuuji@0
|
716 mmdf_abort (stream); /* now punt the file and local data */
|
yuuji@0
|
717 }
|
yuuji@0
|
718
|
yuuji@0
|
719 /* MMDF mail fetch message header
|
yuuji@0
|
720 * Accepts: MAIL stream
|
yuuji@0
|
721 * message # to fetch
|
yuuji@0
|
722 * pointer to returned header text length
|
yuuji@0
|
723 * option flags
|
yuuji@0
|
724 * Returns: message header in RFC822 format
|
yuuji@0
|
725 */
|
yuuji@0
|
726
|
yuuji@0
|
727 /* lines to filter from header */
|
yuuji@0
|
728 static STRINGLIST *mmdf_hlines = NIL;
|
yuuji@0
|
729
|
yuuji@0
|
730 char *mmdf_header (MAILSTREAM *stream,unsigned long msgno,
|
yuuji@0
|
731 unsigned long *length,long flags)
|
yuuji@0
|
732 {
|
yuuji@0
|
733 MESSAGECACHE *elt;
|
yuuji@0
|
734 unsigned char *s,*t,*tl;
|
yuuji@0
|
735 *length = 0; /* default to empty */
|
yuuji@0
|
736 if (flags & FT_UID) return "";/* UID call "impossible" */
|
yuuji@0
|
737 elt = mail_elt (stream,msgno);/* get cache */
|
yuuji@0
|
738 if (!mmdf_hlines) { /* once only code */
|
yuuji@0
|
739 STRINGLIST *lines = mmdf_hlines = mail_newstringlist ();
|
yuuji@0
|
740 lines->text.size = strlen ((char *) (lines->text.data =
|
yuuji@0
|
741 (unsigned char *) "Status"));
|
yuuji@0
|
742 lines = lines->next = mail_newstringlist ();
|
yuuji@0
|
743 lines->text.size = strlen ((char *) (lines->text.data =
|
yuuji@0
|
744 (unsigned char *) "X-Status"));
|
yuuji@0
|
745 lines = lines->next = mail_newstringlist ();
|
yuuji@0
|
746 lines->text.size = strlen ((char *) (lines->text.data =
|
yuuji@0
|
747 (unsigned char *) "X-Keywords"));
|
yuuji@0
|
748 lines = lines->next = mail_newstringlist ();
|
yuuji@0
|
749 lines->text.size = strlen ((char *) (lines->text.data =
|
yuuji@0
|
750 (unsigned char *) "X-UID"));
|
yuuji@0
|
751 lines = lines->next = mail_newstringlist ();
|
yuuji@0
|
752 lines->text.size = strlen ((char *) (lines->text.data =
|
yuuji@0
|
753 (unsigned char *) "X-IMAP"));
|
yuuji@0
|
754 lines = lines->next = mail_newstringlist ();
|
yuuji@0
|
755 lines->text.size = strlen ((char *) (lines->text.data =
|
yuuji@0
|
756 (unsigned char *) "X-IMAPbase"));
|
yuuji@0
|
757 }
|
yuuji@0
|
758 /* go to header position */
|
yuuji@0
|
759 lseek (LOCAL->fd,elt->private.special.offset +
|
yuuji@0
|
760 elt->private.msg.header.offset,L_SET);
|
yuuji@0
|
761
|
yuuji@0
|
762 if (flags & FT_INTERNAL) { /* initial data OK? */
|
yuuji@0
|
763 if (elt->private.msg.header.text.size > LOCAL->buflen) {
|
yuuji@0
|
764 fs_give ((void **) &LOCAL->buf);
|
yuuji@0
|
765 LOCAL->buf = (char *) fs_get ((LOCAL->buflen =
|
yuuji@0
|
766 elt->private.msg.header.text.size) + 1);
|
yuuji@0
|
767 }
|
yuuji@0
|
768 /* read message */
|
yuuji@0
|
769 read (LOCAL->fd,LOCAL->buf,elt->private.msg.header.text.size);
|
yuuji@0
|
770 /* got text, tie off string */
|
yuuji@0
|
771 LOCAL->buf[*length = elt->private.msg.header.text.size] = '\0';
|
yuuji@0
|
772 /* squeeze out CRs (in case from PC) */
|
yuuji@0
|
773 for (s = t = LOCAL->buf,tl = LOCAL->buf + *length; t < tl; t++)
|
yuuji@0
|
774 if (*t != '\r') *s++ = *t;
|
yuuji@0
|
775 *s = '\0';
|
yuuji@0
|
776 *length = s - LOCAL->buf; /* adjust length */
|
yuuji@0
|
777 }
|
yuuji@0
|
778 else { /* need to make a CRLF version */
|
yuuji@0
|
779 read (LOCAL->fd,s = (char *) fs_get (elt->private.msg.header.text.size+1),
|
yuuji@0
|
780 elt->private.msg.header.text.size);
|
yuuji@0
|
781 /* tie off string, and convert to CRLF */
|
yuuji@0
|
782 s[elt->private.msg.header.text.size] = '\0';
|
yuuji@0
|
783 *length = strcrlfcpy (&LOCAL->buf,&LOCAL->buflen,s,
|
yuuji@0
|
784 elt->private.msg.header.text.size);
|
yuuji@0
|
785 fs_give ((void **) &s); /* free readin buffer */
|
yuuji@0
|
786 /* squeeze out spurious CRs */
|
yuuji@0
|
787 for (s = t = LOCAL->buf,tl = LOCAL->buf + *length; t < tl; t++)
|
yuuji@0
|
788 if ((*t != '\r') || (t[1] == '\n')) *s++ = *t;
|
yuuji@0
|
789 *s = '\0';
|
yuuji@0
|
790 *length = s - LOCAL->buf; /* adjust length */
|
yuuji@0
|
791 }
|
yuuji@0
|
792 *length = mail_filter (LOCAL->buf,*length,mmdf_hlines,FT_NOT);
|
yuuji@0
|
793 return (char *) LOCAL->buf; /* return processed copy */
|
yuuji@0
|
794 }
|
yuuji@0
|
795
|
yuuji@0
|
796 /* MMDF mail fetch message text
|
yuuji@0
|
797 * Accepts: MAIL stream
|
yuuji@0
|
798 * message # to fetch
|
yuuji@0
|
799 * pointer to returned stringstruct
|
yuuji@0
|
800 * option flags
|
yuuji@0
|
801 * Returns: T on success, NIL if failure
|
yuuji@0
|
802 */
|
yuuji@0
|
803
|
yuuji@0
|
804 long mmdf_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags)
|
yuuji@0
|
805 {
|
yuuji@0
|
806 char *s;
|
yuuji@0
|
807 unsigned long i;
|
yuuji@0
|
808 MESSAGECACHE *elt;
|
yuuji@0
|
809 /* UID call "impossible" */
|
yuuji@0
|
810 if (flags & FT_UID) return NIL;
|
yuuji@0
|
811 elt = mail_elt (stream,msgno);/* get cache element */
|
yuuji@0
|
812 /* if message not seen */
|
yuuji@0
|
813 if (!(flags & FT_PEEK) && !elt->seen) {
|
yuuji@0
|
814 /* mark message seen and dirty */
|
yuuji@0
|
815 elt->seen = elt->private.dirty = LOCAL->dirty = T;
|
yuuji@0
|
816 MM_FLAGS (stream,msgno);
|
yuuji@0
|
817 }
|
yuuji@0
|
818 s = mmdf_text_work (stream,elt,&i,flags);
|
yuuji@0
|
819 INIT (bs,mail_string,s,i); /* set up stringstruct */
|
yuuji@0
|
820 return T; /* success */
|
yuuji@0
|
821 }
|
yuuji@0
|
822
|
yuuji@0
|
823 /* MMDF mail fetch message text worker routine
|
yuuji@0
|
824 * Accepts: MAIL stream
|
yuuji@0
|
825 * message cache element
|
yuuji@0
|
826 * pointer to returned header text length
|
yuuji@0
|
827 * option flags
|
yuuji@0
|
828 */
|
yuuji@0
|
829
|
yuuji@0
|
830 char *mmdf_text_work (MAILSTREAM *stream,MESSAGECACHE *elt,
|
yuuji@0
|
831 unsigned long *length,long flags)
|
yuuji@0
|
832 {
|
yuuji@0
|
833 FDDATA d;
|
yuuji@0
|
834 STRING bs;
|
yuuji@0
|
835 unsigned char c,*s,*t,*tl,tmp[CHUNKSIZE];
|
yuuji@0
|
836 /* go to text position */
|
yuuji@0
|
837 lseek (LOCAL->fd,elt->private.special.offset +
|
yuuji@0
|
838 elt->private.msg.text.offset,L_SET);
|
yuuji@0
|
839 if (flags & FT_INTERNAL) { /* initial data OK? */
|
yuuji@0
|
840 if (elt->private.msg.text.text.size > LOCAL->buflen) {
|
yuuji@0
|
841 fs_give ((void **) &LOCAL->buf);
|
yuuji@0
|
842 LOCAL->buf = (char *) fs_get ((LOCAL->buflen =
|
yuuji@0
|
843 elt->private.msg.text.text.size) + 1);
|
yuuji@0
|
844 }
|
yuuji@0
|
845 /* read message */
|
yuuji@0
|
846 read (LOCAL->fd,LOCAL->buf,elt->private.msg.text.text.size);
|
yuuji@0
|
847 /* got text, tie off string */
|
yuuji@0
|
848 LOCAL->buf[*length = elt->private.msg.text.text.size] = '\0';
|
yuuji@0
|
849 /* squeeze out CRs (in case from PC) */
|
yuuji@0
|
850 for (s = t = LOCAL->buf,tl = LOCAL->buf + *length; t < tl; t++)
|
yuuji@0
|
851 if (*t != '\r') *s++ = *t;
|
yuuji@0
|
852 *s = '\0';
|
yuuji@0
|
853 *length = s - LOCAL->buf; /* adjust length */
|
yuuji@0
|
854 return (char *) LOCAL->buf;
|
yuuji@0
|
855 }
|
yuuji@0
|
856
|
yuuji@0
|
857 /* have it cached already? */
|
yuuji@0
|
858 if (elt->private.uid != LOCAL->uid) {
|
yuuji@0
|
859 /* not cached, cache it now */
|
yuuji@0
|
860 LOCAL->uid = elt->private.uid;
|
yuuji@0
|
861 /* is buffer big enough? */
|
yuuji@0
|
862 if (elt->rfc822_size > LOCAL->text.size) {
|
yuuji@0
|
863 /* excessively conservative, but the right thing is too hard to do */
|
yuuji@0
|
864 fs_give ((void **) &LOCAL->text.data);
|
yuuji@0
|
865 LOCAL->text.data = (unsigned char *)
|
yuuji@0
|
866 fs_get ((LOCAL->text.size = elt->rfc822_size) + 1);
|
yuuji@0
|
867 }
|
yuuji@0
|
868 d.fd = LOCAL->fd; /* yes, set up file descriptor */
|
yuuji@0
|
869 d.pos = elt->private.special.offset + elt->private.msg.text.offset;
|
yuuji@0
|
870 d.chunk = tmp; /* initial buffer chunk */
|
yuuji@0
|
871 d.chunksize = CHUNKSIZE; /* file chunk size */
|
yuuji@0
|
872 INIT (&bs,fd_string,&d,elt->private.msg.text.text.size);
|
yuuji@0
|
873 for (s = (char *) LOCAL->text.data; SIZE (&bs);) switch (c = SNX (&bs)) {
|
yuuji@0
|
874 case '\r': /* carriage return seen */
|
yuuji@0
|
875 break;
|
yuuji@0
|
876 case '\n':
|
yuuji@0
|
877 *s++ = '\r'; /* insert a CR */
|
yuuji@0
|
878 default:
|
yuuji@0
|
879 *s++ = c; /* copy characters */
|
yuuji@0
|
880 }
|
yuuji@0
|
881 *s = '\0'; /* tie off buffer */
|
yuuji@0
|
882 /* calculate length of cached data */
|
yuuji@0
|
883 LOCAL->textlen = s - LOCAL->text.data;
|
yuuji@0
|
884 }
|
yuuji@0
|
885 *length = LOCAL->textlen; /* return from cache */
|
yuuji@0
|
886 return (char *) LOCAL->text.data;
|
yuuji@0
|
887 }
|
yuuji@0
|
888
|
yuuji@0
|
889 /* MMDF per-message modify flag
|
yuuji@0
|
890 * Accepts: MAIL stream
|
yuuji@0
|
891 * message cache element
|
yuuji@0
|
892 */
|
yuuji@0
|
893
|
yuuji@0
|
894 void mmdf_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt)
|
yuuji@0
|
895 {
|
yuuji@0
|
896 /* only after finishing */
|
yuuji@0
|
897 if (elt->valid) elt->private.dirty = LOCAL->dirty = T;
|
yuuji@0
|
898 }
|
yuuji@0
|
899
|
yuuji@0
|
900
|
yuuji@0
|
901 /* MMDF mail ping mailbox
|
yuuji@0
|
902 * Accepts: MAIL stream
|
yuuji@0
|
903 * Returns: T if stream alive, else NIL
|
yuuji@0
|
904 */
|
yuuji@0
|
905
|
yuuji@0
|
906 long mmdf_ping (MAILSTREAM *stream)
|
yuuji@0
|
907 {
|
yuuji@0
|
908 DOTLOCK lock;
|
yuuji@0
|
909 struct stat sbuf;
|
yuuji@0
|
910 long reparse;
|
yuuji@0
|
911 /* big no-op if not readwrite */
|
yuuji@0
|
912 if (LOCAL && (LOCAL->ld >= 0) && !stream->lock) {
|
yuuji@0
|
913 if (stream->rdonly) { /* does he want to give up readwrite? */
|
yuuji@0
|
914 /* checkpoint if we changed something */
|
yuuji@0
|
915 if (LOCAL->dirty) mmdf_check (stream);
|
yuuji@0
|
916 flock (LOCAL->ld,LOCK_UN);/* release readwrite lock */
|
yuuji@0
|
917 close (LOCAL->ld); /* close the readwrite lock file */
|
yuuji@0
|
918 LOCAL->ld = -1; /* no more readwrite lock fd */
|
yuuji@0
|
919 unlink (LOCAL->lname); /* delete the readwrite lock file */
|
yuuji@0
|
920 }
|
yuuji@0
|
921 else { /* see if need to reparse */
|
yuuji@0
|
922 if (!(reparse = (long) mail_parameters (NIL,GET_NETFSSTATBUG,NIL))) {
|
yuuji@0
|
923 /* get current mailbox size */
|
yuuji@0
|
924 if (LOCAL->fd >= 0) fstat (LOCAL->fd,&sbuf);
|
yuuji@0
|
925 else if (stat (stream->mailbox,&sbuf)) {
|
yuuji@0
|
926 sprintf (LOCAL->buf,"Mailbox stat failed, aborted: %s",
|
yuuji@0
|
927 strerror (errno));
|
yuuji@0
|
928 MM_LOG (LOCAL->buf,ERROR);
|
yuuji@0
|
929 mmdf_abort (stream);
|
yuuji@0
|
930 return NIL;
|
yuuji@0
|
931 }
|
yuuji@0
|
932 reparse = (sbuf.st_size != LOCAL->filesize);
|
yuuji@0
|
933 }
|
yuuji@0
|
934 /* parse if mailbox changed */
|
yuuji@0
|
935 if ((LOCAL->ddirty || reparse) && mmdf_parse (stream,&lock,LOCK_EX)) {
|
yuuji@0
|
936 /* force checkpoint if double-dirty */
|
yuuji@0
|
937 if (LOCAL->ddirty) mmdf_rewrite (stream,NIL,&lock,NIL);
|
yuuji@0
|
938 /* unlock mailbox */
|
yuuji@0
|
939 else mmdf_unlock (LOCAL->fd,stream,&lock);
|
yuuji@0
|
940 mail_unlock (stream); /* and stream */
|
yuuji@0
|
941 /* done with critical */
|
yuuji@0
|
942 MM_NOCRITICAL (stream);
|
yuuji@0
|
943 }
|
yuuji@0
|
944 }
|
yuuji@0
|
945 }
|
yuuji@0
|
946 return LOCAL ? LONGT : NIL; /* return if still alive */
|
yuuji@0
|
947 }
|
yuuji@0
|
948
|
yuuji@0
|
949 /* MMDF mail check mailbox
|
yuuji@0
|
950 * Accepts: MAIL stream
|
yuuji@0
|
951 */
|
yuuji@0
|
952
|
yuuji@0
|
953 void mmdf_check (MAILSTREAM *stream)
|
yuuji@0
|
954 {
|
yuuji@0
|
955 DOTLOCK lock;
|
yuuji@0
|
956 /* parse and lock mailbox */
|
yuuji@0
|
957 if (LOCAL && (LOCAL->ld >= 0) && !stream->lock &&
|
yuuji@0
|
958 mmdf_parse (stream,&lock,LOCK_EX)) {
|
yuuji@0
|
959 /* any unsaved changes? */
|
yuuji@0
|
960 if (LOCAL->dirty && mmdf_rewrite (stream,NIL,&lock,NIL)) {
|
yuuji@0
|
961 if (!stream->silent) MM_LOG ("Checkpoint completed",NIL);
|
yuuji@0
|
962 }
|
yuuji@0
|
963 /* no checkpoint needed, just unlock */
|
yuuji@0
|
964 else mmdf_unlock (LOCAL->fd,stream,&lock);
|
yuuji@0
|
965 mail_unlock (stream); /* unlock the stream */
|
yuuji@0
|
966 MM_NOCRITICAL (stream); /* done with critical */
|
yuuji@0
|
967 }
|
yuuji@0
|
968 }
|
yuuji@0
|
969
|
yuuji@0
|
970
|
yuuji@0
|
971 /* MMDF mail expunge mailbox
|
yuuji@0
|
972 * Accepts: MAIL stream
|
yuuji@0
|
973 * sequence to expunge if non-NIL
|
yuuji@0
|
974 * expunge options
|
yuuji@0
|
975 * Returns: T, always
|
yuuji@0
|
976 */
|
yuuji@0
|
977
|
yuuji@0
|
978 long mmdf_expunge (MAILSTREAM *stream,char *sequence,long options)
|
yuuji@0
|
979 {
|
yuuji@0
|
980 long ret;
|
yuuji@0
|
981 unsigned long i;
|
yuuji@0
|
982 DOTLOCK lock;
|
yuuji@0
|
983 char *msg = NIL;
|
yuuji@0
|
984 if (ret = (sequence ? ((options & EX_UID) ?
|
yuuji@0
|
985 mail_uid_sequence (stream,sequence) :
|
yuuji@0
|
986 mail_sequence (stream,sequence)) : LONGT) &&
|
yuuji@0
|
987 LOCAL && (LOCAL->ld >= 0) && !stream->lock &&
|
yuuji@0
|
988 mmdf_parse (stream,&lock,LOCK_EX)) {
|
yuuji@0
|
989 /* check expunged messages if not dirty */
|
yuuji@0
|
990 for (i = 1; !LOCAL->dirty && (i <= stream->nmsgs); i++) {
|
yuuji@0
|
991 MESSAGECACHE *elt = mail_elt (stream,i);
|
yuuji@0
|
992 if (mail_elt (stream,i)->deleted) LOCAL->dirty = T;
|
yuuji@0
|
993 }
|
yuuji@0
|
994 if (!LOCAL->dirty) { /* not dirty and no expunged messages */
|
yuuji@0
|
995 mmdf_unlock (LOCAL->fd,stream,&lock);
|
yuuji@0
|
996 msg = "No messages deleted, so no update needed";
|
yuuji@0
|
997 }
|
yuuji@0
|
998 else if (mmdf_rewrite (stream,&i,&lock,sequence ? LONGT : NIL)) {
|
yuuji@0
|
999 if (i) sprintf (msg = LOCAL->buf,"Expunged %lu messages",i);
|
yuuji@0
|
1000 else msg = "Mailbox checkpointed, but no messages expunged";
|
yuuji@0
|
1001 }
|
yuuji@0
|
1002 /* rewrite failed */
|
yuuji@0
|
1003 else mmdf_unlock (LOCAL->fd,stream,&lock);
|
yuuji@0
|
1004 mail_unlock (stream); /* unlock the stream */
|
yuuji@0
|
1005 MM_NOCRITICAL (stream); /* done with critical */
|
yuuji@0
|
1006 if (msg && !stream->silent) MM_LOG (msg,NIL);
|
yuuji@0
|
1007 }
|
yuuji@0
|
1008 else if (!stream->silent)
|
yuuji@0
|
1009 MM_LOG ("Expunge ignored on readonly mailbox",WARN);
|
yuuji@0
|
1010 return ret;
|
yuuji@0
|
1011 }
|
yuuji@0
|
1012
|
yuuji@0
|
1013 /* MMDF mail copy message(s)
|
yuuji@0
|
1014 * Accepts: MAIL stream
|
yuuji@0
|
1015 * sequence
|
yuuji@0
|
1016 * destination mailbox
|
yuuji@0
|
1017 * copy options
|
yuuji@0
|
1018 * Returns: T if copy successful, else NIL
|
yuuji@0
|
1019 */
|
yuuji@0
|
1020
|
yuuji@0
|
1021 long mmdf_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options)
|
yuuji@0
|
1022 {
|
yuuji@0
|
1023 struct stat sbuf;
|
yuuji@0
|
1024 int fd;
|
yuuji@0
|
1025 char *s,file[MAILTMPLEN];
|
yuuji@0
|
1026 DOTLOCK lock;
|
yuuji@0
|
1027 time_t tp[2];
|
yuuji@0
|
1028 unsigned long i,j;
|
yuuji@0
|
1029 MESSAGECACHE *elt;
|
yuuji@0
|
1030 long ret = T;
|
yuuji@0
|
1031 mailproxycopy_t pc =
|
yuuji@0
|
1032 (mailproxycopy_t) mail_parameters (stream,GET_MAILPROXYCOPY,NIL);
|
yuuji@0
|
1033 copyuid_t cu = (copyuid_t) (mail_parameters (NIL,GET_USERHASNOLIFE,NIL) ?
|
yuuji@0
|
1034 NIL : mail_parameters (NIL,GET_COPYUID,NIL));
|
yuuji@0
|
1035 SEARCHSET *source = cu ? mail_newsearchset () : NIL;
|
yuuji@0
|
1036 SEARCHSET *dest = cu ? mail_newsearchset () : NIL;
|
yuuji@0
|
1037 MAILSTREAM *tstream = NIL;
|
yuuji@0
|
1038 if (!((options & CP_UID) ? mail_uid_sequence (stream,sequence) :
|
yuuji@0
|
1039 mail_sequence (stream,sequence))) return NIL;
|
yuuji@0
|
1040 /* make sure destination is valid */
|
yuuji@0
|
1041 if (!(mmdf_valid (mailbox) || !errno))
|
yuuji@0
|
1042 switch (errno) {
|
yuuji@0
|
1043 case ENOENT: /* no such file? */
|
yuuji@0
|
1044 if (compare_cstring (mailbox,"INBOX")) {
|
yuuji@0
|
1045 MM_NOTIFY (stream,"[TRYCREATE] Must create mailbox before copy",NIL);
|
yuuji@0
|
1046 return NIL;
|
yuuji@0
|
1047 }
|
yuuji@0
|
1048 if (pc) return (*pc) (stream,sequence,mailbox,options);
|
yuuji@0
|
1049 mmdf_create (NIL,"INBOX");/* create empty INBOX */
|
yuuji@0
|
1050 case EACCES: /* file protected */
|
yuuji@0
|
1051 sprintf (LOCAL->buf,"Can't access destination: %.80s",mailbox);
|
yuuji@0
|
1052 MM_LOG (LOCAL->buf,ERROR);
|
yuuji@0
|
1053 return NIL;
|
yuuji@0
|
1054 case EINVAL:
|
yuuji@0
|
1055 if (pc) return (*pc) (stream,sequence,mailbox,options);
|
yuuji@0
|
1056 sprintf (LOCAL->buf,"Invalid MMDF-format mailbox name: %.80s",mailbox);
|
yuuji@0
|
1057 MM_LOG (LOCAL->buf,ERROR);
|
yuuji@0
|
1058 return NIL;
|
yuuji@0
|
1059 default:
|
yuuji@0
|
1060 if (pc) return (*pc) (stream,sequence,mailbox,options);
|
yuuji@0
|
1061 sprintf (LOCAL->buf,"Not a MMDF-format mailbox: %.80s",mailbox);
|
yuuji@0
|
1062 MM_LOG (LOCAL->buf,ERROR);
|
yuuji@0
|
1063 return NIL;
|
yuuji@0
|
1064 }
|
yuuji@0
|
1065
|
yuuji@0
|
1066 /* try to open rewrite for UIDPLUS */
|
yuuji@0
|
1067 if ((tstream = mail_open_work (&mmdfdriver,NIL,mailbox,
|
yuuji@0
|
1068 OP_SILENT|OP_NOKOD)) && tstream->rdonly)
|
yuuji@0
|
1069 tstream = mail_close (tstream);
|
yuuji@0
|
1070 if (cu && !tstream) { /* wanted a COPYUID? */
|
yuuji@0
|
1071 sprintf (LOCAL->buf,"Unable to write-open mailbox for COPYUID: %.80s",
|
yuuji@0
|
1072 mailbox);
|
yuuji@0
|
1073 MM_LOG (LOCAL->buf,WARN);
|
yuuji@0
|
1074 cu = NIL; /* don't try to do COPYUID */
|
yuuji@0
|
1075 }
|
yuuji@0
|
1076 LOCAL->buf[0] = '\0';
|
yuuji@0
|
1077 MM_CRITICAL (stream); /* go critical */
|
yuuji@0
|
1078 if ((fd = mmdf_lock (dummy_file (file,mailbox),O_WRONLY|O_APPEND,
|
yuuji@0
|
1079 (long) mail_parameters (NIL,GET_MBXPROTECTION,NIL),
|
yuuji@0
|
1080 &lock,LOCK_EX)) < 0) {
|
yuuji@0
|
1081 MM_NOCRITICAL (stream); /* done with critical */
|
yuuji@0
|
1082 sprintf (LOCAL->buf,"Can't open destination mailbox: %s",strerror (errno));
|
yuuji@0
|
1083 MM_LOG (LOCAL->buf,ERROR); /* log the error */
|
yuuji@0
|
1084 return NIL; /* failed */
|
yuuji@0
|
1085 }
|
yuuji@0
|
1086 fstat (fd,&sbuf); /* get current file size */
|
yuuji@0
|
1087 /* write all requested messages to mailbox */
|
yuuji@0
|
1088 for (i = 1; ret && (i <= stream->nmsgs); i++)
|
yuuji@0
|
1089 if ((elt = mail_elt (stream,i))->sequence) {
|
yuuji@0
|
1090 lseek (LOCAL->fd,elt->private.special.offset,L_SET);
|
yuuji@0
|
1091 read (LOCAL->fd,LOCAL->buf,elt->private.special.text.size);
|
yuuji@0
|
1092 if (write (fd,LOCAL->buf,elt->private.special.text.size) < 0) ret = NIL;
|
yuuji@0
|
1093 else { /* internal header succeeded */
|
yuuji@0
|
1094 s = mmdf_header (stream,i,&j,FT_INTERNAL);
|
yuuji@0
|
1095 /* header size, sans trailing newline */
|
yuuji@0
|
1096 if (j && (s[j - 2] == '\n')) j--;
|
yuuji@0
|
1097 if (write (fd,s,j) < 0) ret = NIL;
|
yuuji@0
|
1098 else { /* message header succeeded */
|
yuuji@0
|
1099 j = tstream ? /* write UIDPLUS data if have readwrite */
|
yuuji@0
|
1100 mmdf_xstatus (stream,LOCAL->buf,elt,++(tstream->uid_last),LONGT) :
|
yuuji@0
|
1101 mmdf_xstatus (stream,LOCAL->buf,elt,NIL,NIL);
|
yuuji@0
|
1102 if (write (fd,LOCAL->buf,j) < 0) ret = NIL;
|
yuuji@0
|
1103 else { /* message status succeeded */
|
yuuji@0
|
1104 s = mmdf_text_work (stream,elt,&j,FT_INTERNAL);
|
yuuji@0
|
1105 if ((write (fd,s,j) < 0) || (write (fd,mmdfhdr,MMDFHDRLEN) < 0))
|
yuuji@0
|
1106 ret = NIL;
|
yuuji@0
|
1107 else if (cu) { /* need to pass back new UID? */
|
yuuji@0
|
1108 mail_append_set (source,mail_uid (stream,i));
|
yuuji@0
|
1109 mail_append_set (dest,tstream->uid_last);
|
yuuji@0
|
1110 }
|
yuuji@0
|
1111 }
|
yuuji@0
|
1112 }
|
yuuji@0
|
1113 }
|
yuuji@0
|
1114 }
|
yuuji@0
|
1115
|
yuuji@0
|
1116 if (!ret || fsync (fd)) { /* force out the update */
|
yuuji@0
|
1117 sprintf (LOCAL->buf,"Message copy failed: %s",strerror (errno));
|
yuuji@0
|
1118 ftruncate (fd,sbuf.st_size);
|
yuuji@0
|
1119 ret = NIL;
|
yuuji@0
|
1120 }
|
yuuji@0
|
1121 /* force UIDVALIDITY assignment now */
|
yuuji@0
|
1122 if (tstream && !tstream->uid_validity) tstream->uid_validity = time (0);
|
yuuji@0
|
1123 /* return sets if doing COPYUID */
|
yuuji@0
|
1124 if (cu && ret) (*cu) (stream,mailbox,tstream->uid_validity,source,dest);
|
yuuji@0
|
1125 else { /* flush any sets we may have built */
|
yuuji@0
|
1126 mail_free_searchset (&source);
|
yuuji@0
|
1127 mail_free_searchset (&dest);
|
yuuji@0
|
1128 }
|
yuuji@0
|
1129 tp[1] = time (0); /* set mtime to now */
|
yuuji@0
|
1130 if (ret) tp[0] = tp[1] - 1; /* set atime to now-1 if successful copy */
|
yuuji@0
|
1131 else tp[0] = /* else preserve \Marked status */
|
yuuji@0
|
1132 ((sbuf.st_ctime > sbuf.st_atime) || (sbuf.st_mtime > sbuf.st_atime)) ?
|
yuuji@0
|
1133 sbuf.st_atime : tp[1];
|
yuuji@0
|
1134 utime (file,tp); /* set the times */
|
yuuji@0
|
1135 mmdf_unlock (fd,NIL,&lock); /* unlock and close mailbox */
|
yuuji@0
|
1136 if (tstream) { /* update last UID if we can */
|
yuuji@0
|
1137 MMDFLOCAL *local = (MMDFLOCAL *) tstream->local;
|
yuuji@0
|
1138 local->dirty = T; /* do a rewrite */
|
yuuji@0
|
1139 local->appending = T; /* but not at the cost of marking as old */
|
yuuji@0
|
1140 tstream = mail_close (tstream);
|
yuuji@0
|
1141 }
|
yuuji@0
|
1142 /* log the error */
|
yuuji@0
|
1143 if (!ret) MM_LOG (LOCAL->buf,ERROR);
|
yuuji@0
|
1144 /* delete if requested message */
|
yuuji@0
|
1145 else if (options & CP_MOVE) for (i = 1; i <= stream->nmsgs; i++)
|
yuuji@0
|
1146 if ((elt = mail_elt (stream,i))->sequence)
|
yuuji@0
|
1147 elt->deleted = elt->private.dirty = LOCAL->dirty = T;
|
yuuji@0
|
1148 MM_NOCRITICAL (stream); /* release critical */
|
yuuji@0
|
1149 return ret;
|
yuuji@0
|
1150 }
|
yuuji@0
|
1151
|
yuuji@0
|
1152 /* MMDF mail append message from stringstruct
|
yuuji@0
|
1153 * Accepts: MAIL stream
|
yuuji@0
|
1154 * destination mailbox
|
yuuji@0
|
1155 * append callback
|
yuuji@0
|
1156 * data for callback
|
yuuji@0
|
1157 * Returns: T if append successful, else NIL
|
yuuji@0
|
1158 */
|
yuuji@0
|
1159
|
yuuji@0
|
1160 #define BUFLEN 8*MAILTMPLEN
|
yuuji@0
|
1161
|
yuuji@0
|
1162 long mmdf_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data)
|
yuuji@0
|
1163 {
|
yuuji@0
|
1164 struct stat sbuf;
|
yuuji@0
|
1165 int fd;
|
yuuji@0
|
1166 unsigned long i;
|
yuuji@0
|
1167 char *flags,*date,buf[BUFLEN],tmp[MAILTMPLEN],file[MAILTMPLEN];
|
yuuji@0
|
1168 time_t tp[2];
|
yuuji@0
|
1169 FILE *sf,*df;
|
yuuji@0
|
1170 MESSAGECACHE elt;
|
yuuji@0
|
1171 DOTLOCK lock;
|
yuuji@0
|
1172 STRING *message;
|
yuuji@0
|
1173 unsigned long uidlocation = 0;
|
yuuji@0
|
1174 appenduid_t au = (appenduid_t)
|
yuuji@0
|
1175 (mail_parameters (NIL,GET_USERHASNOLIFE,NIL) ? NIL :
|
yuuji@0
|
1176 mail_parameters (NIL,GET_APPENDUID,NIL));
|
yuuji@0
|
1177 SEARCHSET *dst = au ? mail_newsearchset () : NIL;
|
yuuji@0
|
1178 long ret = LONGT;
|
yuuji@0
|
1179 MAILSTREAM *tstream = NIL;
|
yuuji@0
|
1180 /* default stream to prototype */
|
yuuji@0
|
1181 if (!stream) { /* stream specified? */
|
yuuji@0
|
1182 stream = &mmdfproto; /* no, default stream to prototype */
|
yuuji@0
|
1183 for (i = 0; i < NUSERFLAGS && stream->user_flags[i]; ++i)
|
yuuji@0
|
1184 fs_give ((void **) &stream->user_flags[i]);
|
yuuji@0
|
1185 }
|
yuuji@0
|
1186 if (!mmdf_valid (mailbox)) switch (errno) {
|
yuuji@0
|
1187 case ENOENT: /* no such file? */
|
yuuji@0
|
1188 if (compare_cstring (mailbox,"INBOX")) {
|
yuuji@0
|
1189 MM_NOTIFY (stream,"[TRYCREATE] Must create mailbox before append",NIL);
|
yuuji@0
|
1190 return NIL;
|
yuuji@0
|
1191 }
|
yuuji@0
|
1192 mmdf_create (NIL,"INBOX"); /* create empty INBOX */
|
yuuji@0
|
1193 case 0: /* merely empty file? */
|
yuuji@0
|
1194 tstream = stream;
|
yuuji@0
|
1195 break;
|
yuuji@0
|
1196 case EACCES: /* file protected */
|
yuuji@0
|
1197 sprintf (tmp,"Can't access destination: %.80s",mailbox);
|
yuuji@0
|
1198 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1199 return NIL;
|
yuuji@0
|
1200 case EINVAL:
|
yuuji@0
|
1201 sprintf (tmp,"Invalid MMDF-format mailbox name: %.80s",mailbox);
|
yuuji@0
|
1202 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1203 return NIL;
|
yuuji@0
|
1204 default:
|
yuuji@0
|
1205 sprintf (tmp,"Not a MMDF-format mailbox: %.80s",mailbox);
|
yuuji@0
|
1206 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1207 return NIL;
|
yuuji@0
|
1208 }
|
yuuji@0
|
1209 /* get sniffing stream for keywords */
|
yuuji@0
|
1210 else if (!(tstream = mail_open (NIL,mailbox,
|
yuuji@0
|
1211 OP_READONLY|OP_SILENT|OP_NOKOD|OP_SNIFF))) {
|
yuuji@0
|
1212 sprintf (tmp,"Unable to examine mailbox for APPEND: %.80s",mailbox);
|
yuuji@0
|
1213 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1214 return NIL;
|
yuuji@0
|
1215 }
|
yuuji@0
|
1216
|
yuuji@0
|
1217 /* get first message */
|
yuuji@0
|
1218 if (!MM_APPEND (af) (tstream,data,&flags,&date,&message)) return NIL;
|
yuuji@0
|
1219 if (!(sf = tmpfile ())) { /* must have scratch file */
|
yuuji@0
|
1220 sprintf (tmp,".%lx.%lx",(unsigned long) time (0),(unsigned long)getpid ());
|
yuuji@0
|
1221 if (!stat (tmp,&sbuf) || !(sf = fopen (tmp,"wb+"))) {
|
yuuji@0
|
1222 sprintf (tmp,"Unable to create scratch file: %.80s",strerror (errno));
|
yuuji@0
|
1223 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1224 return NIL;
|
yuuji@0
|
1225 }
|
yuuji@0
|
1226 unlink (tmp);
|
yuuji@0
|
1227 }
|
yuuji@0
|
1228 do { /* parse date */
|
yuuji@0
|
1229 if (!date) rfc822_date (date = tmp);
|
yuuji@0
|
1230 if (!mail_parse_date (&elt,date)) {
|
yuuji@0
|
1231 sprintf (tmp,"Bad date in append: %.80s",date);
|
yuuji@0
|
1232 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1233 }
|
yuuji@0
|
1234 else { /* user wants to suppress time zones? */
|
yuuji@0
|
1235 if (mail_parameters (NIL,GET_NOTIMEZONES,NIL)) {
|
yuuji@0
|
1236 time_t when = mail_longdate (&elt);
|
yuuji@0
|
1237 date = ctime (&when); /* use traditional date */
|
yuuji@0
|
1238 }
|
yuuji@0
|
1239 /* use POSIX-style date */
|
yuuji@0
|
1240 else date = mail_cdate (tmp,&elt);
|
yuuji@0
|
1241 if (!SIZE (message)) MM_LOG ("Append of zero-length message",ERROR);
|
yuuji@0
|
1242 else if (!mmdf_collect_msg (tstream,sf,flags,date,message)) {
|
yuuji@0
|
1243 sprintf (tmp,"Error writing scratch file: %.80s",strerror (errno));
|
yuuji@0
|
1244 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1245 }
|
yuuji@0
|
1246 /* get next message */
|
yuuji@0
|
1247 else if (MM_APPEND (af) (tstream,data,&flags,&date,&message)) continue;
|
yuuji@0
|
1248 }
|
yuuji@0
|
1249 fclose (sf); /* punt scratch file */
|
yuuji@0
|
1250 return NIL; /* give up */
|
yuuji@0
|
1251 } while (message); /* until no more messages */
|
yuuji@0
|
1252 if (fflush (sf)) {
|
yuuji@0
|
1253 sprintf (tmp,"Error finishing scratch file: %.80s",strerror (errno));
|
yuuji@0
|
1254 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1255 fclose (sf); /* punt scratch file */
|
yuuji@0
|
1256 return NIL; /* give up */
|
yuuji@0
|
1257 }
|
yuuji@0
|
1258 i = ftell (sf); /* size of scratch file */
|
yuuji@0
|
1259 if (tstream != stream) tstream = mail_close (tstream);
|
yuuji@0
|
1260
|
yuuji@0
|
1261 MM_CRITICAL (stream); /* go critical */
|
yuuji@0
|
1262 /* try to open readwrite for UIDPLUS */
|
yuuji@0
|
1263 if ((tstream = mail_open_work (&mmdfdriver,NIL,mailbox,
|
yuuji@0
|
1264 OP_SILENT|OP_NOKOD)) && tstream->rdonly)
|
yuuji@0
|
1265 tstream = mail_close (tstream);
|
yuuji@0
|
1266 if (au && !tstream) { /* wanted an APPENDUID? */
|
yuuji@0
|
1267 sprintf (tmp,"Unable to re-open mailbox for APPENDUID: %.80s",mailbox);
|
yuuji@0
|
1268 MM_LOG (tmp,WARN);
|
yuuji@0
|
1269 au = NIL;
|
yuuji@0
|
1270 }
|
yuuji@0
|
1271 if (((fd = mmdf_lock (dummy_file (file,mailbox),O_WRONLY|O_APPEND,
|
yuuji@0
|
1272 (long) mail_parameters (NIL,GET_MBXPROTECTION,NIL),
|
yuuji@0
|
1273 &lock,LOCK_EX)) < 0) ||
|
yuuji@0
|
1274 !(df = fdopen (fd,"ab"))) {
|
yuuji@0
|
1275 MM_NOCRITICAL (stream); /* done with critical */
|
yuuji@0
|
1276 sprintf (tmp,"Can't open append mailbox: %s",strerror (errno));
|
yuuji@0
|
1277 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1278 return NIL;
|
yuuji@0
|
1279 }
|
yuuji@0
|
1280 fstat (fd,&sbuf); /* get current file size */
|
yuuji@0
|
1281 rewind (sf);
|
yuuji@0
|
1282 tp[1] = time (0); /* set mtime to now */
|
yuuji@0
|
1283 /* write all messages */
|
yuuji@0
|
1284 if (!mmdf_append_msgs (tstream,sf,df,au ? dst : NIL) ||
|
yuuji@0
|
1285 (fflush (df) == EOF) || fsync (fd)) {
|
yuuji@0
|
1286 sprintf (buf,"Message append failed: %s",strerror (errno));
|
yuuji@0
|
1287 MM_LOG (buf,ERROR);
|
yuuji@0
|
1288 ftruncate (fd,sbuf.st_size);
|
yuuji@0
|
1289 tp[0] = /* preserve \Marked status */
|
yuuji@0
|
1290 ((sbuf.st_ctime > sbuf.st_atime) || (sbuf.st_mtime > sbuf.st_atime)) ?
|
yuuji@0
|
1291 sbuf.st_atime : tp[1];
|
yuuji@0
|
1292 ret = NIL; /* return error */
|
yuuji@0
|
1293 }
|
yuuji@0
|
1294 else tp[0] = tp[1] - 1; /* set atime to now-1 if successful copy */
|
yuuji@0
|
1295 utime (file,tp); /* set the times */
|
yuuji@0
|
1296 fclose (sf); /* done with scratch file */
|
yuuji@0
|
1297 /* force UIDVALIDITY assignment now */
|
yuuji@0
|
1298 if (tstream && !tstream->uid_validity) tstream->uid_validity = time (0);
|
yuuji@0
|
1299 /* return sets if doing APPENDUID */
|
yuuji@0
|
1300 if (au && ret) (*au) (mailbox,tstream->uid_validity,dst);
|
yuuji@0
|
1301 else mail_free_searchset (&dst);
|
yuuji@0
|
1302 mmdf_unlock (fd,NIL,&lock); /* unlock and close mailbox */
|
yuuji@0
|
1303 fclose (df);
|
yuuji@0
|
1304 if (tstream) { /* update last UID if we can */
|
yuuji@0
|
1305 MMDFLOCAL *local = (MMDFLOCAL *) tstream->local;
|
yuuji@0
|
1306 local->dirty = T; /* do a rewrite */
|
yuuji@0
|
1307 local->appending = T; /* but not at the cost of marking as old */
|
yuuji@0
|
1308 tstream = mail_close (tstream);
|
yuuji@0
|
1309 }
|
yuuji@0
|
1310 MM_NOCRITICAL (stream); /* release critical */
|
yuuji@0
|
1311 return ret;
|
yuuji@0
|
1312 }
|
yuuji@0
|
1313
|
yuuji@0
|
1314 /* Collect and write single message to append scratch file
|
yuuji@0
|
1315 * Accepts: MAIL stream
|
yuuji@0
|
1316 * scratch file
|
yuuji@0
|
1317 * flags
|
yuuji@0
|
1318 * date
|
yuuji@0
|
1319 * message stringstruct
|
yuuji@0
|
1320 * Returns: NIL if write error, else T
|
yuuji@0
|
1321 */
|
yuuji@0
|
1322
|
yuuji@0
|
1323 int mmdf_collect_msg (MAILSTREAM *stream,FILE *sf,char *flags,char *date,
|
yuuji@0
|
1324 STRING *msg)
|
yuuji@0
|
1325 {
|
yuuji@0
|
1326 unsigned char *s,*t;
|
yuuji@0
|
1327 unsigned long uf;
|
yuuji@0
|
1328 long f = mail_parse_flags (stream,flags,&uf);
|
yuuji@0
|
1329 /* write metadata, note date ends with NL */
|
yuuji@0
|
1330 if (fprintf (sf,"%ld %lu %s",f,SIZE (msg) + 1,date) < 0) return NIL;
|
yuuji@0
|
1331 while (uf) /* write user flags */
|
yuuji@0
|
1332 if ((s = stream->user_flags[find_rightmost_bit (&uf)]) &&
|
yuuji@0
|
1333 (fprintf (sf," %s",s) < 0)) return NIL;
|
yuuji@0
|
1334 if (putc ('\n',sf) == EOF) return NIL;
|
yuuji@0
|
1335 while (SIZE (msg)) { /* copy text to scratch file */
|
yuuji@0
|
1336 for (s = (unsigned char *) msg->curpos, t = s + msg->cursize; s < t; ++s)
|
yuuji@0
|
1337 if (!*s) *s = 0x80; /* disallow NUL */
|
yuuji@0
|
1338 /* write buffered text */
|
yuuji@0
|
1339 if (fwrite (msg->curpos,1,msg->cursize,sf) == msg->cursize)
|
yuuji@0
|
1340 SETPOS (msg,GETPOS (msg) + msg->cursize);
|
yuuji@0
|
1341 else return NIL; /* failed */
|
yuuji@0
|
1342 }
|
yuuji@0
|
1343 /* write trailing newline and return */
|
yuuji@0
|
1344 return (putc ('\n',sf) == EOF) ? NIL : T;
|
yuuji@0
|
1345 }
|
yuuji@0
|
1346
|
yuuji@0
|
1347 /* Append messages from scratch file to mailbox
|
yuuji@0
|
1348 * Accepts: MAIL stream
|
yuuji@0
|
1349 * source file
|
yuuji@0
|
1350 * destination file
|
yuuji@0
|
1351 * uidset to update if non-NIL
|
yuuji@0
|
1352 * Returns: T if success, NIL if failure
|
yuuji@0
|
1353 */
|
yuuji@0
|
1354
|
yuuji@0
|
1355 int mmdf_append_msgs (MAILSTREAM *stream,FILE *sf,FILE *df,SEARCHSET *set)
|
yuuji@0
|
1356 {
|
yuuji@0
|
1357 int c;
|
yuuji@0
|
1358 long f;
|
yuuji@0
|
1359 unsigned long i,j;
|
yuuji@0
|
1360 char *x,tmp[MAILTMPLEN];
|
yuuji@0
|
1361 int hdrp = T;
|
yuuji@0
|
1362 /* get message metadata line */
|
yuuji@0
|
1363 while (fgets (tmp,MAILTMPLEN,sf)) {
|
yuuji@0
|
1364 if (!(isdigit (tmp[0]) && strchr (tmp,'\n'))) return NIL;
|
yuuji@0
|
1365 f = strtol (tmp,&x,10); /* get flags */
|
yuuji@0
|
1366 if (!((*x++ == ' ') && isdigit (*x))) return NIL;
|
yuuji@0
|
1367 i = strtoul (x,&x,10); /* get message size */
|
yuuji@0
|
1368 if ((*x++ != ' ') || /* build initial header */
|
yuuji@0
|
1369 (fprintf (df,"%sFrom %s@%s %sStatus: ",mmdfhdr,myusername(),
|
yuuji@0
|
1370 mylocalhost(),x) < 0) ||
|
yuuji@0
|
1371 (f&fSEEN && (putc ('R',df) == EOF)) ||
|
yuuji@0
|
1372 (fputs ("\nX-Status: ",df) == EOF) ||
|
yuuji@0
|
1373 (f&fDELETED && (putc ('D',df) == EOF)) ||
|
yuuji@0
|
1374 (f&fFLAGGED && (putc ('F',df) == EOF)) ||
|
yuuji@0
|
1375 (f&fANSWERED && (putc ('A',df) == EOF)) ||
|
yuuji@0
|
1376 (f&fDRAFT && (putc ('T',df) == EOF)) ||
|
yuuji@0
|
1377 (fputs ("\nX-Keywords:",df) == EOF)) return NIL;
|
yuuji@0
|
1378 /* copy keywords */
|
yuuji@0
|
1379 while ((c = getc (sf)) != '\n') switch (c) {
|
yuuji@0
|
1380 case EOF:
|
yuuji@0
|
1381 return NIL;
|
yuuji@0
|
1382 default:
|
yuuji@0
|
1383 if (putc (c,df) == EOF) return NIL;
|
yuuji@0
|
1384 }
|
yuuji@0
|
1385 if ((putc ('\n',df) == EOF) ||
|
yuuji@0
|
1386 (set && (fprintf (df,"X-UID: %lu\n",++(stream->uid_last)) < 0)))
|
yuuji@0
|
1387 return NIL;
|
yuuji@0
|
1388
|
yuuji@0
|
1389 for (c = '\n'; i && fgets (tmp,MAILTMPLEN,sf); c = tmp[j-1]) {
|
yuuji@0
|
1390 /* get read line length */
|
yuuji@0
|
1391 if (i < (j = strlen (tmp))) fatal ("mmdf_append_msgs overrun");
|
yuuji@0
|
1392 i -= j; /* number of bytes left */
|
yuuji@0
|
1393 /* squish out ^A and CRs (note copies NUL) */
|
yuuji@0
|
1394 for (x = tmp; x = strpbrk (x,"\01\r"); --j) memmove (x,x+1,j-(x-tmp));
|
yuuji@0
|
1395 if (!j) continue; /* do nothing if line emptied */
|
yuuji@0
|
1396 /* start of line? */
|
yuuji@0
|
1397 if ((c == '\n')) switch (tmp[0]) {
|
yuuji@0
|
1398 case 'S': case 's': /* possible "Status:" */
|
yuuji@0
|
1399 if (hdrp && (j > 6) && ((tmp[1] == 't') || (tmp[1] == 'T')) &&
|
yuuji@0
|
1400 ((tmp[2] == 'a') || (tmp[2] == 'A')) &&
|
yuuji@0
|
1401 ((tmp[3] == 't') || (tmp[3] == 'T')) &&
|
yuuji@0
|
1402 ((tmp[4] == 'u') || (tmp[4] == 'U')) &&
|
yuuji@0
|
1403 ((tmp[5] == 's') || (tmp[5] == 'S')) && (tmp[6] == ':') &&
|
yuuji@0
|
1404 (fputs ("X-Original-",df) == EOF)) return NIL;
|
yuuji@0
|
1405 break;
|
yuuji@0
|
1406 case 'X': case 'x': /* possible X-??? header */
|
yuuji@0
|
1407 if (hdrp && (tmp[1] == '-') &&
|
yuuji@0
|
1408 /* possible X-UID: */
|
yuuji@0
|
1409 (((j > 5) && ((tmp[2] == 'U') || (tmp[2] == 'u')) &&
|
yuuji@0
|
1410 ((tmp[3] == 'I') || (tmp[3] == 'i')) &&
|
yuuji@0
|
1411 ((tmp[4] == 'D') || (tmp[4] == 'd')) && (tmp[5] == ':')) ||
|
yuuji@0
|
1412 /* possible X-IMAP: */
|
yuuji@0
|
1413 ((j > 6) && ((tmp[2] == 'I') || (tmp[2] == 'i')) &&
|
yuuji@0
|
1414 ((tmp[3] == 'M') || (tmp[3] == 'm')) &&
|
yuuji@0
|
1415 ((tmp[4] == 'A') || (tmp[4] == 'a')) &&
|
yuuji@0
|
1416 ((tmp[5] == 'P') || (tmp[5] == 'p')) &&
|
yuuji@0
|
1417 ((tmp[6] == ':') ||
|
yuuji@0
|
1418 /* or X-IMAPbase: */
|
yuuji@0
|
1419 ((j > 10) && ((tmp[6] == 'b') || (tmp[6] == 'B')) &&
|
yuuji@0
|
1420 ((tmp[7] == 'a') || (tmp[7] == 'A')) &&
|
yuuji@0
|
1421 ((tmp[8] == 's') || (tmp[8] == 'S')) &&
|
yuuji@0
|
1422 ((tmp[9] == 'e') || (tmp[9] == 'E')) && (tmp[10] == ':')))) ||
|
yuuji@0
|
1423 /* possible X-Status: */
|
yuuji@0
|
1424 ((j > 8) && ((tmp[2] == 'S') || (tmp[2] == 's')) &&
|
yuuji@0
|
1425 ((tmp[3] == 't') || (tmp[3] == 'T')) &&
|
yuuji@0
|
1426 ((tmp[4] == 'a') || (tmp[4] == 'A')) &&
|
yuuji@0
|
1427 ((tmp[5] == 't') || (tmp[5] == 'T')) &&
|
yuuji@0
|
1428 ((tmp[6] == 'u') || (tmp[6] == 'U')) &&
|
yuuji@0
|
1429 ((tmp[7] == 's') || (tmp[7] == 'S')) && (tmp[8] == ':')) ||
|
yuuji@0
|
1430 /* possible X-Keywords: */
|
yuuji@0
|
1431 ((j > 10) && ((tmp[2] == 'K') || (tmp[2] == 'k')) &&
|
yuuji@0
|
1432 ((tmp[3] == 'e') || (tmp[3] == 'E')) &&
|
yuuji@0
|
1433 ((tmp[4] == 'y') || (tmp[4] == 'Y')) &&
|
yuuji@0
|
1434 ((tmp[5] == 'w') || (tmp[5] == 'W')) &&
|
yuuji@0
|
1435 ((tmp[6] == 'o') || (tmp[6] == 'O')) &&
|
yuuji@0
|
1436 ((tmp[7] == 'r') || (tmp[7] == 'R')) &&
|
yuuji@0
|
1437 ((tmp[8] == 'd') || (tmp[8] == 'D')) &&
|
yuuji@0
|
1438 ((tmp[9] == 's') || (tmp[9] == 'S')) && (tmp[10] == ':'))) &&
|
yuuji@0
|
1439 (fputs ("X-Original-",df) == EOF)) return NIL;
|
yuuji@0
|
1440 break;
|
yuuji@0
|
1441 case '\n': /* blank line */
|
yuuji@0
|
1442 hdrp = NIL;
|
yuuji@0
|
1443 break;
|
yuuji@0
|
1444 default: /* nothing to do */
|
yuuji@0
|
1445 break;
|
yuuji@0
|
1446 }
|
yuuji@0
|
1447 /* just write the line */
|
yuuji@0
|
1448 if (fwrite (tmp,1,j,df) != j) return NIL;
|
yuuji@0
|
1449 }
|
yuuji@0
|
1450 /* make sure read entire msg & wrote trailer */
|
yuuji@0
|
1451 if (i || (fputs (mmdfhdr,df) == EOF)) return NIL;
|
yuuji@0
|
1452 /* update set */
|
yuuji@0
|
1453 if (stream) mail_append_set (set,stream->uid_last);
|
yuuji@0
|
1454 }
|
yuuji@0
|
1455 return T;
|
yuuji@0
|
1456 }
|
yuuji@0
|
1457
|
yuuji@0
|
1458 /* Internal routines */
|
yuuji@0
|
1459
|
yuuji@0
|
1460
|
yuuji@0
|
1461 /* MMDF mail abort stream
|
yuuji@0
|
1462 * Accepts: MAIL stream
|
yuuji@0
|
1463 */
|
yuuji@0
|
1464
|
yuuji@0
|
1465 void mmdf_abort (MAILSTREAM *stream)
|
yuuji@0
|
1466 {
|
yuuji@0
|
1467 if (LOCAL) { /* only if a file is open */
|
yuuji@0
|
1468 if (LOCAL->fd >= 0) close (LOCAL->fd);
|
yuuji@0
|
1469 if (LOCAL->ld >= 0) { /* have a mailbox lock? */
|
yuuji@0
|
1470 flock (LOCAL->ld,LOCK_UN);/* yes, release the lock */
|
yuuji@0
|
1471 close (LOCAL->ld); /* close the lock file */
|
yuuji@0
|
1472 unlink (LOCAL->lname); /* and delete it */
|
yuuji@0
|
1473 }
|
yuuji@0
|
1474 if (LOCAL->lname) fs_give ((void **) &LOCAL->lname);
|
yuuji@0
|
1475 /* free local text buffers */
|
yuuji@0
|
1476 if (LOCAL->buf) fs_give ((void **) &LOCAL->buf);
|
yuuji@0
|
1477 if (LOCAL->text.data) fs_give ((void **) &LOCAL->text.data);
|
yuuji@0
|
1478 if (LOCAL->linebuf) fs_give ((void **) &LOCAL->linebuf);
|
yuuji@0
|
1479 if (LOCAL->line) fs_give ((void **) &LOCAL->line);
|
yuuji@0
|
1480 /* nuke the local data */
|
yuuji@0
|
1481 fs_give ((void **) &stream->local);
|
yuuji@0
|
1482 stream->dtb = NIL; /* log out the DTB */
|
yuuji@0
|
1483 }
|
yuuji@0
|
1484 }
|
yuuji@0
|
1485
|
yuuji@0
|
1486 /* MMDF open and lock mailbox
|
yuuji@0
|
1487 * Accepts: file name to open/lock
|
yuuji@0
|
1488 * file open mode
|
yuuji@0
|
1489 * destination buffer for lock file name
|
yuuji@0
|
1490 * type of locking operation (LOCK_SH or LOCK_EX)
|
yuuji@0
|
1491 */
|
yuuji@0
|
1492
|
yuuji@0
|
1493 int mmdf_lock (char *file,int flags,int mode,DOTLOCK *lock,int op)
|
yuuji@0
|
1494 {
|
yuuji@0
|
1495 int fd;
|
yuuji@0
|
1496 blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL);
|
yuuji@0
|
1497 (*bn) (BLOCK_FILELOCK,NIL);
|
yuuji@0
|
1498 /* try locking the easy way */
|
yuuji@0
|
1499 if (dotlock_lock (file,lock,-1)) {
|
yuuji@0
|
1500 /* got dotlock file, easy open */
|
yuuji@0
|
1501 if ((fd = open (file,flags,mode)) >= 0) flock (fd,op);
|
yuuji@0
|
1502 else dotlock_unlock (lock); /* open failed, free the dotlock */
|
yuuji@0
|
1503 }
|
yuuji@0
|
1504 /* no dot lock file, open file now */
|
yuuji@0
|
1505 else if ((fd = open (file,flags,mode)) >= 0) {
|
yuuji@0
|
1506 /* try paranoid way to make a dot lock file */
|
yuuji@0
|
1507 if (dotlock_lock (file,lock,fd)) {
|
yuuji@0
|
1508 close (fd); /* get fresh fd in case of timing race */
|
yuuji@0
|
1509 if ((fd = open (file,flags,mode)) >= 0) flock (fd,op);
|
yuuji@0
|
1510 /* open failed, free the dotlock */
|
yuuji@0
|
1511 else dotlock_unlock (lock);
|
yuuji@0
|
1512 }
|
yuuji@0
|
1513 else flock (fd,op); /* paranoid way failed, just flock() it */
|
yuuji@0
|
1514 }
|
yuuji@0
|
1515 (*bn) (BLOCK_NONE,NIL);
|
yuuji@0
|
1516 return fd;
|
yuuji@0
|
1517 }
|
yuuji@0
|
1518
|
yuuji@0
|
1519 /* MMDF unlock and close mailbox
|
yuuji@0
|
1520 * Accepts: file descriptor
|
yuuji@0
|
1521 * (optional) mailbox stream to check atime/mtime
|
yuuji@0
|
1522 * (optional) lock file name
|
yuuji@0
|
1523 */
|
yuuji@0
|
1524
|
yuuji@0
|
1525 void mmdf_unlock (int fd,MAILSTREAM *stream,DOTLOCK *lock)
|
yuuji@0
|
1526 {
|
yuuji@0
|
1527 if (stream) { /* need to muck with times? */
|
yuuji@0
|
1528 struct stat sbuf;
|
yuuji@0
|
1529 time_t tp[2];
|
yuuji@0
|
1530 time_t now = time (0);
|
yuuji@0
|
1531 fstat (fd,&sbuf); /* get file times */
|
yuuji@0
|
1532 if (LOCAL->ld >= 0) { /* yes, readwrite session? */
|
yuuji@0
|
1533 tp[0] = now; /* set atime to now */
|
yuuji@0
|
1534 /* set mtime to (now - 1) if necessary */
|
yuuji@0
|
1535 tp[1] = (now > sbuf.st_mtime) ? sbuf.st_mtime : now - 1;
|
yuuji@0
|
1536 }
|
yuuji@0
|
1537 else if (stream->recent) { /* readonly with recent messages */
|
yuuji@0
|
1538 if ((sbuf.st_atime >= sbuf.st_mtime) ||
|
yuuji@0
|
1539 (sbuf.st_atime >= sbuf.st_ctime))
|
yuuji@0
|
1540 /* keep past mtime, whack back atime */
|
yuuji@0
|
1541 tp[0] = (tp[1] = (sbuf.st_mtime < now) ? sbuf.st_mtime : now) - 1;
|
yuuji@0
|
1542 else now = 0; /* no time change needed */
|
yuuji@0
|
1543 }
|
yuuji@0
|
1544 /* readonly with no recent messages */
|
yuuji@0
|
1545 else if ((sbuf.st_atime < sbuf.st_mtime) ||
|
yuuji@0
|
1546 (sbuf.st_atime < sbuf.st_ctime)) {
|
yuuji@0
|
1547 tp[0] = now; /* set atime to now */
|
yuuji@0
|
1548 /* set mtime to (now - 1) if necessary */
|
yuuji@0
|
1549 tp[1] = (now > sbuf.st_mtime) ? sbuf.st_mtime : now - 1;
|
yuuji@0
|
1550 }
|
yuuji@0
|
1551 else now = 0; /* no time change needed */
|
yuuji@0
|
1552 /* set the times, note change */
|
yuuji@0
|
1553 if (now && !utime (stream->mailbox,tp)) LOCAL->filetime = tp[1];
|
yuuji@0
|
1554 }
|
yuuji@0
|
1555 flock (fd,LOCK_UN); /* release flock'ers */
|
yuuji@0
|
1556 if (!stream) close (fd); /* close the file if no stream */
|
yuuji@0
|
1557 dotlock_unlock (lock); /* flush the lock file if any */
|
yuuji@0
|
1558 }
|
yuuji@0
|
1559
|
yuuji@0
|
1560 /* MMDF mail parse and lock mailbox
|
yuuji@0
|
1561 * Accepts: MAIL stream
|
yuuji@0
|
1562 * space to write lock file name
|
yuuji@0
|
1563 * type of locking operation
|
yuuji@0
|
1564 * Returns: T if parse OK, critical & mailbox is locked shared; NIL if failure
|
yuuji@0
|
1565 */
|
yuuji@0
|
1566
|
yuuji@0
|
1567 int mmdf_parse (MAILSTREAM *stream,DOTLOCK *lock,int op)
|
yuuji@0
|
1568 {
|
yuuji@0
|
1569 int ti,zn,m;
|
yuuji@0
|
1570 unsigned long i,j,k;
|
yuuji@0
|
1571 unsigned char c,*s,*t,*u,tmp[MAILTMPLEN],date[30];
|
yuuji@0
|
1572 int retain = T;
|
yuuji@0
|
1573 unsigned long nmsgs = stream->nmsgs;
|
yuuji@0
|
1574 unsigned long prevuid = nmsgs ? mail_elt (stream,nmsgs)->private.uid : 0;
|
yuuji@0
|
1575 unsigned long recent = stream->recent;
|
yuuji@0
|
1576 unsigned long oldnmsgs = stream->nmsgs;
|
yuuji@0
|
1577 short silent = stream->silent;
|
yuuji@0
|
1578 short pseudoseen = NIL;
|
yuuji@0
|
1579 struct stat sbuf;
|
yuuji@0
|
1580 STRING bs;
|
yuuji@0
|
1581 FDDATA d;
|
yuuji@0
|
1582 MESSAGECACHE *elt;
|
yuuji@0
|
1583 mail_lock (stream); /* guard against recursion or pingers */
|
yuuji@0
|
1584 /* toss out previous descriptor */
|
yuuji@0
|
1585 if (LOCAL->fd >= 0) close (LOCAL->fd);
|
yuuji@0
|
1586 MM_CRITICAL (stream); /* open and lock mailbox (shared OK) */
|
yuuji@0
|
1587 if ((LOCAL->fd = mmdf_lock (stream->mailbox,(LOCAL->ld >= 0) ?
|
yuuji@0
|
1588 O_RDWR : O_RDONLY,
|
yuuji@0
|
1589 (long)mail_parameters(NIL,GET_MBXPROTECTION,NIL),
|
yuuji@0
|
1590 lock,op)) < 0) {
|
yuuji@0
|
1591 sprintf (tmp,"Mailbox open failed, aborted: %s",strerror (errno));
|
yuuji@0
|
1592 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1593 mmdf_abort (stream);
|
yuuji@0
|
1594 mail_unlock (stream);
|
yuuji@0
|
1595 MM_NOCRITICAL (stream); /* done with critical */
|
yuuji@0
|
1596 return NIL;
|
yuuji@0
|
1597 }
|
yuuji@0
|
1598 fstat (LOCAL->fd,&sbuf); /* get status */
|
yuuji@0
|
1599 /* validate change in size */
|
yuuji@0
|
1600 if (sbuf.st_size < LOCAL->filesize) {
|
yuuji@0
|
1601 sprintf (tmp,"Mailbox shrank from %lu to %lu bytes, aborted",
|
yuuji@0
|
1602 (unsigned long) LOCAL->filesize,(unsigned long) sbuf.st_size);
|
yuuji@0
|
1603 MM_LOG (tmp,ERROR); /* this is pretty bad */
|
yuuji@0
|
1604 mmdf_unlock (LOCAL->fd,stream,lock);
|
yuuji@0
|
1605 mmdf_abort (stream);
|
yuuji@0
|
1606 mail_unlock (stream);
|
yuuji@0
|
1607 MM_NOCRITICAL (stream); /* done with critical */
|
yuuji@0
|
1608 return NIL;
|
yuuji@0
|
1609 }
|
yuuji@0
|
1610
|
yuuji@0
|
1611 /* new data? */
|
yuuji@0
|
1612 else if (i = sbuf.st_size - LOCAL->filesize) {
|
yuuji@0
|
1613 d.fd = LOCAL->fd; /* yes, set up file descriptor */
|
yuuji@0
|
1614 d.pos = LOCAL->filesize; /* get to that position in the file */
|
yuuji@0
|
1615 d.chunk = LOCAL->buf; /* initial buffer chunk */
|
yuuji@0
|
1616 d.chunksize = CHUNKSIZE; /* file chunk size */
|
yuuji@0
|
1617 INIT (&bs,fd_string,&d,i); /* initialize stringstruct */
|
yuuji@0
|
1618 /* skip leading whitespace for broken MTAs */
|
yuuji@0
|
1619 while (((c = CHR (&bs)) == '\n') || (c == '\r') ||
|
yuuji@0
|
1620 (c == ' ') || (c == '\t')) SNX (&bs);
|
yuuji@0
|
1621 if (SIZE (&bs)) { /* read new data */
|
yuuji@0
|
1622 /* remember internal header position */
|
yuuji@0
|
1623 j = LOCAL->filesize + GETPOS (&bs);
|
yuuji@0
|
1624 s = mmdf_mbxline (stream,&bs,&i);
|
yuuji@0
|
1625 stream->silent = T; /* quell main program new message events */
|
yuuji@0
|
1626 do { /* read MMDF header */
|
yuuji@0
|
1627 if (!(i && ISMMDF (s))){/* see if valid MMDF header */
|
yuuji@0
|
1628 sprintf (tmp,"Unexpected changes to mailbox (try restarting): %.20s",
|
yuuji@0
|
1629 (char *) s);
|
yuuji@0
|
1630 /* see if we can back up to a line */
|
yuuji@0
|
1631 if (i && (j > MMDFHDRLEN)) {
|
yuuji@0
|
1632 SETPOS (&bs,j -= MMDFHDRLEN);
|
yuuji@0
|
1633 /* read previous line */
|
yuuji@0
|
1634 s = mmdf_mbxline (stream,&bs,&i);
|
yuuji@0
|
1635 /* kill the error if it looks good */
|
yuuji@0
|
1636 if (i && ISMMDF (s)) tmp[0] = '\0';
|
yuuji@0
|
1637 }
|
yuuji@0
|
1638 if (tmp[0]) {
|
yuuji@0
|
1639 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1640 mmdf_unlock (LOCAL->fd,stream,lock);
|
yuuji@0
|
1641 mmdf_abort (stream);
|
yuuji@0
|
1642 mail_unlock (stream);
|
yuuji@0
|
1643 MM_NOCRITICAL (stream);
|
yuuji@0
|
1644 return NIL;
|
yuuji@0
|
1645 }
|
yuuji@0
|
1646 }
|
yuuji@0
|
1647 /* instantiate first new message */
|
yuuji@0
|
1648 mail_exists (stream,++nmsgs);
|
yuuji@0
|
1649 (elt = mail_elt (stream,nmsgs))->valid = T;
|
yuuji@0
|
1650 recent++; /* assume recent by default */
|
yuuji@0
|
1651 elt->recent = T;
|
yuuji@0
|
1652 /* note position/size of internal header */
|
yuuji@0
|
1653 elt->private.special.offset = j;
|
yuuji@0
|
1654 elt->private.special.text.size = i;
|
yuuji@0
|
1655
|
yuuji@0
|
1656 s = mmdf_mbxline (stream,&bs,&i);
|
yuuji@0
|
1657 ti = 0; /* assume not a valid date */
|
yuuji@0
|
1658 zn = 0,t = NIL;
|
yuuji@0
|
1659 if (i) VALID (s,t,ti,zn);
|
yuuji@0
|
1660 if (ti) { /* generate plausible IMAPish date string */
|
yuuji@0
|
1661 /* this is also part of header */
|
yuuji@0
|
1662 elt->private.special.text.size += i;
|
yuuji@0
|
1663 date[2] = date[6] = date[20] = '-'; date[11] = ' ';
|
yuuji@0
|
1664 date[14] = date[17] = ':';
|
yuuji@0
|
1665 /* dd */
|
yuuji@0
|
1666 date[0] = t[ti - 2]; date[1] = t[ti - 1];
|
yuuji@0
|
1667 /* mmm */
|
yuuji@0
|
1668 date[3] = t[ti - 6]; date[4] = t[ti - 5]; date[5] = t[ti - 4];
|
yuuji@0
|
1669 /* hh */
|
yuuji@0
|
1670 date[12] = t[ti + 1]; date[13] = t[ti + 2];
|
yuuji@0
|
1671 /* mm */
|
yuuji@0
|
1672 date[15] = t[ti + 4]; date[16] = t[ti + 5];
|
yuuji@0
|
1673 if (t[ti += 6]==':'){ /* ss */
|
yuuji@0
|
1674 date[18] = t[++ti]; date[19] = t[++ti];
|
yuuji@0
|
1675 ti++; /* move to space */
|
yuuji@0
|
1676 }
|
yuuji@0
|
1677 else date[18] = date[19] = '0';
|
yuuji@0
|
1678 /* yy -- advance over timezone if necessary */
|
yuuji@0
|
1679 if (zn == ti) ti += (((t[zn+1] == '+') || (t[zn+1] == '-')) ? 6 : 4);
|
yuuji@0
|
1680 date[7] = t[ti + 1]; date[8] = t[ti + 2];
|
yuuji@0
|
1681 date[9] = t[ti + 3]; date[10] = t[ti + 4];
|
yuuji@0
|
1682 /* zzz */
|
yuuji@0
|
1683 t = zn ? (t + zn + 1) : (unsigned char *) "LCL";
|
yuuji@0
|
1684 date[21] = *t++; date[22] = *t++; date[23] = *t++;
|
yuuji@0
|
1685 if ((date[21] != '+') && (date[21] != '-')) date[24] = '\0';
|
yuuji@0
|
1686 else { /* numeric time zone */
|
yuuji@0
|
1687 date[24] = *t++; date[25] = *t++;
|
yuuji@0
|
1688 date[26] = '\0'; date[20] = ' ';
|
yuuji@0
|
1689 }
|
yuuji@0
|
1690 /* set internal date */
|
yuuji@0
|
1691 if (!mail_parse_date (elt,date)) {
|
yuuji@0
|
1692 sprintf (tmp,"Unable to parse internal date: %s",(char *) date);
|
yuuji@0
|
1693 MM_LOG (tmp,WARN);
|
yuuji@0
|
1694 }
|
yuuji@0
|
1695 }
|
yuuji@0
|
1696 else { /* make date from file date */
|
yuuji@0
|
1697 struct tm *tm = gmtime (&sbuf.st_mtime);
|
yuuji@0
|
1698 elt->day = tm->tm_mday; elt->month = tm->tm_mon + 1;
|
yuuji@0
|
1699 elt->year = tm->tm_year + 1900 - BASEYEAR;
|
yuuji@0
|
1700 elt->hours = tm->tm_hour; elt->minutes = tm->tm_min;
|
yuuji@0
|
1701 elt->seconds = tm->tm_sec;
|
yuuji@0
|
1702 elt->zhours = 0; elt->zminutes = 0;
|
yuuji@0
|
1703 t = NIL; /* suppress line read */
|
yuuji@0
|
1704 }
|
yuuji@0
|
1705 /* header starts here */
|
yuuji@0
|
1706 elt->private.msg.header.offset = elt->private.special.text.size;
|
yuuji@0
|
1707
|
yuuji@0
|
1708 do { /* look for message body */
|
yuuji@0
|
1709 j = GETPOS (&bs); /* note position before line */
|
yuuji@0
|
1710 if (t) s = t = mmdf_mbxline (stream,&bs,&i);
|
yuuji@0
|
1711 else t = s; /* this line read was suppressed */
|
yuuji@0
|
1712 if (ISMMDF (s)) { /* found terminator in header? */
|
yuuji@0
|
1713 SETPOS (&bs,j); /* oops, back up before line */
|
yuuji@0
|
1714 /* must insert a newline */
|
yuuji@0
|
1715 elt->private.spare.data++;
|
yuuji@0
|
1716 break; /* punt */
|
yuuji@0
|
1717 }
|
yuuji@0
|
1718 /* this line is part of header */
|
yuuji@0
|
1719 elt->private.msg.header.text.size += i;
|
yuuji@0
|
1720 if (i) switch (*s) { /* check header lines */
|
yuuji@0
|
1721 case 'X': /* possible X-???: line */
|
yuuji@0
|
1722 if (s[1] == '-') { /* must be immediately followed by hyphen */
|
yuuji@0
|
1723 /* X-Status: becomes Status: in S case */
|
yuuji@0
|
1724 if (s[2] == 'S' && s[3] == 't' && s[4] == 'a' && s[5] == 't' &&
|
yuuji@0
|
1725 s[6] == 'u' && s[7] == 's' && s[8] == ':') s += 2;
|
yuuji@0
|
1726 /* possible X-Keywords */
|
yuuji@0
|
1727 else if (s[2] == 'K' && s[3] == 'e' && s[4] == 'y' &&
|
yuuji@0
|
1728 s[5] == 'w' && s[6] == 'o' && s[7] == 'r' &&
|
yuuji@0
|
1729 s[8] == 'd' && s[9] == 's' && s[10] == ':') {
|
yuuji@0
|
1730 SIZEDTEXT uf;
|
yuuji@0
|
1731 retain = NIL; /* don't retain continuation */
|
yuuji@0
|
1732 s += 11; /* flush leading whitespace */
|
yuuji@0
|
1733 while (*s && (*s != '\n') && ((*s != '\r') || (s[1] != '\n'))){
|
yuuji@0
|
1734 while (*s == ' ') s++;
|
yuuji@0
|
1735 /* find end of keyword */
|
yuuji@0
|
1736 if (!(u = strpbrk (s," \n\r"))) u = s + strlen (s);
|
yuuji@0
|
1737 /* got a keyword? */
|
yuuji@0
|
1738 if ((k = (u - s)) && (k <= MAXUSERFLAG)) {
|
yuuji@0
|
1739 uf.data = (unsigned char *) s;
|
yuuji@0
|
1740 uf.size = k;
|
yuuji@0
|
1741 for (j = 0; (j < NUSERFLAGS) && stream->user_flags[j]; ++j)
|
yuuji@0
|
1742 if (!compare_csizedtext (stream->user_flags[j],&uf)) {
|
yuuji@0
|
1743 elt->user_flags |= ((long) 1) << j;
|
yuuji@0
|
1744 break;
|
yuuji@0
|
1745 }
|
yuuji@0
|
1746 }
|
yuuji@0
|
1747 s = u; /* advance to next keyword */
|
yuuji@0
|
1748 }
|
yuuji@0
|
1749 break;
|
yuuji@0
|
1750 }
|
yuuji@0
|
1751
|
yuuji@0
|
1752 /* possible X-IMAP */
|
yuuji@0
|
1753 else if ((s[2] == 'I') && (s[3] == 'M') && (s[4] == 'A') &&
|
yuuji@0
|
1754 (s[5] == 'P') && ((m = (s[6] == ':')) ||
|
yuuji@0
|
1755 ((s[6] == 'b') && (s[7] == 'a') &&
|
yuuji@0
|
1756 (s[8] == 's') && (s[9] == 'e') &&
|
yuuji@0
|
1757 (s[10] == ':')))) {
|
yuuji@0
|
1758 retain = NIL; /* don't retain continuation */
|
yuuji@0
|
1759 if ((nmsgs == 1) && !stream->uid_validity) {
|
yuuji@0
|
1760 /* advance to data */
|
yuuji@0
|
1761 s += m ? 7 : 11;
|
yuuji@0
|
1762 /* flush whitespace */
|
yuuji@0
|
1763 while (*s == ' ') s++;
|
yuuji@0
|
1764 j = 0; /* slurp UID validity */
|
yuuji@0
|
1765 /* found a digit? */
|
yuuji@0
|
1766 while (isdigit (*s)) {
|
yuuji@0
|
1767 j *= 10; /* yes, add it in */
|
yuuji@0
|
1768 j += *s++ - '0';
|
yuuji@0
|
1769 }
|
yuuji@0
|
1770 /* flush whitespace */
|
yuuji@0
|
1771 while (*s == ' ') s++;
|
yuuji@0
|
1772 /* must have valid UID validity and UID last */
|
yuuji@0
|
1773 if (j && isdigit (*s)) {
|
yuuji@0
|
1774 /* pseudo-header seen if X-IMAP */
|
yuuji@0
|
1775 if (m) pseudoseen = LOCAL->pseudo = T;
|
yuuji@0
|
1776 /* save UID validity */
|
yuuji@0
|
1777 stream->uid_validity = j;
|
yuuji@0
|
1778 j = 0; /* slurp UID last */
|
yuuji@0
|
1779 while (isdigit (*s)) {
|
yuuji@0
|
1780 j *= 10; /* yes, add it in */
|
yuuji@0
|
1781 j += *s++ - '0';
|
yuuji@0
|
1782 }
|
yuuji@0
|
1783 /* save UID last */
|
yuuji@0
|
1784 stream->uid_last = j;
|
yuuji@0
|
1785 /* process keywords */
|
yuuji@0
|
1786 for (j = 0; (*s != '\n') && ((*s != '\r')||(s[1] != '\n'));
|
yuuji@0
|
1787 s = u,j++) {
|
yuuji@0
|
1788 /* flush leading whitespace */
|
yuuji@0
|
1789 while (*s == ' ') s++;
|
yuuji@0
|
1790 u = strpbrk (s," \n\r");
|
yuuji@0
|
1791 /* got a keyword? */
|
yuuji@0
|
1792 if ((j < NUSERFLAGS) && (k = (u - s)) &&
|
yuuji@0
|
1793 (k <= MAXUSERFLAG)) {
|
yuuji@0
|
1794 if (stream->user_flags[j])
|
yuuji@0
|
1795 fs_give ((void **) &stream->user_flags[j]);
|
yuuji@0
|
1796 stream->user_flags[j] = (char *) fs_get (k + 1);
|
yuuji@0
|
1797 strncpy (stream->user_flags[j],s,k);
|
yuuji@0
|
1798 stream->user_flags[j][k] = '\0';
|
yuuji@0
|
1799 }
|
yuuji@0
|
1800 }
|
yuuji@0
|
1801 }
|
yuuji@0
|
1802 }
|
yuuji@0
|
1803 break;
|
yuuji@0
|
1804 }
|
yuuji@0
|
1805
|
yuuji@0
|
1806 /* possible X-UID */
|
yuuji@0
|
1807 else if (s[2] == 'U' && s[3] == 'I' && s[4] == 'D' &&
|
yuuji@0
|
1808 s[5] == ':') {
|
yuuji@0
|
1809 retain = NIL; /* don't retain continuation */
|
yuuji@0
|
1810 /* only believe if have a UID validity */
|
yuuji@0
|
1811 if (stream->uid_validity && ((nmsgs > 1) || !pseudoseen)) {
|
yuuji@0
|
1812 s += 6; /* advance to UID value */
|
yuuji@0
|
1813 /* flush whitespace */
|
yuuji@0
|
1814 while (*s == ' ') s++;
|
yuuji@0
|
1815 j = 0;
|
yuuji@0
|
1816 /* found a digit? */
|
yuuji@0
|
1817 while (isdigit (*s)) {
|
yuuji@0
|
1818 j *= 10; /* yes, add it in */
|
yuuji@0
|
1819 j += *s++ - '0';
|
yuuji@0
|
1820 }
|
yuuji@0
|
1821 /* flush remainder of line */
|
yuuji@0
|
1822 while (*s != '\n') s++;
|
yuuji@0
|
1823 /* make sure not duplicated */
|
yuuji@0
|
1824 if (elt->private.uid)
|
yuuji@0
|
1825 sprintf (tmp,"Message %lu UID %lu already has UID %lu",
|
yuuji@0
|
1826 pseudoseen ? elt->msgno - 1 : elt->msgno,
|
yuuji@0
|
1827 j,elt->private.uid);
|
yuuji@0
|
1828 /* make sure UID doesn't go backwards */
|
yuuji@0
|
1829 else if (j <= prevuid)
|
yuuji@0
|
1830 sprintf (tmp,"Message %lu UID %lu less than %lu",
|
yuuji@0
|
1831 pseudoseen ? elt->msgno - 1 : elt->msgno,
|
yuuji@0
|
1832 j,prevuid + 1);
|
yuuji@0
|
1833 #if 0 /* this is currently broken by UIDPLUS */
|
yuuji@0
|
1834 /* or skip by mailbox's recorded last */
|
yuuji@0
|
1835 else if (j > stream->uid_last)
|
yuuji@0
|
1836 sprintf (tmp,"Message %lu UID %lu greater than last %lu",
|
yuuji@0
|
1837 pseudoseen ? elt->msgno - 1 : elt->msgno,
|
yuuji@0
|
1838 j,stream->uid_last);
|
yuuji@0
|
1839 #endif
|
yuuji@0
|
1840 else { /* normal UID case */
|
yuuji@0
|
1841 prevuid = elt->private.uid = j;
|
yuuji@0
|
1842 #if 1 /* temporary kludge for UIDPLUS */
|
yuuji@0
|
1843 if (prevuid > stream->uid_last) {
|
yuuji@0
|
1844 stream->uid_last = prevuid;
|
yuuji@0
|
1845 LOCAL->ddirty = LOCAL->dirty = T;
|
yuuji@0
|
1846 }
|
yuuji@0
|
1847 #endif
|
yuuji@0
|
1848 break; /* exit this cruft */
|
yuuji@0
|
1849 }
|
yuuji@0
|
1850 MM_LOG (tmp,WARN);
|
yuuji@0
|
1851 /* invalidate UID validity */
|
yuuji@0
|
1852 stream->uid_validity = 0;
|
yuuji@0
|
1853 elt->private.uid = 0;
|
yuuji@0
|
1854 }
|
yuuji@0
|
1855 break;
|
yuuji@0
|
1856 }
|
yuuji@0
|
1857 }
|
yuuji@0
|
1858 /* otherwise fall into S case */
|
yuuji@0
|
1859
|
yuuji@0
|
1860 case 'S': /* possible Status: line */
|
yuuji@0
|
1861 if (s[0] == 'S' && s[1] == 't' && s[2] == 'a' && s[3] == 't' &&
|
yuuji@0
|
1862 s[4] == 'u' && s[5] == 's' && s[6] == ':') {
|
yuuji@0
|
1863 retain = NIL; /* don't retain continuation */
|
yuuji@0
|
1864 s += 6; /* advance to status flags */
|
yuuji@0
|
1865 do switch (*s++) {/* parse flags */
|
yuuji@0
|
1866 case 'R': /* message read */
|
yuuji@0
|
1867 elt->seen = T;
|
yuuji@0
|
1868 break;
|
yuuji@0
|
1869 case 'O': /* message old */
|
yuuji@0
|
1870 if (elt->recent) {
|
yuuji@0
|
1871 elt->recent = NIL;
|
yuuji@0
|
1872 recent--; /* it really wasn't recent */
|
yuuji@0
|
1873 }
|
yuuji@0
|
1874 break;
|
yuuji@0
|
1875 case 'D': /* message deleted */
|
yuuji@0
|
1876 elt->deleted = T;
|
yuuji@0
|
1877 break;
|
yuuji@0
|
1878 case 'F': /* message flagged */
|
yuuji@0
|
1879 elt->flagged = T;
|
yuuji@0
|
1880 break;
|
yuuji@0
|
1881 case 'A': /* message answered */
|
yuuji@0
|
1882 elt->answered = T;
|
yuuji@0
|
1883 break;
|
yuuji@0
|
1884 case 'T': /* message is a draft */
|
yuuji@0
|
1885 elt->draft = T;
|
yuuji@0
|
1886 break;
|
yuuji@0
|
1887 default: /* some other crap */
|
yuuji@0
|
1888 break;
|
yuuji@0
|
1889 } while (*s && (*s != '\n') && ((*s != '\r') || (s[1] != '\n')));
|
yuuji@0
|
1890 break; /* all done */
|
yuuji@0
|
1891 }
|
yuuji@0
|
1892 /* otherwise fall into default case */
|
yuuji@0
|
1893
|
yuuji@0
|
1894 default: /* ordinary header line */
|
yuuji@0
|
1895 if ((*s == 'S') || (*s == 's') ||
|
yuuji@0
|
1896 (((*s == 'X') || (*s == 'x')) && (s[1] == '-'))) {
|
yuuji@0
|
1897 unsigned char *e,*v;
|
yuuji@0
|
1898 /* must match what mail_filter() does */
|
yuuji@0
|
1899 for (u = s,v = tmp,e = u + min (i,MAILTMPLEN - 1);
|
yuuji@0
|
1900 (u < e) && ((c = (*u ? *u : (*u = ' '))) != ':') &&
|
yuuji@0
|
1901 ((c > ' ') || ((c != ' ') && (c != '\t') &&
|
yuuji@0
|
1902 (c != '\r') && (c != '\n')));
|
yuuji@0
|
1903 *v++ = *u++);
|
yuuji@0
|
1904 *v = '\0'; /* tie off */
|
yuuji@0
|
1905 /* matches internal header? */
|
yuuji@0
|
1906 if (!compare_cstring (tmp,"STATUS") ||
|
yuuji@0
|
1907 !compare_cstring (tmp,"X-STATUS") ||
|
yuuji@0
|
1908 !compare_cstring (tmp,"X-KEYWORDS") ||
|
yuuji@0
|
1909 !compare_cstring (tmp,"X-UID") ||
|
yuuji@0
|
1910 !compare_cstring (tmp,"X-IMAP") ||
|
yuuji@0
|
1911 !compare_cstring (tmp,"X-IMAPBASE")) {
|
yuuji@0
|
1912 char err[MAILTMPLEN];
|
yuuji@0
|
1913 sprintf (err,"Discarding bogus %s header in message %lu",
|
yuuji@0
|
1914 (char *) tmp,elt->msgno);
|
yuuji@0
|
1915 MM_LOG (err,WARN);
|
yuuji@0
|
1916 retain = NIL; /* don't retain continuation */
|
yuuji@0
|
1917 break; /* different case or something */
|
yuuji@0
|
1918 }
|
yuuji@0
|
1919 }
|
yuuji@0
|
1920 /* retain or non-continuation? */
|
yuuji@0
|
1921 if (retain || ((*s != ' ') && (*s != '\t'))) {
|
yuuji@0
|
1922 retain = T; /* retaining continuation now */
|
yuuji@0
|
1923 /* line length in LF format newline */
|
yuuji@0
|
1924 for (j = k = 0; j < i; ++j) if (s[j] != '\r') ++k;
|
yuuji@0
|
1925 /* "internal" header size */
|
yuuji@0
|
1926 elt->private.spare.data += k;
|
yuuji@0
|
1927 /* message size */
|
yuuji@0
|
1928 elt->rfc822_size += k + 1;
|
yuuji@0
|
1929 }
|
yuuji@0
|
1930 else {
|
yuuji@0
|
1931 char err[MAILTMPLEN];
|
yuuji@0
|
1932 sprintf (err,"Discarding bogus continuation in msg %lu: %.80s",
|
yuuji@0
|
1933 elt->msgno,(char *) s);
|
yuuji@0
|
1934 if (u = strpbrk (err,"\r\n")) *u = '\0';
|
yuuji@0
|
1935 MM_LOG (err,WARN);
|
yuuji@0
|
1936 break; /* different case or something */
|
yuuji@0
|
1937 }
|
yuuji@0
|
1938 break;
|
yuuji@0
|
1939 }
|
yuuji@0
|
1940 } while (i && (*t != '\n') && ((*t != '\r') || (t[1] != '\n')));
|
yuuji@0
|
1941 /* "internal" header sans trailing newline */
|
yuuji@0
|
1942 if (i) elt->private.spare.data--;
|
yuuji@0
|
1943 /* assign a UID if none found */
|
yuuji@0
|
1944 if (((nmsgs > 1) || !pseudoseen) && !elt->private.uid) {
|
yuuji@0
|
1945 prevuid = elt->private.uid = ++stream->uid_last;
|
yuuji@0
|
1946 elt->private.dirty = T;
|
yuuji@0
|
1947 }
|
yuuji@0
|
1948 else elt->private.dirty = elt->recent;
|
yuuji@0
|
1949
|
yuuji@0
|
1950 /* note size of header, location of text */
|
yuuji@0
|
1951 elt->private.msg.header.text.size =
|
yuuji@0
|
1952 (elt->private.msg.text.offset =
|
yuuji@0
|
1953 (LOCAL->filesize + GETPOS (&bs)) - elt->private.special.offset) -
|
yuuji@0
|
1954 elt->private.special.text.size;
|
yuuji@0
|
1955 /* note current position */
|
yuuji@0
|
1956 j = LOCAL->filesize + GETPOS (&bs);
|
yuuji@0
|
1957 if (i) do { /* look for next message */
|
yuuji@0
|
1958 s = mmdf_mbxline (stream,&bs,&i);
|
yuuji@0
|
1959 if (i) { /* got new data? */
|
yuuji@0
|
1960 if (ISMMDF (s)) break;
|
yuuji@0
|
1961 else { /* not a header line, add it to message */
|
yuuji@0
|
1962 elt->rfc822_size += i;
|
yuuji@0
|
1963 for (j = 0; j < i; ++j) switch (s[j]) {
|
yuuji@0
|
1964 case '\r': /* squeeze out CRs */
|
yuuji@0
|
1965 elt->rfc822_size -= 1;
|
yuuji@0
|
1966 break;
|
yuuji@0
|
1967 case '\n': /* LF becomes CRLF */
|
yuuji@0
|
1968 elt->rfc822_size += 1;
|
yuuji@0
|
1969 break;
|
yuuji@0
|
1970 default:
|
yuuji@0
|
1971 break;
|
yuuji@0
|
1972 }
|
yuuji@0
|
1973 /* update current position */
|
yuuji@0
|
1974 j = LOCAL->filesize + GETPOS (&bs);
|
yuuji@0
|
1975 }
|
yuuji@0
|
1976 }
|
yuuji@0
|
1977 } while (i); /* until found a header */
|
yuuji@0
|
1978 elt->private.msg.text.text.size = j -
|
yuuji@0
|
1979 (elt->private.special.offset + elt->private.msg.text.offset);
|
yuuji@0
|
1980 if (i) { /* get next header line */
|
yuuji@0
|
1981 /* remember first internal header position */
|
yuuji@0
|
1982 j = LOCAL->filesize + GETPOS (&bs);
|
yuuji@0
|
1983 s = mmdf_mbxline (stream,&bs,&i);
|
yuuji@0
|
1984 }
|
yuuji@0
|
1985 /* until end of buffer */
|
yuuji@0
|
1986 } while (!stream->sniff && i);
|
yuuji@0
|
1987 if (pseudoseen) { /* flush pseudo-message if present */
|
yuuji@0
|
1988 /* decrement recent count */
|
yuuji@0
|
1989 if (mail_elt (stream,1)->recent) recent--;
|
yuuji@0
|
1990 /* and the exists count */
|
yuuji@0
|
1991 mail_exists (stream,nmsgs--);
|
yuuji@0
|
1992 mail_expunged(stream,1);/* fake an expunge of that message */
|
yuuji@0
|
1993 }
|
yuuji@0
|
1994 /* need to start a new UID validity? */
|
yuuji@0
|
1995 if (!stream->uid_validity) {
|
yuuji@0
|
1996 stream->uid_validity = time (0);
|
yuuji@0
|
1997 /* in case a whiner with no life */
|
yuuji@0
|
1998 if (mail_parameters (NIL,GET_USERHASNOLIFE,NIL))
|
yuuji@0
|
1999 stream->uid_nosticky = T;
|
yuuji@0
|
2000 else if (nmsgs) { /* don't bother if empty file */
|
yuuji@0
|
2001 /* make dirty to restart UID epoch */
|
yuuji@0
|
2002 LOCAL->ddirty = LOCAL->dirty = T;
|
yuuji@0
|
2003 /* need to rewrite msg 1 if not pseudo */
|
yuuji@0
|
2004 if (!LOCAL->pseudo) mail_elt (stream,1)->private.dirty = T;
|
yuuji@0
|
2005 MM_LOG ("Assigning new unique identifiers to all messages",NIL);
|
yuuji@0
|
2006 }
|
yuuji@0
|
2007 }
|
yuuji@0
|
2008 stream->nmsgs = oldnmsgs; /* whack it back down */
|
yuuji@0
|
2009 stream->silent = silent; /* restore old silent setting */
|
yuuji@0
|
2010 /* notify upper level of new mailbox sizes */
|
yuuji@0
|
2011 mail_exists (stream,nmsgs);
|
yuuji@0
|
2012 mail_recent (stream,recent);
|
yuuji@0
|
2013 /* mark dirty so O flags are set */
|
yuuji@0
|
2014 if (recent) LOCAL->dirty = T;
|
yuuji@0
|
2015 }
|
yuuji@0
|
2016 }
|
yuuji@0
|
2017 /* no change, don't babble if never got time */
|
yuuji@0
|
2018 else if (LOCAL->filetime && LOCAL->filetime != sbuf.st_mtime)
|
yuuji@0
|
2019 MM_LOG ("New mailbox modification time but apparently no changes",WARN);
|
yuuji@0
|
2020 /* update parsed file size and time */
|
yuuji@0
|
2021 LOCAL->filesize = sbuf.st_size;
|
yuuji@0
|
2022 LOCAL->filetime = sbuf.st_mtime;
|
yuuji@0
|
2023 return T; /* return the winnage */
|
yuuji@0
|
2024 }
|
yuuji@0
|
2025
|
yuuji@0
|
2026 /* MMDF read line from mailbox
|
yuuji@0
|
2027 * Accepts: mail stream
|
yuuji@0
|
2028 * stringstruct
|
yuuji@0
|
2029 * pointer to line size
|
yuuji@0
|
2030 * Returns: pointer to input line
|
yuuji@0
|
2031 */
|
yuuji@0
|
2032
|
yuuji@0
|
2033 char *mmdf_mbxline (MAILSTREAM *stream,STRING *bs,unsigned long *size)
|
yuuji@0
|
2034 {
|
yuuji@0
|
2035 unsigned long i,j,k,m;
|
yuuji@0
|
2036 char *s,*t,*te;
|
yuuji@0
|
2037 char *ret = "";
|
yuuji@0
|
2038 /* flush old buffer */
|
yuuji@0
|
2039 if (LOCAL->line) fs_give ((void **) &LOCAL->line);
|
yuuji@0
|
2040 /* if buffer needs refreshing */
|
yuuji@0
|
2041 if (!bs->cursize) SETPOS (bs,GETPOS (bs));
|
yuuji@0
|
2042 if (SIZE (bs)) { /* find newline */
|
yuuji@0
|
2043 /* end of fast scan */
|
yuuji@0
|
2044 te = (t = (s = bs->curpos) + bs->cursize) - 12;
|
yuuji@0
|
2045 while (s < te) if ((*s++ == '\n') || (*s++ == '\n') || (*s++ == '\n') ||
|
yuuji@0
|
2046 (*s++ == '\n') || (*s++ == '\n') || (*s++ == '\n') ||
|
yuuji@0
|
2047 (*s++ == '\n') || (*s++ == '\n') || (*s++ == '\n') ||
|
yuuji@0
|
2048 (*s++ == '\n') || (*s++ == '\n') || (*s++ == '\n')) {
|
yuuji@0
|
2049 --s; /* back up */
|
yuuji@0
|
2050 break; /* exit loop */
|
yuuji@0
|
2051 }
|
yuuji@0
|
2052 /* final character-at-a-time scan */
|
yuuji@0
|
2053 while ((s < t) && (*s != '\n')) ++s;
|
yuuji@0
|
2054 /* difficult case if line spans buffer */
|
yuuji@0
|
2055 if ((i = s - bs->curpos) == bs->cursize) {
|
yuuji@0
|
2056 /* have space in line buffer? */
|
yuuji@0
|
2057 if (i > LOCAL->linebuflen) {
|
yuuji@0
|
2058 fs_give ((void **) &LOCAL->linebuf);
|
yuuji@0
|
2059 LOCAL->linebuf = (char *) fs_get (LOCAL->linebuflen = i);
|
yuuji@0
|
2060 }
|
yuuji@0
|
2061 /* remember what we have so far */
|
yuuji@0
|
2062 memcpy (LOCAL->linebuf,bs->curpos,i);
|
yuuji@0
|
2063 /* load next buffer */
|
yuuji@0
|
2064 SETPOS (bs,k = GETPOS (bs) + i);
|
yuuji@0
|
2065 /* end of fast scan */
|
yuuji@0
|
2066 te = (t = (s = bs->curpos) + bs->cursize) - 12;
|
yuuji@0
|
2067 /* fast scan in overlap buffer */
|
yuuji@0
|
2068 while (s < te) if ((*s++ == '\n') || (*s++ == '\n') || (*s++ == '\n') ||
|
yuuji@0
|
2069 (*s++ == '\n') || (*s++ == '\n') || (*s++ == '\n') ||
|
yuuji@0
|
2070 (*s++ == '\n') || (*s++ == '\n') || (*s++ == '\n') ||
|
yuuji@0
|
2071 (*s++ == '\n') || (*s++ == '\n') || (*s++ == '\n')) {
|
yuuji@0
|
2072 --s; /* back up */
|
yuuji@0
|
2073 break; /* exit loop */
|
yuuji@0
|
2074 }
|
yuuji@0
|
2075
|
yuuji@0
|
2076 /* final character-at-a-time scan */
|
yuuji@0
|
2077 while ((s < t) && (*s != '\n')) ++s;
|
yuuji@0
|
2078 /* huge line? */
|
yuuji@0
|
2079 if ((j = s - bs->curpos) == bs->cursize) {
|
yuuji@0
|
2080 SETPOS (bs,GETPOS (bs) + j);
|
yuuji@0
|
2081 /* look for end of line (s-l-o-w!!) */
|
yuuji@0
|
2082 for (m = SIZE (bs); m && (SNX (bs) != '\n'); --m,++j);
|
yuuji@0
|
2083 SETPOS (bs,k); /* go back to where it started */
|
yuuji@0
|
2084 }
|
yuuji@0
|
2085 /* got size of data, make buffer for return */
|
yuuji@0
|
2086 ret = LOCAL->line = (char *) fs_get (i + j + 2);
|
yuuji@0
|
2087 /* copy first chunk */
|
yuuji@0
|
2088 memcpy (ret,LOCAL->linebuf,i);
|
yuuji@0
|
2089 while (j) { /* copy remainder */
|
yuuji@0
|
2090 if (!bs->cursize) SETPOS (bs,GETPOS (bs));
|
yuuji@0
|
2091 memcpy (ret + i,bs->curpos,k = min (j,bs->cursize));
|
yuuji@0
|
2092 i += k; /* account for this much read in */
|
yuuji@0
|
2093 j -= k;
|
yuuji@0
|
2094 bs->curpos += k; /* increment new position */
|
yuuji@0
|
2095 bs->cursize -= k; /* eat that many bytes */
|
yuuji@0
|
2096 }
|
yuuji@0
|
2097 /* read newline at end */
|
yuuji@0
|
2098 if (SIZE (bs)) ret[i++] = SNX (bs);
|
yuuji@0
|
2099 ret[i] = '\0'; /* makes debugging easier */
|
yuuji@0
|
2100 }
|
yuuji@0
|
2101 else { /* this is easy */
|
yuuji@0
|
2102 ret = bs->curpos; /* string it at this position */
|
yuuji@0
|
2103 bs->curpos += ++i; /* increment new position */
|
yuuji@0
|
2104 bs->cursize -= i; /* eat that many bytes */
|
yuuji@0
|
2105 }
|
yuuji@0
|
2106 *size = i; /* return that to user */
|
yuuji@0
|
2107 }
|
yuuji@0
|
2108 else *size = 0; /* end of data, return empty */
|
yuuji@0
|
2109 /* embedded MMDF header at end of line? */
|
yuuji@0
|
2110 if ((*size > sizeof (MMDFHDRTXT)) &&
|
yuuji@0
|
2111 (s = ret + *size - (i = sizeof (MMDFHDRTXT) - 1)) && ISMMDF (s)) {
|
yuuji@0
|
2112 SETPOS (bs,GETPOS (bs) - i);/* back up to start of MMDF header */
|
yuuji@0
|
2113 *size -= i; /* reduce length of line */
|
yuuji@0
|
2114 ret[*size - 1] = '\n'; /* force newline at end */
|
yuuji@0
|
2115 }
|
yuuji@0
|
2116 return ret;
|
yuuji@0
|
2117 }
|
yuuji@0
|
2118
|
yuuji@0
|
2119 /* MMDF make pseudo-header
|
yuuji@0
|
2120 * Accepts: MAIL stream
|
yuuji@0
|
2121 * buffer to write pseudo-header
|
yuuji@0
|
2122 * Returns: length of pseudo-header
|
yuuji@0
|
2123 */
|
yuuji@0
|
2124
|
yuuji@0
|
2125 unsigned long mmdf_pseudo (MAILSTREAM *stream,char *hdr)
|
yuuji@0
|
2126 {
|
yuuji@0
|
2127 int i;
|
yuuji@0
|
2128 char *s,tmp[MAILTMPLEN];
|
yuuji@0
|
2129 time_t now = time (0);
|
yuuji@0
|
2130 rfc822_fixed_date (tmp);
|
yuuji@0
|
2131 sprintf (hdr,"%sFrom %s %.24s\nDate: %s\nFrom: %s <%s@%.80s>\nSubject: %s\nMessage-ID: <%lu@%.80s>\nX-IMAP: %010lu %010lu",
|
yuuji@0
|
2132 mmdfhdr,pseudo_from,ctime (&now),
|
yuuji@0
|
2133 tmp,pseudo_name,pseudo_from,mylocalhost (),pseudo_subject,
|
yuuji@0
|
2134 (unsigned long) now,mylocalhost (),stream->uid_validity,
|
yuuji@0
|
2135 stream->uid_last);
|
yuuji@0
|
2136 for (s = hdr + strlen (hdr),i = 0; i < NUSERFLAGS; ++i)
|
yuuji@0
|
2137 if (stream->user_flags[i])
|
yuuji@0
|
2138 sprintf (s += strlen (s)," %s",stream->user_flags[i]);
|
yuuji@0
|
2139 sprintf (s += strlen (s),"\nStatus: RO\n\n%s\n%s",pseudo_msg,mmdfhdr);
|
yuuji@0
|
2140 return strlen (hdr);
|
yuuji@0
|
2141 }
|
yuuji@0
|
2142
|
yuuji@0
|
2143 /* MMDF make status string
|
yuuji@0
|
2144 * Accepts: MAIL stream
|
yuuji@0
|
2145 * destination string to write
|
yuuji@0
|
2146 * message cache entry
|
yuuji@0
|
2147 * UID to write if non-zero (else use elt->private.uid)
|
yuuji@0
|
2148 * non-zero flag to write UID (.LT. 0 to write UID base info too)
|
yuuji@0
|
2149 * Returns: length of string
|
yuuji@0
|
2150 */
|
yuuji@0
|
2151
|
yuuji@0
|
2152 unsigned long mmdf_xstatus (MAILSTREAM *stream,char *status,MESSAGECACHE *elt,
|
yuuji@0
|
2153 unsigned long uid,long flag)
|
yuuji@0
|
2154 {
|
yuuji@0
|
2155 char *t,stack[64];
|
yuuji@0
|
2156 char *s = status;
|
yuuji@0
|
2157 unsigned long n;
|
yuuji@0
|
2158 int pad = 50;
|
yuuji@0
|
2159 int sticky = uid ? T : !stream->uid_nosticky;
|
yuuji@0
|
2160 /* This used to use sprintf(), but thanks to certain cretinous C libraries
|
yuuji@0
|
2161 with horribly slow implementations of sprintf() I had to change it to this
|
yuuji@0
|
2162 mess. At least it should be fast. */
|
yuuji@0
|
2163 if ((flag < 0) && sticky) { /* need to write X-IMAPbase: header? */
|
yuuji@0
|
2164 *s++ = 'X'; *s++ = '-'; *s++ = 'I'; *s++ = 'M'; *s++ = 'A'; *s++ = 'P';
|
yuuji@0
|
2165 *s++ = 'b'; *s++ = 'a'; *s++ = 's'; *s++ = 'e'; *s++ = ':'; *s++ = ' ';
|
yuuji@0
|
2166 t = stack;
|
yuuji@0
|
2167 n = stream->uid_validity; /* push UID validity digits on the stack */
|
yuuji@0
|
2168 do *t++ = (char) (n % 10) + '0';
|
yuuji@0
|
2169 while (n /= 10);
|
yuuji@0
|
2170 /* pop UID validity digits from stack */
|
yuuji@0
|
2171 while (t > stack) *s++ = *--t;
|
yuuji@0
|
2172 *s++ = ' ';
|
yuuji@0
|
2173 n = stream->uid_last; /* push UID last digits on the stack */
|
yuuji@0
|
2174 do *t++ = (char) (n % 10) + '0';
|
yuuji@0
|
2175 while (n /= 10);
|
yuuji@0
|
2176 /* pop UID last digits from stack */
|
yuuji@0
|
2177 while (t > stack) *s++ = *--t;
|
yuuji@0
|
2178 for (n = 0; n < NUSERFLAGS; ++n) if (t = stream->user_flags[n])
|
yuuji@0
|
2179 for (*s++ = ' '; *t; *s++ = *t++);
|
yuuji@0
|
2180 *s++ = '\n';
|
yuuji@0
|
2181 pad += 30; /* increased padding if have IMAPbase */
|
yuuji@0
|
2182 }
|
yuuji@0
|
2183 *s++ = 'S'; *s++ = 't'; *s++ = 'a'; *s++ = 't'; *s++ = 'u'; *s++ = 's';
|
yuuji@0
|
2184 *s++ = ':'; *s++ = ' ';
|
yuuji@0
|
2185 if (elt->seen) *s++ = 'R';
|
yuuji@0
|
2186 /* only write O if have a UID */
|
yuuji@0
|
2187 if (flag && (!elt->recent || !LOCAL->appending)) *s++ = 'O';
|
yuuji@0
|
2188 *s++ = '\n';
|
yuuji@0
|
2189 *s++ = 'X'; *s++ = '-'; *s++ = 'S'; *s++ = 't'; *s++ = 'a'; *s++ = 't';
|
yuuji@0
|
2190 *s++ = 'u'; *s++ = 's'; *s++ = ':'; *s++ = ' ';
|
yuuji@0
|
2191 if (elt->deleted) *s++ = 'D';
|
yuuji@0
|
2192 if (elt->flagged) *s++ = 'F';
|
yuuji@0
|
2193 if (elt->answered) *s++ = 'A';
|
yuuji@0
|
2194 if (elt->draft) *s++ = 'T';
|
yuuji@0
|
2195 *s++ = '\n';
|
yuuji@0
|
2196
|
yuuji@0
|
2197 if (sticky) { /* only do this if UIDs sticky */
|
yuuji@0
|
2198 *s++ = 'X'; *s++ = '-'; *s++ = 'K'; *s++ = 'e'; *s++ = 'y'; *s++ = 'w';
|
yuuji@0
|
2199 *s++ = 'o'; *s++ = 'r'; *s++ = 'd'; *s++ = 's'; *s++ = ':';
|
yuuji@0
|
2200 if (n = elt->user_flags) do {
|
yuuji@0
|
2201 *s++ = ' ';
|
yuuji@0
|
2202 for (t = stream->user_flags[find_rightmost_bit (&n)]; *t; *s++ = *t++);
|
yuuji@0
|
2203 } while (n);
|
yuuji@0
|
2204 n = s - status; /* get size of stuff so far */
|
yuuji@0
|
2205 /* pad X-Keywords to make size constant */
|
yuuji@0
|
2206 if (n < pad) for (n = pad - n; n > 0; --n) *s++ = ' ';
|
yuuji@0
|
2207 *s++ = '\n';
|
yuuji@0
|
2208 if (flag) { /* want to include UID? */
|
yuuji@0
|
2209 t = stack;
|
yuuji@0
|
2210 /* push UID digits on the stack */
|
yuuji@0
|
2211 n = uid ? uid : elt->private.uid;
|
yuuji@0
|
2212 do *t++ = (char) (n % 10) + '0';
|
yuuji@0
|
2213 while (n /= 10);
|
yuuji@0
|
2214 *s++ = 'X'; *s++ = '-'; *s++ = 'U'; *s++ = 'I'; *s++ = 'D'; *s++ = ':';
|
yuuji@0
|
2215 *s++ = ' ';
|
yuuji@0
|
2216 /* pop UID from stack */
|
yuuji@0
|
2217 while (t > stack) *s++ = *--t;
|
yuuji@0
|
2218 *s++ = '\n';
|
yuuji@0
|
2219 }
|
yuuji@0
|
2220 }
|
yuuji@0
|
2221 *s++ = '\n'; *s = '\0'; /* end of extended message status */
|
yuuji@0
|
2222 return s - status; /* return size of resulting string */
|
yuuji@0
|
2223 }
|
yuuji@0
|
2224
|
yuuji@0
|
2225 /* Rewrite mailbox file
|
yuuji@0
|
2226 * Accepts: MAIL stream, must be critical and locked
|
yuuji@0
|
2227 * return pointer to number of expunged messages if want expunge
|
yuuji@0
|
2228 * lock file name
|
yuuji@0
|
2229 * expunge sequence, not deleted flag
|
yuuji@0
|
2230 * Returns: T if success and mailbox unlocked, NIL if failure
|
yuuji@0
|
2231 */
|
yuuji@0
|
2232
|
yuuji@0
|
2233 #define OVERFLOWBUFLEN 8192 /* initial overflow buffer length */
|
yuuji@0
|
2234
|
yuuji@0
|
2235 long mmdf_rewrite (MAILSTREAM *stream,unsigned long *nexp,DOTLOCK *lock,
|
yuuji@0
|
2236 long flags)
|
yuuji@0
|
2237 {
|
yuuji@0
|
2238 MESSAGECACHE *elt;
|
yuuji@0
|
2239 MMDFFILE f;
|
yuuji@0
|
2240 char *s;
|
yuuji@0
|
2241 time_t tp[2];
|
yuuji@0
|
2242 long ret,flag;
|
yuuji@0
|
2243 unsigned long i,j;
|
yuuji@0
|
2244 unsigned long recent = stream->recent;
|
yuuji@0
|
2245 unsigned long size = LOCAL->pseudo ? mmdf_pseudo (stream,LOCAL->buf) : 0;
|
yuuji@0
|
2246 if (nexp) *nexp = 0; /* initially nothing expunged */
|
yuuji@0
|
2247 /* calculate size of mailbox after rewrite */
|
yuuji@0
|
2248 for (i = 1,flag = LOCAL->pseudo ? 1 : -1; i <= stream->nmsgs; i++) {
|
yuuji@0
|
2249 elt = mail_elt (stream,i); /* get cache */
|
yuuji@0
|
2250 if (!(nexp && elt->deleted && (flags ? elt->sequence : T))) {
|
yuuji@0
|
2251 /* add RFC822 size of this message */
|
yuuji@0
|
2252 size += elt->private.special.text.size + elt->private.spare.data +
|
yuuji@0
|
2253 mmdf_xstatus (stream,LOCAL->buf,elt,NIL,flag) +
|
yuuji@0
|
2254 elt->private.msg.text.text.size + MMDFHDRLEN;
|
yuuji@0
|
2255 flag = 1; /* only count X-IMAPbase once */
|
yuuji@0
|
2256 }
|
yuuji@0
|
2257 }
|
yuuji@0
|
2258 /* no messages, has a life, and no pseudo */
|
yuuji@0
|
2259 if (!size && !mail_parameters (NIL,GET_USERHASNOLIFE,NIL)) {
|
yuuji@0
|
2260 LOCAL->pseudo = T; /* so make a pseudo-message now */
|
yuuji@0
|
2261 size = mmdf_pseudo (stream,LOCAL->buf);
|
yuuji@0
|
2262 }
|
yuuji@0
|
2263 /* extend the file as necessary */
|
yuuji@0
|
2264 if (ret = mmdf_extend (stream,size)) {
|
yuuji@0
|
2265 /* Set up buffered I/O file structure
|
yuuji@0
|
2266 * curpos current position being written through buffering
|
yuuji@0
|
2267 * filepos current position being written physically to the disk
|
yuuji@0
|
2268 * bufpos current position being written in the buffer
|
yuuji@0
|
2269 * protect current maximum position that can be written to the disk
|
yuuji@0
|
2270 * before buffering is forced
|
yuuji@0
|
2271 * The code tries to buffer so that that disk is written in multiples of
|
yuuji@0
|
2272 * OVERBLOWBUFLEN bytes.
|
yuuji@0
|
2273 */
|
yuuji@0
|
2274 f.stream = stream; /* note mail stream */
|
yuuji@0
|
2275 f.curpos = f.filepos = 0; /* start of file */
|
yuuji@0
|
2276 f.protect = stream->nmsgs ? /* initial protection pointer */
|
yuuji@0
|
2277 mail_elt (stream,1)->private.special.offset : 8192;
|
yuuji@0
|
2278 f.bufpos = f.buf = (char *) fs_get (f.buflen = OVERFLOWBUFLEN);
|
yuuji@0
|
2279
|
yuuji@0
|
2280 if (LOCAL->pseudo) /* update pseudo-header */
|
yuuji@0
|
2281 mmdf_write (&f,LOCAL->buf,mmdf_pseudo (stream,LOCAL->buf));
|
yuuji@0
|
2282 /* loop through all messages */
|
yuuji@0
|
2283 for (i = 1,flag = LOCAL->pseudo ? 1 : -1; i <= stream->nmsgs;) {
|
yuuji@0
|
2284 elt = mail_elt (stream,i);/* get cache */
|
yuuji@0
|
2285 /* expunge this message? */
|
yuuji@0
|
2286 if (nexp && elt->deleted && (flags ? elt->sequence : T)) {
|
yuuji@0
|
2287 /* one less recent message */
|
yuuji@0
|
2288 if (elt->recent) --recent;
|
yuuji@0
|
2289 mail_expunged(stream,i);/* notify upper levels */
|
yuuji@0
|
2290 ++*nexp; /* count up one more expunged message */
|
yuuji@0
|
2291 }
|
yuuji@0
|
2292 else { /* preserve this message */
|
yuuji@0
|
2293 i++; /* advance to next message */
|
yuuji@0
|
2294 if ((flag < 0) || /* need to rewrite message? */
|
yuuji@0
|
2295 elt->private.dirty || (f.curpos != elt->private.special.offset) ||
|
yuuji@0
|
2296 (elt->private.msg.header.text.size !=
|
yuuji@0
|
2297 (elt->private.spare.data +
|
yuuji@0
|
2298 mmdf_xstatus (stream,LOCAL->buf,elt,NIL,flag)))) {
|
yuuji@0
|
2299 unsigned long newoffset = f.curpos;
|
yuuji@0
|
2300 /* yes, seek to internal header */
|
yuuji@0
|
2301 lseek (LOCAL->fd,elt->private.special.offset,L_SET);
|
yuuji@0
|
2302 read (LOCAL->fd,LOCAL->buf,elt->private.special.text.size);
|
yuuji@0
|
2303 /* see if need to squeeze out a CR */
|
yuuji@0
|
2304 if (LOCAL->buf[elt->private.special.text.size - 2] == '\r') {
|
yuuji@0
|
2305 LOCAL->buf[--elt->private.special.text.size - 1] = '\n';
|
yuuji@0
|
2306 --size; /* squeezed out a CR from PC */
|
yuuji@0
|
2307 }
|
yuuji@0
|
2308 /* protection pointer moves to RFC822 header */
|
yuuji@0
|
2309 f.protect = elt->private.special.offset +
|
yuuji@0
|
2310 elt->private.msg.header.offset;
|
yuuji@0
|
2311 /* write internal header */
|
yuuji@0
|
2312 mmdf_write (&f,LOCAL->buf,elt->private.special.text.size);
|
yuuji@0
|
2313 /* get RFC822 header */
|
yuuji@0
|
2314 s = mmdf_header (stream,elt->msgno,&j,FT_INTERNAL);
|
yuuji@0
|
2315 /* in case this got decremented */
|
yuuji@0
|
2316 elt->private.msg.header.offset = elt->private.special.text.size;
|
yuuji@0
|
2317 /* header size, sans trailing newline */
|
yuuji@0
|
2318 if ((j < 2) || (s[j - 2] == '\n')) j--;
|
yuuji@0
|
2319 /* this can happen if CRs were squeezed */
|
yuuji@0
|
2320 if (j < elt->private.spare.data) {
|
yuuji@0
|
2321 /* so fix up counts */
|
yuuji@0
|
2322 size -= elt->private.spare.data - j;
|
yuuji@0
|
2323 elt->private.spare.data = j;
|
yuuji@0
|
2324 }
|
yuuji@0
|
2325 else if (j != elt->private.spare.data)
|
yuuji@0
|
2326 fatal ("header size inconsistent");
|
yuuji@0
|
2327 /* protection pointer moves to RFC822 text */
|
yuuji@0
|
2328 f.protect = elt->private.special.offset +
|
yuuji@0
|
2329 elt->private.msg.text.offset;
|
yuuji@0
|
2330 mmdf_write (&f,s,j); /* write RFC822 header */
|
yuuji@0
|
2331 /* write status and UID */
|
yuuji@0
|
2332 mmdf_write (&f,LOCAL->buf,
|
yuuji@0
|
2333 j = mmdf_xstatus (stream,LOCAL->buf,elt,NIL,flag));
|
yuuji@0
|
2334 flag = 1; /* only write X-IMAPbase once */
|
yuuji@0
|
2335 /* new file header size */
|
yuuji@0
|
2336 elt->private.msg.header.text.size = elt->private.spare.data + j;
|
yuuji@0
|
2337
|
yuuji@0
|
2338 /* did text move? */
|
yuuji@0
|
2339 if (f.curpos != f.protect) {
|
yuuji@0
|
2340 /* get message text */
|
yuuji@0
|
2341 s = mmdf_text_work (stream,elt,&j,FT_INTERNAL);
|
yuuji@0
|
2342 /* this can happen if CRs were squeezed */
|
yuuji@0
|
2343 if (j < elt->private.msg.text.text.size) {
|
yuuji@0
|
2344 /* so fix up counts */
|
yuuji@0
|
2345 size -= elt->private.msg.text.text.size - j;
|
yuuji@0
|
2346 elt->private.msg.text.text.size = j;
|
yuuji@0
|
2347 }
|
yuuji@0
|
2348 /* can't happen it says here */
|
yuuji@0
|
2349 else if (j > elt->private.msg.text.text.size)
|
yuuji@0
|
2350 fatal ("text size inconsistent");
|
yuuji@0
|
2351 /* new text offset, status/UID may change it */
|
yuuji@0
|
2352 elt->private.msg.text.offset = f.curpos - newoffset;
|
yuuji@0
|
2353 /* protection pointer moves to next message */
|
yuuji@0
|
2354 f.protect = (i <= stream->nmsgs) ?
|
yuuji@0
|
2355 mail_elt (stream,i)->private.special.offset :
|
yuuji@0
|
2356 (f.curpos + j + MMDFHDRLEN);
|
yuuji@0
|
2357 mmdf_write (&f,s,j);/* write text */
|
yuuji@0
|
2358 /* write trailing newline */
|
yuuji@0
|
2359 mmdf_write (&f,mmdfhdr,MMDFHDRLEN);
|
yuuji@0
|
2360 }
|
yuuji@0
|
2361 else { /* tie off header and status */
|
yuuji@0
|
2362 mmdf_write (&f,NIL,NIL);
|
yuuji@0
|
2363 f.curpos = f.protect =/* restart everything at end of message */
|
yuuji@0
|
2364 f.filepos += elt->private.msg.text.text.size + MMDFHDRLEN;
|
yuuji@0
|
2365 }
|
yuuji@0
|
2366 /* new internal header offset */
|
yuuji@0
|
2367 elt->private.special.offset = newoffset;
|
yuuji@0
|
2368 elt->private.dirty =NIL;/* message is now clean */
|
yuuji@0
|
2369 }
|
yuuji@0
|
2370 else { /* no need to rewrite this message */
|
yuuji@0
|
2371 /* tie off previous message if needed */
|
yuuji@0
|
2372 mmdf_write (&f,NIL,NIL);
|
yuuji@0
|
2373 f.curpos = f.protect =/* restart everything at end of message */
|
yuuji@0
|
2374 f.filepos += elt->private.special.text.size +
|
yuuji@0
|
2375 elt->private.msg.header.text.size +
|
yuuji@0
|
2376 elt->private.msg.text.text.size + MMDFHDRLEN;
|
yuuji@0
|
2377 }
|
yuuji@0
|
2378 }
|
yuuji@0
|
2379 }
|
yuuji@0
|
2380
|
yuuji@0
|
2381 mmdf_write (&f,NIL,NIL); /* tie off final message */
|
yuuji@0
|
2382 if (size != f.filepos) fatal ("file size inconsistent");
|
yuuji@0
|
2383 fs_give ((void **) &f.buf); /* free buffer */
|
yuuji@0
|
2384 /* make sure tied off */
|
yuuji@0
|
2385 ftruncate (LOCAL->fd,LOCAL->filesize = size);
|
yuuji@0
|
2386 fsync (LOCAL->fd); /* make sure the updates take */
|
yuuji@0
|
2387 if (size && (flag < 0)) fatal ("lost UID base information");
|
yuuji@0
|
2388 /* no longer dirty */
|
yuuji@0
|
2389 LOCAL->ddirty = LOCAL->dirty = NIL;
|
yuuji@0
|
2390 /* notify upper level of new mailbox sizes */
|
yuuji@0
|
2391 mail_exists (stream,stream->nmsgs);
|
yuuji@0
|
2392 mail_recent (stream,recent);
|
yuuji@0
|
2393 /* set atime to now, mtime a second earlier */
|
yuuji@0
|
2394 tp[1] = (tp[0] = time (0)) - 1;
|
yuuji@0
|
2395 /* set the times, note change */
|
yuuji@0
|
2396 if (!utime (stream->mailbox,tp)) LOCAL->filetime = tp[1];
|
yuuji@0
|
2397 close (LOCAL->fd); /* close and reopen file */
|
yuuji@0
|
2398 if ((LOCAL->fd = open (stream->mailbox,O_RDWR,
|
yuuji@0
|
2399 (long) mail_parameters (NIL,GET_MBXPROTECTION,NIL)))
|
yuuji@0
|
2400 < 0) {
|
yuuji@0
|
2401 sprintf (LOCAL->buf,"Mailbox open failed, aborted: %s",strerror (errno));
|
yuuji@0
|
2402 MM_LOG (LOCAL->buf,ERROR);
|
yuuji@0
|
2403 mmdf_abort (stream);
|
yuuji@0
|
2404 }
|
yuuji@0
|
2405 dotlock_unlock (lock); /* flush the lock file */
|
yuuji@0
|
2406 }
|
yuuji@0
|
2407 return ret; /* return state from algorithm */
|
yuuji@0
|
2408 }
|
yuuji@0
|
2409
|
yuuji@0
|
2410 /* Extend MMDF mailbox file
|
yuuji@0
|
2411 * Accepts: MAIL stream
|
yuuji@0
|
2412 * new desired size
|
yuuji@0
|
2413 * Return: T if success, else NIL
|
yuuji@0
|
2414 */
|
yuuji@0
|
2415
|
yuuji@0
|
2416 long mmdf_extend (MAILSTREAM *stream,unsigned long size)
|
yuuji@0
|
2417 {
|
yuuji@0
|
2418 unsigned long i = (size > LOCAL->filesize) ? size - LOCAL->filesize : 0;
|
yuuji@0
|
2419 if (i) { /* does the mailbox need to grow? */
|
yuuji@0
|
2420 if (i > LOCAL->buflen) { /* make sure have enough space */
|
yuuji@0
|
2421 /* this user won the lottery all right */
|
yuuji@0
|
2422 fs_give ((void **) &LOCAL->buf);
|
yuuji@0
|
2423 LOCAL->buf = (char *) fs_get ((LOCAL->buflen = i) + 1);
|
yuuji@0
|
2424 }
|
yuuji@0
|
2425 memset (LOCAL->buf,'\0',i); /* get a block of nulls */
|
yuuji@0
|
2426 while (T) { /* until write successful or punt */
|
yuuji@0
|
2427 lseek (LOCAL->fd,LOCAL->filesize,L_SET);
|
yuuji@0
|
2428 if ((write (LOCAL->fd,LOCAL->buf,i) >= 0) && !fsync (LOCAL->fd)) break;
|
yuuji@0
|
2429 else {
|
yuuji@0
|
2430 long e = errno; /* note error before doing ftruncate */
|
yuuji@0
|
2431 ftruncate (LOCAL->fd,LOCAL->filesize);
|
yuuji@0
|
2432 if (MM_DISKERROR (stream,e,NIL)) {
|
yuuji@0
|
2433 fsync (LOCAL->fd); /* user chose to punt */
|
yuuji@0
|
2434 sprintf (LOCAL->buf,"Unable to extend mailbox: %s",strerror (e));
|
yuuji@0
|
2435 if (!stream->silent) MM_LOG (LOCAL->buf,ERROR);
|
yuuji@0
|
2436 return NIL;
|
yuuji@0
|
2437 }
|
yuuji@0
|
2438 }
|
yuuji@0
|
2439 }
|
yuuji@0
|
2440 }
|
yuuji@0
|
2441 return LONGT;
|
yuuji@0
|
2442 }
|
yuuji@0
|
2443
|
yuuji@0
|
2444 /* Write data to buffered file
|
yuuji@0
|
2445 * Accepts: buffered file pointer
|
yuuji@0
|
2446 * file data or NIL to indicate "flush buffer"
|
yuuji@0
|
2447 * date size (ignored for "flush buffer")
|
yuuji@0
|
2448 * Does not return until success
|
yuuji@0
|
2449 */
|
yuuji@0
|
2450
|
yuuji@0
|
2451 void mmdf_write (MMDFFILE *f,char *buf,unsigned long size)
|
yuuji@0
|
2452 {
|
yuuji@0
|
2453 unsigned long i,j,k;
|
yuuji@0
|
2454 if (buf) { /* doing buffered write? */
|
yuuji@0
|
2455 i = f->bufpos - f->buf; /* yes, get size of current buffer data */
|
yuuji@0
|
2456 /* yes, have space in current buffer chunk? */
|
yuuji@0
|
2457 if (j = i ? ((f->buflen - i) % OVERFLOWBUFLEN) : f->buflen) {
|
yuuji@0
|
2458 /* yes, fill up buffer as much as we can */
|
yuuji@0
|
2459 memcpy (f->bufpos,buf,k = min (j,size));
|
yuuji@0
|
2460 f->bufpos += k; /* new buffer position */
|
yuuji@0
|
2461 f->curpos += k; /* new current position */
|
yuuji@0
|
2462 if (j -= k) return; /* all done if still have buffer free space */
|
yuuji@0
|
2463 buf += k; /* full, get new unwritten data pointer */
|
yuuji@0
|
2464 size -= k; /* new data size */
|
yuuji@0
|
2465 i += k; /* new buffer data size */
|
yuuji@0
|
2466 }
|
yuuji@0
|
2467 /* This chunk of the buffer is full. See if can make some space by
|
yuuji@0
|
2468 * writing to the disk, if there's enough unprotected space to do so.
|
yuuji@0
|
2469 * Try to fill out any unaligned chunk, along with any subsequent full
|
yuuji@0
|
2470 * chunks that will fit in unprotected space.
|
yuuji@0
|
2471 */
|
yuuji@0
|
2472 /* any unprotected space we can write to? */
|
yuuji@0
|
2473 if (j = min (i,f->protect - f->filepos)) {
|
yuuji@0
|
2474 /* yes, filepos not at chunk boundary? */
|
yuuji@0
|
2475 if ((k = f->filepos % OVERFLOWBUFLEN) && ((k = OVERFLOWBUFLEN - k) < j))
|
yuuji@0
|
2476 j -= k; /* yes, and can write out partial chunk */
|
yuuji@0
|
2477 else k = 0; /* no partial chunk to write */
|
yuuji@0
|
2478 /* if at least a chunk free, write that too */
|
yuuji@0
|
2479 if (j > OVERFLOWBUFLEN) k += j - (j % OVERFLOWBUFLEN);
|
yuuji@0
|
2480 if (k) { /* write data if there is anything we can */
|
yuuji@0
|
2481 mmdf_phys_write (f,f->buf,k);
|
yuuji@0
|
2482 /* slide buffer */
|
yuuji@0
|
2483 if (i -= k) memmove (f->buf,f->buf + k,i);
|
yuuji@0
|
2484 f->bufpos = f->buf + i; /* new end of buffer */
|
yuuji@0
|
2485 }
|
yuuji@0
|
2486 }
|
yuuji@0
|
2487
|
yuuji@0
|
2488 /* Have flushed the buffer as best as possible. All done if no more
|
yuuji@0
|
2489 * data to write. Otherwise, if the buffer is empty AND if the unwritten
|
yuuji@0
|
2490 * data is larger than a chunk AND the unprotected space is also larger
|
yuuji@0
|
2491 * than a chunk, then write as many chunks as we can directly from the
|
yuuji@0
|
2492 * data. Buffer the rest, expanding the buffer as needed.
|
yuuji@0
|
2493 */
|
yuuji@0
|
2494 if (size) { /* have more data that we need to buffer? */
|
yuuji@0
|
2495 /* can write any of it to disk instead? */
|
yuuji@0
|
2496 if ((f->bufpos == f->buf) &&
|
yuuji@0
|
2497 ((j = min (f->protect - f->filepos,size)) > OVERFLOWBUFLEN)) {
|
yuuji@0
|
2498 /* write as much as we can right now */
|
yuuji@0
|
2499 mmdf_phys_write (f,buf,j -= (j % OVERFLOWBUFLEN));
|
yuuji@0
|
2500 buf += j; /* new data pointer */
|
yuuji@0
|
2501 size -= j; /* new data size */
|
yuuji@0
|
2502 f->curpos += j; /* advance current pointer */
|
yuuji@0
|
2503 }
|
yuuji@0
|
2504 if (size) { /* still have data that we need to buffer? */
|
yuuji@0
|
2505 /* yes, need to expand the buffer? */
|
yuuji@0
|
2506 if ((i = ((f->bufpos + size) - f->buf)) > f->buflen) {
|
yuuji@0
|
2507 /* note current position in buffer */
|
yuuji@0
|
2508 j = f->bufpos - f->buf;
|
yuuji@0
|
2509 i += OVERFLOWBUFLEN; /* yes, grow another chunk */
|
yuuji@0
|
2510 fs_resize ((void **) &f->buf,f->buflen = i - (i % OVERFLOWBUFLEN));
|
yuuji@0
|
2511 /* in case buffer relocated */
|
yuuji@0
|
2512 f->bufpos = f->buf + j;
|
yuuji@0
|
2513 }
|
yuuji@0
|
2514 /* buffer remaining data */
|
yuuji@0
|
2515 memcpy (f->bufpos,buf,size);
|
yuuji@0
|
2516 f->bufpos += size; /* new end of buffer */
|
yuuji@0
|
2517 f->curpos += size; /* advance current pointer */
|
yuuji@0
|
2518 }
|
yuuji@0
|
2519 }
|
yuuji@0
|
2520 }
|
yuuji@0
|
2521 else { /* flush buffer to disk */
|
yuuji@0
|
2522 mmdf_phys_write (f,f->buf,i = f->bufpos - f->buf);
|
yuuji@0
|
2523 f->bufpos = f->buf; /* reset buffer */
|
yuuji@0
|
2524 /* update positions */
|
yuuji@0
|
2525 f->curpos = f->protect = f->filepos;
|
yuuji@0
|
2526 }
|
yuuji@0
|
2527 }
|
yuuji@0
|
2528
|
yuuji@0
|
2529 /* Physical disk write
|
yuuji@0
|
2530 * Accepts: buffered file pointer
|
yuuji@0
|
2531 * buffer address
|
yuuji@0
|
2532 * buffer size
|
yuuji@0
|
2533 * Does not return until success
|
yuuji@0
|
2534 */
|
yuuji@0
|
2535
|
yuuji@0
|
2536 void mmdf_phys_write (MMDFFILE *f,char *buf,size_t size)
|
yuuji@0
|
2537 {
|
yuuji@0
|
2538 MAILSTREAM *stream = f->stream;
|
yuuji@0
|
2539 /* write data at desired position */
|
yuuji@0
|
2540 while (size && ((lseek (LOCAL->fd,f->filepos,L_SET) < 0) ||
|
yuuji@0
|
2541 (write (LOCAL->fd,buf,size) < 0))) {
|
yuuji@0
|
2542 int e;
|
yuuji@0
|
2543 char tmp[MAILTMPLEN];
|
yuuji@0
|
2544 sprintf (tmp,"Unable to write to mailbox: %s",strerror (e = errno));
|
yuuji@0
|
2545 MM_LOG (tmp,ERROR);
|
yuuji@0
|
2546 MM_DISKERROR (NIL,e,T); /* serious problem, must retry */
|
yuuji@0
|
2547 }
|
yuuji@0
|
2548 f->filepos += size; /* update file position */
|
yuuji@0
|
2549 }
|