hdrline.c 20.1 KB
Newer Older
Thomas Roessler's avatar
Thomas Roessler committed
1
/*
2
 * Copyright (C) 1996-2000,2002,2007 Michael R. Elkins <me@mutt.org>
Thomas Roessler's avatar
Thomas Roessler committed
3 4 5 6 7 8 9 10 11 12 13 14 15
 * 
 *     This program is free software; you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation; either version 2 of the License, or
 *     (at your option) any later version.
 * 
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with this program; if not, write to the Free Software
16
 *     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Thomas Roessler's avatar
Thomas Roessler committed
17 18
 */ 

19 20 21 22
#if HAVE_CONFIG_H
# include "config.h"
#endif

Thomas Roessler's avatar
Thomas Roessler committed
23 24
#include "mutt.h"
#include "mutt_curses.h"
25
#include "sort.h"
26
#include "charset.h"
27
#include "mutt_crypt.h"
28
#include "mutt_idna.h"
Thomas Roessler's avatar
Thomas Roessler committed
29 30 31 32 33 34

#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>

Fabian Groffen's avatar
Fabian Groffen committed
35 36 37 38
#ifdef HAVE_ALLOCA_H
#include <alloca.h>
#endif

39 40
int mutt_is_mail_list (ADDRESS *addr)
{
41 42 43
  if (!mutt_match_rx_list (addr->mailbox, UnMailLists))
    return mutt_match_rx_list (addr->mailbox, MailLists);
  return 0;
44 45 46 47
}

int mutt_is_subscribed_list (ADDRESS *addr)
{
48 49 50 51
  if (!mutt_match_rx_list (addr->mailbox, UnMailLists)
      && !mutt_match_rx_list (addr->mailbox, UnSubscribedLists))
    return mutt_match_rx_list (addr->mailbox, SubscribedLists);
  return 0;
52 53
}

Thomas Roessler's avatar
Thomas Roessler committed
54 55 56 57
/* Search for a mailing list in the list of addresses pointed to by adr.
 * If one is found, print pfx and the name of the list into buf, then
 * return 1.  Otherwise, simply return 0.
 */
Thomas Roessler's avatar
Thomas Roessler committed
58 59 60 61 62
static int
check_for_mailing_list (ADDRESS *adr, char *pfx, char *buf, int buflen)
{
  for (; adr; adr = adr->next)
  {
63
    if (mutt_is_subscribed_list (adr))
Thomas Roessler's avatar
Thomas Roessler committed
64 65 66 67 68 69 70 71 72
    {
      if (pfx && buf && buflen)
	snprintf (buf, buflen, "%s%s", pfx, mutt_get_name (adr));
      return 1;
    }
  }
  return 0;
}

Thomas Roessler's avatar
Thomas Roessler committed
73 74 75 76 77 78 79 80 81
/* Search for a mailing list in the list of addresses pointed to by adr.
 * If one is found, print the address of the list into buf, then return 1.
 * Otherwise, simply return 0.
 */
static int
check_for_mailing_list_addr (ADDRESS *adr, char *buf, int buflen)
{
  for (; adr; adr = adr->next)
  {
82
    if (mutt_is_subscribed_list (adr))
Thomas Roessler's avatar
Thomas Roessler committed
83 84 85 86 87 88 89 90 91 92
    {
      if (buf && buflen)
	snprintf (buf, buflen, "%s", adr->mailbox);
      return 1;
    }
  }
  return 0;
}


Thomas Roessler's avatar
Thomas Roessler committed
93 94 95 96
static int first_mailing_list (char *buf, size_t buflen, ADDRESS *a)
{
  for (; a; a = a->next)
  {
97
    if (mutt_is_subscribed_list (a))
Thomas Roessler's avatar
Thomas Roessler committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    {
      mutt_save_path (buf, buflen, a);
      return 1;
    }
  }
  return 0;
}

