mh_msgset.c 14.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 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 1999-2001, 2005-2007, 2009-2012, 2014-2016 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/>. */

/* MH message sets. */

#include <mh.h>
#include <mailutils/sys/msgset.h>

size_t
mh_msgset_first (mu_msgset_t msgset)
{
  mu_list_t list;
  struct mu_msgrange *r;
  int rc;
  
  rc = mu_msgset_get_list (msgset, &list);
  if (rc)
    {
      mu_diag_funcall (MU_DIAG_ERROR, "mu_msgset_get_list", NULL, rc);
      exit (1);
    }
  rc = mu_list_head (list, (void**)&r);
  if (rc)
    {
      mu_diag_funcall (MU_DIAG_ERROR, "mu_list_get", NULL, rc);
      exit (1);
    }
  return r->msg_beg;
}

size_t
mh_msgset_first_uid (mu_msgset_t msgset)
{
  int rc;
  size_t cur;

  cur = mh_msgset_first (msgset);
  rc = mu_mailbox_translate (msgset->mbox, MU_MAILBOX_MSGNO_TO_UID, cur, &cur);
  if (rc)
    {
      mu_diag_funcall (MU_DIAG_ERROR, "mu_mailbox_translate", NULL, rc);
      exit (1);
    }
  return cur;
}

/* Set the current message to that contained at position 0
   in the given message set.
   FIXME: mbox is superfluous
*/
void
mh_msgset_first_current (mu_mailbox_t mbox, mu_msgset_t msgset)
{
  mh_mailbox_set_cur (mbox, mh_msgset_first_uid (msgset));
}

int
mh_msgset_single_message (mu_msgset_t msgset)
{
  int rc;
  mu_list_t list;
  struct mu_msgrange *r;
  size_t count;
  
  rc = mu_msgset_get_list (msgset, &list);
  if (rc)
    {
      mu_diag_funcall (MU_DIAG_ERROR, "mu_msgset_get_list", NULL, rc);
      exit (1);
    }
  rc = mu_list_count (list, &count);
  if (rc)
    {
      mu_diag_funcall (MU_DIAG_ERROR, "mu_list_count", NULL, rc);
      exit (1);
    }
  if (count != 1)
    return 0;
  rc = mu_list_head (list, (void**)&r);
  if (rc)
    {
      mu_diag_funcall (MU_DIAG_ERROR, "mu_list_get", NULL, rc);
      exit (1);
    }
  return r->msg_beg == r->msg_end;
}

struct msgset_parser
{
  mu_msgset_t msgset;
  char *curp;
  int argc;
  char **argv;

  int sign;
  size_t number;
  int validuid;
};

static void
msgset_parser_init (struct msgset_parser *parser, mu_mailbox_t mbox,
		    int argc, char **argv)
{
  int rc;
  
  rc = mu_msgset_create (&parser->msgset, mbox, MU_MSGSET_NUM);//FIXME: flags?
  if (rc)
    {
      mu_diag_funcall (MU_DIAG_ERROR, "mu_msgset_create", NULL, rc);
      exit (1);
    }
  parser->argc = argc;
  parser->argv = argv;
  parser->curp = "";

  parser->sign = 0;
  parser->number = 0;
}

static void
msgset_abort (const char *arg)
{
  mu_error (_("bad message list `%s'"), arg);
  exit (1);
}

static void
emptyrange_abort (const char *range)
{
  mu_error (_("no messages in range %s"), range);
  exit (1);
}

/* Advance parser to the next argument */
static int
nextarg (struct msgset_parser *parser)
{
  if (parser->argc == 0)
    return 0;
  parser->argc--;
  parser->curp = *parser->argv++;
  return 1;
}

static void msgset_parser_run (struct msgset_parser *parser);

