from.c 12.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 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 315 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 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 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 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 1999-2003, 2005, 2007, 2009-2012 Free Software
   Foundation, Inc.

   GNU Mailutils 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 3, or (at your option)
   any later version.

   GNU Mailutils 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 GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */

#include "mail.h"
#include <mu_umaxtostr.h>

#define ALIGN_UNDEF -1
#define ALIGN_RIGHT 0
#define ALIGN_LEFT  1

struct header_call_args
{
  msgset_t *mspec;
  mu_message_t msg;
  size_t cols_rest;
  char *buf;
  size_t size;
};
  
struct header_segm
{
  struct header_segm *next;
  int align;
  size_t width;
  void *data;
  char *(*get) (struct header_call_args *args, void *data);
};

void
header_ensure_space (struct header_call_args *args, size_t size)
{
  if (size > args->size)
    {
      args->buf = xrealloc (args->buf, size);
      args->size = size;
    }
}

static char *
header_buf_string_len (struct header_call_args *args, const char *str,
		       size_t len)
{
  header_ensure_space (args, len + 1);
  memcpy (args->buf, str, len);
  args->buf[len] = 0;
  return args->buf;
}

static char *
header_buf_string (struct header_call_args *args, const char *str)
{
  if (!str)
    return header_buf_string_len (args, "", 0);
  return header_buf_string_len (args, str, strlen (str));
}

static void
format_pad (size_t n)
{
  for (; n; n--)
    mu_stream_write (mu_strout, " ", 1, NULL);
}

static void
format_headline (struct header_segm *seg, msgset_t *mspec, mu_message_t msg)
{
  int screen_cols = util_getcols () - 2;
  int out_cols = 0;
  struct header_call_args args;

  args.mspec = mspec;
  args.msg = msg;
  args.buf = NULL;
  args.size = 0;
  
  for (; seg; seg = seg->next)
    {
      size_t width, len;
      size_t cols_rest = screen_cols - out_cols;
      char *p;

      args.cols_rest = cols_rest;
      p = seg->get (&args, seg->data);

      if (!p)
	p = "";
      len = strlen (p);
      
      if (seg->width)
	width = seg->width;
      else
	width = len;
      if (width > cols_rest)
	width = cols_rest;

      if (len > width)
	len = width;
      
      if (seg->align == ALIGN_RIGHT)
	{
	  format_pad (width - len);
	  mu_printf ("%*.*s", (int) len, (int) len, p);
	}
      else
	{
	  mu_printf ("%*.*s", (int) len, (int) len, p);
	  format_pad (width - len);
	}
      out_cols += width;
    }

  mu_printf ("\n");
  free (args.buf);
}    

static void
free_headline (struct header_segm *seg)
{
  while (seg)
    {
      struct header_segm *next = seg->next;
      if (seg->data)
	free (seg->data);
      free (seg);
      seg = next;
    }
}


static char *
hdr_text (struct header_call_args *args, void *data)
{
  return data;
}

static char *
hdr_cur (struct header_call_args *args, void *data)
{
  if (is_current_message (args->mspec->msg_part[0]))
    return (char*) data;
  return " ";
}

/* %a */
static char *
hdr_attr (struct header_call_args *args, void *data)
{
  mu_attribute_t attr;
  char cflag;
  
  mu_message_get_attribute (args->msg, &attr);
  
  if (mu_attribute_is_userflag (attr, MAIL_ATTRIBUTE_MBOXED))
    cflag = 'M';
  else if (mu_attribute_is_userflag (attr, MAIL_ATTRIBUTE_PRESERVED))
    cflag = 'P';
  else if (mu_attribute_is_userflag (attr, MAIL_ATTRIBUTE_SAVED))
    cflag = '*';
  else if (mu_attribute_is_userflag (attr, MAIL_ATTRIBUTE_TAGGED))
    cflag = 'T';
  else if (mu_attribute_is_userflag (attr, MAIL_ATTRIBUTE_SHOWN))
    cflag = 'R';
  else if (mu_attribute_is_recent (attr))
    cflag = 'N';
  else if (!mu_attribute_is_read (attr))
    cflag = 'U';
  else
    cflag = ' ';
  return header_buf_string_len (args, &cflag, 1);
}
    
/* %d */
static char *
hdr_date (struct header_call_args *args, void *data)
{
  char date[80];
  mu_header_t hdr;

  mu_message_get_header (args->msg, &hdr);
  
  date[0] = 0;
  if (mailvar_get (NULL, "datefield", mailvar_type_boolean, 0) == 0
      && mu_header_get_value (hdr, MU_HEADER_DATE,
			      date, sizeof (date), NULL) == 0)
    {
      time_t t;
      if (mu_parse_date (date, &t, NULL) == 0)
	strftime (date, sizeof(date), "%a %b %e %H:%M", localtime (&t));
      else
	date[0] = 0;
    }

  if (date[0] == 0)
    {
      const char *p;
      struct tm tm;
      struct mu_timezone tz;
      mu_envelope_t env;
      
      mu_message_get_envelope (args->msg, &env);
      if (mu_envelope_sget_date (env, &p) == 0
          && mu_scan_datetime (p, MU_DATETIME_FROM, &tm, &tz, NULL) == 0)
	strftime (date, sizeof(date), "%a %b %e %H:%M", &tm);
    }
  return header_buf_string (args, date);
}