static void make_from (ENVELOPE *hdr, char *buf, size_t len, int do_lists)
{
  int me;

  me = mutt_addr_is_user (hdr->from);

  if (do_lists || me)
  {
    if (check_for_mailing_list (hdr->to, "To ", buf, len))
      return;
    if (check_for_mailing_list (hdr->cc, "Cc ", buf, len))
      return;
  }

  if (me && hdr->to)
    snprintf (buf, len, "To %s", mutt_get_name (hdr->to));
  else if (me && hdr->cc)
    snprintf (buf, len, "Cc %s", mutt_get_name (hdr->cc));
124 125
  else if (me && hdr->bcc)
    snprintf (buf, len, "Bcc %s", mutt_get_name (hdr->bcc));
Thomas Roessler's avatar
Thomas Roessler committed
126 127 128 129 130 131
  else if (hdr->from)
    strfcpy (buf, mutt_get_name (hdr->from), len);
  else
    *buf = 0;
}

Thomas Roessler's avatar
Thomas Roessler committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
static void make_from_addr (ENVELOPE *hdr, char *buf, size_t len, int do_lists)
{
  int me;

  me = mutt_addr_is_user (hdr->from);

  if (do_lists || me)
  {
    if (check_for_mailing_list_addr (hdr->to, buf, len))
      return;
    if (check_for_mailing_list_addr (hdr->cc, buf, len))
      return;
  }

  if (me && hdr->to)
    snprintf (buf, len, "%s", hdr->to->mailbox);
  else if (me && hdr->cc)
    snprintf (buf, len, "%s", hdr->cc->mailbox);
  else if (hdr->from)
    strfcpy (buf, hdr->from->mailbox, len);
  else
    *buf = 0;
}

156
static int user_in_addr (ADDRESS *a)
Thomas Roessler's avatar
Thomas Roessler committed
157 158 159 160 161 162 163 164 165 166 167 168 169
{
  for (; a; a = a->next)
    if (mutt_addr_is_user (a))
      return 1;
  return 0;
}

/* Return values:
 * 0: user is not in list
 * 1: user is unique recipient
 * 2: user is in the TO list
 * 3: user is in the CC list
 * 4: user is originator
170
 * 5: sent to a subscribed mailinglist
Thomas Roessler's avatar
Thomas Roessler committed
171
 */
172
int mutt_user_is_recipient (HEADER *h)
Thomas Roessler's avatar
Thomas Roessler committed
173
{
174
  ENVELOPE *env = h->env;
Thomas Roessler's avatar
Thomas Roessler committed
175

176
  if(!h->recip_valid)
Thomas Roessler's avatar
Thomas Roessler committed
177
  {
178 179
    h->recip_valid = 1;
    
180
    if (mutt_addr_is_user (env->from))
181
      h->recipient = 4;
182
    else if (user_in_addr (env->to))
183
    {
184
      if (env->to->next || env->cc)
185 186 187 188
	h->recipient = 2; /* non-unique recipient */
      else
	h->recipient = 1; /* unique recipient */
    }
189
    else if (user_in_addr (env->cc))
190
      h->recipient = 3;
191 192 193 194
    else if (check_for_mailing_list (env->to, NULL, NULL, 0))
      h->recipient = 5;
    else if (check_for_mailing_list (env->cc, NULL, NULL, 0))
      h->recipient = 5;
195 196
    else
      h->recipient = 0;
197 198 199
  }
  
  return h->recipient;
Thomas Roessler's avatar
Thomas Roessler committed
200 201
}

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
static char *apply_subject_mods (ENVELOPE *env)
{
  if (env == NULL)
    return NULL;

  if (SubjectRxList == NULL)
    return env->subject;

  if (env->subject == NULL || *env->subject == '\0')
    return env->disp_subj = NULL;

  env->disp_subj = mutt_apply_replace(NULL, 0, env->subject, SubjectRxList);
  return env->disp_subj;
}