static int
_expand_sequence (struct msgset_parser *parser, char *term)
{
  struct mu_wordsplit ws;
  const char *listp;
  int negate = 0;

  listp = mh_global_sequences_get (parser->msgset->mbox, term, NULL);
  if (!listp)
    {
      int len;
      const char *neg = mh_global_profile_get ("Sequence-Negation", NULL);
      if (!neg)
	return 1;
      len = strlen (neg);
      if (strncmp (term, neg, len))
	return 1;
      negate = 1;
      listp = mh_global_sequences_get (parser->msgset->mbox, term + len, NULL);
      if (!listp)
	return 1;
    }

  if (mu_wordsplit (listp, &ws, MU_WRDSF_DEFFLAGS))
    {
      mu_error (_("cannot split line `%s': %s"), listp,
		mu_wordsplit_strerror (&ws));
      exit (1);
    }
  else
    {
      int rc;
      struct msgset_parser clone;

      msgset_parser_init (&clone, parser->msgset->mbox,
			  ws.ws_wordc, ws.ws_wordv);
      msgset_parser_run (&clone);
      mu_wordsplit_free (&ws);
      if (negate)
	{
	  mu_msgset_t negset;
	  
	  rc = mu_msgset_negate (clone.msgset, &negset);
	  if (rc)
	    {
	      mu_diag_funcall (MU_DIAG_ERROR, "mu_msgset_negate", NULL, rc);
	      exit (1);
	    }
	  mu_msgset_free (clone.msgset);
	  clone.msgset = negset;
	}
      rc = mu_msgset_add (parser->msgset, clone.msgset);
      if (rc)
	{
	  mu_diag_funcall (MU_DIAG_ERROR, "mu_msgset_add", NULL, rc);
	  exit (1);
	}
      mu_msgset_free (clone.msgset);
    }
  
  return 0;
}

static int
parse_count (struct msgset_parser *parser)
{
  char *endp;
  if (!*parser->curp && nextarg (parser) == 0)
    return 0;
  if (*parser->curp == '-')
    {
      parser->sign = 1;
      parser->curp++;
    }
  else if (*parser->curp == '+')
    {
      parser->sign = 0;
      parser->curp++;
    }
  parser->number = strtoul (parser->curp, &endp, 10);
  if (*endp)
    msgset_abort (parser->curp);
  parser->curp = endp;
  return 1;
}


static int
msgset_first (mu_mailbox_t mbox, size_t *pnum)
{
  *pnum = 1;
  return 0;
}

static int
msgset_last (mu_mailbox_t mbox, size_t *pnum)
{
  int rc;
  
  rc = mu_mailbox_messages_count (mbox, pnum);
  if (rc)
    {
      mu_error (_("cannot get last message: %s"), mu_strerror (rc));
      exit (1);
    }
  return 0;
}

static int
msgset_cur (mu_mailbox_t mbox, size_t *pnum)
{
  size_t num;
  mh_mailbox_get_cur (mbox, &num);
  return mu_mailbox_translate (mbox, MU_MAILBOX_UID_TO_MSGNO, num, pnum);
}

static int
msgset_prev (mu_mailbox_t mbox, size_t *pnum)
{
  size_t cur_n = 0;
  msgset_cur (mbox, &cur_n);
  if (cur_n < 1)
    {
      mu_error (_("no prev message"));
      exit (1);
    }
  *pnum = cur_n - 1;
  return 0;
}

static int
msgset_next (mu_mailbox_t mbox, size_t *pnum)
{
  size_t cur_n = 0, total = 0;
  msgset_cur (mbox, &cur_n);
  mu_mailbox_messages_count (mbox, &total);
  if (cur_n + 1 > total)
    {
      mu_error (_("no next message"));
      exit (1);
    }
  *pnum = cur_n + 1;
  return 0;
}

struct msgset_keyword
{
  char *name;
  size_t len;
  int (*handler) (mu_mailbox_t mbox, size_t *pnum);
  int sign;
};

static struct msgset_keyword keywords[] = {
#define S(s) #s, sizeof (#s) - 1
  { S(first), msgset_first, 0 },
  { S(last), msgset_last, 1 },
  { S(prev), msgset_prev, 1 },
  { S(next), msgset_next, 0 },
  { S(cur), msgset_cur, 0 },
  { NULL }
};

#define PARSE_EOF  0
#define PARSE_MORE 1
#define PARSE_SUCCESS 2

