Readd auto-detection of compressed boxes; fix maildir code
[s-mailx.git] / nail.h
blob0ed4b10433685823bd601c813c95e76d622a57fa
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 - 2014 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
6 */
7 /*
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
13 * are met:
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
37 * SUCH DAMAGE.
41 * Mail -- a mail program
43 * Author: Kurt Shoens (UCB) March 25, 1978
46 #include "config.h"
48 #include <sys/stat.h>
49 #include <sys/types.h>
51 #include <errno.h>
52 #include <limits.h>
53 #include <setjmp.h>
54 #include <signal.h>
55 #include <stdarg.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <termios.h>
60 #include <time.h>
61 #include <unistd.h>
63 #if defined __STDC_VERSION__ && __STDC_VERSION__ + 0 >= 199901L
64 # include <stdint.h>
65 #else
66 # include <inttypes.h>
67 #endif
69 #ifdef HAVE_C90AMEND1
70 # include <wchar.h>
71 # include <wctype.h>
72 #endif
73 #ifdef HAVE_DEBUG
74 # include <assert.h>
75 #endif
76 #ifdef HAVE_ICONV
77 # include <iconv.h>
78 #endif
79 #ifdef HAVE_REGEX
80 # include <regex.h>
81 #endif
83 #ifdef HAVE_OPENSSL_MD5
84 # include <openssl/md5.h>
85 #endif
88 * Constants, some nail-specific macros
91 #if !defined NI_MAXHOST || NI_MAXHOST < 1025
92 # undef NI_MAXHOST
93 # define NI_MAXHOST 1025
94 #endif
96 /* TODO PATH_MAX: fixed-size buffer is always wrong (think NFS) */
97 #ifndef PATH_MAX
98 # ifdef MAXPATHLEN
99 # define PATH_MAX MAXPATHLEN
100 # else
101 # define PATH_MAX 1024 /* _XOPEN_PATH_MAX POSIX 2008/Cor 1-2013 */
102 # endif
103 #endif
105 #ifndef STDIN_FILENO
106 # define STDIN_FILENO 0
107 #endif
108 #ifndef STDOUT_FILENO
109 # define STDOUT_FILENO 1
110 #endif
111 #ifndef STDERR_FILENO
112 # define STDERR_FILENO 2
113 #endif
115 #ifdef NSIG_MAX
116 # undef NSIG
117 # define NSIG NSIG_MAX
118 #elif !defined NSIG
119 # define NSIG ((sizeof(sigset_t) * 8) - 1)
120 #endif
122 /* */
124 #if BUFSIZ + 0 > 2560 /* TODO simply use BUFSIZ? */
125 # define LINESIZE BUFSIZ /* max readable line width */
126 #else
127 # define LINESIZE 2560
128 #endif
129 #define BUFFER_SIZE (BUFSIZ >= (1u << 13) ? BUFSIZ : (1u << 14))
131 /* Number of Not-Yet-Dead calls that are remembered */
132 #if defined HAVE_DEBUG || defined HAVE_DEVEL || defined HAVE_NYD2
133 # ifdef HAVE_NYD2
134 # define NYD_CALLS_MAX (25 * 99)
135 # elif defined HAVE_DEVEL
136 # define NYD_CALLS_MAX (25 * 66)
137 # else
138 # define NYD_CALLS_MAX (25 * 33)
139 # endif
140 #endif
142 #define APPEND /* New mail goes to end of mailbox */
143 #define CBAD (-15555)
144 #define ESCAPE '~' /* Default escape for sending */
145 #define FIO_STACK_SIZE 20 /* Maximum recursion for sourcing */
146 #define HIST_SIZE 242 /* tty.c: history list default size */
147 #define HSHSIZE 23 /* Hash prime TODO make dynamic, obsolete */
148 #define MAXARGC 1024 /* Maximum list of raw strings */
149 #define MAXEXP 25 /* Maximum expansion of aliases */
150 #define PROMPT_BUFFER_SIZE 80 /* getprompt() bufsize (> 3!) */
152 #define ACCOUNT_NULL "null" /* Name of "null" account */
153 #define MAILRC "~/.mailrc"
154 #define NETRC "~/.netrc"
155 #define TMPDIR_FALLBACK "/tmp"
157 /* Some environment variables for pipe hooks */
158 #define AGENT_USER "NAIL_USER"
159 #define AGENT_USER_ENC "NAIL_USER_ENC"
160 #define AGENT_HOST "NAIL_HOST"
161 #define AGENT_HOST_PORT "NAIL_HOST_PORT"
163 #undef COLOUR
164 #ifdef HAVE_COLOUR
165 # define COLOUR(X) X
166 #else
167 # define COLOUR(X)
168 #endif
169 #define COLOUR_MSGINFO "fg=green"
170 #define COLOUR_PARTINFO "fg=brown"
171 #define COLOUR_FROM_ "fg=brown"
172 #define COLOUR_HEADER "fg=red"
173 #define COLOUR_UHEADER "ft=bold,fg=red"
174 #define COLOUR_TERMS \
175 "cons25,linux,rxvt,rxvt-unicode,screen,sun,vt100,vt220,wsvt25,xterm"
176 #define COLOUR_USER_HEADERS "from,subject"
178 #define FROM_DATEBUF 64 /* Size of RFC 4155 From_ line date */
179 #define DATE_DAYSYEAR 365L
180 #define DATE_SECSMIN 60L
181 #define DATE_MINSHOUR 60L
182 #define DATE_HOURSDAY 24L
183 #define DATE_SECSDAY (DATE_SECSMIN * DATE_MINSHOUR * DATE_HOURSDAY)
185 /* *indentprefix* default as of POSIX */
186 #define INDENT_DEFAULT "\t"
188 /* Default *encoding* as enum conversion below */
189 #define MIME_DEFAULT_ENCODING CONV_TOQP
191 /* Maximum allowed line length in a mail before QP folding is necessary), and
192 * the real limit we go for */
193 #define MIME_LINELEN_MAX 1000
194 #define MIME_LINELEN_LIMIT (MIME_LINELEN_MAX - 50)
196 /* Locations of mime.types(5) */
197 #define MIME_TYPES_USR "~/.mime.types"
198 #define MIME_TYPES_SYS "/etc/mime.types"
200 /* Fallback MIME charsets, if *charset-7bit* and *charset-8bit* or not set */
201 #define CHARSET_7BIT "US-ASCII"
202 #ifdef HAVE_ICONV
203 # define CHARSET_8BIT "UTF-8"
204 # define CHARSET_8BIT_OKEY charset_8bit
205 #else
206 # define CHARSET_8BIT "ISO-8859-1"
207 # define CHARSET_8BIT_OKEY ttycharset
208 #endif
210 /* Some environment variables for pipe hooks */
211 #define PIPEHOOK_FILENAME "NAIL_FILENAME"
212 #define PIPEHOOK_FILENAME_GENERATED "NAIL_FILENAME_GENERATED"
213 #define PIPEHOOK_CONTENT "NAIL_CONTENT"
214 #define PIPEHOOK_CONTENT_EVIDENCE "NAIL_CONTENT_EVIDENCE"
216 /* Is *W* a quoting (ASCII only) character? */
217 #define ISQUOTE(W) \
218 ((W) == L'>' || (W) == L'|' || (W) == L'}' || (W) == L':')
220 /* Maximum number of quote characters (not bytes!) that'll be used on
221 * follow lines when compressing leading quote characters */
222 #define QUOTE_MAX 42
224 /* How much spaces should a <tab> count when *quote-fold*ing? (power-of-two!) */
225 #define QUOTE_TAB_SPACES 8
227 /* Maximum size of a message that is passed through to the spam system */
228 #define SPAM_MAXSIZE 420000
230 /* String dope: dynamic buffer size, and size of the single builtin one that's
231 * used first */
232 #define SBUFFER_SIZE 0x18000u
233 #define SBUFFER_BUILTIN 0x2000u
235 /* These come from the configuration (named Xxy to not clash with sh(1)..) */
236 #ifndef XSHELL
237 # define XSHELL "/bin/sh"
238 #endif
239 #ifndef XLISTER
240 # define XLISTER "ls"
241 #endif
242 #ifndef XPAGER
243 # define XPAGER "more"
244 #endif
247 * CC support, generic macros etc.
250 #if defined __clang__
251 # define CC_CLANG 1
252 # define PREREQ_CLANG(X,Y) \
253 (__clang_major__ + 0 > (X) || \
254 (__clang_major__ + 0 == (X) && __clang_minor__ + 0 >= (Y)))
255 # define __EXTEN __extension__
256 #elif defined __GNUC__
257 # define CC_GCC 1
258 # define PREREQ_GCC(X,Y) \
259 (__GNUC__ + 0 > (X) || (__GNUC__ + 0 == (X) && __GNUC_MINOR__ + 0 >= (Y)))
260 # define __EXTEN __extension__
261 #endif
263 #ifndef CC_CLANG
264 # define CC_CLANG 0
265 # define PREREQ_CLANG(X,Y) 0
266 #endif
267 #ifndef CC_GCC
268 # define CC_GCC 0
269 # define PREREQ_GCC(X,Y) 0
270 #endif
271 #ifndef __EXTEN
272 # define __EXTEN
273 #endif
275 /* XXX Suppress unused return values via #pragma's;
276 * XXX Wild guesses: clang(1) 1.7 and (OpenBSD) gcc(1) 4.2.1 don't work */
277 #if PREREQ_CLANG(3, 4)
278 # pragma clang diagnostic ignored "-Wunused-result"
279 #elif PREREQ_GCC(4, 7)
280 # pragma GCC diagnostic ignored "-Wunused-result"
281 #endif
283 /* For injection macros like DBG(), NATCH_CHAR() */
284 #define COMMA ,
286 #define EMPTY_FILE(F) typedef int CONCAT(avoid_empty_file__, F);
288 /* Pointer to size_t */
289 #define PTR2SIZE(X) ((size_t)(uintptr_t)(X))
291 /* Pointer comparison (types from below) */
292 #define PTRCMP(A,C,B) ((uintptr_t)(A) C (uintptr_t)(B))
294 /* Ditto, compare (maybe mixed-signed) integers cases to T bits, unsigned;
295 * Note: doesn't sign-extend correctly, that's still up to the caller */
296 #define UICMP(T,A,C,B) ((ui ## T ## _t)(A) C (ui ## T ## _t)(B))
298 /* Members in constant array */
299 #ifndef NELEM
300 # define NELEM(A) (sizeof(A) / sizeof(A[0]))
301 #endif
303 /* sizeof() for member fields */
304 #define SIZEOF_FIELD(T,F) sizeof(((T *)NULL)->F)
306 /* Casts-away (*NOT* cast-away) */
307 #define UNUSED(X) ((void)(X))
308 #define UNCONST(P) ((void*)(uintptr_t)(void const*)(P))
309 #define UNVOLATILE(P) ((void*)(uintptr_t)(void volatile*)(P))
310 #define UNXXX(T,C,P) ((T)(uintptr_t)(C)(P))
312 /* __STDC_VERSION__ is ISO C99, so also use __STDC__, which should work */
313 #if defined __STDC__ || defined __STDC_VERSION__ /*|| defined __cplusplus*/
314 # define STRING(X) #X
315 # define XSTRING(X) STRING(X)
316 # define CONCAT(S1,S2) _CONCAT(S1, S2)
317 # define _CONCAT(S1,S2) S1 ## S2
318 #else
319 # define STRING(X) "X"
320 # define XSTRING STRING
321 # define CONCAT(S1,S2) S1/**/S2
322 #endif
324 #if defined __STDC_VERSION__ && __STDC_VERSION__ + 0 >= 199901L
325 /* Variable size arrays and structure fields */
326 # define VFIELD_SIZE(X)
327 # define VFIELD_SIZEOF(T,F) (0)
328 /* Inline functions */
329 # define HAVE_INLINE
330 # define INLINE inline
331 # define SINLINE static inline
332 #else
333 # define VFIELD_SIZE(X) (X)
334 # define VFIELD_SIZEOF(T,F) SIZEOF_FIELD(T, F)
335 # if CC_CLANG || PREREQ_GCC(2, 9)
336 # define INLINE static __inline
337 # define SINLINE static __inline
338 # else
339 # define INLINE
340 # define SINLINE static
341 # endif
342 #endif
344 #undef __FUN__
345 #if defined __STDC_VERSION__ && __STDC_VERSION__ + 0 >= 199901L
346 # define __FUN__ __func__
347 #elif CC_CLANG || PREREQ_GCC(3, 4)
348 # define __FUN__ __FUNCTION__
349 #else
350 # define __FUN__ uagent /* Something that is not a literal */
351 #endif
353 #if defined __predict_true && defined __predict_false
354 # define LIKELY(X) __predict_true(X)
355 # define UNLIKELY(X) __predict_false(X)
356 #elif CC_CLANG || PREREQ_GCC(2, 96)
357 # define LIKELY(X) __builtin_expect(X, 1)
358 # define UNLIKELY(X) __builtin_expect(X, 0)
359 #else
360 # define LIKELY(X) (X)
361 # define UNLIKELY(X) (X)
362 #endif
364 #undef HAVE_NATCH_CHAR
365 #undef NATCH_CHAR
366 #if defined HAVE_SETLOCALE && defined HAVE_C90AMEND1 && defined HAVE_WCWIDTH
367 # define HAVE_NATCH_CHAR
368 # define NATCH_CHAR(X) X
369 #else
370 # define NATCH_CHAR(X)
371 #endif
373 /* Compile-Time-Assert */
374 #define CTA(TEST) _CTA_1(TEST, __LINE__)
375 #define _CTA_1(TEST,L) _CTA_2(TEST, L)
376 #define _CTA_2(TEST,L) \
377 typedef char COMPILE_TIME_ASSERT_failed_at_line_ ## L[(TEST) ? 1 : -1]
379 #undef ISPOW2
380 #define ISPOW2(X) ((((X) - 1) & (X)) == 0)
381 #undef MIN
382 #define MIN(A, B) ((A) < (B) ? (A) : (B))
383 #undef MAX
384 #define MAX(A, B) ((A) < (B) ? (B) : (A))
385 #undef ABS
386 #define ABS(A) ((A) < 0 ? -(A) : (A))
388 #undef DBG
389 #undef NDBG
390 #ifndef HAVE_DEBUG
391 # undef assert
392 # define assert(X) UNUSED(0)
393 # define DBG(X)
394 # define NDBG(X) X
395 #else
396 # define DBG(X) X
397 # define NDBG(X)
398 #endif
400 /* Translation (init in main.c) */
401 #undef _
402 #undef N_
403 #undef V_
404 #define _(S) (S)
405 #define N_(S) (S)
406 #define V_(S) (S)
409 * Types
412 typedef unsigned long ul_it;
413 typedef unsigned int ui_it;
414 typedef unsigned short us_it;
415 typedef unsigned char uc_it;
417 typedef signed long sl_it;
418 typedef signed int si_it;
419 typedef signed short ss_it;
420 typedef signed char sc_it;
422 #ifdef UINT8_MAX
423 # define UI8_MAX UINT8_MAX
424 # define SI8_MIN INT8_MIN
425 # define SI8_MAX INT8_MAX
426 typedef uint8_t ui8_t;
427 typedef int8_t si8_t;
428 #elif UCHAR_MAX != 255
429 # error UCHAR_MAX must be 255
430 #else
431 # define UI8_MAX UCHAR_MAX
432 # define SI8_MIN CHAR_MIN
433 # define SI8_MAX CHAR_MAX
434 typedef unsigned char ui8_t;
435 typedef signed char si8_t;
436 #endif
438 #ifdef UINT16_MAX
439 # define UI16_MAX UINT16_MAX
440 # define SI16_MIN INT16_MIN
441 # define SI16_MAX INT16_MAX
442 typedef uint16_t ui16_t;
443 typedef int16_t si16_t;
444 #elif USHRT_MAX != 0xFFFFu
445 # error USHRT_MAX must be 0xFFFF
446 #else
447 # define UI16_MAX USHRT_MAX
448 # define SI16_MIN SHRT_MIN
449 # define SI16_MAX SHRT_MAX
450 typedef unsigned short ui16_t;
451 typedef signed short si16_t;
452 #endif
454 #ifdef UINT32_MAX
455 # define UI32_MAX UINT32_MAX
456 # define SI32_MIN INT32_MIN
457 # define SI32_MAX INT32_MAX
458 typedef uint32_t ui32_t;
459 typedef int32_t si32_t;
460 #elif ULONG_MAX == 0xFFFFFFFFu
461 # define UI32_MAX ULONG_MAX
462 # define SI32_MIN LONG_MIN
463 # define SI32_MAX LONG_MAX
464 typedef unsigned long int ui32_t;
465 typedef signed long int si32_t;
466 #elif UINT_MAX != 0xFFFFFFFFu
467 # error UINT_MAX must be 0xFFFFFFFF
468 #else
469 # define UI32_MAX UINT_MAX
470 # define SI32_MIN INT_MIN
471 # define SI32_MAX INT_MAX
472 typedef unsigned int ui32_t;
473 typedef signed int si32_t;
474 #endif
476 #ifdef UINT64_MAX
477 # define UI64_MAX UINT64_MAX
478 # define SI64_MIN INT64_MIN
479 # define SI64_MAX INT64_MAX
480 typedef uint64_t ui64_t;
481 #elif ULONG_MAX <= 0xFFFFFFFFu
482 # if !defined ULLONG_MAX || ULLONG_MAX != 0xFFFFFFFFFFFFFFFFu
483 # error We need a 64 bit integer
484 # else
485 # define UI64_MAX ULLONG_MAX
486 # define SI64_MIN LLONG_MIN
487 # define SI64_MAX LLONG_MAX
488 __EXTEN typedef unsigned long long ui64_t;
489 __EXTEN typedef signed long long si64_t;
490 # endif
491 #else
492 # define UI64_MAX ULONG_MAX
493 # define SI64_MIN LONG_MIN
494 # define SI64_MAX LONG_MAX
495 typedef unsigned long ui64_t;
496 typedef signed long si64_t;
497 #endif
499 /* (So that we can use UICMP() for size_t comparison, too) */
500 typedef size_t uiz_t;
501 typedef ssize_t siz_t;
503 #ifndef UINTPTR_MAX
504 # ifdef SIZE_MAX
505 # define uintptr_t size_t
506 # define UINTPTR_MAX SIZE_MAX
507 # else
508 # define uintptr_t unsigned long
509 # define UINTPTR_MAX ULONG_MAX
510 # endif
511 #endif
513 /* XXX Note we don't really deal with that the right way in that we pass size_t
514 * XXX arguments without casting; should do, because above we assert UINT_MAX
515 * XXX is indeed ui32_t -- CTAsserted in main.c */
516 #if defined __STDC_VERSION__ && __STDC_VERSION__ + 0 >= 199901L
517 # define ZFMT "zu"
518 # define __ZFMT_CTA() CTA(1 == 1)
519 #elif defined SIZE_MAX && SIZE_MAX == 0xFFFFFFFFu && ULONG_MAX != UINT_MAX
520 # define ZFMT "u"
521 # define __ZFMT_CTA() CTA(sizeof(size_t) == sizeof(unsigned int))
522 #endif
523 #ifndef ZFMT
524 # define ZFMT "lu"
525 # define __ZFMT_CTA() CTA(sizeof(size_t) == sizeof(unsigned long))
526 #endif
528 enum {FAL0, TRU1};
529 typedef si8_t bool_t;
531 typedef void ( *sighandler_type)(int);
533 enum user_options {
534 OPT_NONE,
535 OPT_DEBUG = 1u<< 0, /* -d / *debug* */
536 OPT_VERB = 1u<< 1, /* -v / *verbose* */
537 OPT_VERBVERB = 1u<<19, /* .. even more verbosity */
538 OPT_EXISTONLY = 1u<< 2, /* -e */
539 OPT_HEADERSONLY = 1u<< 3, /* -H */
540 OPT_HEADERLIST = 1u<< 4, /* -L */
541 OPT_NOSRC = 1u<< 5, /* -n */
542 OPT_E_FLAG = 1u<< 6, /* -E / *skipemptybody* */
543 OPT_F_FLAG = 1u<< 7, /* -F */
544 OPT_N_FLAG = 1u<< 8, /* -N / *header* */
545 OPT_R_FLAG = 1u<< 9, /* -R */
546 OPT_r_FLAG = 1u<<10, /* -r (plus option_r_arg) */
547 OPT_t_FLAG = 1u<<11, /* -t */
548 OPT_u_FLAG = 1u<<12, /* -u / $USER and pw->pw_uid != getuid(2) */
549 OPT_TILDE_FLAG = 1u<<13, /* -~ */
550 OPT_BATCH_FLAG = 1u<<14, /* -# */
552 OPT_SENDMODE = 1u<<15, /* Usage case forces send mode */
553 OPT_INTERACTIVE = 1u<<16, /* isatty(0) */
554 OPT_TTYIN = OPT_INTERACTIVE,
555 OPT_TTYOUT = 1u<<17,
556 OPT_UNICODE = 1u<<18, /* We're in an UTF-8 environment */
558 /* Some easy-access shortcuts */
559 OPT_D_V = OPT_DEBUG | OPT_VERB,
560 OPT_D_VV = OPT_DEBUG | OPT_VERBVERB
562 #define IS_TTY_SESSION() \
563 ((options & (OPT_TTYIN | OPT_TTYOUT)) == (OPT_TTYIN | OPT_TTYOUT))
565 enum exit_status {
566 EXIT_OK = EXIT_SUCCESS,
567 EXIT_ERR = EXIT_FAILURE,
568 EXIT_USE = 2,
569 EXIT_COLL_ABORT = 1<<1, /* Message collection was aborted */
570 EXIT_SEND_ERROR = 1<<2 /* Unspecified send error occurred */
573 enum fedit_mode {
574 FEDIT_NONE = 0,
575 FEDIT_SYSBOX = 1<<0, /* %: prefix */
576 FEDIT_RDONLY = 1<<1, /* Readonly (per-box, OPT_R_FLAG is global) */
577 FEDIT_NEWMAIL = 1<<2 /* `newmail' operation TODO OBSOLETE THIS! */
580 enum fexp_mode {
581 FEXP_FULL, /* Full expansion */
582 FEXP_LOCAL = 1<<0, /* Result must be local file/maildir */
583 FEXP_SHELL = 1<<1, /* No folder %,#,&,+ stuff, yet sh(1) */
584 FEXP_NSHORTCUT = 1<<2, /* Don't expand shortcuts */
585 FEXP_SILENT = 1<<3, /* Don't print but only return errors */
586 FEXP_MULTIOK = 1<<4 /* Expansion to many entries is ok */
589 enum flock_type {
590 FLOCK_READ,
591 FLOCK_WRITE,
592 FLOCK_UNLOCK
595 /* <0 means "stop" unless *prompt* extensions are enabled. */
596 enum prompt_exp {
597 PROMPT_STOP = -1, /* \c */
598 /* *prompt* extensions: \$, \@ etc. */
599 PROMPT_DOLLAR = -2,
600 PROMPT_AT = -3
603 enum colourspec {
604 COLOURSPEC_MSGINFO,
605 COLOURSPEC_PARTINFO,
606 COLOURSPEC_FROM_,
607 COLOURSPEC_HEADER,
608 COLOURSPEC_UHEADER,
609 COLOURSPEC_RESET
612 enum okay {
613 STOP = 0,
614 OKAY = 1
617 enum mimeenc {
618 MIME_NONE, /* message is not in MIME format */
619 MIME_BIN, /* message is in binary encoding */
620 MIME_8B, /* message is in 8bit encoding */
621 MIME_7B, /* message is in 7bit encoding */
622 MIME_QP, /* message is quoted-printable */
623 MIME_B64 /* message is in base64 encoding */
626 enum mime_counter_evidence {
627 MIMECE_NONE,
628 MIMECE_USR_OVWR = 1<<1
631 enum conversion {
632 CONV_NONE, /* no conversion */
633 CONV_7BIT, /* no conversion, is 7bit */
634 CONV_FROMQP, /* convert from quoted-printable */
635 CONV_TOQP, /* convert to quoted-printable */
636 CONV_8BIT, /* convert to 8bit (iconv) */
637 CONV_FROMB64, /* convert from base64 */
638 CONV_FROMB64_T, /* convert from base64/text */
639 CONV_TOB64, /* convert to base64 */
640 CONV_FROMHDR, /* convert from RFC1522 format */
641 CONV_TOHDR, /* convert to RFC1522 format */
642 CONV_TOHDR_A /* convert addresses for header */
645 enum sendaction {
646 SEND_MBOX, /* no conversion to perform */
647 SEND_RFC822, /* no conversion, no From_ line */
648 SEND_TODISP, /* convert to displayable form */
649 SEND_TODISP_ALL, /* same, include all MIME parts */
650 SEND_SHOW, /* convert to 'show' command form */
651 SEND_TOSRCH, /* convert for IMAP SEARCH */
652 SEND_TOFILE, /* convert for saving body to a file */
653 SEND_TOPIPE, /* convert for pipe-content/subc. */
654 SEND_QUOTE, /* convert for quoting */
655 SEND_QUOTE_ALL, /* same, include all MIME parts */
656 SEND_DECRYPT /* decrypt */
659 enum mimecontent {
660 MIME_UNKNOWN, /* unknown content */
661 MIME_SUBHDR, /* inside a multipart subheader */
662 MIME_822, /* message/rfc822 content */
663 MIME_MESSAGE, /* other message/ content */
664 MIME_TEXT_PLAIN, /* text/plain content */
665 MIME_TEXT_HTML, /* text/html content */
666 MIME_TEXT, /* other text/ content */
667 MIME_ALTERNATIVE, /* multipart/alternative content */
668 MIME_DIGEST, /* multipart/digest content */
669 MIME_MULTI, /* other multipart/ content */
670 MIME_PKCS7, /* PKCS7 content */
671 MIME_DISCARD /* content is discarded */
674 /* Content-Transfer-Encodings as defined in RFC 2045:
675 * - Quoted-Printable, section 6.7
676 * - Base64, section 6.8 */
677 #define QP_LINESIZE (4 * 19) /* Max. compliant QP linesize */
679 #define B64_LINESIZE (4 * 19) /* Max. compliant Base64 linesize */
680 #define B64_ENCODE_INPUT_PER_LINE 57 /* Max. input for Base64 encode/line */
682 /* xxx QP came later, maybe rewrite all to use mimecte_flags directly? */
683 enum __mimecte_flags {
684 MIMECTE_NONE,
685 MIMECTE_SALLOC = 1<<0, /* Use salloc(), not srealloc().. */
686 /* ..result .s,.l point to user buffer of *_LINESIZE+[+[+]] bytes instead */
687 MIMECTE_BUF = 1<<1,
688 MIMECTE_CRLF = 1<<2, /* (encode) Append "\r\n" to lines */
689 MIMECTE_LF = 1<<3, /* (encode) Append "\n" to lines */
690 /* (encode) If one of _CRLF/_LF is set, honour *_LINESIZE+[+[+]] and
691 * inject the desired line-ending whenever a linewrap is desired */
692 MIMECTE_MULTILINE = 1<<4,
693 /* (encode) Quote with header rules, do not generate soft NL breaks? */
694 MIMECTE_ISHEAD = 1<<5
697 enum qpflags {
698 QP_NONE = MIMECTE_NONE,
699 QP_SALLOC = MIMECTE_SALLOC,
700 QP_BUF = MIMECTE_BUF,
701 QP_ISHEAD = MIMECTE_ISHEAD
704 enum b64flags {
705 B64_NONE = MIMECTE_NONE,
706 B64_SALLOC = MIMECTE_SALLOC,
707 B64_BUF = MIMECTE_BUF,
708 B64_CRLF = MIMECTE_CRLF,
709 B64_LF = MIMECTE_LF,
710 B64_MULTILINE = MIMECTE_MULTILINE
713 enum oflags {
714 OF_RDONLY = 1<<0,
715 OF_WRONLY = 1<<1,
716 OF_RDWR = 1<<2,
717 OF_APPEND = 1<<3,
718 OF_CREATE = 1<<4,
719 OF_TRUNC = 1<<5,
720 OF_EXCL = 1<<6,
721 OF_CLOEXEC = 1<<7,
722 OF_UNLINK = 1<<8, /* Only for Ftmp(): unlink(2) after creation */
723 OF_HOLDSIGS = 1<<9, /* Mutual with OF_UNLINK - await Ftmp_free() */
724 OF_REGISTER = 1<<10 /* Register file in our file table */
727 enum tdflags {
728 TD_NONE, /* no display conversion */
729 TD_ISPR = 1<<0, /* use isprint() checks */
730 TD_ICONV = 1<<1, /* use iconv() */
731 TD_DELCTRL = 1<<2, /* delete control characters */
734 * NOTE: _TD_EOF and _TD_BUFCOPY may be ORd with enum conversion and
735 * enum sendaction, and may thus NOT clash with their bit range!
737 _TD_EOF = 1<<14, /* EOF seen, last round! */
738 _TD_BUFCOPY = 1<<15 /* Buffer may be constant, copy it */
741 enum protocol {
742 PROTO_FILE, /* refers to a local file */
743 PROTO_POP3, /* is a pop3 server string */
744 PROTO_IMAP, /* is an imap server string */
745 PROTO_MAILDIR, /* refers to a maildir folder */
746 PROTO_UNKNOWN /* unknown protocol */
749 enum cproto {
750 CPROTO_SMTP,
751 CPROTO_POP3,
752 CPROTO_IMAP
755 enum authtype {
756 AUTHTYPE_NONE = 1<<0,
757 AUTHTYPE_PLAIN = 1<<1, /* POP3: APOP is covered by this */
758 AUTHTYPE_LOGIN = 1<<2,
759 AUTHTYPE_CRAM_MD5 = 1<<3,
760 AUTHTYPE_GSSAPI = 1<<4
763 #ifdef HAVE_SSL
764 enum ssl_verify_level {
765 SSL_VERIFY_IGNORE,
766 SSL_VERIFY_WARN,
767 SSL_VERIFY_ASK,
768 SSL_VERIFY_STRICT
770 #endif
772 /* A large enum with all the binary and value options a.k.a their keys.
773 * Only the constant keys are in here, to be looked up via ok_[bv]look(),
774 * ok_[bv]set() and ok_[bv]clear().
775 * Note: see the comments in accmacvar.c before changing *anything* in here! */
776 enum okeys {
777 /* Option keys for binary options */
778 ok_b_add_file_recipients,
779 ok_b_allnet,
780 ok_b_append,
781 ok_b_ask,
782 ok_b_askatend,
783 ok_b_askattach,
784 ok_b_askbcc,
785 ok_b_askcc,
786 ok_b_asksign,
787 ok_b_asksub,
788 ok_b_attachment_ask_content_description,
789 ok_b_attachment_ask_content_disposition,
790 ok_b_attachment_ask_content_id,
791 ok_b_attachment_ask_content_type,
792 ok_b_autocollapse,
793 ok_b_autoprint,
794 ok_b_autothread,
795 ok_b_bang,
796 ok_b_batch_exit_on_error,
797 ok_b_bsdannounce,
798 ok_b_bsdcompat,
799 ok_b_bsdflags,
800 ok_b_bsdheadline,
801 ok_b_bsdmsgs,
802 ok_b_bsdorder,
803 ok_b_bsdset,
804 ok_b_colour_disable,
805 ok_b_colour_pager,
806 ok_b_debug, /* {special=1} */
807 ok_b_disconnected,
808 ok_b_disposition_notification_send,
809 ok_b_dot,
810 ok_b_editalong,
811 ok_b_editheaders,
812 ok_b_emptybox,
813 ok_b_emptystart,
814 ok_b_flipr,
815 ok_b_forward_as_attachment,
816 ok_b_fullnames,
817 ok_b_header, /* {special=1} */
818 ok_b_history_gabby,
819 ok_b_history_gabby_persist,
820 ok_b_hold,
821 ok_b_idna_disable,
822 ok_b_ignore,
823 ok_b_ignoreeof,
824 ok_b_imap_use_starttls,
825 ok_b_keep,
826 ok_b_keep_content_length,
827 ok_b_keepsave,
828 ok_b_line_editor_disable,
829 ok_b_markanswered,
830 ok_b_message_id_disable,
831 ok_b_metoo,
832 ok_b_mime_allow_text_controls,
833 ok_b_netrc_lookup,
834 ok_b_outfolder,
835 ok_b_page,
836 ok_b_piperaw,
837 ok_b_pop3_bulk_load,
838 ok_b_pop3_no_apop,
839 ok_b_pop3_use_starttls,
840 ok_b_print_all_chars,
841 ok_b_print_alternatives,
842 ok_b_quiet,
843 ok_b_quote_as_attachment,
844 ok_b_recipients_in_cc,
845 ok_b_record_resent,
846 ok_b_reply_in_same_charset,
847 ok_b_Replyall,
848 ok_b_rfc822_body_from_, /* {name=rfc822-body-from_} */
849 ok_b_save,
850 ok_b_searchheaders,
851 ok_b_sendcharsets_else_ttycharset,
852 ok_b_sendwait,
853 ok_b_showlast,
854 ok_b_showname,
855 ok_b_showto,
856 ok_b_skipemptybody, /* {special=1} */
857 ok_b_smime_force_encryption,
858 ok_b_smime_no_default_ca,
859 ok_b_smime_sign,
860 ok_b_smtp_use_starttls,
861 ok_b_ssl_no_default_ca,
862 ok_b_ssl_v2_allow,
863 ok_b_v15_compat,
864 ok_b_verbose, /* {special=1} */
865 ok_b_writebackedited,
867 /* Option keys for values options */
868 ok_v_agent_shell_lookup,
869 ok_v_attrlist,
870 ok_v_autobcc,
871 ok_v_autocc,
872 ok_v_autosort,
873 ok_v_charset_7bit,
874 ok_v_charset_8bit,
875 ok_v_cmd,
876 ok_v_colour_from_, /* {name=colour-from_} */
877 ok_v_colour_header,
878 ok_v_colour_msginfo,
879 ok_v_colour_partinfo,
880 ok_v_colour_terms,
881 ok_v_colour_uheader,
882 ok_v_colour_user_headers,
883 ok_v_crt,
884 ok_v_datefield,
885 ok_v_datefield_markout_older,
886 ok_v_DEAD,
887 ok_v_EDITOR,
888 ok_v_encoding,
889 ok_v_escape,
890 ok_v_folder, /* {special=1} */
891 ok_v_folder_hook,
892 ok_v_from,
893 ok_v_fwdheading,
894 ok_v_headline,
895 ok_v_headline_bidi,
896 ok_v_hostname,
897 ok_v_imap_auth,
898 ok_v_imap_cache,
899 ok_v_imap_keepalive,
900 ok_v_imap_list_depth,
901 ok_v_indentprefix,
902 ok_v_line_editor_cursor_right, /* {special=1} */
903 ok_v_LISTER,
904 ok_v_MAIL,
905 ok_v_MBOX,
906 ok_v_mime_counter_evidence,
907 /* TODO v15-compat: mimetypes-load-control -> mimetypes-load / mimetypes */
908 ok_v_mimetypes_load_control,
909 ok_v_NAIL_EXTRA_RC, /* {name=NAIL_EXTRA_RC} */
910 /* TODO v15-compat: NAIL_HEAD -> message-head? */
911 ok_v_NAIL_HEAD, /* {name=NAIL_HEAD} */
912 /* TODO v15-compat: NAIL_HISTFILE -> history-file */
913 ok_v_NAIL_HISTFILE, /* {name=NAIL_HISTFILE} */
914 /* TODO v15-compat: NAIL_HISTSIZE -> history-size{,limit} */
915 ok_v_NAIL_HISTSIZE, /* {name=NAIL_HISTSIZE} */
916 /* TODO v15-compat: NAIL_TAIL -> message-tail? */
917 ok_v_NAIL_TAIL, /* {name=NAIL_TAIL} */
918 ok_v_newfolders,
919 ok_v_newmail,
920 ok_v_ORGANIZATION,
921 ok_v_PAGER,
922 ok_v_password,
923 /* TODO pop3_auth is yet a dummy to enable easier impl. of ccred_lookup()! */
924 ok_v_pop3_auth,
925 ok_v_pop3_keepalive,
926 ok_v_prompt,
927 ok_v_quote,
928 ok_v_quote_fold,
929 ok_v_record,
930 ok_v_replyto,
931 ok_v_screen,
932 ok_v_sendcharsets,
933 ok_v_sender,
934 ok_v_sendmail,
935 ok_v_sendmail_arguments,
936 ok_v_sendmail_progname,
937 ok_v_SHELL,
938 ok_v_Sign,
939 ok_v_sign,
940 ok_v_signature,
941 ok_v_smime_ca_dir,
942 ok_v_smime_ca_file,
943 ok_v_smime_crl_dir,
944 ok_v_smime_crl_file,
945 ok_v_smime_sign_cert,
946 ok_v_smime_sign_include_certs,
947 ok_v_smtp,
948 /* TODO v15-compat: smtp-auth: drop */
949 ok_v_smtp_auth,
950 ok_v_smtp_auth_password,
951 ok_v_smtp_auth_user,
952 ok_v_smtp_hostname,
953 ok_v_spam_command,
954 ok_v_spam_host,
955 ok_v_spam_maxsize,
956 ok_v_spam_port,
957 ok_v_spam_socket,
958 ok_v_spam_user,
959 ok_v_ssl_ca_dir,
960 ok_v_ssl_ca_file,
961 ok_v_ssl_cert,
962 ok_v_ssl_cipher_list,
963 ok_v_ssl_crl_dir,
964 ok_v_ssl_crl_file,
965 ok_v_ssl_key,
966 ok_v_ssl_method,
967 ok_v_ssl_rand_egd,
968 ok_v_ssl_rand_file,
969 ok_v_ssl_verify,
970 ok_v_stealthmua,
971 ok_v_toplines,
972 ok_v_ttycharset,
973 ok_v_user,
974 ok_v_VISUAL
977 enum okey_xlook_mode {
978 OXM_PLAIN = 1<<0, /* Plain key always tested */
979 OXM_H_P = 1<<1, /* Check PLAIN-.url_h_p */
980 OXM_U_H_P = 1<<2, /* Check PLAIN-.url_u_h_p */
981 OXM_ALL = 0x7
984 /* Locale-independent character classes */
985 enum {
986 C_CNTRL = 0000,
987 C_BLANK = 0001,
988 C_WHITE = 0002,
989 C_SPACE = 0004,
990 C_PUNCT = 0010,
991 C_OCTAL = 0020,
992 C_DIGIT = 0040,
993 C_UPPER = 0100,
994 C_LOWER = 0200
997 struct str {
998 char *s; /* the string's content */
999 size_t l; /* the stings's length */
1002 struct colour_table {
1003 /* Plus a copy of *colour-user-headers* */
1004 struct str ct_csinfo[COLOURSPEC_RESET+1 + 1];
1007 struct bidi_info {
1008 struct str bi_start; /* Start of (possibly) bidirectional text */
1009 struct str bi_end; /* End of ... */
1010 size_t bi_pad; /* No of visual columns to reserve for BIDI pad */
1013 struct url {
1014 char const *url_input; /* Input as given (really) */
1015 enum cproto url_cproto; /* Communication protocol as given */
1016 bool_t url_needs_tls; /* Wether the protocol uses SSL/TLS */
1017 bool_t url_had_user; /* Wether .url_user was part of the URL */
1018 ui16_t url_portno; /* atoi .url_port or default, host endian */
1019 char const *url_port; /* Port (if given) or NULL */
1020 char url_proto[14]; /* Communication protocol as 'xy\0//' */
1021 ui8_t url_proto_len; /* Length of .url_proto ('\0' index) */
1022 ui8_t url_proto_xlen; /* .. if '\0' is replaced with ':' */
1023 struct str url_user; /* User, exactly as given / looked up */
1024 struct str url_user_enc; /* User, urlxenc()oded */
1025 struct str url_pass; /* Pass (urlxdec()oded) or NULL */
1026 struct str url_host; /* Service hostname */
1027 struct str url_path; /* CPROTO_IMAP: path suffix or NULL */
1028 /* TODO: url_get_component(url *, enum COMPONENT, str *store) */
1029 struct str url_h_p; /* .url_host[:.url_port] */
1030 /* .url_user@.url_host
1031 * Note: for CPROTO_SMTP this may resolve HOST via *smtp-hostname* (->
1032 * *hostname*)! (And may later be overwritten according to *from*!) */
1033 struct str url_u_h;
1034 struct str url_u_h_p; /* .url_user@.url_host[:.url_port] */
1035 struct str url_eu_h_p; /* .url_user_enc@.url_host[:.url_port] */
1036 char const *url_p_u_h_p; /* .url_proto://.url_u_h_p */
1037 char const *url_p_eu_h_p; /* .url_proto://.url_eu_h_p */
1038 char const *url_p_eu_h_p_p; /* .url_proto://.url_eu_h_p[/.url_path] */
1041 struct ccred {
1042 enum cproto cc_cproto; /* Communication protocol */
1043 enum authtype cc_authtype; /* Desired authentication */
1044 char const *cc_auth; /* Authentication type as string */
1045 struct str cc_user; /* User (urlxdec()oded) or NULL */
1046 struct str cc_pass; /* Password (urlxdec()oded) or NULL */
1049 struct time_current {
1050 time_t tc_time;
1051 struct tm tc_gm;
1052 struct tm tc_local;
1053 char tc_ctime[32];
1056 struct quoteflt {
1057 FILE *qf_os; /* Output stream */
1058 char const *qf_pfix;
1059 ui32_t qf_pfix_len; /* Length of prefix: 0: bypass */
1060 ui32_t qf_qfold_min; /* Simple way: wrote prefix? */
1061 #ifdef HAVE_QUOTE_FOLD
1062 ui32_t qf_qfold_max; /* Otherwise: line lengths */
1063 ui8_t qf_state; /* *quote-fold* state machine */
1064 bool_t qf_brk_isws; /* Breakpoint is at WS */
1065 ui8_t __dummy[2];
1066 ui32_t qf_wscnt; /* Whitespace count */
1067 ui32_t qf_brkl; /* Breakpoint */
1068 ui32_t qf_brkw; /* Visual width, breakpoint */
1069 ui32_t qf_datw; /* Current visual output line width */
1070 struct str qf_dat; /* Current visual output line */
1071 struct str qf_currq; /* Current quote, compressed */
1072 mbstate_t qf_mbps[2];
1073 #endif
1076 struct search_expr {
1077 char const *ss_where; /* to search for the expression (not always used) */
1078 char const *ss_sexpr; /* String search expression; NULL: use .ss_reexpr */
1079 #ifdef HAVE_REGEX
1080 regex_t ss_reexpr;
1081 #endif
1084 struct eval_ctx {
1085 struct str ev_line; /* The terminated data to evaluate */
1086 ui32_t ev_line_size; /* May be used to store line memory size */
1087 bool_t ev_is_recursive; /* Evaluation in evaluation? (collect ~:) */
1088 ui8_t __dummy[3];
1089 bool_t ev_add_history; /* Enter (final) command in history? */
1090 char const *ev_new_content; /* History: reenter line, start with this */
1093 struct termios_state {
1094 struct termios ts_tios;
1095 char *ts_linebuf;
1096 size_t ts_linesize;
1097 bool_t ts_needs_reset;
1100 #define termios_state_reset() \
1101 do {\
1102 if (termios_state.ts_needs_reset) {\
1103 tcsetattr(0, TCSADRAIN, &termios_state.ts_tios);\
1104 termios_state.ts_needs_reset = FAL0;\
1106 } while (0)
1108 struct sock { /* data associated with a socket */
1109 int s_fd; /* file descriptor */
1110 #ifdef HAVE_SSL
1111 int s_use_ssl; /* SSL is used */
1112 # ifdef HAVE_OPENSSL
1113 void *s_ssl; /* SSL object */
1114 void *s_ctx; /* SSL context object */
1115 # endif
1116 #endif
1117 char *s_wbuf; /* for buffered writes */
1118 int s_wbufsize; /* allocated size of s_buf */
1119 int s_wbufpos; /* position of first empty data byte */
1120 char *s_rbufptr; /* read pointer to s_rbuf */
1121 int s_rsz; /* size of last read in s_rbuf */
1122 char const *s_desc; /* description of error messages */
1123 void (*s_onclose)(void); /* execute on close */
1124 char s_rbuf[LINESIZE + 1]; /* for buffered reads */
1127 struct sockconn {
1128 struct url sc_url;
1129 struct ccred sc_cred;
1130 struct sock sc_sock;
1133 struct mailbox {
1134 enum {
1135 MB_NONE = 000, /* no reply expected */
1136 MB_COMD = 001, /* command reply expected */
1137 MB_MULT = 002, /* multiline reply expected */
1138 MB_PREAUTH = 004, /* not in authenticated state */
1139 MB_BYE = 010 /* may accept a BYE state */
1140 } mb_active;
1141 FILE *mb_itf; /* temp file with messages, read open */
1142 FILE *mb_otf; /* same, write open */
1143 char *mb_sorted; /* sort method */
1144 enum {
1145 MB_VOID, /* no type (e. g. connection failed) */
1146 MB_FILE, /* local file */
1147 MB_POP3, /* POP3 mailbox */
1148 MB_IMAP, /* IMAP mailbox */
1149 MB_MAILDIR, /* maildir folder */
1150 MB_CACHE /* cached mailbox */
1151 } mb_type; /* type of mailbox */
1152 enum {
1153 MB_DELE = 01, /* may delete messages in mailbox */
1154 MB_EDIT = 02 /* may edit messages in mailbox */
1155 } mb_perm;
1156 int mb_compressed; /* is a compressed mbox file */
1157 int mb_threaded; /* mailbox has been threaded */
1158 #ifdef HAVE_IMAP
1159 enum mbflags {
1160 MB_NOFLAGS = 000,
1161 MB_UIDPLUS = 001 /* supports IMAP UIDPLUS */
1162 } mb_flags;
1163 unsigned long mb_uidvalidity; /* IMAP unique identifier validity */
1164 char *mb_imap_account; /* name of current IMAP account */
1165 char *mb_imap_pass; /* xxx v15-compat URL workaround */
1166 char *mb_imap_mailbox; /* name of current IMAP mailbox */
1167 char *mb_cache_directory; /* name of cache directory */
1168 #endif
1169 struct sock mb_sock; /* socket structure */
1172 enum needspec {
1173 NEED_UNSPEC, /* unspecified need, don't fetch */
1174 NEED_HEADER, /* need the header of a message */
1175 NEED_BODY /* need header and body of a message */
1178 enum havespec {
1179 HAVE_NOTHING, /* nothing downloaded yet */
1180 HAVE_HEADER = 01, /* header is downloaded */
1181 HAVE_BODY = 02 /* entire message is downloaded */
1184 /* flag bits. Attention: Flags that are used in cache.c may not change */
1185 enum mflag {
1186 MUSED = (1<< 0), /* entry is used, but this bit isn't */
1187 MDELETED = (1<< 1), /* entry has been deleted */
1188 MSAVED = (1<< 2), /* entry has been saved */
1189 MTOUCH = (1<< 3), /* entry has been noticed */
1190 MPRESERVE = (1<< 4), /* keep entry in sys mailbox */
1191 MMARK = (1<< 5), /* message is marked! */
1192 MODIFY = (1<< 6), /* message has been modified */
1193 MNEW = (1<< 7), /* message has never been seen */
1194 MREAD = (1<< 8), /* message has been read sometime. */
1195 MSTATUS = (1<< 9), /* message status has changed */
1196 MBOX = (1<<10), /* Send this to mbox, regardless */
1197 MNOFROM = (1<<11), /* no From line */
1198 MHIDDEN = (1<<12), /* message is hidden to user */
1199 MFULLYCACHED = (1<<13), /* message is completely cached */
1200 MBOXED = (1<<14), /* message has been sent to mbox */
1201 MUNLINKED = (1<<15), /* message was unlinked from cache */
1202 MNEWEST = (1<<16), /* message is very new (newmail) */
1203 MFLAG = (1<<17), /* message has been flagged recently */
1204 MUNFLAG = (1<<18), /* message has been unflagged */
1205 MFLAGGED = (1<<19), /* message is `flagged' */
1206 MANSWER = (1<<20), /* message has been answered recently */
1207 MUNANSWER = (1<<21), /* message has been unanswered */
1208 MANSWERED = (1<<22), /* message is `answered' */
1209 MDRAFT = (1<<23), /* message has been drafted recently */
1210 MUNDRAFT = (1<<24), /* message has been undrafted */
1211 MDRAFTED = (1<<25), /* message is marked as `draft' */
1212 MOLDMARK = (1<<26), /* messages was marked previously */
1213 MSPAM = (1<<27) /* message is classified as spam */
1215 #define MMNORM (MDELETED | MSAVED | MHIDDEN)
1216 #define MMNDEL (MDELETED | MHIDDEN)
1218 #define visible(mp) (((mp)->m_flag & MMNDEL) == 0)
1220 struct mimepart {
1221 enum mflag m_flag; /* flags */
1222 enum havespec m_have; /* downloaded parts of the part */
1223 #ifdef HAVE_SPAM
1224 ui32_t m_spamscore; /* Spam score as int, 24:8 bits */
1225 #endif
1226 int m_block; /* block number of this part */
1227 size_t m_offset; /* offset in block of part */
1228 size_t m_size; /* Bytes in the part */
1229 size_t m_xsize; /* Bytes in the full part */
1230 long m_lines; /* Lines in the message */
1231 long m_xlines; /* Lines in the full message */
1232 time_t m_time; /* time the message was sent */
1233 char const *m_from; /* message sender */
1234 struct mimepart *m_nextpart; /* next part at same level */
1235 struct mimepart *m_multipart; /* parts of multipart */
1236 struct mimepart *m_parent; /* enclosing multipart part */
1237 char *m_ct_type; /* content-type */
1238 char *m_ct_type_plain; /* content-type without specs */
1239 char *m_ct_type_usr_ovwr; /* Forcefully overwritten one */
1240 enum mimecontent m_mimecontent; /* same in enum */
1241 char const *m_charset; /* charset */
1242 char *m_ct_transfer_enc; /* content-transfer-encoding */
1243 enum mimeenc m_mimeenc; /* same in enum */
1244 char *m_partstring; /* part level string */
1245 char *m_filename; /* attachment filename */
1248 struct message {
1249 enum mflag m_flag; /* flags */
1250 enum havespec m_have; /* downloaded parts of the message */
1251 #ifdef HAVE_SPAM
1252 ui32_t m_spamscore; /* Spam score as int, 24:8 bits */
1253 #endif
1254 int m_block; /* block number of this message */
1255 size_t m_offset; /* offset in block of message */
1256 size_t m_size; /* Bytes in the message */
1257 size_t m_xsize; /* Bytes in the full message */
1258 long m_lines; /* Lines in the message */
1259 long m_xlines; /* Lines in the full message */
1260 time_t m_time; /* time the message was sent */
1261 time_t m_date; /* time in the 'Date' field */
1262 unsigned m_idhash; /* hash on Message-ID for threads */
1263 struct message *m_child; /* first child of this message */
1264 struct message *m_younger; /* younger brother of this message */
1265 struct message *m_elder; /* elder brother of this message */
1266 struct message *m_parent; /* parent of this message */
1267 unsigned m_level; /* thread level of message */
1268 long m_threadpos; /* position in threaded display */
1269 #ifdef HAVE_IMAP
1270 unsigned long m_uid; /* IMAP unique identifier */
1271 #endif
1272 char *m_maildir_file; /* original maildir file of msg */
1273 ui32_t m_maildir_hash; /* hash of file name in maildir sub */
1274 int m_collapsed; /* collapsed thread information */
1277 /* Given a file address, determine the block number it represents */
1278 #define mailx_blockof(off) ((int) ((off) / 4096))
1279 #define mailx_offsetof(off) ((int) ((off) % 4096))
1280 #define mailx_positionof(block, offset) ((off_t)(block) * 4096 + (offset))
1282 /* Argument types */
1283 enum argtype {
1284 ARG_MSGLIST = 0, /* Message list type */
1285 ARG_STRLIST = 1, /* A pure string */
1286 ARG_RAWLIST = 2, /* Shell string list */
1287 ARG_NOLIST = 3, /* Just plain 0 */
1288 ARG_NDMLIST = 4, /* Message list, no defaults */
1289 ARG_ECHOLIST = 5, /* Like raw list, but keep quote chars */
1290 ARG_ARGMASK = 7, /* Mask of the above */
1292 ARG_A = 1u<< 4, /* Needs an active mailbox */
1293 ARG_F = 1u<< 5, /* Is a conditional command */
1294 ARG_H = 1u<< 6, /* Never place in history */
1295 ARG_I = 1u<< 7, /* Interactive command bit */
1296 ARG_M = 1u<< 8, /* Legal from send mode bit */
1297 ARG_P = 1u<< 9, /* Autoprint dot after command */
1298 ARG_R = 1u<<10, /* Cannot be called from collect / recursion */
1299 ARG_T = 1u<<11, /* Is a transparent command */
1300 ARG_V = 1u<<12, /* Places data in temporary_arg_v_store */
1301 ARG_W = 1u<<13 /* Invalid when read only bit */
1304 enum gfield {
1305 GTO = 1, /* Grab To: line */
1306 GSUBJECT = 2, /* Likewise, Subject: line */
1307 GCC = 4, /* And the Cc: line */
1308 GBCC = 8, /* And also the Bcc: line */
1310 GNL = 16, /* Print blank line after */
1311 GDEL = 32, /* Entity removed from list */
1312 GCOMMA = 64, /* detract puts in commas */
1313 GUA = 128, /* User-Agent field */
1314 GMIME = 256, /* MIME 1.0 fields */
1315 GMSGID = 512, /* a Message-ID */
1316 /* 1024 */
1317 GIDENT = 2048, /* From:, Reply-To: and Organization: field */
1318 GREF = 4096, /* References: field */
1319 GDATE = 8192, /* Date: field */
1320 GFULL = 16384, /* include full names */
1321 GSKIN = 32768, /* skin names */
1322 GEXTRA = 65536, /* extra fields */
1323 GFILES = 131072 /* include filename addresses */
1325 #define GMASK (GTO | GSUBJECT | GCC | GBCC)
1327 /* Structure used to pass about the current state of a message (header) */
1328 struct header {
1329 struct name *h_to; /* Dynamic "To:" string */
1330 char *h_subject; /* Subject string */
1331 struct name *h_cc; /* Carbon copies string */
1332 struct name *h_bcc; /* Blind carbon copies */
1333 struct name *h_ref; /* References */
1334 struct attachment *h_attach; /* MIME attachments */
1335 char *h_charset; /* preferred charset */
1336 struct name *h_from; /* overridden "From:" field */
1337 struct name *h_replyto; /* overridden "Reply-To:" field */
1338 struct name *h_sender; /* overridden "Sender:" field */
1339 char *h_organization; /* overridden "Organization:" field */
1342 /* Handling of namelist nodes used in processing the recipients of mail and
1343 * aliases, inspection of mail-addresses and all that kind of stuff */
1344 enum nameflags {
1345 NAME_NAME_SALLOC = 1<< 0, /* .n_name is doped */
1346 NAME_FULLNAME_SALLOC = 1<< 1, /* .n_fullname is doped */
1347 NAME_SKINNED = 1<< 2, /* Is actually skin()ned */
1348 NAME_IDNA = 1<< 3, /* IDNA was applied */
1349 NAME_ADDRSPEC_CHECKED = 1<< 4, /* Address has been .. and */
1350 NAME_ADDRSPEC_ISFILE = 1<< 5, /* ..is a file path */
1351 NAME_ADDRSPEC_ISPIPE = 1<< 6, /* ..is a command for piping */
1352 NAME_ADDRSPEC_ISFILEORPIPE = NAME_ADDRSPEC_ISFILE | NAME_ADDRSPEC_ISPIPE,
1353 NAME_ADDRSPEC_ERR_EMPTY = 1<< 7, /* An empty string (or NULL) */
1354 NAME_ADDRSPEC_ERR_ATSEQ = 1<< 8, /* Weird @ sequence */
1355 NAME_ADDRSPEC_ERR_CHAR = 1<< 9, /* Invalid character */
1356 NAME_ADDRSPEC_ERR_IDNA = 1<<10, /* IDNA convertion failed */
1357 NAME_ADDRSPEC_INVALID = NAME_ADDRSPEC_ERR_EMPTY |
1358 NAME_ADDRSPEC_ERR_ATSEQ | NAME_ADDRSPEC_ERR_CHAR |
1359 NAME_ADDRSPEC_ERR_IDNA,
1361 _NAME_SHIFTWC = 11,
1362 _NAME_MAXWC = 0xFFFFF,
1363 _NAME_MASKWC = _NAME_MAXWC << _NAME_SHIFTWC
1366 /* In the !_ERR_EMPTY case, the failing character can be queried */
1367 #define NAME_ADDRSPEC_ERR_GETWC(F) \
1368 ((((unsigned int)(F) & _NAME_MASKWC) >> _NAME_SHIFTWC) & _NAME_MAXWC)
1369 #define NAME_ADDRSPEC_ERR_SET(F, E, WC) \
1370 do {\
1371 (F) = ((F) & ~(NAME_ADDRSPEC_INVALID | _NAME_MASKWC)) |\
1372 (E) | (((unsigned int)(WC) & _NAME_MAXWC) << _NAME_SHIFTWC);\
1373 } while (0)
1375 struct name {
1376 struct name *n_flink; /* Forward link in list. */
1377 struct name *n_blink; /* Backward list link */
1378 enum gfield n_type; /* From which list it came */
1379 enum nameflags n_flags; /* enum nameflags */
1380 char *n_name; /* This fella's name */
1381 char *n_fullname; /* Sometimes, name including comment */
1384 struct addrguts {
1385 char const *ag_input; /* Input string as given */
1386 size_t ag_ilen; /* strlen() of input */
1387 size_t ag_iaddr_start; /* Start of *addr-spec* in .ag_input */
1388 size_t ag_iaddr_aend; /* ..and one past its end */
1389 char *ag_skinned; /* Output (alloced if !=.ag_input) */
1390 size_t ag_slen; /* strlen() of .ag_skinned */
1391 size_t ag_sdom_start; /* Start of domain in .ag_skinned, */
1392 enum nameflags ag_n_flags; /* enum nameflags of .ag_skinned */
1395 /* MIME attachments */
1396 enum attach_conv {
1397 AC_DEFAULT, /* _get_lc() -> _iter_*() */
1398 AC_FIX_OUTCS, /* _get_lc() -> "charset=" .a_charset */
1399 AC_FIX_INCS, /* "charset=".a_input_charset (nocnv) */
1400 AC_TMPFILE /* attachment.a_tmpf is converted */
1403 struct attachment {
1404 struct attachment *a_flink; /* Forward link in list. */
1405 struct attachment *a_blink; /* Backward list link */
1406 char const *a_name; /* file name */
1407 char const *a_content_type; /* content type */
1408 char const *a_content_disposition; /* content disposition */
1409 char const *a_content_id; /* content id */
1410 char const *a_content_description; /* content description */
1411 char const *a_input_charset; /* Interpretation depends on .a_conv */
1412 char const *a_charset; /* ... */
1413 FILE *a_tmpf; /* If AC_TMPFILE */
1414 enum attach_conv a_conv; /* User chosen conversion */
1415 int a_msgno; /* message number */
1418 struct sendbundle {
1419 struct header *sb_hp;
1420 struct name *sb_to;
1421 FILE *sb_input;
1422 struct str sb_signer; /* USER@HOST for signing+ */
1423 struct url sb_url;
1424 struct ccred sb_ccred;
1427 struct group {
1428 struct group *ge_link; /* Next person in this group */
1429 char *ge_name; /* This person's user name */
1432 struct grouphead {
1433 struct grouphead *g_link; /* Next grouphead in list */
1434 char *g_name; /* Name of this group */
1435 struct group *g_list; /* Users in group. */
1438 /* Structure of the hash table of ignored header fields */
1439 struct ignoretab {
1440 int i_count; /* Number of entries */
1441 struct ignore {
1442 struct ignore *i_link; /* Next ignored field in bucket */
1443 char *i_field; /* This ignored field */
1444 } *i_head[HSHSIZE];
1447 /* For the 'shortcut' and 'unshortcut' functionality */
1448 struct shortcut {
1449 struct shortcut *sh_next; /* next shortcut in list */
1450 char *sh_short; /* shortcut string */
1451 char *sh_long; /* expanded form */
1454 /* For saving the current directory and later returning */
1455 struct cw {
1456 #ifdef HAVE_FCHDIR
1457 int cw_fd;
1458 #else
1459 char cw_wd[PATH_MAX];
1460 #endif
1464 * Global variable declarations
1466 * These become instantiated in main.c.
1469 #undef VL
1470 #ifdef _MAIN_SOURCE
1471 # ifndef HAVE_AMALGAMATION
1472 # define VL
1473 # else
1474 # define VL static
1475 # endif
1476 #else
1477 # define VL extern
1478 #endif
1480 VL gid_t effectivegid; /* Saved from when we started up */
1481 VL gid_t realgid; /* Saved from when we started up */
1483 VL int mb_cur_max; /* Value of MB_CUR_MAX */
1484 VL int realscreenheight; /* The real screen height */
1485 VL int scrnwidth; /* Screen width, or best guess */
1486 VL int scrnheight; /* Screen height/guess (4 header) */
1487 VL int enc_has_state; /* Encoding has shift states */
1489 VL char **altnames; /* List of alternate names of user */
1490 VL char const *homedir; /* Path name of home directory */
1491 VL char const *myname; /* My login name */
1492 VL char const *progname; /* Our name */
1493 VL char const *tempdir; /* The temporary directory */
1495 VL int exit_status; /* Exit status */
1496 VL int options; /* Bits of enum user_options */
1497 VL char *option_r_arg; /* Argument to -r option */
1498 VL char const **smopts; /* sendmail(1) opts from commline */
1499 VL size_t smopts_count; /* Entries in smopts */
1501 /* TODO Join as many of these state machine bits into a single carrier! */
1502 VL int inhook; /* Currently executing a hook */
1503 VL bool_t exec_last_comm_error; /* Last execute() command failed */
1504 VL bool_t edit; /* Indicates editing a file */
1505 VL bool_t did_print_dot; /* Current message has been printed */
1506 VL bool_t list_saw_numbers; /* Last *LIST saw numerics */
1507 VL bool_t msglist_is_single; /* Last NDMLIST/MSGLIST chose 1 msg */
1508 VL bool_t loading; /* Loading user definitions */
1509 VL bool_t sourcing; /* Currently reading variant file */
1510 VL bool_t sawcom; /* Set after first command */
1511 VL bool_t starting; /* Still in startup code */
1512 VL bool_t var_clear_allow_undefined; /* v?ok_[bv]clear(): no complain */
1513 VL int noreset; /* String resets suspended */
1515 /* XXX stylish sorting */
1516 VL int msgCount; /* Count of messages read in */
1517 VL struct mailbox mb; /* Current mailbox */
1518 VL int image; /* File descriptor for msg image */
1519 VL char mailname[PATH_MAX]; /* Name of current file TODO URL/object*/
1520 VL char displayname[80 - 40]; /* Prettyfied for display TODO URL/obj*/
1521 VL char prevfile[PATH_MAX]; /* Name of previous file TODO URL/obj */
1522 VL char const *account_name; /* Current account name or NULL */
1523 VL off_t mailsize; /* Size of system mailbox */
1524 VL struct message *dot; /* Pointer to current message */
1525 VL struct message *prevdot; /* Previous current message */
1526 VL struct message *message; /* The actual message structure */
1527 VL struct message *threadroot; /* first threaded message */
1528 VL int imap_created_mailbox; /* hack to get feedback from imap */
1530 VL struct grouphead *groups[HSHSIZE]; /* Pointer to active groups */
1531 VL struct ignoretab ignore[2]; /* ignored and retained fields
1532 * 0 is ignore, 1 is retain */
1533 VL struct ignoretab saveignore[2]; /* ignored and retained fields
1534 * on save to folder */
1535 VL struct ignoretab allignore[2]; /* special, ignore all headers */
1536 VL struct ignoretab fwdignore[2]; /* fields to ignore for forwarding */
1537 VL struct shortcut *shortcuts; /* list of shortcuts */
1539 VL struct time_current time_current; /* time(3); send: mail1() XXXcarrier */
1540 VL struct termios_state termios_state; /* getpassword(); see commands().. */
1542 #ifdef HAVE_COLOUR
1543 VL struct colour_table *colour_table;
1544 #endif
1546 #ifdef HAVE_SSL
1547 VL enum ssl_verify_level ssl_verify_level; /* SSL verification level */
1548 #endif
1550 #ifdef HAVE_ICONV
1551 VL iconv_t iconvd;
1552 #endif
1554 VL sigjmp_buf srbuf;
1555 VL int interrupts;
1556 VL sighandler_type dflpipe;
1557 VL sighandler_type handlerstacktop;
1558 #define handlerpush(f) (savedtop = handlerstacktop, handlerstacktop = (f))
1559 #define handlerpop() (handlerstacktop = savedtop)
1561 /* TODO Temporary hacks unless the codebase doesn't jump and uses pass-by-value
1562 * TODO carrier structs instead of locals */
1563 VL char *temporary_arg_v_store;
1564 VL void *temporary_localopts_store;
1565 /* TODO temporary storage to overcome which_protocol() mess (for PROTO_FILE) */
1566 VL char const *temporary_protocol_ext;
1568 /* The remaining variables need initialization */
1570 #ifndef HAVE_AMALGAMATION
1571 VL char const month_names[12 + 1][4];
1572 VL char const weekday_names[7 + 1][4];
1574 VL char const uagent[]; /* User agent */
1575 VL char const version[]; /* The version string */
1576 VL char const features[]; /* The "feature string" */
1578 VL uc_it const class_char[];
1579 #endif
1582 * Finally, let's include the function prototypes XXX embed
1585 #include "nailfuns.h"
1587 /* s-it-mode */