Thomas Roessler's avatar
Thomas Roessler committed
218
/* %a = address of author
219
 * %A = reply-to address (if present; otherwise: address of author
Thomas Roessler's avatar
Thomas Roessler committed
220 221 222 223
 * %b = filename of the originating folder
 * %B = the list to which the letter was sent
 * %c = size of message in bytes
 * %C = current message number
224 225
 * %d = date and time of message using $date_format and sender's timezone
 * %D = date and time of message using $date_format and local timezone
226 227
 * %e = current message number in thread
 * %E = number of messages in current thread
Thomas Roessler's avatar
Thomas Roessler committed
228 229 230 231 232 233 234 235
 * %f = entire from line
 * %F = like %n, unless from self
 * %i = message-id
 * %l = number of lines in the message
 * %L = like %F, except `lists' are displayed first
 * %m = number of messages in the mailbox
 * %n = name of author
 * %N = score
Thomas Roessler's avatar
Thomas Roessler committed
236
 * %O = like %L, except using address instead of name
237
 * %P = progress indicator for builtin pager
238 239
 * %r = comma separated list of To: recipients
 * %R = comma separated list of Cc: recipients
Thomas Roessler's avatar
Thomas Roessler committed
240 241 242 243 244
 * %s = subject
 * %S = short message status (e.g., N/O/D/!/r/-)
 * %t = `to:' field (recipients)
 * %T = $to_chars
 * %u = user (login) name of author
245
 * %v = first name of author, unless from self
246
 * %X = number of MIME attachments
247 248
 * %y = `x-label:' field (if present)
 * %Y = `x-label:' field (if present, tree unfolded, and != parent's x-label)
Thomas Roessler's avatar
Thomas Roessler committed
249 250 251 252 253
 * %Z = status flags	*/

