hdrline.c 13.7 KB
Newer Older
Thomas Roessler's avatar
Thomas Roessler committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (C) 1996-8 Michael R. Elkins <me@cs.hmc.edu>
 * 
 *     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
 *     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */ 

#include "mutt.h"
#include "mutt_curses.h"
21
#include "sort.h"
Thomas Roessler's avatar
Thomas Roessler committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47


#ifdef _PGPPATH
#include "pgp.h"
#endif



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

int mutt_is_mail_list (ADDRESS *addr)
{
  LIST *p;

  if (addr->mailbox)
  {
    for (p = MailLists; p; p = p->next)
      if (strncasecmp (addr->mailbox, p->data, strlen (p->data)) == 0)
	return 1;
  }
  return 0;
}

Thomas Roessler's avatar
Thomas Roessler committed
48 49 50 51
/* 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
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
static int
check_for_mailing_list (ADDRESS *adr, char *pfx, char *buf, int buflen)
{
  for (; adr; adr = adr->next)
  {
    if (mutt_is_mail_list (adr))
    {
      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
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
/* 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)
  {
    if (mutt_is_mail_list (adr))
    {
      if (buf && buflen)
	snprintf (buf, buflen, "%s", adr->mailbox);
      return 1;
    }
  }
  return 0;
}


Thomas Roessler's avatar
Thomas Roessler committed
87 88 89 90 91 92 93 94 95 96 97 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
static int first_mailing_list (char *buf, size_t buflen, ADDRESS *a)
{
  for (; a; a = a->next)
  {
    if (mutt_is_mail_list (a))
    {
      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));
  else if (hdr->from)
    strfcpy (buf, mutt_get_name (hdr->from), len);
  else
    *buf = 0;
}

Thomas Roessler's avatar
Thomas Roessler committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
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;
}

Thomas Roessler's avatar
Thomas Roessler committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
int mutt_user_is_recipient (ADDRESS *a)
{
  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
 */
static int user_is_recipient (ENVELOPE *hdr)
{
  if (mutt_addr_is_user (hdr->from))
    return 4;

  if (mutt_user_is_recipient (hdr->to))
  {
    if (hdr->to->next || hdr->cc)
      return 2; /* non-unique recipient */
    else
      return 1; /* unique recipient */
  }

  if (mutt_user_is_recipient (hdr->cc))
    return 3;

  return (0);
}

/* %a = address of author
 * %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
 * %d = date and time of message (using strftime)
 * %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
196
 * %O = like %L, except using address instead of name
Thomas Roessler's avatar
Thomas Roessler committed
197 198 199 200 201 202 203
 * %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
 * %Z = status flags	*/

Thomas Roessler's avatar
Thomas Roessler committed
204 205 206 207 208 209
struct hdr_format_info
{
  CONTEXT *ctx;
  HEADER *hdr;
};

