1 /* ========================================================================
2 * Copyright 2008-2010 Mark Crispin
3 * ========================================================================
7 * Program: Mailbox Access routines
11 * Date: 22 November 1989
12 * Last Edited: 15 November 2010
14 * Previous versions of this file were
16 * Copyright 1988-2008 University of Washington
18 * Licensed under the Apache License, Version 2.0 (the "License");
19 * you may not use this file except in compliance with the License.
20 * You may obtain a copy of the License at
22 * http://www.apache.org/licenses/LICENSE-2.0
32 char *Panda_copyright
= "Copyright 2008-2010 Mark Crispin\n";
34 char *UW_copyright
= "Copyright 1988-2008 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";
36 /* c-client global data */
37 /* version of this library */
38 static char *mailcclientversion
= CCLIENTVERSION
;
40 static IDLIST
*idapp
= NIL
;
41 /* list of mail drivers */
42 static DRIVER
*maildrivers
= NIL
;
43 /* list of authenticators */
44 static AUTHENTICATOR
*mailauthenticators
= NIL
;
45 /* SSL driver pointer */
46 static NETDRIVER
*mailssldriver
= NIL
;
47 /* pointer to alternate gets function */
48 static mailgets_t mailgets
= NIL
;
49 /* pointer to read progress function */
50 static readprogress_t mailreadprogress
= NIL
;
51 /* mail cache manipulation function */
52 static mailcache_t mailcache
= mm_cache
;
53 /* RFC-822 output generator */
54 static rfc822out_t mail822out
= NIL
;
55 /* RFC-822 output generator (new style) */
56 static rfc822outfull_t mail822outfull
= NIL
;
57 /* SMTP verbose callback */
58 static smtpverbose_t mailsmtpverbose
= mm_dlog
;
59 /* proxy copy routine */
60 static mailproxycopy_t mailproxycopy
= NIL
;
61 /* RFC-822 external line parse */
62 static parseline_t mailparseline
= NIL
;
63 /* RFC-822 external phrase parser */
64 static parsephrase_t mailparsephrase
= NIL
;
65 static kinit_t mailkinit
= NIL
; /* application kinit callback */
66 /* note network sent command */
67 static sendcommand_t mailsendcommand
= NIL
;
68 /* newsrc file name decision function */
69 static newsrcquery_t mailnewsrcquery
= NIL
;
70 /* ACL results callback */
71 static getacl_t mailaclresults
= NIL
;
72 /* list rights results callback */
73 static listrights_t maillistrightsresults
= NIL
;
74 /* my rights results callback */
75 static myrights_t mailmyrightsresults
= NIL
;
76 /* quota results callback */
77 static quota_t mailquotaresults
= NIL
;
78 /* quota root results callback */
79 static quotaroot_t mailquotarootresults
= NIL
;
80 /* sorted results callback */
81 static sortresults_t mailsortresults
= NIL
;
82 /* threaded results callback */
83 static threadresults_t mailthreadresults
= NIL
;
84 /* COPY UID results */
85 static copyuid_t mailcopyuid
= NIL
;
86 /* APPEND UID results */
87 static appenduid_t mailappenduid
= NIL
;
88 /* free elt extra stuff callback */
89 static freeeltsparep_t mailfreeeltsparep
= NIL
;
90 /* free envelope extra stuff callback */
91 static freeenvelopesparep_t mailfreeenvelopesparep
= NIL
;
92 /* free body extra stuff callback */
93 static freebodysparep_t mailfreebodysparep
= NIL
;
94 /* free stream extra stuff callback */
95 static freestreamsparep_t mailfreestreamsparep
= NIL
;
96 /* SSL start routine */
97 static sslstart_t mailsslstart
= NIL
;
98 /* SSL certificate query */
99 static sslcertificatequery_t mailsslcertificatequery
= NIL
;
100 /* SSL client certificate */
101 static sslclientcert_t mailsslclientcert
= NIL
;
102 /* SSL client private key */
103 static sslclientkey_t mailsslclientkey
= NIL
;
104 /* SSL failure notify */
105 static sslfailure_t mailsslfailure
= NIL
;
107 static long mailsnarfinterval
= 60;
108 /* snarf preservation */
109 static long mailsnarfpreserve
= NIL
;
110 /* newsrc name uses canonical host */
111 static long mailnewsrccanon
= LONGT
;
113 /* supported threaders */
114 static THREADER mailthreadordsub
= {
115 "ORDEREDSUBJECT",mail_thread_orderedsubject
,NIL
117 static THREADER mailthreadlist
= {
118 "REFERENCES",mail_thread_references
,&mailthreadordsub
122 static char *servicename
= "unknown";
123 /* server externally-set authentication ID */
124 static char *externalauthid
= NIL
;
125 static int expungeatping
= T
; /* mail_ping() may call mm_expunged() */
126 static int trysslfirst
= NIL
; /* always try SSL first */
127 static int notimezones
= NIL
; /* write timezones in "From " header */
128 static int trustdns
= T
; /* do DNS canonicalization */
129 static int saslusesptrname
= T
; /* SASL uses name from DNS PTR lookup */
130 /* trustdns also must be set */
131 static int debugsensitive
= NIL
;/* debug telemetry includes sensitive data */
133 /* Default mail cache handler
134 * Accepts: pointer to cache handle
137 * Returns: cache data
140 void *mm_cache (MAILSTREAM
*stream
,unsigned long msgno
,long op
)
145 switch ((int) op
) { /* what function? */
146 case CH_INIT
: /* initialize cache */
147 if (stream
->cache
) { /* flush old cache contents */
148 while (stream
->cachesize
) {
149 mm_cache (stream
,stream
->cachesize
,CH_FREE
);
150 mm_cache (stream
,stream
->cachesize
--,CH_FREESORTCACHE
);
152 fs_give ((void **) &stream
->cache
);
153 fs_give ((void **) &stream
->sc
);
154 stream
->nmsgs
= 0; /* can't have any messages now */
157 case CH_SIZE
: /* (re-)size the cache */
158 if (!stream
->cache
) { /* have a cache already? */
159 /* no, create new cache */
160 n
= (stream
->cachesize
= msgno
+ CACHEINCREMENT
) * sizeof (void *);
161 stream
->cache
= (MESSAGECACHE
**) memset (fs_get (n
),0,n
);
162 stream
->sc
= (SORTCACHE
**) memset (fs_get (n
),0,n
);
164 /* is existing cache size large neough */
165 else if (msgno
> stream
->cachesize
) {
166 i
= stream
->cachesize
; /* remember old size */
167 n
= (stream
->cachesize
= msgno
+ CACHEINCREMENT
) * sizeof (void *);
168 fs_resize ((void **) &stream
->cache
,n
);
169 fs_resize ((void **) &stream
->sc
,n
);
170 while (i
< stream
->cachesize
) {
171 stream
->cache
[i
] = NIL
;
172 stream
->sc
[i
++] = NIL
;
177 case CH_MAKEELT
: /* return elt, make if necessary */
178 if (!stream
->cache
[msgno
- 1])
179 stream
->cache
[msgno
- 1] = mail_new_cache_elt (msgno
);
181 case CH_ELT
: /* return elt */
182 ret
= (void *) stream
->cache
[msgno
- 1];
184 case CH_SORTCACHE
: /* return sortcache entry, make if needed */
185 if (!stream
->sc
[msgno
- 1]) stream
->sc
[msgno
- 1] =
186 (SORTCACHE
*) memset (fs_get (sizeof (SORTCACHE
)),0,sizeof (SORTCACHE
));
187 ret
= (void *) stream
->sc
[msgno
- 1];
189 case CH_FREE
: /* free elt */
190 mail_free_elt (&stream
->cache
[msgno
- 1]);
192 case CH_FREESORTCACHE
:
193 if (stream
->sc
[msgno
- 1]) {
194 if (stream
->sc
[msgno
- 1]->from
)
195 fs_give ((void **) &stream
->sc
[msgno
- 1]->from
);
196 if (stream
->sc
[msgno
- 1]->to
)
197 fs_give ((void **) &stream
->sc
[msgno
- 1]->to
);
198 if (stream
->sc
[msgno
- 1]->cc
)
199 fs_give ((void **) &stream
->sc
[msgno
- 1]->cc
);
200 if (stream
->sc
[msgno
- 1]->subject
)
201 fs_give ((void **) &stream
->sc
[msgno
- 1]->subject
);
202 if (stream
->sc
[msgno
- 1]->unique
&&
203 (stream
->sc
[msgno
- 1]->unique
!= stream
->sc
[msgno
- 1]->message_id
))
204 fs_give ((void **) &stream
->sc
[msgno
- 1]->unique
);
205 if (stream
->sc
[msgno
- 1]->message_id
)
206 fs_give ((void **) &stream
->sc
[msgno
- 1]->message_id
);
207 if (stream
->sc
[msgno
- 1]->references
)
208 mail_free_stringlist (&stream
->sc
[msgno
- 1]->references
);
209 fs_give ((void **) &stream
->sc
[msgno
- 1]);
212 case CH_EXPUNGE
: /* expunge cache slot */
213 for (i
= msgno
- 1; msgno
< stream
->nmsgs
; i
++,msgno
++) {
214 if ((stream
->cache
[i
] = stream
->cache
[msgno
]) != NULL
)
215 stream
->cache
[i
]->msgno
= msgno
;
216 stream
->sc
[i
] = stream
->sc
[msgno
];
218 stream
->cache
[i
] = NIL
; /* top of cache goes away */
222 fatal ("Bad mm_cache op");
228 /* Dummy string driver for complete in-memory strings */
230 static void mail_string_init (STRING
*s
,void *data
,unsigned long size
);
231 static char mail_string_next (STRING
*s
);
232 static void mail_string_setpos (STRING
*s
,unsigned long i
);
234 STRINGDRIVER mail_string
= {
235 mail_string_init
, /* initialize string structure */
236 mail_string_next
, /* get next byte in string structure */
237 mail_string_setpos
/* set position in string structure */
241 /* Initialize mail string structure for in-memory string
242 * Accepts: string structure
247 static void mail_string_init (STRING
*s
,void *data
,unsigned long size
)
249 /* set initial string pointers */
250 s
->chunk
= s
->curpos
= (char *) (s
->data
= data
);
252 s
->size
= s
->chunksize
= s
->cursize
= size
;
253 s
->data1
= s
->offset
= 0; /* never any offset */
257 /* Get next character from string
258 * Accepts: string structure
259 * Returns: character, string structure chunk refreshed
262 static char mail_string_next (STRING
*s
)
264 return *s
->curpos
++; /* return the last byte */
268 /* Set string pointer position
269 * Accepts: string structure
273 static void mail_string_setpos (STRING
*s
,unsigned long i
)
275 s
->curpos
= s
->chunk
+ i
; /* set new position */
276 s
->cursize
= s
->chunksize
- i
;/* and new size */
281 * mail_xxx routines are the interface between this module and the outside
282 * world. Only these routines should be referenced by external callers.
284 * Note that there is an important difference between a "sequence" and a
285 * "message #" (msgno). A sequence is a string representing a sequence in
286 * {"n", "n:m", or combination separated by commas} format, whereas a msgno
287 * is a single integer.
291 /* Mail version check
295 void mail_versioncheck (char *version
)
297 /* attempt to protect again wrong .h */
298 if (strcmp (version
,mailcclientversion
)) {
299 char tmp
[MAILTMPLEN
];
300 sprintf (tmp
,"c-client library version skew, app=%.100s library=%.100s",
301 version
,mailcclientversion
);
308 * Accepts: driver to add to list
311 void mail_link (DRIVER
*driver
)
313 DRIVER
**d
= &maildrivers
;
314 while (*d
) d
= &(*d
)->next
; /* find end of list of drivers */
315 *d
= driver
; /* put driver at the end */
316 driver
->next
= NIL
; /* this driver is the end of the list */
319 /* Mail manipulate driver parameters
320 * Accepts: mail stream
322 * function-dependent value
323 * Returns: function-dependent return value
326 void *mail_parameters (MAILSTREAM
*stream
,long function
,void *value
)
331 switch ((int) function
) {
333 fatal ("SET_INBOXPATH not permitted");
335 if ((stream
|| (stream
= mail_open (NIL
,"INBOX",OP_PROTOTYPE
))) &&
336 stream
->dtb
) ret
= (*stream
->dtb
->parameters
) (function
,value
);
339 fatal ("SET_THREADERS not permitted");
340 case GET_THREADERS
: /* use stream dtb instead of global */
341 ret
= (stream
&& stream
->dtb
) ?
342 /* KLUDGE ALERT: note stream passed as value */
343 (*stream
->dtb
->parameters
) (function
,stream
) : (void *) &mailthreadlist
;
346 fatal ("SET_NAMESPACE not permitted");
348 case SET_NEWSRC
: /* too late on open stream */
349 if (stream
&& stream
->dtb
&& (stream
!= ((*stream
->dtb
->open
) (NIL
))))
350 fatal ("SET_NEWSRC not permitted");
351 else ret
= env_parameters (function
,value
);
354 ret
= (stream
&& stream
->dtb
&& !(stream
->dtb
->flags
& DR_LOCAL
)) ?
355 /* KLUDGE ALERT: note stream passed as value */
356 (*stream
->dtb
->parameters
) (function
,stream
) :
357 env_parameters (function
,value
);
359 case GET_NEWSRC
: /* use stream dtb instead of environment */
360 ret
= (stream
&& stream
->dtb
) ?
361 /* KLUDGE ALERT: note stream passed as value */
362 (*stream
->dtb
->parameters
) (function
,stream
) :
363 env_parameters (function
,value
);
366 fatal ("ENABLE_DEBUG not permitted");
368 fatal ("DISABLE_DEBUG not permitted");
370 fatal ("SET_DIRFMTTEST not permitted");
372 if (!(stream
&& stream
->dtb
&&
373 (ret
= (*stream
->dtb
->parameters
) (function
,NIL
))))
374 fatal ("GET_DIRFMTTEST not permitted");
378 fatal ("SET_DRIVERS not permitted");
379 case GET_DRIVERS
: /* always return global */
380 ret
= (void *) maildrivers
;
383 fatal ("SET_DRIVER not permitted");
385 for (d
= maildrivers
; d
&& compare_cstring (d
->name
,(char *) value
);
390 for (d
= maildrivers
; d
&& compare_cstring (d
->name
,(char *) value
);
392 if ((ret
= (void *) d
) != NULL
) d
->flags
&= ~DR_DISABLE
;
395 for (d
= maildrivers
; d
&& compare_cstring (d
->name
,(char *) value
);
397 if ((ret
= (void *) d
) != NULL
) d
->flags
|= DR_DISABLE
;
399 case ENABLE_AUTHENTICATOR
:
400 for (a
= mailauthenticators
;/* scan authenticators */
401 a
&& compare_cstring (a
->name
,(char *) value
); a
= a
->next
);
402 if ((ret
= (void *) a
) != NULL
) a
->flags
&= ~AU_DISABLE
;
404 case DISABLE_AUTHENTICATOR
:
405 for (a
= mailauthenticators
;/* scan authenticators */
406 a
&& compare_cstring (a
->name
,(char *) value
); a
= a
->next
);
407 if ((ret
= (void *) a
) != NULL
) a
->flags
|= AU_DISABLE
;
409 case UNHIDE_AUTHENTICATOR
:
410 for (a
= mailauthenticators
;/* scan authenticators */
411 a
&& compare_cstring (a
->name
,(char *) value
); a
= a
->next
);
412 if ((ret
= (void *) a
) != NULL
) a
->flags
&= ~AU_HIDE
;
414 case HIDE_AUTHENTICATOR
:
415 for (a
= mailauthenticators
;/* scan authenticators */
416 a
&& compare_cstring (a
->name
,(char *) value
); a
= a
->next
);
417 if ((ret
= (void *) a
) != NULL
) a
->flags
|= AU_HIDE
;
419 case SET_EXTERNALAUTHID
:
420 if (value
) { /* setting external authentication ID */
421 externalauthid
= cpystr ((char *) value
);
422 mail_parameters (NIL
,UNHIDE_AUTHENTICATOR
,"EXTERNAL");
424 else { /* clearing external authentication ID */
425 if (externalauthid
) fs_give ((void **) &externalauthid
);
426 mail_parameters (NIL
,HIDE_AUTHENTICATOR
,"EXTERNAL");
428 case GET_EXTERNALAUTHID
:
429 ret
= (void *) externalauthid
;
433 mailgets
= (mailgets_t
) value
;
435 ret
= (void *) mailgets
;
437 case SET_READPROGRESS
:
438 mailreadprogress
= (readprogress_t
) value
;
439 case GET_READPROGRESS
:
440 ret
= (void *) mailreadprogress
;
443 mailcache
= (mailcache_t
) value
;
445 ret
= (void *) mailcache
;
447 case SET_RFC822OUTPUT
:
448 mail822out
= (rfc822out_t
) value
;
449 case GET_RFC822OUTPUT
:
450 ret
= (void *) mail822out
;
452 case SET_RFC822OUTPUTFULL
:
453 mail822outfull
= (rfc822outfull_t
) value
;
454 case GET_RFC822OUTPUTFULL
:
455 ret
= (void *) mail822outfull
;
457 case SET_SMTPVERBOSE
:
458 mailsmtpverbose
= (smtpverbose_t
) value
;
459 case GET_SMTPVERBOSE
:
460 ret
= (void *) mailsmtpverbose
;
462 case SET_MAILPROXYCOPY
:
463 mailproxycopy
= (mailproxycopy_t
) value
;
464 case GET_MAILPROXYCOPY
:
465 ret
= (void *) mailproxycopy
;
468 mailparseline
= (parseline_t
) value
;
470 ret
= (void *) mailparseline
;
472 case SET_PARSEPHRASE
:
473 mailparsephrase
= (parsephrase_t
) value
;
474 case GET_PARSEPHRASE
:
475 ret
= (void *) mailparsephrase
;
477 case SET_NEWSRCQUERY
:
478 mailnewsrcquery
= (newsrcquery_t
) value
;
479 case GET_NEWSRCQUERY
:
480 ret
= (void *) mailnewsrcquery
;
482 case SET_NEWSRCCANONHOST
:
483 mailnewsrccanon
= (long) value
;
484 case GET_NEWSRCCANONHOST
:
485 ret
= (void *) mailnewsrccanon
;
489 mailcopyuid
= (copyuid_t
) value
;
491 ret
= (void *) mailcopyuid
;
494 mailappenduid
= (appenduid_t
) value
;
496 ret
= (void *) mailappenduid
;
498 case SET_FREEENVELOPESPAREP
:
499 mailfreeenvelopesparep
= (freeenvelopesparep_t
) value
;
500 case GET_FREEENVELOPESPAREP
:
501 ret
= (void *) mailfreeenvelopesparep
;
503 case SET_FREEELTSPAREP
:
504 mailfreeeltsparep
= (freeeltsparep_t
) value
;
505 case GET_FREEELTSPAREP
:
506 ret
= (void *) mailfreeeltsparep
;
508 case SET_FREESTREAMSPAREP
:
509 mailfreestreamsparep
= (freestreamsparep_t
) value
;
510 case GET_FREESTREAMSPAREP
:
511 ret
= (void *) mailfreestreamsparep
;
513 case SET_FREEBODYSPAREP
:
514 mailfreebodysparep
= (freebodysparep_t
) value
;
515 case GET_FREEBODYSPAREP
:
516 ret
= (void *) mailfreebodysparep
;
520 mailsslstart
= (sslstart_t
) value
;
522 ret
= (void *) mailsslstart
;
524 case SET_SSLCERTIFICATEQUERY
:
525 mailsslcertificatequery
= (sslcertificatequery_t
) value
;
526 case GET_SSLCERTIFICATEQUERY
:
527 ret
= (void *) mailsslcertificatequery
;
529 case SET_SSLCLIENTCERT
:
530 mailsslclientcert
= (sslclientcert_t
) value
;
531 case GET_SSLCLIENTCERT
:
532 ret
= (void *) mailsslclientcert
;
534 case SET_SSLCLIENTKEY
:
535 mailsslclientkey
= (sslclientkey_t
) value
;
536 case GET_SSLCLIENTKEY
:
537 ret
= (void *) mailsslclientkey
;
540 mailsslfailure
= (sslfailure_t
) value
;
542 ret
= (void *) mailsslfailure
;
545 mailkinit
= (kinit_t
) value
;
547 ret
= (void *) mailkinit
;
549 case SET_SENDCOMMAND
:
550 mailsendcommand
= (sendcommand_t
) value
;
551 case GET_SENDCOMMAND
:
552 ret
= (void *) mailsendcommand
;
555 case SET_SERVICENAME
:
556 servicename
= (char *) value
;
557 case GET_SERVICENAME
:
558 ret
= (void *) servicename
;
560 case SET_EXPUNGEATPING
:
561 expungeatping
= (value
? T
: NIL
);
562 case GET_EXPUNGEATPING
:
563 ret
= (void *) (expungeatping
? VOIDT
: NIL
);
565 case SET_SORTRESULTS
:
566 mailsortresults
= (sortresults_t
) value
;
567 case GET_SORTRESULTS
:
568 ret
= (void *) mailsortresults
;
570 case SET_THREADRESULTS
:
571 mailthreadresults
= (threadresults_t
) value
;
572 case GET_THREADRESULTS
:
573 ret
= (void *) mailthreadresults
;
576 mailssldriver
= (NETDRIVER
*) value
;
578 ret
= (void *) mailssldriver
;
580 case SET_TRYSSLFIRST
:
581 trysslfirst
= (value
? T
: NIL
);
582 case GET_TRYSSLFIRST
:
583 ret
= (void *) (trysslfirst
? VOIDT
: NIL
);
585 case SET_NOTIMEZONES
:
586 notimezones
= (value
? T
: NIL
);
587 case GET_NOTIMEZONES
:
588 ret
= (void *) (notimezones
? VOIDT
: NIL
);
591 trustdns
= (value
? T
: NIL
);
593 ret
= (void *) (trustdns
? VOIDT
: NIL
);
595 case SET_SASLUSESPTRNAME
:
596 saslusesptrname
= (value
? T
: NIL
);
597 case GET_SASLUSESPTRNAME
:
598 ret
= (void *) (saslusesptrname
? VOIDT
: NIL
);
600 case SET_DEBUGSENSITIVE
:
601 debugsensitive
= (value
? T
: NIL
);
602 case GET_DEBUGSENSITIVE
:
603 ret
= (void *) (debugsensitive
? VOIDT
: NIL
);
607 mailaclresults
= (getacl_t
) value
;
609 ret
= (void *) mailaclresults
;
612 maillistrightsresults
= (listrights_t
) value
;
614 ret
= (void *) maillistrightsresults
;
617 mailmyrightsresults
= (myrights_t
) value
;
619 ret
= (void *) mailmyrightsresults
;
622 mailquotaresults
= (quota_t
) value
;
624 ret
= (void *) mailquotaresults
;
627 mailquotarootresults
= (quotaroot_t
) value
;
629 ret
= (void *) mailquotarootresults
;
631 case SET_SNARFINTERVAL
:
632 mailsnarfinterval
= (long) value
;
633 case GET_SNARFINTERVAL
:
634 ret
= (void *) mailsnarfinterval
;
636 case SET_SNARFPRESERVE
:
637 mailsnarfpreserve
= (long) value
;
638 case GET_SNARFPRESERVE
:
639 ret
= (void *) mailsnarfpreserve
;
641 case SET_SNARFMAILBOXNAME
:
642 if (stream
) { /* have a stream? */
643 if (stream
->snarf
.name
) fs_give ((void **) &stream
->snarf
.name
);
644 stream
->snarf
.name
= cpystr ((char *) value
);
646 else fatal ("SET_SNARFMAILBOXNAME with no stream");
647 case GET_SNARFMAILBOXNAME
:
648 if (stream
) ret
= (void *) stream
->snarf
.name
;
650 case SET_IDPARAMS
: /* program id */
651 idapp
= (IDLIST
*) value
;
653 ret
= (void *) idapp
;
655 if ((r
= smtp_parameters (function
,value
)) != NULL
) ret
= r
;
656 if ((r
= env_parameters (function
,value
)) != NULL
) ret
= r
;
657 if ((r
= tcp_parameters (function
,value
)) != NULL
) ret
= r
;
658 if ((r
= utf8_parameters (function
,value
)) != NULL
) ret
= r
;
659 if (stream
&& stream
->dtb
) {/* if have stream, do for its driver only */
660 if ((r
= (*stream
->dtb
->parameters
) (function
,value
)) != NULL
) ret
= r
;
662 /* else do all drivers */
663 else for (d
= maildrivers
; d
; d
= d
->next
)
664 if ((r
= (d
->parameters
) (function
,value
)) != NULL
) ret
= r
;
670 /* Mail validate mailbox name
671 * Accepts: MAIL stream
673 * purpose string for error message
674 * Return: driver factory on success, NIL on failure
677 DRIVER
*mail_valid (MAILSTREAM
*stream
,char *mailbox
,char *purpose
)
679 char tmp
[MAILTMPLEN
];
680 DRIVER
*factory
= NIL
;
681 /* never allow names with newlines */
682 if (strpbrk (mailbox
,"\015\012")) {
683 if (purpose
) { /* if want an error message */
684 sprintf (tmp
,"Can't %s with such a name",purpose
);
689 /* validate name, find driver factory */
690 if (strlen (mailbox
) < (NETMAXHOST
+(NETMAXUSER
*2)+NETMAXMBX
+NETMAXSRV
+50))
691 for (factory
= maildrivers
; factory
&&
692 ((factory
->flags
& DR_DISABLE
) ||
693 ((factory
->flags
& DR_LOCAL
) && (*mailbox
== '{')) ||
694 !(*factory
->valid
) (mailbox
));
695 factory
= factory
->next
);
696 /* validate factory against non-dummy stream */
697 if (factory
&& stream
&& stream
->dtb
&& (stream
->dtb
!= factory
) &&
698 strcmp (stream
->dtb
->name
,"dummy"))
699 /* factory invalid; if dummy, use stream */
700 factory
= strcmp (factory
->name
,"dummy") ? NIL
: stream
->dtb
;
701 if (!factory
&& purpose
) { /* if want an error message */
702 sprintf (tmp
,"Can't %s %.80s: %s",purpose
,mailbox
,(*mailbox
== '{') ?
703 "invalid remote specification" : "no such mailbox");
706 return factory
; /* return driver factory */
709 /* Mail validate network mailbox name
710 * Accepts: mailbox name
711 * mailbox driver to validate against
712 * pointer to where to return host name if non-NIL
713 * pointer to where to return mailbox name if non-NIL
714 * Returns: driver on success, NIL on failure
717 DRIVER
*mail_valid_net (char *name
,DRIVER
*drv
,char *host
,char *mailbox
)
720 if (!mail_valid_net_parse (name
,&mb
) || strcmp (mb
.service
,drv
->name
))
722 if (host
) strcpy (host
,mb
.host
);
723 if (mailbox
) strcpy (mailbox
,mb
.mailbox
);
728 /* Mail validate network mailbox name
729 * Accepts: mailbox name
730 * NETMBX structure to return values
731 * Returns: T on success, NIL on failure
734 long mail_valid_net_parse (char *name
,NETMBX
*mb
)
736 return mail_valid_net_parse_work (name
,mb
,"imap");
739 /* Mail validate network mailbox name worker routine
740 * Accepts: mailbox name
741 * NETMBX structure to return values
743 * Returns: T on success, NIL on failure
746 long mail_valid_net_parse_work (char *name
,NETMBX
*mb
,char *service
)
749 char c
,*s
,*t
,*v
,tmp
[MAILTMPLEN
],arg
[MAILTMPLEN
];
750 /* initialize structure */
751 memset (mb
,'\0',sizeof (NETMBX
));
752 /* must have host specification */
753 if (*name
++ != '{') return NIL
;
754 if (*name
== '[') { /* if domain literal, find its ending */
755 if (!((v
= strpbrk (name
,"]}")) && (*v
++ == ']'))) return NIL
;
757 /* find end of host name */
758 else if (!(v
= strpbrk (name
,"/:}"))) return NIL
;
759 /* validate length, find mailbox part */
760 if (!((i
= v
- name
) && (i
< NETMAXHOST
) && (t
= strchr (v
,'}')) &&
761 ((j
= t
- v
) < MAILTMPLEN
) && (strlen (t
+1) < (size_t) NETMAXMBX
)))
762 return NIL
; /* invalid mailbox */
763 strncpy (mb
->host
,name
,i
); /* set host name */
764 strncpy (mb
->orighost
,name
,i
);
765 mb
->host
[i
] = mb
->orighost
[i
] = '\0';
766 strcpy (mb
->mailbox
,t
+1); /* set mailbox name */
767 if (t
- v
) { /* any switches or port specification? */
768 strncpy (t
= tmp
,v
,j
); /* copy it */
769 tmp
[j
] = '\0'; /* tie it off */
770 c
= *t
++; /* get first delimiter */
771 do switch (c
) { /* act based upon the character */
772 case ':': /* port specification */
773 if (mb
->port
|| !(mb
->port
= strtoul (t
,&t
,10))) return NIL
;
774 c
= t
? *t
++ : '\0'; /* get delimiter, advance pointer */
776 case '/': /* switch */
778 if ((t
= strpbrk (s
= t
,"/:=")) != NULL
) {
779 c
= *t
; /* remember delimiter for later */
780 *t
++ = '\0'; /* tie off switch name */
782 else c
= '\0'; /* no delimiter */
783 if (c
== '=') { /* parse switches which take arguments */
784 if (*t
== '"') { /* quoted string? */
785 for (v
= arg
,i
= 0,++t
; (c
= *t
++) != '"';) {
786 if (!c
) return NIL
; /* unterminated string */
787 /* quote next character */
788 if (c
== '\\') c
= *t
++;
789 if (!c
) return NIL
; /* can't quote NUL either */
792 c
= *t
++; /* remember delimiter for later */
793 arg
[i
] = '\0'; /* tie off argument */
795 else { /* non-quoted argument */
796 if ((t
= strpbrk (v
= t
,"/:")) != NULL
) {
797 c
= *t
; /* remember delimiter for later */
798 *t
++ = '\0'; /* tie off switch name */
800 else c
= '\0'; /* no delimiter */
801 i
= strlen (v
); /* length of argument */
803 if (!compare_cstring (s
,"service") && (i
< NETMAXSRV
) && !*mb
->service
)
804 lcase (strcpy (mb
->service
,v
));
805 else if (!compare_cstring (s
,"user") && (i
< NETMAXUSER
) && !*mb
->user
)
807 else if (!compare_cstring (s
,"authuser") && (i
< NETMAXUSER
) &&
808 !*mb
->authuser
) strcpy (mb
->authuser
,v
);
812 else { /* non-argument switch */
813 if (!compare_cstring (s
,"anonymous")) mb
->anoflag
= T
;
814 else if (!compare_cstring (s
,"debug")) mb
->dbgflag
= T
;
815 else if (!compare_cstring (s
,"readonly")) mb
->readonlyflag
= T
;
816 else if (!compare_cstring (s
,"secure")) mb
->secflag
= T
;
817 else if (!compare_cstring (s
,"norsh")) mb
->norsh
= T
;
818 else if (!compare_cstring (s
,"loser")) mb
->loser
= T
;
819 else if (!compare_cstring (s
,"tls") && !mb
->notlsflag
)
821 else if (!compare_cstring (s
,"tls-sslv23") && !mb
->notlsflag
)
822 mb
->tlssslv23
= mb
->tlsflag
= T
;
823 else if (!compare_cstring (s
,"notls") && !mb
->tlsflag
)
825 else if (!compare_cstring (s
,"tryssl"))
826 mb
->trysslflag
= mailssldriver
? T
: NIL
;
827 else if (mailssldriver
&& !compare_cstring (s
,"ssl") && !mb
->tlsflag
)
828 mb
->sslflag
= mb
->notlsflag
= T
;
829 else if (!compare_cstring(s
, "tls1")
830 && !mb
->tls1_1
&& !mb
->tls1_2
&& !mb
->dtls1
)
831 mb
->sslflag
= mb
->notlsflag
= mb
->tls1
= T
;
833 else if (!compare_cstring(s
, "tls1_1")
834 && !mb
->tls1
&& !mb
->tls1_2
&& !mb
->dtls1
)
835 mb
->sslflag
= mb
->notlsflag
= mb
->tls1_1
= T
;
836 else if (!compare_cstring(s
, "tls1_2")
837 && !mb
->tls1
&& !mb
->tls1_1
&& !mb
->dtls1
)
838 mb
->sslflag
= mb
->notlsflag
= mb
->tls1_2
= T
;
840 else if (!compare_cstring(s
, "dtls1")
841 && !mb
->tls1
&& !mb
->tls1_1
&& !mb
->tls1_2
)
842 mb
->sslflag
= mb
->notlsflag
= mb
->dtls1
= T
;
843 else if (mailssldriver
&& !compare_cstring (s
,"novalidate-cert"))
845 /* hack for compatibility with the past */
846 else if (mailssldriver
&& !compare_cstring (s
,"validate-cert"));
847 /* service switches below here */
848 else if (*mb
->service
) return NIL
;
849 else if (!compare_cstring (s
,"imap") ||
850 !compare_cstring (s
,"nntp") ||
851 !compare_cstring (s
,"pop3") ||
852 !compare_cstring (s
,"smtp") ||
853 !compare_cstring (s
,"submit"))
854 lcase (strcpy (mb
->service
,s
));
855 else if (!compare_cstring (s
,"imap2") ||
856 !compare_cstring (s
,"imap2bis") ||
857 !compare_cstring (s
,"imap4") ||
858 !compare_cstring (s
,"imap4rev1"))
859 strcpy (mb
->service
,"imap");
860 else if (!compare_cstring (s
,"pop"))
861 strcpy (mb
->service
,"pop3");
862 else return NIL
; /* invalid non-argument switch */
865 default: /* anything else is bogus */
867 } while (c
); /* see if anything more to parse */
869 /* default mailbox name */
870 if (!*mb
->mailbox
) strcpy (mb
->mailbox
,"INBOX");
871 /* default service name */
872 if (!*mb
->service
) strcpy (mb
->service
,service
);
873 /* /norsh only valid if imap */
874 if (mb
->norsh
&& strcmp (mb
->service
,"imap")) return NIL
;
878 /* Mail scan mailboxes for string
879 * Accepts: mail stream
885 void mail_scan (MAILSTREAM
*stream
,char *ref
,char *pat
,char *contents
)
887 int remote
= ((*pat
== '{') || (ref
&& *ref
== '{'));
889 if (ref
&& (strlen (ref
) > NETMAXMBX
)) {
890 char tmp
[MAILTMPLEN
];
891 sprintf (tmp
,"Invalid LIST reference specification: %.80s",ref
);
895 if (strlen (pat
) > NETMAXMBX
) {
896 char tmp
[MAILTMPLEN
];
897 sprintf (tmp
,"Invalid LIST pattern specification: %.80s",pat
);
901 if (*pat
== '{') ref
= NIL
; /* ignore reference if pattern is remote */
902 if (stream
) { /* if have a stream, do it for that stream */
903 if ((d
= stream
->dtb
) && d
->scan
&&
904 !(((d
->flags
& DR_LOCAL
) && remote
)))
905 (*d
->scan
) (stream
,ref
,pat
,contents
);
907 /* otherwise do for all DTB's */
908 else for (d
= maildrivers
; d
; d
= d
->next
)
909 if (d
->scan
&& !((d
->flags
& DR_DISABLE
) ||
910 ((d
->flags
& DR_LOCAL
) && remote
)))
911 (d
->scan
) (NIL
,ref
,pat
,contents
);
914 /* Mail list mailboxes
915 * Accepts: mail stream
920 void mail_list (MAILSTREAM
*stream
,char *ref
,char *pat
)
922 int remote
= ((*pat
== '{') || (ref
&& *ref
== '{'));
923 DRIVER
*d
= maildrivers
;
924 if (ref
&& (strlen (ref
) > NETMAXMBX
)) {
925 char tmp
[MAILTMPLEN
];
926 sprintf (tmp
,"Invalid LIST reference specification: %.80s",ref
);
930 if (strlen (pat
) > NETMAXMBX
) {
931 char tmp
[MAILTMPLEN
];
932 sprintf (tmp
,"Invalid LIST pattern specification: %.80s",pat
);
936 if (*pat
== '{') ref
= NIL
; /* ignore reference if pattern is remote */
937 if (stream
&& stream
->dtb
) { /* if have a stream, do it for that stream */
938 if (!(((d
= stream
->dtb
)->flags
& DR_LOCAL
) && remote
))
939 (*d
->list
) (stream
,ref
,pat
);
941 /* otherwise do for all DTB's */
942 else do if (!((d
->flags
& DR_DISABLE
) ||
943 ((d
->flags
& DR_LOCAL
) && remote
)))
944 (d
->list
) (NIL
,ref
,pat
);
945 while ((d
= d
->next
) != NULL
); /* until at the end */
948 /* Mail list subscribed mailboxes
949 * Accepts: mail stream
953 void mail_lsub (MAILSTREAM
*stream
,char *ref
,char *pat
)
955 int remote
= ((*pat
== '{') || (ref
&& *ref
== '{'));
956 DRIVER
*d
= maildrivers
;
957 if (ref
&& (strlen (ref
) > NETMAXMBX
)) {
958 char tmp
[MAILTMPLEN
];
959 sprintf (tmp
,"Invalid LSUB reference specification: %.80s",ref
);
963 if (strlen (pat
) > NETMAXMBX
) {
964 char tmp
[MAILTMPLEN
];
965 sprintf (tmp
,"Invalid LSUB pattern specification: %.80s",pat
);
969 if (*pat
== '{') ref
= NIL
; /* ignore reference if pattern is remote */
970 if (stream
&& stream
->dtb
) { /* if have a stream, do it for that stream */
971 if (!(((d
= stream
->dtb
)->flags
& DR_LOCAL
) && remote
))
972 (*d
->lsub
) (stream
,ref
,pat
);
974 /* otherwise do for all DTB's */
975 else do if (!((d
->flags
& DR_DISABLE
) ||
976 ((d
->flags
& DR_LOCAL
) && remote
)))
977 (d
->lsub
) (NIL
,ref
,pat
);
978 while ((d
= d
->next
) != NULL
); /* until at the end */
981 /* Mail subscribe to mailbox
982 * Accepts: mail stream
983 * mailbox to add to subscription list
984 * Returns: T on success, NIL on failure
987 long mail_subscribe (MAILSTREAM
*stream
,char *mailbox
)
989 DRIVER
*factory
= mail_valid (stream
,mailbox
,"subscribe to mailbox");
991 (factory
->subscribe
?
992 (*factory
->subscribe
) (stream
,mailbox
) : sm_subscribe (mailbox
)) : NIL
;
996 /* Mail unsubscribe to mailbox
997 * Accepts: mail stream
998 * mailbox to delete from subscription list
999 * Returns: T on success, NIL on failure
1002 long mail_unsubscribe (MAILSTREAM
*stream
,char *mailbox
)
1004 DRIVER
*factory
= mail_valid (stream
,mailbox
,NIL
);
1005 return (factory
&& factory
->unsubscribe
) ?
1006 (*factory
->unsubscribe
) (stream
,mailbox
) : sm_unsubscribe (mailbox
);
1009 /* Mail create mailbox
1010 * Accepts: mail stream
1011 * mailbox name to create
1012 * Returns: T on success, NIL on failure
1015 long mail_create (MAILSTREAM
*stream
,char *mailbox
)
1018 char *s
,*t
,tmp
[MAILTMPLEN
];
1021 /* never allow names with newlines */
1022 if ((s
= strpbrk (mailbox
,"\015\012")) != NULL
) {
1023 MM_LOG ("Can't create mailbox with such a name",ERROR
);
1026 if (strlen (mailbox
) >= (NETMAXHOST
+(NETMAXUSER
*2)+NETMAXMBX
+NETMAXSRV
+50)) {
1027 sprintf (tmp
,"Can't create %.80s: %s",mailbox
,(*mailbox
== '{') ?
1028 "invalid remote specification" : "no such mailbox");
1032 /* create of INBOX invalid */
1033 if (!compare_cstring (mailbox
,"INBOX")) {
1034 MM_LOG ("Can't create INBOX",ERROR
);
1038 if ((s
= mail_utf7_valid (mailbox
)) != NULL
) {
1039 sprintf (tmp
,"Can't create %s: %.80s",s
,mailbox
);
1044 /* see if special driver hack */
1045 if ((mailbox
[0] == '#') && ((mailbox
[1] == 'd') || (mailbox
[1] == 'D')) &&
1046 ((mailbox
[2] == 'r') || (mailbox
[2] == 'R')) &&
1047 ((mailbox
[3] == 'i') || (mailbox
[3] == 'I')) &&
1048 ((mailbox
[4] == 'v') || (mailbox
[4] == 'V')) &&
1049 ((mailbox
[5] == 'e') || (mailbox
[5] == 'E')) &&
1050 ((mailbox
[6] == 'r') || (mailbox
[6] == 'R')) && (mailbox
[7] == '.')) {
1051 /* copy driver until likely delimiter */
1052 if ((s
= strpbrk (t
= mailbox
+8,"/\\:")) && (i
= s
- t
)) {
1057 sprintf (tmp
,"Can't create mailbox %.80s: bad driver syntax",mailbox
);
1061 for (d
= maildrivers
; d
&& strcmp (d
->name
,tmp
); d
= d
->next
);
1062 if (d
) mailbox
= ++s
; /* skip past driver specification */
1064 sprintf (tmp
,"Can't create mailbox %.80s: unknown driver",mailbox
);
1069 /* use stream if one given or deterministic */
1070 else if ((stream
&& stream
->dtb
) ||
1071 (((*mailbox
== '{') || (*mailbox
== '#')) &&
1072 (stream
= mail_open (NIL
,mailbox
,OP_PROTOTYPE
| OP_SILENT
))))
1074 else if ((*mailbox
!= '{') && (ts
= default_proto (NIL
))) d
= ts
->dtb
;
1075 else { /* failed utterly */
1076 sprintf (tmp
,"Can't create mailbox %.80s: indeterminate format",mailbox
);
1080 return (*d
->create
) (stream
,mailbox
);
1083 /* Mail delete mailbox
1084 * Accepts: mail stream
1085 * mailbox name to delete
1086 * Returns: T on success, NIL on failure
1089 long mail_delete (MAILSTREAM
*stream
,char *mailbox
)
1091 DRIVER
*dtb
= mail_valid (stream
,mailbox
,"delete mailbox");
1092 if (!dtb
) return NIL
;
1093 if (((mailbox
[0] == 'I') || (mailbox
[0] == 'i')) &&
1094 ((mailbox
[1] == 'N') || (mailbox
[1] == 'n')) &&
1095 ((mailbox
[2] == 'B') || (mailbox
[2] == 'b')) &&
1096 ((mailbox
[3] == 'O') || (mailbox
[3] == 'o')) &&
1097 ((mailbox
[4] == 'X') || (mailbox
[4] == 'x')) && !mailbox
[5]) {
1098 MM_LOG ("Can't delete INBOX",ERROR
);
1101 return SAFE_DELETE (dtb
,stream
,mailbox
);
1105 /* Mail rename mailbox
1106 * Accepts: mail stream
1109 * Returns: T on success, NIL on failure
1112 long mail_rename (MAILSTREAM
*stream
,char *old
,char *newname
)
1114 char *s
,tmp
[MAILTMPLEN
];
1115 DRIVER
*dtb
= mail_valid (stream
,old
,"rename mailbox");
1116 if (!dtb
) return NIL
;
1118 if ((s
= mail_utf7_valid (newname
)) != NULL
) {
1119 sprintf (tmp
,"Can't rename to %s: %.80s",s
,newname
);
1123 if ((*old
!= '{') && (*old
!= '#') && mail_valid (NIL
,newname
,NIL
)) {
1124 sprintf (tmp
,"Can't rename %.80s: mailbox %.80s already exists",
1129 return SAFE_RENAME (dtb
,stream
,old
,newname
);
1132 /* Validate mailbox as Modified UTF-7
1133 * Accepts: candidate mailbox name
1134 * Returns: error string if error, NIL if valid
1137 char *mail_utf7_valid (char *mailbox
)
1140 for (s
= mailbox
; *s
; s
++) { /* make sure valid name */
1141 /* reserved for future use with UTF-8 */
1142 if (*s
& 0x80) return "mailbox name with 8-bit octet";
1143 /* validate modified UTF-7 */
1144 else if (*s
== '&') while (*++s
!= '-') switch (*s
) {
1146 return "unterminated modified UTF-7 name";
1147 case '+': /* valid modified BASE64 */
1149 break; /* all OK so far */
1150 default: /* must be alphanumeric */
1151 if (!isalnum (*s
)) return "invalid modified UTF-7 name";
1155 return NIL
; /* all OK */
1158 /* Mail status of mailbox
1159 * Accepts: mail stream if open on this mailbox
1162 * Returns: T on success, NIL on failure
1165 long mail_status (MAILSTREAM
*stream
,char *mbx
,long flags
)
1167 DRIVER
*dtb
= mail_valid (stream
,mbx
,"get status of mailbox");
1168 if (!dtb
) return NIL
; /* only if valid */
1169 if (stream
&& ((dtb
!= stream
->dtb
) ||
1170 ((dtb
->flags
& DR_LOCAL
) && strcmp (mbx
,stream
->mailbox
) &&
1171 strcmp (mbx
,stream
->original_mailbox
))))
1172 stream
= NIL
; /* stream not suitable */
1173 return SAFE_STATUS (dtb
,stream
,mbx
,flags
);
1177 /* Mail status of mailbox default handler
1178 * Accepts: mail stream
1181 * Returns: T on success, NIL on failure
1184 long mail_status_default (MAILSTREAM
*stream
,char *mbx
,long flags
)
1188 MAILSTREAM
*tstream
= NIL
;
1189 /* make temporary stream (unless this mbx) */
1190 if (!stream
&& !(stream
= tstream
=
1191 mail_open (NIL
,mbx
,OP_READONLY
|OP_SILENT
))) return NIL
;
1192 status
.flags
= flags
; /* return status values */
1193 status
.messages
= stream
->nmsgs
;
1194 status
.recent
= stream
->recent
;
1195 if (flags
& SA_UNSEEN
) /* must search to get unseen messages */
1196 for (i
= 1,status
.unseen
= 0; i
<= stream
->nmsgs
; i
++)
1197 if (!mail_elt (stream
,i
)->seen
) status
.unseen
++;
1198 status
.uidnext
= stream
->uid_last
+ 1;
1199 status
.uidvalidity
= stream
->uid_validity
;
1200 MM_STATUS(stream
,mbx
,&status
);/* pass status to main program */
1201 if (tstream
) mail_close (tstream
);
1202 return T
; /* success */
1206 * Accepts: candidate stream for recycling
1209 * Returns: stream to use on success, NIL on failure
1212 MAILSTREAM
*mail_open (MAILSTREAM
*stream
,char *name
,long options
)
1215 char c
,*s
,tmp
[MAILTMPLEN
];
1218 switch (name
[0]) { /* see if special handling */
1219 case '#': /* possible special hacks */
1220 if (((name
[1] == 'M') || (name
[1] == 'm')) &&
1221 ((name
[2] == 'O') || (name
[2] == 'o')) &&
1222 ((name
[3] == 'V') || (name
[3] == 'v')) &&
1223 ((name
[4] == 'E') || (name
[4] == 'e')) && (c
= name
[5]) &&
1224 (s
= strchr (name
+6,c
)) && (i
= s
- (name
+ 6)) && (i
< MAILTMPLEN
)) {
1225 if ((stream
= mail_open (stream
,s
+1,options
)) != NULL
) {
1226 strncpy (tmp
,name
+6,i
); /* copy snarf mailbox name */
1227 tmp
[i
] = '\0'; /* tie off name */
1228 mail_parameters (stream
,SET_SNARFMAILBOXNAME
,(void *) tmp
);
1229 stream
->snarf
.options
= options
;
1230 mail_ping (stream
); /* do initial snarf */
1231 /* punt if can't do initial snarf */
1232 if (!stream
->snarf
.time
) stream
= mail_close (stream
);
1236 /* special POP hack */
1237 else if (((name
[1] == 'P') || (name
[1] == 'p')) &&
1238 ((name
[2] == 'O') || (name
[2] == 'o')) &&
1239 ((name
[3] == 'P') || (name
[3] == 'p')) &&
1240 mail_valid_net_parse_work (name
+4,&mb
,"pop3") &&
1241 !strcmp (mb
.service
,"pop3") && !mb
.anoflag
&& !mb
.readonlyflag
) {
1242 if ((stream
= mail_open (stream
,mb
.mailbox
,options
)) != NULL
) {
1243 sprintf (tmp
,"{%.255s",mb
.host
);
1244 if (mb
.port
) sprintf (tmp
+ strlen (tmp
),":%lu",mb
.port
);
1245 if (mb
.user
[0]) sprintf (tmp
+ strlen (tmp
),"/user=%.64s",mb
.user
);
1246 if (mb
.dbgflag
) strcat (tmp
,"/debug");
1247 if (mb
.secflag
) strcat (tmp
,"/secure");
1248 if (mb
.tlsflag
) strcat (tmp
,"/tls");
1249 if (mb
.notlsflag
) strcat (tmp
,"/notls");
1250 if (mb
.sslflag
) strcat (tmp
,"/ssl");
1251 if (mb
.tls1
) strcat (tmp
,"/tls1");
1252 if (mb
.tls1_1
) strcat (tmp
,"/tls1_1");
1253 if (mb
.tls1_2
) strcat (tmp
,"/tls1_2");
1254 if (mb
.dtls1
) strcat (tmp
,"/dtls1");
1255 if (mb
.trysslflag
) strcat (tmp
,"/tryssl");
1256 if (mb
.novalidate
) strcat (tmp
,"/novalidate-cert");
1257 strcat (tmp
,"/pop3/loser}");
1258 mail_parameters (stream
,SET_SNARFMAILBOXNAME
,(void *) tmp
);
1259 mail_ping (stream
); /* do initial snarf */
1261 return stream
; /* return local mailbox stream */
1264 else if ((options
& OP_PROTOTYPE
) &&
1265 ((name
[1] == 'D') || (name
[1] == 'd')) &&
1266 ((name
[2] == 'R') || (name
[2] == 'r')) &&
1267 ((name
[3] == 'I') || (name
[3] == 'i')) &&
1268 ((name
[4] == 'V') || (name
[4] == 'v')) &&
1269 ((name
[5] == 'E') || (name
[5] == 'e')) &&
1270 ((name
[6] == 'R') || (name
[6] == 'r')) && (name
[7] == '.')) {
1271 sprintf (tmp
,"%.80s",name
+8);
1272 /* tie off name at likely delimiter */
1273 if ((s
= strpbrk (tmp
,"/\\:")) != NULL
) *s
++ = '\0';
1275 sprintf (tmp
,"Can't resolve mailbox %.80s: bad driver syntax",name
);
1277 return mail_close (stream
);
1279 for (d
= maildrivers
; d
&& compare_cstring (d
->name
,tmp
); d
= d
->next
);
1280 if (d
) return (*d
->open
) (NIL
);
1281 sprintf (tmp
,"Can't resolve mailbox %.80s: unknown driver",name
);
1283 return mail_close (stream
);
1285 /* fall through to default case */
1286 default: /* not special hack (but could be # name */
1287 d
= mail_valid (NIL
,name
,(options
& OP_SILENT
) ?
1288 (char *) NIL
: "open mailbox");
1290 return d
? mail_open_work (d
,stream
,name
,options
) : stream
;
1293 /* Mail open worker routine
1295 * candidate stream for recycling
1298 * Returns: stream to use on success, NIL on failure
1301 MAILSTREAM
*mail_open_work (DRIVER
*d
,MAILSTREAM
*stream
,char *name
,
1305 char tmp
[MAILTMPLEN
];
1307 if (options
& OP_PROTOTYPE
) return (*d
->open
) (NIL
);
1308 /* name is copied here in case the caller does a re-open using
1309 * stream->mailbox or stream->original_mailbox as the argument.
1311 name
= cpystr (name
); /* make copy of name */
1312 if (stream
) { /* recycling requested? */
1313 if ((stream
->dtb
== d
) && (d
->flags
& DR_RECYCLE
) &&
1314 ((d
->flags
& DR_HALFOPEN
) || !(options
& OP_HALFOPEN
)) &&
1315 mail_usable_network_stream (stream
,name
)) {
1316 /* yes, checkpoint if needed */
1317 if (d
->flags
& DR_XPOINT
) mail_check (stream
);
1318 mail_free_cache (stream
); /* clean up stream */
1319 if (stream
->mailbox
) fs_give ((void **) &stream
->mailbox
);
1320 if (stream
->original_mailbox
)
1321 fs_give ((void **) &stream
->original_mailbox
);
1322 /* flush user flags */
1323 for (i
= 0; i
< NUSERFLAGS
; i
++)
1324 if (stream
->user_flags
[i
]) fs_give ((void **) &stream
->user_flags
[i
]);
1326 else { /* stream not recycleable, babble if net */
1327 if (!stream
->silent
&& stream
->dtb
&& !(stream
->dtb
->flags
&DR_LOCAL
) &&
1328 mail_valid_net_parse (stream
->mailbox
,&mb
)) {
1329 sprintf (tmp
,"Closing connection to %.80s",mb
.host
);
1330 MM_LOG (tmp
,(long) NIL
);
1332 /* flush the old stream */
1333 stream
= mail_close (stream
);
1336 /* check if driver does not support halfopen */
1337 else if ((options
& OP_HALFOPEN
) && !(d
->flags
& DR_HALFOPEN
)) {
1338 fs_give ((void **) &name
);
1342 /* instantiate new stream if not recycling */
1343 if (!stream
) (*mailcache
) (stream
= (MAILSTREAM
*)
1344 memset (fs_get (sizeof (MAILSTREAM
)),0,
1345 sizeof (MAILSTREAM
)),(long) 0,CH_INIT
);
1346 stream
->dtb
= d
; /* set dispatch */
1347 /* set mailbox name */
1348 stream
->mailbox
= cpystr (stream
->original_mailbox
= name
);
1349 /* initialize stream flags */
1350 stream
->inbox
= stream
->lock
= NIL
;
1351 stream
->debug
= (options
& OP_DEBUG
) ? T
: NIL
;
1352 stream
->rdonly
= (options
& OP_READONLY
) ? T
: NIL
;
1353 stream
->anonymous
= (options
& OP_ANONYMOUS
) ? T
: NIL
;
1354 stream
->scache
= (options
& OP_SHORTCACHE
) ? T
: NIL
;
1355 stream
->silent
= (options
& OP_SILENT
) ? T
: NIL
;
1356 stream
->halfopen
= (options
& OP_HALFOPEN
) ? T
: NIL
;
1357 stream
->secure
= (options
& OP_SECURE
) ? T
: NIL
;
1358 stream
->tryssl
= (options
& OP_TRYSSL
) ? T
: NIL
;
1359 stream
->mulnewsrc
= (options
& OP_MULNEWSRC
) ? T
: NIL
;
1360 stream
->nokod
= (options
& OP_NOKOD
) ? T
: NIL
;
1361 stream
->sniff
= (options
& OP_SNIFF
) ? T
: NIL
;
1362 stream
->perm_seen
= stream
->perm_deleted
= stream
->perm_flagged
=
1363 stream
->perm_answered
= stream
->perm_draft
= stream
->kwd_create
= NIL
;
1364 stream
->uid_nosticky
= (d
->flags
& DR_NOSTICKY
) ? T
: NIL
;
1365 stream
->uid_last
= 0; /* default UID validity */
1366 stream
->uid_validity
= (unsigned long) time (0);
1367 /* have driver open, flush if failed */
1368 return ((*d
->open
) (stream
)) ? stream
: mail_close (stream
);
1372 * Accepts: mail stream
1374 * Returns: NIL, always
1377 MAILSTREAM
*mail_close_full (MAILSTREAM
*stream
,long options
)
1380 if (stream
) { /* make sure argument given */
1381 /* do the driver's close action */
1382 if (stream
->dtb
) (*stream
->dtb
->close
) (stream
,options
);
1383 stream
->dtb
= NIL
; /* resign driver */
1384 if (stream
->mailbox
) fs_give ((void **) &stream
->mailbox
);
1385 if (stream
->original_mailbox
)
1386 fs_give ((void **) &stream
->original_mailbox
);
1387 if (stream
->snarf
.name
) fs_give ((void **) &stream
->snarf
.name
);
1388 stream
->sequence
++; /* invalidate sequence */
1389 /* flush user flags */
1390 for (i
= 0; i
< NUSERFLAGS
; i
++)
1391 if (stream
->user_flags
[i
]) fs_give ((void **) &stream
->user_flags
[i
]);
1392 mail_free_cache (stream
); /* finally free the stream's storage */
1393 if (mailfreestreamsparep
&& stream
->sparep
)
1394 (*mailfreestreamsparep
) (&stream
->sparep
);
1395 if (!stream
->use
) fs_give ((void **) &stream
);
1401 * Accepts: mail stream
1404 * Handles provide a way to have multiple pointers to a stream yet allow the
1405 * stream's owner to nuke it or recycle it.
1408 MAILHANDLE
*mail_makehandle (MAILSTREAM
*stream
)
1410 MAILHANDLE
*handle
= (MAILHANDLE
*) fs_get (sizeof (MAILHANDLE
));
1411 handle
->stream
= stream
; /* copy stream */
1412 /* and its sequence */
1413 handle
->sequence
= stream
->sequence
;
1414 stream
->use
++; /* let stream know another handle exists */
1418 void mail_free_idlist (IDLIST
**idlist
)
1420 if (idlist
&& *idlist
){
1421 if((*idlist
)->name
) fs_give((void **)&(*idlist
)->name
);
1422 if((*idlist
)->value
) fs_give((void **)&(*idlist
)->value
);
1423 if((*idlist
)->next
) mail_free_idlist(&(*idlist
)->next
);
1424 fs_give((void **) idlist
);
1429 /* Mail release handle
1430 * Accepts: Mail handle
1433 void mail_free_handle (MAILHANDLE
**handle
)
1436 if (*handle
) { /* only free if exists */
1437 /* resign stream, flush unreferenced zombies */
1438 if ((!--(s
= (*handle
)->stream
)->use
) && !s
->dtb
) fs_give ((void **) &s
);
1439 fs_give ((void **) handle
); /* now flush the handle */
1444 /* Mail get stream handle
1445 * Accepts: Mail handle
1446 * Returns: mail stream or NIL if stream gone
1449 MAILSTREAM
*mail_stream (MAILHANDLE
*handle
)
1451 MAILSTREAM
*s
= handle
->stream
;
1452 return (s
->dtb
&& (handle
->sequence
== s
->sequence
)) ? s
: NIL
;
1455 /* Mail fetch cache element
1456 * Accepts: mail stream
1457 * message # to fetch
1458 * Returns: cache element of this message
1459 * Can also be used to create cache elements for new messages.
1462 MESSAGECACHE
*mail_elt (MAILSTREAM
*stream
,unsigned long msgno
)
1464 if (msgno
< 1 || msgno
> stream
->nmsgs
) {
1465 char tmp
[MAILTMPLEN
];
1466 sprintf (tmp
,"Bad msgno %lu in mail_elt, nmsgs = %lu, mbx=%.80s",
1467 msgno
,stream
->nmsgs
,stream
->mailbox
? stream
->mailbox
: "???");
1470 return (MESSAGECACHE
*) (*mailcache
) (stream
,msgno
,CH_MAKEELT
);
1474 /* Mail fetch fast information
1475 * Accepts: mail stream
1479 * Generally, mail_fetch_structure is preferred
1482 void mail_fetch_fast (MAILSTREAM
*stream
,char *sequence
,long flags
)
1484 /* do the driver's action */
1485 if (stream
->dtb
&& stream
->dtb
->fast
)
1486 (*stream
->dtb
->fast
) (stream
,sequence
,flags
);
1491 * Accepts: mail stream
1496 void mail_fetch_flags (MAILSTREAM
*stream
,char *sequence
,long flags
)
1498 /* do the driver's action */
1499 if (stream
->dtb
&& stream
->dtb
->msgflags
)
1500 (*stream
->dtb
->msgflags
) (stream
,sequence
,flags
);
1503 /* Mail fetch message overview
1504 * Accepts: mail stream
1505 * UID sequence to fetch
1506 * pointer to overview return function
1509 void mail_fetch_overview (MAILSTREAM
*stream
,char *sequence
,overview_t ofn
)
1511 if (stream
->dtb
&& mail_uid_sequence (stream
,sequence
) &&
1512 !(stream
->dtb
->overview
&& (*stream
->dtb
->overview
) (stream
,ofn
)) &&
1514 mail_fetch_overview_default (stream
,ofn
);
1518 /* Mail fetch message overview using sequence numbers instead of UIDs
1519 * Accepts: mail stream
1521 * pointer to overview return function
1524 void mail_fetch_overview_sequence (MAILSTREAM
*stream
,char *sequence
,
1527 if (stream
->dtb
&& mail_sequence (stream
,sequence
) &&
1528 !(stream
->dtb
->overview
&& (*stream
->dtb
->overview
) (stream
,ofn
)) &&
1530 mail_fetch_overview_default (stream
,ofn
);
1534 /* Mail fetch message overview default handler
1535 * Accepts: mail stream with sequence bits lit
1536 * pointer to overview return function
1539 void mail_fetch_overview_default (MAILSTREAM
*stream
,overview_t ofn
)
1545 ov
.optional
.lines
= 0;
1546 ov
.optional
.xref
= NIL
;
1547 for (i
= 1; i
<= stream
->nmsgs
; i
++)
1548 if (((elt
= mail_elt (stream
,i
))->sequence
) &&
1549 (env
= mail_fetch_structure (stream
,i
,NIL
,NIL
)) && ofn
) {
1550 ov
.subject
= env
->subject
;
1551 ov
.from
= env
->from
;
1552 ov
.date
= env
->date
;
1553 ov
.message_id
= env
->message_id
;
1554 ov
.references
= env
->references
;
1555 ov
.optional
.octets
= elt
->rfc822_size
;
1556 (*ofn
) (stream
,mail_uid (stream
,i
),&ov
,i
);
1560 /* Mail fetch message structure
1561 * Accepts: mail stream
1562 * message # to fetch
1563 * pointer to return body
1565 * Returns: envelope of this message, body returned in body value
1567 * Fetches the "fast" information as well
1570 ENVELOPE
*mail_fetch_structure (MAILSTREAM
*stream
,unsigned long msgno
,
1571 BODY
**body
,long flags
)
1577 unsigned long hdrsize
;
1579 /* do the driver's action if specified */
1580 if (stream
->dtb
&& stream
->dtb
->structure
)
1581 return (*stream
->dtb
->structure
) (stream
,msgno
,body
,flags
);
1582 if (flags
& FT_UID
) { /* UID form of call */
1583 if ((msgno
= mail_msgno (stream
,msgno
)) != 0L) flags
&= ~FT_UID
;
1584 else return NIL
; /* must get UID/msgno map first */
1586 elt
= mail_elt (stream
,msgno
);/* get elt for real message number */
1587 if (stream
->scache
) { /* short caching */
1588 if (msgno
!= stream
->msgno
){/* garbage collect if not same message */
1589 mail_gc (stream
,GC_ENV
| GC_TEXTS
);
1590 stream
->msgno
= msgno
; /* this is the current message now */
1592 env
= &stream
->env
; /* get pointers to envelope and body */
1595 else { /* get pointers to elt envelope and body */
1596 env
= &elt
->private.msg
.env
;
1597 b
= &elt
->private.msg
.body
;
1600 if (stream
->dtb
&& ((body
&& !*b
) || !*env
|| (*env
)->incomplete
)) {
1601 mail_free_envelope (env
); /* flush old envelope and body */
1603 /* see if need to fetch the whole thing */
1604 if (body
|| !elt
->rfc822_size
) {
1605 s
= (*stream
->dtb
->header
) (stream
,msgno
,&hdrsize
,flags
& ~FT_INTERNAL
);
1606 /* make copy in case body fetch smashes it */
1607 hdr
= (char *) memcpy (fs_get ((size_t) hdrsize
+1),s
,(size_t) hdrsize
);
1608 hdr
[hdrsize
] = '\0'; /* tie off header */
1609 (*stream
->dtb
->text
) (stream
,msgno
,&bs
,(flags
& ~FT_INTERNAL
) | FT_PEEK
);
1610 if (!elt
->rfc822_size
) elt
->rfc822_size
= hdrsize
+ SIZE (&bs
);
1611 if (body
) /* only parse body if requested */
1612 rfc822_parse_msg (env
,b
,hdr
,hdrsize
,&bs
,BADHOST
,stream
->dtb
->flags
);
1614 rfc822_parse_msg (env
,NIL
,hdr
,hdrsize
,NIL
,BADHOST
,stream
->dtb
->flags
);
1615 fs_give ((void **) &hdr
); /* flush header */
1617 else { /* can save memory doing it this way */
1618 hdr
= (*stream
->dtb
->header
) (stream
,msgno
,&hdrsize
,flags
| FT_INTERNAL
);
1619 if (hdrsize
) { /* in case null header */
1620 c
= hdr
[hdrsize
]; /* preserve what's there */
1621 hdr
[hdrsize
] = '\0'; /* tie off header */
1622 rfc822_parse_msg (env
,NIL
,hdr
,hdrsize
,NIL
,BADHOST
,stream
->dtb
->flags
);
1623 hdr
[hdrsize
] = c
; /* restore in case cached data */
1625 else *env
= mail_newenvelope ();
1628 /* if need date, have date in envelope? */
1629 if (!elt
->day
&& *env
&& (*env
)->date
) mail_parse_date (elt
,(*env
)->date
);
1630 /* sigh, fill in bogus default */
1631 if (!elt
->day
) elt
->day
= elt
->month
= 1;
1632 if (body
) *body
= *b
; /* return the body */
1633 return *env
; /* return the envelope */
1636 /* Mail mark single message (internal use only)
1637 * Accepts: mail stream
1642 static void markseen (MAILSTREAM
*stream
,MESSAGECACHE
*elt
,long flags
)
1647 /* non-peeking and needs to set \Seen? */
1648 if (!(flags
& FT_PEEK
) && !elt
->seen
) {
1649 if (stream
->dtb
->flagmsg
){ /* driver wants per-message call? */
1650 elt
->valid
= NIL
; /* do pre-alteration driver call */
1651 (*stream
->dtb
->flagmsg
) (stream
,elt
);
1652 /* set seen, do post-alteration driver call */
1653 elt
->seen
= elt
->valid
= T
;
1654 (*stream
->dtb
->flagmsg
) (stream
,elt
);
1656 if (stream
->dtb
->flag
) { /* driver wants one-time call? */
1657 /* better safe than sorry, save seq bits */
1658 for (i
= 1; i
<= stream
->nmsgs
; i
++) {
1659 e
= mail_elt (stream
,i
);
1660 e
->private.sequence
= e
->sequence
;
1662 /* call driver to set the message */
1663 sprintf (sequence
,"%lu",elt
->msgno
);
1664 (*stream
->dtb
->flag
) (stream
,sequence
,"\\Seen",ST_SET
);
1665 /* restore sequence bits */
1666 for (i
= 1; i
<= stream
->nmsgs
; i
++) {
1667 e
= mail_elt (stream
,i
);
1668 e
->sequence
= e
->private.sequence
;
1671 /* notify mail program of flag change */
1672 MM_FLAGS (stream
,elt
->msgno
);
1676 /* Mail fetch message
1677 * Accepts: mail stream
1678 * message # to fetch
1679 * pointer to returned length
1681 * Returns: message text
1684 char *mail_fetch_message (MAILSTREAM
*stream
,unsigned long msgno
,
1685 unsigned long *len
,long flags
)
1693 if (len
) *len
= 0; /* default return size */
1694 if (flags
& FT_UID
) { /* UID form of call */
1695 if ((msgno
= mail_msgno (stream
,msgno
)) != 0L) flags
&= ~FT_UID
;
1696 else return ""; /* must get UID/msgno map first */
1698 /* initialize message data identifier */
1699 INIT_GETS (md
,stream
,msgno
,"",0,0);
1700 /* is data already cached? */
1701 if ((t
= &(elt
= mail_elt (stream
,msgno
))->private.msg
.full
.text
)->data
) {
1702 markseen (stream
,elt
,flags
);/* mark message seen */
1703 return mail_fetch_text_return (&md
,t
,len
);
1705 if (!stream
->dtb
) return ""; /* not in cache, must have live driver */
1706 if (stream
->dtb
->msgdata
) return
1707 ((*stream
->dtb
->msgdata
) (stream
,msgno
,"",0,0,NIL
,flags
) && t
->data
) ?
1708 mail_fetch_text_return (&md
,t
,len
) : "";
1709 /* ugh, have to do this the crufty way */
1710 u
= mail_fetch_header (stream
,msgno
,NIL
,NIL
,&i
,flags
);
1711 /* copy in case text method stomps on it */
1712 s
= (char *) memcpy (fs_get ((size_t) i
),u
,(size_t) i
);
1713 if ((*stream
->dtb
->text
) (stream
,msgno
,&bs
,flags
)) {
1714 t
= &stream
->text
; /* build combined copy */
1715 if (t
->data
) fs_give ((void **) &t
->data
);
1716 t
->data
= (unsigned char *) fs_get ((t
->size
= i
+ SIZE (&bs
)) + 1);
1717 if (!elt
->rfc822_size
) elt
->rfc822_size
= t
->size
;
1718 else if (elt
->rfc822_size
!= t
->size
) {
1719 char tmp
[MAILTMPLEN
];
1720 sprintf (tmp
,"Calculated RFC822.SIZE (%lu) != reported size (%lu)",
1721 t
->size
,elt
->rfc822_size
);
1722 mm_log (tmp
,WARN
); /* bug trap */
1724 memcpy (t
->data
,s
,(size_t) i
);
1725 for (u
= (char *) t
->data
+ i
, j
= SIZE (&bs
); j
;) {
1726 memcpy (u
,bs
.curpos
,bs
.cursize
);
1727 u
+= bs
.cursize
; /* update text */
1729 bs
.curpos
+= (bs
.cursize
-1);
1731 (*bs
.dtb
->next
) (&bs
); /* advance to next buffer's worth */
1733 *u
= '\0'; /* tie off data */
1734 u
= mail_fetch_text_return (&md
,t
,len
);
1737 fs_give ((void **) &s
); /* finished with copy of header */
1741 /* Mail fetch message header
1742 * Accepts: mail stream
1743 * message # to fetch
1744 * MIME section specifier (#.#.#...#)
1745 * list of lines to fetch
1746 * pointer to returned length
1748 * Returns: message header in RFC822 format
1750 * Note: never calls a mailgets routine
1753 char *mail_fetch_header (MAILSTREAM
*stream
,unsigned long msgno
,char *section
,
1754 STRINGLIST
*lines
,unsigned long *len
,long flags
)
1758 SIZEDTEXT
*t
= NIL
,rt
;
1761 char tmp
[MAILTMPLEN
];
1762 if (len
) *len
= 0; /* default return size */
1763 if (section
&& (strlen (section
) > (MAILTMPLEN
- 20))) return "";
1764 if (flags
& FT_UID
) { /* UID form of call */
1765 if ((msgno
= mail_msgno (stream
,msgno
)) != 0L) flags
&= ~FT_UID
;
1766 else return ""; /* must get UID/msgno map first */
1768 elt
= mail_elt (stream
,msgno
);/* get cache data */
1769 if (section
&& *section
) { /* nested body header wanted? */
1770 if (!((b
= mail_body (stream
,msgno
,section
)) &&
1771 (b
->type
== TYPEMESSAGE
) && !strcmp (b
->subtype
,"RFC822")))
1772 return ""; /* lose if no body or not MESSAGE/RFC822 */
1773 m
= b
->nested
.msg
; /* point to nested message */
1775 /* else top-level message header wanted */
1776 else m
= &elt
->private.msg
;
1777 if (m
->header
.text
.data
&& mail_match_lines (lines
,m
->lines
,flags
)) {
1778 if (lines
) textcpy (t
= &stream
->text
,&m
->header
.text
);
1779 else t
= &m
->header
.text
; /* in cache, and cache is valid */
1780 markseen (stream
,elt
,flags
);/* mark message seen */
1783 else if (stream
->dtb
) { /* not in cache, has live driver? */
1784 if (stream
->dtb
->msgdata
) { /* has driver section fetch? */
1785 /* build driver section specifier */
1786 if (section
&& *section
) sprintf (tmp
,"%s.HEADER",section
);
1787 else strcpy (tmp
,"HEADER");
1788 if ((*stream
->dtb
->msgdata
) (stream
,msgno
,tmp
,0,0,lines
,flags
)) {
1789 t
= &m
->header
.text
; /* fetch data */
1790 /* don't need to postprocess lines */
1791 if (m
->lines
) lines
= NIL
;
1792 else if (lines
) textcpy (t
= &stream
->text
,&m
->header
.text
);
1795 else if (b
) { /* nested body wanted? */
1796 if (stream
->private.search
.text
) {
1797 rt
.data
= (unsigned char *) stream
->private.search
.text
+
1798 b
->nested
.msg
->header
.offset
;
1799 rt
.size
= b
->nested
.msg
->header
.text
.size
;
1802 else if ((*stream
->dtb
->text
) (stream
,msgno
,&bs
,flags
& ~FT_INTERNAL
)) {
1803 if ((bs
.dtb
->next
== mail_string_next
) && !lines
) {
1804 rt
.data
= (unsigned char *) bs
.curpos
+ b
->nested
.msg
->header
.offset
;
1805 rt
.size
= b
->nested
.msg
->header
.text
.size
;
1806 if (stream
->private.search
.string
)
1807 stream
->private.search
.text
= bs
.curpos
;
1808 t
= &rt
; /* special hack to avoid extra copy */
1810 else textcpyoffstring (t
= &stream
->text
,&bs
,
1811 b
->nested
.msg
->header
.offset
,
1812 b
->nested
.msg
->header
.text
.size
);
1815 else { /* top-level header fetch */
1816 /* mark message seen */
1817 markseen (stream
,elt
,flags
);
1818 if ((rt
.data
= (unsigned char *)
1819 (*stream
->dtb
->header
) (stream
,msgno
,&rt
.size
,flags
)) != NULL
) {
1820 /* make a safe copy if need to filter */
1821 if (lines
) textcpy (t
= &stream
->text
,&rt
);
1822 else t
= &rt
; /* top level header */
1826 if (!t
|| !t
->data
) return "";/* error if no string */
1827 /* filter headers if requested */
1828 if (lines
) t
->size
= mail_filter ((char *) t
->data
,t
->size
,lines
,flags
);
1829 if (len
) *len
= t
->size
; /* return size if requested */
1830 return (char *) t
->data
; /* and text */
1833 /* Mail fetch message text
1834 * Accepts: mail stream
1835 * message # to fetch
1836 * MIME section specifier (#.#.#...#)
1837 * pointer to returned length
1839 * Returns: message text
1842 char *mail_fetch_text (MAILSTREAM
*stream
,unsigned long msgno
,char *section
,
1843 unsigned long *len
,long flags
)
1850 char tmp
[MAILTMPLEN
];
1852 if (len
) *len
= 0; /* default return size */
1853 memset (&stream
->private.string
,NIL
,sizeof (STRING
));
1854 if (section
&& (strlen (section
) > (MAILTMPLEN
- 20))) return "";
1855 if (flags
& FT_UID
) { /* UID form of call */
1856 if ((msgno
= mail_msgno (stream
,msgno
)) != 0L) flags
&= ~FT_UID
;
1857 else return ""; /* must get UID/msgno map first */
1859 elt
= mail_elt (stream
,msgno
);/* get cache data */
1860 if (section
&& *section
) { /* nested body text wanted? */
1861 if (!((b
= mail_body (stream
,msgno
,section
)) &&
1862 (b
->type
== TYPEMESSAGE
) && !strcmp (b
->subtype
,"RFC822")))
1863 return ""; /* lose if no body or not MESSAGE/RFC822 */
1864 p
= &b
->nested
.msg
->text
; /* point at nested message */
1865 /* build IMAP-format section specifier */
1866 sprintf (tmp
,"%s.TEXT",section
);
1867 flags
&= ~FT_INTERNAL
; /* can't win with this set */
1869 else { /* top-level message text wanted */
1870 p
= &elt
->private.msg
.text
;
1871 strcpy (tmp
,"TEXT");
1873 /* initialize message data identifier */
1874 INIT_GETS (md
,stream
,msgno
,section
,0,0);
1875 if (p
->text
.data
) { /* is data already cached? */
1876 markseen (stream
,elt
,flags
);/* mark message seen */
1877 return mail_fetch_text_return (&md
,&p
->text
,len
);
1879 if (!stream
->dtb
) return ""; /* not in cache, must have live driver */
1880 if (stream
->dtb
->msgdata
) return
1881 ((*stream
->dtb
->msgdata
) (stream
,msgno
,tmp
,0,0,NIL
,flags
) && p
->text
.data
)?
1882 mail_fetch_text_return (&md
,&p
->text
,len
) : "";
1883 if (!(*stream
->dtb
->text
) (stream
,msgno
,&bs
,flags
)) return "";
1884 if (section
&& *section
) { /* nested is more complex */
1885 SETPOS (&bs
,p
->offset
);
1886 i
= p
->text
.size
; /* just want this much */
1888 else i
= SIZE (&bs
); /* want entire text */
1889 return mail_fetch_string_return (&md
,&bs
,i
,len
,flags
);
1892 /* Mail fetch message body part MIME headers
1893 * Accepts: mail stream
1894 * message # to fetch
1895 * MIME section specifier (#.#.#...#)
1896 * pointer to returned length
1898 * Returns: message text
1901 char *mail_fetch_mime (MAILSTREAM
*stream
,unsigned long msgno
,char *section
,
1902 unsigned long *len
,long flags
)
1907 char tmp
[MAILTMPLEN
];
1908 if (len
) *len
= 0; /* default return size */
1909 if (section
&& (strlen (section
) > (MAILTMPLEN
- 20))) return "";
1910 if (flags
& FT_UID
) { /* UID form of call */
1911 if ((msgno
= mail_msgno (stream
,msgno
)) != 0L) flags
&= ~FT_UID
;
1912 else return ""; /* must get UID/msgno map first */
1914 flags
&= ~FT_INTERNAL
; /* can't win with this set */
1915 if (!(section
&& *section
&& (b
= mail_body (stream
,msgno
,section
))))
1916 return ""; /* not valid section */
1918 if ((p
= &b
->mime
)->text
.data
) {
1919 /* mark message seen */
1920 markseen (stream
,mail_elt (stream
,msgno
),flags
);
1921 if (len
) *len
= p
->text
.size
;
1922 return (char *) p
->text
.data
;
1924 if (!stream
->dtb
) return ""; /* not in cache, must have live driver */
1925 if (stream
->dtb
->msgdata
) { /* has driver fetch? */
1926 /* build driver section specifier */
1927 sprintf (tmp
,"%s.MIME",section
);
1928 if ((*stream
->dtb
->msgdata
) (stream
,msgno
,tmp
,0,0,NIL
,flags
) &&
1930 if (len
) *len
= p
->text
.size
;
1931 return (char *) p
->text
.data
;
1935 if (len
) *len
= b
->mime
.text
.size
;
1936 if (!b
->mime
.text
.size
) { /* empty MIME header -- mark seen anyway */
1937 markseen (stream
,mail_elt (stream
,msgno
),flags
);
1940 /* have to get it from offset */
1941 if (stream
->private.search
.text
)
1942 return stream
->private.search
.text
+ b
->mime
.offset
;
1943 if (!(*stream
->dtb
->text
) (stream
,msgno
,&bs
,flags
)) {
1947 if (bs
.dtb
->next
== mail_string_next
) {
1948 if (stream
->private.search
.string
) stream
->private.search
.text
= bs
.curpos
;
1949 return bs
.curpos
+ b
->mime
.offset
;
1951 return textcpyoffstring (&stream
->text
,&bs
,b
->mime
.offset
,b
->mime
.text
.size
);
1954 /* Mail fetch message body part
1955 * Accepts: mail stream
1956 * message # to fetch
1957 * MIME section specifier (#.#.#...#)
1958 * pointer to returned length
1960 * Returns: message body
1963 char *mail_fetch_body (MAILSTREAM
*stream
,unsigned long msgno
,char *section
,
1964 unsigned long *len
,long flags
)
1971 char *s
,tmp
[MAILTMPLEN
];
1972 memset (&stream
->private.string
,NIL
,sizeof (STRING
));
1973 if (!(section
&& *section
)) /* top-level text wanted? */
1974 return mail_fetch_message (stream
,msgno
,len
,flags
);
1975 else if (strlen (section
) > (MAILTMPLEN
- 20)) return "";
1976 flags
&= ~FT_INTERNAL
; /* can't win with this set */
1977 /* initialize message data identifier */
1978 INIT_GETS (md
,stream
,msgno
,section
,0,0);
1979 /* kludge for old section 0 header */
1980 if (!strcmp (s
= strcpy (tmp
,section
),"0") ||
1981 ((s
= strstr (tmp
,".0")) && !s
[2])) {
1983 *s
= '\0'; /* tie off section */
1984 /* this silly way so it does mailgets */
1985 ht
.data
= (unsigned char *) mail_fetch_header (stream
,msgno
,
1986 tmp
[0] ? tmp
: NIL
,NIL
,
1988 /* may have UIDs here */
1989 md
.flags
= (flags
& FT_UID
) ? MG_UID
: NIL
;
1990 return mail_fetch_text_return (&md
,&ht
,len
);
1992 if (len
) *len
= 0; /* default return size */
1993 if (flags
& FT_UID
) { /* UID form of call */
1994 if ((msgno
= mail_msgno (stream
,msgno
)) != 0L) flags
&= ~FT_UID
;
1995 else return ""; /* must get UID/msgno map first */
1997 /* must have body */
1998 if (!(b
= mail_body (stream
,msgno
,section
))) return "";
1999 /* have cached text? */
2000 if ((t
= &(p
= &b
->contents
)->text
)->data
) {
2001 /* mark message seen */
2002 markseen (stream
,mail_elt (stream
,msgno
),flags
);
2003 return mail_fetch_text_return (&md
,t
,len
);
2005 if (!stream
->dtb
) return ""; /* not in cache, must have live driver */
2006 if (stream
->dtb
->msgdata
) return
2007 ((*stream
->dtb
->msgdata
)(stream
,msgno
,section
,0,0,NIL
,flags
) && t
->data
) ?
2008 mail_fetch_text_return (&md
,t
,len
) : "";
2009 if (len
) *len
= t
->size
;
2010 if (!t
->size
) { /* empty body part -- mark seen anyway */
2011 markseen (stream
,mail_elt (stream
,msgno
),flags
);
2014 /* copy body from stringstruct offset */
2015 if (stream
->private.search
.text
)
2016 return stream
->private.search
.text
+ p
->offset
;
2017 if (!(*stream
->dtb
->text
) (stream
,msgno
,&bs
,flags
)) {
2021 if (bs
.dtb
->next
== mail_string_next
) {
2022 if (stream
->private.search
.string
) stream
->private.search
.text
= bs
.curpos
;
2023 return bs
.curpos
+ p
->offset
;
2025 SETPOS (&bs
,p
->offset
);
2026 return mail_fetch_string_return (&md
,&bs
,t
->size
,len
,flags
);
2029 /* Mail fetch partial message text
2030 * Accepts: mail stream
2031 * message # to fetch
2032 * MIME section specifier (#.#.#...#)
2033 * offset of first designed byte or 0 to start at beginning
2034 * maximum number of bytes or 0 for all bytes
2036 * Returns: T if successful, else NIL
2039 long mail_partial_text (MAILSTREAM
*stream
,unsigned long msgno
,char *section
,
2040 unsigned long first
,unsigned long last
,long flags
)
2047 char tmp
[MAILTMPLEN
];
2049 if (!mailgets
) fatal ("mail_partial_text() called without a mailgets!");
2050 if (section
&& (strlen (section
) > (MAILTMPLEN
- 20))) return NIL
;
2051 if (flags
& FT_UID
) { /* UID form of call */
2052 if ((msgno
= mail_msgno (stream
,msgno
)) != 0L) flags
&= ~FT_UID
;
2053 else return NIL
; /* must get UID/msgno map first */
2055 elt
= mail_elt (stream
,msgno
);/* get cache data */
2056 flags
&= ~FT_INTERNAL
; /* bogus if this is set */
2057 if (section
&& *section
) { /* nested body text wanted? */
2058 if (!((b
= mail_body (stream
,msgno
,section
)) &&
2059 (b
->type
== TYPEMESSAGE
) && !strcmp (b
->subtype
,"RFC822")))
2060 return NIL
; /* lose if no body or not MESSAGE/RFC822 */
2061 p
= &b
->nested
.msg
->text
; /* point at nested message */
2062 /* build IMAP-format section specifier */
2063 sprintf (tmp
,"%s.TEXT",section
);
2065 else { /* else top-level message text wanted */
2066 p
= &elt
->private.msg
.text
;
2067 strcpy (tmp
,"TEXT");
2070 /* initialize message data identifier */
2071 INIT_GETS (md
,stream
,msgno
,tmp
,first
,last
);
2072 if (p
->text
.data
) { /* is data already cached? */
2073 INIT (&bs
,mail_string
,p
->text
.data
,i
= p
->text
.size
);
2074 markseen (stream
,elt
,flags
);/* mark message seen */
2076 else { /* else get data from driver */
2077 if (!stream
->dtb
) return NIL
;
2078 if (stream
->dtb
->msgdata
) /* driver will handle this */
2079 return (*stream
->dtb
->msgdata
) (stream
,msgno
,tmp
,first
,last
,NIL
,flags
);
2080 if (!(*stream
->dtb
->text
) (stream
,msgno
,&bs
,flags
)) return NIL
;
2081 if (section
&& *section
) { /* nexted if more complex */
2082 SETPOS (&bs
,p
->offset
); /* offset stringstruct to data */
2083 i
= p
->text
.size
; /* maximum size of data */
2085 else i
= SIZE (&bs
); /* just want this much */
2087 if (i
<= first
) i
= first
= 0;/* first byte is beyond end of text */
2088 /* truncate as needed */
2089 else { /* offset and truncate */
2090 SETPOS (&bs
,first
+ GETPOS (&bs
));
2091 i
-= first
; /* reduced size */
2092 if (last
&& (i
> last
)) i
= last
;
2094 /* do the mailgets thing */
2095 (*mailgets
) (mail_read
,&bs
,i
,&md
);
2096 return T
; /* success */
2099 /* Mail fetch partial message body part
2100 * Accepts: mail stream
2101 * message # to fetch
2102 * MIME section specifier (#.#.#...#)
2103 * offset of first designed byte or 0 to start at beginning
2104 * maximum number of bytes or 0 for all bytes
2106 * Returns: T if successful, else NIL
2109 long mail_partial_body (MAILSTREAM
*stream
,unsigned long msgno
,char *section
,
2110 unsigned long first
,unsigned long last
,long flags
)
2118 if (!(section
&& *section
)) /* top-level text wanted? */
2119 return mail_partial_text (stream
,msgno
,NIL
,first
,last
,flags
);
2120 if (!mailgets
) fatal ("mail_partial_body() called without a mailgets!");
2121 if (flags
& FT_UID
) { /* UID form of call */
2122 if ((msgno
= mail_msgno (stream
,msgno
)) != 0L) flags
&= ~FT_UID
;
2123 else return NIL
; /* must get UID/msgno map first */
2125 /* must have body */
2126 if (!(b
= mail_body (stream
,msgno
,section
))) return NIL
;
2127 flags
&= ~FT_INTERNAL
; /* bogus if this is set */
2129 /* initialize message data identifier */
2130 INIT_GETS (md
,stream
,msgno
,section
,first
,last
);
2131 /* have cached text? */
2132 if ((t
= &(p
= &b
->contents
)->text
)->data
) {
2133 /* mark message seen */
2134 markseen (stream
,mail_elt (stream
,msgno
),flags
);
2135 INIT (&bs
,mail_string
,t
->data
,i
= t
->size
);
2137 else { /* else get data from driver */
2138 if (!stream
->dtb
) return NIL
;
2139 if (stream
->dtb
->msgdata
) /* driver will handle this */
2140 return (*stream
->dtb
->msgdata
) (stream
,msgno
,section
,first
,last
,NIL
,
2142 if (!(*stream
->dtb
->text
) (stream
,msgno
,&bs
,flags
)) return NIL
;
2143 if (section
&& *section
) { /* nexted if more complex */
2144 SETPOS (&bs
,p
->offset
); /* offset stringstruct to data */
2145 i
= t
->size
; /* maximum size of data */
2147 else i
= SIZE (&bs
); /* just want this much */
2149 if (i
<= first
) i
= first
= 0;/* first byte is beyond end of text */
2150 else { /* offset and truncate */
2151 SETPOS (&bs
,first
+ GETPOS (&bs
));
2152 i
-= first
; /* reduced size */
2153 if (last
&& (i
> last
)) i
= last
;
2155 /* do the mailgets thing */
2156 (*mailgets
) (mail_read
,&bs
,i
,&md
);
2157 return T
; /* success */
2160 /* Mail return message text
2161 * Accepts: identifier data
2163 * pointer to returned length
2167 char *mail_fetch_text_return (GETS_DATA
*md
,SIZEDTEXT
*t
,unsigned long *len
)
2170 if (len
) *len
= t
->size
; /* return size */
2171 if (t
->size
&& mailgets
) { /* have to do the mailgets thing? */
2172 /* silly but do it anyway for consistency */
2173 INIT (&bs
,mail_string
,t
->data
,t
->size
);
2174 return (*mailgets
) (mail_read
,&bs
,t
->size
,md
);
2176 return t
->size
? (char *) t
->data
: "";
2180 /* Mail return message string
2181 * Accepts: identifier data
2184 * pointer to returned length
2186 * Returns: text, or NIL if stringstruct returned
2189 char *mail_fetch_string_return (GETS_DATA
*md
,STRING
*bs
,unsigned long i
,
2190 unsigned long *len
,long flags
)
2193 if (len
) *len
= i
; /* return size */
2194 /* return stringstruct hack */
2195 if (flags
& FT_RETURNSTRINGSTRUCT
) {
2196 memcpy (&md
->stream
->private.string
,bs
,sizeof (STRING
));
2197 SETPOS (&md
->stream
->private.string
,GETPOS (&md
->stream
->private.string
));
2199 /* have to do the mailgets thing? */
2200 else if (mailgets
) ret
= (*mailgets
) (mail_read
,bs
,i
,md
);
2201 /* special hack to avoid extra copy */
2202 else if (bs
->dtb
->next
== mail_string_next
) ret
= bs
->curpos
;
2203 /* make string copy in memory */
2204 else ret
= textcpyoffstring (&md
->stream
->text
,bs
,GETPOS (bs
),i
);
2208 /* Read data from stringstruct
2209 * Accepts: stringstruct
2210 * size of data to read
2211 * buffer to read into
2212 * Returns: T, always, stringstruct updated
2215 long mail_read (void *stream
,unsigned long size
,char *buffer
)
2218 STRING
*s
= (STRING
*) stream
;
2219 while (size
) { /* until satisfied */
2220 memcpy (buffer
,s
->curpos
,i
= min (s
->cursize
,size
));
2221 buffer
+= i
; /* update buffer */
2222 size
-= i
; /* note that we read this much */
2223 s
->curpos
+= --i
; /* advance that many spaces minus 1 */
2225 SNX (s
); /* now use SNX to advance the last byte */
2231 * Accepts: mail stream
2233 * Returns: UID or zero if dead stream
2236 unsigned long mail_uid (MAILSTREAM
*stream
,unsigned long msgno
)
2238 unsigned long uid
= mail_elt (stream
,msgno
)->private.uid
;
2240 (stream
->dtb
&& stream
->dtb
->uid
) ? (*stream
->dtb
->uid
) (stream
,msgno
) : 0;
2244 /* Mail fetch msgno from UID
2245 * Accepts: mail stream
2247 * Returns: msgno or zero if failed
2250 unsigned long mail_msgno (MAILSTREAM
*stream
,unsigned long uid
)
2252 unsigned long msgno
,delta
,first
,firstuid
,last
,lastuid
,middle
,miduid
;
2253 if (stream
->dtb
) { /* active stream? */
2254 if (stream
->dtb
->msgno
) /* direct way */
2255 return (*stream
->dtb
->msgno
) (stream
,uid
);
2256 else if (stream
->dtb
->uid
) {/* indirect way */
2257 /* Placeholder for now, since currently there are no drivers which
2258 * have a uid method but not a msgno method
2260 for (msgno
= 1; msgno
<= stream
->nmsgs
; msgno
++)
2261 if ((*stream
->dtb
->uid
) (stream
,msgno
) == uid
) return msgno
;
2263 /* binary search since have full map */
2264 else for (first
= 1,last
= stream
->nmsgs
, delta
= (first
<= last
) ? 1 : 0;
2266 (uid
>= (firstuid
= mail_elt (stream
,first
)->private.uid
)) &&
2267 (uid
<= (lastuid
= mail_elt (stream
,last
)->private.uid
));) {
2268 /* done if match at an endpoint */
2269 if (uid
== firstuid
) return first
;
2270 if (uid
== lastuid
) return last
;
2271 /* have anything between endpoints? */
2272 if ((delta
= ((last
- first
) / 2)) != 0L){
2273 if ((miduid
= mail_elt (stream
,middle
= first
+ delta
)->private.uid
)
2275 return middle
; /* found match in middle */
2276 else if (uid
< miduid
) last
= middle
- 1;
2277 else first
= middle
+ 1;
2281 else { /* dead stream, do linear search for UID */
2282 for (msgno
= 1; msgno
<= stream
->nmsgs
; msgno
++)
2283 if (mail_elt (stream
,msgno
)->private.uid
== uid
) return msgno
;
2285 return 0; /* didn't find the UID anywhere */
2288 /* Mail fetch From string for menu
2289 * Accepts: destination string
2291 * message # to fetch
2292 * desired string length
2293 * Returns: string of requested length
2296 void mail_fetchfrom (char *s
,MAILSTREAM
*stream
,unsigned long msgno
,
2300 char tmp
[MAILTMPLEN
];
2301 ENVELOPE
*env
= mail_fetchenvelope (stream
,msgno
);
2302 ADDRESS
*adr
= env
? env
->from
: NIL
;
2303 memset (s
,' ',(size_t)length
);/* fill it with spaces */
2304 s
[length
] = '\0'; /* tie off with null */
2305 /* get first from address from envelope */
2306 while (adr
&& !adr
->host
) adr
= adr
->next
;
2307 if (adr
) { /* if a personal name exists use it */
2308 if (!(t
= adr
->personal
))
2309 sprintf (t
= tmp
,"%.256s@%.256s",adr
->mailbox
,adr
->host
);
2310 memcpy (s
,t
,(size_t) min (length
,(long) strlen (t
)));
2315 /* Mail fetch Subject string for menu
2316 * Accepts: destination string
2318 * message # to fetch
2319 * desired string length
2320 * Returns: string of no more than requested length
2323 void mail_fetchsubject (char *s
,MAILSTREAM
*stream
,unsigned long msgno
,
2326 ENVELOPE
*env
= mail_fetchenvelope (stream
,msgno
);
2327 memset (s
,'\0',(size_t) length
+1);
2328 /* copy subject from envelope */
2329 if (env
&& env
->subject
) strncpy (s
,env
->subject
,(size_t) length
);
2330 else *s
= ' '; /* if no subject then just a space */
2333 /* Mail modify flags
2334 * Accepts: mail stream
2340 void mail_flag (MAILSTREAM
*stream
,char *sequence
,char *flag
,long flags
)
2346 if (!stream
->dtb
) return; /* no-op if no stream */
2347 if ((stream
->dtb
->flagmsg
|| !stream
->dtb
->flag
) &&
2348 ((flags
& ST_UID
) ? mail_uid_sequence (stream
,sequence
) :
2349 mail_sequence (stream
,sequence
)) &&
2350 ((f
= mail_parse_flags (stream
,flag
,&uf
)) || uf
))
2351 for (i
= 1,nf
= (flags
& ST_SET
) ? T
: NIL
; i
<= stream
->nmsgs
; i
++)
2352 if ((elt
= mail_elt (stream
,i
))->sequence
) {
2353 struct { /* old flags */
2354 unsigned int valid
: 1;
2355 unsigned int seen
: 1;
2356 unsigned int deleted
: 1;
2357 unsigned int flagged
: 1;
2358 unsigned int answered
: 1;
2359 unsigned int draft
: 1;
2360 unsigned long user_flags
;
2362 old
.valid
= elt
->valid
; old
.seen
= elt
->seen
;
2363 old
.deleted
= elt
->deleted
; old
.flagged
= elt
->flagged
;
2364 old
.answered
= elt
->answered
; old
.draft
= elt
->draft
;
2365 old
.user_flags
= elt
->user_flags
;
2366 elt
->valid
= NIL
; /* prepare for flag alteration */
2367 if (stream
->dtb
->flagmsg
) (*stream
->dtb
->flagmsg
) (stream
,elt
);
2368 if (f
&fSEEN
) elt
->seen
= nf
;
2369 if (f
&fDELETED
) elt
->deleted
= nf
;
2370 if (f
&fFLAGGED
) elt
->flagged
= nf
;
2371 if (f
&fANSWERED
) elt
->answered
= nf
;
2372 if (f
&fDRAFT
) elt
->draft
= nf
;
2374 if (flags
& ST_SET
) elt
->user_flags
|= uf
;
2375 else elt
->user_flags
&= ~uf
;
2376 elt
->valid
= T
; /* flags now altered */
2377 if ((old
.valid
!= elt
->valid
) || (old
.seen
!= elt
->seen
) ||
2378 (old
.deleted
!= elt
->deleted
) || (old
.flagged
!= elt
->flagged
) ||
2379 (old
.answered
!= elt
->answered
) || (old
.draft
!= elt
->draft
) ||
2380 (old
.user_flags
!= elt
->user_flags
))
2381 MM_FLAGS (stream
,elt
->msgno
);
2382 if (stream
->dtb
->flagmsg
) (*stream
->dtb
->flagmsg
) (stream
,elt
);
2384 /* call driver once */
2385 if (stream
->dtb
->flag
) (*stream
->dtb
->flag
) (stream
,sequence
,flag
,flags
);
2388 /* Mail search for messages
2389 * Accepts: mail stream
2393 * Returns: T if successful, NIL if dead stream, NIL searchpgm or bad charset
2396 long mail_search_full (MAILSTREAM
*stream
,char *charset
,SEARCHPGM
*pgm
,
2401 if (!(flags
& SE_RETAIN
)) /* clear search vector unless retaining */
2402 for (i
= 1; i
<= stream
->nmsgs
; ++i
) mail_elt (stream
,i
)->searched
= NIL
;
2403 if (pgm
&& stream
->dtb
) /* must have a search program and driver */
2404 ret
= (*(stream
->dtb
->search
? stream
->dtb
->search
: mail_search_default
))
2405 (stream
,charset
,pgm
,flags
);
2406 /* flush search program if requested */
2407 if (flags
& SE_FREE
) mail_free_searchpgm (&pgm
);
2412 /* Mail search for messages default handler
2413 * Accepts: mail stream
2417 * Returns: T if successful, NIL if bad charset
2420 long mail_search_default (MAILSTREAM
*stream
,char *charset
,SEARCHPGM
*pgm
,
2425 /* make sure that charset is good */
2426 if ((msg
= utf8_badcharset (charset
)) != NULL
) {
2427 MM_LOG (msg
,ERROR
); /* output error */
2428 fs_give ((void **) &msg
);
2431 utf8_searchpgm (pgm
,charset
);
2432 for (i
= 1; i
<= stream
->nmsgs
; ++i
)
2433 if (mail_search_msg (stream
,i
,NIL
,pgm
)) {
2434 if (flags
& SE_UID
) mm_searched (stream
,mail_uid (stream
,i
));
2435 else { /* mark as searched, notify mail program */
2436 mail_elt (stream
,i
)->searched
= T
;
2437 if (!stream
->silent
) mm_searched (stream
,i
);
2440 return LONGT
; /* search completed */
2443 /* Mail ping mailbox
2444 * Accepts: mail stream
2445 * Returns: stream if still open else NIL
2448 long mail_ping (MAILSTREAM
*stream
)
2450 unsigned long i
,n
,uf
,len
;
2451 char *s
,*f
,tmp
[MAILTMPLEN
],flags
[MAILTMPLEN
];
2456 /* do driver action */
2457 if ((ret
= ((stream
&& stream
->dtb
) ? (stream
->dtb
->ping
) (stream
) : NIL
)) &&
2458 stream
->snarf
.name
&& /* time to snarf? */
2459 /* prohibit faster than once/min */
2460 (time (0) > (time_t) (stream
->snarf
.time
+ min(60,mailsnarfinterval
))) &&
2461 (snarf
= mail_open (NIL
,stream
->snarf
.name
,
2462 stream
->snarf
.options
| OP_SILENT
))) {
2463 if ((n
= snarf
->nmsgs
) && /* yes, have messages to snarf? */
2464 mail_search_full (snarf
,NIL
,mail_criteria ("UNDELETED"),SE_FREE
)) {
2465 for (i
= 1; ret
&& (i
<= n
); i
++) /* for each message */
2466 if ((elt
= mail_elt (snarf
,i
))->searched
&&
2467 (s
= mail_fetch_message (snarf
,i
,&len
,FT_PEEK
)) && len
) {
2468 INIT (&bs
,mail_string
,s
,len
);
2469 if (mailsnarfpreserve
) {
2470 /* yes, make sure have fast data */
2471 if (!elt
->valid
|| !elt
->day
) {
2472 sprintf (tmp
,"%lu",n
);
2473 mail_fetch_fast (snarf
,tmp
,NIL
);
2475 /* initialize flag string */
2476 memset (flags
,0,MAILTMPLEN
);
2477 /* output system flags except \Deleted */
2478 if (elt
->seen
) strcat (flags
," \\Seen");
2479 if (elt
->flagged
) strcat (flags
," \\Flagged");
2480 if (elt
->answered
) strcat (flags
," \\Answered");
2481 if (elt
->draft
) strcat (flags
," \\Draft");
2482 /* any user flags? */
2483 for (uf
= elt
->user_flags
,s
= flags
+ strlen (flags
);
2484 uf
&& (f
= stream
->user_flags
[find_rightmost_bit (&uf
)]) &&
2485 ((MAILTMPLEN
- (s
- tmp
)) > (long) (2 + strlen (f
)));
2486 s
+= strlen (s
)) sprintf (s
," %s",f
);
2487 ret
= mail_append_full (stream
,stream
->mailbox
,flags
+ 1,
2488 mail_date (tmp
,elt
),&bs
);
2490 else ret
= mail_append (stream
,stream
->mailbox
,&bs
);
2492 if (ret
) { /* did snarf succeed? */
2493 /* driver has per-message (or no) flag call */
2494 if (snarf
->dtb
->flagmsg
|| !snarf
->dtb
->flag
) {
2495 elt
->valid
= NIL
; /* prepare for flag alteration */
2496 if (snarf
->dtb
->flagmsg
) (*snarf
->dtb
->flagmsg
) (snarf
,elt
);
2497 /* flags now altered */
2498 elt
->deleted
= elt
->seen
= elt
->valid
= T
;
2499 if (snarf
->dtb
->flagmsg
) (*snarf
->dtb
->flagmsg
) (snarf
,elt
);
2501 /* driver has one-time flag call */
2502 if (snarf
->dtb
->flag
) {
2503 sprintf (tmp
,"%lu",i
);
2504 (*snarf
->dtb
->flag
) (snarf
,tmp
,"\\Deleted \\Seen",ST_SET
);
2507 else { /* copy failed */
2508 sprintf (tmp
,"Unable to move message %lu from %s mailbox",
2509 i
,snarf
->dtb
->name
);
2514 /* expunge the messages */
2515 mail_close_full (snarf
,n
? CL_EXPUNGE
: NIL
);
2516 stream
->snarf
.time
= (unsigned long) time (0);
2517 /* Even if the snarf failed, we don't want to return NIL if the stream
2518 * is still alive. Or at least that's what we currently think.
2520 /* redo the driver's action */
2521 ret
= stream
->dtb
? (*stream
->dtb
->ping
) (stream
) : NIL
;
2526 /* Mail check mailbox
2527 * Accepts: mail stream
2530 void mail_check (MAILSTREAM
*stream
)
2532 /* do the driver's action */
2533 if (stream
->dtb
) (*stream
->dtb
->check
) (stream
);
2537 /* Mail expunge mailbox
2538 * Accepts: mail stream
2539 * sequence to expunge if non-NIL
2541 * Returns: T on success, NIL on failure
2544 long mail_expunge_full (MAILSTREAM
*stream
,char *sequence
,long options
)
2546 /* do the driver's action */
2547 return stream
->dtb
? (*stream
->dtb
->expunge
) (stream
,sequence
,options
) : NIL
;
2551 /* Mail copy message(s)
2552 * Accepts: mail stream
2554 * destination mailbox
2558 long mail_copy_full (MAILSTREAM
*stream
,char *sequence
,char *mailbox
,
2561 return stream
->dtb
?
2562 SAFE_COPY (stream
->dtb
,stream
,sequence
,mailbox
,options
) : NIL
;
2565 /* Append data package to use for old single-message mail_append() interface */
2567 typedef struct mail_append_package
{
2568 char *flags
; /* initial flags */
2569 char *date
; /* message internal date */
2570 STRING
*message
; /* stringstruct of message */
2574 /* Single append message string
2575 * Accepts: mail stream
2576 * package pointer (cast as a void *)
2577 * pointer to return initial flags
2578 * pointer to return message internal date
2579 * pointer to return stringstruct of message to append
2580 * Returns: T, always
2583 static long mail_append_single (MAILSTREAM
*stream
,void *data
,char **flags
,
2584 char **date
,STRING
**message
)
2586 APPENDPACKAGE
*ap
= (APPENDPACKAGE
*) data
;
2587 *flags
= ap
->flags
; /* get desired data from the package */
2589 *message
= ap
->message
;
2590 ap
->message
= NIL
; /* so next callback puts a stop to it */
2591 return LONGT
; /* always return success */
2595 /* Mail append message string
2596 * Accepts: mail stream
2597 * destination mailbox
2599 * message internal date
2600 * stringstruct of message to append
2601 * Returns: T on success, NIL on failure
2604 long mail_append_full (MAILSTREAM
*stream
,char *mailbox
,char *flags
,char *date
,
2608 ap
.flags
= flags
; /* load append package */
2610 ap
.message
= message
;
2611 return mail_append_multiple (stream
,mailbox
,mail_append_single
,(void *) &ap
);
2614 /* Mail append message(s)
2615 * Accepts: mail stream
2616 * destination mailbox
2617 * append data callback
2618 * arbitrary data for callback use
2619 * Returns: T on success, NIL on failure
2622 long mail_append_multiple (MAILSTREAM
*stream
,char *mailbox
,append_t af
,
2625 char *s
,tmp
[MAILTMPLEN
];
2628 /* never allow names with newlines */
2629 if (strpbrk (mailbox
,"\015\012"))
2630 MM_LOG ("Can't append to mailbox with such a name",ERROR
);
2631 else if (strlen (mailbox
) >=
2632 (NETMAXHOST
+(NETMAXUSER
*2)+NETMAXMBX
+NETMAXSRV
+50)) {
2633 sprintf (tmp
,"Can't append %.80s: %s",mailbox
,(*mailbox
== '{') ?
2634 "invalid remote specification" : "no such mailbox");
2637 /* special driver hack? */
2638 else if (!strncmp (lcase (strcpy (tmp
,mailbox
)),"#driver.",8)) {
2639 /* yes, tie off name at likely delimiter */
2640 if (!(s
= strpbrk (tmp
+8,"/\\:"))) {
2641 sprintf (tmp
,"Can't append to mailbox %.80s: bad driver syntax",mailbox
);
2645 *s
++ = '\0'; /* tie off at delimiter */
2646 if (!(d
= (DRIVER
*) mail_parameters (NIL
,GET_DRIVER
,tmp
+8))) {
2647 sprintf (tmp
,"Can't append to mailbox %.80s: unknown driver",mailbox
);
2650 else ret
= SAFE_APPEND (d
,stream
,mailbox
+ (s
- tmp
),af
,data
);
2652 else if ((d
= mail_valid (stream
,mailbox
,NIL
)) != NULL
)
2653 ret
= SAFE_APPEND (d
,stream
,mailbox
,af
,data
);
2654 /* No driver, try for TRYCREATE if no stream. Note that we use the
2655 * createProto here, not the appendProto, since the dummy driver already
2656 * took care of the appendProto case. Otherwise, if appendProto is set to
2657 * NIL, we won't get a TRYCREATE.
2659 else if (!stream
&& (stream
= default_proto (NIL
)) && stream
->dtb
&&
2660 SAFE_APPEND (stream
->dtb
,stream
,mailbox
,af
,data
))
2662 MM_NOTIFY (stream
,"Append validity confusion",WARN
);
2663 /* generate error message */
2664 else mail_valid (stream
,mailbox
,"append to mailbox");
2668 /* Mail garbage collect stream
2669 * Accepts: mail stream
2670 * garbage collection flags
2673 void mail_gc (MAILSTREAM
*stream
,long gcflags
)
2677 /* do the driver's action first */
2678 if (stream
->dtb
&& stream
->dtb
->gc
) (*stream
->dtb
->gc
) (stream
,gcflags
);
2679 stream
->msgno
= 0; /* nothing cached now */
2680 if (gcflags
& GC_ENV
) { /* garbage collect envelopes? */
2681 if (stream
->env
) mail_free_envelope (&stream
->env
);
2682 if (stream
->body
) mail_free_body (&stream
->body
);
2684 if (gcflags
& GC_TEXTS
) { /* free texts */
2685 if (stream
->text
.data
) fs_give ((void **) &stream
->text
.data
);
2686 stream
->text
.size
= 0;
2688 /* garbage collect per-message stuff */
2689 for (i
= 1; i
<= stream
->nmsgs
; i
++)
2690 if ((elt
= (MESSAGECACHE
*) (*mailcache
) (stream
,i
,CH_ELT
)) != NULL
)
2691 mail_gc_msg (&elt
->private.msg
,gcflags
);
2695 /* Mail garbage collect message
2696 * Accepts: message structure
2697 * garbage collection flags
2700 void mail_gc_msg (MESSAGE
*msg
,long gcflags
)
2702 if (gcflags
& GC_ENV
) { /* garbage collect envelopes? */
2703 mail_free_envelope (&msg
->env
);
2704 mail_free_body (&msg
->body
);
2706 if (gcflags
& GC_TEXTS
) { /* garbage collect texts */
2707 if (msg
->full
.text
.data
) fs_give ((void **) &msg
->full
.text
.data
);
2708 if (msg
->header
.text
.data
) {
2709 mail_free_stringlist (&msg
->lines
);
2710 fs_give ((void **) &msg
->header
.text
.data
);
2712 if (msg
->text
.text
.data
) fs_give ((void **) &msg
->text
.text
.data
);
2713 /* now GC all body components */
2714 if (msg
->body
) mail_gc_body (msg
->body
);
2718 /* Mail garbage collect texts in BODY structure
2719 * Accepts: BODY structure
2722 void mail_gc_body (BODY
*body
)
2725 switch (body
->type
) { /* free contents */
2726 case TYPEMULTIPART
: /* multiple part */
2727 for (part
= body
->nested
.part
; part
; part
= part
->next
)
2728 mail_gc_body (&part
->body
);
2730 case TYPEMESSAGE
: /* encapsulated message */
2731 if (body
->subtype
&& !strcmp (body
->subtype
,"RFC822")) {
2732 mail_free_stringlist (&body
->nested
.msg
->lines
);
2733 mail_gc_msg (body
->nested
.msg
,GC_TEXTS
);
2739 if (body
->mime
.text
.data
) fs_give ((void **) &body
->mime
.text
.data
);
2740 if (body
->contents
.text
.data
) fs_give ((void **) &body
->contents
.text
.data
);
2742 /* Mail get body section
2743 * Accepts: body of message
2745 * Returns: pointer to body at given section
2748 BODY
*mail_body_section (BODY
*b
, unsigned char *section
)
2752 /* make sure have a body */
2753 if (section
&& *section
&& b
)
2754 while (*section
) { /* find desired section */
2755 if (isdigit (*section
)) { /* get section specifier */
2756 /* make sure what follows is valid */
2757 if (!(i
= strtoul (section
,(char **) §ion
,10)) ||
2758 (*section
&& ((*section
++ != '.') || !*section
))) return NIL
;
2759 /* multipart content? */
2760 if (b
->type
== TYPEMULTIPART
) {
2761 /* yes, find desired part */
2762 if ((pt
= b
->nested
.part
) != NULL
) while (--i
&& (pt
= pt
->next
));
2763 if (!pt
) return NIL
; /* bad specifier */
2764 b
= &pt
->body
; /* note new body */
2766 /* otherwise must be section 1 */
2767 else if (i
!= 1) return NIL
;
2768 /* need to go down further? */
2769 if (*section
) switch (b
->type
) {
2770 case TYPEMULTIPART
: /* multipart */
2772 case TYPEMESSAGE
: /* embedded message */
2773 if (!strcmp (b
->subtype
,"RFC822")) {
2774 b
= b
->nested
.msg
->body
;
2777 default: /* bogus subpart specification */
2781 else return NIL
; /* unknown section specifier */
2786 /* Mail get body part
2787 * Accepts: mail stream
2790 * Returns: pointer to body
2793 BODY
*mail_body (MAILSTREAM
*stream
,unsigned long msgno
,unsigned char *section
)
2796 /* make sure have a body */
2797 if (section
&& *section
&& mail_fetchstructure (stream
,msgno
,&b
) && b
)
2798 return mail_body_section(b
, section
);
2802 /* Mail output date from elt fields
2803 * Accepts: character string to write into
2804 * elt to get data data from
2805 * Returns: the character string
2808 const char *days
[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
2810 const char *months
[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2811 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
2813 char *mail_date (char *string
,MESSAGECACHE
*elt
)
2815 sprintf (string
,"%2d-%s-%d %02d:%02d:%02d %c%02d%02d",
2816 elt
->day
? elt
->day
: 1,
2817 months
[elt
->month
? (elt
->month
- 1) : 0],
2818 elt
->year
+ BASEYEAR
,elt
->hours
,elt
->minutes
,elt
->seconds
,
2819 elt
->zoccident
? '-' : '+',elt
->zhours
,elt
->zminutes
);
2824 /* Mail output extended-ctime format date from elt fields
2825 * Accepts: character string to write into
2826 * elt to get data data from
2827 * Returns: the character string
2830 char *mail_cdate (char *string
,MESSAGECACHE
*elt
)
2832 char *fmt
= "%s %s %2d %02d:%02d:%02d %4d %s%02d%02d\n";
2833 int d
= elt
->day
? elt
->day
: 1;
2834 int m
= elt
->month
? (elt
->month
- 1) : 0;
2835 int y
= elt
->year
+ BASEYEAR
;
2836 const char *s
= months
[m
];
2837 if (m
< 2) { /* if before March, */
2838 m
+= 10; /* January = month 10 of previous year */
2841 else m
-= 2; /* March is month 0 */
2842 sprintf (string
,fmt
,days
[(int) (d
+ 2 + ((7 + 31 * m
) / 12)
2843 #ifndef USEJULIANCALENDAR
2844 #ifndef USEORTHODOXCALENDAR /* Gregorian calendar */
2849 #else /* Orthodox calendar */
2850 + (2 * (y
/ 900)) + ((y
% 900) >= 200)
2851 + ((y
% 900) >= 600)
2855 + y
+ (y
/ 4)) % 7],
2856 s
,d
,elt
->hours
,elt
->minutes
,elt
->seconds
,elt
->year
+ BASEYEAR
,
2857 elt
->zoccident
? "-" : "+",elt
->zhours
,elt
->zminutes
);
2861 /* Mail parse date into elt fields
2862 * Accepts: elt to write into
2863 * date string to parse
2864 * Returns: T if parse successful, else NIL
2865 * This routine parses dates as follows:
2866 * . leading three alphas followed by comma and space are ignored
2867 * . date accepted in format: mm/dd/yy, mm/dd/yyyy, dd-mmm-yy, dd-mmm-yyyy,
2868 * dd mmm yy, dd mmm yyyy, yyyy-mm-dd, yyyymmdd
2869 * . two and three digit years interpreted according to RFC 2822 rules
2870 * . mandatory end of string if yyyy-mm-dd or yyyymmdd; otherwise optional
2871 * space followed by time:
2872 * . time accepted in format hh:mm:ss or hh:mm
2873 * . end of string accepted
2874 * . timezone accepted: hyphen followed by symbolic timezone, or space
2875 * followed by signed numeric timezone or symbolic timezone
2876 * Examples of normal input:
2877 * . IMAP date-only (SEARCH):
2879 * . IMAP date-time (INTERNALDATE):
2880 * dd-mmm-yyyy hh:mm:ss +zzzz
2882 * www, dd mmm yy hh:mm:ss zzz
2884 * www, dd mmm yyyy hh:mm:ss +zzzz
2887 long mail_parse_date (MESSAGECACHE
*elt
,unsigned char *s
)
2889 unsigned long d
,m
,y
;
2893 char tmp
[MAILTMPLEN
];
2894 static unsigned long maxyear
= 0;
2895 if (!maxyear
) { /* know the end of time yet? */
2896 MESSAGECACHE tmpelt
;
2897 memset (&tmpelt
,0xff,sizeof (MESSAGECACHE
));
2898 maxyear
= BASEYEAR
+ tmpelt
.year
;
2901 elt
->zoccident
= elt
->zhours
= elt
->zminutes
=
2902 elt
->hours
= elt
->minutes
= elt
->seconds
=
2903 elt
->day
= elt
->month
= elt
->year
= 0;
2904 /* make a writeable uppercase copy */
2905 if (s
&& *s
&& (strlen (s
) < (size_t)MAILTMPLEN
)) s
= ucase (strcpy (tmp
,s
));
2907 /* skip over possible day of week */
2908 if (isalpha (*s
) && isalpha (s
[1]) && isalpha (s
[2]) && (s
[3] == ',') &&
2909 (s
[4] == ' ')) s
+= 5;
2910 while (*s
== ' ') s
++; /* parse first number (probable month) */
2911 if (!(m
= strtoul (s
,(char **) &s
,10))) return NIL
;
2913 switch (*s
) { /* different parse based on delimiter */
2914 case '/': /* mm/dd/yy format */
2915 if (isdigit (*++s
) && (d
= strtoul (s
,(char **) &s
,10)) &&
2916 (*s
== '/') && isdigit (*++s
)) {
2917 y
= strtoul (s
,(char **) &s
,10);
2918 if (*s
== '\0') break; /* must end here */
2920 return NIL
; /* bogon */
2921 case ' ': /* dd mmm yy format */
2922 while (s
[1] == ' ') s
++; /* slurp extra whitespace */
2924 if (isdigit (s
[1])) { /* possible ISO 8601 date format? */
2925 y
= m
; /* yes, first number is year */
2926 /* get month and day */
2927 if ((m
= strtoul (s
+1,(char **) &s
,10)) && (*s
++ == '-') &&
2928 (d
= strtoul (s
,(char **) &s
,10)) && !*s
) break;
2929 return NIL
; /* syntax error or time present */
2931 d
= m
; /* dd-mmm-yy[yy], so first number is a day */
2932 /* make sure string long enough! */
2933 if (strlen (s
) < (size_t) 5) return NIL
;
2934 /* Some compilers don't allow `<<' and/or longs in case statements. */
2935 /* slurp up the month string */
2936 ms
= ((s
[1] - 'A') * 1024) + ((s
[2] - 'A') * 32) + (s
[3] - 'A');
2937 switch (ms
) { /* determine the month */
2938 case (('J'-'A') * 1024) + (('A'-'A') * 32) + ('N'-'A'): m
= 1; break;
2939 case (('F'-'A') * 1024) + (('E'-'A') * 32) + ('B'-'A'): m
= 2; break;
2940 case (('M'-'A') * 1024) + (('A'-'A') * 32) + ('R'-'A'): m
= 3; break;
2941 case (('A'-'A') * 1024) + (('P'-'A') * 32) + ('R'-'A'): m
= 4; break;
2942 case (('M'-'A') * 1024) + (('A'-'A') * 32) + ('Y'-'A'): m
= 5; break;
2943 case (('J'-'A') * 1024) + (('U'-'A') * 32) + ('N'-'A'): m
= 6; break;
2944 case (('J'-'A') * 1024) + (('U'-'A') * 32) + ('L'-'A'): m
= 7; break;
2945 case (('A'-'A') * 1024) + (('U'-'A') * 32) + ('G'-'A'): m
= 8; break;
2946 case (('S'-'A') * 1024) + (('E'-'A') * 32) + ('P'-'A'): m
= 9; break;
2947 case (('O'-'A') * 1024) + (('C'-'A') * 32) + ('T'-'A'): m
= 10; break;
2948 case (('N'-'A') * 1024) + (('O'-'A') * 32) + ('V'-'A'): m
= 11; break;
2949 case (('D'-'A') * 1024) + (('E'-'A') * 32) + ('C'-'A'): m
= 12; break;
2950 default: return NIL
; /* unknown month */
2952 if (s
[4] == *s
) s
+= 5; /* advance to year */
2953 else { /* first three were OK, possibly full name */
2954 mi
= *s
; /* note delimiter, skip alphas */
2955 for (s
+= 4; isalpha (*s
); s
++);
2956 /* error if delimiter not here */
2957 if (mi
!= *s
++) return NIL
;
2959 while (*s
== ' ') s
++; /* parse year */
2960 if (isdigit (*s
)) { /* must be a digit here */
2961 y
= strtoul (s
,(char **) &s
,10);
2962 if (*s
== '\0' || *s
== ' ') break;
2964 case '\0': /* ISO 8601 compact date */
2965 if (m
< (BASEYEAR
* 10000)) return NIL
;
2966 y
= m
/ 10000; /* get year */
2967 d
= (m
%= 10000) % 100; /* get day */
2968 m
/= 100; /* and month */
2971 return NIL
; /* unknown date format */
2974 /* minimal validity check of date */
2975 if ((d
> 31) || (m
> 12)) return NIL
;
2976 if (y
< 49) y
+= 2000; /* RFC 2282 rules for two digit years 00-49 */
2977 else if (y
< 999) y
+= 1900; /* 2-digit years 50-99 and 3-digit years */
2978 /* reject prehistoric and far future years */
2979 if ((y
< BASEYEAR
) || (y
> maxyear
)) return NIL
;
2980 /* set values in elt */
2981 elt
->day
= d
; elt
->month
= m
; elt
->year
= y
- BASEYEAR
;
2982 ms
= '\0'; /* initially no time zone string */
2983 if (*s
) { /* time specification present? */
2985 d
= strtoul (s
+1,(char **) &s
,10);
2986 if (*s
!= ':') return NIL
;
2987 m
= strtoul (++s
,(char **) &s
,10);
2988 y
= (*s
== ':') ? strtoul (++s
,(char **) &s
,10) : 0;
2989 /* validity check time */
2990 if ((d
> 23) || (m
> 59) || (y
> 60)) return NIL
;
2991 /* set values in elt */
2992 elt
->hours
= d
; elt
->minutes
= m
; elt
->seconds
= y
;
2993 switch (*s
) { /* time zone specifier? */
2994 case ' ': /* numeric time zone */
2995 while (s
[1] == ' ') s
++; /* slurp extra whitespace */
2996 if (!isalpha (s
[1])) { /* treat as '-' case if alphabetic */
2997 /* test for sign character */
2998 if ((elt
->zoccident
= (*++s
== '-')) || (*s
== '+')) s
++;
2999 /* validate proper timezone */
3000 if (isdigit(*s
) && isdigit(s
[1]) && isdigit(s
[2]) && (s
[2] < '6') &&
3002 elt
->zhours
= (*s
- '0') * 10 + (s
[1] - '0');
3003 elt
->zminutes
= (s
[2] - '0') * 10 + (s
[3] - '0');
3005 return T
; /* all done! */
3008 case '-': /* symbolic time zone */
3009 if (!(ms
= *++s
)) ms
= 'Z';
3010 else if (*++s
) { /* multi-character? */
3011 ms
-= 'A'; ms
*= 1024; /* yes, make compressed three-byte form */
3012 ms
+= ((*s
++ - 'A') * 32);
3013 if (*s
) ms
+= *s
++ - 'A';
3014 if (*s
) ms
= '\0'; /* more than three characters */
3016 default: /* ignore anything else */
3021 /* This is not intended to be a comprehensive list of all possible
3022 * timezone strings. Such a list would be impractical. Rather, this
3023 * listing is intended to incorporate all military, North American, and
3024 * a few special cases such as Japan and the major European zone names,
3025 * such as what might be expected to be found in a Tenex format mailbox
3026 * and spewed from an IMAP server. The trend is to migrate to numeric
3027 * timezones which lack the flavor but also the ambiguity of the names.
3029 * RFC-822 only recognizes UT, GMT, 1-letter military timezones, and the
3030 * 4 CONUS timezones and their summer time variants. [Sorry, Canadian
3031 * Atlantic Provinces, Alaska, and Hawaii.]
3033 switch (ms
) { /* determine the timezone */
3035 case (('U'-'A')*1024)+(('T'-'A')*32):
3036 #ifndef STRICT_RFC822_TIMEZONES
3037 case (('U'-'A')*1024)+(('T'-'A')*32)+'C'-'A':
3040 case (('G'-'A')*1024)+(('M'-'A')*32)+'T'-'A':
3041 case 'Z': elt
->zhours
= 0; break;
3043 /* oriental (from Greenwich) timezones */
3044 #ifndef STRICT_RFC822_TIMEZONES
3046 case (('M'-'A')*1024)+(('E'-'A')*32)+'T'-'A':
3048 #ifdef BRITISH_SUMMER_TIME
3049 /* British Summer */
3050 case (('B'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
3052 case 'A': elt
->zhours
= 1; break;
3053 #ifndef STRICT_RFC822_TIMEZONES
3054 /* Eastern Europe */
3055 case (('E'-'A')*1024)+(('E'-'A')*32)+'T'-'A':
3057 case 'B': elt
->zhours
= 2; break;
3058 case 'C': elt
->zhours
= 3; break;
3059 case 'D': elt
->zhours
= 4; break;
3060 case 'E': elt
->zhours
= 5; break;
3061 case 'F': elt
->zhours
= 6; break;
3062 case 'G': elt
->zhours
= 7; break;
3063 case 'H': elt
->zhours
= 8; break;
3064 #ifndef STRICT_RFC822_TIMEZONES
3066 case (('J'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
3068 case 'I': elt
->zhours
= 9; break;
3069 case 'K': elt
->zhours
= 10; break;
3070 case 'L': elt
->zhours
= 11; break;
3071 case 'M': elt
->zhours
= 12; break;
3073 /* occidental (from Greenwich) timezones */
3074 case 'N': elt
->zoccident
= 1; elt
->zhours
= 1; break;
3075 case 'O': elt
->zoccident
= 1; elt
->zhours
= 2; break;
3076 #ifndef STRICT_RFC822_TIMEZONES
3077 case (('A'-'A')*1024)+(('D'-'A')*32)+'T'-'A':
3079 case 'P': elt
->zoccident
= 1; elt
->zhours
= 3; break;
3080 #ifdef NEWFOUNDLAND_STANDARD_TIME
3082 case (('N'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
3083 elt
->zoccident
= 1; elt
->zhours
= 3; elt
->zminutes
= 30; break;
3085 #ifndef STRICT_RFC822_TIMEZONES
3087 case (('A'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
3090 case (('E'-'A')*1024)+(('D'-'A')*32)+'T'-'A':
3091 case 'Q': elt
->zoccident
= 1; elt
->zhours
= 4; break;
3093 case (('E'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
3094 case (('C'-'A')*1024)+(('D'-'A')*32)+'T'-'A':
3095 case 'R': elt
->zoccident
= 1; elt
->zhours
= 5; break;
3097 case (('C'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
3098 case (('M'-'A')*1024)+(('D'-'A')*32)+'T'-'A':
3099 case 'S': elt
->zoccident
= 1; elt
->zhours
= 6; break;
3101 case (('M'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
3102 case (('P'-'A')*1024)+(('D'-'A')*32)+'T'-'A':
3103 case 'T': elt
->zoccident
= 1; elt
->zhours
= 7; break;
3105 case (('P'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
3106 #ifndef STRICT_RFC822_TIMEZONES
3107 case (('Y'-'A')*1024)+(('D'-'A')*32)+'T'-'A':
3109 case 'U': elt
->zoccident
= 1; elt
->zhours
= 8; break;
3110 #ifndef STRICT_RFC822_TIMEZONES
3112 case (('Y'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
3114 case 'V': elt
->zoccident
= 1; elt
->zhours
= 9; break;
3115 #ifndef STRICT_RFC822_TIMEZONES
3117 case (('H'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
3119 case 'W': elt
->zoccident
= 1; elt
->zhours
= 10; break;
3120 /* Nome/Bering/Samoa */
3121 #ifdef NOME_STANDARD_TIME
3122 case (('N'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
3124 #ifdef BERING_STANDARD_TIME
3125 case (('B'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
3127 #ifdef SAMOA_STANDARD_TIME
3128 case (('S'-'A')*1024)+(('S'-'A')*32)+'T'-'A':
3130 case 'X': elt
->zoccident
= 1; elt
->zhours
= 11; break;
3131 case 'Y': elt
->zoccident
= 1; elt
->zhours
= 12; break;
3133 default: /* unknown time zones treated as local */
3134 tn
= time (0); /* time now... */
3135 t
= localtime (&tn
); /* get local minutes since midnight */
3136 mi
= t
->tm_hour
* 60 + t
->tm_min
;
3137 ms
= t
->tm_yday
; /* note Julian day */
3138 if ((t
= gmtime (&tn
)) != NULL
) { /* minus UTC minutes since midnight */
3139 mi
-= t
->tm_hour
* 60 + t
->tm_min
;
3140 /* ms can be one of:
3141 * 36x local time is December 31, UTC is January 1, offset -24 hours
3142 * 1 local time is 1 day ahead of UTC, offset +24 hours
3143 * 0 local time is same day as UTC, no offset
3144 * -1 local time is 1 day behind UTC, offset -24 hours
3145 * -36x local time is January 1, UTC is December 31, offset +24 hours
3147 if (ms
-= t
->tm_yday
) /* correct offset if different Julian day */
3148 mi
+= ((ms
< 0) == (abs (ms
) == 1)) ? -24*60 : 24*60;
3149 if (mi
< 0) { /* occidental? */
3150 mi
= abs (mi
); /* yup, make positive number */
3151 elt
->zoccident
= 1; /* and note west of UTC */
3153 elt
->zhours
= mi
/ 60; /* now break into hours and minutes */
3154 elt
->zminutes
= mi
% 60;
3161 /* Mail n messages exist
3162 * Accepts: mail stream
3163 * number of messages
3166 void mail_exists (MAILSTREAM
*stream
,unsigned long nmsgs
)
3168 char tmp
[MAILTMPLEN
];
3169 if (nmsgs
> MAXMESSAGES
) {
3170 sprintf (tmp
,"Mailbox has more messages (%lu) exist than maximum (%lu)",
3173 nmsgs
= MAXMESSAGES
; /* cap to maximum */
3174 /* probably will crash in mail_elt() soon enough... */
3176 /* make sure cache is large enough */
3177 (*mailcache
) (stream
,nmsgs
,CH_SIZE
);
3178 stream
->nmsgs
= nmsgs
; /* update stream status */
3179 /* notify main program of change */
3180 if (!stream
->silent
) MM_EXISTS (stream
,nmsgs
);
3184 /* Mail n messages are recent
3185 * Accepts: mail stream
3186 * number of recent messages
3189 void mail_recent (MAILSTREAM
*stream
,unsigned long recent
)
3191 char tmp
[MAILTMPLEN
];
3192 if (recent
<= stream
->nmsgs
) stream
->recent
= recent
;
3194 sprintf (tmp
,"Non-existent recent message(s) %lu, nmsgs=%lu",
3195 recent
,stream
->nmsgs
);
3201 /* Mail message n is expunged
3202 * Accepts: mail stream
3206 void mail_expunged (MAILSTREAM
*stream
,unsigned long msgno
)
3208 char tmp
[MAILTMPLEN
];
3210 if (msgno
> stream
->nmsgs
) {
3211 sprintf (tmp
,"Expunge of non-existent message %lu, nmsgs=%lu",
3212 msgno
,stream
->nmsgs
);
3216 elt
= (MESSAGECACHE
*) (*mailcache
) (stream
,msgno
,CH_ELT
);
3217 /* notify main program of change */
3218 if (!stream
->silent
) MM_EXPUNGED (stream
,msgno
);
3219 if (elt
) { /* if an element is there */
3220 elt
->msgno
= 0; /* invalidate its message number and free */
3221 (*mailcache
) (stream
,msgno
,CH_FREE
);
3222 (*mailcache
) (stream
,msgno
,CH_FREESORTCACHE
);
3224 /* expunge the slot */
3225 (*mailcache
) (stream
,msgno
,CH_EXPUNGE
);
3226 --stream
->nmsgs
; /* update stream status */
3227 if (stream
->msgno
) { /* have stream pointers? */
3228 /* make sure the short cache is nuked */
3229 if (stream
->scache
) mail_gc (stream
,GC_ENV
| GC_TEXTS
);
3230 else stream
->msgno
= 0; /* make sure invalidated in any case */
3235 /* Mail stream status routines */
3239 * Accepts: mail stream
3242 void mail_lock (MAILSTREAM
*stream
)
3245 char tmp
[MAILTMPLEN
];
3246 sprintf (tmp
,"Lock when already locked, mbx=%.80s",
3247 stream
->mailbox
? stream
->mailbox
: "???");
3250 else stream
->lock
= T
; /* lock stream */
3254 /* Mail unlock stream
3255 * Accepts: mail stream
3258 void mail_unlock (MAILSTREAM
*stream
)
3260 if (!stream
->lock
) fatal ("Unlock when not locked");
3261 else stream
->lock
= NIL
; /* unlock stream */
3265 /* Mail turn on debugging telemetry
3266 * Accepts: mail stream
3269 void mail_debug (MAILSTREAM
*stream
)
3271 stream
->debug
= T
; /* turn on debugging telemetry */
3272 if (stream
->dtb
) (*stream
->dtb
->parameters
) (ENABLE_DEBUG
,stream
);
3276 /* Mail turn off debugging telemetry
3277 * Accepts: mail stream
3280 void mail_nodebug (MAILSTREAM
*stream
)
3282 stream
->debug
= NIL
; /* turn off debugging telemetry */
3283 if (stream
->dtb
) (*stream
->dtb
->parameters
) (DISABLE_DEBUG
,stream
);
3287 /* Mail log to debugging telemetry
3289 * flag that data is "sensitive"
3292 void mail_dlog (char *string
,long flag
)
3294 mm_dlog ((debugsensitive
|| !flag
) ? string
: "<suppressed>");
3297 /* Mail parse UID sequence
3298 * Accepts: mail stream
3300 * Returns: T if parse successful, else NIL
3303 long mail_uid_sequence (MAILSTREAM
*stream
,unsigned char *sequence
)
3305 unsigned long i
,j
,k
,x
,y
;
3306 for (i
= 1; i
<= stream
->nmsgs
; i
++) mail_elt (stream
,i
)->sequence
= NIL
;
3307 while (sequence
&& *sequence
){/* while there is something to parse */
3308 if (*sequence
== '*') { /* maximum message */
3309 i
= stream
->nmsgs
? mail_uid (stream
,stream
->nmsgs
) : stream
->uid_last
;
3310 sequence
++; /* skip past * */
3312 /* parse and validate message number */
3313 /* parse and validate message number */
3314 else if (!isdigit (*sequence
)) {
3315 MM_LOG ("Syntax error in sequence",ERROR
);
3318 else if (!(i
= strtoul (sequence
,(char **) &sequence
,10))) {
3319 MM_LOG ("UID may not be zero",ERROR
);
3322 switch (*sequence
) { /* see what the delimiter is */
3323 case ':': /* sequence range */
3324 if (*++sequence
== '*') { /* maximum message */
3325 j
= stream
->nmsgs
? mail_uid (stream
,stream
->nmsgs
) : stream
->uid_last
;
3326 sequence
++; /* skip past * */
3328 /* parse end of range */
3329 else if (!(j
= strtoul (sequence
,(char **) &sequence
,10))) {
3330 MM_LOG ("UID sequence range invalid",ERROR
);
3333 if (*sequence
&& *sequence
++ != ',') {
3334 MM_LOG ("UID sequence range syntax error",ERROR
);
3337 if (i
> j
) { /* swap the range if backwards */
3338 x
= i
; i
= j
; j
= x
;
3340 x
= mail_msgno (stream
,i
);/* get msgnos */
3341 y
= mail_msgno (stream
,j
);/* for both UIDS (don't && it) */
3342 /* easy if both UIDs valid */
3343 if (x
&& y
) while (x
<= y
) mail_elt (stream
,x
++)->sequence
= T
;
3344 /* start UID valid, end is not */
3345 else if (x
) while ((x
<= stream
->nmsgs
) && (mail_uid (stream
,x
) <= j
))
3346 mail_elt (stream
,x
++)->sequence
= T
;
3347 /* end UID valid, start is not */
3348 else if (y
) for (x
= 1; x
<= y
; x
++) {
3349 if (mail_uid (stream
,x
) >= i
) mail_elt (stream
,x
)->sequence
= T
;
3351 /* neither is valid, ugh */
3352 else for (x
= 1; x
<= stream
->nmsgs
; x
++)
3353 if (((k
= mail_uid (stream
,x
)) >= i
) && (k
<= j
))
3354 mail_elt (stream
,x
)->sequence
= T
;
3356 case ',': /* single message */
3357 ++sequence
; /* skip the delimiter, fall into end case */
3358 case '\0': /* end of sequence, mark this message */
3359 if ((x
= mail_msgno (stream
,i
)) != 0L) mail_elt (stream
,x
)->sequence
= T
;
3361 default: /* anything else is a syntax error! */
3362 MM_LOG ("UID sequence syntax error",ERROR
);
3366 return T
; /* successfully parsed sequence */
3369 /* Mail see if line list matches that in cache
3370 * Accepts: candidate line list
3373 * Returns: T if match, NIL if no match
3376 long mail_match_lines (STRINGLIST
*lines
,STRINGLIST
*msglines
,long flags
)
3379 unsigned char *s
,*t
;
3381 if (!msglines
) return T
; /* full header is in cache */
3382 /* need full header but filtered in cache */
3383 if ((flags
& FT_NOT
) || !lines
) return NIL
;
3384 do { /* make sure all present & accounted for */
3385 for (m
= msglines
; m
; m
= m
->next
) if (lines
->text
.size
== m
->text
.size
) {
3386 for (s
= lines
->text
.data
,t
= m
->text
.data
,i
= lines
->text
.size
;
3387 i
&& !compare_uchar (*s
,*t
); s
++,t
++,i
--);
3388 if (!i
) break; /* this line matches */
3390 if (!m
) return NIL
; /* didn't find in the list */
3392 while ((lines
= lines
->next
) != NULL
);
3393 return T
; /* all lines found */
3396 /* Mail filter text by header lines
3397 * Accepts: text to filter, with trailing null
3401 * Returns: new text size, text overwritten
3404 unsigned long mail_filter (char *text
,unsigned long len
,STRINGLIST
*lines
,
3410 char c
,*s
,*e
,*t
,tmp
[MAILTMPLEN
];
3413 char *end
= text
+ len
;
3414 text
[len
] = '\012'; /* guard against running off buffer */
3415 while (src
< end
) { /* process header */
3416 /* slurp header line name */
3417 for (s
= src
,e
= s
+ MAILTMPLEN
- 1,e
= (e
< end
? e
: end
),t
= tmp
;
3418 (s
< e
) && ((c
= (*s
? *s
: (*s
= ' '))) != ':') &&
3420 ((c
!= ' ') && (c
!= '\t') && (c
!= '\015') && (c
!= '\012')));
3422 *t
= '\0'; /* tie off */
3423 notfound
= T
; /* not found yet */
3424 if ((i
= t
- tmp
) != 0L) /* see if found in header */
3425 for (hdrs
= lines
; hdrs
&& notfound
; hdrs
= hdrs
->next
)
3426 if ((hdrs
->text
.size
== i
) && !compare_csizedtext (tmp
,&hdrs
->text
))
3428 /* skip header line if not wanted */
3429 if (i
&& ((flags
& FT_NOT
) ? !notfound
: notfound
))
3430 while (((*src
++ != '\012') && (*src
++ != '\012') && (*src
++ != '\012') &&
3431 (*src
++ != '\012') && (*src
++ != '\012') && (*src
++ != '\012') &&
3432 (*src
++ != '\012') && (*src
++ != '\012') && (*src
++ != '\012') &&
3433 (*src
++ != '\012')) ||
3434 ((src
< end
) && ((*src
== ' ') || (*src
== '\t'))));
3435 else if (src
== dst
) { /* copy to self */
3436 while (((*src
++ != '\012') && (*src
++ != '\012') && (*src
++ != '\012') &&
3437 (*src
++ != '\012') && (*src
++ != '\012') && (*src
++ != '\012') &&
3438 (*src
++ != '\012') && (*src
++ != '\012') && (*src
++ != '\012') &&
3439 (*src
++ != '\012')) ||
3440 ((src
< end
) && ((*src
== ' ') || (*src
== '\t'))));
3441 dst
= src
; /* update destination */
3443 else { /* copy line and any continuation line */
3444 while ((((*dst
++ = *src
++) != '\012') && ((*dst
++ = *src
++) != '\012') &&
3445 ((*dst
++ = *src
++) != '\012') && ((*dst
++ = *src
++) != '\012') &&
3446 ((*dst
++ = *src
++) != '\012') && ((*dst
++ = *src
++) != '\012') &&
3447 ((*dst
++ = *src
++) != '\012') && ((*dst
++ = *src
++) != '\012') &&
3448 ((*dst
++ = *src
++) != '\012') && ((*dst
++ = *src
++) != '\012'))||
3449 ((src
< end
) && ((*src
== ' ') || (*src
== '\t'))));
3450 /* in case hit the guard LF */
3451 if (src
> end
) dst
-= (src
- end
);
3454 *dst
= '\0'; /* tie off destination */
3458 /* Local mail search message
3459 * Accepts: MAIL stream
3461 * optional section specification
3463 * Returns: T if found, NIL otherwise
3466 long mail_search_msg (MAILSTREAM
*stream
,unsigned long msgno
,char *section
,
3470 char tmp
[MAILTMPLEN
];
3471 MESSAGECACHE
*elt
= mail_elt (stream
,msgno
);
3475 unsigned long now
= (unsigned long) time (0);
3476 if (pgm
->msgno
|| pgm
->uid
) { /* message set searches */
3478 /* message sequences */
3479 if (pgm
->msgno
) { /* inside this message sequence set */
3480 for (set
= pgm
->msgno
; set
; set
= set
->next
)
3481 if (set
->last
? ((set
->first
<= set
->last
) ?
3482 ((msgno
>= set
->first
) && (msgno
<= set
->last
)) :
3483 ((msgno
>= set
->last
) && (msgno
<= set
->first
))) :
3484 msgno
== set
->first
) break;
3485 if (!set
) return NIL
; /* not found within sequence */
3487 if (pgm
->uid
) { /* inside this unique identifier set */
3488 unsigned long uid
= mail_uid (stream
,msgno
);
3489 for (set
= pgm
->uid
; set
; set
= set
->next
)
3490 if (set
->last
? ((set
->first
<= set
->last
) ?
3491 ((uid
>= set
->first
) && (uid
<= set
->last
)) :
3492 ((uid
>= set
->last
) && (uid
<= set
->first
))) :
3493 uid
== set
->first
) break;
3494 if (!set
) return NIL
; /* not found within sequence */
3498 /* Fast data searches */
3499 /* need to fetch fast data? */
3500 if ((!elt
->rfc822_size
&& (pgm
->larger
|| pgm
->smaller
)) ||
3501 (!elt
->year
&& (pgm
->before
|| pgm
->on
|| pgm
->since
||
3502 pgm
->older
|| pgm
->younger
)) ||
3503 (!elt
->valid
&& (pgm
->answered
|| pgm
->unanswered
||
3504 pgm
->deleted
|| pgm
->undeleted
||
3505 pgm
->draft
|| pgm
->undraft
||
3506 pgm
->flagged
|| pgm
->unflagged
||
3507 pgm
->recent
|| pgm
->old
||
3508 pgm
->seen
|| pgm
->unseen
||
3509 pgm
->keyword
|| pgm
->unkeyword
))) {
3512 for (i
= elt
->msgno
; /* find last unloaded message in range */
3513 (i
< stream
->nmsgs
) && (ielt
= mail_elt (stream
,i
+1)) &&
3514 ((!ielt
->rfc822_size
&& (pgm
->larger
|| pgm
->smaller
)) ||
3515 (!ielt
->year
&& (pgm
->before
|| pgm
->on
|| pgm
->since
||
3516 pgm
->older
|| pgm
->younger
)) ||
3517 (!ielt
->valid
&& (pgm
->answered
|| pgm
->unanswered
||
3518 pgm
->deleted
|| pgm
->undeleted
||
3519 pgm
->draft
|| pgm
->undraft
||
3520 pgm
->flagged
|| pgm
->unflagged
||
3521 pgm
->recent
|| pgm
->old
||
3522 pgm
->seen
|| pgm
->unseen
||
3523 pgm
->keyword
|| pgm
->unkeyword
))); ++i
);
3524 if (i
== elt
->msgno
) sprintf (tmp
,"%lu",elt
->msgno
);
3525 else sprintf (tmp
,"%lu:%lu",elt
->msgno
,i
);
3526 mail_fetch_fast (stream
,tmp
,NIL
);
3529 if ((pgm
->larger
&& (elt
->rfc822_size
<= pgm
->larger
)) ||
3530 (pgm
->smaller
&& (elt
->rfc822_size
>= pgm
->smaller
))) return NIL
;
3532 if ((pgm
->answered
&& !elt
->answered
) ||
3533 (pgm
->unanswered
&& elt
->answered
) ||
3534 (pgm
->deleted
&& !elt
->deleted
) ||
3535 (pgm
->undeleted
&& elt
->deleted
) ||
3536 (pgm
->draft
&& !elt
->draft
) ||
3537 (pgm
->undraft
&& elt
->draft
) ||
3538 (pgm
->flagged
&& !elt
->flagged
) ||
3539 (pgm
->unflagged
&& elt
->flagged
) ||
3540 (pgm
->recent
&& !elt
->recent
) ||
3541 (pgm
->old
&& elt
->recent
) ||
3542 (pgm
->seen
&& !elt
->seen
) ||
3543 (pgm
->unseen
&& elt
->seen
)) return NIL
;
3545 if ((pgm
->keyword
&& !mail_search_keyword (stream
,elt
,pgm
->keyword
,LONGT
)) ||
3546 (pgm
->unkeyword
&& !mail_search_keyword (stream
,elt
,pgm
->unkeyword
,NIL
)))
3548 /* internal date ranges */
3549 if (pgm
->before
|| pgm
->on
|| pgm
->since
) {
3550 d
= mail_shortdate (elt
->year
,elt
->month
,elt
->day
);
3551 if (pgm
->before
&& (d
>= pgm
->before
)) return NIL
;
3552 if (pgm
->on
&& (d
!= pgm
->on
)) return NIL
;
3553 if (pgm
->since
&& (d
< pgm
->since
)) return NIL
;
3555 if (pgm
->older
|| pgm
->younger
) {
3556 unsigned long msgd
= mail_longdate (elt
);
3557 if (pgm
->older
&& msgd
> (now
- pgm
->older
)) return NIL
;
3558 if (pgm
->younger
&& msgd
< (now
- pgm
->younger
)) return NIL
;
3561 /* envelope searches */
3562 if (pgm
->sentbefore
|| pgm
->senton
|| pgm
->sentsince
||
3563 pgm
->bcc
|| pgm
->cc
|| pgm
->from
|| pgm
->to
|| pgm
->subject
||
3564 pgm
->return_path
|| pgm
->sender
|| pgm
->reply_to
|| pgm
->in_reply_to
||
3565 pgm
->message_id
|| pgm
->newsgroups
|| pgm
->followup_to
||
3569 if (section
) { /* use body part envelope */
3570 BODY
*body
= mail_body (stream
,msgno
,section
);
3571 env
= (body
&& (body
->type
== TYPEMESSAGE
) && body
->subtype
&&
3572 !strcmp (body
->subtype
,"RFC822")) ? body
->nested
.msg
->env
: NIL
;
3574 else { /* use top level envelope if no section */
3575 if (pgm
->header
&& !stream
->scache
&& !(stream
->dtb
->flags
& DR_LOCAL
))
3576 mail_fetch_header(stream
,msgno
,NIL
,NIL
,NIL
,FT_PEEK
|FT_SEARCHLOOKAHEAD
);
3577 env
= mail_fetchenvelope (stream
,msgno
);
3579 if (!env
) return NIL
; /* no envelope obtained */
3580 /* sent date ranges */
3581 if ((pgm
->sentbefore
|| pgm
->senton
|| pgm
->sentsince
) &&
3582 (!mail_parse_date (&delt
,env
->date
) ||
3583 !(d
= mail_shortdate (delt
.year
,delt
.month
,delt
.day
)) ||
3584 (pgm
->sentbefore
&& (d
>= pgm
->sentbefore
)) ||
3585 (pgm
->senton
&& (d
!= pgm
->senton
)) ||
3586 (pgm
->sentsince
&& (d
< pgm
->sentsince
)))) return NIL
;
3587 /* search headers */
3588 if ((pgm
->bcc
&& !mail_search_addr (env
->bcc
,pgm
->bcc
)) ||
3589 (pgm
->cc
&& !mail_search_addr (env
->cc
,pgm
->cc
)) ||
3590 (pgm
->from
&& !mail_search_addr (env
->from
,pgm
->from
)) ||
3591 (pgm
->to
&& !mail_search_addr (env
->to
,pgm
->to
)) ||
3592 (pgm
->subject
&& !mail_search_header_text (env
->subject
,pgm
->subject
)))
3594 /* These criteria are not supported by IMAP and have to be emulated */
3595 if ((pgm
->return_path
&&
3596 !mail_search_addr (env
->return_path
,pgm
->return_path
)) ||
3597 (pgm
->sender
&& !mail_search_addr (env
->sender
,pgm
->sender
)) ||
3598 (pgm
->reply_to
&& !mail_search_addr (env
->reply_to
,pgm
->reply_to
)) ||
3599 (pgm
->in_reply_to
&&
3600 !mail_search_header_text (env
->in_reply_to
,pgm
->in_reply_to
)) ||
3602 !mail_search_header_text (env
->message_id
,pgm
->message_id
)) ||
3604 !mail_search_header_text (env
->newsgroups
,pgm
->newsgroups
)) ||
3605 (pgm
->followup_to
&&
3606 !mail_search_header_text (env
->followup_to
,pgm
->followup_to
)) ||
3608 !mail_search_header_text (env
->references
,pgm
->references
)))
3612 /* search header lines */
3613 for (hdr
= pgm
->header
; hdr
; hdr
= hdr
->next
) {
3617 sth
.next
= stc
.next
= NIL
; /* only one at a time */
3618 sth
.text
.data
= hdr
->line
.data
;
3619 sth
.text
.size
= hdr
->line
.size
;
3620 /* get the header text */
3621 if ((t
= mail_fetch_header (stream
,msgno
,NIL
,&sth
,&s
.size
,
3622 FT_INTERNAL
| FT_PEEK
|
3623 (section
? NIL
: FT_SEARCHLOOKAHEAD
))) &&
3625 if (hdr
->text
.size
) { /* anything matches empty search string */
3626 /* non-empty, copy field data */
3627 s
.data
= (unsigned char *) fs_get (s
.size
+ 1);
3629 for (v
= (char *) s
.data
, e
= t
+ s
.size
; t
< e
;) switch (*t
) {
3630 default: /* non-continuation, skip leading field name */
3631 while ((t
< e
) && (*t
++ != ':'));
3632 if ((t
< e
) && (*t
== ':')) t
++;
3633 case '\t': case ' ': /* copy field data */
3634 while ((t
< e
) && (*t
!= '\015') && (*t
!= '\012')) *v
++ = *t
++;
3635 *v
++ = '\n'; /* tie off line */
3636 while (((*t
== '\015') || (*t
== '\012')) && (t
< e
)) t
++;
3638 /* calculate true size */
3639 s
.size
= v
- (char *) s
.data
;
3640 *v
= '\0'; /* tie off results */
3641 stc
.text
.data
= hdr
->text
.data
;
3642 stc
.text
.size
= hdr
->text
.size
;
3644 if (mail_search_header (&s
,&stc
)) fs_give ((void **) &s
.data
);
3645 else { /* search failed */
3646 fs_give ((void **) &s
.data
);
3651 else return NIL
; /* no matching header text */
3653 /* search strings */
3654 if ((pgm
->text
&& !mail_search_text (stream
,msgno
,section
,pgm
->text
,LONGT
))||
3655 (pgm
->body
&& !mail_search_text (stream
,msgno
,section
,pgm
->body
,NIL
)))
3657 /* logical conditions */
3658 for (or = pgm
->or; or; or = or->next
)
3659 if (!(mail_search_msg (stream
,msgno
,section
,or->first
) ||
3660 mail_search_msg (stream
,msgno
,section
,or->second
))) return NIL
;
3661 for (not = pgm
->not; not; not = not->next
)
3662 if (mail_search_msg (stream
,msgno
,section
,not->pgm
)) return NIL
;
3666 /* Mail search message header null-terminated text
3667 * Accepts: header text
3669 * Returns: T if search found a match
3672 long mail_search_header_text (char *s
,STRINGLIST
*st
)
3675 /* have any text? */
3676 if ((h
.data
= (unsigned char *) s
) != NULL
) {
3677 h
.size
= strlen (s
); /* yes, get its size */
3678 return mail_search_header (&h
,st
);
3684 /* Mail search message header
3685 * Accepts: header as sized text
3687 * Returns: T if search found a match
3690 long mail_search_header (SIZEDTEXT
*hdr
,STRINGLIST
*st
)
3694 /* make UTF-8 version of header */
3695 utf8_mime2text (hdr
,&h
,U8T_CANONICAL
);
3696 while (h
.size
&& ((h
.data
[h
.size
-1]=='\015') || (h
.data
[h
.size
-1]=='\012')))
3697 --h
.size
; /* slice off trailing newlines */
3698 do if (h
.size
? /* search non-empty string */
3699 !ssearch (h
.data
,h
.size
,st
->text
.data
,st
->text
.size
) : st
->text
.size
)
3701 while (ret
&& (st
= st
->next
));
3702 if (h
.data
!= hdr
->data
) fs_give ((void **) &h
.data
);
3706 /* Mail search message body
3707 * Accepts: MAIL stream
3709 * optional section specification
3712 * Returns: T if search found a match
3715 long mail_search_text (MAILSTREAM
*stream
,unsigned long msgno
,char *section
,
3716 STRINGLIST
*st
,long flags
)
3720 STRINGLIST
*s
= mail_newstringlist ();
3721 mailgets_t omg
= mailgets
;
3722 if (stream
->dtb
->flags
& DR_LOWMEM
) mailgets
= mail_search_gets
;
3723 /* strings to search */
3724 for (stream
->private.search
.string
= s
; st
;) {
3725 s
->text
.data
= st
->text
.data
;
3726 s
->text
.size
= st
->text
.size
;
3727 if ((st
= st
->next
) != NULL
) s
= s
->next
= mail_newstringlist ();
3729 stream
->private.search
.text
= NIL
;
3730 if (flags
) { /* want header? */
3732 s
.data
= (unsigned char *)
3733 mail_fetch_header (stream
,msgno
,section
,NIL
,&s
.size
,FT_INTERNAL
|FT_PEEK
);
3734 utf8_mime2text (&s
,&t
,U8T_CANONICAL
);
3735 ret
= mail_search_string_work (&t
,&stream
->private.search
.string
);
3736 if (t
.data
!= s
.data
) fs_give ((void **) &t
.data
);
3738 if (!ret
) { /* still looking for match? */
3739 /* no section, get top-level body */
3740 if (!section
) mail_fetchstructure (stream
,msgno
,&body
);
3741 /* get body of nested message */
3742 else if ((body
= mail_body (stream
,msgno
,section
)) &&
3743 (body
->type
== TYPEMULTIPART
) && body
->subtype
&&
3744 !strcmp (body
->subtype
,"RFC822")) body
= body
->nested
.msg
->body
;
3745 if (body
) ret
= mail_search_body (stream
,msgno
,body
,NIL
,1,flags
);
3747 mailgets
= omg
; /* restore former gets routine */
3748 /* clear searching */
3749 for (s
= stream
->private.search
.string
; s
; s
= s
->next
) s
->text
.data
= NIL
;
3750 mail_free_stringlist (&stream
->private.search
.string
);
3751 stream
->private.search
.text
= NIL
;
3755 /* Mail search message body text parts
3756 * Accepts: MAIL stream
3758 * current body pointer
3759 * hierarchical level prefix
3760 * position at current hierarchical level
3763 * Returns: T if search found a match
3766 long mail_search_body (MAILSTREAM
*stream
,unsigned long msgno
,BODY
*body
,
3767 char *prefix
,unsigned long section
,long flags
)
3771 char *s
,*t
,sect
[MAILTMPLEN
];
3775 if (prefix
&& (strlen (prefix
) > (MAILTMPLEN
- 20))) return NIL
;
3776 sprintf (sect
,"%s%lu",prefix
? prefix
: "",section
++);
3777 if (flags
&& prefix
) { /* want to search MIME header too? */
3778 st
.data
= (unsigned char *) mail_fetch_mime (stream
,msgno
,sect
,&st
.size
,
3779 FT_INTERNAL
| FT_PEEK
);
3780 if (stream
->dtb
->flags
& DR_LOWMEM
) ret
= stream
->private.search
.result
;
3782 /* make UTF-8 version of header */
3783 utf8_mime2text (&st
,&h
,U8T_CANONICAL
);
3784 ret
= mail_search_string_work (&h
,&stream
->private.search
.string
);
3785 if (h
.data
!= st
.data
) fs_give ((void **) &h
.data
);
3788 if (!ret
) switch (body
->type
) {
3790 /* extend prefix if not first time */
3791 s
= prefix
? strcat (sect
,".") : "";
3792 for (i
= 1,part
= body
->nested
.part
; part
&& !ret
; i
++,part
= part
->next
)
3793 ret
= mail_search_body (stream
,msgno
,&part
->body
,s
,i
,flags
);
3796 if (!strcmp (body
->subtype
,"RFC822")) {
3797 if (flags
) { /* want to search nested message header? */
3798 st
.data
= (unsigned char *)
3799 mail_fetch_header (stream
,msgno
,sect
,NIL
,&st
.size
,
3800 FT_INTERNAL
| FT_PEEK
);
3801 if (stream
->dtb
->flags
& DR_LOWMEM
) ret
=stream
->private.search
.result
;
3803 /* make UTF-8 version of header */
3804 utf8_mime2text (&st
,&h
,U8T_CANONICAL
);
3805 ret
= mail_search_string_work (&h
,&stream
->private.search
.string
);
3806 if (h
.data
!= st
.data
) fs_give ((void **) &h
.data
);
3809 if ((body
= body
->nested
.msg
->body
) != NULL
)
3810 ret
= (body
->type
== TYPEMULTIPART
) ?
3811 mail_search_body (stream
,msgno
,body
,(prefix
? prefix
: ""),
3812 section
- 1,flags
) :
3813 mail_search_body (stream
,msgno
,body
,strcat (sect
,"."),1,flags
);
3816 /* non-MESSAGE/RFC822 falls into text case */
3819 s
= mail_fetch_body (stream
,msgno
,sect
,&i
,FT_INTERNAL
| FT_PEEK
);
3820 if (stream
->dtb
->flags
& DR_LOWMEM
) ret
= stream
->private.search
.result
;
3822 for (t
= NIL
,param
= body
->parameter
; param
&& !t
; param
= param
->next
)
3823 if (!strcmp (param
->attribute
,"CHARSET")) t
= param
->value
;
3824 switch (body
->encoding
) { /* what encoding? */
3826 if ((st
.data
= (unsigned char *)
3827 rfc822_base64 ((unsigned char *) s
,i
,&st
.size
)) != NULL
) {
3828 ret
= mail_search_string (&st
,t
,&stream
->private.search
.string
);
3829 fs_give ((void **) &st
.data
);
3832 case ENCQUOTEDPRINTABLE
:
3833 if ((st
.data
= rfc822_qprint ((unsigned char *) s
,i
,&st
.size
)) != NULL
) {
3834 ret
= mail_search_string (&st
,t
,&stream
->private.search
.string
);
3835 fs_give ((void **) &st
.data
);
3839 st
.data
= (unsigned char *) s
;
3841 ret
= mail_search_string (&st
,t
,&stream
->private.search
.string
);
3851 * Accepts: sized text to search
3852 * character set of sized text
3853 * string list of search keys
3854 * Returns: T if search found a match
3857 long mail_search_string (SIZEDTEXT
*s
,char *charset
,STRINGLIST
**st
)
3861 STRINGLIST
**sc
= st
;
3862 /* convert to UTF-8 as best we can */
3863 if (!utf8_text (s
,charset
,&u
,U8T_CANONICAL
))
3864 utf8_text (s
,NIL
,&u
,U8T_CANONICAL
);
3865 ret
= mail_search_string_work (&u
,st
);
3866 if (u
.data
!= s
->data
) fs_give ((void **) &u
.data
);
3871 /* Mail search text worker routine
3872 * Accepts: sized text to search
3873 * string list of search keys
3874 * Returns: T if search found a match
3877 long mail_search_string_work (SIZEDTEXT
*s
,STRINGLIST
**st
)
3880 STRINGLIST
**sc
= st
;
3881 while (*sc
) { /* run down criteria list */
3882 if (ssearch (s
->data
,s
->size
,(*sc
)->text
.data
,(*sc
)->text
.size
)) {
3883 t
= (void *) (*sc
); /* found one, need to flush this */
3884 *sc
= (*sc
)->next
; /* remove it from the list */
3885 fs_give (&t
); /* flush the buffer */
3887 else sc
= &(*sc
)->next
; /* move to next in list */
3889 return *st
? NIL
: LONGT
;
3893 /* Mail search keyword
3894 * Accepts: MAIL stream
3895 * elt to get flags from
3897 * T for keyword search, NIL for unkeyword search
3898 * Returns: T if search found a match
3901 long mail_search_keyword (MAILSTREAM
*stream
,MESSAGECACHE
*elt
,STRINGLIST
*st
,
3905 unsigned long f
= 0;
3908 for (i
= 0; (j
= (i
< NUSERFLAGS
) && stream
->user_flags
[i
]); ++i
)
3909 if (!compare_csizedtext (stream
->user_flags
[i
],&st
->text
)) {
3913 if (flag
&& !j
) return NIL
;
3914 } while ((st
= st
->next
) != NULL
);
3915 tf
= elt
->user_flags
& f
; /* get set flags which match */
3916 return flag
? (f
== tf
) : !tf
;
3919 /* Mail search an address list
3920 * Accepts: address list
3922 * Returns: T if search found a match
3925 #define SEARCHBUFLEN (size_t) 2000
3926 #define SEARCHBUFSLOP (size_t) 5
3928 long mail_search_addr (ADDRESS
*adr
,STRINGLIST
*st
)
3932 char tmp
[SENDBUFLEN
+ 1];
3933 size_t i
= SEARCHBUFLEN
;
3937 txt
.data
= (unsigned char *) fs_get (i
+ SEARCHBUFSLOP
);
3938 /* never an error or next */
3939 tadr
.error
= NIL
,tadr
.next
= NIL
;
3940 /* write address list */
3941 for (txt
.size
= 0,a
= adr
; a
; a
= a
->next
) {
3942 k
= (tadr
.mailbox
= a
->mailbox
) ? 4 + 2*strlen (a
->mailbox
) : 3;
3943 if ((tadr
.personal
= a
->personal
) != NULL
) k
+= 3 + 2*strlen (a
->personal
);
3944 if ((tadr
.adl
= a
->adl
) != NULL
) k
+= 3 + 2*strlen (a
->adl
);
3945 if ((tadr
.host
= a
->host
) != NULL
) k
+= 3 + 2*strlen (a
->host
);
3946 if (tadr
.personal
|| tadr
.adl
) k
+= 2;
3947 if (k
< (SENDBUFLEN
-10)) {/* ignore ridiculous addresses */
3949 rfc822_write_address (tmp
,&tadr
);
3950 /* resize buffer if necessary */
3951 if (((k
= strlen (tmp
)) + txt
.size
) > i
)
3952 fs_resize ((void **) &txt
.data
,SEARCHBUFSLOP
+ (i
+= SEARCHBUFLEN
));
3953 /* add new address */
3954 memcpy (txt
.data
+ txt
.size
,tmp
,k
);
3956 /* another address follows */
3957 if (a
->next
) txt
.data
[txt
.size
++] = ',';
3960 txt
.data
[txt
.size
] = '\0'; /* tie off string */
3961 ret
= mail_search_header (&txt
,st
);
3962 fs_give ((void **) &txt
.data
);
3967 /* Get string for low-memory searching
3968 * Accepts: readin function pointer
3977 * Returns: NIL, always
3980 #define SEARCHSLOP 128
3982 char *mail_search_gets (readfn_t f
,void *stream
,unsigned long size
,
3986 char tmp
[MAILTMPLEN
+SEARCHSLOP
+1];
3988 /* better not be called unless searching */
3989 if (!md
->stream
->private.search
.string
) {
3990 sprintf (tmp
,"Search botch, mbx = %.80s, %s = %lu[%.80s]",
3991 md
->stream
->mailbox
,
3992 (md
->flags
& FT_UID
) ? "UID" : "msg",md
->msgno
,md
->what
);
3995 /* initially no match for search */
3996 md
->stream
->private.search
.result
= NIL
;
3997 /* make sure buffer clear */
3998 memset (st
.data
= (unsigned char *) tmp
,'\0',
3999 (size_t) MAILTMPLEN
+SEARCHSLOP
+1);
4000 /* read first buffer */
4001 (*f
) (stream
,st
.size
= i
= min (size
,(long) MAILTMPLEN
),tmp
);
4002 /* search for text */
4003 if (mail_search_string (&st
,NIL
,&md
->stream
->private.search
.string
))
4004 md
->stream
->private.search
.result
= T
;
4005 else if (size
-= i
) { /* more to do, blat slop down */
4006 memmove (tmp
,tmp
+MAILTMPLEN
-SEARCHSLOP
,(size_t) SEARCHSLOP
);
4007 do { /* read subsequent buffers one at a time */
4008 (*f
) (stream
,i
= min (size
,(long) MAILTMPLEN
),tmp
+SEARCHSLOP
);
4009 st
.size
= i
+ SEARCHSLOP
;
4010 if (mail_search_string (&st
,NIL
,&md
->stream
->private.search
.string
))
4011 md
->stream
->private.search
.result
= T
;
4012 else memmove (tmp
,tmp
+MAILTMPLEN
,(size_t) SEARCHSLOP
);
4014 while ((size
-= i
) && !md
->stream
->private.search
.result
);
4016 if (size
) { /* toss out everything after that */
4017 do (*f
) (stream
,i
= min (size
,(long) MAILTMPLEN
),tmp
);
4023 /* Mail parse search criteria
4025 * Returns: search program if parse successful, else NIL
4028 SEARCHPGM
*mail_criteria (char *criteria
)
4030 SEARCHPGM
*pgm
= NIL
;
4031 char *criterion
,*r
,tmp
[MAILTMPLEN
];
4033 if (criteria
) { /* only if criteria defined */
4034 /* make writeable copy of criteria */
4035 criteria
= cpystr (criteria
);
4036 /* for each criterion */
4037 for (pgm
= mail_newsearchpgm (), criterion
= strtok_r (criteria
," ",&r
);
4038 criterion
; (criterion
= strtok_r (NIL
," ",&r
))) {
4039 f
= NIL
; /* init then scan the criterion */
4040 switch (*ucase (criterion
)) {
4041 case 'A': /* possible ALL, ANSWERED */
4042 if (!strcmp (criterion
+1,"LL")) f
= T
;
4043 else if (!strcmp (criterion
+1,"NSWERED")) f
= pgm
->answered
= T
;
4045 case 'B': /* possible BCC, BEFORE, BODY */
4046 if (!strcmp (criterion
+1,"CC"))
4047 f
= mail_criteria_string (&pgm
->bcc
,&r
);
4048 else if (!strcmp (criterion
+1,"EFORE"))
4049 f
= mail_criteria_date (&pgm
->before
,&r
);
4050 else if (!strcmp (criterion
+1,"ODY"))
4051 f
= mail_criteria_string (&pgm
->body
,&r
);
4053 case 'C': /* possible CC */
4054 if (!strcmp (criterion
+1,"C")) f
= mail_criteria_string (&pgm
->cc
,&r
);
4056 case 'D': /* possible DELETED */
4057 if (!strcmp (criterion
+1,"ELETED")) f
= pgm
->deleted
= T
;
4059 case 'F': /* possible FLAGGED, FROM */
4060 if (!strcmp (criterion
+1,"LAGGED")) f
= pgm
->flagged
= T
;
4061 else if (!strcmp (criterion
+1,"ROM"))
4062 f
= mail_criteria_string (&pgm
->from
,&r
);
4064 case 'K': /* possible KEYWORD */
4065 if (!strcmp (criterion
+1,"EYWORD"))
4066 f
= mail_criteria_string (&pgm
->keyword
,&r
);
4069 case 'N': /* possible NEW */
4070 if (!strcmp (criterion
+1,"EW")) f
= pgm
->recent
= pgm
->unseen
= T
;
4072 case 'O': /* possible OLD, ON */
4073 if (!strcmp (criterion
+1,"LD")) f
= pgm
->old
= T
;
4074 else if (!strcmp (criterion
+1,"N"))
4075 f
= mail_criteria_date (&pgm
->on
,&r
);
4077 case 'R': /* possible RECENT */
4078 if (!strcmp (criterion
+1,"ECENT")) f
= pgm
->recent
= T
;
4080 case 'S': /* possible SEEN, SINCE, SUBJECT */
4081 if (!strcmp (criterion
+1,"EEN")) f
= pgm
->seen
= T
;
4082 else if (!strcmp (criterion
+1,"INCE"))
4083 f
= mail_criteria_date (&pgm
->since
,&r
);
4084 else if (!strcmp (criterion
+1,"UBJECT"))
4085 f
= mail_criteria_string (&pgm
->subject
,&r
);
4087 case 'T': /* possible TEXT, TO */
4088 if (!strcmp (criterion
+1,"EXT"))
4089 f
= mail_criteria_string (&pgm
->text
,&r
);
4090 else if (!strcmp (criterion
+1,"O"))
4091 f
= mail_criteria_string (&pgm
->to
,&r
);
4093 case 'U': /* possible UN* */
4094 if (criterion
[1] == 'N') {
4095 if (!strcmp (criterion
+2,"ANSWERED")) f
= pgm
->unanswered
= T
;
4096 else if (!strcmp (criterion
+2,"DELETED")) f
= pgm
->undeleted
= T
;
4097 else if (!strcmp (criterion
+2,"FLAGGED")) f
= pgm
->unflagged
= T
;
4098 else if (!strcmp (criterion
+2,"KEYWORD"))
4099 f
= mail_criteria_string (&pgm
->unkeyword
,&r
);
4100 else if (!strcmp (criterion
+2,"SEEN")) f
= pgm
->unseen
= T
;
4103 default: /* we will barf below */
4106 if (!f
) { /* if can't identify criterion */
4107 sprintf (tmp
,"Unknown search criterion: %.30s",criterion
);
4109 mail_free_searchpgm (&pgm
);
4113 /* no longer need copy of criteria */
4114 fs_give ((void **) &criteria
);
4120 * Accepts: pointer to date integer to return
4121 * pointer to strtok state
4122 * Returns: T if successful, else NIL
4125 int mail_criteria_date (unsigned short *date
,char **r
)
4127 STRINGLIST
*s
= NIL
;
4129 /* parse the date and return fn if OK */
4130 int ret
= (mail_criteria_string (&s
,r
) &&
4131 mail_parse_date (&elt
,(char *) s
->text
.data
) &&
4132 (*date
= mail_shortdate (elt
.year
,elt
.month
,elt
.day
))) ?
4134 if (s
) mail_free_stringlist (&s
);
4138 /* Calculate shortdate from elt values
4139 * Accepts: year (0 = BASEYEAR)
4140 * month (1 = January)
4142 * Returns: shortdate
4145 unsigned short mail_shortdate (unsigned int year
,unsigned int month
,
4148 return (year
<< 9) + (month
<< 5) + day
;
4152 * Accepts: pointer to stringlist
4153 * pointer to strtok state
4154 * Returns: T if successful, else NIL
4157 int mail_criteria_string (STRINGLIST
**s
,char **r
)
4160 char e
,*d
,*end
= " ",*c
= strtok_r (NIL
,"",r
);
4161 if (!c
) return NIL
; /* missing argument */
4162 switch (*c
) { /* see what the argument is */
4163 case '{': /* literal string */
4164 n
= strtoul (c
+1,&d
,10); /* get its length */
4165 if ((*d
++ == '}') && (*d
++ == '\015') && (*d
++ == '\012') &&
4166 (!(*(c
= d
+ n
)) || (*c
== ' '))) {
4167 e
= *--c
; /* store old delimiter */
4168 *c
= '\377'; /* make sure not a space */
4169 strtok_r (c
," ",r
); /* reset the strtok mechanism */
4170 *c
= e
; /* put character back */
4173 case '\0': /* catch bogons */
4176 case '"': /* quoted string */
4177 if (strchr (c
+1,'"')) end
= "\"";
4178 else return NIL
; /* falls through */
4179 default: /* atomic string */
4180 if ((d
= strtok_r (c
,end
,r
)) != NULL
) n
= strlen (d
);
4184 while (*s
) s
= &(*s
)->next
; /* find tail of list */
4185 *s
= mail_newstringlist (); /* make new entry */
4186 /* return the data */
4187 (*s
)->text
.data
= (unsigned char *) cpystr (d
);
4188 (*s
)->text
.size
= n
;
4192 /* Mail parse set from string
4193 * Accepts: string to parse
4194 * pointer to updated string pointer for return
4195 * Returns: set with pointer updated, or NIL if error
4198 SEARCHSET
*mail_parse_set (char *s
,char **ret
)
4201 SEARCHSET
*set
= NIL
;
4202 while (isdigit (*s
)) {
4203 if (!set
) cur
= set
= mail_newsearchset ();
4204 else cur
= cur
->next
= mail_newsearchset ();
4206 if (!(cur
->first
= strtoul (s
,&s
,10)) ||
4207 ((*s
== ':') && !(isdigit (*++s
) && (cur
->last
= strtoul (s
,&s
,10)))))
4208 break; /* bad value or range */
4209 if (*s
== ',') ++s
; /* point to next value if more */
4210 else { /* end of set */
4211 *ret
= s
; /* set return pointer */
4212 return set
; /* return set */
4215 mail_free_searchset (&set
); /* failure, punt partial set */
4220 /* Mail append to set
4221 * Accepts: head of search set or NIL to do nothing
4223 * Returns: tail of search set or NIL if did nothing
4226 SEARCHSET
*mail_append_set (SEARCHSET
*set
,unsigned long msgno
)
4228 if (set
) { /* find tail */
4229 while (set
->next
) set
= set
->next
;
4230 /* start of set if no first member */
4231 if (!set
->first
) set
->first
= msgno
;
4232 else if (msgno
== (set
->last
? set
->last
: set
->first
) + 1)
4233 set
->last
= msgno
; /* extend range if 1 past current */
4234 else (set
= set
->next
= mail_newsearchset ())->first
= msgno
;
4239 /* Mail sort messages
4240 * Accepts: mail stream
4245 * Returns: vector of sorted message sequences or NIL if error
4248 unsigned long *mail_sort (MAILSTREAM
*stream
,char *charset
,SEARCHPGM
*spg
,
4249 SORTPGM
*pgm
,long flags
)
4251 unsigned long *ret
= NIL
;
4252 if (stream
->dtb
) /* do the driver's action */
4253 ret
= (*(stream
->dtb
->sort
? stream
->dtb
->sort
: mail_sort_msgs
))
4254 (stream
,charset
,spg
,pgm
,flags
);
4255 /* flush search/sort programs if requested */
4256 if (spg
&& (flags
& SE_FREE
)) mail_free_searchpgm (&spg
);
4257 if (flags
& SO_FREE
) mail_free_sortpgm (&pgm
);
4261 /* Mail sort messages work routine
4262 * Accepts: mail stream
4267 * Returns: vector of sorted message sequences or NIL if error
4270 unsigned long *mail_sort_msgs (MAILSTREAM
*stream
,char *charset
,SEARCHPGM
*spg
,
4271 SORTPGM
*pgm
,long flags
)
4275 unsigned long *ret
= NIL
;
4276 if (spg
) { /* only if a search needs to be done */
4277 int silent
= stream
->silent
;
4278 stream
->silent
= T
; /* don't pass up mm_searched() events */
4279 /* search for messages */
4280 mail_search_full (stream
,charset
,spg
,NIL
);
4281 stream
->silent
= silent
; /* restore silence state */
4283 /* initialize progress counters */
4284 pgm
->nmsgs
= pgm
->progress
.cached
= 0;
4285 /* pass 1: count messages to sort */
4286 for (i
= 1; i
<= stream
->nmsgs
; ++i
)
4287 if (mail_elt (stream
,i
)->searched
) pgm
->nmsgs
++;
4288 if (pgm
->nmsgs
) { /* pass 2: sort cache */
4289 sc
= mail_sort_loadcache (stream
,pgm
);
4290 /* pass 3: sort messages */
4291 if (!pgm
->abort
) ret
= mail_sort_cache (stream
,pgm
,sc
,flags
);
4292 fs_give ((void **) &sc
); /* don't need sort vector any more */
4294 /* empty sort results */
4295 else ret
= (unsigned long *) memset (fs_get (sizeof (unsigned long)),0,
4296 sizeof (unsigned long));
4297 /* also return via callback if requested */
4298 if (mailsortresults
) (*mailsortresults
) (stream
,ret
,pgm
->nmsgs
);
4299 return ret
; /* return sort results */
4302 /* Mail sort sortcache vector
4303 * Accepts: mail stream
4307 * Returns: vector of sorted message sequences or NIL if error
4310 unsigned long *mail_sort_cache (MAILSTREAM
*stream
,SORTPGM
*pgm
,SORTCACHE
**sc
,
4313 unsigned long i
,*ret
;
4314 /* pass 3: sort messages */
4315 qsort ((void *) sc
,pgm
->nmsgs
,sizeof (SORTCACHE
*),mail_sort_compare
);
4316 /* optional post sorting */
4317 if (pgm
->postsort
) (*pgm
->postsort
) ((void *) sc
);
4318 /* pass 4: return results */
4319 ret
= (unsigned long *) fs_get ((pgm
->nmsgs
+1) * sizeof (unsigned long));
4320 if (flags
& SE_UID
) /* UID or msgno? */
4321 for (i
= 0; i
< pgm
->nmsgs
; i
++) ret
[i
] = mail_uid (stream
,sc
[i
]->num
);
4322 else for (i
= 0; i
< pgm
->nmsgs
; i
++) ret
[i
] = sc
[i
]->num
;
4323 ret
[pgm
->nmsgs
] = 0; /* tie off message list */
4327 /* Mail load sortcache
4328 * Accepts: mail stream, already searched
4330 * Returns: vector of sortcache pointers matching search
4333 static STRINGLIST maildateline
= {{(unsigned char *) "date",4},NIL
};
4334 static STRINGLIST mailrnfromline
= {{(unsigned char *) ">from",5},NIL
};
4335 static STRINGLIST mailfromline
= {{(unsigned char *) "from",4},
4337 static STRINGLIST mailtonline
= {{(unsigned char *) "to",2},NIL
};
4338 static STRINGLIST mailccline
= {{(unsigned char *) "cc",2},NIL
};
4339 static STRINGLIST mailsubline
= {{(unsigned char *) "subject",7},NIL
};
4341 SORTCACHE
**mail_sort_loadcache (MAILSTREAM
*stream
,SORTPGM
*pgm
)
4343 char *t
,*v
,*x
,tmp
[MAILTMPLEN
];
4346 MESSAGECACHE
*elt
,telt
;
4349 unsigned long i
= (pgm
->nmsgs
) * sizeof (SORTCACHE
*);
4350 sc
= (SORTCACHE
**) memset (fs_get ((size_t) i
),0,(size_t) i
);
4351 /* see what needs to be loaded */
4352 for (i
= 1; !pgm
->abort
&& (i
<= stream
->nmsgs
); i
++)
4353 if ((elt
= mail_elt (stream
,i
))->searched
) {
4354 sc
[pgm
->progress
.cached
++] =
4355 s
= (SORTCACHE
*) (*mailcache
) (stream
,i
,CH_SORTCACHE
);
4356 s
->pgm
= pgm
; /* note sort program */
4358 /* get envelope if cached */
4359 if (stream
->scache
) env
= (i
== stream
->msgno
) ? stream
->env
: NIL
;
4360 else env
= elt
->private.msg
.env
;
4361 for (pg
= pgm
; pg
; pg
= pg
->next
) switch (pg
->function
) {
4362 case SORTARRIVAL
: /* sort by arrival date */
4364 /* internal date unknown but can get? */
4365 if (!elt
->day
&& !(stream
->dtb
->flags
& DR_NOINTDATE
)) {
4366 sprintf (tmp
,"%lu",i
);
4367 mail_fetch_fast (stream
,tmp
,NIL
);
4369 /* wrong thing before 3-Jan-1970 */
4370 s
->arrival
= elt
->day
? mail_longdate (elt
) : 1;
4374 case SORTSIZE
: /* sort by message size */
4376 if (!elt
->rfc822_size
) {
4377 sprintf (tmp
,"%lu",i
);
4378 mail_fetch_fast (stream
,tmp
,NIL
);
4380 s
->size
= elt
->rfc822_size
? elt
->rfc822_size
: 1;
4385 case SORTDATE
: /* sort by date */
4387 if (env
) t
= env
->date
;
4388 else if ((t
= mail_fetch_header (stream
,i
,NIL
,&maildateline
,NIL
,
4389 FT_INTERNAL
| FT_PEEK
)) &&
4390 (t
= strchr (t
,':')))
4391 for (x
= ++t
; (x
= strpbrk (x
,"\012\015")) != NULL
; x
++)
4392 switch (*(v
= ((*x
== '\015') && (x
[1] == '\012')) ? x
+2 : x
+1)){
4393 case ' ': /* erase continuation newlines */
4395 memmove (x
,v
,strlen (v
));
4397 default: /* tie off extraneous text */
4400 /* skip leading whitespace */
4401 if (t
) while ((*t
== ' ') || (*t
== '\t')) t
++;
4402 /* parse date from Date: header */
4403 if (!(t
&& mail_parse_date (&telt
,t
) &&
4404 (s
->date
= mail_longdate (&telt
)))) {
4405 /* failed, use internal date */
4406 if (!(s
->date
= s
->arrival
)) {
4407 /* internal date unknown but can get? */
4408 if (!elt
->day
&& !(stream
->dtb
->flags
& DR_NOINTDATE
)) {
4409 sprintf (tmp
,"%lu",i
);
4410 mail_fetch_fast (stream
,tmp
,NIL
);
4412 /* wrong thing before 3-Jan-1970 */
4413 s
->date
= (s
->arrival
= elt
->day
? mail_longdate (elt
) : 1);
4420 case SORTFROM
: /* sort by first from */
4422 if (env
) s
->from
= env
->from
&& env
->from
->mailbox
?
4423 cpystr (env
->from
->mailbox
) : NIL
;
4424 else if ((t
= mail_fetch_header (stream
,i
,NIL
,&mailfromline
,NIL
,
4425 FT_INTERNAL
| FT_PEEK
)) &&
4426 (t
= strchr (t
,':'))) {
4427 for (x
= ++t
; (x
= strpbrk (x
,"\012\015")) != NULL
; x
++)
4428 switch (*(v
= ((*x
== '\015') && (x
[1] == '\012')) ? x
+2 : x
+1)){
4429 case ' ': /* erase continuation newlines */
4431 memmove (x
,v
,strlen (v
));
4433 case 'f': /* continuation but with extra "From:" */
4435 if ((v
= strchr (v
,':')) != NULL
) {
4436 memmove (x
,v
+1,strlen (v
+1));
4439 default: /* tie off extraneous text */
4442 rfc822_parse_adrlist (&adr
,t
,BADHOST
);
4444 s
->from
= adr
->mailbox
;
4446 mail_free_address (&adr
);
4449 if (!s
->from
) s
->from
= cpystr ("");
4454 case SORTTO
: /* sort by first to */
4456 if (env
) s
->to
= env
->to
&& env
->to
->mailbox
?
4457 cpystr (env
->to
->mailbox
) : NIL
;
4458 else if ((t
= mail_fetch_header (stream
,i
,NIL
,&mailtonline
,NIL
,
4459 FT_INTERNAL
| FT_PEEK
)) &&
4460 (t
= strchr (t
,':'))) {
4461 for (x
= ++t
; (x
= strpbrk (x
,"\012\015")) != NULL
; x
++)
4462 switch (*(v
= ((*x
== '\015') && (x
[1] == '\012')) ? x
+2 : x
+1)){
4463 case ' ': /* erase continuation newlines */
4465 memmove (x
,v
,strlen (v
));
4467 case 't': /* continuation but with extra "To:" */
4469 if ((v
= strchr (v
,':')) != NULL
) {
4470 memmove (x
,v
+1,strlen (v
+1));
4473 default: /* tie off extraneous text */
4476 rfc822_parse_adrlist (&adr
,t
,BADHOST
);
4478 s
->to
= adr
->mailbox
;
4480 mail_free_address (&adr
);
4483 if (!s
->to
) s
->to
= cpystr ("");
4488 case SORTCC
: /* sort by first cc */
4490 if (env
) s
->cc
= env
->cc
&& env
->cc
->mailbox
?
4491 cpystr (env
->cc
->mailbox
) : NIL
;
4492 else if ((t
= mail_fetch_header (stream
,i
,NIL
,&mailccline
,NIL
,
4493 FT_INTERNAL
| FT_PEEK
)) &&
4494 (t
= strchr (t
,':'))) {
4495 for (x
= ++t
; (x
= strpbrk (x
,"\012\015")) != NULL
; x
++)
4496 switch (*(v
= ((*x
== '\015') && (x
[1] == '\012')) ? x
+2 : x
+1)){
4497 case ' ': /* erase continuation newlines */
4499 memmove (x
,v
,strlen (v
));
4501 case 'c': /* continuation but with extra "cc:" */
4503 if ((v
= strchr (v
,':')) != NULL
) {
4504 memmove (x
,v
+1,strlen (v
+1));
4507 default: /* tie off extraneous text */
4510 rfc822_parse_adrlist (&adr
,t
,BADHOST
);
4512 s
->cc
= adr
->mailbox
;
4514 mail_free_address (&adr
);
4517 if (!s
->cc
) s
->cc
= cpystr ("");
4522 case SORTSUBJECT
: /* sort by subject */
4524 /* get subject from envelope if have one */
4525 if (env
) t
= env
->subject
? env
->subject
: "";
4526 /* otherwise snarf from header text */
4527 else if ((t
= mail_fetch_header (stream
,i
,NIL
,&mailsubline
,
4528 NIL
,FT_INTERNAL
| FT_PEEK
)) &&
4529 (t
= strchr (t
,':')))
4530 for (x
= ++t
; (x
= strpbrk (x
,"\012\015")) != NULL
; x
++)
4531 switch (*(v
= ((*x
== '\015') && (x
[1] == '\012')) ? x
+2 : x
+1)){
4532 case ' ': /* erase continuation newlines */
4534 memmove (x
,v
,strlen (v
));
4536 default: /* tie off extraneous text */
4539 else t
= ""; /* empty subject */
4540 /* strip and cache subject */
4541 s
->refwd
= mail_strip_subject (t
,&s
->subject
);
4546 fatal ("Unknown sort function");
4552 /* Strip subjects of extra spaces and leading and trailing cruft for sorting
4553 * Accepts: unstripped subject
4554 * pointer to return stripped subject, in cpystr form
4555 * Returns: T if subject had a re/fwd, NIL otherwise
4558 unsigned int mail_strip_subject (char *t
,char **ret
)
4561 unsigned long i
,slen
;
4563 unsigned int refwd
= NIL
;
4564 if ((src
.size
= strlen (t
)) != 0) { /* have non-empty subject? */
4565 src
.data
= (unsigned char *) t
;
4567 /* make copy, convert MIME2 if needed */
4568 *ret
= s
= (utf8_mime2text (&src
,&dst
,U8T_CANONICAL
) &&
4569 (src
.data
!= dst
.data
)) ? (char *) dst
.data
: cpystr (t
);
4570 /* convert spaces to tab, strip extra spaces */
4571 for (x
= t
= s
, c
= 'x'; *t
; t
++) {
4572 if (c
!= ' ') c
= *x
++ = ((*t
== '\t') ? ' ' : *t
);
4573 else if ((*t
!= '\t') && (*t
!= ' ')) c
= *x
++ = *t
;
4575 *x
= '\0'; /* tie off string */
4577 for (slen
= dst
.size
; s
; slen
= strlen (s
)) {
4578 for (t
= s
+ slen
; t
> s
; ) switch (t
[-1]) {
4579 case ' ': case '\t': /* WSP */
4580 *--t
= '\0'; /* just remove it */
4582 case ')': /* possible "(fwd)" */
4583 if ((t
>= (s
+ 5)) && (t
[-5] == '(') &&
4584 ((t
[-4] == 'F') || (t
[-4] == 'f')) &&
4585 ((t
[-3] == 'W') || (t
[-3] == 'w')) &&
4586 ((t
[-2] == 'D') || (t
[-2] == 'd'))) {
4587 *(t
-= 5) = '\0'; /* remove "(fwd)" */
4588 refwd
= T
; /* note a re/fwd */
4591 default: /* not a subj-trailer */
4596 for (t
= s
; t
; ) switch (*s
) {
4597 case ' ': case '\t': /* WSP */
4598 s
= t
= mail_strip_subject_wsp (s
+ 1);
4600 case 'r': case 'R': /* possible "re" */
4601 if (((s
[1] == 'E') || (s
[1] == 'e')) &&
4602 (t
= mail_strip_subject_wsp (s
+ 2)) &&
4603 (t
= mail_strip_subject_blob (t
)) && (*t
== ':')) {
4604 s
= ++t
; /* found "re" */
4605 refwd
= T
; /* definitely a re/fwd at this point */
4607 else t
= NIL
; /* found subj-middle */
4609 case 'f': case 'F': /* possible "fw" or "fwd" */
4610 if (((s
[1] == 'w') || (s
[1] == 'W')) &&
4611 (((s
[2] == 'd') || (s
[2] == 'D')) ?
4612 (t
= mail_strip_subject_wsp (s
+ 3)) :
4613 (t
= mail_strip_subject_wsp (s
+ 2))) &&
4614 (t
= mail_strip_subject_blob (t
)) && (*t
== ':')) {
4615 s
= ++t
; /* found "fwd" */
4616 refwd
= T
; /* definitely a re/fwd at this point */
4618 else t
= NIL
; /* found subj-middle */
4620 case '[': /* possible subj-blob */
4621 if ((t
= mail_strip_subject_blob (s
)) && *t
) s
= t
;
4622 else t
= NIL
; /* found subj-middle */
4625 t
= NIL
; /* found subj-middle */
4629 /* Netscape-style "[Fwd: ...]"? */
4630 if ((*s
== '[') && ((s
[1] == 'F') || (s
[1] == 'f')) &&
4631 ((s
[2] == 'W') || (s
[2] == 'w')) &&
4632 ((s
[3] == 'D') || (s
[3] == 'd')) && (s
[4] == ':') &&
4633 (s
[i
= strlen (s
) - 1] == ']')) {
4634 s
[i
] = '\0'; /* flush closing "]" */
4635 s
+= 5; /* and leading "[Fwd:" */
4636 refwd
= T
; /* definitely a re/fwd at this point */
4638 else break; /* don't need to loop back to step 2 */
4640 if (s
!= (t
= *ret
)) { /* removed leading text? */
4641 s
= *ret
= cpystr (s
); /* yes, make a fresh return copy */
4642 fs_give ((void **) &t
); /* flush old copy */
4645 else *ret
= cpystr (""); /* empty subject */
4646 return refwd
; /* return re/fwd state */
4649 /* Strip subject wsp helper routine
4651 * Returns: pointer to text after blob
4654 char *mail_strip_subject_wsp (char *s
)
4656 while ((*s
== ' ') || (*s
== '\t')) s
++;
4661 /* Strip subject blob helper routine
4663 * Returns: pointer to text after any blob, NIL if blob-like but not blob
4666 char *mail_strip_subject_blob (char *s
)
4668 if (*s
!= '[') return s
; /* not a blob, ignore */
4669 /* search for end of blob */
4670 while (*++s
!= ']') if ((*s
== '[') || !*s
) return NIL
;
4671 return mail_strip_subject_wsp (s
+ 1);
4674 /* Sort compare messages
4675 * Accept: first message sort cache element
4676 * second message sort cache element
4677 * Returns: -1 if a1 < a2, 0 if a1 == a2, 1 if a1 > a2
4680 int mail_sort_compare (const void *a1
,const void *a2
)
4683 SORTCACHE
*s1
= *(SORTCACHE
**) a1
;
4684 SORTCACHE
*s2
= *(SORTCACHE
**) a2
;
4685 SORTPGM
*pgm
= s1
->pgm
;
4686 if (!s1
->sorted
) { /* this one sorted yet? */
4688 pgm
->progress
.sorted
++; /* another sorted message */
4690 if (!s2
->sorted
) { /* this one sorted yet? */
4692 pgm
->progress
.sorted
++; /* another sorted message */
4695 switch (pgm
->function
) { /* execute search program */
4696 case SORTDATE
: /* sort by date */
4697 i
= compare_ulong (s1
->date
,s2
->date
);
4699 case SORTARRIVAL
: /* sort by arrival date */
4700 i
= compare_ulong (s1
->arrival
,s2
->arrival
);
4702 case SORTSIZE
: /* sort by message size */
4703 i
= compare_ulong (s1
->size
,s2
->size
);
4705 case SORTFROM
: /* sort by first from */
4706 i
= compare_string (s1
->from
,s2
->from
);
4708 case SORTTO
: /* sort by first to */
4709 i
= compare_string (s1
->to
,s2
->to
);
4711 case SORTCC
: /* sort by first cc */
4712 i
= compare_string (s1
->cc
,s2
->cc
);
4714 case SORTSUBJECT
: /* sort by subject */
4715 i
= compare_string (s1
->subject
,s2
->subject
);
4718 if (pgm
->reverse
) i
= -i
; /* flip results if necessary */
4720 while ((pgm
= i
? NIL
: pgm
->next
) != NULL
);
4721 /* return result, avoid 0 if at all possible */
4722 return i
? i
: compare_ulong (s1
->num
,s2
->num
);
4725 /* Return message date as an unsigned long seconds since time began
4726 * Accepts: message cache pointer
4727 * Returns: unsigned long of date
4729 * This routine, like most UNIX systems, is clueless about leap seconds.
4730 * Thus, it treats 23:59:60 as equivalent to 00:00:00 the next day.
4732 * This routine forces any early hours on 1-Jan-1970 in oriental timezones
4733 * to be 1-Jan-1970 00:00:00 UTC, so as to avoid negative longdates.
4736 unsigned long mail_longdate (MESSAGECACHE
*elt
)
4738 unsigned long m
= elt
->month
? elt
->month
: 1;
4739 unsigned long yr
= elt
->year
+ BASEYEAR
;
4740 /* number of days since time began */
4741 unsigned long ret
= (elt
->day
? (elt
->day
- 1) : 0)
4742 + 30 * (m
- 1) + ((m
+ (m
> 8)) / 2)
4743 #ifndef USEJULIANCALENDAR
4744 #ifndef USEORTHODOXCALENDAR /* Gregorian calendar */
4745 + ((yr
/ 400) - (BASEYEAR
/ 400)) - ((yr
/ 100) - (BASEYEAR
/ 100))
4747 - ((yr
/ 4000) - (BASEYEAR
/ 4000))
4750 !(yr
% 4) && ((yr
% 100) || (!(yr
% 400)
4755 #else /* Orthodox calendar */
4756 + ((2*(yr
/ 900)) - (2*(BASEYEAR
/ 900)))
4757 + (((yr
% 900) >= 200) - ((BASEYEAR
% 900) >= 200))
4758 + (((yr
% 900) >= 600) - ((BASEYEAR
% 900) >= 600))
4759 - ((yr
/ 100) - (BASEYEAR
/ 100))
4761 !(yr
% 4) && ((yr
% 100) || ((yr
% 900) == 200) || ((yr
% 900) == 600))
4765 + elt
->year
* 365 + (((unsigned long) (elt
->year
+ (BASEYEAR
% 4))) / 4);
4766 ret
*= 24; ret
+= elt
->hours
; /* date value in hours */
4767 ret
*= 60; ret
+=elt
->minutes
;/* date value in minutes */
4768 yr
= (elt
->zhours
* 60) + elt
->zminutes
;
4769 if (elt
->zoccident
) ret
+= yr
;/* occidental timezone, make UTC */
4770 else if (ret
< yr
) return 0; /* still 31-Dec-1969 in UTC */
4771 else ret
-= yr
; /* oriental timezone, make UTC */
4772 ret
*= 60; ret
+= elt
->seconds
;
4776 /* Mail thread messages
4777 * Accepts: mail stream
4782 * Returns: thread node tree or NIL if error
4785 THREADNODE
*mail_thread (MAILSTREAM
*stream
,char *type
,char *charset
,
4786 SEARCHPGM
*spg
,long flags
)
4788 THREADNODE
*ret
= NIL
;
4789 if (stream
->dtb
) /* must have a live driver */
4790 ret
= stream
->dtb
->thread
? /* do driver's action if available */
4791 (*stream
->dtb
->thread
) (stream
,type
,charset
,spg
,flags
) :
4792 mail_thread_msgs (stream
,type
,charset
,spg
,flags
,mail_sort_msgs
);
4793 /* flush search/sort programs if requested */
4794 if (spg
&& (flags
& SE_FREE
)) mail_free_searchpgm (&spg
);
4799 /* Mail thread messages
4800 * Accepts: mail stream
4806 * Returns: thread node tree or NIL if error
4809 THREADNODE
*mail_thread_msgs (MAILSTREAM
*stream
,char *type
,char *charset
,
4810 SEARCHPGM
*spg
,long flags
,sorter_t sorter
)
4813 for (t
= &mailthreadlist
; t
; t
= t
->next
)
4814 if (!compare_cstring (type
,t
->name
)) {
4815 THREADNODE
*ret
= (*t
->dispatch
) (stream
,charset
,spg
,flags
,sorter
);
4816 if (mailthreadresults
) (*mailthreadresults
) (stream
,ret
);
4819 MM_LOG ("No such thread type",ERROR
);
4823 /* Mail thread ordered subject
4824 * Accepts: mail stream
4829 * Returns: thread node tree
4832 THREADNODE
*mail_thread_orderedsubject (MAILSTREAM
*stream
,char *charset
,
4833 SEARCHPGM
*spg
,long flags
,
4836 THREADNODE
*thr
= NIL
;
4837 THREADNODE
*cur
,*top
,**tc
;
4840 unsigned long i
,j
,*lst
,*ls
;
4841 /* sort by subject+date */
4842 memset (&pgm
,0,sizeof (SORTPGM
));
4843 memset (&pgm2
,0,sizeof (SORTPGM
));
4844 pgm
.function
= SORTSUBJECT
;
4846 pgm2
.function
= SORTDATE
;
4847 if ((lst
= (*sorter
) (stream
,charset
,spg
,&pgm
,flags
& ~(SE_FREE
| SE_UID
))) != NULL
){
4848 if (*(ls
= lst
)) { /* create thread */
4849 /* note first subject */
4850 cur
= top
= thr
= mail_newthreadnode
4851 ((SORTCACHE
*) (*mailcache
) (stream
,*ls
++,CH_SORTCACHE
));
4852 /* note its number */
4853 cur
->num
= (flags
& SE_UID
) ? mail_uid (stream
,*lst
) : *lst
;
4854 i
= 1; /* number of threads */
4855 while (*ls
) { /* build tree */
4856 /* subjects match? */
4857 s
= (SORTCACHE
*) (*mailcache
) (stream
,*ls
++,CH_SORTCACHE
);
4858 if (compare_cstring (top
->sc
->subject
,s
->subject
)) {
4859 i
++; /* have a new thread */
4860 top
= top
->branch
= cur
= mail_newthreadnode (s
);
4862 /* start a child of the top */
4863 else if (cur
== top
) cur
= cur
->next
= mail_newthreadnode (s
);
4864 /* sibling of child */
4865 else cur
= cur
->branch
= mail_newthreadnode (s
);
4866 /* set to msgno or UID as needed */
4867 cur
->num
= (flags
& SE_UID
) ? mail_uid (stream
,s
->num
) : s
->num
;
4869 /* make threadnode cache */
4870 tc
= (THREADNODE
**) fs_get (i
* sizeof (THREADNODE
*));
4871 /* load threadnode cache */
4872 for (j
= 0, cur
= thr
; cur
; cur
= cur
->branch
) tc
[j
++] = cur
;
4873 if (i
!= j
) fatal ("Threadnode cache confusion");
4874 qsort ((void *) tc
,i
,sizeof (THREADNODE
*),mail_thread_compare_date
);
4875 for (j
= 0, --i
; j
< i
; j
++) tc
[j
]->branch
= tc
[j
+1];
4876 tc
[j
]->branch
= NIL
; /* end of root */
4877 thr
= tc
[0]; /* head of data */
4878 fs_give ((void **) &tc
);
4880 fs_give ((void **) &lst
);
4885 /* Mail thread references
4886 * Accepts: mail stream
4891 * Returns: thread node tree
4894 #define REFHASHSIZE 1009 /* arbitrary prime for hash table size */
4896 /* Reference threading container, as described in Jamie Zawinski's web page
4897 * (http://www.jwz.org/doc/threading.html) for this algorithm. These are
4898 * stored as extended data in the hash table (called "id_table" in JWZ's
4899 * document) and are maintained by the hash table routines. The hash table
4900 * routines implement extended data as additional void* words at the end of
4901 * each bucket, hence these strange macros instead of a struct which would
4902 * have been more straightforward.
4905 #define THREADLINKS 3 /* number of thread links */
4907 #define CACHE(data) ((SORTCACHE *) (data)[0])
4908 #define PARENT(data) ((container_t) (data)[1])
4909 #define SETPARENT(data,value) ((container_t) (data[1] = value))
4910 #define SIBLING(data) ((container_t) (data)[2])
4911 #define SETSIBLING(data,value) ((container_t) (data[2] = value))
4912 #define CHILD(data) ((container_t) (data)[3])
4913 #define SETCHILD(data,value) ((container_t) (data[3] = value))
4915 THREADNODE
*mail_thread_references (MAILSTREAM
*stream
,char *charset
,
4916 SEARCHPGM
*spg
,long flags
,sorter_t sorter
)
4918 MESSAGECACHE
*elt
,telt
;
4923 THREADNODE
**tc
,*cur
,*lst
,*nxt
,*sis
,*msg
;
4924 container_t con
,nxc
,prc
,sib
;
4926 char *t
,tmp
[MAILTMPLEN
];
4927 unsigned long j
,nmsgs
;
4928 unsigned long i
= stream
->nmsgs
* sizeof (SORTCACHE
*);
4929 SORTCACHE
**sc
= (SORTCACHE
**) memset (fs_get ((size_t) i
),0,(size_t) i
);
4930 HASHTAB
*ht
= hash_create (REFHASHSIZE
);
4931 THREADNODE
*root
= NIL
;
4932 if (spg
) { /* only if a search needs to be done */
4933 int silent
= stream
->silent
;
4934 stream
->silent
= T
; /* don't pass up mm_searched() events */
4935 /* search for messages */
4936 mail_search_full (stream
,charset
,spg
,NIL
);
4937 stream
->silent
= silent
; /* restore silence state */
4940 /* create SORTCACHE vector of requested msgs */
4941 for (i
= 1, nmsgs
= 0; i
<= stream
->nmsgs
; ++i
)
4942 if (mail_elt (stream
,i
)->searched
)
4943 (sc
[nmsgs
++] = (SORTCACHE
*)(*mailcache
)(stream
,i
,CH_SORTCACHE
))->num
=i
;
4944 /* separate pass so can do overview fetch lookahead */
4945 for (i
= 0; i
< nmsgs
; ++i
) { /* for each requested message */
4946 /* is anything missing in its SORTCACHE? */
4947 if (!((s
= sc
[i
])->date
&& s
->subject
&& s
->message_id
&& s
->references
)) {
4948 /* driver has an overview mechanism? */
4949 if (stream
->dtb
&& stream
->dtb
->overview
) {
4950 /* yes, find following unloaded entries */
4951 for (j
= i
+ 1; (j
< nmsgs
) && !sc
[j
]->references
; ++j
);
4952 sprintf (tmp
,"%lu",mail_uid (stream
,s
->num
));
4953 if (i
!= --j
) /* end of range different? */
4954 sprintf (tmp
+ strlen (tmp
),":%lu",mail_uid (stream
,sc
[j
]->num
));
4955 /* load via overview mechanism */
4956 mail_fetch_overview (stream
,tmp
,mail_thread_loadcache
);
4958 /* still missing data? */
4959 if (!s
->date
|| !s
->subject
|| !s
->message_id
|| !s
->references
) {
4960 /* try to load data from envelope */
4961 if ((env
= mail_fetch_structure (stream
,s
->num
,NIL
,NIL
)) != NULL
) {
4962 if (!s
->date
&& env
->date
&& mail_parse_date (&telt
,env
->date
))
4963 s
->date
= mail_longdate (&telt
);
4964 if (!s
->subject
&& env
->subject
)
4966 mail_strip_subject (env
->subject
,&s
->subject
);
4967 if (!s
->message_id
&& env
->message_id
&& *env
->message_id
)
4968 s
->message_id
= mail_thread_parse_msgid (env
->message_id
,NIL
);
4969 if (!s
->references
&& /* use References: or In-Reply-To: */
4971 mail_thread_parse_references (env
->references
,T
)))
4972 s
->references
= mail_thread_parse_references(env
->in_reply_to
,NIL
);
4975 if (!s
->date
&& !(s
->date
= s
->arrival
)) {
4976 /* internal date unknown but can get? */
4977 if (!(elt
= mail_elt (stream
,s
->num
))->day
&&
4978 !(stream
->dtb
->flags
& DR_NOINTDATE
)) {
4979 sprintf (tmp
,"%lu",s
->num
);
4980 mail_fetch_fast (stream
,tmp
,NIL
);
4982 /* wrong thing before 3-Jan-1970 */
4983 s
->date
= (s
->arrival
= elt
->day
? mail_longdate (elt
) : 1);
4985 if (!s
->subject
) s
->subject
= cpystr ("");
4986 if (!s
->references
) s
->references
= mail_newstringlist ();
4991 /* Step 1 (preliminary) */
4992 /* generate unique string */
4993 sprintf (tmp
,"%s.%lx.%lx@%s",stream
->mailbox
,stream
->uid_validity
,
4994 mail_uid (stream
,s
->num
),mylocalhost ());
4995 /* flush old unique string if not message-id */
4996 if (s
->unique
&& (s
->unique
!= s
->message_id
))
4997 fs_give ((void **) &s
->unique
);
4998 s
->unique
= s
->message_id
? /* don't permit Message ID duplicates */
4999 (hash_lookup (ht
,s
->message_id
) ? cpystr (tmp
) : s
->message_id
) :
5000 (s
->message_id
= cpystr (tmp
));
5001 /* add unique string to hash table */
5002 hash_add (ht
,s
->unique
,s
,THREADLINKS
);
5005 for (i
= 0; i
< nmsgs
; ++i
) { /* for each message in sortcache */
5007 if ((st
= (s
= sc
[i
])->references
) && st
->text
.data
)
5008 for (con
= hash_lookup_and_add (ht
,(char *) st
->text
.data
,NIL
,
5009 THREADLINKS
); (st
= st
->next
) != NULL
; con
= nxc
) {
5010 nxc
= hash_lookup_and_add (ht
,(char *) st
->text
.data
,NIL
,THREADLINKS
);
5011 /* only if no parent & won't introduce loop */
5012 if (!PARENT (nxc
) && !mail_thread_check_child (con
,nxc
)) {
5013 SETPARENT (nxc
,con
); /* establish parent/child link */
5014 /* other children become sibling of this one */
5015 SETSIBLING (nxc
,CHILD (con
));
5016 SETCHILD (con
,nxc
); /* set as child of parent */
5019 else con
= NIL
; /* else message has no ancestors */
5021 if ((prc
= PARENT ((nxc
= hash_lookup (ht
,s
->unique
)))) &&
5022 (prc
!= con
)) { /* break links if have a different parent */
5023 SETPARENT (nxc
,NIL
); /* easy if direct child */
5024 if (nxc
== CHILD (prc
)) SETCHILD (prc
,SIBLING (nxc
));
5025 else { /* otherwise hunt through sisters */
5026 for (sib
= CHILD (prc
); nxc
!= SIBLING (sib
); sib
= SIBLING (sib
));
5027 SETSIBLING (sib
,SIBLING (nxc
));
5029 SETSIBLING (nxc
,NIL
); /* no more little sisters either */
5030 prc
= NIL
; /* no more parent set */
5032 /* need to set parent, and parent is good? */
5033 if (!prc
&& !mail_thread_check_child (con
,nxc
)) {
5034 SETPARENT (nxc
,con
); /* establish parent/child link */
5035 if (con
) { /* if non-root parent, set parent's child */
5036 if (CHILD (con
)) { /* have a child already */
5037 /* find youngest daughter */
5038 for (con
= CHILD (con
); SIBLING (con
); con
= SIBLING (con
));
5039 SETSIBLING (con
,nxc
); /* add new baby sister */
5041 else SETCHILD (con
,nxc
);/* set as only child */
5045 fs_give ((void **) &sc
); /* finished with sortcache vector */
5048 /* search hash table for parentless messages */
5049 for (i
= 0, prc
= con
= NIL
; i
< ht
->size
; i
++)
5050 for (he
= ht
->table
[i
]; he
; he
= he
->next
)
5051 if (!PARENT ((nxc
= he
->data
))) {
5052 /* sibling of previous parentless message */
5053 if (con
) con
= SETSIBLING (con
,nxc
);
5054 else prc
= con
= nxc
; /* first parentless message */
5056 /* Once the dummy containers are pruned, we no longer need the parent
5057 * information, so we can convert the containers to THREADNODEs. Since
5058 * we don't need the id_table any more either, we can reset the hash table
5059 * and reuse it as a subject_table. Resetting the hash table will also
5060 * destroy the containers.
5063 /* prune dummies, convert to threadnode */
5064 root
= mail_thread_c2node (stream
,mail_thread_prune_dummy (prc
,NIL
),flags
);
5066 /* make buffer for sorting */
5067 tc
= (THREADNODE
**) fs_get (nmsgs
* sizeof (THREADNODE
*));
5068 /* load threadcache and count nodes to sort */
5069 for (i
= 0, cur
= root
; cur
; cur
= cur
->branch
) tc
[i
++] = cur
;
5070 if (i
> 1) { /* only if need to sort */
5071 qsort ((void *) tc
,i
,sizeof (THREADNODE
*),mail_thread_compare_date
);
5072 /* relink siblings */
5073 for (j
= 0, --i
; j
< i
; j
++) tc
[j
]->branch
= tc
[j
+1];
5074 tc
[j
]->branch
= NIL
; /* end of root */
5075 root
= tc
[0]; /* establish new root */
5078 hash_reset (ht
); /* discard containers, reset ht */
5080 for (cur
= root
; cur
; cur
= cur
->branch
)
5081 if ((t
= (nxt
= (cur
->sc
? cur
: cur
->next
))->sc
->subject
) && *t
) {
5082 /* add new subject to hash table */
5083 if (!(sub
= hash_lookup (ht
,t
))) hash_add (ht
,t
,cur
,0);
5084 /* if one in table not dummy and */
5085 else if ((s
= (lst
= (THREADNODE
*) sub
[0])->sc
) &&
5086 /* current dummy, or not re/fwd and table is */
5087 (!cur
->sc
|| (!nxt
->sc
->refwd
&& s
->refwd
)))
5088 sub
[0] = (void *) cur
; /* replace with this message */
5092 for (cur
= root
, sis
= NIL
; cur
; cur
= msg
) {
5093 /* do nothing if current message or no sub */
5094 if (!(t
= (cur
->sc
? cur
: cur
->next
)->sc
->subject
) || !*t
||
5095 ((lst
= (THREADNODE
*) (sub
= hash_lookup (ht
,t
))[0]) == cur
))
5096 msg
= (sis
= cur
)->branch
;
5097 else if (!lst
->sc
) { /* is message in the table a dummy? */
5098 /* find youngest daughter of msg in table */
5099 for (msg
= lst
->next
; msg
->branch
; msg
= msg
->branch
);
5100 if (!cur
->sc
) { /* current message a dummy? */
5101 msg
->branch
= cur
->next
;/* current's daughter now dummy's youngest */
5102 msg
= cur
->branch
; /* continue scan at younger sister */
5103 /* now delete this node */
5104 cur
->branch
= cur
->next
= NIL
;
5105 mail_free_threadnode (&cur
);
5107 else { /* current message not a dummy */
5108 msg
->branch
= cur
; /* append as youngest daughter */
5109 msg
= cur
->branch
; /* continue scan at younger sister */
5110 cur
->branch
= NIL
; /* lose our younger sisters */
5113 else { /* no dummies, is current re/fwd, table not? */
5114 if (cur
->sc
->refwd
&& !lst
->sc
->refwd
) {
5115 if (lst
->next
) { /* find youngest daughter of msg in table */
5116 for (msg
= lst
->next
; msg
->branch
; msg
= msg
->branch
);
5117 msg
->branch
= cur
; /* append as youngest daughter */
5119 else lst
->next
= cur
; /* no children, so make the eldest daughter */
5122 else { /* no re/fwd, create a new dummy */
5123 msg
= mail_newthreadnode (NIL
);
5124 if (lst
== root
) { /* msg in table is root? */
5125 root
= lst
->branch
; /* younger sister becomes new root */
5126 /* no longer older sister either */
5127 if (lst
== sis
) sis
= NIL
;
5129 else { /* find older sister of msg in table */
5130 for (nxt
= root
; lst
!= nxt
->branch
; nxt
= nxt
->branch
);
5131 /* remove from older sister */
5132 nxt
->branch
= lst
->branch
;
5134 msg
->next
= lst
; /* msg in table becomes child */
5135 lst
->branch
= cur
; /* current now little sister of msg in table */
5136 if (sis
) { /* have an elder sister? */
5137 if (sis
== lst
) /* rescan if lost her */
5138 for (sis
= root
; cur
!= sis
->branch
; sis
= sis
->branch
);
5139 sis
->branch
= msg
; /* make dummy younger sister of big sister */
5141 else root
= msg
; /* otherwise this is the new root */
5142 sub
[0] = sis
= msg
; /* set new msg in table and new big sister */
5144 msg
= cur
->branch
; /* continue scan at younger sister */
5145 cur
->branch
= NIL
; /* lose our younger sisters */
5147 if (sis
) sis
->branch
= msg
; /* older sister gets this as younger sister */
5148 else root
= msg
; /* otherwise this is the new root */
5150 hash_destroy (&ht
); /* finished with hash table */
5153 root
= mail_thread_sort (root
,tc
);
5154 fs_give ((void **) &tc
); /* finished with sort buffer */
5155 return root
; /* return sorted list */
5158 /* Fetch overview callback to load sortcache for threading
5159 * Accepts: MAIL stream
5160 * UID of this message
5161 * overview of this message
5162 * msgno of this message
5165 void mail_thread_loadcache (MAILSTREAM
*stream
,unsigned long uid
,OVERVIEW
*ov
,
5166 unsigned long msgno
)
5168 if (msgno
&& ov
) { /* just in case */
5169 MESSAGECACHE telt
, *elt
;
5171 SORTCACHE
*s
= (SORTCACHE
*) (*mailcache
) (stream
,msgno
,CH_SORTCACHE
);
5172 if (!s
->subject
&& ov
->subject
) {
5173 s
->refwd
= mail_strip_subject (ov
->subject
,&s
->subject
);
5176 if (!s
->from
&& ov
->from
&& ov
->from
->mailbox
) {
5177 s
->from
= cpystr (ov
->from
->mailbox
);
5180 if (!s
->date
&& ov
->date
&& mail_parse_date (&telt
,ov
->date
)) {
5181 s
->date
= mail_longdate (&telt
);
5184 if (!s
->message_id
&& ov
->message_id
) {
5185 s
->message_id
= mail_thread_parse_msgid (ov
->message_id
,NIL
);
5188 if (!s
->references
&&
5189 !(s
->references
= mail_thread_parse_references (ov
->references
,T
))
5190 && stream
->dtb
&& !strcmp(stream
->dtb
->name
, "imap")
5191 && (elt
= mail_elt (stream
, msgno
)) != NULL
5192 && (env
= elt
->private.msg
.env
) != NULL
5194 && !(s
->references
= mail_thread_parse_references(env
->in_reply_to
, NIL
))) {
5195 /* don't do In-Reply-To with NNTP mailboxes */
5196 s
->references
= mail_newstringlist ();
5199 if (!s
->size
&& ov
->optional
.octets
) {
5200 s
->size
= ov
->optional
.octets
;
5206 /* Thread parse Message ID
5207 * Accepts: pointer to purported Message ID
5208 * pointer to return pointer
5209 * Returns: Message ID or NIL, return pointer updated
5212 char *mail_thread_parse_msgid (char *s
,char **ss
)
5217 if (s
) { /* only for non-NIL strings */
5218 rfc822_skipws (&s
); /* skip whitespace */
5219 /* ignore phrases */
5220 if (((*s
== '<') || (s
= rfc822_parse_phrase (s
))) &&
5221 (adr
= rfc822_parse_routeaddr (s
,&t
,BADHOST
))) {
5222 /* make return msgid */
5223 if (adr
->mailbox
&& adr
->host
)
5224 sprintf (ret
= (char *) fs_get (strlen (adr
->mailbox
) +
5225 strlen (adr
->host
) + 2),"%s@%s",
5226 adr
->mailbox
,adr
->host
);
5227 mail_free_address (&adr
); /* don't need temporary address */
5230 if (ss
) *ss
= t
; /* update return pointer */
5235 /* Thread parse references
5236 * Accepts: pointer to purported references
5237 * parse multiple references flag
5238 * Returns: references or NIL
5241 STRINGLIST
*mail_thread_parse_references (char *s
,long flag
)
5244 STRINGLIST
*ret
= NIL
;
5246 /* found first reference? */
5247 if ((t
= mail_thread_parse_msgid (s
,&s
)) != NULL
) {
5248 (ret
= mail_newstringlist ())->text
.data
= (unsigned char *) t
;
5249 ret
->text
.size
= strlen (t
);
5250 if (flag
) /* parse subsequent references */
5251 for (cur
= ret
; (t
= mail_thread_parse_msgid (s
,&s
)) != NULL
; cur
= cur
->next
) {
5252 (cur
->next
= mail_newstringlist ())->text
.data
= (unsigned char *) t
;
5253 cur
->next
->text
.size
= strlen (t
);
5259 /* Prune dummy messages
5260 * Accepts: candidate container to prune
5261 * older sibling of container, if any
5262 * Returns: container in this position, possibly pruned
5263 * All children and younger siblings are also pruned
5266 container_t
mail_thread_prune_dummy (container_t msg
,container_t ane
)
5268 /* prune container and children */
5269 container_t ret
= msg
? mail_thread_prune_dummy_work (msg
,ane
) : NIL
;
5270 /* prune all younger sisters */
5271 if (ret
) for (ane
= ret
; ane
&& (msg
= SIBLING (ane
)); ane
= msg
)
5272 msg
= mail_thread_prune_dummy_work (msg
,ane
);
5277 /* Prune dummy messages worker routine
5278 * Accepts: candidate container to prune
5279 * older sibling of container, if any
5280 * Returns: container in this position, possibly pruned
5281 * All children are also pruned
5284 container_t
mail_thread_prune_dummy_work (container_t msg
,container_t ane
)
5287 /* get children, if any */
5288 container_t nxt
= mail_thread_prune_dummy (CHILD (msg
),NIL
);
5289 /* just update children if container has msg */
5290 if (CACHE (msg
)) SETCHILD (msg
,nxt
);
5291 else if (!nxt
) { /* delete dummy with no children */
5292 nxt
= SIBLING (msg
); /* get younger sister */
5293 if (ane
) SETSIBLING (ane
,nxt
);
5294 /* prune younger sister if exists */
5295 msg
= nxt
? mail_thread_prune_dummy_work (nxt
,ane
) : NIL
;
5297 /* not if parent root & multiple children */
5298 else if ((cur
= PARENT (msg
)) || !SIBLING (nxt
)) {
5299 /* OK to promote, try younger sister of aunt */
5300 if (ane
) SETSIBLING (ane
,nxt
);
5301 /* otherwise promote to child of grandmother */
5302 else if (cur
) SETCHILD (cur
,nxt
);
5303 SETPARENT (nxt
,cur
); /* set parent as well */
5304 /* look for end of siblings in new container */
5305 for (cur
= nxt
; SIBLING (cur
); cur
= SIBLING (cur
));
5306 /* reattach deleted container's siblings */
5307 SETSIBLING (cur
,SIBLING (msg
));
5308 /* prune and return new container */
5309 msg
= mail_thread_prune_dummy_work (nxt
,ane
);
5311 else SETCHILD (msg
,nxt
); /* in case child pruned */
5312 return msg
; /* return this message */
5315 /* Test that purported mother is not a child of purported daughter
5318 * Returns: T if circular parentage exists, else NIL
5321 long mail_thread_check_child (container_t mother
,container_t daughter
)
5323 if (mother
) { /* only if mother non-NIL */
5324 if (mother
== daughter
) return T
;
5325 for (daughter
= CHILD (daughter
); daughter
; daughter
= SIBLING (daughter
))
5326 if (mail_thread_check_child (mother
,daughter
)) return T
;
5332 /* Generate threadnodes from containers
5333 * Accepts: Mail stream
5336 * Return: threadnode list
5339 THREADNODE
*mail_thread_c2node (MAILSTREAM
*stream
,container_t con
,long flags
)
5341 THREADNODE
*ret
,*cur
;
5344 /* for each container */
5345 for (ret
= cur
= NIL
; con
; con
= SIBLING (con
)) {
5346 s
= CACHE (con
); /* yes, get its sortcache */
5347 /* create node for it */
5348 if (ret
) cur
= cur
->branch
= mail_newthreadnode (s
);
5349 else ret
= cur
= mail_newthreadnode (s
);
5350 /* attach sequence or UID for non-dummy */
5351 if (s
) cur
->num
= (flags
& SE_UID
) ? mail_uid (stream
,s
->num
) : s
->num
;
5352 /* attach the children */
5353 if ((nxt
= CHILD (con
)) != NULL
) cur
->next
= mail_thread_c2node (stream
,nxt
,flags
);
5358 /* Sort thread tree by date
5359 * Accepts: thread tree to sort
5360 * qsort vector to sort
5361 * Returns: sorted thread tree
5364 THREADNODE
*mail_thread_sort (THREADNODE
*thr
,THREADNODE
**tc
)
5368 /* sort children of each thread */
5369 for (cur
= thr
; cur
; cur
= cur
->branch
)
5370 if (cur
->next
) cur
->next
= mail_thread_sort (cur
->next
,tc
);
5371 /* Must do this in a separate pass since recursive call will clobber tc */
5372 /* load threadcache and count nodes to sort */
5373 for (i
= 0, cur
= thr
; cur
; cur
= cur
->branch
) tc
[i
++] = cur
;
5374 if (i
> 1) { /* only if need to sort */
5375 qsort ((void *) tc
,i
,sizeof (THREADNODE
*),mail_thread_compare_date
);
5376 /* relink root siblings */
5377 for (j
= 0, --i
; j
< i
; j
++) tc
[j
]->branch
= tc
[j
+1];
5378 tc
[j
]->branch
= NIL
; /* end of root */
5380 return i
? tc
[0] : NIL
; /* return new head of list */
5384 /* Thread compare date
5385 * Accept: first message sort cache element
5386 * second message sort cache element
5387 * Returns: -1 if a1 < a2, 1 if a1 > a2
5389 * This assumes that a sort cache element is either a message (with a
5390 * sortcache entry) or a dummy with a message (with sortcache entry) child.
5391 * This is true of both the ORDEREDSUBJECT (no dummies) and REFERENCES
5392 * (dummies only at top-level, and with non-dummy children).
5394 * If a new algorithm allows a dummy parent to have a dummy child, this
5395 * routine must be changed if it is to be used by that algorithm.
5397 * Messages with bogus dates are always sorted at the top.
5400 int mail_thread_compare_date (const void *a1
,const void *a2
)
5402 THREADNODE
*t1
= *(THREADNODE
**) a1
;
5403 THREADNODE
*t2
= *(THREADNODE
**) a2
;
5404 SORTCACHE
*s1
= t1
->sc
? t1
->sc
: t1
->next
->sc
;
5405 SORTCACHE
*s2
= t2
->sc
? t2
->sc
: t2
->next
->sc
;
5406 int ret
= compare_ulong (s1
->date
,s2
->date
);
5407 /* use number as final tie-breaker */
5408 return ret
? ret
: compare_ulong (s1
->num
,s2
->num
);
5411 /* Mail parse sequence
5412 * Accepts: mail stream
5414 * Returns: T if parse successful, else NIL
5417 long mail_sequence (MAILSTREAM
*stream
,unsigned char *sequence
)
5419 unsigned long i
,j
,x
;
5420 for (i
= 1; i
<= stream
->nmsgs
; i
++) mail_elt (stream
,i
)->sequence
= NIL
;
5421 while (sequence
&& *sequence
){/* while there is something to parse */
5422 if (*sequence
== '*') { /* maximum message */
5423 if (stream
->nmsgs
) i
= stream
->nmsgs
;
5425 MM_LOG ("No messages, so no maximum message number",ERROR
);
5428 sequence
++; /* skip past * */
5430 /* parse and validate message number */
5431 else if (!isdigit (*sequence
)) {
5432 MM_LOG ("Syntax error in sequence",ERROR
);
5435 else if (!(i
= strtoul (sequence
,(char **) &sequence
,10)) ||
5436 (i
> stream
->nmsgs
)) {
5437 MM_LOG ("Sequence out of range",ERROR
);
5440 switch (*sequence
) { /* see what the delimiter is */
5441 case ':': /* sequence range */
5442 if (*++sequence
== '*') { /* maximum message */
5443 if (stream
->nmsgs
) j
= stream
->nmsgs
;
5445 MM_LOG ("No messages, so no maximum message number",ERROR
);
5448 sequence
++; /* skip past * */
5450 /* parse end of range */
5451 else if (!(j
= strtoul (sequence
,(char **) &sequence
,10)) ||
5452 (j
> stream
->nmsgs
)) {
5453 MM_LOG ("Sequence range invalid",ERROR
);
5456 if (*sequence
&& *sequence
++ != ',') {
5457 MM_LOG ("Sequence range syntax error",ERROR
);
5460 if (i
> j
) { /* swap the range if backwards */
5461 x
= i
; i
= j
; j
= x
;
5463 /* mark each item in the sequence */
5464 while (i
<= j
) mail_elt (stream
,j
--)->sequence
= T
;
5466 case ',': /* single message */
5467 ++sequence
; /* skip the delimiter, fall into end case */
5468 case '\0': /* end of sequence, mark this message */
5469 mail_elt (stream
,i
)->sequence
= T
;
5471 default: /* anything else is a syntax error! */
5472 MM_LOG ("Sequence syntax error",ERROR
);
5476 return T
; /* successfully parsed sequence */
5480 * Accepts: MAIL stream
5481 * flag list as a character string
5482 * pointer to user flags to return
5483 * Returns: system flags
5486 long mail_parse_flags (MAILSTREAM
*stream
,char *flag
,unsigned long *uf
)
5488 char *t
,*n
,*s
,tmp
[MAILTMPLEN
],msg
[MAILTMPLEN
];
5491 *uf
= 0; /* initially no user flags */
5492 if (flag
&& *flag
) { /* no-op if no flag string */
5493 /* check if a list and make sure valid */
5494 if (((i
= (*flag
== '(')) ^ (flag
[strlen (flag
)-1] == ')')) ||
5495 (strlen (flag
) >= MAILTMPLEN
)) {
5496 MM_LOG ("Bad flag list",ERROR
);
5499 /* copy the flag string w/o list construct */
5500 strncpy (n
= tmp
,flag
+i
,(j
= strlen (flag
) - (2*i
)));
5502 while ((t
= n
) && *t
) { /* parse the flags */
5503 /* find end of flag */
5504 if ((n
= strchr (t
,' ')) != NULL
) *n
++ = '\0';
5505 if (*t
== '\\') { /* system flag? */
5506 if (!compare_cstring (t
+1,"SEEN")) f
|= fSEEN
;
5507 else if (!compare_cstring (t
+1,"DELETED")) f
|= fDELETED
;
5508 else if (!compare_cstring (t
+1,"FLAGGED")) f
|= fFLAGGED
;
5509 else if (!compare_cstring (t
+1,"ANSWERED")) f
|= fANSWERED
;
5510 else if (!compare_cstring (t
+1,"DRAFT")) f
|= fDRAFT
;
5512 sprintf (msg
,"Unsupported system flag: %.80s",t
);
5517 else { /* keyword flag */
5518 for (i
= j
= 0; /* user flag, search through table */
5519 !i
&& (j
< NUSERFLAGS
) && (s
= stream
->user_flags
[j
]); ++j
)
5520 if (!compare_cstring (t
,s
)) *uf
|= i
= 1 << j
;
5521 if (!i
) { /* flag not found, can it be created? */
5522 if (stream
->kwd_create
&& (j
< NUSERFLAGS
) && *t
&&
5523 (strlen (t
) <= MAXUSERFLAG
)) {
5524 for (s
= t
; t
&& *s
; s
++) switch (*s
) {
5525 default: /* all other characters */
5526 /* SPACE, CTL, or not CHAR */
5527 if ((*s
> ' ') && (*s
< 0x7f)) break;
5528 case '*': case '%': /* list_wildcards */
5529 case '"': case '\\':/* quoted-specials */
5531 case '(': case ')': case '{':
5532 case ']': /* resp-specials */
5533 sprintf (msg
,"Invalid flag: %.80s",t
);
5537 if (t
) { /* only if valid */
5538 *uf
|= 1 << j
; /* set the bit */
5539 stream
->user_flags
[j
] = cpystr (t
);
5540 /* if out of user flags */
5541 if (j
== NUSERFLAGS
- 1) stream
->kwd_create
= NIL
;
5545 if (*t
) sprintf (msg
,"Unknown flag: %.80s",t
);
5546 else strcpy (msg
,"Empty flag invalid");
5556 /* Mail check network stream for usability with new name
5557 * Accepts: MAIL stream
5558 * candidate new name
5559 * Returns: T if stream can be used, NIL otherwise
5562 long mail_usable_network_stream (MAILSTREAM
*stream
,char *name
)
5566 long ret
= (stream
&& stream
->dtb
&& !(stream
->dtb
->flags
& DR_LOCAL
) &&
5567 mail_valid_net_parse (name
,&nmb
) &&
5568 mail_valid_net_parse (stream
->mailbox
,&smb
) &&
5569 mail_valid_net_parse (stream
->original_mailbox
,&omb
) &&
5570 ((!compare_cstring (smb
.host
,trustdns
?
5571 (s
= tcp_canonical (nmb
.host
)) : nmb
.host
) &&
5572 !strcmp (smb
.service
,nmb
.service
) &&
5573 (!nmb
.port
|| (smb
.port
== nmb
.port
)) &&
5574 (nmb
.anoflag
== stream
->anonymous
) &&
5575 (!nmb
.user
[0] || !strcmp (smb
.user
,nmb
.user
))) ||
5576 (!compare_cstring (omb
.host
,nmb
.host
) &&
5577 !strcmp (omb
.service
,nmb
.service
) &&
5578 (!nmb
.port
|| (omb
.port
== nmb
.port
)) &&
5579 (nmb
.anoflag
== stream
->anonymous
) &&
5580 (!nmb
.user
[0] || !strcmp (omb
.user
,nmb
.user
))))) ? LONGT
: NIL
;
5581 if(s
) fs_give((void **) &s
);
5585 /* Mail data structure instantiation routines */
5588 /* Mail instantiate cache elt
5589 * Accepts: initial message number
5590 * Returns: new cache elt
5593 MESSAGECACHE
*mail_new_cache_elt (unsigned long msgno
)
5595 MESSAGECACHE
*elt
= (MESSAGECACHE
*) memset (fs_get (sizeof (MESSAGECACHE
)),
5596 0,sizeof (MESSAGECACHE
));
5597 elt
->lockcount
= 1; /* initially only cache references it */
5598 elt
->msgno
= msgno
; /* message number */
5603 /* Mail instantiate envelope
5604 * Returns: new envelope
5607 ENVELOPE
*mail_newenvelope (void)
5609 return (ENVELOPE
*) memset (fs_get (sizeof (ENVELOPE
)),0,sizeof (ENVELOPE
));
5613 /* Mail instantiate address
5614 * Returns: new address
5617 ADDRESS
*mail_newaddr (void)
5619 return (ADDRESS
*) memset (fs_get (sizeof (ADDRESS
)),0,sizeof (ADDRESS
));
5622 /* Mail instantiate body
5626 BODY
*mail_newbody (void)
5628 return mail_initbody ((BODY
*) fs_get (sizeof (BODY
)));
5632 /* Mail initialize body
5637 BODY
*mail_initbody (BODY
*body
)
5639 memset ((void *) body
,0,sizeof (BODY
));
5640 body
->type
= TYPETEXT
; /* content type */
5641 body
->encoding
= ENC7BIT
; /* content encoding */
5646 /* Mail instantiate body parameter
5647 * Returns: new body part
5650 PARAMETER
*mail_newbody_parameter (void)
5652 return (PARAMETER
*) memset (fs_get (sizeof(PARAMETER
)),0,sizeof(PARAMETER
));
5656 /* Mail instantiate body part
5657 * Returns: new body part
5660 PART
*mail_newbody_part (void)
5662 PART
*part
= (PART
*) memset (fs_get (sizeof (PART
)),0,sizeof (PART
));
5663 mail_initbody (&part
->body
); /* initialize the body */
5668 /* Mail instantiate body message part
5669 * Returns: new body message part
5672 MESSAGE
*mail_newmsg (void)
5674 return (MESSAGE
*) memset (fs_get (sizeof (MESSAGE
)),0,sizeof (MESSAGE
));
5677 /* Mail instantiate string list
5678 * Returns: new string list
5681 STRINGLIST
*mail_newstringlist (void)
5683 return (STRINGLIST
*) memset (fs_get (sizeof (STRINGLIST
)),0,
5684 sizeof (STRINGLIST
));
5688 /* Mail instantiate new search program
5689 * Returns: new search program
5692 SEARCHPGM
*mail_newsearchpgm (void)
5694 return (SEARCHPGM
*) memset (fs_get (sizeof(SEARCHPGM
)),0,sizeof(SEARCHPGM
));
5698 /* Mail instantiate new search program
5699 * Accepts: header line name
5700 * Returns: new search program
5703 SEARCHHEADER
*mail_newsearchheader (char *line
,char *text
)
5705 SEARCHHEADER
*hdr
= (SEARCHHEADER
*) memset (fs_get (sizeof (SEARCHHEADER
)),
5706 0,sizeof (SEARCHHEADER
));
5707 hdr
->line
.size
= strlen ((char *) (hdr
->line
.data
=
5708 (unsigned char *) cpystr (line
)));
5709 hdr
->text
.size
= strlen ((char *) (hdr
->text
.data
=
5710 (unsigned char *) cpystr (text
)));
5715 /* Mail instantiate new search set
5716 * Returns: new search set
5719 SEARCHSET
*mail_newsearchset (void)
5721 return (SEARCHSET
*) memset (fs_get (sizeof(SEARCHSET
)),0,sizeof(SEARCHSET
));
5725 /* Mail instantiate new search or
5726 * Returns: new search or
5729 SEARCHOR
*mail_newsearchor (void)
5731 SEARCHOR
*or = (SEARCHOR
*) memset (fs_get (sizeof (SEARCHOR
)),0,
5733 or->first
= mail_newsearchpgm ();
5734 or->second
= mail_newsearchpgm ();
5738 /* Mail instantiate new searchpgmlist
5739 * Returns: new searchpgmlist
5742 SEARCHPGMLIST
*mail_newsearchpgmlist (void)
5744 SEARCHPGMLIST
*pgl
= (SEARCHPGMLIST
*)
5745 memset (fs_get (sizeof (SEARCHPGMLIST
)),0,sizeof (SEARCHPGMLIST
));
5746 pgl
->pgm
= mail_newsearchpgm ();
5751 /* Mail instantiate new sortpgm
5752 * Returns: new sortpgm
5755 SORTPGM
*mail_newsortpgm (void)
5757 return (SORTPGM
*) memset (fs_get (sizeof (SORTPGM
)),0,sizeof (SORTPGM
));
5761 /* Mail instantiate new threadnode
5762 * Accepts: sort cache for thread node
5763 * Returns: new threadnode
5766 THREADNODE
*mail_newthreadnode (SORTCACHE
*sc
)
5768 THREADNODE
*thr
= (THREADNODE
*) memset (fs_get (sizeof (THREADNODE
)),0,
5769 sizeof (THREADNODE
));
5770 if (sc
) thr
->sc
= sc
; /* initialize sortcache */
5775 /* Mail instantiate new acllist
5776 * Returns: new acllist
5779 ACLLIST
*mail_newacllist (void)
5781 return (ACLLIST
*) memset (fs_get (sizeof (ACLLIST
)),0,sizeof (ACLLIST
));
5785 /* Mail instantiate new quotalist
5786 * Returns: new quotalist
5789 QUOTALIST
*mail_newquotalist (void)
5791 return (QUOTALIST
*) memset (fs_get (sizeof (QUOTALIST
)),0,
5792 sizeof (QUOTALIST
));
5795 /* Mail garbage collection routines */
5798 /* Mail garbage collect body
5799 * Accepts: pointer to body pointer
5802 void mail_free_body (BODY
**body
)
5804 if (*body
) { /* only free if exists */
5805 mail_free_body_data (*body
);/* free its data */
5806 fs_give ((void **) body
); /* return body to free storage */
5811 /* Mail garbage collect body data
5812 * Accepts: body pointer
5815 void mail_free_body_data (BODY
*body
)
5817 switch (body
->type
) { /* free contents */
5818 case TYPEMULTIPART
: /* multiple part */
5819 mail_free_body_part (&body
->nested
.part
);
5821 case TYPEMESSAGE
: /* encapsulated message */
5822 if (body
->subtype
&& !strcmp (body
->subtype
,"RFC822")) {
5823 mail_free_stringlist (&body
->nested
.msg
->lines
);
5824 mail_gc_msg (body
->nested
.msg
,GC_ENV
| GC_TEXTS
);
5826 if (body
->nested
.msg
) fs_give ((void **) &body
->nested
.msg
);
5831 if (body
->subtype
) fs_give ((void **) &body
->subtype
);
5832 mail_free_body_parameter (&body
->parameter
);
5833 if (body
->id
) fs_give ((void **) &body
->id
);
5834 if (body
->description
) fs_give ((void **) &body
->description
);
5835 if (body
->disposition
.type
) fs_give ((void **) &body
->disposition
.type
);
5836 if (body
->disposition
.parameter
)
5837 mail_free_body_parameter (&body
->disposition
.parameter
);
5838 if (body
->language
) mail_free_stringlist (&body
->language
);
5839 if (body
->location
) fs_give ((void **) &body
->location
);
5840 if (body
->mime
.text
.data
) fs_give ((void **) &body
->mime
.text
.data
);
5841 if (body
->contents
.text
.data
) fs_give ((void **) &body
->contents
.text
.data
);
5842 if (body
->md5
) fs_give ((void **) &body
->md5
);
5843 if (mailfreebodysparep
&& body
->sparep
)
5844 (*mailfreebodysparep
) (&body
->sparep
);
5847 /* Mail garbage collect body parameter
5848 * Accepts: pointer to body parameter pointer
5851 void mail_free_body_parameter (PARAMETER
**parameter
)
5853 if (*parameter
) { /* only free if exists */
5854 if ((*parameter
)->attribute
) fs_give ((void **) &(*parameter
)->attribute
);
5855 if ((*parameter
)->value
) fs_give ((void **) &(*parameter
)->value
);
5856 /* run down the list as necessary */
5857 mail_free_body_parameter (&(*parameter
)->next
);
5858 /* return body part to free storage */
5859 fs_give ((void **) parameter
);
5864 /* Mail garbage collect body part
5865 * Accepts: pointer to body part pointer
5868 void mail_free_body_part (PART
**part
)
5870 if (*part
) { /* only free if exists */
5871 mail_free_body_data (&(*part
)->body
);
5872 /* run down the list as necessary */
5873 mail_free_body_part (&(*part
)->next
);
5874 fs_give ((void **) part
); /* return body part to free storage */
5878 /* Mail garbage collect message cache
5879 * Accepts: mail stream
5881 * The message cache is set to NIL when this function finishes.
5884 void mail_free_cache (MAILSTREAM
*stream
)
5886 /* do driver specific stuff first */
5887 mail_gc (stream
,GC_ELT
| GC_ENV
| GC_TEXTS
);
5888 /* flush the cache */
5889 (*mailcache
) (stream
,(long) 0,CH_INIT
);
5893 /* Mail garbage collect cache element
5894 * Accepts: pointer to cache element pointer
5897 void mail_free_elt (MESSAGECACHE
**elt
)
5899 /* only free if exists and no sharers */
5900 if (*elt
&& !--(*elt
)->lockcount
) {
5901 mail_gc_msg (&(*elt
)->private.msg
,GC_ENV
| GC_TEXTS
);
5902 if (mailfreeeltsparep
&& (*elt
)->sparep
)
5903 (*mailfreeeltsparep
) (&(*elt
)->sparep
);
5904 fs_give ((void **) elt
);
5906 else *elt
= NIL
; /* else simply drop pointer */
5909 /* Mail garbage collect envelope
5910 * Accepts: pointer to envelope pointer
5913 void mail_free_envelope (ENVELOPE
**env
)
5915 if (*env
) { /* only free if exists */
5916 if ((*env
)->remail
) fs_give ((void **) &(*env
)->remail
);
5917 mail_free_address (&(*env
)->return_path
);
5918 if ((*env
)->date
) fs_give ((void **) &(*env
)->date
);
5919 mail_free_address (&(*env
)->from
);
5920 mail_free_address (&(*env
)->sender
);
5921 mail_free_address (&(*env
)->reply_to
);
5922 if ((*env
)->subject
) fs_give ((void **) &(*env
)->subject
);
5923 mail_free_address (&(*env
)->to
);
5924 mail_free_address (&(*env
)->cc
);
5925 mail_free_address (&(*env
)->bcc
);
5926 if ((*env
)->in_reply_to
) fs_give ((void **) &(*env
)->in_reply_to
);
5927 if ((*env
)->message_id
) fs_give ((void **) &(*env
)->message_id
);
5928 if ((*env
)->newsgroups
) fs_give ((void **) &(*env
)->newsgroups
);
5929 if ((*env
)->followup_to
) fs_give ((void **) &(*env
)->followup_to
);
5930 if ((*env
)->references
) fs_give ((void **) &(*env
)->references
);
5931 if (mailfreeenvelopesparep
&& (*env
)->sparep
)
5932 (*mailfreeenvelopesparep
) (&(*env
)->sparep
);
5933 fs_give ((void **) env
); /* return envelope to free storage */
5938 /* Mail garbage collect address
5939 * Accepts: pointer to address pointer
5942 void mail_free_address (ADDRESS
**address
)
5944 if (*address
) { /* only free if exists */
5945 if ((*address
)->personal
) fs_give ((void **) &(*address
)->personal
);
5946 if ((*address
)->adl
) fs_give ((void **) &(*address
)->adl
);
5947 if ((*address
)->mailbox
) fs_give ((void **) &(*address
)->mailbox
);
5948 if ((*address
)->host
) fs_give ((void **) &(*address
)->host
);
5949 if ((*address
)->error
) fs_give ((void **) &(*address
)->error
);
5950 if ((*address
)->orcpt
.type
) fs_give ((void **) &(*address
)->orcpt
.type
);
5951 if ((*address
)->orcpt
.addr
) fs_give ((void **) &(*address
)->orcpt
.addr
);
5952 mail_free_address (&(*address
)->next
);
5953 fs_give ((void **) address
);/* return address to free storage */
5958 /* Mail garbage collect stringlist
5959 * Accepts: pointer to stringlist pointer
5962 void mail_free_stringlist (STRINGLIST
**string
)
5964 if (*string
) { /* only free if exists */
5965 if ((*string
)->text
.data
) fs_give ((void **) &(*string
)->text
.data
);
5966 mail_free_stringlist (&(*string
)->next
);
5967 fs_give ((void **) string
); /* return string to free storage */
5971 /* Mail garbage collect searchpgm
5972 * Accepts: pointer to searchpgm pointer
5975 void mail_free_searchpgm (SEARCHPGM
**pgm
)
5977 if (*pgm
) { /* only free if exists */
5978 mail_free_searchset (&(*pgm
)->msgno
);
5979 mail_free_searchset (&(*pgm
)->uid
);
5980 mail_free_searchor (&(*pgm
)->or);
5981 mail_free_searchpgmlist (&(*pgm
)->not);
5982 mail_free_searchheader (&(*pgm
)->header
);
5983 mail_free_stringlist (&(*pgm
)->bcc
);
5984 mail_free_stringlist (&(*pgm
)->body
);
5985 mail_free_stringlist (&(*pgm
)->cc
);
5986 mail_free_stringlist (&(*pgm
)->from
);
5987 mail_free_stringlist (&(*pgm
)->keyword
);
5988 mail_free_stringlist (&(*pgm
)->subject
);
5989 mail_free_stringlist (&(*pgm
)->text
);
5990 mail_free_stringlist (&(*pgm
)->to
);
5991 mail_free_stringlist (&(*pgm
)->x_gm_ext1
);
5992 fs_give ((void **) pgm
); /* return program to free storage */
5997 /* Mail garbage collect searchheader
5998 * Accepts: pointer to searchheader pointer
6001 void mail_free_searchheader (SEARCHHEADER
**hdr
)
6003 if (*hdr
) { /* only free if exists */
6004 if ((*hdr
)->line
.data
) fs_give ((void **) &(*hdr
)->line
.data
);
6005 if ((*hdr
)->text
.data
) fs_give ((void **) &(*hdr
)->text
.data
);
6006 mail_free_searchheader (&(*hdr
)->next
);
6007 fs_give ((void **) hdr
); /* return header to free storage */
6012 /* Mail garbage collect searchset
6013 * Accepts: pointer to searchset pointer
6016 void mail_free_searchset (SEARCHSET
**set
)
6018 if (*set
) { /* only free if exists */
6019 mail_free_searchset (&(*set
)->next
);
6020 fs_give ((void **) set
); /* return set to free storage */
6024 /* Mail garbage collect searchor
6025 * Accepts: pointer to searchor pointer
6028 void mail_free_searchor (SEARCHOR
**orl
)
6030 if (*orl
) { /* only free if exists */
6031 mail_free_searchpgm (&(*orl
)->first
);
6032 mail_free_searchpgm (&(*orl
)->second
);
6033 mail_free_searchor (&(*orl
)->next
);
6034 fs_give ((void **) orl
); /* return searchor to free storage */
6039 /* Mail garbage collect search program list
6040 * Accepts: pointer to searchpgmlist pointer
6043 void mail_free_searchpgmlist (SEARCHPGMLIST
**pgl
)
6045 if (*pgl
) { /* only free if exists */
6046 mail_free_searchpgm (&(*pgl
)->pgm
);
6047 mail_free_searchpgmlist (&(*pgl
)->next
);
6048 fs_give ((void **) pgl
); /* return searchpgmlist to free storage */
6053 /* Mail garbage collect namespace
6054 * Accepts: poiner to namespace
6057 void mail_free_namespace (NAMESPACE
**n
)
6060 fs_give ((void **) &(*n
)->name
);
6061 mail_free_namespace (&(*n
)->next
);
6062 mail_free_body_parameter (&(*n
)->param
);
6063 fs_give ((void **) n
); /* return namespace to free storage */
6067 /* Mail garbage collect sort program
6068 * Accepts: pointer to sortpgm pointer
6071 void mail_free_sortpgm (SORTPGM
**pgm
)
6073 if (*pgm
) { /* only free if exists */
6074 mail_free_sortpgm (&(*pgm
)->next
);
6075 fs_give ((void **) pgm
); /* return sortpgm to free storage */
6080 /* Mail garbage collect thread node
6081 * Accepts: pointer to threadnode pointer
6084 void mail_free_threadnode (THREADNODE
**thr
)
6086 if (*thr
) { /* only free if exists */
6087 mail_free_threadnode (&(*thr
)->branch
);
6088 mail_free_threadnode (&(*thr
)->next
);
6089 fs_give ((void **) thr
); /* return threadnode to free storage */
6094 /* Mail garbage collect acllist
6095 * Accepts: pointer to acllist pointer
6098 void mail_free_acllist (ACLLIST
**al
)
6100 if (*al
) { /* only free if exists */
6101 if ((*al
)->identifier
) fs_give ((void **) &(*al
)->identifier
);
6102 if ((*al
)->rights
) fs_give ((void **) &(*al
)->rights
);
6103 mail_free_acllist (&(*al
)->next
);
6104 fs_give ((void **) al
); /* return acllist to free storage */
6109 /* Mail garbage collect quotalist
6110 * Accepts: pointer to quotalist pointer
6113 void mail_free_quotalist (QUOTALIST
**ql
)
6115 if (*ql
) { /* only free if exists */
6116 if ((*ql
)->name
) fs_give ((void **) &(*ql
)->name
);
6117 mail_free_quotalist (&(*ql
)->next
);
6118 fs_give ((void **) ql
); /* return quotalist to free storage */
6122 /* Link authenicator
6123 * Accepts: authenticator to add to list
6126 void auth_link (AUTHENTICATOR
*auth
)
6128 if (!auth
->valid
|| (*auth
->valid
) ()) {
6129 AUTHENTICATOR
**a
= &mailauthenticators
;
6130 while (*a
) a
= &(*a
)->next
; /* find end of list of authenticators */
6131 *a
= auth
; /* put authenticator at the end */
6132 auth
->next
= NIL
; /* this authenticator is the end of the list */
6137 /* Authenticate access
6138 * Accepts: mechanism name
6139 * responder function
6142 * Returns: authenticated user name or NIL
6145 char *mail_auth (char *mechanism
,authresponse_t resp
,int argc
,char *argv
[])
6147 AUTHENTICATOR
*auth
;
6148 for (auth
= mailauthenticators
; auth
; auth
= auth
->next
)
6149 if (auth
->server
&& !compare_cstring (auth
->name
,mechanism
))
6150 return (!(auth
->flags
& AU_DISABLE
) &&
6151 ((auth
->flags
& AU_SECURE
) ||
6152 !mail_parameters (NIL
,GET_DISABLEPLAINTEXT
,NIL
))) ?
6153 (*auth
->server
) (resp
,argc
,argv
) : NIL
;
6154 return NIL
; /* no authenticator found */
6157 /* Lookup authenticator index
6158 * Accepts: authenticator index
6159 * Returns: authenticator, or 0 if not found
6162 AUTHENTICATOR
*mail_lookup_auth (unsigned long i
)
6164 AUTHENTICATOR
*auth
= mailauthenticators
;
6165 while (auth
&& --i
) auth
= auth
->next
;
6170 /* Lookup authenticator name
6171 * Accepts: authenticator name
6172 * required authenticator flags
6173 * Returns: index in authenticator chain, or 0 if not found
6176 unsigned int mail_lookup_auth_name (char *mechanism
,long flags
)
6179 AUTHENTICATOR
*auth
;
6180 for (i
= 1, auth
= mailauthenticators
; auth
; i
++, auth
= auth
->next
)
6181 if (auth
->client
&& !(flags
& ~auth
->flags
) &&
6182 !(auth
->flags
& AU_DISABLE
) && !compare_cstring (auth
->name
,mechanism
))
6187 /* Standard TCP/IP network driver */
6189 static NETDRIVER tcpdriver
= {
6190 tcp_open
, /* open connection */
6191 tcp_aopen
, /* open preauthenticated connection */
6192 tcp_getline
, /* get a line */
6193 tcp_getbuffer
, /* get a buffer */
6194 tcp_soutr
, /* output pushed data */
6195 tcp_sout
, /* output string */
6196 tcp_close
, /* close connection */
6197 tcp_host
, /* return host name */
6198 tcp_remotehost
, /* return remote host name */
6199 tcp_port
, /* return port number */
6200 tcp_localhost
/* return local host name */
6205 * Accepts: NETMBX specifier to open
6206 * default network driver
6211 * Returns: Network stream if success, else NIL
6214 NETSTREAM
*net_open (NETMBX
*mb
,NETDRIVER
*dv
,unsigned long port
,
6215 NETDRIVER
*ssld
,char *ssls
,unsigned long sslp
)
6217 NETSTREAM
*stream
= NIL
;
6218 char tmp
[MAILTMPLEN
];
6219 unsigned long flags
= mb
->novalidate
? NET_NOVALIDATECERT
: 0;
6220 flags
|= mb
->tls1
|| mb
->tlsflag
? NET_TRYTLS1
: 0;
6221 flags
|= mb
->tls1_1
? NET_TRYTLS1_1
: 0;
6222 flags
|= mb
->tls1_2
? NET_TRYTLS1_2
: 0;
6223 flags
|= mb
->dtls1
? NET_TRYDTLS1
: 0;
6224 if (strlen (mb
->host
) >= NETMAXHOST
) {
6225 sprintf (tmp
,"Invalid host name: %.80s",mb
->host
);
6228 /* use designated driver if given */
6229 else if (dv
) stream
= net_open_work (dv
,mb
->host
,mb
->service
,port
,mb
->port
,
6231 else if (mb
->sslflag
&& ssld
) /* use ssl if sslflag lit */
6232 stream
= net_open_work (ssld
,mb
->host
,ssls
,sslp
,mb
->port
,flags
);
6233 /* if trysslfirst and can open ssl... */
6234 else if ((mb
->trysslflag
|| trysslfirst
) && ssld
&&
6235 (stream
= net_open_work (ssld
,mb
->host
,ssls
,sslp
,mb
->port
,
6236 flags
| NET_SILENT
| NET_TRYSSL
))) {
6237 if (net_sout (stream
,"",0)) mb
->sslflag
= T
;
6239 net_close (stream
); /* flush fake SSL stream */
6243 /* default to TCP driver */
6244 else stream
= net_open_work (&tcpdriver
,mb
->host
,mb
->service
,port
,mb
->port
,
6249 /* Network open worker routine
6250 * Accepts: network driver
6252 * service name to look up port
6253 * port number if service name not found
6254 * port number to override service name
6255 * flags (passed on top of port)
6256 * Returns: Network stream if success, else NIL
6259 NETSTREAM
*net_open_work (NETDRIVER
*dv
,char *host
,char *service
,
6260 unsigned long port
,unsigned long portoverride
,
6261 unsigned long flags
)
6263 NETSTREAM
*stream
= NIL
;
6265 if (service
&& (*service
== '*')) {
6266 flags
|= NET_NOOPENTIMEOUT
; /* mark that no timeout is desired */
6267 ++service
; /* no longer need the no timeout indicator */
6269 if (portoverride
) { /* explicit port number? */
6270 service
= NIL
; /* yes, override service name */
6271 port
= portoverride
; /* use that instead of default port */
6273 if ((tstream
= (*dv
->open
) (host
,service
,port
| flags
)) != NULL
){
6274 stream
= (NETSTREAM
*) fs_get (sizeof (NETSTREAM
));
6275 stream
->stream
= tstream
;
6282 /* Network authenticated open
6283 * Accepts: network driver
6286 * return user name buffer
6287 * Returns: Network stream if success else NIL
6290 NETSTREAM
*net_aopen (NETDRIVER
*dv
,NETMBX
*mb
,char *service
,char *user
)
6292 NETSTREAM
*stream
= NIL
;
6294 if (!dv
) dv
= &tcpdriver
; /* default to TCP driver */
6295 if ((tstream
= (*dv
->aopen
) (mb
,service
,user
)) != NULL
) {
6296 stream
= (NETSTREAM
*) fs_get (sizeof (NETSTREAM
));
6297 stream
->stream
= tstream
;
6303 /* Network receive line
6304 * Accepts: Network stream
6305 * Returns: text line string or NIL if failure
6308 char *net_getline (NETSTREAM
*stream
)
6310 return (*stream
->dtb
->getline
) (stream
->stream
);
6314 /* Network receive buffer
6315 * Accepts: Network stream (must be void * for use as readfn_t)
6317 * buffer to read into
6318 * Returns: T if success, NIL otherwise
6321 long net_getbuffer (void *st
,unsigned long size
,char *buffer
)
6323 NETSTREAM
*stream
= (NETSTREAM
*) st
;
6324 return (*stream
->dtb
->getbuffer
) (stream
->stream
,size
,buffer
);
6328 /* Network send null-terminated string
6329 * Accepts: Network stream
6331 * Returns: T if success else NIL
6334 long net_soutr (NETSTREAM
*stream
,char *string
)
6336 return (*stream
->dtb
->soutr
) (stream
->stream
,string
);
6340 /* Network send string
6341 * Accepts: Network stream
6344 * Returns: T if success else NIL
6347 long net_sout (NETSTREAM
*stream
,char *string
,unsigned long size
)
6349 return (*stream
->dtb
->sout
) (stream
->stream
,string
,size
);
6353 * Accepts: Network stream
6356 void net_close (NETSTREAM
*stream
)
6358 if (stream
->stream
) (*stream
->dtb
->close
) (stream
->stream
);
6359 fs_give ((void **) &stream
);
6363 /* Network get host name
6364 * Accepts: Network stream
6365 * Returns: host name for this stream
6368 char *net_host (NETSTREAM
*stream
)
6370 return (*stream
->dtb
->host
) (stream
->stream
);
6374 /* Network get remote host name
6375 * Accepts: Network stream
6376 * Returns: host name for this stream
6379 char *net_remotehost (NETSTREAM
*stream
)
6381 return (*stream
->dtb
->remotehost
) (stream
->stream
);
6384 /* Network return port for this stream
6385 * Accepts: Network stream
6386 * Returns: port number for this stream
6389 unsigned long net_port (NETSTREAM
*stream
)
6391 return (*stream
->dtb
->port
) (stream
->stream
);
6395 /* Network get local host name
6396 * Accepts: Network stream
6397 * Returns: local host name
6400 char *net_localhost (NETSTREAM
*stream
)
6402 return (*stream
->dtb
->localhost
) (stream
->stream
);
6405 void free_c_client_module_globals(void)