static const char *
hdr_format_str (char *dest,
		size_t destlen,
254
		size_t col,
255
                int cols,
Thomas Roessler's avatar
Thomas Roessler committed
256 257 258 259 260 261 262 263
		char op,
		const char *src,
		const char *prefix,
		const char *ifstring,
		const char *elsestring,
		unsigned long data,
		format_flag flags)
{
Thomas Roessler's avatar
Thomas Roessler committed
264
  struct hdr_format_info *hfi = (struct hdr_format_info *) data;
265
  HEADER *hdr, *htmp;
Thomas Roessler's avatar
Thomas Roessler committed
266
  CONTEXT *ctx;
267
  char fmt[SHORT_STRING], buf2[LONG_STRING], ch, *p;
Thomas Roessler's avatar
Thomas Roessler committed
268
  int do_locales, i;
269
  int optional = (flags & MUTT_FORMAT_OPTIONAL);
270
  int threads = ((Sort & SORT_MASK) == SORT_THREADS);
271
  int is_index = (flags & MUTT_FORMAT_INDEX);
272 273
#define THREAD_NEW (threads && hdr->collapsed && hdr->num_hidden > 1 && mutt_thread_contains_unread (ctx, hdr) == 1)
#define THREAD_OLD (threads && hdr->collapsed && hdr->num_hidden > 1 && mutt_thread_contains_unread (ctx, hdr) == 2)
Thomas Roessler's avatar
Thomas Roessler committed
274 275
  size_t len;

Thomas Roessler's avatar
Thomas Roessler committed
276 277 278
  hdr = hfi->hdr;
  ctx = hfi->ctx;

Thomas Roessler's avatar
Thomas Roessler committed
279 280 281
  dest[0] = 0;
  switch (op)
  {
282 283 284
    case 'A':
      if(hdr->env->reply_to && hdr->env->reply_to->mailbox)
      {
285
	mutt_format_s (dest, destlen, prefix, mutt_addr_for_display (hdr->env->reply_to));
286 287 288 289
	break;
      }
      /* fall through if 'A' returns nothing */

Thomas Roessler's avatar
Thomas Roessler committed
290
    case 'a':
Thomas Roessler's avatar
Thomas Roessler committed
291 292
      if(hdr->env->from && hdr->env->from->mailbox)
      {
293
	mutt_format_s (dest, destlen, prefix, mutt_addr_for_display (hdr->env->from));
Thomas Roessler's avatar
Thomas Roessler committed
294
      }
Thomas Roessler's avatar
Thomas Roessler committed
295 296
      else
        dest[0] = '\0';
Thomas Roessler's avatar
Thomas Roessler committed
297 298 299 300 301 302
      break;

    case 'B':
      if (!first_mailing_list (dest, destlen, hdr->env->to) &&
	  !first_mailing_list (dest, destlen, hdr->env->cc))
	dest[0] = 0;
303 304 305
      if (dest[0])
      {
	strfcpy (buf2, dest, sizeof(buf2));
306
	mutt_format_s (dest, destlen, prefix, buf2);
307 308 309
	break;
      }
      /* fall through if 'B' returns nothing */
Thomas Roessler's avatar
Thomas Roessler committed
310 311

    case 'b':
Thomas Roessler's avatar
Thomas Roessler committed
312 313 314 315 316 317 318 319 320
      if(ctx)
      {
	if ((p = strrchr (ctx->path, '/')))
	  strfcpy (dest, p + 1, destlen);
	else
	  strfcpy (dest, ctx->path, destlen);
      }
      else 
	strfcpy(dest, "(null)", destlen);
321
      strfcpy (buf2, dest, sizeof(buf2));
322
      mutt_format_s (dest, destlen, prefix, buf2);
Thomas Roessler's avatar
Thomas Roessler committed
323 324
      break;
    
Thomas Roessler's avatar
Thomas Roessler committed
325 326
    case 'c':
      mutt_pretty_size (buf2, sizeof (buf2), (long) hdr->content->length);
327
      mutt_format_s (dest, destlen, prefix, buf2);
Thomas Roessler's avatar
Thomas Roessler committed
328 329 330 331 332 333 334 335
      break;

    case 'C':
      snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
      snprintf (dest, destlen, fmt, hdr->msgno + 1);
      break;

    case 'd':
336
    case 'D':
Thomas Roessler's avatar
Thomas Roessler committed
337 338 339 340 341 342 343 344 345 346 347 348 349
    case '{':
    case '[':
    case '(':
    case '<':

      /* preprocess $date_format to handle %Z */
      {
	const char *cp;
	struct tm *tm; 
	time_t T;

	p = dest;

350
	cp = (op == 'd' || op == 'D') ? (NONULL (DateFmt)) : src;
Thomas Roessler's avatar
Thomas Roessler committed
351 352 353 354 355 356 357 358 359
	if (*cp == '!')
	{
	  do_locales = 0;
	  cp++;
	}
	else
	  do_locales = 1;

	len = destlen - 1;
360
	while (len > 0 && (((op == 'd' || op == 'D') && *cp) ||
Thomas Roessler's avatar
Thomas Roessler committed
361 362 363 364 365 366 367 368
			   (op == '{' && *cp != '}') || 
			   (op == '[' && *cp != ']') ||
			   (op == '(' && *cp != ')') ||
			   (op == '<' && *cp != '>')))
	{
	  if (*cp == '%')
	  {
	    cp++;
369
	    if ((*cp == 'Z' || *cp == 'z') && (op == 'd' || op == '{'))
Thomas Roessler's avatar
Thomas Roessler committed
370 371 372
	    {
	      if (len >= 5)
	      {
Thomas Roessler's avatar
Thomas Roessler committed
373
		sprintf (p, "%c%02u%02u", hdr->zoccident ? '-' : '+',
Thomas Roessler's avatar
Thomas Roessler committed
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
			 hdr->zhours, hdr->zminutes);
		p += 5;
		len -= 5;
	      }
	      else
		break; /* not enough space left */
	    }
	    else
	    {
	      if (len >= 2)
	      {
		*p++ = '%';
		*p++ = *cp;
		len -= 2;
	      }
	      else
		break; /* not enough space */
	    }
	    cp++;
	  }
	  else
	  {
	    *p++ = *cp++;
	    len--;
	  }
	}
	*p = 0;

402
	if (op == '[' || op == 'D')
Thomas Roessler's avatar
Thomas Roessler committed
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
	  tm = localtime (&hdr->date_sent);
	else if (op == '(')
	  tm = localtime (&hdr->received);
	else if (op == '<')
	{
	  T = time (NULL);
	  tm = localtime (&T);
	}
	else
	{
	  /* restore sender's time zone */
	  T = hdr->date_sent;
	  if (hdr->zoccident)
	    T -= (hdr->zhours * 3600 + hdr->zminutes * 60);
	  else
	    T += (hdr->zhours * 3600 + hdr->zminutes * 60);
	  tm = gmtime (&T);
	}

422 423 424 425 426
        if (!do_locales)
          setlocale (LC_TIME, "C");
        strftime (buf2, sizeof (buf2), dest, tm);
        if (!do_locales)
          setlocale (LC_TIME, "");
Thomas Roessler's avatar
Thomas Roessler committed
427

428
	mutt_format_s (dest, destlen, prefix, buf2);
429
	if (len > 0 && op != 'd' && op != 'D') /* Skip ending op */
Thomas Roessler's avatar
Thomas Roessler committed
430 431 432 433
	  src = cp + 1;
      }
      break;

434 435
    case 'e':
      snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
436
      snprintf (dest, destlen, fmt, mutt_messages_in_thread(ctx, hdr, 1));
437 438 439
      break;

    case 'E':
440 441 442 443 444 445 446
      if (!optional)
      {
	snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
	snprintf (dest, destlen, fmt, mutt_messages_in_thread(ctx, hdr, 0));
      }
      else if (mutt_messages_in_thread(ctx, hdr, 0) <= 1)
	optional = 0;
447 448
      break;

Thomas Roessler's avatar
Thomas Roessler committed
449 450
    case 'f':
      buf2[0] = 0;
451
      rfc822_write_address (buf2, sizeof (buf2), hdr->env->from, 1);
452
      mutt_format_s (dest, destlen, prefix, buf2);
Thomas Roessler's avatar
Thomas Roessler committed
453 454 455
      break;

    case 'F':
456 457 458
      if (!optional)
      {
        make_from (hdr->env, buf2, sizeof (buf2), 0);
459
	mutt_format_s (dest, destlen, prefix, buf2);
460 461 462
      }
      else if (mutt_addr_is_user (hdr->env->from))
        optional = 0;
Thomas Roessler's avatar
Thomas Roessler committed
463 464
      break;

465 466 467 468 469 470 471 472 473 474 475 476
    case 'H':
      /* (Hormel) spam score */
      if (optional)
	optional = hdr->env->spam ? 1 : 0;

       if (hdr->env->spam)
         mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->spam->data));
       else
         mutt_format_s (dest, destlen, prefix, "");

      break;

