gboolean mu_msg_part_save (MuMsg *msg, MuMsgOptions opts, const char *fullpath, guint partidx, GError **err) { GMimeObject *part; g_return_val_if_fail (msg, FALSE); g_return_val_if_fail (fullpath, FALSE); g_return_val_if_fail (!((opts & MU_MSG_OPTION_OVERWRITE) && (opts & MU_MSG_OPTION_USE_EXISTING)), FALSE); if (!mu_msg_load_msg_file (msg, err)) return FALSE; part = get_mime_object_at_index (msg, opts, partidx); /* special case: convert a message-part into a message */ if (GMIME_IS_MESSAGE_PART (part)) part = (GMimeObject*)g_mime_message_part_get_message (GMIME_MESSAGE_PART (part)); if (!GMIME_IS_PART(part) && !GMIME_IS_MESSAGE(part)) { g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_GMIME, "unexpected type %s for part %u", G_OBJECT_TYPE_NAME((GObject*)part), partidx); return FALSE; } return save_object (part, opts, fullpath, err); }
static void accumulate_text (MuMsg *msg, MuMsgPart *part, GString **gstrp) { if (GMIME_IS_MESSAGE(part->data)) accumulate_text_message (msg, part, gstrp); else if (GMIME_IS_PART (part->data)) accumulate_text_part (msg, part, gstrp); }
static void interpret_header(GSList** stack, gchar** args) { GMimeObject* o = GMIME_OBJECT((*stack)->data); if ( 0 == g_strcmp0("Date", args[0]) && GMIME_IS_MESSAGE(o) ) { g_mime_message_set_date_as_string(GMIME_MESSAGE(o), g_strchug(args[1])); } else { g_mime_object_set_header(o, args[0], g_strchug(args[1])); } }
static void format_part_reply (mime_node_t *node) { int i; if (node->envelope_file) { printf ("On %s, %s wrote:\n", notmuch_message_get_header (node->envelope_file, "date"), notmuch_message_get_header (node->envelope_file, "from")); } else if (GMIME_IS_MESSAGE (node->part)) { GMimeMessage *message = GMIME_MESSAGE (node->part); InternetAddressList *recipients; const char *recipients_string; printf ("> From: %s\n", g_mime_message_get_sender (message)); recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO); recipients_string = internet_address_list_to_string (recipients, 0); if (recipients_string) printf ("> To: %s\n", recipients_string); recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC); recipients_string = internet_address_list_to_string (recipients, 0); if (recipients_string) printf ("> Cc: %s\n", recipients_string); printf ("> Subject: %s\n", g_mime_message_get_subject (message)); printf ("> Date: %s\n", g_mime_message_get_date_as_string (message)); printf (">\n"); } else if (GMIME_IS_PART (node->part)) { GMimeContentType *content_type = g_mime_object_get_content_type (node->part); GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (node->part); if (g_mime_content_type_is_type (content_type, "application", "pgp-encrypted") || g_mime_content_type_is_type (content_type, "application", "pgp-signature")) { /* Ignore PGP/MIME cruft parts */ } else if (g_mime_content_type_is_type (content_type, "text", "*") && !g_mime_content_type_is_type (content_type, "text", "html")) { GMimeStream *stream_stdout = g_mime_stream_file_new (stdout); g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE); show_text_part_content (node->part, stream_stdout, NOTMUCH_SHOW_TEXT_PART_REPLY); g_object_unref(stream_stdout); } else if (disposition && strcasecmp (g_mime_content_disposition_get_disposition (disposition), GMIME_DISPOSITION_ATTACHMENT) == 0) { const char *filename = g_mime_part_get_filename (GMIME_PART (node->part)); printf ("Attachment: %s (%s)\n", filename, g_mime_content_type_to_string (content_type)); } else { printf ("Non-text part: %s\n", g_mime_content_type_to_string (content_type)); } } for (i = 0; i < node->nchildren; i++) format_part_reply (mime_node_child (node, i)); }
static void add_part(GSList** stack, GMimeObject* p) { GMimeObject* top_part = GMIME_OBJECT((*stack)->data); if ( GMIME_IS_MESSAGE(top_part) ) { GMimeMessage* m = GMIME_MESSAGE(top_part); GMimeObject* msg_part = g_mime_message_get_mime_part(m); GMimeObject* add_part = p; if ( msg_part ) { add_part = add_part_to_existing(msg_part, p); } g_mime_message_set_mime_part(m, add_part); } else { (*stack)->data = add_part_to_existing(top_part, p); } }
static gboolean is_spam (GMimeMessage *mime_message) { const char *spam; g_return_val_if_fail(GMIME_IS_MESSAGE(mime_message), FALSE); /* SpamAssassin */ spam = g_mime_message_get_header(mime_message, "X-Spam-Status"); if (spam && mn_ascii_str_case_has_prefix(spam, "yes")) return TRUE; /* bogofilter */ spam = g_mime_message_get_header(mime_message, "X-Bogosity"); if (spam && mn_ascii_str_case_has_prefix(spam, "yes")) return TRUE; return FALSE; }
char* mu_msg_part_get_text (MuMsg *msg, MuMsgPart *self, MuMsgOptions opts) { GMimeObject *mobj; GMimeMessage *mime_msg; gboolean err; g_return_val_if_fail (msg, NULL); g_return_val_if_fail (self && self->data, NULL); mobj = (GMimeObject*)self->data; err = FALSE; if (GMIME_IS_PART (mobj)) { if (self->part_type & MU_MSG_PART_TYPE_TEXT_PLAIN) return mu_msg_mime_part_to_string ((GMimePart*)mobj, &err); else return NULL; /* non-text MimePart */ } mime_msg = NULL; if (GMIME_IS_MESSAGE_PART (mobj)) mime_msg = g_mime_message_part_get_message ((GMimeMessagePart*)mobj); else if (GMIME_IS_MESSAGE (mobj)) mime_msg = (GMimeMessage*)mobj; if (mime_msg) { GString *gstr; gstr = g_string_sized_new (4096); handle_children (msg, mime_msg, opts, self->index, (MuMsgPartForeachFunc)accumulate_text, &gstr); return g_string_free (gstr, FALSE); } else { g_warning ("%s: cannot get text for %s", __FUNCTION__, G_OBJECT_TYPE_NAME (mobj)); return NULL; } }
int remove_part(GMimeObject *parent, GMimeObject *part) { int attachment = is_attachment(part); if (attachment && GMIME_IS_MULTIPART(parent)) { g_mime_multipart_remove((GMimeMultipart*)parent, part); return 1; } else if (attachment && GMIME_IS_MESSAGE(parent)) { // I'm not sure if this could ever occur or even makes sense. GMimePart *new_part; new_part = g_mime_part_new_with_type("text", "plain"); g_mime_part_set_content_object(new_part, g_mime_data_wrapper_new_with_stream(g_mime_stream_mem_new_with_buffer("Attachment Removed.", 19), GMIME_CONTENT_ENCODING_DEFAULT)); g_mime_message_set_mime_part((GMimeMessage*)parent, (GMimeObject*)new_part); g_object_unref(part); } else if (GMIME_IS_MESSAGE_PART(part)) { remove_message(g_mime_message_part_get_message((GMimeMessagePart*)part)); } return 0; }
static void fold_to(GSList** stack, guint level) { guint min_level = level ? level : 1; while ( g_slist_length(*stack) > min_level ) { /* pop and add to top */ GSList* top = *stack; GMimeObject* part = GMIME_OBJECT(top->data); if ( GMIME_IS_MESSAGE(part) ) { GMimeMessagePart* mgp = g_mime_message_part_new_with_message( "rfc822", GMIME_MESSAGE(part)); part = GMIME_OBJECT(mgp); } *stack = g_slist_remove_link(*stack, top); add_part(stack, part); g_slist_free(top); } }
char* mu_msg_part_get_text (MuMsg *msg, MuMsgPart *self, MuMsgOptions opts) { GMimeObject *mobj; GMimeMessage *mime_msg; gboolean err; g_return_val_if_fail (msg, NULL); g_return_val_if_fail (self && GMIME_IS_OBJECT(self->data), NULL); mobj = (GMimeObject*)self->data; err = FALSE; if (GMIME_IS_PART (mobj)) { if (self->part_type & MU_MSG_PART_TYPE_TEXT_PLAIN) return mu_msg_mime_part_to_string ((GMimePart*)mobj, &err); else return NULL; /* non-text MimePart */ } mime_msg = NULL; if (GMIME_IS_MESSAGE_PART (mobj)) mime_msg = g_mime_message_part_get_message ((GMimeMessagePart*)mobj); else if (GMIME_IS_MESSAGE (mobj)) mime_msg = (GMimeMessage*)mobj; /* apparently, g_mime_message_part_get_message may still * return NULL */ if (mime_msg) return get_text_from_mime_msg (msg, mime_msg, opts); return NULL; }
void format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first) { /* Any changes to the JSON format should be reflected in the file * devel/schemata. */ if (node->envelope_file) { printf ("{"); format_message_json (ctx, node->envelope_file); printf ("\"headers\": "); format_headers_json (ctx, GMIME_MESSAGE (node->part), FALSE); printf (", \"body\": ["); format_part_json (ctx, mime_node_child (node, 0), first); printf ("]}"); return; } void *local = talloc_new (ctx); /* The disposition and content-type metadata are associated with * the envelope for message parts */ GMimeObject *meta = node->envelope_part ? GMIME_OBJECT (node->envelope_part) : node->part; GMimeContentType *content_type = g_mime_object_get_content_type (meta); const char *cid = g_mime_object_get_content_id (meta); const char *filename = GMIME_IS_PART (node->part) ? g_mime_part_get_filename (GMIME_PART (node->part)) : NULL; const char *terminator = ""; int i; if (!first) printf (", "); printf ("{\"id\": %d", node->part_num); if (node->decrypt_attempted) printf (", \"encstatus\": [{\"status\": \"%s\"}]", node->decrypt_success ? "good" : "bad"); if (node->verify_attempted) { printf (", \"sigstatus\": "); format_part_sigstatus_json (node); } printf (", \"content-type\": %s", json_quote_str (local, g_mime_content_type_to_string (content_type))); if (cid) printf (", \"content-id\": %s", json_quote_str (local, cid)); if (filename) printf (", \"filename\": %s", json_quote_str (local, filename)); if (GMIME_IS_PART (node->part)) { /* For non-HTML text parts, we include the content in the * JSON. Since JSON must be Unicode, we handle charset * decoding here and do not report a charset to the caller. * For text/html parts, we do not include the content. If a * caller is interested in text/html parts, it should retrieve * them separately and they will not be decoded. Since this * makes charset decoding the responsibility on the caller, we * report the charset for text/html parts. */ if (g_mime_content_type_is_type (content_type, "text", "html")) { const char *content_charset = g_mime_object_get_content_type_parameter (meta, "charset"); if (content_charset != NULL) printf (", \"content-charset\": %s", json_quote_str (local, content_charset)); } else if (g_mime_content_type_is_type (content_type, "text", "*")) { GMimeStream *stream_memory = g_mime_stream_mem_new (); GByteArray *part_content; show_text_part_content (node->part, stream_memory, 0); part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory)); printf (", \"content\": %s", json_quote_chararray (local, (char *) part_content->data, part_content->len)); g_object_unref (stream_memory); } } else if (GMIME_IS_MULTIPART (node->part)) { printf (", \"content\": ["); terminator = "]"; } else if (GMIME_IS_MESSAGE (node->part)) { printf (", \"content\": [{"); printf ("\"headers\": "); format_headers_json (local, GMIME_MESSAGE (node->part), FALSE); printf (", \"body\": ["); terminator = "]}]"; } talloc_free (local); for (i = 0; i < node->nchildren; i++) format_part_json (ctx, mime_node_child (node, i), i == 0); printf ("%s}", terminator); }
static notmuch_status_t format_part_text (const void *ctx, mime_node_t *node, int indent, const notmuch_show_params_t *params) { /* The disposition and content-type metadata are associated with * the envelope for message parts */ GMimeObject *meta = node->envelope_part ? GMIME_OBJECT (node->envelope_part) : node->part; GMimeContentType *content_type = g_mime_object_get_content_type (meta); const notmuch_bool_t leaf = GMIME_IS_PART (node->part); const char *part_type; int i; if (node->envelope_file) { notmuch_message_t *message = node->envelope_file; part_type = "message"; printf ("\f%s{ id:%s depth:%d match:%d excluded:%d filename:%s\n", part_type, notmuch_message_get_message_id (message), indent, notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? 1 : 0, notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED) ? 1 : 0, notmuch_message_get_filename (message)); } else { GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (meta); const char *cid = g_mime_object_get_content_id (meta); const char *filename = leaf ? g_mime_part_get_filename (GMIME_PART (node->part)) : NULL; if (disposition && strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0) part_type = "attachment"; else part_type = "part"; printf ("\f%s{ ID: %d", part_type, node->part_num); if (filename) printf (", Filename: %s", filename); if (cid) printf (", Content-id: %s", cid); printf (", Content-type: %s\n", g_mime_content_type_to_string (content_type)); } if (GMIME_IS_MESSAGE (node->part)) { GMimeMessage *message = GMIME_MESSAGE (node->part); InternetAddressList *recipients; const char *recipients_string; printf ("\fheader{\n"); if (node->envelope_file) printf ("%s\n", _get_one_line_summary (ctx, node->envelope_file)); printf ("Subject: %s\n", g_mime_message_get_subject (message)); printf ("From: %s\n", g_mime_message_get_sender (message)); recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO); recipients_string = internet_address_list_to_string (recipients, 0); if (recipients_string) printf ("To: %s\n", recipients_string); recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC); recipients_string = internet_address_list_to_string (recipients, 0); if (recipients_string) printf ("Cc: %s\n", recipients_string); printf ("Date: %s\n", g_mime_message_get_date_as_string (message)); printf ("\fheader}\n"); printf ("\fbody{\n"); } if (leaf) { if (g_mime_content_type_is_type (content_type, "text", "*") && !g_mime_content_type_is_type (content_type, "text", "html")) { GMimeStream *stream_stdout = g_mime_stream_file_new (stdout); g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE); show_text_part_content (node->part, stream_stdout, 0); g_object_unref(stream_stdout); } else { printf ("Non-text part: %s\n", g_mime_content_type_to_string (content_type)); } } for (i = 0; i < node->nchildren; i++) format_part_text (ctx, mime_node_child (node, i), indent, params); if (GMIME_IS_MESSAGE (node->part)) printf ("\fbody}\n"); printf ("\f%s}\n", part_type); return NOTMUCH_STATUS_SUCCESS; }
MNMessage * mn_message_new_from_mime_message_full (GType type, MNMailbox *mailbox, GMimeMessage *mime_message, const char *mid, const char *uri, MNMessageFlags flags, gboolean handle_status) { MNMessage *message; const char *id; time_t sent_time; const char *from; const char *subject; char *decoded_from; char *decoded_subject; g_return_val_if_fail(type != 0, NULL); g_return_val_if_fail(MN_IS_MAILBOX(mailbox), NULL); g_return_val_if_fail(GMIME_IS_MESSAGE(mime_message), NULL); if (is_spam(mime_message)) return NULL; if (handle_status) { const char *status; status = g_mime_message_get_header(mime_message, "Status"); if (status && strchr(status, 'R')) return NULL; /* the message was read */ else if (status && strchr(status, 'O')) flags &= ~MN_MESSAGE_NEW; else flags |= MN_MESSAGE_NEW; } id = g_mime_message_get_message_id(mime_message); g_mime_message_get_date(mime_message, &sent_time, NULL); from = g_mime_message_get_sender(mime_message); subject = g_mime_message_get_subject(mime_message); decoded_from = from ? g_mime_utils_header_decode_text(from) : NULL; decoded_subject = subject ? g_mime_utils_header_decode_text(subject) : NULL; message = g_object_new(type, MN_MESSAGE_PROP_MAILBOX(mailbox), MN_MESSAGE_PROP_SENT_TIME(sent_time), MN_MESSAGE_PROP_ID((char *) id), MN_MESSAGE_PROP_MID((char *) mid), MN_MESSAGE_PROP_FROM(decoded_from), MN_MESSAGE_PROP_SUBJECT(decoded_subject), MN_MESSAGE_PROP_URI((char *) uri), MN_MESSAGE_PROP_FLAGS(flags), NULL); g_free(decoded_from); g_free(decoded_subject); return message; }
void format_part_sprinter (const void *ctx, sprinter_t *sp, mime_node_t *node, notmuch_bool_t first, notmuch_bool_t output_body, notmuch_bool_t include_html) { /* Any changes to the JSON or S-Expression format should be * reflected in the file devel/schemata. */ if (node->envelope_file) { sp->begin_map (sp); format_message_sprinter (sp, node->envelope_file); sp->map_key (sp, "headers"); format_headers_sprinter (sp, GMIME_MESSAGE (node->part), FALSE); if (output_body) { sp->map_key (sp, "body"); sp->begin_list (sp); format_part_sprinter (ctx, sp, mime_node_child (node, 0), first, TRUE, include_html); sp->end (sp); } sp->end (sp); return; } /* The disposition and content-type metadata are associated with * the envelope for message parts */ GMimeObject *meta = node->envelope_part ? GMIME_OBJECT (node->envelope_part) : node->part; GMimeContentType *content_type = g_mime_object_get_content_type (meta); const char *cid = g_mime_object_get_content_id (meta); const char *filename = GMIME_IS_PART (node->part) ? g_mime_part_get_filename (GMIME_PART (node->part)) : NULL; int nclose = 0; int i; sp->begin_map (sp); sp->map_key (sp, "id"); sp->integer (sp, node->part_num); if (node->decrypt_attempted) { sp->map_key (sp, "encstatus"); sp->begin_list (sp); sp->begin_map (sp); sp->map_key (sp, "status"); sp->string (sp, node->decrypt_success ? "good" : "bad"); sp->end (sp); sp->end (sp); } if (node->verify_attempted) { sp->map_key (sp, "sigstatus"); format_part_sigstatus_sprinter (sp, node); } sp->map_key (sp, "content-type"); sp->string (sp, g_mime_content_type_to_string (content_type)); if (cid) { sp->map_key (sp, "content-id"); sp->string (sp, cid); } if (filename) { sp->map_key (sp, "filename"); sp->string (sp, filename); } if (GMIME_IS_PART (node->part)) { /* For non-HTML text parts, we include the content in the * JSON. Since JSON must be Unicode, we handle charset * decoding here and do not report a charset to the caller. * For text/html parts, we do not include the content unless * the --include-html option has been passed. If a html part * is not included, it can be requested directly. This makes * charset decoding the responsibility on the caller so we * report the charset for text/html parts. */ if (g_mime_content_type_is_type (content_type, "text", "*") && (include_html || ! g_mime_content_type_is_type (content_type, "text", "html"))) { GMimeStream *stream_memory = g_mime_stream_mem_new (); GByteArray *part_content; show_text_part_content (node->part, stream_memory, 0); part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory)); sp->map_key (sp, "content"); sp->string_len (sp, (char *) part_content->data, part_content->len); g_object_unref (stream_memory); } else { format_omitted_part_meta_sprinter (sp, meta, GMIME_PART (node->part)); } } else if (GMIME_IS_MULTIPART (node->part)) { sp->map_key (sp, "content"); sp->begin_list (sp); nclose = 1; } else if (GMIME_IS_MESSAGE (node->part)) { sp->map_key (sp, "content"); sp->begin_list (sp); sp->begin_map (sp); sp->map_key (sp, "headers"); format_headers_sprinter (sp, GMIME_MESSAGE (node->part), FALSE); sp->map_key (sp, "body"); sp->begin_list (sp); nclose = 3; } for (i = 0; i < node->nchildren; i++) format_part_sprinter (ctx, sp, mime_node_child (node, i), i == 0, TRUE, include_html); /* Close content structures */ for (i = 0; i < nclose; i++) sp->end (sp); /* Close part map */ sp->end (sp); }