[gmime-devel] Using gmime to Remove Attachments



Hello everyone.

I'm trying to write a short program that removes the attachments from an e-mail message(read from stdin) and prints the message(to stdout).  Below is what I have so far.  Unfortunately, it doesn't always remove all the attachments.  I've traced the problem down to something having to do with g_mime_multipart_remove.  I've verified that is_attachment is true for all the attachments in my test cases.  When I put in some additional fprintf's for debugging it looks like remove_each isn't even looking at all the doesn't look at all the mime parts.  However, if I comment out the g_mime_multipart_remove, it looks like remove_attach does get run on all the mime parts.  Does anyone know why it doesn't always remove all of the attachments? Thanks!

I'm using atrpms build of 2.4.7 on CentOS5.5: gmime-2.4.7-1.99.el5.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <regex.h>
#include <gmime/gmime.h>

int is_attachment(GMimeObject *part) {
  if (g_mime_object_get_content_disposition_parameter(part, "filename") ||
      g_mime_object_get_content_type_parameter(part, "name"))
    return 1;
  else
    return 0;
}

void remove_each(GMimeObject *parent, GMimeObject *part, gpointer data) {
  
    if (is_attachment(part) && GMIME_IS_MULTIPART(parent))
      // g_object_unref(part);
      g_mime_multipart_remove((GMimeMultipart*)parent, part);
  else if (GMIME_IS_MESSAGE_PART(part))
    g_mime_message_foreach(g_mime_message_part_get_message((GMimeMessagePart*)part),
                           (GMimeObjectForeachFunc)remove_each, NULL);

}


int main(int argc, char **argv) {
  GMimeStream *stream;
  GMimeParser *parse;
  GMimeMessage *message;
  
  g_mime_init(0);
  
  stream = g_mime_stream_file_new (stdin);
  parse = g_mime_parser_new_with_stream(stream);
  message = g_mime_parser_construct_message(parse);
  
  g_mime_message_foreach(message, (GMimeObjectForeachFunc)remove_each, NULL);
  
  printf(g_mime_object_to_string((GMimeObject*)message));
  
  g_object_unref(message);
  g_object_unref(parse);
  g_object_unref (stream);

  return 0;
}

- Bill


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