Thomas Roessler's avatar
Thomas Roessler committed
477
    case 'i':
478
      mutt_format_s (dest, destlen, prefix, hdr->env->message_id ? hdr->env->message_id : "<no.id>");
Thomas Roessler's avatar
Thomas Roessler committed
479 480 481
      break;

    case 'l':
482
      if (!optional)
483 484 485
      {
	snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
	snprintf (dest, destlen, fmt, (int) hdr->lines);
486
      }
487 488
      else if (hdr->lines <= 0)
        optional = 0;
Thomas Roessler's avatar
Thomas Roessler committed
489 490 491 492 493 494
      break;

    case 'L':
      if (!optional)
      {
	make_from (hdr->env, buf2, sizeof (buf2), 1);
495
	mutt_format_s (dest, destlen, prefix, buf2);
Thomas Roessler's avatar
Thomas Roessler committed
496 497 498 499 500 501 502 503 504
      }
      else if (!check_for_mailing_list (hdr->env->to, NULL, NULL, 0) &&
	       !check_for_mailing_list (hdr->env->cc, NULL, NULL, 0))
      {
	optional = 0;
      }
      break;

    case 'm':
Thomas Roessler's avatar
Thomas Roessler committed
505 506 507 508 509 510 511
      if(ctx)
      {
	snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
	snprintf (dest, destlen, fmt, ctx->msgcount);
      }
      else
	strfcpy(dest, "(null)", destlen);
Thomas Roessler's avatar
Thomas Roessler committed
512 513 514
      break;

    case 'n':
515
      mutt_format_s (dest, destlen, prefix, mutt_get_name (hdr->env->from));
Thomas Roessler's avatar
Thomas Roessler committed
516 517 518
      break;

    case 'N':
519 520 521 522 523 524 525
      if (!optional)
      {
	snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
	snprintf (dest, destlen, fmt, hdr->score);
      }
      else
      {
526
	if (hdr->score == 0)
527 528
	  optional = 0;
      }
Thomas Roessler's avatar
Thomas Roessler committed
529 530
      break;

