3 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
4 * Copyright (C) 2004 g10 Code GmbH
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
29 # include <unistd.h> /* needed for SEEK_SET */
32 # include <unix.h> /* needed for snprintf on QNX. */
34 #include <sys/types.h>
43 #if defined(HAVE_WCTYPE_H) && defined(HAVE_WC_FUNCS)
47 #ifndef _POSIX_PATH_MAX
48 #include <posix1_lim.h>
62 # define MB_LEN_MAX 16
66 # define MUTT_VERSION (VERSION SUBVERSION)
68 # define MUTT_VERSION (VERSION)
71 /* nifty trick I stole from ELM 2.5alpha. */
74 #define INITVAL(x) = x
80 #include "mutt_regex.h"
82 /* flags for mutt_copy_header() */
83 #define CH_UPDATE 1 /* update the status and x-status fields? */
84 #define CH_WEED (1<<1) /* weed the headers? */
85 #define CH_DECODE (1<<2) /* do RFC1522 decoding? */
86 #define CH_XMIT (1<<3) /* transmitting this message? */
87 #define CH_FROM (1<<4) /* retain the "From " message separator? */
88 #define CH_PREFIX (1<<5) /* use Prefix string? */
89 #define CH_NOSTATUS (1<<6) /* supress the status and x-status fields */
90 #define CH_REORDER (1<<7) /* Re-order output of headers */
91 #define CH_NONEWLINE (1<<8) /* don't output terminating newline */
92 #define CH_MIME (1<<9) /* ignore MIME fields */
93 #define CH_UPDATE_LEN (1<<10) /* update Lines: and Content-Length: */
94 #define CH_TXTPLAIN (1<<11) /* generate text/plain MIME headers */
95 #define CH_NOLEN (1<<12) /* don't write Content-Length: and Lines: */
96 #define CH_WEED_DELIVERED (1<<13) /* weed eventual Delivered-To headers */
97 #define CH_FORCE_FROM (1<<14) /* give CH_FROM precedence over CH_WEED? */
98 #define CH_NOQFROM (1<<15) /* give CH_FROM precedence over CH_WEED? */
99 #define CH_UPDATE_IRT (1<<16) /* update In-Reply-To: */
100 #define CH_UPDATE_REFS (1<<17) /* update References: */
102 /* flags for mutt_enter_string() */
103 #define M_ALIAS 1 /* do alias "completion" by calling up the alias-menu */
104 #define M_FILE (1<<1) /* do file completion */
105 #define M_EFILE (1<<2) /* do file completion, plus incoming folders */
106 #define M_CMD (1<<3) /* do completion on previous word */
107 #define M_PASS (1<<4) /* password mode (no echo) */
108 #define M_CLEAR (1<<5) /* clear input if printable character is pressed */
109 #define M_COMMAND (1<<6) /* do command completion */
110 #define M_PATTERN (1<<7) /* pattern mode - only used for history classes */
112 /* flags for mutt_get_token() */
113 #define M_TOKEN_EQUAL 1 /* treat '=' as a special */
114 #define M_TOKEN_CONDENSE (1<<1) /* ^(char) to control chars (macros) */
115 #define M_TOKEN_SPACE (1<<2) /* don't treat whitespace as a term */
116 #define M_TOKEN_QUOTE (1<<3) /* don't interpret quotes */
117 #define M_TOKEN_PATTERN (1<<4) /* !)|~ are terms (for patterns) */
118 #define M_TOKEN_COMMENT (1<<5) /* don't reap comments */
119 #define M_TOKEN_SEMICOLON (1<<6) /* don't treat ; as special */
121 /* flags for km_dokey() */
122 #define M_KM_UNBUFFERED 1 /* don't read from the key buffer */
126 char *data; /* pointer to data */
127 char *dptr; /* current read/write position */
128 size_t dsize; /* length of data */
129 int destroy; /* destroy `data' when done? */
134 int ch; /* raw key pressed */
135 int op; /* function op */
138 /* flags for _mutt_system() */
139 #define M_DETACH_PROCESS 1 /* detach subprocess from group */
141 /* flags for mutt_FormatString() */
144 M_FORMAT_FORCESUBJ = (1<<0), /* print the subject even if unchanged */
145 M_FORMAT_TREE = (1<<1), /* draw the thread tree */
146 M_FORMAT_MAKEPRINT = (1<<2), /* make sure that all chars are printable */
147 M_FORMAT_OPTIONAL = (1<<3),
148 M_FORMAT_STAT_FILE = (1<<4), /* used by mutt_attach_fmt */
149 M_FORMAT_ARROWCURSOR = (1<<5), /* reserve space for arrow_cursor */
150 M_FORMAT_INDEX = (1<<6) /* this is a main index entry */
153 /* types for mutt_add_hook() */
154 #define M_FOLDERHOOK 1
155 #define M_MBOXHOOK (1<<1)
156 #define M_SENDHOOK (1<<2)
157 #define M_FCCHOOK (1<<3)
158 #define M_SAVEHOOK (1<<4)
159 #define M_CHARSETHOOK (1<<5)
160 #define M_ICONVHOOK (1<<6)
161 #define M_MESSAGEHOOK (1<<7)
162 #define M_CRYPTHOOK (1<<8)
163 #define M_ACCOUNTHOOK (1<<9)
164 #define M_REPLYHOOK (1<<10)
165 #define M_SEND2HOOK (1<<11)
167 #ifdef USE_COMPRESSED
168 #define M_OPENHOOK (1<<12)
169 #define M_APPENDHOOK (1<<13)
170 #define M_CLOSEHOOK (1<<14)
173 /* tree characters for linearize_tree and print_enriched_string */
174 #define M_TREE_LLCORNER 1
175 #define M_TREE_ULCORNER 2
176 #define M_TREE_LTEE 3
177 #define M_TREE_HLINE 4
178 #define M_TREE_VLINE 5
179 #define M_TREE_SPACE 6
180 #define M_TREE_RARROW 7
181 #define M_TREE_STAR 8
182 #define M_TREE_HIDDEN 9
183 #define M_TREE_EQUALS 10
184 #define M_TREE_TTEE 11
185 #define M_TREE_BTEE 12
186 #define M_TREE_MISSING 13
187 #define M_TREE_MAX 14
189 #define M_THREAD_COLLAPSE (1<<0)
190 #define M_THREAD_UNCOLLAPSE (1<<1)
191 #define M_THREAD_GET_HIDDEN (1<<2)
192 #define M_THREAD_UNREAD (1<<3)
193 #define M_THREAD_NEXT_UNREAD (1<<4)
197 /* modes for mutt_view_attachment() */
202 /* action codes used by mutt_set_flag() and mutt_pattern_function() */
222 /* actions for mutt_pattern_comp/mutt_pattern_exec */
258 /* Options for Mailcap lookup */
264 /* options for socket code */
270 /* Options for mutt_save_attachment */
275 /* possible arguments to set_quadoption() */
284 /* quad-option vars */
300 OPT_PGPMIMEAUTO, /* ask to revert to PGP/MIME when inline fails */
310 #if defined(USE_SSL) || defined(USE_GNUTLS)
314 OPT_VERIFYSIG, /* verify PGP signatures */
320 OPT_FOLLOWUPTOPOSTER,
321 #endif /* USE_NNTP */
323 /* THIS MUST BE THE LAST VALUE. */
327 /* flags to ci_send_message() */
328 #define SENDREPLY (1<<0)
329 #define SENDGROUPREPLY (1<<1)
330 #define SENDLISTREPLY (1<<2)
331 #define SENDFORWARD (1<<3)
332 #define SENDPOSTPONED (1<<4)
333 #define SENDBATCH (1<<5)
334 #define SENDMAILX (1<<6)
335 #define SENDKEY (1<<7)
336 #define SENDRESEND (1<<8)
337 #define SENDNEWS (1<<9)
339 /* flags to _mutt_select_file() */
340 #define M_SEL_BUFFY (1<<0)
341 #define M_SEL_MULTI (1<<1)
342 #define M_SEL_FOLDER (1<<2)
344 /* flags for parse_spam_list */
391 OPTHIDETHREADSUBJECT,
394 OPTIGNORELISTREPLYTO,
400 # if defined(USE_SSL) || defined(USE_GNUTLS)
404 #if defined(USE_SSL) || defined(USE_NSS) || defined(USE_GNUTLS)
422 OPTMENUSCROLL, /* scroll menu instead of implicit next-page */
423 OPTMETAKEY, /* interpret ALT-x as ESC-x */
428 OPTMIMESUBJECT, /* encode subject line with RFC2047 */
470 #ifdef HAVE_GETADDRINFO
477 OPTWRITEBCC, /* write out a bcc header? */
489 OPTCRYPTREPLYENCRYPT,
491 OPTCRYPTREPLYSIGNENCRYPTED,
495 OPTSDEFAULTDECRYPTKEY,
517 #endif /* USE_NNTP */
521 OPTAUXSORT, /* (pseudo) using auxillary sort function */
522 OPTFORCEREFRESH, /* (pseudo) refresh even during macros */
523 OPTLOCALES, /* (pseudo) set if user has valid locale definition */
524 OPTNOCURSES, /* (pseudo) when sending in batch mode */
525 OPTNEEDREDRAW, /* (pseudo) to notify caller of a submenu */
526 OPTSEARCHREVERSE, /* (pseudo) used by ci_search_command */
527 OPTMSGERR, /* (pseudo) used by mutt_error/mutt_message */
528 OPTSEARCHINVALID, /* (pseudo) used to invalidate the search pat */
529 OPTSIGNALSBLOCKED, /* (pseudo) using by mutt_block_signals () */
530 OPTSYSSIGNALSBLOCKED, /* (pseudo) using by mutt_block_signals_system () */
531 OPTNEEDRESORT, /* (pseudo) used to force a re-sort */
532 OPTRESORTINIT, /* (pseudo) used to force the next resort to be from scratch */
533 OPTVIEWATTACH, /* (pseudo) signals that we are viewing attachments */
534 OPTFORCEREDRAWINDEX, /* (pseudo) used to force a redraw in the main index */
535 OPTFORCEREDRAWPAGER, /* (pseudo) used to force a redraw in the pager */
536 OPTSORTSUBTHREADS, /* (pseudo) used when $sort_aux changes */
537 OPTNEEDRESCORE, /* (pseudo) set when the `score' command is used */
538 OPTATTACHMSG, /* (pseudo) used by attach-message */
539 OPTHIDEREAD, /* (pseudo) whether or not hide read messages */
540 OPTKEEPQUIET, /* (pseudo) shut up the message and refresh
541 * functions while we are executing an
544 OPTMENUCALLER, /* (pseudo) tell menu to give caller a take */
545 OPTREDRAWTREE, /* (pseudo) redraw the thread tree */
546 OPTPGPCHECKTRUST, /* (pseudo) used by pgp_select_key () */
547 OPTDONTHANDLEPGPKEYS, /* (pseudo) used to extract PGP keys */
548 OPTUNBUFFEREDINPUT, /* (pseudo) don't use key buffer */
551 OPTNEWS, /* (pseudo) used to change reader mode */
552 OPTNEWSSEND, /* (pseudo) used to change behavior when posting */
553 OPTNEWSCACHE, /* (pseudo) used to indicate if news cache exist */
559 #define mutt_bit_alloc(n) calloc ((n + 7) / 8, sizeof (char))
560 #define mutt_bit_set(v,n) v[n/8] |= (1 << (n % 8))
561 #define mutt_bit_unset(v,n) v[n/8] &= ~(1 << (n % 8))
562 #define mutt_bit_toggle(v,n) v[n/8] ^= (1 << (n % 8))
563 #define mutt_bit_isset(v,n) (v[n/8] & (1 << (n % 8)))
565 #define set_option(x) mutt_bit_set(Options,x)
566 #define unset_option(x) mutt_bit_unset(Options,x)
567 #define toggle_option(x) mutt_bit_toggle(Options,x)
568 #define option(x) mutt_bit_isset(Options,x)
570 /* Exit values used in send_msg() */
574 typedef struct list_t
580 typedef struct rx_list_t
583 struct rx_list_t *next;
586 typedef struct spam_list_t
591 struct spam_list_t *next;
595 #define mutt_new_list() safe_calloc (1, sizeof (LIST))
596 #define mutt_new_spam_list() safe_calloc (1, sizeof (SPAM_LIST))
597 #define mutt_new_rx_list() safe_calloc (1, sizeof (RX_LIST))
598 void mutt_free_list (LIST **);
599 void mutt_free_rx_list (RX_LIST **);
600 void mutt_free_spam_list (SPAM_LIST **);
601 LIST *mutt_copy_list (LIST *);
602 int mutt_matches_ignore (const char *, LIST *);
603 /* add an element to a list */
604 LIST *mutt_add_list (LIST *, const char *);
606 void mutt_init (int, LIST *);
610 struct alias *self; /* XXX - ugly hack */
619 typedef struct envelope
621 ADDRESS *return_path;
628 ADDRESS *mail_followup_to;
629 char *list_post; /* this stores a mailto URL, or nothing */
631 char *real_subj; /* offset of the real subject */
644 LIST *references; /* message references (in reverse order) */
645 LIST *in_reply_to; /* in-reply-to header content */
646 LIST *userhdrs; /* user defined headers */
649 typedef struct parameter
653 struct parameter *next;
656 /* Information that helps in determing the Content-* of an attachment */
657 typedef struct content
659 long hibin; /* 8-bit characters */
660 long lobin; /* unprintable 7-bit chars (eg., control chars) */
661 long crlf; /* '\r' and '\n' characters */
662 long ascii; /* number of ascii chars */
663 long linemax; /* length of the longest line in the file */
664 unsigned int space : 1; /* whitespace at the end of lines? */
665 unsigned int binary : 1; /* long lines, or CR not in CRLF pair */
666 unsigned int from : 1; /* has a line beginning with "From "? */
667 unsigned int dot : 1; /* has a line consisting of a single dot? */
668 unsigned int cr : 1; /* has CR, even when in a CRLF pair */
673 char *xtype; /* content-type if x-unknown */
674 char *subtype; /* content-type subtype */
675 PARAMETER *parameter; /* parameters of the content-type */
676 char *description; /* content-description */
677 char *form_name; /* Content-Disposition form-data name param */
678 long hdr_offset; /* offset in stream where the headers begin.
679 * this info is used when invoking metamail,
680 * where we need to send the headers of the
683 long offset; /* offset where the actual data begins */
684 long length; /* length (in bytes) of attachment */
685 char *filename; /* when sending a message, this is the file
686 * to which this structure refers
688 char *d_filename; /* filename to be used for the
689 * content-disposition header.
690 * If NULL, filename is used
693 CONTENT *content; /* structure used to store detailed info about
694 * the content of the attachment. this is used
695 * to determine what content-transfer-encoding
696 * is required when sending mail.
698 struct body *next; /* next attachment in the list */
699 struct body *parts; /* parts of a multipart or message/rfc822 */
700 struct header *hdr; /* header information for message/rfc822 */
702 struct attachptr *aptr; /* Menu information, used in recvattach.c */
704 time_t stamp; /* time stamp of last
708 unsigned int type : 4; /* content-type primary type */
709 unsigned int encoding : 3; /* content-transfer-encoding */
710 unsigned int disposition : 2; /* content-disposition */
711 unsigned int use_disp : 1; /* Content-Disposition uses filename= ? */
712 unsigned int unlink : 1; /* flag to indicate the the file named by
713 * "filename" should be unlink()ed before
714 * free()ing this structure
716 unsigned int tagged : 1;
717 unsigned int deleted : 1; /* attachment marked for deletion */
719 unsigned int noconv : 1; /* don't do character set conversion */
720 unsigned int force_charset : 1;
721 /* send mode: don't adjust the character
722 * set when in send-mode.
724 unsigned int is_signed_data : 1; /* A lot of MUAs don't indicate
725 S/MIME signed-data correctly,
726 e.g. they use foo.p7m even for
727 the name of signed data. This
728 flag is used to keep track of
729 the actual message type. It
730 gets set during the verification
731 (which is done if the encryption
732 try failed) and check by the
733 function to figure the type of
736 unsigned int goodsig : 1; /* good cryptographic signature */
737 unsigned int warnsig : 1; /* maybe good signature */
738 unsigned int badsig : 1; /* bad cryptographic signature (needed to check encrypted s/mime-signatures) */
740 unsigned int collapsed : 1; /* used by recvattach */
744 typedef struct header
746 unsigned int security : 11; /* bit 0-6: flags, bit 7,8: application.
747 see: crypt.h pgplib.h, smime.h */
749 unsigned int mime : 1; /* has a Mime-Version header? */
750 unsigned int flagged : 1; /* marked important? */
751 unsigned int tagged : 1;
752 unsigned int appended : 1; /* has been saved */
753 unsigned int purged : 1; /* bypassing the trash folder */
754 unsigned int deleted : 1;
755 unsigned int changed : 1;
756 unsigned int attach_del : 1; /* has an attachment marked for deletion */
757 unsigned int old : 1;
758 unsigned int read : 1;
759 unsigned int expired : 1; /* already expired? */
760 unsigned int superseded : 1; /* got superseded? */
761 unsigned int replied : 1;
762 unsigned int subject_changed : 1; /* used for threading */
763 unsigned int threaded : 1; /* used for threading */
764 unsigned int display_subject : 1; /* used for threading */
765 unsigned int irt_changed : 1; /* In-Reply-To changed to link/break threads */
766 unsigned int refs_changed : 1; /* References changed to break thread */
767 unsigned int recip_valid : 1; /* is_recipient is valid */
768 unsigned int active : 1; /* message is not to be removed */
769 unsigned int trash : 1; /* message is marked as trashed on disk.
770 * This flag is used by the maildir_trash
774 /* timezone of the sender of this message */
775 unsigned int zhours : 5;
776 unsigned int zminutes : 6;
777 unsigned int zoccident : 1;
779 /* bits used for caching when searching */
780 unsigned int searched : 1;
781 unsigned int matched : 1;
783 /* the following are used to support collapsing threads */
784 unsigned int collapsed : 1; /* is this message part of a collapsed thread? */
785 unsigned int limited : 1; /* is this message in a limited view? */
786 size_t num_hidden; /* number of hidden messages in this view */
788 short recipient; /* user_is_recipient()'s return value, cached */
790 int pair; /* color-pair to use when displaying in the index */
792 time_t date_sent; /* time when the message was sent (UTC) */
793 time_t received; /* time when the message was placed in the mailbox */
794 long offset; /* where in the stream does this message begin? */
795 int lines; /* how many lines in the body of this message? */
796 int index; /* the absolute (unsorted) message number */
797 int msgno; /* number displayed to the user */
798 int virtual; /* virtual message number */
800 ENVELOPE *env; /* envelope information */
801 BODY *content; /* list of MIME parts */
807 char *tree; /* character string to print thread tree */
808 struct thread *thread;
810 #ifdef IMAP_EDIT_THREADS
811 ENVELOPE *new_env; /* envelope information for rethreading */
819 int refno; /* message number on server */
822 #if defined USE_POP || defined USE_IMAP || defined USE_NNTP
823 void *data; /* driver-specific data */
826 char *maildir_flags; /* unknown maildir flags */
829 typedef struct thread
831 unsigned int fake_thread : 1;
832 unsigned int duplicate_thread : 1;
833 unsigned int sort_children : 1;
834 unsigned int check_subject : 1;
835 unsigned int visible : 1;
836 unsigned int deep : 1;
837 unsigned int subtree_visible : 2;
838 unsigned int next_subtree_visible : 1;
839 struct thread *parent;
840 struct thread *child;
848 /* flag to mutt_pattern_comp() */
849 #define M_FULL_MSG 1 /* enable body and header matching */
852 M_MATCH_FULL_ADDRESS = 1
855 typedef struct pattern_t
862 struct pattern_t *next;
863 struct pattern_t *child; /* arguments to logical op */
872 time_t mtime_cur; /* used with maildir folders */
875 char *pattern; /* limit pattern string */
876 pattern_t *limit_pattern; /* compiled limit pattern */
878 HEADER *last_tag; /* last tagged msg. used to link threads */
879 THREAD *tree; /* top of thread tree */
880 HASH *id_hash; /* hash table by msg id */
881 HASH *subj_hash; /* hash table by subject */
882 HASH *thread_hash; /* hash table for threading */
883 int *v2r; /* mapping from virtual to real msgno */
884 int hdrmax; /* number of pointers in hdrs */
885 int msgcount; /* number of messages in the mailbox */
886 int vcount; /* the number of virtual messages */
887 int tagged; /* how many messages are tagged? */
888 int new; /* how many new messages? */
889 int unread; /* how many unread messages? */
890 int deleted; /* how many deleted messages */
891 int appended; /* how many saved messages? */
892 int flagged; /* how many flagged messages */
893 int msgnotreadyet; /* which msg "new" in pager, -1 if none */
894 #if defined USE_POP || defined USE_IMAP || defined USE_NNTP
895 void *data; /* driver specific data */
896 #endif /* USE_IMAP */
898 short magic; /* mailbox type */
900 #ifdef USE_COMPRESSED
901 void *compressinfo; /* compressed mbox module private data */
902 char *realpath; /* path to compressed mailbox */
903 #endif /* USE_COMPRESSED */
905 unsigned int locked : 1; /* is the mailbox locked? */
906 unsigned int changed : 1; /* mailbox has been modified */
907 unsigned int readonly : 1; /* don't allow changes to the mailbox */
908 unsigned int dontwrite : 1; /* dont write the mailbox on close */
909 unsigned int append : 1; /* mailbox is opened in append mode */
910 unsigned int quiet : 1; /* inhibit status messages? */
911 unsigned int collapsed : 1; /* are all threads collapsed? */
912 unsigned int closing : 1; /* mailbox is being closed */
915 typedef struct attachptr
932 /* used by enter.c */
944 /* flags for the STATE struct */
945 #define M_DISPLAY (1<<0) /* output is displayed to the user */
946 #define M_VERIFY (1<<1) /* perform signature verification */
947 #define M_PENDINGPREFIX (1<<2) /* prefix to write, but character must follow */
948 #define M_WEED (1<<3) /* weed headers even when not in display mode */
949 #define M_CHARCONV (1<<4) /* Do character set conversions */
950 #define M_PRINTING (1<<5) /* are we printing? - M_DISPLAY "light" */
951 #define M_REPLYING (1<<6) /* are we replying? */
952 #define M_FIRSTDONE (1<<7) /* the first attachment has been done */
954 #define state_set_prefix(s) ((s)->flags |= M_PENDINGPREFIX)
955 #define state_reset_prefix(s) ((s)->flags &= ~M_PENDINGPREFIX)
956 #define state_puts(x,y) fputs(x,(y)->fpout)
957 #define state_putc(x,y) fputc(x,(y)->fpout)
959 void state_mark_attach (STATE *);
960 void state_attach_puts (const char *, STATE *);
961 void state_prefix_putc (char, STATE *);
962 int state_printf(STATE *, const char *, ...);