/* term : NUMBER
        | "first"
	| "last"
	| "cur"
	| "prev"
	| "next"
	;
*/
static int
parse_term (struct msgset_parser *parser, int seq)
{
  size_t tlen;
  char *term;
  
  if (!*parser->curp && nextarg (parser) == 0)
    return PARSE_EOF;

  term = parser->curp;
  parser->curp = mu_str_skip_class (term, MU_CTYPE_ALPHA|MU_CTYPE_DIGIT);
  tlen = parser->curp - term;
  if (mu_isalpha (*term))
    {
      struct msgset_keyword *p;

      for (p = keywords; p->name; p++)
	if (tlen == p->len && memcmp (p->name, term, tlen) == 0)
	  {
	    size_t num;
	    
	    if (p->handler (parser->msgset->mbox, &num))
	      msgset_abort (term);
	    parser->number = num;
	    parser->sign = p->sign;
	    parser->validuid = 1;
	    return PARSE_MORE;
	  }

      if (*parser->curp == 0 && seq)
	{
	  /* See if it is a user-defined sequence */
	  if (_expand_sequence (parser, term) == 0)
	    return PARSE_SUCCESS;
	}
      msgset_abort (term);
    }
  else if (mu_isdigit (*term))
    {
      char *endp;
      size_t num = strtoul (term, &endp, 10);
      if (endp != parser->curp)
	msgset_abort (term);
      
      if (mu_mailbox_translate (parser->msgset->mbox,
				MU_MAILBOX_UID_TO_MSGNO,
				num, &parser->number))
	{
	  parser->validuid = 0;
	  parser->number = num;
	}
      else
	parser->validuid = 1;
      parser->sign = 0;
    }
  else
    msgset_abort (term);
  return PARSE_MORE;
}

static void
add_messages (struct msgset_parser *parser, size_t start, size_t count,
	      int sign)
{
  int rc;

  if (start == 0)
    start = 1;
  if (sign)
    {
      if (count > start)
	count = start;
      rc = mu_msgset_add_range (parser->msgset, start, start - count + 1,
				MU_MSGSET_NUM);
    }
  else
    {
      size_t total;
  
      mu_mailbox_messages_count (parser->msgset->mbox, &total);
      if (start + count > total)
	{
	  count = total - start + 1;
	  if (count == 0)
	    emptyrange_abort (parser->argv[-1]);
	}
      rc = mu_msgset_add_range (parser->msgset, start, start + count - 1,
				MU_MSGSET_NUM);
    }

  if (rc)
    {
      mu_diag_funcall (MU_DIAG_ERROR, "mu_msgset_add_range", NULL, rc);
      exit (1);
    }
}

/* range: term '-' term
        | term ':' count
	;
   count: NUMBER
        | '+' NUMBER
	| '-' NUMBER
	;
*/	
static int
parse_range (struct msgset_parser *parser)
{
  size_t start;

  switch (parse_term (parser, 1))
    {
    case PARSE_EOF:
      return 0;

    case PARSE_SUCCESS:
      return 1;

    case PARSE_MORE:
      break;
    }
      
  start = parser->number;

  if (*parser->curp == ':')
    {
      int validuid = parser->validuid;
      parser->curp++;
      if (parse_count (parser) == 0)
	return 0;
      if (!validuid)
	{
	  if (parser->sign)
	    {
	      while (1)
		{
		  if (--start == 0)
		    emptyrange_abort (parser->argv[-1]);
		  if (mu_mailbox_translate (parser->msgset->mbox,
					    MU_MAILBOX_UID_TO_MSGNO,
					    start, &start) == 0)
		    break;
		}
	    }
	  else
	    {
	      size_t total, lastuid;
	  
	      msgset_last (parser->msgset->mbox, &total);
	      mu_mailbox_translate (parser->msgset->mbox,
				    MU_MAILBOX_MSGNO_TO_UID,
				    total, &lastuid);
	      if (start > lastuid)
		emptyrange_abort (parser->argv[-1]);
	      else
		while (1)
		  {
		    if (start == lastuid)
		      {
			start = total;
			break;
		      }
		    ++start;
		    if (mu_mailbox_translate (parser->msgset->mbox,
					      MU_MAILBOX_UID_TO_MSGNO,
					      start, &start) == 0)
		      break;
		  }
	    }
	}
      add_messages (parser, start, parser->number, parser->sign);
      return 1;
    }
  else if (*parser->curp == '-')
    {
      int validuid = parser->validuid;
      int rc;
      
      parser->curp++;
      if (parse_term (parser, 0) == PARSE_EOF)
	return 0;

      /* If any of UIDs does not exist, try to find the nearest
	 existing one: */
      if (!validuid || !parser->validuid)
	{
	  size_t lastuid, num, maxnum;

	  /* Order the limits */
	  if (!parser->validuid)
	    maxnum = parser->number;
	  else
	    mu_mailbox_translate (parser->msgset->mbox,
				  MU_MAILBOX_MSGNO_TO_UID,
				  parser->number, &maxnum);
	  if (!validuid)
	    num = start;
	  else
	    mu_mailbox_translate (parser->msgset->mbox,
				  MU_MAILBOX_MSGNO_TO_UID,
				  start, &num);

	  if (num > maxnum)
	    {
	      int v;
	      size_t n;

	      n = parser->number;
	      v = parser->validuid;
	      parser->number = start;
	      parser->validuid = validuid;
	      start = n;
	      validuid = v;
	    }

	  /* Adjust upper bound */
	  msgset_last (parser->msgset->mbox, &num);
	  mu_mailbox_translate (parser->msgset->mbox, MU_MAILBOX_MSGNO_TO_UID,
				num, &lastuid);

	  if (!parser->validuid && parser->number > lastuid)
	    {
	      parser->number = num;
	      parser->validuid = 1;
	    }

	  /* Shift the bounds towards each other until they resolve to
	     existing UIDs or clash */
	  do
	    {
	      if (!validuid)
		{
		  ++start;
		  if (start > lastuid)
		    emptyrange_abort (parser->argv[-1]);
		  rc = mu_mailbox_translate (parser->msgset->mbox,
					     MU_MAILBOX_UID_TO_MSGNO,
					     start, &start);
		  if (rc == 0)
		    validuid = 1;
		}
	      if (!parser->validuid)
		{
		  if (parser->number == 1)
		    emptyrange_abort (parser->argv[-1]);
		  --parser->number;
		  rc = mu_mailbox_translate (parser->msgset->mbox,
					     MU_MAILBOX_UID_TO_MSGNO,
					     parser->number, &num);
		  if (rc == 0)
		    {
		      lastuid = parser->number;
		      parser->number = num;
		      parser->validuid = 1;
		    }
		}
	    }
	  while (!validuid || !parser->validuid);
	}
      mu_msgset_add_range (parser->msgset, start, parser->number,
			   MU_MSGSET_NUM);
    }
  else if (!parser->validuid)
    {
      mu_error (_("message %s does not exist"), parser->argv[-1]);
      exit (1);
    }
  else
    mu_msgset_add_range (parser->msgset, start, start, MU_MSGSET_NUM);
  return 1;
}
  
  
/* Parse a message specification saved in a configured PARSER. */
static void
msgset_parser_run (struct msgset_parser *parser)
{
  while (parse_range (parser))
    ;
}