Thomas Roessler's avatar
Thomas Roessler committed
531 532 533 534 535 536
    case 'O':
      if (!optional)
      {
	make_from_addr (hdr->env, buf2, sizeof (buf2), 1);
	if (!option (OPTSAVEADDRESS) && (p = strpbrk (buf2, "%@")))
	  *p = 0;
537
	mutt_format_s (dest, destlen, prefix, buf2);
Thomas Roessler's avatar
Thomas Roessler committed
538 539 540 541 542 543 544 545
      }
      else if (!check_for_mailing_list_addr (hdr->env->to, NULL, 0) &&
	       !check_for_mailing_list_addr (hdr->env->cc, NULL, 0))
      {
	optional = 0;
      }
      break;

546 547 548 549 550 551 552
    case 'M':
      snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
      if (!optional)
      {
	if (threads && is_index && hdr->collapsed && hdr->num_hidden > 1)
	  snprintf (dest, destlen, fmt, hdr->num_hidden);
	else if (is_index && threads)
553
	  mutt_format_s (dest, destlen, prefix, " ");
554
	else
555
	  *dest = '\0';
556 557 558 559 560 561 562 563
      }
      else
      {
	if (!(threads && is_index && hdr->collapsed && hdr->num_hidden > 1))
	  optional = 0;
      }
      break;

564 565 566 567
    case 'P':
      strfcpy(dest, NONULL(hfi->pager_progress), destlen);
      break;

568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
    case 'r':
      buf2[0] = 0;
      rfc822_write_address(buf2, sizeof(buf2), hdr->env->to, 1);
      if (optional && buf2[0] == '\0')
        optional = 0;
      mutt_format_s (dest, destlen, prefix, buf2);
      break;

    case 'R':
      buf2[0] = 0;
      rfc822_write_address(buf2, sizeof(buf2), hdr->env->cc, 1);
      if (optional && buf2[0] == '\0')
        optional = 0;
      mutt_format_s (dest, destlen, prefix, buf2);
      break;

Thomas Roessler's avatar
Thomas Roessler committed
584 585
    case 's':
      {
586 587 588 589 590 591 592 593
	char *subj;
        if (hdr->env->disp_subj)
	  subj = hdr->env->disp_subj;
	else if (SubjectRxList)
	  subj = apply_subject_mods(hdr->env);
	else
	  subj = hdr->env->subject;
	if (flags & MUTT_FORMAT_TREE && !hdr->collapsed)
Thomas Roessler's avatar
Thomas Roessler committed
594
	{
595 596 597 598 599 600 601 602
	  if (flags & MUTT_FORMAT_FORCESUBJ)
	  {
	    mutt_format_s (dest, destlen, "", NONULL (subj));
	    snprintf (buf2, sizeof (buf2), "%s%s", hdr->tree, dest);
	    mutt_format_s_tree (dest, destlen, prefix, buf2);
	  }
	  else
	    mutt_format_s_tree (dest, destlen, prefix, hdr->tree);
Thomas Roessler's avatar
Thomas Roessler committed
603 604
	}
	else
605
	  mutt_format_s (dest, destlen, prefix, NONULL (subj));
Thomas Roessler's avatar
Thomas Roessler committed
606 607 608 609 610 611 612 613 614 615 616 617 618 619
      }
      break;

    case 'S':
      if (hdr->deleted)
	ch = 'D';
      else if (hdr->attach_del)
	ch = 'd';
      else if (hdr->tagged)
	ch = '*';
      else if (hdr->flagged)
	ch = '!';
      else if (hdr->replied)
	ch = 'r';
Thomas Roessler's avatar
Thomas Roessler committed
620
      else if (hdr->read && (ctx && ctx->msgnotreadyet != hdr->msgno))
