rev |
line source |
yuuji@0
|
1 /* ========================================================================
|
yuuji@0
|
2 * Copyright 1988-2008 University of Washington
|
yuuji@0
|
3 *
|
yuuji@0
|
4 * Licensed under the Apache License, Version 2.0 (the "License");
|
yuuji@0
|
5 * you may not use this file except in compliance with the License.
|
yuuji@0
|
6 * You may obtain a copy of the License at
|
yuuji@0
|
7 *
|
yuuji@0
|
8 * http://www.apache.org/licenses/LICENSE-2.0
|
yuuji@0
|
9 *
|
yuuji@0
|
10 *
|
yuuji@0
|
11 * ========================================================================
|
yuuji@0
|
12 */
|
yuuji@0
|
13
|
yuuji@0
|
14 /*
|
yuuji@0
|
15 * Program: Mailbox Access routines
|
yuuji@0
|
16 *
|
yuuji@0
|
17 * Author: Mark Crispin
|
yuuji@0
|
18 * UW Technology
|
yuuji@0
|
19 * University of Washington
|
yuuji@0
|
20 * Seattle, WA 98195
|
yuuji@0
|
21 * Internet: MRC@Washington.EDU
|
yuuji@0
|
22 *
|
yuuji@0
|
23 * Date: 22 November 1989
|
yuuji@0
|
24 * Last Edited: 15 April 2008
|
yuuji@0
|
25 */
|
yuuji@0
|
26
|
yuuji@0
|
27
|
yuuji@0
|
28 #include <ctype.h>
|
yuuji@0
|
29 #include <stdio.h>
|
yuuji@0
|
30 #include <time.h>
|
yuuji@0
|
31 #include "c-client.h"
|
yuuji@0
|
32
|
yuuji@0
|
33 char *UW_copyright = "Copyright 1988-2007 University of Washington\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n";
|
yuuji@0
|
34
|
yuuji@0
|
35 /* c-client global data */
|
yuuji@0
|
36
|
yuuji@0
|
37 /* version of this library */
|
yuuji@0
|
38 static char *mailcclientversion = CCLIENTVERSION;
|
yuuji@0
|
39 /* list of mail drivers */
|
yuuji@0
|
40 static DRIVER *maildrivers = NIL;
|
yuuji@0
|
41 /* list of authenticators */
|
yuuji@0
|
42 static AUTHENTICATOR *mailauthenticators = NIL;
|
yuuji@0
|
43 /* SSL driver pointer */
|
yuuji@0
|
44 static NETDRIVER *mailssldriver = NIL;
|
yuuji@0
|
45 /* pointer to alternate gets function */
|
yuuji@0
|
46 static mailgets_t mailgets = NIL;
|
yuuji@0
|
47 /* pointer to read progress function */
|
yuuji@0
|
48 static readprogress_t mailreadprogress = NIL;
|
yuuji@0
|
49 /* mail cache manipulation function */
|
yuuji@0
|
50 static mailcache_t mailcache = mm_cache;
|
yuuji@0
|
51 /* RFC-822 output generator */
|
yuuji@0
|
52 static rfc822out_t mail822out = NIL;
|
yuuji@0
|
53 /* RFC-822 output generator (new style) */
|
yuuji@0
|
54 static rfc822outfull_t mail822outfull = NIL;
|
yuuji@0
|
55 /* SMTP verbose callback */
|
yuuji@0
|
56 static smtpverbose_t mailsmtpverbose = mm_dlog;
|
yuuji@0
|
57 /* proxy copy routine */
|
yuuji@0
|
58 static mailproxycopy_t mailproxycopy = NIL;
|
yuuji@0
|
59 /* RFC-822 external line parse */
|
yuuji@0
|
60 static parseline_t mailparseline = NIL;
|
yuuji@0
|
61 /* RFC-822 external phrase parser */
|
yuuji@0
|
62 static parsephrase_t mailparsephrase = NIL;
|
yuuji@0
|
63 static kinit_t mailkinit = NIL; /* application kinit callback */
|
yuuji@0
|
64 /* note network sent command */
|
yuuji@0
|
65 static sendcommand_t mailsendcommand = NIL;
|
yuuji@0
|
66 /* newsrc file name decision function */
|
yuuji@0
|
67 static newsrcquery_t mailnewsrcquery = NIL;
|
yuuji@0
|
68 /* ACL results callback */
|
yuuji@0
|
69 static getacl_t mailaclresults = NIL;
|
yuuji@0
|
70 /* list rights results callback */
|
yuuji@0
|
71 static listrights_t maillistrightsresults = NIL;
|
yuuji@0
|
72 /* my rights results callback */
|
yuuji@0
|
73 static myrights_t mailmyrightsresults = NIL;
|
yuuji@0
|
74 /* quota results callback */
|
yuuji@0
|
75 static quota_t mailquotaresults = NIL;
|
yuuji@0
|
76 /* quota root results callback */
|
yuuji@0
|
77 static quotaroot_t mailquotarootresults = NIL;
|
yuuji@0
|
78 /* sorted results callback */
|
yuuji@0
|
79 static sortresults_t mailsortresults = NIL;
|
yuuji@0
|
80 /* threaded results callback */
|
yuuji@0
|
81 static threadresults_t mailthreadresults = NIL;
|
yuuji@0
|
82 /* COPY UID results */
|
yuuji@0
|
83 static copyuid_t mailcopyuid = NIL;
|
yuuji@0
|
84 /* APPEND UID results */
|
yuuji@0
|
85 static appenduid_t mailappenduid = NIL;
|
yuuji@0
|
86 /* free elt extra stuff callback */
|
yuuji@0
|
87 static freeeltsparep_t mailfreeeltsparep = NIL;
|
yuuji@0
|
88 /* free envelope extra stuff callback */
|
yuuji@0
|
89 static freeenvelopesparep_t mailfreeenvelopesparep = NIL;
|
yuuji@0
|
90 /* free body extra stuff callback */
|
yuuji@0
|
91 static freebodysparep_t mailfreebodysparep = NIL;
|
yuuji@0
|
92 /* free stream extra stuff callback */
|
yuuji@0
|
93 static freestreamsparep_t mailfreestreamsparep = NIL;
|
yuuji@0
|
94 /* SSL start routine */
|
yuuji@0
|
95 static sslstart_t mailsslstart = NIL;
|
yuuji@0
|
96 /* SSL certificate query */
|
yuuji@0
|
97 static sslcertificatequery_t mailsslcertificatequery = NIL;
|
yuuji@0
|
98 /* SSL client certificate */
|
yuuji@0
|
99 static sslclientcert_t mailsslclientcert = NIL;
|
yuuji@0
|
100 /* SSL client private key */
|
yuuji@0
|
101 static sslclientkey_t mailsslclientkey = NIL;
|
yuuji@0
|
102 /* SSL failure notify */
|
yuuji@0
|
103 static sslfailure_t mailsslfailure = NIL;
|
yuuji@0
|
104 /* snarf interval */
|
yuuji@0
|
105 static long mailsnarfinterval = 60;
|
yuuji@0
|
106 /* snarf preservation */
|
yuuji@0
|
107 static long mailsnarfpreserve = NIL;
|
yuuji@0
|
108 /* newsrc name uses canonical host */
|
yuuji@0
|
109 static long mailnewsrccanon = LONGT;
|
yuuji@0
|
110
|
yuuji@0
|
111 /* supported threaders */
|
yuuji@0
|
112 static THREADER mailthreadordsub = {
|
yuuji@0
|
113 "ORDEREDSUBJECT",mail_thread_orderedsubject,NIL
|
yuuji@0
|
114 };
|
yuuji@0
|
115 static THREADER mailthreadlist = {
|
yuuji@0
|
116 "REFERENCES",mail_thread_references,&mailthreadordsub
|
yuuji@0
|
117 };
|
yuuji@0
|
118
|
yuuji@0
|
119 /* server name */
|
yuuji@0
|
120 static char *servicename = "unknown";
|
yuuji@0
|
121 /* server externally-set authentication ID */
|
yuuji@0
|
122 static char *externalauthid = NIL;
|
yuuji@0
|
123 static int expungeatping = T; /* mail_ping() may call mm_expunged() */
|
yuuji@0
|
124 static int trysslfirst = NIL; /* always try SSL first */
|
yuuji@0
|
125 static int notimezones = NIL; /* write timezones in "From " header */
|
yuuji@0
|
126 static int trustdns = T; /* do DNS canonicalization */
|
yuuji@0
|
127 static int saslusesptrname = T; /* SASL uses name from DNS PTR lookup */
|
yuuji@0
|
128 /* trustdns also must be set */
|
yuuji@0
|
129 static int debugsensitive = NIL;/* debug telemetry includes sensitive data */
|
yuuji@0
|
130
|
yuuji@0
|
131 /* Default mail cache handler
|
yuuji@0
|
132 * Accepts: pointer to cache handle
|
yuuji@0
|
133 * message number
|
yuuji@0
|
134 * caching function
|
yuuji@0
|
135 * Returns: cache data
|
yuuji@0
|
136 */
|
yuuji@0
|
137
|
yuuji@0
|
138 void *mm_cache (MAILSTREAM *stream,unsigned long msgno,long op)
|
yuuji@0
|
139 {
|
yuuji@0
|
140 size_t n;
|
yuuji@0
|
141 void *ret = NIL;
|
yuuji@0
|
142 unsigned long i;
|
yuuji@0
|
143 switch ((int) op) { /* what function? */
|
yuuji@0
|
144 case CH_INIT: /* initialize cache */
|
yuuji@0
|
145 if (stream->cache) { /* flush old cache contents */
|
yuuji@0
|
146 while (stream->cachesize) {
|
yuuji@0
|
147 mm_cache (stream,stream->cachesize,CH_FREE);
|
yuuji@0
|
148 mm_cache (stream,stream->cachesize--,CH_FREESORTCACHE);
|
yuuji@0
|
149 }
|
yuuji@0
|
150 fs_give ((void **) &stream->cache);
|
yuuji@0
|
151 fs_give ((void **) &stream->sc);
|
yuuji@0
|
152 stream->nmsgs = 0; /* can't have any messages now */
|
yuuji@0
|
153 }
|
yuuji@0
|
154 break;
|
yuuji@0
|
155 case CH_SIZE: /* (re-)size the cache */
|
yuuji@0
|
156 if (!stream->cache) { /* have a cache already? */
|
yuuji@0
|
157 /* no, create new cache */
|
yuuji@0
|
158 n = (stream->cachesize = msgno + CACHEINCREMENT) * sizeof (void *);
|
yuuji@0
|
159 stream->cache = (MESSAGECACHE **) memset (fs_get (n),0,n);
|
yuuji@0
|
160 stream->sc = (SORTCACHE **) memset (fs_get (n),0,n);
|
yuuji@0
|
161 }
|
yuuji@0
|
162 /* is existing cache size large neough */
|
yuuji@0
|
163 else if (msgno > stream->cachesize) {
|
yuuji@0
|
164 i = stream->cachesize; /* remember old size */
|
yuuji@0
|
165 n = (stream->cachesize = msgno + CACHEINCREMENT) * sizeof (void *);
|
yuuji@0
|
166 fs_resize ((void **) &stream->cache,n);
|
yuuji@0
|
167 fs_resize ((void **) &stream->sc,n);
|
yuuji@0
|
168 while (i < stream->cachesize) {
|
yuuji@0
|
169 stream->cache[i] = NIL;
|
yuuji@0
|
170 stream->sc[i++] = NIL;
|
yuuji@0
|
171 }
|
yuuji@0
|
172 }
|
yuuji@0
|
173 break;
|
yuuji@0
|
174
|
yuuji@0
|
175 case CH_MAKEELT: /* return elt, make if necessary */
|
yuuji@0
|
176 if (!stream->cache[msgno - 1])
|
yuuji@0
|
177 stream->cache[msgno - 1] = mail_new_cache_elt (msgno);
|
yuuji@0
|
178 /* falls through */
|
yuuji@0
|
179 case CH_ELT: /* return elt */
|
yuuji@0
|
180 ret = (void *) stream->cache[msgno - 1];
|
yuuji@0
|
181 break;
|
yuuji@0
|
182 case CH_SORTCACHE: /* return sortcache entry, make if needed */
|
yuuji@0
|
183 if (!stream->sc[msgno - 1]) stream->sc[msgno - 1] =
|
yuuji@0
|
184 (SORTCACHE *) memset (fs_get (sizeof (SORTCACHE)),0,sizeof (SORTCACHE));
|
yuuji@0
|
185 ret = (void *) stream->sc[msgno - 1];
|
yuuji@0
|
186 break;
|
yuuji@0
|
187 case CH_FREE: /* free elt */
|
yuuji@0
|
188 mail_free_elt (&stream->cache[msgno - 1]);
|
yuuji@0
|
189 break;
|
yuuji@0
|
190 case CH_FREESORTCACHE:
|
yuuji@0
|
191 if (stream->sc[msgno - 1]) {
|
yuuji@0
|
192 if (stream->sc[msgno - 1]->from)
|
yuuji@0
|
193 fs_give ((void **) &stream->sc[msgno - 1]->from);
|
yuuji@0
|
194 if (stream->sc[msgno - 1]->to)
|
yuuji@0
|
195 fs_give ((void **) &stream->sc[msgno - 1]->to);
|
yuuji@0
|
196 if (stream->sc[msgno - 1]->cc)
|
yuuji@0
|
197 fs_give ((void **) &stream->sc[msgno - 1]->cc);
|
yuuji@0
|
198 if (stream->sc[msgno - 1]->subject)
|
yuuji@0
|
199 fs_give ((void **) &stream->sc[msgno - 1]->subject);
|
yuuji@0
|
200 if (stream->sc[msgno - 1]->unique &&
|
yuuji@0
|
201 (stream->sc[msgno - 1]->unique != stream->sc[msgno - 1]->message_id))
|
yuuji@0
|
202 fs_give ((void **) &stream->sc[msgno - 1]->unique);
|
yuuji@0
|
203 if (stream->sc[msgno - 1]->message_id)
|
yuuji@0
|
204 fs_give ((void **) &stream->sc[msgno - 1]->message_id);
|
yuuji@0
|
205 if (stream->sc[msgno - 1]->references)
|
yuuji@0
|
206 mail_free_stringlist (&stream->sc[msgno - 1]->references);
|
yuuji@0
|
207 fs_give ((void **) &stream->sc[msgno - 1]);
|
yuuji@0
|
208 }
|
yuuji@0
|
209 break;
|
yuuji@0
|
210 case CH_EXPUNGE: /* expunge cache slot */
|
yuuji@0
|
211 for (i = msgno - 1; msgno < stream->nmsgs; i++,msgno++) {
|
yuuji@0
|
212 if (stream->cache[i] = stream->cache[msgno])
|
yuuji@0
|
213 stream->cache[i]->msgno = msgno;
|
yuuji@0
|
214 stream->sc[i] = stream->sc[msgno];
|
yuuji@0
|
215 }
|
yuuji@0
|
216 stream->cache[i] = NIL; /* top of cache goes away */
|
yuuji@0
|
217 stream->sc[i] = NIL;
|
yuuji@0
|
218 break;
|
yuuji@0
|
219 default:
|
yuuji@0
|
220 fatal ("Bad mm_cache op");
|
yuuji@0
|
221 break;
|
yuuji@0
|
222 }
|
yuuji@0
|
223 return ret;
|
yuuji@0
|
224 }
|
yuuji@0
|
225
|
yuuji@0
|
226 /* Dummy string driver for complete in-memory strings */
|
yuuji@0
|
227
|
yuuji@0
|
228 static void mail_string_init (STRING *s,void *data,unsigned long size);
|
yuuji@0
|
229 static char mail_string_next (STRING *s);
|
yuuji@0
|
230 static void mail_string_setpos (STRING *s,unsigned long i);
|
yuuji@0
|
231
|
yuuji@0
|
232 STRINGDRIVER mail_string = {
|
yuuji@0
|
233 mail_string_init, /* initialize string structure */
|
yuuji@0
|
234 mail_string_next, /* get next byte in string structure */
|
yuuji@0
|
235 mail_string_setpos /* set position in string structure */
|
yuuji@0
|
236 };
|
yuuji@0
|
237
|
yuuji@0
|
238
|
yuuji@0
|
239 /* Initialize mail string structure for in-memory string
|
yuuji@0
|
240 * Accepts: string structure
|
yuuji@0
|
241 * pointer to string
|
yuuji@0
|
242 * size of string
|
yuuji@0
|
243 */
|
yuuji@0
|
244
|
yuuji@0
|
245 static void mail_string_init (STRING *s,void *data,unsigned long size)
|
yuuji@0
|
246 {
|
yuuji@0
|
247 /* set initial string pointers */
|
yuuji@0
|
248 s->chunk = s->curpos = (char *) (s->data = data);
|
yuuji@0
|
249 /* and sizes */
|
yuuji@0
|
250 s->size = s->chunksize = s->cursize = size;
|
yuuji@0
|
251 s->data1 = s->offset = 0; /* never any offset */
|
yuuji@0
|
252 }
|
yuuji@0
|
253
|
yuuji@0
|
254
|
yuuji@0
|
255 /* Get next character from string
|
yuuji@0
|
256 * Accepts: string structure
|
yuuji@0
|
257 * Returns: character, string structure chunk refreshed
|
yuuji@0
|
258 */
|
yuuji@0
|
259
|
yuuji@0
|
260 static char mail_string_next (STRING *s)
|
yuuji@0
|
261 {
|
yuuji@0
|
262 return *s->curpos++; /* return the last byte */
|
yuuji@0
|
263 }
|
yuuji@0
|
264
|
yuuji@0
|
265
|
yuuji@0
|
266 /* Set string pointer position
|
yuuji@0
|
267 * Accepts: string structure
|
yuuji@0
|
268 * new position
|
yuuji@0
|
269 */
|
yuuji@0
|
270
|
yuuji@0
|
271 static void mail_string_setpos (STRING *s,unsigned long i)
|
yuuji@0
|
272 {
|
yuuji@0
|
273 s->curpos = s->chunk + i; /* set new position */
|
yuuji@0
|
274 s->cursize = s->chunksize - i;/* and new size */
|
yuuji@0
|
275 }
|
yuuji@0
|
276
|
yuuji@0
|
277 /* Mail routines
|
yuuji@0
|
278 *
|
yuuji@0
|
279 * mail_xxx routines are the interface between this module and the outside
|
yuuji@0
|
280 * world. Only these routines should be referenced by external callers.
|
yuuji@0
|
281 *
|
yuuji@0
|
282 * Note that there is an important difference between a "sequence" and a
|
yuuji@0
|
283 * "message #" (msgno). A sequence is a string representing a sequence in
|
yuuji@0
|
284 * {"n", "n:m", or combination separated by commas} format, whereas a msgno
|
yuuji@0
|
285 * is a single integer.
|
yuuji@0
|
286 *
|
yuuji@0
|
287 */
|
yuuji@0
|
288
|
yuuji@0
|
289 /* Mail version check
|
yuuji@0
|
290 * Accepts: version
|
yuuji@0
|
291 */
|
yuuji@0
|
292
|
yuuji@0
|
293 void mail_versioncheck (char *version)
|
yuuji@0
|
294 {
|
yuuji@0
|
295 /* attempt to protect again wrong .h */
|
yuuji@0
|
296 if (strcmp (version,mailcclientversion)) {
|
yuuji@0
|
297 char tmp[MAILTMPLEN];
|
yuuji@0
|
298 sprintf (tmp,"c-client library version skew, app=%.100s library=%.100s",
|
yuuji@0
|
299 version,mailcclientversion);
|
yuuji@0
|
300 fatal (tmp);
|
yuuji@0
|
301 }
|
yuuji@0
|
302 }
|
yuuji@0
|
303
|
yuuji@0
|
304
|
yuuji@0
|
305 /* Mail link driver
|
yuuji@0
|
306 * Accepts: driver to add to list
|
yuuji@0
|
307 */
|
yuuji@0
|
308
|
yuuji@0
|
309 void mail_link (DRIVER *driver)
|
yuuji@0
|
310 {
|
yuuji@0
|
311 DRIVER **d = &maildrivers;
|
yuuji@0
|
312 while (*d) d = &(*d)->next; /* find end of list of drivers */
|
yuuji@0
|
313 *d = driver; /* put driver at the end */
|
yuuji@0
|
314 driver->next = NIL; /* this driver is the end of the list */
|
yuuji@0
|
315 }
|
yuuji@0
|
316
|
yuuji@0
|
317 /* Mail manipulate driver parameters
|
yuuji@0
|
318 * Accepts: mail stream
|
yuuji@0
|
319 * function code
|
yuuji@0
|
320 * function-dependent value
|
yuuji@0
|
321 * Returns: function-dependent return value
|
yuuji@0
|
322 */
|
yuuji@0
|
323
|
yuuji@0
|
324 void *mail_parameters (MAILSTREAM *stream,long function,void *value)
|
yuuji@0
|
325 {
|
yuuji@0
|
326 void *r,*ret = NIL;
|
yuuji@0
|
327 DRIVER *d;
|
yuuji@0
|
328 AUTHENTICATOR *a;
|
yuuji@0
|
329 switch ((int) function) {
|
yuuji@0
|
330 case SET_INBOXPATH:
|
yuuji@0
|
331 fatal ("SET_INBOXPATH not permitted");
|
yuuji@0
|
332 case GET_INBOXPATH:
|
yuuji@0
|
333 if ((stream || (stream = mail_open (NIL,"INBOX",OP_PROTOTYPE))) &&
|
yuuji@0
|
334 stream->dtb) ret = (*stream->dtb->parameters) (function,value);
|
yuuji@0
|
335 break;
|
yuuji@0
|
336 case SET_THREADERS:
|
yuuji@0
|
337 fatal ("SET_THREADERS not permitted");
|
yuuji@0
|
338 case GET_THREADERS: /* use stream dtb instead of global */
|
yuuji@0
|
339 ret = (stream && stream->dtb) ?
|
yuuji@0
|
340 /* KLUDGE ALERT: note stream passed as value */
|
yuuji@0
|
341 (*stream->dtb->parameters) (function,stream) : (void *) &mailthreadlist;
|
yuuji@0
|
342 break;
|
yuuji@0
|
343 case SET_NAMESPACE:
|
yuuji@0
|
344 fatal ("SET_NAMESPACE not permitted");
|
yuuji@0
|
345 break;
|
yuuji@0
|
346 case SET_NEWSRC: /* too late on open stream */
|
yuuji@0
|
347 if (stream && stream->dtb && (stream != ((*stream->dtb->open) (NIL))))
|
yuuji@0
|
348 fatal ("SET_NEWSRC not permitted");
|
yuuji@0
|
349 else ret = env_parameters (function,value);
|
yuuji@0
|
350 break;
|
yuuji@0
|
351 case GET_NAMESPACE:
|
yuuji@0
|
352 case GET_NEWSRC: /* use stream dtb instead of environment */
|
yuuji@0
|
353 ret = (stream && stream->dtb) ?
|
yuuji@0
|
354 /* KLUDGE ALERT: note stream passed as value */
|
yuuji@0
|
355 (*stream->dtb->parameters) (function,stream) :
|
yuuji@0
|
356 env_parameters (function,value);
|
yuuji@0
|
357 break;
|
yuuji@0
|
358 case ENABLE_DEBUG:
|
yuuji@0
|
359 fatal ("ENABLE_DEBUG not permitted");
|
yuuji@0
|
360 case DISABLE_DEBUG:
|
yuuji@0
|
361 fatal ("DISABLE_DEBUG not permitted");
|
yuuji@0
|
362 case SET_DIRFMTTEST:
|
yuuji@0
|
363 fatal ("SET_DIRFMTTEST not permitted");
|
yuuji@0
|
364 case GET_DIRFMTTEST:
|
yuuji@0
|
365 if (!(stream && stream->dtb &&
|
yuuji@0
|
366 (ret = (*stream->dtb->parameters) (function,NIL))))
|
yuuji@0
|
367 fatal ("GET_DIRFMTTEST not permitted");
|
yuuji@0
|
368 break;
|
yuuji@0
|
369
|
yuuji@0
|
370 case SET_DRIVERS:
|
yuuji@0
|
371 fatal ("SET_DRIVERS not permitted");
|
yuuji@0
|
372 case GET_DRIVERS: /* always return global */
|
yuuji@0
|
373 ret = (void *) maildrivers;
|
yuuji@0
|
374 break;
|
yuuji@0
|
375 case SET_DRIVER:
|
yuuji@0
|
376 fatal ("SET_DRIVER not permitted");
|
yuuji@0
|
377 case GET_DRIVER:
|
yuuji@0
|
378 for (d = maildrivers; d && compare_cstring (d->name,(char *) value);
|
yuuji@0
|
379 d = d->next);
|
yuuji@0
|
380 ret = (void *) d;
|
yuuji@0
|
381 break;
|
yuuji@0
|
382 case ENABLE_DRIVER:
|
yuuji@0
|
383 for (d = maildrivers; d && compare_cstring (d->name,(char *) value);
|
yuuji@0
|
384 d = d->next);
|
yuuji@0
|
385 if (ret = (void *) d) d->flags &= ~DR_DISABLE;
|
yuuji@0
|
386 break;
|
yuuji@0
|
387 case DISABLE_DRIVER:
|
yuuji@0
|
388 for (d = maildrivers; d && compare_cstring (d->name,(char *) value);
|
yuuji@0
|
389 d = d->next);
|
yuuji@0
|
390 if (ret = (void *) d) d->flags |= DR_DISABLE;
|
yuuji@0
|
391 break;
|
yuuji@0
|
392 case ENABLE_AUTHENTICATOR:
|
yuuji@0
|
393 for (a = mailauthenticators;/* scan authenticators */
|
yuuji@0
|
394 a && compare_cstring (a->name,(char *) value); a = a->next);
|
yuuji@0
|
395 if (ret = (void *) a) a->flags &= ~AU_DISABLE;
|
yuuji@0
|
396 break;
|
yuuji@0
|
397 case DISABLE_AUTHENTICATOR:
|
yuuji@0
|
398 for (a = mailauthenticators;/* scan authenticators */
|
yuuji@0
|
399 a && compare_cstring (a->name,(char *) value); a = a->next);
|
yuuji@0
|
400 if (ret = (void *) a) a->flags |= AU_DISABLE;
|
yuuji@0
|
401 break;
|
yuuji@0
|
402 case UNHIDE_AUTHENTICATOR:
|
yuuji@0
|
403 for (a = mailauthenticators;/* scan authenticators */
|
yuuji@0
|
404 a && compare_cstring (a->name,(char *) value); a = a->next);
|
yuuji@0
|
405 if (ret = (void *) a) a->flags &= ~AU_HIDE;
|
yuuji@0
|
406 break;
|
yuuji@0
|
407 case HIDE_AUTHENTICATOR:
|
yuuji@0
|
408 for (a = mailauthenticators;/* scan authenticators */
|
yuuji@0
|
409 a && compare_cstring (a->name,(char *) value); a = a->next);
|
yuuji@0
|
410 if (ret = (void *) a) a->flags |= AU_HIDE;
|
yuuji@0
|
411 break;
|
yuuji@0
|
412 case SET_EXTERNALAUTHID:
|
yuuji@0
|
413 if (value) { /* setting external authentication ID */
|
yuuji@0
|
414 externalauthid = cpystr ((char *) value);
|
yuuji@0
|
415 mail_parameters (NIL,UNHIDE_AUTHENTICATOR,"EXTERNAL");
|
yuuji@0
|
416 }
|
yuuji@0
|
417 else { /* clearing external authentication ID */
|
yuuji@0
|
418 if (externalauthid) fs_give ((void **) &externalauthid);
|
yuuji@0
|
419 mail_parameters (NIL,HIDE_AUTHENTICATOR,"EXTERNAL");
|
yuuji@0
|
420 }
|
yuuji@0
|
421 case GET_EXTERNALAUTHID:
|
yuuji@0
|
422 ret = (void *) externalauthid;
|
yuuji@0
|
423 break;
|
yuuji@0
|
424
|
yuuji@0
|
425 case SET_GETS:
|
yuuji@0
|
426 mailgets = (mailgets_t) value;
|
yuuji@0
|
427 case GET_GETS:
|
yuuji@0
|
428 ret = (void *) mailgets;
|
yuuji@0
|
429 break;
|
yuuji@0
|
430 case SET_READPROGRESS:
|
yuuji@0
|
431 mailreadprogress = (readprogress_t) value;
|
yuuji@0
|
432 case GET_READPROGRESS:
|
yuuji@0
|
433 ret = (void *) mailreadprogress;
|
yuuji@0
|
434 break;
|
yuuji@0
|
435 case SET_CACHE:
|
yuuji@0
|
436 mailcache = (mailcache_t) value;
|
yuuji@0
|
437 case GET_CACHE:
|
yuuji@0
|
438 ret = (void *) mailcache;
|
yuuji@0
|
439 break;
|
yuuji@0
|
440 case SET_RFC822OUTPUT:
|
yuuji@0
|
441 mail822out = (rfc822out_t) value;
|
yuuji@0
|
442 case GET_RFC822OUTPUT:
|
yuuji@0
|
443 ret = (void *) mail822out;
|
yuuji@0
|
444 break;
|
yuuji@0
|
445 case SET_RFC822OUTPUTFULL:
|
yuuji@0
|
446 mail822outfull = (rfc822outfull_t) value;
|
yuuji@0
|
447 case GET_RFC822OUTPUTFULL:
|
yuuji@0
|
448 ret = (void *) mail822outfull;
|
yuuji@0
|
449 break;
|
yuuji@0
|
450 case SET_SMTPVERBOSE:
|
yuuji@0
|
451 mailsmtpverbose = (smtpverbose_t) value;
|
yuuji@0
|
452 case GET_SMTPVERBOSE:
|
yuuji@0
|
453 ret = (void *) mailsmtpverbose;
|
yuuji@0
|
454 break;
|
yuuji@0
|
455 case SET_MAILPROXYCOPY:
|
yuuji@0
|
456 mailproxycopy = (mailproxycopy_t) value;
|
yuuji@0
|
457 case GET_MAILPROXYCOPY:
|
yuuji@0
|
458 ret = (void *) mailproxycopy;
|
yuuji@0
|
459 break;
|
yuuji@0
|
460 case SET_PARSELINE:
|
yuuji@0
|
461 mailparseline = (parseline_t) value;
|
yuuji@0
|
462 case GET_PARSELINE:
|
yuuji@0
|
463 ret = (void *) mailparseline;
|
yuuji@0
|
464 break;
|
yuuji@0
|
465 case SET_PARSEPHRASE:
|
yuuji@0
|
466 mailparsephrase = (parsephrase_t) value;
|
yuuji@0
|
467 case GET_PARSEPHRASE:
|
yuuji@0
|
468 ret = (void *) mailparsephrase;
|
yuuji@0
|
469 break;
|
yuuji@0
|
470 case SET_NEWSRCQUERY:
|
yuuji@0
|
471 mailnewsrcquery = (newsrcquery_t) value;
|
yuuji@0
|
472 case GET_NEWSRCQUERY:
|
yuuji@0
|
473 ret = (void *) mailnewsrcquery;
|
yuuji@0
|
474 break;
|
yuuji@0
|
475 case SET_NEWSRCCANONHOST:
|
yuuji@0
|
476 mailnewsrccanon = (long) value;
|
yuuji@0
|
477 case GET_NEWSRCCANONHOST:
|
yuuji@0
|
478 ret = (void *) mailnewsrccanon;
|
yuuji@0
|
479 break;
|
yuuji@0
|
480
|
yuuji@0
|
481 case SET_COPYUID:
|
yuuji@0
|
482 mailcopyuid = (copyuid_t) value;
|
yuuji@0
|
483 case GET_COPYUID:
|
yuuji@0
|
484 ret = (void *) mailcopyuid;
|
yuuji@0
|
485 break;
|
yuuji@0
|
486 case SET_APPENDUID:
|
yuuji@0
|
487 mailappenduid = (appenduid_t) value;
|
yuuji@0
|
488 case GET_APPENDUID:
|
yuuji@0
|
489 ret = (void *) mailappenduid;
|
yuuji@0
|
490 break;
|
yuuji@0
|
491 case SET_FREEENVELOPESPAREP:
|
yuuji@0
|
492 mailfreeenvelopesparep = (freeenvelopesparep_t) value;
|
yuuji@0
|
493 case GET_FREEENVELOPESPAREP:
|
yuuji@0
|
494 ret = (void *) mailfreeenvelopesparep;
|
yuuji@0
|
495 break;
|
yuuji@0
|
496 case SET_FREEELTSPAREP:
|
yuuji@0
|
497 mailfreeeltsparep = (freeeltsparep_t) value;
|
yuuji@0
|
498 case GET_FREEELTSPAREP:
|
yuuji@0
|
499 ret = (void *) mailfreeeltsparep;
|
yuuji@0
|
500 break;
|
yuuji@0
|
501 case SET_FREESTREAMSPAREP:
|
yuuji@0
|
502 mailfreestreamsparep = (freestreamsparep_t) value;
|
yuuji@0
|
503 case GET_FREESTREAMSPAREP:
|
yuuji@0
|
504 ret = (void *) mailfreestreamsparep;
|
yuuji@0
|
505 break;
|
yuuji@0
|
506 case SET_FREEBODYSPAREP:
|
yuuji@0
|
507 mailfreebodysparep = (freebodysparep_t) value;
|
yuuji@0
|
508 case GET_FREEBODYSPAREP:
|
yuuji@0
|
509 ret = (void *) mailfreebodysparep;
|
yuuji@0
|
510 break;
|
yuuji@0
|
511
|
yuuji@0
|
512 case SET_SSLSTART:
|
yuuji@0
|
513 mailsslstart = (sslstart_t) value;
|
yuuji@0
|
514 case GET_SSLSTART:
|
yuuji@0
|
515 ret = (void *) mailsslstart;
|
yuuji@0
|
516 break;
|
yuuji@0
|
517 case SET_SSLCERTIFICATEQUERY:
|
yuuji@0
|
518 mailsslcertificatequery = (sslcertificatequery_t) value;
|
yuuji@0
|
519 case GET_SSLCERTIFICATEQUERY:
|
yuuji@0
|
520 ret = (void *) mailsslcertificatequery;
|
yuuji@0
|
521 break;
|
yuuji@0
|
522 case SET_SSLCLIENTCERT:
|
yuuji@0
|
523 mailsslclientcert = (sslclientcert_t) value;
|
yuuji@0
|
524 case GET_SSLCLIENTCERT:
|
yuuji@0
|
525 ret = (void *) mailsslclientcert;
|
yuuji@0
|
526 break;
|
yuuji@0
|
527 case SET_SSLCLIENTKEY:
|
yuuji@0
|
528 mailsslclientkey = (sslclientkey_t) value;
|
yuuji@0
|
529 case GET_SSLCLIENTKEY:
|
yuuji@0
|
530 ret = (void *) mailsslclientkey;
|
yuuji@0
|
531 break;
|
yuuji@0
|
532 case SET_SSLFAILURE:
|
yuuji@0
|
533 mailsslfailure = (sslfailure_t) value;
|
yuuji@0
|
534 case GET_SSLFAILURE:
|
yuuji@0
|
535 ret = (void *) mailsslfailure;
|
yuuji@0
|
536 break;
|
yuuji@0
|
537 case SET_KINIT:
|
yuuji@0
|
538 mailkinit = (kinit_t) value;
|
yuuji@0
|
539 case GET_KINIT:
|
yuuji@0
|
540 ret = (void *) mailkinit;
|
yuuji@0
|
541 break;
|
yuuji@0
|
542 case SET_SENDCOMMAND:
|
yuuji@0
|
543 mailsendcommand = (sendcommand_t) value;
|
yuuji@0
|
544 case GET_SENDCOMMAND:
|
yuuji@0
|
545 ret = (void *) mailsendcommand;
|
yuuji@0
|
546 break;
|
yuuji@0
|
547
|
yuuji@0
|
548 case SET_SERVICENAME:
|
yuuji@0
|
549 servicename = (char *) value;
|
yuuji@0
|
550 case GET_SERVICENAME:
|
yuuji@0
|
551 ret = (void *) servicename;
|
yuuji@0
|
552 break;
|
yuuji@0
|
553 case SET_EXPUNGEATPING:
|
yuuji@0
|
554 expungeatping = (value ? T : NIL);
|
yuuji@0
|
555 case GET_EXPUNGEATPING:
|
yuuji@0
|
556 ret = (void *) (expungeatping ? VOIDT : NIL);
|
yuuji@0
|
557 break;
|
yuuji@0
|
558 case SET_SORTRESULTS:
|
yuuji@0
|
559 mailsortresults = (sortresults_t) value;
|
yuuji@0
|
560 case GET_SORTRESULTS:
|
yuuji@0
|
561 ret = (void *) mailsortresults;
|
yuuji@0
|
562 break;
|
yuuji@0
|
563 case SET_THREADRESULTS:
|
yuuji@0
|
564 mailthreadresults = (threadresults_t) value;
|
yuuji@0
|
565 case GET_THREADRESULTS:
|
yuuji@0
|
566 ret = (void *) mailthreadresults;
|
yuuji@0
|
567 break;
|
yuuji@0
|
568 case SET_SSLDRIVER:
|
yuuji@0
|
569 mailssldriver = (NETDRIVER *) value;
|
yuuji@0
|
570 case GET_SSLDRIVER:
|
yuuji@0
|
571 ret = (void *) mailssldriver;
|
yuuji@0
|
572 break;
|
yuuji@0
|
573 case SET_TRYSSLFIRST:
|
yuuji@0
|
574 trysslfirst = (value ? T : NIL);
|
yuuji@0
|
575 case GET_TRYSSLFIRST:
|
yuuji@0
|
576 ret = (void *) (trysslfirst ? VOIDT : NIL);
|
yuuji@0
|
577 break;
|
yuuji@0
|
578 case SET_NOTIMEZONES:
|
yuuji@0
|
579 notimezones = (value ? T : NIL);
|
yuuji@0
|
580 case GET_NOTIMEZONES:
|
yuuji@0
|
581 ret = (void *) (notimezones ? VOIDT : NIL);
|
yuuji@0
|
582 break;
|
yuuji@0
|
583 case SET_TRUSTDNS:
|
yuuji@0
|
584 trustdns = (value ? T : NIL);
|
yuuji@0
|
585 case GET_TRUSTDNS:
|
yuuji@0
|
586 ret = (void *) (trustdns ? VOIDT : NIL);
|
yuuji@0
|
587 break;
|
yuuji@0
|
588 case SET_SASLUSESPTRNAME:
|
yuuji@0
|
589 saslusesptrname = (value ? T : NIL);
|
yuuji@0
|
590 case GET_SASLUSESPTRNAME:
|
yuuji@0
|
591 ret = (void *) (saslusesptrname ? VOIDT : NIL);
|
yuuji@0
|
592 break;
|
yuuji@0
|
593 case SET_DEBUGSENSITIVE:
|
yuuji@0
|
594 debugsensitive = (value ? T : NIL);
|
yuuji@0
|
595 case GET_DEBUGSENSITIVE:
|
yuuji@0
|
596 ret = (void *) (debugsensitive ? VOIDT : NIL);
|
yuuji@0
|
597 break;
|
yuuji@0
|
598
|
yuuji@0
|
599 case SET_ACL:
|
yuuji@0
|
600 mailaclresults = (getacl_t) value;
|
yuuji@0
|
601 case GET_ACL:
|
yuuji@0
|
602 ret = (void *) mailaclresults;
|
yuuji@0
|
603 break;
|
yuuji@0
|
604 case SET_LISTRIGHTS:
|
yuuji@0
|
605 maillistrightsresults = (listrights_t) value;
|
yuuji@0
|
606 case GET_LISTRIGHTS:
|
yuuji@0
|
607 ret = (void *) maillistrightsresults;
|
yuuji@0
|
608 break;
|
yuuji@0
|
609 case SET_MYRIGHTS:
|
yuuji@0
|
610 mailmyrightsresults = (myrights_t) value;
|
yuuji@0
|
611 case GET_MYRIGHTS:
|
yuuji@0
|
612 ret = (void *) mailmyrightsresults;
|
yuuji@0
|
613 break;
|
yuuji@0
|
614 case SET_QUOTA:
|
yuuji@0
|
615 mailquotaresults = (quota_t) value;
|
yuuji@0
|
616 case GET_QUOTA:
|
yuuji@0
|
617 ret = (void *) mailquotaresults;
|
yuuji@0
|
618 break;
|
yuuji@0
|
619 case SET_QUOTAROOT:
|
yuuji@0
|
620 mailquotarootresults = (quotaroot_t) value;
|
yuuji@0
|
621 case GET_QUOTAROOT:
|
yuuji@0
|
622 ret = (void *) mailquotarootresults;
|
yuuji@0
|
623 break;
|
yuuji@0
|
624 case SET_SNARFINTERVAL:
|
yuuji@0
|
625 mailsnarfinterval = (long) value;
|
yuuji@0
|
626 case GET_SNARFINTERVAL:
|
yuuji@0
|
627 ret = (void *) mailsnarfinterval;
|
yuuji@0
|
628 break;
|
yuuji@0
|
629 case SET_SNARFPRESERVE:
|
yuuji@0
|
630 mailsnarfpreserve = (long) value;
|
yuuji@0
|
631 case GET_SNARFPRESERVE:
|
yuuji@0
|
632 ret = (void *) mailsnarfpreserve;
|
yuuji@0
|
633 break;
|
yuuji@0
|
634 case SET_SNARFMAILBOXNAME:
|
yuuji@0
|
635 if (stream) { /* have a stream? */
|
yuuji@0
|
636 if (stream->snarf.name) fs_give ((void **) &stream->snarf.name);
|
yuuji@0
|
637 stream->snarf.name = cpystr ((char *) value);
|
yuuji@0
|
638 }
|
yuuji@0
|
639 else fatal ("SET_SNARFMAILBOXNAME with no stream");
|
yuuji@0
|
640 case GET_SNARFMAILBOXNAME:
|
yuuji@0
|
641 if (stream) ret = (void *) stream->snarf.name;
|
yuuji@0
|
642 break;
|
yuuji@0
|
643 default:
|
yuuji@0
|
644 if (r = smtp_parameters (function,value)) ret = r;
|
yuuji@0
|
645 if (r = env_parameters (function,value)) ret = r;
|
yuuji@0
|
646 if (r = tcp_parameters (function,value)) ret = r;
|
yuuji@0
|
647 if (stream && stream->dtb) {/* if have stream, do for its driver only */
|
yuuji@0
|
648 if (r = (*stream->dtb->parameters) (function,value)) ret = r;
|
yuuji@0
|
649 }
|
yuuji@0
|
650 /* else do all drivers */
|
yuuji@0
|
651 else for (d = maildrivers; d; d = d->next)
|
yuuji@0
|
652 if (r = (d->parameters) (function,value)) ret = r;
|
yuuji@0
|
653 break;
|
yuuji@0
|
654 }
|
yuuji@0
|
655 return ret;
|
yuuji@0
|
656 }
|
yuuji@0
|
657
|
yuuji@0
|
658 /* Mail validate mailbox name
|
yuuji@0
|
659 * Accepts: MAIL stream
|
yuuji@0
|
660 * mailbox name
|
yuuji@0
|
661 * purpose string for error message
|
yuuji@0
|
662 * Return: driver factory on success, NIL on failure
|
yuuji@0
|
663 */
|
yuuji@0
|
664
|
yuuji@0
|
665 DRIVER *mail_valid (MAILSTREAM *stream,char *mailbox,char *purpose)
|
yuuji@0
|
666 {
|
yuuji@0
|
667 char tmp[MAILTMPLEN];
|
yuuji@0
|
668 DRIVER *factory = NIL;
|
yuuji@0
|
669 /* never allow names with newlines */
|
yuuji@0
|
670 if (strpbrk (mailbox,"\015\012")) {
|
yuuji@0
|
671 if (purpose) { /* if want an error message */
|
yuuji@0
|
672 sprintf (tmp,"Can't %s with such a name",purpose);
|
yuuji@0
|
673 MM_LOG (tmp,ERROR);
|
yuuji@0
|
674 }
|
yuuji@0
|
675 return NIL;
|
yuuji@0
|
676 }
|
yuuji@0
|
677 /* validate name, find driver factory */
|
yuuji@0
|
678 if (strlen (mailbox) < (NETMAXHOST+(NETMAXUSER*2)+NETMAXMBX+NETMAXSRV+50))
|
yuuji@0
|
679 for (factory = maildrivers; factory &&
|
yuuji@0
|
680 ((factory->flags & DR_DISABLE) ||
|
yuuji@0
|
681 ((factory->flags & DR_LOCAL) && (*mailbox == '{')) ||
|
yuuji@0
|
682 !(*factory->valid) (mailbox));
|
yuuji@0
|
683 factory = factory->next);
|
yuuji@0
|
684 /* validate factory against non-dummy stream */
|
yuuji@0
|
685 if (factory && stream && stream->dtb && (stream->dtb != factory) &&
|
yuuji@0
|
686 strcmp (stream->dtb->name,"dummy"))
|
yuuji@0
|
687 /* factory invalid; if dummy, use stream */
|
yuuji@0
|
688 factory = strcmp (factory->name,"dummy") ? NIL : stream->dtb;
|
yuuji@0
|
689 if (!factory && purpose) { /* if want an error message */
|
yuuji@0
|
690 sprintf (tmp,"Can't %s %.80s: %s",purpose,mailbox,(*mailbox == '{') ?
|
yuuji@0
|
691 "invalid remote specification" : "no such mailbox");
|
yuuji@0
|
692 MM_LOG (tmp,ERROR);
|
yuuji@0
|
693 }
|
yuuji@0
|
694 return factory; /* return driver factory */
|
yuuji@0
|
695 }
|
yuuji@0
|
696
|
yuuji@0
|
697 /* Mail validate network mailbox name
|
yuuji@0
|
698 * Accepts: mailbox name
|
yuuji@0
|
699 * mailbox driver to validate against
|
yuuji@0
|
700 * pointer to where to return host name if non-NIL
|
yuuji@0
|
701 * pointer to where to return mailbox name if non-NIL
|
yuuji@0
|
702 * Returns: driver on success, NIL on failure
|
yuuji@0
|
703 */
|
yuuji@0
|
704
|
yuuji@0
|
705 DRIVER *mail_valid_net (char *name,DRIVER *drv,char *host,char *mailbox)
|
yuuji@0
|
706 {
|
yuuji@0
|
707 NETMBX mb;
|
yuuji@0
|
708 if (!mail_valid_net_parse (name,&mb) || strcmp (mb.service,drv->name))
|
yuuji@0
|
709 return NIL;
|
yuuji@0
|
710 if (host) strcpy (host,mb.host);
|
yuuji@0
|
711 if (mailbox) strcpy (mailbox,mb.mailbox);
|
yuuji@0
|
712 return drv;
|
yuuji@0
|
713 }
|
yuuji@0
|
714
|
yuuji@0
|
715
|
yuuji@0
|
716 /* Mail validate network mailbox name
|
yuuji@0
|
717 * Accepts: mailbox name
|
yuuji@0
|
718 * NETMBX structure to return values
|
yuuji@0
|
719 * Returns: T on success, NIL on failure
|
yuuji@0
|
720 */
|
yuuji@0
|
721
|
yuuji@0
|
722 long mail_valid_net_parse (char *name,NETMBX *mb)
|
yuuji@0
|
723 {
|
yuuji@0
|
724 return mail_valid_net_parse_work (name,mb,"imap");
|
yuuji@0
|
725 }
|
yuuji@0
|
726
|
yuuji@0
|
727 /* Mail validate network mailbox name worker routine
|
yuuji@0
|
728 * Accepts: mailbox name
|
yuuji@0
|
729 * NETMBX structure to return values
|
yuuji@0
|
730 * default service
|
yuuji@0
|
731 * Returns: T on success, NIL on failure
|
yuuji@0
|
732 */
|
yuuji@0
|
733
|
yuuji@0
|
734 long mail_valid_net_parse_work (char *name,NETMBX *mb,char *service)
|
yuuji@0
|
735 {
|
yuuji@0
|
736 int i,j;
|
yuuji@0
|
737 char c,*s,*t,*v,tmp[MAILTMPLEN],arg[MAILTMPLEN];
|
yuuji@0
|
738 /* initialize structure */
|
yuuji@0
|
739 memset (mb,'\0',sizeof (NETMBX));
|
yuuji@0
|
740 /* must have host specification */
|
yuuji@0
|
741 if (*name++ != '{') return NIL;
|
yuuji@0
|
742 if (*name == '[') { /* if domain literal, find its ending */
|
yuuji@0
|
743 if (!((v = strpbrk (name,"]}")) && (*v++ == ']'))) return NIL;
|
yuuji@0
|
744 }
|
yuuji@0
|
745 /* find end of host name */
|
yuuji@0
|
746 else if (!(v = strpbrk (name,"/:}"))) return NIL;
|
yuuji@0
|
747 /* validate length, find mailbox part */
|
yuuji@0
|
748 if (!((i = v - name) && (i < NETMAXHOST) && (t = strchr (v,'}')) &&
|
yuuji@0
|
749 ((j = t - v) < MAILTMPLEN) && (strlen (t+1) < (size_t) NETMAXMBX)))
|
yuuji@0
|
750 return NIL; /* invalid mailbox */
|
yuuji@0
|
751 strncpy (mb->host,name,i); /* set host name */
|
yuuji@0
|
752 strncpy (mb->orighost,name,i);
|
yuuji@0
|
753 mb->host[i] = mb->orighost[i] = '\0';
|
yuuji@0
|
754 strcpy (mb->mailbox,t+1); /* set mailbox name */
|
yuuji@0
|
755 if (t - v) { /* any switches or port specification? */
|
yuuji@0
|
756 strncpy (t = tmp,v,j); /* copy it */
|
yuuji@0
|
757 tmp[j] = '\0'; /* tie it off */
|
yuuji@0
|
758 c = *t++; /* get first delimiter */
|
yuuji@0
|
759 do switch (c) { /* act based upon the character */
|
yuuji@0
|
760 case ':': /* port specification */
|
yuuji@0
|
761 if (mb->port || !(mb->port = strtoul (t,&t,10))) return NIL;
|
yuuji@0
|
762 c = t ? *t++ : '\0'; /* get delimiter, advance pointer */
|
yuuji@0
|
763 break;
|
yuuji@0
|
764 case '/': /* switch */
|
yuuji@0
|
765 /* find delimiter */
|
yuuji@0
|
766 if (t = strpbrk (s = t,"/:=")) {
|
yuuji@0
|
767 c = *t; /* remember delimiter for later */
|
yuuji@0
|
768 *t++ = '\0'; /* tie off switch name */
|
yuuji@0
|
769 }
|
yuuji@0
|
770 else c = '\0'; /* no delimiter */
|
yuuji@0
|
771 if (c == '=') { /* parse switches which take arguments */
|
yuuji@0
|
772 if (*t == '"') { /* quoted string? */
|
yuuji@0
|
773 for (v = arg,i = 0,++t; (c = *t++) != '"';) {
|
yuuji@0
|
774 if (!c) return NIL; /* unterminated string */
|
yuuji@0
|
775 /* quote next character */
|
yuuji@0
|
776 if (c == '\\') c = *t++;
|
yuuji@0
|
777 if (!c) return NIL; /* can't quote NUL either */
|
yuuji@0
|
778 arg[i++] = c;
|
yuuji@0
|
779 }
|
yuuji@0
|
780 c = *t++; /* remember delimiter for later */
|
yuuji@0
|
781 arg[i] = '\0'; /* tie off argument */
|
yuuji@0
|
782 }
|
yuuji@0
|
783 else { /* non-quoted argument */
|
yuuji@0
|
784 if (t = strpbrk (v = t,"/:")) {
|
yuuji@0
|
785 c = *t; /* remember delimiter for later */
|
yuuji@0
|
786 *t++ = '\0'; /* tie off switch name */
|
yuuji@0
|
787 }
|
yuuji@0
|
788 else c = '\0'; /* no delimiter */
|
yuuji@0
|
789 i = strlen (v); /* length of argument */
|
yuuji@0
|
790 }
|
yuuji@0
|
791 if (!compare_cstring (s,"service") && (i < NETMAXSRV) && !*mb->service)
|
yuuji@0
|
792 lcase (strcpy (mb->service,v));
|
yuuji@0
|
793 else if (!compare_cstring (s,"user") && (i < NETMAXUSER) && !*mb->user)
|
yuuji@0
|
794 strcpy (mb->user,v);
|
yuuji@0
|
795 else if (!compare_cstring (s,"authuser") && (i < NETMAXUSER) &&
|
yuuji@0
|
796 !*mb->authuser) strcpy (mb->authuser,v);
|
yuuji@0
|
797 else return NIL;
|
yuuji@0
|
798 }
|
yuuji@0
|
799
|
yuuji@0
|
800 else { /* non-argument switch */
|
yuuji@0
|
801 if (!compare_cstring (s,"anonymous")) mb->anoflag = T;
|
yuuji@0
|
802 else if (!compare_cstring (s,"debug")) mb->dbgflag = T;
|
yuuji@0
|
803 else if (!compare_cstring (s,"readonly")) mb->readonlyflag = T;
|
yuuji@0
|
804 else if (!compare_cstring (s,"secure")) mb->secflag = T;
|
yuuji@0
|
805 else if (!compare_cstring (s,"norsh")) mb->norsh = T;
|
yuuji@0
|
806 else if (!compare_cstring (s,"loser")) mb->loser = T;
|
yuuji@0
|
807 else if (!compare_cstring (s,"tls") && !mb->notlsflag)
|
yuuji@0
|
808 mb->tlsflag = T;
|
yuuji@0
|
809 else if (!compare_cstring (s,"tls-sslv23") && !mb->notlsflag)
|
yuuji@0
|
810 mb->tlssslv23 = mb->tlsflag = T;
|
yuuji@0
|
811 else if (!compare_cstring (s,"notls") && !mb->tlsflag)
|
yuuji@0
|
812 mb->notlsflag = T;
|
yuuji@0
|
813 else if (!compare_cstring (s,"tryssl"))
|
yuuji@0
|
814 mb->trysslflag = mailssldriver? T : NIL;
|
yuuji@0
|
815 else if (mailssldriver && !compare_cstring (s,"ssl") && !mb->tlsflag)
|
yuuji@0
|
816 mb->sslflag = mb->notlsflag = T;
|
yuuji@0
|
817 else if (mailssldriver && !compare_cstring (s,"novalidate-cert"))
|
yuuji@0
|
818 mb->novalidate = T;
|
yuuji@0
|
819 /* hack for compatibility with the past */
|
yuuji@0
|
820 else if (mailssldriver && !compare_cstring (s,"validate-cert"));
|
yuuji@0
|
821 /* service switches below here */
|
yuuji@0
|
822 else if (*mb->service) return NIL;
|
yuuji@0
|
823 else if (!compare_cstring (s,"imap") ||
|
yuuji@0
|
824 !compare_cstring (s,"nntp") ||
|
yuuji@0
|
825 !compare_cstring (s,"pop3") ||
|
yuuji@0
|
826 !compare_cstring (s,"smtp") ||
|
yuuji@0
|
827 !compare_cstring (s,"submit"))
|
yuuji@0
|
828 lcase (strcpy (mb->service,s));
|
yuuji@0
|
829 else if (!compare_cstring (s,"imap2") ||
|
yuuji@0
|
830 !compare_cstring (s,"imap2bis") ||
|
yuuji@0
|
831 !compare_cstring (s,"imap4") ||
|
yuuji@0
|
832 !compare_cstring (s,"imap4rev1"))
|
yuuji@0
|
833 strcpy (mb->service,"imap");
|
yuuji@0
|
834 else if (!compare_cstring (s,"pop"))
|
yuuji@0
|
835 strcpy (mb->service,"pop3");
|
yuuji@0
|
836 else return NIL; /* invalid non-argument switch */
|
yuuji@0
|
837 }
|
yuuji@0
|
838 break;
|
yuuji@0
|
839 default: /* anything else is bogus */
|
yuuji@0
|
840 return NIL;
|
yuuji@0
|
841 } while (c); /* see if anything more to parse */
|
yuuji@0
|
842 }
|
yuuji@0
|
843 /* default mailbox name */
|
yuuji@0
|
844 if (!*mb->mailbox) strcpy (mb->mailbox,"INBOX");
|
yuuji@0
|
845 /* default service name */
|
yuuji@0
|
846 if (!*mb->service) strcpy (mb->service,service);
|
yuuji@0
|
847 /* /norsh only valid if imap */
|
yuuji@0
|
848 if (mb->norsh && strcmp (mb->service,"imap")) return NIL;
|
yuuji@0
|
849 return T;
|
yuuji@0
|
850 }
|
yuuji@0
|
851
|
yuuji@0
|
852 /* Mail scan mailboxes for string
|
yuuji@0
|
853 * Accepts: mail stream
|
yuuji@0
|
854 * reference
|
yuuji@0
|
855 * pattern to search
|
yuuji@0
|
856 * contents to search
|
yuuji@0
|
857 */
|
yuuji@0
|
858
|
yuuji@0
|
859 void mail_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents)
|
yuuji@0
|
860 {
|
yuuji@0
|
861 int remote = ((*pat == '{') || (ref && *ref == '{'));
|
yuuji@0
|
862 DRIVER *d;
|
yuuji@0
|
863 if (ref && (strlen (ref) > NETMAXMBX)) {
|
yuuji@0
|
864 char tmp[MAILTMPLEN];
|
yuuji@0
|
865 sprintf (tmp,"Invalid LIST reference specification: %.80s",ref);
|
yuuji@0
|
866 MM_LOG (tmp,ERROR);
|
yuuji@0
|
867 return;
|
yuuji@0
|
868 }
|
yuuji@0
|
869 if (strlen (pat) > NETMAXMBX) {
|
yuuji@0
|
870 char tmp[MAILTMPLEN];
|
yuuji@0
|
871 sprintf (tmp,"Invalid LIST pattern specification: %.80s",pat);
|
yuuji@0
|
872 MM_LOG (tmp,ERROR);
|
yuuji@0
|
873 return;
|
yuuji@0
|
874 }
|
yuuji@0
|
875 if (*pat == '{') ref = NIL; /* ignore reference if pattern is remote */
|
yuuji@0
|
876 if (stream) { /* if have a stream, do it for that stream */
|
yuuji@0
|
877 if ((d = stream->dtb) && d->scan &&
|
yuuji@0
|
878 !(((d->flags & DR_LOCAL) && remote)))
|
yuuji@0
|
879 (*d->scan) (stream,ref,pat,contents);
|
yuuji@0
|
880 }
|
yuuji@0
|
881 /* otherwise do for all DTB's */
|
yuuji@0
|
882 else for (d = maildrivers; d; d = d->next)
|
yuuji@0
|
883 if (d->scan && !((d->flags & DR_DISABLE) ||
|
yuuji@0
|
884 ((d->flags & DR_LOCAL) && remote)))
|
yuuji@0
|
885 (d->scan) (NIL,ref,pat,contents);
|
yuuji@0
|
886 }
|
yuuji@0
|
887
|
yuuji@0
|
888 /* Mail list mailboxes
|
yuuji@0
|
889 * Accepts: mail stream
|
yuuji@0
|
890 * reference
|
yuuji@0
|
891 * pattern to search
|
yuuji@0
|
892 */
|
yuuji@0
|
893
|
yuuji@0
|
894 void mail_list (MAILSTREAM *stream,char *ref,char *pat)
|
yuuji@0
|
895 {
|
yuuji@0
|
896 int remote = ((*pat == '{') || (ref && *ref == '{'));
|
yuuji@0
|
897 DRIVER *d = maildrivers;
|
yuuji@0
|
898 if (ref && (strlen (ref) > NETMAXMBX)) {
|
yuuji@0
|
899 char tmp[MAILTMPLEN];
|
yuuji@0
|
900 sprintf (tmp,"Invalid LIST reference specification: %.80s",ref);
|
yuuji@0
|
901 MM_LOG (tmp,ERROR);
|
yuuji@0
|
902 return;
|
yuuji@0
|
903 }
|
yuuji@0
|
904 if (strlen (pat) > NETMAXMBX) {
|
yuuji@0
|
905 char tmp[MAILTMPLEN];
|
yuuji@0
|
906 sprintf (tmp,"Invalid LIST pattern specification: %.80s",pat);
|
yuuji@0
|
907 MM_LOG (tmp,ERROR);
|
yuuji@0
|
908 return;
|
yuuji@0
|
909 }
|
yuuji@0
|
910 if (*pat == '{') ref = NIL; /* ignore reference if pattern is remote */
|
yuuji@0
|
911 if (stream && stream->dtb) { /* if have a stream, do it for that stream */
|
yuuji@0
|
912 if (!(((d = stream->dtb)->flags & DR_LOCAL) && remote))
|
yuuji@0
|
913 (*d->list) (stream,ref,pat);
|
yuuji@0
|
914 }
|
yuuji@0
|
915 /* otherwise do for all DTB's */
|
yuuji@0
|
916 else do if (!((d->flags & DR_DISABLE) ||
|
yuuji@0
|
917 ((d->flags & DR_LOCAL) && remote)))
|
yuuji@0
|
918 (d->list) (NIL,ref,pat);
|
yuuji@0
|
919 while (d = d->next); /* until at the end */
|
yuuji@0
|
920 }
|
yuuji@0
|
921
|
yuuji@0
|
922 /* Mail list subscribed mailboxes
|
yuuji@0
|
923 * Accepts: mail stream
|
yuuji@0
|
924 * pattern to search
|
yuuji@0
|
925 */
|
yuuji@0
|
926
|
yuuji@0
|
927 void mail_lsub (MAILSTREAM *stream,char *ref,char *pat)
|
yuuji@0
|
928 {
|
yuuji@0
|
929 int remote = ((*pat == '{') || (ref && *ref == '{'));
|
yuuji@0
|
930 DRIVER *d = maildrivers;
|
yuuji@0
|
931 if (ref && (strlen (ref) > NETMAXMBX)) {
|
yuuji@0
|
932 char tmp[MAILTMPLEN];
|
yuuji@0
|
933 sprintf (tmp,"Invalid LSUB reference specification: %.80s",ref);
|
yuuji@0
|
934 MM_LOG (tmp,ERROR);
|
yuuji@0
|
935 return;
|
yuuji@0
|
936 }
|
yuuji@0
|
937 if (strlen (pat) > NETMAXMBX) {
|
yuuji@0
|
938 char tmp[MAILTMPLEN];
|
yuuji@0
|
939 sprintf (tmp,"Invalid LSUB pattern specification: %.80s",pat);
|
yuuji@0
|
940 MM_LOG (tmp,ERROR);
|
yuuji@0
|
941 return;
|
yuuji@0
|
942 }
|
yuuji@0
|
943 if (*pat == '{') ref = NIL; /* ignore reference if pattern is remote */
|
yuuji@0
|
944 if (stream && stream->dtb) { /* if have a stream, do it for that stream */
|
yuuji@0
|
945 if (!(((d = stream->dtb)->flags & DR_LOCAL) && remote))
|
yuuji@0
|
946 (*d->lsub) (stream,ref,pat);
|
yuuji@0
|
947 }
|
yuuji@0
|
948 /* otherwise do for all DTB's */
|
yuuji@0
|
949 else do if (!((d->flags & DR_DISABLE) ||
|
yuuji@0
|
950 ((d->flags & DR_LOCAL) && remote)))
|
yuuji@0
|
951 (d->lsub) (NIL,ref,pat);
|
yuuji@0
|
952 while (d = d->next); /* until at the end */
|
yuuji@0
|
953 }
|
yuuji@0
|
954
|
yuuji@0
|
955 /* Mail subscribe to mailbox
|
yuuji@0
|
956 * Accepts: mail stream
|
yuuji@0
|
957 * mailbox to add to subscription list
|
yuuji@0
|
958 * Returns: T on success, NIL on failure
|
yuuji@0
|
959 */
|
yuuji@0
|
960
|
yuuji@0
|
961 long mail_subscribe (MAILSTREAM *stream,char *mailbox)
|
yuuji@0
|
962 {
|
yuuji@0
|
963 DRIVER *factory = mail_valid (stream,mailbox,"subscribe to mailbox");
|
yuuji@0
|
964 return factory ?
|
yuuji@0
|
965 (factory->subscribe ?
|
yuuji@0
|
966 (*factory->subscribe) (stream,mailbox) : sm_subscribe (mailbox)) : NIL;
|
yuuji@0
|
967 }
|
yuuji@0
|
968
|
yuuji@0
|
969
|
yuuji@0
|
970 /* Mail unsubscribe to mailbox
|
yuuji@0
|
971 * Accepts: mail stream
|
yuuji@0
|
972 * mailbox to delete from subscription list
|
yuuji@0
|
973 * Returns: T on success, NIL on failure
|
yuuji@0
|
974 */
|
yuuji@0
|
975
|
yuuji@0
|
976 long mail_unsubscribe (MAILSTREAM *stream,char *mailbox)
|
yuuji@0
|
977 {
|
yuuji@0
|
978 DRIVER *factory = mail_valid (stream,mailbox,NIL);
|
yuuji@0
|
979 return (factory && factory->unsubscribe) ?
|
yuuji@0
|
980 (*factory->unsubscribe) (stream,mailbox) : sm_unsubscribe (mailbox);
|
yuuji@0
|
981 }
|
yuuji@0
|
982
|
yuuji@0
|
983 /* Mail create mailbox
|
yuuji@0
|
984 * Accepts: mail stream
|
yuuji@0
|
985 * mailbox name to create
|
yuuji@0
|
986 * Returns: T on success, NIL on failure
|
yuuji@0
|
987 */
|
yuuji@0
|
988
|
yuuji@0
|
989 long mail_create (MAILSTREAM *stream,char *mailbox)
|
yuuji@0
|
990 {
|
yuuji@0
|
991 MAILSTREAM *ts;
|
yuuji@0
|
992 char *s,*t,tmp[MAILTMPLEN];
|
yuuji@0
|
993 size_t i;
|
yuuji@0
|
994 DRIVER *d;
|
yuuji@0
|
995 /* never allow names with newlines */
|
yuuji@0
|
996 if (s = strpbrk (mailbox,"\015\012")) {
|
yuuji@0
|
997 MM_LOG ("Can't create mailbox with such a name",ERROR);
|
yuuji@0
|
998 return NIL;
|
yuuji@0
|
999 }
|
yuuji@0
|
1000 if (strlen (mailbox) >= (NETMAXHOST+(NETMAXUSER*2)+NETMAXMBX+NETMAXSRV+50)) {
|
yuuji@0
|
1001 sprintf (tmp,"Can't create %.80s: %s",mailbox,(*mailbox == '{') ?
|
yuuji@0
|
1002 "invalid remote specification" : "no such mailbox");
|
yuuji@0
|
1003 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1004 return NIL;
|
yuuji@0
|
1005 }
|
yuuji@0
|
1006 /* create of INBOX invalid */
|
yuuji@0
|
1007 if (!compare_cstring (mailbox,"INBOX")) {
|
yuuji@0
|
1008 MM_LOG ("Can't create INBOX",ERROR);
|
yuuji@0
|
1009 return NIL;
|
yuuji@0
|
1010 }
|
yuuji@0
|
1011 /* validate name */
|
yuuji@0
|
1012 if (s = mail_utf7_valid (mailbox)) {
|
yuuji@0
|
1013 sprintf (tmp,"Can't create %s: %.80s",s,mailbox);
|
yuuji@0
|
1014 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1015 return NIL;
|
yuuji@0
|
1016 }
|
yuuji@0
|
1017
|
yuuji@0
|
1018 /* see if special driver hack */
|
yuuji@0
|
1019 if ((mailbox[0] == '#') && ((mailbox[1] == 'd') || (mailbox[1] == 'D')) &&
|
yuuji@0
|
1020 ((mailbox[2] == 'r') || (mailbox[2] == 'R')) &&
|
yuuji@0
|
1021 ((mailbox[3] == 'i') || (mailbox[3] == 'I')) &&
|
yuuji@0
|
1022 ((mailbox[4] == 'v') || (mailbox[4] == 'V')) &&
|
yuuji@0
|
1023 ((mailbox[5] == 'e') || (mailbox[5] == 'E')) &&
|
yuuji@0
|
1024 ((mailbox[6] == 'r') || (mailbox[6] == 'R')) && (mailbox[7] == '.')) {
|
yuuji@0
|
1025 /* copy driver until likely delimiter */
|
yuuji@0
|
1026 if ((s = strpbrk (t = mailbox+8,"/\\:")) && (i = s - t)) {
|
yuuji@0
|
1027 strncpy (tmp,t,i);
|
yuuji@0
|
1028 tmp[i] = '\0';
|
yuuji@0
|
1029 }
|
yuuji@0
|
1030 else {
|
yuuji@0
|
1031 sprintf (tmp,"Can't create mailbox %.80s: bad driver syntax",mailbox);
|
yuuji@0
|
1032 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1033 return NIL;
|
yuuji@0
|
1034 }
|
yuuji@0
|
1035 for (d = maildrivers; d && strcmp (d->name,tmp); d = d->next);
|
yuuji@0
|
1036 if (d) mailbox = ++s; /* skip past driver specification */
|
yuuji@0
|
1037 else {
|
yuuji@0
|
1038 sprintf (tmp,"Can't create mailbox %.80s: unknown driver",mailbox);
|
yuuji@0
|
1039 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1040 return NIL;
|
yuuji@0
|
1041 }
|
yuuji@0
|
1042 }
|
yuuji@0
|
1043 /* use stream if one given or deterministic */
|
yuuji@0
|
1044 else if ((stream && stream->dtb) ||
|
yuuji@0
|
1045 (((*mailbox == '{') || (*mailbox == '#')) &&
|
yuuji@0
|
1046 (stream = mail_open (NIL,mailbox,OP_PROTOTYPE | OP_SILENT))))
|
yuuji@0
|
1047 d = stream->dtb;
|
yuuji@0
|
1048 else if ((*mailbox != '{') && (ts = default_proto (NIL))) d = ts->dtb;
|
yuuji@0
|
1049 else { /* failed utterly */
|
yuuji@0
|
1050 sprintf (tmp,"Can't create mailbox %.80s: indeterminate format",mailbox);
|
yuuji@0
|
1051 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1052 return NIL;
|
yuuji@0
|
1053 }
|
yuuji@0
|
1054 return (*d->create) (stream,mailbox);
|
yuuji@0
|
1055 }
|
yuuji@0
|
1056
|
yuuji@0
|
1057 /* Mail delete mailbox
|
yuuji@0
|
1058 * Accepts: mail stream
|
yuuji@0
|
1059 * mailbox name to delete
|
yuuji@0
|
1060 * Returns: T on success, NIL on failure
|
yuuji@0
|
1061 */
|
yuuji@0
|
1062
|
yuuji@0
|
1063 long mail_delete (MAILSTREAM *stream,char *mailbox)
|
yuuji@0
|
1064 {
|
yuuji@0
|
1065 DRIVER *dtb = mail_valid (stream,mailbox,"delete mailbox");
|
yuuji@0
|
1066 if (!dtb) return NIL;
|
yuuji@0
|
1067 if (((mailbox[0] == 'I') || (mailbox[0] == 'i')) &&
|
yuuji@0
|
1068 ((mailbox[1] == 'N') || (mailbox[1] == 'n')) &&
|
yuuji@0
|
1069 ((mailbox[2] == 'B') || (mailbox[2] == 'b')) &&
|
yuuji@0
|
1070 ((mailbox[3] == 'O') || (mailbox[3] == 'o')) &&
|
yuuji@0
|
1071 ((mailbox[4] == 'X') || (mailbox[4] == 'x')) && !mailbox[5]) {
|
yuuji@0
|
1072 MM_LOG ("Can't delete INBOX",ERROR);
|
yuuji@0
|
1073 return NIL;
|
yuuji@0
|
1074 }
|
yuuji@0
|
1075 return SAFE_DELETE (dtb,stream,mailbox);
|
yuuji@0
|
1076 }
|
yuuji@0
|
1077
|
yuuji@0
|
1078
|
yuuji@0
|
1079 /* Mail rename mailbox
|
yuuji@0
|
1080 * Accepts: mail stream
|
yuuji@0
|
1081 * old mailbox name
|
yuuji@0
|
1082 * new mailbox name
|
yuuji@0
|
1083 * Returns: T on success, NIL on failure
|
yuuji@0
|
1084 */
|
yuuji@0
|
1085
|
yuuji@0
|
1086 long mail_rename (MAILSTREAM *stream,char *old,char *newname)
|
yuuji@0
|
1087 {
|
yuuji@0
|
1088 char *s,tmp[MAILTMPLEN];
|
yuuji@0
|
1089 DRIVER *dtb = mail_valid (stream,old,"rename mailbox");
|
yuuji@0
|
1090 if (!dtb) return NIL;
|
yuuji@0
|
1091 /* validate name */
|
yuuji@0
|
1092 if (s = mail_utf7_valid (newname)) {
|
yuuji@0
|
1093 sprintf (tmp,"Can't rename to %s: %.80s",s,newname);
|
yuuji@0
|
1094 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1095 return NIL;
|
yuuji@0
|
1096 }
|
yuuji@0
|
1097 if ((*old != '{') && (*old != '#') && mail_valid (NIL,newname,NIL)) {
|
yuuji@0
|
1098 sprintf (tmp,"Can't rename %.80s: mailbox %.80s already exists",
|
yuuji@0
|
1099 old,newname);
|
yuuji@0
|
1100 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1101 return NIL;
|
yuuji@0
|
1102 }
|
yuuji@0
|
1103 return SAFE_RENAME (dtb,stream,old,newname);
|
yuuji@0
|
1104 }
|
yuuji@0
|
1105
|
yuuji@0
|
1106 /* Validate mailbox as Modified UTF-7
|
yuuji@0
|
1107 * Accepts: candidate mailbox name
|
yuuji@0
|
1108 * Returns: error string if error, NIL if valid
|
yuuji@0
|
1109 */
|
yuuji@0
|
1110
|
yuuji@0
|
1111 char *mail_utf7_valid (char *mailbox)
|
yuuji@0
|
1112 {
|
yuuji@0
|
1113 char *s;
|
yuuji@0
|
1114 for (s = mailbox; *s; s++) { /* make sure valid name */
|
yuuji@0
|
1115 /* reserved for future use with UTF-8 */
|
yuuji@0
|
1116 if (*s & 0x80) return "mailbox name with 8-bit octet";
|
yuuji@0
|
1117 /* validate modified UTF-7 */
|
yuuji@0
|
1118 else if (*s == '&') while (*++s != '-') switch (*s) {
|
yuuji@0
|
1119 case '\0':
|
yuuji@0
|
1120 return "unterminated modified UTF-7 name";
|
yuuji@0
|
1121 case '+': /* valid modified BASE64 */
|
yuuji@0
|
1122 case ',':
|
yuuji@0
|
1123 break; /* all OK so far */
|
yuuji@0
|
1124 default: /* must be alphanumeric */
|
yuuji@0
|
1125 if (!isalnum (*s)) return "invalid modified UTF-7 name";
|
yuuji@0
|
1126 break;
|
yuuji@0
|
1127 }
|
yuuji@0
|
1128 }
|
yuuji@0
|
1129 return NIL; /* all OK */
|
yuuji@0
|
1130 }
|
yuuji@0
|
1131
|
yuuji@0
|
1132 /* Mail status of mailbox
|
yuuji@0
|
1133 * Accepts: mail stream if open on this mailbox
|
yuuji@0
|
1134 * mailbox name
|
yuuji@0
|
1135 * status flags
|
yuuji@0
|
1136 * Returns: T on success, NIL on failure
|
yuuji@0
|
1137 */
|
yuuji@0
|
1138
|
yuuji@0
|
1139 long mail_status (MAILSTREAM *stream,char *mbx,long flags)
|
yuuji@0
|
1140 {
|
yuuji@0
|
1141 DRIVER *dtb = mail_valid (stream,mbx,"get status of mailbox");
|
yuuji@0
|
1142 if (!dtb) return NIL; /* only if valid */
|
yuuji@0
|
1143 if (stream && ((dtb != stream->dtb) ||
|
yuuji@0
|
1144 ((dtb->flags & DR_LOCAL) && strcmp (mbx,stream->mailbox) &&
|
yuuji@0
|
1145 strcmp (mbx,stream->original_mailbox))))
|
yuuji@0
|
1146 stream = NIL; /* stream not suitable */
|
yuuji@0
|
1147 return SAFE_STATUS (dtb,stream,mbx,flags);
|
yuuji@0
|
1148 }
|
yuuji@0
|
1149
|
yuuji@0
|
1150
|
yuuji@0
|
1151 /* Mail status of mailbox default handler
|
yuuji@0
|
1152 * Accepts: mail stream
|
yuuji@0
|
1153 * mailbox name
|
yuuji@0
|
1154 * status flags
|
yuuji@0
|
1155 * Returns: T on success, NIL on failure
|
yuuji@0
|
1156 */
|
yuuji@0
|
1157
|
yuuji@0
|
1158 long mail_status_default (MAILSTREAM *stream,char *mbx,long flags)
|
yuuji@0
|
1159 {
|
yuuji@0
|
1160 MAILSTATUS status;
|
yuuji@0
|
1161 unsigned long i;
|
yuuji@0
|
1162 MAILSTREAM *tstream = NIL;
|
yuuji@0
|
1163 /* make temporary stream (unless this mbx) */
|
yuuji@0
|
1164 if (!stream && !(stream = tstream =
|
yuuji@0
|
1165 mail_open (NIL,mbx,OP_READONLY|OP_SILENT))) return NIL;
|
yuuji@0
|
1166 status.flags = flags; /* return status values */
|
yuuji@0
|
1167 status.messages = stream->nmsgs;
|
yuuji@0
|
1168 status.recent = stream->recent;
|
yuuji@0
|
1169 if (flags & SA_UNSEEN) /* must search to get unseen messages */
|
yuuji@0
|
1170 for (i = 1,status.unseen = 0; i <= stream->nmsgs; i++)
|
yuuji@0
|
1171 if (!mail_elt (stream,i)->seen) status.unseen++;
|
yuuji@0
|
1172 status.uidnext = stream->uid_last + 1;
|
yuuji@0
|
1173 status.uidvalidity = stream->uid_validity;
|
yuuji@0
|
1174 MM_STATUS(stream,mbx,&status);/* pass status to main program */
|
yuuji@0
|
1175 if (tstream) mail_close (tstream);
|
yuuji@0
|
1176 return T; /* success */
|
yuuji@0
|
1177 }
|
yuuji@0
|
1178
|
yuuji@0
|
1179 /* Mail open
|
yuuji@0
|
1180 * Accepts: candidate stream for recycling
|
yuuji@0
|
1181 * mailbox name
|
yuuji@0
|
1182 * open options
|
yuuji@0
|
1183 * Returns: stream to use on success, NIL on failure
|
yuuji@0
|
1184 */
|
yuuji@0
|
1185
|
yuuji@0
|
1186 MAILSTREAM *mail_open (MAILSTREAM *stream,char *name,long options)
|
yuuji@0
|
1187 {
|
yuuji@0
|
1188 int i;
|
yuuji@0
|
1189 char c,*s,tmp[MAILTMPLEN];
|
yuuji@0
|
1190 NETMBX mb;
|
yuuji@0
|
1191 DRIVER *d;
|
yuuji@0
|
1192 switch (name[0]) { /* see if special handling */
|
yuuji@0
|
1193 case '#': /* possible special hacks */
|
yuuji@0
|
1194 if (((name[1] == 'M') || (name[1] == 'm')) &&
|
yuuji@0
|
1195 ((name[2] == 'O') || (name[2] == 'o')) &&
|
yuuji@0
|
1196 ((name[3] == 'V') || (name[3] == 'v')) &&
|
yuuji@0
|
1197 ((name[4] == 'E') || (name[4] == 'e')) && (c = name[5]) &&
|
yuuji@0
|
1198 (s = strchr (name+6,c)) && (i = s - (name + 6)) && (i < MAILTMPLEN)) {
|
yuuji@0
|
1199 if (stream = mail_open (stream,s+1,options)) {
|
yuuji@0
|
1200 strncpy (tmp,name+6,i); /* copy snarf mailbox name */
|
yuuji@0
|
1201 tmp[i] = '\0'; /* tie off name */
|
yuuji@0
|
1202 mail_parameters (stream,SET_SNARFMAILBOXNAME,(void *) tmp);
|
yuuji@0
|
1203 stream->snarf.options = options;
|
yuuji@0
|
1204 mail_ping (stream); /* do initial snarf */
|
yuuji@0
|
1205 /* punt if can't do initial snarf */
|
yuuji@0
|
1206 if (!stream->snarf.time) stream = mail_close (stream);
|
yuuji@0
|
1207 }
|
yuuji@0
|
1208 return stream;
|
yuuji@0
|
1209 }
|
yuuji@0
|
1210 /* special POP hack */
|
yuuji@0
|
1211 else if (((name[1] == 'P') || (name[1] == 'p')) &&
|
yuuji@0
|
1212 ((name[2] == 'O') || (name[2] == 'o')) &&
|
yuuji@0
|
1213 ((name[3] == 'P') || (name[3] == 'p')) &&
|
yuuji@0
|
1214 mail_valid_net_parse_work (name+4,&mb,"pop3") &&
|
yuuji@0
|
1215 !strcmp (mb.service,"pop3") && !mb.anoflag && !mb.readonlyflag) {
|
yuuji@0
|
1216 if (stream = mail_open (stream,mb.mailbox,options)) {
|
yuuji@0
|
1217 sprintf (tmp,"{%.255s",mb.host);
|
yuuji@0
|
1218 if (mb.port) sprintf (tmp + strlen (tmp),":%lu",mb.port);
|
yuuji@0
|
1219 if (mb.user[0]) sprintf (tmp + strlen (tmp),"/user=%.64s",mb.user);
|
yuuji@0
|
1220 if (mb.dbgflag) strcat (tmp,"/debug");
|
yuuji@0
|
1221 if (mb.secflag) strcat (tmp,"/secure");
|
yuuji@0
|
1222 if (mb.tlsflag) strcat (tmp,"/tls");
|
yuuji@0
|
1223 if (mb.notlsflag) strcat (tmp,"/notls");
|
yuuji@0
|
1224 if (mb.sslflag) strcat (tmp,"/ssl");
|
yuuji@0
|
1225 if (mb.trysslflag) strcat (tmp,"/tryssl");
|
yuuji@0
|
1226 if (mb.novalidate) strcat (tmp,"/novalidate-cert");
|
yuuji@0
|
1227 strcat (tmp,"/pop3/loser}");
|
yuuji@0
|
1228 mail_parameters (stream,SET_SNARFMAILBOXNAME,(void *) tmp);
|
yuuji@0
|
1229 mail_ping (stream); /* do initial snarf */
|
yuuji@0
|
1230 }
|
yuuji@0
|
1231 return stream; /* return local mailbox stream */
|
yuuji@0
|
1232 }
|
yuuji@0
|
1233
|
yuuji@0
|
1234 else if ((options & OP_PROTOTYPE) &&
|
yuuji@0
|
1235 ((name[1] == 'D') || (name[1] == 'd')) &&
|
yuuji@0
|
1236 ((name[2] == 'R') || (name[2] == 'r')) &&
|
yuuji@0
|
1237 ((name[3] == 'I') || (name[3] == 'i')) &&
|
yuuji@0
|
1238 ((name[4] == 'V') || (name[4] == 'v')) &&
|
yuuji@0
|
1239 ((name[5] == 'E') || (name[5] == 'e')) &&
|
yuuji@0
|
1240 ((name[6] == 'R') || (name[6] == 'r')) && (name[7] == '.')) {
|
yuuji@0
|
1241 sprintf (tmp,"%.80s",name+8);
|
yuuji@0
|
1242 /* tie off name at likely delimiter */
|
yuuji@0
|
1243 if (s = strpbrk (tmp,"/\\:")) *s++ = '\0';
|
yuuji@0
|
1244 else {
|
yuuji@0
|
1245 sprintf (tmp,"Can't resolve mailbox %.80s: bad driver syntax",name);
|
yuuji@0
|
1246 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1247 return mail_close (stream);
|
yuuji@0
|
1248 }
|
yuuji@0
|
1249 for (d = maildrivers; d && compare_cstring (d->name,tmp); d = d->next);
|
yuuji@0
|
1250 if (d) return (*d->open) (NIL);
|
yuuji@0
|
1251 sprintf (tmp,"Can't resolve mailbox %.80s: unknown driver",name);
|
yuuji@0
|
1252 MM_LOG (tmp,ERROR);
|
yuuji@0
|
1253 return mail_close (stream);
|
yuuji@0
|
1254 }
|
yuuji@0
|
1255 /* fall through to default case */
|
yuuji@0
|
1256 default: /* not special hack (but could be # name */
|
yuuji@0
|
1257 d = mail_valid (NIL,name,(options & OP_SILENT) ?
|
yuuji@0
|
1258 (char *) NIL : "open mailbox");
|
yuuji@0
|
1259 }
|
yuuji@0
|
1260 return d ? mail_open_work (d,stream,name,options) : stream;
|
yuuji@0
|
1261 }
|
yuuji@0
|
1262
|
yuuji@0
|
1263 /* Mail open worker routine
|
yuuji@0
|
1264 * Accepts: factory
|
yuuji@0
|
1265 * candidate stream for recycling
|
yuuji@0
|
1266 * mailbox name
|
yuuji@0
|
1267 * open options
|
yuuji@0
|
1268 * Returns: stream to use on success, NIL on failure
|
yuuji@0
|
1269 */
|
yuuji@0
|
1270
|
yuuji@0
|
1271 MAILSTREAM *mail_open_work (DRIVER *d,MAILSTREAM *stream,char *name,
|
yuuji@0
|
1272 long options)
|
yuuji@0
|
1273 {
|
yuuji@0
|
1274 int i;
|
yuuji@0
|
1275 char tmp[MAILTMPLEN];
|
yuuji@0
|
1276 NETMBX mb;
|
yuuji@0
|
1277 if (options & OP_PROTOTYPE) return (*d->open) (NIL);
|
yuuji@0
|
1278 /* name is copied here in case the caller does a re-open using
|
yuuji@0
|
1279 * stream->mailbox or stream->original_mailbox as the argument.
|
yuuji@0
|
1280 */
|
yuuji@0
|
1281 name = cpystr (name); /* make copy of name */
|
yuuji@0
|
1282 if (stream) { /* recycling requested? */
|
yuuji@0
|
1283 if ((stream->dtb == d) && (d->flags & DR_RECYCLE) &&
|
yuuji@0
|
1284 ((d->flags & DR_HALFOPEN) || !(options & OP_HALFOPEN)) &&
|
yuuji@0
|
1285 mail_usable_network_stream (stream,name)) {
|
yuuji@0
|
1286 /* yes, checkpoint if needed */
|
yuuji@0
|
1287 if (d->flags & DR_XPOINT) mail_check (stream);
|
yuuji@0
|
1288 mail_free_cache (stream); /* clean up stream */
|
yuuji@0
|
1289 if (stream->mailbox) fs_give ((void **) &stream->mailbox);
|
yuuji@0
|
1290 if (stream->original_mailbox)
|
yuuji@0
|
1291 fs_give ((void **) &stream->original_mailbox);
|
yuuji@0
|
1292 /* flush user flags */
|
yuuji@0
|
1293 for (i = 0; i < NUSERFLAGS; i++)
|
yuuji@0
|
1294 if (stream->user_flags[i]) fs_give ((void **) &stream->user_flags[i]);
|
yuuji@0
|
1295 }
|
yuuji@0
|
1296 else { /* stream not recycleable, babble if net */
|
yuuji@0
|
1297 if (!stream->silent && stream->dtb && !(stream->dtb->flags&DR_LOCAL) &&
|
yuuji@0
|
1298 mail_valid_net_parse (stream->mailbox,&mb)) {
|
yuuji@0
|
1299 sprintf (tmp,"Closing connection to %.80s",mb.host);
|
yuuji@0
|
1300 MM_LOG (tmp,(long) NIL);
|
yuuji@0
|
1301 }
|
yuuji@0
|
1302 /* flush the old stream */
|
yuuji@0
|
1303 stream = mail_close (stream);
|
yuuji@0
|
1304 }
|
yuuji@0
|
1305 }
|
yuuji@0
|
1306 /* check if driver does not support halfopen */
|
yuuji@0
|
1307 else if ((options & OP_HALFOPEN) && !(d->flags & DR_HALFOPEN)) {
|
yuuji@0
|
1308 fs_give ((void **) &name);
|
yuuji@0
|
1309 return NIL;
|
yuuji@0
|
1310 }
|
yuuji@0
|
1311
|
yuuji@0
|
1312 /* instantiate new stream if not recycling */
|
yuuji@0
|
1313 if (!stream) (*mailcache) (stream = (MAILSTREAM *)
|
yuuji@0
|
1314 memset (fs_get (sizeof (MAILSTREAM)),0,
|
yuuji@0
|
1315 sizeof (MAILSTREAM)),(long) 0,CH_INIT);
|
yuuji@0
|
1316 stream->dtb = d; /* set dispatch */
|
yuuji@0
|
1317 /* set mailbox name */
|
yuuji@0
|
1318 stream->mailbox = cpystr (stream->original_mailbox = name);
|
yuuji@0
|
1319 /* initialize stream flags */
|
yuuji@0
|
1320 stream->inbox = stream->lock = NIL;
|
yuuji@0
|
1321 stream->debug = (options & OP_DEBUG) ? T : NIL;
|
yuuji@0
|
1322 stream->rdonly = (options & OP_READONLY) ? T : NIL;
|
yuuji@0
|
1323 stream->anonymous = (options & OP_ANONYMOUS) ? T : NIL;
|
yuuji@0
|
1324 stream->scache = (options & OP_SHORTCACHE) ? T : NIL;
|
yuuji@0
|
1325 stream->silent = (options & OP_SILENT) ? T : NIL;
|
yuuji@0
|
1326 stream->halfopen = (options & OP_HALFOPEN) ? T : NIL;
|
yuuji@0
|
1327 stream->secure = (options & OP_SECURE) ? T : NIL;
|
yuuji@0
|
1328 stream->tryssl = (options & OP_TRYSSL) ? T : NIL;
|
yuuji@0
|
1329 stream->mulnewsrc = (options & OP_MULNEWSRC) ? T : NIL;
|
yuuji@0
|
1330 stream->nokod = (options & OP_NOKOD) ? T : NIL;
|
yuuji@0
|
1331 stream->sniff = (options & OP_SNIFF) ? T : NIL;
|
yuuji@0
|
1332 stream->perm_seen = stream->perm_deleted = stream->perm_flagged =
|
yuuji@0
|
1333 stream->perm_answered = stream->perm_draft = stream->kwd_create = NIL;
|
yuuji@0
|
1334 stream->uid_nosticky = (d->flags & DR_NOSTICKY) ? T : NIL;
|
yuuji@0
|
1335 stream->uid_last = 0; /* default UID validity */
|
yuuji@0
|
1336 stream->uid_validity = (unsigned long) time (0);
|
yuuji@0
|
1337 /* have driver open, flush if failed */
|
yuuji@0
|
1338 return ((*d->open) (stream)) ? stream : mail_close (stream);
|
yuuji@0
|
1339 }
|
yuuji@0
|
1340
|
yuuji@0
|
1341 /* Mail close
|
yuuji@0
|
1342 * Accepts: mail stream
|
yuuji@0
|
1343 * close options
|
yuuji@0
|
1344 * Returns: NIL, always
|
yuuji@0
|
1345 */
|
yuuji@0
|
1346
|
yuuji@0
|
1347 MAILSTREAM *mail_close_full (MAILSTREAM *stream,long options)
|
yuuji@0
|
1348 {
|
yuuji@0
|
1349 int i;
|
yuuji@0
|
1350 if (stream) { /* make sure argument given */
|
yuuji@0
|
1351 /* do the driver's close action */
|
yuuji@0
|
1352 if (stream->dtb) (*stream->dtb->close) (stream,options);
|
yuuji@0
|
1353 stream->dtb = NIL; /* resign driver */
|
yuuji@0
|
1354 if (stream->mailbox) fs_give ((void **) &stream->mailbox);
|
yuuji@0
|
1355 if (stream->original_mailbox)
|
yuuji@0
|
1356 fs_give ((void **) &stream->original_mailbox);
|
yuuji@0
|
1357 if (stream->snarf.name) fs_give ((void **) &stream->snarf.name);
|
yuuji@0
|
1358 stream->sequence++; /* invalidate sequence */
|
yuuji@0
|
1359 /* flush user flags */
|
yuuji@0
|
1360 for (i = 0; i < NUSERFLAGS; i++)
|
yuuji@0
|
1361 if (stream->user_flags[i]) fs_give ((void **) &stream->user_flags[i]);
|
yuuji@0
|
1362 mail_free_cache (stream); /* finally free the stream's storage */
|
yuuji@0
|
1363 if (mailfreestreamsparep && stream->sparep)
|
yuuji@0
|
1364 (*mailfreestreamsparep) (&stream->sparep);
|
yuuji@0
|
1365 if (!stream->use) fs_give ((void **) &stream);
|
yuuji@0
|
1366 }
|
yuuji@0
|
1367 return NIL;
|
yuuji@0
|
1368 }
|
yuuji@0
|
1369
|
yuuji@0
|
1370 /* Mail make handle
|
yuuji@0
|
1371 * Accepts: mail stream
|
yuuji@0
|
1372 * Returns: handle
|
yuuji@0
|
1373 *
|
yuuji@0
|
1374 * Handles provide a way to have multiple pointers to a stream yet allow the
|
yuuji@0
|
1375 * stream's owner to nuke it or recycle it.
|
yuuji@0
|
1376 */
|
yuuji@0
|
1377
|
yuuji@0
|
1378 MAILHANDLE *mail_makehandle (MAILSTREAM *stream)
|
yuuji@0
|
1379 {
|
yuuji@0
|
1380 MAILHANDLE *handle = (MAILHANDLE *) fs_get (sizeof (MAILHANDLE));
|
yuuji@0
|
1381 handle->stream = stream; /* copy stream */
|
yuuji@0
|
1382 /* and its sequence */
|
yuuji@0
|
1383 handle->sequence = stream->sequence;
|
yuuji@0
|
1384 stream->use++; /* let stream know another handle exists */
|
yuuji@0
|
1385 return handle;
|
yuuji@0
|
1386 }
|
yuuji@0
|
1387
|
yuuji@0
|
1388
|
yuuji@0
|
1389 /* Mail release handle
|
yuuji@0
|
1390 * Accepts: Mail handle
|
yuuji@0
|
1391 */
|
yuuji@0
|
1392
|
yuuji@0
|
1393 void mail_free_handle (MAILHANDLE **handle)
|
yuuji@0
|
1394 {
|
yuuji@0
|
1395 MAILSTREAM *s;
|
yuuji@0
|
1396 if (*handle) { /* only free if exists */
|
yuuji@0
|
1397 /* resign stream, flush unreferenced zombies */
|
yuuji@0
|
1398 if ((!--(s = (*handle)->stream)->use) && !s->dtb) fs_give ((void **) &s);
|
yuuji@0
|
1399 fs_give ((void **) handle); /* now flush the handle */
|
yuuji@0
|
1400 }
|
yuuji@0
|
1401 }
|
yuuji@0
|
1402
|
yuuji@0
|
1403
|
yuuji@0
|
1404 /* Mail get stream handle
|
yuuji@0
|
1405 * Accepts: Mail handle
|
yuuji@0
|
1406 * Returns: mail stream or NIL if stream gone
|
yuuji@0
|
1407 */
|
yuuji@0
|
1408
|
yuuji@0
|
1409 MAILSTREAM *mail_stream (MAILHANDLE *handle)
|
yuuji@0
|
1410 {
|
yuuji@0
|
1411 MAILSTREAM *s = handle->stream;
|
yuuji@0
|
1412 return (s->dtb && (handle->sequence == s->sequence)) ? s : NIL;
|
yuuji@0
|
1413 }
|
yuuji@0
|
1414
|
yuuji@0
|
1415 /* Mail fetch cache element
|
yuuji@0
|
1416 * Accepts: mail stream
|
yuuji@0
|
1417 * message # to fetch
|
yuuji@0
|
1418 * Returns: cache element of this message
|
yuuji@0
|
1419 * Can also be used to create cache elements for new messages.
|
yuuji@0
|
1420 */
|
yuuji@0
|
1421
|
yuuji@0
|
1422 MESSAGECACHE *mail_elt (MAILSTREAM *stream,unsigned long msgno)
|
yuuji@0
|
1423 {
|
yuuji@0
|
1424 if (msgno < 1 || msgno > stream->nmsgs) {
|
yuuji@0
|
1425 char tmp[MAILTMPLEN];
|
yuuji@0
|
1426 sprintf (tmp,"Bad msgno %lu in mail_elt, nmsgs = %lu, mbx=%.80s",
|
yuuji@0
|
1427 msgno,stream->nmsgs,stream->mailbox ? stream->mailbox : "???");
|
yuuji@0
|
1428 fatal (tmp);
|
yuuji@0
|
1429 }
|
yuuji@0
|
1430 return (MESSAGECACHE *) (*mailcache) (stream,msgno,CH_MAKEELT);
|
yuuji@0
|
1431 }
|
yuuji@0
|
1432
|
yuuji@0
|
1433
|
yuuji@0
|
1434 /* Mail fetch fast information
|
yuuji@0
|
1435 * Accepts: mail stream
|
yuuji@0
|
1436 * sequence
|
yuuji@0
|
1437 * option flags
|
yuuji@0
|
1438 *
|
yuuji@0
|
1439 * Generally, mail_fetch_structure is preferred
|
yuuji@0
|
1440 */
|
yuuji@0
|
1441
|
yuuji@0
|
1442 void mail_fetch_fast (MAILSTREAM *stream,char *sequence,long flags)
|
yuuji@0
|
1443 {
|
yuuji@0
|
1444 /* do the driver's action */
|
yuuji@0
|
1445 if (stream->dtb && stream->dtb->fast)
|
yuuji@0
|
1446 (*stream->dtb->fast) (stream,sequence,flags);
|
yuuji@0
|
1447 }
|
yuuji@0
|
1448
|
yuuji@0
|
1449
|
yuuji@0
|
1450 /* Mail fetch flags
|
yuuji@0
|
1451 * Accepts: mail stream
|
yuuji@0
|
1452 * sequence
|
yuuji@0
|
1453 * option flags
|
yuuji@0
|
1454 */
|
yuuji@0
|
1455
|
yuuji@0
|
1456 void mail_fetch_flags (MAILSTREAM *stream,char *sequence,long flags)
|
yuuji@0
|
1457 {
|
yuuji@0
|
1458 /* do the driver's action */
|
yuuji@0
|
1459 if (stream->dtb && stream->dtb->msgflags)
|
yuuji@0
|
1460 (*stream->dtb->msgflags) (stream,sequence,flags);
|
yuuji@0
|
1461 }
|
yuuji@0
|
1462
|
yuuji@0
|
1463 /* Mail fetch message overview
|
yuuji@0
|
1464 * Accepts: mail stream
|
yuuji@0
|
1465 * UID sequence to fetch
|
yuuji@0
|
1466 * pointer to overview return function
|
yuuji@0
|
1467 */
|
yuuji@0
|
1468
|
yuuji@0
|
1469 void mail_fetch_overview (MAILSTREAM *stream,char *sequence,overview_t ofn)
|
yuuji@0
|
1470 {
|
yuuji@0
|
1471 if (stream->dtb && mail_uid_sequence (stream,sequence) &&
|
yuuji@0
|
1472 !(stream->dtb->overview && (*stream->dtb->overview) (stream,ofn)) &&
|
yuuji@0
|
1473 mail_ping (stream))
|
yuuji@0
|
1474 mail_fetch_overview_default (stream,ofn);
|
yuuji@0
|
1475 }
|
yuuji@0
|
1476
|
yuuji@0
|
1477
|
yuuji@0
|
1478 /* Mail fetch message overview using sequence numbers instead of UIDs
|
yuuji@0
|
1479 * Accepts: mail stream
|
yuuji@0
|
1480 * sequence to fetch
|
yuuji@0
|
1481 * pointer to overview return function
|
yuuji@0
|
1482 */
|
yuuji@0
|
1483
|
yuuji@0
|
1484 void mail_fetch_overview_sequence (MAILSTREAM *stream,char *sequence,
|
yuuji@0
|
1485 overview_t ofn)
|
yuuji@0
|
1486 {
|
yuuji@0
|
1487 if (stream->dtb && mail_sequence (stream,sequence) &&
|
yuuji@0
|
1488 !(stream->dtb->overview && (*stream->dtb->overview) (stream,ofn)) &&
|
yuuji@0
|
1489 mail_ping (stream))
|
yuuji@0
|
1490 mail_fetch_overview_default (stream,ofn);
|
yuuji@0
|
1491 }
|
yuuji@0
|
1492
|
yuuji@0
|
1493
|
yuuji@0
|
1494 /* Mail fetch message overview default handler
|
yuuji@0
|
1495 * Accepts: mail stream with sequence bits lit
|
yuuji@0
|
1496 * pointer to overview return function
|
yuuji@0
|
1497 */
|
yuuji@0
|
1498
|
yuuji@0
|
1499 void mail_fetch_overview_default (MAILSTREAM *stream,overview_t ofn)
|
yuuji@0
|
1500 {
|
yuuji@0
|
1501 MESSAGECACHE *elt;
|
yuuji@0
|
1502 ENVELOPE *env;
|
yuuji@0
|
1503 OVERVIEW ov;
|
yuuji@0
|
1504 unsigned long i;
|
yuuji@0
|
1505 ov.optional.lines = 0;
|
yuuji@0
|
1506 ov.optional.xref = NIL;
|
yuuji@0
|
1507 for (i = 1; i <= stream->nmsgs; i++)
|
yuuji@0
|
1508 if (((elt = mail_elt (stream,i))->sequence) &&
|
yuuji@0
|
1509 (env = mail_fetch_structure (stream,i,NIL,NIL)) && ofn) {
|
yuuji@0
|
1510 ov.subject = env->subject;
|
yuuji@0
|
1511 ov.from = env->from;
|
yuuji@0
|
1512 ov.date = env->date;
|
yuuji@0
|
1513 ov.message_id = env->message_id;
|
yuuji@0
|
1514 ov.references = env->references;
|
yuuji@0
|
1515 ov.optional.octets = elt->rfc822_size;
|
yuuji@0
|
1516 (*ofn) (stream,mail_uid (stream,i),&ov,i);
|
yuuji@0
|
1517 }
|
yuuji@0
|
1518 }
|
yuuji@0
|
1519
|
yuuji@0
|
1520 /* Mail fetch message structure
|
yuuji@0
|
1521 * Accepts: mail stream
|
yuuji@0
|
1522 * message # to fetch
|
yuuji@0
|
1523 * pointer to return body
|
yuuji@0
|
1524 * option flags
|
yuuji@0
|
1525 * Returns: envelope of this message, body returned in body value
|
yuuji@0
|
1526 *
|
yuuji@0
|
1527 * Fetches the "fast" information as well
|
yuuji@0
|
1528 */
|
yuuji@0
|
1529
|
yuuji@0
|
1530 ENVELOPE *mail_fetch_structure (MAILSTREAM *stream,unsigned long msgno,
|
yuuji@0
|
1531 BODY **body,long flags)
|
yuuji@0
|
1532 {
|
yuuji@0
|
1533 ENVELOPE **env;
|
yuuji@0
|
1534 BODY **b;
|
yuuji@0
|
1535 MESSAGECACHE *elt;
|
yuuji@0
|
1536 char c,*s,*hdr;
|
yuuji@0
|
1537 unsigned long hdrsize;
|
yuuji@0
|
1538 STRING bs;
|
yuuji@0
|
1539 /* do the driver's action if specified */
|
yuuji@0
|
1540 if (stream->dtb && stream->dtb->structure)
|
yuuji@0
|
1541 return (*stream->dtb->structure) (stream,msgno,body,flags);
|
yuuji@0
|
1542 if (flags & FT_UID) { /* UID form of call */
|
yuuji@0
|
1543 if (msgno = mail_msgno (stream,msgno)) flags &= ~FT_UID;
|
yuuji@0
|
1544 else return NIL; /* must get UID/msgno map first */
|
yuuji@0
|
1545 }
|
yuuji@0
|
1546 elt = mail_elt (stream,msgno);/* get elt for real message number */
|
yuuji@0
|
1547 if (stream->scache) { /* short caching */
|
yuuji@0
|
1548 if (msgno != stream->msgno){/* garbage collect if not same message */
|
yuuji@0
|
1549 mail_gc (stream,GC_ENV | GC_TEXTS);
|
yuuji@0
|
1550 stream->msgno = msgno; /* this is the current message now */
|
yuuji@0
|
1551 }
|
yuuji@0
|
1552 env = &stream->env; /* get pointers to envelope and body */
|
yuuji@0
|
1553 b = &stream->body;
|
yuuji@0
|
1554 }
|
yuuji@0
|
1555 else { /* get pointers to elt envelope and body */
|
yuuji@0
|
1556 env = &elt->private.msg.env;
|
yuuji@0
|
1557 b = &elt->private.msg.body;
|
yuuji@0
|
1558 }
|
yuuji@0
|
1559
|
yuuji@0
|
1560 if (stream->dtb && ((body && !*b) || !*env || (*env)->incomplete)) {
|
yuuji@0
|
1561 mail_free_envelope (env); /* flush old envelope and body */
|
yuuji@0
|
1562 mail_free_body (b);
|
yuuji@0
|
1563 /* see if need to fetch the whole thing */
|
yuuji@0
|
1564 if (body || !elt->rfc822_size) {
|
yuuji@0
|
1565 s = (*stream->dtb->header) (stream,msgno,&hdrsize,flags & ~FT_INTERNAL);
|
yuuji@0
|
1566 /* make copy in case body fetch smashes it */
|
yuuji@0
|
1567 hdr = (char *) memcpy (fs_get ((size_t) hdrsize+1),s,(size_t) hdrsize);
|
yuuji@0
|
1568 hdr[hdrsize] = '\0'; /* tie off header */
|
yuuji@0
|
1569 (*stream->dtb->text) (stream,msgno,&bs,(flags & ~FT_INTERNAL) | FT_PEEK);
|
yuuji@0
|
1570 if (!elt->rfc822_size) elt->rfc822_size = hdrsize + SIZE (&bs);
|
yuuji@0
|
1571 if (body) /* only parse body if requested */
|
yuuji@0
|
1572 rfc822_parse_msg (env,b,hdr,hdrsize,&bs,BADHOST,stream->dtb->flags);
|
yuuji@0
|
1573 else
|
yuuji@0
|
1574 rfc822_parse_msg (env,NIL,hdr,hdrsize,NIL,BADHOST,stream->dtb->flags);
|
yuuji@0
|
1575 fs_give ((void **) &hdr); /* flush header */
|
yuuji@0
|
1576 }
|
yuuji@0
|
1577 else { /* can save memory doing it this way */
|
yuuji@0
|
1578 hdr = (*stream->dtb->header) (stream,msgno,&hdrsize,flags | FT_INTERNAL);
|
yuuji@0
|
1579 if (hdrsize) { /* in case null header */
|
yuuji@0
|
1580 c = hdr[hdrsize]; /* preserve what's there */
|
yuuji@0
|
1581 hdr[hdrsize] = '\0'; /* tie off header */
|
yuuji@0
|
1582 rfc822_parse_msg (env,NIL,hdr,hdrsize,NIL,BADHOST,stream->dtb->flags);
|
yuuji@0
|
1583 hdr[hdrsize] = c; /* restore in case cached data */
|
yuuji@0
|
1584 }
|
yuuji@0
|
1585 else *env = mail_newenvelope ();
|
yuuji@0
|
1586 }
|
yuuji@0
|
1587 }
|
yuuji@0
|
1588 /* if need date, have date in envelope? */
|
yuuji@0
|
1589 if (!elt->day && *env && (*env)->date) mail_parse_date (elt,(*env)->date);
|
yuuji@0
|
1590 /* sigh, fill in bogus default */
|
yuuji@0
|
1591 if (!elt->day) elt->day = elt->month = 1;
|
yuuji@0
|
1592 if (body) *body = *b; /* return the body */
|
yuuji@0
|
1593 return *env; /* return the envelope */
|
yuuji@0
|
1594 }
|
yuuji@0
|
1595
|
yuuji@0
|
1596 /* Mail mark single message (internal use only)
|
yuuji@0
|
1597 * Accepts: mail stream
|
yuuji@0
|
1598 * elt to mark
|
yuuji@0
|
1599 * fetch flags
|
yuuji@0
|
1600 */
|
yuuji@0
|
1601
|
yuuji@0
|
1602 static void markseen (MAILSTREAM *stream,MESSAGECACHE *elt,long flags)
|
yuuji@0
|
1603 {
|
yuuji@0
|
1604 unsigned long i;
|
yuuji@0
|
1605 char sequence[20];
|
yuuji@0
|
1606 MESSAGECACHE *e;
|
yuuji@0
|
1607 /* non-peeking and needs to set \Seen? */
|
yuuji@0
|
1608 if (!(flags & FT_PEEK) && !elt->seen) {
|
yuuji@0
|
1609 if (stream->dtb->flagmsg){ /* driver wants per-message call? */
|
yuuji@0
|
1610 elt->valid = NIL; /* do pre-alteration driver call */
|
yuuji@0
|
1611 (*stream->dtb->flagmsg) (stream,elt);
|
yuuji@0
|
1612 /* set seen, do post-alteration driver call */
|
yuuji@0
|
1613 elt->seen = elt->valid = T;
|
yuuji@0
|
1614 (*stream->dtb->flagmsg) (stream,elt);
|
yuuji@0
|
1615 }
|
yuuji@0
|
1616 if (stream->dtb->flag) { /* driver wants one-time call? */
|
yuuji@0
|
1617 /* better safe than sorry, save seq bits */
|
yuuji@0
|
1618 for (i = 1; i <= stream->nmsgs; i++) {
|
yuuji@0
|
1619 e = mail_elt (stream,i);
|
yuuji@0
|
1620 e->private.sequence = e->sequence;
|
yuuji@0
|
1621 }
|
yuuji@0
|
1622 /* call driver to set the message */
|
yuuji@0
|
1623 sprintf (sequence,"%lu",elt->msgno);
|
yuuji@0
|
1624 (*stream->dtb->flag) (stream,sequence,"\\Seen",ST_SET);
|
yuuji@0
|
1625 /* restore sequence bits */
|
yuuji@0
|
1626 for (i = 1; i <= stream->nmsgs; i++) {
|
yuuji@0
|
1627 e = mail_elt (stream,i);
|
yuuji@0
|
1628 e->sequence = e->private.sequence;
|
yuuji@0
|
1629 }
|
yuuji@0
|
1630 }
|
yuuji@0
|
1631 /* notify mail program of flag change */
|
yuuji@0
|
1632 MM_FLAGS (stream,elt->msgno);
|
yuuji@0
|
1633 }
|
yuuji@0
|
1634 }
|
yuuji@0
|
1635
|
yuuji@0
|
1636 /* Mail fetch message
|
yuuji@0
|
1637 * Accepts: mail stream
|
yuuji@0
|
1638 * message # to fetch
|
yuuji@0
|
1639 * pointer to returned length
|
yuuji@0
|
1640 * flags
|
yuuji@0
|
1641 * Returns: message text
|
yuuji@0
|
1642 */
|
yuuji@0
|
1643
|
yuuji@0
|
1644 char *mail_fetch_message (MAILSTREAM *stream,unsigned long msgno,
|
yuuji@0
|
1645 unsigned long *len,long flags)
|
yuuji@0
|
1646 {
|
yuuji@0
|
1647 GETS_DATA md;
|
yuuji@0
|
1648 SIZEDTEXT *t;
|
yuuji@0
|
1649 STRING bs;
|
yuuji@0
|
1650 MESSAGECACHE *elt;
|
yuuji@0
|
1651 char *s,*u;
|
yuuji@0
|
1652 unsigned long i,j;
|
yuuji@0
|
1653 if (len) *len = 0; /* default return size */
|
yuuji@0
|
1654 if (flags & FT_UID) { /* UID form of call */
|
yuuji@0
|
1655 if (msgno = mail_msgno (stream,msgno)) flags &= ~FT_UID;
|
yuuji@0
|
1656 else return ""; /* must get UID/msgno map first */
|
yuuji@0
|
1657 }
|
yuuji@0
|
1658 /* initialize message data identifier */
|
yuuji@0
|
1659 INIT_GETS (md,stream,msgno,"",0,0);
|
yuuji@0
|
1660 /* is data already cached? */
|
yuuji@0
|
1661 if ((t = &(elt = mail_elt (stream,msgno))->private.msg.full.text)->data) {
|
yuuji@0
|
1662 markseen (stream,elt,flags);/* mark message seen */
|
yuuji@0
|
1663 return mail_fetch_text_return (&md,t,len);
|
yuuji@0
|
1664 }
|
yuuji@0
|
1665 if (!stream->dtb) return ""; /* not in cache, must have live driver */
|
yuuji@0
|
1666 if (stream->dtb->msgdata) return
|
yuuji@0
|
1667 ((*stream->dtb->msgdata) (stream,msgno,"",0,0,NIL,flags) && t->data) ?
|
yuuji@0
|
1668 mail_fetch_text_return (&md,t,len) : "";
|
yuuji@0
|
1669 /* ugh, have to do this the crufty way */
|
yuuji@0
|
1670 u = mail_fetch_header (stream,msgno,NIL,NIL,&i,flags);
|
yuuji@0
|
1671 /* copy in case text method stomps on it */
|
yuuji@0
|
1672 s = (char *) memcpy (fs_get ((size_t) i),u,(size_t) i);
|
yuuji@0
|
1673 if ((*stream->dtb->text) (stream,msgno,&bs,flags)) {
|
yuuji@0
|
1674 t = &stream->text; /* build combined copy */
|
yuuji@0
|
1675 if (t->data) fs_give ((void **) &t->data);
|
yuuji@0
|
1676 t->data = (unsigned char *) fs_get ((t->size = i + SIZE (&bs)) + 1);
|
yuuji@0
|
1677 if (!elt->rfc822_size) elt->rfc822_size = t->size;
|
yuuji@0
|
1678 else if (elt->rfc822_size != t->size) {
|
yuuji@0
|
1679 char tmp[MAILTMPLEN];
|
yuuji@0
|
1680 sprintf (tmp,"Calculated RFC822.SIZE (%lu) != reported size (%lu)",
|
yuuji@0
|
1681 t->size,elt->rfc822_size);
|
yuuji@0
|
1682 mm_log (tmp,WARN); /* bug trap */
|
yuuji@0
|
1683 }
|
yuuji@0
|
1684 memcpy (t->data,s,(size_t) i);
|
yuuji@0
|
1685 for (u = (char *) t->data + i, j = SIZE (&bs); j;) {
|
yuuji@0
|
1686 memcpy (u,bs.curpos,bs.cursize);
|
yuuji@0
|
1687 u += bs.cursize; /* update text */
|
yuuji@0
|
1688 j -= bs.cursize;
|
yuuji@0
|
1689 bs.curpos += (bs.cursize -1);
|
yuuji@0
|
1690 bs.cursize = 0;
|
yuuji@0
|
1691 (*bs.dtb->next) (&bs); /* advance to next buffer's worth */
|
yuuji@0
|
1692 }
|
yuuji@0
|
1693 *u = '\0'; /* tie off data */
|
yuuji@0
|
1694 u = mail_fetch_text_return (&md,t,len);
|
yuuji@0
|
1695 }
|
yuuji@0
|
1696 else u = "";
|
yuuji@0
|
1697 fs_give ((void **) &s); /* finished with copy of header */
|
yuuji@0
|
1698 return u;
|
yuuji@0
|
1699 }
|
yuuji@0
|
1700
|
yuuji@0
|
1701 /* Mail fetch message header
|
yuuji@0
|
1702 * Accepts: mail stream
|
yuuji@0
|
1703 * message # to fetch
|
yuuji@0
|
1704 * MIME section specifier (#.#.#...#)
|
yuuji@0
|
1705 * list of lines to fetch
|
yuuji@0
|
1706 * pointer to returned length
|
yuuji@0
|
1707 * flags
|
yuuji@0
|
1708 * Returns: message header in RFC822 format
|
yuuji@0
|
1709 *
|
yuuji@0
|
1710 * Note: never calls a mailgets routine
|
yuuji@0
|
1711 */
|
yuuji@0
|
1712
|
yuuji@0
|
1713 char *mail_fetch_header (MAILSTREAM *stream,unsigned long msgno,char *section,
|
yuuji@0
|
1714 STRINGLIST *lines,unsigned long *len,long flags)
|
yuuji@0
|
1715 {
|
yuuji@0
|
1716 STRING bs;
|
yuuji@0
|
1717 BODY *b = NIL;
|
yuuji@0
|
1718 SIZEDTEXT *t = NIL,rt;
|
yuuji@0
|
1719 MESSAGE *m = NIL;
|
yuuji@0
|
1720 MESSAGECACHE *elt;
|
yuuji@0
|
1721 char tmp[MAILTMPLEN];
|
yuuji@0
|
1722 if (len) *len = 0; /* default return size */
|
yuuji@0
|
1723 if (section && (strlen (section) > (MAILTMPLEN - 20))) return "";
|
yuuji@0
|
1724 if (flags & FT_UID) { /* UID form of call */
|
yuuji@0
|
1725 if (msgno = mail_msgno (stream,msgno)) flags &= ~FT_UID;
|
yuuji@0
|
1726 else return ""; /* must get UID/msgno map first */
|
yuuji@0
|
1727 }
|
yuuji@0
|
1728 elt = mail_elt (stream,msgno);/* get cache data */
|
yuuji@0
|
1729 if (section && *section) { /* nested body header wanted? */
|
yuuji@0
|
1730 if (!((b = mail_body (stream,msgno,section)) &&
|
yuuji@0
|
1731 (b->type == TYPEMESSAGE) && !strcmp (b->subtype,"RFC822")))
|
yuuji@0
|
1732 return ""; /* lose if no body or not MESSAGE/RFC822 */
|
yuuji@0
|
1733 m = b->nested.msg; /* point to nested message */
|
yuuji@0
|
1734 }
|
yuuji@0
|
1735 /* else top-level message header wanted */
|
yuuji@0
|
1736 else m = &elt->private.msg;
|
yuuji@0
|
1737 if (m->header.text.data && mail_match_lines (lines,m->lines,flags)) {
|
yuuji@0
|
1738 if (lines) textcpy (t = &stream->text,&m->header.text);
|
yuuji@0
|
1739 else t = &m->header.text; /* in cache, and cache is valid */
|
yuuji@0
|
1740 markseen (stream,elt,flags);/* mark message seen */
|
yuuji@0
|
1741 }
|
yuuji@0
|
1742
|
yuuji@0
|
1743 else if (stream->dtb) { /* not in cache, has live driver? */
|
yuuji@0
|
1744 if (stream->dtb->msgdata) { /* has driver section fetch? */
|
yuuji@0
|
1745 /* build driver section specifier */
|
yuuji@0
|
1746 if (section && *section) sprintf (tmp,"%s.HEADER",section);
|
yuuji@0
|
1747 else strcpy (tmp,"HEADER");
|
yuuji@0
|
1748 if ((*stream->dtb->msgdata) (stream,msgno,tmp,0,0,lines,flags)) {
|
yuuji@0
|
1749 t = &m->header.text; /* fetch data */
|
yuuji@0
|
1750 /* don't need to postprocess lines */
|
yuuji@0
|
1751 if (m->lines) lines = NIL;
|
yuuji@0
|
1752 else if (lines) textcpy (t = &stream->text,&m->header.text);
|
yuuji@0
|
1753 }
|
yuuji@0
|
1754 }
|
yuuji@0
|
1755 else if (b) { /* nested body wanted? */
|
yuuji@0
|
1756 if (stream->private.search.text) {
|
yuuji@0
|
1757 rt.data = (unsigned char *) stream->private.search.text +
|
yuuji@0
|
1758 b->nested.msg->header.offset;
|
yuuji@0
|
1759 rt.size = b->nested.msg->header.text.size;
|
yuuji@0
|
1760 t = &rt;
|
yuuji@0
|
1761 }
|
yuuji@0
|
1762 else if ((*stream->dtb->text) (stream,msgno,&bs,flags & ~FT_INTERNAL)) {
|
yuuji@0
|
1763 if ((bs.dtb->next == mail_string_next) && !lines) {
|
yuuji@0
|
1764 rt.data = (unsigned char *) bs.curpos + b->nested.msg->header.offset;
|
yuuji@0
|
1765 rt.size = b->nested.msg->header.text.size;
|
yuuji@0
|
1766 if (stream->private.search.string)
|
yuuji@0
|
1767 stream->private.search.text = bs.curpos;
|
yuuji@0
|
1768 t = &rt; /* special hack to avoid extra copy */
|
yuuji@0
|
1769 }
|
yuuji@0
|
1770 else textcpyoffstring (t = &stream->text,&bs,
|
yuuji@0
|
1771 b->nested.msg->header.offset,
|
yuuji@0
|
1772 b->nested.msg->header.text.size);
|
yuuji@0
|
1773 }
|
yuuji@0
|
1774 }
|
yuuji@0
|
1775 else { /* top-level header fetch */
|
yuuji@0
|
1776 /* mark message seen */
|
yuuji@0
|
1777 markseen (stream,elt,flags);
|
yuuji@0
|
1778 if (rt.data = (unsigned char *)
|
yuuji@0
|
1779 (*stream->dtb->header) (stream,msgno,&rt.size,flags)) {
|
yuuji@0
|
1780 /* make a safe copy if need to filter */
|
yuuji@0
|
1781 if (lines) textcpy (t = &stream->text,&rt);
|
yuuji@0
|
1782 else t = &rt; /* top level header */
|
yuuji@0
|
1783 }
|
yuuji@0
|
1784 }
|
yuuji@0
|
1785 }
|
yuuji@0
|
1786 if (!t || !t->data) return "";/* error if no string */
|
yuuji@0
|
1787 /* filter headers if requested */
|
yuuji@0
|
1788 if (lines) t->size = mail_filter ((char *) t->data,t->size,lines,flags);
|
yuuji@0
|
1789 if (len) *len = t->size; /* return size if requested */
|
yuuji@0
|
1790 return (char *) t->data; /* and text */
|
yuuji@0
|
1791 }
|
yuuji@0
|
1792
|
yuuji@0
|
1793 /* Mail fetch message text
|
yuuji@0
|
1794 * Accepts: mail stream
|
yuuji@0
|
1795 * message # to fetch
|
yuuji@0
|
1796 * MIME section specifier (#.#.#...#)
|
yuuji@0
|
1797 * pointer to returned length
|
yuuji@0
|
1798 * flags
|
yuuji@0
|
1799 * Returns: message text
|
yuuji@0
|
1800 */
|
yuuji@0
|
1801
|
yuuji@0
|
1802 char *mail_fetch_text (MAILSTREAM *stream,unsigned long msgno,char *section,
|
yuuji@0
|
1803 unsigned long *len,long flags)
|
yuuji@0
|
1804 {
|
yuuji@0
|
1805 GETS_DATA md;
|
yuuji@0
|
1806 PARTTEXT *p;
|
yuuji@0
|
1807 STRING bs;
|
yuuji@0
|
1808 MESSAGECACHE *elt;
|
yuuji@0
|
1809 BODY *b = NIL;
|
yuuji@0
|
1810 char tmp[MAILTMPLEN];
|
yuuji@0
|
1811 unsigned long i;
|
yuuji@0
|
1812 if (len) *len = 0; /* default return size */
|
yuuji@0
|
1813 memset (&stream->private.string,NIL,sizeof (STRING));
|
yuuji@0
|
1814 if (section && (strlen (section) > (MAILTMPLEN - 20))) return "";
|
yuuji@0
|
1815 if (flags & FT_UID) { /* UID form of call */
|
yuuji@0
|
1816 if (msgno = mail_msgno (stream,msgno)) flags &= ~FT_UID;
|
yuuji@0
|
1817 else return ""; /* must get UID/msgno map first */
|
yuuji@0
|
1818 }
|
yuuji@0
|
1819 elt = mail_elt (stream,msgno);/* get cache data */
|
yuuji@0
|
1820 if (section && *section) { /* nested body text wanted? */
|
yuuji@0
|
1821 if (!((b = mail_body (stream,msgno,section)) &&
|
yuuji@0
|
1822 (b->type == TYPEMESSAGE) && !strcmp (b->subtype,"RFC822")))
|
yuuji@0
|
1823 return ""; /* lose if no body or not MESSAGE/RFC822 */
|
yuuji@0
|
1824 p = &b->nested.msg->text; /* point at nested message */
|
yuuji@0
|
1825 /* build IMAP-format section specifier */
|
yuuji@0
|
1826 sprintf (tmp,"%s.TEXT",section);
|
yuuji@0
|
1827 flags &= ~FT_INTERNAL; /* can't win with this set */
|
yuuji@0
|
1828 }
|
yuuji@0
|
1829 else { /* top-level message text wanted */
|
yuuji@0
|
1830 p = &elt->private.msg.text;
|
yuuji@0
|
1831 strcpy (tmp,"TEXT");
|
yuuji@0
|
1832 }
|
yuuji@0
|
1833 /* initialize message data identifier */
|
yuuji@0
|
1834 INIT_GETS (md,stream,msgno,section,0,0);
|
yuuji@0
|
1835 if (p->text.data) { /* is data already cached? */
|
yuuji@0
|
1836 markseen (stream,elt,flags);/* mark message seen */
|
yuuji@0
|
1837 return mail_fetch_text_return (&md,&p->text,len);
|
yuuji@0
|
1838 }
|
yuuji@0
|
1839 if (!stream->dtb) return ""; /* not in cache, must have live driver */
|
yuuji@0
|
1840 if (stream->dtb->msgdata) return
|
yuuji@0
|
1841 ((*stream->dtb->msgdata) (stream,msgno,tmp,0,0,NIL,flags) && p->text.data)?
|
yuuji@0
|
1842 mail_fetch_text_return (&md,&p->text,len) : "";
|
yuuji@0
|
1843 if (!(*stream->dtb->text) (stream,msgno,&bs,flags)) return "";
|
yuuji@0
|
1844 if (section && *section) { /* nested is more complex */
|
yuuji@0
|
1845 SETPOS (&bs,p->offset);
|
yuuji@0
|
1846 i = p->text.size; /* just want this much */
|
yuuji@0
|
1847 }
|
yuuji@0
|
1848 else i = SIZE (&bs); /* want entire text */
|
yuuji@0
|
1849 return mail_fetch_string_return (&md,&bs,i,len,flags);
|
yuuji@0
|
1850 }
|
yuuji@0
|
1851
|
yuuji@0
|
1852 /* Mail fetch message body part MIME headers
|
yuuji@0
|
1853 * Accepts: mail stream
|
yuuji@0
|
1854 * message # to fetch
|
yuuji@0
|
1855 * MIME section specifier (#.#.#...#)
|
yuuji@0
|
1856 * pointer to returned length
|
yuuji@0
|
1857 * flags
|
yuuji@0
|
1858 * Returns: message text
|
yuuji@0
|
1859 */
|
yuuji@0
|
1860
|
yuuji@0
|
1861 char *mail_fetch_mime (MAILSTREAM *stream,unsigned long msgno,char *section,
|
yuuji@0
|
1862 unsigned long *len,long flags)
|
yuuji@0
|
1863 {
|
yuuji@0
|
1864 PARTTEXT *p;
|
yuuji@0
|
1865 STRING bs;
|
yuuji@0
|
1866 BODY *b;
|
yuuji@0
|
1867 char tmp[MAILTMPLEN];
|
yuuji@0
|
1868 if (len) *len = 0; /* default return size */
|
yuuji@0
|
1869 if (section && (strlen (section) > (MAILTMPLEN - 20))) return "";
|
yuuji@0
|
1870 if (flags & FT_UID) { /* UID form of call */
|
yuuji@0
|
1871 if (msgno = mail_msgno (stream,msgno)) flags &= ~FT_UID;
|
yuuji@0
|
1872 else return ""; /* must get UID/msgno map first */
|
yuuji@0
|
1873 }
|
yuuji@0
|
1874 flags &= ~FT_INTERNAL; /* can't win with this set */
|
yuuji@0
|
1875 if (!(section && *section && (b = mail_body (stream,msgno,section))))
|
yuuji@0
|
1876 return ""; /* not valid section */
|
yuuji@0
|
1877 /* in cache? */
|
yuuji@0
|
1878 if ((p = &b->mime)->text.data) {
|
yuuji@0
|
1879 /* mark message seen */
|
yuuji@0
|
1880 markseen (stream,mail_elt (stream,msgno),flags);
|
yuuji@0
|
1881 if (len) *len = p->text.size;
|
yuuji@0
|
1882 return (char *) p->text.data;
|
yuuji@0
|
1883 }
|
yuuji@0
|
1884 if (!stream->dtb) return ""; /* not in cache, must have live driver */
|
yuuji@0
|
1885 if (stream->dtb->msgdata) { /* has driver fetch? */
|
yuuji@0
|
1886 /* build driver section specifier */
|
yuuji@0
|
1887 sprintf (tmp,"%s.MIME",section);
|
yuuji@0
|
1888 if ((*stream->dtb->msgdata) (stream,msgno,tmp,0,0,NIL,flags) &&
|
yuuji@0
|
1889 p->text.data) {
|
yuuji@0
|
1890 if (len) *len = p->text.size;
|
yuuji@0
|
1891 return (char *) p->text.data;
|
yuuji@0
|
1892 }
|
yuuji@0
|
1893 else return "";
|
yuuji@0
|
1894 }
|
yuuji@0
|
1895 if (len) *len = b->mime.text.size;
|
yuuji@0
|
1896 if (!b->mime.text.size) { /* empty MIME header -- mark seen anyway */
|
yuuji@0
|
1897 markseen (stream,mail_elt (stream,msgno),flags);
|
yuuji@0
|
1898 return "";
|
yuuji@0
|
1899 }
|
yuuji@0
|
1900 /* have to get it from offset */
|
yuuji@0
|
1901 if (stream->private.search.text)
|
yuuji@0
|
1902 return stream->private.search.text + b->mime.offset;
|
yuuji@0
|
1903 if (!(*stream->dtb->text) (stream,msgno,&bs,flags)) {
|
yuuji@0
|
1904 if (len) *len = 0;
|
yuuji@0
|
1905 return "";
|
yuuji@0
|
1906 }
|
yuuji@0
|
1907 if (bs.dtb->next == mail_string_next) {
|
yuuji@0
|
1908 if (stream->private.search.string) stream->private.search.text = bs.curpos;
|
yuuji@0
|
1909 return bs.curpos + b->mime.offset;
|
yuuji@0
|
1910 }
|
yuuji@0
|
1911 return textcpyoffstring (&stream->text,&bs,b->mime.offset,b->mime.text.size);
|
yuuji@0
|
1912 }
|
yuuji@0
|
1913
|
yuuji@0
|
1914 /* Mail fetch message body part
|
yuuji@0
|
1915 * Accepts: mail stream
|
yuuji@0
|
1916 * message # to fetch
|
yuuji@0
|
1917 * MIME section specifier (#.#.#...#)
|
yuuji@0
|
1918 * pointer to returned length
|
yuuji@0
|
1919 * flags
|
yuuji@0
|
1920 * Returns: message body
|
yuuji@0
|
1921 */
|
yuuji@0
|
1922
|
yuuji@0
|
1923 char *mail_fetch_body (MAILSTREAM *stream,unsigned long msgno,char *section,
|
yuuji@0
|
1924 unsigned long *len,long flags)
|
yuuji@0
|
1925 {
|
yuuji@0
|
1926 GETS_DATA md;
|
yuuji@0
|
1927 PARTTEXT *p;
|
yuuji@0
|
1928 STRING bs;
|
yuuji@0
|
1929 BODY *b;
|
yuuji@0
|
1930 SIZEDTEXT *t;
|
yuuji@0
|
1931 char *s,tmp[MAILTMPLEN];
|
yuuji@0
|
1932 memset (&stream->private.string,NIL,sizeof (STRING));
|
yuuji@0
|
1933 if (!(section && *section)) /* top-level text wanted? */
|
yuuji@0
|
1934 return mail_fetch_message (stream,msgno,len,flags);
|
yuuji@0
|
1935 else if (strlen (section) > (MAILTMPLEN - 20)) return "";
|
yuuji@0
|
1936 flags &= ~FT_INTERNAL; /* can't win with this set */
|
yuuji@0
|
1937 /* initialize message data identifier */
|
yuuji@0
|
1938 INIT_GETS (md,stream,msgno,section,0,0);
|
yuuji@0
|
1939 /* kludge for old section 0 header */
|
yuuji@0
|
1940 if (!strcmp (s = strcpy (tmp,section),"0") ||
|
yuuji@0
|
1941 ((s = strstr (tmp,".0")) && !s[2])) {
|
yuuji@0
|
1942 SIZEDTEXT ht;
|
yuuji@0
|
1943 *s = '\0'; /* tie off section */
|
yuuji@0
|
1944 /* this silly way so it does mailgets */
|
yuuji@0
|
1945 ht.data = (unsigned char *) mail_fetch_header (stream,msgno,
|
yuuji@0
|
1946 tmp[0] ? tmp : NIL,NIL,
|
yuuji@0
|
1947 &ht.size,flags);
|
yuuji@0
|
1948 /* may have UIDs here */
|
yuuji@0
|
1949 md.flags = (flags & FT_UID) ? MG_UID : NIL;
|
yuuji@0
|
1950 return mail_fetch_text_return (&md,&ht,len);
|
yuuji@0
|
1951 }
|
yuuji@0
|
1952 if (len) *len = 0; /* default return size */
|
yuuji@0
|
1953 if (flags & FT_UID) { /* UID form of call */
|
yuuji@0
|
1954 if (msgno = mail_msgno (stream,msgno)) flags &= ~FT_UID;
|
yuuji@0
|
1955 else return ""; /* must get UID/msgno map first */
|
yuuji@0
|
1956 }
|
yuuji@0
|
1957 /* must have body */
|
yuuji@0
|
1958 if (!(b = mail_body (stream,msgno,section))) return "";
|
yuuji@0
|
1959 /* have cached text? */
|
yuuji@0
|
1960 if ((t = &(p = &b->contents)->text)->data) {
|
yuuji@0
|
1961 /* mark message seen */
|
yuuji@0
|
1962 markseen (stream,mail_elt (stream,msgno),flags);
|
yuuji@0
|
1963 return mail_fetch_text_return (&md,t,len);
|
yuuji@0
|
1964 }
|
yuuji@0
|
1965 if (!stream->dtb) return ""; /* not in cache, must have live driver */
|
yuuji@0
|
1966 if (stream->dtb->msgdata) return
|
yuuji@0
|
1967 ((*stream->dtb->msgdata)(stream,msgno,section,0,0,NIL,flags) && t->data) ?
|
yuuji@0
|
1968 mail_fetch_text_return (&md,t,len) : "";
|
yuuji@0
|
1969 if (len) *len = t->size;
|
yuuji@0
|
1970 if (!t->size) { /* empty body part -- mark seen anyway */
|
yuuji@0
|
1971 markseen (stream,mail_elt (stream,msgno),flags);
|
yuuji@0
|
1972 return "";
|
yuuji@0
|
1973 }
|
yuuji@0
|
1974 /* copy body from stringstruct offset */
|
yuuji@0
|
1975 if (stream->private.search.text)
|
yuuji@0
|
1976 return stream->private.search.text + p->offset;
|
yuuji@0
|
1977 if (!(*stream->dtb->text) (stream,msgno,&bs,flags)) {
|
yuuji@0
|
1978 if (len) *len = 0;
|
yuuji@0
|
1979 return "";
|
yuuji@0
|
1980 }
|
yuuji@0
|
1981 if (bs.dtb->next == mail_string_next) {
|
yuuji@0
|
1982 if (stream->private.search.string) stream->private.search.text = bs.curpos;
|
yuuji@0
|
1983 return bs.curpos + p->offset;
|
yuuji@0
|
1984 }
|
yuuji@0
|
1985 SETPOS (&bs,p->offset);
|
yuuji@0
|
1986 return mail_fetch_string_return (&md,&bs,t->size,len,flags);
|
yuuji@0
|
1987 }
|
yuuji@0
|
1988
|
yuuji@0
|
1989 /* Mail fetch partial message text
|
yuuji@0
|
1990 * Accepts: mail stream
|
yuuji@0
|
1991 * message # to fetch
|
yuuji@0
|
1992 * MIME section specifier (#.#.#...#)
|
yuuji@0
|
1993 * offset of first designed byte or 0 to start at beginning
|
yuuji@0
|
1994 * maximum number of bytes or 0 for all bytes
|
yuuji@0
|
1995 * flags
|
yuuji@0
|
1996 * Returns: T if successful, else NIL
|
yuuji@0
|
1997 */
|
yuuji@0
|
1998
|
yuuji@0
|
1999 long mail_partial_text (MAILSTREAM *stream,unsigned long msgno,char *section,
|
yuuji@0
|
2000 unsigned long first,unsigned long last,long flags)
|
yuuji@0
|
2001 {
|
yuuji@0
|
2002 GETS_DATA md;
|
yuuji@0
|
2003 PARTTEXT *p = NIL;
|
yuuji@0
|
2004 MESSAGECACHE *elt;
|
yuuji@0
|
2005 STRING bs;
|
yuuji@0
|
2006 BODY *b;
|
yuuji@0
|
2007 char tmp[MAILTMPLEN];
|
yuuji@0
|
2008 unsigned long i;
|
yuuji@0
|
2009 if (!mailgets) fatal ("mail_partial_text() called without a mailgets!");
|
yuuji@0
|
2010 if (section && (strlen (section) > (MAILTMPLEN - 20))) return NIL;
|
yuuji@0
|
2011 if (flags & FT_UID) { /* UID form of call */
|
yuuji@0
|
2012 if (msgno = mail_msgno (stream,msgno)) flags &= ~FT_UID;
|
yuuji@0
|
2013 else return NIL; /* must get UID/msgno map first */
|
yuuji@0
|
2014 }
|
yuuji@0
|
2015 elt = mail_elt (stream,msgno);/* get cache data */
|
yuuji@0
|
2016 flags &= ~FT_INTERNAL; /* bogus if this is set */
|
yuuji@0
|
2017 if (section && *section) { /* nested body text wanted? */
|
yuuji@0
|
2018 if (!((b = mail_body (stream,msgno,section)) &&
|
yuuji@0
|
2019 (b->type == TYPEMESSAGE) && !strcmp (b->subtype,"RFC822")))
|
yuuji@0
|
2020 return NIL; /* lose if no body or not MESSAGE/RFC822 */
|
yuuji@0
|
2021 p = &b->nested.msg->text; /* point at nested message */
|
yuuji@0
|
2022 /* build IMAP-format section specifier */
|
yuuji@0
|
2023 sprintf (tmp,"%s.TEXT",section);
|
yuuji@0
|
2024 }
|
yuuji@0
|
2025 else { /* else top-level message text wanted */
|
yuuji@0
|
2026 p = &elt->private.msg.text;
|
yuuji@0
|
2027 strcpy (tmp,"TEXT");
|
yuuji@0
|
2028 }
|
yuuji@0
|
2029
|
yuuji@0
|
2030 /* initialize message data identifier */
|
yuuji@0
|
2031 INIT_GETS (md,stream,msgno,tmp,first,last);
|
yuuji@0
|
2032 if (p->text.data) { /* is data already cached? */
|
yuuji@0
|
2033 INIT (&bs,mail_string,p->text.data,i = p->text.size);
|
yuuji@0
|
2034 markseen (stream,elt,flags);/* mark message seen */
|
yuuji@0
|
2035 }
|
yuuji@0
|
2036 else { /* else get data from driver */
|
yuuji@0
|
2037 if (!stream->dtb) return NIL;
|
yuuji@0
|
2038 if (stream->dtb->msgdata) /* driver will handle this */
|
yuuji@0
|
2039 return (*stream->dtb->msgdata) (stream,msgno,tmp,first,last,NIL,flags);
|
yuuji@0
|
2040 if (!(*stream->dtb->text) (stream,msgno,&bs,flags)) return NIL;
|
yuuji@0
|
2041 if (section && *section) { /* nexted if more complex */
|
yuuji@0
|
2042 SETPOS (&bs,p->offset); /* offset stringstruct to data */
|
yuuji@0
|
2043 i = p->text.size; /* maximum size of data */
|
yuuji@0
|
2044 }
|
yuuji@0
|
2045 else i = SIZE (&bs); /* just want this much */
|
yuuji@0
|
2046 }
|
yuuji@0
|
2047 if (i <= first) i = first = 0;/* first byte is beyond end of text */
|
yuuji@0
|
2048 /* truncate as needed */
|
yuuji@0
|
2049 else { /* offset and truncate */
|
yuuji@0
|
2050 SETPOS (&bs,first + GETPOS (&bs));
|
yuuji@0
|
2051 i -= first; /* reduced size */
|
yuuji@0
|
2052 if (last && (i > last)) i = last;
|
yuuji@0
|
2053 }
|
yuuji@0
|
2054 /* do the mailgets thing */
|
yuuji@0
|
2055 (*mailgets) (mail_read,&bs,i,&md);
|
yuuji@0
|
2056 return T; /* success */
|
yuuji@0
|
2057 }
|
yuuji@0
|
2058
|
yuuji@0
|
2059 /* Mail fetch partial message body part
|
yuuji@0
|
2060 * Accepts: mail stream
|
yuuji@0
|
2061 * message # to fetch
|
yuuji@0
|
2062 * MIME section specifier (#.#.#...#)
|
yuuji@0
|
2063 * offset of first designed byte or 0 to start at beginning
|
yuuji@0
|
2064 * maximum number of bytes or 0 for all bytes
|
yuuji@0
|
2065 * flags
|
yuuji@0
|
2066 * Returns: T if successful, else NIL
|
yuuji@0
|
2067 */
|
yuuji@0
|
2068
|
yuuji@0
|
2069 long mail_partial_body (MAILSTREAM *stream,unsigned long msgno,char *section,
|
yuuji@0
|
2070 unsigned long first,unsigned long last,long flags)
|
yuuji@0
|
2071 {
|
yuuji@0
|
2072 GETS_DATA md;
|
yuuji@0
|
2073 PARTTEXT *p;
|
yuuji@0
|
2074 STRING bs;
|
yuuji@0
|
2075 BODY *b;
|
yuuji@0
|
2076 SIZEDTEXT *t;
|
yuuji@0
|
2077 unsigned long i;
|
yuuji@0
|
2078 if (!(section && *section)) /* top-level text wanted? */
|
yuuji@0
|
2079 return mail_partial_text (stream,msgno,NIL,first,last,flags);
|
yuuji@0
|
2080 if (!mailgets) fatal ("mail_partial_body() called without a mailgets!");
|
yuuji@0
|
2081 if (flags & FT_UID) { /* UID form of call */
|
yuuji@0
|
2082 if (msgno = mail_msgno (stream,msgno)) flags &= ~FT_UID;
|
yuuji@0
|
2083 else return NIL; /* must get UID/msgno map first */
|
yuuji@0
|
2084 }
|
yuuji@0
|
2085 /* must have body */
|
yuuji@0
|
2086 if (!(b = mail_body (stream,msgno,section))) return NIL;
|
yuuji@0
|
2087 flags &= ~FT_INTERNAL; /* bogus if this is set */
|
yuuji@0
|
2088
|
yuuji@0
|
2089 /* initialize message data identifier */
|
yuuji@0
|
2090 INIT_GETS (md,stream,msgno,section,first,last);
|
yuuji@0
|
2091 /* have cached text? */
|
yuuji@0
|
2092 if ((t = &(p = &b->contents)->text)->data) {
|
yuuji@0
|
2093 /* mark message seen */
|
yuuji@0
|
2094 markseen (stream,mail_elt (stream,msgno),flags);
|
yuuji@0
|
2095 INIT (&bs,mail_string,t->data,i = t->size);
|
yuuji@0
|
2096 }
|
yuuji@0
|
2097 else { /* else get data from driver */
|
yuuji@0
|
2098 if (!stream->dtb) return NIL;
|
yuuji@0
|
2099 if (stream->dtb->msgdata) /* driver will handle this */
|
yuuji@0
|
2100 return (*stream->dtb->msgdata) (stream,msgno,section,first,last,NIL,
|
yuuji@0
|
2101 flags);
|
yuuji@0
|
2102 if (!(*stream->dtb->text) (stream,msgno,&bs,flags)) return NIL;
|
yuuji@0
|
2103 if (section && *section) { /* nexted if more complex */
|
yuuji@0
|
2104 SETPOS (&bs,p->offset); /* offset stringstruct to data */
|
yuuji@0
|
2105 i = t->size; /* maximum size of data */
|
yuuji@0
|
2106 }
|
yuuji@0
|
2107 else i = SIZE (&bs); /* just want this much */
|
yuuji@0
|
2108 }
|
yuuji@0
|
2109 if (i <= first) i = first = 0;/* first byte is beyond end of text */
|
yuuji@0
|
2110 else { /* offset and truncate */
|
yuuji@0
|
2111 SETPOS (&bs,first + GETPOS (&bs));
|
yuuji@0
|
2112 i -= first; /* reduced size */
|
yuuji@0
|
2113 if (last && (i > last)) i = last;
|
yuuji@0
|
2114 }
|
yuuji@0
|
2115 /* do the mailgets thing */
|
yuuji@0
|
2116 (*mailgets) (mail_read,&bs,i,&md);
|
yuuji@0
|
2117 return T; /* success */
|
yuuji@0
|
2118 }
|
yuuji@0
|
2119
|
yuuji@0
|
2120 /* Mail return message text
|
yuuji@0
|
2121 * Accepts: identifier data
|
yuuji@0
|
2122 * sized text
|
yuuji@0
|
2123 * pointer to returned length
|
yuuji@0
|
2124 * Returns: text
|
yuuji@0
|
2125 */
|
yuuji@0
|
2126
|
yuuji@0
|
2127 char *mail_fetch_text_return (GETS_DATA *md,SIZEDTEXT *t,unsigned long *len)
|
yuuji@0
|
2128 {
|
yuuji@0
|
2129 STRING bs;
|
yuuji@0
|
2130 if (len) *len = t->size; /* return size */
|
yuuji@0
|
2131 if (t->size && mailgets) { /* have to do the mailgets thing? */
|
yuuji@0
|
2132 /* silly but do it anyway for consistency */
|
yuuji@0
|
2133 INIT (&bs,mail_string,t->data,t->size);
|
yuuji@0
|
2134 return (*mailgets) (mail_read,&bs,t->size,md);
|
yuuji@0
|
2135 }
|
yuuji@0
|
2136 return t->size ? (char *) t->data : "";
|
yuuji@0
|
2137 }
|
yuuji@0
|
2138
|
yuuji@0
|
2139
|
yuuji@0
|
2140 /* Mail return message string
|
yuuji@0
|
2141 * Accepts: identifier data
|
yuuji@0
|
2142 * stringstruct
|
yuuji@0
|
2143 * text length
|
yuuji@0
|
2144 * pointer to returned length
|
yuuji@0
|
2145 * flags
|
yuuji@0
|
2146 * Returns: text, or NIL if stringstruct returned
|
yuuji@0
|
2147 */
|
yuuji@0
|
2148
|
yuuji@0
|
2149 char *mail_fetch_string_return (GETS_DATA *md,STRING *bs,unsigned long i,
|
yuuji@0
|
2150 unsigned long *len,long flags)
|
yuuji@0
|
2151 {
|
yuuji@0
|
2152 char *ret = NIL;
|
yuuji@0
|
2153 if (len) *len = i; /* return size */
|
yuuji@0
|
2154 /* return stringstruct hack */
|
yuuji@0
|
2155 if (flags & FT_RETURNSTRINGSTRUCT) {
|
yuuji@0
|
2156 memcpy (&md->stream->private.string,bs,sizeof (STRING));
|
yuuji@0
|
2157 SETPOS (&md->stream->private.string,GETPOS (&md->stream->private.string));
|
yuuji@0
|
2158 }
|
yuuji@0
|
2159 /* have to do the mailgets thing? */
|
yuuji@0
|
2160 else if (mailgets) ret = (*mailgets) (mail_read,bs,i,md);
|
yuuji@0
|
2161 /* special hack to avoid extra copy */
|
yuuji@0
|
2162 else if (bs->dtb->next == mail_string_next) ret = bs->curpos;
|
yuuji@0
|
2163 /* make string copy in memory */
|
yuuji@0
|
2164 else ret = textcpyoffstring (&md->stream->text,bs,GETPOS (bs),i);
|
yuuji@0
|
2165 return ret;
|
yuuji@0
|
2166 }
|
yuuji@0
|
2167
|
yuuji@0
|
2168 /* Read data from stringstruct
|
yuuji@0
|
2169 * Accepts: stringstruct
|
yuuji@0
|
2170 * size of data to read
|
yuuji@0
|
2171 * buffer to read into
|
yuuji@0
|
2172 * Returns: T, always, stringstruct updated
|
yuuji@0
|
2173 */
|
yuuji@0
|
2174
|
yuuji@0
|
2175 long mail_read (void *stream,unsigned long size,char *buffer)
|
yuuji@0
|
2176 {
|
yuuji@0
|
2177 unsigned long i;
|
yuuji@0
|
2178 STRING *s = (STRING *) stream;
|
yuuji@0
|
2179 while (size) { /* until satisfied */
|
yuuji@0
|
2180 memcpy (buffer,s->curpos,i = min (s->cursize,size));
|
yuuji@0
|
2181 buffer += i; /* update buffer */
|
yuuji@0
|
2182 size -= i; /* note that we read this much */
|
yuuji@0
|
2183 s->curpos += --i; /* advance that many spaces minus 1 */
|
yuuji@0
|
2184 s->cursize -= i;
|
yuuji@0
|
2185 SNX (s); /* now use SNX to advance the last byte */
|
yuuji@0
|
2186 }
|
yuuji@0
|
2187 return T;
|
yuuji@0
|
2188 }
|
yuuji@0
|
2189
|
yuuji@0
|
2190 /* Mail fetch UID
|
yuuji@0
|
2191 * Accepts: mail stream
|
yuuji@0
|
2192 * message number
|
yuuji@0
|
2193 * Returns: UID or zero if dead stream
|
yuuji@0
|
2194 */
|
yuuji@0
|
2195
|
yuuji@0
|
2196 unsigned long mail_uid (MAILSTREAM *stream,unsigned long msgno)
|
yuuji@0
|
2197 {
|
yuuji@0
|
2198 unsigned long uid = mail_elt (stream,msgno)->private.uid;
|
yuuji@0
|
2199 return uid ? uid :
|
yuuji@0
|
2200 (stream->dtb && stream->dtb->uid) ? (*stream->dtb->uid) (stream,msgno) : 0;
|
yuuji@0
|
2201 }
|
yuuji@0
|
2202
|
yuuji@0
|
2203
|
yuuji@0
|
2204 /* Mail fetch msgno from UID
|
yuuji@0
|
2205 * Accepts: mail stream
|
yuuji@0
|
2206 * UID
|
yuuji@0
|
2207 * Returns: msgno or zero if failed
|
yuuji@0
|
2208 */
|
yuuji@0
|
2209
|
yuuji@0
|
2210 unsigned long mail_msgno (MAILSTREAM *stream,unsigned long uid)
|
yuuji@0
|
2211 {
|
yuuji@0
|
2212 unsigned long msgno,delta,first,firstuid,last,lastuid,middle,miduid;
|
yuuji@0
|
2213 if (stream->dtb) { /* active stream? */
|
yuuji@0
|
2214 if (stream->dtb->msgno) /* direct way */
|
yuuji@0
|
2215 return (*stream->dtb->msgno) (stream,uid);
|
yuuji@0
|
2216 else if (stream->dtb->uid) {/* indirect way */
|
yuuji@0
|
2217 /* Placeholder for now, since currently there are no drivers which
|
yuuji@0
|
2218 * have a uid method but not a msgno method
|
yuuji@0
|
2219 */
|
yuuji@0
|
2220 for (msgno = 1; msgno <= stream->nmsgs; msgno++)
|
yuuji@0
|
2221 if ((*stream->dtb->uid) (stream,msgno) == uid) return msgno;
|
yuuji@0
|
2222 }
|
yuuji@0
|
2223 /* binary search since have full map */
|
yuuji@0
|
2224 else for (first = 1,last = stream->nmsgs, delta = (first <= last) ? 1 : 0;
|
yuuji@0
|
2225 delta &&
|
yuuji@0
|
2226 (uid >= (firstuid = mail_elt (stream,first)->private.uid)) &&
|
yuuji@0
|
2227 (uid <= (lastuid = mail_elt (stream,last)->private.uid));) {
|
yuuji@0
|
2228 /* done if match at an endpoint */
|
yuuji@0
|
2229 if (uid == firstuid) return first;
|
yuuji@0
|
2230 if (uid == lastuid) return last;
|
yuuji@0
|
2231 /* have anything between endpoints? */
|
yuuji@0
|
2232 if (delta = ((last - first) / 2)) {
|
yuuji@0
|
2233 if ((miduid = mail_elt (stream,middle = first + delta)->private.uid)
|
yuuji@0
|
2234 == uid)
|
yuuji@0
|
2235 return middle; /* found match in middle */
|
yuuji@0
|
2236 else if (uid < miduid) last = middle - 1;
|
yuuji@0
|
2237 else first = middle + 1;
|
yuuji@0
|
2238 }
|
yuuji@0
|
2239 }
|
yuuji@0
|
2240 }
|
yuuji@0
|
2241 else { /* dead stream, do linear search for UID */
|
yuuji@0
|
2242 for (msgno = 1; msgno <= stream->nmsgs; msgno++)
|
yuuji@0
|
2243 if (mail_elt (stream,msgno)->private.uid == uid) return msgno;
|
yuuji@0
|
2244 }
|
yuuji@0
|
2245 return 0; /* didn't find the UID anywhere */
|
yuuji@0
|
2246 }
|
yuuji@0
|
2247
|
yuuji@0
|
2248 /* Mail fetch From string for menu
|
yuuji@0
|
2249 * Accepts: destination string
|
yuuji@0
|
2250 * mail stream
|
yuuji@0
|
2251 * message # to fetch
|
yuuji@0
|
2252 * desired string length
|
yuuji@0
|
2253 * Returns: string of requested length
|
yuuji@0
|
2254 */
|
yuuji@0
|
2255
|
yuuji@0
|
2256 void mail_fetchfrom (char *s,MAILSTREAM *stream,unsigned long msgno,
|
yuuji@0
|
2257 long length)
|
yuuji@0
|
2258 {
|
yuuji@0
|
2259 char *t;
|
yuuji@0
|
2260 char tmp[MAILTMPLEN];
|
yuuji@0
|
2261 ENVELOPE *env = mail_fetchenvelope (stream,msgno);
|
yuuji@0
|
2262 ADDRESS *adr = env ? env->from : NIL;
|
yuuji@0
|
2263 memset (s,' ',(size_t)length);/* fill it with spaces */
|
yuuji@0
|
2264 s[length] = '\0'; /* tie off with null */
|
yuuji@0
|
2265 /* get first from address from envelope */
|
yuuji@0
|
2266 while (adr && !adr->host) adr = adr->next;
|
yuuji@0
|
2267 if (adr) { /* if a personal name exists use it */
|
yuuji@0
|
2268 if (!(t = adr->personal))
|
yuuji@0
|
2269 sprintf (t = tmp,"%.256s@%.256s",adr->mailbox,adr->host);
|
yuuji@0
|
2270 memcpy (s,t,(size_t) min (length,(long) strlen (t)));
|
yuuji@0
|
2271 }
|
yuuji@0
|
2272 }
|
yuuji@0
|
2273
|
yuuji@0
|
2274
|
yuuji@0
|
2275 /* Mail fetch Subject string for menu
|
yuuji@0
|
2276 * Accepts: destination string
|
yuuji@0
|
2277 * mail stream
|
yuuji@0
|
2278 * message # to fetch
|
yuuji@0
|
2279 * desired string length
|
yuuji@0
|
2280 * Returns: string of no more than requested length
|
yuuji@0
|
2281 */
|
yuuji@0
|
2282
|
yuuji@0
|
2283 void mail_fetchsubject (char *s,MAILSTREAM *stream,unsigned long msgno,
|
yuuji@0
|
2284 long length)
|
yuuji@0
|
2285 {
|
yuuji@0
|
2286 ENVELOPE *env = mail_fetchenvelope (stream,msgno);
|
yuuji@0
|
2287 memset (s,'\0',(size_t) length+1);
|
yuuji@0
|
2288 /* copy subject from envelope */
|
yuuji@0
|
2289 if (env && env->subject) strncpy (s,env->subject,(size_t) length);
|
yuuji@0
|
2290 else *s = ' '; /* if no subject then just a space */
|
yuuji@0
|
2291 }
|
yuuji@0
|
2292
|
yuuji@0
|
2293 /* Mail modify flags
|
yuuji@0
|
2294 * Accepts: mail stream
|
yuuji@0
|
2295 * sequence
|
yuuji@0
|
2296 * flag(s)
|
yuuji@0
|
2297 * option flags
|
yuuji@0
|
2298 */
|
yuuji@0
|
2299
|
yuuji@0
|
2300 void mail_flag (MAILSTREAM *stream,char *sequence,char *flag,long flags)
|
yuuji@0
|
2301 {
|
yuuji@0
|
2302 MESSAGECACHE *elt;
|
yuuji@0
|
2303 unsigned long i,uf;
|
yuuji@0
|
2304 long f;
|
yuuji@0
|
2305 short nf;
|
yuuji@0
|
2306 if (!stream->dtb) return; /* no-op if no stream */
|
yuuji@0
|
2307 if ((stream->dtb->flagmsg || !stream->dtb->flag) &&
|
yuuji@0
|
2308 ((flags & ST_UID) ? mail_uid_sequence (stream,sequence) :
|
yuuji@0
|
2309 mail_sequence (stream,sequence)) &&
|
yuuji@0
|
2310 ((f = mail_parse_flags (stream,flag,&uf)) || uf))
|
yuuji@0
|
2311 for (i = 1,nf = (flags & ST_SET) ? T : NIL; i <= stream->nmsgs; i++)
|
yuuji@0
|
2312 if ((elt = mail_elt (stream,i))->sequence) {
|
yuuji@0
|
2313 struct { /* old flags */
|
yuuji@0
|
2314 unsigned int valid : 1;
|
yuuji@0
|
2315 unsigned int seen : 1;
|
yuuji@0
|
2316 unsigned int deleted : 1;
|
yuuji@0
|
2317 unsigned int flagged : 1;
|
yuuji@0
|
2318 unsigned int answered : 1;
|
yuuji@0
|
2319 unsigned int draft : 1;
|
yuuji@0
|
2320 unsigned long user_flags;
|
yuuji@0
|
2321 } old;
|
yuuji@0
|
2322 old.valid = elt->valid; old.seen = elt->seen;
|
yuuji@0
|
2323 old.deleted = elt->deleted; old.flagged = elt->flagged;
|
yuuji@0
|
2324 old.answered = elt->answered; old.draft = elt->draft;
|
yuuji@0
|
2325 old.user_flags = elt->user_flags;
|
yuuji@0
|
2326 elt->valid = NIL; /* prepare for flag alteration */
|
yuuji@0
|
2327 if (stream->dtb->flagmsg) (*stream->dtb->flagmsg) (stream,elt);
|
yuuji@0
|
2328 if (f&fSEEN) elt->seen = nf;
|
yuuji@0
|
2329 if (f&fDELETED) elt->deleted = nf;
|
yuuji@0
|
2330 if (f&fFLAGGED) elt->flagged = nf;
|
yuuji@0
|
2331 if (f&fANSWERED) elt->answered = nf;
|
yuuji@0
|
2332 if (f&fDRAFT) elt->draft = nf;
|
yuuji@0
|
2333 /* user flags */
|
yuuji@0
|
2334 if (flags & ST_SET) elt->user_flags |= uf;
|
yuuji@0
|
2335 else elt->user_flags &= ~uf;
|
yuuji@0
|
2336 elt->valid = T; /* flags now altered */
|
yuuji@0
|
2337 if ((old.valid != elt->valid) || (old.seen != elt->seen) ||
|
yuuji@0
|
2338 (old.deleted != elt->deleted) || (old.flagged != elt->flagged) ||
|
yuuji@0
|
2339 (old.answered != elt->answered) || (old.draft != elt->draft) ||
|
yuuji@0
|
2340 (old.user_flags != elt->user_flags))
|
yuuji@0
|
2341 MM_FLAGS (stream,elt->msgno);
|
yuuji@0
|
2342 if (stream->dtb->flagmsg) (*stream->dtb->flagmsg) (stream,elt);
|
yuuji@0
|
2343 }
|
yuuji@0
|
2344 /* call driver once */
|
yuuji@0
|
2345 if (stream->dtb->flag) (*stream->dtb->flag) (stream,sequence,flag,flags);
|
yuuji@0
|
2346 }
|
yuuji@0
|
2347
|
yuuji@0
|
2348 /* Mail search for messages
|
yuuji@0
|
2349 * Accepts: mail stream
|
yuuji@0
|
2350 * character set
|
yuuji@0
|
2351 * search program
|
yuuji@0
|
2352 * option flags
|
yuuji@0
|
2353 * Returns: T if successful, NIL if dead stream, NIL searchpgm or bad charset
|
yuuji@0
|
2354 */
|
yuuji@0
|
2355
|
yuuji@0
|
2356 long mail_search_full (MAILSTREAM *stream,char *charset,SEARCHPGM *pgm,
|
yuuji@0
|
2357 long flags)
|
yuuji@0
|
2358 {
|
yuuji@0
|
2359 unsigned long i;
|
yuuji@0
|
2360 long ret = NIL;
|
yuuji@0
|
2361 if (!(flags & SE_RETAIN)) /* clear search vector unless retaining */
|
yuuji@0
|
2362 for (i = 1; i <= stream->nmsgs; ++i) mail_elt (stream,i)->searched = NIL;
|
yuuji@0
|
2363 if (pgm && stream->dtb) /* must have a search program and driver */
|
yuuji@0
|
2364 ret = (*(stream->dtb->search ? stream->dtb->search : mail_search_default))
|
yuuji@0
|
2365 (stream,charset,pgm,flags);
|
yuuji@0
|
2366 /* flush search program if requested */
|
yuuji@0
|
2367 if (flags & SE_FREE) mail_free_searchpgm (&pgm);
|
yuuji@0
|
2368 return ret;
|
yuuji@0
|
2369 }
|
yuuji@0
|
2370
|
yuuji@0
|
2371
|
yuuji@0
|
2372 /* Mail search for messages default handler
|
yuuji@0
|
2373 * Accepts: mail stream
|
yuuji@0
|
2374 * character set
|
yuuji@0
|
2375 * search program
|
yuuji@0
|
2376 * option flags
|
yuuji@0
|
2377 * Returns: T if successful, NIL if bad charset
|
yuuji@0
|
2378 */
|
yuuji@0
|
2379
|
yuuji@0
|
2380 long mail_search_default (MAILSTREAM *stream,char *charset,SEARCHPGM *pgm,
|
yuuji@0
|
2381 long flags)
|
yuuji@0
|
2382 {
|
yuuji@0
|
2383 unsigned long i;
|
yuuji@0
|
2384 char *msg;
|
yuuji@0
|
2385 /* make sure that charset is good */
|
yuuji@0
|
2386 if (msg = utf8_badcharset (charset)) {
|
yuuji@0
|
2387 MM_LOG (msg,ERROR); /* output error */
|
yuuji@0
|
2388 fs_give ((void **) &msg);
|
yuuji@0
|
2389 return NIL;
|
yuuji@0
|
2390 }
|
yuuji@0
|
2391 utf8_searchpgm (pgm,charset);
|
yuuji@0
|
2392 for (i = 1; i <= stream->nmsgs; ++i)
|
yuuji@0
|
2393 if (mail_search_msg (stream,i,NIL,pgm)) {
|
yuuji@0
|
2394 if (flags & SE_UID) mm_searched (stream,mail_uid (stream,i));
|
yuuji@0
|
2395 else { /* mark as searched, notify mail program */
|
yuuji@0
|
2396 mail_elt (stream,i)->searched = T;
|
yuuji@0
|
2397 if (!stream->silent) mm_searched (stream,i);
|
yuuji@0
|
2398 }
|
yuuji@0
|
2399 }
|
yuuji@0
|
2400 return LONGT; /* search completed */
|
yuuji@0
|
2401 }
|
yuuji@0
|
2402
|
yuuji@0
|
2403 /* Mail ping mailbox
|
yuuji@0
|
2404 * Accepts: mail stream
|
yuuji@0
|
2405 * Returns: stream if still open else NIL
|
yuuji@0
|
2406 */
|
yuuji@0
|
2407
|
yuuji@0
|
2408 long mail_ping (MAILSTREAM *stream)
|
yuuji@0
|
2409 {
|
yuuji@0
|
2410 unsigned long i,n,uf,len;
|
yuuji@0
|
2411 char *s,*f,tmp[MAILTMPLEN],flags[MAILTMPLEN];
|
yuuji@0
|
2412 MAILSTREAM *snarf;
|
yuuji@0
|
2413 MESSAGECACHE *elt;
|
yuuji@0
|
2414 STRING bs;
|
yuuji@0
|
2415 long ret;
|
yuuji@0
|
2416 /* do driver action */
|
yuuji@0
|
2417 if ((ret = ((stream && stream->dtb) ? (stream->dtb->ping) (stream) : NIL)) &&
|
yuuji@0
|
2418 stream->snarf.name && /* time to snarf? */
|
yuuji@0
|
2419 /* prohibit faster than once/min */
|
yuuji@0
|
2420 (time (0) > (time_t) (stream->snarf.time + min(60,mailsnarfinterval))) &&
|
yuuji@0
|
2421 (snarf = mail_open (NIL,stream->snarf.name,
|
yuuji@0
|
2422 stream->snarf.options | OP_SILENT))) {
|
yuuji@0
|
2423 if ((n = snarf->nmsgs) && /* yes, have messages to snarf? */
|
yuuji@0
|
2424 mail_search_full (snarf,NIL,mail_criteria ("UNDELETED"),SE_FREE)) {
|
yuuji@0
|
2425 for (i = 1; ret && (i <= n); i++) /* for each message */
|
yuuji@0
|
2426 if ((elt = mail_elt (snarf,i))->searched &&
|
yuuji@0
|
2427 (s = mail_fetch_message (snarf,i,&len,FT_PEEK)) && len) {
|
yuuji@0
|
2428 INIT (&bs,mail_string,s,len);
|
yuuji@0
|
2429 if (mailsnarfpreserve) {
|
yuuji@0
|
2430 /* yes, make sure have fast data */
|
yuuji@0
|
2431 if (!elt->valid || !elt->day) {
|
yuuji@0
|
2432 sprintf (tmp,"%lu",n);
|
yuuji@0
|
2433 mail_fetch_fast (snarf,tmp,NIL);
|
yuuji@0
|
2434 }
|
yuuji@0
|
2435 /* initialize flag string */
|
yuuji@0
|
2436 memset (flags,0,MAILTMPLEN);
|
yuuji@0
|
2437 /* output system flags except \Deleted */
|
yuuji@0
|
2438 if (elt->seen) strcat (flags," \\Seen");
|
yuuji@0
|
2439 if (elt->flagged) strcat (flags," \\Flagged");
|
yuuji@0
|
2440 if (elt->answered) strcat (flags," \\Answered");
|
yuuji@0
|
2441 if (elt->draft) strcat (flags," \\Draft");
|
yuuji@0
|
2442 /* any user flags? */
|
yuuji@0
|
2443 for (uf = elt->user_flags,s = flags + strlen (flags);
|
yuuji@0
|
2444 uf && (f = stream->user_flags[find_rightmost_bit (&uf)]) &&
|
yuuji@0
|
2445 ((MAILTMPLEN - (s - tmp)) > (long) (2 + strlen (f)));
|
yuuji@0
|
2446 s += strlen (s)) sprintf (s," %s",f);
|
yuuji@0
|
2447 ret = mail_append_full (stream,stream->mailbox,flags + 1,
|
yuuji@0
|
2448 mail_date (tmp,elt),&bs);
|
yuuji@0
|
2449 }
|
yuuji@0
|
2450 else ret = mail_append (stream,stream->mailbox,&bs);
|
yuuji@0
|
2451
|
yuuji@0
|
2452 if (ret) { /* did snarf succeed? */
|
yuuji@0
|
2453 /* driver has per-message (or no) flag call */
|
yuuji@0
|
2454 if (snarf->dtb->flagmsg || !snarf->dtb->flag) {
|
yuuji@0
|
2455 elt->valid = NIL; /* prepare for flag alteration */
|
yuuji@0
|
2456 if (snarf->dtb->flagmsg) (*snarf->dtb->flagmsg) (snarf,elt);
|
yuuji@0
|
2457 /* flags now altered */
|
yuuji@0
|
2458 elt->deleted = elt->seen = elt->valid = T;
|
yuuji@0
|
2459 if (snarf->dtb->flagmsg) (*snarf->dtb->flagmsg) (snarf,elt);
|
yuuji@0
|
2460 }
|
yuuji@0
|
2461 /* driver has one-time flag call */
|
yuuji@0
|
2462 if (snarf->dtb->flag) {
|
yuuji@0
|
2463 sprintf (tmp,"%lu",i);
|
yuuji@0
|
2464 (*snarf->dtb->flag) (snarf,tmp,"\\Deleted \\Seen",ST_SET);
|
yuuji@0
|
2465 }
|
yuuji@0
|
2466 }
|
yuuji@0
|
2467 else { /* copy failed */
|
yuuji@0
|
2468 sprintf (tmp,"Unable to move message %lu from %s mailbox",
|
yuuji@0
|
2469 i,snarf->dtb->name);
|
yuuji@0
|
2470 mm_log (tmp,WARN);
|
yuuji@0
|
2471 }
|
yuuji@0
|
2472 }
|
yuuji@0
|
2473 }
|
yuuji@0
|
2474 /* expunge the messages */
|
yuuji@0
|
2475 mail_close_full (snarf,n ? CL_EXPUNGE : NIL);
|
yuuji@0
|
2476 stream->snarf.time = (unsigned long) time (0);
|
yuuji@0
|
2477 /* Even if the snarf failed, we don't want to return NIL if the stream
|
yuuji@0
|
2478 * is still alive. Or at least that's what we currently think.
|
yuuji@0
|
2479 */
|
yuuji@0
|
2480 /* redo the driver's action */
|
yuuji@0
|
2481 ret = stream->dtb ? (*stream->dtb->ping) (stream) : NIL;
|
yuuji@0
|
2482 }
|
yuuji@0
|
2483 return ret;
|
yuuji@0
|
2484 }
|
yuuji@0
|
2485
|
yuuji@0
|
2486 /* Mail check mailbox
|
yuuji@0
|
2487 * Accepts: mail stream
|
yuuji@0
|
2488 */
|
yuuji@0
|
2489
|
yuuji@0
|
2490 void mail_check (MAILSTREAM *stream)
|
yuuji@0
|
2491 {
|
yuuji@0
|
2492 /* do the driver's action */
|
yuuji@0
|
2493 if (stream->dtb) (*stream->dtb->check) (stream);
|
yuuji@0
|
2494 }
|
yuuji@0
|
2495
|
yuuji@0
|
2496
|
yuuji@0
|
2497 /* Mail expunge mailbox
|
yuuji@0
|
2498 * Accepts: mail stream
|
yuuji@0
|
2499 * sequence to expunge if non-NIL
|
yuuji@0
|
2500 * expunge options
|
yuuji@0
|
2501 * Returns: T on success, NIL on failure
|
yuuji@0
|
2502 */
|
yuuji@0
|
2503
|
yuuji@0
|
2504 long mail_expunge_full (MAILSTREAM *stream,char *sequence,long options)
|
yuuji@0
|
2505 {
|
yuuji@0
|
2506 /* do the driver's action */
|
yuuji@0
|
2507 return stream->dtb ? (*stream->dtb->expunge) (stream,sequence,options) : NIL;
|
yuuji@0
|
2508 }
|
yuuji@0
|
2509
|
yuuji@0
|
2510
|
yuuji@0
|
2511 /* Mail copy message(s)
|
yuuji@0
|
2512 * Accepts: mail stream
|
yuuji@0
|
2513 * sequence
|
yuuji@0
|
2514 * destination mailbox
|
yuuji@0
|
2515 * flags
|
yuuji@0
|
2516 */
|
yuuji@0
|
2517
|
yuuji@0
|
2518 long mail_copy_full (MAILSTREAM *stream,char *sequence,char *mailbox,
|
yuuji@0
|
2519 long options)
|
yuuji@0
|
2520 {
|
yuuji@0
|
2521 return stream->dtb ?
|
yuuji@0
|
2522 SAFE_COPY (stream->dtb,stream,sequence,mailbox,options) : NIL;
|
yuuji@0
|
2523 }
|
yuuji@0
|
2524
|
yuuji@0
|
2525 /* Append data package to use for old single-message mail_append() interface */
|
yuuji@0
|
2526
|
yuuji@0
|
2527 typedef struct mail_append_package {
|
yuuji@0
|
2528 char *flags; /* initial flags */
|
yuuji@0
|
2529 char *date; /* message internal date */
|
yuuji@0
|
2530 STRING *message; /* stringstruct of message */
|
yuuji@0
|
2531 } APPENDPACKAGE;
|
yuuji@0
|
2532
|
yuuji@0
|
2533
|
yuuji@0
|
2534 /* Single append message string
|
yuuji@0
|
2535 * Accepts: mail stream
|
yuuji@0
|
2536 * package pointer (cast as a void *)
|
yuuji@0
|
2537 * pointer to return initial flags
|
yuuji@0
|
2538 * pointer to return message internal date
|
yuuji@0
|
2539 * pointer to return stringstruct of message to append
|
yuuji@0
|
2540 * Returns: T, always
|
yuuji@0
|
2541 */
|
yuuji@0
|
2542
|
yuuji@0
|
2543 static long mail_append_single (MAILSTREAM *stream,void *data,char **flags,
|
yuuji@0
|
2544 char **date,STRING **message)
|
yuuji@0
|
2545 {
|
yuuji@0
|
2546 APPENDPACKAGE *ap = (APPENDPACKAGE *) data;
|
yuuji@0
|
2547 *flags = ap->flags; /* get desired data from the package */
|
yuuji@0
|
2548 *date = ap->date;
|
yuuji@0
|
2549 *message = ap->message;
|
yuuji@0
|
2550 ap->message = NIL; /* so next callback puts a stop to it */
|
yuuji@0
|
2551 return LONGT; /* always return success */
|
yuuji@0
|
2552 }
|
yuuji@0
|
2553
|
yuuji@0
|
2554
|
yuuji@0
|
2555 /* Mail append message string
|
yuuji@0
|
2556 * Accepts: mail stream
|
yuuji@0
|
2557 * destination mailbox
|
yuuji@0
|
2558 * initial flags
|
yuuji@0
|
2559 * message internal date
|
yuuji@0
|
2560 * stringstruct of message to append
|
yuuji@0
|
2561 * Returns: T on success, NIL on failure
|
yuuji@0
|
2562 */
|
yuuji@0
|
2563
|
yuuji@0
|
2564 long mail_append_full (MAILSTREAM *stream,char *mailbox,char *flags,char *date,
|
yuuji@0
|
2565 STRING *message)
|
yuuji@0
|
2566 {
|
yuuji@0
|
2567 APPENDPACKAGE ap;
|
yuuji@0
|
2568 ap.flags = flags; /* load append package */
|
yuuji@0
|
2569 ap.date = date;
|
yuuji@0
|
2570 ap.message = message;
|
yuuji@0
|
2571 return mail_append_multiple (stream,mailbox,mail_append_single,(void *) &ap);
|
yuuji@0
|
2572 }
|
yuuji@0
|
2573
|
yuuji@0
|
2574 /* Mail append message(s)
|
yuuji@0
|
2575 * Accepts: mail stream
|
yuuji@0
|
2576 * destination mailbox
|
yuuji@0
|
2577 * append data callback
|
yuuji@0
|
2578 * arbitrary data for callback use
|
yuuji@0
|
2579 * Returns: T on success, NIL on failure
|
yuuji@0
|
2580 */
|
yuuji@0
|
2581
|
yuuji@0
|
2582 long mail_append_multiple (MAILSTREAM *stream,char *mailbox,append_t af,
|
yuuji@0
|
2583 void *data)
|
yuuji@0
|
2584 {
|
yuuji@0
|
2585 char *s,tmp[MAILTMPLEN];
|
yuuji@0
|
2586 DRIVER *d = NIL;
|
yuuji@0
|
2587 long ret = NIL;
|
yuuji@0
|
2588 /* never allow names with newlines */
|
yuuji@0
|
2589 if (strpbrk (mailbox,"\015\012"))
|
yuuji@0
|
2590 MM_LOG ("Can't append to mailbox with such a name",ERROR);
|
yuuji@0
|
2591 else if (strlen (mailbox) >=
|
yuuji@0
|
2592 (NETMAXHOST+(NETMAXUSER*2)+NETMAXMBX+NETMAXSRV+50)) {
|
yuuji@0
|
2593 sprintf (tmp,"Can't append %.80s: %s",mailbox,(*mailbox == '{') ?
|
yuuji@0
|
2594 "invalid remote specification" : "no such mailbox");
|
yuuji@0
|
2595 MM_LOG (tmp,ERROR);
|
yuuji@0
|
2596 }
|
yuuji@0
|
2597 /* special driver hack? */
|
yuuji@0
|
2598 else if (!strncmp (lcase (strcpy (tmp,mailbox)),"#driver.",8)) {
|
yuuji@0
|
2599 /* yes, tie off name at likely delimiter */
|
yuuji@0
|
2600 if (!(s = strpbrk (tmp+8,"/\\:"))) {
|
yuuji@0
|
2601 sprintf (tmp,"Can't append to mailbox %.80s: bad driver syntax",mailbox);
|
yuuji@0
|
2602 MM_LOG (tmp,ERROR);
|
yuuji@0
|
2603 return NIL;
|
yuuji@0
|
2604 }
|
yuuji@0
|
2605 *s++ = '\0'; /* tie off at delimiter */
|
yuuji@0
|
2606 if (!(d = (DRIVER *) mail_parameters (NIL,GET_DRIVER,tmp+8))) {
|
yuuji@0
|
2607 sprintf (tmp,"Can't append to mailbox %.80s: unknown driver",mailbox);
|
yuuji@0
|
2608 MM_LOG (tmp,ERROR);
|
yuuji@0
|
2609 }
|
yuuji@0
|
2610 else ret = SAFE_APPEND (d,stream,mailbox + (s - tmp),af,data);
|
yuuji@0
|
2611 }
|
yuuji@0
|
2612 else if (d = mail_valid (stream,mailbox,NIL))
|
yuuji@0
|
2613 ret = SAFE_APPEND (d,stream,mailbox,af,data);
|
yuuji@0
|
2614 /* No driver, try for TRYCREATE if no stream. Note that we use the
|
yuuji@0
|
2615 * createProto here, not the appendProto, since the dummy driver already
|
yuuji@0
|
2616 * took care of the appendProto case. Otherwise, if appendProto is set to
|
yuuji@0
|
2617 * NIL, we won't get a TRYCREATE.
|
yuuji@0
|
2618 */
|
yuuji@0
|
2619 else if (!stream && (stream = default_proto (NIL)) && stream->dtb &&
|
yuuji@0
|
2620 SAFE_APPEND (stream->dtb,stream,mailbox,af,data))
|
yuuji@0
|
2621 /* timing race? */
|
yuuji@0
|
2622 MM_NOTIFY (stream,"Append validity confusion",WARN);
|
yuuji@0
|
2623 /* generate error message */
|
yuuji@0
|
2624 else mail_valid (stream,mailbox,"append to mailbox");
|
yuuji@0
|
2625 return ret;
|
yuuji@0
|
2626 }
|
yuuji@0
|
2627
|
yuuji@0
|
2628 /* Mail garbage collect stream
|
yuuji@0
|
2629 * Accepts: mail stream
|
yuuji@0
|
2630 * garbage collection flags
|
yuuji@0
|
2631 */
|
yuuji@0
|
2632
|
yuuji@0
|
2633 void mail_gc (MAILSTREAM *stream,long gcflags)
|
yuuji@0
|
2634 {
|
yuuji@0
|
2635 MESSAGECACHE *elt;
|
yuuji@0
|
2636 unsigned long i;
|
yuuji@0
|
2637 /* do the driver's action first */
|
yuuji@0
|
2638 if (stream->dtb && stream->dtb->gc) (*stream->dtb->gc) (stream,gcflags);
|
yuuji@0
|
2639 stream->msgno = 0; /* nothing cached now */
|
yuuji@0
|
2640 if (gcflags & GC_ENV) { /* garbage collect envelopes? */
|
yuuji@0
|
2641 if (stream->env) mail_free_envelope (&stream->env);
|
yuuji@0
|
2642 if (stream->body) mail_free_body (&stream->body);
|
yuuji@0
|
2643 }
|
yuuji@0
|
2644 if (gcflags & GC_TEXTS) { /* free texts */
|
yuuji@0
|
2645 if (stream->text.data) fs_give ((void **) &stream->text.data);
|
yuuji@0
|
2646 stream->text.size = 0;
|
yuuji@0
|
2647 }
|
yuuji@0
|
2648 /* garbage collect per-message stuff */
|
yuuji@0
|
2649 for (i = 1; i <= stream->nmsgs; i++)
|
yuuji@0
|
2650 if (elt = (MESSAGECACHE *) (*mailcache) (stream,i,CH_ELT))
|
yuuji@0
|
2651 mail_gc_msg (&elt->private.msg,gcflags);
|
yuuji@0
|
2652 }
|
yuuji@0
|
2653
|
yuuji@0
|
2654
|
yuuji@0
|
2655 /* Mail garbage collect message
|
yuuji@0
|
2656 * Accepts: message structure
|
yuuji@0
|
2657 * garbage collection flags
|
yuuji@0
|
2658 */
|
yuuji@0
|
2659
|
yuuji@0
|
2660 void mail_gc_msg (MESSAGE *msg,long gcflags)
|
yuuji@0
|
2661 {
|
yuuji@0
|
2662 if (gcflags & GC_ENV) { /* garbage collect envelopes? */
|
yuuji@0
|
2663 mail_free_envelope (&msg->env);
|
yuuji@0
|
2664 mail_free_body (&msg->body);
|
yuuji@0
|
2665 }
|
yuuji@0
|
2666 if (gcflags & GC_TEXTS) { /* garbage collect texts */
|
yuuji@0
|
2667 if (msg->full.text.data) fs_give ((void **) &msg->full.text.data);
|
yuuji@0
|
2668 if (msg->header.text.data) {
|
yuuji@0
|
2669 mail_free_stringlist (&msg->lines);
|
yuuji@0
|
2670 fs_give ((void **) &msg->header.text.data);
|
yuuji@0
|
2671 }
|
yuuji@0
|
2672 if (msg->text.text.data) fs_give ((void **) &msg->text.text.data);
|
yuuji@0
|
2673 /* now GC all body components */
|
yuuji@0
|
2674 if (msg->body) mail_gc_body (msg->body);
|
yuuji@0
|
2675 }
|
yuuji@0
|
2676 }
|
yuuji@0
|
2677
|
yuuji@0
|
2678 /* Mail garbage collect texts in BODY structure
|
yuuji@0
|
2679 * Accepts: BODY structure
|
yuuji@0
|
2680 */
|
yuuji@0
|
2681
|
yuuji@0
|
2682 void mail_gc_body (BODY *body)
|
yuuji@0
|
2683 {
|
yuuji@0
|
2684 PART *part;
|
yuuji@0
|
2685 switch (body->type) { /* free contents */
|
yuuji@0
|
2686 case TYPEMULTIPART: /* multiple part */
|
yuuji@0
|
2687 for (part = body->nested.part; part; part = part->next)
|
yuuji@0
|
2688 mail_gc_body (&part->body);
|
yuuji@0
|
2689 break;
|
yuuji@0
|
2690 case TYPEMESSAGE: /* encapsulated message */
|
yuuji@0
|
2691 if (body->subtype && !strcmp (body->subtype,"RFC822")) {
|
yuuji@0
|
2692 mail_free_stringlist (&body->nested.msg->lines);
|
yuuji@0
|
2693 mail_gc_msg (body->nested.msg,GC_TEXTS);
|
yuuji@0
|
2694 }
|
yuuji@0
|
2695 break;
|
yuuji@0
|
2696 default:
|
yuuji@0
|
2697 break;
|
yuuji@0
|
2698 }
|
yuuji@0
|
2699 if (body->mime.text.data) fs_give ((void **) &body->mime.text.data);
|
yuuji@0
|
2700 if (body->contents.text.data) fs_give ((void **) &body->contents.text.data);
|
yuuji@0
|
2701 }
|
yuuji@0
|
2702
|
yuuji@0
|
2703 /* Mail get body part
|
yuuji@0
|
2704 * Accepts: mail stream
|
yuuji@0
|
2705 * message number
|
yuuji@0
|
2706 * section specifier
|
yuuji@0
|
2707 * Returns: pointer to body
|
yuuji@0
|
2708 */
|
yuuji@0
|
2709
|
yuuji@0
|
2710 BODY *mail_body (MAILSTREAM *stream,unsigned long msgno,unsigned char *section)
|
yuuji@0
|
2711 {
|
yuuji@0
|
2712 BODY *b = NIL;
|
yuuji@0
|
2713 PART *pt;
|
yuuji@0
|
2714 unsigned long i;
|
yuuji@0
|
2715 /* make sure have a body */
|
yuuji@0
|
2716 if (section && *section && mail_fetchstructure (stream,msgno,&b) && b)
|
yuuji@0
|
2717 while (*section) { /* find desired section */
|
yuuji@0
|
2718 if (isdigit (*section)) { /* get section specifier */
|
yuuji@0
|
2719 /* make sure what follows is valid */
|
yuuji@0
|
2720 if (!(i = strtoul (section,(char **) §ion,10)) ||
|
yuuji@0
|
2721 (*section && ((*section++ != '.') || !*section))) return NIL;
|
yuuji@0
|
2722 /* multipart content? */
|
yuuji@0
|
2723 if (b->type == TYPEMULTIPART) {
|
yuuji@0
|
2724 /* yes, find desired part */
|
yuuji@0
|
2725 if (pt = b->nested.part) while (--i && (pt = pt->next));
|
yuuji@0
|
2726 if (!pt) return NIL; /* bad specifier */
|
yuuji@0
|
2727 b = &pt->body; /* note new body */
|
yuuji@0
|
2728 }
|
yuuji@0
|
2729 /* otherwise must be section 1 */
|
yuuji@0
|
2730 else if (i != 1) return NIL;
|
yuuji@0
|
2731 /* need to go down further? */
|
yuuji@0
|
2732 if (*section) switch (b->type) {
|
yuuji@0
|
2733 case TYPEMULTIPART: /* multipart */
|
yuuji@0
|
2734 break;
|
yuuji@0
|
2735 case TYPEMESSAGE: /* embedded message */
|
yuuji@0
|
2736 if (!strcmp (b->subtype,"RFC822")) {
|
yuuji@0
|
2737 b = b->nested.msg->body;
|
yuuji@0
|
2738 break;
|
yuuji@0
|
2739 }
|
yuuji@0
|
2740 default: /* bogus subpart specification */
|
yuuji@0
|
2741 return NIL;
|
yuuji@0
|
2742 }
|
yuuji@0
|
2743 }
|
yuuji@0
|
2744 else return NIL; /* unknown section specifier */
|
yuuji@0
|
2745 }
|
yuuji@0
|
2746 return b;
|
yuuji@0
|
2747 }
|
yuuji@0
|
2748
|
yuuji@0
|
2749 /* Mail output date from elt fields
|
yuuji@0
|
2750 * Accepts: character string to write into
|
yuuji@0
|
2751 * elt to get data data from
|
yuuji@0
|
2752 * Returns: the character string
|
yuuji@0
|
2753 */
|
yuuji@0
|
2754
|
yuuji@0
|
2755 const char *days[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
|
yuuji@0
|
2756
|
yuuji@0
|
2757 const char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
yuuji@0
|
2758 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
|
yuuji@0
|
2759
|
yuuji@0
|
2760 char *mail_date (char *string,MESSAGECACHE *elt)
|
yuuji@0
|
2761 {
|
yuuji@0
|
2762 sprintf (string,"%2d-%s-%d %02d:%02d:%02d %c%02d%02d",
|
yuuji@0
|
2763 elt->day ? elt->day : 1,
|
yuuji@0
|
2764 months[elt->month ? (elt->month - 1) : 0],
|
yuuji@0
|
2765 elt->year + BASEYEAR,elt->hours,elt->minutes,elt->seconds,
|
yuuji@0
|
2766 elt->zoccident ? '-' : '+',elt->zhours,elt->zminutes);
|
yuuji@0
|
2767 return string;
|
yuuji@0
|
2768 }
|
yuuji@0
|
2769
|
yuuji@0
|
2770
|
yuuji@0
|
2771 /* Mail output extended-ctime format date from elt fields
|
yuuji@0
|
2772 * Accepts: character string to write into
|
yuuji@0
|
2773 * elt to get data data from
|
yuuji@0
|
2774 * Returns: the character string
|
yuuji@0
|
2775 */
|
yuuji@0
|
2776
|
yuuji@0
|
2777 char *mail_cdate (char *string,MESSAGECACHE *elt)
|
yuuji@0
|
2778 {
|
yuuji@0
|
2779 char *fmt = "%s %s %2d %02d:%02d:%02d %4d %s%02d%02d\n";
|
yuuji@0
|
2780 int d = elt->day ? elt->day : 1;
|
yuuji@0
|
2781 int m = elt->month ? (elt->month - 1) : 0;
|
yuuji@0
|
2782 int y = elt->year + BASEYEAR;
|
yuuji@0
|
2783 const char *s = months[m];
|
yuuji@0
|
2784 if (m < 2) { /* if before March, */
|
yuuji@0
|
2785 m += 10; /* January = month 10 of previous year */
|
yuuji@0
|
2786 y--;
|
yuuji@0
|
2787 }
|
yuuji@0
|
2788 else m -= 2; /* March is month 0 */
|
yuuji@0
|
2789 sprintf (string,fmt,days[(int) (d + 2 + ((7 + 31 * m) / 12)
|
yuuji@0
|
2790 #ifndef USEJULIANCALENDAR
|
yuuji@0
|
2791 #ifndef USEORTHODOXCALENDAR /* Gregorian calendar */
|
yuuji@0
|
2792 + (y / 400)
|
yuuji@0
|
2793 #ifdef Y4KBUGFIX
|
yuuji@0
|
2794 - (y / 4000)
|
yuuji@0
|
2795 #endif
|
yuuji@0
|
2796 #else /* Orthodox calendar */
|
yuuji@0
|
2797 + (2 * (y / 900)) + ((y % 900) >= 200)
|
yuuji@0
|
2798 + ((y % 900) >= 600)
|
yuuji@0
|
2799 #endif
|
yuuji@0
|
2800 - (y / 100)
|
yuuji@0
|
2801 #endif
|
yuuji@0
|
2802 + y + (y / 4)) % 7],
|
yuuji@0
|
2803 s,d,elt->hours,elt->minutes,elt->seconds,elt->year + BASEYEAR,
|
yuuji@0
|
2804 elt->zoccident ? "-" : "+",elt->zhours,elt->zminutes);
|
yuuji@0
|
2805 return string;
|
yuuji@0
|
2806 }
|
yuuji@0
|
2807
|
yuuji@0
|
2808 /* Mail parse date into elt fields
|
yuuji@0
|
2809 * Accepts: elt to write into
|
yuuji@0
|
2810 * date string to parse
|
yuuji@0
|
2811 * Returns: T if parse successful, else NIL
|
yuuji@0
|
2812 * This routine parses dates as follows:
|
yuuji@0
|
2813 * . leading three alphas followed by comma and space are ignored
|
yuuji@0
|
2814 * . date accepted in format: mm/dd/yy, mm/dd/yyyy, dd-mmm-yy, dd-mmm-yyyy,
|
yuuji@0
|
2815 * dd mmm yy, dd mmm yyyy, yyyy-mm-dd, yyyymmdd
|
yuuji@0
|
2816 * . two and three digit years interpreted according to RFC 2822 rules
|
yuuji@0
|
2817 * . mandatory end of string if yyyy-mm-dd or yyyymmdd; otherwise optional
|
yuuji@0
|
2818 * space followed by time:
|
yuuji@0
|
2819 * . time accepted in format hh:mm:ss or hh:mm
|
yuuji@0
|
2820 * . end of string accepted
|
yuuji@0
|
2821 * . timezone accepted: hyphen followed by symbolic timezone, or space
|
yuuji@0
|
2822 * followed by signed numeric timezone or symbolic timezone
|
yuuji@0
|
2823 * Examples of normal input:
|
yuuji@0
|
2824 * . IMAP date-only (SEARCH):
|
yuuji@0
|
2825 * dd-mmm-yyyy
|
yuuji@0
|
2826 * . IMAP date-time (INTERNALDATE):
|
yuuji@0
|
2827 * dd-mmm-yyyy hh:mm:ss +zzzz
|
yuuji@0
|
2828 * . RFC-822:
|
yuuji@0
|
2829 * www, dd mmm yy hh:mm:ss zzz
|
yuuji@0
|
2830 * . RFC-2822:
|
yuuji@0
|
2831 * www, dd mmm yyyy hh:mm:ss +zzzz
|
yuuji@0
|
2832 */
|
yuuji@0
|
2833
|
yuuji@0
|
2834 long mail_parse_date (MESSAGECACHE *elt,unsigned char *s)
|
yuuji@0
|
2835 {
|
yuuji@0
|
2836 unsigned long d,m,y;
|
yuuji@0
|
2837 int mi,ms;
|
yuuji@0
|
2838 struct tm *t;
|
yuuji@0
|
2839 time_t tn;
|
yuuji@0
|
2840 char tmp[MAILTMPLEN];
|
yuuji@0
|
2841 static unsigned long maxyear = 0;
|
yuuji@0
|
2842 if (!maxyear) { /* know the end of time yet? */
|
yuuji@0
|
2843 MESSAGECACHE tmpelt;
|
yuuji@0
|
2844 memset (&tmpelt,0xff,sizeof (MESSAGECACHE));
|
yuuji@0
|
2845 maxyear = BASEYEAR + tmpelt.year;
|
yuuji@0
|
2846 }
|
yuuji@0
|
2847 /* clear elt */
|
yuuji@0
|
2848 elt->zoccident = elt->zhours = elt->zminutes =
|
yuuji@0
|
2849 elt->hours = elt->minutes = elt->seconds =
|
yuuji@0
|
2850 elt->day = elt->month = elt->year = 0;
|
yuuji@0
|
2851 /* make a writeable uppercase copy */
|
yuuji@0
|
2852 if (s && *s && (strlen (s) < (size_t)MAILTMPLEN)) s = ucase (strcpy (tmp,s));
|
yuuji@0
|
2853 else return NIL;
|
yuuji@0
|
2854 /* skip over possible day of week */
|
yuuji@0
|
2855 if (isalpha (*s) && isalpha (s[1]) && isalpha (s[2]) && (s[3] == ',') &&
|
yuuji@0
|
2856 (s[4] == ' ')) s += 5;
|
yuuji@0
|
2857 while (*s == ' ') s++; /* parse first number (probable month) */
|
yuuji@0
|
2858 if (!(m = strtoul (s,(char **) &s,10))) return NIL;
|
yuuji@0
|
2859
|
yuuji@0
|
2860 switch (*s) { /* different parse based on delimiter */
|
yuuji@0
|
2861 case '/': /* mm/dd/yy format */
|
yuuji@0
|
2862 if (isdigit (*++s) && (d = strtoul (s,(char **) &s,10)) &&
|
yuuji@0
|
2863 (*s == '/') && isdigit (*++s)) {
|
yuuji@0
|
2864 y = strtoul (s,(char **) &s,10);
|
yuuji@0
|
2865 if (*s == '\0') break; /* must end here */
|
yuuji@0
|
2866 }
|
yuuji@0
|
2867 return NIL; /* bogon */
|
yuuji@0
|
2868 case ' ': /* dd mmm yy format */
|
yuuji@0
|
2869 while (s[1] == ' ') s++; /* slurp extra whitespace */
|
yuuji@0
|
2870 case '-':
|
yuuji@0
|
2871 if (isdigit (s[1])) { /* possible ISO 8601 date format? */
|
yuuji@0
|
2872 y = m; /* yes, first number is year */
|
yuuji@0
|
2873 /* get month and day */
|
yuuji@0
|
2874 if ((m = strtoul (s+1,(char **) &s,10)) && (*s++ == '-') &&
|
yuuji@0
|
2875 (d = strtoul (s,(char **) &s,10)) && !*s) break;
|
yuuji@0
|
2876 return NIL; /* syntax error or time present */
|
yuuji@0
|
2877 }
|
yuuji@0
|
2878 d = m; /* dd-mmm-yy[yy], so first number is a day */
|
yuuji@0
|
2879 /* make sure string long enough! */
|
yuuji@0
|
2880 if (strlen (s) < (size_t) 5) return NIL;
|
yuuji@0
|
2881 /* Some compilers don't allow `<<' and/or longs in case statements. */
|
yuuji@0
|
2882 /* slurp up the month string */
|
yuuji@0
|
2883 ms = ((s[1] - 'A') * 1024) + ((s[2] - 'A') * 32) + (s[3] - 'A');
|
yuuji@0
|
2884 switch (ms) { /* determine the month */
|
yuuji@0
|
2885 case (('J'-'A') * 1024) + (('A'-'A') * 32) + ('N'-'A'): m = 1; break;
|
yuuji@0
|
2886 case (('F'-'A') * 1024) + (('E'-'A') * 32) + ('B'-'A'): m = 2; break;
|
yuuji@0
|
2887 case (('M'-'A') * 1024) + (('A'-'A') * 32) + ('R'-'A'): m = 3; break;
|
yuuji@0
|
2888 case (('A'-'A') * 1024) + (('P'-'A') * 32) + ('R'-'A'): m = 4; break;
|
yuuji@0
|
2889 case (('M'-'A') * 1024) + (('A'-'A') * 32) + ('Y'-'A'): m = 5; break;
|
yuuji@0
|
2890 case (('J'-'A') * 1024) + (('U'-'A') * 32) + ('N'-'A'): m = 6; break;
|
yuuji@0
|
2891 case (('J'-'A') * 1024) + (('U'-'A') * 32) + ('L'-'A'): m = 7; break;
|
yuuji@0
|
2892 case (('A'-'A') * 1024) + (('U'-'A') * 32) + ('G'-'A'): m = 8; break;
|
yuuji@0
|
2893 case (('S'-'A') * 1024) + (('E'-'A') * 32) + ('P'-'A'): m = 9; break;
|
yuuji@0
|
2894 case (('O'-'A') * 1024) + (('C'-'A') * 32) + ('T'-'A'): m = 10; break;
|
yuuji@0
|
2895 case (('N'-'A') * 1024) + (('O'-'A') * 32) + ('V'-'A'): m = 11; break;
|
yuuji@0
|
2896 case (('D'-'A') * 1024) + (('E'-'A') * 32) + ('C'-'A'): m = 12; break;
|
yuuji@0
|
2897 default: return NIL; /* unknown month */
|
yuuji@0
|
2898 }
|
yuuji@0
|
2899 if (s[4] == *s) s += 5; /* advance to year */
|
yuuji@0
|
2900 else { /* first three were OK, possibly full name */
|
yuuji@0
|
2901 mi = *s; /* note delimiter, skip alphas */
|
yuuji@0
|
2902 for (s += 4; isalpha (*s); s++);
|
yuuji@0
|
2903 /* error if delimiter not here */
|
yuuji@0
|
2904 if (mi != *s++) return NIL;
|
yuuji@0
|
2905 }
|
yuuji@0
|
2906 while (*s == ' ') s++; /* parse year */
|
yuuji@0
|
2907 if (isdigit (*s)) { /* must be a digit here */
|
yuuji@0
|
2908 y = strtoul (s,(char **) &s,10);
|
yuuji@0
|
2909 if (*s == '\0' || *s == ' ') break;
|
yuuji@0
|
2910 }
|
yuuji@0
|
2911 case '\0': /* ISO 8601 compact date */
|
yuuji@0
|
2912 if (m < (BASEYEAR * 10000)) return NIL;
|
yuuji@0
|
2913 y = m / 10000; /* get year */
|
yuuji@0
|
2914 d = (m %= 10000) % 100; /* get day */
|
yuuji@0
|
2915 m /= 100; /* and month */
|
yuuji@0
|
2916 break;
|
yuuji@0
|
2917 default:
|
yuuji@0
|
2918 return NIL; /* unknown date format */
|
yuuji@0
|
2919 }
|
yuuji@0
|
2920
|
yuuji@0
|
2921 /* minimal validity check of date */
|
yuuji@0
|
2922 if ((d > 31) || (m > 12)) return NIL;
|
yuuji@0
|
2923 if (y < 49) y += 2000; /* RFC 2282 rules for two digit years 00-49 */
|
yuuji@0
|
2924 else if (y < 999) y += 1900; /* 2-digit years 50-99 and 3-digit years */
|
yuuji@0
|
2925 /* reject prehistoric and far future years */
|
yuuji@0
|
2926 if ((y < BASEYEAR) || (y > maxyear)) return NIL;
|
yuuji@0
|
2927 /* set values in elt */
|
yuuji@0
|
2928 elt->day = d; elt->month = m; elt->year = y - BASEYEAR;
|
yuuji@0
|
2929 ms = '\0'; /* initially no time zone string */
|
yuuji@0
|
2930 if (*s) { /* time specification present? */
|
yuuji@0
|
2931 /* parse time */
|
yuuji@0
|
2932 d = strtoul (s+1,(char **) &s,10);
|
yuuji@0
|
2933 if (*s != ':') return NIL;
|
yuuji@0
|
2934 m = strtoul (++s,(char **) &s,10);
|
yuuji@0
|
2935 y = (*s == ':') ? strtoul (++s,(char **) &s,10) : 0;
|
yuuji@0
|
2936 /* validity check time */
|
yuuji@0
|
2937 if ((d > 23) || (m > 59) || (y > 60)) return NIL;
|
yuuji@0
|
2938 /* set values in elt */
|
yuuji@0
|
2939 elt->hours = d; elt->minutes = m; elt->seconds = y;
|
yuuji@0
|
2940 switch (*s) { /* time zone specifier? */
|
yuuji@0
|
2941 case ' ': /* numeric time zone */
|
yuuji@0
|
2942 while (s[1] == ' ') s++; /* slurp extra whitespace */
|
yuuji@0
|
2943 if (!isalpha (s[1])) { /* treat as '-' case if alphabetic */
|
yuuji@0
|
2944 /* test for sign character */
|
yuuji@0
|
2945 if ((elt->zoccident = (*++s == '-')) || (*s == '+')) s++;
|
yuuji@0
|
2946 /* validate proper timezone */
|
yuuji@0
|
2947 if (isdigit(*s) && isdigit(s[1]) && isdigit(s[2]) && (s[2] < '6') &&
|
yuuji@0
|
2948 isdigit(s[3])) {
|
yuuji@0
|
2949 elt->zhours = (*s - '0') * 10 + (s[1] - '0');
|
yuuji@0
|
2950 elt->zminutes = (s[2] - '0') * 10 + (s[3] - '0');
|
yuuji@0
|
2951 }
|
yuuji@0
|
2952 return T; /* all done! */
|
yuuji@0
|
2953 }
|
yuuji@0
|
2954 /* falls through */
|
yuuji@0
|
2955 case '-': /* symbolic time zone */
|
yuuji@0
|
2956 if (!(ms = *++s)) ms = 'Z';
|
yuuji@0
|
2957 else if (*++s) { /* multi-character? */
|
yuuji@0
|
2958 ms -= 'A'; ms *= 1024; /* yes, make compressed three-byte form */
|
yuuji@0
|
2959 ms += ((*s++ - 'A') * 32);
|
yuuji@0
|
2960 if (*s) ms += *s++ - 'A';
|
yuuji@0
|
2961 if (*s) ms = '\0'; /* more than three characters */
|
yuuji@0
|
2962 }
|
yuuji@0
|
2963 default: /* ignore anything else */
|
yuuji@0
|
2964 break;
|
yuuji@0
|
2965 }
|
yuuji@0
|
2966 }
|
yuuji@0
|
2967
|
yuuji@0
|
2968 /* This is not intended to be a comprehensive list of all possible
|
yuuji@0
|
2969 * timezone strings. Such a list would be impractical. Rather, this
|
yuuji@0
|
2970 * listing is intended to incorporate all military, North American, and
|
yuuji@0
|
2971 * a few special cases such as Japan and the major European zone names,
|
yuuji@0
|
2972 * such as what might be expected to be found in a Tenex format mailbox
|
yuuji@0
|
2973 * and spewed from an IMAP server. The trend is to migrate to numeric
|
yuuji@0
|
2974 * timezones which lack the flavor but also the ambiguity of the names.
|
yuuji@0
|
2975 *
|
yuuji@0
|
2976 * RFC-822 only recognizes UT, GMT, 1-letter military timezones, and the
|
yuuji@0
|
2977 * 4 CONUS timezones and their summer time variants. [Sorry, Canadian
|
yuuji@0
|
2978 * Atlantic Provinces, Alaska, and Hawaii.]
|
yuuji@0
|
2979 */
|
yuuji@0
|
2980 switch (ms) { /* determine the timezone */
|
yuuji@0
|
2981 /* Universal */
|
yuuji@0
|
2982 case (('U'-'A')*1024)+(('T'-'A')*32):
|
yuuji@0
|
2983 #ifndef STRICT_RFC822_TIMEZONES
|
yuuji@0
|
2984 case (('U'-'A')*1024)+(('T'-'A')*32)+'C'-'A':
|
yuuji@0
|
2985 #endif
|
yuuji@0
|
2986 /* Greenwich */
|
yuuji@0
|
2987 case (('G'-'A')*1024)+(('M'-'A')*32)+'T'-'A':
|
yuuji@0
|
2988 case 'Z': elt->zhours = 0; break;
|
yuuji@0
|
2989
|
yuuji@0
|
2990 /* oriental (from Greenwich) timezones */
|
yuuji@0
|
2991 #ifndef STRICT_RFC822_TIMEZONES
|
yuuji@0
|
2992 /* Middle Europe */
|
yuuji@0
|
2993 case (('M'-'A')*1024)+(('E'-'A')*32)+'T'-'A':
|
yuuji@0
|
2994 #endif
|
yuuji@0
|
2995 #ifdef BRITISH_SUMMER_TIME
|
yuuji@0
|
2996 /* British Summer */
|
yuuji@0
|
2997 case (('B'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
|
yuuji@0
|
2998 #endif
|
yuuji@0
|
2999 case 'A': elt->zhours = 1; break;
|
yuuji@0
|
3000 #ifndef STRICT_RFC822_TIMEZONES
|
yuuji@0
|
3001 /* Eastern Europe */
|
yuuji@0
|
3002 case (('E'-'A')*1024)+(('E'-'A')*32)+'T'-'A':
|
yuuji@0
|
3003 #endif
|
yuuji@0
|
3004 case 'B': elt->zhours = 2; break;
|
yuuji@0
|
3005 case 'C': elt->zhours = 3; break;
|
yuuji@0
|
3006 case 'D': elt->zhours = 4; break;
|
yuuji@0
|
3007 case 'E': elt->zhours = 5; break;
|
yuuji@0
|
3008 case 'F': elt->zhours = 6; break;
|
yuuji@0
|
3009 case 'G': elt->zhours = 7; break;
|
yuuji@0
|
3010 case 'H': elt->zhours = 8; break;
|
yuuji@0
|
3011 #ifndef STRICT_RFC822_TIMEZONES
|
yuuji@0
|
3012 /* Japan */
|
yuuji@0
|
3013 case (('J'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
|
yuuji@0
|
3014 #endif
|
yuuji@0
|
3015 case 'I': elt->zhours = 9; break;
|
yuuji@0
|
3016 case 'K': elt->zhours = 10; break;
|
yuuji@0
|
3017 case 'L': elt->zhours = 11; break;
|
yuuji@0
|
3018 case 'M': elt->zhours = 12; break;
|
yuuji@0
|
3019
|
yuuji@0
|
3020 /* occidental (from Greenwich) timezones */
|
yuuji@0
|
3021 case 'N': elt->zoccident = 1; elt->zhours = 1; break;
|
yuuji@0
|
3022 case 'O': elt->zoccident = 1; elt->zhours = 2; break;
|
yuuji@0
|
3023 #ifndef STRICT_RFC822_TIMEZONES
|
yuuji@0
|
3024 case (('A'-'A')*1024)+(('D'-'A')*32)+'T'-'A':
|
yuuji@0
|
3025 #endif
|
yuuji@0
|
3026 case 'P': elt->zoccident = 1; elt->zhours = 3; break;
|
yuuji@0
|
3027 #ifdef NEWFOUNDLAND_STANDARD_TIME
|
yuuji@0
|
3028 /* Newfoundland */
|
yuuji@0
|
3029 case (('N'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
|
yuuji@0
|
3030 elt->zoccident = 1; elt->zhours = 3; elt->zminutes = 30; break;
|
yuuji@0
|
3031 #endif
|
yuuji@0
|
3032 #ifndef STRICT_RFC822_TIMEZONES
|
yuuji@0
|
3033 /* Atlantic */
|
yuuji@0
|
3034 case (('A'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
|
yuuji@0
|
3035 #endif
|
yuuji@0
|
3036 /* CONUS */
|
yuuji@0
|
3037 case (('E'-'A')*1024)+(('D'-'A')*32)+'T'-'A':
|
yuuji@0
|
3038 case 'Q': elt->zoccident = 1; elt->zhours = 4; break;
|
yuuji@0
|
3039 /* Eastern */
|
yuuji@0
|
3040 case (('E'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
|
yuuji@0
|
3041 case (('C'-'A')*1024)+(('D'-'A')*32)+'T'-'A':
|
yuuji@0
|
3042 case 'R': elt->zoccident = 1; elt->zhours = 5; break;
|
yuuji@0
|
3043 /* Central */
|
yuuji@0
|
3044 case (('C'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
|
yuuji@0
|
3045 case (('M'-'A')*1024)+(('D'-'A')*32)+'T'-'A':
|
yuuji@0
|
3046 case 'S': elt->zoccident = 1; elt->zhours = 6; break;
|
yuuji@0
|
3047 /* Mountain */
|
yuuji@0
|
3048 case (('M'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
|
yuuji@0
|
3049 case (('P'-'A')*1024)+(('D'-'A')*32)+'T'-'A':
|
yuuji@0
|
3050 case 'T': elt->zoccident = 1; elt->zhours = 7; break;
|
yuuji@0
|
3051 /* Pacific */
|
yuuji@0
|
3052 case (('P'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
|
yuuji@0
|
3053 #ifndef STRICT_RFC822_TIMEZONES
|
yuuji@0
|
3054 case (('Y'-'A')*1024)+(('D'-'A')*32)+'T'-'A':
|
yuuji@0
|
3055 #endif
|
yuuji@0
|
3056 case 'U': elt->zoccident = 1; elt->zhours = 8; break;
|
yuuji@0
|
3057 #ifndef STRICT_RFC822_TIMEZONES
|
yuuji@0
|
3058 /* Yukon */
|
yuuji@0
|
3059 case (('Y'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
|
yuuji@0
|
3060 #endif
|
yuuji@0
|
3061 case 'V': elt->zoccident = 1; elt->zhours = 9; break;
|
yuuji@0
|
3062 #ifndef STRICT_RFC822_TIMEZONES
|
yuuji@0
|
3063 /* Hawaii */
|
yuuji@0
|
3064 case (('H'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
|
yuuji@0
|
3065 #endif
|
yuuji@0
|
3066 case 'W': elt->zoccident = 1; elt->zhours = 10; break;
|
yuuji@0
|
3067 /* Nome/Bering/Samoa */
|
yuuji@0
|
3068 #ifdef NOME_STANDARD_TIME
|
yuuji@0
|
3069 case (('N'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
|
yuuji@0
|
3070 #endif
|
yuuji@0
|
3071 #ifdef BERING_STANDARD_TIME
|
yuuji@0
|
3072 case (('B'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
|
yuuji@0
|
3073 #endif
|
yuuji@0
|
3074 #ifdef SAMOA_STANDARD_TIME
|
yuuji@0
|
3075 case (('S'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
|
yuuji@0
|
3076 #endif
|
yuuji@0
|
3077 case 'X': elt->zoccident = 1; elt->zhours = 11; break;
|
yuuji@0
|
3078 case 'Y': elt->zoccident = 1; elt->zhours = 12; break;
|
yuuji@0
|
3079
|
yuuji@0
|
3080 default: /* unknown time zones treated as local */
|
yuuji@0
|
3081 tn = time (0); /* time now... */
|
yuuji@0
|
3082 t = localtime (&tn); /* get local minutes since midnight */
|
yuuji@0
|
3083 mi = t->tm_hour * 60 + t->tm_min;
|
yuuji@0
|
3084 ms = t->tm_yday; /* note Julian day */
|
yuuji@0
|
3085 if (t = gmtime (&tn)) { /* minus UTC minutes since midnight */
|
yuuji@0
|
3086 mi -= t->tm_hour * 60 + t->tm_min;
|
yuuji@0
|
3087 /* ms can be one of:
|
yuuji@0
|
3088 * 36x local time is December 31, UTC is January 1, offset -24 hours
|
yuuji@0
|
3089 * 1 local time is 1 day ahead of UTC, offset +24 hours
|
yuuji@0
|
3090 * 0 local time is same day as UTC, no offset
|
yuuji@0
|
3091 * -1 local time is 1 day behind UTC, offset -24 hours
|
yuuji@0
|
3092 * -36x local time is January 1, UTC is December 31, offset +24 hours
|
yuuji@0
|
3093 */
|
yuuji@0
|
3094 if (ms -= t->tm_yday) /* correct offset if different Julian day */
|
yuuji@0
|
3095 mi += ((ms < 0) == (abs (ms) == 1)) ? -24*60 : 24*60;
|
yuuji@0
|
3096 if (mi < 0) { /* occidental? */
|
yuuji@0
|
3097 mi = abs (mi); /* yup, make positive number */
|
yuuji@0
|
3098 elt->zoccident = 1; /* and note west of UTC */
|
yuuji@0
|
3099 }
|
yuuji@0
|
3100 elt->zhours = mi / 60; /* now break into hours and minutes */
|
yuuji@0
|
3101 elt->zminutes = mi % 60;
|
yuuji@0
|
3102 }
|
yuuji@0
|
3103 break;
|
yuuji@0
|
3104 }
|
yuuji@0
|
3105 return T;
|
yuuji@0
|
3106 }
|
yuuji@0
|
3107
|
yuuji@0
|
3108 /* Mail n messages exist
|
yuuji@0
|
3109 * Accepts: mail stream
|
yuuji@0
|
3110 * number of messages
|
yuuji@0
|
3111 */
|
yuuji@0
|
3112
|
yuuji@0
|
3113 void mail_exists (MAILSTREAM *stream,unsigned long nmsgs)
|
yuuji@0
|
3114 {
|
yuuji@0
|
3115 char tmp[MAILTMPLEN];
|
yuuji@0
|
3116 if (nmsgs > MAXMESSAGES) {
|
yuuji@0
|
3117 sprintf (tmp,"Mailbox has more messages (%lu) exist than maximum (%lu)",
|
yuuji@0
|
3118 nmsgs,MAXMESSAGES);
|
yuuji@0
|
3119 mm_log (tmp,ERROR);
|
yuuji@0
|
3120 nmsgs = MAXMESSAGES; /* cap to maximum */
|
yuuji@0
|
3121 /* probably will crash in mail_elt() soon enough... */
|
yuuji@0
|
3122 }
|
yuuji@0
|
3123 /* make sure cache is large enough */
|
yuuji@0
|
3124 (*mailcache) (stream,nmsgs,CH_SIZE);
|
yuuji@0
|
3125 stream->nmsgs = nmsgs; /* update stream status */
|
yuuji@0
|
3126 /* notify main program of change */
|
yuuji@0
|
3127 if (!stream->silent) MM_EXISTS (stream,nmsgs);
|
yuuji@0
|
3128 }
|
yuuji@0
|
3129
|
yuuji@0
|
3130
|
yuuji@0
|
3131 /* Mail n messages are recent
|
yuuji@0
|
3132 * Accepts: mail stream
|
yuuji@0
|
3133 * number of recent messages
|
yuuji@0
|
3134 */
|
yuuji@0
|
3135
|
yuuji@0
|
3136 void mail_recent (MAILSTREAM *stream,unsigned long recent)
|
yuuji@0
|
3137 {
|
yuuji@0
|
3138 char tmp[MAILTMPLEN];
|
yuuji@0
|
3139 if (recent <= stream->nmsgs) stream->recent = recent;
|
yuuji@0
|
3140 else {
|
yuuji@0
|
3141 sprintf (tmp,"Non-existent recent message(s) %lu, nmsgs=%lu",
|
yuuji@0
|
3142 recent,stream->nmsgs);
|
yuuji@0
|
3143 mm_log (tmp,ERROR);
|
yuuji@0
|
3144 }
|
yuuji@0
|
3145 }
|
yuuji@0
|
3146
|
yuuji@0
|
3147
|
yuuji@0
|
3148 /* Mail message n is expunged
|
yuuji@0
|
3149 * Accepts: mail stream
|
yuuji@0
|
3150 * message #
|
yuuji@0
|
3151 */
|
yuuji@0
|
3152
|
yuuji@0
|
3153 void mail_expunged (MAILSTREAM *stream,unsigned long msgno)
|
yuuji@0
|
3154 {
|
yuuji@0
|
3155 char tmp[MAILTMPLEN];
|
yuuji@0
|
3156 MESSAGECACHE *elt;
|
yuuji@0
|
3157 if (msgno > stream->nmsgs) {
|
yuuji@0
|
3158 sprintf (tmp,"Expunge of non-existent message %lu, nmsgs=%lu",
|
yuuji@0
|
3159 msgno,stream->nmsgs);
|
yuuji@0
|
3160 mm_log (tmp,ERROR);
|
yuuji@0
|
3161 }
|
yuuji@0
|
3162 else {
|
yuuji@0
|
3163 elt = (MESSAGECACHE *) (*mailcache) (stream,msgno,CH_ELT);
|
yuuji@0
|
3164 /* notify main program of change */
|
yuuji@0
|
3165 if (!stream->silent) MM_EXPUNGED (stream,msgno);
|
yuuji@0
|
3166 if (elt) { /* if an element is there */
|
yuuji@0
|
3167 elt->msgno = 0; /* invalidate its message number and free */
|
yuuji@0
|
3168 (*mailcache) (stream,msgno,CH_FREE);
|
yuuji@0
|
3169 (*mailcache) (stream,msgno,CH_FREESORTCACHE);
|
yuuji@0
|
3170 }
|
yuuji@0
|
3171 /* expunge the slot */
|
yuuji@0
|
3172 (*mailcache) (stream,msgno,CH_EXPUNGE);
|
yuuji@0
|
3173 --stream->nmsgs; /* update stream status */
|
yuuji@0
|
3174 if (stream->msgno) { /* have stream pointers? */
|
yuuji@0
|
3175 /* make sure the short cache is nuked */
|
yuuji@0
|
3176 if (stream->scache) mail_gc (stream,GC_ENV | GC_TEXTS);
|
yuuji@0
|
3177 else stream->msgno = 0; /* make sure invalidated in any case */
|
yuuji@0
|
3178 }
|
yuuji@0
|
3179 }
|
yuuji@0
|
3180 }
|
yuuji@0
|
3181
|
yuuji@0
|
3182 /* Mail stream status routines */
|
yuuji@0
|
3183
|
yuuji@0
|
3184
|
yuuji@0
|
3185 /* Mail lock stream
|
yuuji@0
|
3186 * Accepts: mail stream
|
yuuji@0
|
3187 */
|
yuuji@0
|
3188
|
yuuji@0
|
3189 void mail_lock (MAILSTREAM *stream)
|
yuuji@0
|
3190 {
|
yuuji@0
|
3191 if (stream->lock) {
|
yuuji@0
|
3192 char tmp[MAILTMPLEN];
|
yuuji@0
|
3193 sprintf (tmp,"Lock when already locked, mbx=%.80s",
|
yuuji@0
|
3194 stream->mailbox ? stream->mailbox : "???");
|
yuuji@0
|
3195 fatal (tmp);
|
yuuji@0
|
3196 }
|
yuuji@0
|
3197 else stream->lock = T; /* lock stream */
|
yuuji@0
|
3198 }
|
yuuji@0
|
3199
|
yuuji@0
|
3200
|
yuuji@0
|
3201 /* Mail unlock stream
|
yuuji@0
|
3202 * Accepts: mail stream
|
yuuji@0
|
3203 */
|
yuuji@0
|
3204
|
yuuji@0
|
3205 void mail_unlock (MAILSTREAM *stream)
|
yuuji@0
|
3206 {
|
yuuji@0
|
3207 if (!stream->lock) fatal ("Unlock when not locked");
|
yuuji@0
|
3208 else stream->lock = NIL; /* unlock stream */
|
yuuji@0
|
3209 }
|
yuuji@0
|
3210
|
yuuji@0
|
3211
|
yuuji@0
|
3212 /* Mail turn on debugging telemetry
|
yuuji@0
|
3213 * Accepts: mail stream
|
yuuji@0
|
3214 */
|
yuuji@0
|
3215
|
yuuji@0
|
3216 void mail_debug (MAILSTREAM *stream)
|
yuuji@0
|
3217 {
|
yuuji@0
|
3218 stream->debug = T; /* turn on debugging telemetry */
|
yuuji@0
|
3219 if (stream->dtb) (*stream->dtb->parameters) (ENABLE_DEBUG,stream);
|
yuuji@0
|
3220 }
|
yuuji@0
|
3221
|
yuuji@0
|
3222
|
yuuji@0
|
3223 /* Mail turn off debugging telemetry
|
yuuji@0
|
3224 * Accepts: mail stream
|
yuuji@0
|
3225 */
|
yuuji@0
|
3226
|
yuuji@0
|
3227 void mail_nodebug (MAILSTREAM *stream)
|
yuuji@0
|
3228 {
|
yuuji@0
|
3229 stream->debug = NIL; /* turn off debugging telemetry */
|
yuuji@0
|
3230 if (stream->dtb) (*stream->dtb->parameters) (DISABLE_DEBUG,stream);
|
yuuji@0
|
3231 }
|
yuuji@0
|
3232
|
yuuji@0
|
3233
|
yuuji@0
|
3234 /* Mail log to debugging telemetry
|
yuuji@0
|
3235 * Accepts: message
|
yuuji@0
|
3236 * flag that data is "sensitive"
|
yuuji@0
|
3237 */
|
yuuji@0
|
3238
|
yuuji@0
|
3239 void mail_dlog (char *string,long flag)
|
yuuji@0
|
3240 {
|
yuuji@0
|
3241 mm_dlog ((debugsensitive || !flag) ? string : "<suppressed>");
|
yuuji@0
|
3242 }
|
yuuji@0
|
3243
|
yuuji@0
|
3244 /* Mail parse UID sequence
|
yuuji@0
|
3245 * Accepts: mail stream
|
yuuji@0
|
3246 * sequence to parse
|
yuuji@0
|
3247 * Returns: T if parse successful, else NIL
|
yuuji@0
|
3248 */
|
yuuji@0
|
3249
|
yuuji@0
|
3250 long mail_uid_sequence (MAILSTREAM *stream,unsigned char *sequence)
|
yuuji@0
|
3251 {
|
yuuji@0
|
3252 unsigned long i,j,k,x,y;
|
yuuji@0
|
3253 for (i = 1; i <= stream->nmsgs; i++) mail_elt (stream,i)->sequence = NIL;
|
yuuji@0
|
3254 while (sequence && *sequence){/* while there is something to parse */
|
yuuji@0
|
3255 if (*sequence == '*') { /* maximum message */
|
yuuji@0
|
3256 i = stream->nmsgs ? mail_uid (stream,stream->nmsgs) : stream->uid_last;
|
yuuji@0
|
3257 sequence++; /* skip past * */
|
yuuji@0
|
3258 }
|
yuuji@0
|
3259 /* parse and validate message number */
|
yuuji@0
|
3260 /* parse and validate message number */
|
yuuji@0
|
3261 else if (!isdigit (*sequence)) {
|
yuuji@0
|
3262 MM_LOG ("Syntax error in sequence",ERROR);
|
yuuji@0
|
3263 return NIL;
|
yuuji@0
|
3264 }
|
yuuji@0
|
3265 else if (!(i = strtoul (sequence,(char **) &sequence,10))) {
|
yuuji@0
|
3266 MM_LOG ("UID may not be zero",ERROR);
|
yuuji@0
|
3267 return NIL;
|
yuuji@0
|
3268 }
|
yuuji@0
|
3269 switch (*sequence) { /* see what the delimiter is */
|
yuuji@0
|
3270 case ':': /* sequence range */
|
yuuji@0
|
3271 if (*++sequence == '*') { /* maximum message */
|
yuuji@0
|
3272 j = stream->nmsgs ? mail_uid (stream,stream->nmsgs) : stream->uid_last;
|
yuuji@0
|
3273 sequence++; /* skip past * */
|
yuuji@0
|
3274 }
|
yuuji@0
|
3275 /* parse end of range */
|
yuuji@0
|
3276 else if (!(j = strtoul (sequence,(char **) &sequence,10))) {
|
yuuji@0
|
3277 MM_LOG ("UID sequence range invalid",ERROR);
|
yuuji@0
|
3278 return NIL;
|
yuuji@0
|
3279 }
|
yuuji@0
|
3280 if (*sequence && *sequence++ != ',') {
|
yuuji@0
|
3281 MM_LOG ("UID sequence range syntax error",ERROR);
|
yuuji@0
|
3282 return NIL;
|
yuuji@0
|
3283 }
|
yuuji@0
|
3284 if (i > j) { /* swap the range if backwards */
|
yuuji@0
|
3285 x = i; i = j; j = x;
|
yuuji@0
|
3286 }
|
yuuji@0
|
3287 x = mail_msgno (stream,i);/* get msgnos */
|
yuuji@0
|
3288 y = mail_msgno (stream,j);/* for both UIDS (don't && it) */
|
yuuji@0
|
3289 /* easy if both UIDs valid */
|
yuuji@0
|
3290 if (x && y) while (x <= y) mail_elt (stream,x++)->sequence = T;
|
yuuji@0
|
3291 /* start UID valid, end is not */
|
yuuji@0
|
3292 else if (x) while ((x <= stream->nmsgs) && (mail_uid (stream,x) <= j))
|
yuuji@0
|
3293 mail_elt (stream,x++)->sequence = T;
|
yuuji@0
|
3294 /* end UID valid, start is not */
|
yuuji@0
|
3295 else if (y) for (x = 1; x <= y; x++) {
|
yuuji@0
|
3296 if (mail_uid (stream,x) >= i) mail_elt (stream,x)->sequence = T;
|
yuuji@0
|
3297 }
|
yuuji@0
|
3298 /* neither is valid, ugh */
|
yuuji@0
|
3299 else for (x = 1; x <= stream->nmsgs; x++)
|
yuuji@0
|
3300 if (((k = mail_uid (stream,x)) >= i) && (k <= j))
|
yuuji@0
|
3301 mail_elt (stream,x)->sequence = T;
|
yuuji@0
|
3302 break;
|
yuuji@0
|
3303 case ',': /* single message */
|
yuuji@0
|
3304 ++sequence; /* skip the delimiter, fall into end case */
|
yuuji@0
|
3305 case '\0': /* end of sequence, mark this message */
|
yuuji@0
|
3306 if (x = mail_msgno (stream,i)) mail_elt (stream,x)->sequence = T;
|
yuuji@0
|
3307 break;
|
yuuji@0
|
3308 default: /* anything else is a syntax error! */
|
yuuji@0
|
3309 MM_LOG ("UID sequence syntax error",ERROR);
|
yuuji@0
|
3310 return NIL;
|
yuuji@0
|
3311 }
|
yuuji@0
|
3312 }
|
yuuji@0
|
3313 return T; /* successfully parsed sequence */
|
yuuji@0
|
3314 }
|
yuuji@0
|
3315
|
yuuji@0
|
3316 /* Mail see if line list matches that in cache
|
yuuji@0
|
3317 * Accepts: candidate line list
|
yuuji@0
|
3318 * cached line list
|
yuuji@0
|
3319 * matching flags
|
yuuji@0
|
3320 * Returns: T if match, NIL if no match
|
yuuji@0
|
3321 */
|
yuuji@0
|
3322
|
yuuji@0
|
3323 long mail_match_lines (STRINGLIST *lines,STRINGLIST *msglines,long flags)
|
yuuji@0
|
3324 {
|
yuuji@0
|
3325 unsigned long i;
|
yuuji@0
|
3326 unsigned char *s,*t;
|
yuuji@0
|
3327 STRINGLIST *m;
|
yuuji@0
|
3328 if (!msglines) return T; /* full header is in cache */
|
yuuji@0
|
3329 /* need full header but filtered in cache */
|
yuuji@0
|
3330 if ((flags & FT_NOT) || !lines) return NIL;
|
yuuji@0
|
3331 do { /* make sure all present & accounted for */
|
yuuji@0
|
3332 for (m = msglines; m; m = m->next) if (lines->text.size == m->text.size) {
|
yuuji@0
|
3333 for (s = lines->text.data,t = m->text.data,i = lines->text.size;
|
yuuji@0
|
3334 i && !compare_uchar (*s,*t); s++,t++,i--);
|
yuuji@0
|
3335 if (!i) break; /* this line matches */
|
yuuji@0
|
3336 }
|
yuuji@0
|
3337 if (!m) return NIL; /* didn't find in the list */
|
yuuji@0
|
3338 }
|
yuuji@0
|
3339 while (lines = lines->next);
|
yuuji@0
|
3340 return T; /* all lines found */
|
yuuji@0
|
3341 }
|
yuuji@0
|
3342
|
yuuji@0
|
3343 /* Mail filter text by header lines
|
yuuji@0
|
3344 * Accepts: text to filter, with trailing null
|
yuuji@0
|
3345 * length of text
|
yuuji@0
|
3346 * list of lines
|
yuuji@0
|
3347 * fetch flags
|
yuuji@0
|
3348 * Returns: new text size, text overwritten
|
yuuji@0
|
3349 */
|
yuuji@0
|
3350
|
yuuji@0
|
3351 unsigned long mail_filter (char *text,unsigned long len,STRINGLIST *lines,
|
yuuji@0
|
3352 long flags)
|
yuuji@0
|
3353 {
|
yuuji@0
|
3354 STRINGLIST *hdrs;
|
yuuji@0
|
3355 int notfound;
|
yuuji@0
|
3356 unsigned long i;
|
yuuji@0
|
3357 char c,*s,*e,*t,tmp[MAILTMPLEN];
|
yuuji@0
|
3358 char *src = text;
|
yuuji@0
|
3359 char *dst = src;
|
yuuji@0
|
3360 char *end = text + len;
|
yuuji@0
|
3361 text[len] = '\012'; /* guard against running off buffer */
|
yuuji@0
|
3362 while (src < end) { /* process header */
|
yuuji@0
|
3363 /* slurp header line name */
|
yuuji@0
|
3364 for (s = src,e = s + MAILTMPLEN - 1,e = (e < end ? e : end),t = tmp;
|
yuuji@0
|
3365 (s < e) && ((c = (*s ? *s : (*s = ' '))) != ':') &&
|
yuuji@0
|
3366 ((c > ' ') ||
|
yuuji@0
|
3367 ((c != ' ') && (c != '\t') && (c != '\015') && (c != '\012')));
|
yuuji@0
|
3368 *t++ = *s++);
|
yuuji@0
|
3369 *t = '\0'; /* tie off */
|
yuuji@0
|
3370 notfound = T; /* not found yet */
|
yuuji@0
|
3371 if (i = t - tmp) /* see if found in header */
|
yuuji@0
|
3372 for (hdrs = lines; hdrs && notfound; hdrs = hdrs->next)
|
yuuji@0
|
3373 if ((hdrs->text.size == i) && !compare_csizedtext (tmp,&hdrs->text))
|
yuuji@0
|
3374 notfound = NIL;
|
yuuji@0
|
3375 /* skip header line if not wanted */
|
yuuji@0
|
3376 if (i && ((flags & FT_NOT) ? !notfound : notfound))
|
yuuji@0
|
3377 while (((*src++ != '\012') && (*src++ != '\012') && (*src++ != '\012') &&
|
yuuji@0
|
3378 (*src++ != '\012') && (*src++ != '\012') && (*src++ != '\012') &&
|
yuuji@0
|
3379 (*src++ != '\012') && (*src++ != '\012') && (*src++ != '\012') &&
|
yuuji@0
|
3380 (*src++ != '\012')) || ((*src == ' ') || (*src == '\t')));
|
yuuji@0
|
3381 else if (src == dst) { /* copy to self */
|
yuuji@0
|
3382 while (((*src++ != '\012') && (*src++ != '\012') && (*src++ != '\012') &&
|
yuuji@0
|
3383 (*src++ != '\012') && (*src++ != '\012') && (*src++ != '\012') &&
|
yuuji@0
|
3384 (*src++ != '\012') && (*src++ != '\012') && (*src++ != '\012') &&
|
yuuji@0
|
3385 (*src++ != '\012')) || ((*src == ' ') || (*src == '\t')));
|
yuuji@0
|
3386 dst = src; /* update destination */
|
yuuji@0
|
3387 }
|
yuuji@0
|
3388 else { /* copy line and any continuation line */
|
yuuji@0
|
3389 while ((((*dst++ = *src++) != '\012') && ((*dst++ = *src++) != '\012') &&
|
yuuji@0
|
3390 ((*dst++ = *src++) != '\012') && ((*dst++ = *src++) != '\012') &&
|
yuuji@0
|
3391 ((*dst++ = *src++) != '\012') && ((*dst++ = *src++) != '\012') &&
|
yuuji@0
|
3392 ((*dst++ = *src++) != '\012') && ((*dst++ = *src++) != '\012') &&
|
yuuji@0
|
3393 ((*dst++ = *src++) != '\012') && ((*dst++ = *src++) != '\012'))||
|
yuuji@0
|
3394 ((*src == ' ') || (*src == '\t')));
|
yuuji@0
|
3395 /* in case hit the guard LF */
|
yuuji@0
|
3396 if (src > end) dst -= (src - end);
|
yuuji@0
|
3397 }
|
yuuji@0
|
3398 }
|
yuuji@0
|
3399 *dst = '\0'; /* tie off destination */
|
yuuji@0
|
3400 return dst - text;
|
yuuji@0
|
3401 }
|
yuuji@0
|
3402
|
yuuji@0
|
3403 /* Local mail search message
|
yuuji@0
|
3404 * Accepts: MAIL stream
|
yuuji@0
|
3405 * message number
|
yuuji@0
|
3406 * optional section specification
|
yuuji@0
|
3407 * search program
|
yuuji@0
|
3408 * Returns: T if found, NIL otherwise
|
yuuji@0
|
3409 */
|
yuuji@0
|
3410
|
yuuji@0
|
3411 long mail_search_msg (MAILSTREAM *stream,unsigned long msgno,char *section,
|
yuuji@0
|
3412 SEARCHPGM *pgm)
|
yuuji@0
|
3413 {
|
yuuji@0
|
3414 unsigned short d;
|
yuuji@0
|
3415 char tmp[MAILTMPLEN];
|
yuuji@0
|
3416 MESSAGECACHE *elt = mail_elt (stream,msgno);
|
yuuji@0
|
3417 SEARCHHEADER *hdr;
|
yuuji@0
|
3418 SEARCHOR *or;
|
yuuji@0
|
3419 SEARCHPGMLIST *not;
|
yuuji@0
|
3420 unsigned long now = (unsigned long) time (0);
|
yuuji@0
|
3421 if (pgm->msgno || pgm->uid) { /* message set searches */
|
yuuji@0
|
3422 SEARCHSET *set;
|
yuuji@0
|
3423 /* message sequences */
|
yuuji@0
|
3424 if (pgm->msgno) { /* inside this message sequence set */
|
yuuji@0
|
3425 for (set = pgm->msgno; set; set = set->next)
|
yuuji@0
|
3426 if (set->last ? ((set->first <= set->last) ?
|
yuuji@0
|
3427 ((msgno >= set->first) && (msgno <= set->last)) :
|
yuuji@0
|
3428 ((msgno >= set->last) && (msgno <= set->first))) :
|
yuuji@0
|
3429 msgno == set->first) break;
|
yuuji@0
|
3430 if (!set) return NIL; /* not found within sequence */
|
yuuji@0
|
3431 }
|
yuuji@0
|
3432 if (pgm->uid) { /* inside this unique identifier set */
|
yuuji@0
|
3433 unsigned long uid = mail_uid (stream,msgno);
|
yuuji@0
|
3434 for (set = pgm->uid; set; set = set->next)
|
yuuji@0
|
3435 if (set->last ? ((set->first <= set->last) ?
|
yuuji@0
|
3436 ((uid >= set->first) && (uid <= set->last)) :
|
yuuji@0
|
3437 ((uid >= set->last) && (uid <= set->first))) :
|
yuuji@0
|
3438 uid == set->first) break;
|
yuuji@0
|
3439 if (!set) return NIL; /* not found within sequence */
|
yuuji@0
|
3440 }
|
yuuji@0
|
3441 }
|
yuuji@0
|
3442
|
yuuji@0
|
3443 /* Fast data searches */
|
yuuji@0
|
3444 /* need to fetch fast data? */
|
yuuji@0
|
3445 if ((!elt->rfc822_size && (pgm->larger || pgm->smaller)) ||
|
yuuji@0
|
3446 (!elt->year && (pgm->before || pgm->on || pgm->since ||
|
yuuji@0
|
3447 pgm->older || pgm->younger)) ||
|
yuuji@0
|
3448 (!elt->valid && (pgm->answered || pgm->unanswered ||
|
yuuji@0
|
3449 pgm->deleted || pgm->undeleted ||
|
yuuji@0
|
3450 pgm->draft || pgm->undraft ||
|
yuuji@0
|
3451 pgm->flagged || pgm->unflagged ||
|
yuuji@0
|
3452 pgm->recent || pgm->old ||
|
yuuji@0
|
3453 pgm->seen || pgm->unseen ||
|
yuuji@0
|
3454 pgm->keyword || pgm->unkeyword))) {
|
yuuji@0
|
3455 unsigned long i;
|
yuuji@0
|
3456 MESSAGECACHE *ielt;
|
yuuji@0
|
3457 for (i = elt->msgno; /* find last unloaded message in range */
|
yuuji@0
|
3458 (i < stream->nmsgs) && (ielt = mail_elt (stream,i+1)) &&
|
yuuji@0
|
3459 ((!ielt->rfc822_size && (pgm->larger || pgm->smaller)) ||
|
yuuji@0
|
3460 (!ielt->year && (pgm->before || pgm->on || pgm->since ||
|
yuuji@0
|
3461 pgm->older || pgm->younger)) ||
|
yuuji@0
|
3462 (!ielt->valid && (pgm->answered || pgm->unanswered ||
|
yuuji@0
|
3463 pgm->deleted || pgm->undeleted ||
|
yuuji@0
|
3464 pgm->draft || pgm->undraft ||
|
yuuji@0
|
3465 pgm->flagged || pgm->unflagged ||
|
yuuji@0
|
3466 pgm->recent || pgm->old ||
|
yuuji@0
|
3467 pgm->seen || pgm->unseen ||
|
yuuji@0
|
3468 pgm->keyword || pgm->unkeyword))); ++i);
|
yuuji@0
|
3469 if (i == elt->msgno) sprintf (tmp,"%lu",elt->msgno);
|
yuuji@0
|
3470 else sprintf (tmp,"%lu:%lu",elt->msgno,i);
|
yuuji@0
|
3471 mail_fetch_fast (stream,tmp,NIL);
|
yuuji@0
|
3472 }
|
yuuji@0
|
3473 /* size ranges */
|
yuuji@0
|
3474 if ((pgm->larger && (elt->rfc822_size <= pgm->larger)) ||
|
yuuji@0
|
3475 (pgm->smaller && (elt->rfc822_size >= pgm->smaller))) return NIL;
|
yuuji@0
|
3476 /* message flags */
|
yuuji@0
|
3477 if ((pgm->answered && !elt->answered) ||
|
yuuji@0
|
3478 (pgm->unanswered && elt->answered) ||
|
yuuji@0
|
3479 (pgm->deleted && !elt->deleted) ||
|
yuuji@0
|
3480 (pgm->undeleted && elt->deleted) ||
|
yuuji@0
|
3481 (pgm->draft && !elt->draft) ||
|
yuuji@0
|
3482 (pgm->undraft && elt->draft) ||
|
yuuji@0
|
3483 (pgm->flagged && !elt->flagged) ||
|
yuuji@0
|
3484 (pgm->unflagged && elt->flagged) ||
|
yuuji@0
|
3485 (pgm->recent && !elt->recent) ||
|
yuuji@0
|
3486 (pgm->old && elt->recent) ||
|
yuuji@0
|
3487 (pgm->seen && !elt->seen) ||
|
yuuji@0
|
3488 (pgm->unseen && elt->seen)) return NIL;
|
yuuji@0
|
3489 /* keywords */
|
yuuji@0
|
3490 if ((pgm->keyword && !mail_search_keyword (stream,elt,pgm->keyword,LONGT)) ||
|
yuuji@0
|
3491 (pgm->unkeyword && !mail_search_keyword (stream,elt,pgm->unkeyword,NIL)))
|
yuuji@0
|
3492 return NIL;
|
yuuji@0
|
3493 /* internal date ranges */
|
yuuji@0
|
3494 if (pgm->before || pgm->on || pgm->since) {
|
yuuji@0
|
3495 d = mail_shortdate (elt->year,elt->month,elt->day);
|
yuuji@0
|
3496 if (pgm->before && (d >= pgm->before)) return NIL;
|
yuuji@0
|
3497 if (pgm->on && (d != pgm->on)) return NIL;
|
yuuji@0
|
3498 if (pgm->since && (d < pgm->since)) return NIL;
|
yuuji@0
|
3499 }
|
yuuji@0
|
3500 if (pgm->older || pgm->younger) {
|
yuuji@0
|
3501 unsigned long msgd = mail_longdate (elt);
|
yuuji@0
|
3502 if (pgm->older && msgd > (now - pgm->older)) return NIL;
|
yuuji@0
|
3503 if (pgm->younger && msgd < (now - pgm->younger)) return NIL;
|
yuuji@0
|
3504 }
|
yuuji@0
|
3505
|
yuuji@0
|
3506 /* envelope searches */
|
yuuji@0
|
3507 if (pgm->sentbefore || pgm->senton || pgm->sentsince ||
|
yuuji@0
|
3508 pgm->bcc || pgm->cc || pgm->from || pgm->to || pgm->subject ||
|
yuuji@0
|
3509 pgm->return_path || pgm->sender || pgm->reply_to || pgm->in_reply_to ||
|
yuuji@0
|
3510 pgm->message_id || pgm->newsgroups || pgm->followup_to ||
|
yuuji@0
|
3511 pgm->references) {
|
yuuji@0
|
3512 ENVELOPE *env;
|
yuuji@0
|
3513 MESSAGECACHE delt;
|
yuuji@0
|
3514 if (section) { /* use body part envelope */
|
yuuji@0
|
3515 BODY *body = mail_body (stream,msgno,section);
|
yuuji@0
|
3516 env = (body && (body->type == TYPEMESSAGE) && body->subtype &&
|
yuuji@0
|
3517 !strcmp (body->subtype,"RFC822")) ? body->nested.msg->env : NIL;
|
yuuji@0
|
3518 }
|
yuuji@0
|
3519 else { /* use top level envelope if no section */
|
yuuji@0
|
3520 if (pgm->header && !stream->scache && !(stream->dtb->flags & DR_LOCAL))
|
yuuji@0
|
3521 mail_fetch_header(stream,msgno,NIL,NIL,NIL,FT_PEEK|FT_SEARCHLOOKAHEAD);
|
yuuji@0
|
3522 env = mail_fetchenvelope (stream,msgno);
|
yuuji@0
|
3523 }
|
yuuji@0
|
3524 if (!env) return NIL; /* no envelope obtained */
|
yuuji@0
|
3525 /* sent date ranges */
|
yuuji@0
|
3526 if ((pgm->sentbefore || pgm->senton || pgm->sentsince) &&
|
yuuji@0
|
3527 (!mail_parse_date (&delt,env->date) ||
|
yuuji@0
|
3528 !(d = mail_shortdate (delt.year,delt.month,delt.day)) ||
|
yuuji@0
|
3529 (pgm->sentbefore && (d >= pgm->sentbefore)) ||
|
yuuji@0
|
3530 (pgm->senton && (d != pgm->senton)) ||
|
yuuji@0
|
3531 (pgm->sentsince && (d < pgm->sentsince)))) return NIL;
|
yuuji@0
|
3532 /* search headers */
|
yuuji@0
|
3533 if ((pgm->bcc && !mail_search_addr (env->bcc,pgm->bcc)) ||
|
yuuji@0
|
3534 (pgm->cc && !mail_search_addr (env->cc,pgm->cc)) ||
|
yuuji@0
|
3535 (pgm->from && !mail_search_addr (env->from,pgm->from)) ||
|
yuuji@0
|
3536 (pgm->to && !mail_search_addr (env->to,pgm->to)) ||
|
yuuji@0
|
3537 (pgm->subject && !mail_search_header_text (env->subject,pgm->subject)))
|
yuuji@0
|
3538 return NIL;
|
yuuji@0
|
3539 /* These criteria are not supported by IMAP and have to be emulated */
|
yuuji@0
|
3540 if ((pgm->return_path &&
|
yuuji@0
|
3541 !mail_search_addr (env->return_path,pgm->return_path)) ||
|
yuuji@0
|
3542 (pgm->sender && !mail_search_addr (env->sender,pgm->sender)) ||
|
yuuji@0
|
3543 (pgm->reply_to && !mail_search_addr (env->reply_to,pgm->reply_to)) ||
|
yuuji@0
|
3544 (pgm->in_reply_to &&
|
yuuji@0
|
3545 !mail_search_header_text (env->in_reply_to,pgm->in_reply_to)) ||
|
yuuji@0
|
3546 (pgm->message_id &&
|
yuuji@0
|
3547 !mail_search_header_text (env->message_id,pgm->message_id)) ||
|
yuuji@0
|
3548 (pgm->newsgroups &&
|
yuuji@0
|
3549 !mail_search_header_text (env->newsgroups,pgm->newsgroups)) ||
|
yuuji@0
|
3550 (pgm->followup_to &&
|
yuuji@0
|
3551 !mail_search_header_text (env->followup_to,pgm->followup_to)) ||
|
yuuji@0
|
3552 (pgm->references &&
|
yuuji@0
|
3553 !mail_search_header_text (env->references,pgm->references)))
|
yuuji@0
|
3554 return NIL;
|
yuuji@0
|
3555 }
|
yuuji@0
|
3556
|
yuuji@0
|
3557 /* search header lines */
|
yuuji@0
|
3558 for (hdr = pgm->header; hdr; hdr = hdr->next) {
|
yuuji@0
|
3559 char *t,*e,*v;
|
yuuji@0
|
3560 SIZEDTEXT s;
|
yuuji@0
|
3561 STRINGLIST sth,stc;
|
yuuji@0
|
3562 sth.next = stc.next = NIL; /* only one at a time */
|
yuuji@0
|
3563 sth.text.data = hdr->line.data;
|
yuuji@0
|
3564 sth.text.size = hdr->line.size;
|
yuuji@0
|
3565 /* get the header text */
|
yuuji@0
|
3566 if ((t = mail_fetch_header (stream,msgno,NIL,&sth,&s.size,
|
yuuji@0
|
3567 FT_INTERNAL | FT_PEEK |
|
yuuji@0
|
3568 (section ? NIL : FT_SEARCHLOOKAHEAD))) &&
|
yuuji@0
|
3569 strchr (t,':')) {
|
yuuji@0
|
3570 if (hdr->text.size) { /* anything matches empty search string */
|
yuuji@0
|
3571 /* non-empty, copy field data */
|
yuuji@0
|
3572 s.data = (unsigned char *) fs_get (s.size + 1);
|
yuuji@0
|
3573 /* for each line */
|
yuuji@0
|
3574 for (v = (char *) s.data, e = t + s.size; t < e;) switch (*t) {
|
yuuji@0
|
3575 default: /* non-continuation, skip leading field name */
|
yuuji@0
|
3576 while ((t < e) && (*t++ != ':'));
|
yuuji@0
|
3577 if ((t < e) && (*t == ':')) t++;
|
yuuji@0
|
3578 case '\t': case ' ': /* copy field data */
|
yuuji@0
|
3579 while ((t < e) && (*t != '\015') && (*t != '\012')) *v++ = *t++;
|
yuuji@0
|
3580 *v++ = '\n'; /* tie off line */
|
yuuji@0
|
3581 while (((*t == '\015') || (*t == '\012')) && (t < e)) t++;
|
yuuji@0
|
3582 }
|
yuuji@0
|
3583 /* calculate true size */
|
yuuji@0
|
3584 s.size = v - (char *) s.data;
|
yuuji@0
|
3585 *v = '\0'; /* tie off results */
|
yuuji@0
|
3586 stc.text.data = hdr->text.data;
|
yuuji@0
|
3587 stc.text.size = hdr->text.size;
|
yuuji@0
|
3588 /* search header */
|
yuuji@0
|
3589 if (mail_search_header (&s,&stc)) fs_give ((void **) &s.data);
|
yuuji@0
|
3590 else { /* search failed */
|
yuuji@0
|
3591 fs_give ((void **) &s.data);
|
yuuji@0
|
3592 return NIL;
|
yuuji@0
|
3593 }
|
yuuji@0
|
3594 }
|
yuuji@0
|
3595 }
|
yuuji@0
|
3596 else return NIL; /* no matching header text */
|
yuuji@0
|
3597 }
|
yuuji@0
|
3598 /* search strings */
|
yuuji@0
|
3599 if ((pgm->text && !mail_search_text (stream,msgno,section,pgm->text,LONGT))||
|
yuuji@0
|
3600 (pgm->body && !mail_search_text (stream,msgno,section,pgm->body,NIL)))
|
yuuji@0
|
3601 return NIL;
|
yuuji@0
|
3602 /* logical conditions */
|
yuuji@0
|
3603 for (or = pgm->or; or; or = or->next)
|
yuuji@0
|
3604 if (!(mail_search_msg (stream,msgno,section,or->first) ||
|
yuuji@0
|
3605 mail_search_msg (stream,msgno,section,or->second))) return NIL;
|
yuuji@0
|
3606 for (not = pgm->not; not; not = not->next)
|
yuuji@0
|
3607 if (mail_search_msg (stream,msgno,section,not->pgm)) return NIL;
|
yuuji@0
|
3608 return T;
|
yuuji@0
|
3609 }
|
yuuji@0
|
3610
|
yuuji@0
|
3611 /* Mail search message header null-terminated text
|
yuuji@0
|
3612 * Accepts: header text
|
yuuji@0
|
3613 * strings to search
|
yuuji@0
|
3614 * Returns: T if search found a match
|
yuuji@0
|
3615 */
|
yuuji@0
|
3616
|
yuuji@0
|
3617 long mail_search_header_text (char *s,STRINGLIST *st)
|
yuuji@0
|
3618 {
|
yuuji@0
|
3619 SIZEDTEXT h;
|
yuuji@0
|
3620 /* have any text? */
|
yuuji@0
|
3621 if (h.data = (unsigned char *) s) {
|
yuuji@0
|
3622 h.size = strlen (s); /* yes, get its size */
|
yuuji@0
|
3623 return mail_search_header (&h,st);
|
yuuji@0
|
3624 }
|
yuuji@0
|
3625 return NIL;
|
yuuji@0
|
3626 }
|
yuuji@0
|
3627
|
yuuji@0
|
3628
|
yuuji@0
|
3629 /* Mail search message header
|
yuuji@0
|
3630 * Accepts: header as sized text
|
yuuji@0
|
3631 * strings to search
|
yuuji@0
|
3632 * Returns: T if search found a match
|
yuuji@0
|
3633 */
|
yuuji@0
|
3634
|
yuuji@0
|
3635 long mail_search_header (SIZEDTEXT *hdr,STRINGLIST *st)
|
yuuji@0
|
3636 {
|
yuuji@0
|
3637 SIZEDTEXT h;
|
yuuji@0
|
3638 long ret = LONGT;
|
yuuji@0
|
3639 /* make UTF-8 version of header */
|
yuuji@0
|
3640 utf8_mime2text (hdr,&h,U8T_CANONICAL);
|
yuuji@0
|
3641 while (h.size && ((h.data[h.size-1]=='\015') || (h.data[h.size-1]=='\012')))
|
yuuji@0
|
3642 --h.size; /* slice off trailing newlines */
|
yuuji@0
|
3643 do if (h.size ? /* search non-empty string */
|
yuuji@0
|
3644 !ssearch (h.data,h.size,st->text.data,st->text.size) : st->text.size)
|
yuuji@0
|
3645 ret = NIL;
|
yuuji@0
|
3646 while (ret && (st = st->next));
|
yuuji@0
|
3647 if (h.data != hdr->data) fs_give ((void **) &h.data);
|
yuuji@0
|
3648 return ret;
|
yuuji@0
|
3649 }
|
yuuji@0
|
3650
|
yuuji@0
|
3651 /* Mail search message body
|
yuuji@0
|
3652 * Accepts: MAIL stream
|
yuuji@0
|
3653 * message number
|
yuuji@0
|
3654 * optional section specification
|
yuuji@0
|
3655 * string list
|
yuuji@0
|
3656 * flags
|
yuuji@0
|
3657 * Returns: T if search found a match
|
yuuji@0
|
3658 */
|
yuuji@0
|
3659
|
yuuji@0
|
3660 long mail_search_text (MAILSTREAM *stream,unsigned long msgno,char *section,
|
yuuji@0
|
3661 STRINGLIST *st,long flags)
|
yuuji@0
|
3662 {
|
yuuji@0
|
3663 BODY *body;
|
yuuji@0
|
3664 long ret = NIL;
|
yuuji@0
|
3665 STRINGLIST *s = mail_newstringlist ();
|
yuuji@0
|
3666 mailgets_t omg = mailgets;
|
yuuji@0
|
3667 if (stream->dtb->flags & DR_LOWMEM) mailgets = mail_search_gets;
|
yuuji@0
|
3668 /* strings to search */
|
yuuji@0
|
3669 for (stream->private.search.string = s; st;) {
|
yuuji@0
|
3670 s->text.data = st->text.data;
|
yuuji@0
|
3671 s->text.size = st->text.size;
|
yuuji@0
|
3672 if (st = st->next) s = s->next = mail_newstringlist ();
|
yuuji@0
|
3673 }
|
yuuji@0
|
3674 stream->private.search.text = NIL;
|
yuuji@0
|
3675 if (flags) { /* want header? */
|
yuuji@0
|
3676 SIZEDTEXT s,t;
|
yuuji@0
|
3677 s.data = (unsigned char *)
|
yuuji@0
|
3678 mail_fetch_header (stream,msgno,section,NIL,&s.size,FT_INTERNAL|FT_PEEK);
|
yuuji@0
|
3679 utf8_mime2text (&s,&t,U8T_CANONICAL);
|
yuuji@0
|
3680 ret = mail_search_string_work (&t,&stream->private.search.string);
|
yuuji@0
|
3681 if (t.data != s.data) fs_give ((void **) &t.data);
|
yuuji@0
|
3682 }
|
yuuji@0
|
3683 if (!ret) { /* still looking for match? */
|
yuuji@0
|
3684 /* no section, get top-level body */
|
yuuji@0
|
3685 if (!section) mail_fetchstructure (stream,msgno,&body);
|
yuuji@0
|
3686 /* get body of nested message */
|
yuuji@0
|
3687 else if ((body = mail_body (stream,msgno,section)) &&
|
yuuji@0
|
3688 (body->type == TYPEMULTIPART) && body->subtype &&
|
yuuji@0
|
3689 !strcmp (body->subtype,"RFC822")) body = body->nested.msg->body;
|
yuuji@0
|
3690 if (body) ret = mail_search_body (stream,msgno,body,NIL,1,flags);
|
yuuji@0
|
3691 }
|
yuuji@0
|
3692 mailgets = omg; /* restore former gets routine */
|
yuuji@0
|
3693 /* clear searching */
|
yuuji@0
|
3694 for (s = stream->private.search.string; s; s = s->next) s->text.data = NIL;
|
yuuji@0
|
3695 mail_free_stringlist (&stream->private.search.string);
|
yuuji@0
|
3696 stream->private.search.text = NIL;
|
yuuji@0
|
3697 return ret;
|
yuuji@0
|
3698 }
|
yuuji@0
|
3699
|
yuuji@0
|
3700 /* Mail search message body text parts
|
yuuji@0
|
3701 * Accepts: MAIL stream
|
yuuji@0
|
3702 * message number
|
yuuji@0
|
3703 * current body pointer
|
yuuji@0
|
3704 * hierarchical level prefix
|
yuuji@0
|
3705 * position at current hierarchical level
|
yuuji@0
|
3706 * string list
|
yuuji@0
|
3707 * flags
|
yuuji@0
|
3708 * Returns: T if search found a match
|
yuuji@0
|
3709 */
|
yuuji@0
|
3710
|
yuuji@0
|
3711 long mail_search_body (MAILSTREAM *stream,unsigned long msgno,BODY *body,
|
yuuji@0
|
3712 char *prefix,unsigned long section,long flags)
|
yuuji@0
|
3713 {
|
yuuji@0
|
3714 long ret = NIL;
|
yuuji@0
|
3715 unsigned long i;
|
yuuji@0
|
3716 char *s,*t,sect[MAILTMPLEN];
|
yuuji@0
|
3717 SIZEDTEXT st,h;
|
yuuji@0
|
3718 PART *part;
|
yuuji@0
|
3719 PARAMETER *param;
|
yuuji@0
|
3720 if (prefix && (strlen (prefix) > (MAILTMPLEN - 20))) return NIL;
|
yuuji@0
|
3721 sprintf (sect,"%s%lu",prefix ? prefix : "",section++);
|
yuuji@0
|
3722 if (flags && prefix) { /* want to search MIME header too? */
|
yuuji@0
|
3723 st.data = (unsigned char *) mail_fetch_mime (stream,msgno,sect,&st.size,
|
yuuji@0
|
3724 FT_INTERNAL | FT_PEEK);
|
yuuji@0
|
3725 if (stream->dtb->flags & DR_LOWMEM) ret = stream->private.search.result;
|
yuuji@0
|
3726 else {
|
yuuji@0
|
3727 /* make UTF-8 version of header */
|
yuuji@0
|
3728 utf8_mime2text (&st,&h,U8T_CANONICAL);
|
yuuji@0
|
3729 ret = mail_search_string_work (&h,&stream->private.search.string);
|
yuuji@0
|
3730 if (h.data != st.data) fs_give ((void **) &h.data);
|
yuuji@0
|
3731 }
|
yuuji@0
|
3732 }
|
yuuji@0
|
3733 if (!ret) switch (body->type) {
|
yuuji@0
|
3734 case TYPEMULTIPART:
|
yuuji@0
|
3735 /* extend prefix if not first time */
|
yuuji@0
|
3736 s = prefix ? strcat (sect,".") : "";
|
yuuji@0
|
3737 for (i = 1,part = body->nested.part; part && !ret; i++,part = part->next)
|
yuuji@0
|
3738 ret = mail_search_body (stream,msgno,&part->body,s,i,flags);
|
yuuji@0
|
3739 break;
|
yuuji@0
|
3740 case TYPEMESSAGE:
|
yuuji@0
|
3741 if (!strcmp (body->subtype,"RFC822")) {
|
yuuji@0
|
3742 if (flags) { /* want to search nested message header? */
|
yuuji@0
|
3743 st.data = (unsigned char *)
|
yuuji@0
|
3744 mail_fetch_header (stream,msgno,sect,NIL,&st.size,
|
yuuji@0
|
3745 FT_INTERNAL | FT_PEEK);
|
yuuji@0
|
3746 if (stream->dtb->flags & DR_LOWMEM) ret =stream->private.search.result;
|
yuuji@0
|
3747 else {
|
yuuji@0
|
3748 /* make UTF-8 version of header */
|
yuuji@0
|
3749 utf8_mime2text (&st,&h,U8T_CANONICAL);
|
yuuji@0
|
3750 ret = mail_search_string_work (&h,&stream->private.search.string);
|
yuuji@0
|
3751 if (h.data != st.data) fs_give ((void **) &h.data);
|
yuuji@0
|
3752 }
|
yuuji@0
|
3753 }
|
yuuji@0
|
3754 if (body = body->nested.msg->body)
|
yuuji@0
|
3755 ret = (body->type == TYPEMULTIPART) ?
|
yuuji@0
|
3756 mail_search_body (stream,msgno,body,(prefix ? prefix : ""),
|
yuuji@0
|
3757 section - 1,flags) :
|
yuuji@0
|
3758 mail_search_body (stream,msgno,body,strcat (sect,"."),1,flags);
|
yuuji@0
|
3759 break;
|
yuuji@0
|
3760 }
|
yuuji@0
|
3761 /* non-MESSAGE/RFC822 falls into text case */
|
yuuji@0
|
3762
|
yuuji@0
|
3763 case TYPETEXT:
|
yuuji@0
|
3764 s = mail_fetch_body (stream,msgno,sect,&i,FT_INTERNAL | FT_PEEK);
|
yuuji@0
|
3765 if (stream->dtb->flags & DR_LOWMEM) ret = stream->private.search.result;
|
yuuji@0
|
3766 else {
|
yuuji@0
|
3767 for (t = NIL,param = body->parameter; param && !t; param = param->next)
|
yuuji@0
|
3768 if (!strcmp (param->attribute,"CHARSET")) t = param->value;
|
yuuji@0
|
3769 switch (body->encoding) { /* what encoding? */
|
yuuji@0
|
3770 case ENCBASE64:
|
yuuji@0
|
3771 if (st.data = (unsigned char *)
|
yuuji@0
|
3772 rfc822_base64 ((unsigned char *) s,i,&st.size)) {
|
yuuji@0
|
3773 ret = mail_search_string (&st,t,&stream->private.search.string);
|
yuuji@0
|
3774 fs_give ((void **) &st.data);
|
yuuji@0
|
3775 }
|
yuuji@0
|
3776 break;
|
yuuji@0
|
3777 case ENCQUOTEDPRINTABLE:
|
yuuji@0
|
3778 if (st.data = rfc822_qprint ((unsigned char *) s,i,&st.size)) {
|
yuuji@0
|
3779 ret = mail_search_string (&st,t,&stream->private.search.string);
|
yuuji@0
|
3780 fs_give ((void **) &st.data);
|
yuuji@0
|
3781 }
|
yuuji@0
|
3782 break;
|
yuuji@0
|
3783 default:
|
yuuji@0
|
3784 st.data = (unsigned char *) s;
|
yuuji@0
|
3785 st.size = i;
|
yuuji@0
|
3786 ret = mail_search_string (&st,t,&stream->private.search.string);
|
yuuji@0
|
3787 break;
|
yuuji@0
|
3788 }
|
yuuji@0
|
3789 }
|
yuuji@0
|
3790 break;
|
yuuji@0
|
3791 }
|
yuuji@0
|
3792 return ret;
|
yuuji@0
|
3793 }
|
yuuji@0
|
3794
|
yuuji@0
|
3795 /* Mail search text
|
yuuji@0
|
3796 * Accepts: sized text to search
|
yuuji@0
|
3797 * character set of sized text
|
yuuji@0
|
3798 * string list of search keys
|
yuuji@0
|
3799 * Returns: T if search found a match
|
yuuji@0
|
3800 */
|
yuuji@0
|
3801
|
yuuji@0
|
3802 long mail_search_string (SIZEDTEXT *s,char *charset,STRINGLIST **st)
|
yuuji@0
|
3803 {
|
yuuji@0
|
3804 SIZEDTEXT u;
|
yuuji@0
|
3805 long ret;
|
yuuji@0
|
3806 STRINGLIST **sc = st;
|
yuuji@0
|
3807 /* convert to UTF-8 as best we can */
|
yuuji@0
|
3808 if (!utf8_text (s,charset,&u,U8T_CANONICAL))
|
yuuji@0
|
3809 utf8_text (s,NIL,&u,U8T_CANONICAL);
|
yuuji@0
|
3810 ret = mail_search_string_work (&u,st);
|
yuuji@0
|
3811 if (u.data != s->data) fs_give ((void **) &u.data);
|
yuuji@0
|
3812 return ret;
|
yuuji@0
|
3813 }
|
yuuji@0
|
3814
|
yuuji@0
|
3815
|
yuuji@0
|
3816 /* Mail search text worker routine
|
yuuji@0
|
3817 * Accepts: sized text to search
|
yuuji@0
|
3818 * string list of search keys
|
yuuji@0
|
3819 * Returns: T if search found a match
|
yuuji@0
|
3820 */
|
yuuji@0
|
3821
|
yuuji@0
|
3822 long mail_search_string_work (SIZEDTEXT *s,STRINGLIST **st)
|
yuuji@0
|
3823 {
|
yuuji@0
|
3824 void *t;
|
yuuji@0
|
3825 STRINGLIST **sc = st;
|
yuuji@0
|
3826 while (*sc) { /* run down criteria list */
|
yuuji@0
|
3827 if (ssearch (s->data,s->size,(*sc)->text.data,(*sc)->text.size)) {
|
yuuji@0
|
3828 t = (void *) (*sc); /* found one, need to flush this */
|
yuuji@0
|
3829 *sc = (*sc)->next; /* remove it from the list */
|
yuuji@0
|
3830 fs_give (&t); /* flush the buffer */
|
yuuji@0
|
3831 }
|
yuuji@0
|
3832 else sc = &(*sc)->next; /* move to next in list */
|
yuuji@0
|
3833 }
|
yuuji@0
|
3834 return *st ? NIL : LONGT;
|
yuuji@0
|
3835 }
|
yuuji@0
|
3836
|
yuuji@0
|
3837
|
yuuji@0
|
3838 /* Mail search keyword
|
yuuji@0
|
3839 * Accepts: MAIL stream
|
yuuji@0
|
3840 * elt to get flags from
|
yuuji@0
|
3841 * keyword list
|
yuuji@0
|
3842 * T for keyword search, NIL for unkeyword search
|
yuuji@0
|
3843 * Returns: T if search found a match
|
yuuji@0
|
3844 */
|
yuuji@0
|
3845
|
yuuji@0
|
3846 long mail_search_keyword (MAILSTREAM *stream,MESSAGECACHE *elt,STRINGLIST *st,
|
yuuji@0
|
3847 long flag)
|
yuuji@0
|
3848 {
|
yuuji@0
|
3849 int i,j;
|
yuuji@0
|
3850 unsigned long f = 0;
|
yuuji@0
|
3851 unsigned long tf;
|
yuuji@0
|
3852 do {
|
yuuji@0
|
3853 for (i = 0; (j = (i < NUSERFLAGS) && stream->user_flags[i]); ++i)
|
yuuji@0
|
3854 if (!compare_csizedtext (stream->user_flags[i],&st->text)) {
|
yuuji@0
|
3855 f |= (1 << i);
|
yuuji@0
|
3856 break;
|
yuuji@0
|
3857 }
|
yuuji@0
|
3858 if (flag && !j) return NIL;
|
yuuji@0
|
3859 } while (st = st->next);
|
yuuji@0
|
3860 tf = elt->user_flags & f; /* get set flags which match */
|
yuuji@0
|
3861 return flag ? (f == tf) : !tf;
|
yuuji@0
|
3862 }
|
yuuji@0
|
3863
|
yuuji@0
|
3864 /* Mail search an address list
|
yuuji@0
|
3865 * Accepts: address list
|
yuuji@0
|
3866 * string list
|
yuuji@0
|
3867 * Returns: T if search found a match
|
yuuji@0
|
3868 */
|
yuuji@0
|
3869
|
yuuji@0
|
3870 #define SEARCHBUFLEN (size_t) 2000
|
yuuji@0
|
3871 #define SEARCHBUFSLOP (size_t) 5
|
yuuji@0
|
3872
|
yuuji@0
|
3873 long mail_search_addr (ADDRESS *adr,STRINGLIST *st)
|
yuuji@0
|
3874 {
|
yuuji@0
|
3875 ADDRESS *a,tadr;
|
yuuji@0
|
3876 SIZEDTEXT txt;
|
yuuji@0
|
3877 char tmp[SENDBUFLEN + 1];
|
yuuji@0
|
3878 size_t i = SEARCHBUFLEN;
|
yuuji@0
|
3879 size_t k;
|
yuuji@0
|
3880 long ret = NIL;
|
yuuji@0
|
3881 if (adr) {
|
yuuji@0
|
3882 txt.data = (unsigned char *) fs_get (i + SEARCHBUFSLOP);
|
yuuji@0
|
3883 /* never an error or next */
|
yuuji@0
|
3884 tadr.error = NIL,tadr.next = NIL;
|
yuuji@0
|
3885 /* write address list */
|
yuuji@0
|
3886 for (txt.size = 0,a = adr; a; a = a->next) {
|
yuuji@0
|
3887 k = (tadr.mailbox = a->mailbox) ? 4 + 2*strlen (a->mailbox) : 3;
|
yuuji@0
|
3888 if (tadr.personal = a->personal) k += 3 + 2*strlen (a->personal);
|
yuuji@0
|
3889 if (tadr.adl = a->adl) k += 3 + 2*strlen (a->adl);
|
yuuji@0
|
3890 if (tadr.host = a->host) k += 3 + 2*strlen (a->host);
|
yuuji@0
|
3891 if (tadr.personal || tadr.adl) k += 2;
|
yuuji@0
|
3892 if (k < (SENDBUFLEN-10)) {/* ignore ridiculous addresses */
|
yuuji@0
|
3893 tmp[0] = '\0';
|
yuuji@0
|
3894 rfc822_write_address (tmp,&tadr);
|
yuuji@0
|
3895 /* resize buffer if necessary */
|
yuuji@0
|
3896 if (((k = strlen (tmp)) + txt.size) > i)
|
yuuji@0
|
3897 fs_resize ((void **) &txt.data,SEARCHBUFSLOP + (i += SEARCHBUFLEN));
|
yuuji@0
|
3898 /* add new address */
|
yuuji@0
|
3899 memcpy (txt.data + txt.size,tmp,k);
|
yuuji@0
|
3900 txt.size += k;
|
yuuji@0
|
3901 /* another address follows */
|
yuuji@0
|
3902 if (a->next) txt.data[txt.size++] = ',';
|
yuuji@0
|
3903 }
|
yuuji@0
|
3904 }
|
yuuji@0
|
3905 txt.data[txt.size] = '\0'; /* tie off string */
|
yuuji@0
|
3906 ret = mail_search_header (&txt,st);
|
yuuji@0
|
3907 fs_give ((void **) &txt.data);
|
yuuji@0
|
3908 }
|
yuuji@0
|
3909 return ret;
|
yuuji@0
|
3910 }
|
yuuji@0
|
3911
|
yuuji@0
|
3912 /* Get string for low-memory searching
|
yuuji@0
|
3913 * Accepts: readin function pointer
|
yuuji@0
|
3914 * stream to use
|
yuuji@0
|
3915 * number of bytes
|
yuuji@0
|
3916 * gets data packet
|
yuuji@0
|
3917
|
yuuji@0
|
3918 * mail stream
|
yuuji@0
|
3919 * message number
|
yuuji@0
|
3920 * descriptor string
|
yuuji@0
|
3921 * option flags
|
yuuji@0
|
3922 * Returns: NIL, always
|
yuuji@0
|
3923 */
|
yuuji@0
|
3924
|
yuuji@0
|
3925 #define SEARCHSLOP 128
|
yuuji@0
|
3926
|
yuuji@0
|
3927 char *mail_search_gets (readfn_t f,void *stream,unsigned long size,
|
yuuji@0
|
3928 GETS_DATA *md)
|
yuuji@0
|
3929 {
|
yuuji@0
|
3930 unsigned long i;
|
yuuji@0
|
3931 char tmp[MAILTMPLEN+SEARCHSLOP+1];
|
yuuji@0
|
3932 SIZEDTEXT st;
|
yuuji@0
|
3933 /* better not be called unless searching */
|
yuuji@0
|
3934 if (!md->stream->private.search.string) {
|
yuuji@0
|
3935 sprintf (tmp,"Search botch, mbx = %.80s, %s = %lu[%.80s]",
|
yuuji@0
|
3936 md->stream->mailbox,
|
yuuji@0
|
3937 (md->flags & FT_UID) ? "UID" : "msg",md->msgno,md->what);
|
yuuji@0
|
3938 fatal (tmp);
|
yuuji@0
|
3939 }
|
yuuji@0
|
3940 /* initially no match for search */
|
yuuji@0
|
3941 md->stream->private.search.result = NIL;
|
yuuji@0
|
3942 /* make sure buffer clear */
|
yuuji@0
|
3943 memset (st.data = (unsigned char *) tmp,'\0',
|
yuuji@0
|
3944 (size_t) MAILTMPLEN+SEARCHSLOP+1);
|
yuuji@0
|
3945 /* read first buffer */
|
yuuji@0
|
3946 (*f) (stream,st.size = i = min (size,(long) MAILTMPLEN),tmp);
|
yuuji@0
|
3947 /* search for text */
|
yuuji@0
|
3948 if (mail_search_string (&st,NIL,&md->stream->private.search.string))
|
yuuji@0
|
3949 md->stream->private.search.result = T;
|
yuuji@0
|
3950 else if (size -= i) { /* more to do, blat slop down */
|
yuuji@0
|
3951 memmove (tmp,tmp+MAILTMPLEN-SEARCHSLOP,(size_t) SEARCHSLOP);
|
yuuji@0
|
3952 do { /* read subsequent buffers one at a time */
|
yuuji@0
|
3953 (*f) (stream,i = min (size,(long) MAILTMPLEN),tmp+SEARCHSLOP);
|
yuuji@0
|
3954 st.size = i + SEARCHSLOP;
|
yuuji@0
|
3955 if (mail_search_string (&st,NIL,&md->stream->private.search.string))
|
yuuji@0
|
3956 md->stream->private.search.result = T;
|
yuuji@0
|
3957 else memmove (tmp,tmp+MAILTMPLEN,(size_t) SEARCHSLOP);
|
yuuji@0
|
3958 }
|
yuuji@0
|
3959 while ((size -= i) && !md->stream->private.search.result);
|
yuuji@0
|
3960 }
|
yuuji@0
|
3961 if (size) { /* toss out everything after that */
|
yuuji@0
|
3962 do (*f) (stream,i = min (size,(long) MAILTMPLEN),tmp);
|
yuuji@0
|
3963 while (size -= i);
|
yuuji@0
|
3964 }
|
yuuji@0
|
3965 return NIL;
|
yuuji@0
|
3966 }
|
yuuji@0
|
3967
|
yuuji@0
|
3968 /* Mail parse search criteria
|
yuuji@0
|
3969 * Accepts: criteria
|
yuuji@0
|
3970 * Returns: search program if parse successful, else NIL
|
yuuji@0
|
3971 */
|
yuuji@0
|
3972
|
yuuji@0
|
3973 SEARCHPGM *mail_criteria (char *criteria)
|
yuuji@0
|
3974 {
|
yuuji@0
|
3975 SEARCHPGM *pgm = NIL;
|
yuuji@0
|
3976 char *criterion,*r,tmp[MAILTMPLEN];
|
yuuji@0
|
3977 int f;
|
yuuji@0
|
3978 if (criteria) { /* only if criteria defined */
|
yuuji@0
|
3979 /* make writeable copy of criteria */
|
yuuji@0
|
3980 criteria = cpystr (criteria);
|
yuuji@0
|
3981 /* for each criterion */
|
yuuji@0
|
3982 for (pgm = mail_newsearchpgm (), criterion = strtok_r (criteria," ",&r);
|
yuuji@0
|
3983 criterion; (criterion = strtok_r (NIL," ",&r))) {
|
yuuji@0
|
3984 f = NIL; /* init then scan the criterion */
|
yuuji@0
|
3985 switch (*ucase (criterion)) {
|
yuuji@0
|
3986 case 'A': /* possible ALL, ANSWERED */
|
yuuji@0
|
3987 if (!strcmp (criterion+1,"LL")) f = T;
|
yuuji@0
|
3988 else if (!strcmp (criterion+1,"NSWERED")) f = pgm->answered = T;
|
yuuji@0
|
3989 break;
|
yuuji@0
|
3990 case 'B': /* possible BCC, BEFORE, BODY */
|
yuuji@0
|
3991 if (!strcmp (criterion+1,"CC"))
|
yuuji@0
|
3992 f = mail_criteria_string (&pgm->bcc,&r);
|
yuuji@0
|
3993 else if (!strcmp (criterion+1,"EFORE"))
|
yuuji@0
|
3994 f = mail_criteria_date (&pgm->before,&r);
|
yuuji@0
|
3995 else if (!strcmp (criterion+1,"ODY"))
|
yuuji@0
|
3996 f = mail_criteria_string (&pgm->body,&r);
|
yuuji@0
|
3997 break;
|
yuuji@0
|
3998 case 'C': /* possible CC */
|
yuuji@0
|
3999 if (!strcmp (criterion+1,"C")) f = mail_criteria_string (&pgm->cc,&r);
|
yuuji@0
|
4000 break;
|
yuuji@0
|
4001 case 'D': /* possible DELETED */
|
yuuji@0
|
4002 if (!strcmp (criterion+1,"ELETED")) f = pgm->deleted = T;
|
yuuji@0
|
4003 break;
|
yuuji@0
|
4004 case 'F': /* possible FLAGGED, FROM */
|
yuuji@0
|
4005 if (!strcmp (criterion+1,"LAGGED")) f = pgm->flagged = T;
|
yuuji@0
|
4006 else if (!strcmp (criterion+1,"ROM"))
|
yuuji@0
|
4007 f = mail_criteria_string (&pgm->from,&r);
|
yuuji@0
|
4008 break;
|
yuuji@0
|
4009 case 'K': /* possible KEYWORD */
|
yuuji@0
|
4010 if (!strcmp (criterion+1,"EYWORD"))
|
yuuji@0
|
4011 f = mail_criteria_string (&pgm->keyword,&r);
|
yuuji@0
|
4012 break;
|
yuuji@0
|
4013
|
yuuji@0
|
4014 case 'N': /* possible NEW */
|
yuuji@0
|
4015 if (!strcmp (criterion+1,"EW")) f = pgm->recent = pgm->unseen = T;
|
yuuji@0
|
4016 break;
|
yuuji@0
|
4017 case 'O': /* possible OLD, ON */
|
yuuji@0
|
4018 if (!strcmp (criterion+1,"LD")) f = pgm->old = T;
|
yuuji@0
|
4019 else if (!strcmp (criterion+1,"N"))
|
yuuji@0
|
4020 f = mail_criteria_date (&pgm->on,&r);
|
yuuji@0
|
4021 break;
|
yuuji@0
|
4022 case 'R': /* possible RECENT */
|
yuuji@0
|
4023 if (!strcmp (criterion+1,"ECENT")) f = pgm->recent = T;
|
yuuji@0
|
4024 break;
|
yuuji@0
|
4025 case 'S': /* possible SEEN, SINCE, SUBJECT */
|
yuuji@0
|
4026 if (!strcmp (criterion+1,"EEN")) f = pgm->seen = T;
|
yuuji@0
|
4027 else if (!strcmp (criterion+1,"INCE"))
|
yuuji@0
|
4028 f = mail_criteria_date (&pgm->since,&r);
|
yuuji@0
|
4029 else if (!strcmp (criterion+1,"UBJECT"))
|
yuuji@0
|
4030 f = mail_criteria_string (&pgm->subject,&r);
|
yuuji@0
|
4031 break;
|
yuuji@0
|
4032 case 'T': /* possible TEXT, TO */
|
yuuji@0
|
4033 if (!strcmp (criterion+1,"EXT"))
|
yuuji@0
|
4034 f = mail_criteria_string (&pgm->text,&r);
|
yuuji@0
|
4035 else if (!strcmp (criterion+1,"O"))
|
yuuji@0
|
4036 f = mail_criteria_string (&pgm->to,&r);
|
yuuji@0
|
4037 break;
|
yuuji@0
|
4038 case 'U': /* possible UN* */
|
yuuji@0
|
4039 if (criterion[1] == 'N') {
|
yuuji@0
|
4040 if (!strcmp (criterion+2,"ANSWERED")) f = pgm->unanswered = T;
|
yuuji@0
|
4041 else if (!strcmp (criterion+2,"DELETED")) f = pgm->undeleted = T;
|
yuuji@0
|
4042 else if (!strcmp (criterion+2,"FLAGGED")) f = pgm->unflagged = T;
|
yuuji@0
|
4043 else if (!strcmp (criterion+2,"KEYWORD"))
|
yuuji@0
|
4044 f = mail_criteria_string (&pgm->unkeyword,&r);
|
yuuji@0
|
4045 else if (!strcmp (criterion+2,"SEEN")) f = pgm->unseen = T;
|
yuuji@0
|
4046 }
|
yuuji@0
|
4047 break;
|
yuuji@0
|
4048 default: /* we will barf below */
|
yuuji@0
|
4049 break;
|
yuuji@0
|
4050 }
|
yuuji@0
|
4051 if (!f) { /* if can't identify criterion */
|
yuuji@0
|
4052 sprintf (tmp,"Unknown search criterion: %.30s",criterion);
|
yuuji@0
|
4053 MM_LOG (tmp,ERROR);
|
yuuji@0
|
4054 mail_free_searchpgm (&pgm);
|
yuuji@0
|
4055 break;
|
yuuji@0
|
4056 }
|
yuuji@0
|
4057 }
|
yuuji@0
|
4058 /* no longer need copy of criteria */
|
yuuji@0
|
4059 fs_give ((void **) &criteria);
|
yuuji@0
|
4060 }
|
yuuji@0
|
4061 return pgm;
|
yuuji@0
|
4062 }
|
yuuji@0
|
4063
|
yuuji@0
|
4064 /* Parse a date
|
yuuji@0
|
4065 * Accepts: pointer to date integer to return
|
yuuji@0
|
4066 * pointer to strtok state
|
yuuji@0
|
4067 * Returns: T if successful, else NIL
|
yuuji@0
|
4068 */
|
yuuji@0
|
4069
|
yuuji@0
|
4070 int mail_criteria_date (unsigned short *date,char **r)
|
yuuji@0
|
4071 {
|
yuuji@0
|
4072 STRINGLIST *s = NIL;
|
yuuji@0
|
4073 MESSAGECACHE elt;
|
yuuji@0
|
4074 /* parse the date and return fn if OK */
|
yuuji@0
|
4075 int ret = (mail_criteria_string (&s,r) &&
|
yuuji@0
|
4076 mail_parse_date (&elt,(char *) s->text.data) &&
|
yuuji@0
|
4077 (*date = mail_shortdate (elt.year,elt.month,elt.day))) ?
|
yuuji@0
|
4078 T : NIL;
|
yuuji@0
|
4079 if (s) mail_free_stringlist (&s);
|
yuuji@0
|
4080 return ret;
|
yuuji@0
|
4081 }
|
yuuji@0
|
4082
|
yuuji@0
|
4083 /* Calculate shortdate from elt values
|
yuuji@0
|
4084 * Accepts: year (0 = BASEYEAR)
|
yuuji@0
|
4085 * month (1 = January)
|
yuuji@0
|
4086 * day
|
yuuji@0
|
4087 * Returns: shortdate
|
yuuji@0
|
4088 */
|
yuuji@0
|
4089
|
yuuji@0
|
4090 unsigned short mail_shortdate (unsigned int year,unsigned int month,
|
yuuji@0
|
4091 unsigned int day)
|
yuuji@0
|
4092 {
|
yuuji@0
|
4093 return (year << 9) + (month << 5) + day;
|
yuuji@0
|
4094 }
|
yuuji@0
|
4095
|
yuuji@0
|
4096 /* Parse a string
|
yuuji@0
|
4097 * Accepts: pointer to stringlist
|
yuuji@0
|
4098 * pointer to strtok state
|
yuuji@0
|
4099 * Returns: T if successful, else NIL
|
yuuji@0
|
4100 */
|
yuuji@0
|
4101
|
yuuji@0
|
4102 int mail_criteria_string (STRINGLIST **s,char **r)
|
yuuji@0
|
4103 {
|
yuuji@0
|
4104 unsigned long n;
|
yuuji@0
|
4105 char e,*d,*end = " ",*c = strtok_r (NIL,"",r);
|
yuuji@0
|
4106 if (!c) return NIL; /* missing argument */
|
yuuji@0
|
4107 switch (*c) { /* see what the argument is */
|
yuuji@0
|
4108 case '{': /* literal string */
|
yuuji@0
|
4109 n = strtoul (c+1,&d,10); /* get its length */
|
yuuji@0
|
4110 if ((*d++ == '}') && (*d++ == '\015') && (*d++ == '\012') &&
|
yuuji@0
|
4111 (!(*(c = d + n)) || (*c == ' '))) {
|
yuuji@0
|
4112 e = *--c; /* store old delimiter */
|
yuuji@0
|
4113 *c = '\377'; /* make sure not a space */
|
yuuji@0
|
4114 strtok_r (c," ",r); /* reset the strtok mechanism */
|
yuuji@0
|
4115 *c = e; /* put character back */
|
yuuji@0
|
4116 break;
|
yuuji@0
|
4117 }
|
yuuji@0
|
4118 case '\0': /* catch bogons */
|
yuuji@0
|
4119 case ' ':
|
yuuji@0
|
4120 return NIL;
|
yuuji@0
|
4121 case '"': /* quoted string */
|
yuuji@0
|
4122 if (strchr (c+1,'"')) end = "\"";
|
yuuji@0
|
4123 else return NIL; /* falls through */
|
yuuji@0
|
4124 default: /* atomic string */
|
yuuji@0
|
4125 if (d = strtok_r (c,end,r)) n = strlen (d);
|
yuuji@0
|
4126 else return NIL;
|
yuuji@0
|
4127 break;
|
yuuji@0
|
4128 }
|
yuuji@0
|
4129 while (*s) s = &(*s)->next; /* find tail of list */
|
yuuji@0
|
4130 *s = mail_newstringlist (); /* make new entry */
|
yuuji@0
|
4131 /* return the data */
|
yuuji@0
|
4132 (*s)->text.data = (unsigned char *) cpystr (d);
|
yuuji@0
|
4133 (*s)->text.size = n;
|
yuuji@0
|
4134 return T;
|
yuuji@0
|
4135 }
|
yuuji@0
|
4136
|
yuuji@0
|
4137 /* Mail parse set from string
|
yuuji@0
|
4138 * Accepts: string to parse
|
yuuji@0
|
4139 * pointer to updated string pointer for return
|
yuuji@0
|
4140 * Returns: set with pointer updated, or NIL if error
|
yuuji@0
|
4141 */
|
yuuji@0
|
4142
|
yuuji@0
|
4143 SEARCHSET *mail_parse_set (char *s,char **ret)
|
yuuji@0
|
4144 {
|
yuuji@0
|
4145 SEARCHSET *cur;
|
yuuji@0
|
4146 SEARCHSET *set = NIL;
|
yuuji@0
|
4147 while (isdigit (*s)) {
|
yuuji@0
|
4148 if (!set) cur = set = mail_newsearchset ();
|
yuuji@0
|
4149 else cur = cur->next = mail_newsearchset ();
|
yuuji@0
|
4150 /* parse value */
|
yuuji@0
|
4151 if (!(cur->first = strtoul (s,&s,10)) ||
|
yuuji@0
|
4152 ((*s == ':') && !(isdigit (*++s) && (cur->last = strtoul (s,&s,10)))))
|
yuuji@0
|
4153 break; /* bad value or range */
|
yuuji@0
|
4154 if (*s == ',') ++s; /* point to next value if more */
|
yuuji@0
|
4155 else { /* end of set */
|
yuuji@0
|
4156 *ret = s; /* set return pointer */
|
yuuji@0
|
4157 return set; /* return set */
|
yuuji@0
|
4158 }
|
yuuji@0
|
4159 }
|
yuuji@0
|
4160 mail_free_searchset (&set); /* failure, punt partial set */
|
yuuji@0
|
4161 return NIL;
|
yuuji@0
|
4162 }
|
yuuji@0
|
4163
|
yuuji@0
|
4164
|
yuuji@0
|
4165 /* Mail append to set
|
yuuji@0
|
4166 * Accepts: head of search set or NIL to do nothing
|
yuuji@0
|
4167 * message to add
|
yuuji@0
|
4168 * Returns: tail of search set or NIL if did nothing
|
yuuji@0
|
4169 */
|
yuuji@0
|
4170
|
yuuji@0
|
4171 SEARCHSET *mail_append_set (SEARCHSET *set,unsigned long msgno)
|
yuuji@0
|
4172 {
|
yuuji@0
|
4173 if (set) { /* find tail */
|
yuuji@0
|
4174 while (set->next) set = set->next;
|
yuuji@0
|
4175 /* start of set if no first member */
|
yuuji@0
|
4176 if (!set->first) set->first = msgno;
|
yuuji@0
|
4177 else if (msgno == (set->last ? set->last : set->first) + 1)
|
yuuji@0
|
4178 set->last = msgno; /* extend range if 1 past current */
|
yuuji@0
|
4179 else (set = set->next = mail_newsearchset ())->first = msgno;
|
yuuji@0
|
4180 }
|
yuuji@0
|
4181 return set;
|
yuuji@0
|
4182 }
|
yuuji@0
|
4183
|
yuuji@0
|
4184 /* Mail sort messages
|
yuuji@0
|
4185 * Accepts: mail stream
|
yuuji@0
|
4186 * character set
|
yuuji@0
|
4187 * search program
|
yuuji@0
|
4188 * sort program
|
yuuji@0
|
4189 * option flags
|
yuuji@0
|
4190 * Returns: vector of sorted message sequences or NIL if error
|
yuuji@0
|
4191 */
|
yuuji@0
|
4192
|
yuuji@0
|
4193 unsigned long *mail_sort (MAILSTREAM *stream,char *charset,SEARCHPGM *spg,
|
yuuji@0
|
4194 SORTPGM *pgm,long flags)
|
yuuji@0
|
4195 {
|
yuuji@0
|
4196 unsigned long *ret = NIL;
|
yuuji@0
|
4197 if (stream->dtb) /* do the driver's action */
|
yuuji@0
|
4198 ret = (*(stream->dtb->sort ? stream->dtb->sort : mail_sort_msgs))
|
yuuji@0
|
4199 (stream,charset,spg,pgm,flags);
|
yuuji@0
|
4200 /* flush search/sort programs if requested */
|
yuuji@0
|
4201 if (spg && (flags & SE_FREE)) mail_free_searchpgm (&spg);
|
yuuji@0
|
4202 if (flags & SO_FREE) mail_free_sortpgm (&pgm);
|
yuuji@0
|
4203 return ret;
|
yuuji@0
|
4204 }
|
yuuji@0
|
4205
|
yuuji@0
|
4206 /* Mail sort messages work routine
|
yuuji@0
|
4207 * Accepts: mail stream
|
yuuji@0
|
4208 * character set
|
yuuji@0
|
4209 * search program
|
yuuji@0
|
4210 * sort program
|
yuuji@0
|
4211 * option flags
|
yuuji@0
|
4212 * Returns: vector of sorted message sequences or NIL if error
|
yuuji@0
|
4213 */
|
yuuji@0
|
4214
|
yuuji@0
|
4215 unsigned long *mail_sort_msgs (MAILSTREAM *stream,char *charset,SEARCHPGM *spg,
|
yuuji@0
|
4216 SORTPGM *pgm,long flags)
|
yuuji@0
|
4217 {
|
yuuji@0
|
4218 unsigned long i;
|
yuuji@0
|
4219 SORTCACHE **sc;
|
yuuji@0
|
4220 unsigned long *ret = NIL;
|
yuuji@0
|
4221 if (spg) { /* only if a search needs to be done */
|
yuuji@0
|
4222 int silent = stream->silent;
|
yuuji@0
|
4223 stream->silent = T; /* don't pass up mm_searched() events */
|
yuuji@0
|
4224 /* search for messages */
|
yuuji@0
|
4225 mail_search_full (stream,charset,spg,NIL);
|
yuuji@0
|
4226 stream->silent = silent; /* restore silence state */
|
yuuji@0
|
4227 }
|
yuuji@0
|
4228 /* initialize progress counters */
|
yuuji@0
|
4229 pgm->nmsgs = pgm->progress.cached = 0;
|
yuuji@0
|
4230 /* pass 1: count messages to sort */
|
yuuji@0
|
4231 for (i = 1; i <= stream->nmsgs; ++i)
|
yuuji@0
|
4232 if (mail_elt (stream,i)->searched) pgm->nmsgs++;
|
yuuji@0
|
4233 if (pgm->nmsgs) { /* pass 2: sort cache */
|
yuuji@0
|
4234 sc = mail_sort_loadcache (stream,pgm);
|
yuuji@0
|
4235 /* pass 3: sort messages */
|
yuuji@0
|
4236 if (!pgm->abort) ret = mail_sort_cache (stream,pgm,sc,flags);
|
yuuji@0
|
4237 fs_give ((void **) &sc); /* don't need sort vector any more */
|
yuuji@0
|
4238 }
|
yuuji@0
|
4239 /* empty sort results */
|
yuuji@0
|
4240 else ret = (unsigned long *) memset (fs_get (sizeof (unsigned long)),0,
|
yuuji@0
|
4241 sizeof (unsigned long));
|
yuuji@0
|
4242 /* also return via callback if requested */
|
yuuji@0
|
4243 if (mailsortresults) (*mailsortresults) (stream,ret,pgm->nmsgs);
|
yuuji@0
|
4244 return ret; /* return sort results */
|
yuuji@0
|
4245 }
|
yuuji@0
|
4246
|
yuuji@0
|
4247 /* Mail sort sortcache vector
|
yuuji@0
|
4248 * Accepts: mail stream
|
yuuji@0
|
4249 * sort program
|
yuuji@0
|
4250 * sortcache vector
|
yuuji@0
|
4251 * option flags
|
yuuji@0
|
4252 * Returns: vector of sorted message sequences or NIL if error
|
yuuji@0
|
4253 */
|
yuuji@0
|
4254
|
yuuji@0
|
4255 unsigned long *mail_sort_cache (MAILSTREAM *stream,SORTPGM *pgm,SORTCACHE **sc,
|
yuuji@0
|
4256 long flags)
|
yuuji@0
|
4257 {
|
yuuji@0
|
4258 unsigned long i,*ret;
|
yuuji@0
|
4259 /* pass 3: sort messages */
|
yuuji@0
|
4260 qsort ((void *) sc,pgm->nmsgs,sizeof (SORTCACHE *),mail_sort_compare);
|
yuuji@0
|
4261 /* optional post sorting */
|
yuuji@0
|
4262 if (pgm->postsort) (*pgm->postsort) ((void *) sc);
|
yuuji@0
|
4263 /* pass 4: return results */
|
yuuji@0
|
4264 ret = (unsigned long *) fs_get ((pgm->nmsgs+1) * sizeof (unsigned long));
|
yuuji@0
|
4265 if (flags & SE_UID) /* UID or msgno? */
|
yuuji@0
|
4266 for (i = 0; i < pgm->nmsgs; i++) ret[i] = mail_uid (stream,sc[i]->num);
|
yuuji@0
|
4267 else for (i = 0; i < pgm->nmsgs; i++) ret[i] = sc[i]->num;
|
yuuji@0
|
4268 ret[pgm->nmsgs] = 0; /* tie off message list */
|
yuuji@0
|
4269 return ret;
|
yuuji@0
|
4270 }
|
yuuji@0
|
4271
|
yuuji@0
|
4272 /* Mail load sortcache
|
yuuji@0
|
4273 * Accepts: mail stream, already searched
|
yuuji@0
|
4274 * sort program
|
yuuji@0
|
4275 * Returns: vector of sortcache pointers matching search
|
yuuji@0
|
4276 */
|
yuuji@0
|
4277
|
yuuji@0
|
4278 static STRINGLIST maildateline = {{(unsigned char *) "date",4},NIL};
|
yuuji@0
|
4279 static STRINGLIST mailrnfromline = {{(unsigned char *) ">from",5},NIL};
|
yuuji@0
|
4280 static STRINGLIST mailfromline = {{(unsigned char *) "from",4},
|
yuuji@0
|
4281 &mailrnfromline};
|
yuuji@0
|
4282 static STRINGLIST mailtonline = {{(unsigned char *) "to",2},NIL};
|
yuuji@0
|
4283 static STRINGLIST mailccline = {{(unsigned char *) "cc",2},NIL};
|
yuuji@0
|
4284 static STRINGLIST mailsubline = {{(unsigned char *) "subject",7},NIL};
|
yuuji@0
|
4285
|
yuuji@0
|
4286 SORTCACHE **mail_sort_loadcache (MAILSTREAM *stream,SORTPGM *pgm)
|
yuuji@0
|
4287 {
|
yuuji@0
|
4288 char *t,*v,*x,tmp[MAILTMPLEN];
|
yuuji@0
|
4289 SORTPGM *pg;
|
yuuji@0
|
4290 SORTCACHE *s,**sc;
|
yuuji@0
|
4291 MESSAGECACHE *elt,telt;
|
yuuji@0
|
4292 ENVELOPE *env;
|
yuuji@0
|
4293 ADDRESS *adr = NIL;
|
yuuji@0
|
4294 unsigned long i = (pgm->nmsgs) * sizeof (SORTCACHE *);
|
yuuji@0
|
4295 sc = (SORTCACHE **) memset (fs_get ((size_t) i),0,(size_t) i);
|
yuuji@0
|
4296 /* see what needs to be loaded */
|
yuuji@0
|
4297 for (i = 1; !pgm->abort && (i <= stream->nmsgs); i++)
|
yuuji@0
|
4298 if ((elt = mail_elt (stream,i))->searched) {
|
yuuji@0
|
4299 sc[pgm->progress.cached++] =
|
yuuji@0
|
4300 s = (SORTCACHE *) (*mailcache) (stream,i,CH_SORTCACHE);
|
yuuji@0
|
4301 s->pgm = pgm; /* note sort program */
|
yuuji@0
|
4302 s->num = i;
|
yuuji@0
|
4303 /* get envelope if cached */
|
yuuji@0
|
4304 if (stream->scache) env = (i == stream->msgno) ? stream->env : NIL;
|
yuuji@0
|
4305 else env = elt->private.msg.env;
|
yuuji@0
|
4306 for (pg = pgm; pg; pg = pg->next) switch (pg->function) {
|
yuuji@0
|
4307 case SORTARRIVAL: /* sort by arrival date */
|
yuuji@0
|
4308 if (!s->arrival) {
|
yuuji@0
|
4309 /* internal date unknown but can get? */
|
yuuji@0
|
4310 if (!elt->day && !(stream->dtb->flags & DR_NOINTDATE)) {
|
yuuji@0
|
4311 sprintf (tmp,"%lu",i);
|
yuuji@0
|
4312 mail_fetch_fast (stream,tmp,NIL);
|
yuuji@0
|
4313 }
|
yuuji@0
|
4314 /* wrong thing before 3-Jan-1970 */
|
yuuji@0
|
4315 s->arrival = elt->day ? mail_longdate (elt) : 1;
|
yuuji@0
|
4316 s->dirty = T;
|
yuuji@0
|
4317 }
|
yuuji@0
|
4318 break;
|
yuuji@0
|
4319 case SORTSIZE: /* sort by message size */
|
yuuji@0
|
4320 if (!s->size) {
|
yuuji@0
|
4321 if (!elt->rfc822_size) {
|
yuuji@0
|
4322 sprintf (tmp,"%lu",i);
|
yuuji@0
|
4323 mail_fetch_fast (stream,tmp,NIL);
|
yuuji@0
|
4324 }
|
yuuji@0
|
4325 s->size = elt->rfc822_size ? elt->rfc822_size : 1;
|
yuuji@0
|
4326 s->dirty = T;
|
yuuji@0
|
4327 }
|
yuuji@0
|
4328 break;
|
yuuji@0
|
4329
|
yuuji@0
|
4330 case SORTDATE: /* sort by date */
|
yuuji@0
|
4331 if (!s->date) {
|
yuuji@0
|
4332 if (env) t = env->date;
|
yuuji@0
|
4333 else if ((t = mail_fetch_header (stream,i,NIL,&maildateline,NIL,
|
yuuji@0
|
4334 FT_INTERNAL | FT_PEEK)) &&
|
yuuji@0
|
4335 (t = strchr (t,':')))
|
yuuji@0
|
4336 for (x = ++t; x = strpbrk (x,"\012\015"); x++)
|
yuuji@0
|
4337 switch (*(v = ((*x == '\015') && (x[1] == '\012')) ? x+2 : x+1)){
|
yuuji@0
|
4338 case ' ': /* erase continuation newlines */
|
yuuji@0
|
4339 case '\t':
|
yuuji@0
|
4340 memmove (x,v,strlen (v));
|
yuuji@0
|
4341 break;
|
yuuji@0
|
4342 default: /* tie off extraneous text */
|
yuuji@0
|
4343 *x = x[1] = '\0';
|
yuuji@0
|
4344 }
|
yuuji@0
|
4345 /* skip leading whitespace */
|
yuuji@0
|
4346 if (t) while ((*t == ' ') || (*t == '\t')) t++;
|
yuuji@0
|
4347 /* parse date from Date: header */
|
yuuji@0
|
4348 if (!(t && mail_parse_date (&telt,t) &&
|
yuuji@0
|
4349 (s->date = mail_longdate (&telt)))) {
|
yuuji@0
|
4350 /* failed, use internal date */
|
yuuji@0
|
4351 if (!(s->date = s->arrival)) {
|
yuuji@0
|
4352 /* internal date unknown but can get? */
|
yuuji@0
|
4353 if (!elt->day && !(stream->dtb->flags & DR_NOINTDATE)) {
|
yuuji@0
|
4354 sprintf (tmp,"%lu",i);
|
yuuji@0
|
4355 mail_fetch_fast (stream,tmp,NIL);
|
yuuji@0
|
4356 }
|
yuuji@0
|
4357 /* wrong thing before 3-Jan-1970 */
|
yuuji@0
|
4358 s->date = (s->arrival = elt->day ? mail_longdate (elt) : 1);
|
yuuji@0
|
4359 }
|
yuuji@0
|
4360 }
|
yuuji@0
|
4361 s->dirty = T;
|
yuuji@0
|
4362 }
|
yuuji@0
|
4363 break;
|
yuuji@0
|
4364
|
yuuji@0
|
4365 case SORTFROM: /* sort by first from */
|
yuuji@0
|
4366 if (!s->from) {
|
yuuji@0
|
4367 if (env) s->from = env->from && env->from->mailbox ?
|
yuuji@0
|
4368 cpystr (env->from->mailbox) : NIL;
|
yuuji@0
|
4369 else if ((t = mail_fetch_header (stream,i,NIL,&mailfromline,NIL,
|
yuuji@0
|
4370 FT_INTERNAL | FT_PEEK)) &&
|
yuuji@0
|
4371 (t = strchr (t,':'))) {
|
yuuji@0
|
4372 for (x = ++t; x = strpbrk (x,"\012\015"); x++)
|
yuuji@0
|
4373 switch (*(v = ((*x == '\015') && (x[1] == '\012')) ? x+2 : x+1)){
|
yuuji@0
|
4374 case ' ': /* erase continuation newlines */
|
yuuji@0
|
4375 case '\t':
|
yuuji@0
|
4376 memmove (x,v,strlen (v));
|
yuuji@0
|
4377 break;
|
yuuji@0
|
4378 case 'f': /* continuation but with extra "From:" */
|
yuuji@0
|
4379 case 'F':
|
yuuji@0
|
4380 if (v = strchr (v,':')) {
|
yuuji@0
|
4381 memmove (x,v+1,strlen (v+1));
|
yuuji@0
|
4382 break;
|
yuuji@0
|
4383 }
|
yuuji@0
|
4384 default: /* tie off extraneous text */
|
yuuji@0
|
4385 *x = x[1] = '\0';
|
yuuji@0
|
4386 }
|
yuuji@0
|
4387 if (adr = rfc822_parse_address (&adr,adr,&t,BADHOST,0)) {
|
yuuji@0
|
4388 s->from = adr->mailbox;
|
yuuji@0
|
4389 adr->mailbox = NIL;
|
yuuji@0
|
4390 mail_free_address (&adr);
|
yuuji@0
|
4391 }
|
yuuji@0
|
4392 }
|
yuuji@0
|
4393 if (!s->from) s->from = cpystr ("");
|
yuuji@0
|
4394 s->dirty = T;
|
yuuji@0
|
4395 }
|
yuuji@0
|
4396 break;
|
yuuji@0
|
4397
|
yuuji@0
|
4398 case SORTTO: /* sort by first to */
|
yuuji@0
|
4399 if (!s->to) {
|
yuuji@0
|
4400 if (env) s->to = env->to && env->to->mailbox ?
|
yuuji@0
|
4401 cpystr (env->to->mailbox) : NIL;
|
yuuji@0
|
4402 else if ((t = mail_fetch_header (stream,i,NIL,&mailtonline,NIL,
|
yuuji@0
|
4403 FT_INTERNAL | FT_PEEK)) &&
|
yuuji@0
|
4404 (t = strchr (t,':'))) {
|
yuuji@0
|
4405 for (x = ++t; x = strpbrk (x,"\012\015"); x++)
|
yuuji@0
|
4406 switch (*(v = ((*x == '\015') && (x[1] == '\012')) ? x+2 : x+1)){
|
yuuji@0
|
4407 case ' ': /* erase continuation newlines */
|
yuuji@0
|
4408 case '\t':
|
yuuji@0
|
4409 memmove (x,v,strlen (v));
|
yuuji@0
|
4410 break;
|
yuuji@0
|
4411 case 't': /* continuation but with extra "To:" */
|
yuuji@0
|
4412 case 'T':
|
yuuji@0
|
4413 if (v = strchr (v,':')) {
|
yuuji@0
|
4414 memmove (x,v+1,strlen (v+1));
|
yuuji@0
|
4415 break;
|
yuuji@0
|
4416 }
|
yuuji@0
|
4417 default: /* tie off extraneous text */
|
yuuji@0
|
4418 *x = x[1] = '\0';
|
yuuji@0
|
4419 }
|
yuuji@0
|
4420 if (adr = rfc822_parse_address (&adr,adr,&t,BADHOST,0)) {
|
yuuji@0
|
4421 s->to = adr->mailbox;
|
yuuji@0
|
4422 adr->mailbox = NIL;
|
yuuji@0
|
4423 mail_free_address (&adr);
|
yuuji@0
|
4424 }
|
yuuji@0
|
4425 }
|
yuuji@0
|
4426 if (!s->to) s->to = cpystr ("");
|
yuuji@0
|
4427 s->dirty = T;
|
yuuji@0
|
4428 }
|
yuuji@0
|
4429 break;
|
yuuji@0
|
4430
|
yuuji@0
|
4431 case SORTCC: /* sort by first cc */
|
yuuji@0
|
4432 if (!s->cc) {
|
yuuji@0
|
4433 if (env) s->cc = env->cc && env->cc->mailbox ?
|
yuuji@0
|
4434 cpystr (env->cc->mailbox) : NIL;
|
yuuji@0
|
4435 else if ((t = mail_fetch_header (stream,i,NIL,&mailccline,NIL,
|
yuuji@0
|
4436 FT_INTERNAL | FT_PEEK)) &&
|
yuuji@0
|
4437 (t = strchr (t,':'))) {
|
yuuji@0
|
4438 for (x = ++t; x = strpbrk (x,"\012\015"); x++)
|
yuuji@0
|
4439 switch (*(v = ((*x == '\015') && (x[1] == '\012')) ? x+2 : x+1)){
|
yuuji@0
|
4440 case ' ': /* erase continuation newlines */
|
yuuji@0
|
4441 case '\t':
|
yuuji@0
|
4442 memmove (x,v,strlen (v));
|
yuuji@0
|
4443 break;
|
yuuji@0
|
4444 case 't': /* continuation but with extra "To:" */
|
yuuji@0
|
4445 case 'T':
|
yuuji@0
|
4446 if (v = strchr (v,':')) {
|
yuuji@0
|
4447 memmove (x,v+1,strlen (v+1));
|
yuuji@0
|
4448 break;
|
yuuji@0
|
4449 }
|
yuuji@0
|
4450 default: /* tie off extraneous text */
|
yuuji@0
|
4451 *x = x[1] = '\0';
|
yuuji@0
|
4452 }
|
yuuji@0
|
4453 if (adr = rfc822_parse_address (&adr,adr,&t,BADHOST,0)) {
|
yuuji@0
|
4454 s->cc = adr->mailbox;
|
yuuji@0
|
4455 adr->mailbox = NIL;
|
yuuji@0
|
4456 mail_free_address (&adr);
|
yuuji@0
|
4457 }
|
yuuji@0
|
4458 }
|
yuuji@0
|
4459 if (!s->cc) s->cc = cpystr ("");
|
yuuji@0
|
4460 s->dirty = T;
|
yuuji@0
|
4461 }
|
yuuji@0
|
4462 break;
|
yuuji@0
|
4463
|
yuuji@0
|
4464 case SORTSUBJECT: /* sort by subject */
|
yuuji@0
|
4465 if (!s->subject) {
|
yuuji@0
|
4466 /* get subject from envelope if have one */
|
yuuji@0
|
4467 if (env) t = env->subject ? env->subject : "";
|
yuuji@0
|
4468 /* otherwise snarf from header text */
|
yuuji@0
|
4469 else if ((t = mail_fetch_header (stream,i,NIL,&mailsubline,
|
yuuji@0
|
4470 NIL,FT_INTERNAL | FT_PEEK)) &&
|
yuuji@0
|
4471 (t = strchr (t,':')))
|
yuuji@0
|
4472 for (x = ++t; x = strpbrk (x,"\012\015"); x++)
|
yuuji@0
|
4473 switch (*(v = ((*x == '\015') && (x[1] == '\012')) ? x+2 : x+1)){
|
yuuji@0
|
4474 case ' ': /* erase continuation newlines */
|
yuuji@0
|
4475 case '\t':
|
yuuji@0
|
4476 memmove (x,v,strlen (v));
|
yuuji@0
|
4477 break;
|
yuuji@0
|
4478 default: /* tie off extraneous text */
|
yuuji@0
|
4479 *x = x[1] = '\0';
|
yuuji@0
|
4480 }
|
yuuji@0
|
4481 else t = ""; /* empty subject */
|
yuuji@0
|
4482 /* strip and cache subject */
|
yuuji@0
|
4483 s->refwd = mail_strip_subject (t,&s->subject);
|
yuuji@0
|
4484 s->dirty = T;
|
yuuji@0
|
4485 }
|
yuuji@0
|
4486 break;
|
yuuji@0
|
4487 default:
|
yuuji@0
|
4488 fatal ("Unknown sort function");
|
yuuji@0
|
4489 }
|
yuuji@0
|
4490 }
|
yuuji@0
|
4491 return sc;
|
yuuji@0
|
4492 }
|
yuuji@0
|
4493
|
yuuji@0
|
4494 /* Strip subjects of extra spaces and leading and trailing cruft for sorting
|
yuuji@0
|
4495 * Accepts: unstripped subject
|
yuuji@0
|
4496 * pointer to return stripped subject, in cpystr form
|
yuuji@0
|
4497 * Returns: T if subject had a re/fwd, NIL otherwise
|
yuuji@0
|
4498 */
|
yuuji@0
|
4499
|
yuuji@0
|
4500 unsigned int mail_strip_subject (char *t,char **ret)
|
yuuji@0
|
4501 {
|
yuuji@0
|
4502 SIZEDTEXT src,dst;
|
yuuji@0
|
4503 unsigned long i,slen;
|
yuuji@0
|
4504 char c,*s,*x;
|
yuuji@0
|
4505 unsigned int refwd = NIL;
|
yuuji@0
|
4506 if (src.size = strlen (t)) { /* have non-empty subject? */
|
yuuji@0
|
4507 src.data = (unsigned char *) t;
|
yuuji@0
|
4508 /* Step 1 */
|
yuuji@0
|
4509 /* make copy, convert MIME2 if needed */
|
yuuji@0
|
4510 *ret = s = (utf8_mime2text (&src,&dst,U8T_CANONICAL) &&
|
yuuji@0
|
4511 (src.data != dst.data)) ? (char *) dst.data : cpystr (t);
|
yuuji@0
|
4512 /* convert spaces to tab, strip extra spaces */
|
yuuji@0
|
4513 for (x = t = s, c = 'x'; *t; t++) {
|
yuuji@0
|
4514 if (c != ' ') c = *x++ = ((*t == '\t') ? ' ' : *t);
|
yuuji@0
|
4515 else if ((*t != '\t') && (*t != ' ')) c = *x++ = *t;
|
yuuji@0
|
4516 }
|
yuuji@0
|
4517 *x = '\0'; /* tie off string */
|
yuuji@0
|
4518 /* Step 2 */
|
yuuji@0
|
4519 for (slen = dst.size; s; slen = strlen (s)) {
|
yuuji@0
|
4520 for (t = s + slen; t > s; ) switch (t[-1]) {
|
yuuji@0
|
4521 case ' ': case '\t': /* WSP */
|
yuuji@0
|
4522 *--t = '\0'; /* just remove it */
|
yuuji@0
|
4523 break;
|
yuuji@0
|
4524 case ')': /* possible "(fwd)" */
|
yuuji@0
|
4525 if ((t >= (s + 5)) && (t[-5] == '(') &&
|
yuuji@0
|
4526 ((t[-4] == 'F') || (t[-4] == 'f')) &&
|
yuuji@0
|
4527 ((t[-3] == 'W') || (t[-3] == 'w')) &&
|
yuuji@0
|
4528 ((t[-2] == 'D') || (t[-2] == 'd'))) {
|
yuuji@0
|
4529 *(t -= 5) = '\0'; /* remove "(fwd)" */
|
yuuji@0
|
4530 refwd = T; /* note a re/fwd */
|
yuuji@0
|
4531 break;
|
yuuji@0
|
4532 }
|
yuuji@0
|
4533 default: /* not a subj-trailer */
|
yuuji@0
|
4534 t = s;
|
yuuji@0
|
4535 break;
|
yuuji@0
|
4536 }
|
yuuji@0
|
4537 /* Steps 3-5 */
|
yuuji@0
|
4538 for (t = s; t; ) switch (*s) {
|
yuuji@0
|
4539 case ' ': case '\t': /* WSP */
|
yuuji@0
|
4540 s = t = mail_strip_subject_wsp (s + 1);
|
yuuji@0
|
4541 break;
|
yuuji@0
|
4542 case 'r': case 'R': /* possible "re" */
|
yuuji@0
|
4543 if (((s[1] == 'E') || (s[1] == 'e')) &&
|
yuuji@0
|
4544 (t = mail_strip_subject_wsp (s + 2)) &&
|
yuuji@0
|
4545 (t = mail_strip_subject_blob (t)) && (*t == ':')) {
|
yuuji@0
|
4546 s = ++t; /* found "re" */
|
yuuji@0
|
4547 refwd = T; /* definitely a re/fwd at this point */
|
yuuji@0
|
4548 }
|
yuuji@0
|
4549 else t = NIL; /* found subj-middle */
|
yuuji@0
|
4550 break;
|
yuuji@0
|
4551 case 'f': case 'F': /* possible "fw" or "fwd" */
|
yuuji@0
|
4552 if (((s[1] == 'w') || (s[1] == 'W')) &&
|
yuuji@0
|
4553 (((s[2] == 'd') || (s[2] == 'D')) ?
|
yuuji@0
|
4554 (t = mail_strip_subject_wsp (s + 3)) :
|
yuuji@0
|
4555 (t = mail_strip_subject_wsp (s + 2))) &&
|
yuuji@0
|
4556 (t = mail_strip_subject_blob (t)) && (*t == ':')) {
|
yuuji@0
|
4557 s = ++t; /* found "fwd" */
|
yuuji@0
|
4558 refwd = T; /* definitely a re/fwd at this point */
|
yuuji@0
|
4559 }
|
yuuji@0
|
4560 else t = NIL; /* found subj-middle */
|
yuuji@0
|
4561 break;
|
yuuji@0
|
4562 case '[': /* possible subj-blob */
|
yuuji@0
|
4563 if ((t = mail_strip_subject_blob (s)) && *t) s = t;
|
yuuji@0
|
4564 else t = NIL; /* found subj-middle */
|
yuuji@0
|
4565 break;
|
yuuji@0
|
4566 default:
|
yuuji@0
|
4567 t = NIL; /* found subj-middle */
|
yuuji@0
|
4568 break;
|
yuuji@0
|
4569 }
|
yuuji@0
|
4570 /* Step 6 */
|
yuuji@0
|
4571 /* Netscape-style "[Fwd: ...]"? */
|
yuuji@0
|
4572 if ((*s == '[') && ((s[1] == 'F') || (s[1] == 'f')) &&
|
yuuji@0
|
4573 ((s[2] == 'W') || (s[2] == 'w')) &&
|
yuuji@0
|
4574 ((s[3] == 'D') || (s[3] == 'd')) && (s[4] == ':') &&
|
yuuji@0
|
4575 (s[i = strlen (s) - 1] == ']')) {
|
yuuji@0
|
4576 s[i] = '\0'; /* flush closing "]" */
|
yuuji@0
|
4577 s += 5; /* and leading "[Fwd:" */
|
yuuji@0
|
4578 refwd = T; /* definitely a re/fwd at this point */
|
yuuji@0
|
4579 }
|
yuuji@0
|
4580 else break; /* don't need to loop back to step 2 */
|
yuuji@0
|
4581 }
|
yuuji@0
|
4582 if (s != (t = *ret)) { /* removed leading text? */
|
yuuji@0
|
4583 s = *ret = cpystr (s); /* yes, make a fresh return copy */
|
yuuji@0
|
4584 fs_give ((void **) &t); /* flush old copy */
|
yuuji@0
|
4585 }
|
yuuji@0
|
4586 }
|
yuuji@0
|
4587 else *ret = cpystr (""); /* empty subject */
|
yuuji@0
|
4588 return refwd; /* return re/fwd state */
|
yuuji@0
|
4589 }
|
yuuji@0
|
4590
|
yuuji@0
|
4591 /* Strip subject wsp helper routine
|
yuuji@0
|
4592 * Accepts: text
|
yuuji@0
|
4593 * Returns: pointer to text after blob
|
yuuji@0
|
4594 */
|
yuuji@0
|
4595
|
yuuji@0
|
4596 char *mail_strip_subject_wsp (char *s)
|
yuuji@0
|
4597 {
|
yuuji@0
|
4598 while ((*s == ' ') || (*s == '\t')) s++;
|
yuuji@0
|
4599 return s;
|
yuuji@0
|
4600 }
|
yuuji@0
|
4601
|
yuuji@0
|
4602
|
yuuji@0
|
4603 /* Strip subject blob helper routine
|
yuuji@0
|
4604 * Accepts: text
|
yuuji@0
|
4605 * Returns: pointer to text after any blob, NIL if blob-like but not blob
|
yuuji@0
|
4606 */
|
yuuji@0
|
4607
|
yuuji@0
|
4608 char *mail_strip_subject_blob (char *s)
|
yuuji@0
|
4609 {
|
yuuji@0
|
4610 if (*s != '[') return s; /* not a blob, ignore */
|
yuuji@0
|
4611 /* search for end of blob */
|
yuuji@0
|
4612 while (*++s != ']') if ((*s == '[') || !*s) return NIL;
|
yuuji@0
|
4613 return mail_strip_subject_wsp (s + 1);
|
yuuji@0
|
4614 }
|
yuuji@0
|
4615
|
yuuji@0
|
4616 /* Sort compare messages
|
yuuji@0
|
4617 * Accept: first message sort cache element
|
yuuji@0
|
4618 * second message sort cache element
|
yuuji@0
|
4619 * Returns: -1 if a1 < a2, 0 if a1 == a2, 1 if a1 > a2
|
yuuji@0
|
4620 */
|
yuuji@0
|
4621
|
yuuji@0
|
4622 int mail_sort_compare (const void *a1,const void *a2)
|
yuuji@0
|
4623 {
|
yuuji@0
|
4624 int i = 0;
|
yuuji@0
|
4625 SORTCACHE *s1 = *(SORTCACHE **) a1;
|
yuuji@0
|
4626 SORTCACHE *s2 = *(SORTCACHE **) a2;
|
yuuji@0
|
4627 SORTPGM *pgm = s1->pgm;
|
yuuji@0
|
4628 if (!s1->sorted) { /* this one sorted yet? */
|
yuuji@0
|
4629 s1->sorted = T;
|
yuuji@0
|
4630 pgm->progress.sorted++; /* another sorted message */
|
yuuji@0
|
4631 }
|
yuuji@0
|
4632 if (!s2->sorted) { /* this one sorted yet? */
|
yuuji@0
|
4633 s2->sorted = T;
|
yuuji@0
|
4634 pgm->progress.sorted++; /* another sorted message */
|
yuuji@0
|
4635 }
|
yuuji@0
|
4636 do {
|
yuuji@0
|
4637 switch (pgm->function) { /* execute search program */
|
yuuji@0
|
4638 case SORTDATE: /* sort by date */
|
yuuji@0
|
4639 i = compare_ulong (s1->date,s2->date);
|
yuuji@0
|
4640 break;
|
yuuji@0
|
4641 case SORTARRIVAL: /* sort by arrival date */
|
yuuji@0
|
4642 i = compare_ulong (s1->arrival,s2->arrival);
|
yuuji@0
|
4643 break;
|
yuuji@0
|
4644 case SORTSIZE: /* sort by message size */
|
yuuji@0
|
4645 i = compare_ulong (s1->size,s2->size);
|
yuuji@0
|
4646 break;
|
yuuji@0
|
4647 case SORTFROM: /* sort by first from */
|
yuuji@0
|
4648 i = compare_cstring (s1->from,s2->from);
|
yuuji@0
|
4649 break;
|
yuuji@0
|
4650 case SORTTO: /* sort by first to */
|
yuuji@0
|
4651 i = compare_cstring (s1->to,s2->to);
|
yuuji@0
|
4652 break;
|
yuuji@0
|
4653 case SORTCC: /* sort by first cc */
|
yuuji@0
|
4654 i = compare_cstring (s1->cc,s2->cc);
|
yuuji@0
|
4655 break;
|
yuuji@0
|
4656 case SORTSUBJECT: /* sort by subject */
|
yuuji@0
|
4657 i = compare_cstring (s1->subject,s2->subject);
|
yuuji@0
|
4658 break;
|
yuuji@0
|
4659 }
|
yuuji@0
|
4660 if (pgm->reverse) i = -i; /* flip results if necessary */
|
yuuji@0
|
4661 }
|
yuuji@0
|
4662 while (pgm = i ? NIL : pgm->next);
|
yuuji@0
|
4663 /* return result, avoid 0 if at all possible */
|
yuuji@0
|
4664 return i ? i : compare_ulong (s1->num,s2->num);
|
yuuji@0
|
4665 }
|
yuuji@0
|
4666
|
yuuji@0
|
4667 /* Return message date as an unsigned long seconds since time began
|
yuuji@0
|
4668 * Accepts: message cache pointer
|
yuuji@0
|
4669 * Returns: unsigned long of date
|
yuuji@0
|
4670 *
|
yuuji@0
|
4671 * This routine, like most UNIX systems, is clueless about leap seconds.
|
yuuji@0
|
4672 * Thus, it treats 23:59:60 as equivalent to 00:00:00 the next day.
|
yuuji@0
|
4673 *
|
yuuji@0
|
4674 * This routine forces any early hours on 1-Jan-1970 in oriental timezones
|
yuuji@0
|
4675 * to be 1-Jan-1970 00:00:00 UTC, so as to avoid negative longdates.
|
yuuji@0
|
4676 */
|
yuuji@0
|
4677
|
yuuji@0
|
4678 unsigned long mail_longdate (MESSAGECACHE *elt)
|
yuuji@0
|
4679 {
|
yuuji@0
|
4680 unsigned long m = elt->month ? elt->month : 1;
|
yuuji@0
|
4681 unsigned long yr = elt->year + BASEYEAR;
|
yuuji@0
|
4682 /* number of days since time began */
|
yuuji@0
|
4683 unsigned long ret = (elt->day ? (elt->day - 1) : 0)
|
yuuji@0
|
4684 + 30 * (m - 1) + ((m + (m > 8)) / 2)
|
yuuji@0
|
4685 #ifndef USEJULIANCALENDAR
|
yuuji@0
|
4686 #ifndef USEORTHODOXCALENDAR /* Gregorian calendar */
|
yuuji@0
|
4687 + ((yr / 400) - (BASEYEAR / 400)) - ((yr / 100) - (BASEYEAR / 100))
|
yuuji@0
|
4688 #ifdef Y4KBUGFIX
|
yuuji@0
|
4689 - ((yr / 4000) - (BASEYEAR / 4000))
|
yuuji@0
|
4690 #endif
|
yuuji@0
|
4691 - ((m < 3) ?
|
yuuji@0
|
4692 !(yr % 4) && ((yr % 100) || (!(yr % 400)
|
yuuji@0
|
4693 #ifdef Y4KBUGFIX
|
yuuji@0
|
4694 && (yr % 4000)
|
yuuji@0
|
4695 #endif
|
yuuji@0
|
4696 )) : 2)
|
yuuji@0
|
4697 #else /* Orthodox calendar */
|
yuuji@0
|
4698 + ((2*(yr / 900)) - (2*(BASEYEAR / 900)))
|
yuuji@0
|
4699 + (((yr % 900) >= 200) - ((BASEYEAR % 900) >= 200))
|
yuuji@0
|
4700 + (((yr % 900) >= 600) - ((BASEYEAR % 900) >= 600))
|
yuuji@0
|
4701 - ((yr / 100) - (BASEYEAR / 100))
|
yuuji@0
|
4702 - ((m < 3) ?
|
yuuji@0
|
4703 !(yr % 4) && ((yr % 100) || ((yr % 900) == 200) || ((yr % 900) == 600))
|
yuuji@0
|
4704 : 2)
|
yuuji@0
|
4705 #endif
|
yuuji@0
|
4706 #endif
|
yuuji@0
|
4707 + elt->year * 365 + (((unsigned long) (elt->year + (BASEYEAR % 4))) / 4);
|
yuuji@0
|
4708 ret *= 24; ret += elt->hours; /* date value in hours */
|
yuuji@0
|
4709 ret *= 60; ret +=elt->minutes;/* date value in minutes */
|
yuuji@0
|
4710 yr = (elt->zhours * 60) + elt->zminutes;
|
yuuji@0
|
4711 if (elt->zoccident) ret += yr;/* occidental timezone, make UTC */
|
yuuji@0
|
4712 else if (ret < yr) return 0; /* still 31-Dec-1969 in UTC */
|
yuuji@0
|
4713 else ret -= yr; /* oriental timezone, make UTC */
|
yuuji@0
|
4714 ret *= 60; ret += elt->seconds;
|
yuuji@0
|
4715 return ret;
|
yuuji@0
|
4716 }
|
yuuji@0
|
4717
|
yuuji@0
|
4718 /* Mail thread messages
|
yuuji@0
|
4719 * Accepts: mail stream
|
yuuji@0
|
4720 * thread type
|
yuuji@0
|
4721 * character set
|
yuuji@0
|
4722 * search program
|
yuuji@0
|
4723 * option flags
|
yuuji@0
|
4724 * Returns: thread node tree or NIL if error
|
yuuji@0
|
4725 */
|
yuuji@0
|
4726
|
yuuji@0
|
4727 THREADNODE *mail_thread (MAILSTREAM *stream,char *type,char *charset,
|
yuuji@0
|
4728 SEARCHPGM *spg,long flags)
|
yuuji@0
|
4729 {
|
yuuji@0
|
4730 THREADNODE *ret = NIL;
|
yuuji@0
|
4731 if (stream->dtb) /* must have a live driver */
|
yuuji@0
|
4732 ret = stream->dtb->thread ? /* do driver's action if available */
|
yuuji@0
|
4733 (*stream->dtb->thread) (stream,type,charset,spg,flags) :
|
yuuji@0
|
4734 mail_thread_msgs (stream,type,charset,spg,flags,mail_sort_msgs);
|
yuuji@0
|
4735 /* flush search/sort programs if requested */
|
yuuji@0
|
4736 if (spg && (flags & SE_FREE)) mail_free_searchpgm (&spg);
|
yuuji@0
|
4737 return ret;
|
yuuji@0
|
4738 }
|
yuuji@0
|
4739
|
yuuji@0
|
4740
|
yuuji@0
|
4741 /* Mail thread messages
|
yuuji@0
|
4742 * Accepts: mail stream
|
yuuji@0
|
4743 * thread type
|
yuuji@0
|
4744 * character set
|
yuuji@0
|
4745 * search program
|
yuuji@0
|
4746 * option flags
|
yuuji@0
|
4747 * sorter routine
|
yuuji@0
|
4748 * Returns: thread node tree or NIL if error
|
yuuji@0
|
4749 */
|
yuuji@0
|
4750
|
yuuji@0
|
4751 THREADNODE *mail_thread_msgs (MAILSTREAM *stream,char *type,char *charset,
|
yuuji@0
|
4752 SEARCHPGM *spg,long flags,sorter_t sorter)
|
yuuji@0
|
4753 {
|
yuuji@0
|
4754 THREADER *t;
|
yuuji@0
|
4755 for (t = &mailthreadlist; t; t = t->next)
|
yuuji@0
|
4756 if (!compare_cstring (type,t->name)) {
|
yuuji@0
|
4757 THREADNODE *ret = (*t->dispatch) (stream,charset,spg,flags,sorter);
|
yuuji@0
|
4758 if (mailthreadresults) (*mailthreadresults) (stream,ret);
|
yuuji@0
|
4759 return ret;
|
yuuji@0
|
4760 }
|
yuuji@0
|
4761 MM_LOG ("No such thread type",ERROR);
|
yuuji@0
|
4762 return NIL;
|
yuuji@0
|
4763 }
|
yuuji@0
|
4764
|
yuuji@0
|
4765 /* Mail thread ordered subject
|
yuuji@0
|
4766 * Accepts: mail stream
|
yuuji@0
|
4767 * character set
|
yuuji@0
|
4768 * search program
|
yuuji@0
|
4769 * option flags
|
yuuji@0
|
4770 * sorter routine
|
yuuji@0
|
4771 * Returns: thread node tree
|
yuuji@0
|
4772 */
|
yuuji@0
|
4773
|
yuuji@0
|
4774 THREADNODE *mail_thread_orderedsubject (MAILSTREAM *stream,char *charset,
|
yuuji@0
|
4775 SEARCHPGM *spg,long flags,
|
yuuji@0
|
4776 sorter_t sorter)
|
yuuji@0
|
4777 {
|
yuuji@0
|
4778 THREADNODE *thr = NIL;
|
yuuji@0
|
4779 THREADNODE *cur,*top,**tc;
|
yuuji@0
|
4780 SORTPGM pgm,pgm2;
|
yuuji@0
|
4781 SORTCACHE *s;
|
yuuji@0
|
4782 unsigned long i,j,*lst,*ls;
|
yuuji@0
|
4783 /* sort by subject+date */
|
yuuji@0
|
4784 memset (&pgm,0,sizeof (SORTPGM));
|
yuuji@0
|
4785 memset (&pgm2,0,sizeof (SORTPGM));
|
yuuji@0
|
4786 pgm.function = SORTSUBJECT;
|
yuuji@0
|
4787 pgm.next = &pgm2;
|
yuuji@0
|
4788 pgm2.function = SORTDATE;
|
yuuji@0
|
4789 if (lst = (*sorter) (stream,charset,spg,&pgm,flags & ~(SE_FREE | SE_UID))){
|
yuuji@0
|
4790 if (*(ls = lst)) { /* create thread */
|
yuuji@0
|
4791 /* note first subject */
|
yuuji@0
|
4792 cur = top = thr = mail_newthreadnode
|
yuuji@0
|
4793 ((SORTCACHE *) (*mailcache) (stream,*ls++,CH_SORTCACHE));
|
yuuji@0
|
4794 /* note its number */
|
yuuji@0
|
4795 cur->num = (flags & SE_UID) ? mail_uid (stream,*lst) : *lst;
|
yuuji@0
|
4796 i = 1; /* number of threads */
|
yuuji@0
|
4797 while (*ls) { /* build tree */
|
yuuji@0
|
4798 /* subjects match? */
|
yuuji@0
|
4799 s = (SORTCACHE *) (*mailcache) (stream,*ls++,CH_SORTCACHE);
|
yuuji@0
|
4800 if (compare_cstring (top->sc->subject,s->subject)) {
|
yuuji@0
|
4801 i++; /* have a new thread */
|
yuuji@0
|
4802 top = top->branch = cur = mail_newthreadnode (s);
|
yuuji@0
|
4803 }
|
yuuji@0
|
4804 /* start a child of the top */
|
yuuji@0
|
4805 else if (cur == top) cur = cur->next = mail_newthreadnode (s);
|
yuuji@0
|
4806 /* sibling of child */
|
yuuji@0
|
4807 else cur = cur->branch = mail_newthreadnode (s);
|
yuuji@0
|
4808 /* set to msgno or UID as needed */
|
yuuji@0
|
4809 cur->num = (flags & SE_UID) ? mail_uid (stream,s->num) : s->num;
|
yuuji@0
|
4810 }
|
yuuji@0
|
4811 /* make threadnode cache */
|
yuuji@0
|
4812 tc = (THREADNODE **) fs_get (i * sizeof (THREADNODE *));
|
yuuji@0
|
4813 /* load threadnode cache */
|
yuuji@0
|
4814 for (j = 0, cur = thr; cur; cur = cur->branch) tc[j++] = cur;
|
yuuji@0
|
4815 if (i != j) fatal ("Threadnode cache confusion");
|
yuuji@0
|
4816 qsort ((void *) tc,i,sizeof (THREADNODE *),mail_thread_compare_date);
|
yuuji@0
|
4817 for (j = 0, --i; j < i; j++) tc[j]->branch = tc[j+1];
|
yuuji@0
|
4818 tc[j]->branch = NIL; /* end of root */
|
yuuji@0
|
4819 thr = tc[0]; /* head of data */
|
yuuji@0
|
4820 fs_give ((void **) &tc);
|
yuuji@0
|
4821 }
|
yuuji@0
|
4822 fs_give ((void **) &lst);
|
yuuji@0
|
4823 }
|
yuuji@0
|
4824 return thr;
|
yuuji@0
|
4825 }
|
yuuji@0
|
4826
|
yuuji@0
|
4827 /* Mail thread references
|
yuuji@0
|
4828 * Accepts: mail stream
|
yuuji@0
|
4829 * character set
|
yuuji@0
|
4830 * search program
|
yuuji@0
|
4831 * option flags
|
yuuji@0
|
4832 * sorter routine
|
yuuji@0
|
4833 * Returns: thread node tree
|
yuuji@0
|
4834 */
|
yuuji@0
|
4835
|
yuuji@0
|
4836 #define REFHASHSIZE 1009 /* arbitrary prime for hash table size */
|
yuuji@0
|
4837
|
yuuji@0
|
4838 /* Reference threading container, as described in Jamie Zawinski's web page
|
yuuji@0
|
4839 * (http://www.jwz.org/doc/threading.html) for this algorithm. These are
|
yuuji@0
|
4840 * stored as extended data in the hash table (called "id_table" in JWZ's
|
yuuji@0
|
4841 * document) and are maintained by the hash table routines. The hash table
|
yuuji@0
|
4842 * routines implement extended data as additional void* words at the end of
|
yuuji@0
|
4843 * each bucket, hence these strange macros instead of a struct which would
|
yuuji@0
|
4844 * have been more straightforward.
|
yuuji@0
|
4845 */
|
yuuji@0
|
4846
|
yuuji@0
|
4847 #define THREADLINKS 3 /* number of thread links */
|
yuuji@0
|
4848
|
yuuji@0
|
4849 #define CACHE(data) ((SORTCACHE *) (data)[0])
|
yuuji@0
|
4850 #define PARENT(data) ((container_t) (data)[1])
|
yuuji@0
|
4851 #define SETPARENT(data,value) ((container_t) (data[1] = value))
|
yuuji@0
|
4852 #define SIBLING(data) ((container_t) (data)[2])
|
yuuji@0
|
4853 #define SETSIBLING(data,value) ((container_t) (data[2] = value))
|
yuuji@0
|
4854 #define CHILD(data) ((container_t) (data)[3])
|
yuuji@0
|
4855 #define SETCHILD(data,value) ((container_t) (data[3] = value))
|
yuuji@0
|
4856
|
yuuji@0
|
4857 THREADNODE *mail_thread_references (MAILSTREAM *stream,char *charset,
|
yuuji@0
|
4858 SEARCHPGM *spg,long flags,sorter_t sorter)
|
yuuji@0
|
4859 {
|
yuuji@0
|
4860 MESSAGECACHE *elt,telt;
|
yuuji@0
|
4861 ENVELOPE *env;
|
yuuji@0
|
4862 SORTCACHE *s;
|
yuuji@0
|
4863 STRINGLIST *st;
|
yuuji@0
|
4864 HASHENT *he;
|
yuuji@0
|
4865 THREADNODE **tc,*cur,*lst,*nxt,*sis,*msg;
|
yuuji@0
|
4866 container_t con,nxc,prc,sib;
|
yuuji@0
|
4867 void **sub;
|
yuuji@0
|
4868 char *t,tmp[MAILTMPLEN];
|
yuuji@0
|
4869 unsigned long j,nmsgs;
|
yuuji@0
|
4870 unsigned long i = stream->nmsgs * sizeof (SORTCACHE *);
|
yuuji@0
|
4871 SORTCACHE **sc = (SORTCACHE **) memset (fs_get ((size_t) i),0,(size_t) i);
|
yuuji@0
|
4872 HASHTAB *ht = hash_create (REFHASHSIZE);
|
yuuji@0
|
4873 THREADNODE *root = NIL;
|
yuuji@0
|
4874 if (spg) { /* only if a search needs to be done */
|
yuuji@0
|
4875 int silent = stream->silent;
|
yuuji@0
|
4876 stream->silent = T; /* don't pass up mm_searched() events */
|
yuuji@0
|
4877 /* search for messages */
|
yuuji@0
|
4878 mail_search_full (stream,charset,spg,NIL);
|
yuuji@0
|
4879 stream->silent = silent; /* restore silence state */
|
yuuji@0
|
4880 }
|
yuuji@0
|
4881
|
yuuji@0
|
4882 /* create SORTCACHE vector of requested msgs */
|
yuuji@0
|
4883 for (i = 1, nmsgs = 0; i <= stream->nmsgs; ++i)
|
yuuji@0
|
4884 if (mail_elt (stream,i)->searched)
|
yuuji@0
|
4885 (sc[nmsgs++] = (SORTCACHE *)(*mailcache)(stream,i,CH_SORTCACHE))->num =i;
|
yuuji@0
|
4886 /* separate pass so can do overview fetch lookahead */
|
yuuji@0
|
4887 for (i = 0; i < nmsgs; ++i) { /* for each requested message */
|
yuuji@0
|
4888 /* is anything missing in its SORTCACHE? */
|
yuuji@0
|
4889 if (!((s = sc[i])->date && s->subject && s->message_id && s->references)) {
|
yuuji@0
|
4890 /* driver has an overview mechanism? */
|
yuuji@0
|
4891 if (stream->dtb && stream->dtb->overview) {
|
yuuji@0
|
4892 /* yes, find following unloaded entries */
|
yuuji@0
|
4893 for (j = i + 1; (j < nmsgs) && !sc[j]->references; ++j);
|
yuuji@0
|
4894 sprintf (tmp,"%lu",mail_uid (stream,s->num));
|
yuuji@0
|
4895 if (i != --j) /* end of range different? */
|
yuuji@0
|
4896 sprintf (tmp + strlen (tmp),":%lu",mail_uid (stream,sc[j]->num));
|
yuuji@0
|
4897 /* load via overview mechanism */
|
yuuji@0
|
4898 mail_fetch_overview (stream,tmp,mail_thread_loadcache);
|
yuuji@0
|
4899 }
|
yuuji@0
|
4900 /* still missing data? */
|
yuuji@0
|
4901 if (!s->date || !s->subject || !s->message_id || !s->references) {
|
yuuji@0
|
4902 /* try to load data from envelope */
|
yuuji@0
|
4903 if (env = mail_fetch_structure (stream,s->num,NIL,NIL)) {
|
yuuji@0
|
4904 if (!s->date && env->date && mail_parse_date (&telt,env->date))
|
yuuji@0
|
4905 s->date = mail_longdate (&telt);
|
yuuji@0
|
4906 if (!s->subject && env->subject)
|
yuuji@0
|
4907 s->refwd =
|
yuuji@0
|
4908 mail_strip_subject (env->subject,&s->subject);
|
yuuji@0
|
4909 if (!s->message_id && env->message_id && *env->message_id)
|
yuuji@0
|
4910 s->message_id = mail_thread_parse_msgid (env->message_id,NIL);
|
yuuji@0
|
4911 if (!s->references && /* use References: or In-Reply-To: */
|
yuuji@0
|
4912 !(s->references =
|
yuuji@0
|
4913 mail_thread_parse_references (env->references,T)))
|
yuuji@0
|
4914 s->references = mail_thread_parse_references(env->in_reply_to,NIL);
|
yuuji@0
|
4915 }
|
yuuji@0
|
4916 /* last resort */
|
yuuji@0
|
4917 if (!s->date && !(s->date = s->arrival)) {
|
yuuji@0
|
4918 /* internal date unknown but can get? */
|
yuuji@0
|
4919 if (!(elt = mail_elt (stream,s->num))->day &&
|
yuuji@0
|
4920 !(stream->dtb->flags & DR_NOINTDATE)) {
|
yuuji@0
|
4921 sprintf (tmp,"%lu",s->num);
|
yuuji@0
|
4922 mail_fetch_fast (stream,tmp,NIL);
|
yuuji@0
|
4923 }
|
yuuji@0
|
4924 /* wrong thing before 3-Jan-1970 */
|
yuuji@0
|
4925 s->date = (s->arrival = elt->day ? mail_longdate (elt) : 1);
|
yuuji@0
|
4926 }
|
yuuji@0
|
4927 if (!s->subject) s->subject = cpystr ("");
|
yuuji@0
|
4928 if (!s->references) s->references = mail_newstringlist ();
|
yuuji@0
|
4929 s->dirty = T;
|
yuuji@0
|
4930 }
|
yuuji@0
|
4931 }
|
yuuji@0
|
4932
|
yuuji@0
|
4933 /* Step 1 (preliminary) */
|
yuuji@0
|
4934 /* generate unique string */
|
yuuji@0
|
4935 sprintf (tmp,"%s.%lx.%lx@%s",stream->mailbox,stream->uid_validity,
|
yuuji@0
|
4936 mail_uid (stream,s->num),mylocalhost ());
|
yuuji@0
|
4937 /* flush old unique string if not message-id */
|
yuuji@0
|
4938 if (s->unique && (s->unique != s->message_id))
|
yuuji@0
|
4939 fs_give ((void **) &s->unique);
|
yuuji@0
|
4940 s->unique = s->message_id ? /* don't permit Message ID duplicates */
|
yuuji@0
|
4941 (hash_lookup (ht,s->message_id) ? cpystr (tmp) : s->message_id) :
|
yuuji@0
|
4942 (s->message_id = cpystr (tmp));
|
yuuji@0
|
4943 /* add unique string to hash table */
|
yuuji@0
|
4944 hash_add (ht,s->unique,s,THREADLINKS);
|
yuuji@0
|
4945 }
|
yuuji@0
|
4946 /* Step 1 */
|
yuuji@0
|
4947 for (i = 0; i < nmsgs; ++i) { /* for each message in sortcache */
|
yuuji@0
|
4948 /* Step 1A */
|
yuuji@0
|
4949 if ((st = (s = sc[i])->references) && st->text.data)
|
yuuji@0
|
4950 for (con = hash_lookup_and_add (ht,(char *) st->text.data,NIL,
|
yuuji@0
|
4951 THREADLINKS); st = st->next; con = nxc) {
|
yuuji@0
|
4952 nxc = hash_lookup_and_add (ht,(char *) st->text.data,NIL,THREADLINKS);
|
yuuji@0
|
4953 /* only if no parent & won't introduce loop */
|
yuuji@0
|
4954 if (!PARENT (nxc) && !mail_thread_check_child (con,nxc)) {
|
yuuji@0
|
4955 SETPARENT (nxc,con); /* establish parent/child link */
|
yuuji@0
|
4956 /* other children become sibling of this one */
|
yuuji@0
|
4957 SETSIBLING (nxc,CHILD (con));
|
yuuji@0
|
4958 SETCHILD (con,nxc); /* set as child of parent */
|
yuuji@0
|
4959 }
|
yuuji@0
|
4960 }
|
yuuji@0
|
4961 else con = NIL; /* else message has no ancestors */
|
yuuji@0
|
4962 /* Step 1B */
|
yuuji@0
|
4963 if ((prc = PARENT ((nxc = hash_lookup (ht,s->unique)))) &&
|
yuuji@0
|
4964 (prc != con)) { /* break links if have a different parent */
|
yuuji@0
|
4965 SETPARENT (nxc,NIL); /* easy if direct child */
|
yuuji@0
|
4966 if (nxc == CHILD (prc)) SETCHILD (prc,SIBLING (nxc));
|
yuuji@0
|
4967 else { /* otherwise hunt through sisters */
|
yuuji@0
|
4968 for (sib = CHILD (prc); nxc != SIBLING (sib); sib = SIBLING (sib));
|
yuuji@0
|
4969 SETSIBLING (sib,SIBLING (nxc));
|
yuuji@0
|
4970 }
|
yuuji@0
|
4971 SETSIBLING (nxc,NIL); /* no more little sisters either */
|
yuuji@0
|
4972 prc = NIL; /* no more parent set */
|
yuuji@0
|
4973 }
|
yuuji@0
|
4974 /* need to set parent, and parent is good? */
|
yuuji@0
|
4975 if (!prc && !mail_thread_check_child (con,nxc)) {
|
yuuji@0
|
4976 SETPARENT (nxc,con); /* establish parent/child link */
|
yuuji@0
|
4977 if (con) { /* if non-root parent, set parent's child */
|
yuuji@0
|
4978 if (CHILD (con)) { /* have a child already */
|
yuuji@0
|
4979 /* find youngest daughter */
|
yuuji@0
|
4980 for (con = CHILD (con); SIBLING (con); con = SIBLING (con));
|
yuuji@0
|
4981 SETSIBLING (con,nxc); /* add new baby sister */
|
yuuji@0
|
4982 }
|
yuuji@0
|
4983 else SETCHILD (con,nxc);/* set as only child */
|
yuuji@0
|
4984 }
|
yuuji@0
|
4985 }
|
yuuji@0
|
4986 }
|
yuuji@0
|
4987 fs_give ((void **) &sc); /* finished with sortcache vector */
|
yuuji@0
|
4988
|
yuuji@0
|
4989 /* Step 2 */
|
yuuji@0
|
4990 /* search hash table for parentless messages */
|
yuuji@0
|
4991 for (i = 0, prc = con = NIL; i < ht->size; i++)
|
yuuji@0
|
4992 for (he = ht->table[i]; he; he = he->next)
|
yuuji@0
|
4993 if (!PARENT ((nxc = he->data))) {
|
yuuji@0
|
4994 /* sibling of previous parentless message */
|
yuuji@0
|
4995 if (con) con = SETSIBLING (con,nxc);
|
yuuji@0
|
4996 else prc = con = nxc; /* first parentless message */
|
yuuji@0
|
4997 }
|
yuuji@0
|
4998 /* Once the dummy containers are pruned, we no longer need the parent
|
yuuji@0
|
4999 * information, so we can convert the containers to THREADNODEs. Since
|
yuuji@0
|
5000 * we don't need the id_table any more either, we can reset the hash table
|
yuuji@0
|
5001 * and reuse it as a subject_table. Resetting the hash table will also
|
yuuji@0
|
5002 * destroy the containers.
|
yuuji@0
|
5003 */
|
yuuji@0
|
5004 /* Step 3 */
|
yuuji@0
|
5005 /* prune dummies, convert to threadnode */
|
yuuji@0
|
5006 root = mail_thread_c2node (stream,mail_thread_prune_dummy (prc,NIL),flags);
|
yuuji@0
|
5007 /* Step 4 */
|
yuuji@0
|
5008 /* make buffer for sorting */
|
yuuji@0
|
5009 tc = (THREADNODE **) fs_get (nmsgs * sizeof (THREADNODE *));
|
yuuji@0
|
5010 /* load threadcache and count nodes to sort */
|
yuuji@0
|
5011 for (i = 0, cur = root; cur ; cur = cur->branch) tc[i++] = cur;
|
yuuji@0
|
5012 if (i > 1) { /* only if need to sort */
|
yuuji@0
|
5013 qsort ((void *) tc,i,sizeof (THREADNODE *),mail_thread_compare_date);
|
yuuji@0
|
5014 /* relink siblings */
|
yuuji@0
|
5015 for (j = 0, --i; j < i; j++) tc[j]->branch = tc[j+1];
|
yuuji@0
|
5016 tc[j]->branch = NIL; /* end of root */
|
yuuji@0
|
5017 root = tc[0]; /* establish new root */
|
yuuji@0
|
5018 }
|
yuuji@0
|
5019 /* Step 5A */
|
yuuji@0
|
5020 hash_reset (ht); /* discard containers, reset ht */
|
yuuji@0
|
5021 /* Step 5B */
|
yuuji@0
|
5022 for (cur = root; cur; cur = cur->branch)
|
yuuji@0
|
5023 if ((t = (nxt = (cur->sc ? cur : cur->next))->sc->subject) && *t) {
|
yuuji@0
|
5024 /* add new subject to hash table */
|
yuuji@0
|
5025 if (!(sub = hash_lookup (ht,t))) hash_add (ht,t,cur,0);
|
yuuji@0
|
5026 /* if one in table not dummy and */
|
yuuji@0
|
5027 else if ((s = (lst = (THREADNODE *) sub[0])->sc) &&
|
yuuji@0
|
5028 /* current dummy, or not re/fwd and table is */
|
yuuji@0
|
5029 (!cur->sc || (!nxt->sc->refwd && s->refwd)))
|
yuuji@0
|
5030 sub[0] = (void *) cur; /* replace with this message */
|
yuuji@0
|
5031 }
|
yuuji@0
|
5032
|
yuuji@0
|
5033 /* Step 5C */
|
yuuji@0
|
5034 for (cur = root, sis = NIL; cur; cur = msg) {
|
yuuji@0
|
5035 /* do nothing if current message or no sub */
|
yuuji@0
|
5036 if (!(t = (cur->sc ? cur : cur->next)->sc->subject) || !*t ||
|
yuuji@0
|
5037 ((lst = (THREADNODE *) (sub = hash_lookup (ht,t))[0]) == cur))
|
yuuji@0
|
5038 msg = (sis = cur)->branch;
|
yuuji@0
|
5039 else if (!lst->sc) { /* is message in the table a dummy? */
|
yuuji@0
|
5040 /* find youngest daughter of msg in table */
|
yuuji@0
|
5041 for (msg = lst->next; msg->branch; msg = msg->branch);
|
yuuji@0
|
5042 if (!cur->sc) { /* current message a dummy? */
|
yuuji@0
|
5043 msg->branch = cur->next;/* current's daughter now dummy's youngest */
|
yuuji@0
|
5044 msg = cur->branch; /* continue scan at younger sister */
|
yuuji@0
|
5045 /* now delete this node */
|
yuuji@0
|
5046 cur->branch = cur->next = NIL;
|
yuuji@0
|
5047 mail_free_threadnode (&cur);
|
yuuji@0
|
5048 }
|
yuuji@0
|
5049 else { /* current message not a dummy */
|
yuuji@0
|
5050 msg->branch = cur; /* append as youngest daughter */
|
yuuji@0
|
5051 msg = cur->branch; /* continue scan at younger sister */
|
yuuji@0
|
5052 cur->branch = NIL; /* lose our younger sisters */
|
yuuji@0
|
5053 }
|
yuuji@0
|
5054 }
|
yuuji@0
|
5055 else { /* no dummies, is current re/fwd, table not? */
|
yuuji@0
|
5056 if (cur->sc->refwd && !lst->sc->refwd) {
|
yuuji@0
|
5057 if (lst->next) { /* find youngest daughter of msg in table */
|
yuuji@0
|
5058 for (msg = lst->next; msg->branch; msg = msg->branch);
|
yuuji@0
|
5059 msg->branch = cur; /* append as youngest daughter */
|
yuuji@0
|
5060 }
|
yuuji@0
|
5061 else lst->next = cur; /* no children, so make the eldest daughter */
|
yuuji@0
|
5062 }
|
yuuji@0
|
5063
|
yuuji@0
|
5064 else { /* no re/fwd, create a new dummy */
|
yuuji@0
|
5065 msg = mail_newthreadnode (NIL);
|
yuuji@0
|
5066 if (lst == root) { /* msg in table is root? */
|
yuuji@0
|
5067 root = lst->branch; /* younger sister becomes new root */
|
yuuji@0
|
5068 /* no longer older sister either */
|
yuuji@0
|
5069 if (lst == sis) sis = NIL;
|
yuuji@0
|
5070 }
|
yuuji@0
|
5071 else { /* find older sister of msg in table */
|
yuuji@0
|
5072 for (nxt = root; lst != nxt->branch; nxt = nxt->branch);
|
yuuji@0
|
5073 /* remove from older sister */
|
yuuji@0
|
5074 nxt->branch = lst->branch;
|
yuuji@0
|
5075 }
|
yuuji@0
|
5076 msg->next = lst; /* msg in table becomes child */
|
yuuji@0
|
5077 lst->branch = cur; /* current now little sister of msg in table */
|
yuuji@0
|
5078 if (sis) { /* have an elder sister? */
|
yuuji@0
|
5079 if (sis == lst) /* rescan if lost her */
|
yuuji@0
|
5080 for (sis = root; cur != sis->branch; sis = sis->branch);
|
yuuji@0
|
5081 sis->branch = msg; /* make dummy younger sister of big sister */
|
yuuji@0
|
5082 }
|
yuuji@0
|
5083 else root = msg; /* otherwise this is the new root */
|
yuuji@0
|
5084 sub[0] = sis = msg; /* set new msg in table and new big sister */
|
yuuji@0
|
5085 }
|
yuuji@0
|
5086 msg = cur->branch; /* continue scan at younger sister */
|
yuuji@0
|
5087 cur->branch = NIL; /* lose our younger sisters */
|
yuuji@0
|
5088 }
|
yuuji@0
|
5089 if (sis) sis->branch = msg; /* older sister gets this as younger sister */
|
yuuji@0
|
5090 else root = msg; /* otherwise this is the new root */
|
yuuji@0
|
5091 }
|
yuuji@0
|
5092 hash_destroy (&ht); /* finished with hash table */
|
yuuji@0
|
5093 /* Step 6 */
|
yuuji@0
|
5094 /* sort threads */
|
yuuji@0
|
5095 root = mail_thread_sort (root,tc);
|
yuuji@0
|
5096 fs_give ((void **) &tc); /* finished with sort buffer */
|
yuuji@0
|
5097 return root; /* return sorted list */
|
yuuji@0
|
5098 }
|
yuuji@0
|
5099
|
yuuji@0
|
5100 /* Fetch overview callback to load sortcache for threading
|
yuuji@0
|
5101 * Accepts: MAIL stream
|
yuuji@0
|
5102 * UID of this message
|
yuuji@0
|
5103 * overview of this message
|
yuuji@0
|
5104 * msgno of this message
|
yuuji@0
|
5105 */
|
yuuji@0
|
5106
|
yuuji@0
|
5107 void mail_thread_loadcache (MAILSTREAM *stream,unsigned long uid,OVERVIEW *ov,
|
yuuji@0
|
5108 unsigned long msgno)
|
yuuji@0
|
5109 {
|
yuuji@0
|
5110 if (msgno && ov) { /* just in case */
|
yuuji@0
|
5111 MESSAGECACHE telt;
|
yuuji@0
|
5112 SORTCACHE *s = (SORTCACHE *) (*mailcache) (stream,msgno,CH_SORTCACHE);
|
yuuji@0
|
5113 if (!s->subject && ov->subject) {
|
yuuji@0
|
5114 s->refwd = mail_strip_subject (ov->subject,&s->subject);
|
yuuji@0
|
5115 s->dirty = T;
|
yuuji@0
|
5116 }
|
yuuji@0
|
5117 if (!s->from && ov->from && ov->from->mailbox) {
|
yuuji@0
|
5118 s->from = cpystr (ov->from->mailbox);
|
yuuji@0
|
5119 s->dirty = T;
|
yuuji@0
|
5120 }
|
yuuji@0
|
5121 if (!s->date && ov->date && mail_parse_date (&telt,ov->date)) {
|
yuuji@0
|
5122 s->date = mail_longdate (&telt);
|
yuuji@0
|
5123 s->dirty = T;
|
yuuji@0
|
5124 }
|
yuuji@0
|
5125 if (!s->message_id && ov->message_id) {
|
yuuji@0
|
5126 s->message_id = mail_thread_parse_msgid (ov->message_id,NIL);
|
yuuji@0
|
5127 s->dirty = T;
|
yuuji@0
|
5128 }
|
yuuji@0
|
5129 if (!s->references &&
|
yuuji@0
|
5130 !(s->references = mail_thread_parse_references (ov->references,T))) {
|
yuuji@0
|
5131 /* don't do In-Reply-To with NNTP mailboxes */
|
yuuji@0
|
5132 s->references = mail_newstringlist ();
|
yuuji@0
|
5133 s->dirty = T;
|
yuuji@0
|
5134 }
|
yuuji@0
|
5135 if (!s->size && ov->optional.octets) {
|
yuuji@0
|
5136 s->size = ov->optional.octets;
|
yuuji@0
|
5137 s->dirty = T;
|
yuuji@0
|
5138 }
|
yuuji@0
|
5139 }
|
yuuji@0
|
5140 }
|
yuuji@0
|
5141
|
yuuji@0
|
5142 /* Thread parse Message ID
|
yuuji@0
|
5143 * Accepts: pointer to purported Message ID
|
yuuji@0
|
5144 * pointer to return pointer
|
yuuji@0
|
5145 * Returns: Message ID or NIL, return pointer updated
|
yuuji@0
|
5146 */
|
yuuji@0
|
5147
|
yuuji@0
|
5148 char *mail_thread_parse_msgid (char *s,char **ss)
|
yuuji@0
|
5149 {
|
yuuji@0
|
5150 char *ret = NIL;
|
yuuji@0
|
5151 char *t = NIL;
|
yuuji@0
|
5152 ADDRESS *adr;
|
yuuji@0
|
5153 if (s) { /* only for non-NIL strings */
|
yuuji@0
|
5154 rfc822_skipws (&s); /* skip whitespace */
|
yuuji@0
|
5155 /* ignore phrases */
|
yuuji@0
|
5156 if (((*s == '<') || (s = rfc822_parse_phrase (s))) &&
|
yuuji@0
|
5157 (adr = rfc822_parse_routeaddr (s,&t,BADHOST))) {
|
yuuji@0
|
5158 /* make return msgid */
|
yuuji@0
|
5159 if (adr->mailbox && adr->host)
|
yuuji@0
|
5160 sprintf (ret = (char *) fs_get (strlen (adr->mailbox) +
|
yuuji@0
|
5161 strlen (adr->host) + 2),"%s@%s",
|
yuuji@0
|
5162 adr->mailbox,adr->host);
|
yuuji@0
|
5163 mail_free_address (&adr); /* don't need temporary address */
|
yuuji@0
|
5164 }
|
yuuji@0
|
5165 }
|
yuuji@0
|
5166 if (ss) *ss = t; /* update return pointer */
|
yuuji@0
|
5167 return ret;
|
yuuji@0
|
5168 }
|
yuuji@0
|
5169
|
yuuji@0
|
5170
|
yuuji@0
|
5171 /* Thread parse references
|
yuuji@0
|
5172 * Accepts: pointer to purported references
|
yuuji@0
|
5173 * parse multiple references flag
|
yuuji@0
|
5174 * Returns: references or NIL
|
yuuji@0
|
5175 */
|
yuuji@0
|
5176
|
yuuji@0
|
5177 STRINGLIST *mail_thread_parse_references (char *s,long flag)
|
yuuji@0
|
5178 {
|
yuuji@0
|
5179 char *t;
|
yuuji@0
|
5180 STRINGLIST *ret = NIL;
|
yuuji@0
|
5181 STRINGLIST *cur;
|
yuuji@0
|
5182 /* found first reference? */
|
yuuji@0
|
5183 if (t = mail_thread_parse_msgid (s,&s)) {
|
yuuji@0
|
5184 (ret = mail_newstringlist ())->text.data = (unsigned char *) t;
|
yuuji@0
|
5185 ret->text.size = strlen (t);
|
yuuji@0
|
5186 if (flag) /* parse subsequent references */
|
yuuji@0
|
5187 for (cur = ret; t = mail_thread_parse_msgid (s,&s); cur = cur->next) {
|
yuuji@0
|
5188 (cur->next = mail_newstringlist ())->text.data = (unsigned char *) t;
|
yuuji@0
|
5189 cur->next->text.size = strlen (t);
|
yuuji@0
|
5190 }
|
yuuji@0
|
5191 }
|
yuuji@0
|
5192 return ret;
|
yuuji@0
|
5193 }
|
yuuji@0
|
5194
|
yuuji@0
|
5195 /* Prune dummy messages
|
yuuji@0
|
5196 * Accepts: candidate container to prune
|
yuuji@0
|
5197 * older sibling of container, if any
|
yuuji@0
|
5198 * Returns: container in this position, possibly pruned
|
yuuji@0
|
5199 * All children and younger siblings are also pruned
|
yuuji@0
|
5200 */
|
yuuji@0
|
5201
|
yuuji@0
|
5202 container_t mail_thread_prune_dummy (container_t msg,container_t ane)
|
yuuji@0
|
5203 {
|
yuuji@0
|
5204 /* prune container and children */
|
yuuji@0
|
5205 container_t ret = msg ? mail_thread_prune_dummy_work (msg,ane) : NIL;
|
yuuji@0
|
5206 /* prune all younger sisters */
|
yuuji@0
|
5207 if (ret) for (ane = ret; ane && (msg = SIBLING (ane)); ane = msg)
|
yuuji@0
|
5208 msg = mail_thread_prune_dummy_work (msg,ane);
|
yuuji@0
|
5209 return ret;
|
yuuji@0
|
5210 }
|
yuuji@0
|
5211
|
yuuji@0
|
5212
|
yuuji@0
|
5213 /* Prune dummy messages worker routine
|
yuuji@0
|
5214 * Accepts: candidate container to prune
|
yuuji@0
|
5215 * older sibling of container, if any
|
yuuji@0
|
5216 * Returns: container in this position, possibly pruned
|
yuuji@0
|
5217 * All children are also pruned
|
yuuji@0
|
5218 */
|
yuuji@0
|
5219
|
yuuji@0
|
5220 container_t mail_thread_prune_dummy_work (container_t msg,container_t ane)
|
yuuji@0
|
5221 {
|
yuuji@0
|
5222 container_t cur;
|
yuuji@0
|
5223 /* get children, if any */
|
yuuji@0
|
5224 container_t nxt = mail_thread_prune_dummy (CHILD (msg),NIL);
|
yuuji@0
|
5225 /* just update children if container has msg */
|
yuuji@0
|
5226 if (CACHE (msg)) SETCHILD (msg,nxt);
|
yuuji@0
|
5227 else if (!nxt) { /* delete dummy with no children */
|
yuuji@0
|
5228 nxt = SIBLING (msg); /* get younger sister */
|
yuuji@0
|
5229 if (ane) SETSIBLING (ane,nxt);
|
yuuji@0
|
5230 /* prune younger sister if exists */
|
yuuji@0
|
5231 msg = nxt ? mail_thread_prune_dummy_work (nxt,ane) : NIL;
|
yuuji@0
|
5232 }
|
yuuji@0
|
5233 /* not if parent root & multiple children */
|
yuuji@0
|
5234 else if ((cur = PARENT (msg)) || !SIBLING (nxt)) {
|
yuuji@0
|
5235 /* OK to promote, try younger sister of aunt */
|
yuuji@0
|
5236 if (ane) SETSIBLING (ane,nxt);
|
yuuji@0
|
5237 /* otherwise promote to child of grandmother */
|
yuuji@0
|
5238 else if (cur) SETCHILD (cur,nxt);
|
yuuji@0
|
5239 SETPARENT (nxt,cur); /* set parent as well */
|
yuuji@0
|
5240 /* look for end of siblings in new container */
|
yuuji@0
|
5241 for (cur = nxt; SIBLING (cur); cur = SIBLING (cur));
|
yuuji@0
|
5242 /* reattach deleted container's siblings */
|
yuuji@0
|
5243 SETSIBLING (cur,SIBLING (msg));
|
yuuji@0
|
5244 /* prune and return new container */
|
yuuji@0
|
5245 msg = mail_thread_prune_dummy_work (nxt,ane);
|
yuuji@0
|
5246 }
|
yuuji@0
|
5247 else SETCHILD (msg,nxt); /* in case child pruned */
|
yuuji@0
|
5248 return msg; /* return this message */
|
yuuji@0
|
5249 }
|
yuuji@0
|
5250
|
yuuji@0
|
5251 /* Test that purported mother is not a child of purported daughter
|
yuuji@0
|
5252 * Accepts: mother
|
yuuji@0
|
5253 * purported daugher
|
yuuji@0
|
5254 * Returns: T if circular parentage exists, else NIL
|
yuuji@0
|
5255 */
|
yuuji@0
|
5256
|
yuuji@0
|
5257 long mail_thread_check_child (container_t mother,container_t daughter)
|
yuuji@0
|
5258 {
|
yuuji@0
|
5259 if (mother) { /* only if mother non-NIL */
|
yuuji@0
|
5260 if (mother == daughter) return T;
|
yuuji@0
|
5261 for (daughter = CHILD (daughter); daughter; daughter = SIBLING (daughter))
|
yuuji@0
|
5262 if (mail_thread_check_child (mother,daughter)) return T;
|
yuuji@0
|
5263 }
|
yuuji@0
|
5264 return NIL;
|
yuuji@0
|
5265 }
|
yuuji@0
|
5266
|
yuuji@0
|
5267
|
yuuji@0
|
5268 /* Generate threadnodes from containers
|
yuuji@0
|
5269 * Accepts: Mail stream
|
yuuji@0
|
5270 * container
|
yuuji@0
|
5271 * flags
|
yuuji@0
|
5272 * Return: threadnode list
|
yuuji@0
|
5273 */
|
yuuji@0
|
5274
|
yuuji@0
|
5275 THREADNODE *mail_thread_c2node (MAILSTREAM *stream,container_t con,long flags)
|
yuuji@0
|
5276 {
|
yuuji@0
|
5277 THREADNODE *ret,*cur;
|
yuuji@0
|
5278 SORTCACHE *s;
|
yuuji@0
|
5279 container_t nxt;
|
yuuji@0
|
5280 /* for each container */
|
yuuji@0
|
5281 for (ret = cur = NIL; con; con = SIBLING (con)) {
|
yuuji@0
|
5282 s = CACHE (con); /* yes, get its sortcache */
|
yuuji@0
|
5283 /* create node for it */
|
yuuji@0
|
5284 if (ret) cur = cur->branch = mail_newthreadnode (s);
|
yuuji@0
|
5285 else ret = cur = mail_newthreadnode (s);
|
yuuji@0
|
5286 /* attach sequence or UID for non-dummy */
|
yuuji@0
|
5287 if (s) cur->num = (flags & SE_UID) ? mail_uid (stream,s->num) : s->num;
|
yuuji@0
|
5288 /* attach the children */
|
yuuji@0
|
5289 if (nxt = CHILD (con)) cur->next = mail_thread_c2node (stream,nxt,flags);
|
yuuji@0
|
5290 }
|
yuuji@0
|
5291 return ret;
|
yuuji@0
|
5292 }
|
yuuji@0
|
5293
|
yuuji@0
|
5294 /* Sort thread tree by date
|
yuuji@0
|
5295 * Accepts: thread tree to sort
|
yuuji@0
|
5296 * qsort vector to sort
|
yuuji@0
|
5297 * Returns: sorted thread tree
|
yuuji@0
|
5298 */
|
yuuji@0
|
5299
|
yuuji@0
|
5300 THREADNODE *mail_thread_sort (THREADNODE *thr,THREADNODE **tc)
|
yuuji@0
|
5301 {
|
yuuji@0
|
5302 unsigned long i,j;
|
yuuji@0
|
5303 THREADNODE *cur;
|
yuuji@0
|
5304 /* sort children of each thread */
|
yuuji@0
|
5305 for (cur = thr; cur; cur = cur->branch)
|
yuuji@0
|
5306 if (cur->next) cur->next = mail_thread_sort (cur->next,tc);
|
yuuji@0
|
5307 /* Must do this in a separate pass since recursive call will clobber tc */
|
yuuji@0
|
5308 /* load threadcache and count nodes to sort */
|
yuuji@0
|
5309 for (i = 0, cur = thr; cur; cur = cur->branch) tc[i++] = cur;
|
yuuji@0
|
5310 if (i > 1) { /* only if need to sort */
|
yuuji@0
|
5311 qsort ((void *) tc,i,sizeof (THREADNODE *),mail_thread_compare_date);
|
yuuji@0
|
5312 /* relink root siblings */
|
yuuji@0
|
5313 for (j = 0, --i; j < i; j++) tc[j]->branch = tc[j+1];
|
yuuji@0
|
5314 tc[j]->branch = NIL; /* end of root */
|
yuuji@0
|
5315 }
|
yuuji@0
|
5316 return i ? tc[0] : NIL; /* return new head of list */
|
yuuji@0
|
5317 }
|
yuuji@0
|
5318
|
yuuji@0
|
5319
|
yuuji@0
|
5320 /* Thread compare date
|
yuuji@0
|
5321 * Accept: first message sort cache element
|
yuuji@0
|
5322 * second message sort cache element
|
yuuji@0
|
5323 * Returns: -1 if a1 < a2, 1 if a1 > a2
|
yuuji@0
|
5324 *
|
yuuji@0
|
5325 * This assumes that a sort cache element is either a message (with a
|
yuuji@0
|
5326 * sortcache entry) or a dummy with a message (with sortcache entry) child.
|
yuuji@0
|
5327 * This is true of both the ORDEREDSUBJECT (no dummies) and REFERENCES
|
yuuji@0
|
5328 * (dummies only at top-level, and with non-dummy children).
|
yuuji@0
|
5329 *
|
yuuji@0
|
5330 * If a new algorithm allows a dummy parent to have a dummy child, this
|
yuuji@0
|
5331 * routine must be changed if it is to be used by that algorithm.
|
yuuji@0
|
5332 *
|
yuuji@0
|
5333 * Messages with bogus dates are always sorted at the top.
|
yuuji@0
|
5334 */
|
yuuji@0
|
5335
|
yuuji@0
|
5336 int mail_thread_compare_date (const void *a1,const void *a2)
|
yuuji@0
|
5337 {
|
yuuji@0
|
5338 THREADNODE *t1 = *(THREADNODE **) a1;
|
yuuji@0
|
5339 THREADNODE *t2 = *(THREADNODE **) a2;
|
yuuji@0
|
5340 SORTCACHE *s1 = t1->sc ? t1->sc : t1->next->sc;
|
yuuji@0
|
5341 SORTCACHE *s2 = t2->sc ? t2->sc : t2->next->sc;
|
yuuji@0
|
5342 int ret = compare_ulong (s1->date,s2->date);
|
yuuji@0
|
5343 /* use number as final tie-breaker */
|
yuuji@0
|
5344 return ret ? ret : compare_ulong (s1->num,s2->num);
|
yuuji@0
|
5345 }
|
yuuji@0
|
5346
|
yuuji@0
|
5347 /* Mail parse sequence
|
yuuji@0
|
5348 * Accepts: mail stream
|
yuuji@0
|
5349 * sequence to parse
|
yuuji@0
|
5350 * Returns: T if parse successful, else NIL
|
yuuji@0
|
5351 */
|
yuuji@0
|
5352
|
yuuji@0
|
5353 long mail_sequence (MAILSTREAM *stream,unsigned char *sequence)
|
yuuji@0
|
5354 {
|
yuuji@0
|
5355 unsigned long i,j,x;
|
yuuji@0
|
5356 for (i = 1; i <= stream->nmsgs; i++) mail_elt (stream,i)->sequence = NIL;
|
yuuji@0
|
5357 while (sequence && *sequence){/* while there is something to parse */
|
yuuji@0
|
5358 if (*sequence == '*') { /* maximum message */
|
yuuji@0
|
5359 if (stream->nmsgs) i = stream->nmsgs;
|
yuuji@0
|
5360 else {
|
yuuji@0
|
5361 MM_LOG ("No messages, so no maximum message number",ERROR);
|
yuuji@0
|
5362 return NIL;
|
yuuji@0
|
5363 }
|
yuuji@0
|
5364 sequence++; /* skip past * */
|
yuuji@0
|
5365 }
|
yuuji@0
|
5366 /* parse and validate message number */
|
yuuji@0
|
5367 else if (!isdigit (*sequence)) {
|
yuuji@0
|
5368 MM_LOG ("Syntax error in sequence",ERROR);
|
yuuji@0
|
5369 return NIL;
|
yuuji@0
|
5370 }
|
yuuji@0
|
5371 else if (!(i = strtoul (sequence,(char **) &sequence,10)) ||
|
yuuji@0
|
5372 (i > stream->nmsgs)) {
|
yuuji@0
|
5373 MM_LOG ("Sequence out of range",ERROR);
|
yuuji@0
|
5374 return NIL;
|
yuuji@0
|
5375 }
|
yuuji@0
|
5376 switch (*sequence) { /* see what the delimiter is */
|
yuuji@0
|
5377 case ':': /* sequence range */
|
yuuji@0
|
5378 if (*++sequence == '*') { /* maximum message */
|
yuuji@0
|
5379 if (stream->nmsgs) j = stream->nmsgs;
|
yuuji@0
|
5380 else {
|
yuuji@0
|
5381 MM_LOG ("No messages, so no maximum message number",ERROR);
|
yuuji@0
|
5382 return NIL;
|
yuuji@0
|
5383 }
|
yuuji@0
|
5384 sequence++; /* skip past * */
|
yuuji@0
|
5385 }
|
yuuji@0
|
5386 /* parse end of range */
|
yuuji@0
|
5387 else if (!(j = strtoul (sequence,(char **) &sequence,10)) ||
|
yuuji@0
|
5388 (j > stream->nmsgs)) {
|
yuuji@0
|
5389 MM_LOG ("Sequence range invalid",ERROR);
|
yuuji@0
|
5390 return NIL;
|
yuuji@0
|
5391 }
|
yuuji@0
|
5392 if (*sequence && *sequence++ != ',') {
|
yuuji@0
|
5393 MM_LOG ("Sequence range syntax error",ERROR);
|
yuuji@0
|
5394 return NIL;
|
yuuji@0
|
5395 }
|
yuuji@0
|
5396 if (i > j) { /* swap the range if backwards */
|
yuuji@0
|
5397 x = i; i = j; j = x;
|
yuuji@0
|
5398 }
|
yuuji@0
|
5399 /* mark each item in the sequence */
|
yuuji@0
|
5400 while (i <= j) mail_elt (stream,j--)->sequence = T;
|
yuuji@0
|
5401 break;
|
yuuji@0
|
5402 case ',': /* single message */
|
yuuji@0
|
5403 ++sequence; /* skip the delimiter, fall into end case */
|
yuuji@0
|
5404 case '\0': /* end of sequence, mark this message */
|
yuuji@0
|
5405 mail_elt (stream,i)->sequence = T;
|
yuuji@0
|
5406 break;
|
yuuji@0
|
5407 default: /* anything else is a syntax error! */
|
yuuji@0
|
5408 MM_LOG ("Sequence syntax error",ERROR);
|
yuuji@0
|
5409 return NIL;
|
yuuji@0
|
5410 }
|
yuuji@0
|
5411 }
|
yuuji@0
|
5412 return T; /* successfully parsed sequence */
|
yuuji@0
|
5413 }
|
yuuji@0
|
5414
|
yuuji@0
|
5415 /* Parse flag list
|
yuuji@0
|
5416 * Accepts: MAIL stream
|
yuuji@0
|
5417 * flag list as a character string
|
yuuji@0
|
5418 * pointer to user flags to return
|
yuuji@0
|
5419 * Returns: system flags
|
yuuji@0
|
5420 */
|
yuuji@0
|
5421
|
yuuji@0
|
5422 long mail_parse_flags (MAILSTREAM *stream,char *flag,unsigned long *uf)
|
yuuji@0
|
5423 {
|
yuuji@0
|
5424 char *t,*n,*s,tmp[MAILTMPLEN],msg[MAILTMPLEN];
|
yuuji@0
|
5425 short f = 0;
|
yuuji@0
|
5426 long i,j;
|
yuuji@0
|
5427 *uf = 0; /* initially no user flags */
|
yuuji@0
|
5428 if (flag && *flag) { /* no-op if no flag string */
|
yuuji@0
|
5429 /* check if a list and make sure valid */
|
yuuji@0
|
5430 if (((i = (*flag == '(')) ^ (flag[strlen (flag)-1] == ')')) ||
|
yuuji@0
|
5431 (strlen (flag) >= MAILTMPLEN)) {
|
yuuji@0
|
5432 MM_LOG ("Bad flag list",ERROR);
|
yuuji@0
|
5433 return NIL;
|
yuuji@0
|
5434 }
|
yuuji@0
|
5435 /* copy the flag string w/o list construct */
|
yuuji@0
|
5436 strncpy (n = tmp,flag+i,(j = strlen (flag) - (2*i)));
|
yuuji@0
|
5437 tmp[j] = '\0';
|
yuuji@0
|
5438 while ((t = n) && *t) { /* parse the flags */
|
yuuji@0
|
5439 /* find end of flag */
|
yuuji@0
|
5440 if (n = strchr (t,' ')) *n++ = '\0';
|
yuuji@0
|
5441 if (*t == '\\') { /* system flag? */
|
yuuji@0
|
5442 if (!compare_cstring (t+1,"SEEN")) f |= fSEEN;
|
yuuji@0
|
5443 else if (!compare_cstring (t+1,"DELETED")) f |= fDELETED;
|
yuuji@0
|
5444 else if (!compare_cstring (t+1,"FLAGGED")) f |= fFLAGGED;
|
yuuji@0
|
5445 else if (!compare_cstring (t+1,"ANSWERED")) f |= fANSWERED;
|
yuuji@0
|
5446 else if (!compare_cstring (t+1,"DRAFT")) f |= fDRAFT;
|
yuuji@0
|
5447 else {
|
yuuji@0
|
5448 sprintf (msg,"Unsupported system flag: %.80s",t);
|
yuuji@0
|
5449 MM_LOG (msg,WARN);
|
yuuji@0
|
5450 }
|
yuuji@0
|
5451 }
|
yuuji@0
|
5452
|
yuuji@0
|
5453 else { /* keyword flag */
|
yuuji@0
|
5454 for (i = j = 0; /* user flag, search through table */
|
yuuji@0
|
5455 !i && (j < NUSERFLAGS) && (s = stream->user_flags[j]); ++j)
|
yuuji@0
|
5456 if (!compare_cstring (t,s)) *uf |= i = 1 << j;
|
yuuji@0
|
5457 if (!i) { /* flag not found, can it be created? */
|
yuuji@0
|
5458 if (stream->kwd_create && (j < NUSERFLAGS) && *t &&
|
yuuji@0
|
5459 (strlen (t) <= MAXUSERFLAG)) {
|
yuuji@0
|
5460 for (s = t; t && *s; s++) switch (*s) {
|
yuuji@0
|
5461 default: /* all other characters */
|
yuuji@0
|
5462 /* SPACE, CTL, or not CHAR */
|
yuuji@0
|
5463 if ((*s > ' ') && (*s < 0x7f)) break;
|
yuuji@0
|
5464 case '*': case '%': /* list_wildcards */
|
yuuji@0
|
5465 case '"': case '\\':/* quoted-specials */
|
yuuji@0
|
5466 /* atom_specials */
|
yuuji@0
|
5467 case '(': case ')': case '{':
|
yuuji@0
|
5468 case ']': /* resp-specials */
|
yuuji@0
|
5469 sprintf (msg,"Invalid flag: %.80s",t);
|
yuuji@0
|
5470 MM_LOG (msg,WARN);
|
yuuji@0
|
5471 t = NIL;
|
yuuji@0
|
5472 }
|
yuuji@0
|
5473 if (t) { /* only if valid */
|
yuuji@0
|
5474 *uf |= 1 << j; /* set the bit */
|
yuuji@0
|
5475 stream->user_flags[j] = cpystr (t);
|
yuuji@0
|
5476 /* if out of user flags */
|
yuuji@0
|
5477 if (j == NUSERFLAGS - 1) stream->kwd_create = NIL;
|
yuuji@0
|
5478 }
|
yuuji@0
|
5479 }
|
yuuji@0
|
5480 else {
|
yuuji@0
|
5481 if (*t) sprintf (msg,"Unknown flag: %.80s",t);
|
yuuji@0
|
5482 else strcpy (msg,"Empty flag invalid");
|
yuuji@0
|
5483 MM_LOG (msg,WARN);
|
yuuji@0
|
5484 }
|
yuuji@0
|
5485 }
|
yuuji@0
|
5486 }
|
yuuji@0
|
5487 }
|
yuuji@0
|
5488 }
|
yuuji@0
|
5489 return f;
|
yuuji@0
|
5490 }
|
yuuji@0
|
5491
|
yuuji@0
|
5492 /* Mail check network stream for usability with new name
|
yuuji@0
|
5493 * Accepts: MAIL stream
|
yuuji@0
|
5494 * candidate new name
|
yuuji@0
|
5495 * Returns: T if stream can be used, NIL otherwise
|
yuuji@0
|
5496 */
|
yuuji@0
|
5497
|
yuuji@0
|
5498 long mail_usable_network_stream (MAILSTREAM *stream,char *name)
|
yuuji@0
|
5499 {
|
yuuji@0
|
5500 NETMBX smb,nmb,omb;
|
yuuji@0
|
5501 return (stream && stream->dtb && !(stream->dtb->flags & DR_LOCAL) &&
|
yuuji@0
|
5502 mail_valid_net_parse (name,&nmb) &&
|
yuuji@0
|
5503 mail_valid_net_parse (stream->mailbox,&smb) &&
|
yuuji@0
|
5504 mail_valid_net_parse (stream->original_mailbox,&omb) &&
|
yuuji@0
|
5505 ((!compare_cstring (smb.host,
|
yuuji@0
|
5506 trustdns ? tcp_canonical (nmb.host) : nmb.host)&&
|
yuuji@0
|
5507 !strcmp (smb.service,nmb.service) &&
|
yuuji@0
|
5508 (!nmb.port || (smb.port == nmb.port)) &&
|
yuuji@0
|
5509 (nmb.anoflag == stream->anonymous) &&
|
yuuji@0
|
5510 (!nmb.user[0] || !strcmp (smb.user,nmb.user))) ||
|
yuuji@0
|
5511 (!compare_cstring (omb.host,nmb.host) &&
|
yuuji@0
|
5512 !strcmp (omb.service,nmb.service) &&
|
yuuji@0
|
5513 (!nmb.port || (omb.port == nmb.port)) &&
|
yuuji@0
|
5514 (nmb.anoflag == stream->anonymous) &&
|
yuuji@0
|
5515 (!nmb.user[0] || !strcmp (omb.user,nmb.user))))) ? LONGT : NIL;
|
yuuji@0
|
5516 }
|
yuuji@0
|
5517
|
yuuji@0
|
5518 /* Mail data structure instantiation routines */
|
yuuji@0
|
5519
|
yuuji@0
|
5520
|
yuuji@0
|
5521 /* Mail instantiate cache elt
|
yuuji@0
|
5522 * Accepts: initial message number
|
yuuji@0
|
5523 * Returns: new cache elt
|
yuuji@0
|
5524 */
|
yuuji@0
|
5525
|
yuuji@0
|
5526 MESSAGECACHE *mail_new_cache_elt (unsigned long msgno)
|
yuuji@0
|
5527 {
|
yuuji@0
|
5528 MESSAGECACHE *elt = (MESSAGECACHE *) memset (fs_get (sizeof (MESSAGECACHE)),
|
yuuji@0
|
5529 0,sizeof (MESSAGECACHE));
|
yuuji@0
|
5530 elt->lockcount = 1; /* initially only cache references it */
|
yuuji@0
|
5531 elt->msgno = msgno; /* message number */
|
yuuji@0
|
5532 return elt;
|
yuuji@0
|
5533 }
|
yuuji@0
|
5534
|
yuuji@0
|
5535
|
yuuji@0
|
5536 /* Mail instantiate envelope
|
yuuji@0
|
5537 * Returns: new envelope
|
yuuji@0
|
5538 */
|
yuuji@0
|
5539
|
yuuji@0
|
5540 ENVELOPE *mail_newenvelope (void)
|
yuuji@0
|
5541 {
|
yuuji@0
|
5542 return (ENVELOPE *) memset (fs_get (sizeof (ENVELOPE)),0,sizeof (ENVELOPE));
|
yuuji@0
|
5543 }
|
yuuji@0
|
5544
|
yuuji@0
|
5545
|
yuuji@0
|
5546 /* Mail instantiate address
|
yuuji@0
|
5547 * Returns: new address
|
yuuji@0
|
5548 */
|
yuuji@0
|
5549
|
yuuji@0
|
5550 ADDRESS *mail_newaddr (void)
|
yuuji@0
|
5551 {
|
yuuji@0
|
5552 return (ADDRESS *) memset (fs_get (sizeof (ADDRESS)),0,sizeof (ADDRESS));
|
yuuji@0
|
5553 }
|
yuuji@0
|
5554
|
yuuji@0
|
5555 /* Mail instantiate body
|
yuuji@0
|
5556 * Returns: new body
|
yuuji@0
|
5557 */
|
yuuji@0
|
5558
|
yuuji@0
|
5559 BODY *mail_newbody (void)
|
yuuji@0
|
5560 {
|
yuuji@0
|
5561 return mail_initbody ((BODY *) fs_get (sizeof (BODY)));
|
yuuji@0
|
5562 }
|
yuuji@0
|
5563
|
yuuji@0
|
5564
|
yuuji@0
|
5565 /* Mail initialize body
|
yuuji@0
|
5566 * Accepts: body
|
yuuji@0
|
5567 * Returns: body
|
yuuji@0
|
5568 */
|
yuuji@0
|
5569
|
yuuji@0
|
5570 BODY *mail_initbody (BODY *body)
|
yuuji@0
|
5571 {
|
yuuji@0
|
5572 memset ((void *) body,0,sizeof (BODY));
|
yuuji@0
|
5573 body->type = TYPETEXT; /* content type */
|
yuuji@0
|
5574 body->encoding = ENC7BIT; /* content encoding */
|
yuuji@0
|
5575 return body;
|
yuuji@0
|
5576 }
|
yuuji@0
|
5577
|
yuuji@0
|
5578
|
yuuji@0
|
5579 /* Mail instantiate body parameter
|
yuuji@0
|
5580 * Returns: new body part
|
yuuji@0
|
5581 */
|
yuuji@0
|
5582
|
yuuji@0
|
5583 PARAMETER *mail_newbody_parameter (void)
|
yuuji@0
|
5584 {
|
yuuji@0
|
5585 return (PARAMETER *) memset (fs_get (sizeof(PARAMETER)),0,sizeof(PARAMETER));
|
yuuji@0
|
5586 }
|
yuuji@0
|
5587
|
yuuji@0
|
5588
|
yuuji@0
|
5589 /* Mail instantiate body part
|
yuuji@0
|
5590 * Returns: new body part
|
yuuji@0
|
5591 */
|
yuuji@0
|
5592
|
yuuji@0
|
5593 PART *mail_newbody_part (void)
|
yuuji@0
|
5594 {
|
yuuji@0
|
5595 PART *part = (PART *) memset (fs_get (sizeof (PART)),0,sizeof (PART));
|
yuuji@0
|
5596 mail_initbody (&part->body); /* initialize the body */
|
yuuji@0
|
5597 return part;
|
yuuji@0
|
5598 }
|
yuuji@0
|
5599
|
yuuji@0
|
5600
|
yuuji@0
|
5601 /* Mail instantiate body message part
|
yuuji@0
|
5602 * Returns: new body message part
|
yuuji@0
|
5603 */
|
yuuji@0
|
5604
|
yuuji@0
|
5605 MESSAGE *mail_newmsg (void)
|
yuuji@0
|
5606 {
|
yuuji@0
|
5607 return (MESSAGE *) memset (fs_get (sizeof (MESSAGE)),0,sizeof (MESSAGE));
|
yuuji@0
|
5608 }
|
yuuji@0
|
5609
|
yuuji@0
|
5610 /* Mail instantiate string list
|
yuuji@0
|
5611 * Returns: new string list
|
yuuji@0
|
5612 */
|
yuuji@0
|
5613
|
yuuji@0
|
5614 STRINGLIST *mail_newstringlist (void)
|
yuuji@0
|
5615 {
|
yuuji@0
|
5616 return (STRINGLIST *) memset (fs_get (sizeof (STRINGLIST)),0,
|
yuuji@0
|
5617 sizeof (STRINGLIST));
|
yuuji@0
|
5618 }
|
yuuji@0
|
5619
|
yuuji@0
|
5620
|
yuuji@0
|
5621 /* Mail instantiate new search program
|
yuuji@0
|
5622 * Returns: new search program
|
yuuji@0
|
5623 */
|
yuuji@0
|
5624
|
yuuji@0
|
5625 SEARCHPGM *mail_newsearchpgm (void)
|
yuuji@0
|
5626 {
|
yuuji@0
|
5627 return (SEARCHPGM *) memset (fs_get (sizeof(SEARCHPGM)),0,sizeof(SEARCHPGM));
|
yuuji@0
|
5628 }
|
yuuji@0
|
5629
|
yuuji@0
|
5630
|
yuuji@0
|
5631 /* Mail instantiate new search program
|
yuuji@0
|
5632 * Accepts: header line name
|
yuuji@0
|
5633 * Returns: new search program
|
yuuji@0
|
5634 */
|
yuuji@0
|
5635
|
yuuji@0
|
5636 SEARCHHEADER *mail_newsearchheader (char *line,char *text)
|
yuuji@0
|
5637 {
|
yuuji@0
|
5638 SEARCHHEADER *hdr = (SEARCHHEADER *) memset (fs_get (sizeof (SEARCHHEADER)),
|
yuuji@0
|
5639 0,sizeof (SEARCHHEADER));
|
yuuji@0
|
5640 hdr->line.size = strlen ((char *) (hdr->line.data =
|
yuuji@0
|
5641 (unsigned char *) cpystr (line)));
|
yuuji@0
|
5642 hdr->text.size = strlen ((char *) (hdr->text.data =
|
yuuji@0
|
5643 (unsigned char *) cpystr (text)));
|
yuuji@0
|
5644 return hdr;
|
yuuji@0
|
5645 }
|
yuuji@0
|
5646
|
yuuji@0
|
5647
|
yuuji@0
|
5648 /* Mail instantiate new search set
|
yuuji@0
|
5649 * Returns: new search set
|
yuuji@0
|
5650 */
|
yuuji@0
|
5651
|
yuuji@0
|
5652 SEARCHSET *mail_newsearchset (void)
|
yuuji@0
|
5653 {
|
yuuji@0
|
5654 return (SEARCHSET *) memset (fs_get (sizeof(SEARCHSET)),0,sizeof(SEARCHSET));
|
yuuji@0
|
5655 }
|
yuuji@0
|
5656
|
yuuji@0
|
5657
|
yuuji@0
|
5658 /* Mail instantiate new search or
|
yuuji@0
|
5659 * Returns: new search or
|
yuuji@0
|
5660 */
|
yuuji@0
|
5661
|
yuuji@0
|
5662 SEARCHOR *mail_newsearchor (void)
|
yuuji@0
|
5663 {
|
yuuji@0
|
5664 SEARCHOR *or = (SEARCHOR *) memset (fs_get (sizeof (SEARCHOR)),0,
|
yuuji@0
|
5665 sizeof (SEARCHOR));
|
yuuji@0
|
5666 or->first = mail_newsearchpgm ();
|
yuuji@0
|
5667 or->second = mail_newsearchpgm ();
|
yuuji@0
|
5668 return or;
|
yuuji@0
|
5669 }
|
yuuji@0
|
5670
|
yuuji@0
|
5671 /* Mail instantiate new searchpgmlist
|
yuuji@0
|
5672 * Returns: new searchpgmlist
|
yuuji@0
|
5673 */
|
yuuji@0
|
5674
|
yuuji@0
|
5675 SEARCHPGMLIST *mail_newsearchpgmlist (void)
|
yuuji@0
|
5676 {
|
yuuji@0
|
5677 SEARCHPGMLIST *pgl = (SEARCHPGMLIST *)
|
yuuji@0
|
5678 memset (fs_get (sizeof (SEARCHPGMLIST)),0,sizeof (SEARCHPGMLIST));
|
yuuji@0
|
5679 pgl->pgm = mail_newsearchpgm ();
|
yuuji@0
|
5680 return pgl;
|
yuuji@0
|
5681 }
|
yuuji@0
|
5682
|
yuuji@0
|
5683
|
yuuji@0
|
5684 /* Mail instantiate new sortpgm
|
yuuji@0
|
5685 * Returns: new sortpgm
|
yuuji@0
|
5686 */
|
yuuji@0
|
5687
|
yuuji@0
|
5688 SORTPGM *mail_newsortpgm (void)
|
yuuji@0
|
5689 {
|
yuuji@0
|
5690 return (SORTPGM *) memset (fs_get (sizeof (SORTPGM)),0,sizeof (SORTPGM));
|
yuuji@0
|
5691 }
|
yuuji@0
|
5692
|
yuuji@0
|
5693
|
yuuji@0
|
5694 /* Mail instantiate new threadnode
|
yuuji@0
|
5695 * Accepts: sort cache for thread node
|
yuuji@0
|
5696 * Returns: new threadnode
|
yuuji@0
|
5697 */
|
yuuji@0
|
5698
|
yuuji@0
|
5699 THREADNODE *mail_newthreadnode (SORTCACHE *sc)
|
yuuji@0
|
5700 {
|
yuuji@0
|
5701 THREADNODE *thr = (THREADNODE *) memset (fs_get (sizeof (THREADNODE)),0,
|
yuuji@0
|
5702 sizeof (THREADNODE));
|
yuuji@0
|
5703 if (sc) thr->sc = sc; /* initialize sortcache */
|
yuuji@0
|
5704 return thr;
|
yuuji@0
|
5705 }
|
yuuji@0
|
5706
|
yuuji@0
|
5707
|
yuuji@0
|
5708 /* Mail instantiate new acllist
|
yuuji@0
|
5709 * Returns: new acllist
|
yuuji@0
|
5710 */
|
yuuji@0
|
5711
|
yuuji@0
|
5712 ACLLIST *mail_newacllist (void)
|
yuuji@0
|
5713 {
|
yuuji@0
|
5714 return (ACLLIST *) memset (fs_get (sizeof (ACLLIST)),0,sizeof (ACLLIST));
|
yuuji@0
|
5715 }
|
yuuji@0
|
5716
|
yuuji@0
|
5717
|
yuuji@0
|
5718 /* Mail instantiate new quotalist
|
yuuji@0
|
5719 * Returns: new quotalist
|
yuuji@0
|
5720 */
|
yuuji@0
|
5721
|
yuuji@0
|
5722 QUOTALIST *mail_newquotalist (void)
|
yuuji@0
|
5723 {
|
yuuji@0
|
5724 return (QUOTALIST *) memset (fs_get (sizeof (QUOTALIST)),0,
|
yuuji@0
|
5725 sizeof (QUOTALIST));
|
yuuji@0
|
5726 }
|
yuuji@0
|
5727
|
yuuji@0
|
5728 /* Mail garbage collection routines */
|
yuuji@0
|
5729
|
yuuji@0
|
5730
|
yuuji@0
|
5731 /* Mail garbage collect body
|
yuuji@0
|
5732 * Accepts: pointer to body pointer
|
yuuji@0
|
5733 */
|
yuuji@0
|
5734
|
yuuji@0
|
5735 void mail_free_body (BODY **body)
|
yuuji@0
|
5736 {
|
yuuji@0
|
5737 if (*body) { /* only free if exists */
|
yuuji@0
|
5738 mail_free_body_data (*body);/* free its data */
|
yuuji@0
|
5739 fs_give ((void **) body); /* return body to free storage */
|
yuuji@0
|
5740 }
|
yuuji@0
|
5741 }
|
yuuji@0
|
5742
|
yuuji@0
|
5743
|
yuuji@0
|
5744 /* Mail garbage collect body data
|
yuuji@0
|
5745 * Accepts: body pointer
|
yuuji@0
|
5746 */
|
yuuji@0
|
5747
|
yuuji@0
|
5748 void mail_free_body_data (BODY *body)
|
yuuji@0
|
5749 {
|
yuuji@0
|
5750 switch (body->type) { /* free contents */
|
yuuji@0
|
5751 case TYPEMULTIPART: /* multiple part */
|
yuuji@0
|
5752 mail_free_body_part (&body->nested.part);
|
yuuji@0
|
5753 break;
|
yuuji@0
|
5754 case TYPEMESSAGE: /* encapsulated message */
|
yuuji@0
|
5755 if (body->subtype && !strcmp (body->subtype,"RFC822")) {
|
yuuji@0
|
5756 mail_free_stringlist (&body->nested.msg->lines);
|
yuuji@0
|
5757 mail_gc_msg (body->nested.msg,GC_ENV | GC_TEXTS);
|
yuuji@0
|
5758 }
|
yuuji@0
|
5759 if (body->nested.msg) fs_give ((void **) &body->nested.msg);
|
yuuji@0
|
5760 break;
|
yuuji@0
|
5761 default:
|
yuuji@0
|
5762 break;
|
yuuji@0
|
5763 }
|
yuuji@0
|
5764 if (body->subtype) fs_give ((void **) &body->subtype);
|
yuuji@0
|
5765 mail_free_body_parameter (&body->parameter);
|
yuuji@0
|
5766 if (body->id) fs_give ((void **) &body->id);
|
yuuji@0
|
5767 if (body->description) fs_give ((void **) &body->description);
|
yuuji@0
|
5768 if (body->disposition.type) fs_give ((void **) &body->disposition.type);
|
yuuji@0
|
5769 if (body->disposition.parameter)
|
yuuji@0
|
5770 mail_free_body_parameter (&body->disposition.parameter);
|
yuuji@0
|
5771 if (body->language) mail_free_stringlist (&body->language);
|
yuuji@0
|
5772 if (body->location) fs_give ((void **) &body->location);
|
yuuji@0
|
5773 if (body->mime.text.data) fs_give ((void **) &body->mime.text.data);
|
yuuji@0
|
5774 if (body->contents.text.data) fs_give ((void **) &body->contents.text.data);
|
yuuji@0
|
5775 if (body->md5) fs_give ((void **) &body->md5);
|
yuuji@0
|
5776 if (mailfreebodysparep && body->sparep)
|
yuuji@0
|
5777 (*mailfreebodysparep) (&body->sparep);
|
yuuji@0
|
5778 }
|
yuuji@0
|
5779
|
yuuji@0
|
5780 /* Mail garbage collect body parameter
|
yuuji@0
|
5781 * Accepts: pointer to body parameter pointer
|
yuuji@0
|
5782 */
|
yuuji@0
|
5783
|
yuuji@0
|
5784 void mail_free_body_parameter (PARAMETER **parameter)
|
yuuji@0
|
5785 {
|
yuuji@0
|
5786 if (*parameter) { /* only free if exists */
|
yuuji@0
|
5787 if ((*parameter)->attribute) fs_give ((void **) &(*parameter)->attribute);
|
yuuji@0
|
5788 if ((*parameter)->value) fs_give ((void **) &(*parameter)->value);
|
yuuji@0
|
5789 /* run down the list as necessary */
|
yuuji@0
|
5790 mail_free_body_parameter (&(*parameter)->next);
|
yuuji@0
|
5791 /* return body part to free storage */
|
yuuji@0
|
5792 fs_give ((void **) parameter);
|
yuuji@0
|
5793 }
|
yuuji@0
|
5794 }
|
yuuji@0
|
5795
|
yuuji@0
|
5796
|
yuuji@0
|
5797 /* Mail garbage collect body part
|
yuuji@0
|
5798 * Accepts: pointer to body part pointer
|
yuuji@0
|
5799 */
|
yuuji@0
|
5800
|
yuuji@0
|
5801 void mail_free_body_part (PART **part)
|
yuuji@0
|
5802 {
|
yuuji@0
|
5803 if (*part) { /* only free if exists */
|
yuuji@0
|
5804 mail_free_body_data (&(*part)->body);
|
yuuji@0
|
5805 /* run down the list as necessary */
|
yuuji@0
|
5806 mail_free_body_part (&(*part)->next);
|
yuuji@0
|
5807 fs_give ((void **) part); /* return body part to free storage */
|
yuuji@0
|
5808 }
|
yuuji@0
|
5809 }
|
yuuji@0
|
5810
|
yuuji@0
|
5811 /* Mail garbage collect message cache
|
yuuji@0
|
5812 * Accepts: mail stream
|
yuuji@0
|
5813 *
|
yuuji@0
|
5814 * The message cache is set to NIL when this function finishes.
|
yuuji@0
|
5815 */
|
yuuji@0
|
5816
|
yuuji@0
|
5817 void mail_free_cache (MAILSTREAM *stream)
|
yuuji@0
|
5818 {
|
yuuji@0
|
5819 /* do driver specific stuff first */
|
yuuji@0
|
5820 mail_gc (stream,GC_ELT | GC_ENV | GC_TEXTS);
|
yuuji@0
|
5821 /* flush the cache */
|
yuuji@0
|
5822 (*mailcache) (stream,(long) 0,CH_INIT);
|
yuuji@0
|
5823 }
|
yuuji@0
|
5824
|
yuuji@0
|
5825
|
yuuji@0
|
5826 /* Mail garbage collect cache element
|
yuuji@0
|
5827 * Accepts: pointer to cache element pointer
|
yuuji@0
|
5828 */
|
yuuji@0
|
5829
|
yuuji@0
|
5830 void mail_free_elt (MESSAGECACHE **elt)
|
yuuji@0
|
5831 {
|
yuuji@0
|
5832 /* only free if exists and no sharers */
|
yuuji@0
|
5833 if (*elt && !--(*elt)->lockcount) {
|
yuuji@0
|
5834 mail_gc_msg (&(*elt)->private.msg,GC_ENV | GC_TEXTS);
|
yuuji@0
|
5835 if (mailfreeeltsparep && (*elt)->sparep)
|
yuuji@0
|
5836 (*mailfreeeltsparep) (&(*elt)->sparep);
|
yuuji@0
|
5837 fs_give ((void **) elt);
|
yuuji@0
|
5838 }
|
yuuji@0
|
5839 else *elt = NIL; /* else simply drop pointer */
|
yuuji@0
|
5840 }
|
yuuji@0
|
5841
|
yuuji@0
|
5842 /* Mail garbage collect envelope
|
yuuji@0
|
5843 * Accepts: pointer to envelope pointer
|
yuuji@0
|
5844 */
|
yuuji@0
|
5845
|
yuuji@0
|
5846 void mail_free_envelope (ENVELOPE **env)
|
yuuji@0
|
5847 {
|
yuuji@0
|
5848 if (*env) { /* only free if exists */
|
yuuji@0
|
5849 if ((*env)->remail) fs_give ((void **) &(*env)->remail);
|
yuuji@0
|
5850 mail_free_address (&(*env)->return_path);
|
yuuji@0
|
5851 if ((*env)->date) fs_give ((void **) &(*env)->date);
|
yuuji@0
|
5852 mail_free_address (&(*env)->from);
|
yuuji@0
|
5853 mail_free_address (&(*env)->sender);
|
yuuji@0
|
5854 mail_free_address (&(*env)->reply_to);
|
yuuji@0
|
5855 if ((*env)->subject) fs_give ((void **) &(*env)->subject);
|
yuuji@0
|
5856 mail_free_address (&(*env)->to);
|
yuuji@0
|
5857 mail_free_address (&(*env)->cc);
|
yuuji@0
|
5858 mail_free_address (&(*env)->bcc);
|
yuuji@0
|
5859 if ((*env)->in_reply_to) fs_give ((void **) &(*env)->in_reply_to);
|
yuuji@0
|
5860 if ((*env)->message_id) fs_give ((void **) &(*env)->message_id);
|
yuuji@0
|
5861 if ((*env)->newsgroups) fs_give ((void **) &(*env)->newsgroups);
|
yuuji@0
|
5862 if ((*env)->followup_to) fs_give ((void **) &(*env)->followup_to);
|
yuuji@0
|
5863 if ((*env)->references) fs_give ((void **) &(*env)->references);
|
yuuji@0
|
5864 if (mailfreeenvelopesparep && (*env)->sparep)
|
yuuji@0
|
5865 (*mailfreeenvelopesparep) (&(*env)->sparep);
|
yuuji@0
|
5866 fs_give ((void **) env); /* return envelope to free storage */
|
yuuji@0
|
5867 }
|
yuuji@0
|
5868 }
|
yuuji@0
|
5869
|
yuuji@0
|
5870
|
yuuji@0
|
5871 /* Mail garbage collect address
|
yuuji@0
|
5872 * Accepts: pointer to address pointer
|
yuuji@0
|
5873 */
|
yuuji@0
|
5874
|
yuuji@0
|
5875 void mail_free_address (ADDRESS **address)
|
yuuji@0
|
5876 {
|
yuuji@0
|
5877 if (*address) { /* only free if exists */
|
yuuji@0
|
5878 if ((*address)->personal) fs_give ((void **) &(*address)->personal);
|
yuuji@0
|
5879 if ((*address)->adl) fs_give ((void **) &(*address)->adl);
|
yuuji@0
|
5880 if ((*address)->mailbox) fs_give ((void **) &(*address)->mailbox);
|
yuuji@0
|
5881 if ((*address)->host) fs_give ((void **) &(*address)->host);
|
yuuji@0
|
5882 if ((*address)->error) fs_give ((void **) &(*address)->error);
|
yuuji@0
|
5883 if ((*address)->orcpt.type) fs_give ((void **) &(*address)->orcpt.type);
|
yuuji@0
|
5884 if ((*address)->orcpt.addr) fs_give ((void **) &(*address)->orcpt.addr);
|
yuuji@0
|
5885 mail_free_address (&(*address)->next);
|
yuuji@0
|
5886 fs_give ((void **) address);/* return address to free storage */
|
yuuji@0
|
5887 }
|
yuuji@0
|
5888 }
|
yuuji@0
|
5889
|
yuuji@0
|
5890
|
yuuji@0
|
5891 /* Mail garbage collect stringlist
|
yuuji@0
|
5892 * Accepts: pointer to stringlist pointer
|
yuuji@0
|
5893 */
|
yuuji@0
|
5894
|
yuuji@0
|
5895 void mail_free_stringlist (STRINGLIST **string)
|
yuuji@0
|
5896 {
|
yuuji@0
|
5897 if (*string) { /* only free if exists */
|
yuuji@0
|
5898 if ((*string)->text.data) fs_give ((void **) &(*string)->text.data);
|
yuuji@0
|
5899 mail_free_stringlist (&(*string)->next);
|
yuuji@0
|
5900 fs_give ((void **) string); /* return string to free storage */
|
yuuji@0
|
5901 }
|
yuuji@0
|
5902 }
|
yuuji@0
|
5903
|
yuuji@0
|
5904 /* Mail garbage collect searchpgm
|
yuuji@0
|
5905 * Accepts: pointer to searchpgm pointer
|
yuuji@0
|
5906 */
|
yuuji@0
|
5907
|
yuuji@0
|
5908 void mail_free_searchpgm (SEARCHPGM **pgm)
|
yuuji@0
|
5909 {
|
yuuji@0
|
5910 if (*pgm) { /* only free if exists */
|
yuuji@0
|
5911 mail_free_searchset (&(*pgm)->msgno);
|
yuuji@0
|
5912 mail_free_searchset (&(*pgm)->uid);
|
yuuji@0
|
5913 mail_free_searchor (&(*pgm)->or);
|
yuuji@0
|
5914 mail_free_searchpgmlist (&(*pgm)->not);
|
yuuji@0
|
5915 mail_free_searchheader (&(*pgm)->header);
|
yuuji@0
|
5916 mail_free_stringlist (&(*pgm)->bcc);
|
yuuji@0
|
5917 mail_free_stringlist (&(*pgm)->body);
|
yuuji@0
|
5918 mail_free_stringlist (&(*pgm)->cc);
|
yuuji@0
|
5919 mail_free_stringlist (&(*pgm)->from);
|
yuuji@0
|
5920 mail_free_stringlist (&(*pgm)->keyword);
|
yuuji@0
|
5921 mail_free_stringlist (&(*pgm)->subject);
|
yuuji@0
|
5922 mail_free_stringlist (&(*pgm)->text);
|
yuuji@0
|
5923 mail_free_stringlist (&(*pgm)->to);
|
yuuji@0
|
5924 fs_give ((void **) pgm); /* return program to free storage */
|
yuuji@0
|
5925 }
|
yuuji@0
|
5926 }
|
yuuji@0
|
5927
|
yuuji@0
|
5928
|
yuuji@0
|
5929 /* Mail garbage collect searchheader
|
yuuji@0
|
5930 * Accepts: pointer to searchheader pointer
|
yuuji@0
|
5931 */
|
yuuji@0
|
5932
|
yuuji@0
|
5933 void mail_free_searchheader (SEARCHHEADER **hdr)
|
yuuji@0
|
5934 {
|
yuuji@0
|
5935 if (*hdr) { /* only free if exists */
|
yuuji@0
|
5936 if ((*hdr)->line.data) fs_give ((void **) &(*hdr)->line.data);
|
yuuji@0
|
5937 if ((*hdr)->text.data) fs_give ((void **) &(*hdr)->text.data);
|
yuuji@0
|
5938 mail_free_searchheader (&(*hdr)->next);
|
yuuji@0
|
5939 fs_give ((void **) hdr); /* return header to free storage */
|
yuuji@0
|
5940 }
|
yuuji@0
|
5941 }
|
yuuji@0
|
5942
|
yuuji@0
|
5943
|
yuuji@0
|
5944 /* Mail garbage collect searchset
|
yuuji@0
|
5945 * Accepts: pointer to searchset pointer
|
yuuji@0
|
5946 */
|
yuuji@0
|
5947
|
yuuji@0
|
5948 void mail_free_searchset (SEARCHSET **set)
|
yuuji@0
|
5949 {
|
yuuji@0
|
5950 if (*set) { /* only free if exists */
|
yuuji@0
|
5951 mail_free_searchset (&(*set)->next);
|
yuuji@0
|
5952 fs_give ((void **) set); /* return set to free storage */
|
yuuji@0
|
5953 }
|
yuuji@0
|
5954 }
|
yuuji@0
|
5955
|
yuuji@0
|
5956 /* Mail garbage collect searchor
|
yuuji@0
|
5957 * Accepts: pointer to searchor pointer
|
yuuji@0
|
5958 */
|
yuuji@0
|
5959
|
yuuji@0
|
5960 void mail_free_searchor (SEARCHOR **orl)
|
yuuji@0
|
5961 {
|
yuuji@0
|
5962 if (*orl) { /* only free if exists */
|
yuuji@0
|
5963 mail_free_searchpgm (&(*orl)->first);
|
yuuji@0
|
5964 mail_free_searchpgm (&(*orl)->second);
|
yuuji@0
|
5965 mail_free_searchor (&(*orl)->next);
|
yuuji@0
|
5966 fs_give ((void **) orl); /* return searchor to free storage */
|
yuuji@0
|
5967 }
|
yuuji@0
|
5968 }
|
yuuji@0
|
5969
|
yuuji@0
|
5970
|
yuuji@0
|
5971 /* Mail garbage collect search program list
|
yuuji@0
|
5972 * Accepts: pointer to searchpgmlist pointer
|
yuuji@0
|
5973 */
|
yuuji@0
|
5974
|
yuuji@0
|
5975 void mail_free_searchpgmlist (SEARCHPGMLIST **pgl)
|
yuuji@0
|
5976 {
|
yuuji@0
|
5977 if (*pgl) { /* only free if exists */
|
yuuji@0
|
5978 mail_free_searchpgm (&(*pgl)->pgm);
|
yuuji@0
|
5979 mail_free_searchpgmlist (&(*pgl)->next);
|
yuuji@0
|
5980 fs_give ((void **) pgl); /* return searchpgmlist to free storage */
|
yuuji@0
|
5981 }
|
yuuji@0
|
5982 }
|
yuuji@0
|
5983
|
yuuji@0
|
5984
|
yuuji@0
|
5985 /* Mail garbage collect namespace
|
yuuji@0
|
5986 * Accepts: poiner to namespace
|
yuuji@0
|
5987 */
|
yuuji@0
|
5988
|
yuuji@0
|
5989 void mail_free_namespace (NAMESPACE **n)
|
yuuji@0
|
5990 {
|
yuuji@0
|
5991 if (*n) {
|
yuuji@0
|
5992 fs_give ((void **) &(*n)->name);
|
yuuji@0
|
5993 mail_free_namespace (&(*n)->next);
|
yuuji@0
|
5994 mail_free_body_parameter (&(*n)->param);
|
yuuji@0
|
5995 fs_give ((void **) n); /* return namespace to free storage */
|
yuuji@0
|
5996 }
|
yuuji@0
|
5997 }
|
yuuji@0
|
5998
|
yuuji@0
|
5999 /* Mail garbage collect sort program
|
yuuji@0
|
6000 * Accepts: pointer to sortpgm pointer
|
yuuji@0
|
6001 */
|
yuuji@0
|
6002
|
yuuji@0
|
6003 void mail_free_sortpgm (SORTPGM **pgm)
|
yuuji@0
|
6004 {
|
yuuji@0
|
6005 if (*pgm) { /* only free if exists */
|
yuuji@0
|
6006 mail_free_sortpgm (&(*pgm)->next);
|
yuuji@0
|
6007 fs_give ((void **) pgm); /* return sortpgm to free storage */
|
yuuji@0
|
6008 }
|
yuuji@0
|
6009 }
|
yuuji@0
|
6010
|
yuuji@0
|
6011
|
yuuji@0
|
6012 /* Mail garbage collect thread node
|
yuuji@0
|
6013 * Accepts: pointer to threadnode pointer
|
yuuji@0
|
6014 */
|
yuuji@0
|
6015
|
yuuji@0
|
6016 void mail_free_threadnode (THREADNODE **thr)
|
yuuji@0
|
6017 {
|
yuuji@0
|
6018 if (*thr) { /* only free if exists */
|
yuuji@0
|
6019 mail_free_threadnode (&(*thr)->branch);
|
yuuji@0
|
6020 mail_free_threadnode (&(*thr)->next);
|
yuuji@0
|
6021 fs_give ((void **) thr); /* return threadnode to free storage */
|
yuuji@0
|
6022 }
|
yuuji@0
|
6023 }
|
yuuji@0
|
6024
|
yuuji@0
|
6025
|
yuuji@0
|
6026 /* Mail garbage collect acllist
|
yuuji@0
|
6027 * Accepts: pointer to acllist pointer
|
yuuji@0
|
6028 */
|
yuuji@0
|
6029
|
yuuji@0
|
6030 void mail_free_acllist (ACLLIST **al)
|
yuuji@0
|
6031 {
|
yuuji@0
|
6032 if (*al) { /* only free if exists */
|
yuuji@0
|
6033 if ((*al)->identifier) fs_give ((void **) &(*al)->identifier);
|
yuuji@0
|
6034 if ((*al)->rights) fs_give ((void **) &(*al)->rights);
|
yuuji@0
|
6035 mail_free_acllist (&(*al)->next);
|
yuuji@0
|
6036 fs_give ((void **) al); /* return acllist to free storage */
|
yuuji@0
|
6037 }
|
yuuji@0
|
6038 }
|
yuuji@0
|
6039
|
yuuji@0
|
6040
|
yuuji@0
|
6041 /* Mail garbage collect quotalist
|
yuuji@0
|
6042 * Accepts: pointer to quotalist pointer
|
yuuji@0
|
6043 */
|
yuuji@0
|
6044
|
yuuji@0
|
6045 void mail_free_quotalist (QUOTALIST **ql)
|
yuuji@0
|
6046 {
|
yuuji@0
|
6047 if (*ql) { /* only free if exists */
|
yuuji@0
|
6048 if ((*ql)->name) fs_give ((void **) &(*ql)->name);
|
yuuji@0
|
6049 mail_free_quotalist (&(*ql)->next);
|
yuuji@0
|
6050 fs_give ((void **) ql); /* return quotalist to free storage */
|
yuuji@0
|
6051 }
|
yuuji@0
|
6052 }
|
yuuji@0
|
6053
|
yuuji@0
|
6054 /* Link authenicator
|
yuuji@0
|
6055 * Accepts: authenticator to add to list
|
yuuji@0
|
6056 */
|
yuuji@0
|
6057
|
yuuji@0
|
6058 void auth_link (AUTHENTICATOR *auth)
|
yuuji@0
|
6059 {
|
yuuji@0
|
6060 if (!auth->valid || (*auth->valid) ()) {
|
yuuji@0
|
6061 AUTHENTICATOR **a = &mailauthenticators;
|
yuuji@0
|
6062 while (*a) a = &(*a)->next; /* find end of list of authenticators */
|
yuuji@0
|
6063 *a = auth; /* put authenticator at the end */
|
yuuji@0
|
6064 auth->next = NIL; /* this authenticator is the end of the list */
|
yuuji@0
|
6065 }
|
yuuji@0
|
6066 }
|
yuuji@0
|
6067
|
yuuji@0
|
6068
|
yuuji@0
|
6069 /* Authenticate access
|
yuuji@0
|
6070 * Accepts: mechanism name
|
yuuji@0
|
6071 * responder function
|
yuuji@0
|
6072 * argument count
|
yuuji@0
|
6073 * argument vector
|
yuuji@0
|
6074 * Returns: authenticated user name or NIL
|
yuuji@0
|
6075 */
|
yuuji@0
|
6076
|
yuuji@0
|
6077 char *mail_auth (char *mechanism,authresponse_t resp,int argc,char *argv[])
|
yuuji@0
|
6078 {
|
yuuji@0
|
6079 AUTHENTICATOR *auth;
|
yuuji@0
|
6080 for (auth = mailauthenticators; auth; auth = auth->next)
|
yuuji@0
|
6081 if (auth->server && !compare_cstring (auth->name,mechanism))
|
yuuji@0
|
6082 return (!(auth->flags & AU_DISABLE) &&
|
yuuji@0
|
6083 ((auth->flags & AU_SECURE) ||
|
yuuji@4
|
6084 #ifdef QMAIL
|
yuuji@4
|
6085 getenv("INTRANET") ||
|
yuuji@4
|
6086 #endif
|
yuuji@0
|
6087 !mail_parameters (NIL,GET_DISABLEPLAINTEXT,NIL))) ?
|
yuuji@0
|
6088 (*auth->server) (resp,argc,argv) : NIL;
|
yuuji@0
|
6089 return NIL; /* no authenticator found */
|
yuuji@0
|
6090 }
|
yuuji@0
|
6091
|
yuuji@0
|
6092 /* Lookup authenticator index
|
yuuji@0
|
6093 * Accepts: authenticator index
|
yuuji@0
|
6094 * Returns: authenticator, or 0 if not found
|
yuuji@0
|
6095 */
|
yuuji@0
|
6096
|
yuuji@0
|
6097 AUTHENTICATOR *mail_lookup_auth (unsigned long i)
|
yuuji@0
|
6098 {
|
yuuji@0
|
6099 AUTHENTICATOR *auth = mailauthenticators;
|
yuuji@0
|
6100 while (auth && --i) auth = auth->next;
|
yuuji@0
|
6101 return auth;
|
yuuji@0
|
6102 }
|
yuuji@0
|
6103
|
yuuji@0
|
6104
|
yuuji@0
|
6105 /* Lookup authenticator name
|
yuuji@0
|
6106 * Accepts: authenticator name
|
yuuji@0
|
6107 * required authenticator flags
|
yuuji@0
|
6108 * Returns: index in authenticator chain, or 0 if not found
|
yuuji@0
|
6109 */
|
yuuji@0
|
6110
|
yuuji@0
|
6111 unsigned int mail_lookup_auth_name (char *mechanism,long flags)
|
yuuji@0
|
6112 {
|
yuuji@0
|
6113 int i;
|
yuuji@0
|
6114 AUTHENTICATOR *auth;
|
yuuji@0
|
6115 for (i = 1, auth = mailauthenticators; auth; i++, auth = auth->next)
|
yuuji@0
|
6116 if (auth->client && !(flags & ~auth->flags) &&
|
yuuji@0
|
6117 !(auth->flags & AU_DISABLE) && !compare_cstring (auth->name,mechanism))
|
yuuji@0
|
6118 return i;
|
yuuji@0
|
6119 return 0;
|
yuuji@0
|
6120 }
|
yuuji@0
|
6121
|
yuuji@0
|
6122 /* Standard TCP/IP network driver */
|
yuuji@0
|
6123
|
yuuji@0
|
6124 static NETDRIVER tcpdriver = {
|
yuuji@0
|
6125 tcp_open, /* open connection */
|
yuuji@0
|
6126 tcp_aopen, /* open preauthenticated connection */
|
yuuji@0
|
6127 tcp_getline, /* get a line */
|
yuuji@0
|
6128 tcp_getbuffer, /* get a buffer */
|
yuuji@0
|
6129 tcp_soutr, /* output pushed data */
|
yuuji@0
|
6130 tcp_sout, /* output string */
|
yuuji@0
|
6131 tcp_close, /* close connection */
|
yuuji@0
|
6132 tcp_host, /* return host name */
|
yuuji@0
|
6133 tcp_remotehost, /* return remote host name */
|
yuuji@0
|
6134 tcp_port, /* return port number */
|
yuuji@0
|
6135 tcp_localhost /* return local host name */
|
yuuji@0
|
6136 };
|
yuuji@0
|
6137
|
yuuji@0
|
6138
|
yuuji@0
|
6139 /* Network open
|
yuuji@0
|
6140 * Accepts: NETMBX specifier to open
|
yuuji@0
|
6141 * default network driver
|
yuuji@0
|
6142 * default port
|
yuuji@0
|
6143 * SSL driver
|
yuuji@0
|
6144 * SSL service name
|
yuuji@0
|
6145 * SSL driver port
|
yuuji@0
|
6146 * Returns: Network stream if success, else NIL
|
yuuji@0
|
6147 */
|
yuuji@0
|
6148
|
yuuji@0
|
6149 NETSTREAM *net_open (NETMBX *mb,NETDRIVER *dv,unsigned long port,
|
yuuji@0
|
6150 NETDRIVER *ssld,char *ssls,unsigned long sslp)
|
yuuji@0
|
6151 {
|
yuuji@0
|
6152 NETSTREAM *stream = NIL;
|
yuuji@0
|
6153 char tmp[MAILTMPLEN];
|
yuuji@0
|
6154 unsigned long flags = mb->novalidate ? NET_NOVALIDATECERT : 0;
|
yuuji@0
|
6155 if (strlen (mb->host) >= NETMAXHOST) {
|
yuuji@0
|
6156 sprintf (tmp,"Invalid host name: %.80s",mb->host);
|
yuuji@0
|
6157 MM_LOG (tmp,ERROR);
|
yuuji@0
|
6158 }
|
yuuji@0
|
6159 /* use designated driver if given */
|
yuuji@0
|
6160 else if (dv) stream = net_open_work (dv,mb->host,mb->service,port,mb->port,
|
yuuji@0
|
6161 flags);
|
yuuji@0
|
6162 else if (mb->sslflag && ssld) /* use ssl if sslflag lit */
|
yuuji@0
|
6163 stream = net_open_work (ssld,mb->host,ssls,sslp,mb->port,flags);
|
yuuji@0
|
6164 /* if trysslfirst and can open ssl... */
|
yuuji@0
|
6165 else if ((mb->trysslflag || trysslfirst) && ssld &&
|
yuuji@0
|
6166 (stream = net_open_work (ssld,mb->host,ssls,sslp,mb->port,
|
yuuji@0
|
6167 flags | NET_SILENT | NET_TRYSSL))) {
|
yuuji@0
|
6168 if (net_sout (stream,"",0)) mb->sslflag = T;
|
yuuji@0
|
6169 else {
|
yuuji@0
|
6170 net_close (stream); /* flush fake SSL stream */
|
yuuji@0
|
6171 stream = NIL;
|
yuuji@0
|
6172 }
|
yuuji@0
|
6173 }
|
yuuji@0
|
6174 /* default to TCP driver */
|
yuuji@0
|
6175 else stream = net_open_work (&tcpdriver,mb->host,mb->service,port,mb->port,
|
yuuji@0
|
6176 flags);
|
yuuji@0
|
6177 return stream;
|
yuuji@0
|
6178 }
|
yuuji@0
|
6179
|
yuuji@0
|
6180 /* Network open worker routine
|
yuuji@0
|
6181 * Accepts: network driver
|
yuuji@0
|
6182 * host name
|
yuuji@0
|
6183 * service name to look up port
|
yuuji@0
|
6184 * port number if service name not found
|
yuuji@0
|
6185 * port number to override service name
|
yuuji@0
|
6186 * flags (passed on top of port)
|
yuuji@0
|
6187 * Returns: Network stream if success, else NIL
|
yuuji@0
|
6188 */
|
yuuji@0
|
6189
|
yuuji@0
|
6190 NETSTREAM *net_open_work (NETDRIVER *dv,char *host,char *service,
|
yuuji@0
|
6191 unsigned long port,unsigned long portoverride,
|
yuuji@0
|
6192 unsigned long flags)
|
yuuji@0
|
6193 {
|
yuuji@0
|
6194 NETSTREAM *stream = NIL;
|
yuuji@0
|
6195 void *tstream;
|
yuuji@0
|
6196 if (service && (*service == '*')) {
|
yuuji@0
|
6197 flags |= NET_NOOPENTIMEOUT; /* mark that no timeout is desired */
|
yuuji@0
|
6198 ++service; /* no longer need the no timeout indicator */
|
yuuji@0
|
6199 }
|
yuuji@0
|
6200 if (portoverride) { /* explicit port number? */
|
yuuji@0
|
6201 service = NIL; /* yes, override service name */
|
yuuji@0
|
6202 port = portoverride; /* use that instead of default port */
|
yuuji@0
|
6203 }
|
yuuji@0
|
6204 if (tstream = (*dv->open) (host,service,port | flags)) {
|
yuuji@0
|
6205 stream = (NETSTREAM *) fs_get (sizeof (NETSTREAM));
|
yuuji@0
|
6206 stream->stream = tstream;
|
yuuji@0
|
6207 stream->dtb = dv;
|
yuuji@0
|
6208 }
|
yuuji@0
|
6209 return stream;
|
yuuji@0
|
6210 }
|
yuuji@0
|
6211
|
yuuji@0
|
6212
|
yuuji@0
|
6213 /* Network authenticated open
|
yuuji@0
|
6214 * Accepts: network driver
|
yuuji@0
|
6215 * NETMBX specifier
|
yuuji@0
|
6216 * service specifier
|
yuuji@0
|
6217 * return user name buffer
|
yuuji@0
|
6218 * Returns: Network stream if success else NIL
|
yuuji@0
|
6219 */
|
yuuji@0
|
6220
|
yuuji@0
|
6221 NETSTREAM *net_aopen (NETDRIVER *dv,NETMBX *mb,char *service,char *user)
|
yuuji@0
|
6222 {
|
yuuji@0
|
6223 NETSTREAM *stream = NIL;
|
yuuji@0
|
6224 void *tstream;
|
yuuji@0
|
6225 if (!dv) dv = &tcpdriver; /* default to TCP driver */
|
yuuji@0
|
6226 if (tstream = (*dv->aopen) (mb,service,user)) {
|
yuuji@0
|
6227 stream = (NETSTREAM *) fs_get (sizeof (NETSTREAM));
|
yuuji@0
|
6228 stream->stream = tstream;
|
yuuji@0
|
6229 stream->dtb = dv;
|
yuuji@0
|
6230 }
|
yuuji@0
|
6231 return stream;
|
yuuji@0
|
6232 }
|
yuuji@0
|
6233
|
yuuji@0
|
6234 /* Network receive line
|
yuuji@0
|
6235 * Accepts: Network stream
|
yuuji@0
|
6236 * Returns: text line string or NIL if failure
|
yuuji@0
|
6237 */
|
yuuji@0
|
6238
|
yuuji@0
|
6239 char *net_getline (NETSTREAM *stream)
|
yuuji@0
|
6240 {
|
yuuji@0
|
6241 return (*stream->dtb->getline) (stream->stream);
|
yuuji@0
|
6242 }
|
yuuji@0
|
6243
|
yuuji@0
|
6244
|
yuuji@0
|
6245 /* Network receive buffer
|
yuuji@0
|
6246 * Accepts: Network stream (must be void * for use as readfn_t)
|
yuuji@0
|
6247 * size in bytes
|
yuuji@0
|
6248 * buffer to read into
|
yuuji@0
|
6249 * Returns: T if success, NIL otherwise
|
yuuji@0
|
6250 */
|
yuuji@0
|
6251
|
yuuji@0
|
6252 long net_getbuffer (void *st,unsigned long size,char *buffer)
|
yuuji@0
|
6253 {
|
yuuji@0
|
6254 NETSTREAM *stream = (NETSTREAM *) st;
|
yuuji@0
|
6255 return (*stream->dtb->getbuffer) (stream->stream,size,buffer);
|
yuuji@0
|
6256 }
|
yuuji@0
|
6257
|
yuuji@0
|
6258
|
yuuji@0
|
6259 /* Network send null-terminated string
|
yuuji@0
|
6260 * Accepts: Network stream
|
yuuji@0
|
6261 * string pointer
|
yuuji@0
|
6262 * Returns: T if success else NIL
|
yuuji@0
|
6263 */
|
yuuji@0
|
6264
|
yuuji@0
|
6265 long net_soutr (NETSTREAM *stream,char *string)
|
yuuji@0
|
6266 {
|
yuuji@0
|
6267 return (*stream->dtb->soutr) (stream->stream,string);
|
yuuji@0
|
6268 }
|
yuuji@0
|
6269
|
yuuji@0
|
6270
|
yuuji@0
|
6271 /* Network send string
|
yuuji@0
|
6272 * Accepts: Network stream
|
yuuji@0
|
6273 * string pointer
|
yuuji@0
|
6274 * byte count
|
yuuji@0
|
6275 * Returns: T if success else NIL
|
yuuji@0
|
6276 */
|
yuuji@0
|
6277
|
yuuji@0
|
6278 long net_sout (NETSTREAM *stream,char *string,unsigned long size)
|
yuuji@0
|
6279 {
|
yuuji@0
|
6280 return (*stream->dtb->sout) (stream->stream,string,size);
|
yuuji@0
|
6281 }
|
yuuji@0
|
6282
|
yuuji@0
|
6283 /* Network close
|
yuuji@0
|
6284 * Accepts: Network stream
|
yuuji@0
|
6285 */
|
yuuji@0
|
6286
|
yuuji@0
|
6287 void net_close (NETSTREAM *stream)
|
yuuji@0
|
6288 {
|
yuuji@0
|
6289 if (stream->stream) (*stream->dtb->close) (stream->stream);
|
yuuji@0
|
6290 fs_give ((void **) &stream);
|
yuuji@0
|
6291 }
|
yuuji@0
|
6292
|
yuuji@0
|
6293
|
yuuji@0
|
6294 /* Network get host name
|
yuuji@0
|
6295 * Accepts: Network stream
|
yuuji@0
|
6296 * Returns: host name for this stream
|
yuuji@0
|
6297 */
|
yuuji@0
|
6298
|
yuuji@0
|
6299 char *net_host (NETSTREAM *stream)
|
yuuji@0
|
6300 {
|
yuuji@0
|
6301 return (*stream->dtb->host) (stream->stream);
|
yuuji@0
|
6302 }
|
yuuji@0
|
6303
|
yuuji@0
|
6304
|
yuuji@0
|
6305 /* Network get remote host name
|
yuuji@0
|
6306 * Accepts: Network stream
|
yuuji@0
|
6307 * Returns: host name for this stream
|
yuuji@0
|
6308 */
|
yuuji@0
|
6309
|
yuuji@0
|
6310 char *net_remotehost (NETSTREAM *stream)
|
yuuji@0
|
6311 {
|
yuuji@0
|
6312 return (*stream->dtb->remotehost) (stream->stream);
|
yuuji@0
|
6313 }
|
yuuji@0
|
6314
|
yuuji@0
|
6315 /* Network return port for this stream
|
yuuji@0
|
6316 * Accepts: Network stream
|
yuuji@0
|
6317 * Returns: port number for this stream
|
yuuji@0
|
6318 */
|
yuuji@0
|
6319
|
yuuji@0
|
6320 unsigned long net_port (NETSTREAM *stream)
|
yuuji@0
|
6321 {
|
yuuji@0
|
6322 return (*stream->dtb->port) (stream->stream);
|
yuuji@0
|
6323 }
|
yuuji@0
|
6324
|
yuuji@0
|
6325
|
yuuji@0
|
6326 /* Network get local host name
|
yuuji@0
|
6327 * Accepts: Network stream
|
yuuji@0
|
6328 * Returns: local host name
|
yuuji@0
|
6329 */
|
yuuji@0
|
6330
|
yuuji@0
|
6331 char *net_localhost (NETSTREAM *stream)
|
yuuji@0
|
6332 {
|
yuuji@0
|
6333 return (*stream->dtb->localhost) (stream->stream);
|
yuuji@0
|
6334 }
|