Thomas Roessler's avatar
Thomas Roessler committed
210 211 212 213 214 215 216 217 218 219 220
static const char *
hdr_format_str (char *dest,
		size_t destlen,
		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
221 222 223
  struct hdr_format_info *hfi = (struct hdr_format_info *) data;
  HEADER *hdr;
  CONTEXT *ctx;
Thomas Roessler's avatar
Thomas Roessler committed
224 225 226
  char fmt[SHORT_STRING], buf2[SHORT_STRING], ch, *p;
  int do_locales, i;
  int optional = (flags & M_FORMAT_OPTIONAL);
227 228
  int threads = ((Sort & SORT_MASK) == SORT_THREADS);
  int is_index = (flags & M_FORMAT_INDEX);
229 230
#define NEW (threads && hdr->collapsed && hdr->num_hidden > 1 && mutt_thread_contains_unread (ctx, hdr) == 1)
#define OLD (threads && hdr->collapsed && hdr->num_hidden > 1 && mutt_thread_contains_unread (ctx, hdr) == 2)
Thomas Roessler's avatar
Thomas Roessler committed
231 232
  size_t len;

Thomas Roessler's avatar
Thomas Roessler committed
233 234 235
  hdr = hfi->hdr;
  ctx = hfi->ctx;

Thomas Roessler's avatar
Thomas Roessler committed
236 237 238 239
  dest[0] = 0;
  switch (op)
  {
    case 'a':
Thomas Roessler's avatar
Thomas Roessler committed
240 241 242 243 244
      if(hdr->env->from && hdr->env->from->mailbox)
      {
	snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
	snprintf (dest, destlen, fmt, hdr->env->from->mailbox);
      }
Thomas Roessler's avatar
Thomas Roessler committed
245 246
      else
        dest[0] = '\0';
Thomas Roessler's avatar
Thomas Roessler committed
247 248 249 250 251 252 253 254 255
      break;

    case 'B':
      if (!first_mailing_list (dest, destlen, hdr->env->to) &&
	  !first_mailing_list (dest, destlen, hdr->env->cc))
	dest[0] = 0;
      break;

    case 'b':
Thomas Roessler's avatar
Thomas Roessler committed
256 257 258 259 260 261 262 263 264
      if(ctx)
      {
	if ((p = strrchr (ctx->path, '/')))
	  strfcpy (dest, p + 1, destlen);
	else
	  strfcpy (dest, ctx->path, destlen);
      }
      else 
	strfcpy(dest, "(null)", destlen);
Thomas Roessler's avatar
Thomas Roessler committed
265 266
      break;
    
Thomas Roessler's avatar
Thomas Roessler committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
    case 'c':
      mutt_pretty_size (buf2, sizeof (buf2), (long) hdr->content->length);
      snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
      snprintf (dest, destlen, fmt, buf2);
      break;

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

    case 'd':
    case '{':
    case '[':
    case '(':
    case '<':

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

	p = dest;

	cp = (op == 'd') ? (NONULL (DateFmt)) : src;
	if (*cp == '!')
	{
	  do_locales = 0;
	  cp++;
	}
	else
	  do_locales = 1;

	len = destlen - 1;
	while (len > 0 && ((op == 'd' && *cp) ||
			   (op == '{' && *cp != '}') || 
			   (op == '[' && *cp != ']') ||
			   (op == '(' && *cp != ')') ||
			   (op == '<' && *cp != '>')))
	{
	  if (*cp == '%')
	  {
	    cp++;
	    if (*cp == 'Z' && (op == 'd' || op == '{'))
	    {
	      if (len >= 5)
	      {
Thomas Roessler's avatar
Thomas Roessler committed
315
		sprintf (p, "%c%02u%02u", hdr->zoccident ? '-' : '+',
Thomas Roessler's avatar
Thomas Roessler committed
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 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 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
			 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;

	if (do_locales && Locale)
	  setlocale (LC_TIME, Locale);

	if (op == '[')
	  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);
	}

	strftime (buf2, sizeof (buf2), dest, tm);

	if (do_locales)
	  setlocale (LC_TIME, "C");

	snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
	snprintf (dest, destlen, fmt, buf2);
	if (len > 0 && op != 'd')
	  src = cp + 1;
      }
      break;

    case 'f':
      buf2[0] = 0;
      rfc822_write_address (buf2, sizeof (buf2), hdr->env->from);
      snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
      snprintf (dest, destlen, fmt, buf2);
      break;

    case 'F':
      snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
      make_from (hdr->env, buf2, sizeof (buf2), 0);
      snprintf (dest, destlen, fmt, buf2);
      break;

    case 'i':
      snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
      snprintf (dest, destlen, fmt, hdr->env->message_id ? hdr->env->message_id : "<no.id>");
      break;

    case 'l':
      snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
      snprintf (dest, destlen, fmt, (int) hdr->lines);
      break;

    case 'L':
      if (!optional)
      {
	make_from (hdr->env, buf2, sizeof (buf2), 1);
	snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
	snprintf (dest, destlen, fmt, buf2);
      }
      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
417 418 419 420 421 422 423
      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
424 425 426 427 428 429 430 431 432 433 434 435
      break;

    case 'n':
      snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
      snprintf (dest, destlen, fmt, mutt_get_name (hdr->env->from));
      break;

    case 'N':
      snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
      snprintf (dest, destlen, fmt, hdr->score);
      break;

Thomas Roessler's avatar
Thomas Roessler committed
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
    case 'O':
      if (!optional)
      {
	make_from_addr (hdr->env, buf2, sizeof (buf2), 1);
	if (!option (OPTSAVEADDRESS) && (p = strpbrk (buf2, "%@")))
	  *p = 0;
	snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
	snprintf (dest, destlen, fmt, buf2);
      }
      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;

Thomas Roessler's avatar
Thomas Roessler committed
452
    case 's':
453 454 455 456 457
      
      snprintf (fmt, sizeof (fmt), "%s%%%ss", threads && is_index ? "   " : "", prefix);
      if (threads && is_index && hdr->collapsed && hdr->num_hidden > 1)
	snprintf (dest, destlen, "%2d %s", hdr->num_hidden, NONULL(hdr->env->subject));
      else if (flags & M_FORMAT_TREE)
Thomas Roessler's avatar
Thomas Roessler committed
458 459 460
      {
	if (flags & M_FORMAT_FORCESUBJ)
	{
461
	  snprintf (buf2, sizeof (buf2), "%s%s", hdr->tree, NONULL (hdr->env->subject));
Thomas Roessler's avatar
Thomas Roessler committed
462 463 464 465 466 467
	  snprintf (dest, destlen, fmt, buf2);
	}
	else
	  snprintf (dest, destlen, fmt, hdr->tree);
      }
      else
468
	snprintf (dest, destlen, fmt, NONULL (hdr->env->subject));
Thomas Roessler's avatar
Thomas Roessler committed
469 470 471 472 473 474 475 476 477 478 479 480 481
      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
482
      else if (hdr->read && (ctx && ctx->msgnotreadyet != hdr->msgno))
Thomas Roessler's avatar
Thomas Roessler committed
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
	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));
      }
      snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
      snprintf (dest, destlen, fmt, buf2);
      break;

    case 'T':
      snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
      snprintf (dest, destlen, fmt,
		(Tochars && ((i = user_is_recipient (hdr->env))) < strlen (Tochars)) ? Tochars[i] : ' ');
      break;

    case 'u':
      if (hdr->env->from && hdr->env->from->mailbox)
      {
	strfcpy (buf2, hdr->env->from->mailbox, sizeof (buf2));
	if ((p = strpbrk (buf2, "%@")))
	  *p = 0;
      }
      else
	buf2[0] = 0;
      snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
      snprintf (dest, destlen, fmt, buf2);
      break;

    case 'Z':
      if (hdr->mailcap)
	ch = 'M';



#ifdef _PGPPATH
      else if (hdr->pgp & PGPENCRYPT)
      	ch = 'P';
      else if (hdr->pgp & PGPSIGN)
        ch = 'S';
      else if (hdr->pgp & PGPKEY)
        ch = 'K';
#endif



      else
	ch = ' ';
      snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
      snprintf (buf2, sizeof (buf2),
549 550 551
		"%c%c%c", (NEW ? 'n' : (OLD ? 'o' : 
		((hdr->read && (ctx && ctx->msgnotreadyet != hdr->msgno))
		? (hdr->replied ? 'r' : ' ') : (hdr->old ? 'O' : 'N')))),
Thomas Roessler's avatar
Thomas Roessler committed
552 553 554 555 556 557 558 559 560 561 562 563 564
		hdr->deleted ? 'D' : (hdr->attach_del ? 'd' : ch),
		hdr->tagged ? '*' :
		(hdr->flagged ? '!' :
		 (Tochars && ((i = user_is_recipient (hdr->env)) < strlen (Tochars)) ? Tochars[user_is_recipient (hdr->env)] : ' ')));
      snprintf (dest, destlen, fmt, buf2);
      break;

    default:
      snprintf (dest, destlen, "%%%s%c", prefix, op);
      break;
  }

  if (optional)
565
    mutt_FormatString (dest, destlen, ifstring, hdr_format_str, (unsigned long) hfi, flags);
Thomas Roessler's avatar
Thomas Roessler committed
566
  else if (flags & M_FORMAT_OPTIONAL)
567
    mutt_FormatString (dest, destlen, elsestring, hdr_format_str, (unsigned long) hfi, flags);
Thomas Roessler's avatar
Thomas Roessler committed
568 569

  return (src);
570
#undef NEW
571
#undef OLD
Thomas Roessler's avatar
Thomas Roessler committed
572 573 574
}

void
Thomas Roessler's avatar
Thomas Roessler committed
575
_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
576
{
Thomas Roessler's avatar
Thomas Roessler committed
577 578 579 580 581 582
  struct hdr_format_info hfi;

  hfi.hdr = hdr;
  hfi.ctx = ctx;

  mutt_FormatString (dest, destlen, s, hdr_format_str, (unsigned long) &hfi, flags);
Thomas Roessler's avatar
Thomas Roessler committed
583
}