Thomas Roessler's avatar
Thomas Roessler committed
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
	ch = '-';
      else if (hdr->old)
	ch = 'O';
      else
	ch = 'N';

      /* FOO - this is probably unsafe, but we are not likely to have such
	 a short string passed into this routine */
      *dest = ch;
      *(dest + 1) = 0;
      break;

    case 't':
      buf2[0] = 0;
      if (!check_for_mailing_list (hdr->env->to, "To ", buf2, sizeof (buf2)) &&
	  !check_for_mailing_list (hdr->env->cc, "Cc ", buf2, sizeof (buf2)))
      {
	if (hdr->env->to)
	  snprintf (buf2, sizeof (buf2), "To %s", mutt_get_name (hdr->env->to));
	else if (hdr->env->cc)
	  snprintf (buf2, sizeof (buf2), "Cc %s", mutt_get_name (hdr->env->cc));
      }
643
      mutt_format_s (dest, destlen, prefix, buf2);
Thomas Roessler's avatar
Thomas Roessler committed
644 645 646
      break;

    case 'T':
647
      snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
Thomas Roessler's avatar
Thomas Roessler committed
648
      snprintf (dest, destlen, fmt,
649
		(Tochars && ((i = mutt_user_is_recipient (hdr))) < Tochars->len) ? Tochars->chars[i] : " ");
Thomas Roessler's avatar
Thomas Roessler committed
650 651 652 653 654
      break;

    case 'u':
      if (hdr->env->from && hdr->env->from->mailbox)
      {
655
	strfcpy (buf2, mutt_addr_for_display (hdr->env->from), sizeof (buf2));
Thomas Roessler's avatar
Thomas Roessler committed
656 657 658 659 660
	if ((p = strpbrk (buf2, "%@")))
	  *p = 0;
      }
      else
	buf2[0] = 0;
661
      mutt_format_s (dest, destlen, prefix, buf2);
Thomas Roessler's avatar
Thomas Roessler committed
662 663
      break;

664 665 666 667
    case 'v':
      if (mutt_addr_is_user (hdr->env->from)) 
      {
	if (hdr->env->to)
668
	  mutt_format_s (buf2, sizeof (buf2), prefix, mutt_get_name (hdr->env->to));
669
	else if (hdr->env->cc)
670
	  mutt_format_s (buf2, sizeof (buf2), prefix, mutt_get_name (hdr->env->cc));
671 672 673 674
	else
	  *buf2 = 0;
      }
      else
675
	mutt_format_s (buf2, sizeof (buf2), prefix, mutt_get_name (hdr->env->from));
676 677
      if ((p = strpbrk (buf2, " %@")))
	*p = 0;
678
      mutt_format_s (dest, destlen, prefix, buf2);
679 680
      break;

Thomas Roessler's avatar
Thomas Roessler committed
681
    case 'Z':
682 683
    
      ch = ' ';
Thomas Roessler's avatar
Thomas Roessler committed
684

685
      if (WithCrypto && hdr->security & GOODSIGN)
Thomas Roessler's avatar
Thomas Roessler committed
686
        ch = 'S';
687
      else if (WithCrypto && hdr->security & ENCRYPT)
Thomas Roessler's avatar
Thomas Roessler committed
688
      	ch = 'P';
689
      else if (WithCrypto && hdr->security & SIGN)
Thomas Roessler's avatar
Thomas Roessler committed
690
        ch = 's';
691 692
      else if ((WithCrypto & APPLICATION_PGP) &&
               ((hdr->security & PGPKEY) == PGPKEY))
Thomas Roessler's avatar
Thomas Roessler committed
693 694 695
        ch = 'K';

      snprintf (buf2, sizeof (buf2),
696
		"%c%c%s", (THREAD_NEW ? 'n' : (THREAD_OLD ? 'o' :
697 698
		((hdr->read && (ctx && ctx->msgnotreadyet != hdr->msgno))
		? (hdr->replied ? 'r' : ' ') : (hdr->old ? 'O' : 'N')))),
Thomas Roessler's avatar
Thomas Roessler committed
699
		hdr->deleted ? 'D' : (hdr->attach_del ? 'd' : ch),
700 701 702
		hdr->tagged ? "*" :
		(hdr->flagged ? "!" :
		 (Tochars && ((i = mutt_user_is_recipient (hdr)) < Tochars->len) ? Tochars->chars[i] : " ")));
703
      mutt_format_s (dest, destlen, prefix, buf2);
Thomas Roessler's avatar
Thomas Roessler committed
704 705
      break;

