/*------------------------------------------------------------------ * testsieve.c * This program tests the functionality of the mailutils libraries, * specifically libmailbox and libsieve. * * The program expects as input a mailbox location and a file containing * a sieve script. It reads and runs the sieve script for each message of * the mailbox in turn. ------------------------------------------------------------------*/ #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; mailbox_t mbox; size_t msgno, total = 0; int status; 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(); /* Create the instance of the sieve machine */ status = 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 (); } sieve_set_debug_level (mach, debug, SIEVE_DEBUG_LEVEL); } status = sieve_compile(mach,argv[2]); if (status !=0) { mu_error ("Error compile sieve script: %s", mu_strerror (status)); exit (EXIT_FAILURE);; } status = mailbox_create(&mbox, argv[1]); if (status != 0) { mu_error ("mailbox_create: %s", mu_strerror (status)); exit (EXIT_FAILURE); } status = mailbox_open(mbox, MU_STREAM_READ); if (status != 0) { mu_error ("mailbox_open: %s", mu_strerror (status)); exit (EXIT_FAILURE); } mailbox_messages_count (mbox, &total); for (msgno = 1; msgno <= total; msgno++) { message_t msg; header_t hdr; attribute_t attr; if ((status = mailbox_get_message (mbox, msgno, &msg)) != 0 || (status = message_get_header (msg, &hdr)) != 0) { mu_error ("Error message: %s", mu_strerror (status)); exit (EXIT_FAILURE); } message_get_attribute (msg, &attr); attribute_unset_deleted (attr); if (header_aget_value (hdr, MU_HEADER_FROM, &from)) from = strdup ("(NO FROM)"); if (header_aget_value (hdr, MU_HEADER_SUBJECT, &subject)) subject = strdup ("(NO SUBJECT)"); status = sieve_message(mach,msg); if (status != 0) mu_error ("Error sieve_message: %s", mu_strerror (status)); else status = attribute_is_deleted (attr) == 0; printf ("\nFrom: %s\n Subject: %s\n Sieve flag: %d\n", from, subject,status); free (from); free (subject); } status = mailbox_close (mbox); if (status != 0) { mu_error ("mailbox_close: %s", mu_strerror (status)); exit (EXIT_FAILURE); } sieve_machine_destroy(&mach); mailbox_destroy (&mbox); return 0; }