Sieve: "stdin" working mode.
* sieve/sieve.c: New working mode: if `-f -' is given, treat stdin as an RFC2822 message, apply the script to it and exit with 1 if the message gets deleted, 0 if it is not and EX_SOFTWARE on software errors. In traditional mode, exit with standard sysexit codes. * testsuite/sieve/i-numeric.exp: Reflect changes in sieve. * NEWS: Update.
Showing
4 changed files
with
162 additions
and
93 deletions
1 | 2008-10-23 Sergey Poznyakoff <gray@gnu.org.ua> | ||
2 | |||
3 | * sieve/sieve.c: New working mode: if `-f -' is given, treat | ||
4 | stdin as an RFC2822 message, apply the script to it and exit with | ||
5 | 1 if the message gets deleted, 0 if it is not and EX_SOFTWARE on | ||
6 | software errors. | ||
7 | In traditional mode, exit with standard sysexit codes. | ||
8 | * testsuite/sieve/i-numeric.exp: Reflect changes in sieve. | ||
9 | * NEWS: Update. | ||
10 | |||
1 | 2008-10-19 Sergey Poznyakoff <gray@gnu.org.ua> | 11 | 2008-10-19 Sergey Poznyakoff <gray@gnu.org.ua> |
2 | 12 | ||
3 | * auth/sql.c (decode_tuple): Use new tuple parsing mode, unless | 13 | * auth/sql.c (decode_tuple): Use new tuple parsing mode, unless | ... | ... |
1 | GNU mailutils NEWS -- history of user-visible changes. 2008-10-16 | 1 | GNU mailutils NEWS -- history of user-visible changes. 2008-10-23 |
2 | Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, | 2 | Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, |
3 | 2008 Free Software Foundation, Inc. | 3 | 2008 Free Software Foundation, Inc. |
4 | See the end of file for copying conditions. | 4 | See the end of file for copying conditions. |
... | @@ -474,6 +474,14 @@ from the available data. | ... | @@ -474,6 +474,14 @@ from the available data. |
474 | New options `--bulletin-db' and `--bulletin-source' implement bulletin | 474 | New options `--bulletin-db' and `--bulletin-source' implement bulletin |
475 | facility. | 475 | facility. |
476 | 476 | ||
477 | * The `sieve' utility. | ||
478 | |||
479 | Exit codes conform to sysexits.h. | ||
480 | |||
481 | New working mode: if `-f -' is given, treat stdin as an RFC2822 | ||
482 | message, apply the script to it and exit with 1 if the message gets | ||
483 | deleted, 0 if it is not and EX_SOFTWARE on software errors. | ||
484 | |||
477 | * MH changes | 485 | * MH changes |
478 | 486 | ||
479 | ** sortm uses stable sort algorithm. | 487 | ** sortm uses stable sort algorithm. | ... | ... |
... | @@ -30,6 +30,7 @@ | ... | @@ -30,6 +30,7 @@ |
30 | #include <string.h> | 30 | #include <string.h> |
31 | #include <unistd.h> | 31 | #include <unistd.h> |
32 | #include <syslog.h> | 32 | #include <syslog.h> |
33 | #include <sysexits.h> | ||
33 | 34 | ||
34 | #include <mu_asprintf.h> | 35 | #include <mu_asprintf.h> |
35 | #include <mailutils/argcv.h> | 36 | #include <mailutils/argcv.h> |
... | @@ -366,108 +367,48 @@ _sieve_action_log (void *unused, | ... | @@ -366,108 +367,48 @@ _sieve_action_log (void *unused, |
366 | mu_debug_set_locus (debug, NULL, 0); | 367 | mu_debug_set_locus (debug, NULL, 0); |
367 | } | 368 | } |
368 | 369 | ||
369 | int | 370 | static int |
370 | main (int argc, char *argv[]) | 371 | sieve_message (mu_sieve_machine_t mach) |
371 | { | 372 | { |
372 | mu_sieve_machine_t mach; | ||
373 | mu_wicket_t wicket = 0; | ||
374 | mu_ticket_t ticket = 0; | ||
375 | mu_debug_t debug = 0; | ||
376 | mu_mailbox_t mbox = 0; | ||
377 | int rc; | 373 | int rc; |
374 | mu_stream_t instr; | ||
375 | mu_message_t msg; | ||
376 | mu_attribute_t attr; | ||
378 | 377 | ||
379 | /* Native Language Support */ | 378 | rc = mu_stdio_stream_create (&instr, stdin, 0); |
380 | MU_APP_INIT_NLS (); | ||
381 | |||
382 | mu_argp_init (program_version, NULL); | ||
383 | #ifdef WITH_TLS | ||
384 | mu_gocs_register ("tls", mu_tls_module_init); | ||
385 | #endif | ||
386 | mu_gocs_register ("sieve", mu_sieve_module_init); | ||
387 | |||
388 | mu_register_all_formats (); | ||
389 | |||
390 | tickets = mu_tilde_expansion ("~/.tickets", "/", NULL); | ||
391 | tickets_default = 1; | ||
392 | debug_level = MU_DEBUG_LEVEL_MASK (MU_DEBUG_ERROR); | ||
393 | mu_log_facility = 0; | ||
394 | |||
395 | if (mu_app_init (&argp, sieve_argp_capa, sieve_cfg_param, | ||
396 | argc, argv, ARGP_IN_ORDER, NULL, NULL)) | ||
397 | exit (1); | ||
398 | |||
399 | /* Sieve interpreter setup. */ | ||
400 | rc = mu_sieve_machine_init (&mach, NULL); | ||
401 | if (rc) | 379 | if (rc) |
402 | { | 380 | { |
403 | mu_error (_("Cannot initialize sieve machine: %s"), mu_strerror (rc)); | 381 | mu_error (_("Cannot create stream: %s"), mu_strerror (rc)); |
404 | return 1; | 382 | return EX_SOFTWARE; |
405 | } | ||
406 | |||
407 | if (mu_log_facility) | ||
408 | { | ||
409 | mu_debug_t debug; | ||
410 | |||
411 | mu_diag_get_debug (&debug); | ||
412 | openlog (MU_LOG_TAG (), LOG_PID, mu_log_facility); | ||
413 | mu_debug_set_print (debug, mu_diag_syslog_printer, NULL); | ||
414 | } | 383 | } |
415 | 384 | rc = mu_stream_open (instr); | |
416 | mu_sieve_set_debug (mach, _sieve_debug_printer); | ||
417 | if (verbose) | ||
418 | mu_sieve_set_logger (mach, _sieve_action_log); | ||
419 | |||
420 | rc = mu_sieve_compile (mach, script); | ||
421 | if (rc) | 385 | if (rc) |
422 | return 1; | ||
423 | |||
424 | /* We can finish if its only a compilation check. */ | ||
425 | if (compile_only) | ||
426 | { | 386 | { |
427 | if (compile_only == 2) | 387 | mu_error (_("Cannot open stream: %s"), mu_strerror (rc)); |
428 | mu_sieve_disass (mach); | 388 | return EX_SOFTWARE; |
429 | return 0; | ||
430 | } | ||
431 | |||
432 | /* Create a ticket, if we can. */ | ||
433 | if (tickets) | ||
434 | { | ||
435 | if ((rc = mu_wicket_create (&wicket, tickets)) == 0) | ||
436 | { | ||
437 | if ((rc = mu_wicket_get_ticket (wicket, &ticket, 0, 0)) != 0) | ||
438 | { | ||
439 | mu_error (_("ticket_get failed: %s"), mu_strerror (rc)); | ||
440 | goto cleanup; | ||
441 | } | ||
442 | } | ||
443 | else if (!(tickets_default && errno == ENOENT)) | ||
444 | { | ||
445 | mu_error (_("mu_wicket_create `%s' failed: %s"), | ||
446 | tickets, mu_strerror (rc)); | ||
447 | goto cleanup; | ||
448 | } | ||
449 | if (ticket) | ||
450 | mu_sieve_set_ticket (mach, ticket); | ||
451 | } | 389 | } |
452 | 390 | rc = mu_stream_to_message (instr, &msg); | |
453 | /* Create a debug object, if needed. */ | 391 | if (rc) |
454 | if (debug_level) | ||
455 | { | ||
456 | if ((rc = mu_debug_create (&debug, mach))) | ||
457 | { | ||
458 | mu_error (_("mu_debug_create failed: %s"), mu_strerror (rc)); | ||
459 | goto cleanup; | ||
460 | } | ||
461 | if ((rc = mu_debug_set_level (debug, debug_level))) | ||
462 | { | 392 | { |
463 | mu_error (_("mu_debug_set_level failed: %s"), | 393 | mu_error (_("Cannot create message from stream: %s"), |
464 | mu_strerror (rc)); | 394 | mu_strerror (rc)); |
465 | goto cleanup; | 395 | return EX_SOFTWARE; |
466 | } | ||
467 | mu_sieve_set_debug_object (mach, debug); | ||
468 | } | 396 | } |
397 | mu_message_get_attribute (msg, &attr); | ||
398 | mu_attribute_unset_deleted (attr); | ||
399 | rc = mu_sieve_message (mach, msg); | ||
400 | if (rc) | ||
401 | /* FIXME: switch (rc)...*/ | ||
402 | return EX_SOFTWARE; | ||
469 | 403 | ||
470 | mu_sieve_set_debug_level (mach, sieve_debug); | 404 | return mu_attribute_is_deleted (attr) ? 1 : EX_OK; |
405 | } | ||
406 | |||
407 | static int | ||
408 | sieve_mailbox (mu_sieve_machine_t mach, mu_ticket_t ticket, mu_debug_t debug) | ||
409 | { | ||
410 | int rc; | ||
411 | mu_mailbox_t mbox = NULL; | ||
471 | 412 | ||
472 | /* Create, give a ticket to, and open the mailbox. */ | 413 | /* Create, give a ticket to, and open the mailbox. */ |
473 | if ((rc = mu_mailbox_create_default (&mbox, mbox_url)) != 0) | 414 | if ((rc = mu_mailbox_create_default (&mbox, mbox_url)) != 0) |
... | @@ -536,7 +477,7 @@ main (int argc, char *argv[]) | ... | @@ -536,7 +477,7 @@ main (int argc, char *argv[]) |
536 | /* Process the mailbox */ | 477 | /* Process the mailbox */ |
537 | rc = mu_sieve_mailbox (mach, mbox); | 478 | rc = mu_sieve_mailbox (mach, mbox); |
538 | 479 | ||
539 | cleanup: | 480 | cleanup: |
540 | if (mbox && !(sieve_debug & MU_SIEVE_DRY_RUN)) | 481 | if (mbox && !(sieve_debug & MU_SIEVE_DRY_RUN)) |
541 | { | 482 | { |
542 | int e; | 483 | int e; |
... | @@ -562,8 +503,118 @@ cleanup: | ... | @@ -562,8 +503,118 @@ cleanup: |
562 | mu_sieve_machine_destroy (&mach); | 503 | mu_sieve_machine_destroy (&mach); |
563 | mu_mailbox_close (mbox); | 504 | mu_mailbox_close (mbox); |
564 | mu_mailbox_destroy (&mbox); | 505 | mu_mailbox_destroy (&mbox); |
506 | /* FIXME: switch (rc) ... */ | ||
507 | return rc ? EX_SOFTWARE : EX_OK; | ||
508 | } | ||
509 | |||
510 | int | ||
511 | main (int argc, char *argv[]) | ||
512 | { | ||
513 | mu_sieve_machine_t mach; | ||
514 | mu_wicket_t wicket = 0; | ||
515 | mu_ticket_t ticket = 0; | ||
516 | mu_debug_t debug = 0; | ||
517 | int rc; | ||
518 | |||
519 | /* Native Language Support */ | ||
520 | MU_APP_INIT_NLS (); | ||
521 | |||
522 | mu_argp_init (program_version, NULL); | ||
523 | #ifdef WITH_TLS | ||
524 | mu_gocs_register ("tls", mu_tls_module_init); | ||
525 | #endif | ||
526 | mu_gocs_register ("sieve", mu_sieve_module_init); | ||
527 | |||
528 | mu_register_all_formats (); | ||
529 | |||
530 | tickets = mu_tilde_expansion ("~/.tickets", "/", NULL); | ||
531 | tickets_default = 1; | ||
532 | debug_level = MU_DEBUG_LEVEL_MASK (MU_DEBUG_ERROR); | ||
533 | mu_log_facility = 0; | ||
534 | |||
535 | if (mu_app_init (&argp, sieve_argp_capa, sieve_cfg_param, | ||
536 | argc, argv, ARGP_IN_ORDER, NULL, NULL)) | ||
537 | exit (EX_USAGE); | ||
538 | |||
539 | /* Sieve interpreter setup. */ | ||
540 | rc = mu_sieve_machine_init (&mach, NULL); | ||
541 | if (rc) | ||
542 | { | ||
543 | mu_error (_("Cannot initialize sieve machine: %s"), mu_strerror (rc)); | ||
544 | return EX_SOFTWARE; | ||
545 | } | ||
546 | |||
547 | if (mu_log_facility) | ||
548 | { | ||
549 | mu_debug_t debug; | ||
550 | |||
551 | mu_diag_get_debug (&debug); | ||
552 | openlog (MU_LOG_TAG (), LOG_PID, mu_log_facility); | ||
553 | mu_debug_set_print (debug, mu_diag_syslog_printer, NULL); | ||
554 | } | ||
555 | |||
556 | mu_sieve_set_debug (mach, _sieve_debug_printer); | ||
557 | if (verbose) | ||
558 | mu_sieve_set_logger (mach, _sieve_action_log); | ||
559 | |||
560 | rc = mu_sieve_compile (mach, script); | ||
561 | if (rc) | ||
562 | return EX_CONFIG; | ||
563 | |||
564 | /* We can finish if its only a compilation check. */ | ||
565 | if (compile_only) | ||
566 | { | ||
567 | if (compile_only == 2) | ||
568 | mu_sieve_disass (mach); | ||
569 | return EX_OK; | ||
570 | } | ||
571 | |||
572 | /* Create a ticket, if we can. */ | ||
573 | if (tickets) | ||
574 | { | ||
575 | if ((rc = mu_wicket_create (&wicket, tickets)) == 0) | ||
576 | { | ||
577 | if ((rc = mu_wicket_get_ticket (wicket, &ticket, 0, 0)) != 0) | ||
578 | { | ||
579 | mu_error (_("ticket_get failed: %s"), mu_strerror (rc)); | ||
580 | return EX_SOFTWARE; /* FIXME: really? */ | ||
581 | } | ||
582 | } | ||
583 | else if (!(tickets_default && errno == ENOENT)) | ||
584 | { | ||
585 | mu_error (_("mu_wicket_create `%s' failed: %s"), | ||
586 | tickets, mu_strerror (rc)); | ||
587 | return EX_SOFTWARE; | ||
588 | } | ||
589 | if (ticket) | ||
590 | mu_sieve_set_ticket (mach, ticket); | ||
591 | } | ||
592 | |||
593 | /* Create a debug object, if needed. */ | ||
594 | if (debug_level) | ||
595 | { | ||
596 | if ((rc = mu_debug_create (&debug, mach))) | ||
597 | { | ||
598 | mu_error (_("mu_debug_create failed: %s"), mu_strerror (rc)); | ||
599 | return EX_SOFTWARE; | ||
600 | } | ||
601 | if ((rc = mu_debug_set_level (debug, debug_level))) | ||
602 | { | ||
603 | mu_error (_("mu_debug_set_level failed: %s"), | ||
604 | mu_strerror (rc)); | ||
605 | return EX_SOFTWARE; | ||
606 | } | ||
607 | mu_sieve_set_debug_object (mach, debug); | ||
608 | } | ||
609 | |||
610 | mu_sieve_set_debug_level (mach, sieve_debug); | ||
611 | |||
612 | if (mbox_url && strcmp (mbox_url, "-") == 0) | ||
613 | rc = sieve_message (mach); | ||
614 | else | ||
615 | rc = sieve_mailbox (mach, ticket, debug); | ||
565 | mu_debug_destroy (&debug, mach); | 616 | mu_debug_destroy (&debug, mach); |
566 | 617 | ||
567 | return rc ? 1 : 0; | 618 | return rc; |
568 | } | 619 | } |
569 | 620 | ... | ... |
... | @@ -22,7 +22,7 @@ sieve_test i-numeric-is.sv -pattern \ | ... | @@ -22,7 +22,7 @@ sieve_test i-numeric-is.sv -pattern \ |
22 | "DISCARD on msg uid 2: marking as deleted"\ | 22 | "DISCARD on msg uid 2: marking as deleted"\ |
23 | "IMPLICIT KEEP on msg uid 3" | 23 | "IMPLICIT KEEP on msg uid 3" |
24 | 24 | ||
25 | sieve_test i-numeric-contains.sv -retcode 1 -pattern \ | 25 | sieve_test i-numeric-contains.sv -retcode 78 -pattern \ |
26 | -re ".*i-numeric-contains.sv:9: comparator `i;ascii-numeric' is incompatible with match type `contains' in call to `header'" | 26 | -re ".*i-numeric-contains.sv:9: comparator `i;ascii-numeric' is incompatible with match type `contains' in call to `header'" |
27 | 27 | ||
28 | # end of i-numeric.exp | 28 | # end of i-numeric.exp | ... | ... |
-
Please register or sign in to post a comment