706 707
    case 'X':
      {
708
	int count = mutt_count_body_parts (ctx, hdr);
709 710 711 712 713 714 715 716 717 718

	/* The recursion allows messages without depth to return 0. */
	if (optional)
          optional = count != 0;

        snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
        snprintf (dest, destlen, fmt, count);
      }
      break;

719 720 721 722
     case 'y':
       if (optional)
	 optional = hdr->env->x_label ? 1 : 0;

723
       mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->x_label));
724 725 726 727 728 729 730
       break;
 
    case 'Y':
      if (hdr->env->x_label)
      {
	i = 1;	/* reduce reuse recycle */
	htmp = NULL;
731
	if (flags & MUTT_FORMAT_TREE
732 733 734
	    && (hdr->thread->prev && hdr->thread->prev->message
		&& hdr->thread->prev->message->env->x_label))
	  htmp = hdr->thread->prev->message;
735
	else if (flags & MUTT_FORMAT_TREE
736 737 738
		 && (hdr->thread->parent && hdr->thread->parent->message
		     && hdr->thread->parent->message->env->x_label))
	  htmp = hdr->thread->parent->message;
739 740 741 742 743 744 745 746 747 748 749
	if (htmp && mutt_strcasecmp (hdr->env->x_label,
				     htmp->env->x_label) == 0)
	  i = 0;
      }
      else
	i = 0;

      if (optional)
	optional = i;

      if (i)
750
        mutt_format_s (dest, destlen, prefix, NONULL (hdr->env->x_label));
751
      else
752
        mutt_format_s (dest, destlen, prefix, "");
753 754

      break;
755

756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
    case '@':
    {
      const char *end = src;
      static unsigned char recurse = 0;

      while (*end && *end != '@')
        end++;
      if ((*end == '@') && (recurse < 20))
      {
        recurse++;
        mutt_substrcpy (buf2, src, end, sizeof(buf2));
        mutt_FormatString (buf2, sizeof(buf2), col, cols,
                           NONULL (mutt_idxfmt_hook (buf2, ctx, hdr)),
                           hdr_format_str, (unsigned long) hfi, flags);
        mutt_format_s (dest, destlen, prefix, buf2);
        recurse--;

        src = end + 1;
        break;
      }
      /* otherwise fall through */
    }

Thomas Roessler's avatar
Thomas Roessler committed
779 780 781 782 783 784
    default:
      snprintf (dest, destlen, "%%%s%c", prefix, op);
      break;
  }

  if (optional)
785
    mutt_FormatString (dest, destlen, col, cols, ifstring, hdr_format_str, (unsigned long) hfi, flags);
786
  else if (flags & MUTT_FORMAT_OPTIONAL)
787
    mutt_FormatString (dest, destlen, col, cols, elsestring, hdr_format_str, (unsigned long) hfi, flags);
Thomas Roessler's avatar
Thomas Roessler committed
788 789

  return (src);
790 791
#undef THREAD_NEW
#undef THREAD_OLD
Thomas Roessler's avatar
Thomas Roessler committed
792 793 794
}

void
Thomas Roessler's avatar
Thomas Roessler committed
795
_mutt_make_string (char *dest, size_t destlen, const char *s, CONTEXT *ctx, HEADER *hdr, format_flag flags)
Thomas Roessler's avatar
Thomas Roessler committed
796
{
Thomas Roessler's avatar
Thomas Roessler committed
797 798 799 800
  struct hdr_format_info hfi;

  hfi.hdr = hdr;
  hfi.ctx = ctx;
801
  hfi.pager_progress = 0;
Thomas Roessler's avatar
Thomas Roessler committed
802

803
  mutt_FormatString (dest, destlen, 0, MuttIndexWindow->cols, s, hdr_format_str, (unsigned long) &hfi, flags);
Thomas Roessler's avatar
Thomas Roessler committed
804
}
805 806

void
807
mutt_make_string_info (char *dst, size_t dstlen, int cols, const char *s, struct hdr_format_info *hfi, format_flag flags)
808
{
809
  mutt_FormatString (dst, dstlen, 0, cols, s, hdr_format_str, (unsigned long) hfi, flags);
810
}