rev |
line source |
yuuji@0
|
1 /* ========================================================================
|
yuuji@0
|
2 * Copyright 1988-2007 University of Washington
|
yuuji@0
|
3 *
|
yuuji@0
|
4 * Licensed under the Apache License, Version 2.0 (the "License");
|
yuuji@0
|
5 * you may not use this file except in compliance with the License.
|
yuuji@0
|
6 * You may obtain a copy of the License at
|
yuuji@0
|
7 *
|
yuuji@0
|
8 * http://www.apache.org/licenses/LICENSE-2.0
|
yuuji@0
|
9 *
|
yuuji@0
|
10 *
|
yuuji@0
|
11 * ========================================================================
|
yuuji@0
|
12 */
|
yuuji@0
|
13
|
yuuji@0
|
14 /*
|
yuuji@0
|
15 * Program: Tenex mail routines
|
yuuji@0
|
16 *
|
yuuji@0
|
17 * Author: Mark Crispin
|
yuuji@0
|
18 * Networks and Distributed Computing
|
yuuji@0
|
19 * Computing & Communications
|
yuuji@0
|
20 * University of Washington
|
yuuji@0
|
21 * Administration Building, AG-44
|
yuuji@0
|
22 * Seattle, WA 98195
|
yuuji@0
|
23 * Internet: MRC@CAC.Washington.EDU
|
yuuji@0
|
24 *
|
yuuji@0
|
25 * Date: 22 May 1990
|
yuuji@0
|
26 * Last Edited: 11 October 2007
|
yuuji@0
|
27 */
|
yuuji@0
|
28
|
yuuji@0
|
29
|
yuuji@0
|
30 /* FILE TIME SEMANTICS
|
yuuji@0
|
31 *
|
yuuji@0
|
32 * The atime is the last read time of the file.
|
yuuji@0
|
33 * The mtime is the last flags update time of the file.
|
yuuji@0
|
34 * The ctime is the last write time of the file.
|
yuuji@0
|
35 *
|
yuuji@0
|
36 * TEXT SIZE SEMANTICS
|
yuuji@0
|
37 *
|
yuuji@0
|
38 * Most of the text sizes are in internal (LF-only) form, except for the
|
yuuji@0
|
39 * msg.text size. Beware.
|
yuuji@0
|
40 */
|
yuuji@0
|
41
|
yuuji@0
|
42 #include <stdio.h>
|
yuuji@0
|
43 #include <ctype.h>
|
yuuji@0
|
44 #include <errno.h>
|
yuuji@0
|
45 extern int errno; /* just in case */
|
yuuji@0
|
46 #include "mail.h"
|
yuuji@0
|
47 #include "osdep.h"
|
yuuji@0
|
48 #include <sys/stat.h>
|
yuuji@0
|
49 #include "misc.h"
|
yuuji@0
|
50 #include "dummy.h"
|
yuuji@0
|
51
|
yuuji@0
|
52 /* TENEX I/O stream local data */
|
yuuji@0
|
53
|
yuuji@0
|
54 typedef struct tenex_local {
|
yuuji@0
|
55 unsigned int shouldcheck: 1; /* if ping should do a check instead */
|
yuuji@0
|
56 unsigned int mustcheck: 1; /* if ping must do a check instead */
|
yuuji@0
|
57 int fd; /* file descriptor for I/O */
|
yuuji@0
|
58 off_t filesize; /* file size parsed */
|
yuuji@0
|
59 time_t filetime; /* last file time */
|
yuuji@0
|
60 time_t lastsnarf; /* local snarf time */
|
yuuji@0
|
61 unsigned char *buf; /* temporary buffer */
|
yuuji@0
|
62 unsigned long buflen; /* current size of temporary buffer */
|
yuuji@0
|
63 unsigned long uid; /* current text uid */
|
yuuji@0
|
64 SIZEDTEXT text; /* current text */
|
yuuji@0
|
65 } TENEXLOCAL;
|
yuuji@0
|
66
|
yuuji@0
|
67
|
yuuji@0
|
68 /* Convenient access to local data */
|
yuuji@0
|
69
|
yuuji@0
|
70 #define LOCAL ((TENEXLOCAL *) stream->local)
|
yuuji@0
|
71
|
yuuji@0
|
72
|
yuuji@0
|
73 /* Function prototypes */
|
yuuji@0
|
74
|
yuuji@0
|
75 DRIVER *tenex_valid (char *name);
|
yuuji@0
|
76 int tenex_isvalid (char *name,char *tmp);
|
yuuji@0
|
77 void *tenex_parameters (long function,void *value);
|
yuuji@0
|
78 void tenex_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents);
|
yuuji@0
|
79 void tenex_list (MAILSTREAM *stream,char *ref,char *pat);
|
yuuji@0
|
80 void tenex_lsub (MAILSTREAM *stream,char *ref,char *pat);
|
yuuji@0
|
81 long tenex_create (MAILSTREAM *stream,char *mailbox);
|
yuuji@0
|
82 long tenex_delete (MAILSTREAM *stream,char *mailbox);
|
yuuji@0
|
83 long tenex_rename (MAILSTREAM *stream,char *old,char *newname);
|
yuuji@0
|
84 long tenex_status (MAILSTREAM *stream,char *mbx,long flags);
|
yuuji@0
|
85 MAILSTREAM *tenex_open (MAILSTREAM *stream);
|
yuuji@0
|
86 void tenex_close (MAILSTREAM *stream,long options);
|
yuuji@0
|
87 void tenex_fast (MAILSTREAM *stream,char *sequence,long flags);
|
yuuji@0
|
88 void tenex_flags (MAILSTREAM *stream,char *sequence,long flags);
|
yuuji@0
|
89 char *tenex_header (MAILSTREAM *stream,unsigned long msgno,
|
yuuji@0
|
90 unsigned long *length,long flags);
|
yuuji@0
|
91 long tenex_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags);
|
yuuji@0
|
92 void tenex_flag (MAILSTREAM *stream,char *sequence,char *flag,long flags);
|
yuuji@0
|
93 void tenex_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt);
|
yuuji@0
|
94 long tenex_ping (MAILSTREAM *stream);
|
yuuji@0
|
95 void tenex_check (MAILSTREAM *stream);
|
yuuji@0
|
96 void tenex_snarf (MAILSTREAM *stream);
|
yuuji@0
|
97 long tenex_expunge (MAILSTREAM *stream,char *sequence,long options);
|
yuuji@0
|
98 long tenex_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options);
|
yuuji@0
|
99 long tenex_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data);
|
yuuji@0
|
100
|
yuuji@0
|
101 unsigned long tenex_size (MAILSTREAM *stream,unsigned long m);
|
yuuji@0
|
102 char *tenex_file (char *dst,char *name);
|
yuuji@0
|
103 long tenex_parse (MAILSTREAM *stream);
|
yuuji@0
|
104 MESSAGECACHE *tenex_elt (MAILSTREAM *stream,unsigned long msgno);
|
yuuji@0
|
105 void tenex_read_flags (MAILSTREAM *stream,MESSAGECACHE *elt);
|
yuuji@0
|
106 void tenex_update_status (MAILSTREAM *stream,unsigned long msgno,
|
yuuji@0
|
107 long syncflag);
|
yuuji@0
|
108 unsigned long tenex_hdrpos (MAILSTREAM *stream,unsigned long msgno,
|
yuuji@0
|
109 unsigned long *size);
|
yuuji@0
|
110
|
yuuji@0
|
111 /* Tenex mail routines */
|
yuuji@0
|
112
|
yuuji@0
|
113
|
yuuji@0
|
114 /* Driver dispatch used by MAIL */
|
yuuji@0
|
115
|
yuuji@0
|
116 DRIVER tenexdriver = {
|
yuuji@0
|
117 "tenex", /* driver name */
|
yuuji@0
|
118 DR_LOCAL|DR_MAIL|DR_NOSTICKY|DR_LOCKING,
|
yuuji@0
|
119 /* driver flags */
|
yuuji@0
|
120 (DRIVER *) NIL, /* next driver */
|
yuuji@0
|
121 tenex_valid, /* mailbox is valid for us */
|
yuuji@0
|
122 tenex_parameters, /* manipulate parameters */
|
yuuji@0
|
123 tenex_scan, /* scan mailboxes */
|
yuuji@0
|
124 tenex_list, /* list mailboxes */
|
yuuji@0
|
125 tenex_lsub, /* list subscribed mailboxes */
|
yuuji@0
|
126 NIL, /* subscribe to mailbox */
|
yuuji@0
|
127 NIL, /* unsubscribe from mailbox */
|
yuuji@0
|
128 dummy_create, /* create mailbox */
|
yuuji@0
|
129 tenex_delete, /* delete mailbox */
|
yuuji@0
|
130 tenex_rename, /* rename mailbox */
|
yuuji@0
|
131 tenex_status, /* status of mailbox */
|
yuuji@0
|
132 tenex_open, /* open mailbox */
|
yuuji@0
|
133 tenex_close, /* close mailbox */
|
yuuji@0
|
134 tenex_fast, /* fetch message "fast" attributes */
|
yuuji@0
|
135 tenex_flags, /* fetch message flags */
|
yuuji@0
|
136 NIL, /* fetch overview */
|
yuuji@0
|
137 NIL, /* fetch message envelopes */
|
yuuji@0
|
138 tenex_header, /* fetch message header */
|
yuuji@0
|
139 tenex_text, /* fetch message body */
|
yuuji@0
|
140 NIL, /* fetch partial message text */
|
yuuji@0
|
141 NIL, /* unique identifier */
|
yuuji@0
|
142 NIL, /* message number */
|
yuuji@0
|
143 tenex_flag, /* modify flags */
|
yuuji@0
|
144 tenex_flagmsg, /* per-message modify flags */
|
yuuji@0
|
145 NIL, /* search for message based on criteria */
|
yuuji@0
|
146 NIL, /* sort messages */
|
yuuji@0
|
147 NIL, /* thread messages */
|
yuuji@0
|
148 tenex_ping, /* ping mailbox to see if still alive */
|
yuuji@0
|
149 tenex_check, /* check for new messages */
|
yuuji@0
|
150 tenex_expunge, /* expunge deleted messages */
|
yuuji@0
|
151 tenex_copy, /* copy messages to another mailbox */
|
yuuji@0
|
152 tenex_append, /* append string message to mailbox */
|
yuuji@0
|
153 NIL /* garbage collect stream */
|
yuuji@0
|
154 };
|
yuuji@0
|
155
|
yuuji@0
|
156 /* prototype stream */
|
yuuji@0
|
157 MAILSTREAM tenexproto = {&tenexdriver};
|
yuuji@0
|
158
|
yuuji@0
|
159 /* Tenex mail validate mailbox
|
yuuji@0
|
160 * Accepts: mailbox name
|
yuuji@0
|
161 * Returns: our driver if name is valid, NIL otherwise
|
yuuji@0
|
162 */
|
yuuji@0
|
163
|
yuuji@0
|
164 DRIVER *tenex_valid (char *name)
|
yuuji@0
|
165 {
|
yuuji@0
|
166 char tmp[MAILTMPLEN];
|
yuuji@0
|
167 return tenex_isvalid (name,tmp) ? &tenexdriver : NIL;
|
yuuji@0
|
168 }
|
yuuji@0
|
169
|
yuuji@0
|
170
|
yuuji@0
|
171 /* Tenex mail test for valid mailbox
|
yuuji@0
|
172 * Accepts: mailbox name
|
yuuji@0
|
173 * Returns: T if valid, NIL otherwise
|
yuuji@0
|
174 */
|
yuuji@0
|
175
|
yuuji@0
|
176 int tenex_isvalid (char *name,char *tmp)
|
yuuji@0
|
177 {
|
yuuji@0
|
178 int fd;
|
yuuji@0
|
179 int ret = NIL;
|
yuuji@0
|
180 char *s,file[MAILTMPLEN];
|
yuuji@0
|
181 struct stat sbuf;
|
yuuji@0
|
182 time_t tp[2];
|
yuuji@0
|
183 errno = EINVAL; /* assume invalid argument */
|
yuuji@0
|
184 /* if file, get its status */
|
yuuji@0
|
185 if ((s = tenex_file (file,name)) && !stat (s,&sbuf)) {
|
yuuji@0
|
186 if (!sbuf.st_size) { /* allow empty file if INBOX */
|
yuuji@0
|
187 if ((s = mailboxfile (tmp,name)) && !*s) ret = T;
|
yuuji@0
|
188 else errno = 0; /* empty file */
|
yuuji@0
|
189 }
|
yuuji@0
|
190 else if ((fd = open (file,O_RDONLY,NIL)) >= 0) {
|
yuuji@0
|
191 memset (tmp,'\0',MAILTMPLEN);
|
yuuji@0
|
192 if ((read (fd,tmp,64) >= 0) && (s = strchr (tmp,'\012')) &&
|
yuuji@0
|
193 (s[-1] != '\015')) { /* valid format? */
|
yuuji@0
|
194 *s = '\0'; /* tie off header */
|
yuuji@0
|
195 /* must begin with dd-mmm-yy" */
|
yuuji@0
|
196 ret = (((tmp[2] == '-' && tmp[6] == '-') ||
|
yuuji@0
|
197 (tmp[1] == '-' && tmp[5] == '-')) &&
|
yuuji@0
|
198 (s = strchr (tmp+18,',')) && strchr (s+2,';')) ? T : NIL;
|
yuuji@0
|
199 }
|
yuuji@0
|
200 else errno = -1; /* bogus format */
|
yuuji@0
|
201 close (fd); /* close the file */
|
yuuji@0
|
202 /* \Marked status? */
|
yuuji@0
|
203 if (sbuf.st_ctime > sbuf.st_atime) {
|
yuuji@0
|
204 tp[0] = sbuf.st_atime; /* preserve atime and mtime */
|
yuuji@0
|
205 tp[1] = sbuf.st_mtime;
|
yuuji@0
|
206 utime (file,tp); /* set the times */
|
yuuji@0
|
207 }
|
yuuji@0
|
208 }
|
yuuji@0
|
209 }
|
yuuji@0
|
210 /* in case INBOX but not tenex format */
|
yuuji@0
|
211 else if ((errno == ENOENT) && !compare_cstring (name,"INBOX")) errno = -1;
|
yuuji@0
|
212 return ret; /* return what we should */
|
yuuji@0
|
213 }
|
yuuji@0
|
214
|
yuuji@0
|
215 /* Tenex manipulate driver parameters
|
yuuji@0
|
216 * Accepts: function code
|
yuuji@0
|
217 * function-dependent value
|
yuuji@0
|
218 * Returns: function-dependent return value
|
yuuji@0
|
219 */
|
yuuji@0
|
220
|
yuuji@0
|
221 void *tenex_parameters (long function,void *value)
|
yuuji@0
|
222 {
|
yuuji@0
|
223 void *ret = NIL;
|
yuuji@0
|
224 switch ((int) function) {
|
yuuji@0
|
225 case GET_INBOXPATH:
|
yuuji@0
|
226 if (value) ret = tenex_file ((char *) value,"INBOX");
|
yuuji@0
|
227 break;
|
yuuji@0
|
228 }
|
yuuji@0
|
229 return ret;
|
yuuji@0
|
230 }
|
yuuji@0
|
231
|
yuuji@0
|
232
|
yuuji@0
|
233 /* Tenex mail scan mailboxes
|
yuuji@0
|
234 * Accepts: mail stream
|
yuuji@0
|
235 * reference
|
yuuji@0
|
236 * pattern to search
|
yuuji@0
|
237 * string to scan
|
yuuji@0
|
238 */
|
yuuji@0
|
239
|
yuuji@0
|
240 void tenex_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents)
|
yuuji@0
|
241 {
|
yuuji@0
|
242 if (stream) dummy_scan (NIL,ref,pat,contents);
|
yuuji@0
|
243 }
|
yuuji@0
|
244
|
yuuji@0
|
245
|
yuuji@0
|
246 /* Tenex mail list mailboxes
|
yuuji@0
|
247 * Accepts: mail stream
|
yuuji@0
|
248 * reference
|
yuuji@0
|
249 * pattern to search
|
yuuji@0
|
250 */
|
yuuji@0
|
251
|
yuuji@0
|
252 void tenex_list (MAILSTREAM *stream,char *ref,char *pat)
|
yuuji@0
|
253 {
|
yuuji@0
|
254 if (stream) dummy_list (NIL,ref,pat);
|
yuuji@0
|
255 }
|
yuuji@0
|
256
|
yuuji@0
|
257
|
yuuji@0
|
258 /* Tenex mail list subscribed mailboxes
|
yuuji@0
|
259 * Accepts: mail stream
|
yuuji@0
|
260 * reference
|
yuuji@0
|
261 * pattern to search
|
yuuji@0
|
262 */
|
yuuji@0
|
263
|
yuuji@0
|
264 void tenex_lsub (MAILSTREAM *stream,char *ref,char *pat)
|
yuuji@0
|
265 {
|
yuuji@0
|
266 if (stream) dummy_lsub (NIL,ref,pat);
|
yuuji@0
|
267 }
|
yuuji@0
|
268
|
yuuji@0
|
269 /* Tenex mail delete mailbox
|
yuuji@0
|
270 * Accepts: MAIL stream
|
yuuji@0
|
271 * mailbox name to delete
|
yuuji@0
|
272 * Returns: T on success, NIL on failure
|
yuuji@0
|
273 */
|
yuuji@0
|
274
|
yuuji@0
|
275 long tenex_delete (MAILSTREAM *stream,char *mailbox)
|
yuuji@0
|
276 {
|
yuuji@0
|
277 return tenex_rename (stream,mailbox,NIL);
|
yuuji@0
|
278 }
|
yuuji@0
|
279
|
yuuji@0
|
280
|
yuuji@0
|
281 /* Tenex mail rename mailbox
|
yuuji@0
|
282 * Accepts: MAIL stream
|
yuuji@0
|
283 * old mailbox name
|
yuuji@0
|
284 * new mailbox name (or NIL for delete)
|
yuuji@0
|
285 * Returns: T on success, NIL on failure
|
yuuji@0
|
286 */
|
yuuji@0
|
287
|
yuuji@0
|
288 long tenex_rename (MAILSTREAM *stream,char *old,char *newname)
|
yuuji@0
|
289 {
|
yuuji@0
|
290 long ret = T;
|
yuuji@0
|
291 char c,*s,tmp[MAILTMPLEN],file[MAILTMPLEN],lock[MAILTMPLEN];
|
yuuji@0
|
292 int fd,ld;
|
yuuji@0
|
293 struct stat sbuf;
|
yuuji@0
|
294 if (!tenex_file (file,old) ||
|
yuuji@0
|
295 (newname && (!((s = mailboxfile (tmp,newname)) && *s) ||
|
yuuji@0
|
296 ((s = strrchr (tmp,'/')) && !s[1])))) {
|
yuuji@0
|
297 sprintf (tmp,newname ?
|
yuuji@0
|
298 "Can't rename mailbox %.80s to %.80s: invalid name" :
|
yuuji@0
|
299 "Can't delete mailbox %.80s: invalid name",
|
yuuji@0
|
300 old,newname);
|
yuuji@0
|
301 MM_LOG (tmp,ERROR);
|
yuuji@0
|
302 return NIL;
|
yuuji@0
|
303 }
|
yuuji@0
|
304 else if ((fd = open (file,O_RDWR,NIL)) < 0) {
|
yuuji@0
|
305 sprintf (tmp,"Can't open mailbox %.80s: %s",old,strerror (errno));
|
yuuji@0
|
306 MM_LOG (tmp,ERROR);
|
yuuji@0
|
307 return NIL;
|
yuuji@0
|
308 }
|
yuuji@0
|
309 /* get exclusive parse/append permission */
|
yuuji@0
|
310 if ((ld = lockfd (fd,lock,LOCK_EX)) < 0) {
|
yuuji@0
|
311 MM_LOG ("Unable to lock rename mailbox",ERROR);
|
yuuji@0
|
312 return NIL;
|
yuuji@0
|
313 }
|
yuuji@0
|
314 /* lock out other users */
|
yuuji@0
|
315 if (flock (fd,LOCK_EX|LOCK_NB)) {
|
yuuji@0
|
316 close (fd); /* couldn't lock, give up on it then */
|
yuuji@0
|
317 sprintf (tmp,"Mailbox %.80s is in use by another process",old);
|
yuuji@0
|
318 MM_LOG (tmp,ERROR);
|
yuuji@0
|
319 unlockfd (ld,lock); /* release exclusive parse/append permission */
|
yuuji@0
|
320 return NIL;
|
yuuji@0
|
321 }
|
yuuji@0
|
322
|
yuuji@0
|
323 if (newname) { /* want rename? */
|
yuuji@0
|
324 if (s = strrchr (tmp,'/')) {/* found superior to destination name? */
|
yuuji@0
|
325 c = *++s; /* remember first character of inferior */
|
yuuji@0
|
326 *s = '\0'; /* tie off to get just superior */
|
yuuji@0
|
327 /* name doesn't exist, create it */
|
yuuji@0
|
328 if ((stat (tmp,&sbuf) || ((sbuf.st_mode & S_IFMT) != S_IFDIR)) &&
|
yuuji@0
|
329 !dummy_create_path (stream,tmp,get_dir_protection (newname)))
|
yuuji@0
|
330 ret = NIL;
|
yuuji@0
|
331 else *s = c; /* restore full name */
|
yuuji@0
|
332 }
|
yuuji@0
|
333 /* rename the file */
|
yuuji@0
|
334 if (ret && rename (file,tmp)) {
|
yuuji@0
|
335 sprintf (tmp,"Can't rename mailbox %.80s to %.80s: %s",old,newname,
|
yuuji@0
|
336 strerror (errno));
|
yuuji@0
|
337 MM_LOG (tmp,ERROR);
|
yuuji@0
|
338 ret = NIL; /* set failure */
|
yuuji@0
|
339 }
|
yuuji@0
|
340 }
|
yuuji@0
|
341 else if (unlink (file)) {
|
yuuji@0
|
342 sprintf (tmp,"Can't delete mailbox %.80s: %s",old,strerror (errno));
|
yuuji@0
|
343 MM_LOG (tmp,ERROR);
|
yuuji@0
|
344 ret = NIL; /* set failure */
|
yuuji@0
|
345 }
|
yuuji@0
|
346 flock (fd,LOCK_UN); /* release lock on the file */
|
yuuji@0
|
347 close (fd); /* close the file */
|
yuuji@0
|
348 unlockfd (ld,lock); /* release exclusive parse/append permission */
|
yuuji@0
|
349 /* recreate file if renamed INBOX */
|
yuuji@0
|
350 if (ret && !compare_cstring (old,"INBOX")) dummy_create (NIL,"mail.txt");
|
yuuji@0
|
351 return ret; /* return success */
|
yuuji@0
|
352 }
|
yuuji@0
|
353
|
yuuji@0
|
354 /* Tenex Mail status
|
yuuji@0
|
355 * Accepts: mail stream
|
yuuji@0
|
356 * mailbox name
|
yuuji@0
|
357 * status flags
|
yuuji@0
|
358 * Returns: T on success, NIL on failure
|
yuuji@0
|
359 */
|
yuuji@0
|
360
|
yuuji@0
|
361 long tenex_status (MAILSTREAM *stream,char *mbx,long flags)
|
yuuji@0
|
362 {
|
yuuji@0
|
363 MAILSTATUS status;
|
yuuji@0
|
364 unsigned long i;
|
yuuji@0
|
365 MAILSTREAM *tstream = NIL;
|
yuuji@0
|
366 MAILSTREAM *systream = NIL;
|
yuuji@0
|
367 /* make temporary stream (unless this mbx) */
|
yuuji@0
|
368 if (!stream && !(stream = tstream =
|
yuuji@0
|
369 mail_open (NIL,mbx,OP_READONLY|OP_SILENT))) return NIL;
|
yuuji@0
|
370 status.flags = flags; /* return status values */
|
yuuji@0
|
371 status.messages = stream->nmsgs;
|
yuuji@0
|
372 status.recent = stream->recent;
|
yuuji@0
|
373 if (flags & SA_UNSEEN) /* must search to get unseen messages */
|
yuuji@0
|
374 for (i = 1,status.unseen = 0; i <= stream->nmsgs; i++)
|
yuuji@0
|
375 if (!mail_elt (stream,i)->seen) status.unseen++;
|
yuuji@0
|
376 status.uidnext = stream->uid_last + 1;
|
yuuji@0
|
377 status.uidvalidity = stream->uid_validity;
|
yuuji@0
|
378 /* calculate post-snarf results */
|
yuuji@0
|
379 if (!status.recent && stream->inbox &&
|
yuuji@0
|
380 (systream = mail_open (NIL,sysinbox (),OP_READONLY|OP_SILENT))) {
|
yuuji@0
|
381 status.messages += systream->nmsgs;
|
yuuji@0
|
382 status.recent += systream->recent;
|
yuuji@0
|
383 if (flags & SA_UNSEEN) /* must search to get unseen messages */
|
yuuji@0
|
384 for (i = 1; i <= systream->nmsgs; i++)
|
yuuji@0
|
385 if (!mail_elt (systream,i)->seen) status.unseen++;
|
yuuji@0
|
386 /* kludge but probably good enough */
|
yuuji@0
|
387 status.uidnext += systream->nmsgs;
|
yuuji@0
|
388 }
|
yuuji@0
|
389 MM_STATUS(stream,mbx,&status);/* pass status to main program */
|
yuuji@0
|
390 if (tstream) mail_close (tstream);
|
yuuji@0
|
391 if (systream) mail_close (systream);
|
yuuji@0
|
392 return T; /* success */
|
yuuji@0
|
393 }
|
yuuji@0
|
394
|
yuuji@0
|
395 /* Tenex mail open
|
yuuji@0
|
396 * Accepts: stream to open
|
yuuji@0
|
397 * Returns: stream on success, NIL on failure
|
yuuji@0
|
398 */
|
yuuji@0
|
399
|
yuuji@0
|
400 MAILSTREAM *tenex_open (MAILSTREAM *stream)
|
yuuji@0
|
401 {
|
yuuji@0
|
402 int fd,ld;
|
yuuji@0
|
403 char tmp[MAILTMPLEN];
|
yuuji@0
|
404 blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL);
|
yuuji@0
|
405 /* return prototype for OP_PROTOTYPE call */
|
yuuji@0
|
406 if (!stream) return user_flags (&tenexproto);
|
yuuji@0
|
407 if (stream->local) fatal ("tenex recycle stream");
|
yuuji@0
|
408 user_flags (stream); /* set up user flags */
|
yuuji@0
|
409 /* canonicalize the mailbox name */
|
yuuji@0
|
410 if (!tenex_file (tmp,stream->mailbox)) {
|
yuuji@0
|
411 sprintf (tmp,"Can't open - invalid name: %.80s",stream->mailbox);
|
yuuji@0
|
412 MM_LOG (tmp,ERROR);
|
yuuji@0
|
413 }
|
yuuji@0
|
414 if (stream->rdonly ||
|
yuuji@0
|
415 (fd = open (tmp,O_RDWR,NIL)) < 0) {
|
yuuji@0
|
416 if ((fd = open (tmp,O_RDONLY,NIL)) < 0) {
|
yuuji@0
|
417 sprintf (tmp,"Can't open mailbox: %s",strerror (errno));
|
yuuji@0
|
418 MM_LOG (tmp,ERROR);
|
yuuji@0
|
419 return NIL;
|
yuuji@0
|
420 }
|
yuuji@0
|
421 else if (!stream->rdonly) { /* got it, but readonly */
|
yuuji@0
|
422 MM_LOG ("Can't get write access to mailbox, access is readonly",WARN);
|
yuuji@0
|
423 stream->rdonly = T;
|
yuuji@0
|
424 }
|
yuuji@0
|
425 }
|
yuuji@0
|
426 stream->local = fs_get (sizeof (TENEXLOCAL));
|
yuuji@0
|
427 LOCAL->buf = (char *) fs_get (CHUNKSIZE);
|
yuuji@0
|
428 LOCAL->buflen = CHUNKSIZE - 1;
|
yuuji@0
|
429 LOCAL->text.data = (unsigned char *) fs_get (CHUNKSIZE);
|
yuuji@0
|
430 LOCAL->text.size = CHUNKSIZE - 1;
|
yuuji@0
|
431
|
yuuji@0
|
432 /* note if an INBOX or not */
|
yuuji@0
|
433 stream->inbox = !compare_cstring (stream->mailbox,"INBOX");
|
yuuji@0
|
434 LOCAL->fd = fd; /* bind the file */
|
yuuji@0
|
435 /* flush old name */
|
yuuji@0
|
436 fs_give ((void **) &stream->mailbox);
|
yuuji@0
|
437 /* save canonical name */
|
yuuji@0
|
438 stream->mailbox = cpystr (tmp);
|
yuuji@0
|
439 /* get shared parse permission */
|
yuuji@0
|
440 if ((ld = lockfd (fd,tmp,LOCK_SH)) < 0) {
|
yuuji@0
|
441 MM_LOG ("Unable to lock open mailbox",ERROR);
|
yuuji@0
|
442 return NIL;
|
yuuji@0
|
443 }
|
yuuji@0
|
444 (*bn) (BLOCK_FILELOCK,NIL);
|
yuuji@0
|
445 flock (LOCAL->fd,LOCK_SH); /* lock the file */
|
yuuji@0
|
446 (*bn) (BLOCK_NONE,NIL);
|
yuuji@0
|
447 unlockfd (ld,tmp); /* release shared parse permission */
|
yuuji@0
|
448 LOCAL->filesize = 0; /* initialize parsed file size */
|
yuuji@0
|
449 /* time not set up yet */
|
yuuji@0
|
450 LOCAL->lastsnarf = LOCAL->filetime = 0;
|
yuuji@0
|
451 LOCAL->mustcheck = LOCAL->shouldcheck = NIL;
|
yuuji@0
|
452 stream->sequence++; /* bump sequence number */
|
yuuji@0
|
453 /* parse mailbox */
|
yuuji@0
|
454 stream->nmsgs = stream->recent = 0;
|
yuuji@0
|
455 if (tenex_ping (stream) && !stream->nmsgs)
|
yuuji@0
|
456 MM_LOG ("Mailbox is empty",(long) NIL);
|
yuuji@0
|
457 if (!LOCAL) return NIL; /* failure if stream died */
|
yuuji@0
|
458 stream->perm_seen = stream->perm_deleted =
|
yuuji@0
|
459 stream->perm_flagged = stream->perm_answered = stream->perm_draft =
|
yuuji@0
|
460 stream->rdonly ? NIL : T;
|
yuuji@0
|
461 stream->perm_user_flags = stream->rdonly ? NIL : 0xffffffff;
|
yuuji@0
|
462 return stream; /* return stream to caller */
|
yuuji@0
|
463 }
|
yuuji@0
|
464
|
yuuji@0
|
465 /* Tenex mail close
|
yuuji@0
|
466 * Accepts: MAIL stream
|
yuuji@0
|
467 * close options
|
yuuji@0
|
468 */
|
yuuji@0
|
469
|
yuuji@0
|
470 void tenex_close (MAILSTREAM *stream,long options)
|
yuuji@0
|
471 {
|
yuuji@0
|
472 if (stream && LOCAL) { /* only if a file is open */
|
yuuji@0
|
473 int silent = stream->silent;
|
yuuji@0
|
474 stream->silent = T; /* note this stream is dying */
|
yuuji@0
|
475 if (options & CL_EXPUNGE) tenex_expunge (stream,NIL,NIL);
|
yuuji@0
|
476 stream->silent = silent; /* restore previous status */
|
yuuji@0
|
477 flock (LOCAL->fd,LOCK_UN); /* unlock local file */
|
yuuji@0
|
478 close (LOCAL->fd); /* close the local file */
|
yuuji@0
|
479 /* free local text buffer */
|
yuuji@0
|
480 if (LOCAL->buf) fs_give ((void **) &LOCAL->buf);
|
yuuji@0
|
481 if (LOCAL->text.data) fs_give ((void **) &LOCAL->text.data);
|
yuuji@0
|
482 /* nuke the local data */
|
yuuji@0
|
483 fs_give ((void **) &stream->local);
|
yuuji@0
|
484 stream->dtb = NIL; /* log out the DTB */
|
yuuji@0
|
485 }
|
yuuji@0
|
486 }
|
yuuji@0
|
487
|
yuuji@0
|
488 /* Tenex mail fetch fast data
|
yuuji@0
|
489 * Accepts: MAIL stream
|
yuuji@0
|
490 * sequence
|
yuuji@0
|
491 * option flags
|
yuuji@0
|
492 */
|
yuuji@0
|
493
|
yuuji@0
|
494 void tenex_fast (MAILSTREAM *stream,char *sequence,long flags)
|
yuuji@0
|
495 {
|
yuuji@0
|
496 STRING bs;
|
yuuji@0
|
497 MESSAGECACHE *elt;
|
yuuji@0
|
498 unsigned long i;
|
yuuji@0
|
499 if (stream && LOCAL &&
|
yuuji@0
|
500 ((flags & FT_UID) ? mail_uid_sequence (stream,sequence) :
|
yuuji@0
|
501 mail_sequence (stream,sequence)))
|
yuuji@0
|
502 for (i = 1; i <= stream->nmsgs; i++)
|
yuuji@0
|
503 if ((elt = mail_elt (stream,i))->sequence) {
|
yuuji@0
|
504 if (!elt->rfc822_size) { /* have header size yet? */
|
yuuji@0
|
505 lseek (LOCAL->fd,elt->private.special.offset +
|
yuuji@0
|
506 elt->private.special.text.size,L_SET);
|
yuuji@0
|
507 /* resize bigbuf if necessary */
|
yuuji@0
|
508 if (LOCAL->buflen < elt->private.msg.full.text.size) {
|
yuuji@0
|
509 fs_give ((void **) &LOCAL->buf);
|
yuuji@0
|
510 LOCAL->buflen = elt->private.msg.full.text.size;
|
yuuji@0
|
511 LOCAL->buf = (char *) fs_get (LOCAL->buflen + 1);
|
yuuji@0
|
512 }
|
yuuji@0
|
513 /* tie off string */
|
yuuji@0
|
514 LOCAL->buf[elt->private.msg.full.text.size] = '\0';
|
yuuji@0
|
515 /* read in the message */
|
yuuji@0
|
516 read (LOCAL->fd,LOCAL->buf,elt->private.msg.full.text.size);
|
yuuji@0
|
517 INIT (&bs,mail_string,(void *) LOCAL->buf,
|
yuuji@0
|
518 elt->private.msg.full.text.size);
|
yuuji@0
|
519 /* calculate its CRLF size */
|
yuuji@0
|
520 elt->rfc822_size = strcrlflen (&bs);
|
yuuji@0
|
521 }
|
yuuji@0
|
522 tenex_elt (stream,i); /* get current flags from file */
|
yuuji@0
|
523 }
|
yuuji@0
|
524 }
|
yuuji@0
|
525
|
yuuji@0
|
526
|
yuuji@0
|
527 /* Tenex mail fetch flags
|
yuuji@0
|
528 * Accepts: MAIL stream
|
yuuji@0
|
529 * sequence
|
yuuji@0
|
530 * option flags
|
yuuji@0
|
531 * Sniffs at file to get flags
|
yuuji@0
|
532 */
|
yuuji@0
|
533
|
yuuji@0
|
534 void tenex_flags (MAILSTREAM *stream,char *sequence,long flags)
|
yuuji@0
|
535 {
|
yuuji@0
|
536 unsigned long i;
|
yuuji@0
|
537 if (stream && LOCAL &&
|
yuuji@0
|
538 ((flags & FT_UID) ? mail_uid_sequence (stream,sequence) :
|
yuuji@0
|
539 mail_sequence (stream,sequence)))
|
yuuji@0
|
540 for (i = 1; i <= stream->nmsgs; i++)
|
yuuji@0
|
541 if (mail_elt (stream,i)->sequence) tenex_elt (stream,i);
|
yuuji@0
|
542 }
|
yuuji@0
|
543
|
yuuji@0
|
544 /* TENEX mail fetch message header
|
yuuji@0
|
545 * Accepts: MAIL stream
|
yuuji@0
|
546 * message # to fetch
|
yuuji@0
|
547 * pointer to returned header text length
|
yuuji@0
|
548 * option flags
|
yuuji@0
|
549 * Returns: message header in RFC822 format
|
yuuji@0
|
550 */
|
yuuji@0
|
551
|
yuuji@0
|
552 char *tenex_header (MAILSTREAM *stream,unsigned long msgno,
|
yuuji@0
|
553 unsigned long *length,long flags)
|
yuuji@0
|
554 {
|
yuuji@0
|
555 char *s;
|
yuuji@0
|
556 unsigned long i;
|
yuuji@0
|
557 *length = 0; /* default to empty */
|
yuuji@0
|
558 if (flags & FT_UID) return "";/* UID call "impossible" */
|
yuuji@0
|
559 /* get to header position */
|
yuuji@0
|
560 lseek (LOCAL->fd,tenex_hdrpos (stream,msgno,&i),L_SET);
|
yuuji@0
|
561 if (flags & FT_INTERNAL) {
|
yuuji@0
|
562 if (i > LOCAL->buflen) { /* resize if not enough space */
|
yuuji@0
|
563 fs_give ((void **) &LOCAL->buf);
|
yuuji@0
|
564 LOCAL->buf = (char *) fs_get (LOCAL->buflen = i + 1);
|
yuuji@0
|
565 }
|
yuuji@0
|
566 /* slurp the data */
|
yuuji@0
|
567 read (LOCAL->fd,LOCAL->buf,*length = i);
|
yuuji@0
|
568 }
|
yuuji@0
|
569 else {
|
yuuji@0
|
570 s = (char *) fs_get (i + 1);/* get readin buffer */
|
yuuji@0
|
571 s[i] = '\0'; /* tie off string */
|
yuuji@0
|
572 read (LOCAL->fd,s,i); /* slurp the data */
|
yuuji@0
|
573 /* make CRLF copy of string */
|
yuuji@0
|
574 *length = strcrlfcpy (&LOCAL->buf,&LOCAL->buflen,s,i);
|
yuuji@0
|
575 fs_give ((void **) &s); /* free readin buffer */
|
yuuji@0
|
576 }
|
yuuji@0
|
577 return (char *) LOCAL->buf;
|
yuuji@0
|
578 }
|
yuuji@0
|
579
|
yuuji@0
|
580 /* TENEX mail fetch message text (body only)
|
yuuji@0
|
581 * Accepts: MAIL stream
|
yuuji@0
|
582 * message # to fetch
|
yuuji@0
|
583 * pointer to returned stringstruct
|
yuuji@0
|
584 * option flags
|
yuuji@0
|
585 * Returns: T, always
|
yuuji@0
|
586 */
|
yuuji@0
|
587
|
yuuji@0
|
588 long tenex_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags)
|
yuuji@0
|
589 {
|
yuuji@0
|
590 char *s;
|
yuuji@0
|
591 unsigned long i,j;
|
yuuji@0
|
592 MESSAGECACHE *elt;
|
yuuji@0
|
593 /* UID call "impossible" */
|
yuuji@0
|
594 if (flags & FT_UID) return NIL;
|
yuuji@0
|
595 /* get message status */
|
yuuji@0
|
596 elt = tenex_elt (stream,msgno);
|
yuuji@0
|
597 /* if message not seen */
|
yuuji@0
|
598 if (!(flags & FT_PEEK) && !elt->seen) {
|
yuuji@0
|
599 elt->seen = T; /* mark message as seen */
|
yuuji@0
|
600 /* recalculate status */
|
yuuji@0
|
601 tenex_update_status (stream,msgno,T);
|
yuuji@0
|
602 MM_FLAGS (stream,msgno);
|
yuuji@0
|
603 }
|
yuuji@0
|
604 if (flags & FT_INTERNAL) { /* if internal representation wanted */
|
yuuji@0
|
605 /* find header position */
|
yuuji@0
|
606 i = tenex_hdrpos (stream,msgno,&j);
|
yuuji@0
|
607 if (i > LOCAL->buflen) { /* resize if not enough space */
|
yuuji@0
|
608 fs_give ((void **) &LOCAL->buf);
|
yuuji@0
|
609 LOCAL->buf = (char *) fs_get (LOCAL->buflen = i + 1);
|
yuuji@0
|
610 }
|
yuuji@0
|
611 /* go to text position */
|
yuuji@0
|
612 lseek (LOCAL->fd,i + j,L_SET);
|
yuuji@0
|
613 /* slurp the data */
|
yuuji@0
|
614 read (LOCAL->fd,LOCAL->buf,i);
|
yuuji@0
|
615 /* set up stringstruct for internal */
|
yuuji@0
|
616 INIT (bs,mail_string,LOCAL->buf,i);
|
yuuji@0
|
617 }
|
yuuji@0
|
618 else { /* normal form, previous text cached? */
|
yuuji@0
|
619 if (elt->private.uid == LOCAL->uid)
|
yuuji@0
|
620 i = elt->private.msg.text.text.size;
|
yuuji@0
|
621 else { /* not cached, cache it now */
|
yuuji@0
|
622 LOCAL->uid = elt->private.uid;
|
yuuji@0
|
623 /* find header position */
|
yuuji@0
|
624 i = tenex_hdrpos (stream,msgno,&j);
|
yuuji@0
|
625 /* go to text position */
|
yuuji@0
|
626 lseek (LOCAL->fd,i + j,L_SET);
|
yuuji@0
|
627 s = (char *) fs_get ((i = tenex_size (stream,msgno) - j) + 1);
|
yuuji@0
|
628 s[i] = '\0'; /* tie off string */
|
yuuji@0
|
629 read (LOCAL->fd,s,i); /* slurp the data */
|
yuuji@0
|
630 /* make CRLF copy of string */
|
yuuji@0
|
631 i = elt->private.msg.text.text.size =
|
yuuji@0
|
632 strcrlfcpy (&LOCAL->text.data,&LOCAL->text.size,s,i);
|
yuuji@0
|
633 fs_give ((void **) &s); /* free readin buffer */
|
yuuji@0
|
634 }
|
yuuji@0
|
635 /* set up stringstruct */
|
yuuji@0
|
636 INIT (bs,mail_string,LOCAL->text.data,i);
|
yuuji@0
|
637 }
|
yuuji@0
|
638 return T; /* success */
|
yuuji@0
|
639 }
|
yuuji@0
|
640
|
yuuji@0
|
641 /* Tenex mail modify flags
|
yuuji@0
|
642 * Accepts: MAIL stream
|
yuuji@0
|
643 * sequence
|
yuuji@0
|
644 * flag(s)
|
yuuji@0
|
645 * option flags
|
yuuji@0
|
646 */
|
yuuji@0
|
647
|
yuuji@0
|
648 void tenex_flag (MAILSTREAM *stream,char *sequence,char *flag,long flags)
|
yuuji@0
|
649 {
|
yuuji@0
|
650 time_t tp[2];
|
yuuji@0
|
651 struct stat sbuf;
|
yuuji@0
|
652 if (!stream->rdonly) { /* make sure the update takes */
|
yuuji@0
|
653 fsync (LOCAL->fd);
|
yuuji@0
|
654 fstat (LOCAL->fd,&sbuf); /* get current write time */
|
yuuji@0
|
655 tp[1] = LOCAL->filetime = sbuf.st_mtime;
|
yuuji@0
|
656 tp[0] = time (0); /* make sure read comes after all that */
|
yuuji@0
|
657 utime (stream->mailbox,tp);
|
yuuji@0
|
658 }
|
yuuji@0
|
659 }
|
yuuji@0
|
660
|
yuuji@0
|
661
|
yuuji@0
|
662 /* Tenex mail per-message modify flags
|
yuuji@0
|
663 * Accepts: MAIL stream
|
yuuji@0
|
664 * message cache element
|
yuuji@0
|
665 */
|
yuuji@0
|
666
|
yuuji@0
|
667 void tenex_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt)
|
yuuji@0
|
668 {
|
yuuji@0
|
669 struct stat sbuf;
|
yuuji@0
|
670 /* maybe need to do a checkpoint? */
|
yuuji@0
|
671 if (LOCAL->filetime && !LOCAL->shouldcheck) {
|
yuuji@0
|
672 fstat (LOCAL->fd,&sbuf); /* get current write time */
|
yuuji@0
|
673 if (LOCAL->filetime < sbuf.st_mtime) LOCAL->shouldcheck = T;
|
yuuji@0
|
674 LOCAL->filetime = 0; /* don't do this test for any other messages */
|
yuuji@0
|
675 }
|
yuuji@0
|
676 /* recalculate status */
|
yuuji@0
|
677 tenex_update_status (stream,elt->msgno,NIL);
|
yuuji@0
|
678 }
|
yuuji@0
|
679
|
yuuji@0
|
680 /* Tenex mail ping mailbox
|
yuuji@0
|
681 * Accepts: MAIL stream
|
yuuji@0
|
682 * Returns: T if stream still alive, NIL if not
|
yuuji@0
|
683 */
|
yuuji@0
|
684
|
yuuji@0
|
685 long tenex_ping (MAILSTREAM *stream)
|
yuuji@0
|
686 {
|
yuuji@0
|
687 unsigned long i = 1;
|
yuuji@0
|
688 long r = T;
|
yuuji@0
|
689 int ld;
|
yuuji@0
|
690 char lock[MAILTMPLEN];
|
yuuji@0
|
691 struct stat sbuf;
|
yuuji@0
|
692 if (stream && LOCAL) { /* only if stream already open */
|
yuuji@0
|
693 fstat (LOCAL->fd,&sbuf); /* get current file poop */
|
yuuji@0
|
694 if (LOCAL->filetime && !(LOCAL->mustcheck || LOCAL->shouldcheck) &&
|
yuuji@0
|
695 (LOCAL->filetime < sbuf.st_mtime)) LOCAL->shouldcheck = T;
|
yuuji@0
|
696 /* check for changed message status */
|
yuuji@0
|
697 if (LOCAL->mustcheck || LOCAL->shouldcheck) {
|
yuuji@0
|
698 LOCAL->filetime = sbuf.st_mtime;
|
yuuji@0
|
699 if (LOCAL->shouldcheck) /* babble when we do this unilaterally */
|
yuuji@0
|
700 MM_NOTIFY (stream,"[CHECK] Checking for flag updates",NIL);
|
yuuji@0
|
701 while (i <= stream->nmsgs) tenex_elt (stream,i++);
|
yuuji@0
|
702 LOCAL->mustcheck = LOCAL->shouldcheck = NIL;
|
yuuji@0
|
703 }
|
yuuji@0
|
704 /* get shared parse/append permission */
|
yuuji@0
|
705 if ((sbuf.st_size != LOCAL->filesize) &&
|
yuuji@0
|
706 ((ld = lockfd (LOCAL->fd,lock,LOCK_SH)) >= 0)) {
|
yuuji@0
|
707 /* parse resulting mailbox */
|
yuuji@0
|
708 r = (tenex_parse (stream)) ? T : NIL;
|
yuuji@0
|
709 unlockfd (ld,lock); /* release shared parse/append permission */
|
yuuji@0
|
710 }
|
yuuji@0
|
711 if (LOCAL) { /* stream must still be alive */
|
yuuji@0
|
712 /* snarf if this is a read-write inbox */
|
yuuji@0
|
713 if (stream->inbox && !stream->rdonly) {
|
yuuji@0
|
714 tenex_snarf (stream);
|
yuuji@0
|
715 fstat (LOCAL->fd,&sbuf);/* see if file changed now */
|
yuuji@0
|
716 if ((sbuf.st_size != LOCAL->filesize) &&
|
yuuji@0
|
717 ((ld = lockfd (LOCAL->fd,lock,LOCK_SH)) >= 0)) {
|
yuuji@0
|
718 /* parse resulting mailbox */
|
yuuji@0
|
719 r = (tenex_parse (stream)) ? T : NIL;
|
yuuji@0
|
720 unlockfd (ld,lock); /* release shared parse/append permission */
|
yuuji@0
|
721 }
|
yuuji@0
|
722 }
|
yuuji@0
|
723 }
|
yuuji@0
|
724 }
|
yuuji@0
|
725 return r; /* return result of the parse */
|
yuuji@0
|
726 }
|
yuuji@0
|
727
|
yuuji@0
|
728
|
yuuji@0
|
729 /* Tenex mail check mailbox (reparses status too)
|
yuuji@0
|
730 * Accepts: MAIL stream
|
yuuji@0
|
731 */
|
yuuji@0
|
732
|
yuuji@0
|
733 void tenex_check (MAILSTREAM *stream)
|
yuuji@0
|
734 {
|
yuuji@0
|
735 /* mark that a check is desired */
|
yuuji@0
|
736 if (LOCAL) LOCAL->mustcheck = T;
|
yuuji@0
|
737 if (tenex_ping (stream)) MM_LOG ("Check completed",(long) NIL);
|
yuuji@0
|
738 }
|
yuuji@0
|
739
|
yuuji@0
|
740 /* Tenex mail snarf messages from system inbox
|
yuuji@0
|
741 * Accepts: MAIL stream
|
yuuji@0
|
742 */
|
yuuji@0
|
743
|
yuuji@0
|
744 void tenex_snarf (MAILSTREAM *stream)
|
yuuji@0
|
745 {
|
yuuji@0
|
746 unsigned long i = 0;
|
yuuji@0
|
747 unsigned long j,r,hdrlen,txtlen;
|
yuuji@0
|
748 struct stat sbuf;
|
yuuji@0
|
749 char *hdr,*txt,lock[MAILTMPLEN],tmp[MAILTMPLEN];
|
yuuji@0
|
750 MESSAGECACHE *elt;
|
yuuji@0
|
751 MAILSTREAM *sysibx = NIL;
|
yuuji@0
|
752 int ld;
|
yuuji@0
|
753 /* give up if can't get exclusive permission */
|
yuuji@0
|
754 if ((time (0) >= (LOCAL->lastsnarf +
|
yuuji@0
|
755 (long) mail_parameters (NIL,GET_SNARFINTERVAL,NIL))) &&
|
yuuji@0
|
756 strcmp (sysinbox (),stream->mailbox) &&
|
yuuji@0
|
757 ((ld = lockfd (LOCAL->fd,lock,LOCK_EX)) >= 0)) {
|
yuuji@0
|
758 MM_CRITICAL (stream); /* go critical */
|
yuuji@0
|
759 /* sizes match and anything in sysinbox? */
|
yuuji@0
|
760 if (!stat (sysinbox (),&sbuf) && sbuf.st_size &&
|
yuuji@0
|
761 !fstat (LOCAL->fd,&sbuf) && (sbuf.st_size == LOCAL->filesize) &&
|
yuuji@0
|
762 (sysibx = mail_open (sysibx,sysinbox (),OP_SILENT)) &&
|
yuuji@0
|
763 (!sysibx->rdonly) && (r = sysibx->nmsgs)) {
|
yuuji@0
|
764 /* yes, go to end of file in our mailbox */
|
yuuji@0
|
765 lseek (LOCAL->fd,sbuf.st_size,L_SET);
|
yuuji@0
|
766 /* for each message in sysibx mailbox */
|
yuuji@0
|
767 while (r && (++i <= sysibx->nmsgs)) {
|
yuuji@0
|
768 /* snarf message from system INBOX */
|
yuuji@0
|
769 hdr = cpystr (mail_fetchheader_full(sysibx,i,NIL,&hdrlen,FT_INTERNAL));
|
yuuji@0
|
770 txt = mail_fetchtext_full (sysibx,i,&txtlen,FT_INTERNAL|FT_PEEK);
|
yuuji@0
|
771 /* if have a message */
|
yuuji@0
|
772 if (j = hdrlen + txtlen) {
|
yuuji@0
|
773 /* calculate header line */
|
yuuji@0
|
774 mail_date (LOCAL->buf,elt = mail_elt (sysibx,i));
|
yuuji@0
|
775 sprintf (LOCAL->buf + strlen (LOCAL->buf),
|
yuuji@0
|
776 ",%lu;0000000000%02o\n",j,(unsigned)
|
yuuji@0
|
777 ((fSEEN * elt->seen) + (fDELETED * elt->deleted) +
|
yuuji@0
|
778 (fFLAGGED * elt->flagged) + (fANSWERED * elt->answered) +
|
yuuji@0
|
779 (fDRAFT * elt->draft)));
|
yuuji@0
|
780 /* copy message */
|
yuuji@0
|
781 if ((write (LOCAL->fd,LOCAL->buf,strlen (LOCAL->buf)) < 0) ||
|
yuuji@0
|
782 (write (LOCAL->fd,hdr,hdrlen) < 0) ||
|
yuuji@0
|
783 (write (LOCAL->fd,txt,txtlen) < 0)) r = 0;
|
yuuji@0
|
784 }
|
yuuji@0
|
785 fs_give ((void **) &hdr);
|
yuuji@0
|
786 }
|
yuuji@0
|
787
|
yuuji@0
|
788 /* make sure all the updates take */
|
yuuji@0
|
789 if (fsync (LOCAL->fd)) r = 0;
|
yuuji@0
|
790 if (r) { /* delete all the messages we copied */
|
yuuji@0
|
791 if (r == 1) strcpy (tmp,"1");
|
yuuji@0
|
792 else sprintf (tmp,"1:%lu",r);
|
yuuji@0
|
793 mail_flag (sysibx,tmp,"\\Deleted",ST_SET);
|
yuuji@0
|
794 mail_expunge (sysibx); /* now expunge all those messages */
|
yuuji@0
|
795 }
|
yuuji@0
|
796 else {
|
yuuji@0
|
797 sprintf (LOCAL->buf,"Can't copy new mail: %s",strerror (errno));
|
yuuji@0
|
798 MM_LOG (LOCAL->buf,WARN);
|
yuuji@0
|
799 ftruncate (LOCAL->fd,sbuf.st_size);
|
yuuji@0
|
800 }
|
yuuji@0
|
801 fstat (LOCAL->fd,&sbuf); /* yes, get current file size */
|
yuuji@0
|
802 LOCAL->filetime = sbuf.st_mtime;
|
yuuji@0
|
803 }
|
yuuji@0
|
804 if (sysibx) mail_close (sysibx);
|
yuuji@0
|
805 MM_NOCRITICAL (stream); /* release critical */
|
yuuji@0
|
806 unlockfd (ld,lock); /* release exclusive parse/append permission */
|
yuuji@0
|
807 LOCAL->lastsnarf = time (0);/* note time of last snarf */
|
yuuji@0
|
808 }
|
yuuji@0
|
809 }
|
yuuji@0
|
810
|
yuuji@0
|
811 /* Tenex mail expunge mailbox
|
yuuji@0
|
812 * Accepts: MAIL stream
|
yuuji@0
|
813 * sequence to expunge if non-NIL
|
yuuji@0
|
814 * expunge options
|
yuuji@0
|
815 * Returns: T, always
|
yuuji@0
|
816 */
|
yuuji@0
|
817
|
yuuji@0
|
818 long tenex_expunge (MAILSTREAM *stream,char *sequence,long options)
|
yuuji@0
|
819 {
|
yuuji@0
|
820 long ret;
|
yuuji@0
|
821 time_t tp[2];
|
yuuji@0
|
822 struct stat sbuf;
|
yuuji@0
|
823 off_t pos = 0;
|
yuuji@0
|
824 int ld;
|
yuuji@0
|
825 unsigned long i = 1;
|
yuuji@0
|
826 unsigned long j,k,m,recent;
|
yuuji@0
|
827 unsigned long n = 0;
|
yuuji@0
|
828 unsigned long delta = 0;
|
yuuji@0
|
829 char lock[MAILTMPLEN];
|
yuuji@0
|
830 MESSAGECACHE *elt;
|
yuuji@0
|
831 blocknotify_t bn = (blocknotify_t) mail_parameters (NIL,GET_BLOCKNOTIFY,NIL);
|
yuuji@0
|
832 if (!(ret = (sequence ? ((options & EX_UID) ?
|
yuuji@0
|
833 mail_uid_sequence (stream,sequence) :
|
yuuji@0
|
834 mail_sequence (stream,sequence)) : LONGT) &&
|
yuuji@0
|
835 tenex_ping (stream))); /* parse sequence if given, ping stream */
|
yuuji@0
|
836 else if (stream->rdonly) MM_LOG ("Expunge ignored on readonly mailbox",WARN);
|
yuuji@0
|
837 else {
|
yuuji@0
|
838 if (LOCAL->filetime && !LOCAL->shouldcheck) {
|
yuuji@0
|
839 fstat (LOCAL->fd,&sbuf); /* get current write time */
|
yuuji@0
|
840 if (LOCAL->filetime < sbuf.st_mtime) LOCAL->shouldcheck = T;
|
yuuji@0
|
841 }
|
yuuji@0
|
842 /* The cretins who designed flock() created a window of vulnerability in
|
yuuji@0
|
843 * upgrading locks from shared to exclusive or downgrading from exclusive
|
yuuji@0
|
844 * to shared. Rather than maintain the lock at shared status at a minimum,
|
yuuji@0
|
845 * flock() actually *releases* the former lock. Obviously they never talked
|
yuuji@0
|
846 * to any database guys. Fortunately, we have the parse/append permission
|
yuuji@0
|
847 * lock. If we require this lock before going exclusive on the mailbox,
|
yuuji@0
|
848 * another process can not sneak in and steal the exclusive mailbox lock on
|
yuuji@0
|
849 * us, because it will block on trying to get parse/append permission first.
|
yuuji@0
|
850 */
|
yuuji@0
|
851 /* get exclusive parse/append permission */
|
yuuji@0
|
852 if ((ld = lockfd (LOCAL->fd,lock,LOCK_EX)) < 0)
|
yuuji@0
|
853 MM_LOG ("Unable to lock expunge mailbox",ERROR);
|
yuuji@0
|
854 /* make sure see any newly-arrived messages */
|
yuuji@0
|
855 else if (!tenex_parse (stream));
|
yuuji@0
|
856 /* get exclusive access */
|
yuuji@0
|
857 else if (flock (LOCAL->fd,LOCK_EX|LOCK_NB)) {
|
yuuji@0
|
858 (*bn) (BLOCK_FILELOCK,NIL);
|
yuuji@0
|
859 flock (LOCAL->fd,LOCK_SH);/* recover previous lock */
|
yuuji@0
|
860 (*bn) (BLOCK_NONE,NIL);
|
yuuji@0
|
861 MM_LOG ("Can't expunge because mailbox is in use by another process",
|
yuuji@0
|
862 ERROR);
|
yuuji@0
|
863 unlockfd (ld,lock); /* release exclusive parse/append permission */
|
yuuji@0
|
864 }
|
yuuji@0
|
865
|
yuuji@0
|
866 else {
|
yuuji@0
|
867 MM_CRITICAL (stream); /* go critical */
|
yuuji@0
|
868 recent = stream->recent; /* get recent now that pinged and locked */
|
yuuji@0
|
869 /* for each message */
|
yuuji@0
|
870 while (i <= stream->nmsgs) {
|
yuuji@0
|
871 /* get cache element */
|
yuuji@0
|
872 elt = tenex_elt (stream,i);
|
yuuji@0
|
873 /* number of bytes to smash or preserve */
|
yuuji@0
|
874 k = elt->private.special.text.size + tenex_size (stream,i);
|
yuuji@0
|
875 /* if need to expunge this message */
|
yuuji@0
|
876 if (elt->deleted && (sequence ? elt->sequence : T)) {
|
yuuji@0
|
877 /* if recent, note one less recent message */
|
yuuji@0
|
878 if (elt->recent) --recent;
|
yuuji@0
|
879 delta += k; /* number of bytes to delete */
|
yuuji@0
|
880 /* notify upper levels */
|
yuuji@0
|
881 mail_expunged (stream,i);
|
yuuji@0
|
882 n++; /* count up one more expunged message */
|
yuuji@0
|
883 }
|
yuuji@0
|
884 else if (i++ && delta) {/* preserved message */
|
yuuji@0
|
885 /* first byte to preserve */
|
yuuji@0
|
886 j = elt->private.special.offset;
|
yuuji@0
|
887 do { /* read from source position */
|
yuuji@0
|
888 m = min (k,LOCAL->buflen);
|
yuuji@0
|
889 lseek (LOCAL->fd,j,L_SET);
|
yuuji@0
|
890 read (LOCAL->fd,LOCAL->buf,m);
|
yuuji@0
|
891 pos = j - delta; /* write to destination position */
|
yuuji@0
|
892 lseek (LOCAL->fd,pos,L_SET);
|
yuuji@0
|
893 while (T) {
|
yuuji@0
|
894 lseek (LOCAL->fd,pos,L_SET);
|
yuuji@0
|
895 if (write (LOCAL->fd,LOCAL->buf,m) > 0) break;
|
yuuji@0
|
896 MM_NOTIFY (stream,strerror (errno),WARN);
|
yuuji@0
|
897 MM_DISKERROR (stream,errno,T);
|
yuuji@0
|
898 }
|
yuuji@0
|
899 pos += m; /* new position */
|
yuuji@0
|
900 j += m; /* next chunk, perhaps */
|
yuuji@0
|
901 } while (k -= m); /* until done */
|
yuuji@0
|
902 /* note the new address of this text */
|
yuuji@0
|
903 elt->private.special.offset -= delta;
|
yuuji@0
|
904 }
|
yuuji@0
|
905 /* preserved but no deleted messages */
|
yuuji@0
|
906 else pos = elt->private.special.offset + k;
|
yuuji@0
|
907 }
|
yuuji@0
|
908
|
yuuji@0
|
909 if (n) { /* truncate file after last message */
|
yuuji@0
|
910 if (pos != (LOCAL->filesize -= delta)) {
|
yuuji@0
|
911 sprintf (LOCAL->buf,
|
yuuji@0
|
912 "Calculated size mismatch %lu != %lu, delta = %lu",
|
yuuji@0
|
913 (unsigned long) pos,(unsigned long) LOCAL->filesize,delta);
|
yuuji@0
|
914 MM_LOG (LOCAL->buf,WARN);
|
yuuji@0
|
915 LOCAL->filesize = pos;/* fix it then */
|
yuuji@0
|
916 }
|
yuuji@0
|
917 ftruncate (LOCAL->fd,LOCAL->filesize);
|
yuuji@0
|
918 sprintf (LOCAL->buf,"Expunged %lu messages",n);
|
yuuji@0
|
919 /* output the news */
|
yuuji@0
|
920 MM_LOG (LOCAL->buf,(long) NIL);
|
yuuji@0
|
921 }
|
yuuji@0
|
922 else MM_LOG ("No messages deleted, so no update needed",(long) NIL);
|
yuuji@0
|
923 fsync (LOCAL->fd); /* force disk update */
|
yuuji@0
|
924 fstat (LOCAL->fd,&sbuf); /* get new write time */
|
yuuji@0
|
925 tp[1] = LOCAL->filetime = sbuf.st_mtime;
|
yuuji@0
|
926 tp[0] = time (0); /* reset atime to now */
|
yuuji@0
|
927 utime (stream->mailbox,tp);
|
yuuji@0
|
928 MM_NOCRITICAL (stream); /* release critical */
|
yuuji@0
|
929 /* notify upper level of new mailbox size */
|
yuuji@0
|
930 mail_exists (stream,stream->nmsgs);
|
yuuji@0
|
931 mail_recent (stream,recent);
|
yuuji@0
|
932 (*bn) (BLOCK_FILELOCK,NIL);
|
yuuji@0
|
933 flock (LOCAL->fd,LOCK_SH);/* allow sharers again */
|
yuuji@0
|
934 (*bn) (BLOCK_NONE,NIL);
|
yuuji@0
|
935 unlockfd (ld,lock); /* release exclusive parse/append permission */
|
yuuji@0
|
936 }
|
yuuji@0
|
937 }
|
yuuji@0
|
938 return LONGT;
|
yuuji@0
|
939 }
|
yuuji@0
|
940
|
yuuji@0
|
941 /* Tenex mail copy message(s)
|
yuuji@0
|
942 * Accepts: MAIL stream
|
yuuji@0
|
943 * sequence
|
yuuji@0
|
944 * destination mailbox
|
yuuji@0
|
945 * copy options
|
yuuji@0
|
946 * Returns: T if success, NIL if failed
|
yuuji@0
|
947 */
|
yuuji@0
|
948
|
yuuji@0
|
949 long tenex_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options)
|
yuuji@0
|
950 {
|
yuuji@0
|
951 struct stat sbuf;
|
yuuji@0
|
952 time_t tp[2];
|
yuuji@0
|
953 MESSAGECACHE *elt;
|
yuuji@0
|
954 unsigned long i,j,k;
|
yuuji@0
|
955 long ret = LONGT;
|
yuuji@0
|
956 int fd,ld;
|
yuuji@0
|
957 char file[MAILTMPLEN],lock[MAILTMPLEN];
|
yuuji@0
|
958 mailproxycopy_t pc =
|
yuuji@0
|
959 (mailproxycopy_t) mail_parameters (stream,GET_MAILPROXYCOPY,NIL);
|
yuuji@0
|
960 /* make sure valid mailbox */
|
yuuji@0
|
961 if (!tenex_isvalid (mailbox,LOCAL->buf)) switch (errno) {
|
yuuji@0
|
962 case ENOENT: /* no such file? */
|
yuuji@0
|
963 MM_NOTIFY (stream,"[TRYCREATE] Must create mailbox before copy",NIL);
|
yuuji@0
|
964 return NIL;
|
yuuji@0
|
965 case 0: /* merely empty file? */
|
yuuji@0
|
966 break;
|
yuuji@0
|
967 case EACCES: /* file protected */
|
yuuji@0
|
968 sprintf (LOCAL->buf,"Can't access destination: %.80s",mailbox);
|
yuuji@0
|
969 MM_LOG (LOCAL->buf,ERROR);
|
yuuji@0
|
970 return NIL;
|
yuuji@0
|
971 case EINVAL:
|
yuuji@0
|
972 if (pc) return (*pc) (stream,sequence,mailbox,options);
|
yuuji@0
|
973 sprintf (LOCAL->buf,"Invalid Tenex-format mailbox name: %.80s",mailbox);
|
yuuji@0
|
974 MM_LOG (LOCAL->buf,ERROR);
|
yuuji@0
|
975 return NIL;
|
yuuji@0
|
976 default:
|
yuuji@0
|
977 if (pc) return (*pc) (stream,sequence,mailbox,options);
|
yuuji@0
|
978 sprintf (LOCAL->buf,"Not a Tenex-format mailbox: %.80s",mailbox);
|
yuuji@0
|
979 MM_LOG (LOCAL->buf,ERROR);
|
yuuji@0
|
980 return NIL;
|
yuuji@0
|
981 }
|
yuuji@0
|
982 if (!((options & CP_UID) ? mail_uid_sequence (stream,sequence) :
|
yuuji@0
|
983 mail_sequence (stream,sequence))) return NIL;
|
yuuji@0
|
984 /* got file? */
|
yuuji@0
|
985 if ((fd = open (tenex_file(file,mailbox),O_RDWR,NIL)) < 0) {
|
yuuji@0
|
986 sprintf (LOCAL->buf,"Unable to open copy mailbox: %s",strerror (errno));
|
yuuji@0
|
987 MM_LOG (LOCAL->buf,ERROR);
|
yuuji@0
|
988 return NIL;
|
yuuji@0
|
989 }
|
yuuji@0
|
990 MM_CRITICAL (stream); /* go critical */
|
yuuji@0
|
991 /* get exclusive parse/append permission */
|
yuuji@0
|
992 if (flock (fd,LOCK_SH) || ((ld = lockfd (fd,lock,LOCK_EX)) < 0)) {
|
yuuji@0
|
993 MM_LOG ("Unable to lock copy mailbox",ERROR);
|
yuuji@0
|
994 MM_NOCRITICAL (stream);
|
yuuji@0
|
995 return NIL;
|
yuuji@0
|
996 }
|
yuuji@0
|
997 fstat (fd,&sbuf); /* get current file size */
|
yuuji@0
|
998 lseek (fd,sbuf.st_size,L_SET);/* move to end of file */
|
yuuji@0
|
999
|
yuuji@0
|
1000 /* for each requested message */
|
yuuji@0
|
1001 for (i = 1; ret && (i <= stream->nmsgs); i++)
|
yuuji@0
|
1002 if ((elt = mail_elt (stream,i))->sequence) {
|
yuuji@0
|
1003 lseek (LOCAL->fd,elt->private.special.offset,L_SET);
|
yuuji@0
|
1004 /* number of bytes to copy */
|
yuuji@0
|
1005 k = elt->private.special.text.size + tenex_size (stream,i);
|
yuuji@0
|
1006 do { /* read from source position */
|
yuuji@0
|
1007 j = min (k,LOCAL->buflen);
|
yuuji@0
|
1008 read (LOCAL->fd,LOCAL->buf,j);
|
yuuji@0
|
1009 if (write (fd,LOCAL->buf,j) < 0) ret = NIL;
|
yuuji@0
|
1010 } while (ret && (k -= j));/* until done */
|
yuuji@0
|
1011 }
|
yuuji@0
|
1012 /* make sure all the updates take */
|
yuuji@0
|
1013 if (!(ret && (ret = !fsync (fd)))) {
|
yuuji@0
|
1014 sprintf (LOCAL->buf,"Unable to write message: %s",strerror (errno));
|
yuuji@0
|
1015 MM_LOG (LOCAL->buf,ERROR);
|
yuuji@0
|
1016 ftruncate (fd,sbuf.st_size);
|
yuuji@0
|
1017 }
|
yuuji@0
|
1018 if (ret) tp[0] = time (0) - 1;/* set atime to now-1 if successful copy */
|
yuuji@0
|
1019 /* else preserve \Marked status */
|
yuuji@0
|
1020 else tp[0] = (sbuf.st_ctime > sbuf.st_atime) ? sbuf.st_atime : time(0);
|
yuuji@0
|
1021 tp[1] = sbuf.st_mtime; /* preserve mtime */
|
yuuji@0
|
1022 utime (file,tp); /* set the times */
|
yuuji@0
|
1023 close (fd); /* close the file */
|
yuuji@0
|
1024 unlockfd (ld,lock); /* release exclusive parse/append permission */
|
yuuji@0
|
1025 MM_NOCRITICAL (stream); /* release critical */
|
yuuji@0
|
1026 /* delete all requested messages */
|
yuuji@0
|
1027 if (ret && (options & CP_MOVE)) {
|
yuuji@0
|
1028 for (i = 1; i <= stream->nmsgs; i++)
|
yuuji@0
|
1029 if ((elt = tenex_elt (stream,i))->sequence) {
|
yuuji@0
|
1030 elt->deleted = T; /* mark message deleted */
|
yuuji@0
|
1031 /* recalculate status */
|
yuuji@0
|
1032 tenex_update_status (stream,i,NIL);
|
yuuji@0
|
1033 }
|
yuuji@0
|
1034 if (!stream->rdonly) { /* make sure the update takes */
|
yuuji@0
|
1035 fsync (LOCAL->fd);
|
yuuji@0
|
1036 fstat (LOCAL->fd,&sbuf); /* get current write time */
|
yuuji@0
|
1037 tp[1] = LOCAL->filetime = sbuf.st_mtime;
|
yuuji@0
|
1038 tp[0] = time (0); /* make sure atime remains greater */
|
yuuji@0
|
1039 utime (stream->mailbox,tp);
|
yuuji@0
|
1040 }
|
yuuji@0
|
1041 }
|
yuuji@0
|
1042 if (ret && mail_parameters (NIL,GET_COPYUID,NIL))
|
yuuji@0
|
1043 MM_LOG ("Can not return meaningful COPYUID with this mailbox format",WARN);
|
yuuji@0
|
1044 return ret;
|
yuuji@0
|
1045 }
|
yuuji@0
|
1046
|
yuuji@0
|
1047 /* Tenex mail append message from stringstruct
|
yuuji@0
|
1048 * Accepts: MAIL stream
|
yuuji@0
|
1049 * destination mailbox
|
yuuji@0
|
1050 * append callback
|
yuuji@0
|
1051 * data for callback
|
yuuji@0
|
1052 * Returns: T if append successful, else NIL
|
yuuji@0
|
1053 */
|
yuuji@0
|
1054
|
yuuji@0
|
1055 long tenex_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data)
|
yuuji@0
|
1056 {
|
yuuji@0
|
1057 struct stat sbuf;
|
yuuji@0
|
1058 int fd,ld,c;
|
yuuji@0
|
1059 char *flags,*date,tmp[MAILTMPLEN],file[MAILTMPLEN],lock[MAILTMPLEN];
|
yuuji@0
|
1060 time_t tp[2];
|
yuuji@0
|
1061 FILE *df;
|
yuuji@0
|
1062 MESSAGECACHE elt;
|
yuuji@0
|
1063 long f;
|
yuuji@0
|
1064 unsigned long i,j,uf,size;
|
yuuji@0
|
1065 STRING *message;
|
yuuji@0
|
1066 long ret = LONGT;
|
yuuji@0
|
1067 /* default stream to prototype */
|
yuuji@0
|
1068 if (!stream) stream = user_flags (&tenexproto);
|
yuuji@0
|
1069 /* make sure valid mailbox */
|
yuuji@0
|
1070 if (!tenex_isvalid (mailbox,tmp)) switch (errno) {
|
yuuji@0
|
1071 case ENOENT: /* no such file? */
|
yuuji@0
|
1072 if (!compare_cstring (mailbox,"INBOX")) dummy_create (NIL,"mail.txt");
|
yuuji@0
|
1073 else {
|
yuuji@0
|
1074 MM_NOTIFY (stream,"[TRYCREATE] Must create mailbox before append",NIL);
|
yuuji@0
|
1075 return NIL;
|
yuuji@0
|
1076 }
|
yuuji@0
|
1077 /* falls through */
|
yuuji@0
|
1078 case 0: /* merely empty file? */
|
yuuji@0
|
1079 break;
|
yuuji@0
|
1080 case EACCES: /* file protected */
|
yuuji@0
|
1081 sprintf (tmp,"Can't access destination: %.80s",mailbox);
|
yuuji@0
|
1082 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1083 return NIL;
|
yuuji@0
|
1084 case EINVAL:
|
yuuji@0
|
1085 sprintf (tmp,"Invalid TENEX-format mailbox name: %.80s",mailbox);
|
yuuji@0
|
1086 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1087 return NIL;
|
yuuji@0
|
1088 default:
|
yuuji@0
|
1089 sprintf (tmp,"Not a TENEX-format mailbox: %.80s",mailbox);
|
yuuji@0
|
1090 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1091 return NIL;
|
yuuji@0
|
1092 }
|
yuuji@0
|
1093 /* get first message */
|
yuuji@0
|
1094 if (!MM_APPEND (af) (stream,data,&flags,&date,&message)) return NIL;
|
yuuji@0
|
1095
|
yuuji@0
|
1096 /* open destination mailbox */
|
yuuji@0
|
1097 if (((fd = open (tenex_file (file,mailbox),O_WRONLY|O_APPEND,NIL)) < 0) ||
|
yuuji@0
|
1098 !(df = fdopen (fd,"ab"))) {
|
yuuji@0
|
1099 sprintf (tmp,"Can't open append mailbox: %s",strerror (errno));
|
yuuji@0
|
1100 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1101 return NIL;
|
yuuji@0
|
1102 }
|
yuuji@0
|
1103 /* get parse/append permission */
|
yuuji@0
|
1104 if (flock (fd,LOCK_SH) || ((ld = lockfd (fd,lock,LOCK_EX)) < 0)) {
|
yuuji@0
|
1105 MM_LOG ("Unable to lock append mailbox",ERROR);
|
yuuji@0
|
1106 close (fd);
|
yuuji@0
|
1107 return NIL;
|
yuuji@0
|
1108 }
|
yuuji@0
|
1109 MM_CRITICAL (stream); /* go critical */
|
yuuji@0
|
1110 fstat (fd,&sbuf); /* get current file size */
|
yuuji@0
|
1111 errno = 0;
|
yuuji@0
|
1112 do { /* parse flags */
|
yuuji@0
|
1113 if (!SIZE (message)) { /* guard against zero-length */
|
yuuji@0
|
1114 MM_LOG ("Append of zero-length message",ERROR);
|
yuuji@0
|
1115 ret = NIL;
|
yuuji@0
|
1116 break;
|
yuuji@0
|
1117 }
|
yuuji@0
|
1118 f = mail_parse_flags (stream,flags,&i);
|
yuuji@0
|
1119 /* reverse bits (dontcha wish we had CIRC?) */
|
yuuji@0
|
1120 for (uf = 0; i; uf |= 1 << (29 - find_rightmost_bit (&i)));
|
yuuji@0
|
1121 if (date) { /* parse date if given */
|
yuuji@0
|
1122 if (!mail_parse_date (&elt,date)) {
|
yuuji@0
|
1123 sprintf (tmp,"Bad date in append: %.80s",date);
|
yuuji@0
|
1124 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1125 ret = NIL; /* mark failure */
|
yuuji@0
|
1126 break;
|
yuuji@0
|
1127 }
|
yuuji@0
|
1128 mail_date (tmp,&elt); /* write preseved date */
|
yuuji@0
|
1129 }
|
yuuji@0
|
1130 else internal_date (tmp); /* get current date in IMAP format */
|
yuuji@0
|
1131 i = GETPOS (message); /* remember current position */
|
yuuji@0
|
1132 for (j = SIZE (message), size = 0; j; --j)
|
yuuji@0
|
1133 if (SNX (message) != '\015') ++size;
|
yuuji@0
|
1134 SETPOS (message,i); /* restore position */
|
yuuji@0
|
1135 /* write header */
|
yuuji@0
|
1136 if (fprintf (df,"%s,%lu;%010lo%02lo\n",tmp,size,uf,(unsigned long) f) < 0)
|
yuuji@0
|
1137 ret = NIL;
|
yuuji@0
|
1138 else { /* write message */
|
yuuji@0
|
1139 while (size) if ((c = 0xff & SNX (message)) != '\015') {
|
yuuji@0
|
1140 if (putc (c,df) != EOF) --size;
|
yuuji@0
|
1141 else break;
|
yuuji@0
|
1142 }
|
yuuji@0
|
1143 /* get next message */
|
yuuji@0
|
1144 if (size || !MM_APPEND (af) (stream,data,&flags,&date,&message))
|
yuuji@0
|
1145 ret = NIL;
|
yuuji@0
|
1146 }
|
yuuji@0
|
1147 } while (ret && message);
|
yuuji@0
|
1148 /* if error... */
|
yuuji@0
|
1149 if (!ret || (fflush (df) == EOF)) {
|
yuuji@0
|
1150 ftruncate (fd,sbuf.st_size);/* revert file */
|
yuuji@0
|
1151 close (fd); /* make sure fclose() doesn't corrupt us */
|
yuuji@0
|
1152 if (errno) {
|
yuuji@0
|
1153 sprintf (tmp,"Message append failed: %s",strerror (errno));
|
yuuji@0
|
1154 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1155 }
|
yuuji@0
|
1156 ret = NIL;
|
yuuji@0
|
1157 }
|
yuuji@0
|
1158 if (ret) tp[0] = time (0) - 1;/* set atime to now-1 if successful copy */
|
yuuji@0
|
1159 /* else preserve \Marked status */
|
yuuji@0
|
1160 else tp[0] = (sbuf.st_ctime > sbuf.st_atime) ? sbuf.st_atime : time(0);
|
yuuji@0
|
1161 tp[1] = sbuf.st_mtime; /* preserve mtime */
|
yuuji@0
|
1162 utime (file,tp); /* set the times */
|
yuuji@0
|
1163 fclose (df); /* close the file */
|
yuuji@0
|
1164 unlockfd (ld,lock); /* release exclusive parse/append permission */
|
yuuji@0
|
1165 MM_NOCRITICAL (stream); /* release critical */
|
yuuji@0
|
1166 if (ret && mail_parameters (NIL,GET_APPENDUID,NIL))
|
yuuji@0
|
1167 MM_LOG ("Can not return meaningful APPENDUID with this mailbox format",
|
yuuji@0
|
1168 WARN);
|
yuuji@0
|
1169 return ret;
|
yuuji@0
|
1170 }
|
yuuji@0
|
1171
|
yuuji@0
|
1172 /* Internal routines */
|
yuuji@0
|
1173
|
yuuji@0
|
1174
|
yuuji@0
|
1175 /* Tenex mail return internal message size in bytes
|
yuuji@0
|
1176 * Accepts: MAIL stream
|
yuuji@0
|
1177 * message #
|
yuuji@0
|
1178 * Returns: internal size of message
|
yuuji@0
|
1179 */
|
yuuji@0
|
1180
|
yuuji@0
|
1181 unsigned long tenex_size (MAILSTREAM *stream,unsigned long m)
|
yuuji@0
|
1182 {
|
yuuji@0
|
1183 MESSAGECACHE *elt = mail_elt (stream,m);
|
yuuji@0
|
1184 return ((m < stream->nmsgs) ? mail_elt (stream,m+1)->private.special.offset :
|
yuuji@0
|
1185 LOCAL->filesize) -
|
yuuji@0
|
1186 (elt->private.special.offset + elt->private.special.text.size);
|
yuuji@0
|
1187 }
|
yuuji@0
|
1188
|
yuuji@0
|
1189
|
yuuji@0
|
1190 /* Tenex mail generate file string
|
yuuji@0
|
1191 * Accepts: temporary buffer to write into
|
yuuji@0
|
1192 * mailbox name string
|
yuuji@0
|
1193 * Returns: local file string or NIL if failure
|
yuuji@0
|
1194 */
|
yuuji@0
|
1195
|
yuuji@0
|
1196 char *tenex_file (char *dst,char *name)
|
yuuji@0
|
1197 {
|
yuuji@0
|
1198 char tmp[MAILTMPLEN];
|
yuuji@0
|
1199 char *s = mailboxfile (dst,name);
|
yuuji@0
|
1200 /* return our standard inbox */
|
yuuji@0
|
1201 return (s && !*s) ? mailboxfile (dst,tenex_isvalid ("~/INBOX",tmp) ?
|
yuuji@0
|
1202 "~/INBOX" : "mail.txt") : s;
|
yuuji@0
|
1203 }
|
yuuji@0
|
1204
|
yuuji@0
|
1205 /* Tenex mail parse mailbox
|
yuuji@0
|
1206 * Accepts: MAIL stream
|
yuuji@0
|
1207 * Returns: T if parse OK
|
yuuji@0
|
1208 * NIL if failure, stream aborted
|
yuuji@0
|
1209 */
|
yuuji@0
|
1210
|
yuuji@0
|
1211 long tenex_parse (MAILSTREAM *stream)
|
yuuji@0
|
1212 {
|
yuuji@0
|
1213 struct stat sbuf;
|
yuuji@0
|
1214 MESSAGECACHE *elt = NIL;
|
yuuji@0
|
1215 unsigned char c,*s,*t,*x;
|
yuuji@0
|
1216 char tmp[MAILTMPLEN];
|
yuuji@0
|
1217 unsigned long i,j;
|
yuuji@0
|
1218 long curpos = LOCAL->filesize;
|
yuuji@0
|
1219 long nmsgs = stream->nmsgs;
|
yuuji@0
|
1220 long recent = stream->recent;
|
yuuji@0
|
1221 short added = NIL;
|
yuuji@0
|
1222 short silent = stream->silent;
|
yuuji@0
|
1223 fstat (LOCAL->fd,&sbuf); /* get status */
|
yuuji@0
|
1224 if (sbuf.st_size < curpos) { /* sanity check */
|
yuuji@0
|
1225 sprintf (tmp,"Mailbox shrank from %lu to %lu!",
|
yuuji@0
|
1226 (unsigned long) curpos,(unsigned long) sbuf.st_size);
|
yuuji@0
|
1227 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1228 tenex_close (stream,NIL);
|
yuuji@0
|
1229 return NIL;
|
yuuji@0
|
1230 }
|
yuuji@0
|
1231 stream->silent = T; /* don't pass up exists events yet */
|
yuuji@0
|
1232 while (sbuf.st_size - curpos){/* while there is stuff to parse */
|
yuuji@0
|
1233 /* get to that position in the file */
|
yuuji@0
|
1234 lseek (LOCAL->fd,curpos,L_SET);
|
yuuji@0
|
1235 if ((i = read (LOCAL->fd,LOCAL->buf,64)) <= 0) {
|
yuuji@0
|
1236 sprintf (tmp,"Unable to read internal header at %lu, size = %lu: %s",
|
yuuji@0
|
1237 (unsigned long) curpos,(unsigned long) sbuf.st_size,
|
yuuji@0
|
1238 i ? strerror (errno) : "no data read");
|
yuuji@0
|
1239 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1240 tenex_close (stream,NIL);
|
yuuji@0
|
1241 return NIL;
|
yuuji@0
|
1242 }
|
yuuji@0
|
1243 LOCAL->buf[i] = '\0'; /* tie off buffer just in case */
|
yuuji@0
|
1244 if (!(s = strchr (LOCAL->buf,'\012'))) {
|
yuuji@0
|
1245 sprintf (tmp,"Unable to find newline at %lu in %lu bytes, text: %s",
|
yuuji@0
|
1246 (unsigned long) curpos,i,(char *) LOCAL->buf);
|
yuuji@0
|
1247 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1248 tenex_close (stream,NIL);
|
yuuji@0
|
1249 return NIL;
|
yuuji@0
|
1250 }
|
yuuji@0
|
1251 *s = '\0'; /* tie off header line */
|
yuuji@0
|
1252 i = (s + 1) - LOCAL->buf; /* note start of text offset */
|
yuuji@0
|
1253 if (!((s = strchr (LOCAL->buf,',')) && (t = strchr (s+1,';')))) {
|
yuuji@0
|
1254 sprintf (tmp,"Unable to parse internal header at %lu: %s",
|
yuuji@0
|
1255 (unsigned long) curpos,(char *) LOCAL->buf);
|
yuuji@0
|
1256 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1257 tenex_close (stream,NIL);
|
yuuji@0
|
1258 return NIL;
|
yuuji@0
|
1259 }
|
yuuji@0
|
1260 *s++ = '\0'; *t++ = '\0'; /* tie off fields */
|
yuuji@0
|
1261
|
yuuji@0
|
1262 added = T; /* note that a new message was added */
|
yuuji@0
|
1263 /* swell the cache */
|
yuuji@0
|
1264 mail_exists (stream,++nmsgs);
|
yuuji@0
|
1265 /* instantiate an elt for this message */
|
yuuji@0
|
1266 (elt = mail_elt (stream,nmsgs))->valid = T;
|
yuuji@0
|
1267 elt->private.uid = ++stream->uid_last;
|
yuuji@0
|
1268 /* note file offset of header */
|
yuuji@0
|
1269 elt->private.special.offset = curpos;
|
yuuji@0
|
1270 /* in case error */
|
yuuji@0
|
1271 elt->private.special.text.size = 0;
|
yuuji@0
|
1272 /* header size not known yet */
|
yuuji@0
|
1273 elt->private.msg.header.text.size = 0;
|
yuuji@0
|
1274 x = s; /* parse the header components */
|
yuuji@0
|
1275 if (mail_parse_date (elt,LOCAL->buf) &&
|
yuuji@0
|
1276 (elt->private.msg.full.text.size = strtoul (s,(char **) &s,10)) &&
|
yuuji@0
|
1277 (!(s && *s)) && isdigit (t[0]) && isdigit (t[1]) && isdigit (t[2]) &&
|
yuuji@0
|
1278 isdigit (t[3]) && isdigit (t[4]) && isdigit (t[5]) &&
|
yuuji@0
|
1279 isdigit (t[6]) && isdigit (t[7]) && isdigit (t[8]) &&
|
yuuji@0
|
1280 isdigit (t[9]) && isdigit (t[10]) && isdigit (t[11]) && !t[12])
|
yuuji@0
|
1281 elt->private.special.text.size = i;
|
yuuji@0
|
1282 else { /* oops */
|
yuuji@0
|
1283 sprintf (tmp,"Unable to parse internal header elements at %ld: %s,%s;%s",
|
yuuji@0
|
1284 curpos,(char *) LOCAL->buf,(char *) x,(char *) t);
|
yuuji@0
|
1285 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1286 tenex_close (stream,NIL);
|
yuuji@0
|
1287 return NIL;
|
yuuji@0
|
1288 }
|
yuuji@0
|
1289 /* make sure didn't run off end of file */
|
yuuji@0
|
1290 if ((curpos += (elt->private.msg.full.text.size + i)) > sbuf.st_size) {
|
yuuji@0
|
1291 sprintf (tmp,"Last message (at %lu) runs past end of file (%lu > %lu)",
|
yuuji@0
|
1292 elt->private.special.offset,(unsigned long) curpos,
|
yuuji@0
|
1293 (unsigned long) sbuf.st_size);
|
yuuji@0
|
1294 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1295 tenex_close (stream,NIL);
|
yuuji@0
|
1296 return NIL;
|
yuuji@0
|
1297 }
|
yuuji@0
|
1298 c = t[10]; /* remember first system flags byte */
|
yuuji@0
|
1299 t[10] = '\0'; /* tie off flags */
|
yuuji@0
|
1300 j = strtoul (t,NIL,8); /* get user flags value */
|
yuuji@0
|
1301 t[10] = c; /* restore first system flags byte */
|
yuuji@0
|
1302 /* set up all valid user flags (reversed!) */
|
yuuji@0
|
1303 while (j) if (((i = 29 - find_rightmost_bit (&j)) < NUSERFLAGS) &&
|
yuuji@0
|
1304 stream->user_flags[i]) elt->user_flags |= 1 << i;
|
yuuji@0
|
1305 /* calculate system flags */
|
yuuji@0
|
1306 if ((j = ((t[10]-'0') * 8) + t[11]-'0') & fSEEN) elt->seen = T;
|
yuuji@0
|
1307 if (j & fDELETED) elt->deleted = T;
|
yuuji@0
|
1308 if (j & fFLAGGED) elt->flagged = T;
|
yuuji@0
|
1309 if (j & fANSWERED) elt->answered = T;
|
yuuji@0
|
1310 if (j & fDRAFT) elt->draft = T;
|
yuuji@0
|
1311 if (!(j & fOLD)) { /* newly arrived message? */
|
yuuji@0
|
1312 elt->recent = T;
|
yuuji@0
|
1313 recent++; /* count up a new recent message */
|
yuuji@0
|
1314 /* mark it as old */
|
yuuji@0
|
1315 tenex_update_status (stream,nmsgs,NIL);
|
yuuji@0
|
1316 }
|
yuuji@0
|
1317 }
|
yuuji@0
|
1318 fsync (LOCAL->fd); /* make sure all the fOLD flags take */
|
yuuji@0
|
1319 /* update parsed file size and time */
|
yuuji@0
|
1320 LOCAL->filesize = sbuf.st_size;
|
yuuji@0
|
1321 fstat (LOCAL->fd,&sbuf); /* get status again to ensure time is right */
|
yuuji@0
|
1322 LOCAL->filetime = sbuf.st_mtime;
|
yuuji@0
|
1323 if (added && !stream->rdonly){/* make sure atime updated */
|
yuuji@0
|
1324 time_t tp[2];
|
yuuji@0
|
1325 tp[0] = time (0);
|
yuuji@0
|
1326 tp[1] = LOCAL->filetime;
|
yuuji@0
|
1327 utime (stream->mailbox,tp);
|
yuuji@0
|
1328 }
|
yuuji@0
|
1329 stream->silent = silent; /* can pass up events now */
|
yuuji@0
|
1330 mail_exists (stream,nmsgs); /* notify upper level of new mailbox size */
|
yuuji@0
|
1331 mail_recent (stream,recent); /* and of change in recent messages */
|
yuuji@0
|
1332 return LONGT; /* return the winnage */
|
yuuji@0
|
1333 }
|
yuuji@0
|
1334
|
yuuji@0
|
1335 /* Tenex get cache element with status updating from file
|
yuuji@0
|
1336 * Accepts: MAIL stream
|
yuuji@0
|
1337 * message number
|
yuuji@0
|
1338 * Returns: cache element
|
yuuji@0
|
1339 */
|
yuuji@0
|
1340
|
yuuji@0
|
1341 MESSAGECACHE *tenex_elt (MAILSTREAM *stream,unsigned long msgno)
|
yuuji@0
|
1342 {
|
yuuji@0
|
1343 MESSAGECACHE *elt = mail_elt (stream,msgno);
|
yuuji@0
|
1344 struct { /* old flags */
|
yuuji@0
|
1345 unsigned int seen : 1;
|
yuuji@0
|
1346 unsigned int deleted : 1;
|
yuuji@0
|
1347 unsigned int flagged : 1;
|
yuuji@0
|
1348 unsigned int answered : 1;
|
yuuji@0
|
1349 unsigned int draft : 1;
|
yuuji@0
|
1350 unsigned long user_flags;
|
yuuji@0
|
1351 } old;
|
yuuji@0
|
1352 old.seen = elt->seen; old.deleted = elt->deleted; old.flagged = elt->flagged;
|
yuuji@0
|
1353 old.answered = elt->answered; old.draft = elt->draft;
|
yuuji@0
|
1354 old.user_flags = elt->user_flags;
|
yuuji@0
|
1355 tenex_read_flags (stream,elt);
|
yuuji@0
|
1356 if ((old.seen != elt->seen) || (old.deleted != elt->deleted) ||
|
yuuji@0
|
1357 (old.flagged != elt->flagged) || (old.answered != elt->answered) ||
|
yuuji@0
|
1358 (old.draft != elt->draft) || (old.user_flags != elt->user_flags))
|
yuuji@0
|
1359 MM_FLAGS (stream,msgno); /* let top level know */
|
yuuji@0
|
1360 return elt;
|
yuuji@0
|
1361 }
|
yuuji@0
|
1362
|
yuuji@0
|
1363 /* Tenex read flags from file
|
yuuji@0
|
1364 * Accepts: MAIL stream
|
yuuji@0
|
1365 * Returns: cache element
|
yuuji@0
|
1366 */
|
yuuji@0
|
1367
|
yuuji@0
|
1368 void tenex_read_flags (MAILSTREAM *stream,MESSAGECACHE *elt)
|
yuuji@0
|
1369 {
|
yuuji@0
|
1370 unsigned long i,j;
|
yuuji@0
|
1371 /* noop if readonly and have valid flags */
|
yuuji@0
|
1372 if (stream->rdonly && elt->valid) return;
|
yuuji@0
|
1373 /* set the seek pointer */
|
yuuji@0
|
1374 lseek (LOCAL->fd,(off_t) elt->private.special.offset +
|
yuuji@0
|
1375 elt->private.special.text.size - 13,L_SET);
|
yuuji@0
|
1376 /* read the new flags */
|
yuuji@0
|
1377 if (read (LOCAL->fd,LOCAL->buf,12) < 0) {
|
yuuji@0
|
1378 sprintf (LOCAL->buf,"Unable to read new status: %s",strerror (errno));
|
yuuji@0
|
1379 fatal (LOCAL->buf);
|
yuuji@0
|
1380 }
|
yuuji@0
|
1381 /* calculate system flags */
|
yuuji@0
|
1382 i = (((LOCAL->buf[10]-'0') * 8) + LOCAL->buf[11]-'0');
|
yuuji@0
|
1383 elt->seen = i & fSEEN ? T : NIL; elt->deleted = i & fDELETED ? T : NIL;
|
yuuji@0
|
1384 elt->flagged = i & fFLAGGED ? T : NIL;
|
yuuji@0
|
1385 elt->answered = i & fANSWERED ? T : NIL; elt->draft = i & fDRAFT ? T : NIL;
|
yuuji@0
|
1386 LOCAL->buf[10] = '\0'; /* tie off flags */
|
yuuji@0
|
1387 j = strtoul(LOCAL->buf,NIL,8);/* get user flags value */
|
yuuji@0
|
1388 /* set up all valid user flags (reversed!) */
|
yuuji@0
|
1389 while (j) if (((i = 29 - find_rightmost_bit (&j)) < NUSERFLAGS) &&
|
yuuji@0
|
1390 stream->user_flags[i]) elt->user_flags |= 1 << i;
|
yuuji@0
|
1391 elt->valid = T; /* have valid flags now */
|
yuuji@0
|
1392 }
|
yuuji@0
|
1393
|
yuuji@0
|
1394 /* Tenex update status string
|
yuuji@0
|
1395 * Accepts: MAIL stream
|
yuuji@0
|
1396 * message number
|
yuuji@0
|
1397 * flag saying whether or not to sync
|
yuuji@0
|
1398 */
|
yuuji@0
|
1399
|
yuuji@0
|
1400 void tenex_update_status (MAILSTREAM *stream,unsigned long msgno,long syncflag)
|
yuuji@0
|
1401 {
|
yuuji@0
|
1402 time_t tp[2];
|
yuuji@0
|
1403 struct stat sbuf;
|
yuuji@0
|
1404 MESSAGECACHE *elt = mail_elt (stream,msgno);
|
yuuji@0
|
1405 unsigned long j,k = 0;
|
yuuji@0
|
1406 /* readonly */
|
yuuji@0
|
1407 if (stream->rdonly || !elt->valid) tenex_read_flags (stream,elt);
|
yuuji@0
|
1408 else { /* readwrite */
|
yuuji@0
|
1409 j = elt->user_flags; /* get user flags */
|
yuuji@0
|
1410 /* reverse bits (dontcha wish we had CIRC?) */
|
yuuji@0
|
1411 while (j) k |= 1 << (29 - find_rightmost_bit (&j));
|
yuuji@0
|
1412 /* print new flag string */
|
yuuji@0
|
1413 sprintf (LOCAL->buf,"%010lo%02o",k,(unsigned)
|
yuuji@0
|
1414 (fOLD + (fSEEN * elt->seen) + (fDELETED * elt->deleted) +
|
yuuji@0
|
1415 (fFLAGGED * elt->flagged) + (fANSWERED * elt->answered) +
|
yuuji@0
|
1416 (fDRAFT * elt->draft)));
|
yuuji@0
|
1417 /* get to that place in the file */
|
yuuji@0
|
1418 lseek (LOCAL->fd,(off_t) elt->private.special.offset +
|
yuuji@0
|
1419 elt->private.special.text.size - 13,L_SET);
|
yuuji@0
|
1420 /* write new flags */
|
yuuji@0
|
1421 write (LOCAL->fd,LOCAL->buf,12);
|
yuuji@0
|
1422 if (syncflag) { /* sync if requested */
|
yuuji@0
|
1423 fsync (LOCAL->fd);
|
yuuji@0
|
1424 fstat (LOCAL->fd,&sbuf); /* get new write time */
|
yuuji@0
|
1425 tp[1] = LOCAL->filetime = sbuf.st_mtime;
|
yuuji@0
|
1426 tp[0] = time (0); /* make sure read is later */
|
yuuji@0
|
1427 utime (stream->mailbox,tp);
|
yuuji@0
|
1428 }
|
yuuji@0
|
1429 }
|
yuuji@0
|
1430 }
|
yuuji@0
|
1431
|
yuuji@0
|
1432 /* Tenex locate header for a message
|
yuuji@0
|
1433 * Accepts: MAIL stream
|
yuuji@0
|
1434 * message number
|
yuuji@0
|
1435 * pointer to returned header size
|
yuuji@0
|
1436 * Returns: position of header in file
|
yuuji@0
|
1437 */
|
yuuji@0
|
1438
|
yuuji@0
|
1439 unsigned long tenex_hdrpos (MAILSTREAM *stream,unsigned long msgno,
|
yuuji@0
|
1440 unsigned long *size)
|
yuuji@0
|
1441 {
|
yuuji@0
|
1442 unsigned long siz;
|
yuuji@0
|
1443 long i = 0;
|
yuuji@0
|
1444 char c = '\0';
|
yuuji@0
|
1445 char *s = NIL;
|
yuuji@0
|
1446 MESSAGECACHE *elt = tenex_elt (stream,msgno);
|
yuuji@0
|
1447 unsigned long ret = elt->private.special.offset +
|
yuuji@0
|
1448 elt->private.special.text.size;
|
yuuji@0
|
1449 unsigned long msiz = tenex_size (stream,msgno);
|
yuuji@0
|
1450 /* is header size known? */
|
yuuji@0
|
1451 if (!(*size = elt->private.msg.header.text.size)) {
|
yuuji@0
|
1452 lseek (LOCAL->fd,ret,L_SET);/* get to header position */
|
yuuji@0
|
1453 /* search message for LF LF */
|
yuuji@0
|
1454 for (siz = 0; siz < msiz; siz++) {
|
yuuji@0
|
1455 if (--i <= 0) /* read another buffer as necessary */
|
yuuji@0
|
1456 read (LOCAL->fd,s = LOCAL->buf,i = min (msiz-siz,(long) MAILTMPLEN));
|
yuuji@0
|
1457 /* two newline sequence? */
|
yuuji@0
|
1458 if ((c == '\012') && (*s == '\012')) {
|
yuuji@0
|
1459 /* yes, note for later */
|
yuuji@0
|
1460 elt->private.msg.header.text.size = (*size = siz + 1);
|
yuuji@0
|
1461
|
yuuji@0
|
1462 return ret; /* return to caller */
|
yuuji@0
|
1463 }
|
yuuji@0
|
1464 else c = *s++; /* next character */
|
yuuji@0
|
1465 }
|
yuuji@0
|
1466 /* header consumes entire message */
|
yuuji@0
|
1467 elt->private.msg.header.text.size = *size = msiz;
|
yuuji@0
|
1468 }
|
yuuji@0
|
1469 return ret;
|
yuuji@0
|
1470 }
|