annotate src/osdep/unix/unix.c @ 3:2366b362676d draft

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

yatex.org