1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Header inclusion, macros, constants, types and the global var declarations.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2013 Steffen "Daode" Nurpmeso <sdaoden@users.sf.net>.
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * Mail -- a mail program
43 * Author: Kurt Shoens (UCB) March 25, 1978
49 #include <sys/types.h>
63 #if defined __STDC_VERSION__ && __STDC_VERSION__ + 0 >= 199901L
66 # include <inttypes.h>
81 * CC support, generic macros etc.
85 #if defined __GNUC__ || defined __clang__
86 # define __EXTEN __extension__
88 # define __PREREQ(X,Y) \
89 (__GNUC__ > (X) || (__GNUC__ == (X) && __GNUC_MINOR__ >= (Y)))
91 # define __PREREQ(X,Y) 1
95 # define __PREREQ(X,Y) 0
98 #define EMPTY_FILE(F) typedef int CONCAT(avoid_empty_file__, F);
100 /* Pointer to size_t */
101 #define PTR2SIZE(X) ((size_t)(uintptr_t)(X))
103 /* Pointer comparison (types from below) */
104 #define PTRCMP(A,C,B) ((uintptr_t)(A) C (uintptr_t)(B))
106 /* Ditto, compare (maybe mixed-signed) integers cases to T bits, unsigned;
107 * Note: doesn't sign-extend correctly, that's still up to the caller */
108 #define UICMP(T,A,C,B) ((ui ## T ## _t)(A) C (ui ## T ## _t)(B))
110 /* Members in constant array */
112 # define NELEM(A) (sizeof(A) / sizeof(A[0]))
115 /* sizeof() for member fields */
116 #define SIZEOF_FIELD(T,F) sizeof(((T *)NULL)->F)
118 /* Casts-away (*NOT* cast-away) */
119 #define UNUSED(X) ((void)(X))
120 #define UNCONST(P) ((void*)(uintptr_t)(void const*)(P))
121 #define UNVOLATILE(P) ((void*)(uintptr_t)(void volatile*)(P))
122 #define UNXXX(T,C,P) ((T)(uintptr_t)(C)(P))
124 /* __STDC_VERSION__ is ISO C99, so also use __STDC__, which should work */
125 #if defined __STDC__ || defined __STDC_VERSION__ /*|| defined __cplusplus*/
126 # define STRING(X) #X
127 # define XSTRING(X) STRING(X)
128 # define CONCAT(S1,S2) _CONCAT(S1, S2)
129 # define _CONCAT(S1,S2) S1 ## S2
131 # define STRING(X) "X"
132 # define XSTRING STRING
133 # define CONCAT(S1,S2) S1/**/S2
136 #if defined __STDC_VERSION__ && __STDC_VERSION__ + 0 >= 199901L
137 /* Variable size arrays and structure fields */
138 # define VFIELD_SIZE(X)
139 # define VFIELD_SIZEOF(T,F) (0)
140 /* Inline functions */
142 # define INLINE inline
143 # define SINLINE static inline
145 # define VFIELD_SIZE(X) (X)
146 # define VFIELD_SIZEOF(T,F) SIZEOF_FIELD(T, F)
148 # define INLINE static __inline
149 # define SINLINE static __inline
152 # define SINLINE static
156 #if defined __predict_true && defined __predict_false
157 # define LIKELY(X) __predict_true(X)
158 # define UNLIKELY(X) __predict_false(X)
159 #elif __PREREQ(2, 96)
160 # define LIKELY(X) __builtin_expect(X, 1)
161 # define UNLIKELY(X) __builtin_expect(X, 0)
163 # define LIKELY(X) (X)
164 # define UNLIKELY(X) (X)
167 /* Compile-Time-Assert */
168 #define CTA(TEST) _CTA_1(TEST, __LINE__)
169 #define _CTA_1(TEST,L) _CTA_2(TEST, L)
170 #define _CTA_2(TEST,L) \
171 typedef char COMPILE_TIME_ASSERT_failed_at_line_ ## L[(TEST) ? 1 : -1]
174 #define ISPOW2(X) ((((X) - 1) & (X)) == 0)
176 #define MIN(A, B) ((A) < (B) ? (A) : (B))
178 #define MAX(A, B) ((A) < (B) ? (B) : (A))
180 #define ABS(A) ((A) < 0 ? -(A) : (A))
182 #define smin(a, b) ((a) < (b) ? (a) : (b)) /* TODO OBSOLETE */
183 #define smax(a, b) ((a) < (b) ? (b) : (a)) /* TODO OBSOLETE */
187 # define assert(X) UNUSED(0)
190 /* Translation (init in main.c) */
194 # define tr(c,d) catgets(catd, CATSET, c, d)
200 * Constants, some nail-specific macros
203 #if !defined NI_MAXHOST || NI_MAXHOST < 1025
205 # define NI_MAXHOST 1025
210 # define MAXPATHLEN PATH_MAX
212 # define MAXPATHLEN 1024
214 #elif defined PATH_MAX && MAXPATHLEN < PATH_MAX
216 # define MAXPATHLEN PATH_MAX
220 # define STDIN_FILENO 0
222 #ifndef STDOUT_FILENO
223 # define STDOUT_FILENO 1
225 #ifndef STDERR_FILENO
226 # define STDERR_FILENO 2
231 # define NSIG NSIG_MAX
233 # define NSIG ((sizeof(sigset_t) * 8) - 1)
238 #if BUFSIZ > 2560 /* TODO simply use BUFSIZ? */
239 # define LINESIZE BUFSIZ /* max readable line width */
241 # define LINESIZE 2560
243 #define BUFFER_SIZE (BUFSIZ >= (1u << 13) ? BUFSIZ : (1u << 14))
245 #define CBAD (-15555)
246 #define APPEND /* New mail goes to end of mailbox */
247 #define ESCAPE '~' /* Default escape for sending */
248 #define HIST_SIZE 242 /* tty.c: history list default size */
249 #define HSHSIZE 23 /* Hash prime (aliases, vars, macros) */
250 #define MAXARGC 1024 /* Maximum list of raw strings */
251 #define MAXEXP 25 /* Maximum expansion of aliases */
252 #define PROMPT_BUFFER_SIZE 80 /* getprompt() bufsize (> 3!) */
254 #define ACCOUNT_NULL "null" /* Name of "null" account */
255 #define MAILRC "~/.mailrc"
256 #define TMPDIR_FALLBACK "/tmp"
258 #define FROM_DATEBUF 64 /* Size of RFC 4155 From_ line date */
259 #define DATE_DAYSYEAR 365L
260 #define DATE_SECSMIN 60L
261 #define DATE_MINSHOUR 60L
262 #define DATE_HOURSDAY 24L
263 #define DATE_SECSDAY (DATE_SECSMIN * DATE_MINSHOUR * DATE_HOURSDAY)
265 /* Default *encoding* as enum conversion below */
266 #define MIME_DEFAULT_ENCODING CONV_TOQP
268 /* Maximum allowed line length in a mail before QP folding is necessary), and
269 * the real limit we go for */
270 #define MIME_LINELEN_MAX 1000
271 #define MIME_LINELEN_LIMIT (MIME_LINELEN_MAX - 50)
273 /* Locations of mime.types(5) */
274 #define MIME_TYPES_USR "~/.mime.types"
275 #define MIME_TYPES_SYS "/etc/mime.types"
277 /* Fallback MIME charsets, if *charset-7bit* and *charset-8bit* or not set */
278 #define CHARSET_7BIT "US-ASCII"
280 # define CHARSET_8BIT "UTF-8"
281 # define CHARSET_8BIT_VAR "charset-8bit"
283 # define CHARSET_8BIT "ISO-8859-1"
284 # define CHARSET_8BIT_VAR "ttycharset"
287 /* Is *W* a quoting (ASCII only) character? */
289 ((W) == L'>' || (W) == L'|' || (W) == L'}' || (W) == L':')
291 /* Maximum number of quote characters (not bytes!) that'll be used on
292 * follow lines when compressing leading quote characters */
295 /* How much spaces should a <tab> count when *quote-fold*ing? (power-of-two!) */
296 #define QUOTE_TAB_SPACES 8
298 /* Maximum size of a message that is passed through to the spam system */
299 #define SPAM_MAXSIZE 420000
301 /* String dope: dynamic buffer size, and size of the single builtin one that's
303 #define SBUFFER_SIZE 0x18000u
304 #define SBUFFER_BUILTIN 0x2000u
306 /* These come from the configuration (named Xxy to not clash with sh(1)..) */
308 # define XSHELL "/bin/sh"
312 # define XLISTER "ls"
314 #define LISTER XLISTER
316 # define XPAGER "more"
324 /* TODO convert all integer types to the new [su]i(8|16|32|64)_t */
325 typedef unsigned long ul_it
;
326 typedef unsigned int ui_it
;
327 typedef unsigned short us_it
;
328 typedef unsigned char uc_it
;
330 typedef signed long sl_it
;
331 typedef signed int si_it
;
332 typedef signed short ss_it
;
333 typedef signed char sc_it
;
336 # define UI8_MAX UINT8_MAX
337 # define SI8_MIN INT8_MIN
338 # define SI8_MAX INT8_MAX
339 typedef uint8_t ui8_t
;
340 typedef int8_t si8_t
;
341 #elif UCHAR_MAX != 255
342 # error UCHAR_MAX must be 255
344 # define UI8_MAX UCHAR_MAX
345 # define SI8_MIN CHAR_MIN
346 # define SI8_MAX CHAR_MAX
347 typedef unsigned char ui8_t
;
348 typedef signed char si8_t
;
352 # define UI16_MAX UINT16_MAX
353 # define SI16_MIN INT16_MIN
354 # define SI16_MAX INT16_MAX
355 typedef uint16_t ui16_t
;
356 typedef int16_t si16_t
;
357 #elif USHRT_MAX != 0xFFFFu
358 # error USHRT_MAX must be 0xFFFF
360 # define UI16_MAX USHRT_MAX
361 # define SI16_MIN SHRT_MIN
362 # define SI16_MAX SHRT_MAX
363 typedef unsigned short ui16_t
;
364 typedef signed short si16_t
;
368 # define UI32_MAX UINT32_MAX
369 # define SI32_MIN INT32_MIN
370 # define SI32_MAX INT32_MAX
371 typedef uint32_t ui32_t
;
372 typedef int32_t si32_t
;
373 #elif ULONG_MAX == 0xFFFFFFFFu
374 # define UI32_MAX ULONG_MAX
375 # define SI32_MIN LONG_MIN
376 # define SI32_MAX LONG_MAX
377 typedef unsigned long int ui32_t
;
378 typedef signed long int si32_t
;
379 #elif UINT_MAX != 0xFFFFFFFFu
380 # error UINT_MAX must be 0xFFFFFFFF
382 # define UI32_MAX UINT_MAX
383 # define SI32_MIN INT_MIN
384 # define SI32_MAX INT_MAX
385 typedef unsigned int ui32_t
;
386 typedef signed int si32_t
;
390 # define UI64_MAX UINT64_MAX
391 # define SI64_MIN INT64_MIN
392 # define SI64_MAX INT64_MAX
393 typedef uint64_t ui64_t
;
394 #elif ULONG_MAX <= 0xFFFFFFFFu
395 # if !defined ULLONG_MAX || ULLONG_MAX != 0xFFFFFFFFFFFFFFFFu
396 # error We need a 64 bit integer
398 # define UI64_MAX ULLONG_MAX
399 # define SI64_MIN LLONG_MIN
400 # define SI64_MAX LLONG_MAX
401 __EXTEN
typedef unsigned long long ui64_t
;
402 __EXTEN
typedef signed long long si64_t
;
405 # define UI64_MAX ULONG_MAX
406 # define SI64_MIN LONG_MIN
407 # define SI64_MAX LONG_MAX
408 typedef unsigned long ui64_t
;
409 typedef signed long si64_t
;
412 /* (So that we can use UICMP() for size_t comparison, too) */
413 typedef size_t uiz_t
;
414 /*typedef ssize_t siz_t;*/
418 # define uintptr_t size_t
419 # define UINTPTR_MAX SIZE_MAX
421 # define uintptr_t unsigned long
422 # define UINTPTR_MAX ULONG_MAX
426 /* XXX Note we don't really deal with that the right way in that we pass size_t
427 * XXX arguments without casting; should do, because above we assert UINT_MAX
428 * XXX is indeed ui32_t */
429 #if defined __STDC_VERSION__ && __STDC_VERSION__ + 0 >= 199901L
431 #elif defined SIZE_MAX && SIZE_MAX == 0xFFFFFFFFu && ULONG_MAX != UINT_MAX
438 typedef enum {FAL0
, TRU1
} bool_t
;
440 typedef void ( *sighandler_type
)(int);
444 OPT_DEBUG
= 1u<< 0, /* -d / *debug* */
445 OPT_VERBOSE
= 1u<< 1, /* -v / *verbose* */
446 OPT_EXISTONLY
= 1u<< 2, /* -e */
447 OPT_HEADERSONLY
= 1u<< 3, /* -H */
448 OPT_NOSRC
= 1u<< 4, /* -n */
449 OPT_E_FLAG
= 1u<< 5, /* -E / *skipemptybody* */
450 OPT_F_FLAG
= 1u<< 6, /* -F */
451 OPT_N_FLAG
= 1u<< 7, /* -N / *header* */
452 OPT_R_FLAG
= 1u<< 8, /* -R */
453 OPT_r_FLAG
= 1u<< 9, /* -r (plus option_r_arg) */
454 OPT_t_FLAG
= 1u<<10, /* -t */
455 OPT_u_FLAG
= 1u<<11, /* -u given, or USER != getpwnam(3) */
456 OPT_TILDE_FLAG
= 1u<<12, /* -~ */
457 OPT_BATCH_FLAG
= 1u<<13, /* -# */
459 OPT_SENDMODE
= 1u<<14, /* Usage case forces send mode */
460 OPT_INTERACTIVE
= 1u<<15, /* isatty(0) */
461 OPT_TTYIN
= OPT_INTERACTIVE
,
464 #define IS_TTY_SESSION() \
465 ((options & (OPT_TTYIN | OPT_TTYOUT)) == (OPT_TTYIN | OPT_TTYOUT))
468 EXIT_OK
= EXIT_SUCCESS
,
469 EXIT_ERR
= EXIT_FAILURE
,
470 EXIT_COLL_ABORT
= 1<<1, /* Message collection was aborted */
471 EXIT_SEND_ERROR
= 1<<2 /* Unspecified send error occurred */
475 FEXP_FULL
, /* Full expansion */
476 FEXP_LOCAL
= 1<<0, /* Result must be local file/maildir */
477 FEXP_SHELL
= 1<<1, /* No folder %,#,&,+ stuff, yet sh(1) */
478 FEXP_NSHORTCUT
= 1<<2, /* Don't expand shortcuts */
479 FEXP_SILENT
= 1<<3, /* Don't print but only return errors */
480 FEXP_MULTIOK
= 1<<4 /* Expansion to many entries is ok */
485 LNED_LF_ESC
= 1<<0, /* LF can be backslash escaped */
486 LNED_HIST_ADD
= 1<<1 /* Add completed line to history */
489 /* <0 means "stop" unless *prompt* extensions are enabled. */
491 PROMPT_STOP
= -1, /* \c */
492 /* *prompt* extensions: \$, \@ etc. */
503 MIME_NONE
, /* message is not in MIME format */
504 MIME_BIN
, /* message is in binary encoding */
505 MIME_8B
, /* message is in 8bit encoding */
506 MIME_7B
, /* message is in 7bit encoding */
507 MIME_QP
, /* message is quoted-printable */
508 MIME_B64
/* message is in base64 encoding */
512 CONV_NONE
, /* no conversion */
513 CONV_7BIT
, /* no conversion, is 7bit */
514 CONV_FROMQP
, /* convert from quoted-printable */
515 CONV_TOQP
, /* convert to quoted-printable */
516 CONV_8BIT
, /* convert to 8bit (iconv) */
517 CONV_FROMB64
, /* convert from base64 */
518 CONV_FROMB64_T
, /* convert from base64/text */
519 CONV_TOB64
, /* convert to base64 */
520 CONV_FROMHDR
, /* convert from RFC1522 format */
521 CONV_TOHDR
, /* convert to RFC1522 format */
522 CONV_TOHDR_A
/* convert addresses for header */
526 SEND_MBOX
, /* no conversion to perform */
527 SEND_RFC822
, /* no conversion, no From_ line */
528 SEND_TODISP
, /* convert to displayable form */
529 SEND_TODISP_ALL
, /* same, include all MIME parts */
530 SEND_SHOW
, /* convert to 'show' command form */
531 SEND_TOSRCH
, /* convert for IMAP SEARCH */
532 SEND_TOFLTR
, /* convert for spam mail filtering */
533 SEND_TOFILE
, /* convert for saving body to a file */
534 SEND_TOPIPE
, /* convert for pipe-content/subc. */
535 SEND_QUOTE
, /* convert for quoting */
536 SEND_QUOTE_ALL
, /* same, include all MIME parts */
537 SEND_DECRYPT
/* decrypt */
541 MIME_UNKNOWN
, /* unknown content */
542 MIME_SUBHDR
, /* inside a multipart subheader */
543 MIME_822
, /* message/rfc822 content */
544 MIME_MESSAGE
, /* other message/ content */
545 MIME_TEXT_PLAIN
, /* text/plain content */
546 MIME_TEXT_HTML
, /* text/html content */
547 MIME_TEXT
, /* other text/ content */
548 MIME_ALTERNATIVE
, /* multipart/alternative content */
549 MIME_DIGEST
, /* multipart/digest content */
550 MIME_MULTI
, /* other multipart/ content */
551 MIME_PKCS7
, /* PKCS7 content */
552 MIME_DISCARD
/* content is discarded */
556 TD_NONE
= 0, /* no display conversion */
557 TD_ISPR
= 1<<0, /* use isprint() checks */
558 TD_ICONV
= 1<<1, /* use iconv() */
559 TD_DELCTRL
= 1<<2, /* delete control characters */
562 * NOTE: _TD_EOF and _TD_BUFCOPY may be ORd with enum conversion and
563 * enum sendaction, and may thus NOT clash with their bit range!
565 _TD_EOF
= 1<<14,/* EOF seen, last round! */
566 _TD_BUFCOPY
= 1<<15 /* Buffer may be constant, copy it */
570 PROTO_FILE
, /* refers to a local file */
571 PROTO_POP3
, /* is a pop3 server string */
572 PROTO_IMAP
, /* is an imap server string */
573 PROTO_MAILDIR
, /* refers to a maildir folder */
574 PROTO_UNKNOWN
/* unknown protocol */
578 enum ssl_vrfy_level
{
587 char *s
; /* the string's content */
588 size_t l
; /* the stings's length */
591 struct time_current
{
599 FILE *qf_os
; /* Output stream */
601 ui_it qf_pfix_len
; /* Length of prefix: 0: bypass */
602 ui_it qf_qfold_min
; /* Simple way: wrote prefix? */
603 #ifdef HAVE_QUOTE_FOLD
604 ui_it qf_qfold_max
; /* Otherwise: line lengths */
605 uc_it qf_state
; /* *quote-fold* state machine */
606 bool_t qf_brk_isws
; /* Breakpoint is at WS */
608 ui_it qf_wscnt
; /* Whitespace count */
609 ui_it qf_brkl
; /* Breakpoint */
610 ui_it qf_brkw
; /* Visual width, breakpoint */
611 ui_it qf_datw
; /* Current visual output line width */
612 struct str qf_dat
; /* Current visual output line */
613 struct str qf_currq
; /* Current quote, compressed */
614 mbstate_t qf_mbps
[2];
618 struct termios_state
{
619 struct termios ts_tios
;
622 bool_t ts_needs_reset
;
625 #define termios_state_reset() \
627 if (termios_state.ts_needs_reset) {\
628 tcsetattr(0, TCSADRAIN, &termios_state.ts_tios);\
629 termios_state.ts_needs_reset = FAL0;\
633 struct sock
{ /* data associated with a socket */
634 int s_fd
; /* file descriptor */
636 int s_use_ssl
; /* SSL is used */
638 void *s_ssl
; /* SSL object */
639 void *s_ctx
; /* SSL context object */
642 char *s_wbuf
; /* for buffered writes */
643 int s_wbufsize
; /* allocated size of s_buf */
644 int s_wbufpos
; /* position of first empty data byte */
645 char *s_rbufptr
; /* read pointer to s_rbuf */
646 int s_rsz
; /* size of last read in s_rbuf */
647 char const *s_desc
; /* description of error messages */
648 void (*s_onclose
)(void); /* execute on close */
649 char s_rbuf
[LINESIZE
+ 1]; /* for buffered reads */
654 MB_NONE
= 000, /* no reply expected */
655 MB_COMD
= 001, /* command reply expected */
656 MB_MULT
= 002, /* multiline reply expected */
657 MB_PREAUTH
= 004, /* not in authenticated state */
658 MB_BYE
= 010 /* may accept a BYE state */
660 FILE *mb_itf
; /* temp file with messages, read open */
661 FILE *mb_otf
; /* same, write open */
662 char *mb_sorted
; /* sort method */
664 MB_VOID
, /* no type (e. g. connection failed) */
665 MB_FILE
, /* local file */
666 MB_POP3
, /* POP3 mailbox */
667 MB_IMAP
, /* IMAP mailbox */
668 MB_MAILDIR
, /* maildir folder */
669 MB_CACHE
/* cached mailbox */
670 } mb_type
; /* type of mailbox */
672 MB_DELE
= 01, /* may delete messages in mailbox */
673 MB_EDIT
= 02 /* may edit messages in mailbox */
675 int mb_compressed
; /* is a compressed mbox file */
676 int mb_threaded
; /* mailbox has been threaded */
680 MB_UIDPLUS
= 001 /* supports IMAP UIDPLUS */
682 unsigned long mb_uidvalidity
; /* IMAP unique identifier validity */
683 char *mb_imap_account
; /* name of current IMAP account */
684 char *mb_imap_mailbox
; /* name of current IMAP mailbox */
685 char *mb_cache_directory
; /* name of cache directory */
687 struct sock mb_sock
; /* socket structure */
691 NEED_UNSPEC
, /* unspecified need, don't fetch */
692 NEED_HEADER
, /* need the header of a message */
693 NEED_BODY
/* need header and body of a message */
697 HAVE_NOTHING
= 0, /* nothing downloaded yet */
698 HAVE_HEADER
= 01, /* header is downloaded */
699 HAVE_BODY
= 02 /* entire message is downloaded */
703 * flag bits. Attention: Flags that are used in cache.c may not change.
706 MUSED
= (1<<0), /* entry is used, but this bit isn't */
707 MDELETED
= (1<<1), /* entry has been deleted */
708 MSAVED
= (1<<2), /* entry has been saved */
709 MTOUCH
= (1<<3), /* entry has been noticed */
710 MPRESERVE
= (1<<4), /* keep entry in sys mailbox */
711 MMARK
= (1<<5), /* message is marked! */
712 MODIFY
= (1<<6), /* message has been modified */
713 MNEW
= (1<<7), /* message has never been seen */
714 MREAD
= (1<<8), /* message has been read sometime. */
715 MSTATUS
= (1<<9), /* message status has changed */
716 MBOX
= (1<<10), /* Send this to mbox, regardless */
717 MNOFROM
= (1<<11), /* no From line */
718 MHIDDEN
= (1<<12), /* message is hidden to user */
719 MFULLYCACHED
= (1<<13), /* message is completely cached */
720 MBOXED
= (1<<14), /* message has been sent to mbox */
721 MUNLINKED
= (1<<15), /* message was unlinked from cache */
722 MNEWEST
= (1<<16), /* message is very new (newmail) */
723 MFLAG
= (1<<17), /* message has been flagged recently */
724 MUNFLAG
= (1<<18), /* message has been unflagged */
725 MFLAGGED
= (1<<19), /* message is `flagged' */
726 MANSWER
= (1<<20), /* message has been answered recently */
727 MUNANSWER
= (1<<21), /* message has been unanswered */
728 MANSWERED
= (1<<22), /* message is `answered' */
729 MDRAFT
= (1<<23), /* message has been drafted recently */
730 MUNDRAFT
= (1<<24), /* message has been undrafted */
731 MDRAFTED
= (1<<25), /* message is marked as `draft' */
732 MOLDMARK
= (1<<26), /* messages was marked previously */
733 MSPAM
= (1<<27) /* message is classified as spam */
737 enum mflag m_flag
; /* flags */
738 enum havespec m_have
; /* downloaded parts of the part */
740 ui_it m_spamscore
; /* Spam score as int, 24:8 bits */
742 int m_block
; /* block number of this part */
743 size_t m_offset
; /* offset in block of part */
744 size_t m_size
; /* Bytes in the part */
745 size_t m_xsize
; /* Bytes in the full part */
746 long m_lines
; /* Lines in the message */
747 long m_xlines
; /* Lines in the full message */
748 time_t m_time
; /* time the message was sent */
749 char const *m_from
; /* message sender */
750 struct mimepart
*m_nextpart
; /* next part at same level */
751 struct mimepart
*m_multipart
; /* parts of multipart */
752 struct mimepart
*m_parent
; /* enclosing multipart part */
753 char *m_ct_type
; /* content-type */
754 char *m_ct_type_plain
; /* content-type without specs */
755 enum mimecontent m_mimecontent
; /* same in enum */
756 char const *m_charset
; /* charset */
757 char *m_ct_transfer_enc
; /* content-transfer-encoding */
758 enum mimeenc m_mimeenc
; /* same in enum */
759 char *m_partstring
; /* part level string */
760 char *m_filename
; /* attachment filename */
764 enum mflag m_flag
; /* flags */
765 enum havespec m_have
; /* downloaded parts of the message */
767 ui_it m_spamscore
; /* Spam score as int, 24:8 bits */
769 int m_block
; /* block number of this message */
770 size_t m_offset
; /* offset in block of message */
771 size_t m_size
; /* Bytes in the message */
772 size_t m_xsize
; /* Bytes in the full message */
773 long m_lines
; /* Lines in the message */
774 long m_xlines
; /* Lines in the full message */
775 time_t m_time
; /* time the message was sent */
776 time_t m_date
; /* time in the 'Date' field */
777 unsigned m_idhash
; /* hash on Message-ID for threads */
778 struct message
*m_child
; /* first child of this message */
779 struct message
*m_younger
; /* younger brother of this message */
780 struct message
*m_elder
; /* elder brother of this message */
781 struct message
*m_parent
; /* parent of this message */
782 unsigned m_level
; /* thread level of message */
783 long m_threadpos
; /* position in threaded display */
785 unsigned long m_uid
; /* IMAP unique identifier */
787 char *m_maildir_file
; /* original maildir file of msg */
788 unsigned m_maildir_hash
; /* hash of file name in maildir sub */
789 int m_collapsed
; /* collapsed thread information */
793 * Given a file address, determine the block number it represents.
795 #define mailx_blockof(off) ((int) ((off) / 4096))
796 #define mailx_offsetof(off) ((int) ((off) % 4096))
797 #define mailx_positionof(block, offset) ((off_t)(block) * 4096 + (offset))
803 MSGLIST
= 0, /* Message list type */
804 STRLIST
= 1, /* A pure string */
805 RAWLIST
= 2, /* Shell string list */
806 NOLIST
= 3, /* Just plain 0 */
807 NDMLIST
= 4, /* Message list, no defaults */
808 ECHOLIST
= 5, /* Like raw list, but keep quote chars */
809 P
= 040, /* Autoprint dot after command */
810 I
= 0100, /* Interactive command bit */
811 M
= 0200, /* Legal from send mode bit */
812 W
= 0400, /* Illegal when read only bit */
813 F
= 01000, /* Is a conditional command */
814 T
= 02000, /* Is a transparent command */
815 R
= 04000, /* Cannot be called from collect */
816 A
= 010000 /* Needs an active mailbox */
820 * Oft-used mask values
822 #define MMNORM (MDELETED|MSAVED|MHIDDEN) /* Look at save *and* delete bits */
823 #define MMNDEL (MDELETED|MHIDDEN) /* Look only at deleted bit */
826 GTO
= 1, /* Grab To: line */
827 GSUBJECT
= 2, /* Likewise, Subject: line */
828 GCC
= 4, /* And the Cc: line */
829 GBCC
= 8, /* And also the Bcc: line */
831 GNL
= 16, /* Print blank line after */
832 GDEL
= 32, /* Entity removed from list */
833 GCOMMA
= 64, /* detract puts in commas */
834 GUA
= 128, /* User-Agent field */
835 GMIME
= 256, /* MIME 1.0 fields */
836 GMSGID
= 512, /* a Message-ID */
837 /* 1024 */ /* unused */
838 GIDENT
= 2048, /* From:, Reply-To: and Organization: field */
839 GREF
= 4096, /* References: field */
840 GDATE
= 8192, /* Date: field */
841 GFULL
= 16384, /* include full names */
842 GSKIN
= 32768, /* skin names */
843 GEXTRA
= 65536, /* extra fields */
844 GFILES
= 131072 /* include filename addresses */
847 #define GMASK (GTO|GSUBJECT|GCC|GBCC) /* Mask of places from whence */
849 #define visible(mp) (((mp)->m_flag & (MDELETED|MHIDDEN)) == 0)
852 * Structure used to pass about the current state of a message (header).
855 struct name
*h_to
; /* Dynamic "To:" string */
856 char *h_subject
; /* Subject string */
857 struct name
*h_cc
; /* Carbon copies string */
858 struct name
*h_bcc
; /* Blind carbon copies */
859 struct name
*h_ref
; /* References */
860 struct attachment
*h_attach
; /* MIME attachments */
861 char *h_charset
; /* preferred charset */
862 struct name
*h_from
; /* overridden "From:" field */
863 struct name
*h_replyto
; /* overridden "Reply-To:" field */
864 struct name
*h_sender
; /* overridden "Sender:" field */
865 char *h_organization
; /* overridden "Organization:" field */
869 * Handling of namelist nodes used in processing the recipients of mail and
870 * aliases, inspection of mail-addresses and all that kind of stuff.
874 NAME_NAME_SALLOC
= 1<< 0, /* .n_name is doped */
875 NAME_FULLNAME_SALLOC
= 1<< 1, /* .n_fullname is doped */
876 NAME_SKINNED
= 1<< 2, /* Is actually skin()ned */
877 NAME_IDNA
= 1<< 3, /* IDNA was applied */
878 NAME_ADDRSPEC_CHECKED
= 1<< 4, /* Address has been .. and */
879 NAME_ADDRSPEC_ISFILE
= 1<< 5, /* ..is a file path */
880 NAME_ADDRSPEC_ISPIPE
= 1<< 6, /* ..is a command for piping */
881 NAME_ADDRSPEC_ISFILEORPIPE
= NAME_ADDRSPEC_ISFILE
|
882 NAME_ADDRSPEC_ISPIPE
,
883 NAME_ADDRSPEC_ERR_EMPTY
= 1<< 7, /* An empty string (or NULL) */
884 NAME_ADDRSPEC_ERR_ATSEQ
= 1<< 8, /* Weird @ sequence */
885 NAME_ADDRSPEC_ERR_CHAR
= 1<< 9, /* Invalid character */
886 NAME_ADDRSPEC_ERR_IDNA
= 1<<10, /* IDNA convertion failed */
887 NAME_ADDRSPEC_INVALID
= NAME_ADDRSPEC_ERR_EMPTY
|
888 NAME_ADDRSPEC_ERR_ATSEQ
|
889 NAME_ADDRSPEC_ERR_CHAR
|
890 NAME_ADDRSPEC_ERR_IDNA
,
893 _NAME_MAXWC
= 0xFFFFF,
894 _NAME_MASKWC
= _NAME_MAXWC
<< _NAME_SHIFTWC
897 /* In the !_ERR_EMPTY case, the failing character can be queried */
898 #define NAME_ADDRSPEC_ERR_GETWC(F) \
899 ((((unsigned int)(F) & _NAME_MASKWC) >> _NAME_SHIFTWC) & _NAME_MAXWC)
900 #define NAME_ADDRSPEC_ERR_SET(F, E, WC) \
901 do (F) = ((F) & ~(NAME_ADDRSPEC_INVALID | _NAME_MASKWC)) | \
902 (E) | (((unsigned int)(WC) & _NAME_MAXWC) << _NAME_SHIFTWC); \
906 struct name
*n_flink
; /* Forward link in list. */
907 struct name
*n_blink
; /* Backward list link */
908 enum gfield n_type
; /* From which list it came */
909 enum nameflags n_flags
; /* enum nameflags */
910 char *n_name
; /* This fella's name */
911 char *n_fullname
; /* Sometimes, name including comment */
915 char const *ag_input
; /* Input string as given */
916 size_t ag_ilen
; /* strlen() of input */
917 size_t ag_iaddr_start
; /* Start of *addr-spec* in .ag_input */
918 size_t ag_iaddr_aend
; /* ..and one past its end */
919 char *ag_skinned
; /* Output (alloced if !=.ag_input) */
920 size_t ag_slen
; /* strlen() of .ag_skinned */
921 size_t ag_sdom_start
; /* Start of domain in .ag_skinned, */
922 enum nameflags ag_n_flags
; /* enum nameflags of .ag_skinned */
930 AC_DEFAULT
, /* _get_lc() -> _iter_*() */
931 AC_FIX_OUTCS
, /* _get_lc() -> "charset=" .a_charset */
932 AC_FIX_INCS
, /* "charset=".a_input_charset (nocnv) */
933 AC_TMPFILE
/* attachment.a_tmpf is converted */
937 struct attachment
*a_flink
; /* Forward link in list. */
938 struct attachment
*a_blink
; /* Backward list link */
939 char const *a_name
; /* file name */
940 char const *a_content_type
; /* content type */
941 char const *a_content_disposition
; /* content disposition */
942 char const *a_content_id
; /* content id */
943 char const *a_content_description
; /* content description */
944 char const *a_input_charset
; /* Interpretation depends on .a_conv */
945 char const *a_charset
; /* ... */
946 FILE *a_tmpf
; /* If AC_TMPFILE */
947 enum attach_conv a_conv
; /* User chosen conversion */
948 int a_msgno
; /* message number */
952 struct group
*ge_link
; /* Next person in this group */
953 char *ge_name
; /* This person's user name */
957 struct grouphead
*g_link
; /* Next grouphead in list */
958 char *g_name
; /* Name of this group */
959 struct group
*g_list
; /* Users in group. */
963 * Structure of the hash table of ignored header fields
966 int i_count
; /* Number of entries */
968 struct ignore
*i_link
; /* Next ignored field in bucket */
969 char *i_field
; /* This ignored field */
974 * Token values returned by the scanner used for argument lists.
975 * Also, sizes of scanner-related things.
978 TEOL
= 0, /* End of the command line */
979 TNUMBER
= 1, /* A message number */
980 TDASH
= 2, /* A simple dash */
981 TSTRING
= 3, /* A string (possibly containing -) */
982 TDOT
= 4, /* A "." */
983 TUP
= 5, /* An "^" */
984 TDOLLAR
= 6, /* A "$" */
985 TSTAR
= 7, /* A "*" */
986 TOPEN
= 8, /* An '(' */
987 TCLOSE
= 9, /* A ')' */
988 TPLUS
= 10, /* A '+' */
989 TERROR
= 11, /* A lexical error */
990 TCOMMA
= 12, /* A ',' */
991 TSEMI
= 13, /* A ';' */
992 TBACK
= 14 /* A '`' */
995 #define REGDEP 2 /* Maximum regret depth. */
998 * Constants for conditional commands. These describe whether
999 * we should be executing stuff or not.
1002 CANY
= 0, /* Execute in send or receive mode */
1003 CRCV
= 1, /* Execute in receive mode only */
1004 CSEND
= 2, /* Execute in send mode only */
1005 CTERM
= 3, /* Execute only if stdin is a tty */
1006 CNONTERM
= 4 /* Execute only if stdin not tty */
1010 * For the 'shortcut' and 'unshortcut' functionality.
1013 struct shortcut
*sh_next
; /* next shortcut in list */
1014 char *sh_short
; /* shortcut string */
1015 char *sh_long
; /* expanded form */
1019 * Kludges to handle the change from setexit / reset to setjmp / longjmp
1021 #define setexit() (void)sigsetjmp(srbuf, 1)
1022 #define reset(x) siglongjmp(srbuf, x)
1025 * Content-Transfer-Encodings as defined in RFC 2045:
1026 * - Quoted-Printable, section 6.7
1027 * - Base64, section 6.8
1030 #define QP_LINESIZE (4 * 19 +1) /* Max. compliant QP linesize (+1) */
1032 #define B64_LINESIZE (4 * 19 +1) /* Max. compl. Base64 linesize (+1) */
1033 #define B64_ENCODE_INPUT_PER_LINE 57 /* Max. input for Base64 encode/line */
1035 /* xxx QP came later, maybe rewrite all to use mimecte_flags directly? */
1036 enum __mimecte_flags
{
1038 MIMECTE_SALLOC
= 1<<0, /* Use salloc(), not srealloc().. */
1039 /* ..result .s,.l point to user buffer of *_LINESIZE+ bytes instead */
1041 MIMECTE_CRLF
= 1<<2, /* (encode) Append "\r\n" to lines */
1042 MIMECTE_LF
= 1<<3, /* (encode) Append "\n" to lines */
1043 /* (encode) If one of _CRLF/_LF is set, honour *_LINESIZE and
1044 * inject the desired line-ending whenever a linewrap is desired */
1045 MIMECTE_MULTILINE
= 1<<4,
1046 /* (encode) Quote with header rules, do not generate soft NL breaks? */
1047 MIMECTE_ISHEAD
= 1<<5
1051 QP_NONE
= MIMECTE_NONE
,
1052 QP_SALLOC
= MIMECTE_SALLOC
,
1053 QP_BUF
= MIMECTE_BUF
,
1054 QP_ISHEAD
= MIMECTE_ISHEAD
1058 B64_NONE
= MIMECTE_NONE
,
1059 B64_SALLOC
= MIMECTE_SALLOC
,
1060 B64_BUF
= MIMECTE_BUF
,
1061 B64_CRLF
= MIMECTE_CRLF
,
1062 B64_LF
= MIMECTE_LF
,
1063 B64_MULTILINE
= MIMECTE_MULTILINE
1067 * Locale-independent character classes.
1082 extern uc_it
const class_char
[];
1084 #define __ischarof(C, FLAGS) \
1085 (asciichar(C) && (class_char[(uc_it)(C)] & (FLAGS)) != 0)
1087 #define asciichar(c) ((uc_it)(c) <= 0177)
1088 #define alnumchar(c) __ischarof(c, C_DIGIT|C_OCTAL|C_UPPER|C_LOWER)
1089 #define alphachar(c) __ischarof(c, C_UPPER|C_LOWER)
1090 #define blankchar(c) __ischarof(c, C_BLANK)
1091 #define blankspacechar(c) __ischarof(c, C_BLANK|C_SPACE)
1092 #define cntrlchar(c) __ischarof(c, C_CNTRL)
1093 #define digitchar(c) __ischarof(c, C_DIGIT|C_OCTAL)
1094 #define lowerchar(c) __ischarof(c, C_LOWER)
1095 #define punctchar(c) __ischarof(c, C_PUNCT)
1096 #define spacechar(c) __ischarof(c, C_BLANK|C_SPACE|C_WHITE)
1097 #define upperchar(c) __ischarof(c, C_UPPER)
1098 #define whitechar(c) __ischarof(c, C_BLANK|C_WHITE)
1099 #define octalchar(c) __ischarof(c, C_OCTAL)
1101 #define upperconv(c) (lowerchar(c) ? (char)((uc_it)(c) - 'a' + 'A') : (c))
1102 #define lowerconv(c) (upperchar(c) ? (char)((uc_it)(c) - 'A' + 'a') : (c))
1104 #define fieldnamechar(c) \
1105 (asciichar(c) && (c) > 040 && (c) != 0177 && (c) != ':')
1108 * Try to use alloca() for some function-local buffers and data,
1109 * fall back to smalloc()/free() if not available.
1112 # define ac_alloc(n) HAVE_ALLOCA(n)
1113 # define ac_free(n) do {} while (0)
1115 # define ac_alloc(n) smalloc(n)
1116 # define ac_free(n) free(n)
1120 * Single-threaded, use unlocked I/O.
1122 #ifdef HAVE_PUTC_UNLOCKED
1124 # define getc(c) getc_unlocked(c)
1126 # define putc(c, f) putc_unlocked(c, f)
1128 # define putchar(c) putc_unlocked((c), stdout)
1132 * Truncate a file to the last character written. This is
1133 * useful just before closing an old file that was opened
1136 #define ftrunc(stream) do {\
1139 off = ftell(stream);\
1141 ftruncate(fileno(stream), off);\
1145 * fflush() and rewind()
1147 #define fflush_rewind(stream) do { \
1153 * For saving the current directory and later returning.
1159 char cw_wd
[MAXPATHLEN
];
1164 * Global variable declarations
1166 * These become instantiated in main.c.
1171 # ifndef HAVE_AMALGAMATION
1180 VL gid_t effectivegid
; /* Saved from when we started up */
1181 VL gid_t realgid
; /* Saved from when we started up */
1183 VL
int mb_cur_max
; /* Value of MB_CUR_MAX */
1184 VL
int realscreenheight
; /* The real screen height */
1185 VL
int scrnwidth
; /* Screen width, or best guess */
1186 VL
int scrnheight
; /* Screen height/guess (4 header) */
1187 VL
int utf8
; /* Locale uses UTF-8 encoding */
1188 VL
int enc_has_state
; /* Encoding has shift states */
1190 VL
char **altnames
; /* List of alternate names of user */
1191 VL
char const *homedir
; /* Path name of home directory */
1192 VL
char const *myname
; /* My login name */
1193 VL
char const *progname
; /* Our name */
1194 VL
char const *tempdir
; /* The temporary directory */
1196 VL
int exit_status
; /* Exit status */
1197 VL
int options
; /* Bits of enum user_options */
1198 VL
char *option_r_arg
; /* Argument to -r option */
1199 VL
char const **smopts
; /* sendmail(1) opts from commline */
1200 VL
size_t smopts_count
; /* Entries in smopts */
1202 /* TODO Join as many of these state machine bits into a single carrier! */
1203 VL
int inhook
; /* Currently executing a hook */
1204 VL bool_t exec_last_comm_error
; /* Last execute() command failed */
1205 VL bool_t edit
; /* Indicates editing a file */
1206 VL bool_t did_print_dot
; /* Current message has been printed */
1207 VL bool_t msglist_is_single
; /* Last NDMLIST/MSGLIST chose 1 msg */
1208 VL bool_t loading
; /* Loading user definitions */
1209 VL bool_t sourcing
; /* Currently reading variant file */
1210 VL bool_t sawcom
; /* Set after first command */
1211 VL bool_t starting
; /* Still in startup code */
1212 VL bool_t unset_allow_undefined
; /* Allow to unset undefined vars */
1213 VL
int noreset
; /* String resets suspended */
1215 /* XXX stylish sorting */
1216 VL
int msgCount
; /* Count of messages read in */
1217 VL
enum condition cond
; /* State of conditional exc. */
1218 VL
struct mailbox mb
; /* Current mailbox */
1219 VL
int image
; /* File descriptor for msg image */
1220 VL
char mailname
[MAXPATHLEN
]; /* Name of current file */
1221 VL
char displayname
[80 - 40]; /* Prettyfied for display */
1222 VL
char prevfile
[MAXPATHLEN
]; /* Name of previous file */
1223 VL
char const *account_name
; /* Current account name or NULL */
1224 VL off_t mailsize
; /* Size of system mailbox */
1225 VL
struct message
*dot
; /* Pointer to current message */
1226 VL
struct message
*prevdot
; /* Previous current message */
1227 VL
struct message
*message
; /* The actual message structure */
1228 VL
struct message
*threadroot
; /* first threaded message */
1229 VL
int imap_created_mailbox
; /* hack to get feedback from imap */
1230 VL
int msgspace
; /* Number of allocated struct m */
1232 VL
struct grouphead
*groups
[HSHSIZE
]; /* Pointer to active groups */
1233 VL
struct ignoretab ignore
[2]; /* ignored and retained fields
1234 * 0 is ignore, 1 is retain */
1235 VL
struct ignoretab saveignore
[2]; /* ignored and retained fields
1236 * on save to folder */
1237 VL
struct ignoretab allignore
[2]; /* special, ignore all headers */
1238 VL
struct ignoretab fwdignore
[2]; /* fields to ignore for forwarding */
1239 VL
struct shortcut
*shortcuts
; /* list of shortcuts */
1241 VL
struct time_current time_current
; /* time(3); send: mail1() XXXcarrier*/
1242 VL
struct termios_state termios_state
; /* getpassword(); see commands().. */
1245 VL
enum ssl_vrfy_level ssl_vrfy_level
; /* SSL verification level */
1256 VL sighandler_type dflpipe
;
1257 VL sigjmp_buf srbuf
;
1259 VL sighandler_type handlerstacktop
;
1260 #define handlerpush(f) (savedtop = handlerstacktop, handlerstacktop = (f))
1261 #define handlerpop() (handlerstacktop = savedtop)
1263 /* The remaining variables need initialization */
1265 #ifndef HAVE_AMALGAMATION
1266 VL
char const month_names
[12 + 1][4];
1267 VL
char const weekday_names
[7 + 1][4];
1269 VL
char const uagent
[]; /* User agent */
1270 VL
char const version
[]; /* The version string */
1271 VL
char const features
[]; /* The "feature string" */
1275 * Finally, let's include the function prototypes XXX embed
1280 /* vim:set fenc=utf-8:s-it-mode (TODO only partial true) */