/* Parse a message specification from (argc;argv).  */
void
mh_msgset_parse (mu_msgset_t *msgset, mu_mailbox_t mbox, 
		 int argc, char **argv, char *def)
{
  struct msgset_parser parser;
  char *xargv[2];
  
  if (argc == 0)
    {
      argc = 1;
      argv = xargv;
      argv[0] = def ? def : "cur";
      argv[1] = NULL;
    }

  if (argc == 1 &&
      (strcmp (argv[0], "all") == 0 || strcmp (argv[0], ".") == 0))
    {
      argc = 1;
      argv = xargv;
      argv[0] = "first-last";
      argv[1] = NULL;
    }
  
  msgset_parser_init (&parser, mbox, argc, argv);
  msgset_parser_run (&parser);
  *msgset = parser.msgset;
}

void
mh_msgset_parse_string (mu_msgset_t *msgset, mu_mailbox_t mbox, 
			const char *string, char *def)
{
  struct mu_wordsplit ws;
  
  if (mu_wordsplit (string, &ws, MU_WRDSF_DEFFLAGS))
    {
      mu_error (_("cannot split line `%s': %s"), string,
		mu_wordsplit_strerror (&ws));
      exit (1);
    }
  mh_msgset_parse (msgset, mbox, ws.ws_wordc, ws.ws_wordv, def);
  mu_wordsplit_free (&ws);
}


/* Retrieve the message with the given sequence number.
   Returns ordinal number of the message in the mailbox if found,
   zero otherwise. The retrieved message is stored in the location
   pointed to by mesg, unless it is NULL. */
   
size_t
mh_get_message (mu_mailbox_t mbox, size_t seqno, mu_message_t *mesg)
{
  int rc;
  size_t num;

  if (mu_mailbox_translate (mbox, MU_MAILBOX_UID_TO_MSGNO, seqno, &num))
    return 0;
  if (mesg)
    {
      rc = mu_mailbox_get_message (mbox, num, mesg);
      if (rc)
	{
	  mu_diag_funcall (MU_DIAG_ERROR, "mu_mailbox_get_message", NULL, rc);
	  exit (1);
	}
    }
  return num;
}