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

yatex.org