* SMIME: Change the default signature digest from sha1 to sha-256,
[alpine.git] / pith / detoken.c
blob6f0584ab9fb2761f0792f1439dbe54d1da697064
1 #if !defined(lint) && !defined(DOS)
2 static char rcsid[] = "$Id: detoken.c 769 2007-10-24 00:15:40Z hubert@u.washington.edu $";
3 #endif
5 /*
6 * ========================================================================
7 * Copyright 2006 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/detoken.h"
20 #include "../pith/state.h"
21 #include "../pith/conf.h"
22 #include "../pith/status.h"
23 #include "../pith/pattern.h"
24 #include "../pith/reply.h"
25 #include "../pith/mailindx.h"
26 #include "../pith/options.h"
30 * Hook to read signature from local file
32 char *(*pith_opt_get_signature_file)(char *, int, int, int);
36 * Internal prototypes
38 char *detoken_guts(char *, int, ENVELOPE *, ACTION_S *, REDRAFT_POS_S **, int, int *);
39 char *handle_if_token(char *, char *, int, ENVELOPE *, ACTION_S *, char **);
40 char *get_token_arg(char *, char **);
44 * Detokenize signature or template files.
46 * If is_sig, we always use literal sigs before sigfiles if they are
47 * defined. So, check for role->litsig and use it. If it doesn't exist, use
48 * the global literal sig if defined. Else the role->sig file or the
49 * global signature file.
51 * If !is_sig, use role->template.
53 * So we start with a literal signature or a signature or template file.
54 * If that's a file, we read it first. The file could be remote.
55 * Then we detokenize the literal signature or file contents and return
56 * an allocated string which the caller frees.
58 * Args role -- See above about what happens depending on is_sig.
59 * relative to the pinerc dir.
60 * env -- The envelope to use for detokenizing. May be NULL.
61 * prenewlines -- How many blank lines should be included at start.
62 * postnewlines -- How many blank lines should be included after.
63 * is_sig -- This is a signature (not a template file).
64 * redraft_pos -- This is a return value. If it is non-NULL coming in,
65 * then the cursor position is returned here.
66 * impl -- This is a combination argument which is both an input
67 * argument and a return value. If it is non-NULL and = 0,
68 * that means that we want the cursor position returned here,
69 * even if that position is set implicitly to the end of
70 * the output string. If it is = 1 coming in, that means
71 * we only want the cursor position to be set if it is set
72 * explicitly. If it is 2, or if redraft_pos is NULL,
73 * we don't set it at all.
74 * If the cursor position gets set explicitly by a
75 * _CURSORPOS_ token in the file then this is set to 2
76 * on return. If the cursor position is set implicitly to
77 * the end of the included file, then this is set to 1
78 * on return.
80 * Returns -- An allocated string is returned.
82 char *
83 detoken(ACTION_S *role, ENVELOPE *env, int prenewlines, int postnewlines,
84 int is_sig, REDRAFT_POS_S **redraft_pos, int *impl)
86 char *ret = NULL,
87 *src = NULL,
88 *literal_sig = NULL,
89 *sigfile = NULL;
91 if(is_sig){
93 * If role->litsig is set, we use it;
94 * Else, if VAR_LITERAL_SIG is set, we use that;
95 * Else, if role->sig is set, we use that;
96 * Else, if VAR_SIGNATURE_FILE is set, we use that.
97 * This can be a little surprising if you set the VAR_LITERAL_SIG
98 * and don't set a role->litsig but do set a role->sig. The
99 * VAR_LITERAL_SIG will be used, not role->sig. The reason for this
100 * is mostly that it is much easier to display the right stuff
101 * in the various config screens if we do it that way. Besides,
102 * people will typically use only literal sigs or only sig files,
103 * there is no reason to mix them, so we don't provide support to
104 * do so.
106 if(role && role->litsig)
107 literal_sig = role->litsig;
108 else if(ps_global->VAR_LITERAL_SIG)
109 literal_sig = ps_global->VAR_LITERAL_SIG;
110 else if(role && role->sig)
111 sigfile = role->sig;
112 else
113 sigfile = ps_global->VAR_SIGNATURE_FILE;
115 else if(role && role->template)
116 sigfile = role->template;
118 if(literal_sig)
119 src = get_signature_lit(literal_sig, prenewlines, postnewlines, is_sig,1);
120 else if(sigfile && pith_opt_get_signature_file)
121 src = (*pith_opt_get_signature_file)(sigfile, prenewlines, postnewlines, is_sig);
123 if(src){
124 if(*src)
125 ret = detoken_src(src, FOR_TEMPLATE, env, role, redraft_pos, impl);
127 fs_give((void **)&src);
130 return(ret);
135 * Filter the source string from the template file and return an allocated
136 * copy of the result with text replacements for the tokens.
137 * Fill in offset in redraft_pos.
139 * This is really inefficient but who cares? It's just cpu time.
141 char *
142 detoken_src(char *src, int for_what, ENVELOPE *env, ACTION_S *role,
143 REDRAFT_POS_S **redraft_pos, int *impl)
145 int loopcnt = 25; /* just in case, avoid infinite loop */
146 char *ret, *str1, *str2;
147 int done = 0;
149 if(!src)
150 return(src);
153 * We keep running it through until it stops changing so user can
154 * nest calls to token stuff.
156 str1 = src;
157 do {
158 /* short-circuit if no chance it will change */
159 if(strindex(str1, '_'))
160 str2 = detoken_guts(str1, for_what, env, role, NULL, 0, NULL);
161 else
162 str2 = str1;
164 if(str1 && str2 && (str1 == str2 || !strcmp(str1, str2))){
165 done++; /* It stopped changing */
166 if(str1 && str1 != src && str1 != str2)
167 fs_give((void **)&str1);
169 else{ /* Still changing */
170 if(str1 && str1 != src && str1 != str2)
171 fs_give((void **)&str1);
173 str1 = str2;
176 } while(str2 && !done && loopcnt-- > 0);
179 * Have to run it through once more to get the redraft_pos and
180 * to remove any backslash escape for a token.
182 if((str2 && strindex(str2, '_')) ||
183 (impl && *impl == 0 && redraft_pos && !*redraft_pos)){
184 ret = detoken_guts(str2, for_what, env, role, redraft_pos, 1, impl);
185 if(str2 != src)
186 fs_give((void **)&str2);
188 else if(str2){
189 if(str2 == src)
190 ret = cpystr(str2);
191 else
192 ret = str2;
195 return(ret);
200 * The guts of the detokenizing routines. Filter the src string looking for
201 * tokens and replace them with the appropriate text. In the case of the
202 * cursor_pos token we set redraft_pos instead.
204 * Args src -- The source string
205 * for_what --
206 * env -- Envelope to look in for token replacements.
207 * redraft_pos -- Return the redraft offset here, if non-zero.
208 * last_pass -- This is a flag to tell detoken_guts whether or not to do
209 * the replacement for _CURSORPOS_. Leave it as is until
210 * the last pass. We need this because we want to defer
211 * cursor placement until the very last call to detoken,
212 * otherwise we'd have to keep track of the cursor
213 * position as subsequent text replacements (nested)
214 * take place.
215 * This same flag is also used to decide when to eliminate
216 * backslash escapes from in front of tokens. The only
217 * use of backslash escapes is to escape an entire token.
218 * That is, \_DATE_ is a literal _DATE_, but any other
219 * backslash is a literal backslash. That way, nobody
220 * but wackos will have to worry about backslashes.
221 * impl -- This is a combination argument which is both an input
222 * argument and a return value. If it is non-NULL and 0
223 * coming in, that means that we should set redraft_pos,
224 * even if that position is set implicitly to the end of
225 * the output string. If it is 1 coming in, that means
226 * we only want the cursor position to be set if it is set
227 * explicitly. If it is 2 coming in (or if
228 * redraft_pos is NULL) then we don't set it at all.
229 * If the cursor position gets set explicitly by a
230 * _CURSORPOS_ token in the file then this is set to 2
231 * on return. If the cursor position is set implicitly to
232 * the end of the included file, then this is set to 1
233 * on return.
235 * Returns pointer to alloced result
237 char *
238 detoken_guts(char *src, int for_what, ENVELOPE *env, ACTION_S *role,
239 REDRAFT_POS_S **redraft_pos, int last_pass, int *impl)
241 #define MAXSUB 500
242 char *p, *q = NULL, *dst = NULL;
243 char subbuf[MAXSUB+1], *repl;
244 INDEX_PARSE_T *pt;
245 long l, cnt = 0L;
246 int sizing_pass = 1, suppress_tokens = 0;
248 if(!src)
249 return(NULL);
251 top:
254 * The tokens we look for begin with _. The only escaping mechanism
255 * is a backslash in front of a token. This will give you the literal
256 * token. So \_DATE_ is a literal _DATE_.
257 * Tokens like _word_ are replaced with the appropriate text if
258 * word is recognized. If _word_ is followed immediately by a left paren
259 * it is an if-else thingie. _word_(match_this,if_text,else_text) means to
260 * replace that with either the if_text or else_text depending on whether
261 * what _word_ (without the paren) would produce matches match_this or not.
263 p = src;
264 while(*p){
265 switch(*p){
266 case '_': /* possible start of token */
267 if(!suppress_tokens &&
268 (pt = itoktype(p+1, for_what | DELIM_USCORE)) != NULL){
269 char *free_this = NULL;
271 p += (strlen(pt->name) + 2); /* skip over token */
273 repl = subbuf;
274 subbuf[0] = '\0';
276 if(pt->ctype == iCursorPos){
277 if(!last_pass){ /* put it back */
278 subbuf[0] = '_';
279 strncpy(subbuf+1, pt->name, sizeof(subbuf)-2);
280 subbuf[sizeof(subbuf)-1] = '\0';
281 strncat(subbuf, "_", sizeof(subbuf)-strlen(subbuf)-1);
282 subbuf[sizeof(subbuf)-1] = '\0';
285 if(!sizing_pass){
286 if(q-dst < cnt+1)
287 *q = '\0';
289 l = strlen(dst);
290 if(redraft_pos && impl && *impl != 2){
291 if(!*redraft_pos){
292 *redraft_pos =
293 (REDRAFT_POS_S *)fs_get(sizeof(**redraft_pos));
294 memset((void *)*redraft_pos, 0,
295 sizeof(**redraft_pos));
296 (*redraft_pos)->hdrname = cpystr(":");
299 (*redraft_pos)->offset = l;
300 *impl = 2; /* set explicitly */
304 else if(pt->what_for & FOR_REPLY_INTRO)
305 repl = get_reply_data(env, role, pt->ctype,
306 subbuf, sizeof(subbuf)-1);
308 if(*p == LPAREN){ /* if-else construct */
309 char *skip_ahead;
311 repl = free_this = handle_if_token(repl, p, for_what,
312 env, role,
313 &skip_ahead);
314 p = skip_ahead;
317 if(repl && repl[0]){
318 if(sizing_pass)
319 cnt += (long)strlen(repl);
320 else{
321 strncpy(q, repl, cnt-(q-dst));
322 dst[cnt] = '\0';
323 q += strlen(repl);
327 if(free_this)
328 fs_give((void **)&free_this);
330 else{ /* unrecognized token, treat it just like text */
331 suppress_tokens = 0;
332 if(sizing_pass)
333 cnt++;
334 else if(q-dst < cnt+1)
335 *q++ = *p;
337 p++;
340 break;
342 case BSLASH:
344 * If a real token follows the backslash, then the backslash
345 * is here to escape the token. Otherwise, it's just a
346 * regular character.
348 if(*(p+1) == '_' &&
349 ((pt = itoktype(p+2, for_what | DELIM_USCORE)) != NULL)){
351 * Backslash is escape for literal token.
352 * If we're on the last pass we want to eliminate the
353 * backslash, otherwise we keep it.
354 * In either case, suppress_tokens will cause the token
355 * lookup to be skipped above so that the token will
356 * be treated as literal text.
358 suppress_tokens++;
359 if(last_pass){
360 p++;
361 break;
363 /* else, fall through and keep backslash */
365 /* this is a literal backslash, fall through */
367 default:
368 if(sizing_pass)
369 cnt++;
370 else if(q-dst < cnt+1)
371 *q++ = *p; /* copy the character */
373 p++;
374 break;
378 if(!sizing_pass && q-dst < cnt+1)
379 *q = '\0';
381 if(sizing_pass){
382 sizing_pass = 0;
384 * Now we're done figuring out how big the answer will be. We
385 * allocate space for it and go back through filling it in.
387 cnt = MAX(cnt, 0L);
388 q = dst = (char *)fs_get((cnt + 1) * sizeof(char));
389 goto top;
393 * Set redraft_pos to character following the template, unless
394 * it has already been set.
396 if(dst && impl && *impl == 0 && redraft_pos && !*redraft_pos){
397 *redraft_pos = (REDRAFT_POS_S *)fs_get(sizeof(**redraft_pos));
398 memset((void *)*redraft_pos, 0, sizeof(**redraft_pos));
399 (*redraft_pos)->offset = strlen(dst);
400 (*redraft_pos)->hdrname = cpystr(":");
401 *impl = 1;
404 if(dst && cnt >= 0)
405 dst[cnt] = '\0';
407 return(dst);
412 * Do the if-else part of the detokenization for one case of if-else.
413 * The input src should like (match_this, if_matched, else)...
415 * Args expands_to -- This is what the token to the left of the paren
416 * expanded to, and this is the thing we're going to
417 * compare with the match_this part.
418 * src -- The source string beginning with the left paren.
419 * for_what --
420 * env --
421 * skip_ahead -- Tells caller how long the (...) part was so caller can
422 * skip over that part of the source.
424 * Returns -- an allocated string which is the answer, or NULL if nothing.
426 char *
427 handle_if_token(char *expands_to, char *src, int for_what, ENVELOPE *env,
428 ACTION_S *role, char **skip_ahead)
430 char *ret = NULL;
431 char *skip_to;
432 char *match_this, *if_matched, *else_part;
434 if(skip_ahead)
435 *skip_ahead = src;
437 if(!src || *src != LPAREN){
438 dprint((1,"botch calling handle_if_token, missing paren\n"));
439 return(ret);
442 if(!*++src){
443 q_status_message(SM_ORDER, 3, 3,
444 "Unexpected end of token string in Reply-LeadIn, Sig, or template");
445 return(ret);
448 match_this = get_token_arg(src, &skip_to);
451 * If the match_this argument is a token, detokenize it first.
453 if(match_this && *match_this == '_'){
454 char *exp_match_this;
456 exp_match_this = detoken_src(match_this, for_what, env,
457 role, NULL, NULL);
458 fs_give((void **)&match_this);
459 match_this = exp_match_this;
462 if(!match_this)
463 match_this = cpystr("");
465 if(!expands_to)
466 expands_to = "";
468 src = skip_to;
469 while(src && *src && (isspace((unsigned char)*src) || *src == ','))
470 src++;
472 if_matched = get_token_arg(src, &skip_to);
473 src = skip_to;
474 while(src && *src && (isspace((unsigned char)*src) || *src == ','))
475 src++;
477 else_part = get_token_arg(src, &skip_to);
478 src = skip_to;
479 while(src && *src && *src != RPAREN)
480 src++;
482 if(src && *src == RPAREN)
483 src++;
485 if(skip_ahead)
486 *skip_ahead = src;
488 if(!strcmp(match_this, expands_to)){
489 ret = if_matched;
490 if(else_part)
491 fs_give((void **)&else_part);
493 else{
494 ret = else_part;
495 if(if_matched)
496 fs_give((void **)&if_matched);
499 fs_give((void **)&match_this);
501 return(ret);
505 char *
506 get_token_arg(char *src, char **skip_to)
508 int quotes = 0, done = 0;
509 char *ret = NULL, *p;
511 while(*src && isspace((unsigned char)*src)) /* skip space before string */
512 src++;
514 if(*src == RPAREN){
515 if(skip_to)
516 *skip_to = src;
518 return(ret);
521 p = ret = (char *)fs_get((strlen(src) + 1) * sizeof(char));
522 while(!done){
523 switch(*src){
524 case QUOTE:
525 if(++quotes == 2)
526 done++;
528 src++;
529 break;
531 case BSLASH: /* don't count \" as a quote, just copy */
532 if(*(src+1) == BSLASH || *(src+1) == QUOTE){
533 src++; /* skip backslash */
534 *p++ = *src++;
536 else
537 src++;
539 break;
541 case SPACE:
542 case TAB:
543 case RPAREN:
544 case COMMA:
545 if(quotes)
546 *p++ = *src++;
547 else
548 done++;
550 break;
552 case '\0':
553 done++;
554 break;
556 default:
557 *p++ = *src++;
558 break;
562 *p = '\0';
563 if(skip_to)
564 *skip_to = src;
566 return(ret);