annotate src/osdep/unix/mix.c @ 0:ada5e610ab86

imap-2007e
author yuuji@gentei.org
date Mon, 14 Sep 2009 15:17:45 +0900
parents
children
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: MIX mail routines
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
16 *
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
17 * Author(s): 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: 1 March 2006
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
24 * Last Edited: 7 May 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 #include <stdio.h>
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
29 #include <ctype.h>
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
30 #include <errno.h>
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
31 extern int errno; /* just in case */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
32 #include "mail.h"
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
33 #include "osdep.h"
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
34 #include <pwd.h>
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
35 #include <sys/stat.h>
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
36 #include <sys/time.h>
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
37 #include "misc.h"
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
38 #include "dummy.h"
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
39 #include "fdstring.h"
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
40
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
41 /* MIX definitions */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
42
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
43 #define MEGABYTE (1024*1024)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
44
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
45 #define MIXDATAROLL MEGABYTE /* size at which we roll to a new file */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
46
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
47
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
48 /* MIX files */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
49
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
50 #define MIXNAME ".mix" /* prefix for all MIX file names */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
51 #define MIXMETA "meta" /* suffix for metadata */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
52 #define MIXINDEX "index" /* suffix for index */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
53 #define MIXSTATUS "status" /* suffix for status */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
54 #define MIXSORTCACHE "sortcache"/* suffix for sortcache */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
55 #define METAMAX (MEGABYTE-1) /* maximum metadata file size (sanity check) */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
56
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
57
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
58 /* MIX file formats */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
59
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
60 /* sequence format (all but msg files) */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
61 #define SEQFMT "S%08lx\015\012"
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
62 /* metadata file format */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
63 #define MTAFMT "V%08lx\015\012L%08lx\015\012N%08lx\015\012"
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
64 /* index file record format */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
65 #define IXRFMT ":%08lx:%04d%02d%02d%02d%02d%02d%c%02d%02d:%08lx:%08lx:%08lx:%08lx:%08lx:\015\012"
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
66 /* status file record format */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
67 #define STRFMT ":%08lx:%08lx:%04x:%08lx:\015\012"
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
68 /* message file header format */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
69 #define MSRFMT "%s%08lx:%04d%02d%02d%02d%02d%02d%c%02d%02d:%08lx:\015\012"
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
70 #define MSGTOK ":msg:"
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
71 #define MSGTSZ (sizeof(MSGTOK)-1)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
72 /* sortcache file record format */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
73 #define SCRFMT ":%08lx:%08lx:%08lx:%08lx:%08lx:%c%08lx:%08lx:%08lx:\015\012"
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
74
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
75 /* MIX I/O stream local data */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
76
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
77 typedef struct mix_local {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
78 unsigned long curmsg; /* current message file number */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
79 unsigned long newmsg; /* current new message file number */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
80 time_t lastsnarf; /* last snarf time */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
81 int msgfd; /* file description of current msg file */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
82 int mfd; /* file descriptor of open metadata */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
83 unsigned long metaseq; /* metadata sequence */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
84 char *index; /* mailbox index name */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
85 unsigned long indexseq; /* index sequence */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
86 char *status; /* mailbox status name */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
87 unsigned long statusseq; /* status sequence */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
88 char *sortcache; /* mailbox sortcache name */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
89 unsigned long sortcacheseq; /* sortcache sequence */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
90 unsigned char *buf; /* temporary buffer */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
91 unsigned long buflen; /* current size of temporary buffer */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
92 unsigned int expok : 1; /* non-zero if expunge reports OK */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
93 unsigned int internal : 1; /* internally opened, do not validate */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
94 } MIXLOCAL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
95
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
96
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
97 #define MIXBURP struct mix_burp
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
98
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
99 MIXBURP {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
100 unsigned long fileno; /* message file number */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
101 char *name; /* message file name */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
102 SEARCHSET *tail; /* tail of ranges */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
103 SEARCHSET set; /* set of retained ranges */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
104 MIXBURP *next; /* next file to burp */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
105 };
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
106
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
107
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
108 /* Convenient access to local data */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
109
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
110 #define LOCAL ((MIXLOCAL *) stream->local)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
111
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
112 /* Function prototypes */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
113
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
114 DRIVER *mix_valid (char *name);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
115 long mix_isvalid (char *name,char *meta);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
116 void *mix_parameters (long function,void *value);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
117 long mix_dirfmttest (char *name);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
118 void mix_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
119 long mix_scan_contents (char *name,char *contents,unsigned long csiz,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
120 unsigned long fsiz);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
121 void mix_list (MAILSTREAM *stream,char *ref,char *pat);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
122 void mix_lsub (MAILSTREAM *stream,char *ref,char *pat);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
123 long mix_subscribe (MAILSTREAM *stream,char *mailbox);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
124 long mix_unsubscribe (MAILSTREAM *stream,char *mailbox);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
125 long mix_create (MAILSTREAM *stream,char *mailbox);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
126 long mix_delete (MAILSTREAM *stream,char *mailbox);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
127 long mix_rename (MAILSTREAM *stream,char *old,char *newname);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
128 int mix_rselect (struct direct *name);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
129 MAILSTREAM *mix_open (MAILSTREAM *stream);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
130 void mix_close (MAILSTREAM *stream,long options);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
131 void mix_abort (MAILSTREAM *stream);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
132 char *mix_header (MAILSTREAM *stream,unsigned long msgno,unsigned long *length,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
133 long flags);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
134 long mix_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
135 void mix_flag (MAILSTREAM *stream,char *sequence,char *flag,long flags);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
136 unsigned long *mix_sort (MAILSTREAM *stream,char *charset,SEARCHPGM *spg,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
137 SORTPGM *pgm,long flags);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
138 THREADNODE *mix_thread (MAILSTREAM *stream,char *type,char *charset,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
139 SEARCHPGM *spg,long flags);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
140 long mix_ping (MAILSTREAM *stream);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
141 void mix_check (MAILSTREAM *stream);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
142 long mix_expunge (MAILSTREAM *stream,char *sequence,long options);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
143 int mix_select (struct direct *name);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
144 int mix_msgfsort (const void *d1,const void *d2);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
145 long mix_addset (SEARCHSET **set,unsigned long start,unsigned long size);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
146 long mix_burp (MAILSTREAM *stream,MIXBURP *burp,unsigned long *reclaimed);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
147 long mix_burp_check (SEARCHSET *set,size_t size,char *file);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
148 long mix_copy (MAILSTREAM *stream,char *sequence,char *mailbox,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
149 long options);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
150 long mix_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
151 long mix_append_msg (MAILSTREAM *stream,FILE *f,char *flags,MESSAGECACHE *delt,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
152 STRING *msg,SEARCHSET *set,unsigned long seq);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
153
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
154 FILE *mix_parse (MAILSTREAM *stream,FILE **idxf,long iflags,long sflags);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
155 char *mix_meta_slurp (MAILSTREAM *stream,unsigned long *seq);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
156 long mix_meta_update (MAILSTREAM *stream);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
157 long mix_index_update (MAILSTREAM *stream,FILE *idxf,long flag);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
158 long mix_status_update (MAILSTREAM *stream,FILE *statf,long flag);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
159 FILE *mix_data_open (MAILSTREAM *stream,int *fd,long *size,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
160 unsigned long newsize);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
161 FILE *mix_sortcache_open (MAILSTREAM *stream);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
162 long mix_sortcache_update (MAILSTREAM *stream,FILE **sortcache);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
163 char *mix_read_record (FILE *f,char *buf,unsigned long buflen,char *type);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
164 unsigned long mix_read_sequence (FILE *f);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
165 char *mix_dir (char *dst,char *name);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
166 char *mix_file (char *dst,char *dir,char *name);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
167 char *mix_file_data (char *dst,char *dir,unsigned long data);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
168 unsigned long mix_modseq (unsigned long oldseq);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
169
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
170 /* MIX mail routines */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
171
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
172
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
173 /* Driver dispatch used by MAIL */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
174
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
175 DRIVER mixdriver = {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
176 "mix", /* driver name */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
177 /* driver flags */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
178 DR_MAIL|DR_LOCAL|DR_NOFAST|DR_CRLF|DR_LOCKING|DR_DIRFMT|DR_MODSEQ,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
179 (DRIVER *) NIL, /* next driver */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
180 mix_valid, /* mailbox is valid for us */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
181 mix_parameters, /* manipulate parameters */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
182 mix_scan, /* scan mailboxes */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
183 mix_list, /* find mailboxes */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
184 mix_lsub, /* find subscribed mailboxes */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
185 mix_subscribe, /* subscribe to mailbox */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
186 mix_unsubscribe, /* unsubscribe from mailbox */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
187 mix_create, /* create mailbox */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
188 mix_delete, /* delete mailbox */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
189 mix_rename, /* rename mailbox */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
190 mail_status_default, /* status of mailbox */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
191 mix_open, /* open mailbox */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
192 mix_close, /* close mailbox */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
193 NIL, /* fetch message "fast" attributes */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
194 NIL, /* fetch message flags */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
195 NIL, /* fetch overview */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
196 NIL, /* fetch message envelopes */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
197 mix_header, /* fetch message header only */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
198 mix_text, /* fetch message body only */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
199 NIL, /* fetch partial message test */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
200 NIL, /* unique identifier */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
201 NIL, /* message number */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
202 mix_flag, /* modify flags */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
203 NIL, /* per-message modify flags */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
204 NIL, /* search for message based on criteria */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
205 mix_sort, /* sort messages */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
206 mix_thread, /* thread messages */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
207 mix_ping, /* ping mailbox to see if still alive */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
208 mix_check, /* check for new messages */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
209 mix_expunge, /* expunge deleted messages */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
210 mix_copy, /* copy messages to another mailbox */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
211 mix_append, /* append string message to mailbox */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
212 NIL /* garbage collect stream */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
213 };
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
214
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
215 /* prototype stream */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
216 MAILSTREAM mixproto = {&mixdriver};
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
217
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
218 /* MIX mail validate mailbox
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
219 * Accepts: mailbox name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
220 * Returns: our driver if name is valid, NIL otherwise
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
221 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
222
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
223 DRIVER *mix_valid (char *name)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
224 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
225 char tmp[MAILTMPLEN];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
226 return mix_isvalid (name,tmp) ? &mixdriver : NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
227 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
228
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
229
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
230 /* MIX mail test for valid mailbox
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
231 * Accepts: mailbox name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
232 * buffer to return meta name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
233 * Returns: T if valid, NIL otherwise, metadata name written in both cases
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
234 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
235
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
236 long mix_isvalid (char *name,char *meta)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
237 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
238 char dir[MAILTMPLEN];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
239 struct stat sbuf;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
240 /* validate name as directory */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
241 if (!(errno = ((strlen (name) > NETMAXMBX) ? ENAMETOOLONG : NIL)) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
242 *mix_dir (dir,name) && mix_file (meta,dir,MIXMETA) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
243 !stat (dir,&sbuf) && ((sbuf.st_mode & S_IFMT) == S_IFDIR)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
244 /* name is directory; is it mix? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
245 if (!stat (meta,&sbuf) && ((sbuf.st_mode & S_IFMT) == S_IFREG))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
246 return LONGT;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
247 else errno = NIL; /* directory but not mix */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
248 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
249 return NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
250 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
251
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
252 /* MIX manipulate driver parameters
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
253 * Accepts: function code
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
254 * function-dependent value
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
255 * Returns: function-dependent return value
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 void *mix_parameters (long function,void *value)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
259 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
260 void *ret = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
261 switch ((int) function) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
262 case GET_INBOXPATH:
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
263 if (value) ret = mailboxfile ((char *) value,"~/INBOX");
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
264 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
265 case GET_DIRFMTTEST:
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
266 ret = (void *) mix_dirfmttest;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
267 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
268 case GET_SCANCONTENTS:
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
269 ret = (void *) mix_scan_contents;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
270 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
271 case SET_ONETIMEEXPUNGEATPING:
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
272 if (value) ((MIXLOCAL *) ((MAILSTREAM *) value)->local)->expok = T;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
273 case GET_ONETIMEEXPUNGEATPING:
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
274 if (value) ret = (void *)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
275 (((MIXLOCAL *) ((MAILSTREAM *) value)->local)->expok ? VOIDT : NIL);
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
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
282 /* MIX test for directory format internal node
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
283 * Accepts: candidate node name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
284 * Returns: T if internal name, NIL otherwise
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
285 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
286
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
287 long mix_dirfmttest (char *name)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
288 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
289 /* belongs to MIX if starts with .mix */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
290 return strncmp (name,MIXNAME,sizeof (MIXNAME) - 1) ? NIL : LONGT;
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 /* MIX mail scan mailboxes
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
294 * Accepts: mail stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
295 * reference
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
296 * pattern to search
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
297 * string to scan
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 mix_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
301 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
302 if (stream) dummy_scan (NIL,ref,pat,contents);
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 /* MIX scan mailbox for contents
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
307 * Accepts: mailbox name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
308 * desired contents
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
309 * contents size
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
310 * file size (ignored)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
311 * Returns: NIL if contents not found, T if found
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
312 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
313
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
314 long mix_scan_contents (char *name,char *contents,unsigned long csiz,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
315 unsigned long fsiz)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
316 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
317 long i,nfiles;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
318 void *a;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
319 char *s;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
320 long ret = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
321 size_t namelen = strlen (name);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
322 struct stat sbuf;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
323 struct direct **names = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
324 if ((nfiles = scandir (name,&names,mix_select,mix_msgfsort)) > 0)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
325 for (i = 0; i < nfiles; ++i) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
326 if (!ret) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
327 sprintf (s = (char *) fs_get (namelen + strlen (names[i]->d_name) + 2),
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
328 "%s/%s",name,names[i]->d_name);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
329 if (!stat (s,&sbuf) && (csiz <= sbuf.st_size))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
330 ret = dummy_scan_contents (s,contents,csiz,sbuf.st_size);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
331 fs_give ((void **) &s);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
332 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
333 fs_give ((void **) &names[i]);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
334 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
335 /* free directory list */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
336 if (a = (void *) names) fs_give ((void **) &a);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
337 return ret;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
338 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
339
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
340 /* MIX list mailboxes
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
341 * Accepts: mail stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
342 * reference
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
343 * pattern to search
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
344 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
345
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
346 void mix_list (MAILSTREAM *stream,char *ref,char *pat)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
347 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
348 if (stream) dummy_list (NIL,ref,pat);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
349 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
350
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
351
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
352 /* MIX list subscribed mailboxes
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
353 * Accepts: mail stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
354 * reference
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
355 * pattern to search
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
356 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
357
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
358 void mix_lsub (MAILSTREAM *stream,char *ref,char *pat)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
359 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
360 if (stream) dummy_lsub (NIL,ref,pat);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
361 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
362
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
363 /* MIX mail subscribe to mailbox
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
364 * Accepts: mail stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
365 * mailbox to add to subscription list
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
366 * Returns: T on success, NIL on failure
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
367 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
368
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
369 long mix_subscribe (MAILSTREAM *stream,char *mailbox)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
370 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
371 return sm_subscribe (mailbox);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
372 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
373
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
374
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
375 /* MIX mail unsubscribe to mailbox
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
376 * Accepts: mail stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
377 * mailbox to delete from subscription list
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
378 * Returns: T on success, NIL on failure
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 long mix_unsubscribe (MAILSTREAM *stream,char *mailbox)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
382 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
383 return sm_unsubscribe (mailbox);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
384 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
385
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
386 /* MIX mail create mailbox
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
387 * Accepts: mail stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
388 * mailbox name to create
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
389 * Returns: T on success, NIL on failure
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
390 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
391
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
392 long mix_create (MAILSTREAM *stream,char *mailbox)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
393 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
394 DRIVER *test;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
395 FILE *f;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
396 int c,i;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
397 char *t,tmp[MAILTMPLEN],file[MAILTMPLEN];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
398 char *s = strrchr (mailbox,'/');
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
399 unsigned long now = time (NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
400 long ret = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
401 /* always create \NoSelect if trailing / */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
402 if (s && !s[1]) return dummy_create (stream,mailbox);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
403 /* validate name */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
404 if (mix_dirfmttest (s ? s + 1 : mailbox))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
405 sprintf(tmp,"Can't create mailbox %.80s: invalid MIX-format name",mailbox);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
406 /* must not already exist */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
407 else if ((test = mail_valid (NIL,mailbox,NIL)) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
408 strcmp (test->name,"dummy"))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
409 sprintf (tmp,"Can't create mailbox %.80s: mailbox already exists",mailbox);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
410 /* create directory and metadata */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
411 else if (!dummy_create_path (stream,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
412 mix_file (file,mix_dir (tmp,mailbox),MIXMETA),
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
413 get_dir_protection (mailbox)))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
414 sprintf (tmp,"Can't create mailbox %.80s: %.80s",mailbox,strerror (errno));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
415 else if (!(f = fopen (file,"w")))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
416 sprintf (tmp,"Can't re-open metadata %.80s: %.80s",mailbox,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
417 strerror (errno));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
418 else { /* success, write initial metadata */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
419 fprintf (f,SEQFMT,now);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
420 fprintf (f,MTAFMT,now,0,now);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
421 for (i = 0, c = 'K'; (i < NUSERFLAGS) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
422 (t = (stream && stream->user_flags[i]) ? stream->user_flags[i] :
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
423 default_user_flag (i)) && *t; ++i) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
424 putc (c,f); /* write another keyword */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
425 fputs (t,f);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
426 c = ' '; /* delimiter is now space */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
427 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
428 fclose (f);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
429 set_mbx_protections (mailbox,file);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
430 /* point to suffix */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
431 s = file + strlen (file) - (sizeof (MIXMETA) - 1);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
432 strcpy (s,MIXINDEX); /* create index */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
433 if (!dummy_create_path (stream,file,get_dir_protection (mailbox)))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
434 sprintf (tmp,"Can't create mix mailbox index: %.80s",strerror (errno));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
435 else {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
436 set_mbx_protections (mailbox,file);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
437 strcpy (s,MIXSTATUS); /* create status */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
438 if (!dummy_create_path (stream,file,get_dir_protection (mailbox)))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
439 sprintf (tmp,"Can't create mix mailbox status: %.80s",
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
440 strerror (errno));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
441 else {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
442 set_mbx_protections (mailbox,file);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
443 sprintf (s,"%08lx",now);/* message file */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
444 if (!dummy_create_path (stream,file,get_dir_protection (mailbox)))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
445 sprintf (tmp,"Can't create mix mailbox data: %.80s",
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
446 strerror (errno));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
447 else {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
448 set_mbx_protections (mailbox,file);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
449 ret = LONGT; /* declare success at this point */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
450 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
451 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
452 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
453 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
454 if (!ret) MM_LOG (tmp,ERROR); /* some error */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
455 return ret;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
456 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
457
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
458 /* MIX mail delete mailbox
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
459 * mailbox name to delete
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
460 * Returns: T on success, NIL on failure
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
461 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
462
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
463 long mix_delete (MAILSTREAM *stream,char *mailbox)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
464 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
465 DIR *dirp;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
466 struct direct *d;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
467 int fd = -1;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
468 char *s,tmp[MAILTMPLEN];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
469 if (!mix_isvalid (mailbox,tmp))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
470 sprintf (tmp,"Can't delete mailbox %.80s: no such mailbox",mailbox);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
471 else if (((fd = open (tmp,O_RDWR,NIL)) < 0) || flock (fd,LOCK_EX|LOCK_NB))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
472 sprintf (tmp,"Can't lock mailbox for delete: %.80s",mailbox);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
473 /* delete metadata */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
474 else if (unlink (tmp)) sprintf (tmp,"Can't delete mailbox %.80s index: %80s",
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
475 mailbox,strerror (errno));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
476 else {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
477 close (fd); /* close descriptor on deleted metadata */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
478 /* get directory name */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
479 *(s = strrchr (tmp,'/')) = '\0';
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
480 if (dirp = opendir (tmp)) { /* open directory */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
481 *s++ = '/'; /* restore delimiter */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
482 /* massacre messages */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
483 while (d = readdir (dirp)) if (mix_dirfmttest (d->d_name)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
484 strcpy (s,d->d_name); /* make path */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
485 unlink (tmp); /* sayonara */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
486 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
487 closedir (dirp); /* flush directory */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
488 *(s = strrchr (tmp,'/')) = '\0';
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
489 if (rmdir (tmp)) { /* try to remove the directory */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
490 sprintf (tmp,"Can't delete name %.80s: %.80s",
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
491 mailbox,strerror (errno));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
492 MM_LOG (tmp,WARN);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
493 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
494 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
495 return T; /* always success */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
496 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
497 if (fd >= 0) close (fd); /* close any descriptor on metadata */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
498 MM_LOG (tmp,ERROR); /* something failed */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
499 return NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
500 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
501
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
502 /* MIX mail rename mailbox
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
503 * Accepts: MIX mail stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
504 * old mailbox name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
505 * new mailbox name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
506 * Returns: T on success, NIL on failure
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 long mix_rename (MAILSTREAM *stream,char *old,char *newname)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
510 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
511 char c,*s,tmp[MAILTMPLEN],tmp1[MAILTMPLEN];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
512 struct stat sbuf;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
513 int fd = -1;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
514 if (!mix_isvalid (old,tmp))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
515 sprintf (tmp,"Can't rename mailbox %.80s: no such mailbox",old);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
516 else if (((fd = open (tmp,O_RDWR,NIL)) < 0) || flock (fd,LOCK_EX|LOCK_NB))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
517 sprintf (tmp,"Can't lock mailbox for rename: %.80s",old);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
518 else if (mix_dirfmttest ((s = strrchr (newname,'/')) ? s + 1 : newname))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
519 sprintf (tmp,"Can't rename to mailbox %.80s: invalid MIX-format name",
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
520 newname);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
521 /* new mailbox name must not be valid */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
522 else if (mix_isvalid (newname,tmp))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
523 sprintf (tmp,"Can't rename to mailbox %.80s: destination already exists",
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
524 newname);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
525 else {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
526 mix_dir (tmp,old); /* build old directory name */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
527 mix_dir (tmp1,newname); /* and new directory name */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
528 /* easy if not INBOX */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
529 if (compare_cstring (old,"INBOX")) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
530 /* found superior to destination name? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
531 if (s = strrchr (tmp1,'/')) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
532 c = *++s; /* remember first character of inferior */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
533 *s = '\0'; /* tie off to get just superior */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
534 /* name doesn't exist, create it */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
535 if ((stat (tmp1,&sbuf) || ((sbuf.st_mode & S_IFMT) != S_IFDIR)) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
536 !dummy_create_path (stream,tmp1,get_dir_protection (newname)))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
537 return NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
538 *s = c; /* restore full name */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
539 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
540 if (!rename (tmp,tmp1)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
541 close (fd); /* close descriptor on metadata */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
542 return LONGT;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
543 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
544 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
545
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
546 /* RFC 3501 requires this */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
547 else if (dummy_create_path (stream,strcat (tmp1,"/"),
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
548 get_dir_protection (newname))) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
549 void *a;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
550 int i,n,lasterror;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
551 char *src,*dst;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
552 struct direct **names = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
553 size_t srcl = strlen (tmp);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
554 size_t dstl = strlen (tmp1);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
555 /* rename each mix file to new directory */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
556 for (i = lasterror = 0,n = scandir (tmp,&names,mix_rselect,alphasort);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
557 i < n; ++i) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
558 size_t len = strlen (names[i]->d_name);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
559 sprintf (src = (char *) fs_get (srcl + len + 2),"%s/%s",
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
560 tmp,names[i]->d_name);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
561 sprintf (dst = (char *) fs_get (dstl + len + 1),"%s%s",
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
562 tmp1,names[i]->d_name);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
563 if (rename (src,dst)) lasterror = errno;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
564 fs_give ((void **) &src);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
565 fs_give ((void **) &dst);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
566 fs_give ((void **) &names[i]);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
567 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
568 /* free directory list */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
569 if (a = (void *) names) fs_give ((void **) &a);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
570 if (lasterror) errno = lasterror;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
571 else {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
572 close (fd); /* close descriptor on metadata */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
573 return mix_create (NIL,"INBOX");
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
574 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
575 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
576 sprintf (tmp,"Can't rename mailbox %.80s to %.80s: %.80s",
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
577 old,newname,strerror (errno));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
578 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
579 if (fd >= 0) close (fd); /* close any descriptor on metadata */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
580 MM_LOG (tmp,ERROR); /* something failed */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
581 return NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
582 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
583
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
584
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
585 /* MIX test for mix name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
586 * Accepts: candidate directory name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
587 * Returns: T if mix file name, NIL otherwise
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
588 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
589
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
590 int mix_rselect (struct direct *name)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
591 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
592 return mix_dirfmttest (name->d_name);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
593 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
594
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
595 /* MIX mail open
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
596 * Accepts: stream to open
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
597 * Returns: stream on success, NIL on failure
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
598 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
599
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
600 MAILSTREAM *mix_open (MAILSTREAM *stream)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
601 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
602 short silent;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
603 /* return prototype for OP_PROTOTYPE call */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
604 if (!stream) return user_flags (&mixproto);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
605 if (stream->local) fatal ("mix recycle stream");
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
606 stream->local = memset (fs_get (sizeof (MIXLOCAL)),0,sizeof (MIXLOCAL));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
607 /* note if an INBOX or not */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
608 stream->inbox = !compare_cstring (stream->mailbox,"INBOX");
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
609 /* make temporary buffer */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
610 LOCAL->buf = (char *) fs_get (CHUNKSIZE);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
611 LOCAL->buflen = CHUNKSIZE - 1;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
612 /* set stream->mailbox to be directory name */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
613 mix_dir (LOCAL->buf,stream->mailbox);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
614 fs_give ((void **) &stream->mailbox);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
615 stream->mailbox = cpystr (LOCAL->buf);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
616 LOCAL->msgfd = -1; /* currently no file open */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
617 if (!(((!stream->rdonly && /* open metadata file */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
618 ((LOCAL->mfd = open (mix_file (LOCAL->buf,stream->mailbox,MIXMETA),
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
619 O_RDWR,NIL)) >= 0)) ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
620 ((stream->rdonly = T) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
621 ((LOCAL->mfd = open (mix_file (LOCAL->buf,stream->mailbox,MIXMETA),
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
622 O_RDONLY,NIL)) >= 0))) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
623 !flock (LOCAL->mfd,LOCK_SH))) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
624 MM_LOG ("Error opening mix metadata file",ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
625 mix_abort (stream);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
626 stream = NIL; /* open fails */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
627 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
628 else { /* metadata open, complete open */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
629 LOCAL->index = cpystr (mix_file (LOCAL->buf,stream->mailbox,MIXINDEX));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
630 LOCAL->status = cpystr (mix_file (LOCAL->buf,stream->mailbox,MIXSTATUS));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
631 LOCAL->sortcache = cpystr (mix_file (LOCAL->buf,stream->mailbox,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
632 MIXSORTCACHE));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
633 stream->sequence++; /* bump sequence number */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
634 /* parse mailbox */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
635 stream->nmsgs = stream->recent = 0;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
636 if (silent = stream->silent) LOCAL->internal = T;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
637 stream->silent = T;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
638 if (mix_ping (stream)) { /* do initial ping */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
639 /* try burping in case we are exclusive */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
640 if (!stream->rdonly) mix_expunge (stream,"",NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
641 if (!(stream->nmsgs || stream->silent))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
642 MM_LOG ("Mailbox is empty",(long) NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
643 stream->silent = silent; /* now notify upper level */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
644 mail_exists (stream,stream->nmsgs);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
645 stream->perm_seen = stream->perm_deleted = stream->perm_flagged =
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
646 stream->perm_answered = stream->perm_draft = stream->rdonly ? NIL : T;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
647 stream->perm_user_flags = stream->rdonly ? NIL : 0xffffffff;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
648 stream->kwd_create = /* can we create new user flags? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
649 (stream->user_flags[NUSERFLAGS-1] || stream->rdonly) ? NIL : T;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
650 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
651 else { /* got murdelyzed in ping */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
652 mix_abort (stream);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
653 stream = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
654 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
655 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
656 return stream; /* return stream to caller */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
657 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
658
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
659 /* MIX mail close
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
660 * Accepts: MAIL stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
661 * close options
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
662 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
663
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
664 void mix_close (MAILSTREAM *stream,long options)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
665 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
666 if (LOCAL) { /* only if a file is open */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
667 int silent = stream->silent;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
668 stream->silent = T; /* note this stream is dying */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
669 /* burp-only or expunge */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
670 mix_expunge (stream,(options & CL_EXPUNGE) ? NIL : "",NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
671 mix_abort (stream);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
672 stream->silent = silent; /* reset silent state */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
673 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
674 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
675
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
676
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
677 /* MIX mail abort stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
678 * Accepts: MAIL stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
679 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
680
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
681 void mix_abort (MAILSTREAM *stream)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
682 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
683 if (LOCAL) { /* only if a file is open */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
684 /* close current message file if open */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
685 if (LOCAL->msgfd >= 0) close (LOCAL->msgfd);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
686 /* close current metadata file if open */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
687 if (LOCAL->mfd >= 0) close (LOCAL->mfd);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
688 if (LOCAL->index) fs_give ((void **) &LOCAL->index);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
689 if (LOCAL->status) fs_give ((void **) &LOCAL->status);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
690 if (LOCAL->sortcache) fs_give ((void **) &LOCAL->sortcache);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
691 /* free local scratch buffer */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
692 if (LOCAL->buf) fs_give ((void **) &LOCAL->buf);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
693 /* nuke the local data */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
694 fs_give ((void **) &stream->local);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
695 stream->dtb = NIL; /* log out the DTB */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
696 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
697 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
698
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
699 /* MIX mail fetch message header
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
700 * Accepts: MAIL stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
701 * message # to fetch
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
702 * pointer to returned header text length
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
703 * option flags
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
704 * Returns: message header in RFC822 format
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
705 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
706
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
707 char *mix_header (MAILSTREAM *stream,unsigned long msgno,unsigned long *length,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
708 long flags)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
709 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
710 unsigned long i,j,k;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
711 int fd;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
712 char *s,tmp[MAILTMPLEN];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
713 MESSAGECACHE *elt;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
714 if (length) *length = 0; /* default return */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
715 if (flags & FT_UID) return "";/* UID call "impossible" */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
716 elt = mail_elt (stream,msgno);/* get elt */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
717 /* is message in current message file? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
718 if ((LOCAL->msgfd < 0) || (elt->private.spare.data != LOCAL->curmsg)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
719 if (LOCAL->msgfd >= 0) close (LOCAL->msgfd);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
720 if ((LOCAL->msgfd = open (mix_file_data (LOCAL->buf,stream->mailbox,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
721 elt->private.spare.data),
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
722 O_RDONLY,NIL)) < 0) return "";
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
723 /* got file */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
724 LOCAL->curmsg = elt->private.spare.data;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
725 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
726 lseek (LOCAL->msgfd,elt->private.special.offset,L_SET);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
727 /* size of special data and header */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
728 j = elt->private.msg.header.offset + elt->private.msg.header.text.size;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
729 if (j > LOCAL->buflen) { /* is buffer big enough? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
730 /* no, make one that is */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
731 fs_give ((void **) &LOCAL->buf);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
732 LOCAL->buf = (char *) fs_get ((LOCAL->buflen = j) + 1);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
733 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
734 /* Maybe someday validate internaldate too */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
735 /* slurp special data + header, validate */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
736 if ((read (LOCAL->msgfd,LOCAL->buf,j) == j) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
737 !strncmp (LOCAL->buf,MSGTOK,MSGTSZ) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
738 (elt->private.uid == strtoul ((char *) LOCAL->buf + MSGTSZ,&s,16)) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
739 (*s++ == ':') && (s = strchr (s,':')) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
740 (k = strtoul (s+1,&s,16)) && (*s++ == ':') &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
741 (s < (char *) (LOCAL->buf + elt->private.msg.header.offset))) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
742 /* won, set offset and size of message */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
743 i = elt->private.msg.header.offset;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
744 *length = elt->private.msg.header.text.size;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
745 if (k != elt->rfc822_size) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
746 sprintf (tmp,"Inconsistency in mix message size, uid=%lx (%lu != %lu)",
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
747 elt->private.uid,elt->rfc822_size,k);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
748 MM_LOG (tmp,WARN);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
749 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
750 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
751 else { /* document the problem */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
752 LOCAL->buf[100] = '\0'; /* tie off buffer at no more than 100 octets */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
753 /* or at newline, whichever is first */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
754 if (s = strpbrk (LOCAL->buf,"\015\012")) *s = '\0';
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
755 sprintf (tmp,"Error reading mix message header, uid=%lx, s=%.0lx, h=%s",
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
756 elt->private.uid,elt->rfc822_size,LOCAL->buf);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
757 MM_LOG (tmp,ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
758 *length = i = j = 0; /* default to empty */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
759 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
760 LOCAL->buf[j] = '\0'; /* tie off buffer at the end */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
761 return (char *) LOCAL->buf + i;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
762 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
763
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
764 /* MIX mail fetch message text (body only)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
765 * Accepts: MAIL stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
766 * message # to fetch
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
767 * pointer to returned stringstruct
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
768 * option flags
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
769 * Returns: T on success, NIL on failure
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
770 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
771
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
772 long mix_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
773 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
774 unsigned long i;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
775 FDDATA d;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
776 MESSAGECACHE *elt;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
777 /* UID call "impossible" */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
778 if (flags & FT_UID) return NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
779 elt = mail_elt (stream,msgno);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
780 /* is message in current message file? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
781 if ((LOCAL->msgfd < 0) || (elt->private.spare.data != LOCAL->curmsg)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
782 if (LOCAL->msgfd >= 0) close (LOCAL->msgfd);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
783 if ((LOCAL->msgfd = open (mix_file_data (LOCAL->buf,stream->mailbox,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
784 elt->private.spare.data),
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
785 O_RDONLY,NIL)) < 0) return NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
786 /* got file */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
787 LOCAL->curmsg = elt->private.spare.data;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
788 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
789 /* doing non-peek fetch? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
790 if (!(flags & FT_PEEK) && !elt->seen) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
791 FILE *idxf; /* yes, process metadata/index/status */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
792 FILE *statf = mix_parse (stream,&idxf,NIL,LONGT);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
793 elt->seen = T; /* mark as seen */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
794 MM_FLAGS (stream,elt->msgno);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
795 /* update status file if possible */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
796 if (statf && !stream->rdonly) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
797 elt->private.mod = LOCAL->statusseq = mix_modseq (LOCAL->statusseq);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
798 mix_status_update (stream,statf,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
799 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
800 if (idxf) fclose (idxf); /* release index and status file */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
801 if (statf) fclose (statf);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
802 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
803 d.fd = LOCAL->msgfd; /* set up file descriptor */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
804 /* offset of message text */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
805 d.pos = elt->private.special.offset + elt->private.msg.header.offset +
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
806 elt->private.msg.header.text.size;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
807 d.chunk = LOCAL->buf; /* initial buffer chunk */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
808 d.chunksize = CHUNKSIZE; /* chunk size */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
809 INIT (bs,fd_string,&d,elt->rfc822_size - elt->private.msg.header.text.size);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
810 return T;
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 /* MIX mail modify flags
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
814 * Accepts: MAIL stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
815 * sequence
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
816 * flag(s)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
817 * option flags
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
818 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
819
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
820 void mix_flag (MAILSTREAM *stream,char *sequence,char *flag,long flags)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
821 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
822 MESSAGECACHE *elt;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
823 unsigned long i,uf,ffkey;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
824 long f;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
825 short nf;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
826 FILE *idxf;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
827 FILE *statf = mix_parse (stream,&idxf,NIL,LONGT);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
828 unsigned long seq = mix_modseq (LOCAL->statusseq);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
829 /* find first free key */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
830 for (ffkey = 0; (ffkey < NUSERFLAGS) && stream->user_flags[ffkey]; ++ffkey);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
831 /* parse sequence and flags */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
832 if (((flags & ST_UID) ? mail_uid_sequence (stream,sequence) :
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
833 mail_sequence (stream,sequence)) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
834 ((f = mail_parse_flags (stream,flag,&uf)) || uf)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
835 /* alter flags */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
836 for (i = 1,nf = (flags & ST_SET) ? T : NIL; i <= stream->nmsgs; i++)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
837 if ((elt = mail_elt (stream,i))->sequence) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
838 struct { /* old flags */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
839 unsigned int seen : 1;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
840 unsigned int deleted : 1;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
841 unsigned int flagged : 1;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
842 unsigned int answered : 1;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
843 unsigned int draft : 1;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
844 unsigned long user_flags;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
845 } old;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
846 old.seen = elt->seen; old.deleted = elt->deleted;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
847 old.flagged = elt->flagged; old.answered = elt->answered;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
848 old.draft = elt->draft; old.user_flags = elt->user_flags;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
849 if (f&fSEEN) elt->seen = nf;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
850 if (f&fDELETED) elt->deleted = nf;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
851 if (f&fFLAGGED) elt->flagged = nf;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
852 if (f&fANSWERED) elt->answered = nf;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
853 if (f&fDRAFT) elt->draft = nf;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
854 /* user flags */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
855 if (flags & ST_SET) elt->user_flags |= uf;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
856 else elt->user_flags &= ~uf;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
857 if ((old.seen != elt->seen) || (old.deleted != elt->deleted) ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
858 (old.flagged != elt->flagged) ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
859 (old.answered != elt->answered) || (old.draft != elt->draft) ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
860 (old.user_flags != elt->user_flags)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
861 if (!stream->rdonly) elt->private.mod = LOCAL->statusseq = seq;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
862 MM_FLAGS (stream,elt->msgno);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
863 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
864 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
865 /* update status file after change */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
866 if (statf && (seq == LOCAL->statusseq))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
867 mix_status_update (stream,statf,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
868 /* update metadata if created a keyword */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
869 if ((ffkey < NUSERFLAGS) && stream->user_flags[ffkey] &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
870 !mix_meta_update (stream))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
871 MM_LOG ("Error updating mix metadata after keyword creation",ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
872 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
873 if (statf) fclose (statf); /* release status file if still open */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
874 if (idxf) fclose (idxf); /* release index file */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
875 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
876
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
877 /* MIX mail sort messages
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
878 * Accepts: mail stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
879 * character set
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
880 * search program
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
881 * sort program
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
882 * option flags
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
883 * Returns: vector of sorted message sequences or NIL if error
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
884 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
885
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
886 unsigned long *mix_sort (MAILSTREAM *stream,char *charset,SEARCHPGM *spg,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
887 SORTPGM *pgm,long flags)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
888 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
889 unsigned long *ret;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
890 FILE *sortcache = mix_sortcache_open (stream);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
891 ret = mail_sort_msgs (stream,charset,spg,pgm,flags);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
892 mix_sortcache_update (stream,&sortcache);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
893 return ret;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
894 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
895
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
896
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
897 /* MIX mail thread messages
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
898 * Accepts: mail stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
899 * thread type
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
900 * character set
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
901 * search program
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
902 * option flags
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
903 * Returns: thread node tree or NIL if error
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
904 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
905
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
906 THREADNODE *mix_thread (MAILSTREAM *stream,char *type,char *charset,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
907 SEARCHPGM *spg,long flags)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
908 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
909 THREADNODE *ret;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
910 FILE *sortcache = mix_sortcache_open (stream);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
911 ret = mail_thread_msgs (stream,type,charset,spg,flags,mail_sort_msgs);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
912 mix_sortcache_update (stream,&sortcache);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
913 return ret;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
914 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
915
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
916 /* MIX mail ping mailbox
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
917 * Accepts: MAIL stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
918 * Returns: T if stream alive, else NIL
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
919 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
920
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
921 static int snarfing = 0; /* lock against recursive snarfing */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
922
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
923 long mix_ping (MAILSTREAM *stream)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
924 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
925 FILE *idxf,*statf;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
926 struct stat sbuf;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
927 STRING msg;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
928 MESSAGECACHE *elt;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
929 int mfd,ifd,sfd;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
930 unsigned long i,msglen;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
931 char *message,date[MAILTMPLEN],flags[MAILTMPLEN];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
932 MAILSTREAM *sysibx = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
933 long ret = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
934 long snarfok = LONGT;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
935 /* time to snarf? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
936 if (stream->inbox && !stream->rdonly && !snarfing &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
937 (time (0) >= (LOCAL->lastsnarf +
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
938 (time_t) mail_parameters (NIL,GET_SNARFINTERVAL,NIL)))) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
939 appenduid_t au = (appenduid_t) mail_parameters (NIL,GET_APPENDUID,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
940 copyuid_t cu = (copyuid_t) mail_parameters (NIL,GET_COPYUID,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
941 MM_CRITICAL (stream); /* go critical */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
942 snarfing = T; /* don't recursively snarf */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
943 /* disable APPENDUID/COPYUID callbacks */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
944 mail_parameters (NIL,SET_APPENDUID,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
945 mail_parameters (NIL,SET_COPYUID,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
946 /* sizes match and anything in sysinbox? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
947 if (!stat (sysinbox (),&sbuf) && ((sbuf.st_mode & S_IFMT) == S_IFREG) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
948 sbuf.st_size && (sysibx = mail_open (sysibx,sysinbox (),OP_SILENT)) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
949 !sysibx->rdonly && sysibx->nmsgs) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
950 /* for each message in sysibx mailbox */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
951 for (i = 1; snarfok && (i <= sysibx->nmsgs); ++i)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
952 if (!(elt = mail_elt (sysibx,i))->deleted &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
953 (message = mail_fetch_message (sysibx,i,&msglen,FT_PEEK)) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
954 msglen) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
955 mail_date (date,elt); /* make internal date string */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
956 /* make flag string */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
957 flags[0] = flags[1] = '\0';
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
958 if (elt->seen) strcat (flags," \\Seen");
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
959 if (elt->flagged) strcat (flags," \\Flagged");
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
960 if (elt->answered) strcat (flags," \\Answered");
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
961 if (elt->draft) strcat (flags," \\Draft");
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
962 flags[0] = '(';
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
963 strcat (flags,")");
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
964 INIT (&msg,mail_string,message,msglen);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
965 if (snarfok = mail_append_full (stream,"INBOX",flags,date,&msg)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
966 char sequence[15];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
967 sprintf (sequence,"%lu",i);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
968 mail_flag (sysibx,sequence,"\\Deleted",ST_SET);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
969 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
970 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
971
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
972 /* now expunge all those messages */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
973 if (snarfok) mail_expunge (sysibx);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
974 else {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
975 sprintf (LOCAL->buf,"Can't copy new mail at message: %lu",i - 1);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
976 MM_LOG (LOCAL->buf,WARN);
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 if (sysibx) mail_close (sysibx);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
980 /* reenable APPENDUID/COPYUID */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
981 mail_parameters (NIL,SET_APPENDUID,(void *) au);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
982 mail_parameters (NIL,SET_COPYUID,(void *) cu);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
983 snarfing = NIL; /* no longer snarfing */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
984 MM_NOCRITICAL (stream); /* release critical */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
985 LOCAL->lastsnarf = time (0);/* note time of last snarf */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
986 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
987 /* expunging OK if global flag set */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
988 if (mail_parameters (NIL,GET_EXPUNGEATPING,NIL)) LOCAL->expok = T;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
989 /* process metadata/index/status */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
990 if (statf = mix_parse (stream,&idxf,LONGT,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
991 (LOCAL->internal ? NIL : LONGT))) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
992 fclose (statf); /* just close the status file */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
993 ret = LONGT; /* declare success */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
994 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
995 if (idxf) fclose (idxf); /* release index file */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
996 LOCAL->expok = NIL; /* expunge no longer OK */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
997 if (!ret) mix_abort (stream); /* murdelyze stream if ping fails */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
998 return ret;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
999 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1000
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1001
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1002 /* MIX mail checkpoint mailbox (burp only)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1003 * Accepts: MAIL stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1004 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1005
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1006 void mix_check (MAILSTREAM *stream)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1007 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1008 if (stream->rdonly) /* won't do on readonly files! */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1009 MM_LOG ("Checkpoint ignored on readonly mailbox",NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1010 /* do burp-only expunge action */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1011 if (mix_expunge (stream,"",NIL)) MM_LOG ("Check completed",(long) NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1012 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1013
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1014 /* MIX mail expunge mailbox
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1015 * Accepts: MAIL stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1016 * sequence to expunge if non-NIL, empty string for burp only
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1017 * expunge options
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1018 * Returns: T on success, NIL if failure
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1019 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1020
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1021 long mix_expunge (MAILSTREAM *stream,char *sequence,long options)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1022 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1023 FILE *idxf = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1024 FILE *statf = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1025 MESSAGECACHE *elt;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1026 int ifd,sfd;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1027 long ret;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1028 unsigned long i;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1029 unsigned long nexp = 0;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1030 unsigned long reclaimed = 0;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1031 int burponly = (sequence && !*sequence);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1032 LOCAL->expok = T; /* expunge during ping is OK */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1033 if (!(ret = burponly || !sequence ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1034 ((options & EX_UID) ?
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1035 mail_uid_sequence (stream,sequence) :
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1036 mail_sequence (stream,sequence))) || stream->rdonly);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1037 /* read index and open status exclusive */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1038 else if (statf = mix_parse (stream,&idxf,LONGT,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1039 LOCAL->internal ? NIL : LONGT)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1040 /* expunge unless just burping */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1041 if (!burponly) for (i = 1; i <= stream->nmsgs;) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1042 elt = mail_elt (stream,i);/* need to expunge this message? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1043 if (sequence ? elt->sequence : elt->deleted) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1044 ++nexp; /* yes, make it so */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1045 mail_expunged (stream,i);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1046 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1047 else ++i; /* otherwise advance to next message */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1048 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1049
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1050 /* burp if can get exclusive access */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1051 if (!flock (LOCAL->mfd,LOCK_EX|LOCK_NB)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1052 void *a;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1053 struct direct **names = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1054 long nfiles = scandir (stream->mailbox,&names,mix_select,mix_msgfsort);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1055 if (nfiles > 0) { /* if have message files */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1056 MIXBURP *burp,*cur;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1057 /* initialize burp list */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1058 for (i = 0, burp = cur = NIL; i < nfiles; ++i) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1059 MIXBURP *nxt = (MIXBURP *) memset (fs_get (sizeof (MIXBURP)),0,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1060 sizeof (MIXBURP));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1061 /* another file found */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1062 if (cur) cur = cur->next = nxt;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1063 else cur = burp = nxt;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1064 cur->name = names[i]->d_name;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1065 cur->fileno = strtoul (cur->name + sizeof (MIXNAME) - 1,NIL,16);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1066 cur->tail = &cur->set;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1067 fs_give ((void **) &names[i]);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1068 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1069 /* now load ranges */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1070 for (i = 1, cur = burp; ret && (i <= stream->nmsgs); i++) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1071 /* is this message in current set? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1072 elt = mail_elt (stream,i);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1073 if (cur && (elt->private.spare.data != cur->fileno)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1074 /* restart if necessary */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1075 if (elt->private.spare.data < cur->fileno) cur = burp;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1076 /* hunt for appropriate mailbox */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1077 while (cur && (elt->private.spare.data > cur->fileno))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1078 cur = cur->next;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1079 /* ought to have found it now... */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1080 if (cur && (elt->private.spare.data != cur->fileno)) cur = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1081 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1082 /* if found, add to set */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1083 if (cur) ret = mix_addset (&cur->tail,elt->private.special.offset,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1084 elt->private.msg.header.offset +
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1085 elt->rfc822_size);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1086 else { /* uh-oh */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1087 sprintf (LOCAL->buf,"Can't locate mix message file %.08lx",
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1088 elt->private.spare.data);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1089 MM_LOG (LOCAL->buf,ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1090 ret = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1091 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1092 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1093 if (ret) /* if no errors, burp all files */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1094 for (cur = burp; ret && cur; cur = cur->next) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1095 /* if non-empty, burp it */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1096 if (cur->set.last) ret = mix_burp (stream,cur,&reclaimed);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1097 /* empty, delete it unless new msg file */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1098 else if (mix_file_data (LOCAL->buf,stream->mailbox,cur->fileno) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1099 ((cur->fileno == LOCAL->newmsg) ?
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1100 truncate (LOCAL->buf,0) : unlink (LOCAL->buf))) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1101 sprintf (LOCAL->buf,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1102 "Can't delete empty message file %.80s: %.80s",
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1103 cur->name,strerror (errno));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1104 MM_LOG (LOCAL->buf,WARN);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1105 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1106 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1107 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1108 else MM_LOG ("No mix message files found during expunge",WARN);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1109 /* free directory list */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1110 if (a = (void *) names) fs_give ((void **) &a);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1111 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1112
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1113 /* either way, re-acquire shared lock */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1114 if (flock (LOCAL->mfd,LOCK_SH|LOCK_NB))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1115 fatal ("Unable to re-acquire metadata shared lock!");
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1116 /* Do this step even if ret is NIL (meaning some burp problem)! */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1117 if (nexp || reclaimed) { /* rewrite index and status if changed */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1118 LOCAL->indexseq = mix_modseq (LOCAL->indexseq);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1119 if (mix_index_update (stream,idxf,NIL)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1120 LOCAL->statusseq = mix_modseq (LOCAL->statusseq);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1121 /* set failure if update fails */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1122 ret = mix_status_update (stream,statf,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1123 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1124 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1125 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1126 if (statf) fclose (statf); /* close status if still open */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1127 if (idxf) fclose (idxf); /* close index if still open */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1128 LOCAL->expok = NIL; /* cancel expok */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1129 if (ret) { /* only if success */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1130 char *s = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1131 if (nexp) sprintf (s = LOCAL->buf,"Expunged %lu messages",nexp);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1132 else if (reclaimed)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1133 sprintf (s=LOCAL->buf,"Reclaimed %lu bytes of expunged space",reclaimed);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1134 else if (!burponly)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1135 s = stream->rdonly ? "Expunge ignored on readonly mailbox" :
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1136 "No messages deleted, so no update needed";
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1137 if (s) MM_LOG (s,(long) NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1138 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1139 return ret;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1140 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1141
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1142 /* MIX test for message file name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1143 * Accepts: candidate directory name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1144 * Returns: T if message file name, NIL otherwise
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1145 *
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1146 * ".mix" with no suffix was used by experimental versions
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1147 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1148
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1149 int mix_select (struct direct *name)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1150 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1151 char c,*s;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1152 /* make sure name has prefix */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1153 if (mix_dirfmttest (name->d_name)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1154 for (c = *(s = name->d_name + sizeof (MIXNAME) - 1); c && isxdigit (c);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1155 c = *s++);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1156 if (!c) return T; /* all-hex or no suffix */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1157 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1158 return NIL; /* not suffix or non-hex */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1159 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1160
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1161
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1162 /* MIX msg file name comparision
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1163 * Accepts: first candidate directory entry
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1164 * second candidate directory entry
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1165 * Returns: -1 if d1 < d2, 0 if d1 == d2, 1 d1 > d2
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1166 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1167
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1168 int mix_msgfsort (const void *d1,const void *d2)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1169 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1170 char *n1 = (*(struct direct **) d1)->d_name + sizeof (MIXNAME) - 1;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1171 char *n2 = (*(struct direct **) d2)->d_name + sizeof (MIXNAME) - 1;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1172 return compare_ulong (*n1 ? strtoul (n1,NIL,16) : 0,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1173 *n2 ? strtoul (n2,NIL,16) : 0);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1174 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1175
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1176
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1177 /* MIX add a range to a set
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1178 * Accepts: pointer to set to add
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1179 * start of set
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1180 * size of set
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1181 * Returns: T if success, set updated, NIL otherwise
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1182 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1183
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1184 long mix_addset (SEARCHSET **set,unsigned long start,unsigned long size)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1185 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1186 SEARCHSET *s = *set;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1187 if (start < s->last) { /* sanity check */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1188 char tmp[MAILTMPLEN];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1189 sprintf (tmp,"Backwards-running mix index %lu < %lu",start,s->last);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1190 MM_LOG (tmp,ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1191 return NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1192 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1193 /* range initially empty? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1194 if (!s->last) s->first = start;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1195 else if (start > s->last) /* no, start new range if can't append */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1196 (*set = s = s->next = mail_newsearchset ())->first = start;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1197 s->last = start + size; /* end of current range */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1198 return LONGT;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1199 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1200
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1201 /* MIX burp message file
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1202 * Accepts: MAIL stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1203 * current burp block for this message
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1204 * Returns: T if successful, NIL if failed
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1205 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1206
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1207 static char *staterr = "Error in stat of mix message file %.80s: %.80s";
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1208 static char *truncerr = "Error truncating mix message file %.80s: %.80s";
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1209
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1210 long mix_burp (MAILSTREAM *stream,MIXBURP *burp,unsigned long *reclaimed)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1211 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1212 MESSAGECACHE *elt;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1213 SEARCHSET *set;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1214 struct stat sbuf;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1215 off_t rpos,wpos;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1216 size_t size,wsize,wpending,written;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1217 int fd;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1218 FILE *f;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1219 void *s;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1220 unsigned long i;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1221 long ret = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1222 /* build file name */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1223 mix_file_data (LOCAL->buf,stream->mailbox,burp->fileno);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1224 /* need to burp at start or multiple ranges? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1225 if (!burp->set.first && !burp->set.next) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1226 /* easy case, single range at start of file */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1227 if (stat (LOCAL->buf,&sbuf)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1228 sprintf (LOCAL->buf,staterr,burp->name,strerror (errno));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1229 MM_LOG (LOCAL->buf,ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1230 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1231 /* is this range sane? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1232 else if (mix_burp_check (&burp->set,sbuf.st_size,LOCAL->buf)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1233 /* if matches range then no burp needed! */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1234 if (burp->set.last == sbuf.st_size) ret = LONGT;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1235 /* just need to remove cruft at end */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1236 else if (ret = !truncate (LOCAL->buf,burp->set.last))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1237 *reclaimed += sbuf.st_size - burp->set.last;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1238 else {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1239 sprintf (LOCAL->buf,truncerr,burp->name,strerror (errno));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1240 MM_LOG (LOCAL->buf,ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1241 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1242 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1243 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1244 /* have to do more work, get the file */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1245 else if (((fd = open (LOCAL->buf,O_RDWR,NIL)) < 0) ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1246 !(f = fdopen (fd,"r+b"))) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1247 sprintf (LOCAL->buf,"Error opening mix message file %.80s: %.80s",
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1248 burp->name,strerror (errno));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1249 MM_LOG (LOCAL->buf,ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1250 if (fd >= 0) close (fd); /* in case fdopen() failure */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1251 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1252 else if (fstat (fd,&sbuf)) { /* get file size */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1253 sprintf (LOCAL->buf,staterr,burp->name,strerror (errno));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1254 MM_LOG (LOCAL->buf,ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1255 fclose (f);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1256 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1257
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1258 /* only if sane */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1259 else if (mix_burp_check (&burp->set,sbuf.st_size,LOCAL->buf)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1260 /* make sure each range starts with token */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1261 for (set = &burp->set; set; set = set->next)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1262 if (fseek (f,set->first,SEEK_SET) ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1263 (fread (LOCAL->buf,1,MSGTSZ,f) != MSGTSZ) ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1264 strncmp (LOCAL->buf,MSGTOK,MSGTSZ)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1265 sprintf (LOCAL->buf,"Bad message token in mix message file at %lu",
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1266 set->first);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1267 MM_LOG (LOCAL->buf,ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1268 fclose (f);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1269 return NIL; /* burp fails for this file */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1270 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1271 /* burp out each old message */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1272 for (set = &burp->set, wpos = 0; set; set = set->next) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1273 /* move down this range */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1274 for (rpos = set->first, size = set->last - set->first;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1275 size; size -= wsize) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1276 if (rpos != wpos) { /* data to skip at start? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1277 /* no, slide this buffer down */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1278 wsize = min (size,LOCAL->buflen);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1279 /* failure is not an option here */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1280 while (fseek (f,rpos,SEEK_SET) ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1281 (fread (LOCAL->buf,1,wsize,f) != wsize)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1282 MM_NOTIFY (stream,strerror (errno),WARN);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1283 MM_DISKERROR (stream,errno,T);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1284 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1285 /* nor here */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1286 while (fseek (f,wpos,SEEK_SET)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1287 MM_NOTIFY (stream,strerror (errno),WARN);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1288 MM_DISKERROR (stream,errno,T);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1289 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1290 /* and especially not here */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1291 for (s = LOCAL->buf, wpending = wsize; wpending; wpending -= written)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1292 if (!(written = fwrite (LOCAL->buf,1,wpending,f))) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1293 MM_NOTIFY (stream,strerror (errno),WARN);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1294 MM_DISKERROR (stream,errno,T);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1295 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1296 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1297 else wsize = size; /* nothing to skip, say we wrote it all */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1298 rpos += wsize; wpos += wsize;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1299 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1300 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1301
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1302 while (fflush (f)) { /* failure also not an option here... */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1303 MM_NOTIFY (stream,strerror (errno),WARN);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1304 MM_DISKERROR (stream,errno,T);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1305 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1306 if (ftruncate (fd,wpos)) { /* flush cruft at end of file */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1307 sprintf (LOCAL->buf,truncerr,burp->name,strerror (errno));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1308 MM_LOG (LOCAL->buf,WARN);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1309 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1310 else *reclaimed += rpos - wpos;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1311 ret = !fclose (f); /* close file */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1312 /* slide down message positions in index */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1313 for (i = 1,rpos = 0; i <= stream->nmsgs; ++i)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1314 if ((elt = mail_elt (stream,i))->private.spare.data == burp->fileno) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1315 elt->private.special.offset = rpos;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1316 rpos += elt->private.msg.header.offset + elt->rfc822_size;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1317 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1318 /* debugging */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1319 if (rpos != wpos) fatal ("burp size consistency check!");
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1320 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1321 return ret;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1322 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1323
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1324
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1325 /* MIX burp sanity check to make sure not burping off end of file
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1326 * Accepts: burp set
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1327 * file size
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1328 * file name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1329 * Returns: T if sane, NIL if insane
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 long mix_burp_check (SEARCHSET *set,size_t size,char *file)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1333 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1334 do if (set->last > size) { /* sanity check */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1335 char tmp[MAILTMPLEN];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1336 sprintf (tmp,"Unexpected short mix message file %.80s %lu < %lu",
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1337 file,size,set->last);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1338 MM_LOG (tmp,ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1339 return NIL; /* don't burp this file at all */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1340 } while (set = set->next);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1341 return LONGT;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1342 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1343
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1344 /* MIX mail copy message(s)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1345 * Accepts: MAIL stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1346 * sequence
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1347 * destination mailbox
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1348 * copy options
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1349 * Returns: T if copy successful, else NIL
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1350 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1351
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1352 long mix_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1353 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1354 FDDATA d;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1355 STRING st;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1356 char tmp[2*MAILTMPLEN];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1357 long ret = mix_isvalid (mailbox,LOCAL->buf);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1358 mailproxycopy_t pc =
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1359 (mailproxycopy_t) mail_parameters (stream,GET_MAILPROXYCOPY,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1360 MAILSTREAM *astream = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1361 FILE *idxf = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1362 FILE *msgf = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1363 FILE *statf = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1364 if (!ret) switch (errno) { /* make sure valid mailbox */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1365 case NIL: /* no error in stat() */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1366 if (pc) return (*pc) (stream,sequence,mailbox,options);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1367 sprintf (tmp,"Not a MIX-format mailbox: %.80s",mailbox);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1368 MM_LOG (tmp,ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1369 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1370 default: /* some stat() error */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1371 MM_NOTIFY (stream,"[TRYCREATE] Must create mailbox before copy",NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1372 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1373 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1374 /* get sequence to copy */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1375 else if (!(ret = ((options & CP_UID) ? mail_uid_sequence (stream,sequence) :
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1376 mail_sequence (stream,sequence))));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1377 /* acquire stream to append */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1378 else if (ret = ((astream = mail_open (NIL,mailbox,OP_SILENT)) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1379 !astream->rdonly &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1380 (((MIXLOCAL *) astream->local)->expok = T) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1381 (statf = mix_parse (astream,&idxf,LONGT,NIL))) ?
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1382 LONGT : NIL) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1383 int fd;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1384 unsigned long i;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1385 MESSAGECACHE *elt;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1386 unsigned long newsize,hdrsize,size;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1387 MIXLOCAL *local = (MIXLOCAL *) astream->local;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1388 unsigned long seq = mix_modseq (local->metaseq);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1389 /* make sure new modseq fits */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1390 if (local->indexseq > seq) seq = local->indexseq + 1;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1391 if (local->statusseq > seq) seq = local->statusseq + 1;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1392 /* calculate size of per-message header */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1393 sprintf (local->buf,MSRFMT,MSGTOK,0,0,0,0,0,0,0,'+',0,0,0);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1394 hdrsize = strlen (local->buf);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1395
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1396 MM_CRITICAL (stream); /* go critical */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1397 astream->silent = T; /* no events here */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1398 /* calculate size that will be added */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1399 for (i = 1, newsize = 0; i <= stream->nmsgs; ++i)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1400 if ((elt = mail_elt (stream,i))->sequence)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1401 newsize += hdrsize + elt->rfc822_size;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1402 /* open data file */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1403 if (msgf = mix_data_open (astream,&fd,&size,newsize)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1404 char *t;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1405 unsigned long j,uid,uidv;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1406 copyuid_t cu = (copyuid_t) mail_parameters (NIL,GET_COPYUID,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1407 SEARCHSET *source = cu ? mail_newsearchset () : NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1408 SEARCHSET *dest = cu ? mail_newsearchset () : NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1409 for (i = 1,uid = uidv = 0; ret && (i <= stream->nmsgs); ++i)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1410 if (((elt = mail_elt (stream,i))->sequence) && elt->rfc822_size) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1411 /* is message in current message file? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1412 if ((LOCAL->msgfd < 0) ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1413 (elt->private.spare.data != LOCAL->curmsg)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1414 if (LOCAL->msgfd >= 0) close (LOCAL->msgfd);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1415 if ((LOCAL->msgfd = open (mix_file_data (LOCAL->buf,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1416 stream->mailbox,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1417 elt->private.spare.data),
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1418 O_RDONLY,NIL)) >= 0)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1419 LOCAL->curmsg = elt->private.spare.data;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1420 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1421 if (LOCAL->msgfd < 0) ret = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1422 else { /* got file */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1423 d.fd = LOCAL->msgfd;/* set up file descriptor */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1424 /* start of message */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1425 d.pos = elt->private.special.offset +
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1426 elt->private.msg.header.offset;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1427 d.chunk = LOCAL->buf;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1428 d.chunksize = CHUNKSIZE;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1429 INIT (&st,fd_string,&d,elt->rfc822_size);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1430 /* init flag string */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1431 tmp[0] = tmp[1] = '\0';
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1432 if (j = elt->user_flags) do
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1433 if ((t = stream->user_flags[find_rightmost_bit (&j)]) && *t)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1434 strcat (strcat (tmp," "),t);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1435 while (j);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1436 if (elt->seen) strcat (tmp," \\Seen");
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1437 if (elt->deleted) strcat (tmp," \\Deleted");
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1438 if (elt->flagged) strcat (tmp," \\Flagged");
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1439 if (elt->answered) strcat (tmp," \\Answered");
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1440 if (elt->draft) strcat (tmp," \\Draft");
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1441 tmp[0] = '('; /* wrap list */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1442 strcat (tmp,")");
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1443 /* if append OK, add to source set */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1444 if ((ret = mix_append_msg (astream,msgf,tmp,elt,&st,dest,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1445 seq)) && source)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1446 mail_append_set (source,mail_uid (stream,i));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1447 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1448 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1449
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1450 /* finish write if success */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1451 if (ret && (ret = !fflush (msgf))) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1452 fclose (msgf); /* all good, close the msg file now */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1453 /* write new metadata, index, and status */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1454 local->metaseq = local->indexseq = local->statusseq = seq;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1455 if (ret = (mix_meta_update (astream) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1456 mix_index_update (astream,idxf,LONGT))) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1457 /* success, delete if doing a move */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1458 if (options & CP_MOVE)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1459 for (i = 1; i <= stream->nmsgs; i++)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1460 if ((elt = mail_elt (stream,i))->sequence) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1461 elt->deleted = T;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1462 if (!stream->rdonly) elt->private.mod = LOCAL->statusseq = seq;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1463 MM_FLAGS (stream,elt->msgno);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1464 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1465 /* done with status file now */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1466 mix_status_update (astream,statf,LONGT);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1467 /* return sets if doing COPYUID */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1468 if (cu) (*cu) (stream,mailbox,astream->uid_validity,source,dest);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1469 source = dest = NIL; /* don't free these sets now */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1470 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1471 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1472 else { /* error */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1473 if (errno) { /* output error message if system call error */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1474 sprintf (tmp,"Message copy failed: %.80s",strerror (errno));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1475 MM_LOG (tmp,ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1476 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1477 ftruncate (fd,size); /* revert file */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1478 close (fd); /* make sure that fclose doesn't corrupt us */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1479 fclose (msgf); /* free the stdio resources */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1480 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1481 /* flush any sets remaining */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1482 mail_free_searchset (&source);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1483 mail_free_searchset (&dest);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1484 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1485 else { /* message file open failed */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1486 sprintf (tmp,"Error opening copy message file: %.80s",
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1487 strerror (errno));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1488 MM_LOG (tmp,ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1489 ret = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1490 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1491 MM_NOCRITICAL (stream);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1492 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1493 else MM_LOG ("Can't open copy mailbox",ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1494 if (statf) fclose (statf); /* close status if still open */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1495 if (idxf) fclose (idxf); /* close index if still open */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1496 /* finished with append stream */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1497 if (astream) mail_close (astream);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1498 return ret; /* return state */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1499 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1500
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1501 /* MIX mail append message from stringstruct
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1502 * Accepts: MAIL stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1503 * destination mailbox
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1504 * append callback
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1505 * data for callback
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1506 * Returns: T if append successful, else NIL
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1507 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1508
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1509 long mix_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1510 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1511 STRING *message;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1512 char *flags,*date,tmp[MAILTMPLEN];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1513 /* N.B.: can't use LOCAL->buf for tmp */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1514 long ret = mix_isvalid (mailbox,tmp);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1515 /* default stream to prototype */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1516 if (!stream) stream = user_flags (&mixproto);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1517 if (!ret) switch (errno) { /* if not valid mailbox */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1518 case ENOENT: /* no such file? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1519 if (ret = compare_cstring (mailbox,"INBOX") ?
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1520 NIL : mix_create (NIL,"INBOX"))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1521 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1522 MM_NOTIFY (stream,"[TRYCREATE] Must create mailbox before append",NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1523 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1524 default:
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1525 sprintf (tmp,"Not a MIX-format mailbox: %.80s",mailbox);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1526 MM_LOG (tmp,ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1527 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1528 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1529
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1530 /* get first message */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1531 if (ret && MM_APPEND (af) (stream,data,&flags,&date,&message)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1532 MAILSTREAM *astream;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1533 FILE *idxf = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1534 FILE *msgf = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1535 FILE *statf = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1536 if (ret = ((astream = mail_open (NIL,mailbox,OP_SILENT)) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1537 !astream->rdonly &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1538 (((MIXLOCAL *) astream->local)->expok = T) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1539 (statf = mix_parse (astream,&idxf,LONGT,NIL))) ?
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1540 LONGT : NIL) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1541 int fd;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1542 unsigned long size,hdrsize;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1543 MESSAGECACHE elt;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1544 MIXLOCAL *local = (MIXLOCAL *) astream->local;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1545 unsigned long seq = mix_modseq (local->metaseq);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1546 /* make sure new modseq fits */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1547 if (local->indexseq > seq) seq = local->indexseq + 1;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1548 if (local->statusseq > seq) seq = local->statusseq + 1;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1549 /* calculate size of per-message header */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1550 sprintf (local->buf,MSRFMT,MSGTOK,0,0,0,0,0,0,0,'+',0,0,0);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1551 hdrsize = strlen (local->buf);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1552 MM_CRITICAL (astream); /* go critical */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1553 astream->silent = T; /* no events here */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1554 /* open data file */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1555 if (msgf = mix_data_open (astream,&fd,&size,hdrsize + SIZE (message))) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1556 appenduid_t au = (appenduid_t) mail_parameters (NIL,GET_APPENDUID,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1557 SEARCHSET *dst = au ? mail_newsearchset () : NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1558 while (ret && message) {/* while good to go and have messages */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1559 errno = NIL; /* in case one of these causes failure */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1560 /* guard against zero-length */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1561 if (!(ret = SIZE (message)))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1562 MM_LOG ("Append of zero-length message",ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1563 else if (date && !(ret = mail_parse_date (&elt,date))) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1564 sprintf (tmp,"Bad date in append: %.80s",date);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1565 MM_LOG (tmp,ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1566 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1567 else {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1568 if (!date) { /* if date not specified, use now */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1569 internal_date (tmp);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1570 mail_parse_date (&elt,tmp);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1571 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1572 ret = mix_append_msg (astream,msgf,flags,&elt,message,dst,seq) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1573 MM_APPEND (af) (stream,data,&flags,&date,&message);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1574 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1575 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1576
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1577 /* finish write if success */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1578 if (ret && (ret = !fflush (msgf))) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1579 fclose (msgf); /* all good, close the msg file now */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1580 /* write new metadata, index, and status */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1581 local->metaseq = local->indexseq = local->statusseq = seq;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1582 if ((ret = (mix_meta_update (astream) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1583 mix_index_update (astream,idxf,LONGT) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1584 mix_status_update (astream,statf,LONGT))) && au) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1585 (*au) (mailbox,astream->uid_validity,dst);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1586 dst = NIL; /* don't free this set now */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1587 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1588 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1589 else { /* failure */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1590 if (errno) { /* output error message if system call error */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1591 sprintf (tmp,"Message append failed: %.80s",strerror (errno));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1592 MM_LOG (tmp,ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1593 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1594 ftruncate (fd,size); /* revert all writes to file*/
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1595 close (fd); /* make sure that fclose doesn't corrupt us */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1596 fclose (msgf); /* free the stdio resources */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1597 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1598 /* flush any set remaining */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1599 mail_free_searchset (&dst);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1600 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1601 else { /* message file open failed */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1602 sprintf (tmp,"Error opening append message file: %.80s",
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1603 strerror (errno));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1604 MM_LOG (tmp,ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1605 ret = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1606 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1607 MM_NOCRITICAL (astream); /* release critical */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1608 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1609 else MM_LOG ("Can't open append mailbox",ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1610 if (statf) fclose (statf); /* close status if still open */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1611 if (idxf) fclose (idxf); /* close index if still open */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1612 if (astream) mail_close (astream);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1613 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1614 return ret;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1615 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1616
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1617 /* MIX mail append single message
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1618 * Accepts: MAIL stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1619 * flags for new message if non-NIL
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1620 * elt with source date if non-NIL
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1621 * stringstruct of message text
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1622 * searchset to place UID
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1623 * modseq of message
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1624 * Returns: T if success, NIL if failure
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1625 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1626
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1627 long mix_append_msg (MAILSTREAM *stream,FILE *f,char *flags,MESSAGECACHE *delt,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1628 STRING *msg,SEARCHSET *set,unsigned long seq)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1629 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1630 MESSAGECACHE *elt;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1631 int c,cs;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1632 unsigned long i,j,k,uf,hoff;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1633 long sf;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1634 stream->kwd_create = NIL; /* don't copy unknown keywords */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1635 sf = mail_parse_flags (stream,flags,&uf);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1636 /* swell the cache */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1637 mail_exists (stream,++stream->nmsgs);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1638 /* assign new UID from metadata */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1639 (elt = mail_elt (stream,stream->nmsgs))->private.uid = ++stream->uid_last;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1640 elt->private.mod = seq; /* set requested modseq in status */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1641 elt->rfc822_size = SIZE (msg);/* copy message size and date to index */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1642 elt->year = delt->year; elt->month = delt->month; elt->day = delt->day;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1643 elt->hours = delt->hours; elt->minutes = delt->minutes;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1644 elt->seconds = delt->seconds; elt->zoccident = delt->zoccident;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1645 elt->zhours = delt->zhours; elt->zminutes = delt->zminutes;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1646 /*
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1647 * Do NOT set elt->valid here! mix_status_update() uses it to determine
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1648 * whether a message should be marked as old.
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1649 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1650 if (sf&fSEEN) elt->seen = T; /* copy flags to status */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1651 if (sf&fDELETED) elt->deleted = T;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1652 if (sf&fFLAGGED) elt->flagged = T;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1653 if (sf&fANSWERED) elt->answered = T;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1654 if (sf&fDRAFT) elt->draft = T;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1655 elt->user_flags |= uf;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1656 /* message is in new message file */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1657 elt->private.spare.data = LOCAL->newmsg;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1658
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1659 /* offset to message internal header */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1660 elt->private.special.offset = ftell (f);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1661 /* build header for message */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1662 fprintf (f,MSRFMT,MSGTOK,elt->private.uid,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1663 elt->year + BASEYEAR,elt->month,elt->day,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1664 elt->hours,elt->minutes,elt->seconds,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1665 elt->zoccident ? '-' : '+',elt->zhours,elt->zminutes,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1666 elt->rfc822_size);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1667 /* offset to header from internal header */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1668 elt->private.msg.header.offset = ftell (f) - elt->private.special.offset;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1669 for (cs = 0; SIZE (msg); ) { /* copy message */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1670 if (elt->private.msg.header.text.size) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1671 if (msg->cursize) /* blat entire chunk if have it */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1672 for (j = msg->cursize; j; j -= k)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1673 if (!(k = fwrite (msg->curpos,1,j,f))) return NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1674 SETPOS (msg,GETPOS (msg) + msg->cursize);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1675 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1676 else { /* still searching for delimiter */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1677 c = 0xff & SNX (msg); /* get source character */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1678 if (putc (c,f) == EOF) return NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1679 switch (cs) { /* decide what to do based on state */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1680 case 0: /* previous char ordinary */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1681 if (c == '\015') cs = 1;/* advance if CR */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1682 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1683 case 1: /* previous CR, advance if LF */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1684 cs = (c == '\012') ? 2 : 0;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1685 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1686 case 2: /* previous CRLF, advance if CR */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1687 cs = (c == '\015') ? 3 : 0;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1688 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1689 case 3: /* previous CRLFCR, done if LF */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1690 if (c == '\012') elt->private.msg.header.text.size =
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1691 elt->rfc822_size - SIZE (msg);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1692 cs = 0; /* reset mechanism */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1693 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1694 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1695 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1696 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1697 /* if no delimiter, header is entire msg */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1698 if (!elt->private.msg.header.text.size)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1699 elt->private.msg.header.text.size = elt->rfc822_size;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1700 /* add this message to set */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1701 mail_append_set (set,elt->private.uid);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1702 return LONGT; /* success */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1703 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1704
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1705 /* MIX mail read metadata, index, and status
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1706 * Accepts: MAIL stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1707 * returned index file
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1708 * index file flags (non-NIL if want to add/remove messages)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1709 * status file flags (non-NIL if want to update elt->valid and old)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1710 * Returns: open status file, or NIL if failure
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1711 *
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1712 * Note that this routine can return an open index file even if it fails!
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1713 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1714
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1715 static char *shortmsg =
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1716 "message %lu (UID=%.08lx) truncated by %lu byte(s) (%lu < %lu)";
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1717
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1718 FILE *mix_parse (MAILSTREAM *stream,FILE **idxf,long iflags,long sflags)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1719 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1720 int fd;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1721 unsigned long i;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1722 char *s,*t;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1723 struct stat sbuf;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1724 FILE *statf = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1725 short metarepairneeded = 0;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1726 short indexrepairneeded = 0;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1727 short silent = stream->silent;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1728 *idxf = NIL; /* in case error */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1729 /* readonly means no updates */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1730 if (stream->rdonly) iflags = sflags = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1731 /* open index file */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1732 if ((fd = open (LOCAL->index,iflags ? O_RDWR : O_RDONLY,NIL)) < 0)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1733 MM_LOG ("Error opening mix index file",ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1734 /* acquire exclusive access and FILE */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1735 else if (!flock (fd,iflags ? LOCK_EX : LOCK_SH) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1736 !(*idxf = fdopen (fd,iflags ? "r+b" : "rb"))) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1737 MM_LOG ("Error obtaining stream on mix index file",ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1738 flock (fd,LOCK_UN); /* relinquish lock */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1739 close (fd);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1740 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1741
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1742 /* slurp metadata */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1743 else if (s = mix_meta_slurp (stream,&i)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1744 unsigned long j = 0; /* non-zero if UIDVALIDITY/UIDLAST changed */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1745 if (i != LOCAL->metaseq) { /* metadata changed? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1746 char *t,*k;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1747 LOCAL->metaseq = i; /* note new metadata sequence */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1748 while (s && *s) { /* parse entire metadata file */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1749 /* locate end of line */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1750 if (s = strstr (t = s,"\015\012")) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1751 *s = '\0'; /* tie off line */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1752 s += 2; /* skip past CRLF */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1753 switch (*t++) { /* parse line */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1754 case 'V': /* UIDVALIDITY */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1755 if (!isxdigit (*t) || !(i = strtoul (t,&t,16))) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1756 MM_LOG ("Error in mix metadata file UIDVALIDITY record",ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1757 return NIL; /* give up */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1758 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1759 if (i != stream->uid_validity) j = stream->uid_validity = i;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1760 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1761 case 'L': /* new UIDLAST */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1762 if (!isxdigit (*t)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1763 MM_LOG ("Error in mix metadata file UIDLAST record",ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1764 return NIL; /* give up */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1765 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1766 if ((i = strtoul (t,&t,16)) != stream->uid_last)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1767 j = stream->uid_last = i;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1768 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1769 case 'N': /* new message file */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1770 if (!isxdigit (*t)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1771 MM_LOG ("Error in mix metadata file new msg record",ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1772 return NIL; /* give up */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1773 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1774 if ((i = strtoul (t,&t,16)) != stream->uid_last)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1775 LOCAL->newmsg = i;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1776 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1777 case 'K': /* new keyword list */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1778 for (i = 0; t && *t && (i < NUSERFLAGS); ++i) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1779 if (t = strchr (k = t,' ')) *t++ = '\0';
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1780 /* make sure keyword non-empty */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1781 if (*k && (strlen (k) <= MAXUSERFLAG)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1782 /* in case value changes (shouldn't happen) */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1783 if (stream->user_flags[i] && strcmp (stream->user_flags[i],k)){
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1784 char tmp[MAILTMPLEN];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1785 sprintf (tmp,"flag rename old=%.80s new=%.80s",
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1786 stream->user_flags[i],k);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1787 MM_LOG (tmp,WARN);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1788 fs_give ((void **) &stream->user_flags[i]);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1789 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1790 if (!stream->user_flags[i]) stream->user_flags[i] = cpystr (k);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1791 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1792 else break; /* empty keyword */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1793 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1794 if ((i < NUSERFLAGS) && stream->user_flags[i]) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1795 MM_LOG ("Error in mix metadata file keyword record",ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1796 return NIL; /* give up */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1797 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1798 else if (i == NUSERFLAGS) stream->kwd_create = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1799 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1800 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1801 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1802 if (t && *t) { /* junk in line */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1803 MM_LOG ("Error in mix metadata record",ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1804 return NIL; /* give up */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1805 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1806 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1807 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1808
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1809 /* get sequence */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1810 if (!(i = mix_read_sequence (*idxf)) || (i < LOCAL->indexseq)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1811 MM_LOG ("Error in mix index file sequence record",ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1812 return NIL; /* give up */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1813 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1814 /* sequence changed from last time? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1815 else if (j || (i > LOCAL->indexseq)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1816 unsigned long uid,nmsgs,curfile,curfilesize,curpos;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1817 char *t,*msg,tmp[MAILTMPLEN];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1818 /* start with no messages */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1819 curfile = curfilesize = curpos = nmsgs = 0;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1820 /* update sequence iff expunging OK */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1821 if (LOCAL->expok) LOCAL->indexseq = i;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1822 /* get first elt */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1823 while ((s = mix_read_record (*idxf,LOCAL->buf,LOCAL->buflen,"index")) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1824 *s)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1825 switch (*s) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1826 case ':': /* message record */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1827 if (!(isxdigit (*++s) && (uid = strtoul (s,&t,16)))) msg = "UID";
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1828 else if (!((*t++ == ':') && isdigit (*t) && isdigit (t[1]) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1829 isdigit (t[2]) && isdigit (t[3]) && isdigit (t[4]) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1830 isdigit (t[5]) && isdigit (t[6]) && isdigit (t[7]) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1831 isdigit (t[8]) && isdigit (t[9]) && isdigit (t[10]) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1832 isdigit (t[11]) && isdigit (t[12]) && isdigit (t[13]) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1833 ((t[14] == '+') || (t[14] == '-')) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1834 isdigit (t[15]) && isdigit (t[16]) && isdigit (t[17]) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1835 isdigit (t[18]))) msg = "internaldate";
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1836 else if ((*(s = t+19) != ':') || !isxdigit (*++s)) msg = "size";
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1837 else {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1838 unsigned int y = (((*t - '0') * 1000) + ((t[1] - '0') * 100) +
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1839 ((t[2] - '0') * 10) + t[3] - '0') - BASEYEAR;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1840 unsigned int m = ((t[4] - '0') * 10) + t[5] - '0';
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1841 unsigned int d = ((t[6] - '0') * 10) + t[7] - '0';
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1842 unsigned int hh = ((t[8] - '0') * 10) + t[9] - '0';
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1843 unsigned int mm = ((t[10] - '0') * 10) + t[11] - '0';
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1844 unsigned int ss = ((t[12] - '0') * 10) + t[13] - '0';
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1845 unsigned int z = (t[14] == '-') ? 1 : 0;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1846 unsigned int zh = ((t[15] - '0') * 10) + t[16] - '0';
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1847 unsigned int zm = ((t[17] - '0') * 10) + t[18] - '0';
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1848 unsigned long size = strtoul (s,&s,16);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1849 if ((*s++ == ':') && isxdigit (*s)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1850 unsigned long file = strtoul (s,&s,16);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1851 if ((*s++ == ':') && isxdigit (*s)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1852 unsigned long pos = strtoul (s,&s,16);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1853 if ((*s++ == ':') && isxdigit (*s)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1854 unsigned long hpos = strtoul (s,&s,16);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1855 if ((*s++ == ':') && isxdigit (*s)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1856 unsigned long hsiz = strtoul (s,&s,16);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1857 if (uid > stream->uid_last) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1858 sprintf (tmp,"mix index invalid UID (%08lx < %08lx)",
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1859 uid,stream->uid_last);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1860 if (stream->rdonly) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1861 MM_LOG (tmp,ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1862 return NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1863 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1864 strcat (tmp,", repaired");
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1865 MM_LOG (tmp,WARN);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1866 stream->uid_last = uid;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1867 metarepairneeded = T;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1868 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1869
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1870 /* ignore expansion values */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1871 if (*s++ == ':') {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1872 MESSAGECACHE *elt;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1873 ++nmsgs; /* this is another mesage */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1874 /* within current known range of messages? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1875 while (nmsgs <= stream->nmsgs) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1876 /* yes, get corresponding elt */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1877 elt = mail_elt (stream,nmsgs);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1878 /* existing message with matching data? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1879 if (uid == elt->private.uid) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1880 /* beware of Dracula's resurrection */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1881 if (elt->private.ghost) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1882 sprintf (tmp,"mix index data unexpunged UID: %lx",
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1883 uid);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1884 MM_LOG (tmp,ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1885 return NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1886 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1887 /* also of static data changing */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1888 if ((size != elt->rfc822_size) ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1889 (file != elt->private.spare.data) ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1890 (pos != elt->private.special.offset) ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1891 (hpos != elt->private.msg.header.offset) ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1892 (hsiz != elt->private.msg.header.text.size) ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1893 (y != elt->year) || (m != elt->month) ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1894 (d != elt->day) || (hh != elt->hours) ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1895 (mm != elt->minutes) || (ss != elt->seconds) ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1896 (z != elt->zoccident) || (zh != elt->zhours) ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1897 (zm != elt->zminutes)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1898 sprintf (tmp,"mix index data mismatch: %lx",uid);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1899 MM_LOG (tmp,ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1900 return NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1901 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1902 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1903 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1904 /* existing msg with lower UID is expunged */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1905 else if (uid > elt->private.uid) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1906 if (LOCAL->expok) mail_expunged (stream,nmsgs);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1907 else {/* message expunged, but not yet for us */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1908 ++nmsgs;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1909 elt->private.ghost = T;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1910 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1911 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1912 else { /* unexpected message record */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1913 sprintf (tmp,"mix index UID mismatch (%lx < %lx)",
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1914 uid,elt->private.uid);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1915 MM_LOG (tmp,ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1916 return NIL;
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
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1920 /* time to create a new message? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1921 if (nmsgs > stream->nmsgs) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1922 /* defer announcing until later */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1923 stream->silent = T;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1924 mail_exists (stream,nmsgs);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1925 stream->silent = silent;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1926 (elt = mail_elt (stream,nmsgs))->recent = T;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1927 elt->private.uid = uid; elt->rfc822_size = size;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1928 elt->private.spare.data = file;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1929 elt->private.special.offset = pos;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1930 elt->private.msg.header.offset = hpos;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1931 elt->private.msg.header.text.size = hsiz;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1932 elt->year = y; elt->month = m; elt->day = d;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1933 elt->hours = hh; elt->minutes = mm;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1934 elt->seconds = ss; elt->zoccident = z;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1935 elt->zhours = zh; elt->zminutes = zm;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1936 /* message in same file? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1937 if (curfile == file) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1938 if (pos < curpos) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1939 MESSAGECACHE *plt = mail_elt (stream,elt->msgno-1);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1940 /* uh-oh, calculate delta? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1941 i = curpos - pos;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1942 sprintf (tmp,shortmsg,plt->msgno,plt->private.uid,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1943 i,pos,curpos);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1944 /* possible to fix? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1945 if (!stream->rdonly && LOCAL->expok &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1946 (i < plt->rfc822_size)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1947 plt->rfc822_size -= i;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1948 if (plt->rfc822_size <
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1949 plt->private.msg.header.text.size)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1950 plt->private.msg.header.text.size =
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1951 plt->rfc822_size;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1952 strcat (tmp,", repaired");
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1953 indexrepairneeded = T;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1954 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1955 MM_LOG (tmp,WARN);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1956 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1957 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1958 else { /* new file, restart */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1959 if (stat (mix_file_data (LOCAL->buf,stream->mailbox,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1960 curfile = file),&sbuf)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1961 sprintf (tmp,"Missing mix data file: %.500s",
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1962 LOCAL->buf);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1963 MM_LOG (tmp,ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1964 return NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1965 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1966 curfile = file;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1967 curfilesize = sbuf.st_size;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1968 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1969
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1970 /* position of message in file */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1971 curpos = pos + elt->private.msg.header.offset +
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1972 elt->rfc822_size;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1973 /* short file? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1974 if (curfilesize < curpos) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1975 /* uh-oh, calculate delta? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1976 i = curpos - curfilesize;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1977 sprintf (tmp,shortmsg,elt->msgno,elt->private.uid,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1978 i,curfilesize,curpos);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1979 /* possible to fix? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1980 if (!stream->rdonly && LOCAL->expok &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1981 (i < elt->rfc822_size)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1982 elt->rfc822_size -= i;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1983 if (elt->rfc822_size <
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1984 elt->private.msg.header.text.size)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1985 elt->private.msg.header.text.size =
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1986 elt->rfc822_size;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1987 strcat (tmp,", repaired");
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1988 indexrepairneeded = T;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1989 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1990 MM_LOG (tmp,WARN);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1991 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1992 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1993 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1994 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1995 else msg = "expansion";
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1996 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1997 else msg = "header size";
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1998 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1999 else msg = "header position";
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2000 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2001 else msg = "message position";
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2002 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2003 else msg = "file#";
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2004 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2005 sprintf (tmp,"Error in %s in mix index file: %.500s",msg,s);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2006 MM_LOG (tmp,ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2007 return NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2008 default:
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2009 sprintf (tmp,"Unknown record in mix index file: %.500s",s);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2010 MM_LOG (tmp,ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2011 return NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2012 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2013 if (!s) return NIL; /* barfage from mix_read_record() */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2014 /* expunge trailing messages not in index */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2015 if (LOCAL->expok) while (nmsgs < stream->nmsgs)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2016 mail_expunged (stream,stream->nmsgs);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2017 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2018
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2019 /* repair metadata and index if needed */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2020 if ((metarepairneeded ? mix_meta_update (stream) : T) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2021 (indexrepairneeded ? mix_index_update (stream,*idxf,NIL) : T)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2022 MESSAGECACHE *elt;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2023 int fd;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2024 unsigned long uid,uf,sf,mod;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2025 char *s;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2026 int updatep = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2027 /* open status file */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2028 if ((fd = open (LOCAL->status,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2029 stream->rdonly ? O_RDONLY : O_RDWR,NIL)) < 0)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2030 MM_LOG ("Error opening mix status file",ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2031 /* acquire exclusive access and FILE */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2032 else if (!flock (fd,stream->rdonly ? LOCK_SH : LOCK_EX) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2033 !(statf = fdopen (fd,stream->rdonly ? "rb" : "r+b"))) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2034 MM_LOG ("Error obtaining stream on mix status file",ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2035 flock (fd,LOCK_UN); /* relinquish lock */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2036 close (fd);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2037 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2038 /* get sequence */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2039 else if (!(i = mix_read_sequence (statf)) ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2040 ((i < LOCAL->statusseq) && stream->nmsgs && (i != 1))) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2041 sprintf (LOCAL->buf,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2042 "Error in mix status sequence record, i=%lx, seq=%lx",
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2043 i,LOCAL->statusseq);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2044 MM_LOG (LOCAL->buf,ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2045 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2046 /* sequence changed from last time? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2047 else if (i != LOCAL->statusseq) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2048 /* update sequence, get first elt */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2049 if (i > LOCAL->statusseq) LOCAL->statusseq = i;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2050 if (stream->nmsgs) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2051 elt = mail_elt (stream,i = 1);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2052
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2053 /* read message records */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2054 while ((t = s = mix_read_record (statf,LOCAL->buf,LOCAL->buflen,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2055 "status")) && *s && (*s++ == ':') &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2056 isxdigit (*s)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2057 uid = strtoul (s,&s,16);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2058 if ((*s++ == ':') && isxdigit (*s)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2059 uf = strtoul (s,&s,16);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2060 if ((*s++ == ':') && isxdigit (*s)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2061 sf = strtoul (s,&s,16);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2062 if ((*s++ == ':') && isxdigit (*s)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2063 mod = strtoul (s,&s,16);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2064 /* ignore expansion values */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2065 if (*s++ == ':') {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2066 /* need to move ahead to next elt? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2067 while ((uid > elt->private.uid) && (i < stream->nmsgs))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2068 elt = mail_elt (stream,++i);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2069 /* update elt if altered */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2070 if ((uid == elt->private.uid) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2071 (!elt->valid || (mod != elt->private.mod))) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2072 elt->user_flags = uf;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2073 elt->private.mod = mod;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2074 elt->seen = (sf & fSEEN) ? T : NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2075 elt->deleted = (sf & fDELETED) ? T : NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2076 elt->flagged = (sf & fFLAGGED) ? T : NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2077 elt->answered = (sf & fANSWERED) ? T : NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2078 elt->draft = (sf & fDRAFT) ? T : NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2079 /* announce if altered existing message */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2080 if (elt->valid) MM_FLAGS (stream,elt->msgno);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2081 /* first time, is old message? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2082 else if (sf & fOLD) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2083 /* yes, clear recent and set valid */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2084 elt->recent = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2085 elt->valid = T;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2086 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2087 /* recent, allowed to update its status? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2088 else if (sflags) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2089 /* yes, set valid and check in status */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2090 elt->valid = T;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2091 elt->private.mod = mix_modseq (elt->private.mod);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2092 updatep = T;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2093 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2094 /* leave valid unset and recent if sflags not set */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2095 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2096 continue; /* everything looks good */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2097 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2098 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2099 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2100 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2101 break; /* error somewhere */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2102 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2103
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2104 if (t && *t) { /* non-null means bogus record */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2105 char msg[MAILTMPLEN];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2106 sprintf (msg,"Error in mix status file message record%s: %.80s",
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2107 stream->rdonly ? "" : ", fixing",t);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2108 MM_LOG (msg,WARN);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2109 /* update it if not readonly */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2110 if (!stream->rdonly) updatep = T;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2111 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2112 if (updatep) { /* need to update? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2113 LOCAL->statusseq = mix_modseq (LOCAL->statusseq);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2114 mix_status_update (stream,statf,LONGT);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2115 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2116 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2117 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2118 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2119 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2120 if (statf) { /* still happy? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2121 unsigned long j;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2122 stream->silent = silent; /* now notify upper level */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2123 mail_exists (stream,stream->nmsgs);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2124 for (i = 1, j = 0; i <= stream->nmsgs; ++i)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2125 if (mail_elt (stream,i)->recent) ++j;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2126 mail_recent (stream,j);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2127 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2128 return statf;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2129 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2130
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2131 /* MIX metadata file routines */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2132
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2133 /* MIX read metadata
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2134 * Accepts: MAIL stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2135 * return pointer for modseq
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2136 * Returns: pointer to metadata after modseq or NIL if failure
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2137 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2138
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2139 char *mix_meta_slurp (MAILSTREAM *stream,unsigned long *seq)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2140 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2141 struct stat sbuf;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2142 char *s;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2143 char *ret = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2144 if (fstat (LOCAL->mfd,&sbuf))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2145 MM_LOG ("Error obtaining size of mix metatdata file",ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2146 if (sbuf.st_size > LOCAL->buflen) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2147 /* should be just a few dozen bytes */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2148 if (sbuf.st_size > METAMAX) fatal ("absurd mix metadata file size");
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2149 fs_give ((void **) &LOCAL->buf);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2150 LOCAL->buf = (char *) fs_get ((LOCAL->buflen = sbuf.st_size) + 1);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2151 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2152 /* read current metadata file */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2153 LOCAL->buf[sbuf.st_size] = '\0';
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2154 if (lseek (LOCAL->mfd,0,L_SET) ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2155 (read (LOCAL->mfd,s = LOCAL->buf,sbuf.st_size) != sbuf.st_size))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2156 MM_LOG ("Error reading mix metadata file",ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2157 else if ((*s != 'S') || !isxdigit (s[1]) ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2158 ((*seq = strtoul (s+1,&s,16)) < LOCAL->metaseq) ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2159 (*s++ != '\015') || (*s++ != '\012'))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2160 MM_LOG ("Error in mix metadata file sequence record",ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2161 else ret = s;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2162 return ret;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2163 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2164
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2165 /* MIX update metadata
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2166 * Accepts: MAIL stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2167 * Returns: T on success, NIL if error
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2168 *
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2169 * Index MUST be locked!!
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2170 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2171
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2172 long mix_meta_update (MAILSTREAM *stream)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2173 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2174 long ret;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2175 /* do nothing if stream readonly */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2176 if (stream->rdonly) ret = LONGT;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2177 else {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2178 unsigned char c,*s,*ss,*t;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2179 unsigned long i;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2180 /* The worst-case metadata is limited to:
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2181 * 4 * (1 + 8 + 2) + (NUSERFLAGS * (MAXUSERFLAG + 1))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2182 * which comes out to 1994 octets. This is much smaller than the normal
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2183 * CHUNKSIZE definition of 64K, and CHUNKSIZE is the smallest size of
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2184 * LOCAL->buf.
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2185 *
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2186 * If more stuff gets added to the metadata, or if you change the value
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2187 * of NUSERFLAGS, MAXUSERFLAG or CHUNKSIZE, be sure to recalculate the
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2188 * above assertation.
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2189 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2190 sprintf (LOCAL->buf,SEQFMT,LOCAL->metaseq = mix_modseq (LOCAL->metaseq));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2191 sprintf (LOCAL->buf + strlen (LOCAL->buf),MTAFMT,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2192 stream->uid_validity,stream->uid_last,LOCAL->newmsg);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2193 for (i = 0, c = 'K', s = ss = LOCAL->buf + strlen (LOCAL->buf);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2194 (i < NUSERFLAGS) && (t = stream->user_flags[i]); ++i) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2195 if (!*t) fatal ("impossible empty keyword");
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2196 *s++ = c; /* write delimiter */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2197 while (*t) *s++ = *t++; /* write keyword */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2198 c = ' '; /* delimiter is now space */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2199 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2200 if (s != ss) { /* tie off keywords line */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2201 *s++ = '\015'; *s++ = '\012';
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2202 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2203 /* calculate length of metadata */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2204 if ((i = s - LOCAL->buf) > LOCAL->buflen)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2205 fatal ("impossible buffer overflow");
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2206 lseek (LOCAL->mfd,0,L_SET); /* rewind file */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2207 /* write new metadata */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2208 ret = (write (LOCAL->mfd,LOCAL->buf,i) == i) ? LONGT : NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2209 ftruncate (LOCAL->mfd,i); /* and tie off at that point */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2210 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2211 return ret;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2212 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2213
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2214 /* MIX index file routines */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2215
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2216
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2217 /* MIX update index
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2218 * Accepts: MAIL stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2219 * open FILE
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2220 * expansion check flag
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2221 * Returns: T on success, NIL if error
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2222 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2223
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2224 long mix_index_update (MAILSTREAM *stream,FILE *idxf,long flag)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2225 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2226 unsigned long i;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2227 long ret = LONGT;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2228 if (!stream->rdonly) { /* do nothing if stream readonly */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2229 if (flag) { /* need to do expansion check? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2230 char tmp[MAILTMPLEN];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2231 size_t size;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2232 struct stat sbuf;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2233 /* calculate file size we need */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2234 for (i = 1, size = 0; i <= stream->nmsgs; ++i)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2235 if (!mail_elt (stream,i)->private.ghost) ++size;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2236 if (size) { /* Winston Smith's first dairy entry */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2237 sprintf (tmp,IXRFMT,0,14,4,4,13,0,0,'+',0,0,0,0,0,0,0);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2238 size *= strlen (tmp);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2239 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2240 /* calculate file size we need */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2241 sprintf (tmp,SEQFMT,LOCAL->indexseq);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2242 size += strlen (tmp);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2243 /* get current file size */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2244 if (fstat (fileno (idxf),&sbuf)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2245 MM_LOG ("Error getting size of mix index file",ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2246 ret = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2247 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2248 /* need to write additional space? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2249 else if (sbuf.st_size < size) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2250 void *buf = fs_get (size -= sbuf.st_size);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2251 memset (buf,0,size);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2252 if (fseek (idxf,0,SEEK_END) || (fwrite (buf,1,size,idxf) != size) ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2253 fflush (idxf)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2254 fseek (idxf,sbuf.st_size,SEEK_SET);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2255 ftruncate (fileno (idxf),sbuf.st_size);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2256 MM_LOG ("Error extending mix index file",ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2257 ret = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2258 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2259 fs_give ((void **) &buf);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2260 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2261 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2262
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2263 if (ret) { /* if still good to go */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2264 rewind (idxf); /* let's start at the very beginning */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2265 /* write modseq first */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2266 fprintf (idxf,SEQFMT,LOCAL->indexseq);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2267 /* then write all messages */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2268 for (i = 1; ret && (i <= stream->nmsgs); i++) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2269 MESSAGECACHE *elt = mail_elt (stream,i);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2270 if (!elt->private.ghost)/* only write living messages */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2271 fprintf (idxf,IXRFMT,elt->private.uid,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2272 elt->year + BASEYEAR,elt->month,elt->day,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2273 elt->hours,elt->minutes,elt->seconds,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2274 elt->zoccident ? '-' : '+',elt->zhours,elt->zminutes,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2275 elt->rfc822_size,elt->private.spare.data,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2276 elt->private.special.offset,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2277 elt->private.msg.header.offset,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2278 elt->private.msg.header.text.size);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2279 if (ferror (idxf)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2280 MM_LOG ("Error updating mix index file",ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2281 ret = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2282 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2283 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2284 if (fflush (idxf)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2285 MM_LOG ("Error flushing mix index file",ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2286 ret = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2287 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2288 if (ret) ftruncate (fileno (idxf),ftell (idxf));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2289 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2290 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2291 return ret;
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 /* MIX status file routines */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2295
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2296
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2297 /* MIX update status
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2298 * Accepts: MAIL stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2299 * pointer to open FILE
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2300 * expansion check flag
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2301 * Returns: T on success, NIL if error
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2302 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2303
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2304 long mix_status_update (MAILSTREAM *stream,FILE *statf,long flag)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2305 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2306 unsigned long i;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2307 char tmp[MAILTMPLEN];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2308 long ret = LONGT;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2309 if (!stream->rdonly) { /* do nothing if stream readonly */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2310 if (flag) { /* need to do expansion check? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2311 char tmp[MAILTMPLEN];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2312 size_t size;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2313 struct stat sbuf;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2314 /* calculate file size we need */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2315 for (i = 1, size = 0; i <= stream->nmsgs; ++i)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2316 if (!mail_elt (stream,i)->private.ghost) ++size;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2317 if (size) { /* number of living messages */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2318 sprintf (tmp,STRFMT,0,0,0,0);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2319 size *= strlen (tmp);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2320 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2321 sprintf (tmp,SEQFMT,LOCAL->statusseq);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2322 size += strlen (tmp);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2323 /* get current file size */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2324 if (fstat (fileno (statf),&sbuf)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2325 MM_LOG ("Error getting size of mix status file",ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2326 ret = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2327 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2328 /* need to write additional space? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2329 else if (sbuf.st_size < size) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2330 void *buf = fs_get (size -= sbuf.st_size);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2331 memset (buf,0,size);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2332 if (fseek (statf,0,SEEK_END) || (fwrite (buf,1,size,statf) != size) ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2333 fflush (statf)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2334 fseek (statf,sbuf.st_size,SEEK_SET);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2335 ftruncate (fileno (statf),sbuf.st_size);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2336 MM_LOG ("Error extending mix status file",ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2337 ret = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2338 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2339 fs_give ((void **) &buf);
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
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2343 if (ret) { /* if still good to go */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2344 rewind (statf); /* let's start at the very beginning */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2345 /* write sequence */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2346 fprintf (statf,SEQFMT,LOCAL->statusseq);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2347 /* write message status records */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2348 for (i = 1; ret && (i <= stream->nmsgs); ++i) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2349 MESSAGECACHE *elt = mail_elt (stream,i);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2350 /* make sure all messages have a modseq */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2351 if (!elt->private.mod) elt->private.mod = LOCAL->statusseq;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2352 if (!elt->private.ghost)/* only write living messages */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2353 fprintf (statf,STRFMT,elt->private.uid,elt->user_flags,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2354 (fSEEN * elt->seen) + (fDELETED * elt->deleted) +
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2355 (fFLAGGED * elt->flagged) + (fANSWERED * elt->answered) +
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2356 (fDRAFT * elt->draft) + (elt->valid ? fOLD : NIL),
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2357 elt->private.mod);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2358 if (ferror (statf)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2359 sprintf (tmp,"Error updating mix status file: %.80s",
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2360 strerror (errno));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2361 MM_LOG (tmp,ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2362 ret = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2363 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2364 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2365 if (ret && fflush (statf)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2366 MM_LOG ("Error flushing mix status file",ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2367 ret = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2368 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2369 if (ret) ftruncate (fileno (statf),ftell (statf));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2370 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2371 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2372 return ret;
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 /* MIX data file routines */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2376
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2377
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2378 /* MIX open data file
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2379 * Accepts: MAIL stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2380 * pointer to returned fd if success
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2381 * pointer to returned size if success
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2382 * size of new data to be added
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2383 * Returns: open FILE, or NIL if failure
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2384 *
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2385 * The curend test assumes that the last message of the mailbox is the furthest
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2386 * point that the current data file extends, and thus that is all that needs to
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2387 * be tested for short file prevention.
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 FILE *mix_data_open (MAILSTREAM *stream,int *fd,long *size,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2391 unsigned long newsize)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2392 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2393 FILE *msgf = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2394 struct stat sbuf;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2395 MESSAGECACHE *elt = stream->nmsgs ? mail_elt (stream,stream->nmsgs) : NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2396 unsigned long curend = (elt && (elt->private.spare.data == LOCAL->newmsg)) ?
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2397 elt->private.special.offset + elt->private.msg.header.offset +
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2398 elt->rfc822_size : 0;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2399 /* allow create if curend 0 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2400 if ((*fd = open (mix_file_data (LOCAL->buf,stream->mailbox,LOCAL->newmsg),
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2401 O_RDWR | (curend ? NIL : O_CREAT),NIL)) >= 0) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2402 fstat (*fd,&sbuf); /* get current file size */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2403 /* can we use this file? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2404 if ((curend <= sbuf.st_size) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2405 (!sbuf.st_size || ((sbuf.st_size + newsize) <= MIXDATAROLL)))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2406 *size = sbuf.st_size; /* yes, return current size */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2407 else { /* short file or becoming too long */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2408 if (curend > sbuf.st_size) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2409 char tmp[MAILTMPLEN];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2410 sprintf (tmp,"short mix message file %.08lx (%ld > %ld), rolling",
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2411 LOCAL->newmsg,curend,sbuf.st_size);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2412 MM_LOG (tmp,WARN); /* shouldn't happen */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2413 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2414 close (*fd); /* roll to a new file */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2415 while ((*fd = open (mix_file_data
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2416 (LOCAL->buf,stream->mailbox,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2417 LOCAL->newmsg = mix_modseq (LOCAL->newmsg)),
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2418 O_RDWR | O_CREAT | O_EXCL,sbuf.st_mode)) < 0);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2419 *size = 0; /* brand new file */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2420 fchmod (*fd,sbuf.st_mode);/* with same mode as previous file */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2421 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2422 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2423 if (*fd >= 0) { /* have a data file? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2424 /* yes, get stdio and set position */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2425 if (msgf = fdopen (*fd,"r+b")) fseek (msgf,*size,SEEK_SET);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2426 else close (*fd); /* fdopen() failed? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2427 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2428 return msgf; /* return results */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2429 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2430
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2431 /* MIX open sortcache
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2432 * Accepts: MAIL stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2433 * Returns: open FILE, or NIL if failure or could only get readonly sortcache
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2434 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2435
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2436 FILE *mix_sortcache_open (MAILSTREAM *stream)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2437 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2438 int fd,refwd;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2439 unsigned long i,uid,sentdate,fromlen,tolen,cclen,subjlen,msgidlen,reflen;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2440 char *s,*t,*msg,tmp[MAILTMPLEN];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2441 MESSAGECACHE *elt;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2442 SORTCACHE *sc;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2443 STRINGLIST *sl;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2444 struct stat sbuf;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2445 int rdonly = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2446 FILE *srtcf = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2447 mailcache_t mc = (mailcache_t) mail_parameters (NIL,GET_CACHE,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2448 fstat (LOCAL->mfd,&sbuf);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2449 if (!stream->nmsgs); /* do nothing if mailbox empty */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2450 /* open sortcache file */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2451 else if (((fd = open (LOCAL->sortcache,O_RDWR|O_CREAT,sbuf.st_mode)) < 0) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2452 !(rdonly = ((fd = open (LOCAL->sortcache,O_RDONLY,NIL)) >= 0)))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2453 MM_LOG ("Error opening mix sortcache file",WARN);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2454 /* acquire lock and FILE */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2455 else if (!flock (fd,rdonly ? LOCK_SH : LOCK_EX) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2456 !(srtcf = fdopen (fd,rdonly ? "rb" : "r+b"))) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2457 MM_LOG ("Error obtaining stream on mix sortcache file",WARN);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2458 flock (fd,LOCK_UN); /* relinquish lock */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2459 close (fd);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2460 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2461 else if (!(i = mix_read_sequence (srtcf)) || (i < LOCAL->sortcacheseq))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2462 MM_LOG ("Error in mix sortcache file sequence record",WARN);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2463 /* sequence changed from last time? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2464 else if (i > LOCAL->sortcacheseq) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2465 LOCAL->sortcacheseq = i; /* update sequence */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2466 while ((s = t = mix_read_record (srtcf,LOCAL->buf,LOCAL->buflen,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2467 "sortcache")) && *s &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2468 (msg = "uid") && (*s++ == ':') && isxdigit (*s)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2469 uid = strtoul (s,&s,16);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2470 if ((*s++ == ':') && isxdigit (*s)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2471 sentdate = strtoul (s,&s,16);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2472 if ((*s++ == ':') && isxdigit (*s)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2473 fromlen = strtoul (s,&s,16);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2474 if ((*s++ == ':') && isxdigit (*s)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2475 tolen = strtoul (s,&s,16);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2476 if ((*s++ == ':') && isxdigit (*s)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2477 cclen = strtoul (s,&s,16);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2478 if ((*s++ == ':') && ((*s == 'R') || (*s == ' ')) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2479 isxdigit (s[1])) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2480 refwd = (*s++ == 'R') ? T : NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2481 subjlen = strtoul (s,&s,16);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2482 if ((*s++ == ':') && isxdigit (*s)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2483 msgidlen = strtoul (s,&s,16);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2484 if ((*s++ == ':') && isxdigit (*s)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2485 reflen = strtoul (s,&s,16);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2486 /* ignore expansion values */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2487 if (*s++ == ':') {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2488
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2489 if (i = mail_msgno (stream,uid)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2490 sc = (SORTCACHE *) (*mc) (stream,i,CH_SORTCACHE);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2491 sc->size = (elt = mail_elt (stream,i))->rfc822_size;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2492 sc->date = sentdate;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2493 sc->arrival = elt->day ? mail_longdate (elt) : 1;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2494 if (refwd) sc->refwd = T;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2495 if (fromlen) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2496 if (sc->from) fseek (srtcf,fromlen + 2,SEEK_CUR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2497 else if ((getc (srtcf) != 'F') ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2498 (fread (sc->from = (char *) fs_get(fromlen),
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2499 1,fromlen-1,srtcf) != (fromlen-1))||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2500 (sc->from[fromlen-1] = '\0') ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2501 (getc (srtcf) != '\015') ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2502 (getc (srtcf) != '\012')) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2503 msg = "from data";
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2504 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2505 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2506 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2507 if (tolen) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2508 if (sc->to) fseek (srtcf,tolen + 2,SEEK_CUR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2509 else if ((getc (srtcf) != 'T') ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2510 (fread (sc->to = (char *) fs_get (tolen),
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2511 1,tolen-1,srtcf) != (tolen - 1)) ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2512 (sc->to[tolen-1] = '\0') ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2513 (getc (srtcf) != '\015') ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2514 (getc (srtcf) != '\012')) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2515 msg = "to data";
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2516 break;
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 if (cclen) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2520 if (sc->cc) fseek (srtcf,cclen + 2,SEEK_CUR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2521 else if ((getc (srtcf) != 'C') ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2522 (fread (sc->cc = (char *) fs_get (cclen),
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2523 1,cclen-1,srtcf) != (cclen - 1)) ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2524 (sc->cc[cclen-1] = '\0') ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2525 (getc (srtcf) != '\015') ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2526 (getc (srtcf) != '\012')) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2527 msg = "cc data";
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2528 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2529 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2530 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2531 if (subjlen) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2532 if (sc->subject) fseek (srtcf,subjlen + 2,SEEK_CUR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2533 else if ((getc (srtcf) != 'S') ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2534 (fread (sc->subject =
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2535 (char *) fs_get (subjlen),1,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2536 subjlen-1,srtcf) != (subjlen-1))||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2537 (sc->subject[subjlen-1] = '\0') ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2538 (getc (srtcf) != '\015') ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2539 (getc (srtcf) != '\012')) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2540 msg = "subject data";
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2541 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2542 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2543 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2544
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2545 if (msgidlen) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2546 if (sc->message_id)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2547 fseek (srtcf,msgidlen + 2,SEEK_CUR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2548 else if ((getc (srtcf) != 'M') ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2549 (fread (sc->message_id =
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2550 (char *) fs_get (msgidlen),1,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2551 msgidlen-1,srtcf) != (msgidlen-1))||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2552 (sc->message_id[msgidlen-1] = '\0') ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2553 (getc (srtcf) != '\015') ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2554 (getc (srtcf) != '\012')) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2555 msg = "message-id data";
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2556 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2557 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2558 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2559 if (reflen) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2560 if (sc->references) fseek(srtcf,reflen + 2,SEEK_CUR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2561 /* make sure it fits */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2562 else {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2563 if (reflen >= LOCAL->buflen) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2564 fs_give ((void **) &LOCAL->buf);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2565 LOCAL->buf = (char *)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2566 fs_get ((LOCAL->buflen = reflen) + 1);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2567 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2568 if ((getc (srtcf) != 'R') ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2569 (fread (LOCAL->buf,1,reflen-1,srtcf) !=
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2570 (reflen - 1)) ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2571 (LOCAL->buf[reflen-1] = '\0') ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2572 (getc (srtcf) != '\015') ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2573 (getc (srtcf) != '\012')) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2574 msg = "references data";
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2575 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2576 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2577 for (s = LOCAL->buf,sl = NIL,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2578 sc->references = mail_newstringlist ();
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2579 s && *s; s += i + 1) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2580 if ((i = strtoul (s,&s,16)) && (*s++ == ':') &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2581 (s[i] == ':')) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2582 if (sl) sl = sl->next = mail_newstringlist();
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2583 else sl = sc->references;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2584 s[i] = '\0';
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2585 sl->text.data = cpystr (s);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2586 sl->text.size = i;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2587 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2588 else s = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2589 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2590 if (!s || *s ||
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2591 (s != ((char *) LOCAL->buf + reflen - 1))) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2592 msg = "references length consistency check";
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2593 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2594 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2595 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2596 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2597 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2598
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2599 /* UID not found, ignore this message */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2600 else fseek (srtcf,((fromlen ? fromlen + 2 : 0) +
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2601 (tolen ? tolen + 2 : 0) +
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2602 (cclen ? cclen + 2 : 0) +
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2603 (subjlen ? subjlen + 2 : 0) +
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2604 (msgidlen ? msgidlen + 2 : 0) +
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2605 (reflen ? reflen + 2 : 0)),
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2606 SEEK_CUR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2607 continue;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2608 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2609 else msg = "expansion";
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2610 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2611 else msg = "references";
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2612 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2613 else msg = "message-id";
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2614 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2615 else msg = "subject";
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2616 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2617 else msg = "cc";
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2618 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2619 else msg = "to";
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2620 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2621 else msg = "from";
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2622 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2623 else msg = "sentdate";
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2624 break; /* error somewhere */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2625 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2626 if (!t || *t) { /* error detected? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2627 if (t) { /* non-null means bogus record */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2628 sprintf (tmp,"Error in %s in mix sortcache record: %.500s",msg,t);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2629 MM_LOG (tmp,WARN);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2630 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2631 fclose (srtcf); /* either way, must punt */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2632 srtcf = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2633 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2634 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2635 if (rdonly && srtcf) { /* can't update if readonly */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2636 unlink (LOCAL->sortcache); /* try deleting it */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2637 fclose (srtcf); /* so close it and return as if error */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2638 srtcf = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2639 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2640 else fchmod (fd,sbuf.st_mode);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2641 return srtcf;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2642 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2643
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2644 /* MIX update and close sortcache
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2645 * Accepts: MAIL stream
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2646 * pointer to open FILE (if FILE is NIL, do nothing)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2647 * Returns: T on success, NIL on error
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2648 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2649
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2650 long mix_sortcache_update (MAILSTREAM *stream,FILE **sortcache)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2651 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2652 FILE *f = *sortcache;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2653 long ret = LONGT;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2654 if (f) { /* ignore if no file */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2655 unsigned long i,j;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2656 mailcache_t mc = (mailcache_t) mail_parameters (NIL,GET_CACHE,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2657 for (i = 1; (i <= stream->nmsgs) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2658 !((SORTCACHE *) (*mc) (stream,i,CH_SORTCACHE))->dirty; ++i);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2659 if (i <= stream->nmsgs) { /* only update if some entry is dirty */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2660 rewind (f); /* let's start at the very beginning */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2661 /* write sequence */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2662 fprintf (f,SEQFMT,LOCAL->sortcacheseq = mix_modseq(LOCAL->sortcacheseq));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2663 for (i = 1; ret && (i <= stream->nmsgs); ++i) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2664 MESSAGECACHE *elt = mail_elt (stream,i);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2665 SORTCACHE *s = (SORTCACHE *) (*mc) (stream,i,CH_SORTCACHE);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2666 STRINGLIST *sl;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2667 s->dirty = NIL; /* no longer dirty */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2668 if (sl = s->references) /* count length of references */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2669 for (j = 1; sl && sl->text.data; sl = sl->next)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2670 j += 10 + sl->text.size;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2671 else j = 0; /* no references yet */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2672 fprintf (f,SCRFMT,elt->private.uid,s->date,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2673 s->from ? strlen (s->from) + 1 : 0,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2674 s->to ? strlen (s->to) + 1 : 0,s->cc ? strlen (s->cc) + 1 : 0,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2675 s->refwd ? 'R' : ' ',s->subject ? strlen (s->subject) + 1: 0,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2676 s->message_id ? strlen (s->message_id) + 1 : 0,j);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2677 if (s->from) fprintf (f,"F%s\015\012",s->from);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2678 if (s->to) fprintf (f,"T%s\015\012",s->to);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2679 if (s->cc) fprintf (f,"C%s\015\012",s->cc);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2680 if (s->subject) fprintf (f,"S%s\015\012",s->subject);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2681 if (s->message_id) fprintf (f,"M%s\015\012",s->message_id);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2682 if (j) { /* any references to write? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2683 fputc ('R',f); /* yes, do so */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2684 for (sl = s->references; sl && sl->text.data; sl = sl->next)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2685 fprintf (f,"%08lx:%s:",sl->text.size,sl->text.data);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2686 fputs ("\015\012",f);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2687 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2688 if (ferror (f)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2689 MM_LOG ("Error updating mix sortcache file",WARN);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2690 ret = NIL;
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 if (ret && fflush (f)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2694 MM_LOG ("Error flushing mix sortcache file",WARN);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2695 ret = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2696 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2697 if (ret) ftruncate (fileno (f),ftell (f));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2698 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2699 if (fclose (f)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2700 MM_LOG ("Error closing mix sortcache file",WARN);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2701 ret = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2702 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2703 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2704 return ret;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2705 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2706
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2707 /* MIX generic file routines */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2708
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2709 /* MIX read record
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2710 * Accepts: open FILE
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2711 * buffer
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2712 * buffer length
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2713 * record type
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2714 * Returns: buffer if success, else NIL (zero-length buffer means EOF)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2715 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2716
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2717 char *mix_read_record (FILE *f,char *buf,unsigned long buflen,char *type)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2718 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2719 char *s,tmp[MAILTMPLEN];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2720 /* ensure string tied off */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2721 buf[buflen-2] = buf[buflen-1] = '\0';
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2722 while (fgets (buf,buflen-1,f)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2723 if (s = strchr (buf,'\012')) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2724 if ((s != buf) && (s[-1] == '\015')) --s;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2725 *s = '\0'; /* tie off buffer */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2726 if (s != buf) return buf; /* return if non-empty buffer */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2727 sprintf (tmp,"Empty mix %s record",type);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2728 MM_LOG (tmp,WARN);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2729 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2730 else if (buf[buflen-2]) { /* overlong record is bad news */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2731 sprintf (tmp,"Oversize mix %s record: %.512s",type,buf);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2732 MM_LOG (tmp,ERROR);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2733 return NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2734 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2735 else {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2736 sprintf (tmp,"Truncated mix %s record: %.512s",type,buf);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2737 MM_LOG (tmp,WARN);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2738 return buf; /* pass to caller anyway */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2739 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2740 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2741 buf[0] = '\0'; /* return empty buffer on EOF */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2742 return buf;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2743 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2744
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2745 /* MIX read sequence record
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2746 * Accepts: open FILE
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2747 * Returns: sequence value, or NIL if failure
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2748 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2749
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2750 unsigned long mix_read_sequence (FILE *f)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2751 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2752 unsigned long ret;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2753 char *s,tmp[MAILTMPLEN];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2754 if (!mix_read_record (f,tmp,MAILTMPLEN-1,"sequence")) return NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2755 switch (tmp[0]) { /* examine record */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2756 case '\0': /* end of file */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2757 ret = 1; /* start a new sequence regime */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2758 break;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2759 case 'S': /* sequence record */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2760 if (isxdigit (tmp[1])) { /* must be followed by hex value */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2761 ret = strtoul (tmp+1,&s,16);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2762 if (!*s) break; /* and nothing more */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2763 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2764 /* drop into default case */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2765 default: /* anything else is an error */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2766 return NIL; /* return error */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2767 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2768 return ret;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2769 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2770
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2771 /* MIX internal routines */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2772
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2773
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2774 /* MIX mail build directory name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2775 * Accepts: destination string
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2776 * source
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2777 * Returns: destination or empty string if error
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2778 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2779
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2780 char *mix_dir (char *dst,char *name)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2781 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2782 char *s;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2783 /* empty string if mailboxfile fails */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2784 if (!mailboxfile (dst,name)) *dst = '\0';
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2785 /* driver-selected INBOX */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2786 else if (!*dst) mailboxfile (dst,"~/INBOX");
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2787 /* tie off unnecessary trailing / */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2788 else if ((s = strrchr (dst,'/')) && !s[1]) *s = '\0';
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2789 return dst;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2790 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2791
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2792
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2793 /* MIX mail build file name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2794 * Accepts: destination string
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2795 * directory name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2796 * file name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2797 * Returns: destination
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2798 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2799
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2800 char *mix_file (char *dst,char *dir,char *name)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2801 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2802 sprintf (dst,"%.500s/%.80s%.80s",dir,MIXNAME,name);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2803 return dst;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2804 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2805
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2806
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2807 /* MIX mail build file name from data file number
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2808 * Accepts: destination string
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2809 * directory name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2810 * data file number
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2811 * Returns: destination
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2812 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2813
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2814 char *mix_file_data (char *dst,char *dir,unsigned long data)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2815 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2816 char tmp[MAILTMPLEN];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2817 if (data) sprintf (tmp,"%08lx",data);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2818 else tmp[0] = '\0'; /* compatibility with experimental version */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2819 return mix_file (dst,dir,tmp);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2820 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2821
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2822 /* MIX mail get new modseq
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2823 * Accepts: old modseq
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2824 * Returns: new modseq value
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2825 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2826
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2827 unsigned long mix_modseq (unsigned long oldseq)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2828 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2829 /* normally time now */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2830 unsigned long ret = (unsigned long) time (NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2831 /* ensure that modseq doesn't go backwards */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2832 if (ret <= oldseq) ret = oldseq + 1;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2833 return ret;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2834 }

yatex.org