/* %f */
static char *
hdr_from (struct header_call_args *args, void *data)
{
  char *from = NULL;
  
  if (mailvar_get (NULL, "fromfield", mailvar_type_boolean, 0) == 0)
    {  
      mu_header_t hdr;
      
      if (mu_message_get_header (args->msg, &hdr) == 0
	  && mu_header_aget_value_unfold (hdr, MU_HEADER_FROM, &from) == 0)
	{
	  mu_address_t address = NULL;
	  if (mu_address_create (&address, from) == 0)
	    {
	      char *name;
	      const char *email;
	  
	      if (mu_address_sget_email (address, 1, &email) == 0)
		{
		  if (mailvar_get (NULL, "showto",
				   mailvar_type_boolean, 0) == 0
		      && mail_is_my_name (email))
		    {
		      char *tmp;

		      if (mu_header_aget_value_unfold (hdr, MU_HEADER_TO, 
						       &tmp) == 0)
			{
			  mu_address_t addr_to;
			  if (mu_address_create (&addr_to, tmp) == 0)
			    {
			      mu_address_destroy (&address);
			      address = addr_to;
			    }
			  free (tmp);
			}
		    }
		}
	      
	      if ((mu_address_aget_personal (address, 1, &name) == 0
		   && name)
		  || (mu_address_aget_email (address, 1, &name) == 0
		      && name))
		{
		  free (from);
		  from = name;
		}
	      mu_address_destroy (&address);
	    }
	}
      util_rfc2047_decode (&from);
    }
  else
    {
      mu_envelope_t env = NULL;
      const char *sender = "";
      
      if (mu_message_get_envelope (args->msg, &env) == 0)
	mu_envelope_sget_sender (env, &sender);
      from = strdup (sender);
    }

  header_buf_string (args, from);
  free (from);
  return args->buf;
}

/* %l */
static char *
hdr_lines (struct header_call_args *args, void *data)
{
  size_t m_lines;
  char buf[UINTMAX_STRSIZE_BOUND];

  mu_message_lines (args->msg, &m_lines);
  
  return header_buf_string (args, umaxtostr (m_lines, buf));
}

/* %L */
static char *
hdr_quick_lines (struct header_call_args *args, void *data)
{
  size_t m_lines;
  char buf[UINTMAX_STRSIZE_BOUND];
  int rc;
  const char *p;
  
  rc = mu_message_quick_lines (args->msg, &m_lines);
  if (rc == 0)
    p = umaxtostr (m_lines, buf);
  else
    p = "NA";
  return header_buf_string (args, p);
}

/* %m */
static char *
hdr_number (struct header_call_args *args, void *data)
{
  char buf[UINTMAX_STRSIZE_BOUND];
  return header_buf_string (args, umaxtostr (args->mspec->msg_part[0], buf));
}

/* %o */
static char *
hdr_size (struct header_call_args *args, void *data)
{
  size_t m_size;
  char buf[UINTMAX_STRSIZE_BOUND];
  mu_message_size (args->msg, &m_size);
  
  return header_buf_string (args, umaxtostr (m_size, buf));
}

/* %s */
static char *
hdr_subject (struct header_call_args *args, void *data)
{
  mu_header_t hdr;
  char *subj = NULL;
  
  mu_message_get_header (args->msg, &hdr);
  mu_header_aget_value_unfold (hdr, MU_HEADER_SUBJECT, &subj);
  util_rfc2047_decode (&subj);
  
  header_buf_string (args, subj);
  free (subj);
  return args->buf;
}

/* %S */
static char *
hdr_q_subject (struct header_call_args *args, void *data)
{
  mu_header_t hdr;
  char *subj = NULL;
  size_t len;

  if (args->cols_rest <= 2)
    return "\"\"";
  
  mu_message_get_header (args->msg, &hdr);
  mu_header_aget_value_unfold (hdr, MU_HEADER_SUBJECT, &subj);
  if (!subj)
    return "";
  util_rfc2047_decode (&subj);

  len = strlen (subj);
  if (len + 2 > args->cols_rest)
    len = args->cols_rest - 2;
  header_ensure_space (args, len + 3);
  args->buf[0] = '"';
  memcpy (args->buf + 1, subj, len);
  args->buf[len+1] = '"';
  args->buf[len+2] = 0;
  free (subj);
  return args->buf;
}


static struct header_segm *
new_header_segment (int align, size_t width,
		    void *data,
		    char *(*get) (struct header_call_args *, void *))
{
  struct header_segm *seg = xmalloc (sizeof (*seg));
  seg->next = NULL;
  seg->align = align;
  seg->width = width;
  seg->data = data;
  seg->get = get;
  return seg;
}

