Re: [gmime-devel] Need code snippet for MIME decoding



Hi Jeff,

I am facing one more issue with this program. In this program datatype of variable "name" is const char *. Once we access the file name in to this variable we are opening a file with the same name on the current directory and dumping the content. "name = g_mime_part_get_filename ((GMimePart *) part);" and fd=open(name,....)

 If I want to change the location in which I should get the content what needs to be done? Reason I am asking is I already wrote the program to define my own location and append this file name with that location so that I can open this file on that location. But by accessing the variable "name" it is giving segmentation fault. Even if I do a strlen(name) after the line "name = g_mime_part_get_filename ((GMimePart *) part); , it is giving segmentation fault. Why I am not able to do operations with this variable? But I am able to open file with this name. Can you please help....

Thanks
Prasad.


On Fri, Jan 25, 2013 at 9:01 AM, Jeffrey Stedfast <fejj gnome org> wrote:
Hi Prasad,

I'm not in front of my Linux machine, so the following code snippet might not work, but this is roughly how you'd do what you want:

int extract_pdfs_from_message (const char *filename)
{
    GMimeContentType *content_type;
    GMimeDataWrapper *content;
    GMimeMessage *message;
    GMimeStream *stream;
    GMimeParser *parser;
    GMimePartIter *iter;
    GMimeObject *part;
    const char *name;
    int pdfs = 0;
    int fd;

    /* open the message file */
    if ((fd = open (filename, O_RDONLY)) == -1)
        return -1;

    /* create a parser for the message stream */
    stream = g_mime_stream_fs_new (fd);
    parser = g_mime_parser_new_with_stream (stream);
    g_object_unref (stream);

    /* parse the message */
    message = g_mime_parser_construct_message (parser);
    g_object_unref (parser);

    /* iterate over each part to find all application/pdf's */
    iter = g_mime_part_iter_new ((GMimeObject *) message);
    g_object_unref (message);

    do {
        part = g_mime_part_iter_get_current (iter);
        content_type = g_mime_object_get_content_type (part);
        if (GMIME_IS_PART (part) && g_mime_content_type_is_type (content_type, "application", "pdf")) {
            /* save the pdf file */
            name = g_mime_part_get_filename ((GMimePart *) part);
            if ((fd = open (name, O_CREAT | O_WRONLY, 0666)) != -1) {
                content = g_mime_part_get_content_object ((GMimePart *) part);
                stream = g_mime_stream_fs_new (fd);

                /* this decodes the raw content stream and writes it to another stream */
                if (g_mime_data_wrapper_write_to_stream (stream) != -1) {
                    g_mime_stream_flush (stream);
                } else {
                    /* error writing to the stream... */
                }

                g_object_unref (stream);
            } else {
                /* error opening the output file */
            }

            pdfs++;
        } else {
            /* these are not the drones you are looking for... */
        }
    } while (g_mime_part_iter_next (iter));

    g_object_unref (iter);

    return pdfs;

}

On 1/24/2013 11:05 AM, prasad antony wrote:
Hi Jeff,

First of all thank you so much for your fast reply. Really appreciate it.

I have tried with test-parser.c and some other programs available in the tests and examples directories. But it didn't help me to achieve what I am looking for, may be because of my lack of knowledge in this area. I am getting the stream again in the same format (encrypted) after parsing though gmime parser.

I have one attachment as a pdf file (test.pdf) in the mail with some simple text mail message and I can see that pdf is encrypted in the raw format of mail stream what I have once I captured it. Is there anyway to get the test.pdf file back to use it for further processing using some gmime APIs. Will you be able to help me with some more deeper information about how to do that?

If this is not the way how we are getting the attachment for further processing, please help me how to proceed with that?

Thanks
Prasad.


On Wed, Jan 23, 2013 at 5:25 AM, Jeffrey Stedfast <fejj gnome org> wrote:
Hi Prasad,

There are some sample programs included in the gmime source code that show you how to do what you want (look in the examples directory and/or look at tests/test-parser.c).

Functions like g_mime_message_get_subject() return UTF-8.

GMime also includes functions such as g_mime_iconv*() which will convert between charsets if you need them.

Hope that helps,

Jeff

On 1/22/2013 5:38 AM, Prasad Antony M wrote:
Hi,

I have a complete mail in raw format.
Mail can contain multiple attachments too.
In the mail either headers or message body/attachment
or both will be in encrypted format (usually base64).

Does anyone has code snippet to decode
this complete mail contents where ever is
required using gmime,
and give each and every mail contents (subject, From, To etc
and actual message and attachments)
in separate variables (in case of headers) or
arrays (in case of message body and attachments),
those I can use to process further.

I would like to get message body and attachments
in separate arrays.
Also is there any code which is handling
the UTF-8 character sets too?

Thanks In Advance
Prasad Antony

_______________________________________________
gmime-devel-list mailing list
gmime-devel-list gnome org
https://mail.gnome.org/mailman/listinfo/gmime-devel-list







[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]