/*------------------------------------------------------------------ * sieve_eng_test.c * This program tests the sieve engine. * * The program expects as input a mailbox location and a file containing * a sieve script. For each message in the mailbox, it creates a new * instance of a sieve machine, compiles the script and passes the message * in the engine. ------------------------------------------------------------------*/ #include #include #include #include #include "mailutils/mailutils.h" #define MU_DEBUG_LEVEL (MU_DEBUG_ERROR|MU_DEBUG_TRACE|MU_DEBUG_PROT|MU_SIEVE_DEBUG_TRACE) #define SIEVE_DEBUG_LEVEL 0 int main(int argc, char* argv[]) { char *from; char *subject; mu_mailbox_t mbox; size_t msgno, total = 0; int status; mu_sieve_machine_t mach; mu_debug_t debug = NULL; /* Register the formats. */ /* This supports mbox, maildir and mh */ mu_register_local_mbox_formats(); /* mu_register_all_mbox_formats (); */ /* Register the mailer formats */ mu_register_all_mailer_formats(); status = mu_mailbox_create(&mbox, argv[1]); if (status != 0) { mu_error ("mailbox_create: %s", mu_strerror (status)); exit (EXIT_FAILURE); } status = mu_mailbox_open(mbox, MU_STREAM_READ); if (status != 0) { mu_error ("mailbox_open: %s", mu_strerror (status)); exit (EXIT_FAILURE); } mu_mailbox_messages_count (mbox, &total); for (msgno = 1; msgno <= total; msgno++) { mu_message_t msg; mu_header_t hdr; mu_attribute_t attr; if ((status = mu_mailbox_get_message (mbox, msgno, &msg)) != 0 || (status = mu_message_get_header (msg, &hdr)) != 0) { mu_error ("Error message: %s", mu_strerror (status)); exit (EXIT_FAILURE); } mu_message_get_attribute (msg, &attr); mu_attribute_unset_deleted (attr); if (mu_header_aget_value (hdr, MU_HEADER_FROM, &from)) from = strdup ("(NO FROM)"); if (mu_header_aget_value (hdr, MU_HEADER_SUBJECT, &subject)) subject = strdup ("(NO SUBJECT)"); /* Create the instance of the sieve machine */ status = mu_sieve_machine_init(&mach,NULL); if (status !=0) { mu_error ("Cannot initialize sieve machine: %s", mu_strerror (status)); exit (EXIT_FAILURE);; } if (getenv ("MU_DEBUG")) { if ((status = mu_debug_create (&debug, mach))) { mu_error ("mu_debug_create: %s", mu_strerror (status)); abort (); } if ((status = mu_debug_set_level (debug, MU_DEBUG_LEVEL))) { mu_error ("mu_debug_set_level: %s", mu_strerror (status)); abort (); } mu_sieve_set_debug_level (mach, debug, SIEVE_DEBUG_LEVEL); } status = mu_sieve_compile(mach,argv[2]); if (status !=0) { mu_error ("Error compile sieve script: %s", mu_strerror (status)); exit (EXIT_FAILURE);; } status = mu_sieve_message(mach,msg); if (status != 0) mu_error ("Error sieve_message: %s", mu_strerror (status)); else status = mu_attribute_is_deleted (attr) == 0; mu_sieve_machine_destroy(&mach); printf ("\nFrom: %s\n Subject: %s\n Sieve flag: %d\n", from, subject,status); free (from); free (subject); } status = mu_mailbox_close (mbox); if (status != 0) { mu_error ("mailbox_close: %s", mu_strerror (status)); exit (EXIT_FAILURE); } mu_mailbox_destroy (&mbox); return 0; }