struct header_segm *
compile_headline (const char *str)
{
  struct header_segm *head = NULL, *tail = NULL;
  char *text;
  int align;
  size_t width;
  
#define ALIGN_STRING (align == ALIGN_UNDEF ? ALIGN_LEFT : ALIGN_RIGHT)
#define ALIGN_NUMBER (align == ALIGN_UNDEF ? ALIGN_RIGHT : ALIGN_LEFT)
#define ATTACH(p)				\
  do						\
    {						\
      if (!head)				\
	head = p;				\
      else					\
	tail->next = p;				\
      tail = p;					\
    }						\
  while (0)
      
  while (*str)
    {
      struct header_segm *seg;
      size_t len;
      char *p = strchr (str, '%');
      if (!p)
	len = strlen (str);
      else
	len = p - str;
      if (len)
	{
	  text = xmalloc (len + 1);
	  memcpy (text, str, len);
	  text[len] = 0;
	  seg = new_header_segment (ALIGN_LEFT, 0, text, hdr_text);
	  ATTACH (seg);
	}
      if (!p)
	break;

      str = ++p;

      if (*str == '-')
	{
	  str++;
	  align = ALIGN_LEFT;
	}
      else if (*str == '+')
	{
	  str++;
	  align = ALIGN_RIGHT;
	}
      else
	align = ALIGN_UNDEF;
      
      if (mu_isdigit (*str))
	width = strtoul (str, (char**)&str, 10);
      else
	width = 0;

      switch (*str++)
	{
	case '%':
	  seg = new_header_segment (ALIGN_LEFT, 0, xstrdup ("%"), hdr_text);
	  break;
	  
	case 'a': /* Message attributes. */
	  seg = new_header_segment (ALIGN_STRING, width, NULL, hdr_attr);
	  break;

	  /* FIXME: %c    The score of the message. */
	  
	case 'd': /* Message date */
	  seg = new_header_segment (ALIGN_STRING, width, NULL, hdr_date);
	  break;
	  
	  /* FIXME: %e    The indenting level in threaded mode. */
	  
	case 'f': /* Message sender */
	  seg = new_header_segment (ALIGN_STRING, width, NULL, hdr_from);
	  break;

	  /* FIXME: %i    The message thread structure. */
	  
	case 'l': /* The number of lines of the message */
	  seg = new_header_segment (ALIGN_NUMBER, width, NULL, hdr_lines);
	  break;

	case 'L': /* Same, but in quick mode */
	  seg = new_header_segment (ALIGN_NUMBER, width, NULL,
				    hdr_quick_lines);
	  break;
	  
	case 'm': /* Message number */
	  seg = new_header_segment (ALIGN_NUMBER, width, NULL, hdr_number);
	  break;
	  
	case 'o': /* The number of octets (bytes) in the message */
	  seg = new_header_segment (ALIGN_NUMBER, width, NULL, hdr_size);
	  break;
	  
	case 's': /* Message subject (if any) */
	  seg = new_header_segment (ALIGN_STRING, width, NULL, hdr_subject);
	  break;

	case 'S': /* Message subject (if any) in double quotes */
	  seg = new_header_segment (ALIGN_STRING, width, NULL, hdr_q_subject);
	  break;
	  
	  /* FIXME: %t    The position in threaded/sorted order. */
	  
	case '>': /* A `>' for the current message, otherwise ` ' */
	  seg = new_header_segment (ALIGN_STRING, width, xstrdup (">"), hdr_cur);
	  break;
	  
	case '<': /* A `<' for the current message, otherwise ` ' */
	  seg = new_header_segment (ALIGN_STRING, width, xstrdup ("<"), hdr_cur);
	  break;

	default:
	  mu_error (_("unknown escape: %%%c"), str[-1]);
	  len = str - p;
	  text = xmalloc (len);
	  memcpy (text, p, len-1);
	  text[len-1] = 0;
	  seg = new_header_segment (ALIGN_STRING, width, text, hdr_text);
	}
      ATTACH (seg);
    }
  return head;
#undef ALIGN_STRING
#undef ALIGN_NUMBER
#undef ATTACH
}

/* FIXME: Should it be part of struct mailvar_variable for "headline"? */
static struct header_segm *mail_header_line;

void
mail_compile_headline (struct mailvar_variable *var)
{
  free_headline (mail_header_line);
  mail_header_line = compile_headline (var->value.string);
}


/*
 * f[rom] [msglist]
 */

int
mail_from0 (msgset_t *mspec, mu_message_t msg, void *data)
{
  format_headline (mail_header_line, mspec, msg);
  return 0;
}

int
mail_from (int argc, char **argv)
{
  return util_foreach_msg (argc, argv, MSG_NODELETED|MSG_SILENT,
			   mail_from0, NULL);
}