* Actually add alpine.exe to the repositoty.
[alpine.git] / pith / pattern.c
blobaded260b67981e8cf3bd649b20dfa2aa16326036
1 #if !defined(lint) && !defined(DOS)
2 static char rcsid[] = "$Id: pattern.c 1204 2009-02-02 19:54:23Z hubert@u.washington.edu $";
3 #endif
4 /*
5 * ========================================================================
6 * Copyright 2013-2017 Eduardo Chappa
7 * Copyright 2006-2009 University of Washington
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * ========================================================================
18 #include "../pith/headers.h"
19 #include "../pith/pattern.h"
20 #include "../pith/state.h"
21 #include "../pith/conf.h"
22 #include "../pith/string.h"
23 #include "../pith/msgno.h"
24 #include "../pith/status.h"
25 #include "../pith/list.h"
26 #include "../pith/flag.h"
27 #include "../pith/tempfile.h"
28 #include "../pith/addrstring.h"
29 #include "../pith/search.h"
30 #include "../pith/mailcmd.h"
31 #include "../pith/filter.h"
32 #include "../pith/save.h"
33 #include "../pith/mimedesc.h"
34 #include "../pith/reply.h"
35 #include "../pith/folder.h"
36 #include "../pith/maillist.h"
37 #include "../pith/sort.h"
38 #include "../pith/copyaddr.h"
39 #include "../pith/pipe.h"
40 #include "../pith/list.h"
41 #include "../pith/news.h"
42 #include "../pith/util.h"
43 #include "../pith/sequence.h"
44 #include "../pith/detoken.h"
45 #include "../pith/busy.h"
46 #include "../pith/indxtype.h"
47 #include "../pith/mailindx.h"
48 #include "../pith/send.h"
49 #include "../pith/icache.h"
50 #include "../pith/ablookup.h"
51 #include "../pith/keyword.h"
55 * Internal prototypes
57 void open_any_patterns(long);
58 void sub_open_any_patterns(long);
59 void sub_close_patterns(long);
60 int sub_any_patterns(long, PAT_STATE *);
61 PAT_LINE_S *parse_pat_lit(char *);
62 PAT_LINE_S *parse_pat_inherit(void);
63 PAT_S *parse_pat(char *);
64 void parse_patgrp_slash(char *, PATGRP_S *);
65 void parse_action_slash(char *, ACTION_S *);
66 ARBHDR_S *parse_arbhdr(char *);
67 char *next_arb(char *);
68 PAT_S *first_any_pattern(PAT_STATE *);
69 PAT_S *last_any_pattern(PAT_STATE *);
70 PAT_S *prev_any_pattern(PAT_STATE *);
71 PAT_S *next_any_pattern(PAT_STATE *);
72 int sub_write_patterns(long);
73 int write_pattern_file(char **, PAT_LINE_S *);
74 int write_pattern_lit(char **, PAT_LINE_S *);
75 int write_pattern_inherit(char **, PAT_LINE_S *);
76 char *data_for_patline(PAT_S *);
77 int charsets_present_in_msg(MAILSTREAM *, unsigned long, STRLIST_S *);
78 void collect_charsets_from_subj(ENVELOPE *, STRLIST_S **);
79 void collect_charsets_from_body(BODY *, STRLIST_S **);
80 SEARCHPGM *next_not(SEARCHPGM *);
81 SEARCHOR *next_or(SEARCHOR **);
82 void set_up_search_pgm(char *, PATTERN_S *, SEARCHPGM *);
83 void add_type_to_pgm(char *, PATTERN_S *, SEARCHPGM *);
84 void set_srch(char *, char *, SEARCHPGM *);
85 void set_srch_hdr(char *, char *, SEARCHPGM *);
86 void set_search_by_age(INTVL_S *, SEARCHPGM *, int);
87 void set_search_by_size(INTVL_S *, SEARCHPGM *);
88 int non_eh(char *);
89 void add_eh(char **, char **, char *, int *);
90 void set_extra_hdrs(char *);
91 int is_ascii_string(char *);
92 ACTION_S *combine_inherited_role_guts(ACTION_S *);
93 int move_filtered_msgs(MAILSTREAM *, MSGNO_S *, char *, int, char *);
94 void set_some_flags(MAILSTREAM *, MSGNO_S *, long, char **, char **, int, char *);
98 * optional hook for external-program filter test
100 void (*pith_opt_filter_pattern_cmd)(char **, SEARCHSET *, MAILSTREAM *, long, INTVL_S *);
103 void
104 role_process_filters(void)
106 int i;
107 MAILSTREAM *stream;
108 MSGNO_S *msgmap;
110 for(i = 0; i < ps_global->s_pool.nstream; i++){
111 stream = ps_global->s_pool.streams[i];
112 if(stream && pine_mail_ping(stream)){
113 msgmap = sp_msgmap(stream);
114 if(msgmap)
115 reprocess_filter_patterns(stream, msgmap, MI_REFILTERING);
122 add_to_pattern(PAT_S *pat, long int rflags)
124 PAT_LINE_S *new_patline, *patline;
125 PAT_S *new_pat;
126 PAT_STATE dummy;
128 if(!any_patterns(rflags, &dummy))
129 return(0);
131 /* need a new patline */
132 new_patline = (PAT_LINE_S *)fs_get(sizeof(*new_patline));
133 memset((void *)new_patline, 0, sizeof(*new_patline));
134 new_patline->type = Literal;
135 (*cur_pat_h)->dirtypinerc = 1;
137 /* and a copy of pat */
138 new_pat = copy_pat(pat);
140 /* tie together */
141 new_patline->first = new_patline->last = new_pat;
142 new_pat->patline = new_patline;
146 * Manipulate bits directly in pattern list.
147 * Cur_pat_h is set by any_patterns.
151 /* find last patline */
152 for(patline = (*cur_pat_h)->patlinehead;
153 patline && patline->next;
154 patline = patline->next)
157 /* add new patline to end of list */
158 if(patline){
159 patline->next = new_patline;
160 new_patline->prev = patline;
162 else
163 (*cur_pat_h)->patlinehead = new_patline;
165 return(1);
170 * Does pattern quoting. Takes the string that the user sees and converts
171 * it to the config file string.
173 * Args: src -- The source string.
175 * The last arg to add_escapes causes \, and \\ to be replaced with hex
176 * versions of comma and backslash. That's so we can embed commas in
177 * list variables without having them act as separators. If the user wants
178 * a literal comma, they type backslash comma.
179 * If /, \, or " appear (other than the special cases in previous sentence)
180 * they are backslash-escaped like \/, \\, or \".
182 * Returns: An allocated string with quoting added.
184 * The caller is responsible for freeing the memory allocated for the answer.
186 char *
187 add_pat_escapes(char *src)
189 return(add_escapes(src, "/\\\"", '\\', "", ",\\"));
194 * Undoes the escape quoting done by add_pat_escapes.
196 * Args: src -- The source string.
198 * Returns: A string with backslash quoting removed or NULL. The string starts
199 * at src and goes until the end of src or until a / is reached. The
200 * / is not included in the string. /'s may be quoted by preceding
201 * them with a backslash (\) and \'s may also be quoted by
202 * preceding them with a \. In fact, \ quotes any character.
203 * Not quite, \nnn is octal escape, \xXX is hex escape.
204 * Hex escapes are undone but left with a backslash in front.
206 * The caller is responsible for freeing the memory allocated for the answer.
208 char *
209 remove_pat_escapes(char *src)
211 char *ans = NULL, *q, *p;
212 int done = 0;
214 if(src){
215 p = q = (char *)fs_get(strlen(src) + 1);
217 while(!done){
218 switch(*src){
219 case '\\':
220 src++;
221 if(*src){
222 if(isdigit((unsigned char)*src)){ /* octal escape */
223 *p++ = '\\';
224 *p++ = (char)read_octal(&src);
226 else if((*src == 'x' || *src == 'X') &&
227 *(src+1) && *(src+2) && isxpair(src+1)){
228 *p++ = '\\';
229 *p++ = (char)read_hex(src+1);
230 src += 3;
232 else
233 *p++ = *src++;
236 break;
238 case '\0':
239 case '/':
240 done++;
241 break;
243 default:
244 *p++ = *src++;
245 break;
249 *p = '\0';
251 ans = cpystr(q);
252 fs_give((void **)&q);
255 return(ans);
260 * This takes envelope data and adds the backslash escapes that the user
261 * would have been responsible for adding if editing manually.
262 * It just escapes commas and backslashes.
264 * Caller must free result.
266 char *
267 add_roletake_escapes(char *src)
269 return(add_escapes(src, ",\\", '\\', "", ""));
273 * This function only escapes commas.
275 char *
276 add_comma_escapes(char *src)
278 return(add_escapes(src, ",", '\\', "", ""));
283 * These are the global pattern handles which all of the pattern routines
284 * use. Once we open one of these we usually leave it open until exiting
285 * pine. The _any versions are only used if we are altering our configuration,
286 * the _ne (NonEmpty) versions are used routinely. We open the patterns by
287 * calling either nonempty_patterns (normal use) or any_patterns (config).
289 * There are eight different pinerc variables which contain patterns. They are
290 * patterns-filters2, patterns-roles, patterns-scores2, patterns-indexcolors,
291 * patterns-other, and the old patterns, patterns-filters, and patterns-scores.
292 * The first five are the active patterns variables and the old variable are
293 * kept around so that we can convert old patterns to new. The reason we
294 * split it into five separate variables is so that each can independently
295 * be controlled by the main pinerc or by the exception pinerc. The reason
296 * for the change to filters2 and scores2 was so we could change the semantics
297 * of how rules work when there are pieces in the rule that we don't
298 * understand. We added a rule to detect 8bitSubjects. So a user might have
299 * a filter that deletes messages with 8bitSubjects. The problem was that
300 * that same filter in a old patterns-filters pine would match because it
301 * would ignore the 8bitSubject part of the pattern and match on the rest.
302 * So we changed the semantics so that rules with unknown pieces would be
303 * ignored instead of used. We had to change variable names at the same time
304 * because we were adding the 8bit thing and the old pines are still out
305 * there. Filters and Scores can both be dangerous. Roles, Colors, and Other
306 * seem less dangerous so not worth adding a new variable for them.
308 * Each of the eight variables has its own handle and status variables below.
309 * That means that they operate independently.
311 * Looking at just a single one of those variables, it has four possible
312 * values. In normal use, we use the current_val of the variable to set
313 * up the patterns. We do that by calling nonempty_patterns() with the
314 * appropriate rflags. When editing configurations, we have the other two
315 * variables to deal with: main_user_val and post_user_val.
316 * We only ever deal with one of those at a time, so we re-use the variables.
317 * However, we do sometimes want to deal with one of those and at the same
318 * time refer to the current current_val. For example, if we are editing
319 * the post or main user_val for the filters variable, we still want
320 * to check for new mail. If we find new mail we'll want to call
321 * process_filter_patterns which uses the current_val for filter patterns.
322 * That means we have to provide for the case where we are using current_val
323 * at the same time as we're using one of the user_vals. That's why we have
324 * both the _ne variables (NonEmpty) and the _any variables.
326 * In any_patterns (and first_pattern...) use_flags may only be set to
327 * one value at a time, whereas rflags may be more than one value OR'd together.
329 PAT_HANDLE **cur_pat_h;
330 static PAT_HANDLE *pattern_h_roles_ne, *pattern_h_roles_any,
331 *pattern_h_scores_ne, *pattern_h_scores_any,
332 *pattern_h_filts_ne, *pattern_h_filts_any,
333 *pattern_h_filts_cfg,
334 *pattern_h_filts_ne, *pattern_h_filts_any,
335 *pattern_h_incol_ne, *pattern_h_incol_any,
336 *pattern_h_other_ne, *pattern_h_other_any,
337 *pattern_h_srch_ne, *pattern_h_srch_any,
338 *pattern_h_oldpat_ne, *pattern_h_oldpat_any;
341 * These contain the PAT_OPEN_MASK open status and the PAT_USE_MASK use status.
343 static long *cur_pat_status;
344 static long pat_status_roles_ne, pat_status_roles_any,
345 pat_status_scores_ne, pat_status_scores_any,
346 pat_status_filts_ne, pat_status_filts_any,
347 pat_status_incol_ne, pat_status_incol_any,
348 pat_status_other_ne, pat_status_other_any,
349 pat_status_srch_ne, pat_status_srch_any,
350 pat_status_oldpat_ne, pat_status_oldpat_any,
351 pat_status_oldfilt_ne, pat_status_oldfilt_any,
352 pat_status_oldscore_ne, pat_status_oldscore_any;
354 #define SET_PATTYPE(rflags) \
355 set_pathandle(rflags); \
356 cur_pat_status = \
357 ((rflags) & PAT_USE_CURRENT) \
358 ? (((rflags) & ROLE_DO_INCOLS) ? &pat_status_incol_ne : \
359 ((rflags) & ROLE_DO_OTHER) ? &pat_status_other_ne : \
360 ((rflags) & ROLE_DO_FILTER) ? &pat_status_filts_ne : \
361 ((rflags) & ROLE_DO_SCORES) ? &pat_status_scores_ne : \
362 ((rflags) & ROLE_DO_ROLES) ? &pat_status_roles_ne : \
363 ((rflags) & ROLE_DO_SRCH) ? &pat_status_srch_ne : \
364 ((rflags) & ROLE_OLD_FILT) ? &pat_status_oldfilt_ne : \
365 ((rflags) & ROLE_OLD_SCORE) ? &pat_status_oldscore_ne :\
366 &pat_status_oldpat_ne) \
367 : (((rflags) & ROLE_DO_INCOLS) ? &pat_status_incol_any : \
368 ((rflags) & ROLE_DO_OTHER) ? &pat_status_other_any : \
369 ((rflags) & ROLE_DO_FILTER) ? &pat_status_filts_any : \
370 ((rflags) & ROLE_DO_SCORES) ? &pat_status_scores_any : \
371 ((rflags) & ROLE_DO_ROLES) ? &pat_status_roles_any : \
372 ((rflags) & ROLE_DO_SRCH) ? &pat_status_srch_any : \
373 ((rflags) & ROLE_OLD_FILT) ? &pat_status_oldfilt_any :\
374 ((rflags) & ROLE_OLD_SCORE) ? &pat_status_oldscore_any:\
375 &pat_status_oldpat_any);
376 #define CANONICAL_RFLAGS(rflags) \
377 ((((rflags) & (ROLE_DO_ROLES | ROLE_REPLY | ROLE_FORWARD | ROLE_COMPOSE)) \
378 ? ROLE_DO_ROLES : 0) | \
379 (((rflags) & (ROLE_DO_INCOLS | ROLE_INCOL)) \
380 ? ROLE_DO_INCOLS : 0) | \
381 (((rflags) & (ROLE_DO_SCORES | ROLE_SCORE)) \
382 ? ROLE_DO_SCORES : 0) | \
383 (((rflags) & (ROLE_DO_FILTER)) \
384 ? ROLE_DO_FILTER : 0) | \
385 (((rflags) & (ROLE_DO_OTHER)) \
386 ? ROLE_DO_OTHER : 0) | \
387 (((rflags) & (ROLE_DO_SRCH)) \
388 ? ROLE_DO_SRCH : 0) | \
389 (((rflags) & (ROLE_OLD_FILT)) \
390 ? ROLE_OLD_FILT : 0) | \
391 (((rflags) & (ROLE_OLD_SCORE)) \
392 ? ROLE_OLD_SCORE : 0) | \
393 (((rflags) & (ROLE_OLD_PAT)) \
394 ? ROLE_OLD_PAT : 0))
396 #define SETPGMSTATUS(val,yes,no) \
397 switch(val){ \
398 case PAT_STAT_YES: \
399 (yes) = 1; \
400 break; \
401 case PAT_STAT_NO: \
402 (no) = 1; \
403 break; \
404 case PAT_STAT_EITHER: \
405 default: \
406 break; \
409 #define SET_STATUS(srchin,srchfor,assignto) \
410 {char *qq, *pp; \
411 int ii; \
412 NAMEVAL_S *vv; \
413 if((qq = srchstr(srchin, srchfor)) != NULL){ \
414 if((pp = remove_pat_escapes(qq+strlen(srchfor))) != NULL){ \
415 for(ii = 0; (vv = role_status_types(ii)); ii++) \
416 if(!strucmp(pp, vv->shortname)){ \
417 assignto = vv->value; \
418 break; \
421 fs_give((void **)&pp); \
426 #define SET_MSGSTATE(srchin,srchfor,assignto) \
427 {char *qq, *pp; \
428 int ii; \
429 NAMEVAL_S *vv; \
430 if((qq = srchstr(srchin, srchfor)) != NULL){ \
431 if((pp = remove_pat_escapes(qq+strlen(srchfor))) != NULL){ \
432 for(ii = 0; (vv = msg_state_types(ii)); ii++) \
433 if(!strucmp(pp, vv->shortname)){ \
434 assignto = vv->value; \
435 break; \
438 fs_give((void **)&pp); \
443 #define PATTERN_N (9)
446 void
447 set_pathandle(long int rflags)
449 cur_pat_h = (rflags & PAT_USE_CURRENT)
450 ? ((rflags & ROLE_DO_INCOLS) ? &pattern_h_incol_ne :
451 (rflags & ROLE_DO_OTHER) ? &pattern_h_other_ne :
452 (rflags & ROLE_DO_FILTER) ? &pattern_h_filts_ne :
453 (rflags & ROLE_DO_SCORES) ? &pattern_h_scores_ne :
454 (rflags & ROLE_DO_ROLES) ? &pattern_h_roles_ne :
455 (rflags & ROLE_DO_SRCH) ? &pattern_h_srch_ne :
456 &pattern_h_oldpat_ne)
457 : ((rflags & PAT_USE_CHANGED)
458 ? &pattern_h_filts_cfg
459 : ((rflags & ROLE_DO_INCOLS) ? &pattern_h_incol_any :
460 (rflags & ROLE_DO_OTHER) ? &pattern_h_other_any :
461 (rflags & ROLE_DO_FILTER) ? &pattern_h_filts_any :
462 (rflags & ROLE_DO_SCORES) ? &pattern_h_scores_any :
463 (rflags & ROLE_DO_ROLES) ? &pattern_h_roles_any :
464 (rflags & ROLE_DO_SRCH) ? &pattern_h_srch_any :
465 &pattern_h_oldpat_any));
470 * Rflags may be more than one pattern type OR'd together. It also contains
471 * the "use" parameter.
473 void
474 open_any_patterns(long int rflags)
476 long canon_rflags;
478 dprint((7, "open_any_patterns(0x%x)\n", rflags));
480 canon_rflags = CANONICAL_RFLAGS(rflags);
482 if(canon_rflags & ROLE_DO_INCOLS)
483 sub_open_any_patterns(ROLE_DO_INCOLS | (rflags & PAT_USE_MASK));
484 if(canon_rflags & ROLE_DO_FILTER)
485 sub_open_any_patterns(ROLE_DO_FILTER | (rflags & PAT_USE_MASK));
486 if(canon_rflags & ROLE_DO_OTHER)
487 sub_open_any_patterns(ROLE_DO_OTHER | (rflags & PAT_USE_MASK));
488 if(canon_rflags & ROLE_DO_SCORES)
489 sub_open_any_patterns(ROLE_DO_SCORES | (rflags & PAT_USE_MASK));
490 if(canon_rflags & ROLE_DO_ROLES)
491 sub_open_any_patterns(ROLE_DO_ROLES | (rflags & PAT_USE_MASK));
492 if(canon_rflags & ROLE_DO_SRCH)
493 sub_open_any_patterns(ROLE_DO_SRCH | (rflags & PAT_USE_MASK));
494 if(canon_rflags & ROLE_OLD_FILT)
495 sub_open_any_patterns(ROLE_OLD_FILT | (rflags & PAT_USE_MASK));
496 if(canon_rflags & ROLE_OLD_SCORE)
497 sub_open_any_patterns(ROLE_OLD_SCORE | (rflags & PAT_USE_MASK));
498 if(canon_rflags & ROLE_OLD_PAT)
499 sub_open_any_patterns(ROLE_OLD_PAT | (rflags & PAT_USE_MASK));
504 * This should only be called with a single pattern type (plus use flags).
505 * We assume that patterns of this type are closed before this is called.
506 * This always succeeds unless we run out of memory, in which case fs_get
507 * never returns.
509 void
510 sub_open_any_patterns(long int rflags)
512 PAT_LINE_S *patline = NULL, *pl = NULL;
513 char **t = NULL;
514 struct variable *var;
516 SET_PATTYPE(rflags);
518 *cur_pat_h = (PAT_HANDLE *)fs_get(sizeof(**cur_pat_h));
519 memset((void *)*cur_pat_h, 0, sizeof(**cur_pat_h));
521 if(rflags & ROLE_DO_ROLES)
522 var = &ps_global->vars[V_PAT_ROLES];
523 else if(rflags & ROLE_DO_FILTER)
524 var = &ps_global->vars[V_PAT_FILTS];
525 else if(rflags & ROLE_DO_OTHER)
526 var = &ps_global->vars[V_PAT_OTHER];
527 else if(rflags & ROLE_DO_SCORES)
528 var = &ps_global->vars[V_PAT_SCORES];
529 else if(rflags & ROLE_DO_INCOLS)
530 var = &ps_global->vars[V_PAT_INCOLS];
531 else if(rflags & ROLE_DO_SRCH)
532 var = &ps_global->vars[V_PAT_SRCH];
533 else if(rflags & ROLE_OLD_FILT)
534 var = &ps_global->vars[V_PAT_FILTS_OLD];
535 else if(rflags & ROLE_OLD_SCORE)
536 var = &ps_global->vars[V_PAT_SCORES_OLD];
537 else if(rflags & ROLE_OLD_PAT)
538 var = &ps_global->vars[V_PATTERNS];
540 switch(rflags & PAT_USE_MASK){
541 case PAT_USE_CURRENT:
542 t = var->current_val.l;
543 break;
544 case PAT_USE_CHANGED:
546 * some trickery to only use changed if actually changed.
547 * otherwise, use current_val
549 t = var->is_changed_val ? var->changed_val.l : var->current_val.l;
550 break;
551 case PAT_USE_MAIN:
552 t = var->main_user_val.l;
553 break;
554 case PAT_USE_POST:
555 t = var->post_user_val.l;
556 break;
559 if(t){
560 for(; t[0] && t[0][0]; t++){
561 if(*t && !strncmp("LIT:", *t, 4))
562 patline = parse_pat_lit(*t + 4);
563 else if(*t && !strncmp("FILE:", *t, 5))
564 patline = parse_pat_file(*t + 5);
565 else if(rflags & (PAT_USE_MAIN | PAT_USE_POST) &&
566 patline == NULL && *t && !strcmp(INHERIT, *t))
567 patline = parse_pat_inherit();
568 else
569 patline = NULL;
571 if(patline){
572 if(pl){
573 pl->next = patline;
574 patline->prev = pl;
575 pl = pl->next;
577 else{
578 (*cur_pat_h)->patlinehead = patline;
579 pl = patline;
582 else
583 q_status_message1(SM_ORDER, 0, 3,
584 "Invalid patterns line \"%.200s\"", *t);
588 *cur_pat_status = PAT_OPENED | (rflags & PAT_USE_MASK);
592 void
593 close_every_pattern(void)
595 close_patterns(ROLE_DO_INCOLS | ROLE_DO_FILTER | ROLE_DO_SCORES
596 | ROLE_DO_OTHER | ROLE_DO_ROLES | ROLE_DO_SRCH
597 | ROLE_OLD_FILT | ROLE_OLD_SCORE | ROLE_OLD_PAT
598 | PAT_USE_CURRENT);
600 * Since there is only one set of variables for the other three uses
601 * we can just close any one of them. There can only be one open at
602 * a time.
604 close_patterns(ROLE_DO_INCOLS | ROLE_DO_FILTER | ROLE_DO_SCORES
605 | ROLE_DO_OTHER | ROLE_DO_ROLES | ROLE_DO_SRCH
606 | ROLE_OLD_FILT | ROLE_OLD_SCORE | ROLE_OLD_PAT
607 | PAT_USE_MAIN);
612 * Can be called with more than one pattern type.
614 void
615 close_patterns(long int rflags)
617 long canon_rflags;
619 dprint((7, "close_patterns(0x%x)\n", rflags));
621 canon_rflags = CANONICAL_RFLAGS(rflags);
623 if(canon_rflags & ROLE_DO_INCOLS)
624 sub_close_patterns(ROLE_DO_INCOLS | (rflags & PAT_USE_MASK));
625 if(canon_rflags & ROLE_DO_OTHER)
626 sub_close_patterns(ROLE_DO_OTHER | (rflags & PAT_USE_MASK));
627 if(canon_rflags & ROLE_DO_FILTER)
628 sub_close_patterns(ROLE_DO_FILTER | (rflags & PAT_USE_MASK));
629 if(canon_rflags & ROLE_DO_SCORES)
630 sub_close_patterns(ROLE_DO_SCORES | (rflags & PAT_USE_MASK));
631 if(canon_rflags & ROLE_DO_ROLES)
632 sub_close_patterns(ROLE_DO_ROLES | (rflags & PAT_USE_MASK));
633 if(canon_rflags & ROLE_DO_SRCH)
634 sub_close_patterns(ROLE_DO_SRCH | (rflags & PAT_USE_MASK));
635 if(canon_rflags & ROLE_OLD_FILT)
636 sub_close_patterns(ROLE_OLD_FILT | (rflags & PAT_USE_MASK));
637 if(canon_rflags & ROLE_OLD_SCORE)
638 sub_close_patterns(ROLE_OLD_SCORE | (rflags & PAT_USE_MASK));
639 if(canon_rflags & ROLE_OLD_PAT)
640 sub_close_patterns(ROLE_OLD_PAT | (rflags & PAT_USE_MASK));
645 * Can be called with only a single pattern type.
647 void
648 sub_close_patterns(long int rflags)
650 SET_PATTYPE(rflags);
652 if(*cur_pat_h != NULL){
653 free_patline(&(*cur_pat_h)->patlinehead);
654 fs_give((void **)cur_pat_h);
657 *cur_pat_status = PAT_CLOSED;
659 scores_are_used(SCOREUSE_INVALID);
664 * Can be called with more than one pattern type.
665 * Nonempty always uses PAT_USE_CURRENT (the current_val).
668 nonempty_patterns(long int rflags, PAT_STATE *pstate)
670 return(any_patterns((rflags & ROLE_MASK) | PAT_USE_CURRENT, pstate));
675 * Initializes pstate and parses and sets up appropriate pattern variables.
676 * May be called with more than one pattern type OR'd together in rflags.
677 * Pstate will keep track of that and next_pattern et. al. will increment
678 * through all of those pattern types.
681 any_patterns(long int rflags, PAT_STATE *pstate)
683 int ret = 0;
684 long canon_rflags;
686 dprint((7, "any_patterns(0x%x)\n", rflags));
688 memset((void *)pstate, 0, sizeof(*pstate));
689 pstate->rflags = rflags;
691 canon_rflags = CANONICAL_RFLAGS(pstate->rflags);
693 if(canon_rflags & ROLE_DO_INCOLS)
694 ret += sub_any_patterns(ROLE_DO_INCOLS, pstate);
695 if(canon_rflags & ROLE_DO_OTHER)
696 ret += sub_any_patterns(ROLE_DO_OTHER, pstate);
697 if(canon_rflags & ROLE_DO_FILTER)
698 ret += sub_any_patterns(ROLE_DO_FILTER, pstate);
699 if(canon_rflags & ROLE_DO_SCORES)
700 ret += sub_any_patterns(ROLE_DO_SCORES, pstate);
701 if(canon_rflags & ROLE_DO_ROLES)
702 ret += sub_any_patterns(ROLE_DO_ROLES, pstate);
703 if(canon_rflags & ROLE_DO_SRCH)
704 ret += sub_any_patterns(ROLE_DO_SRCH, pstate);
705 if(canon_rflags & ROLE_OLD_FILT)
706 ret += sub_any_patterns(ROLE_OLD_FILT, pstate);
707 if(canon_rflags & ROLE_OLD_SCORE)
708 ret += sub_any_patterns(ROLE_OLD_SCORE, pstate);
709 if(canon_rflags & ROLE_OLD_PAT)
710 ret += sub_any_patterns(ROLE_OLD_PAT, pstate);
712 return(ret);
717 sub_any_patterns(long int rflags, PAT_STATE *pstate)
719 SET_PATTYPE(rflags | (pstate->rflags & PAT_USE_MASK));
721 if(*cur_pat_h &&
722 (((pstate->rflags & PAT_USE_MASK) == PAT_USE_CURRENT &&
723 (*cur_pat_status & PAT_USE_MASK) != PAT_USE_CURRENT) ||
724 ((pstate->rflags & PAT_USE_MASK) != PAT_USE_CURRENT &&
725 ((*cur_pat_status & PAT_OPEN_MASK) != PAT_OPENED ||
726 (*cur_pat_status & PAT_USE_MASK) !=
727 (pstate->rflags & PAT_USE_MASK)))))
728 close_patterns(rflags | (pstate->rflags & PAT_USE_MASK));
730 /* open_any always succeeds */
731 if(!*cur_pat_h && ((*cur_pat_status & PAT_OPEN_MASK) == PAT_CLOSED))
732 open_any_patterns(rflags | (pstate->rflags & PAT_USE_MASK));
734 if(!*cur_pat_h){ /* impossible */
735 *cur_pat_status = PAT_CLOSED;
736 return(0);
740 * Opening nonempty can fail. That just means there aren't any
741 * patterns of that type.
743 if((pstate->rflags & PAT_USE_MASK) == PAT_USE_CURRENT &&
744 !(*cur_pat_h)->patlinehead)
745 *cur_pat_status = (PAT_OPEN_FAILED | PAT_USE_CURRENT);
747 return(((*cur_pat_status & PAT_OPEN_MASK) == PAT_OPENED) ? 1 : 0);
752 edit_pattern(PAT_S *newpat, int pos, long int rflags)
754 PAT_S *oldpat;
755 PAT_LINE_S *tpatline;
756 int i;
757 PAT_STATE pstate;
759 if(!any_patterns(rflags, &pstate)) return(1);
761 for(i = 0, tpatline = (*cur_pat_h)->patlinehead;
762 i < pos && tpatline; tpatline = tpatline->next, i++);
763 if(i != pos) return(1);
764 oldpat = tpatline->first;
765 free_pat(&oldpat);
766 tpatline->first = tpatline->last = newpat;
767 newpat->patline = tpatline;
768 tpatline->dirty = 1;
770 (*cur_pat_h)->dirtypinerc = 1;
771 write_patterns(rflags);
773 return(0);
777 add_pattern(PAT_S *newpat, long int rflags)
779 PAT_LINE_S *tpatline, *newpatline;
780 PAT_STATE pstate;
782 any_patterns(rflags, &pstate);
784 for(tpatline = (*cur_pat_h)->patlinehead;
785 tpatline && tpatline->next ; tpatline = tpatline->next);
786 newpatline = (PAT_LINE_S *)fs_get(sizeof(PAT_LINE_S));
787 if(tpatline)
788 tpatline->next = newpatline;
789 else
790 (*cur_pat_h)->patlinehead = newpatline;
791 memset((void *)newpatline, 0, sizeof(PAT_LINE_S));
792 newpatline->prev = tpatline;
793 newpatline->first = newpatline->last = newpat;
794 newpatline->type = Literal;
795 newpat->patline = newpatline;
796 newpatline->dirty = 1;
798 (*cur_pat_h)->dirtypinerc = 1;
799 write_patterns(rflags);
801 return(0);
805 delete_pattern(int pos, long int rflags)
807 PAT_LINE_S *tpatline;
808 int i;
809 PAT_STATE pstate;
811 if(!any_patterns(rflags, &pstate)) return(1);
813 for(i = 0, tpatline = (*cur_pat_h)->patlinehead;
814 i < pos && tpatline; tpatline = tpatline->next, i++);
815 if(i != pos) return(1);
817 if(tpatline == (*cur_pat_h)->patlinehead)
818 (*cur_pat_h)->patlinehead = tpatline->next;
819 if(tpatline->prev) tpatline->prev->next = tpatline->next;
820 if(tpatline->next) tpatline->next->prev = tpatline->prev;
821 tpatline->prev = NULL;
822 tpatline->next = NULL;
824 free_patline(&tpatline);
826 (*cur_pat_h)->dirtypinerc = 1;
827 write_patterns(rflags);
829 return(0);
833 shuffle_pattern(int pos, int up, long int rflags)
835 PAT_LINE_S *tpatline, *shufpatline;
836 int i;
837 PAT_STATE pstate;
839 if(!any_patterns(rflags, &pstate)) return(1);
841 for(i = 0, tpatline = (*cur_pat_h)->patlinehead;
842 i < pos && tpatline; tpatline = tpatline->next, i++);
843 if(i != pos) return(1);
845 if(up == 1){
846 if(tpatline->prev == NULL) return(1);
847 shufpatline = tpatline->prev;
848 tpatline->prev = shufpatline->prev;
849 if(shufpatline->prev)
850 shufpatline->prev->next = tpatline;
851 if(tpatline->next)
852 tpatline->next->prev = shufpatline;
853 shufpatline->next = tpatline->next;
854 shufpatline->prev = tpatline;
855 tpatline->next = shufpatline;
856 if(shufpatline == (*cur_pat_h)->patlinehead)
857 (*cur_pat_h)->patlinehead = tpatline;
859 else if(up == -1){
860 if(tpatline->next == NULL) return(1);
861 shufpatline = tpatline->next;
862 tpatline->next = shufpatline->next;
863 if(shufpatline->next)
864 shufpatline->next->prev = tpatline;
865 if(tpatline->prev)
866 tpatline->prev->next = shufpatline;
867 shufpatline->prev = tpatline->prev;
868 shufpatline->next = tpatline;
869 tpatline->prev = shufpatline;
870 if(tpatline == (*cur_pat_h)->patlinehead)
871 (*cur_pat_h)->patlinehead = shufpatline;
873 else return(1);
875 shufpatline->dirty = 1;
876 tpatline->dirty = 1;
878 (*cur_pat_h)->dirtypinerc = 1;
879 write_patterns(rflags);
881 return(0);
884 PAT_LINE_S *
885 parse_pat_lit(char *litpat)
887 PAT_LINE_S *patline;
888 PAT_S *pat;
890 patline = (PAT_LINE_S *)fs_get(sizeof(*patline));
891 memset((void *)patline, 0, sizeof(*patline));
892 patline->type = Literal;
895 if((pat = parse_pat(litpat)) != NULL){
896 pat->patline = patline;
897 patline->first = pat;
898 patline->last = pat;
901 return(patline);
906 * This always returns a patline even if we can't read the file. The patline
907 * returned will say readonly in the worst case and there will be no patterns.
908 * If the file doesn't exist, this creates it if possible.
910 PAT_LINE_S *
911 parse_pat_file(char *filename)
913 #define BUF_SIZE 5000
914 PAT_LINE_S *patline;
915 PAT_S *pat, *p;
916 char path[MAXPATH+1], buf[BUF_SIZE];
917 char *dir, *q;
918 FILE *fp;
919 int ok = 0, some_pats = 0;
920 struct variable *vars = ps_global->vars;
922 signature_path(filename, path, MAXPATH);
924 if(VAR_OPER_DIR && !in_dir(VAR_OPER_DIR, path)){
925 q_status_message1(SM_ORDER | SM_DING, 3, 4,
926 "Can't use Roles file outside of %.200s",
927 VAR_OPER_DIR);
928 return(NULL);
931 patline = (PAT_LINE_S *)fs_get(sizeof(*patline));
932 memset((void *)patline, 0, sizeof(*patline));
933 patline->type = File;
934 patline->filename = cpystr(filename);
935 patline->filepath = cpystr(path);
937 if((q = last_cmpnt(path)) != NULL){
938 int save;
940 save = *--q;
941 *q = '\0';
942 dir = cpystr(*path ? path : "/");
943 *q = save;
945 else
946 dir = cpystr(".");
948 #if defined(DOS) || defined(OS2)
950 * If the dir has become a drive letter and : (e.g. "c:")
951 * then append a "\". The library function access() in the
952 * win 16 version of MSC seems to require this.
954 if(isalpha((unsigned char) *dir)
955 && *(dir+1) == ':' && *(dir+2) == '\0'){
956 *(dir+2) = '\\';
957 *(dir+3) = '\0';
959 #endif /* DOS || OS2 */
962 * Even if we can edit the file itself, we aren't going
963 * to be able to change it unless we can also write in
964 * the directory that contains it (because we write into a
965 * temp file and then rename).
967 if(can_access(dir, EDIT_ACCESS) != 0)
968 patline->readonly = 1;
970 if(can_access(path, EDIT_ACCESS) == 0){
971 if(patline->readonly)
972 q_status_message1(SM_ORDER, 0, 3,
973 "Pattern file directory (%.200s) is ReadOnly", dir);
975 else if(can_access(path, READ_ACCESS) == 0)
976 patline->readonly = 1;
978 if(can_access(path, ACCESS_EXISTS) == 0){
979 if((fp = our_fopen(path, "rb")) != NULL){
980 /* Check to see if this is a valid patterns file */
981 if(fp_file_size(fp) <= 0L)
982 ok++;
983 else{
984 size_t len;
986 len = strlen(PATTERN_MAGIC);
987 if(fread(buf, sizeof(char), len+3, fp) == len+3){
988 buf[len+3] = '\0';
989 buf[len] = '\0';
990 if(strcmp(buf, PATTERN_MAGIC) == 0){
991 if(atoi(PATTERN_FILE_VERS) < atoi(buf + len + 1))
992 q_status_message1(SM_ORDER, 0, 4,
993 "Pattern file \"%.200s\" is made by newer Alpine, will try to use it anyway",
994 filename);
996 ok++;
997 some_pats++;
998 /* toss rest of first line */
999 (void)fgets(buf, BUF_SIZE, fp);
1004 if(!ok){
1005 patline->readonly = 1;
1006 q_status_message1(SM_ORDER | SM_DING, 3, 4,
1007 "\"%.200s\" is not a Pattern file", path);
1010 p = NULL;
1011 while(some_pats && fgets(buf, BUF_SIZE, fp) != NULL){
1012 if((pat = parse_pat(buf)) != NULL){
1013 pat->patline = patline;
1014 if(!patline->first)
1015 patline->first = pat;
1017 patline->last = pat;
1019 if(p){
1020 p->next = pat;
1021 pat->prev = p;
1022 p = p->next;
1024 else
1025 p = pat;
1029 (void)fclose(fp);
1031 else{
1032 patline->readonly = 1;
1033 q_status_message2(SM_ORDER | SM_DING, 3, 4,
1034 "Error \"%.200s\" reading pattern file \"%.200s\"",
1035 error_description(errno), path);
1038 else{ /* doesn't exist yet, try to create it */
1039 if(patline->readonly)
1040 q_status_message1(SM_ORDER, 0, 3,
1041 "Pattern file directory (%.200s) is ReadOnly", dir);
1042 else{
1044 * We try to create it by making up an empty patline and calling
1045 * write_pattern_file.
1047 patline->dirty = 1;
1048 if(write_pattern_file(NULL, patline) != 0){
1049 patline->readonly = 1;
1050 patline->dirty = 0;
1051 q_status_message1(SM_ORDER | SM_DING, 3, 4,
1052 "Error creating pattern file \"%.200s\"",
1053 path);
1058 if(dir)
1059 fs_give((void **)&dir);
1061 return(patline);
1065 PAT_LINE_S *
1066 parse_pat_inherit(void)
1068 PAT_LINE_S *patline;
1069 PAT_S *pat;
1071 patline = (PAT_LINE_S *)fs_get(sizeof(*patline));
1072 memset((void *)patline, 0, sizeof(*patline));
1073 patline->type = Inherit;
1075 pat = (PAT_S *)fs_get(sizeof(*pat));
1076 memset((void *)pat, 0, sizeof(*pat));
1077 pat->inherit = 1;
1079 pat->patline = patline;
1080 patline->first = pat;
1081 patline->last = pat;
1083 return(patline);
1088 * There are three forms that a PATTERN_S has at various times. There is
1089 * the actual PATTERN_S struct which is used internally and is used whenever
1090 * we are actually doing something with the pattern, like filtering or
1091 * something. There is the version that goes in the config file. And there
1092 * is the version the user edits.
1094 * To go between these three forms we have the helper routines
1096 * pattern_to_config
1097 * config_to_pattern
1098 * pattern_to_editlist
1099 * editlist_to_pattern
1101 * Here's what is supposed to be happening. A PATTERN_S is a linked list
1102 * of strings with nothing escaped. That is, a backslash or a comma is
1103 * just in there as a backslash or comma.
1105 * The version the user edits is very similar. Because we have historically
1106 * used commas as separators the user has always had to enter a \, in order
1107 * to put a real comma in one of the items. That is the only difference
1108 * between a PATTERN_S string and the editlist strings. Note that backslashes
1109 * themselves are not escaped. A backslash which is not followed by a comma
1110 * is a backslash. It doesn't escape the following character. That's a bit
1111 * odd, it is that way because most people will never know about this
1112 * backslash stuff but PC-Pine users may have backslashes in folder names.
1114 * The version that goes in the config file has a few transformations made.
1115 * PATTERN_S intermediate_form Config string
1116 * , \, \x2C
1117 * \ \\ \x5C
1118 * / \/
1119 * " \"
1121 * The commas are turned into hex commas so that we can tell the separators
1122 * in the comma-separated lists from those commas.
1123 * The backslashes are escaped because they escape commas.
1124 * The /'s are escaped because they separate pattern pieces.
1125 * The "'s are escaped because they are significant to parse_list when
1126 * parsing the config file.
1127 * hubert - 2004-04-01
1128 * (date is only coincidental!)
1130 * Addendum. The not's are handled separately from all the strings. Not sure
1131 * why that is or if there is a good reason. Nevertheless, now is not the
1132 * time to figure it out so leave it that way.
1133 * hubert - 2004-07-14
1135 PAT_S *
1136 parse_pat(char *str)
1138 PAT_S *pat = NULL;
1139 char *p, *q, *astr, *pstr;
1140 int backslashed;
1141 #define PTRN "pattern="
1142 #define PTRNLEN 8
1143 #define ACTN "action="
1144 #define ACTNLEN 7
1146 if(str)
1147 removing_trailing_white_space(str);
1149 if(!str || !*str || *str == '#')
1150 return(pat);
1152 pat = (PAT_S *)fs_get(sizeof(*pat));
1153 memset((void *)pat, 0, sizeof(*pat));
1155 if((p = srchstr(str, PTRN)) != NULL){
1156 pat->patgrp = (PATGRP_S *)fs_get(sizeof(*pat->patgrp));
1157 memset((void *)pat->patgrp, 0, sizeof(*pat->patgrp));
1158 pat->patgrp->fldr_type = FLDR_DEFL;
1159 pat->patgrp->inabook = IAB_DEFL;
1160 pat->patgrp->cat_lim = -1L;
1162 if((pstr = copy_quoted_string_asis(p+PTRNLEN)) != NULL){
1163 /* move to next slash */
1164 for(q=pstr, backslashed=0; *q; q++){
1165 switch(*q){
1166 case '\\':
1167 backslashed = !backslashed;
1168 break;
1170 case '/':
1171 if(!backslashed){
1172 parse_patgrp_slash(q, pat->patgrp);
1173 if(pat->patgrp->bogus && !pat->raw)
1174 pat->raw = cpystr(str);
1177 /* fall through */
1179 default:
1180 backslashed = 0;
1181 break;
1185 /* we always force a nickname */
1186 if(!pat->patgrp->nick)
1187 pat->patgrp->nick = cpystr("Alternate Role");
1189 fs_give((void **)&pstr);
1193 if((p = srchstr(str, ACTN)) != NULL){
1194 pat->action = (ACTION_S *)fs_get(sizeof(*pat->action));
1195 memset((void *)pat->action, 0, sizeof(*pat->action));
1196 pat->action->startup_rule = IS_NOTSET;
1197 pat->action->repl_type = ROLE_REPL_DEFL;
1198 pat->action->forw_type = ROLE_FORW_DEFL;
1199 pat->action->comp_type = ROLE_COMP_DEFL;
1200 pat->action->nick = cpystr((pat->patgrp && pat->patgrp->nick
1201 && pat->patgrp->nick[0])
1202 ? pat->patgrp->nick : "Alternate Role");
1204 if((astr = copy_quoted_string_asis(p+ACTNLEN)) != NULL){
1205 /* move to next slash */
1206 for(q=astr, backslashed=0; *q; q++){
1207 switch(*q){
1208 case '\\':
1209 backslashed = !backslashed;
1210 break;
1212 case '/':
1213 if(!backslashed){
1214 parse_action_slash(q, pat->action);
1215 if(pat->action->bogus && !pat->raw)
1216 pat->raw = cpystr(str);
1219 /* fall through */
1221 default:
1222 backslashed = 0;
1223 break;
1227 fs_give((void **)&astr);
1229 if(!pat->action->is_a_score)
1230 pat->action->scoreval = 0L;
1232 if(pat->action->is_a_filter)
1233 pat->action->kill = (pat->action->folder
1234 || pat->action->kill == -1) ? 0 : 1;
1235 else{
1236 if(pat->action->folder)
1237 free_pattern(&pat->action->folder);
1240 if(!pat->action->is_a_role){
1241 pat->action->repl_type = ROLE_NOTAROLE_DEFL;
1242 pat->action->forw_type = ROLE_NOTAROLE_DEFL;
1243 pat->action->comp_type = ROLE_NOTAROLE_DEFL;
1244 if(pat->action->from)
1245 mail_free_address(&pat->action->from);
1246 if(pat->action->replyto)
1247 mail_free_address(&pat->action->replyto);
1248 if(pat->action->fcc)
1249 fs_give((void **)&pat->action->fcc);
1250 if(pat->action->litsig)
1251 fs_give((void **)&pat->action->litsig);
1252 if(pat->action->sig)
1253 fs_give((void **)&pat->action->sig);
1254 if(pat->action->template)
1255 fs_give((void **)&pat->action->template);
1256 if(pat->action->cstm)
1257 free_list_array(&pat->action->cstm);
1258 if(pat->action->smtp)
1259 free_list_array(&pat->action->smtp);
1260 if(pat->action->nntp)
1261 free_list_array(&pat->action->nntp);
1262 if(pat->action->inherit_nick)
1263 fs_give((void **)&pat->action->inherit_nick);
1266 if(!pat->action->is_a_incol){
1267 if(pat->action->incol)
1268 free_color_pair(&pat->action->incol);
1271 if(!pat->action->is_a_other){
1272 pat->action->sort_is_set = 0;
1273 pat->action->sortorder = 0;
1274 pat->action->revsort = 0;
1275 pat->action->startup_rule = IS_NOTSET;
1276 if(pat->action->index_format)
1277 fs_give((void **)&pat->action->index_format);
1282 return(pat);
1287 * Fill in one member of patgrp from str.
1289 * The multiple constant strings are lame but it evolved this way from
1290 * previous versions and isn't worth fixing.
1292 void
1293 parse_patgrp_slash(char *str, PATGRP_S *patgrp)
1295 char *p;
1297 if(!patgrp)
1298 alpine_panic("NULL patgrp to parse_patgrp_slash");
1299 else if(!(str && *str)){
1300 alpine_panic("NULL or empty string to parse_patgrp_slash");
1301 patgrp->bogus = 1;
1303 else if(!strncmp(str, "/NICK=", 6))
1304 patgrp->nick = remove_pat_escapes(str+6);
1305 else if(!strncmp(str, "/COMM=", 6))
1306 patgrp->comment = remove_pat_escapes(str+6);
1307 else if(!strncmp(str, "/TO=", 4) || !strncmp(str, "/!TO=", 5))
1308 patgrp->to = parse_pattern("TO", str, 1);
1309 else if(!strncmp(str, "/CC=", 4) || !strncmp(str, "/!CC=", 5))
1310 patgrp->cc = parse_pattern("CC", str, 1);
1311 else if(!strncmp(str, "/RECIP=", 7) || !strncmp(str, "/!RECIP=", 8))
1312 patgrp->recip = parse_pattern("RECIP", str, 1);
1313 else if(!strncmp(str, "/PARTIC=", 8) || !strncmp(str, "/!PARTIC=", 9))
1314 patgrp->partic = parse_pattern("PARTIC", str, 1);
1315 else if(!strncmp(str, "/FROM=", 6) || !strncmp(str, "/!FROM=", 7))
1316 patgrp->from = parse_pattern("FROM", str, 1);
1317 else if(!strncmp(str, "/SENDER=", 8) || !strncmp(str, "/!SENDER=", 9))
1318 patgrp->sender = parse_pattern("SENDER", str, 1);
1319 else if(!strncmp(str, "/NEWS=", 6) || !strncmp(str, "/!NEWS=", 7))
1320 patgrp->news = parse_pattern("NEWS", str, 1);
1321 else if(!strncmp(str, "/SUBJ=", 6) || !strncmp(str, "/!SUBJ=", 7))
1322 patgrp->subj = parse_pattern("SUBJ", str, 1);
1323 else if(!strncmp(str, "/ALL=", 5) || !strncmp(str, "/!ALL=", 6))
1324 patgrp->alltext = parse_pattern("ALL", str, 1);
1325 else if(!strncmp(str, "/BODY=", 6) || !strncmp(str, "/!BODY=", 7))
1326 patgrp->bodytext = parse_pattern("BODY", str, 1);
1327 else if(!strncmp(str, "/KEY=", 5) || !strncmp(str, "/!KEY=", 6))
1328 patgrp->keyword = parse_pattern("KEY", str, 1);
1329 else if(!strncmp(str, "/CHAR=", 6) || !strncmp(str, "/!CHAR=", 7))
1330 patgrp->charsets = parse_pattern("CHAR", str, 1);
1331 else if(!strncmp(str, "/FOLDER=", 8) || !strncmp(str, "/!FOLDER=", 9))
1332 patgrp->folder = parse_pattern("FOLDER", str, 1);
1333 else if(!strncmp(str, "/ABOOKS=", 8) || !strncmp(str, "/!ABOOKS=", 9))
1334 patgrp->abooks = parse_pattern("ABOOKS", str, 1);
1336 * A problem with arbhdrs is that more than one of them can appear in
1337 * the string. We come back here the second time, but we already took
1338 * care of the whole thing on the first pass. Hence the check for
1339 * arbhdr already set.
1341 else if(!strncmp(str, "/ARB", 4) || !strncmp(str, "/!ARB", 5)
1342 || !strncmp(str, "/EARB", 5) || !strncmp(str, "/!EARB", 6)){
1343 if(!patgrp->arbhdr)
1344 patgrp->arbhdr = parse_arbhdr(str);
1345 /* else do nothing */
1347 else if(!strncmp(str, "/SENTDATE=", 10))
1348 patgrp->age_uses_sentdate = 1;
1349 else if(!strncmp(str, "/SCOREI=", 8)){
1350 if((p = remove_pat_escapes(str+8)) != NULL){
1351 if((patgrp->score = parse_intvl(p)) != NULL)
1352 patgrp->do_score = 1;
1354 fs_give((void **)&p);
1357 else if(!strncmp(str, "/AGE=", 5)){
1358 if((p = remove_pat_escapes(str+5)) != NULL){
1359 if((patgrp->age = parse_intvl(p)) != NULL)
1360 patgrp->do_age = 1;
1362 fs_give((void **)&p);
1365 else if(!strncmp(str, "/SIZE=", 6)){
1366 if((p = remove_pat_escapes(str+6)) != NULL){
1367 if((patgrp->size = parse_intvl(p)) != NULL)
1368 patgrp->do_size = 1;
1370 fs_give((void **)&p);
1373 else if(!strncmp(str, "/CATCMD=", 8)){
1374 if((p = remove_pat_escapes(str+8)) != NULL){
1375 int commas = 0;
1376 char *q;
1378 /* count elements in list */
1379 for(q = p; q && *q; q++)
1380 if(*q == ',')
1381 commas++;
1383 patgrp->category_cmd = parse_list(p, commas+1, PL_REMSURRQUOT,NULL);
1384 fs_give((void **)&p);
1387 else if(!strncmp(str, "/CATVAL=", 8)){
1388 if((p = remove_pat_escapes(str+8)) != NULL){
1389 if((patgrp->cat = parse_intvl(p)) != NULL)
1390 patgrp->do_cat = 1;
1392 fs_give((void **)&p);
1395 else if(!strncmp(str, "/CATLIM=", 8)){
1396 if((p = remove_pat_escapes(str+8)) != NULL){
1397 long i;
1399 i = atol(p);
1400 patgrp->cat_lim = i;
1401 fs_give((void **)&p);
1404 else if(!strncmp(str, "/FLDTYPE=", 9)){
1405 if((p = remove_pat_escapes(str+9)) != NULL){
1406 int i;
1407 NAMEVAL_S *v;
1409 for(i = 0; (v = pat_fldr_types(i)); i++)
1410 if(!strucmp(p, v->shortname)){
1411 patgrp->fldr_type = v->value;
1412 break;
1415 fs_give((void **)&p);
1418 else if(!strncmp(str, "/AFROM=", 7)){
1419 if((p = remove_pat_escapes(str+7)) != NULL){
1420 int i;
1421 NAMEVAL_S *v;
1423 for(i = 0; (v = inabook_fldr_types(i)); i++)
1424 if(!strucmp(p, v->shortname)){
1425 patgrp->inabook |= v->value;
1426 break;
1429 /* to match old semantics */
1430 patgrp->inabook |= IAB_FROM;
1431 patgrp->inabook |= IAB_REPLYTO;
1433 fs_give((void **)&p);
1436 else if(!strncmp(str, "/AFROMA=", 8)){
1437 if((p = remove_pat_escapes(str+8)) != NULL){
1439 /* make sure AFROMA comes after AFROM in config lines */
1440 patgrp->inabook &= ~IAB_ADDR_MASK;
1442 if(strchr(p, 'F'))
1443 patgrp->inabook |= IAB_FROM;
1444 if(strchr(p, 'R'))
1445 patgrp->inabook |= IAB_REPLYTO;
1446 if(strchr(p, 'S'))
1447 patgrp->inabook |= IAB_SENDER;
1448 if(strchr(p, 'T'))
1449 patgrp->inabook |= IAB_TO;
1450 if(strchr(p, 'C'))
1451 patgrp->inabook |= IAB_CC;
1453 fs_give((void **)&p);
1456 else if(!strncmp(str, "/STATN=", 7)){
1457 SET_STATUS(str,"/STATN=",patgrp->stat_new);
1459 else if(!strncmp(str, "/STATR=", 7)){
1460 SET_STATUS(str,"/STATR=",patgrp->stat_rec);
1462 else if(!strncmp(str, "/STATI=", 7)){
1463 SET_STATUS(str,"/STATI=",patgrp->stat_imp);
1465 else if(!strncmp(str, "/STATA=", 7)){
1466 SET_STATUS(str,"/STATA=",patgrp->stat_ans);
1468 else if(!strncmp(str, "/STATD=", 7)){
1469 SET_STATUS(str,"/STATD=",patgrp->stat_del);
1471 else if(!strncmp(str, "/8BITS=", 7)){
1472 SET_STATUS(str,"/8BITS=",patgrp->stat_8bitsubj);
1474 else if(!strncmp(str, "/BOM=", 5)){
1475 SET_STATUS(str,"/BOM=",patgrp->stat_bom);
1477 else if(!strncmp(str, "/BOY=", 5)){
1478 SET_STATUS(str,"/BOY=",patgrp->stat_boy);
1480 else{
1481 char save;
1483 patgrp->bogus = 1;
1485 if((p = strindex(str, '=')) != NULL){
1486 save = *(p+1);
1487 *(p+1) = '\0';
1490 dprint((1,
1491 "parse_patgrp_slash(%.20s): unrecognized in \"%s\"\n",
1492 str ? str : "?",
1493 (patgrp && patgrp->nick) ? patgrp->nick : ""));
1494 q_status_message4(SM_ORDER, 1, 3,
1495 "Warning: unrecognized pattern element \"%.20s\"%.20s%.20s%.20s",
1496 str, patgrp->nick ? " in rule \"" : "",
1497 patgrp->nick ? patgrp->nick : "", patgrp->nick ? "\"" : "");
1499 if(p)
1500 *(p+1) = save;
1506 * Fill in one member of action struct from str.
1508 * The multiple constant strings are lame but it evolved this way from
1509 * previous versions and isn't worth fixing.
1511 void
1512 parse_action_slash(char *str, ACTION_S *action)
1514 char *p;
1515 int stateval, i;
1516 NAMEVAL_S *v;
1518 if(!action)
1519 alpine_panic("NULL action to parse_action_slash");
1520 else if(!(str && *str))
1521 alpine_panic("NULL or empty string to parse_action_slash");
1522 else if(!strncmp(str, "/ROLE=1", 7))
1523 action->is_a_role = 1;
1524 else if(!strncmp(str, "/OTHER=1", 8))
1525 action->is_a_other = 1;
1526 else if(!strncmp(str, "/ISINCOL=1", 10))
1527 action->is_a_incol = 1;
1528 else if(!strncmp(str, "/ISSRCH=1", 9))
1529 action->is_a_srch = 1;
1531 * This is unfortunate. If a new filter is set to only set
1532 * state bits it will be interpreted by an older pine which
1533 * doesn't have that feature like a filter that is set to Delete.
1534 * So we change the filter indicator to FILTER=2 to disable the
1535 * filter for older versions.
1537 else if(!strncmp(str, "/FILTER=1", 9) || !strncmp(str, "/FILTER=2", 9))
1538 action->is_a_filter = 1;
1539 else if(!strncmp(str, "/ISSCORE=1", 10))
1540 action->is_a_score = 1;
1541 else if(!strncmp(str, "/SCORE=", 7)){
1542 if((p = remove_pat_escapes(str+7)) != NULL){
1543 long i;
1545 i = atol(p);
1546 if(i >= SCORE_MIN && i <= SCORE_MAX)
1547 action->scoreval = i;
1549 fs_give((void **)&p);
1552 else if(!strncmp(str, "/SCOREHDRTOK=", 13))
1553 action->scorevalhdrtok = config_to_hdrtok(str+13);
1554 else if(!strncmp(str, "/FOLDER=", 8))
1555 action->folder = parse_pattern("FOLDER", str, 1);
1556 else if(!strncmp(str, "/KEYSET=", 8))
1557 action->keyword_set = parse_pattern("KEYSET", str, 1);
1558 else if(!strncmp(str, "/KEYCLR=", 8))
1559 action->keyword_clr = parse_pattern("KEYCLR", str, 1);
1560 else if(!strncmp(str, "/NOKILL=", 8))
1561 action->kill = -1;
1562 else if(!strncmp(str, "/NOTDEL=", 8))
1563 action->move_only_if_not_deleted = 1;
1564 else if(!strncmp(str, "/NONTERM=", 9))
1565 action->non_terminating = 1;
1566 else if(!strncmp(str, "/STATI=", 7)){
1567 stateval = ACT_STAT_LEAVE;
1568 SET_MSGSTATE(str,"/STATI=",stateval);
1569 switch(stateval){
1570 case ACT_STAT_LEAVE:
1571 break;
1572 case ACT_STAT_SET:
1573 action->state_setting_bits |= F_FLAG;
1574 break;
1575 case ACT_STAT_CLEAR:
1576 action->state_setting_bits |= F_UNFLAG;
1577 break;
1580 else if(!strncmp(str, "/STATD=", 7)){
1581 stateval = ACT_STAT_LEAVE;
1582 SET_MSGSTATE(str,"/STATD=",stateval);
1583 switch(stateval){
1584 case ACT_STAT_LEAVE:
1585 break;
1586 case ACT_STAT_SET:
1587 action->state_setting_bits |= F_DEL;
1588 break;
1589 case ACT_STAT_CLEAR:
1590 action->state_setting_bits |= F_UNDEL;
1591 break;
1594 else if(!strncmp(str, "/STATA=", 7)){
1595 stateval = ACT_STAT_LEAVE;
1596 SET_MSGSTATE(str,"/STATA=",stateval);
1597 switch(stateval){
1598 case ACT_STAT_LEAVE:
1599 break;
1600 case ACT_STAT_SET:
1601 action->state_setting_bits |= F_ANS;
1602 break;
1603 case ACT_STAT_CLEAR:
1604 action->state_setting_bits |= F_UNANS;
1605 break;
1608 else if(!strncmp(str, "/STATN=", 7)){
1609 stateval = ACT_STAT_LEAVE;
1610 SET_MSGSTATE(str,"/STATN=",stateval);
1611 switch(stateval){
1612 case ACT_STAT_LEAVE:
1613 break;
1614 case ACT_STAT_SET:
1615 action->state_setting_bits |= F_UNSEEN;
1616 break;
1617 case ACT_STAT_CLEAR:
1618 action->state_setting_bits |= F_SEEN;
1619 break;
1622 else if(!strncmp(str, "/RTYPE=", 7)){
1623 /* reply type */
1624 action->repl_type = ROLE_REPL_DEFL;
1625 if((p = remove_pat_escapes(str+7)) != NULL){
1626 for(i = 0; (v = role_repl_types(i)); i++)
1627 if(!strucmp(p, v->shortname)){
1628 action->repl_type = v->value;
1629 break;
1632 fs_give((void **)&p);
1635 else if(!strncmp(str, "/FTYPE=", 7)){
1636 /* forward type */
1637 action->forw_type = ROLE_FORW_DEFL;
1638 if((p = remove_pat_escapes(str+7)) != NULL){
1639 for(i = 0; (v = role_forw_types(i)); i++)
1640 if(!strucmp(p, v->shortname)){
1641 action->forw_type = v->value;
1642 break;
1645 fs_give((void **)&p);
1648 else if(!strncmp(str, "/CTYPE=", 7)){
1649 /* compose type */
1650 action->comp_type = ROLE_COMP_DEFL;
1651 if((p = remove_pat_escapes(str+7)) != NULL){
1652 for(i = 0; (v = role_comp_types(i)); i++)
1653 if(!strucmp(p, v->shortname)){
1654 action->comp_type = v->value;
1655 break;
1658 fs_give((void **)&p);
1661 else if(!strncmp(str, "/FROM=", 6)){
1662 /* get the from */
1663 if((p = remove_pat_escapes(str+6)) != NULL){
1664 rfc822_parse_adrlist(&action->from, p,
1665 ps_global->maildomain);
1666 fs_give((void **)&p);
1669 else if(!strncmp(str, "/REPL=", 6)){
1670 /* get the reply-to */
1671 if((p = remove_pat_escapes(str+6)) != NULL){
1672 rfc822_parse_adrlist(&action->replyto, p,
1673 ps_global->maildomain);
1674 fs_give((void **)&p);
1677 else if(!strncmp(str, "/FCC=", 5))
1678 action->fcc = remove_pat_escapes(str+5);
1679 else if(!strncmp(str, "/LSIG=", 6))
1680 action->litsig = remove_pat_escapes(str+6);
1681 else if(!strncmp(str, "/SIG=", 5))
1682 action->sig = remove_pat_escapes(str+5);
1683 else if(!strncmp(str, "/TEMPLATE=", 10))
1684 action->template = remove_pat_escapes(str+10);
1685 /* get the custom headers */
1686 else if(!strncmp(str, "/CSTM=", 6)){
1687 if((p = remove_pat_escapes(str+6)) != NULL){
1688 int commas = 0;
1689 char *q;
1691 /* count elements in list */
1692 for(q = p; q && *q; q++)
1693 if(*q == ',')
1694 commas++;
1696 action->cstm = parse_list(p, commas+1, 0, NULL);
1697 fs_give((void **)&p);
1700 else if(!strncmp(str, "/SMTP=", 6)){
1701 if((p = remove_pat_escapes(str+6)) != NULL){
1702 int commas = 0;
1703 char *q;
1705 /* count elements in list */
1706 for(q = p; q && *q; q++)
1707 if(*q == ',')
1708 commas++;
1710 action->smtp = parse_list(p, commas+1, PL_REMSURRQUOT, NULL);
1711 fs_give((void **)&p);
1714 else if(!strncmp(str, "/NNTP=", 6)){
1715 if((p = remove_pat_escapes(str+6)) != NULL){
1716 int commas = 0;
1717 char *q;
1719 /* count elements in list */
1720 for(q = p; q && *q; q++)
1721 if(*q == ',')
1722 commas++;
1724 action->nntp = parse_list(p, commas+1, PL_REMSURRQUOT, NULL);
1725 fs_give((void **)&p);
1728 else if(!strncmp(str, "/INICK=", 7))
1729 action->inherit_nick = remove_pat_escapes(str+7);
1730 else if(!strncmp(str, "/INCOL=", 7)){
1731 if((p = remove_pat_escapes(str+7)) != NULL){
1732 char *fg = NULL, *bg = NULL, *z;
1735 * Color should look like
1736 * /FG=white/BG=red
1738 if((z = srchstr(p, "/FG=")) != NULL)
1739 fg = remove_pat_escapes(z+4);
1740 if((z = srchstr(p, "/BG=")) != NULL)
1741 bg = remove_pat_escapes(z+4);
1743 if(fg && *fg && bg && *bg)
1744 action->incol = new_color_pair(fg, bg);
1746 if(fg)
1747 fs_give((void **)&fg);
1748 if(bg)
1749 fs_give((void **)&bg);
1750 fs_give((void **)&p);
1753 /* per-folder sort */
1754 else if(!strncmp(str, "/SORT=", 6)){
1755 if((p = remove_pat_escapes(str+6)) != NULL){
1756 SortOrder def_sort;
1757 int def_sort_rev;
1759 if(decode_sort(p, &def_sort, &def_sort_rev) != -1){
1760 action->sort_is_set = 1;
1761 action->sortorder = def_sort;
1762 action->revsort = (def_sort_rev ? 1 : 0);
1765 fs_give((void **)&p);
1768 /* per-folder index-format */
1769 else if(!strncmp(str, "/IFORM=", 7))
1770 action->index_format = remove_pat_escapes(str+7);
1771 /* per-folder startup-rule */
1772 else if(!strncmp(str, "/START=", 7)){
1773 if((p = remove_pat_escapes(str+7)) != NULL){
1774 for(i = 0; (v = startup_rules(i)); i++)
1775 if(!strucmp(p, S_OR_L(v))){
1776 action->startup_rule = v->value;
1777 break;
1780 fs_give((void **)&p);
1783 else{
1784 char save;
1786 action->bogus = 1;
1788 if((p = strindex(str, '=')) != NULL){
1789 save = *(p+1);
1790 *(p+1) = '\0';
1793 dprint((1,
1794 "parse_action_slash(%.20s): unrecognized in \"%s\"\n",
1795 str ? str : "?",
1796 (action && action->nick) ? action->nick : ""));
1797 q_status_message4(SM_ORDER, 1, 3,
1798 "Warning: unrecognized pattern action \"%.20s\"%.20s%.20s%.20s",
1799 str, action->nick ? " in rule \"" : "",
1800 action->nick ? action->nick : "", action->nick ? "\"" : "");
1802 if(p)
1803 *(p+1) = save;
1809 * Str looks like (min,max) or a comma-separated list of these.
1811 * Parens are optional if unambiguous, whitespace is ignored.
1812 * If min is left out it is -INF. If max is left out it is INF.
1813 * If only one number and no comma number is min and max is INF.
1815 * Returns the INTVL_S list.
1817 INTVL_S *
1818 parse_intvl(char *str)
1820 char *q;
1821 long left, right;
1822 INTVL_S *ret = NULL, **next;
1824 if(!str)
1825 return(ret);
1827 q = str;
1829 for(;;){
1830 left = right = INTVL_UNDEF;
1832 /* skip to first number */
1833 while(isspace((unsigned char) *q) || *q == LPAREN)
1834 q++;
1836 /* min number */
1837 if(*q == COMMA || !struncmp(q, "-INF", 4))
1838 left = - INTVL_INF;
1839 else if(*q == '-' || isdigit((unsigned char) *q))
1840 left = atol(q);
1842 if(left != INTVL_UNDEF){
1843 /* skip to second number */
1844 while(*q && *q != COMMA && *q != RPAREN)
1845 q++;
1846 if(*q == COMMA)
1847 q++;
1848 while(isspace((unsigned char) *q))
1849 q++;
1851 /* max number */
1852 if(*q == '\0' || *q == RPAREN || !struncmp(q, "INF", 3))
1853 right = INTVL_INF;
1854 else if(*q == '-' || isdigit((unsigned char) *q))
1855 right = atol(q);
1858 if(left == INTVL_UNDEF || right == INTVL_UNDEF
1859 || left > right){
1860 if(left != INTVL_UNDEF || right != INTVL_UNDEF || *q){
1861 if(left != INTVL_UNDEF && right != INTVL_UNDEF
1862 && left > right)
1863 q_status_message1(SM_ORDER, 3, 5,
1864 _("Error: Interval \"%s\", min > max"), str);
1865 else
1866 q_status_message1(SM_ORDER, 3, 5,
1867 _("Error: Interval \"%s\": syntax is (min,max)"), str);
1869 if(ret)
1870 free_intvl(&ret);
1872 ret = NULL;
1875 break;
1877 else{
1878 if(!ret){
1879 ret = (INTVL_S *) fs_get(sizeof(*ret));
1880 memset((void *) ret, 0, sizeof(*ret));
1881 ret->imin = left;
1882 ret->imax = right;
1883 next = &ret->next;
1885 else{
1886 *next = (INTVL_S *) fs_get(sizeof(*ret));
1887 memset((void *) *next, 0, sizeof(*ret));
1888 (*next)->imin = left;
1889 (*next)->imax = right;
1890 next = &(*next)->next;
1893 /* skip to next interval in list */
1894 while(*q && *q != COMMA && *q != RPAREN)
1895 q++;
1896 if(*q == RPAREN)
1897 q++;
1898 while(*q && *q != COMMA)
1899 q++;
1900 if(*q == COMMA)
1901 q++;
1905 return(ret);
1910 * Returns string that looks like "(left,right),(left2,right2)".
1911 * Caller is responsible for freeing memory.
1913 char *
1914 stringform_of_intvl(INTVL_S *intvl)
1916 char *res = NULL;
1918 if(intvl && intvl->imin != INTVL_UNDEF && intvl->imax != INTVL_UNDEF
1919 && intvl->imin <= intvl->imax){
1920 char lbuf[20], rbuf[20], buf[45], *p;
1921 INTVL_S *iv;
1922 int count = 0;
1923 size_t reslen;
1925 /* find a max size and allocate it for the result */
1926 for(iv = intvl;
1927 (iv && iv->imin != INTVL_UNDEF && iv->imax != INTVL_UNDEF
1928 && iv->imin <= iv->imax);
1929 iv = iv->next)
1930 count++;
1932 reslen = count * 50 * sizeof(char);
1933 res = (char *) fs_get(reslen+1);
1934 memset((void *) res, 0, reslen+1);
1935 p = res;
1937 for(iv = intvl;
1938 (iv && iv->imin != INTVL_UNDEF && iv->imax != INTVL_UNDEF
1939 && iv->imin <= iv->imax);
1940 iv = iv->next){
1942 if(iv->imin == - INTVL_INF){
1943 strncpy(lbuf, "-INF", sizeof(lbuf));
1944 lbuf[sizeof(lbuf)-1] = '\0';
1946 else
1947 snprintf(lbuf, sizeof(lbuf), "%ld", iv->imin);
1949 if(iv->imax == INTVL_INF){
1950 strncpy(rbuf, "INF", sizeof(rbuf));
1951 rbuf[sizeof(rbuf)-1] = '\0';
1953 else
1954 snprintf(rbuf, sizeof(rbuf), "%ld", iv->imax);
1956 snprintf(buf, sizeof(buf), "%.1s(%.20s,%.20s)", (p == res) ? "" : ",",
1957 lbuf, rbuf);
1959 sstrncpy(&p, buf, reslen+1 -(p-res));
1962 res[reslen] = '\0';
1965 return(res);
1969 char *
1970 hdrtok_to_stringform(HEADER_TOK_S *hdrtok)
1972 char buf[1024], nbuf[10];
1973 char *res = NULL;
1974 char *p, *ptr;
1976 if(hdrtok){
1977 snprintf(nbuf, sizeof(nbuf), "%d", hdrtok->fieldnum);
1978 ptr = buf;
1979 sstrncpy(&ptr, hdrtok->hdrname ? hdrtok->hdrname : "", sizeof(buf)-(ptr-buf));
1980 sstrncpy(&ptr, "(", sizeof(buf)-(ptr-buf));
1981 sstrncpy(&ptr, nbuf, sizeof(buf)-(ptr-buf));
1982 sstrncpy(&ptr, ",\"", sizeof(buf)-(ptr-buf));
1983 p = hdrtok->fieldseps;
1984 while(p && *p){
1985 if((*p == '\"' || *p == '\\') && ptr-buf < sizeof(buf))
1986 *ptr++ = '\\';
1988 if(ptr-buf < sizeof(buf))
1989 *ptr++ = *p++;
1992 sstrncpy(&ptr, "\")", sizeof(buf)-(ptr-buf));
1994 if(ptr-buf < sizeof(buf))
1995 *ptr = '\0';
1996 else
1997 buf[sizeof(buf)-1] = '\0';
1999 res = cpystr(buf);
2002 return(res);
2006 HEADER_TOK_S *
2007 stringform_to_hdrtok(char *str)
2009 char *p, *q, *w, hdrname[200];
2010 HEADER_TOK_S *hdrtok = NULL;
2012 if(str && *str){
2013 p = str;
2014 hdrname[0] = '\0';
2015 w = hdrname;
2017 if(*p == '\"'){ /* quoted name */
2018 p++;
2019 while(w < hdrname + sizeof(hdrname)-1 && *p != '\"'){
2020 if(*p == '\\')
2021 p++;
2023 *w++ = *p++;
2026 *w = '\0';
2027 if(*p == '\"')
2028 p++;
2030 else{
2031 while(w < hdrname + sizeof(hdrname)-1 &&
2032 !(!(*p & 0x80) && isspace((unsigned char)*p)) &&
2033 *p != '(')
2034 *w++ = *p++;
2036 *w = '\0';
2039 if(hdrname[0])
2040 hdrtok = new_hdrtok(hdrname);
2042 if(hdrtok){
2043 if(*p == '('){
2044 p++;
2046 if(*p && isdigit((unsigned char) *p)){
2047 q = p;
2048 while(*p && isdigit((unsigned char) *p))
2049 p++;
2051 hdrtok->fieldnum = atoi(q);
2053 if(*p == ','){
2054 int j;
2056 p++;
2057 /* don't use default */
2058 if(*p && *p != ')' && hdrtok->fieldseps){
2059 hdrtok->fieldseps[0] = '\0';
2060 hdrtok->fieldsepcnt = 0;
2063 j = 0;
2064 if(*p == '\"' && strchr(p+1, '\"')){ /* quoted */
2065 p++;
2066 while(*p && *p != '\"'){
2067 if(hdrtok->fieldseps)
2068 fs_resize((void **) &hdrtok->fieldseps, j+2);
2070 if(*p == '\\' && *(p+1))
2071 p++;
2073 if(hdrtok->fieldseps){
2074 hdrtok->fieldseps[j++] = *p++;
2075 hdrtok->fieldseps[j] = '\0';
2076 hdrtok->fieldsepcnt = j;
2080 else{
2081 while(*p && *p != ')'){
2082 if(hdrtok->fieldseps)
2083 fs_resize((void **) &hdrtok->fieldseps, j+2);
2085 if(*p == '\\' && *(p+1))
2086 p++;
2088 if(hdrtok->fieldseps){
2089 hdrtok->fieldseps[j++] = *p++;
2090 hdrtok->fieldseps[j] = '\0';
2091 hdrtok->fieldsepcnt = j;
2096 else{
2097 q_status_message(SM_ORDER | SM_DING, 3, 3, "Missing 2nd argument should be field number, a non-negative digit");
2100 else{
2101 q_status_message(SM_ORDER | SM_DING, 3, 3, "1st argument should be field number, a non-negative digit");
2104 else{
2105 q_status_message1(SM_ORDER | SM_DING, 3, 3, "Missing left parenthesis in %s", str);
2108 else{
2109 q_status_message1(SM_ORDER | SM_DING, 3, 3, "Missing header name in %s", str);
2113 return(hdrtok);
2117 char *
2118 hdrtok_to_config(HEADER_TOK_S *hdrtok)
2120 char *ptr, buf[1024], nbuf[10], *p1, *p2, *p3;
2121 char *res = NULL;
2123 if(hdrtok){
2124 snprintf(nbuf, sizeof(nbuf), "%d", hdrtok->fieldnum);
2125 memset(buf, 0, sizeof(buf));
2126 ptr = buf;
2127 sstrncpy(&ptr, "/HN=", sizeof(buf)-(ptr-buf));
2128 sstrncpy(&ptr, (p1=add_pat_escapes(hdrtok->hdrname ? hdrtok->hdrname : "")), sizeof(buf)-(ptr-buf));
2129 sstrncpy(&ptr, "/FN=", sizeof(buf)-(ptr-buf));
2130 sstrncpy(&ptr, (p2=add_pat_escapes(nbuf)), sizeof(buf)-(ptr-buf));
2131 sstrncpy(&ptr, "/FS=", sizeof(buf)-(ptr-buf));
2132 sstrncpy(&ptr, (p3=add_pat_escapes(hdrtok->fieldseps ? hdrtok->fieldseps : "")), sizeof(buf)-(ptr-buf));
2134 buf[sizeof(buf)-1] = '\0';
2135 res = add_pat_escapes(buf);
2137 if(p1)
2138 fs_give((void **)&p1);
2140 if(p2)
2141 fs_give((void **)&p2);
2143 if(p3)
2144 fs_give((void **)&p3);
2147 return(res);
2151 HEADER_TOK_S *
2152 config_to_hdrtok(char *str)
2154 HEADER_TOK_S *hdrtok = NULL;
2155 char *p, *q;
2156 int j;
2158 if(str && *str){
2159 if((q = remove_pat_escapes(str)) != NULL){
2160 char *hn = NULL, *fn = NULL, *fs = NULL, *z;
2162 if((z = srchstr(q, "/HN=")) != NULL)
2163 hn = remove_pat_escapes(z+4);
2164 if((z = srchstr(q, "/FN=")) != NULL)
2165 fn = remove_pat_escapes(z+4);
2166 if((z = srchstr(q, "/FS=")) != NULL)
2167 fs = remove_pat_escapes(z+4);
2169 hdrtok = new_hdrtok(hn);
2170 if(fn)
2171 hdrtok->fieldnum = atoi(fn);
2173 if(fs && *fs){
2174 if(hdrtok->fieldseps){
2175 hdrtok->fieldseps[0] = '\0';
2176 hdrtok->fieldsepcnt = 0;
2179 p = fs;
2180 j = 0;
2181 if(*p == '\"' && strchr(p+1, '\"')){
2182 p++;
2183 while(*p && *p != '\"'){
2184 if(hdrtok->fieldseps)
2185 fs_resize((void **) &hdrtok->fieldseps, j+2);
2187 if(*p == '\\' && *(p+1))
2188 p++;
2190 if(hdrtok->fieldseps){
2191 hdrtok->fieldseps[j++] = *p++;
2192 hdrtok->fieldseps[j] = '\0';
2193 hdrtok->fieldsepcnt = j;
2197 else{
2198 while(*p){
2199 if(hdrtok->fieldseps)
2200 fs_resize((void **) &hdrtok->fieldseps, j+2);
2202 if(*p == '\\' && *(p+1))
2203 p++;
2205 if(hdrtok->fieldseps){
2206 hdrtok->fieldseps[j++] = *p++;
2207 hdrtok->fieldseps[j] = '\0';
2208 hdrtok->fieldsepcnt = j;
2214 if(hn)
2215 fs_give((void **)&hn);
2216 if(fn)
2217 fs_give((void **)&fn);
2218 if(fs)
2219 fs_give((void **)&fs);
2221 fs_give((void **)&q);
2225 return(hdrtok);
2230 * Args -- flags - SCOREUSE_INVALID Mark scores_in_use invalid so that we'll
2231 * recalculate if we want to use it again.
2232 * - SCOREUSE_GET Return whether scores are being used or not.
2234 * Returns -- 0 - Scores not being used at all.
2235 * >0 - Scores are used. The return value consists of flag values
2236 * OR'd together. Possible values are:
2238 * SCOREUSE_INCOLS - scores needed for index line colors
2239 * SCOREUSE_ROLES - scores needed for roles
2240 * SCOREUSE_FILTERS - scores needed for filters
2241 * SCOREUSE_OTHER - scores needed for other stuff
2242 * SCOREUSE_INDEX - scores needed for index drawing
2244 * SCOREUSE_STATEDEP - scores depend on message state
2247 scores_are_used(int flags)
2249 static int scores_in_use = -1;
2250 long type1, type2;
2251 int scores_are_defined, scores_are_used_somewhere = 0;
2252 PAT_STATE pstate1, pstate2;
2254 if(flags & SCOREUSE_INVALID) /* mark invalid so we recalculate next time */
2255 scores_in_use = -1;
2256 else if(scores_in_use == -1){
2259 * Check the patterns to see if scores are potentially
2260 * being used.
2261 * The first_pattern() in the if checks whether there are any
2262 * non-zero scorevals. The loop checks whether any patterns
2263 * use those non-zero scorevals.
2265 type1 = ROLE_SCORE;
2266 type2 = (ROLE_REPLY | ROLE_FORWARD | ROLE_COMPOSE |
2267 ROLE_INCOL | ROLE_DO_FILTER);
2268 scores_are_defined = nonempty_patterns(type1, &pstate1)
2269 && first_pattern(&pstate1);
2270 if(scores_are_defined)
2271 scores_are_used_somewhere =
2272 ((nonempty_patterns(type2, &pstate2) && first_pattern(&pstate2))
2273 || ps_global->a_format_contains_score
2274 || mn_get_sort(ps_global->msgmap) == SortScore);
2276 if(scores_are_used_somewhere){
2277 PAT_S *pat;
2280 * Careful. nonempty_patterns() may call close_pattern()
2281 * which will set scores_in_use to -1! So we have to be
2282 * sure to reset it after we call nonempty_patterns().
2284 scores_in_use = 0;
2285 if(ps_global->a_format_contains_score
2286 || mn_get_sort(ps_global->msgmap) == SortScore)
2287 scores_in_use |= SCOREUSE_INDEX;
2289 if(nonempty_patterns(type2, &pstate2))
2290 for(pat = first_pattern(&pstate2);
2291 pat;
2292 pat = next_pattern(&pstate2))
2293 if(pat->patgrp && !pat->patgrp->bogus && pat->patgrp->do_score){
2294 if(pat->action && pat->action->is_a_incol)
2295 scores_in_use |= SCOREUSE_INCOLS;
2296 if(pat->action && pat->action->is_a_role)
2297 scores_in_use |= SCOREUSE_ROLES;
2298 if(pat->action && pat->action->is_a_filter)
2299 scores_in_use |= SCOREUSE_FILTERS;
2300 if(pat->action && pat->action->is_a_other)
2301 scores_in_use |= SCOREUSE_OTHER;
2305 * Note whether scores depend on message state or not.
2307 if(scores_in_use)
2308 for(pat = first_pattern(&pstate1);
2309 pat;
2310 pat = next_pattern(&pstate1))
2311 if(patgrp_depends_on_active_state(pat->patgrp)){
2312 scores_in_use |= SCOREUSE_STATEDEP;
2313 break;
2317 else
2318 scores_in_use = 0;
2321 return((scores_in_use == -1) ? 0 : scores_in_use);
2326 patgrp_depends_on_state(PATGRP_S *patgrp)
2328 return(patgrp && (patgrp_depends_on_active_state(patgrp)
2329 || patgrp->stat_rec != PAT_STAT_EITHER));
2334 * Recent doesn't count for this function because it doesn't change while
2335 * the mailbox is open.
2338 patgrp_depends_on_active_state(PATGRP_S *patgrp)
2340 return(patgrp && !patgrp->bogus
2341 && (patgrp->stat_new != PAT_STAT_EITHER ||
2342 patgrp->stat_del != PAT_STAT_EITHER ||
2343 patgrp->stat_imp != PAT_STAT_EITHER ||
2344 patgrp->stat_ans != PAT_STAT_EITHER ||
2345 patgrp->keyword));
2350 * Look for label in str and return a pointer to parsed string.
2351 * Actually, we look for "label=" or "!label=", the second means NOT.
2352 * Converts from string from patterns file which looks like
2353 * /NEWS=comp.mail.,comp.mail.pine/TO=...
2354 * This is the string that came from pattern="string" with the pattern=
2355 * and outer quotes removed.
2356 * This converts the string to a PATTERN_S list and returns
2357 * an allocated copy.
2359 PATTERN_S *
2360 parse_pattern(char *label, char *str, int hex_to_backslashed)
2362 char copy[50]; /* local copy of label */
2363 char copynot[50]; /* local copy of label, NOT'ed */
2364 char *q, *labeled_str;
2365 PATTERN_S *head = NULL;
2367 if(!label || !str)
2368 return(NULL);
2370 q = copy;
2371 sstrncpy(&q, "/", sizeof(copy));
2372 sstrncpy(&q, label, sizeof(copy) - (q-copy));
2373 sstrncpy(&q, "=", sizeof(copy) - (q-copy));
2374 copy[sizeof(copy)-1] = '\0';
2375 q = copynot;
2376 sstrncpy(&q, "/!", sizeof(copynot));
2377 sstrncpy(&q, label, sizeof(copynot) - (q-copynot));
2378 sstrncpy(&q, "=", sizeof(copynot) - (q-copynot));
2379 copynot[sizeof(copynot)-1] = '\0';
2381 if(hex_to_backslashed){
2382 if((q = srchstr(str, copy)) != NULL){
2383 head = config_to_pattern(q+strlen(copy));
2385 else if((q = srchstr(str, copynot)) != NULL){
2386 head = config_to_pattern(q+strlen(copynot));
2387 head->not = 1;
2390 else{
2391 if((q = srchstr(str, copy)) != NULL){
2392 if((labeled_str =
2393 remove_backslash_escapes(q+strlen(copy))) != NULL){
2394 head = string_to_pattern(labeled_str);
2395 fs_give((void **)&labeled_str);
2398 else if((q = srchstr(str, copynot)) != NULL){
2399 if((labeled_str =
2400 remove_backslash_escapes(q+strlen(copynot))) != NULL){
2401 head = string_to_pattern(labeled_str);
2402 head->not = 1;
2403 fs_give((void **)&labeled_str);
2408 return(head);
2413 * Look for /ARB's in str and return a pointer to parsed ARBHDR_S.
2414 * Actually, we look for /!ARB and /!EARB as well. Those mean NOT.
2415 * Converts from string from patterns file which looks like
2416 * /ARB<fieldname1>=pattern/.../ARB<fieldname2>=pattern...
2417 * This is the string that came from pattern="string" with the pattern=
2418 * and outer quotes removed.
2419 * This converts the string to a ARBHDR_S list and returns
2420 * an allocated copy.
2422 ARBHDR_S *
2423 parse_arbhdr(char *str)
2425 char *q, *s, *equals, *noesc;
2426 int not, empty, skip;
2427 ARBHDR_S *ahdr = NULL, *a, *aa;
2428 PATTERN_S *p = NULL;
2430 if(!str)
2431 return(NULL);
2433 aa = NULL;
2434 for(s = str; (q = next_arb(s)); s = q+1){
2435 not = (q[1] == '!') ? 1 : 0;
2436 empty = (q[not+1] == 'E') ? 1 : 0;
2437 skip = 4 + not + empty;
2438 if((noesc = remove_pat_escapes(q+skip)) != NULL){
2439 if(*noesc != '=' && (equals = strindex(noesc, '=')) != NULL){
2440 a = (ARBHDR_S *)fs_get(sizeof(*a));
2441 memset((void *)a, 0, sizeof(*a));
2442 *equals = '\0';
2443 a->isemptyval = empty;
2444 a->field = cpystr(noesc);
2445 if(empty)
2446 a->p = string_to_pattern("");
2447 else if(*(equals+1) &&
2448 (p = string_to_pattern(equals+1)) != NULL)
2449 a->p = p;
2451 if(not && a->p)
2452 a->p->not = 1;
2454 /* keep them in the same order */
2455 if(aa){
2456 aa->next = a;
2457 aa = aa->next;
2459 else{
2460 ahdr = a;
2461 aa = ahdr;
2465 fs_give((void **)&noesc);
2469 return(ahdr);
2473 char *
2474 next_arb(char *start)
2476 char *q1, *q2, *q3, *q4, *p;
2478 q1 = srchstr(start, "/ARB");
2479 q2 = srchstr(start, "/!ARB");
2480 q3 = srchstr(start, "/EARB");
2481 q4 = srchstr(start, "/!EARB");
2483 p = q1;
2484 if(!p || (q2 && q2 < p))
2485 p = q2;
2486 if(!p || (q3 && q3 < p))
2487 p = q3;
2488 if(!p || (q4 && q4 < p))
2489 p = q4;
2491 return(p);
2496 * Converts a string to a PATTERN_S list and returns an
2497 * allocated copy. The source string looks like
2498 * string1,string2,...
2499 * Commas and backslashes may be backslash-escaped in the original string
2500 * in order to include actual commas and backslashes in the pattern.
2501 * So \, is an actual comma and , is the separator character.
2503 PATTERN_S *
2504 string_to_pattern(char *str)
2506 char *q, *s, *workspace;
2507 PATTERN_S *p, *head = NULL, **nextp;
2509 if(!str)
2510 return(head);
2513 * We want an empty string to cause an empty substring in the pattern
2514 * instead of returning a NULL pattern. That can be used as a way to
2515 * match any header. For example, if all the patterns but the news
2516 * pattern were null and the news pattern was a substring of "" then
2517 * we use that to match any message with a newsgroups header.
2519 if(!*str){
2520 head = (PATTERN_S *)fs_get(sizeof(*p));
2521 memset((void *)head, 0, sizeof(*head));
2522 head->substring = cpystr("");
2524 else{
2525 nextp = &head;
2526 workspace = (char *)fs_get((strlen(str)+1) * sizeof(char));
2527 s = workspace;
2528 *s = '\0';
2529 q = str;
2530 do {
2531 switch(*q){
2532 case COMMA:
2533 case '\0':
2534 *s = '\0';
2535 removing_leading_and_trailing_white_space(workspace);
2536 p = (PATTERN_S *)fs_get(sizeof(*p));
2537 memset((void *)p, 0, sizeof(*p));
2538 p->substring = cpystr(workspace);
2540 convert_possibly_encoded_str_to_utf8(&p->substring);
2542 *nextp = p;
2543 nextp = &p->next;
2544 s = workspace;
2545 *s = '\0';
2546 break;
2548 case BSLASH:
2549 if(*(q+1) == COMMA || *(q+1) == BSLASH)
2550 *s++ = *(++q);
2551 else
2552 *s++ = *q;
2554 break;
2556 default:
2557 *s++ = *q;
2558 break;
2560 } while(*q++);
2562 fs_give((void **)&workspace);
2565 return(head);
2570 * Converts a PATTERN_S list to a string.
2571 * The resulting string is allocated here and looks like
2572 * string1,string2,...
2573 * Commas and backslashes in the original pattern
2574 * end up backslash-escaped in the string.
2576 char *
2577 pattern_to_string(PATTERN_S *pattern)
2579 PATTERN_S *p;
2580 char *result = NULL, *q, *s;
2581 size_t n;
2583 if(!pattern)
2584 return(result);
2586 /* how much space is needed? */
2587 n = 0;
2588 for(p = pattern; p; p = p->next){
2589 n += (p == pattern) ? 0 : 1;
2590 for(s = p->substring; s && *s; s++){
2591 if(*s == COMMA || *s == BSLASH)
2592 n++;
2594 n++;
2598 q = result = (char *)fs_get(++n);
2599 for(p = pattern; p; p = p->next){
2600 if(p != pattern)
2601 *q++ = COMMA;
2603 for(s = p->substring; s && *s; s++){
2604 if(*s == COMMA || *s == BSLASH)
2605 *q++ = '\\';
2607 *q++ = *s;
2611 *q = '\0';
2613 return(result);
2618 * Do the escaping necessary to take a string for a pattern into a comma-
2619 * separated string with escapes suitable for the config file.
2620 * Returns an allocated copy of that string.
2621 * In particular
2622 * , -> \, -> \x2C
2623 * \ -> \\ -> \x5C
2624 * " -> \"
2625 * / -> \/
2627 char *
2628 pattern_to_config(PATTERN_S *pat)
2630 char *s, *res = NULL;
2632 s = pattern_to_string(pat);
2633 if(s){
2634 res = add_pat_escapes(s);
2635 fs_give((void **) &s);
2638 return(res);
2642 * Opposite of pattern_to_config.
2644 PATTERN_S *
2645 config_to_pattern(char *str)
2647 char *s;
2648 PATTERN_S *pat = NULL;
2650 s = remove_pat_escapes(str);
2651 if(s){
2652 pat = string_to_pattern(s);
2653 fs_give((void **) &s);
2656 return(pat);
2661 * Converts an array of strings to a PATTERN_S list and returns an
2662 * allocated copy.
2663 * The list strings may not contain commas directly, because the UI turns
2664 * those into separate list members. Instead, the user types \, and
2665 * that backslash comma is converted to a comma here.
2666 * It is a bit odd. Backslash itself is not escaped. A backslash which is
2667 * not followed by a comma is a literal backslash, a backslash followed by
2668 * a comma is a comma.
2670 PATTERN_S *
2671 editlist_to_pattern(char **list)
2673 PATTERN_S *head = NULL;
2675 if(!(list && *list))
2676 return(head);
2679 * We want an empty string to cause an empty substring in the pattern
2680 * instead of returning a NULL pattern. That can be used as a way to
2681 * match any header. For example, if all the patterns but the news
2682 * pattern were null and the news pattern was a substring of "" then
2683 * we use that to match any message with a newsgroups header.
2685 if(!list[0][0]){
2686 head = (PATTERN_S *) fs_get(sizeof(*head));
2687 memset((void *) head, 0, sizeof(*head));
2688 head->substring = cpystr("");
2690 else{
2691 char *str, *s, *q, *workspace = NULL;
2692 size_t l = 0;
2693 PATTERN_S *p, **nextp;
2694 int i;
2696 nextp = &head;
2697 for(i = 0; (str = list[i]); i++){
2698 if(str[0]){
2699 if(!workspace){
2700 l = strlen(str) + 1;
2701 workspace = (char *) fs_get(l * sizeof(char));
2703 else if(strlen(str) + 1 > l){
2704 l = strlen(str) + 1;
2705 fs_give((void **) &workspace);
2706 workspace = (char *) fs_get(l * sizeof(char));
2709 s = workspace;
2710 *s = '\0';
2711 q = str;
2712 do {
2713 switch(*q){
2714 case '\0':
2715 *s = '\0';
2716 removing_leading_and_trailing_white_space(workspace);
2717 p = (PATTERN_S *) fs_get(sizeof(*p));
2718 memset((void *) p, 0, sizeof(*p));
2719 p->substring = cpystr(workspace);
2720 *nextp = p;
2721 nextp = &p->next;
2722 s = workspace;
2723 *s = '\0';
2724 break;
2726 case BSLASH:
2727 if(*(q+1) == COMMA)
2728 *s++ = *(++q);
2729 else
2730 *s++ = *q;
2732 break;
2734 default:
2735 *s++ = *q;
2736 break;
2738 } while(*q++);
2743 return(head);
2748 * Converts a PATTERN_S to an array of strings and returns an allocated copy.
2749 * Commas are converted to backslash-comma, because the text_tool UI uses
2750 * commas to separate items.
2751 * It is a bit odd. Backslash itself is not escaped. A backslash which is
2752 * not followed by a comma is a literal backslash, a backslash followed by
2753 * a comma is a comma.
2755 char **
2756 pattern_to_editlist(PATTERN_S *pat)
2758 int cnt, i;
2759 PATTERN_S *p;
2760 char **list = NULL;
2762 if(!pat)
2763 return(list);
2765 /* how many in list? */
2766 for(cnt = 0, p = pat; p; p = p->next)
2767 cnt++;
2769 list = (char **) fs_get((cnt + 1) * sizeof(*list));
2770 memset((void *) list, 0, (cnt + 1) * sizeof(*list));
2772 for(i = 0, p = pat; p; p = p->next, i++)
2773 list[i] = add_comma_escapes(p->substring);
2775 return(list);
2779 PATGRP_S *
2780 nick_to_patgrp(char *nick, int rflags)
2782 PAT_S *pat;
2783 PAT_STATE pstate;
2784 PATGRP_S *patgrp = NULL;
2786 if(!(nick && *nick
2787 && nonempty_patterns(rflags, &pstate) && first_pattern(&pstate)))
2788 return(patgrp);
2790 for(pat = first_pattern(&pstate);
2791 !patgrp && pat;
2792 pat = next_pattern(&pstate))
2793 if(pat->patgrp && pat->patgrp->nick && !strcmp(pat->patgrp->nick, nick))
2794 patgrp = copy_patgrp(pat->patgrp);
2796 return(patgrp);
2801 * Must be called with a pstate, we don't check for it.
2802 * It respects the cur_rflag_num in pstate. That is, it doesn't start over
2803 * at i=1, it starts at cur_rflag_num.
2805 PAT_S *
2806 first_any_pattern(PAT_STATE *pstate)
2808 PAT_LINE_S *patline = NULL;
2809 int i;
2810 long local_rflag;
2813 * The rest of pstate should be set before coming here.
2814 * In particular, the rflags should be set by a call to nonempty_patterns
2815 * or any_patterns, and cur_rflag_num should be set.
2817 pstate->patlinecurrent = NULL;
2818 pstate->patcurrent = NULL;
2821 * The order of these is important. It is the same as the order
2822 * used for next_any_pattern and opposite of the order used by
2823 * last and prev. For next_any's benefit, we allow cur_rflag_num to
2824 * start us out past the first set.
2826 for(i = pstate->cur_rflag_num; i <= PATTERN_N; i++){
2828 local_rflag = 0L;
2830 switch(i){
2831 case 1:
2832 local_rflag = ROLE_DO_SRCH & CANONICAL_RFLAGS(pstate->rflags);
2833 break;
2835 case 2:
2836 local_rflag = ROLE_DO_INCOLS & CANONICAL_RFLAGS(pstate->rflags);
2837 break;
2839 case 3:
2840 local_rflag = ROLE_DO_ROLES & CANONICAL_RFLAGS(pstate->rflags);
2841 break;
2843 case 4:
2844 local_rflag = ROLE_DO_FILTER & CANONICAL_RFLAGS(pstate->rflags);
2845 break;
2847 case 5:
2848 local_rflag = ROLE_DO_SCORES & CANONICAL_RFLAGS(pstate->rflags);
2849 break;
2851 case 6:
2852 local_rflag = ROLE_DO_OTHER & CANONICAL_RFLAGS(pstate->rflags);
2853 break;
2855 case 7:
2856 local_rflag = ROLE_OLD_FILT & CANONICAL_RFLAGS(pstate->rflags);
2857 break;
2859 case 8:
2860 local_rflag = ROLE_OLD_SCORE & CANONICAL_RFLAGS(pstate->rflags);
2861 break;
2863 case PATTERN_N:
2864 local_rflag = ROLE_OLD_PAT & CANONICAL_RFLAGS(pstate->rflags);
2865 break;
2868 if(local_rflag){
2869 SET_PATTYPE(local_rflag | (pstate->rflags & PAT_USE_MASK));
2871 if(*cur_pat_h){
2872 /* Find first patline with a pat */
2873 for(patline = (*cur_pat_h)->patlinehead;
2874 patline && !patline->first;
2875 patline = patline->next)
2879 if(patline){
2880 pstate->cur_rflag_num = i;
2881 pstate->patlinecurrent = patline;
2882 pstate->patcurrent = patline->first;
2886 if(pstate->patcurrent)
2887 break;
2890 return(pstate->patcurrent);
2895 * Return first pattern of the specified types. These types were set by a
2896 * previous call to any_patterns or nonempty_patterns.
2898 * Args -- pstate pattern state. This is set here and passed back for
2899 * use by next_pattern. Must be non-null.
2900 * It must have been initialized previously by a call to
2901 * nonempty_patterns or any_patterns.
2903 PAT_S *
2904 first_pattern(PAT_STATE *pstate)
2906 PAT_S *pat;
2907 long rflags;
2909 pstate->cur_rflag_num = 1;
2911 rflags = pstate->rflags;
2913 for(pat = first_any_pattern(pstate);
2914 pat && !((pat->action &&
2915 ((rflags & ROLE_DO_ROLES && pat->action->is_a_role) ||
2916 (rflags & (ROLE_DO_INCOLS|ROLE_INCOL) &&
2917 pat->action->is_a_incol) ||
2918 (rflags & ROLE_DO_OTHER && pat->action->is_a_other) ||
2919 (rflags & ROLE_DO_SRCH && pat->action->is_a_srch) ||
2920 (rflags & ROLE_DO_SCORES && pat->action->is_a_score) ||
2921 (rflags & ROLE_SCORE && (pat->action->scoreval
2922 || pat->action->scorevalhdrtok)) ||
2923 (rflags & ROLE_DO_FILTER && pat->action->is_a_filter) ||
2924 (rflags & ROLE_REPLY &&
2925 (pat->action->repl_type == ROLE_REPL_YES ||
2926 pat->action->repl_type == ROLE_REPL_NOCONF)) ||
2927 (rflags & ROLE_FORWARD &&
2928 (pat->action->forw_type == ROLE_FORW_YES ||
2929 pat->action->forw_type == ROLE_FORW_NOCONF)) ||
2930 (rflags & ROLE_COMPOSE &&
2931 (pat->action->comp_type == ROLE_COMP_YES ||
2932 pat->action->comp_type == ROLE_COMP_NOCONF)) ||
2933 (rflags & ROLE_OLD_FILT) ||
2934 (rflags & ROLE_OLD_SCORE) ||
2935 (rflags & ROLE_OLD_PAT)))
2937 pat->inherit);
2938 pat = next_any_pattern(pstate))
2941 return(pat);
2946 * Just like first_any_pattern.
2948 PAT_S *
2949 last_any_pattern(PAT_STATE *pstate)
2951 PAT_LINE_S *patline = NULL;
2952 int i;
2953 long local_rflag;
2956 * The rest of pstate should be set before coming here.
2957 * In particular, the rflags should be set by a call to nonempty_patterns
2958 * or any_patterns, and cur_rflag_num should be set.
2960 pstate->patlinecurrent = NULL;
2961 pstate->patcurrent = NULL;
2963 for(i = pstate->cur_rflag_num; i >= 1; i--){
2965 local_rflag = 0L;
2967 switch(i){
2968 case 1:
2969 local_rflag = ROLE_DO_SRCH & CANONICAL_RFLAGS(pstate->rflags);
2970 break;
2972 case 2:
2973 local_rflag = ROLE_DO_INCOLS & CANONICAL_RFLAGS(pstate->rflags);
2974 break;
2976 case 3:
2977 local_rflag = ROLE_DO_ROLES & CANONICAL_RFLAGS(pstate->rflags);
2978 break;
2980 case 4:
2981 local_rflag = ROLE_DO_FILTER & CANONICAL_RFLAGS(pstate->rflags);
2982 break;
2984 case 5:
2985 local_rflag = ROLE_DO_SCORES & CANONICAL_RFLAGS(pstate->rflags);
2986 break;
2988 case 6:
2989 local_rflag = ROLE_DO_OTHER & CANONICAL_RFLAGS(pstate->rflags);
2990 break;
2992 case 7:
2993 local_rflag = ROLE_OLD_FILT & CANONICAL_RFLAGS(pstate->rflags);
2994 break;
2996 case 8:
2997 local_rflag = ROLE_OLD_SCORE & CANONICAL_RFLAGS(pstate->rflags);
2998 break;
3000 case PATTERN_N:
3001 local_rflag = ROLE_OLD_PAT & CANONICAL_RFLAGS(pstate->rflags);
3002 break;
3005 if(local_rflag){
3006 SET_PATTYPE(local_rflag | (pstate->rflags & PAT_USE_MASK));
3008 pstate->patlinecurrent = NULL;
3009 pstate->patcurrent = NULL;
3011 if(*cur_pat_h){
3012 /* Find last patline with a pat */
3013 for(patline = (*cur_pat_h)->patlinehead;
3014 patline;
3015 patline = patline->next)
3016 if(patline->last)
3017 pstate->patlinecurrent = patline;
3019 if(pstate->patlinecurrent)
3020 pstate->patcurrent = pstate->patlinecurrent->last;
3023 if(pstate->patcurrent)
3024 pstate->cur_rflag_num = i;
3026 if(pstate->patcurrent)
3027 break;
3031 return(pstate->patcurrent);
3036 * Return last pattern of the specified types. These types were set by a
3037 * previous call to any_patterns or nonempty_patterns.
3039 * Args -- pstate pattern state. This is set here and passed back for
3040 * use by prev_pattern. Must be non-null.
3041 * It must have been initialized previously by a call to
3042 * nonempty_patterns or any_patterns.
3044 PAT_S *
3045 last_pattern(PAT_STATE *pstate)
3047 PAT_S *pat;
3048 long rflags;
3050 pstate->cur_rflag_num = PATTERN_N;
3052 rflags = pstate->rflags;
3054 for(pat = last_any_pattern(pstate);
3055 pat && !((pat->action &&
3056 ((rflags & ROLE_DO_ROLES && pat->action->is_a_role) ||
3057 (rflags & (ROLE_DO_INCOLS|ROLE_INCOL) &&
3058 pat->action->is_a_incol) ||
3059 (rflags & ROLE_DO_OTHER && pat->action->is_a_other) ||
3060 (rflags & ROLE_DO_SRCH && pat->action->is_a_srch) ||
3061 (rflags & ROLE_DO_SCORES && pat->action->is_a_score) ||
3062 (rflags & ROLE_SCORE && (pat->action->scoreval
3063 || pat->action->scorevalhdrtok)) ||
3064 (rflags & ROLE_DO_FILTER && pat->action->is_a_filter) ||
3065 (rflags & ROLE_REPLY &&
3066 (pat->action->repl_type == ROLE_REPL_YES ||
3067 pat->action->repl_type == ROLE_REPL_NOCONF)) ||
3068 (rflags & ROLE_FORWARD &&
3069 (pat->action->forw_type == ROLE_FORW_YES ||
3070 pat->action->forw_type == ROLE_FORW_NOCONF)) ||
3071 (rflags & ROLE_COMPOSE &&
3072 (pat->action->comp_type == ROLE_COMP_YES ||
3073 pat->action->comp_type == ROLE_COMP_NOCONF)) ||
3074 (rflags & ROLE_OLD_FILT) ||
3075 (rflags & ROLE_OLD_SCORE) ||
3076 (rflags & ROLE_OLD_PAT)))
3078 pat->inherit);
3079 pat = prev_any_pattern(pstate))
3082 return(pat);
3087 * This assumes that pstate is valid.
3089 PAT_S *
3090 next_any_pattern(PAT_STATE *pstate)
3092 PAT_LINE_S *patline;
3094 if(pstate->patlinecurrent){
3095 if(pstate->patcurrent && pstate->patcurrent->next)
3096 pstate->patcurrent = pstate->patcurrent->next;
3097 else{
3098 /* Find next patline with a pat */
3099 for(patline = pstate->patlinecurrent->next;
3100 patline && !patline->first;
3101 patline = patline->next)
3104 if(patline){
3105 pstate->patlinecurrent = patline;
3106 pstate->patcurrent = patline->first;
3108 else{
3109 pstate->patlinecurrent = NULL;
3110 pstate->patcurrent = NULL;
3115 /* we've reached the last, try the next rflag_num (the next pattern type) */
3116 if(!pstate->patcurrent){
3117 pstate->cur_rflag_num++;
3118 pstate->patcurrent = first_any_pattern(pstate);
3121 return(pstate->patcurrent);
3126 * Return next pattern of the specified types. These types were set by a
3127 * previous call to any_patterns or nonempty_patterns.
3129 * Args -- pstate pattern state. This is set by first_pattern or last_pattern.
3131 PAT_S *
3132 next_pattern(PAT_STATE *pstate)
3134 PAT_S *pat;
3135 long rflags;
3137 rflags = pstate->rflags;
3139 for(pat = next_any_pattern(pstate);
3140 pat && !((pat->action &&
3141 ((rflags & ROLE_DO_ROLES && pat->action->is_a_role) ||
3142 (rflags & (ROLE_DO_INCOLS|ROLE_INCOL) &&
3143 pat->action->is_a_incol) ||
3144 (rflags & ROLE_DO_OTHER && pat->action->is_a_other) ||
3145 (rflags & ROLE_DO_SRCH && pat->action->is_a_srch) ||
3146 (rflags & ROLE_DO_SCORES && pat->action->is_a_score) ||
3147 (rflags & ROLE_SCORE && (pat->action->scoreval
3148 || pat->action->scorevalhdrtok)) ||
3149 (rflags & ROLE_DO_FILTER && pat->action->is_a_filter) ||
3150 (rflags & ROLE_REPLY &&
3151 (pat->action->repl_type == ROLE_REPL_YES ||
3152 pat->action->repl_type == ROLE_REPL_NOCONF)) ||
3153 (rflags & ROLE_FORWARD &&
3154 (pat->action->forw_type == ROLE_FORW_YES ||
3155 pat->action->forw_type == ROLE_FORW_NOCONF)) ||
3156 (rflags & ROLE_COMPOSE &&
3157 (pat->action->comp_type == ROLE_COMP_YES ||
3158 pat->action->comp_type == ROLE_COMP_NOCONF)) ||
3159 (rflags & ROLE_OLD_FILT) ||
3160 (rflags & ROLE_OLD_SCORE) ||
3161 (rflags & ROLE_OLD_PAT)))
3163 pat->inherit);
3164 pat = next_any_pattern(pstate))
3167 return(pat);
3172 * This assumes that pstate is valid.
3174 PAT_S *
3175 prev_any_pattern(PAT_STATE *pstate)
3177 PAT_LINE_S *patline;
3179 if(pstate->patlinecurrent){
3180 if(pstate->patcurrent && pstate->patcurrent->prev)
3181 pstate->patcurrent = pstate->patcurrent->prev;
3182 else{
3183 /* Find prev patline with a pat */
3184 for(patline = pstate->patlinecurrent->prev;
3185 patline && !patline->last;
3186 patline = patline->prev)
3189 if(patline){
3190 pstate->patlinecurrent = patline;
3191 pstate->patcurrent = patline->last;
3193 else{
3194 pstate->patlinecurrent = NULL;
3195 pstate->patcurrent = NULL;
3200 if(!pstate->patcurrent){
3201 pstate->cur_rflag_num--;
3202 pstate->patcurrent = last_any_pattern(pstate);
3205 return(pstate->patcurrent);
3210 * Return prev pattern of the specified types. These types were set by a
3211 * previous call to any_patterns or nonempty_patterns.
3213 * Args -- pstate pattern state. This is set by first_pattern or last_pattern.
3215 PAT_S *
3216 prev_pattern(PAT_STATE *pstate)
3218 PAT_S *pat;
3219 long rflags;
3221 rflags = pstate->rflags;
3223 for(pat = prev_any_pattern(pstate);
3224 pat && !((pat->action &&
3225 ((rflags & ROLE_DO_ROLES && pat->action->is_a_role) ||
3226 (rflags & (ROLE_DO_INCOLS|ROLE_INCOL) &&
3227 pat->action->is_a_incol) ||
3228 (rflags & ROLE_DO_OTHER && pat->action->is_a_other) ||
3229 (rflags & ROLE_DO_SRCH && pat->action->is_a_srch) ||
3230 (rflags & ROLE_DO_SCORES && pat->action->is_a_score) ||
3231 (rflags & ROLE_SCORE && (pat->action->scoreval
3232 || pat->action->scorevalhdrtok)) ||
3233 (rflags & ROLE_DO_FILTER && pat->action->is_a_filter) ||
3234 (rflags & ROLE_REPLY &&
3235 (pat->action->repl_type == ROLE_REPL_YES ||
3236 pat->action->repl_type == ROLE_REPL_NOCONF)) ||
3237 (rflags & ROLE_FORWARD &&
3238 (pat->action->forw_type == ROLE_FORW_YES ||
3239 pat->action->forw_type == ROLE_FORW_NOCONF)) ||
3240 (rflags & ROLE_COMPOSE &&
3241 (pat->action->comp_type == ROLE_COMP_YES ||
3242 pat->action->comp_type == ROLE_COMP_NOCONF)) ||
3243 (rflags & ROLE_OLD_FILT) ||
3244 (rflags & ROLE_OLD_SCORE) ||
3245 (rflags & ROLE_OLD_PAT)))
3247 pat->inherit);
3248 pat = prev_any_pattern(pstate))
3251 return(pat);
3256 * Rflags may be more than one pattern type OR'd together.
3259 write_patterns(long int rflags)
3261 int canon_rflags;
3262 int err = 0;
3264 dprint((7, "write_patterns(0x%x)\n", rflags));
3266 canon_rflags = CANONICAL_RFLAGS(rflags);
3268 if(canon_rflags & ROLE_DO_INCOLS)
3269 err += sub_write_patterns(ROLE_DO_INCOLS | (rflags & PAT_USE_MASK));
3270 if(!err && canon_rflags & ROLE_DO_OTHER)
3271 err += sub_write_patterns(ROLE_DO_OTHER | (rflags & PAT_USE_MASK));
3272 if(!err && canon_rflags & ROLE_DO_FILTER)
3273 err += sub_write_patterns(ROLE_DO_FILTER | (rflags & PAT_USE_MASK));
3274 if(!err && canon_rflags & ROLE_DO_SCORES)
3275 err += sub_write_patterns(ROLE_DO_SCORES | (rflags & PAT_USE_MASK));
3276 if(!err && canon_rflags & ROLE_DO_ROLES)
3277 err += sub_write_patterns(ROLE_DO_ROLES | (rflags & PAT_USE_MASK));
3278 if(!err && canon_rflags & ROLE_DO_SRCH)
3279 err += sub_write_patterns(ROLE_DO_SRCH | (rflags & PAT_USE_MASK));
3281 if(!err && !(rflags & PAT_USE_CHANGED))
3282 write_pinerc(ps_global, (rflags & PAT_USE_MAIN) ? Main : Post, WRP_NONE);
3284 return(err);
3289 sub_write_patterns(long int rflags)
3291 int err = 0, lineno = 0;
3292 char **lvalue = NULL;
3293 PAT_LINE_S *patline;
3295 SET_PATTYPE(rflags);
3297 if(!(*cur_pat_h)){
3298 q_status_message(SM_ORDER | SM_DING, 3, 4,
3299 "Unknown error saving patterns");
3300 return(-1);
3303 if((*cur_pat_h)->dirtypinerc){
3304 /* Count how many lines will be in patterns variable */
3305 for(patline = (*cur_pat_h)->patlinehead;
3306 patline;
3307 patline = patline->next)
3308 lineno++;
3310 lvalue = (char **)fs_get((lineno+1)*sizeof(char *));
3311 memset(lvalue, 0, (lineno+1) * sizeof(char *));
3314 for(patline = (*cur_pat_h)->patlinehead, lineno = 0;
3315 !err && patline;
3316 patline = patline->next, lineno++){
3317 if(patline->type == File)
3318 err = write_pattern_file((*cur_pat_h)->dirtypinerc
3319 ? &lvalue[lineno] : NULL, patline);
3320 else if(patline->type == Literal && (*cur_pat_h)->dirtypinerc)
3321 err = write_pattern_lit(&lvalue[lineno], patline);
3322 else if(patline->type == Inherit)
3323 err = write_pattern_inherit((*cur_pat_h)->dirtypinerc
3324 ? &lvalue[lineno] : NULL, patline);
3327 if((*cur_pat_h)->dirtypinerc){
3328 if(err)
3329 free_list_array(&lvalue);
3330 else{
3331 char ***alval;
3332 struct variable *var;
3334 if(rflags & ROLE_DO_ROLES)
3335 var = &ps_global->vars[V_PAT_ROLES];
3336 else if(rflags & ROLE_DO_OTHER)
3337 var = &ps_global->vars[V_PAT_OTHER];
3338 else if(rflags & ROLE_DO_FILTER)
3339 var = &ps_global->vars[V_PAT_FILTS];
3340 else if(rflags & ROLE_DO_SCORES)
3341 var = &ps_global->vars[V_PAT_SCORES];
3342 else if(rflags & ROLE_DO_INCOLS)
3343 var = &ps_global->vars[V_PAT_INCOLS];
3344 else if(rflags & ROLE_DO_SRCH)
3345 var = &ps_global->vars[V_PAT_SRCH];
3347 alval = (rflags & PAT_USE_CHANGED) ? &(var->changed_val.l)
3348 : ALVAL(var, (rflags & PAT_USE_MAIN) ? Main : Post);
3349 if(*alval)
3350 free_list_array(alval);
3352 if(rflags & PAT_USE_CHANGED) var->is_changed_val = 1;
3354 *alval = lvalue;
3356 if(!(rflags & PAT_USE_CHANGED))
3357 set_current_val(var, TRUE, TRUE);
3361 if(!err)
3362 (*cur_pat_h)->dirtypinerc = 0;
3364 return(err);
3369 * Write pattern lines into a file.
3371 * Args lvalue -- Pointer to char * to fill in variable value
3372 * patline --
3374 * Returns 0 -- all is ok, lvalue has been filled in, file has been written
3375 * else -- error, lvalue untouched, file not written
3378 write_pattern_file(char **lvalue, PAT_LINE_S *patline)
3380 char *p, *tfile;
3381 int fd = -1, err = 0;
3382 FILE *fp_new;
3383 PAT_S *pat;
3385 dprint((7, "write_pattern_file(%s)\n",
3386 (patline && patline->filepath) ? patline->filepath : "?"));
3388 if(lvalue){
3389 size_t l;
3391 l = strlen(patline->filename) + 5;
3392 p = (char *) fs_get((l+1) * sizeof(char));
3393 strncpy(p, "FILE:", l+1);
3394 p[l] = '\0';
3395 strncat(p, patline->filename, l+1-1-strlen(p));
3396 p[l] = '\0';
3397 *lvalue = p;
3400 if(patline->readonly || !patline->dirty) /* doesn't need writing */
3401 return(err);
3403 /* Get a tempfile to write the patterns into */
3404 if(((tfile = tempfile_in_same_dir(patline->filepath, ".pt", NULL)) == NULL)
3405 || ((fd = our_open(tfile, O_TRUNC|O_WRONLY|O_CREAT|O_BINARY, 0600)) < 0)
3406 || ((fp_new = fdopen(fd, "w")) == NULL)){
3407 q_status_message1(SM_ORDER | SM_DING, 3, 4,
3408 "Can't write in directory containing file \"%.200s\"",
3409 patline->filepath);
3410 if(tfile){
3411 our_unlink(tfile);
3412 fs_give((void **)&tfile);
3415 if(fd >= 0)
3416 close(fd);
3418 return(-1);
3421 dprint((9, "write_pattern_file: writing into %s\n",
3422 tfile ? tfile : "?"));
3424 if(fprintf(fp_new, "%s %s\n", PATTERN_MAGIC, PATTERN_FILE_VERS) == EOF)
3425 err--;
3427 for(pat = patline->first; !err && pat; pat = pat->next){
3428 if((p = data_for_patline(pat)) != NULL){
3429 if(fprintf(fp_new, "%s\n", p) == EOF)
3430 err--;
3432 fs_give((void **)&p);
3436 if(err || fclose(fp_new) == EOF){
3437 if(err)
3438 (void)fclose(fp_new);
3440 err--;
3441 q_status_message2(SM_ORDER | SM_DING, 3, 4,
3442 "I/O error: \"%.200s\": %.200s",
3443 tfile, error_description(errno));
3446 if(!err && rename_file(tfile, patline->filepath) < 0){
3447 err--;
3448 q_status_message3(SM_ORDER | SM_DING, 3, 4,
3449 _("Error renaming \"%s\" to \"%s\": %s"),
3450 tfile, patline->filepath, error_description(errno));
3451 dprint((2,
3452 "write_pattern_file: Error renaming (%s,%s): %s\n",
3453 tfile ? tfile : "?",
3454 (patline && patline->filepath) ? patline->filepath : "?",
3455 error_description(errno)));
3458 if(tfile){
3459 our_unlink(tfile);
3460 fs_give((void **)&tfile);
3463 if(!err)
3464 patline->dirty = 0;
3466 return(err);
3471 * Write literal pattern lines into lvalue (pinerc variable).
3473 * Args lvalue -- Pointer to char * to fill in variable value
3474 * patline --
3476 * Returns 0 -- all is ok, lvalue has been filled in, file has been written
3477 * else -- error, lvalue untouched, file not written
3480 write_pattern_lit(char **lvalue, PAT_LINE_S *patline)
3482 char *p = NULL;
3483 int err = 0;
3484 PAT_S *pat;
3486 pat = patline ? patline->first : NULL;
3488 if(pat && lvalue && (p = data_for_patline(pat)) != NULL){
3489 size_t l;
3491 l = strlen(p) + 4;
3492 *lvalue = (char *) fs_get((l+1) * sizeof(char));
3493 strncpy(*lvalue, "LIT:", l+1);
3494 (*lvalue)[l] = '\0';
3495 strncat(*lvalue, p, l+1-1-strlen(*lvalue));
3496 (*lvalue)[l] = '\0';
3498 else{
3499 q_status_message(SM_ORDER | SM_DING, 3, 4,
3500 _("Unknown error saving pattern variable"));
3501 err--;
3504 if(p)
3505 fs_give((void **)&p);
3507 return(err);
3512 write_pattern_inherit(char **lvalue, PAT_LINE_S *patline)
3514 int err = 0;
3516 if(patline && patline->type == Inherit && lvalue)
3517 *lvalue = cpystr(INHERIT);
3518 else
3519 err--;
3521 return(err);
3526 char *
3527 data_for_patline(PAT_S *pat)
3529 char *p = NULL, *q, *to_pat = NULL,
3530 *news_pat = NULL, *from_pat = NULL,
3531 *sender_pat = NULL, *cc_pat = NULL, *subj_pat = NULL,
3532 *arb_pat = NULL, *fldr_type_pat = NULL, *fldr_pat = NULL,
3533 *afrom_type_pat = NULL, *abooks_pat = NULL,
3534 *alltext_pat = NULL, *scorei_pat = NULL, *recip_pat = NULL,
3535 *keyword_pat = NULL, *charset_pat = NULL,
3536 *bodytext_pat = NULL, *age_pat = NULL, *sentdate = NULL,
3537 *size_pat = NULL,
3538 *category_cmd = NULL, *category_pat = NULL,
3539 *category_lim = NULL,
3540 *partic_pat = NULL, *stat_new_val = NULL,
3541 *stat_rec_val = NULL,
3542 *stat_imp_val = NULL, *stat_del_val = NULL,
3543 *stat_ans_val = NULL, *stat_8bit_val = NULL,
3544 *stat_bom_val = NULL, *stat_boy_val = NULL,
3545 *from_act = NULL, *replyto_act = NULL, *fcc_act = NULL,
3546 *sig_act = NULL, *nick = NULL, *templ_act = NULL,
3547 *litsig_act = NULL, *cstm_act = NULL, *smtp_act = NULL,
3548 *nntp_act = NULL, *comment = NULL,
3549 *repl_val = NULL, *forw_val = NULL, *comp_val = NULL,
3550 *incol_act = NULL, *inherit_nick = NULL,
3551 *score_act = NULL, *hdrtok_act = NULL,
3552 *sort_act = NULL, *iform_act = NULL, *start_act = NULL,
3553 *folder_act = NULL, *filt_ifnotdel = NULL,
3554 *filt_nokill = NULL, *filt_del_val = NULL,
3555 *filt_imp_val = NULL, *filt_ans_val = NULL,
3556 *filt_new_val = NULL, *filt_nonterm = NULL,
3557 *keyword_set = NULL, *keyword_clr = NULL;
3558 int to_not = 0, news_not = 0, from_not = 0,
3559 sender_not = 0, cc_not = 0, subj_not = 0,
3560 partic_not = 0, recip_not = 0, alltext_not, bodytext_not,
3561 keyword_not = 0, charset_not = 0;
3562 size_t l;
3563 ACTION_S *action = NULL;
3564 NAMEVAL_S *f;
3566 if(!pat)
3567 return(p);
3569 if((pat->patgrp && pat->patgrp->bogus)
3570 || (pat->action && pat->action->bogus)){
3571 if(pat->raw)
3572 p = cpystr(pat->raw);
3574 return(p);
3577 if(pat->patgrp){
3578 if(pat->patgrp->nick)
3579 if((nick = add_pat_escapes(pat->patgrp->nick)) && !*nick)
3580 fs_give((void **) &nick);
3582 if(pat->patgrp->comment)
3583 if((comment = add_pat_escapes(pat->patgrp->comment)) && !*comment)
3584 fs_give((void **) &comment);
3586 if(pat->patgrp->to){
3587 to_pat = pattern_to_config(pat->patgrp->to);
3588 to_not = pat->patgrp->to->not;
3591 if(pat->patgrp->from){
3592 from_pat = pattern_to_config(pat->patgrp->from);
3593 from_not = pat->patgrp->from->not;
3596 if(pat->patgrp->sender){
3597 sender_pat = pattern_to_config(pat->patgrp->sender);
3598 sender_not = pat->patgrp->sender->not;
3601 if(pat->patgrp->cc){
3602 cc_pat = pattern_to_config(pat->patgrp->cc);
3603 cc_not = pat->patgrp->cc->not;
3606 if(pat->patgrp->recip){
3607 recip_pat = pattern_to_config(pat->patgrp->recip);
3608 recip_not = pat->patgrp->recip->not;
3611 if(pat->patgrp->partic){
3612 partic_pat = pattern_to_config(pat->patgrp->partic);
3613 partic_not = pat->patgrp->partic->not;
3616 if(pat->patgrp->news){
3617 news_pat = pattern_to_config(pat->patgrp->news);
3618 news_not = pat->patgrp->news->not;
3621 if(pat->patgrp->subj){
3622 subj_pat = pattern_to_config(pat->patgrp->subj);
3623 subj_not = pat->patgrp->subj->not;
3626 if(pat->patgrp->alltext){
3627 alltext_pat = pattern_to_config(pat->patgrp->alltext);
3628 alltext_not = pat->patgrp->alltext->not;
3631 if(pat->patgrp->bodytext){
3632 bodytext_pat = pattern_to_config(pat->patgrp->bodytext);
3633 bodytext_not = pat->patgrp->bodytext->not;
3636 if(pat->patgrp->keyword){
3637 keyword_pat = pattern_to_config(pat->patgrp->keyword);
3638 keyword_not = pat->patgrp->keyword->not;
3641 if(pat->patgrp->charsets){
3642 charset_pat = pattern_to_config(pat->patgrp->charsets);
3643 charset_not = pat->patgrp->charsets->not;
3646 if(pat->patgrp->arbhdr){
3647 ARBHDR_S *a;
3648 char *p1 = NULL, *p2 = NULL, *p3 = NULL, *p4 = NULL;
3649 int len = 0;
3651 /* This is brute force dumb, but who cares? */
3652 for(a = pat->patgrp->arbhdr; a; a = a->next){
3653 if(a->field && a->field[0]){
3654 p1 = pattern_to_string(a->p);
3655 p1 = p1 ? p1 : cpystr("");
3656 l = strlen(a->field)+strlen(p1)+1;
3657 p2 = (char *) fs_get((l+1) * sizeof(char));
3658 snprintf(p2, l+1, "%s=%s", a->field, p1);
3659 p3 = add_pat_escapes(p2);
3660 l = strlen(p3)+6;
3661 p4 = (char *) fs_get((l+1) * sizeof(char));
3662 snprintf(p4, l+1, "/%s%sARB%s",
3663 (a->p && a->p->not) ? "!" : "",
3664 a->isemptyval ? "E" : "", p3);
3665 len += strlen(p4);
3667 if(p1)
3668 fs_give((void **)&p1);
3669 if(p2)
3670 fs_give((void **)&p2);
3671 if(p3)
3672 fs_give((void **)&p3);
3673 if(p4)
3674 fs_give((void **)&p4);
3678 p = arb_pat = (char *)fs_get((len + 1) * sizeof(char));
3680 for(a = pat->patgrp->arbhdr; a; a = a->next){
3681 if(a->field && a->field[0]){
3682 p1 = pattern_to_string(a->p);
3683 p1 = p1 ? p1 : cpystr("");
3684 l = strlen(a->field)+strlen(p1)+1;
3685 p2 = (char *) fs_get((l+1) * sizeof(char));
3686 snprintf(p2, l+1, "%s=%s", a->field, p1);
3687 p3 = add_pat_escapes(p2);
3688 l = strlen(p3)+6;
3689 p4 = (char *) fs_get((l+1) * sizeof(char));
3690 snprintf(p4, l+1, "/%s%sARB%s",
3691 (a->p && a->p->not) ? "!" : "",
3692 a->isemptyval ? "E" : "", p3);
3693 sstrncpy(&p, p4, len+1-(p-arb_pat));
3695 if(p1)
3696 fs_give((void **)&p1);
3697 if(p2)
3698 fs_give((void **)&p2);
3699 if(p3)
3700 fs_give((void **)&p3);
3701 if(p4)
3702 fs_give((void **)&p4);
3706 arb_pat[len] = '\0';
3709 if(pat->patgrp->age_uses_sentdate)
3710 sentdate = cpystr("/SENTDATE=1");
3712 if(pat->patgrp->do_score){
3713 p = stringform_of_intvl(pat->patgrp->score);
3714 if(p){
3715 scorei_pat = add_pat_escapes(p);
3716 fs_give((void **)&p);
3720 if(pat->patgrp->do_age){
3721 p = stringform_of_intvl(pat->patgrp->age);
3722 if(p){
3723 age_pat = add_pat_escapes(p);
3724 fs_give((void **)&p);
3728 if(pat->patgrp->do_size){
3729 p = stringform_of_intvl(pat->patgrp->size);
3730 if(p){
3731 size_pat = add_pat_escapes(p);
3732 fs_give((void **)&p);
3736 if(pat->patgrp->category_cmd && pat->patgrp->category_cmd[0]){
3737 size_t sz;
3738 char **l, *q;
3740 /* concatenate into string with commas first */
3741 sz = 0;
3742 for(l = pat->patgrp->category_cmd; l[0] && l[0][0]; l++)
3743 sz += strlen(l[0]) + 1;
3745 if(sz){
3746 char *p;
3747 int first_one = 1;
3749 q = (char *)fs_get(sz);
3750 memset(q, 0, sz);
3751 p = q;
3752 for(l = pat->patgrp->category_cmd; l[0] && l[0][0]; l++){
3753 if(!first_one)
3754 sstrncpy(&p, ",", sz-(p-q));
3756 first_one = 0;
3757 sstrncpy(&p, l[0], sz-(p-q));
3760 q[sz-1] = '\0';
3762 category_cmd = add_pat_escapes(q);
3763 fs_give((void **)&q);
3767 if(pat->patgrp->do_cat){
3768 p = stringform_of_intvl(pat->patgrp->cat);
3769 if(p){
3770 category_pat = add_pat_escapes(p);
3771 fs_give((void **)&p);
3775 if(pat->patgrp->cat_lim != -1L){
3776 category_lim = (char *) fs_get(20 * sizeof(char));
3777 snprintf(category_lim, 20, "%ld", pat->patgrp->cat_lim);
3780 if((f = pat_fldr_types(pat->patgrp->fldr_type)) != NULL)
3781 fldr_type_pat = f->shortname;
3783 if(pat->patgrp->folder)
3784 fldr_pat = pattern_to_config(pat->patgrp->folder);
3786 if((f = inabook_fldr_types(pat->patgrp->inabook)) != NULL
3787 && f->value != IAB_DEFL)
3788 afrom_type_pat = f->shortname;
3790 if(pat->patgrp->abooks)
3791 abooks_pat = pattern_to_config(pat->patgrp->abooks);
3793 if(pat->patgrp->stat_new != PAT_STAT_EITHER &&
3794 (f = role_status_types(pat->patgrp->stat_new)) != NULL)
3795 stat_new_val = f->shortname;
3797 if(pat->patgrp->stat_rec != PAT_STAT_EITHER &&
3798 (f = role_status_types(pat->patgrp->stat_rec)) != NULL)
3799 stat_rec_val = f->shortname;
3801 if(pat->patgrp->stat_del != PAT_STAT_EITHER &&
3802 (f = role_status_types(pat->patgrp->stat_del)) != NULL)
3803 stat_del_val = f->shortname;
3805 if(pat->patgrp->stat_ans != PAT_STAT_EITHER &&
3806 (f = role_status_types(pat->patgrp->stat_ans)) != NULL)
3807 stat_ans_val = f->shortname;
3809 if(pat->patgrp->stat_imp != PAT_STAT_EITHER &&
3810 (f = role_status_types(pat->patgrp->stat_imp)) != NULL)
3811 stat_imp_val = f->shortname;
3813 if(pat->patgrp->stat_8bitsubj != PAT_STAT_EITHER &&
3814 (f = role_status_types(pat->patgrp->stat_8bitsubj)) != NULL)
3815 stat_8bit_val = f->shortname;
3817 if(pat->patgrp->stat_bom != PAT_STAT_EITHER &&
3818 (f = role_status_types(pat->patgrp->stat_bom)) != NULL)
3819 stat_bom_val = f->shortname;
3821 if(pat->patgrp->stat_boy != PAT_STAT_EITHER &&
3822 (f = role_status_types(pat->patgrp->stat_boy)) != NULL)
3823 stat_boy_val = f->shortname;
3826 if(pat->action){
3827 action = pat->action;
3829 if(action->is_a_score){
3830 if(action->scoreval != 0L &&
3831 action->scoreval >= SCORE_MIN && action->scoreval <= SCORE_MAX){
3832 score_act = (char *) fs_get(5 * sizeof(char));
3833 snprintf(score_act, 5, "%ld", pat->action->scoreval);
3836 if(action->scorevalhdrtok)
3837 hdrtok_act = hdrtok_to_config(action->scorevalhdrtok);
3840 if(action->is_a_role){
3841 if(action->inherit_nick)
3842 inherit_nick = add_pat_escapes(action->inherit_nick);
3843 if(action->fcc)
3844 fcc_act = add_pat_escapes(action->fcc);
3845 if(action->litsig)
3846 litsig_act = add_pat_escapes(action->litsig);
3847 if(action->sig)
3848 sig_act = add_pat_escapes(action->sig);
3849 if(action->template)
3850 templ_act = add_pat_escapes(action->template);
3852 if(action->cstm){
3853 size_t sz;
3854 char **l, *q;
3856 /* concatenate into string with commas first */
3857 sz = 0;
3858 for(l = action->cstm; l[0] && l[0][0]; l++)
3859 sz += strlen(l[0]) + 1;
3861 if(sz){
3862 char *p;
3863 int first_one = 1;
3865 q = (char *)fs_get(sz);
3866 memset(q, 0, sz);
3867 p = q;
3868 for(l = action->cstm; l[0] && l[0][0]; l++){
3869 if((!struncmp(l[0], "from", 4) &&
3870 (l[0][4] == ':' || l[0][4] == '\0')) ||
3871 (!struncmp(l[0], "reply-to", 8) &&
3872 (l[0][8] == ':' || l[0][8] == '\0')))
3873 continue;
3875 if(!first_one)
3876 sstrncpy(&p, ",", sz-(p-q));
3878 first_one = 0;
3879 sstrncpy(&p, l[0], sz-(p-q));
3882 q[sz-1] = '\0';
3884 cstm_act = add_pat_escapes(q);
3885 fs_give((void **)&q);
3889 if(action->smtp){
3890 size_t sz;
3891 char **l, *q;
3893 /* concatenate into string with commas first */
3894 sz = 0;
3895 for(l = action->smtp; l[0] && l[0][0]; l++)
3896 sz += strlen(l[0]) + 1;
3898 if(sz){
3899 char *p;
3900 int first_one = 1;
3902 q = (char *)fs_get(sz);
3903 memset(q, 0, sz);
3904 p = q;
3905 for(l = action->smtp; l[0] && l[0][0]; l++){
3906 if(!first_one)
3907 sstrncpy(&p, ",", sz-(p-q));
3909 first_one = 0;
3910 sstrncpy(&p, l[0], sz-(p-q));
3913 q[sz-1] = '\0';
3915 smtp_act = add_pat_escapes(q);
3916 fs_give((void **)&q);
3920 if(action->nntp){
3921 size_t sz;
3922 char **l, *q;
3924 /* concatenate into string with commas first */
3925 sz = 0;
3926 for(l = action->nntp; l[0] && l[0][0]; l++)
3927 sz += strlen(l[0]) + 1;
3929 if(sz){
3930 char *p;
3931 int first_one = 1;
3933 q = (char *)fs_get(sz);
3934 memset(q, 0, sz);
3935 p = q;
3936 for(l = action->nntp; l[0] && l[0][0]; l++){
3937 if(!first_one)
3938 sstrncpy(&p, ",", sz-(p-q));
3940 first_one = 0;
3941 sstrncpy(&p, l[0], sz-(p-q));
3944 q[sz-1] = '\0';
3946 nntp_act = add_pat_escapes(q);
3947 fs_give((void **)&q);
3951 if((f = role_repl_types(action->repl_type)) != NULL)
3952 repl_val = f->shortname;
3954 if((f = role_forw_types(action->forw_type)) != NULL)
3955 forw_val = f->shortname;
3957 if((f = role_comp_types(action->comp_type)) != NULL)
3958 comp_val = f->shortname;
3961 if(action->is_a_incol && action->incol){
3962 char *ptr, buf[256], *p1, *p2;
3964 ptr = buf;
3965 memset(buf, 0, sizeof(buf));
3966 sstrncpy(&ptr, "/FG=", sizeof(buf)-(ptr-buf));
3967 sstrncpy(&ptr, (p1=add_pat_escapes(action->incol->fg)), sizeof(buf)-(ptr-buf));
3968 sstrncpy(&ptr, "/BG=", sizeof(buf)-(ptr-buf));
3969 sstrncpy(&ptr, (p2=add_pat_escapes(action->incol->bg)), sizeof(buf)-(ptr-buf));
3970 buf[sizeof(buf)-1] = '\0';
3971 /* the colors will be doubly escaped */
3972 incol_act = add_pat_escapes(buf);
3973 if(p1)
3974 fs_give((void **)&p1);
3976 if(p2)
3977 fs_give((void **)&p2);
3980 if(action->is_a_other){
3981 char buf[256];
3983 if(action->sort_is_set){
3984 snprintf(buf, sizeof(buf), "%.50s%.50s",
3985 sort_name(action->sortorder),
3986 action->revsort ? "/Reverse" : "");
3987 sort_act = add_pat_escapes(buf);
3990 if(action->index_format)
3991 iform_act = add_pat_escapes(action->index_format);
3993 if(action->startup_rule != IS_NOTSET &&
3994 (f = startup_rules(action->startup_rule)) != NULL)
3995 start_act = S_OR_L(f);
3998 if(action->is_a_role && action->from){
3999 char *bufp;
4000 size_t len;
4002 len = est_size(action->from);
4003 bufp = (char *) fs_get(len * sizeof(char));
4004 p = addr_string_mult(action->from, bufp, len);
4005 if(p){
4006 from_act = add_pat_escapes(p);
4007 fs_give((void **)&p);
4011 if(action->is_a_role && action->replyto){
4012 char *bufp;
4013 size_t len;
4015 len = est_size(action->replyto);
4016 bufp = (char *) fs_get(len * sizeof(char));
4017 p = addr_string_mult(action->replyto, bufp, len);
4018 if(p){
4019 replyto_act = add_pat_escapes(p);
4020 fs_give((void **)&p);
4024 if(action->is_a_filter){
4025 if(action->folder){
4026 if((folder_act = pattern_to_config(action->folder)) != NULL){
4027 if(action->move_only_if_not_deleted)
4028 filt_ifnotdel = cpystr("/NOTDEL=1");
4032 if(action->keyword_set)
4033 keyword_set = pattern_to_config(action->keyword_set);
4035 if(action->keyword_clr)
4036 keyword_clr = pattern_to_config(action->keyword_clr);
4038 if(!action->kill)
4039 filt_nokill = cpystr("/NOKILL=1");
4041 if(action->non_terminating)
4042 filt_nonterm = cpystr("/NONTERM=1");
4044 if(action->state_setting_bits){
4045 char buf[256];
4046 int dval, nval, ival, aval;
4048 buf[0] = '\0';
4049 p = buf;
4051 convert_statebits_to_vals(action->state_setting_bits,
4052 &dval, &aval, &ival, &nval);
4053 if(dval != ACT_STAT_LEAVE &&
4054 (f = msg_state_types(dval)) != NULL)
4055 filt_del_val = f->shortname;
4057 if(aval != ACT_STAT_LEAVE &&
4058 (f = msg_state_types(aval)) != NULL)
4059 filt_ans_val = f->shortname;
4061 if(ival != ACT_STAT_LEAVE &&
4062 (f = msg_state_types(ival)) != NULL)
4063 filt_imp_val = f->shortname;
4065 if(nval != ACT_STAT_LEAVE &&
4066 (f = msg_state_types(nval)) != NULL)
4067 filt_new_val = f->shortname;
4072 l = strlen(nick ? nick : "Alternate Role") +
4073 strlen(comment ? comment : "") +
4074 strlen(to_pat ? to_pat : "") +
4075 strlen(from_pat ? from_pat : "") +
4076 strlen(sender_pat ? sender_pat : "") +
4077 strlen(cc_pat ? cc_pat : "") +
4078 strlen(recip_pat ? recip_pat : "") +
4079 strlen(partic_pat ? partic_pat : "") +
4080 strlen(news_pat ? news_pat : "") +
4081 strlen(subj_pat ? subj_pat : "") +
4082 strlen(alltext_pat ? alltext_pat : "") +
4083 strlen(bodytext_pat ? bodytext_pat : "") +
4084 strlen(arb_pat ? arb_pat : "") +
4085 strlen(scorei_pat ? scorei_pat : "") +
4086 strlen(keyword_pat ? keyword_pat : "") +
4087 strlen(charset_pat ? charset_pat : "") +
4088 strlen(age_pat ? age_pat : "") +
4089 strlen(size_pat ? size_pat : "") +
4090 strlen(category_cmd ? category_cmd : "") +
4091 strlen(category_pat ? category_pat : "") +
4092 strlen(category_lim ? category_lim : "") +
4093 strlen(fldr_pat ? fldr_pat : "") +
4094 strlen(abooks_pat ? abooks_pat : "") +
4095 strlen(sentdate ? sentdate : "") +
4096 strlen(inherit_nick ? inherit_nick : "") +
4097 strlen(score_act ? score_act : "") +
4098 strlen(hdrtok_act ? hdrtok_act : "") +
4099 strlen(from_act ? from_act : "") +
4100 strlen(replyto_act ? replyto_act : "") +
4101 strlen(fcc_act ? fcc_act : "") +
4102 strlen(litsig_act ? litsig_act : "") +
4103 strlen(cstm_act ? cstm_act : "") +
4104 strlen(smtp_act ? smtp_act : "") +
4105 strlen(nntp_act ? nntp_act : "") +
4106 strlen(sig_act ? sig_act : "") +
4107 strlen(incol_act ? incol_act : "") +
4108 strlen(sort_act ? sort_act : "") +
4109 strlen(iform_act ? iform_act : "") +
4110 strlen(start_act ? start_act : "") +
4111 strlen(filt_ifnotdel ? filt_ifnotdel : "") +
4112 strlen(filt_nokill ? filt_nokill : "") +
4113 strlen(filt_nonterm ? filt_nonterm : "") +
4114 (folder_act ? (strlen(folder_act) + 8) : 0) +
4115 strlen(keyword_set ? keyword_set : "") +
4116 strlen(keyword_clr ? keyword_clr : "") +
4117 strlen(templ_act ? templ_act : "") + 540;
4119 * The +540 above is larger than needed but not everything is accounted
4120 * for with the strlens.
4122 p = (char *) fs_get(l * sizeof(char));
4124 q = p;
4125 sstrncpy(&q, "pattern=\"/NICK=", l-(q-p));
4127 if(nick){
4128 sstrncpy(&q, nick, l-(q-p));
4129 fs_give((void **) &nick);
4131 else
4132 sstrncpy(&q, "Alternate Role", l-(q-p));
4134 if(comment){
4135 sstrncpy(&q, "/", l-(q-p));
4136 sstrncpy(&q, "COMM=", l-(q-p));
4137 sstrncpy(&q, comment, l-(q-p));
4138 fs_give((void **) &comment);
4141 if(to_pat){
4142 sstrncpy(&q, "/", l-(q-p));
4143 if(to_not)
4144 sstrncpy(&q, "!", l-(q-p));
4146 sstrncpy(&q, "TO=", l-(q-p));
4147 sstrncpy(&q, to_pat, l-(q-p));
4148 fs_give((void **) &to_pat);
4151 if(from_pat){
4152 sstrncpy(&q, "/", l-(q-p));
4153 if(from_not)
4154 sstrncpy(&q, "!", l-(q-p));
4156 sstrncpy(&q, "FROM=", l-(q-p));
4157 sstrncpy(&q, from_pat, l-(q-p));
4158 fs_give((void **) &from_pat);
4161 if(sender_pat){
4162 sstrncpy(&q, "/", l-(q-p));
4163 if(sender_not)
4164 sstrncpy(&q, "!", l-(q-p));
4166 sstrncpy(&q, "SENDER=", l-(q-p));
4167 sstrncpy(&q, sender_pat, l-(q-p));
4168 fs_give((void **) &sender_pat);
4171 if(cc_pat){
4172 sstrncpy(&q,"/", l-(q-p));
4173 if(cc_not)
4174 sstrncpy(&q, "!", l-(q-p));
4176 sstrncpy(&q,"CC=", l-(q-p));
4177 sstrncpy(&q, cc_pat, l-(q-p));
4178 fs_give((void **) &cc_pat);
4181 if(recip_pat){
4182 sstrncpy(&q, "/", l-(q-p));
4183 if(recip_not)
4184 sstrncpy(&q, "!", l-(q-p));
4186 sstrncpy(&q, "RECIP=", l-(q-p));
4187 sstrncpy(&q, recip_pat, l-(q-p));
4188 fs_give((void **) &recip_pat);
4191 if(partic_pat){
4192 sstrncpy(&q, "/", l-(q-p));
4193 if(partic_not)
4194 sstrncpy(&q, "!", l-(q-p));
4196 sstrncpy(&q, "PARTIC=", l-(q-p));
4197 sstrncpy(&q, partic_pat, l-(q-p));
4198 fs_give((void **) &partic_pat);
4201 if(news_pat){
4202 sstrncpy(&q, "/", l-(q-p));
4203 if(news_not)
4204 sstrncpy(&q, "!", l-(q-p));
4206 sstrncpy(&q, "NEWS=", l-(q-p));
4207 sstrncpy(&q, news_pat, l-(q-p));
4208 fs_give((void **) &news_pat);
4211 if(subj_pat){
4212 sstrncpy(&q, "/", l-(q-p));
4213 if(subj_not)
4214 sstrncpy(&q, "!", l-(q-p));
4216 sstrncpy(&q, "SUBJ=", l-(q-p));
4217 sstrncpy(&q, subj_pat, l-(q-p));
4218 fs_give((void **)&subj_pat);
4221 if(alltext_pat){
4222 sstrncpy(&q, "/", l-(q-p));
4223 if(alltext_not)
4224 sstrncpy(&q, "!", l-(q-p));
4226 sstrncpy(&q, "ALL=", l-(q-p));
4227 sstrncpy(&q, alltext_pat, l-(q-p));
4228 fs_give((void **) &alltext_pat);
4231 if(bodytext_pat){
4232 sstrncpy(&q, "/", l-(q-p));
4233 if(bodytext_not)
4234 sstrncpy(&q, "!", l-(q-p));
4236 sstrncpy(&q, "BODY=", l-(q-p));
4237 sstrncpy(&q, bodytext_pat, l-(q-p));
4238 fs_give((void **) &bodytext_pat);
4241 if(keyword_pat){
4242 sstrncpy(&q, "/", l-(q-p));
4243 if(keyword_not)
4244 sstrncpy(&q, "!", l-(q-p));
4246 sstrncpy(&q, "KEY=", l-(q-p));
4247 sstrncpy(&q, keyword_pat, l-(q-p));
4248 fs_give((void **) &keyword_pat);
4251 if(charset_pat){
4252 sstrncpy(&q, "/", l-(q-p));
4253 if(charset_not)
4254 sstrncpy(&q, "!", l-(q-p));
4256 sstrncpy(&q, "CHAR=", l-(q-p));
4257 sstrncpy(&q, charset_pat, l-(q-p));
4258 fs_give((void **) &charset_pat);
4261 if(arb_pat){
4262 sstrncpy(&q, arb_pat, l-(q-p));
4263 fs_give((void **)&arb_pat);
4266 if(scorei_pat){
4267 sstrncpy(&q, "/SCOREI=", l-(q-p));
4268 sstrncpy(&q, scorei_pat, l-(q-p));
4269 fs_give((void **) &scorei_pat);
4272 if(age_pat){
4273 sstrncpy(&q, "/AGE=", l-(q-p));
4274 sstrncpy(&q, age_pat, l-(q-p));
4275 fs_give((void **) &age_pat);
4278 if(size_pat){
4279 sstrncpy(&q, "/SIZE=", l-(q-p));
4280 sstrncpy(&q, size_pat, l-(q-p));
4281 fs_give((void **) &size_pat);
4284 if(category_cmd){
4285 sstrncpy(&q, "/CATCMD=", l-(q-p));
4286 sstrncpy(&q, category_cmd, l-(q-p));
4287 fs_give((void **) &category_cmd);
4290 if(category_pat){
4291 sstrncpy(&q, "/CATVAL=", l-(q-p));
4292 sstrncpy(&q, category_pat, l-(q-p));
4293 fs_give((void **) &category_pat);
4296 if(category_lim){
4297 sstrncpy(&q, "/CATLIM=", l-(q-p));
4298 sstrncpy(&q, category_lim, l-(q-p));
4299 fs_give((void **) &category_lim);
4302 if(sentdate){
4303 sstrncpy(&q, sentdate, l-(q-p));
4304 fs_give((void **) &sentdate);
4307 if(fldr_type_pat){
4308 sstrncpy(&q, "/FLDTYPE=", l-(q-p));
4309 sstrncpy(&q, fldr_type_pat, l-(q-p));
4312 if(fldr_pat){
4313 sstrncpy(&q, "/FOLDER=", l-(q-p));
4314 sstrncpy(&q, fldr_pat, l-(q-p));
4315 fs_give((void **) &fldr_pat);
4318 if(afrom_type_pat){
4319 sstrncpy(&q, "/AFROM=", l-(q-p));
4320 sstrncpy(&q, afrom_type_pat, l-(q-p));
4323 * Add address types. If it is From or Reply-to
4324 * leave this out so it will still work with pine.
4326 if((pat->patgrp->inabook & IAB_FROM
4327 && pat->patgrp->inabook & IAB_REPLYTO
4328 && !(pat->patgrp->inabook & IAB_SENDER)
4329 && !(pat->patgrp->inabook & IAB_TO)
4330 && !(pat->patgrp->inabook & IAB_CC))
4332 (!(pat->patgrp->inabook & IAB_FROM)
4333 && !(pat->patgrp->inabook & IAB_REPLYTO)
4334 && !(pat->patgrp->inabook & IAB_SENDER)
4335 && !(pat->patgrp->inabook & IAB_TO)
4336 && !(pat->patgrp->inabook & IAB_CC))){
4337 ; /* leave it out */
4339 else{
4340 sstrncpy(&q, "/AFROMA=", l-(q-p));
4341 if(pat->patgrp->inabook & IAB_FROM)
4342 sstrncpy(&q, "F", l-(q-p));
4344 if(pat->patgrp->inabook & IAB_REPLYTO)
4345 sstrncpy(&q, "R", l-(q-p));
4347 if(pat->patgrp->inabook & IAB_SENDER)
4348 sstrncpy(&q, "S", l-(q-p));
4350 if(pat->patgrp->inabook & IAB_TO)
4351 sstrncpy(&q, "T", l-(q-p));
4353 if(pat->patgrp->inabook & IAB_CC)
4354 sstrncpy(&q, "C", l-(q-p));
4358 if(abooks_pat){
4359 sstrncpy(&q, "/ABOOKS=", l-(q-p));
4360 sstrncpy(&q, abooks_pat, l-(q-p));
4361 fs_give((void **) &abooks_pat);
4364 if(stat_new_val){
4365 sstrncpy(&q, "/STATN=", l-(q-p));
4366 sstrncpy(&q, stat_new_val, l-(q-p));
4369 if(stat_rec_val){
4370 sstrncpy(&q, "/STATR=", l-(q-p));
4371 sstrncpy(&q, stat_rec_val, l-(q-p));
4374 if(stat_del_val){
4375 sstrncpy(&q, "/STATD=", l-(q-p));
4376 sstrncpy(&q, stat_del_val, l-(q-p));
4379 if(stat_imp_val){
4380 sstrncpy(&q, "/STATI=", l-(q-p));
4381 sstrncpy(&q, stat_imp_val, l-(q-p));
4384 if(stat_ans_val){
4385 sstrncpy(&q, "/STATA=", l-(q-p));
4386 sstrncpy(&q, stat_ans_val, l-(q-p));
4389 if(stat_8bit_val){
4390 sstrncpy(&q, "/8BITS=", l-(q-p));
4391 sstrncpy(&q, stat_8bit_val, l-(q-p));
4394 if(stat_bom_val){
4395 sstrncpy(&q, "/BOM=", l-(q-p));
4396 sstrncpy(&q, stat_bom_val, l-(q-p));
4399 if(stat_boy_val){
4400 sstrncpy(&q, "/BOY=", l-(q-p));
4401 sstrncpy(&q, stat_boy_val, l-(q-p));
4404 sstrncpy(&q, "\" action=\"", l-(q-p));
4406 if(inherit_nick && *inherit_nick){
4407 sstrncpy(&q, "/INICK=", l-(q-p));
4408 sstrncpy(&q, inherit_nick, l-(q-p));
4409 fs_give((void **)&inherit_nick);
4412 if(action){
4413 if(action->is_a_role)
4414 sstrncpy(&q, "/ROLE=1", l-(q-p));
4416 if(action->is_a_incol)
4417 sstrncpy(&q, "/ISINCOL=1", l-(q-p));
4419 if(action->is_a_srch)
4420 sstrncpy(&q, "/ISSRCH=1", l-(q-p));
4422 if(action->is_a_score)
4423 sstrncpy(&q, "/ISSCORE=1", l-(q-p));
4425 if(action->is_a_filter){
4427 * Older pine will interpret a filter that has no folder
4428 * as a Delete, even if we set it up here to be a Just Set
4429 * State filter. Disable the filter for older versions in that
4430 * case. If kill is set then Delete is what is supposed to
4431 * happen, so that's ok. If folder is set then Move is what is
4432 * supposed to happen, so ok.
4434 if(!action->kill && !action->folder)
4435 sstrncpy(&q, "/FILTER=2", l-(q-p));
4436 else
4437 sstrncpy(&q, "/FILTER=1", l-(q-p));
4440 if(action->is_a_other)
4441 sstrncpy(&q, "/OTHER=1", l-(q-p));
4444 if(score_act){
4445 sstrncpy(&q, "/SCORE=", l-(q-p));
4446 sstrncpy(&q, score_act, l-(q-p));
4447 fs_give((void **)&score_act);
4450 if(hdrtok_act){
4451 sstrncpy(&q, "/SCOREHDRTOK=", l-(q-p));
4452 sstrncpy(&q, hdrtok_act, l-(q-p));
4453 fs_give((void **)&hdrtok_act);
4456 if(from_act){
4457 sstrncpy(&q, "/FROM=", l-(q-p));
4458 sstrncpy(&q, from_act, l-(q-p));
4459 fs_give((void **) &from_act);
4462 if(replyto_act){
4463 sstrncpy(&q, "/REPL=", l-(q-p));
4464 sstrncpy(&q, replyto_act, l-(q-p));
4465 fs_give((void **)&replyto_act);
4468 if(fcc_act){
4469 sstrncpy(&q, "/FCC=", l-(q-p));
4470 sstrncpy(&q, fcc_act, l-(q-p));
4471 fs_give((void **)&fcc_act);
4474 if(litsig_act){
4475 sstrncpy(&q, "/LSIG=", l-(q-p));
4476 sstrncpy(&q, litsig_act, l-(q-p));
4477 fs_give((void **)&litsig_act);
4480 if(sig_act){
4481 sstrncpy(&q, "/SIG=", l-(q-p));
4482 sstrncpy(&q, sig_act, l-(q-p));
4483 fs_give((void **)&sig_act);
4486 if(templ_act){
4487 sstrncpy(&q, "/TEMPLATE=", l-(q-p));
4488 sstrncpy(&q, templ_act, l-(q-p));
4489 fs_give((void **)&templ_act);
4492 if(cstm_act){
4493 sstrncpy(&q, "/CSTM=", l-(q-p));
4494 sstrncpy(&q, cstm_act, l-(q-p));
4495 fs_give((void **)&cstm_act);
4498 if(smtp_act){
4499 sstrncpy(&q, "/SMTP=", l-(q-p));
4500 sstrncpy(&q, smtp_act, l-(q-p));
4501 fs_give((void **)&smtp_act);
4504 if(nntp_act){
4505 sstrncpy(&q, "/NNTP=", l-(q-p));
4506 sstrncpy(&q, nntp_act, l-(q-p));
4507 fs_give((void **)&nntp_act);
4510 if(repl_val){
4511 sstrncpy(&q, "/RTYPE=", l-(q-p));
4512 sstrncpy(&q, repl_val, l-(q-p));
4515 if(forw_val){
4516 sstrncpy(&q, "/FTYPE=", l-(q-p));
4517 sstrncpy(&q, forw_val, l-(q-p));
4520 if(comp_val){
4521 sstrncpy(&q, "/CTYPE=", l-(q-p));
4522 sstrncpy(&q, comp_val, l-(q-p));
4525 if(incol_act){
4526 sstrncpy(&q, "/INCOL=", l-(q-p));
4527 sstrncpy(&q, incol_act, l-(q-p));
4528 fs_give((void **)&incol_act);
4531 if(sort_act){
4532 sstrncpy(&q, "/SORT=", l-(q-p));
4533 sstrncpy(&q, sort_act, l-(q-p));
4534 fs_give((void **)&sort_act);
4537 if(iform_act){
4538 sstrncpy(&q, "/IFORM=", l-(q-p));
4539 sstrncpy(&q, iform_act, l-(q-p));
4540 fs_give((void **)&iform_act);
4543 if(start_act){
4544 sstrncpy(&q, "/START=", l-(q-p));
4545 sstrncpy(&q, start_act, l-(q-p));
4548 if(folder_act){
4549 sstrncpy(&q, "/FOLDER=", l-(q-p));
4550 sstrncpy(&q, folder_act, l-(q-p));
4551 fs_give((void **) &folder_act);
4554 if(filt_ifnotdel){
4555 sstrncpy(&q, filt_ifnotdel, l-(q-p));
4556 fs_give((void **) &filt_ifnotdel);
4559 if(filt_nonterm){
4560 sstrncpy(&q, filt_nonterm, l-(q-p));
4561 fs_give((void **) &filt_nonterm);
4564 if(filt_nokill){
4565 sstrncpy(&q, filt_nokill, l-(q-p));
4566 fs_give((void **) &filt_nokill);
4569 if(filt_new_val){
4570 sstrncpy(&q, "/STATN=", l-(q-p));
4571 sstrncpy(&q, filt_new_val, l-(q-p));
4574 if(filt_del_val){
4575 sstrncpy(&q, "/STATD=", l-(q-p));
4576 sstrncpy(&q, filt_del_val, l-(q-p));
4579 if(filt_imp_val){
4580 sstrncpy(&q, "/STATI=", l-(q-p));
4581 sstrncpy(&q, filt_imp_val, l-(q-p));
4584 if(filt_ans_val){
4585 sstrncpy(&q, "/STATA=", l-(q-p));
4586 sstrncpy(&q, filt_ans_val, l-(q-p));
4589 if(keyword_set){
4590 sstrncpy(&q, "/KEYSET=", l-(q-p));
4591 sstrncpy(&q, keyword_set, l-(q-p));
4592 fs_give((void **) &keyword_set);
4595 if(keyword_clr){
4596 sstrncpy(&q, "/KEYCLR=", l-(q-p));
4597 sstrncpy(&q, keyword_clr, l-(q-p));
4598 fs_give((void **) &keyword_clr);
4601 if(q-p < l)
4602 *q++ = '\"';
4604 if(q-p < l)
4605 *q = '\0';
4607 p[l-1] = '\0';
4609 return(p);
4613 void
4614 convert_statebits_to_vals(long int bits, int *dval, int *aval, int *ival, int *nval)
4616 if(dval)
4617 *dval = ACT_STAT_LEAVE;
4618 if(aval)
4619 *aval = ACT_STAT_LEAVE;
4620 if(ival)
4621 *ival = ACT_STAT_LEAVE;
4622 if(nval)
4623 *nval = ACT_STAT_LEAVE;
4625 if(ival){
4626 if(bits & F_FLAG)
4627 *ival = ACT_STAT_SET;
4628 else if(bits & F_UNFLAG)
4629 *ival = ACT_STAT_CLEAR;
4632 if(aval){
4633 if(bits & F_ANS)
4634 *aval = ACT_STAT_SET;
4635 else if(bits & F_UNANS)
4636 *aval = ACT_STAT_CLEAR;
4639 if(dval){
4640 if(bits & F_DEL)
4641 *dval = ACT_STAT_SET;
4642 else if(bits & F_UNDEL)
4643 *dval = ACT_STAT_CLEAR;
4646 if(nval){
4647 if(bits & F_UNSEEN)
4648 *nval = ACT_STAT_SET;
4649 else if(bits & F_SEEN)
4650 *nval = ACT_STAT_CLEAR;
4656 * The "searched" bit will be set for each message which matches.
4658 * Args: patgrp -- Pattern to search with
4659 * stream --
4660 * searchset -- Restrict search to this set
4661 * section -- Searching a section of the message, not the whole thing
4662 * get_score -- Function to return the score for a message
4663 * flags -- Most of these are flags to mail_search_full. However, we
4664 * overload the flags namespace and pass some flags of our
4665 * own in here that we pick off before calling mail_search.
4666 * Danger, danger, don't overlap with flag values defined
4667 * for c-client (that we want to use). Flags that we will
4668 * use here are:
4669 * MP_IN_CCLIENT_CB
4670 * If this is set we are in a callback from c-client
4671 * because some imap data arrived. We don't want to
4672 * call c-client again because it isn't re-entrant safe.
4673 * This is only a problem if we need to get the text of
4674 * a message to do the search, the envelope is cached
4675 * already.
4676 * MP_NOT
4677 * We want a ! of the patgrp in the search.
4678 * We also throw in SE_FREE for free, since we create
4679 * the search program here.
4681 * Returns: 1 if any message in the searchset matches this pattern
4682 * 0 if no matches
4683 * -1 if couldn't perform search because of no_fetch restriction
4686 match_pattern(PATGRP_S *patgrp, MAILSTREAM *stream, SEARCHSET *searchset,
4687 char *section, long int (*get_score)(MAILSTREAM *, long int),
4688 long int flags)
4690 SEARCHPGM *pgm;
4691 SEARCHSET *s;
4692 MESSAGECACHE *mc;
4693 long i, msgno = 0L;
4694 int in_client_callback = 0, not = 0;
4696 dprint((7, "match_pattern\n"));
4699 * Is the current folder the right type and possibly the right specific
4700 * folder for a match?
4702 if(!(patgrp && !patgrp->bogus && match_pattern_folder(patgrp, stream)))
4703 return(0);
4706 * NULL searchset means that there is no message to compare against.
4707 * This is a match if the folder type matches above (that gets
4708 * us here), and there are no patterns to match against.
4710 * It is not totally clear what should be done in the case of an empty
4711 * search set. If there is search criteria, and someone does something
4712 * that is not specific to any messages (composing from scratch,
4713 * forwarding an attachment), then we can't be sure what a user would
4714 * expect. The original way was to just use the role, which we'll
4715 * preserve here.
4717 if(!searchset)
4718 return(1);
4721 * change by sderr : match_pattern_folder will sometimes
4722 * accept NULL streams, but if we are not in a folder-type-only
4723 * match test, we don't
4725 if(!stream)
4726 return(0);
4728 if(flags & MP_IN_CCLIENT_CB){
4729 in_client_callback++;
4730 flags &= ~MP_IN_CCLIENT_CB;
4733 if(flags & MP_NOT){
4734 not++;
4735 flags &= ~MP_NOT;
4738 flags |= SE_FREE;
4740 if(patgrp->stat_bom != PAT_STAT_EITHER){
4741 if(patgrp->stat_bom == PAT_STAT_YES){
4742 if(!ps_global->beginning_of_month){
4743 return(0);
4746 else if(patgrp->stat_bom == PAT_STAT_NO){
4747 if(ps_global->beginning_of_month){
4748 return(0);
4753 if(patgrp->stat_boy != PAT_STAT_EITHER){
4754 if(patgrp->stat_boy == PAT_STAT_YES){
4755 if(!ps_global->beginning_of_year){
4756 return(0);
4759 else if(patgrp->stat_boy == PAT_STAT_NO){
4760 if(ps_global->beginning_of_year){
4761 return(0);
4766 if(in_client_callback && is_imap_stream(stream)
4767 && (patgrp->alltext || patgrp->bodytext
4768 || (patgrp->inabook != IAB_EITHER
4769 && any_addressbook_in_remote_stream(stream))))
4770 return(-1);
4772 pgm = match_pattern_srchpgm(patgrp, stream, searchset);
4773 if(not && !(is_imap_stream(stream) && !modern_imap_stream(stream))){
4774 SEARCHPGM *srchpgm;
4776 srchpgm = pgm;
4777 pgm = mail_newsearchpgm();
4778 pgm->not = mail_newsearchpgmlist();
4779 pgm->not->pgm = srchpgm;
4782 if((patgrp->alltext || patgrp->bodytext)
4783 && (!is_imap_stream(stream) || modern_imap_stream(stream)))
4785 * Cache isn't going to work. Search on server.
4786 * Except that is likely to not work on an old imap server because
4787 * the OR criteria won't work and we are likely to have some ORs.
4788 * So turn off the NOSERVER flag (and search on server if remote)
4789 * unless the server is an old server. It doesn't matter if we
4790 * turn if off if it's not an imap stream, but we do it anyway.
4792 flags &= ~SE_NOSERVER;
4794 if(section){
4796 * Mail_search_full only searches the top-level msg. We want to
4797 * search an attached msg instead. First do the stuff
4798 * that mail_search_full would have done before calling
4799 * mail_search_msg, then call mail_search_msg with a section number.
4800 * Mail_search_msg does take a section number even though
4801 * mail_search_full doesn't.
4805 * We'll only ever set section if the searchset is a single message.
4807 if(pgm->msgno->next == NULL && pgm->msgno->first == pgm->msgno->last)
4808 msgno = pgm->msgno->first;
4810 for(i = 1L; i <= stream->nmsgs; i++)
4811 if((mc = mail_elt(stream, i)) != NULL)
4812 mc->searched = NIL;
4814 if(mail_search_msg(stream,msgno,section,pgm)
4815 && msgno > 0L && msgno <= stream->nmsgs
4816 && (mc = mail_elt(stream, msgno)))
4817 mc->searched = T;
4819 if(flags & SE_FREE)
4820 mail_free_searchpgm(&pgm);
4822 else{
4824 * Here we could be checking on the return value to see if
4825 * the search was "successful" or not. It may be the case
4826 * that we'd want to stop trying filtering if we got some
4827 * sort of error, but for now we would just continue on
4828 * to the next filter.
4830 pine_mail_search_full(stream, "UTF-8", pgm, flags);
4833 /* we searched without the not, reverse it */
4834 if(not && is_imap_stream(stream) && !modern_imap_stream(stream)){
4835 for(msgno = 1L; msgno < mn_get_total(sp_msgmap(stream)); msgno++)
4836 if(stream && msgno && msgno <= stream->nmsgs
4837 && (mc=mail_elt(stream,msgno)) && mc->searched)
4838 mc->searched = NIL;
4839 else
4840 mc->searched = T;
4843 /* check scores */
4844 if(get_score && scores_are_used(SCOREUSE_GET) && patgrp->do_score){
4845 char *savebits;
4846 SEARCHSET *ss;
4849 * Get_score may call build_header_line recursively (we may
4850 * be in build_header_line now) so we have to preserve and
4851 * restore the sequence bits.
4853 savebits = (char *)fs_get((stream->nmsgs+1) * sizeof(char));
4855 for(i = 1L; i <= stream->nmsgs; i++){
4856 if((mc = mail_elt(stream, i)) != NULL){
4857 savebits[i] = mc->sequence;
4858 mc->sequence = 0;
4863 * Build a searchset which will get all the scores that we
4864 * need but not more.
4866 for(s = searchset; s; s = s->next)
4867 for(msgno = s->first; msgno <= s->last; msgno++)
4868 if(msgno > 0L && msgno <= stream->nmsgs
4869 && (mc = mail_elt(stream, msgno)) && mc->searched
4870 && get_msg_score(stream, msgno) == SCORE_UNDEF)
4871 mc->sequence = 1;
4873 if((ss = build_searchset(stream)) != NULL){
4874 (void)calculate_some_scores(stream, ss, in_client_callback);
4875 mail_free_searchset(&ss);
4879 * Now check the scores versus the score intervals to see if
4880 * any of the messages which have matched up to this point can
4881 * be tossed because they don't match the score interval.
4883 for(s = searchset; s; s = s->next)
4884 for(msgno = s->first; msgno <= s->last; msgno++)
4885 if(msgno > 0L && msgno <= stream->nmsgs
4886 && (mc = mail_elt(stream, msgno)) && mc->searched){
4887 long score;
4889 score = (*get_score)(stream, msgno);
4892 * If the score is outside all of the intervals,
4893 * turn off the searched bit.
4894 * So that means we check each interval and if
4895 * it is inside any interval we stop and leave
4896 * the bit set. If it is outside we keep checking.
4898 if(score != SCORE_UNDEF){
4899 INTVL_S *iv;
4901 for(iv = patgrp->score; iv; iv = iv->next)
4902 if(score >= iv->imin && score <= iv->imax)
4903 break;
4905 if(!iv)
4906 mc->searched = NIL;
4910 for(i = 1L; i <= stream->nmsgs; i++)
4911 if((mc = mail_elt(stream, i)) != NULL)
4912 mc->sequence = savebits[i];
4914 fs_give((void **)&savebits);
4917 /* if there are still matches, check for 8bit subject match */
4918 if(patgrp->stat_8bitsubj != PAT_STAT_EITHER)
4919 find_8bitsubj_in_messages(stream, searchset, patgrp->stat_8bitsubj, 1);
4921 /* if there are still matches, check for charset matches */
4922 if(patgrp->charsets)
4923 find_charsets_in_messages(stream, searchset, patgrp, 1);
4925 /* Still matches, check addrbook */
4926 if(patgrp->inabook != IAB_EITHER)
4927 address_in_abook(stream, searchset, patgrp->inabook, patgrp->abooks);
4929 /* Still matches? Run the categorization command on each msg. */
4930 if(pith_opt_filter_pattern_cmd)
4931 (*pith_opt_filter_pattern_cmd)(patgrp->category_cmd, searchset, stream, patgrp->cat_lim, patgrp->cat);
4933 for(s = searchset; s; s = s->next)
4934 for(msgno = s->first; msgno > 0L && msgno <= s->last; msgno++)
4935 if(msgno > 0L && msgno <= stream->nmsgs
4936 && (mc = mail_elt(stream, msgno)) && mc->searched)
4937 return(1);
4939 return(0);
4944 * Look through messages in searchset to see if they contain 8bit
4945 * characters in their subjects. All of the messages in
4946 * searchset should initially have the searched bit set. Turn off the
4947 * searched bit where appropriate.
4949 void
4950 find_8bitsubj_in_messages(MAILSTREAM *stream, SEARCHSET *searchset,
4951 int stat_8bitsubj, int saveseqbits)
4953 char *savebits = NULL;
4954 SEARCHSET *s, *ss = NULL;
4955 MESSAGECACHE *mc;
4956 long count = 0L;
4957 unsigned long msgno;
4960 * If we are being called while in build_header_line we may
4961 * call build_header_line recursively. So save and restore the
4962 * sequence bits.
4964 if(saveseqbits)
4965 savebits = (char *) fs_get((stream->nmsgs+1) * sizeof(char));
4967 for(msgno = 1L; msgno <= stream->nmsgs; msgno++){
4968 if((mc = mail_elt(stream, msgno)) != NULL){
4969 if(savebits)
4970 savebits[msgno] = mc->sequence;
4972 mc->sequence = 0;
4977 * Build a searchset so we can look at all the envelopes
4978 * we need to look at but only those we need to look at.
4979 * Everything with the searched bit set is still a
4980 * possibility, so restrict to that set.
4983 for(s = searchset; s; s = s->next)
4984 for(msgno = s->first; msgno <= s->last; msgno++)
4985 if(msgno > 0L && msgno <= stream->nmsgs
4986 && (mc = mail_elt(stream, msgno)) && mc->searched){
4987 mc->sequence = 1;
4988 count++;
4991 ss = build_searchset(stream);
4993 if(count){
4994 SEARCHSET **sset;
4996 mail_parameters(NULL, SET_FETCHLOOKAHEADLIMIT, (void *) count);
4999 * This causes the lookahead to fetch precisely
5000 * the messages we want (in the searchset) instead
5001 * of just fetching the next 20 sequential
5002 * messages. If the searching so far has caused
5003 * a sparse searchset in a large mailbox, the
5004 * difference can be substantial.
5005 * This resets automatically after the first fetch.
5007 sset = (SEARCHSET **) mail_parameters(stream,
5008 GET_FETCHLOOKAHEAD,
5009 (void *) stream);
5010 if(sset)
5011 *sset = ss;
5014 for(s = ss; s; s = s->next){
5015 for(msgno = s->first; msgno <= s->last; msgno++){
5016 ENVELOPE *e;
5018 if(!stream || msgno <= 0L || msgno > stream->nmsgs)
5019 continue;
5021 e = pine_mail_fetchenvelope(stream, msgno);
5022 if(stat_8bitsubj == PAT_STAT_YES){
5023 if(e && e->subject){
5024 char *p;
5026 for(p = e->subject; *p; p++)
5027 if(*p & 0x80)
5028 break;
5030 if(!*p && msgno > 0L && msgno <= stream->nmsgs
5031 && (mc = mail_elt(stream, msgno)))
5032 mc->searched = NIL;
5034 else if(msgno > 0L && msgno <= stream->nmsgs
5035 && (mc = mail_elt(stream, msgno)))
5036 mc->searched = NIL;
5038 else if(stat_8bitsubj == PAT_STAT_NO){
5039 if(e && e->subject){
5040 char *p;
5042 for(p = e->subject; *p; p++)
5043 if(*p & 0x80)
5044 break;
5046 if(*p && msgno > 0L && msgno <= stream->nmsgs
5047 && (mc = mail_elt(stream, msgno)))
5048 mc->searched = NIL;
5054 if(savebits){
5055 for(msgno = 1L; msgno <= stream->nmsgs; msgno++)
5056 if((mc = mail_elt(stream, msgno)) != NULL)
5057 mc->sequence = savebits[msgno];
5059 fs_give((void **) &savebits);
5062 if(ss)
5063 mail_free_searchset(&ss);
5068 * Look through messages in searchset to see if they contain any of the
5069 * charsets or scripts listed in charsets pattern. All of the messages in
5070 * searchset should initially have the searched bit set. Turn off the
5071 * searched bit where appropriate.
5073 void
5074 find_charsets_in_messages(MAILSTREAM *stream, SEARCHSET *searchset,
5075 PATGRP_S *patgrp, int saveseqbits)
5077 char *savebits = NULL;
5078 unsigned long msgno;
5079 long count = 0L;
5080 MESSAGECACHE *mc;
5081 SEARCHSET *s, *ss;
5083 if(!stream || !patgrp)
5084 return;
5087 * When we actually want to use charsets, we convert it into a list
5088 * of charsets instead of the mixed list of scripts and charsets and
5089 * we eliminate duplicates. This is more efficient when we actually
5090 * do the lookups and compares.
5092 if(!patgrp->charsets_list){
5093 PATTERN_S *cs;
5094 const CHARSET *cset;
5095 STRLIST_S *sl = NULL, *newsl;
5096 unsigned long scripts = 0L;
5097 SCRIPT *script;
5099 for(cs = patgrp->charsets; cs; cs = cs->next){
5101 * Run through the charsets pattern looking for
5102 * scripts and set the corresponding script bits.
5103 * If it isn't a script, it is a character set.
5105 if(cs->substring && (script = utf8_script(cs->substring)))
5106 scripts |= script->script;
5107 else{
5108 /* add it to list as a specific character set */
5109 newsl = new_strlist(cs->substring);
5110 if(compare_strlists_for_match(sl, newsl)) /* already in list */
5111 free_strlist(&newsl);
5112 else{
5113 newsl->next = sl;
5114 sl = newsl;
5120 * Now scripts has a bit set for each script the user
5121 * specified in the charsets pattern. Go through all of
5122 * the known charsets and include ones in these scripts.
5124 if(scripts){
5125 for(cset = utf8_charset(NIL); cset && cset->name; cset++){
5126 if(cset->script & scripts){
5128 /* filter this out of each script, not very useful */
5129 if(!strucmp("ISO-2022-JP-2", cset->name)
5130 || !strucmp("UTF-7", cset->name)
5131 || !strucmp("UTF-8", cset->name))
5132 continue;
5134 /* add cset->name to the list */
5135 newsl = new_strlist(cset->name);
5136 if(compare_strlists_for_match(sl, newsl))
5137 free_strlist(&newsl);
5138 else{
5139 newsl->next = sl;
5140 sl = newsl;
5146 patgrp->charsets_list = sl;
5150 * This may call build_header_line recursively because we may be in
5151 * build_header_line now. So we have to preserve and restore the
5152 * sequence bits since we want to use them here.
5154 if(saveseqbits)
5155 savebits = (char *) fs_get((stream->nmsgs+1) * sizeof(char));
5157 for(msgno = 1L; msgno <= stream->nmsgs; msgno++){
5158 if((mc = mail_elt(stream, msgno)) != NULL){
5159 if(savebits)
5160 savebits[msgno] = mc->sequence;
5162 mc->sequence = 0;
5168 * Build a searchset so we can look at all the bodies
5169 * we need to look at but only those we need to look at.
5170 * Everything with the searched bit set is still a
5171 * possibility, so restrict to that set.
5174 for(s = searchset; s; s = s->next)
5175 for(msgno = s->first; msgno <= s->last; msgno++)
5176 if(msgno > 0L && msgno <= stream->nmsgs
5177 && (mc = mail_elt(stream, msgno)) && mc->searched){
5178 mc->sequence = 1;
5179 count++;
5182 ss = build_searchset(stream);
5184 if(count){
5185 SEARCHSET **sset;
5187 mail_parameters(NULL, SET_FETCHLOOKAHEADLIMIT, (void *) count);
5190 * This causes the lookahead to fetch precisely
5191 * the messages we want (in the searchset) instead
5192 * of just fetching the next 20 sequential
5193 * messages. If the searching so far has caused
5194 * a sparse searchset in a large mailbox, the
5195 * difference can be substantial.
5196 * This resets automatically after the first fetch.
5198 sset = (SEARCHSET **) mail_parameters(stream,
5199 GET_FETCHLOOKAHEAD,
5200 (void *) stream);
5201 if(sset)
5202 *sset = ss;
5205 for(s = ss; s; s = s->next){
5206 for(msgno = s->first; msgno <= s->last; msgno++){
5208 if(msgno <= 0L || msgno > stream->nmsgs)
5209 continue;
5211 if(patgrp->charsets_list
5212 && charsets_present_in_msg(stream,msgno,patgrp->charsets_list)){
5213 if(patgrp->charsets->not){
5214 if((mc = mail_elt(stream, msgno)))
5215 mc->searched = NIL;
5217 /* else leave it */
5219 else{ /* charset isn't in message */
5220 if(!patgrp->charsets->not){
5221 if((mc = mail_elt(stream, msgno)))
5222 mc->searched = NIL;
5224 /* else leave it */
5229 if(savebits){
5230 for(msgno = 1L; msgno <= stream->nmsgs; msgno++)
5231 if((mc = mail_elt(stream, msgno)) != NULL)
5232 mc->sequence = savebits[msgno];
5234 fs_give((void **) &savebits);
5237 if(ss)
5238 mail_free_searchset(&ss);
5243 * Look for any of the charsets in this particular message.
5245 * Returns 1 if there is a match, 0 otherwise.
5248 charsets_present_in_msg(MAILSTREAM *stream, long unsigned int rawmsgno, STRLIST_S *charsets)
5250 BODY *body = NULL;
5251 ENVELOPE *env = NULL;
5252 STRLIST_S *msg_charsets = NULL;
5253 int ret = 0;
5255 if(charsets && stream && rawmsgno > 0L && rawmsgno <= stream->nmsgs){
5256 env = pine_mail_fetchstructure(stream, rawmsgno, &body);
5257 collect_charsets_from_subj(env, &msg_charsets);
5258 collect_charsets_from_body(body, &msg_charsets);
5259 if(msg_charsets){
5260 ret = compare_strlists_for_match(msg_charsets, charsets);
5261 free_strlist(&msg_charsets);
5265 return(ret);
5269 void
5270 collect_charsets_from_subj(ENVELOPE *env, STRLIST_S **listptr)
5272 STRLIST_S *newsl;
5273 char *text, *e;
5275 if(listptr && env && env->subject){
5276 /* find encoded word */
5277 for(text = env->subject; *text; text++){
5278 if((*text == '=') && (text[1] == '?') && isalpha(text[2]) &&
5279 (e = strchr(text+2,'?'))){
5280 *e = '\0'; /* tie off charset name */
5282 newsl = new_strlist(text+2);
5283 *e = '?';
5285 if(compare_strlists_for_match(*listptr, newsl))
5286 free_strlist(&newsl);
5287 else{
5288 newsl->next = *listptr;
5289 *listptr = newsl;
5298 * Check for any of the charsets in any of the charset params in
5299 * any of the text parts of the body of a message. Put them in the list
5300 * pointed to by listptr.
5302 void
5303 collect_charsets_from_body(struct mail_bodystruct *body, STRLIST_S **listptr)
5305 PART *part;
5306 char *cset;
5308 if(listptr && body){
5309 switch(body->type){
5310 case TYPEMULTIPART:
5311 for(part = body->nested.part; part; part = part->next)
5312 collect_charsets_from_body(&part->body, listptr);
5314 break;
5316 case TYPEMESSAGE:
5317 if(!strucmp(body->subtype, "RFC822")){
5318 collect_charsets_from_subj(body->nested.msg->env, listptr);
5319 collect_charsets_from_body(body->nested.msg->body, listptr);
5320 break;
5322 /* else fall through to text case */
5324 case TYPETEXT:
5325 cset = parameter_val(body->parameter, "charset");
5326 if(cset){
5327 STRLIST_S *newsl;
5329 newsl = new_strlist(cset);
5331 if(compare_strlists_for_match(*listptr, newsl))
5332 free_strlist(&newsl);
5333 else{
5334 newsl->next = *listptr;
5335 *listptr = newsl;
5338 fs_give((void **) &cset);
5341 break;
5343 default: /* non-text terminal mode */
5344 break;
5351 * If any of the names in list1 is the same as any of the names in list2
5352 * then return 1, else return 0. Comparison is case independent.
5355 compare_strlists_for_match(STRLIST_S *list1, STRLIST_S *list2)
5357 int ret = 0;
5358 STRLIST_S *cs1, *cs2;
5360 for(cs1 = list1; !ret && cs1; cs1 = cs1->next)
5361 for(cs2 = list2; !ret && cs2; cs2 = cs2->next)
5362 if(cs1->name && cs2->name && !strucmp(cs1->name, cs2->name))
5363 ret = 1;
5365 return(ret);
5370 match_pattern_folder(PATGRP_S *patgrp, MAILSTREAM *stream)
5372 int is_news;
5374 /* change by sderr : we match FLDR_ANY even if stream is NULL */
5375 return((patgrp->fldr_type == FLDR_ANY)
5376 || (stream
5377 && (((is_news = IS_NEWS(stream))
5378 && patgrp->fldr_type == FLDR_NEWS)
5379 || (!is_news && patgrp->fldr_type == FLDR_EMAIL)
5380 || (patgrp->fldr_type == FLDR_SPECIFIC
5381 && match_pattern_folder_specific(patgrp->folder,
5382 stream, FOR_PATTERN)))));
5387 * Returns positive if this stream is open on one of the folders in the
5388 * folders argument, 0 otherwise.
5390 * If FOR_PATTERN is set, this interprets simple names as nicknames in
5391 * the incoming collection, otherwise it treats simple names as being in
5392 * the primary collection.
5393 * If FOR_FILT is set, the folder names are detokenized before being used.
5396 match_pattern_folder_specific(PATTERN_S *folders, MAILSTREAM *stream, int flags)
5398 PATTERN_S *p;
5399 int match = 0;
5400 char *patfolder, *free_this = NULL;
5402 dprint((8, "match_pattern_folder_specific\n"));
5404 if(!(stream && stream->mailbox && stream->mailbox[0]))
5405 return(0);
5408 * For each of the folders in the pattern, see if we get
5409 * a match. We're just looking for any match. If none match,
5410 * we return 0, otherwise we fall through and check the rest
5411 * of the pattern. The fact that the string is called "substring"
5412 * is not meaningful. We're just using the convenient pattern
5413 * structure to store a list of folder names. They aren't
5414 * substrings of names, they are the whole name.
5416 for(p = folders; !match && p; p = p->next){
5417 free_this = NULL;
5418 if(flags & FOR_FILTER)
5419 patfolder = free_this = detoken_src(p->substring, FOR_FILT, NULL,
5420 NULL, NULL, NULL);
5421 else
5422 patfolder = p->substring;
5424 if(patfolder
5425 && (!strucmp(patfolder, ps_global->inbox_name)
5426 || !strcmp(patfolder, ps_global->VAR_INBOX_PATH))){
5427 if(sp_flagged(stream, SP_INBOX))
5428 match++;
5430 else{
5431 char *fname;
5432 char *t, *streamfolder;
5433 char tmp1[MAILTMPLEN], tmp2[MAX(MAILTMPLEN,NETMAXMBX)];
5434 CONTEXT_S *cntxt = NULL;
5436 if(flags & FOR_PATTERN){
5438 * See if patfolder is a nickname in the incoming collection.
5439 * If so, use its real name instead.
5441 if(patfolder[0] &&
5442 (ps_global->context_list->use & CNTXT_INCMNG) &&
5443 (fname = (folder_is_nick(patfolder,
5444 FOLDERS(ps_global->context_list),
5445 0))))
5446 patfolder = fname;
5448 else{
5449 char *save_ref = NULL;
5452 * If it's an absolute pathname, we treat is as a local file
5453 * instead of interpreting it in the primary context.
5455 if(!is_absolute_path(patfolder)
5456 && !(cntxt = default_save_context(ps_global->context_list)))
5457 cntxt = ps_global->context_list;
5460 * Because this check is independent of where the user is
5461 * in the folder hierarchy and has nothing to do with that,
5462 * we want to ignore the reference field built into the
5463 * context. Zero it out temporarily here.
5465 if(cntxt && cntxt->dir){
5466 save_ref = cntxt->dir->ref;
5467 cntxt->dir->ref = NULL;
5470 patfolder = context_apply(tmp1, cntxt, patfolder, sizeof(tmp1));
5471 if(save_ref)
5472 cntxt->dir->ref = save_ref;
5475 switch(patfolder[0]){
5476 case '{':
5477 if(stream->mailbox[0] == '{' &&
5478 same_stream(patfolder, stream) &&
5479 (streamfolder = strindex(&stream->mailbox[1], '}')) &&
5480 (t = strindex(&patfolder[1], '}')) &&
5481 (!strcmp(t+1, streamfolder+1) ||
5482 (*(t+1) == '\0' && !strcmp("INBOX", streamfolder+1))))
5483 match++;
5485 break;
5487 case '#':
5488 if(!strcmp(patfolder, stream->mailbox))
5489 match++;
5491 break;
5493 default:
5494 t = (strlen(patfolder) < (MAILTMPLEN/2))
5495 ? mailboxfile(tmp2, patfolder) : NULL;
5496 if(t && *t && !strcmp(t, stream->mailbox))
5497 match++;
5499 break;
5503 if(free_this)
5504 fs_give((void **) &free_this);
5507 return(match);
5512 * generate a search program corresponding to the provided patgrp
5514 SEARCHPGM *
5515 match_pattern_srchpgm(PATGRP_S *patgrp, MAILSTREAM *stream, SEARCHSET *searchset)
5517 SEARCHPGM *pgm, *tmppgm;
5518 SEARCHOR *or;
5519 SEARCHSET **sp;
5521 pgm = mail_newsearchpgm();
5523 sp = &pgm->msgno;
5524 /* copy the searchset */
5525 while(searchset){
5526 SEARCHSET *s;
5528 s = mail_newsearchset();
5529 s->first = searchset->first;
5530 s->last = searchset->last;
5531 searchset = searchset->next;
5532 *sp = s;
5533 sp = &s->next;
5536 if(!patgrp)
5537 return(pgm);
5539 if(patgrp->subj){
5540 if(patgrp->subj->not)
5541 tmppgm = next_not(pgm);
5542 else
5543 tmppgm = pgm;
5545 set_up_search_pgm("subject", patgrp->subj, tmppgm);
5548 if(patgrp->cc){
5549 if(patgrp->cc->not)
5550 tmppgm = next_not(pgm);
5551 else
5552 tmppgm = pgm;
5554 set_up_search_pgm("cc", patgrp->cc, tmppgm);
5557 if(patgrp->from){
5558 if(patgrp->from->not)
5559 tmppgm = next_not(pgm);
5560 else
5561 tmppgm = pgm;
5563 set_up_search_pgm("from", patgrp->from, tmppgm);
5566 if(patgrp->to){
5567 if(patgrp->to->not)
5568 tmppgm = next_not(pgm);
5569 else
5570 tmppgm = pgm;
5572 set_up_search_pgm("to", patgrp->to, tmppgm);
5575 if(patgrp->sender){
5576 if(patgrp->sender->not)
5577 tmppgm = next_not(pgm);
5578 else
5579 tmppgm = pgm;
5581 set_up_search_pgm("sender", patgrp->sender, tmppgm);
5584 if(patgrp->news){
5585 if(patgrp->news->not)
5586 tmppgm = next_not(pgm);
5587 else
5588 tmppgm = pgm;
5590 set_up_search_pgm("newsgroups", patgrp->news, tmppgm);
5593 /* To OR Cc */
5594 if(patgrp->recip){
5595 if(patgrp->recip->not)
5596 tmppgm = next_not(pgm);
5597 else
5598 tmppgm = pgm;
5600 or = next_or(&tmppgm->or);
5602 set_up_search_pgm("to", patgrp->recip, or->first);
5603 set_up_search_pgm("cc", patgrp->recip, or->second);
5606 /* To OR Cc OR From */
5607 if(patgrp->partic){
5608 if(patgrp->partic->not)
5609 tmppgm = next_not(pgm);
5610 else
5611 tmppgm = pgm;
5613 or = next_or(&tmppgm->or);
5615 set_up_search_pgm("to", patgrp->partic, or->first);
5617 or->second->or = mail_newsearchor();
5618 set_up_search_pgm("cc", patgrp->partic, or->second->or->first);
5619 set_up_search_pgm("from", patgrp->partic, or->second->or->second);
5622 if(patgrp->arbhdr){
5623 ARBHDR_S *a;
5625 for(a = patgrp->arbhdr; a; a = a->next)
5626 if(a->field && a->field[0] && a->p){
5627 if(a->p->not)
5628 tmppgm = next_not(pgm);
5629 else
5630 tmppgm = pgm;
5632 set_up_search_pgm(a->field, a->p, tmppgm);
5636 if(patgrp->alltext){
5637 if(patgrp->alltext->not)
5638 tmppgm = next_not(pgm);
5639 else
5640 tmppgm = pgm;
5642 set_up_search_pgm("alltext", patgrp->alltext, tmppgm);
5645 if(patgrp->bodytext){
5646 if(patgrp->bodytext->not)
5647 tmppgm = next_not(pgm);
5648 else
5649 tmppgm = pgm;
5651 set_up_search_pgm("bodytext", patgrp->bodytext, tmppgm);
5654 if(patgrp->keyword){
5655 PATTERN_S *p_old, *p_new, *new_pattern = NULL, **nextp;
5656 char *q;
5658 if(patgrp->keyword->not)
5659 tmppgm = next_not(pgm);
5660 else
5661 tmppgm = pgm;
5664 * The keyword entries may be nicknames instead of the actual
5665 * keywords, so those need to be converted to actual keywords.
5667 * If we search for keywords that are not defined for a folder
5668 * we may get error messages back that we don't want instead of
5669 * just no match. We will build a replacement pattern here which
5670 * contains only the defined subset of the keywords.
5673 nextp = &new_pattern;
5675 for(p_old = patgrp->keyword; p_old; p_old = p_old->next){
5676 q = nick_to_keyword(p_old->substring);
5677 if(user_flag_index(stream, q) >= 0){
5678 p_new = (PATTERN_S *) fs_get(sizeof(*p_new));
5679 memset(p_new, 0, sizeof(*p_new));
5680 p_new->substring = cpystr(q);
5681 *nextp = p_new;
5682 nextp = &p_new->next;
5687 * If there are some matching keywords that are defined in
5688 * the folder, then we are ok because we will match only if
5689 * we match one of those. However, if the list is empty, then
5690 * we can't just leave this part of the search program empty.
5691 * That would result in a match instead of not a match.
5692 * We can fake our way around the problem with NOT. If the
5693 * list is empty we want the opposite, so we insert a NOT in
5694 * front of an empty program. We may end up with NOT NOT if
5695 * this was already NOT'd, but that's ok, too. Alternatively,
5696 * we could undo the first NOT instead.
5699 if(new_pattern){
5700 set_up_search_pgm("keyword", new_pattern, tmppgm);
5701 free_pattern(&new_pattern);
5703 else
5704 (void) next_not(tmppgm); /* add NOT of something that matches,
5705 so the NOT thing doesn't match */
5708 if(patgrp->do_age && patgrp->age){
5709 INTVL_S *iv;
5710 SEARCHOR *or;
5712 tmppgm = pgm;
5714 for(iv = patgrp->age; iv; iv = iv->next){
5715 if(iv->next){
5716 or = next_or(&tmppgm->or);
5717 set_search_by_age(iv, or->first, patgrp->age_uses_sentdate);
5718 tmppgm = or->second;
5720 else
5721 set_search_by_age(iv, tmppgm, patgrp->age_uses_sentdate);
5725 if(patgrp->do_size && patgrp->size){
5726 INTVL_S *iv;
5727 SEARCHOR *or;
5729 tmppgm = pgm;
5731 for(iv = patgrp->size; iv; iv = iv->next){
5732 if(iv->next){
5733 or = next_or(&tmppgm->or);
5734 set_search_by_size(iv, or->first);
5735 tmppgm = or->second;
5737 else
5738 set_search_by_size(iv, tmppgm);
5742 SETPGMSTATUS(patgrp->stat_new,pgm->unseen,pgm->seen);
5743 SETPGMSTATUS(patgrp->stat_rec,pgm->recent,pgm->old);
5744 SETPGMSTATUS(patgrp->stat_del,pgm->deleted,pgm->undeleted);
5745 SETPGMSTATUS(patgrp->stat_imp,pgm->flagged,pgm->unflagged);
5746 SETPGMSTATUS(patgrp->stat_ans,pgm->answered,pgm->unanswered);
5748 return(pgm);
5752 SEARCHPGM *
5753 next_not(SEARCHPGM *pgm)
5755 SEARCHPGMLIST *not, **not_ptr;
5757 if(!pgm)
5758 return(NULL);
5760 /* find next unused not slot */
5761 for(not = pgm->not; not && not->next; not = not->next)
5764 if(not)
5765 not_ptr = &not->next;
5766 else
5767 not_ptr = &pgm->not;
5769 /* allocate */
5770 *not_ptr = mail_newsearchpgmlist();
5772 return((*not_ptr)->pgm);
5776 SEARCHOR *
5777 next_or(struct search_or **startingor)
5779 SEARCHOR *or, **or_ptr;
5781 /* find next unused or slot */
5782 for(or = (*startingor); or && or->next; or = or->next)
5785 if(or)
5786 or_ptr = &or->next;
5787 else
5788 or_ptr = startingor;
5790 /* allocate */
5791 *or_ptr = mail_newsearchor();
5793 return(*or_ptr);
5797 void
5798 set_up_search_pgm(char *field, PATTERN_S *pattern, SEARCHPGM *pgm)
5800 SEARCHOR *or;
5802 if(field && pattern && pgm){
5805 * To is special because we want to use the ReSent-To header instead
5806 * of the To header if it exists. We set up something like:
5808 * if((resent-to matches pat1 or pat2...)
5809 * OR
5810 * (<resent-to doesn't exist> AND (to matches pat1 or pat2...)))
5812 * Some servers (Exchange, apparently) seem to have trouble with
5813 * the search for the empty string to decide if the header exists
5814 * or not. So, we will search for either the empty string OR the
5815 * header with a SPACE in it. Some still have trouble with this
5816 * so we are changing it to be off by default.
5818 if(!strucmp(field, "to") && F_ON(F_USE_RESENTTO, ps_global)){
5819 or = next_or(&pgm->or);
5821 add_type_to_pgm("resent-to", pattern, or->first);
5823 /* check for resent-to doesn't exist */
5824 or->second->not = mail_newsearchpgmlist();
5826 or->second->not->pgm->or = mail_newsearchor();
5827 set_srch("resent-to", " ", or->second->not->pgm->or->first);
5828 set_srch("resent-to", "", or->second->not->pgm->or->second);
5830 /* now add the real To search to second */
5831 add_type_to_pgm(field, pattern, or->second);
5833 else
5834 add_type_to_pgm(field, pattern, pgm);
5839 void
5840 add_type_to_pgm(char *field, PATTERN_S *pattern, SEARCHPGM *pgm)
5842 PATTERN_S *p;
5843 SEARCHOR *or;
5844 SEARCHPGM *notpgm, *tpgm;
5845 int cnt = 0;
5847 if(field && pattern && pgm){
5849 * Here is a weird bit of logic. What we want here is simply
5850 * A or B or C or D
5851 * for all of the elements of pattern. Ors are a bit complicated.
5852 * The list of ORs in the SEARCHPGM structure are ANDed together,
5853 * not ORd together. It's for things like
5854 * Subject A or B AND From C or D
5855 * The Subject part would be one member of the OR list and the From
5856 * part would be another member of the OR list. Instead we want
5857 * a big OR which may have more than two members (first and second)
5858 * but the structure just has two members. So we have to build an
5859 * OR tree and we build it by going down one branch of the tree
5860 * instead of by balancing the branches.
5862 * or
5863 * / \
5864 * first==A second
5865 * / \
5866 * first==B second
5867 * / \
5868 * first==C second==D
5870 * There is an additional problem. Some servers don't like deeply
5871 * nested logic in the SEARCH command. The tree above produces a
5872 * fairly deeply nested command if the user wants to match on
5873 * several different From addresses or Subjects...
5874 * We use the tried and true equation
5876 * (A or B) == !(!A and !B)
5878 * to change the deeply nested OR tree into ANDs which aren't nested.
5879 * Right now we're only doing that if the nesting is fairly deep.
5880 * We can think of some reasons to do that. First, we know that the
5881 * OR thing works, that's what we've been using for a while and the
5882 * only problem is the deep nesting. 2nd, it is easier to understand.
5883 * 3rd, it looks dumb to use NOT NOT A instead of A.
5884 * It is probably dumb to mix the two, but what the heck.
5885 * Hubert 2003-04-02
5887 for(p = pattern; p; p = p->next)
5888 cnt++;
5890 if(cnt < 10){ /* use ORs if count is low */
5891 for(p = pattern; p; p = p->next){
5892 if(p->next){
5893 or = next_or(&pgm->or);
5895 set_srch(field, p->substring ? p->substring : "", or->first);
5896 pgm = or->second;
5898 else
5899 set_srch(field, p->substring ? p->substring : "", pgm);
5902 else{ /* else use ANDs */
5903 /* ( A or B or C ) <=> ! ( !A and !B and !C ) */
5905 /* first, NOT of the whole thing */
5906 notpgm = next_not(pgm);
5908 /* then the not list is ANDed together */
5909 for(p = pattern; p; p = p->next){
5910 tpgm = next_not(notpgm);
5911 set_srch(field, p->substring ? p->substring : "", tpgm);
5918 void
5919 set_srch(char *field, char *value, SEARCHPGM *pgm)
5921 char *decoded;
5922 STRINGLIST **list;
5924 if(!(field && value && pgm))
5925 return;
5927 if(!strucmp(field, "subject"))
5928 list = &pgm->subject;
5929 else if(!strucmp(field, "from"))
5930 list = &pgm->from;
5931 else if(!strucmp(field, "to"))
5932 list = &pgm->to;
5933 else if(!strucmp(field, "cc"))
5934 list = &pgm->cc;
5935 else if(!strucmp(field, "sender"))
5936 list = &pgm->sender;
5937 else if(!strucmp(field, "reply-to"))
5938 list = &pgm->reply_to;
5939 else if(!strucmp(field, "in-reply-to"))
5940 list = &pgm->in_reply_to;
5941 else if(!strucmp(field, "message-id"))
5942 list = &pgm->message_id;
5943 else if(!strucmp(field, "newsgroups"))
5944 list = &pgm->newsgroups;
5945 else if(!strucmp(field, "followup-to"))
5946 list = &pgm->followup_to;
5947 else if(!strucmp(field, "alltext"))
5948 list = &pgm->text;
5949 else if(!strucmp(field, "bodytext"))
5950 list = &pgm->body;
5951 else if(!strucmp(field, "keyword"))
5952 list = &pgm->keyword;
5953 else{
5954 set_srch_hdr(field, value, pgm);
5955 return;
5958 if(!list)
5959 return;
5961 *list = mail_newstringlist();
5962 decoded = (char *)rfc1522_decode_to_utf8((unsigned char *)tmp_20k_buf, SIZEOF_20KBUF, value);
5964 (*list)->text.data = (unsigned char *)cpystr(decoded);
5965 (*list)->text.size = strlen(decoded);
5969 void
5970 set_srch_hdr(char *field, char *value, SEARCHPGM *pgm)
5972 char *decoded;
5973 SEARCHHEADER **hdr;
5975 if(!(field && value && pgm))
5976 return;
5978 hdr = &pgm->header;
5979 if(!hdr)
5980 return;
5982 decoded = (char *)rfc1522_decode_to_utf8((unsigned char *)tmp_20k_buf,
5983 SIZEOF_20KBUF, value);
5984 while(*hdr && (*hdr)->next)
5985 *hdr = (*hdr)->next;
5987 if(*hdr)
5988 (*hdr)->next = mail_newsearchheader(field, decoded);
5989 else
5990 *hdr = mail_newsearchheader(field, decoded);
5994 void
5995 set_search_by_age(INTVL_S *age, SEARCHPGM *pgm, int age_uses_sentdate)
5997 time_t now, comparetime;
5998 struct tm *tm;
5999 unsigned short i;
6001 if(!(age && pgm))
6002 return;
6004 now = time(0);
6006 if(age->imin >= 0L && age->imin == age->imax){
6007 comparetime = now;
6008 comparetime -= (age->imin * 86400L);
6009 tm = localtime(&comparetime);
6010 if(tm && tm->tm_year >= 70){
6011 i = mail_shortdate(tm->tm_year - 70, tm->tm_mon + 1,
6012 tm->tm_mday);
6013 if(age_uses_sentdate)
6014 pgm->senton = i;
6015 else
6016 pgm->on = i;
6019 else{
6021 * The 20000's are just protecting against overflows.
6022 * That's back past the start of email time, anyway.
6024 if(age->imin > 0L && age->imin < 20000L){
6025 comparetime = now;
6026 comparetime -= ((age->imin - 1L) * 86400L);
6027 tm = localtime(&comparetime);
6028 if(tm && tm->tm_year >= 70){
6029 i = mail_shortdate(tm->tm_year - 70, tm->tm_mon + 1,
6030 tm->tm_mday);
6031 if(age_uses_sentdate)
6032 pgm->sentbefore = i;
6033 else
6034 pgm->before = i;
6038 if(age->imax >= 0L && age->imax < 20000L){
6039 comparetime = now;
6040 comparetime -= (age->imax * 86400L);
6041 tm = localtime(&comparetime);
6042 if(tm && tm->tm_year >= 70){
6043 i = mail_shortdate(tm->tm_year - 70, tm->tm_mon + 1,
6044 tm->tm_mday);
6045 if(age_uses_sentdate)
6046 pgm->sentsince = i;
6047 else
6048 pgm->since = i;
6055 void
6056 set_search_by_size(INTVL_S *size, SEARCHPGM *pgm)
6058 if(!(size && pgm))
6059 return;
6062 * INTVL_S intervals include the endpoints, pgm larger and smaller
6063 * do not include the endpoints.
6065 if(size->imin != INTVL_UNDEF && size->imin > 0L)
6066 pgm->larger = size->imin - 1L;
6068 if(size->imax != INTVL_UNDEF && size->imax >= 0L && size->imax != INTVL_INF)
6069 pgm->smaller = size->imax + 1L;
6073 static char *extra_hdrs;
6076 * Run through the patterns and note which headers we'll need to ask for
6077 * which aren't normally asked for and so won't be cached.
6079 void
6080 calc_extra_hdrs(void)
6082 PAT_S *pat = NULL;
6083 int alloced_size;
6084 long type = (ROLE_INCOL | ROLE_SCORE);
6085 ARBHDR_S *a;
6086 PAT_STATE pstate;
6087 char *q, *p = NULL, *hdrs[MLCMD_COUNT + 1], **pp;
6088 INDEX_COL_S *cdesc;
6089 #define INITIALSIZE 1000
6091 q = (char *)fs_get((INITIALSIZE+1) * sizeof(char));
6092 q[0] = '\0';
6093 alloced_size = INITIALSIZE;
6094 p = q;
6097 * *ALWAYS* make sure Resent-To is in the set of
6098 * extra headers getting fetched.
6100 * This is because we *will* reference it when we're
6101 * building header lines and thus want it fetched with
6102 * the standard envelope data. Worse, in the IMAP case
6103 * we're called back from c-client with the envelope data
6104 * so we can format and display the index lines as they
6105 * arrive, so we have to ensure the resent-to field
6106 * is in the cache so we don't reenter c-client
6107 * to look for it from the callback. Yeouch.
6109 add_eh(&q, &p, "resent-to", &alloced_size);
6110 add_eh(&q, &p, "resent-date", &alloced_size);
6111 add_eh(&q, &p, "resent-from", &alloced_size);
6112 add_eh(&q, &p, "resent-cc", &alloced_size);
6113 add_eh(&q, &p, "resent-subject", &alloced_size);
6116 * Sniff at viewer-hdrs too so we can include them
6117 * if there are any...
6119 for(pp = ps_global->VAR_VIEW_HEADERS; pp && *pp; pp++)
6120 if(non_eh(*pp))
6121 add_eh(&q, &p, *pp, &alloced_size);
6124 * Be sure to ask for List management headers too
6125 * since we'll offer their use in the message view
6127 for(pp = rfc2369_hdrs(hdrs); *pp; pp++)
6128 add_eh(&q, &p, *pp, &alloced_size);
6130 if(nonempty_patterns(type, &pstate))
6131 for(pat = first_pattern(&pstate);
6132 pat;
6133 pat = next_pattern(&pstate)){
6135 * This section wouldn't be necessary if sender was retreived
6136 * from the envelope. But if not, we do need to add it.
6138 if(pat->patgrp && pat->patgrp->sender)
6139 add_eh(&q, &p, "sender", &alloced_size);
6141 if(pat->patgrp && pat->patgrp->arbhdr)
6142 for(a = pat->patgrp->arbhdr; a; a = a->next)
6143 if(a->field && a->field[0] && a->p && non_eh(a->field))
6144 add_eh(&q, &p, a->field, &alloced_size);
6148 * Check for use of HEADER or X-Priority in index-format.
6150 for(cdesc = ps_global->index_disp_format; cdesc->ctype != iNothing; cdesc++){
6151 if(cdesc->ctype == iHeader && cdesc->hdrtok && cdesc->hdrtok->hdrname
6152 && cdesc->hdrtok->hdrname[0] && non_eh(cdesc->hdrtok->hdrname))
6153 add_eh(&q, &p, cdesc->hdrtok->hdrname, &alloced_size);
6154 else if(cdesc->ctype == iPrio
6155 || cdesc->ctype == iPrioAlpha
6156 || cdesc->ctype == iPrioBang)
6157 add_eh(&q, &p, PRIORITYNAME, &alloced_size);
6161 * Check for use of scorevalhdrtok in scoring patterns.
6163 type = ROLE_SCORE;
6164 if(nonempty_patterns(type, &pstate))
6165 for(pat = first_pattern(&pstate);
6166 pat;
6167 pat = next_pattern(&pstate)){
6169 * This section wouldn't be necessary if sender was retreived
6170 * from the envelope. But if not, we do need to add it.
6172 if(pat->action && pat->action->scorevalhdrtok
6173 && pat->action->scorevalhdrtok->hdrname
6174 && pat->action->scorevalhdrtok->hdrname[0]
6175 && non_eh(pat->action->scorevalhdrtok->hdrname))
6176 add_eh(&q, &p, pat->action->scorevalhdrtok->hdrname, &alloced_size);
6179 set_extra_hdrs(q);
6180 if(q)
6181 fs_give((void **)&q);
6186 non_eh(char *field)
6188 char **t;
6189 static char *existing[] = {"subject", "from", "to", "cc", "sender",
6190 "reply-to", "in-reply-to", "message-id",
6191 "path", "newsgroups", "followup-to",
6192 "references", NULL};
6195 * If it is one of these, we should already have it
6196 * from the envelope or from the extra headers c-client
6197 * already adds to the list (hdrheader and hdrtrailer
6198 * in imap4r1.c, Aug 99, slh).
6200 for(t = existing; *t; t++)
6201 if(!strucmp(field, *t))
6202 return(FALSE);
6204 return(TRUE);
6209 * Add field to extra headers string if not already there.
6211 void
6212 add_eh(char **start, char **ptr, char *field, int *asize)
6214 char *s;
6216 /* already there? */
6217 for(s = *start; (s = srchstr(s, field)) != NULL; s++)
6218 if(s[strlen(field)] == SPACE || s[strlen(field)] == '\0')
6219 return;
6221 /* enough space for it? */
6222 while(strlen(field) + (*ptr - *start) + 1 > *asize){
6223 (*asize) *= 2;
6224 fs_resize((void **)start, (*asize)+1);
6225 *ptr = *start + strlen(*start);
6228 if(*ptr > *start)
6229 sstrncpy(ptr, " ", *asize-(*ptr - *start));
6231 sstrncpy(ptr, field, *asize-(*ptr - *start));
6233 (*start)[*asize] = '\0';
6237 void
6238 set_extra_hdrs(char *hdrs)
6240 free_extra_hdrs();
6241 if(hdrs && *hdrs)
6242 extra_hdrs = cpystr(hdrs);
6246 char *
6247 get_extra_hdrs(void)
6249 return(extra_hdrs);
6253 void
6254 free_extra_hdrs(void)
6256 if(extra_hdrs)
6257 fs_give((void **)&extra_hdrs);
6262 is_ascii_string(char *str)
6264 if(!str)
6265 return(0);
6267 while(*str && isascii(*str))
6268 str++;
6270 return(*str == '\0');
6274 void
6275 free_patline(PAT_LINE_S **patline)
6277 if(patline && *patline){
6278 free_patline(&(*patline)->next);
6279 if((*patline)->filename)
6280 fs_give((void **)&(*patline)->filename);
6281 if((*patline)->filepath)
6282 fs_give((void **)&(*patline)->filepath);
6283 free_pat(&(*patline)->first);
6284 fs_give((void **)patline);
6289 void
6290 free_pat(PAT_S **pat)
6292 if(pat && *pat){
6293 free_pat(&(*pat)->next);
6294 free_patgrp(&(*pat)->patgrp);
6295 free_action(&(*pat)->action);
6296 if((*pat)->raw)
6297 fs_give((void **)&(*pat)->raw);
6299 fs_give((void **)pat);
6304 void
6305 free_patgrp(PATGRP_S **patgrp)
6307 if(patgrp && *patgrp){
6308 if((*patgrp)->nick)
6309 fs_give((void **) &(*patgrp)->nick);
6311 if((*patgrp)->comment)
6312 fs_give((void **) &(*patgrp)->comment);
6314 if((*patgrp)->category_cmd)
6315 free_list_array(&(*patgrp)->category_cmd);
6317 if((*patgrp)->charsets_list)
6318 free_strlist(&(*patgrp)->charsets_list);
6320 free_pattern(&(*patgrp)->to);
6321 free_pattern(&(*patgrp)->cc);
6322 free_pattern(&(*patgrp)->recip);
6323 free_pattern(&(*patgrp)->partic);
6324 free_pattern(&(*patgrp)->from);
6325 free_pattern(&(*patgrp)->sender);
6326 free_pattern(&(*patgrp)->news);
6327 free_pattern(&(*patgrp)->subj);
6328 free_pattern(&(*patgrp)->alltext);
6329 free_pattern(&(*patgrp)->bodytext);
6330 free_pattern(&(*patgrp)->keyword);
6331 free_pattern(&(*patgrp)->charsets);
6332 free_pattern(&(*patgrp)->folder);
6333 free_arbhdr(&(*patgrp)->arbhdr);
6334 free_intvl(&(*patgrp)->score);
6335 free_intvl(&(*patgrp)->age);
6336 fs_give((void **) patgrp);
6341 void
6342 free_pattern(PATTERN_S **pattern)
6344 if(pattern && *pattern){
6345 free_pattern(&(*pattern)->next);
6346 if((*pattern)->substring)
6347 fs_give((void **)&(*pattern)->substring);
6348 fs_give((void **)pattern);
6353 void
6354 free_arbhdr(ARBHDR_S **arbhdr)
6356 if(arbhdr && *arbhdr){
6357 free_arbhdr(&(*arbhdr)->next);
6358 if((*arbhdr)->field)
6359 fs_give((void **)&(*arbhdr)->field);
6360 free_pattern(&(*arbhdr)->p);
6361 fs_give((void **)arbhdr);
6366 void
6367 free_intvl(INTVL_S **intvl)
6369 if(intvl && *intvl){
6370 free_intvl(&(*intvl)->next);
6371 fs_give((void **) intvl);
6376 void
6377 free_action(ACTION_S **action)
6379 if(action && *action){
6380 if((*action)->from)
6381 mail_free_address(&(*action)->from);
6382 if((*action)->replyto)
6383 mail_free_address(&(*action)->replyto);
6384 if((*action)->fcc)
6385 fs_give((void **)&(*action)->fcc);
6386 if((*action)->litsig)
6387 fs_give((void **)&(*action)->litsig);
6388 if((*action)->sig)
6389 fs_give((void **)&(*action)->sig);
6390 if((*action)->template)
6391 fs_give((void **)&(*action)->template);
6392 if((*action)->scorevalhdrtok)
6393 free_hdrtok(&(*action)->scorevalhdrtok);
6394 if((*action)->cstm)
6395 free_list_array(&(*action)->cstm);
6396 if((*action)->smtp)
6397 free_list_array(&(*action)->smtp);
6398 if((*action)->nntp)
6399 free_list_array(&(*action)->nntp);
6400 if((*action)->nick)
6401 fs_give((void **)&(*action)->nick);
6402 if((*action)->inherit_nick)
6403 fs_give((void **)&(*action)->inherit_nick);
6404 if((*action)->incol)
6405 free_color_pair(&(*action)->incol);
6406 if((*action)->folder)
6407 free_pattern(&(*action)->folder);
6408 if((*action)->index_format)
6409 fs_give((void **)&(*action)->index_format);
6410 if((*action)->keyword_set)
6411 free_pattern(&(*action)->keyword_set);
6412 if((*action)->keyword_clr)
6413 free_pattern(&(*action)->keyword_clr);
6415 fs_give((void **)action);
6421 * Returns an allocated copy of the pat.
6423 * Args pat -- the source pat
6425 * Returns a copy of pat.
6427 PAT_S *
6428 copy_pat(PAT_S *pat)
6430 PAT_S *new_pat = NULL;
6432 if(pat){
6433 new_pat = (PAT_S *)fs_get(sizeof(*new_pat));
6434 memset((void *)new_pat, 0, sizeof(*new_pat));
6436 new_pat->patgrp = copy_patgrp(pat->patgrp);
6437 new_pat->action = copy_action(pat->action);
6440 return(new_pat);
6445 * Returns an allocated copy of the patgrp.
6447 * Args patgrp -- the source patgrp
6449 * Returns a copy of patgrp.
6451 PATGRP_S *
6452 copy_patgrp(PATGRP_S *patgrp)
6454 char *p;
6455 PATGRP_S *new_patgrp = NULL;
6457 if(patgrp){
6458 new_patgrp = (PATGRP_S *)fs_get(sizeof(*new_patgrp));
6459 memset((void *)new_patgrp, 0, sizeof(*new_patgrp));
6461 if(patgrp->nick)
6462 new_patgrp->nick = cpystr(patgrp->nick);
6464 if(patgrp->comment)
6465 new_patgrp->comment = cpystr(patgrp->comment);
6467 if(patgrp->to){
6468 p = pattern_to_string(patgrp->to);
6469 new_patgrp->to = string_to_pattern(p);
6470 fs_give((void **)&p);
6471 new_patgrp->to->not = patgrp->to->not;
6474 if(patgrp->from){
6475 p = pattern_to_string(patgrp->from);
6476 new_patgrp->from = string_to_pattern(p);
6477 fs_give((void **)&p);
6478 new_patgrp->from->not = patgrp->from->not;
6481 if(patgrp->sender){
6482 p = pattern_to_string(patgrp->sender);
6483 new_patgrp->sender = string_to_pattern(p);
6484 fs_give((void **)&p);
6485 new_patgrp->sender->not = patgrp->sender->not;
6488 if(patgrp->cc){
6489 p = pattern_to_string(patgrp->cc);
6490 new_patgrp->cc = string_to_pattern(p);
6491 fs_give((void **)&p);
6492 new_patgrp->cc->not = patgrp->cc->not;
6495 if(patgrp->recip){
6496 p = pattern_to_string(patgrp->recip);
6497 new_patgrp->recip = string_to_pattern(p);
6498 fs_give((void **)&p);
6499 new_patgrp->recip->not = patgrp->recip->not;
6502 if(patgrp->partic){
6503 p = pattern_to_string(patgrp->partic);
6504 new_patgrp->partic = string_to_pattern(p);
6505 fs_give((void **)&p);
6506 new_patgrp->partic->not = patgrp->partic->not;
6509 if(patgrp->news){
6510 p = pattern_to_string(patgrp->news);
6511 new_patgrp->news = string_to_pattern(p);
6512 fs_give((void **)&p);
6513 new_patgrp->news->not = patgrp->news->not;
6516 if(patgrp->subj){
6517 p = pattern_to_string(patgrp->subj);
6518 new_patgrp->subj = string_to_pattern(p);
6519 fs_give((void **)&p);
6520 new_patgrp->subj->not = patgrp->subj->not;
6523 if(patgrp->alltext){
6524 p = pattern_to_string(patgrp->alltext);
6525 new_patgrp->alltext = string_to_pattern(p);
6526 fs_give((void **)&p);
6527 new_patgrp->alltext->not = patgrp->alltext->not;
6530 if(patgrp->bodytext){
6531 p = pattern_to_string(patgrp->bodytext);
6532 new_patgrp->bodytext = string_to_pattern(p);
6533 fs_give((void **)&p);
6534 new_patgrp->bodytext->not = patgrp->bodytext->not;
6537 if(patgrp->keyword){
6538 p = pattern_to_string(patgrp->keyword);
6539 new_patgrp->keyword = string_to_pattern(p);
6540 fs_give((void **)&p);
6541 new_patgrp->keyword->not = patgrp->keyword->not;
6544 if(patgrp->charsets){
6545 p = pattern_to_string(patgrp->charsets);
6546 new_patgrp->charsets = string_to_pattern(p);
6547 fs_give((void **)&p);
6548 new_patgrp->charsets->not = patgrp->charsets->not;
6551 if(patgrp->charsets_list)
6552 new_patgrp->charsets_list = copy_strlist(patgrp->charsets_list);
6554 if(patgrp->arbhdr){
6555 ARBHDR_S *aa, *a, *new_a;
6557 aa = NULL;
6558 for(a = patgrp->arbhdr; a; a = a->next){
6559 new_a = (ARBHDR_S *)fs_get(sizeof(*new_a));
6560 memset((void *)new_a, 0, sizeof(*new_a));
6562 if(a->field)
6563 new_a->field = cpystr(a->field);
6565 if(a->p){
6566 p = pattern_to_string(a->p);
6567 new_a->p = string_to_pattern(p);
6568 fs_give((void **)&p);
6569 new_a->p->not = a->p->not;
6572 new_a->isemptyval = a->isemptyval;
6574 if(aa){
6575 aa->next = new_a;
6576 aa = aa->next;
6578 else{
6579 new_patgrp->arbhdr = new_a;
6580 aa = new_patgrp->arbhdr;
6585 new_patgrp->fldr_type = patgrp->fldr_type;
6587 if(patgrp->folder){
6588 p = pattern_to_string(patgrp->folder);
6589 new_patgrp->folder = string_to_pattern(p);
6590 fs_give((void **)&p);
6593 new_patgrp->inabook = patgrp->inabook;
6595 if(patgrp->abooks){
6596 p = pattern_to_string(patgrp->abooks);
6597 new_patgrp->abooks = string_to_pattern(p);
6598 fs_give((void **)&p);
6601 new_patgrp->do_score = patgrp->do_score;
6602 if(patgrp->score){
6603 INTVL_S *intvl, *iv, *new_iv;
6605 intvl = NULL;
6606 for(iv = patgrp->score; iv; iv = iv->next){
6607 new_iv = (INTVL_S *) fs_get(sizeof(*new_iv));
6608 memset((void *) new_iv, 0, sizeof(*new_iv));
6610 new_iv->imin = iv->imin;
6611 new_iv->imax = iv->imax;
6613 if(intvl){
6614 intvl->next = new_iv;
6615 intvl = intvl->next;
6617 else{
6618 new_patgrp->score = new_iv;
6619 intvl = new_patgrp->score;
6624 new_patgrp->do_age = patgrp->do_age;
6625 if(patgrp->age){
6626 INTVL_S *intvl, *iv, *new_iv;
6628 intvl = NULL;
6629 for(iv = patgrp->age; iv; iv = iv->next){
6630 new_iv = (INTVL_S *) fs_get(sizeof(*new_iv));
6631 memset((void *) new_iv, 0, sizeof(*new_iv));
6633 new_iv->imin = iv->imin;
6634 new_iv->imax = iv->imax;
6636 if(intvl){
6637 intvl->next = new_iv;
6638 intvl = intvl->next;
6640 else{
6641 new_patgrp->age = new_iv;
6642 intvl = new_patgrp->age;
6647 new_patgrp->age_uses_sentdate = patgrp->age_uses_sentdate;
6649 new_patgrp->do_size = patgrp->do_size;
6650 if(patgrp->size){
6651 INTVL_S *intvl, *iv, *new_iv;
6653 intvl = NULL;
6654 for(iv = patgrp->size; iv; iv = iv->next){
6655 new_iv = (INTVL_S *) fs_get(sizeof(*new_iv));
6656 memset((void *) new_iv, 0, sizeof(*new_iv));
6658 new_iv->imin = iv->imin;
6659 new_iv->imax = iv->imax;
6661 if(intvl){
6662 intvl->next = new_iv;
6663 intvl = intvl->next;
6665 else{
6666 new_patgrp->size = new_iv;
6667 intvl = new_patgrp->size;
6672 new_patgrp->stat_new = patgrp->stat_new;
6673 new_patgrp->stat_rec = patgrp->stat_rec;
6674 new_patgrp->stat_del = patgrp->stat_del;
6675 new_patgrp->stat_imp = patgrp->stat_imp;
6676 new_patgrp->stat_ans = patgrp->stat_ans;
6678 new_patgrp->stat_8bitsubj = patgrp->stat_8bitsubj;
6679 new_patgrp->stat_bom = patgrp->stat_bom;
6680 new_patgrp->stat_boy = patgrp->stat_boy;
6682 new_patgrp->do_cat = patgrp->do_cat;
6683 if(patgrp->cat){
6684 INTVL_S *intvl, *iv, *new_iv;
6686 intvl = NULL;
6687 for(iv = patgrp->cat; iv; iv = iv->next){
6688 new_iv = (INTVL_S *) fs_get(sizeof(*new_iv));
6689 memset((void *) new_iv, 0, sizeof(*new_iv));
6691 new_iv->imin = iv->imin;
6692 new_iv->imax = iv->imax;
6694 if(intvl){
6695 intvl->next = new_iv;
6696 intvl = intvl->next;
6698 else{
6699 new_patgrp->cat = new_iv;
6700 intvl = new_patgrp->cat;
6705 if(patgrp->category_cmd)
6706 new_patgrp->category_cmd = copy_list_array(patgrp->category_cmd);
6709 return(new_patgrp);
6714 * Returns an allocated copy of the action.
6716 * Args action -- the source action
6718 * Returns a copy of action.
6720 ACTION_S *
6721 copy_action(ACTION_S *action)
6723 ACTION_S *newaction = NULL;
6724 char *p;
6726 if(action){
6727 newaction = (ACTION_S *)fs_get(sizeof(*newaction));
6728 memset((void *)newaction, 0, sizeof(*newaction));
6730 newaction->is_a_role = action->is_a_role;
6731 newaction->is_a_incol = action->is_a_incol;
6732 newaction->is_a_score = action->is_a_score;
6733 newaction->is_a_filter = action->is_a_filter;
6734 newaction->is_a_other = action->is_a_other;
6735 newaction->is_a_srch = action->is_a_srch;
6736 newaction->repl_type = action->repl_type;
6737 newaction->forw_type = action->forw_type;
6738 newaction->comp_type = action->comp_type;
6739 newaction->scoreval = action->scoreval;
6740 newaction->kill = action->kill;
6741 newaction->state_setting_bits = action->state_setting_bits;
6742 newaction->move_only_if_not_deleted = action->move_only_if_not_deleted;
6743 newaction->non_terminating = action->non_terminating;
6744 newaction->sort_is_set = action->sort_is_set;
6745 newaction->sortorder = action->sortorder;
6746 newaction->revsort = action->revsort;
6747 newaction->startup_rule = action->startup_rule;
6749 if(action->from)
6750 newaction->from = copyaddrlist(action->from);
6751 if(action->replyto)
6752 newaction->replyto = copyaddrlist(action->replyto);
6753 if(action->cstm)
6754 newaction->cstm = copy_list_array(action->cstm);
6755 if(action->smtp)
6756 newaction->smtp = copy_list_array(action->smtp);
6757 if(action->nntp)
6758 newaction->nntp = copy_list_array(action->nntp);
6759 if(action->fcc)
6760 newaction->fcc = cpystr(action->fcc);
6761 if(action->litsig)
6762 newaction->litsig = cpystr(action->litsig);
6763 if(action->sig)
6764 newaction->sig = cpystr(action->sig);
6765 if(action->template)
6766 newaction->template = cpystr(action->template);
6767 if(action->nick)
6768 newaction->nick = cpystr(action->nick);
6769 if(action->inherit_nick)
6770 newaction->inherit_nick = cpystr(action->inherit_nick);
6771 if(action->incol)
6772 newaction->incol = new_color_pair(action->incol->fg,
6773 action->incol->bg);
6774 if(action->scorevalhdrtok){
6775 newaction->scorevalhdrtok = new_hdrtok(action->scorevalhdrtok->hdrname);
6776 if(action->scorevalhdrtok && action->scorevalhdrtok->fieldseps){
6777 if(newaction->scorevalhdrtok->fieldseps)
6778 fs_give((void **) &newaction->scorevalhdrtok->fieldseps);
6780 newaction->scorevalhdrtok->fieldseps = cpystr(action->scorevalhdrtok->fieldseps);
6784 if(action->folder){
6785 p = pattern_to_string(action->folder);
6786 newaction->folder = string_to_pattern(p);
6787 fs_give((void **) &p);
6790 if(action->keyword_set){
6791 p = pattern_to_string(action->keyword_set);
6792 newaction->keyword_set = string_to_pattern(p);
6793 fs_give((void **) &p);
6796 if(action->keyword_clr){
6797 p = pattern_to_string(action->keyword_clr);
6798 newaction->keyword_clr = string_to_pattern(p);
6799 fs_give((void **) &p);
6802 if(action->index_format)
6803 newaction->index_format = cpystr(action->index_format);
6806 return(newaction);
6811 * Given a role, return an allocated role. If this role inherits from
6812 * another role, then do the correct inheriting so that the result is
6813 * the role we want to use. The inheriting that is done is just the set
6814 * of set- actions. This is for role stuff, no inheriting happens for scores
6815 * or for colors.
6817 * Args role -- The source role
6819 * Returns a role.
6821 ACTION_S *
6822 combine_inherited_role(ACTION_S *role)
6824 PAT_STATE pstate;
6825 PAT_S *pat;
6828 * Protect against loops in the role inheritance.
6830 if(role && role->is_a_role && nonempty_patterns(ROLE_DO_ROLES, &pstate))
6831 for(pat = first_pattern(&pstate); pat; pat = next_pattern(&pstate))
6832 if(pat->action){
6833 if(pat->action == role)
6834 pat->action->been_here_before = 1;
6835 else
6836 pat->action->been_here_before = 0;
6839 return(combine_inherited_role_guts(role));
6843 ACTION_S *
6844 combine_inherited_role_guts(ACTION_S *role)
6846 ACTION_S *newrole = NULL, *inherit_role = NULL;
6847 PAT_STATE pstate;
6849 if(role && role->is_a_role){
6850 newrole = (ACTION_S *)fs_get(sizeof(*newrole));
6851 memset((void *)newrole, 0, sizeof(*newrole));
6853 newrole->repl_type = role->repl_type;
6854 newrole->forw_type = role->forw_type;
6855 newrole->comp_type = role->comp_type;
6856 newrole->is_a_role = role->is_a_role;
6858 if(role->inherit_nick && role->inherit_nick[0] &&
6859 nonempty_patterns(ROLE_DO_ROLES, &pstate)){
6860 PAT_S *pat;
6862 /* find the inherit_nick pattern */
6863 for(pat = first_pattern(&pstate);
6864 pat;
6865 pat = next_pattern(&pstate)){
6866 if(pat->patgrp &&
6867 pat->patgrp->nick &&
6868 !strucmp(role->inherit_nick, pat->patgrp->nick)){
6869 /* found it, if it has a role, use it */
6870 if(!pat->action->been_here_before){
6871 pat->action->been_here_before = 1;
6872 inherit_role = pat->action;
6875 break;
6880 * inherit_role might inherit further from other roles.
6881 * In any case, we copy it so that we'll consistently have
6882 * an allocated copy.
6884 if(inherit_role){
6885 if(inherit_role->inherit_nick && inherit_role->inherit_nick[0])
6886 inherit_role = combine_inherited_role_guts(inherit_role);
6887 else
6888 inherit_role = copy_action(inherit_role);
6892 if(role->from)
6893 newrole->from = copyaddrlist(role->from);
6894 else if(inherit_role && inherit_role->from)
6895 newrole->from = copyaddrlist(inherit_role->from);
6897 if(role->replyto)
6898 newrole->replyto = copyaddrlist(role->replyto);
6899 else if(inherit_role && inherit_role->replyto)
6900 newrole->replyto = copyaddrlist(inherit_role->replyto);
6902 if(role->fcc)
6903 newrole->fcc = cpystr(role->fcc);
6904 else if(inherit_role && inherit_role->fcc)
6905 newrole->fcc = cpystr(inherit_role->fcc);
6907 if(role->litsig)
6908 newrole->litsig = cpystr(role->litsig);
6909 else if(inherit_role && inherit_role->litsig)
6910 newrole->litsig = cpystr(inherit_role->litsig);
6912 if(role->sig)
6913 newrole->sig = cpystr(role->sig);
6914 else if(inherit_role && inherit_role->sig)
6915 newrole->sig = cpystr(inherit_role->sig);
6917 if(role->template)
6918 newrole->template = cpystr(role->template);
6919 else if(inherit_role && inherit_role->template)
6920 newrole->template = cpystr(inherit_role->template);
6922 if(role->cstm)
6923 newrole->cstm = copy_list_array(role->cstm);
6924 else if(inherit_role && inherit_role->cstm)
6925 newrole->cstm = copy_list_array(inherit_role->cstm);
6927 if(role->smtp)
6928 newrole->smtp = copy_list_array(role->smtp);
6929 else if(inherit_role && inherit_role->smtp)
6930 newrole->smtp = copy_list_array(inherit_role->smtp);
6932 if(role->nntp)
6933 newrole->nntp = copy_list_array(role->nntp);
6934 else if(inherit_role && inherit_role->nntp)
6935 newrole->nntp = copy_list_array(inherit_role->nntp);
6937 if(role->nick)
6938 newrole->nick = cpystr(role->nick);
6940 if(inherit_role)
6941 free_action(&inherit_role);
6944 return(newrole);
6948 void
6949 mail_expunge_prefilter(MAILSTREAM *stream, int flags)
6951 int sfdo_state = 0, /* Some Filter Depends On or Sets State */
6952 sfdo_scores = 0, /* Some Filter Depends On Scores */
6953 ssdo_state = 0; /* Some Score Depends On State */
6955 if(!stream || !sp_flagged(stream, SP_LOCKED))
6956 return;
6959 * An Expunge causes a re-examination of the filters to
6960 * see if any state changes have caused new matches.
6963 sfdo_scores = (scores_are_used(SCOREUSE_GET) & SCOREUSE_FILTERS);
6964 if(sfdo_scores)
6965 ssdo_state = (scores_are_used(SCOREUSE_GET) & SCOREUSE_STATEDEP);
6967 if(!(sfdo_scores && ssdo_state))
6968 sfdo_state = some_filter_depends_on_active_state();
6971 if(sfdo_state || (sfdo_scores && ssdo_state)){
6972 if(sfdo_scores && ssdo_state)
6973 clear_folder_scores(stream);
6975 reprocess_filter_patterns(stream, sp_msgmap(stream),
6976 (flags & MI_CLOSING) |
6977 MI_REFILTERING | MI_STATECHGONLY);
6982 /*----------------------------------------------------------------------
6983 Dispatch messages matching FILTER patterns.
6985 Args:
6986 stream -- mail stream serving messages
6987 msgmap -- sequence to msgno mapping table
6988 recent -- number of recent messages to check (but really only its
6989 nonzeroness is used)
6991 When we're done, any filtered messages are filtered and the message
6992 mapping table has any filtered messages removed.
6993 ---*/
6994 void
6995 process_filter_patterns(MAILSTREAM *stream, MSGNO_S *msgmap, long int recent)
6997 long i, n, raw;
6998 imapuid_t uid;
6999 int we_cancel = 0, any_msgs = 0, any_to_filter = 0;
7000 int exbits, nt = 0, pending_actions = 0, for_debugging = 0;
7001 int cleared_index_cache = 0;
7002 long rflags = ROLE_DO_FILTER;
7003 char *nick = NULL;
7004 char busymsg[80];
7005 MSGNO_S *tmpmap = NULL;
7006 MESSAGECACHE *mc;
7007 PAT_S *pat, *nextpat = NULL;
7008 SEARCHPGM *pgm = NULL;
7009 SEARCHSET *srchset = NULL;
7010 long flags = (SE_NOPREFETCH|SE_FREE);
7011 PAT_STATE pstate;
7013 dprint((5, "process_filter_patterns(stream=%s, recent=%ld)\n",
7014 !stream ? "<null>" :
7015 sp_flagged(stream, SP_INBOX) ? "inbox" :
7016 stream->original_mailbox ? stream->original_mailbox :
7017 stream->mailbox ? stream->mailbox :
7018 "?",
7019 recent));
7021 if(!msgmap || !stream)
7022 return;
7024 if(!recent)
7025 sp_set_flags(stream, sp_flags(stream) | SP_FILTERED);
7027 while(stream && stream->nmsgs && nonempty_patterns(rflags, &pstate)){
7029 for_debugging++;
7030 pending_actions = 0;
7031 nextpat = NULL;
7033 uid = mail_uid(stream, stream->nmsgs);
7036 * Some of the search stuff won't work on old servers so we
7037 * get the data and search locally. Big performance hit.
7039 if(is_imap_stream(stream) && !modern_imap_stream(stream))
7040 flags |= SE_NOSERVER;
7043 * ignore all previously filtered messages
7044 * and, if requested, anything not a recent
7045 * arrival...
7047 * Here we're using spare6 (MN_STMP), meaning we'll only
7048 * search the ones with spare6 marked, new messages coming
7049 * in will not be considered. There used to be orig_nmsgs,
7050 * which kept track of this, but if a message gets expunged,
7051 * then a new message could be lower than orig_nmsgs.
7053 for(i = 1; i <= stream->nmsgs; i++)
7054 if(msgno_exceptions(stream, i, "0", &exbits, FALSE)){
7055 if(exbits & MSG_EX_FILTERED){
7056 if((mc = mail_elt(stream, i)) != NULL)
7057 mc->spare6 = 0;
7059 else if(!recent || !(exbits & MSG_EX_TESTED)){
7060 if((mc = mail_elt(stream, i)) != NULL)
7061 mc->spare6 = 1;
7063 any_to_filter++;
7065 else if((mc = mail_elt(stream, i)) != NULL)
7066 mc->spare6 = 0;
7068 else{
7069 if((mc = mail_elt(stream, i)) != NULL)
7070 mc->spare6 = !recent;
7072 any_to_filter += !recent;
7075 if(!any_to_filter){
7076 dprint((5, "No messages need filtering\n"));
7079 /* Then start searching */
7080 for(pat = first_pattern(&pstate); any_to_filter && pat; pat = nextpat){
7081 nextpat = next_pattern(&pstate);
7082 dprint((5,
7083 "Trying filter \"%s\"\n",
7084 (pat->patgrp && pat->patgrp->nick)
7085 ? pat->patgrp->nick : "?"));
7086 if(pat->patgrp && !pat->patgrp->bogus
7087 && pat->action && !pat->action->bogus
7088 && !trivial_patgrp(pat->patgrp)
7089 && match_pattern_folder(pat->patgrp, stream)
7090 && !match_pattern_folder_specific(pat->action->folder,
7091 stream, FOR_FILTER)){
7094 * We could just keep track of spare6 accurately when
7095 * we change the msgno_exceptions flags, but...
7097 for(i = 1; i <= stream->nmsgs; i++){
7098 if((mc=mail_elt(stream, i)) && mc->spare6){
7099 if(msgno_exceptions(stream, i, "0", &exbits, FALSE)){
7100 if(exbits & MSG_EX_FILTERED)
7101 mc->sequence = 0;
7102 else if(!recent || !(exbits & MSG_EX_TESTED))
7103 mc->sequence = 1;
7104 else
7105 mc->sequence = 0;
7107 else
7108 mc->sequence = !recent;
7110 else
7111 mc->sequence = 0;
7114 if(!(srchset = build_searchset(stream))){
7115 dprint((5, "Empty searchset\n"));
7116 continue; /* nothing to search, move on */
7119 #ifdef DEBUG
7120 {SEARCHSET *s;
7121 dprint((5, "searchset="));
7122 for(s = srchset; s; s = s->next){
7123 if(s->first == s->last || s->last == 0L){
7124 dprint((5, " %ld", s->first));
7126 else{
7127 dprint((5, " %ld-%ld", s->first, s->last));
7130 dprint((5, "\n"));
7132 #endif
7133 nick = (pat && pat->patgrp && pat->patgrp->nick
7134 && pat->patgrp->nick[0]) ? pat->patgrp->nick : NULL;
7135 snprintf(busymsg, sizeof(busymsg), _("Processing filter \"%s\""),
7136 nick ? nick : "?");
7139 * The strange last argument is so that the busy message
7140 * won't come out until after a second if the user sets
7141 * the feature to quell "filtering done". That's because
7142 * they are presumably interested in the filtering actions
7143 * themselves more than what is happening, so they'd
7144 * rather see the action messages instead of the processing
7145 * message. That's my theory anyway.
7147 if(F_OFF(F_QUELL_FILTER_MSGS, ps_global))
7148 any_msgs = we_cancel = busy_cue(busymsg, NULL,
7149 F_ON(F_QUELL_FILTER_DONE_MSG, ps_global)
7150 ? 1 : 0);
7152 if(pat->patgrp->stat_bom != PAT_STAT_EITHER){
7153 if(pat->patgrp->stat_bom == PAT_STAT_YES){
7154 if(!ps_global->beginning_of_month){
7155 dprint((5,
7156 "Filter %s wants beginning of month and it isn't bom\n",
7157 nick ? nick : "?"));
7158 continue;
7161 else if(pat->patgrp->stat_bom == PAT_STAT_NO){
7162 if(ps_global->beginning_of_month){
7163 dprint((5,
7164 "Filter %s does not want beginning of month and it is bom\n",
7165 nick ? nick : "?"));
7166 continue;
7171 if(pat->patgrp->stat_boy != PAT_STAT_EITHER){
7172 if(pat->patgrp->stat_boy == PAT_STAT_YES){
7173 if(!ps_global->beginning_of_year){
7174 dprint((5,
7175 "Filter %s wants beginning of year and it isn't boy\n",
7176 nick ? nick : "?"));
7177 continue;
7180 else if(pat->patgrp->stat_boy == PAT_STAT_NO){
7181 if(ps_global->beginning_of_year){
7182 dprint((5,
7183 "Filter %s does not want beginning of year and it is boy\n",
7184 nick ? nick : "?"));
7185 continue;
7190 pgm = match_pattern_srchpgm(pat->patgrp, stream, srchset);
7192 pine_mail_search_full(stream, "UTF-8", pgm, flags);
7194 /* check scores */
7195 if(scores_are_used(SCOREUSE_GET) & SCOREUSE_FILTERS &&
7196 pat->patgrp->do_score){
7197 SEARCHSET *s, *ss;
7200 * Build a searchset so we can get all the scores we
7201 * need and only the scores we need efficiently.
7204 for(i = 1; i <= stream->nmsgs; i++)
7205 if((mc = mail_elt(stream, i)) != NULL)
7206 mc->sequence = 0;
7208 for(s = srchset; s; s = s->next)
7209 for(i = s->first; i <= s->last; i++)
7210 if(i > 0L && stream && i <= stream->nmsgs
7211 && (mc=mail_elt(stream, i)) && mc->searched &&
7212 get_msg_score(stream, i) == SCORE_UNDEF)
7213 mc->sequence = 1;
7215 if((ss = build_searchset(stream)) != NULL){
7216 (void)calculate_some_scores(stream, ss, 0);
7217 mail_free_searchset(&ss);
7221 * Now check the patterns which have matched so far
7222 * to see if their score is in the score interval.
7224 for(s = srchset; s; s = s->next)
7225 for(i = s->first; i <= s->last; i++)
7226 if(i > 0L && stream && i <= stream->nmsgs
7227 && (mc=mail_elt(stream, i)) && mc->searched){
7228 long score;
7230 score = get_msg_score(stream, i);
7233 * If the score is outside all of the intervals,
7234 * turn off the searched bit.
7235 * So that means we check each interval and if
7236 * it is inside any interval we stop and leave
7237 * the bit set. If it is outside we keep checking.
7239 if(score != SCORE_UNDEF){
7240 INTVL_S *iv;
7242 for(iv = pat->patgrp->score; iv; iv = iv->next)
7243 if(score >= iv->imin && score <= iv->imax)
7244 break;
7246 if(!iv)
7247 mc->searched = NIL;
7252 /* check for 8bit subject match or not */
7253 if(pat->patgrp->stat_8bitsubj != PAT_STAT_EITHER)
7254 find_8bitsubj_in_messages(stream, srchset,
7255 pat->patgrp->stat_8bitsubj, 0);
7257 /* if there are still matches, check for charset matches */
7258 if(pat->patgrp->charsets)
7259 find_charsets_in_messages(stream, srchset, pat->patgrp, 0);
7261 if(pat->patgrp->inabook != IAB_EITHER)
7262 address_in_abook(stream, srchset, pat->patgrp->inabook, pat->patgrp->abooks);
7264 /* Still matches? Run the categorization command on each msg. */
7265 if(pith_opt_filter_pattern_cmd)
7266 (*pith_opt_filter_pattern_cmd)(pat->patgrp->category_cmd, srchset, stream, pat->patgrp->cat_lim, pat->patgrp->cat);
7268 if(we_cancel){
7269 cancel_busy_cue(-1);
7270 we_cancel = 0;
7273 nt = pat->action->non_terminating;
7274 pending_actions = MAX(nt, pending_actions);
7277 * Change some state bits.
7278 * This used to only happen if kill was not set, but
7279 * it can be useful to Delete a message even if killing.
7280 * That way, it will show up in another pine that isn't
7281 * running the same filter as Deleted, so the user won't
7282 * bother looking at it. Hubert 2004-11-16
7284 if(pat->action->state_setting_bits
7285 || pat->action->keyword_set
7286 || pat->action->keyword_clr){
7287 tmpmap = NULL;
7288 mn_init(&tmpmap, stream->nmsgs);
7290 for(i = 1L, n = 0L; i <= stream->nmsgs; i++)
7291 if((mc = mail_elt(stream, i)) && mc->searched
7292 && !(msgno_exceptions(stream, i, "0", &exbits, FALSE)
7293 && (exbits & MSG_EX_FILTERED))){
7294 if(!n++){
7295 mn_set_cur(tmpmap, i);
7297 else{
7298 mn_add_cur(tmpmap, i);
7302 if(n){
7303 long flagbits;
7304 char **keywords_to_set = NULL,
7305 **keywords_to_clr = NULL;
7306 PATTERN_S *pp;
7307 int cnt;
7309 flagbits = pat->action->state_setting_bits;
7311 if(pat->action->keyword_set){
7312 for(cnt = 0, pp = pat->action->keyword_set;
7313 pp; pp = pp->next)
7314 cnt++;
7316 keywords_to_set = (char **) fs_get((cnt+1) *
7317 sizeof(*keywords_to_set));
7318 memset(keywords_to_set, 0,
7319 (cnt+1) * sizeof(*keywords_to_set));
7320 for(cnt = 0, pp = pat->action->keyword_set;
7321 pp; pp = pp->next){
7322 char *q;
7324 q = nick_to_keyword(pp->substring);
7325 if(q && q[0])
7326 keywords_to_set[cnt++] = cpystr(q);
7329 flagbits |= F_KEYWORD;
7332 if(pat->action->keyword_clr){
7333 for(cnt = 0, pp = pat->action->keyword_clr;
7334 pp; pp = pp->next)
7335 cnt++;
7337 keywords_to_clr = (char **) fs_get((cnt+1) *
7338 sizeof(*keywords_to_clr));
7339 memset(keywords_to_clr, 0,
7340 (cnt+1) * sizeof(*keywords_to_clr));
7341 for(cnt = 0, pp = pat->action->keyword_clr;
7342 pp; pp = pp->next){
7343 char *q;
7345 q = nick_to_keyword(pp->substring);
7346 if(q && q[0])
7347 keywords_to_clr[cnt++] = cpystr(q);
7350 flagbits |= F_UNKEYWORD;
7353 set_some_flags(stream, tmpmap, flagbits,
7354 keywords_to_set, keywords_to_clr, 1,
7355 nick);
7358 mn_give(&tmpmap);
7362 * The two halves of the if-else are almost the same and
7363 * could probably be combined cleverly. The if clause
7364 * is simply setting the MSG_EX_FILTERED bit, and leaving
7365 * n set to zero. The msgno_exclude is not done in this case.
7366 * The else clause excludes each message (because it is
7367 * either filtered into nothing or moved to folder). The
7368 * exclude messes with the msgmap and that changes max_msgno,
7369 * so the loop control is a little tricky.
7371 if(!(pat->action->kill || pat->action->folder)){
7372 n = 0L;
7373 for(i = 1L; i <= mn_get_total(msgmap); i++)
7374 if((raw = mn_m2raw(msgmap, i)) > 0L
7375 && stream && raw <= stream->nmsgs
7376 && (mc = mail_elt(stream, raw)) && mc->searched){
7377 dprint((5,
7378 "FILTER matching \"%s\": msg %ld%s\n",
7379 nick ? nick : "unnamed",
7380 raw, nt ? " (dont stop)" : ""));
7381 if(msgno_exceptions(stream, raw, "0", &exbits, FALSE))
7382 exbits |= (nt ? MSG_EX_FILTONCE : MSG_EX_FILTERED);
7383 else
7384 exbits = (nt ? MSG_EX_FILTONCE : MSG_EX_FILTERED);
7387 * If this matched an earlier non-terminating rule
7388 * we've been keeping track of that so that we can
7389 * turn it into a permanent match at the end.
7390 * However, now we've matched another rule that is
7391 * terminating so we don't have to worry about it
7392 * anymore. Turn off the flag.
7394 if(!nt && exbits & MSG_EX_FILTONCE)
7395 exbits ^= MSG_EX_FILTONCE;
7397 exbits &= ~MSG_EX_STATECHG;
7399 msgno_exceptions(stream, raw, "0", &exbits, TRUE);
7402 else{
7403 for(i = 1L, n = 0L; i <= mn_get_total(msgmap); )
7404 if((raw = mn_m2raw(msgmap, i))
7405 && raw > 0L && stream && raw <= stream->nmsgs
7406 && (mc = mail_elt(stream, raw)) && mc->searched){
7407 dprint((5,
7408 "FILTER matching \"%s\": msg %ld %s%s\n",
7409 nick ? nick : "unnamed",
7410 raw, pat->action->folder ? "filed" : "killed",
7411 nt ? " (dont stop)" : ""));
7412 if(nt)
7413 i++;
7414 else{
7415 if(!cleared_index_cache
7416 && stream == ps_global->mail_stream){
7417 cleared_index_cache = 1;
7418 clear_index_cache(stream, 0);
7421 msgno_exclude(stream, msgmap, i, 1);
7423 * If this message is new, decrement
7424 * new_mail_count. Previously, the caller would
7425 * do this by counting MN_EXCLUDE before and after,
7426 * but the results weren't accurate in the case
7427 * where new messages arrived while filtering,
7428 * or the filtered message could have gotten
7429 * expunged.
7431 if(msgno_exceptions(stream, raw, "0", &exbits,
7432 FALSE)
7433 && (exbits & MSG_EX_RECENT)){
7434 long l, ll;
7436 l = sp_new_mail_count(stream);
7437 ll = sp_recent_since_visited(stream);
7438 dprint((5, "New message being filtered, decrement new_mail_count: %ld -> %ld\n", l, l-1L));
7439 if(l > 0L)
7440 sp_set_new_mail_count(stream, l-1L);
7441 if(ll > 0L)
7442 sp_set_recent_since_visited(stream, ll-1L);
7446 if(msgno_exceptions(stream, raw, "0", &exbits, FALSE))
7447 exbits |= (nt ? MSG_EX_FILTONCE : MSG_EX_FILTERED);
7448 else
7449 exbits = (nt ? MSG_EX_FILTONCE : MSG_EX_FILTERED);
7451 /* set pending exclusion for later */
7452 if(nt)
7453 exbits |= MSG_EX_PEND_EXLD;
7456 * If this matched an earlier non-terminating rule
7457 * we've been keeping track of that so that we can
7458 * turn it into a permanent match at the end.
7459 * However, now we've matched another rule that is
7460 * terminating so we don't have to worry about it
7461 * anymore. Turn off the flags.
7463 if(!nt && exbits & MSG_EX_FILTONCE){
7464 exbits ^= MSG_EX_FILTONCE;
7466 /* we've already excluded it, too */
7467 if(exbits & MSG_EX_PEND_EXLD)
7468 exbits ^= MSG_EX_PEND_EXLD;
7471 exbits &= ~MSG_EX_STATECHG;
7473 msgno_exceptions(stream, raw, "0", &exbits, TRUE);
7474 n++;
7476 else
7477 i++;
7480 if(n && pat->action->folder){
7481 PATTERN_S *p;
7482 int err = 0;
7484 tmpmap = NULL;
7485 mn_init(&tmpmap, stream->nmsgs);
7488 * For everything matching msg that hasn't
7489 * already been saved somewhere, do it...
7491 for(i = 1L, n = 0L; i <= stream->nmsgs; i++)
7492 if((mc = mail_elt(stream, i)) && mc->searched
7493 && !(msgno_exceptions(stream, i, "0", &exbits, FALSE)
7494 && (exbits & MSG_EX_FILED))){
7495 if(!n++){
7496 mn_set_cur(tmpmap, i);
7498 else{
7499 mn_add_cur(tmpmap, i);
7504 * Remove already deleted messages from the tmp
7505 * message map.
7506 * There is a bug with this. If a filter moves a
7507 * message to another folder _and_ sets the deleted
7508 * status, then the setting of the deleted status
7509 * will already have happened above in set_some_flags.
7510 * So if the move_only_if_not_deleted bit is set that
7511 * message will never be moved. A workaround for the
7512 * user is to not set the move-only-if-not-deleted
7513 * option.
7515 if(n && pat->action->move_only_if_not_deleted){
7516 char *seq;
7517 MSGNO_S *tmpmap2 = NULL;
7518 long nn = 0L;
7519 MESSAGECACHE *mc;
7521 mn_init(&tmpmap2, stream->nmsgs);
7524 * First, make sure elts are valid for all the
7525 * interesting messages.
7527 if((seq = invalid_elt_sequence(stream, tmpmap)) != NULL){
7528 pine_mail_fetch_flags(stream, seq, NIL);
7529 fs_give((void **) &seq);
7532 for(i = mn_first_cur(tmpmap); i > 0L;
7533 i = mn_next_cur(tmpmap)){
7534 mc = ((raw = mn_m2raw(tmpmap, i)) > 0L
7535 && stream && raw <= stream->nmsgs)
7536 ? mail_elt(stream, raw) : NULL;
7537 if(mc && !mc->deleted){
7538 if(!nn++){
7539 mn_set_cur(tmpmap2, i);
7541 else{
7542 mn_add_cur(tmpmap2, i);
7547 mn_give(&tmpmap);
7548 tmpmap = tmpmap2;
7549 n = nn;
7552 if(n){
7553 for(p = pat->action->folder; p; p = p->next){
7554 int dval;
7555 int flags_for_save;
7556 char *processed_name;
7558 /* does this filter set delete bit? ... */
7559 convert_statebits_to_vals(pat->action->state_setting_bits, &dval, NULL, NULL, NULL);
7560 /* ... if so, tell save not to fix it before copy */
7561 flags_for_save = SV_FOR_FILT | SV_INBOXWOCNTXT |
7562 (nt ? 0 : SV_DELETE) |
7563 ((dval != ACT_STAT_SET) ? SV_FIX_DELS : 0);
7564 processed_name = detoken_src(p->substring,
7565 FOR_FILT, NULL,
7566 NULL, NULL, NULL);
7567 if(move_filtered_msgs(stream, tmpmap,
7568 (processed_name && *processed_name)
7569 ? processed_name : p->substring,
7570 flags_for_save, nick)){
7572 * If we filtered into the current
7573 * folder, chuck a ping down the
7574 * stream so the user can notice it
7575 * before the next new mail check...
7577 if(ps_global->mail_stream
7578 && ps_global->mail_stream != stream
7579 && match_pattern_folder_specific(
7580 pat->action->folder,
7581 ps_global->mail_stream,
7582 FOR_FILTER)){
7583 (void) pine_mail_ping(ps_global->mail_stream);
7586 else{
7587 err = 1;
7588 break;
7591 if(processed_name)
7592 fs_give((void **) &processed_name);
7595 if(!err)
7596 for(n = mn_first_cur(tmpmap);
7597 n > 0L;
7598 n = mn_next_cur(tmpmap)){
7600 if(msgno_exceptions(stream, mn_m2raw(tmpmap, n),
7601 "0", &exbits, FALSE))
7602 exbits |= (nt ? MSG_EX_FILEONCE : MSG_EX_FILED);
7603 else
7604 exbits = (nt ? MSG_EX_FILEONCE : MSG_EX_FILED);
7606 exbits &= ~MSG_EX_STATECHG;
7608 msgno_exceptions(stream, mn_m2raw(tmpmap, n),
7609 "0", &exbits, TRUE);
7613 mn_give(&tmpmap);
7616 mail_free_searchset(&srchset);
7620 * If this is the last rule,
7621 * we make sure we delete messages that we delayed deleting
7622 * in the save. We delayed so that the deletion wouldn't have
7623 * an effect on later rules. We convert any temporary
7624 * FILED (FILEONCE) and FILTERED (FILTONCE) flags
7625 * (which were set by an earlier non-terminating rule)
7626 * to permanent. We also exclude some messages from the view.
7628 if(pending_actions && !nextpat){
7630 pending_actions = 0;
7631 tmpmap = NULL;
7632 mn_init(&tmpmap, stream->nmsgs);
7634 for(i = 1L, n = 0L; i <= mn_get_total(msgmap); i++){
7636 raw = mn_m2raw(msgmap, i);
7637 if(msgno_exceptions(stream, raw, "0", &exbits, FALSE)){
7638 if(exbits & MSG_EX_FILEONCE){
7639 if(!n++){
7640 mn_set_cur(tmpmap, raw);
7642 else{
7643 mn_add_cur(tmpmap, raw);
7649 if(n)
7650 set_some_flags(stream, tmpmap, F_DEL, NULL, NULL, 0, NULL);
7652 mn_give(&tmpmap);
7654 for(i = 1L; i <= mn_get_total(msgmap); i++){
7655 raw = mn_m2raw(msgmap, i);
7656 if(msgno_exceptions(stream, raw, "0", &exbits, FALSE)){
7657 if(exbits & MSG_EX_PEND_EXLD){
7658 if(!cleared_index_cache
7659 && stream == ps_global->mail_stream){
7660 cleared_index_cache = 1;
7661 clear_index_cache(stream, 0);
7664 msgno_exclude(stream, msgmap, i, 1);
7665 if(msgno_exceptions(stream, raw, "0",
7666 &exbits, FALSE)
7667 && (exbits & MSG_EX_RECENT)){
7668 long l, ll;
7671 * If this message is new, decrement
7672 * new_mail_count. See the above
7673 * call to msgno_exclude.
7675 l = sp_new_mail_count(stream);
7676 ll = sp_recent_since_visited(stream);
7677 dprint((5, "New message being filtered. Decrement new_mail_count: %ld -> %ld\n", l, l-1L));
7678 if(l > 0L)
7679 sp_set_new_mail_count(stream, l - 1L);
7680 if(ll > 0L)
7681 sp_set_recent_since_visited(stream, ll - 1L);
7684 i--; /* to compensate for loop's i++ */
7687 /* get rid of temporary flags */
7688 if(exbits & (MSG_EX_FILTONCE | MSG_EX_FILEONCE |
7689 MSG_EX_PEND_EXLD)){
7690 if(exbits & MSG_EX_FILTONCE){
7691 /* convert to permament */
7692 exbits ^= MSG_EX_FILTONCE;
7693 exbits |= MSG_EX_FILTERED;
7696 /* convert to permament */
7697 if(exbits & MSG_EX_FILEONCE){
7698 exbits ^= MSG_EX_FILEONCE;
7699 exbits |= MSG_EX_FILED;
7702 if(exbits & MSG_EX_PEND_EXLD)
7703 exbits ^= MSG_EX_PEND_EXLD;
7705 exbits &= ~MSG_EX_STATECHG;
7707 msgno_exceptions(stream, raw, "0", &exbits,TRUE);
7714 /* New mail arrival means start over */
7715 if(mail_uid(stream, stream->nmsgs) == uid)
7716 break;
7717 /* else, go again */
7719 recent = 1; /* only check recent ones now */
7722 if(!recent){
7723 /* clear status change flags */
7724 for(i = 1; i <= stream->nmsgs; i++){
7725 if(msgno_exceptions(stream, i, "0", &exbits, FALSE)){
7726 if(exbits & MSG_EX_STATECHG){
7727 exbits &= ~MSG_EX_STATECHG;
7728 msgno_exceptions(stream, i, "0", &exbits, TRUE);
7734 /* clear any private "recent" flags and add TESTED flag */
7735 for(i = 1; i <= stream->nmsgs; i++){
7736 if(msgno_exceptions(stream, i, "0", &exbits, FALSE)){
7737 if(exbits & MSG_EX_RECENT
7738 || !(exbits & MSG_EX_TESTED)
7739 || (!recent && exbits & MSG_EX_STATECHG)){
7740 exbits &= ~MSG_EX_RECENT;
7741 exbits |= MSG_EX_TESTED;
7742 if(!recent)
7743 exbits &= ~MSG_EX_STATECHG;
7745 msgno_exceptions(stream, i, "0", &exbits, TRUE);
7748 else{
7749 exbits = MSG_EX_TESTED;
7750 msgno_exceptions(stream, i, "0", &exbits, TRUE);
7753 /* clear any stmp flags just in case */
7754 if((mc = mail_elt(stream, i)) != NULL)
7755 mc->spare6 = 0;
7758 msgmap->flagged_stmp = 0L;
7760 if(any_msgs && F_OFF(F_QUELL_FILTER_MSGS, ps_global)
7761 && F_OFF(F_QUELL_FILTER_DONE_MSG, ps_global)){
7762 q_status_message(SM_ORDER, 0, 1, _("filtering done"));
7763 display_message('x');
7769 * Re-check the filters for matches because a change of message state may
7770 * have changed the results.
7772 void
7773 reprocess_filter_patterns(MAILSTREAM *stream, MSGNO_S *msgmap, int flags)
7775 if(stream){
7776 long i;
7777 int exbits;
7779 if(msgno_include(stream, msgmap, flags)
7780 && stream == ps_global->mail_stream
7781 && !(flags & MI_CLOSING)){
7782 clear_index_cache(stream, 0);
7783 refresh_sort(stream, msgmap, SRT_NON);
7784 ps_global->mangled_header = 1;
7788 * Passing 1 in the last argument causes it to only look at the
7789 * messages we included above, which should be only the ones we
7790 * need to look at.
7792 process_filter_patterns(stream, msgmap,
7793 (flags & MI_STATECHGONLY) ? 1L : 0);
7795 /* clear status change flags */
7796 for(i = 1; i <= stream->nmsgs; i++){
7797 if(msgno_exceptions(stream, i, "0", &exbits, FALSE)){
7798 if(exbits & MSG_EX_STATECHG){
7799 exbits &= ~MSG_EX_STATECHG;
7800 msgno_exceptions(stream, i, "0", &exbits, TRUE);
7809 * When killing or filtering we don't want to match by mistake. So if
7810 * a pattern has nothing set except the Current Folder Type (which is always
7811 * set to something) we'll consider it to be trivial and not a match.
7812 * match_pattern uses this to determine if there is a match, where it is
7813 * just triggered on the Current Folder Type.
7816 trivial_patgrp(PATGRP_S *patgrp)
7818 int ret = 1;
7820 if(patgrp){
7821 if(patgrp->subj || patgrp->cc || patgrp->from || patgrp->to ||
7822 patgrp->sender || patgrp->news || patgrp->recip || patgrp->partic ||
7823 patgrp->alltext || patgrp->bodytext)
7824 ret = 0;
7826 if(ret && patgrp->do_age)
7827 ret = 0;
7829 if(ret && patgrp->do_size)
7830 ret = 0;
7832 if(ret && patgrp->do_score)
7833 ret = 0;
7835 if(ret && patgrp->category_cmd && patgrp->category_cmd[0])
7836 ret = 0;
7838 if(ret && patgrp_depends_on_state(patgrp))
7839 ret = 0;
7841 if(ret && patgrp->stat_8bitsubj != PAT_STAT_EITHER)
7842 ret = 0;
7844 if(ret && patgrp->charsets)
7845 ret = 0;
7847 if(ret && patgrp->stat_bom != PAT_STAT_EITHER)
7848 ret = 0;
7850 if(ret && patgrp->stat_boy != PAT_STAT_EITHER)
7851 ret = 0;
7853 if(ret && patgrp->inabook != IAB_EITHER)
7854 ret = 0;
7856 if(ret && patgrp->arbhdr){
7857 ARBHDR_S *a;
7859 for(a = patgrp->arbhdr; a && ret; a = a->next)
7860 if(a->field && a->field[0] && a->p)
7861 ret = 0;
7865 return(ret);
7870 some_filter_depends_on_active_state(void)
7872 long rflags = ROLE_DO_FILTER;
7873 PAT_S *pat;
7874 PAT_STATE pstate;
7875 int ret = 0;
7877 if(nonempty_patterns(rflags, &pstate)){
7879 for(pat = first_pattern(&pstate);
7880 pat && !ret;
7881 pat = next_pattern(&pstate))
7882 if(patgrp_depends_on_active_state(pat->patgrp))
7883 ret++;
7886 return(ret);
7890 /*----------------------------------------------------------------------
7891 Move all messages with sequence bit lit to dstfldr
7893 Args: stream -- stream to use
7894 msgmap -- map of messages to be moved
7895 dstfldr -- folder to receive moved messages
7896 flags_for_save
7898 Returns: nonzero on success or on readonly stream
7899 ----*/
7901 move_filtered_msgs(MAILSTREAM *stream, MSGNO_S *msgmap, char *dstfldr,
7902 int flags_for_save, char *nick)
7904 long n;
7905 int we_cancel = 0, width;
7906 CONTEXT_S *save_context = NULL;
7907 char buf[MAX_SCREEN_COLS+1], sbuf[MAX_SCREEN_COLS+1];
7908 char *save_ref = NULL;
7909 #define FILTMSG_MAX 30
7911 if(!stream)
7912 return 0;
7914 if(READONLY_FOLDER(stream)){
7915 dprint((1,
7916 "Can't delete messages in readonly folder \"%s\"\n",
7917 STREAMNAME(stream)));
7918 q_status_message1(SM_ORDER, 1, 3,
7919 _("Can't delete messages in readonly folder \"%s\""),
7920 STREAMNAME(stream));
7921 return 1;
7924 buf[0] = '\0';
7926 width = MAX(10, ps_global->ttyo ? ps_global->ttyo->screen_cols : 80);
7927 snprintf(buf, sizeof(buf), "%.30s%.2sMoving %.10s filtered message%.2s to \"\"",
7928 nick ? nick : "", nick ? ": " : "",
7929 comatose(mn_total_cur(msgmap)), plural(mn_total_cur(msgmap)));
7930 /* 2 is for brackets, 5 is for " DONE" in busy alarm */
7931 width -= (strlen(buf) + 2 + 5);
7932 snprintf(buf, sizeof(buf), "%.30s%.2sMoving %.10s filtered message%.2s to \"%s\"",
7933 nick ? nick : "", nick ? ": " : "",
7934 comatose(mn_total_cur(msgmap)), plural(mn_total_cur(msgmap)),
7935 short_str(dstfldr, sbuf, sizeof(sbuf), width, FrontDots));
7937 dprint((5, "%s\n", buf));
7939 if(F_OFF(F_QUELL_FILTER_MSGS, ps_global))
7940 we_cancel = busy_cue(buf, NULL, 0);
7942 if(!is_absolute_path(dstfldr)
7943 && !(save_context = default_save_context(ps_global->context_list)))
7944 save_context = ps_global->context_list;
7947 * Because this save is happening independent of where the user is
7948 * in the folder hierarchy and has nothing to do with that, we want
7949 * to ignore the reference field built into the context. Zero it out
7950 * temporarily here so it won't affect the results of context_apply
7951 * in save.
7953 * This might be a problem elsewhere, as well. The same thing as this
7954 * is also done in match_pattern_folder_specific, which is also only
7955 * called from within process_filter_patterns. But there could be
7956 * others. We could have a separate function, something like
7957 * copy_default_save_context(), that automatically zeroes out the
7958 * reference field in the copy. However, some of the uses of
7959 * default_save_context() require that a pointer into the actual
7960 * context list is returned, so this would have to be done carefully.
7961 * Besides, we don't know of any other problems so we'll just change
7962 * these known cases for now.
7964 if(save_context && save_context->dir){
7965 save_ref = save_context->dir->ref;
7966 save_context->dir->ref = NULL;
7969 n = save(ps_global, stream, save_context, dstfldr, msgmap, flags_for_save);
7971 if(save_ref)
7972 save_context->dir->ref = save_ref;
7974 if(n != mn_total_cur(msgmap)){
7975 int exbits;
7976 long x;
7978 buf[0] = '\0';
7980 /* Clear "filtered" flags for failed messages */
7981 for(x = mn_first_cur(msgmap); x > 0L; x = mn_next_cur(msgmap))
7982 if(n-- <= 0 && msgno_exceptions(stream, mn_m2raw(msgmap, x),
7983 "0", &exbits, FALSE)){
7984 exbits &= ~(MSG_EX_FILTONCE | MSG_EX_FILEONCE |
7985 MSG_EX_FILTERED | MSG_EX_FILED);
7986 msgno_exceptions(stream, mn_m2raw(msgmap, x),
7987 "0", &exbits, TRUE);
7990 /* then re-incorporate them into folder they belong */
7991 (void) msgno_include(stream, sp_msgmap(stream), MI_NONE);
7992 clear_index_cache(stream, 0);
7993 refresh_sort(stream, sp_msgmap(stream), SRT_NON);
7994 ps_global->mangled_header = 1;
7996 else{
7997 snprintf(buf, sizeof(buf), _("Filtered all %s message to \"%s\""),
7998 comatose(n), dstfldr);
7999 dprint((5, "%s\n", buf));
8002 if(we_cancel)
8003 cancel_busy_cue(buf[0] ? 0 : -1);
8005 return(buf[0] != '\0');
8009 /*----------------------------------------------------------------------
8010 Move all messages with sequence bit lit to dstfldr
8012 Args: stream -- stream to use
8013 msgmap -- which messages to set
8014 flagbits -- which flags to set or clear
8015 kw_on -- keywords to set
8016 kw_off -- keywords to clear
8017 verbose -- 1 => busy alarm after 1 second
8018 2 => forced busy alarm
8019 ----*/
8020 void
8021 set_some_flags(MAILSTREAM *stream, MSGNO_S *msgmap, long int flagbits,
8022 char **kw_on, char **kw_off, int verbose, char *nick)
8024 long count = 0L, flipped_flags;
8025 int we_cancel = 0;
8026 char buf[150], *seq;
8028 if(!stream)
8029 return;
8031 if(READONLY_FOLDER(stream)){
8032 dprint((1, "Can't set flags in readonly folder \"%s\"\n",
8033 STREAMNAME(stream)));
8034 q_status_message1(SM_ORDER, 1, 3,
8035 _("Can't set flags in readonly folder \"%s\""),
8036 STREAMNAME(stream));
8037 return;
8040 /* use this to determine if anything needs to be done */
8041 flipped_flags = ((flagbits & F_ANS) ? F_UNANS : 0) |
8042 ((flagbits & F_UNANS) ? F_ANS : 0) |
8043 ((flagbits & F_FLAG) ? F_UNFLAG : 0) |
8044 ((flagbits & F_UNFLAG) ? F_FLAG : 0) |
8045 ((flagbits & F_DEL) ? F_UNDEL : 0) |
8046 ((flagbits & F_UNDEL) ? F_DEL : 0) |
8047 ((flagbits & F_SEEN) ? F_UNSEEN : 0) |
8048 ((flagbits & F_UNSEEN) ? F_SEEN : 0) |
8049 ((flagbits & F_KEYWORD) ? F_UNKEYWORD : 0) |
8050 ((flagbits & F_UNKEYWORD) ? F_KEYWORD : 0);
8051 if((seq = currentf_sequence(stream, msgmap, flipped_flags, &count, 0,
8052 kw_off, kw_on)) != NULL){
8053 char *sets = NULL, *clears = NULL;
8054 char *ps, *pc, **t;
8055 size_t clen, slen;
8057 /* allocate enough space for mail_flags arguments */
8058 for(slen=100, t = kw_on; t && *t; t++)
8059 slen += (strlen(*t) + 1);
8061 sets = (char *) fs_get(slen * sizeof(*sets));
8063 for(clen=100, t = kw_off; t && *t; t++)
8064 clen += (strlen(*t) + 1);
8066 clears = (char *) fs_get(clen * sizeof(*clears));
8068 sets[0] = clears[0] = '\0';
8069 ps = sets;
8070 pc = clears;
8072 snprintf(buf, sizeof(buf), "%.30s%.2sSetting flags in %.10s message%.10s",
8073 nick ? nick : "", nick ? ": " : "",
8074 comatose(count), plural(count));
8076 if(F_OFF(F_QUELL_FILTER_MSGS, ps_global))
8077 we_cancel = busy_cue(buf, NULL, verbose ? 0 : 1);
8080 * What's going on here? If we want to set more than one flag
8081 * we can do it with a single roundtrip by combining the arguments
8082 * into a single call and separating them with spaces.
8084 if(flagbits & F_ANS)
8085 sstrncpy(&ps, "\\ANSWERED", slen-(ps-sets));
8086 if(flagbits & F_FLAG){
8087 if(ps > sets)
8088 sstrncpy(&ps, " ", slen-(ps-sets));
8090 sstrncpy(&ps, "\\FLAGGED", slen-(ps-sets));
8092 if(flagbits & F_DEL){
8093 if(ps > sets)
8094 sstrncpy(&ps, " ", slen-(ps-sets));
8096 sstrncpy(&ps, "\\DELETED", slen-(ps-sets));
8098 if(flagbits & F_SEEN){
8099 if(ps > sets)
8100 sstrncpy(&ps, " ", slen-(ps-sets));
8102 sstrncpy(&ps, "\\SEEN", slen-(ps-sets));
8104 if(flagbits & F_KEYWORD){
8105 for(t = kw_on; t && *t; t++){
8106 int i;
8109 * We may be able to tell that this will fail before
8110 * we actually try it.
8112 if(stream->kwd_create ||
8113 (((i=user_flag_index(stream, *t)) >= 0) && i < NUSERFLAGS)){
8114 if(ps > sets)
8115 sstrncpy(&ps, " ", slen-(ps-sets));
8117 sstrncpy(&ps, *t, slen-(ps-sets));
8119 else{
8120 int some_defined = 0;
8121 static int msg_delivered = 0;
8123 some_defined = some_user_flags_defined(stream);
8125 if(msg_delivered++ < 2){
8126 char b[200], c[200], *p;
8127 int w;
8129 if(some_defined){
8130 snprintf(b, sizeof(b), "Can't set \"%.30s\". No more keywords in ", keyword_to_nick(*t));
8131 w = MIN((ps_global->ttyo ? ps_global->ttyo->screen_cols : 80) - strlen(b) - 1 - 2, sizeof(c)-1);
8132 p = short_str(STREAMNAME(stream), c, sizeof(c), w, FrontDots);
8133 q_status_message2(SM_ORDER, 3, 3, "%s%s!", b, p);
8135 else{
8136 snprintf(b, sizeof(b), "Can't set \"%.30s\". Can't add keywords in ", keyword_to_nick(*t));
8137 w = MIN((ps_global->ttyo ? ps_global->ttyo->screen_cols : 80) - strlen(b) - 1 - 2, sizeof(c)-1);
8138 p = short_str(STREAMNAME(stream), c, sizeof(c), w, FrontDots);
8139 q_status_message2(SM_ORDER, 3, 3, "%s%s!", b, p);
8143 if(some_defined){
8144 dprint((1, "Can't set keyword \"%s\". No more keywords allowed in %s\n", *t, stream->mailbox ? stream->mailbox : "target folder"));
8146 else{
8147 dprint((1, "Can't set keyword \"%s\". Can't add keywords in %s\n", *t, stream->mailbox ? stream->mailbox : "target folder"));
8153 /* need a separate call for the clears */
8154 if(flagbits & F_UNANS)
8155 sstrncpy(&pc, "\\ANSWERED", clen-(pc-clears));
8156 if(flagbits & F_UNFLAG){
8157 if(pc > clears)
8158 sstrncpy(&pc, " ", clen-(pc-clears));
8160 sstrncpy(&pc, "\\FLAGGED", clen-(pc-clears));
8162 if(flagbits & F_UNDEL){
8163 if(pc > clears)
8164 sstrncpy(&pc, " ", clen-(pc-clears));
8166 sstrncpy(&pc, "\\DELETED", clen-(pc-clears));
8168 if(flagbits & F_UNSEEN){
8169 if(pc > clears)
8170 sstrncpy(&pc, " ", clen-(pc-clears));
8172 sstrncpy(&pc, "\\SEEN", clen-(pc-clears));
8174 if(flagbits & F_UNKEYWORD){
8175 for(t = kw_off; t && *t; t++){
8176 if(pc > clears)
8177 sstrncpy(&pc, " ", clen-(pc-clears));
8179 sstrncpy(&pc, *t, clen-(pc-clears));
8184 if(sets[0])
8185 mail_flag(stream, seq, sets, ST_SET);
8187 if(clears[0])
8188 mail_flag(stream, seq, clears, 0L);
8190 fs_give((void **) &sets);
8191 fs_give((void **) &clears);
8192 fs_give((void **) &seq);
8194 if(we_cancel)
8195 cancel_busy_cue(buf[0] ? 0 : -1);
8201 * Delete messages which are marked FILTERED and excluded.
8202 * Messages which are FILTERED but not excluded are those that have had
8203 * their state set by a filter pattern, but are to remain in the same
8204 * folder.
8206 void
8207 delete_filtered_msgs(MAILSTREAM *stream)
8209 int exbits;
8210 long i;
8211 char *seq;
8212 MESSAGECACHE *mc;
8214 for(i = 1L; i <= stream->nmsgs; i++)
8215 if(msgno_exceptions(stream, i, "0", &exbits, FALSE)
8216 && (exbits & MSG_EX_FILTERED)
8217 && get_lflag(stream, NULL, i, MN_EXLD)){
8218 if((mc = mail_elt(stream, i)) != NULL)
8219 mc->sequence = 1;
8221 else if((mc = mail_elt(stream, i)) != NULL)
8222 mc->sequence = 0;
8224 if((seq = build_sequence(stream, NULL, NULL)) != NULL){
8225 mail_flag(stream, seq, "\\DELETED", ST_SET | ST_SILENT);
8226 fs_give((void **) &seq);