1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Message, message array, n_getmsglist(), and related operations.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2018 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
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. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #define n_FILE message
38 #ifndef HAVE_AMALGAMATION
42 /* Token values returned by the scanner used for argument lists.
43 * Also, sizes of scanner-related things */
45 a_MESSAGE_T_EOL
, /* End of the command line */
46 a_MESSAGE_T_NUMBER
, /* Message number */
47 a_MESSAGE_T_MINUS
, /* - */
48 a_MESSAGE_T_STRING
, /* A string (possibly containing -) */
49 a_MESSAGE_T_DOT
, /* . */
50 a_MESSAGE_T_UP
, /* ^ */
51 a_MESSAGE_T_DOLLAR
, /* $ */
52 a_MESSAGE_T_ASTER
, /* * */
53 a_MESSAGE_T_OPEN
, /* ( */
54 a_MESSAGE_T_CLOSE
, /* ) */
55 a_MESSAGE_T_PLUS
, /* + */
56 a_MESSAGE_T_COMMA
, /* , */
57 a_MESSAGE_T_SEMI
, /* ; */
58 a_MESSAGE_T_BACK
, /* ` */
59 a_MESSAGE_T_ERROR
/* Lexical error */
62 enum a_message_idfield
{
63 a_MESSAGE_ID_REFERENCES
,
64 a_MESSAGE_ID_IN_REPLY_TO
68 a_MESSAGE_S_NEW
= 1u<<0,
69 a_MESSAGE_S_OLD
= 1u<<1,
70 a_MESSAGE_S_UNREAD
= 1u<<2,
71 a_MESSAGE_S_DELETED
= 1u<<3,
72 a_MESSAGE_S_READ
= 1u<<4,
73 a_MESSAGE_S_FLAG
= 1u<<5,
74 a_MESSAGE_S_ANSWERED
= 1u<<6,
75 a_MESSAGE_S_DRAFT
= 1u<<7,
76 a_MESSAGE_S_SPAM
= 1u<<8,
77 a_MESSAGE_S_SPAMUNSURE
= 1u<<9,
78 a_MESSAGE_S_MLIST
= 1u<<10,
79 a_MESSAGE_S_MLSUBSCRIBE
= 1u<<11
82 struct a_message_coltab
{
83 char mco_char
; /* What to find past : */
85 int mco_bit
; /* Associated modifier bit */
86 int mco_mask
; /* m_status bits to mask */
87 int mco_equal
; /* ... must equal this */
95 struct a_message_speclex
{
96 char *msl_str
; /* If parsed a string */
97 int msl_no
; /* If parsed a number TODO siz_t! */
98 char msl__smallstrbuf
[4];
99 /* We directly adjust pointer in .ca_arg.ca_str.s, do not adjust .l */
100 struct n_cmd_arg
*msl_cap
;
101 char const *msl_input_orig
;
104 static struct a_message_coltab
const a_message_coltabs
[] = {
105 {'n', {0,}, a_MESSAGE_S_NEW
, MNEW
, MNEW
},
106 {'o', {0,}, a_MESSAGE_S_OLD
, MNEW
, 0},
107 {'u', {0,}, a_MESSAGE_S_UNREAD
, MREAD
, 0},
108 {'d', {0,}, a_MESSAGE_S_DELETED
, MDELETED
, MDELETED
},
109 {'r', {0,}, a_MESSAGE_S_READ
, MREAD
, MREAD
},
110 {'f', {0,}, a_MESSAGE_S_FLAG
, MFLAGGED
, MFLAGGED
},
111 {'a', {0,}, a_MESSAGE_S_ANSWERED
, MANSWERED
, MANSWERED
},
112 {'t', {0,}, a_MESSAGE_S_DRAFT
, MDRAFTED
, MDRAFTED
},
113 {'s', {0,}, a_MESSAGE_S_SPAM
, MSPAM
, MSPAM
},
114 {'S', {0,}, a_MESSAGE_S_SPAMUNSURE
, MSPAMUNSURE
, MSPAMUNSURE
},
115 /* These have no per-message flags, but must be evaluated */
116 {'l', {0,}, a_MESSAGE_S_MLIST
, 0, 0},
117 {'L', {0,}, a_MESSAGE_S_MLSUBSCRIBE
, 0, 0},
120 static struct a_message_lex
const a_message_singles
[] = {
121 {'$', a_MESSAGE_T_DOLLAR
},
122 {'.', a_MESSAGE_T_DOT
},
123 {'^', a_MESSAGE_T_UP
},
124 {'*', a_MESSAGE_T_ASTER
},
125 {'-', a_MESSAGE_T_MINUS
},
126 {'+', a_MESSAGE_T_PLUS
},
127 {'(', a_MESSAGE_T_OPEN
},
128 {')', a_MESSAGE_T_CLOSE
},
129 {',', a_MESSAGE_T_COMMA
},
130 {';', a_MESSAGE_T_SEMI
},
131 {'`', a_MESSAGE_T_BACK
}
134 /* Slots in ::message */
135 static size_t a_message_mem_space
;
137 /* Mark entire threads */
138 static bool_t a_message_threadflag
;
140 /* :d on its way HACK TODO */
141 static bool_t a_message_list_saw_d
, a_message_list_last_saw_d
;
143 /* Lazy load message header fields */
144 static enum okay
a_message_get_header(struct message
*mp
);
146 /* Append, taking care of resizes TODO vector */
147 static char **a_message_add_to_nmadat(char ***nmadat
, size_t *nmasize
,
148 char **np
, char *string
);
150 /* Mark all messages that the user wanted from the command line in the message
151 * structure. Return 0 on success, -1 on error */
152 static int a_message_markall(char const *orig
, struct n_cmd_arg
*cap
, int f
);
154 /* Turn the character after a colon modifier into a bit value */
155 static int a_message_evalcol(int col
);
157 /* Check the passed message number for legality and proper flags. Unless f is
158 * MDELETED the message has to be undeleted */
159 static bool_t
a_message_check(int mno
, int f
);
161 /* Scan out a single lexical item and return its token number, updating *mslp */
162 static int a_message_scan(struct a_message_speclex
*mslp
);
164 /* See if the passed name sent the passed message */
165 static bool_t
a_message_match_sender(struct message
*mp
, char const *str
,
168 /* Check whether the given message-id or references match */
169 static bool_t
a_message_match_mid(struct message
*mp
, char const *id
,
170 enum a_message_idfield idfield
);
172 /* See if the given string matches.
173 * For the purpose of the scan, we ignore case differences.
174 * This is the engine behind the "/" search */
175 static bool_t
a_message_match_dash(struct message
*mp
, char const *str
);
177 /* See if the given search expression matches.
178 * For the purpose of the scan, we ignore case differences.
179 * This is the engine behind the "@[..@].." search */
180 static bool_t
a_message_match_at(struct message
*mp
, struct search_expr
*sep
);
182 /* Unmark the named message */
183 static void a_message_unmark(int mesg
);
185 /* Return the message number corresponding to the passed meta character */
186 static int a_message_metamess(int meta
, int f
);
188 /* Helper for mark(): self valid, threading enabled */
189 static void a_message__threadmark(struct message
*self
, int f
);
192 a_message_get_header(struct message
*mp
){
204 rv
= pop3_header(mp
);
210 rv
= imap_header(mp
);
223 a_message_add_to_nmadat(char ***nmadat
, size_t *nmasize
, /* TODO Vector */
224 char **np
, char *string
){
228 if((idx
= PTR2SIZE(np
- *nmadat
)) >= *nmasize
){
233 narr
= n_autorec_alloc(i
* sizeof *np
);
234 memcpy(narr
, *nmadat
, i
>>= 1);
244 a_message_markall(char const *orig
, struct n_cmd_arg
*cap
, int f
){
245 struct a_message_speclex msl
;
246 enum a_message_idfield idfield
;
249 char **nmadat_lofi
, **nmadat
, **np
, **nq
, *cp
;
250 struct message
*mp
, *mx
;
251 int i
, valdot
, beg
, colmod
, tok
, colresult
;
254 a_ALLNET
= 1u<<0, /* (CTA()d to be == TRU1 */
255 a_ALLOC
= 1u<<1, /* Have allocated something */
258 a_ANY
= 1u<<4, /* Have marked just ANY */
259 a_RANGE
= 1u<<5, /* Seen dash, await close */
261 a_TOPEN
= 1u<<9, /* ( used (and didn't match) */
262 a_TBACK
= 1u<<10, /* ` used (and didn't match) */
264 a_HAVE_IMAP_HEADERS
= 1u<<14,
266 a_LOG
= 1u<<29, /* Log errors */
270 n_LCTA((ui32_t
)a_ALLNET
== (ui32_t
)TRU1
,
271 "Constant is converted to bool_t via AND, thus");
273 /* Update message array: clear MMARK but remember its former state for ` */
274 for(i
= msgCount
; i
-- > 0;){
277 mf
= (mp
= &message
[i
])->m_flag
;
286 memset(&msl
, 0, sizeof msl
);
288 msl
.msl_input_orig
= orig
;
291 nmadat_lofi
= n_lofi_alloc((nmasize
= 64) * sizeof *np
); /* TODO vector */
293 n_UNINIT(idfield
, a_MESSAGE_ID_REFERENCES
);
294 a_message_threadflag
= FAL0
;
295 valdot
= (int)PTR2SIZE(dot
- message
+ 1);
298 flags
= a_ALLOC
| (mb
.mb_threaded
? a_THREADED
: 0) |
299 ((!(n_pstate
& n_PS_HOOK_MASK
) || (n_poption
& n_PO_D_V
))
302 while((tok
= a_message_scan(&msl
)) != a_MESSAGE_T_EOL
){
303 if((a_message_threadflag
= (tok
< 0)))
307 case a_MESSAGE_T_NUMBER
:
308 n_pstate
|= n_PS_MSGLIST_GABBY
;
310 if(!a_message_check(msl
.msl_no
, f
))
316 if(!(flags
& a_THREADED
)){
324 for(; i
<= msl
.msl_no
; ++i
){
325 mp
= &message
[i
- 1];
326 if(!(mp
->m_flag
& MHIDDEN
) &&
327 (f
== MDELETED
|| !(mp
->m_flag
& MDELETED
))){
333 /* TODO threaded ranges are a mess */
352 mp
= &message
[i
- 1];
353 if(!(mp
->m_flag
& MHIDDEN
) &&
354 (f
== MDELETED
|| !(mp
->m_flag
& MDELETED
))){
361 /* We may have reached the endpoint. If we were still
362 * detecting the direction to search for it, restart.
363 * Otherwise finished */
364 if(i
== msl
.msl_no
){ /* XXX */
373 mx
= (tf
& a_T_DIR_PREV
) ? prev_in_thread(mp
)
374 : next_in_thread(mp
);
376 /* We anyway have failed to reach the endpoint in this
377 * direction; if we already switched that, report error */
378 if(!(tf
& a_T_DIR_PREV
)){
383 id
= N_("Range crosses multiple threads\n");
386 i
= (int)PTR2SIZE(mx
- message
+ 1);
392 /* Could be an inclusive range? */
393 if(msl
.msl_cap
!= NULL
&&
394 msl
.msl_cap
->ca_arg
.ca_str
.s
[0] == '-'){
395 if(*++msl
.msl_cap
->ca_arg
.ca_str
.s
== '\0')
396 msl
.msl_cap
= msl
.msl_cap
->ca_next
;
405 case a_MESSAGE_T_PLUS
:
406 n_pstate
&= ~n_PS_MSGLIST_DIRECT
;
407 n_pstate
|= n_PS_MSGLIST_GABBY
;
410 if(flags
& a_THREADED
){
411 mx
= next_in_thread(&message
[i
- 1]);
412 i
= mx
? (int)PTR2SIZE(mx
- message
+ 1) : msgCount
+ 1;
416 id
= N_("Referencing beyond last message\n");
419 }while(message
[i
- 1].m_flag
== MHIDDEN
||
420 (message
[i
- 1].m_flag
& MDELETED
) != (unsigned)f
);
423 case a_MESSAGE_T_MINUS
:
424 n_pstate
&= ~n_PS_MSGLIST_DIRECT
;
425 n_pstate
|= n_PS_MSGLIST_GABBY
;
428 if(flags
& a_THREADED
){
429 mx
= prev_in_thread(&message
[i
- 1]);
430 i
= mx
? (int)PTR2SIZE(mx
- message
+ 1) : 0;
434 id
= N_("Referencing before first message\n");
437 }while(message
[i
- 1].m_flag
== MHIDDEN
||
438 (message
[i
- 1].m_flag
& MDELETED
) != (unsigned)f
);
441 case a_MESSAGE_T_STRING
:
442 n_pstate
&= ~n_PS_MSGLIST_DIRECT
;
446 /* This may be a colon modifier */
447 if((cp
= msl
.msl_str
)[0] != ':')
448 np
= a_message_add_to_nmadat(&nmadat
, &nmasize
, np
,
449 savestr(msl
.msl_str
));
451 while(*++cp
!= '\0'){
452 colresult
= a_message_evalcol(*cp
);
455 n_err(_("Unknown colon modifier: %s\n"), msl
.msl_str
);
458 if(colresult
== a_MESSAGE_S_DELETED
){
459 a_message_list_saw_d
= TRU1
;
466 case a_MESSAGE_T_OPEN
:
467 n_pstate
&= ~n_PS_MSGLIST_DIRECT
;
472 #ifdef HAVE_IMAP_SEARCH
476 if((ires
= imap_search(msl
.msl_str
, f
)) >= 0){
484 n_err(_("Optional selector not available: %s\n"), msl
.msl_str
);
487 case a_MESSAGE_T_DOLLAR
:
489 case a_MESSAGE_T_SEMI
:
490 n_pstate
|= n_PS_MSGLIST_GABBY
;
492 case a_MESSAGE_T_DOT
: /* Don't set _GABBY for dot to allow history.. */
493 n_pstate
&= ~n_PS_MSGLIST_DIRECT
;
494 if((msl
.msl_no
= a_message_metamess(msl
.msl_str
[0], f
)) == -1)
497 case a_MESSAGE_T_BACK
:
498 n_pstate
&= ~n_PS_MSGLIST_DIRECT
;
503 for(i
= 0; i
< msgCount
; ++i
){
504 if((mp
= &message
[i
])->m_flag
& MHIDDEN
)
506 if((mp
->m_flag
& MDELETED
) != (unsigned)f
){
507 if(!a_message_list_last_saw_d
)
509 a_message_list_saw_d
= TRU1
;
511 if(mp
->m_flag
& MOLDMARK
){
518 case a_MESSAGE_T_ASTER
:
519 n_pstate
&= ~n_PS_MSGLIST_DIRECT
;
524 case a_MESSAGE_T_COMMA
:
525 n_pstate
&= ~n_PS_MSGLIST_DIRECT
;
526 n_pstate
|= n_PS_MSGLIST_GABBY
;
531 if(!(flags
& a_HAVE_IMAP_HEADERS
) && mb
.mb_type
== MB_IMAP
){
532 flags
|= a_HAVE_IMAP_HEADERS
;
533 imap_getheaders(1, msgCount
);
538 if((cp
= hfield1("in-reply-to", dot
)) != NULL
)
539 idfield
= a_MESSAGE_ID_IN_REPLY_TO
;
540 else if((cp
= hfield1("references", dot
)) != NULL
){
543 if((enp
= extract(cp
, GREF
)) != NULL
){
544 while(enp
->n_flink
!= NULL
)
547 idfield
= a_MESSAGE_ID_REFERENCES
;
555 id
= N_("Message-ID of parent of \"dot\" is indeterminable\n");
558 }else if(flags
& a_LOG
)
559 n_err(_("Ignoring redundant specification of , selector\n"));
561 case a_MESSAGE_T_ERROR
:
562 n_pstate
&= ~n_PS_MSGLIST_DIRECT
;
563 n_pstate
|= n_PS_MSGLIST_GABBY
;
567 /* Explicitly disallow invalid ranges for future safety */
568 if(msl
.msl_cap
!= NULL
&& msl
.msl_cap
->ca_arg
.ca_str
.s
[0] == '-' &&
571 n_err(_("Ignoring invalid range in: %s\n"), msl
.msl_input_orig
);
572 if(*++msl
.msl_cap
->ca_arg
.ca_str
.s
== '\0')
573 msl
.msl_cap
= msl
.msl_cap
->ca_next
;
577 id
= N_("Missing second range argument\n");
581 np
= a_message_add_to_nmadat(&nmadat
, &nmasize
, np
, NULL
);
584 /* * is special at this point, after we have parsed the entire line */
586 for(i
= 0; i
< msgCount
; ++i
){
587 if((mp
= &message
[i
])->m_flag
& MHIDDEN
)
589 if(!a_message_list_saw_d
&& (mp
->m_flag
& MDELETED
) != (unsigned)f
)
599 /* If any names were given, add any messages which match */
600 if(np
> nmadat
|| id
!= NULL
){
601 struct search_expr
*sep
;
605 /* The @ search works with struct search_expr, so build an array.
606 * To simplify array, i.e., regex_t destruction, and optimize for the
607 * common case we walk the entire array even in case of errors */
608 /* XXX Like many other things around here: this should be outsourced */
610 j
= PTR2SIZE(np
- nmadat
) * sizeof(*sep
);
611 sep
= n_lofi_alloc(j
);
614 for(j
= 0, nq
= nmadat
; *nq
!= NULL
; ++j
, ++nq
){
617 sep
[j
].ss_body
= x
= xsave
= *nq
;
618 if(*x
!= '@' || (flags
& a_ERROR
))
621 /* Cramp the namelist */
622 for(y
= &x
[1];; ++y
){
632 if(x
== NULL
|| &x
[-1] == xsave
)
634 sep
[j
].ss_field
= "subject";
638 sep
[j
].ss_skin
= TRU1
;
641 n_err(_("[@..]@ search expression: no namelist, "
642 "only \"~\" skin indicator\n"));
647 cp
= savestrbuf(xsave
, PTR2SIZE(x
- xsave
));
649 /* Namelist could be a regular expression, too */
651 if(n_is_maybe_regex(cp
)){
654 assert(sep
[j
].ss_field
== NULL
);
655 if((s
= regcomp(&sep
[j
].ss__fieldre_buf
, cp
,
656 REG_EXTENDED
| REG_ICASE
| REG_NOSUB
)) != 0){
658 n_err(_("Invalid regular expression: %s: %s\n"),
659 n_shexp_quote_cp(cp
, FAL0
),
660 n_regex_err_to_doc(NULL
, s
));
664 sep
[j
].ss_fieldre
= &sep
[j
].ss__fieldre_buf
;
670 /* Because of the special cases we need to trim explicitly
671 * here, they are not covered by n_strsep() */
673 sio
.l
= PTR2SIZE(x
- xsave
);
674 if(*(cp
= n_str_trim(&sio
, n_STR_TRIM_BOTH
)->s
) == '\0')
675 goto jat_where_default
;
676 sep
[j
].ss_field
= cp
;
680 /* The actual search expression. If it is empty we only test the
681 * field(s) for existence */
682 x
= &(x
== NULL
? *nq
: x
)[1];
684 sep
[j
].ss_field_exists
= TRU1
;
686 }else if(n_is_maybe_regex(x
)){
689 sep
[j
].ss_body
= NULL
;
690 if((s
= regcomp(&sep
[j
].ss__bodyre_buf
, x
,
691 REG_EXTENDED
| REG_ICASE
| REG_NOSUB
)) != 0){
693 n_err(_("Invalid regular expression: %s: %s\n"),
694 n_shexp_quote_cp(x
, FAL0
),
695 n_regex_err_to_doc(NULL
, s
));
699 sep
[j
].ss_bodyre
= &sep
[j
].ss__bodyre_buf
;
705 goto jnamesearch_sepfree
;
708 /* Iterate the entire message array */
710 if(!(flags
& a_HAVE_IMAP_HEADERS
) && mb
.mb_type
== MB_IMAP
){
711 flags
|= a_HAVE_IMAP_HEADERS
;
712 imap_getheaders(1, msgCount
);
717 n_autorec_relax_create();
718 for(i
= 0; i
< msgCount
; ++i
){
719 if((mp
= &message
[i
])->m_flag
& (MMARK
| MHIDDEN
))
721 if(!a_message_list_saw_d
&& (mp
->m_flag
& MDELETED
) != (unsigned)f
)
726 for(nq
= nmadat
; *nq
!= NULL
; ++nq
){
728 if(a_message_match_at(mp
, &sep
[PTR2SIZE(nq
- nmadat
)])){
732 }else if(**nq
== '/'){
733 if(a_message_match_dash(mp
, *nq
)){
737 }else if(a_message_match_sender(mp
, *nq
, (flags
& a_ALLNET
))){
743 if(!(flags
& a_TMP
) &&
744 id
!= NULL
&& a_message_match_mid(mp
, id
, idfield
))
751 n_autorec_relax_unroll();
753 n_autorec_relax_gut();
758 for(j
= PTR2SIZE(np
- nmadat
); j
-- != 0;){
759 if(sep
[j
].ss_fieldre
!= NULL
)
760 regfree(sep
[j
].ss_fieldre
);
761 if(sep
[j
].ss_bodyre
!= NULL
)
762 regfree(sep
[j
].ss_bodyre
);
771 /* If any colon modifiers were given, go through and mark any messages which
772 * do satisfy the modifiers */
774 for(i
= 0; i
< msgCount
; ++i
){
775 struct a_message_coltab
const *colp
;
777 if((mp
= &message
[i
])->m_flag
& (MMARK
| MHIDDEN
))
779 if(!a_message_list_saw_d
&& (mp
->m_flag
& MDELETED
) != (unsigned)f
)
782 for(colp
= a_message_coltabs
;
783 PTRCMP(colp
, <, &a_message_coltabs
[n_NELEM(a_message_coltabs
)]);
785 if(colp
->mco_bit
& colmod
){
786 /* Is this a colon modifier that requires evaluation? */
787 if(colp
->mco_mask
== 0){
788 if(colp
->mco_bit
& (a_MESSAGE_S_MLIST
|
789 a_MESSAGE_S_MLSUBSCRIBE
)){
790 enum mlist_state what
;
792 what
= (colp
->mco_bit
& a_MESSAGE_S_MLIST
) ? MLIST_KNOWN
794 if(what
== is_mlist_mp(mp
, what
))
797 }else if((mp
->m_flag
& colp
->mco_mask
798 ) == (enum mflag
)colp
->mco_equal
){
808 /* It shall be an error if ` didn't match anything, and nothing else did */
809 if((flags
& (a_TBACK
| a_ANY
)) == a_TBACK
){
810 id
= N_("No previously marked messages\n");
812 }else if(!(flags
& a_ANY
))
815 assert(!(flags
& a_ERROR
));
818 n_lofi_free(nmadat_lofi
);
820 return (flags
& a_ERROR
) ? -1 : 0;
823 id
= N_("Invalid range endpoint\n");
826 id
= N_("No applicable messages\n");
836 a_message_evalcol(int col
){
837 struct a_message_coltab
const *colp
;
842 for(colp
= a_message_coltabs
;
843 PTRCMP(colp
, <, &a_message_coltabs
[n_NELEM(a_message_coltabs
)]);
845 if(colp
->mco_char
== col
){
854 a_message_check(int mno
, int f
){
858 if(mno
< 1 || mno
> msgCount
){
859 n_err(_("%d: Invalid message number\n"), mno
);
861 }else if(((mp
= &message
[mno
- 1])->m_flag
& MHIDDEN
) ||
862 (f
!= MDELETED
&& (mp
->m_flag
& MDELETED
) != 0))
863 n_err(_("%d: inappropriate message\n"), mno
);
871 a_message_scan(struct a_message_speclex
*mslp
){
872 struct a_message_lex
const *lp
;
877 rv
= a_MESSAGE_T_EOL
;
879 /* Empty cap's even for IGNORE_EMPTY (quoted empty tokens produce output) */
880 for(;; mslp
->msl_cap
= mslp
->msl_cap
->ca_next
){
881 if(mslp
->msl_cap
== NULL
)
884 cp
= mslp
->msl_cap
->ca_arg
.ca_str
.s
;
885 if((c
= *cp
++) != '\0')
889 /* Select members of a message thread */
892 if(c
== '\0' || spacechar(c
)){
893 mslp
->msl_str
= mslp
->msl__smallstrbuf
;
894 mslp
->msl_str
[0] = '.';
895 mslp
->msl_str
[1] = '\0';
897 mslp
->msl_cap
= mslp
->msl_cap
->ca_next
;
900 n_err(_("Message list: invalid syntax: %s (in %s)\n"),
901 n_shexp_quote_cp(cp
, FAL0
),
902 n_shexp_quote_cp(mslp
->msl_input_orig
, FAL0
));
903 rv
= a_MESSAGE_T_ERROR
;
906 rv
= a_MESSAGE_T_DOT
| INT_MIN
;
913 /* If the leading character is a digit, scan the number and convert it
914 * on the fly. Return a_MESSAGE_T_NUMBER when done */
918 mslp
->msl_no
= (mslp
->msl_no
* 10) + c
- '0'; /* XXX inline atoi */
919 while((c
= *cp
++, digitchar(c
)));
922 mslp
->msl_cap
= mslp
->msl_cap
->ca_next
;
925 /* This could be a range */
927 mslp
->msl_cap
->ca_arg
.ca_str
.s
= cp
;
931 rv
|= a_MESSAGE_T_NUMBER
;
935 /* An IMAP SEARCH list. Note that a_MESSAGE_T_OPEN has always been included
936 * in singles[] in Mail and mailx. Thus although there is no formal
937 * definition for (LIST) lists, they do not collide with historical
938 * practice because a subject string (LIST) could never been matched
945 (tocp
= mslp
->msl_str
= mslp
->msl_cap
->ca_arg
.ca_str
.s
)[0] = '(';
950 if ((c
= *cp
++) == '\0') {
952 n_err(_("Missing )\n"));
953 n_err(_("P.S.: message specifications are now shell tokens, "
954 "making it necessary to enclose IMAP search expressions "
955 "in (single) quotes, e.g., '(from \"me\")'\n"));
956 rv
= a_MESSAGE_T_ERROR
;
959 if (inquote
&& c
== '\\') {
972 else if (spacechar(c
)) {
973 /* Replace unquoted whitespace by single space characters, to make
974 * the string IMAP SEARCH conformant */
980 } while (c
!= ')' || level
> 0);
984 mslp
->msl_cap
= mslp
->msl_cap
->ca_next
;
985 rv
|= a_MESSAGE_T_OPEN
;
989 /* Check for single character tokens; return such if found */
990 for(lp
= a_message_singles
;
991 PTRCMP(lp
, <, &a_message_singles
[n_NELEM(a_message_singles
)]); ++lp
){
992 if(c
== lp
->ml_char
){
993 mslp
->msl_str
= mslp
->msl__smallstrbuf
;
994 mslp
->msl_str
[0] = c
;
995 mslp
->msl_str
[1] = '\0';
998 mslp
->msl_cap
= mslp
->msl_cap
->ca_next
;
1004 mslp
->msl_cap
= mslp
->msl_cap
->ca_next
;
1005 mslp
->msl_str
= --cp
;
1006 rv
= a_MESSAGE_T_STRING
;
1013 a_message_match_sender(struct message
*mp
, char const *str
, bool_t allnet
){
1014 char const *str_base
, *np_base
, *np
;
1022 /* Empty string doesn't match */
1023 namep
= lextract(n_header_senderfield_of(mp
), GFULL
| GSKIN
);
1025 if(namep
== NULL
|| *(str_base
= str
) == '\0')
1028 /* *allnet* is POSIX and, since it explicitly mentions login and user names,
1029 * most likely case-sensitive. XXX Still allow substr matching, though
1030 * XXX possibly the first letter should be case-insensitive, then? */
1032 for(; namep
!= NULL
; str
= str_base
, namep
= namep
->n_flink
){
1033 for(np_base
= np
= namep
->n_name
;;){
1034 if((sc
= *str
++) == '@')
1036 if((nc
= *np
++) == '@' || nc
== '\0' || sc
== '\0'){
1037 if((rv
= (sc
== '\0')))
1048 /* TODO POSIX says ~"match any address as shown in header overview",
1049 * TODO but a normalized match would be more sane i guess.
1050 * TODO struct name should gain a comparison method, normalize realname
1051 * TODO content (in TODO) and thus match as likewise
1052 * TODO "Buddy (Today) <here>" and "(Now) Buddy <here>" */
1053 bool_t again_base
, again
;
1055 again_base
= ok_blook(showname
);
1057 for(; namep
!= NULL
; str
= str_base
, namep
= namep
->n_flink
){
1060 np_base
= np
= again
? namep
->n_fullname
: namep
->n_name
;
1064 if((nc
= *np
++) == '\0' || sc
== '\0'){
1065 if((rv
= (sc
== '\0')))
1077 /* And really if i want to match 'on@' then i want it to match even if
1078 * *showname* is set! */
1091 a_message_match_mid(struct message
*mp
, char const *id
,
1092 enum a_message_idfield idfield
){
1099 if((cp
= hfield1("message-id", mp
)) != NULL
){
1101 case a_MESSAGE_ID_REFERENCES
:
1102 if(!msgidcmp(id
, cp
))
1105 case a_MESSAGE_ID_IN_REPLY_TO
:{
1108 if((np
= extract(id
, GREF
)) != NULL
)
1110 if(!msgidcmp(np
->n_name
, cp
)){
1114 }while((np
= np
->n_flink
) != NULL
);
1124 a_message_match_dash(struct message
*mp
, char const *str
){
1125 static char lastscan
[128];
1128 char *hfield
, *hbody
;
1137 n_strscpy(lastscan
, str
, sizeof lastscan
); /* XXX use new n_str object! */
1139 /* Now look, ignoring case, for the word in the string */
1140 if(ok_blook(searchheaders
) && (hfield
= strchr(str
, ':'))){
1143 l
= PTR2SIZE(hfield
- str
);
1144 hfield
= n_lofi_alloc(l
+1);
1145 memcpy(hfield
, str
, l
);
1147 hbody
= hfieldX(hfield
, mp
);
1148 n_lofi_free(hfield
);
1149 hfield
= n_UNCONST(str
+ l
+ 1);
1151 hfield
= n_UNCONST(str
);
1152 hbody
= hfield1("subject", mp
);
1157 in
.l
= strlen(in
.s
= hbody
);
1158 mime_fromhdr(&in
, &out
, TD_ICONV
);
1159 rv
= substr(out
.s
, hfield
);
1167 a_message_match_at(struct message
*mp
, struct search_expr
*sep
){
1172 /* Namelist regex only matches headers.
1173 * And there are the special cases header/<, "body"/> and "text"/=, the
1174 * latter two of which need to be handled in here */
1175 if((field
= sep
->ss_field
) != NULL
){
1176 if(!asccasecmp(field
, "body") || (field
[1] == '\0' && field
[0] == '>')){
1179 rv
= message_match(mp
, sep
, rv
);
1181 }else if(!asccasecmp(field
, "text") ||
1182 (field
[1] == '\0' && field
[0] == '=')){
1188 rv
= n_header_match(mp
, sep
);
1195 a_message_unmark(int mesg
){
1200 if(i
< 1 || UICMP(z
, i
, >, msgCount
))
1201 n_panic(_("Bad message number to unmark"));
1202 message
[--i
].m_flag
&= ~MMARK
;
1207 a_message_metamess(int meta
, int f
)
1215 case '^': /* First 'good' message left */
1216 mp
= mb
.mb_threaded
? threadroot
: message
;
1217 while (PTRCMP(mp
, <, message
+ msgCount
)) {
1218 if (!(mp
->m_flag
& MHIDDEN
) && (mp
->m_flag
& MDELETED
) == (ui32_t
)f
) {
1219 c
= (int)PTR2SIZE(mp
- message
+ 1);
1222 if (mb
.mb_threaded
) {
1223 mp
= next_in_thread(mp
);
1229 if (!(n_pstate
& n_PS_HOOK_MASK
))
1230 n_err(_("No applicable messages\n"));
1233 case '$': /* Last 'good message left */
1235 ? this_in_thread(threadroot
, -1) : message
+ msgCount
- 1;
1236 while (mp
>= message
) {
1237 if (!(mp
->m_flag
& MHIDDEN
) && (mp
->m_flag
& MDELETED
) == (ui32_t
)f
) {
1238 c
= (int)PTR2SIZE(mp
- message
+ 1);
1241 if (mb
.mb_threaded
) {
1242 mp
= prev_in_thread(mp
);
1248 if (!(n_pstate
& n_PS_HOOK_MASK
))
1249 n_err(_("No applicable messages\n"));
1253 /* Current message */
1254 m
= dot
- message
+ 1;
1255 if ((dot
->m_flag
& MHIDDEN
) || (dot
->m_flag
& MDELETED
) != (ui32_t
)f
) {
1256 n_err(_("%d: inappropriate message\n"), m
);
1263 /* Previously current message */
1264 if (prevdot
== NULL
) {
1265 n_err(_("No previously current message\n"));
1268 m
= prevdot
- message
+ 1;
1269 if ((prevdot
->m_flag
& MHIDDEN
) ||
1270 (prevdot
->m_flag
& MDELETED
) != (ui32_t
)f
) {
1271 n_err(_("%d: inappropriate message\n"), m
);
1278 n_err(_("Unknown selector: %c\n"), c
);
1290 a_message__threadmark(struct message
*self
, int f
){
1292 if(!(self
->m_flag
& MHIDDEN
) &&
1293 (f
== MDELETED
|| !(self
->m_flag
& MDELETED
) || a_message_list_saw_d
))
1294 self
->m_flag
|= MMARK
;
1296 if((self
= self
->m_child
) != NULL
){
1298 while((self
= self
->m_younger
) != NULL
)
1299 if(self
->m_child
!= NULL
)
1301 a_message__threadmark(self
, f
);
1303 self
->m_flag
|= MMARK
;
1309 setinput(struct mailbox
*mp
, struct message
*m
, enum needspec need
){
1318 ok
= (m
->m_content_info
& CI_HAVE_HEADER
) ? OKAY
1319 : a_message_get_header(m
);
1322 ok
= (m
->m_content_info
& CI_HAVE_BODY
) ? OKAY
: get_body(m
);
1333 if(fseek(mp
->mb_itf
, (long)mailx_positionof(m
->m_block
, m
->m_offset
),
1335 n_perr(_("fseek"), 0);
1336 n_panic(_("temporary file seek"));
1345 get_body(struct message
*mp
){
1376 message_reset(void){
1378 if(message
!= NULL
){
1383 a_message_mem_space
= 0;
1388 message_append(struct message
*mp
){
1390 if(UICMP(z
, msgCount
+ 1, >=, a_message_mem_space
)){
1391 /* XXX remove _mem_space magics (or use s_Vector) */
1392 a_message_mem_space
= ((a_message_mem_space
>= 128 &&
1393 a_message_mem_space
<= 1000000)
1394 ? a_message_mem_space
<< 1 : a_message_mem_space
+ 64);
1395 message
= n_realloc(message
, a_message_mem_space
* sizeof(*message
));
1399 message
[msgCount
- 1] = *mp
;
1401 memset(&message
[msgCount
- 1], 0, sizeof *message
);
1407 message_append_null(void){
1410 message_append(NULL
);
1412 message
[msgCount
].m_size
= 0;
1413 message
[msgCount
].m_lines
= 0;
1418 message_match(struct message
*mp
, struct search_expr
const *sep
,
1419 bool_t with_headers
){
1421 size_t *linesize
, cnt
;
1428 if((fp
= Ftmp(NULL
, "mpmatch", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
)
1431 if(sendmp(mp
, fp
, NULL
, NULL
, SEND_TOSRCH
, NULL
) < 0)
1436 line
= &termios_state
.ts_linebuf
; /* XXX line pool */
1437 linesize
= &termios_state
.ts_linesize
; /* XXX line pool */
1440 while(fgetline(line
, linesize
, &cnt
, NULL
, fp
, 0))
1444 while(fgetline(line
, linesize
, &cnt
, NULL
, fp
, 0)){
1446 if(sep
->ss_bodyre
!= NULL
){
1447 if(regexec(sep
->ss_bodyre
, *line
, 0,NULL
, 0) == REG_NOMATCH
)
1451 if(!substr(*line
, sep
->ss_body
))
1465 setdot(struct message
*mp
){
1469 n_pstate
&= ~n_PS_DID_PRINT_DOT
;
1472 uncollapse1(dot
, 0);
1478 touch(struct message
*mp
){
1480 mp
->m_flag
|= MTOUCH
;
1481 if(!(mp
->m_flag
& MREAD
))
1482 mp
->m_flag
|= MREAD
| MSTATUS
;
1487 n_getmsglist(char const *buf
, int *vector
, int flags
)
1493 n_pstate
&= ~n_PS_ARGLIST_MASK
;
1494 a_message_list_last_saw_d
= a_message_list_saw_d
;
1495 a_message_list_saw_d
= FAL0
;
1503 n_pstate
|= n_PS_MSGLIST_DIRECT
;
1505 /* TODO Parse the message spec into an ARGV (not here -> cmd_arg_parse?) */
1507 n_CMD_ARG_DESC_SUBCLASS_DEF(getmsglist
, 1, pseudo_cad
){
1508 {n_CMD_ARG_DESC_WYSH
| n_CMD_ARG_DESC_OPTION
|
1509 n_CMD_ARG_DESC_GREEDY
| n_CMD_ARG_DESC_HONOUR_STOP
,
1510 n_SHEXP_PARSE_TRIM_IFSSPACE
| n_SHEXP_PARSE_IFS_VAR
|
1511 n_SHEXP_PARSE_IGNORE_EMPTY
}
1512 }n_CMD_ARG_DESC_SUBCLASS_DEF_END
;
1513 struct n_cmd_arg_ctx cac
;
1515 cac
.cac_desc
= n_CMD_ARG_DESC_SUBCLASS_CAST(&pseudo_cad
);
1516 cac
.cac_indat
= buf
;
1517 cac
.cac_inlen
= UIZ_MAX
;
1518 cac
.cac_msgflag
= flags
;
1519 cac
.cac_msgmask
= 0;
1520 if(!n_cmd_arg_parse(&cac
)){
1523 }else if(cac
.cac_no
== 0){
1526 }else if((mc
= a_message_markall(buf
, cac
.cac_arg
, flags
)) < 0){
1533 if(n_pstate
& n_PS_HOOK_NEWMAIL
){
1535 for(mp
= message
; mp
< &message
[msgCount
]; ++mp
)
1536 if(mp
->m_flag
& MMARK
){
1537 if(!(mp
->m_flag
& MNEWEST
))
1538 a_message_unmark((int)PTR2SIZE(mp
- message
+ 1));
1548 if(mb
.mb_threaded
== 0){
1549 for(mp
= message
; mp
< &message
[msgCount
]; ++mp
)
1550 if(mp
->m_flag
& MMARK
)
1551 *ip
++ = (int)PTR2SIZE(mp
- message
+ 1);
1553 for(mp
= threadroot
; mp
!= NULL
; mp
= next_in_thread(mp
))
1554 if(mp
->m_flag
& MMARK
)
1555 *ip
++ = (int)PTR2SIZE(mp
- message
+ 1);
1558 mc
= (int)PTR2SIZE(ip
- vector
);
1560 n_pstate
&= ~n_PS_MSGLIST_DIRECT
;
1573 if (msgCount
== 0) {
1581 mb
.mb_threaded
? (mp
!= NULL
) : PTRCMP(mp
, <, message
+ msgCount
);
1582 mb
.mb_threaded
? (mp
= next_in_thread(mp
)) : ++mp
) {
1583 if (!(mp
->m_flag
& MHIDDEN
) && (mp
->m_flag
& m
) == (ui32_t
)f
) {
1584 rv
= (int)PTR2SIZE(mp
- message
+ 1);
1589 if (dot
> message
) {
1590 for (mp
= dot
- 1; (mb
.mb_threaded
? (mp
!= NULL
) : (mp
>= message
));
1591 mb
.mb_threaded
? (mp
= prev_in_thread(mp
)) : --mp
) {
1592 if (!(mp
->m_flag
& MHIDDEN
) && (mp
->m_flag
& m
) == (ui32_t
)f
) {
1593 rv
= (int)PTR2SIZE(mp
- message
+ 1);
1605 mark(int mno
, int f
){
1611 if(i
< 1 || i
> msgCount
)
1612 n_panic(_("Bad message number to mark"));
1615 if(mb
.mb_threaded
== 1 && a_message_threadflag
)
1616 a_message__threadmark(mp
, f
);
1618 assert(!(mp
->m_flag
& MHIDDEN
));
1619 mp
->m_flag
|= MMARK
;