Package: libmailutils4
Version: 1:2.99.97-3
Severity: normal
Tags: upstream

Dear Maintainer,

I'm writing a program using the mailutils library and have the following
sequence of commands:

1) Iterate through all messages in a mailbox with using
   mu_mailbox_messages_count() and mu_mailbox_get_message(),
   do some stuff but do NOT call mu_message_get_body()

2) Iterate through the messages a second time, same as above but
   DO retrieve the body this time.

Step 2 will for most (not all) messages result in a zero size message
body when you call mu_message_is_multipart() before retrieving the body.
There are two possible workarounds, first is not to call
mu_message_is_multipart(), second is to retrieve the body during step 1
(just calling mu_body_size() on it is enough).

I will attach a short sample program to illustrate the problem.

Best regards,
Andre

-- System Information:
Debian Release: wheezy/sid
  APT prefers unstable
  APT policy: (500, 'unstable'), (1, 'experimental')
Architecture: i386 (i686)

Kernel: Linux 3.2.0-4-686-pae (SMP w/1 CPU core)
Locale: LANG=de_DE.UTF-8, LC_CTYPE=de_DE.UTF-8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/dash

Versions of packages libmailutils4 depends on:
ii  guile-1.8-libs    1.8.8+1-8
ii  libc6             2.13-37
ii  libgcrypt11       1.5.0-3
ii  libgdbm3          1.8.3-11
ii  libgmp10          2:5.0.5+dfsg-2
ii  libgnutls26       2.12.20-2
ii  libgsasl7         1.8.0-2
ii  libldap-2.4-2     2.4.31-1
ii  libltdl7          2.4.2-1.2
ii  libmysqlclient18  5.5.28+dfsg-1
ii  libpam0g          1.1.3-7.1
ii  libpython2.7      2.7.3-5
ii  mailutils-common  1:2.99.97-3

libmailutils4 recommends no packages.

libmailutils4 suggests no packages.

-- no debconf information
/* Compile this with: 
 * gcc -ggdb -O0 -o mailtest mailtest.c -l mailutils -lmu_pop -lmu_imap -lmu_maildir -lmu_mh -lmu_auth -lmu_mbox
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <mailutils/mailutils.h>

void showMessages( mu_mailbox_t *mbox, int withBody, int withMultipartCheck )
{
    size_t msgno, total = 0;
    char *from;
    char *subject;
    int status;

    mu_mailbox_messages_count (*mbox, &total);
    for (msgno = 1; msgno <= total; msgno++) {
        mu_message_t msg;
        mu_header_t hdr;

        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);
        }
        printf ("msg no.: %2u, ", msgno);
        if( withBody ) {
            mu_body_t body;
            size_t bodySize;
            int isMultipart;
            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);
            }
            if( withMultipartCheck ) {
                status = mu_message_is_multipart( msg, &isMultipart );
                if( 0 != status ) {
                    fprintf( stderr, "failed to check multipart property: %s",
                        mu_strerror( status ) );
                }
            }
            // get message body
            status = mu_message_get_body( msg, &body );
            if( 0 != status ) {
                fprintf(stderr, "failed to get message body: %s", mu_strerror( status ) );
            }
            // get size of body
            status = mu_body_size( body, &bodySize );
            if( 0 != status ) {
                fprintf( stderr, "failed to get message body size: %s", mu_strerror( status ) );
            }
            printf( "body size: %d\n", bodySize );
        } else {
            printf( "not requesting body\n" );
        }
    }
}

int main (int argc, const char **argv)
{
    mu_mailbox_t mbox;
    int status;
    int provokeError = 0;

    if( argc < 2 ) {
        printf( "call this as:\n"
            "%s [imap-url] [-provokeError]\n\n", argv[0] );
        exit( EXIT_SUCCESS );
    }

    /* Register the formats. */
    mu_register_all_mbox_formats ();

    status = mu_mailbox_create_default (&mbox, argv[1]);
    if (status != 0) {
        mu_error ("mu_mailbox_create with imap url '%s' returned: %s", argv[1], mu_strerror (status));
        exit (EXIT_FAILURE);
    }
    status = mu_mailbox_open (mbox, MU_STREAM_READ);
    if (status != 0) {
        mu_error ("mu_mailbox_open: %s", mu_strerror (status));
        exit (EXIT_FAILURE);
    }

    if( argc > 2 && 0 == strcmp( "-provokeError", argv[2] ) ) {
        printf( "provoking the error by calling mu_message_is_multipart() before requesting the body in second iteration\n" );
        provokeError = 1;
    }
    printf("=======> first iteration, without requesting message body\n" );
    showMessages( &mbox, 0, 0 );
    printf("=======> second iteration, with request of message body\n" );
    printf("         will%s do the mu_message_is_multipart() check\n", provokeError?"":" NOT" );
    showMessages( &mbox, 1, provokeError );

    status = mu_mailbox_close (mbox);
    if (status != 0) {
        mu_error ("mu_mailbox_close: %s", mu_strerror (status));
        exit (EXIT_FAILURE);
    }
    mu_mailbox_destroy (&mbox);
    return 0;
}

Reply via email to