int main (int argc, char* argv[]) { if (argc < 2) { fprintf(stderr, "Usage: %s MSG_FILE\n", argv[0]); return 1; } g_mime_init(0); FILE* fp = fopen(argv[1], "r"); if (fp == NULL) { fprintf(stderr, "Cannot open the file %s\n", argv[1]); return 1; } GMimeStream* stream = g_mime_stream_file_new(fp); GMimeParser* parser = g_mime_parser_new_with_stream(stream); GMimeMessage* message = g_mime_parser_construct_message(parser); if (message == NULL) { fprintf(stderr, "Cannot construct message\n"); return 1; } printf("Date : %s\n", g_mime_message_get_date_as_string(message)); printf("Subject : %s\n", g_mime_message_get_subject(message)); printf("From : %s\n", g_mime_message_get_sender(message)); printf("To : %s\n", internet_address_list_to_string( g_mime_message_get_all_recipients(message), FALSE)); fclose(fp); return 0; }
static void msg_contact_foreach_file (MuMsg *msg, MuMsgContactForeachFunc func, gpointer user_data) { int i; struct { GMimeRecipientType _gmime_type; MuMsgContactType _type; } ctypes[] = { {GMIME_RECIPIENT_TYPE_TO, MU_MSG_CONTACT_TYPE_TO}, {GMIME_RECIPIENT_TYPE_CC, MU_MSG_CONTACT_TYPE_CC}, {GMIME_RECIPIENT_TYPE_BCC, MU_MSG_CONTACT_TYPE_BCC}, }; /* sender */ addresses_foreach (g_mime_message_get_sender (msg->_file->_mime_msg), MU_MSG_CONTACT_TYPE_FROM, func, user_data); /* reply_to */ addresses_foreach (g_mime_message_get_reply_to (msg->_file->_mime_msg), MU_MSG_CONTACT_TYPE_REPLY_TO, func, user_data); /* get to, cc, bcc */ for (i = 0; i != G_N_ELEMENTS(ctypes); ++i) { InternetAddressList *addrlist; addrlist = g_mime_message_get_recipients (msg->_file->_mime_msg, ctypes[i]._gmime_type); address_list_foreach (addrlist, ctypes[i]._type, func, user_data); } }
static void format_headers_message_part_json (GMimeMessage *message) { void *ctx = talloc_new (NULL); void *ctx_quote = talloc_new (ctx); InternetAddressList *recipients; const char *recipients_string; printf ("%s: %s", json_quote_str (ctx_quote, "From"), json_quote_str (ctx_quote, 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 (", %s: %s", json_quote_str (ctx_quote, "To"), json_quote_str (ctx_quote, 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 (", %s: %s", json_quote_str (ctx_quote, "Cc"), json_quote_str (ctx_quote, recipients_string)); printf (", %s: %s", json_quote_str (ctx_quote, "Subject"), json_quote_str (ctx_quote, g_mime_message_get_subject (message))); printf (", %s: %s", json_quote_str (ctx_quote, "Date"), json_quote_str (ctx_quote, g_mime_message_get_date_as_string (message))); talloc_free (ctx_quote); talloc_free (ctx); }
void format_headers_sprinter (sprinter_t *sp, GMimeMessage *message, notmuch_bool_t reply) { /* Any changes to the JSON or S-Expression format should be * reflected in the file devel/schemata. */ InternetAddressList *recipients; const char *recipients_string; const char *reply_to_string; sp->begin_map (sp); sp->map_key (sp, "Subject"); sp->string (sp, g_mime_message_get_subject (message)); sp->map_key (sp, "From"); sp->string (sp, 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) { sp->map_key (sp, "To"); sp->string (sp, 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) { sp->map_key (sp, "Cc"); sp->string (sp, recipients_string); } recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_BCC); recipients_string = internet_address_list_to_string (recipients, 0); if (recipients_string) { sp->map_key (sp, "Bcc"); sp->string (sp, recipients_string); } reply_to_string = g_mime_message_get_reply_to (message); if (reply_to_string) { sp->map_key (sp, "Reply-To"); sp->string (sp, reply_to_string); } if (reply) { sp->map_key (sp, "In-reply-to"); sp->string (sp, g_mime_object_get_header (GMIME_OBJECT (message), "In-reply-to")); sp->map_key (sp, "References"); sp->string (sp, g_mime_object_get_header (GMIME_OBJECT (message), "References")); } else { sp->map_key (sp, "Date"); sp->string (sp, g_mime_message_get_date_as_string (message)); } sp->end (sp); }
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)); }
void format_headers_json (const void *ctx, GMimeMessage *message, notmuch_bool_t reply) { void *local = talloc_new (ctx); InternetAddressList *recipients; const char *recipients_string; printf ("{%s: %s", json_quote_str (local, "Subject"), json_quote_str (local, g_mime_message_get_subject (message))); printf (", %s: %s", json_quote_str (local, "From"), json_quote_str (local, 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 (", %s: %s", json_quote_str (local, "To"), json_quote_str (local, 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 (", %s: %s", json_quote_str (local, "Cc"), json_quote_str (local, recipients_string)); if (reply) { printf (", %s: %s", json_quote_str (local, "In-reply-to"), json_quote_str (local, g_mime_object_get_header (GMIME_OBJECT (message), "In-reply-to"))); printf (", %s: %s", json_quote_str (local, "References"), json_quote_str (local, g_mime_object_get_header (GMIME_OBJECT (message), "References"))); } else { printf (", %s: %s", json_quote_str (local, "Date"), json_quote_str (local, g_mime_message_get_date_as_string (message))); } printf ("}"); talloc_free (local); }
void extract_headers (GMimeMessage *message, GMimeStream *out_stream) { const char *subject; char *escaped_subject; const char *sender; char *escaped_sender; const char *reply_to; char *escaped_reply_to; time_t t; int tz; char buf[64]; struct tm *t_m; subject = g_mime_message_get_subject(message); escaped_subject = str_replace((char *) subject, "\"", ""); g_mime_stream_printf (out_stream, "subject: \"%s\", ", escaped_subject); g_mime_stream_printf (out_stream, "message_id: \"%s\", ", g_mime_message_get_message_id(message)); sender = g_mime_message_get_sender(message); escaped_sender = str_replace((char *) sender, "\"", ""); g_mime_stream_printf (out_stream, "sender: \"%s\", ", escaped_sender); reply_to = g_mime_message_get_reply_to(message); escaped_reply_to = str_replace((char *) reply_to, "\"", ""); g_mime_stream_printf (out_stream, "reply_to: \"%s\", ", escaped_reply_to); g_mime_message_get_date (message, &t, &tz); t_m = localtime (&t); strftime(buf, sizeof(buf) - 1, "%c", t_m); g_mime_stream_printf (out_stream, "date: \"%s\", ", buf); strftime(buf, sizeof(buf) - 1, "%Y%m%d%H%M%S", t_m); g_mime_stream_printf (out_stream, "sort_date: \"%s\", ", buf); InternetAddressList *recipients; recipients = g_mime_message_get_recipients(message, GMIME_RECIPIENT_TYPE_TO); g_mime_stream_printf (out_stream, "recipients_to: ["); extract_addresses(recipients, out_stream); g_mime_stream_printf (out_stream, "], "); recipients = g_mime_message_get_recipients(message, GMIME_RECIPIENT_TYPE_CC); g_mime_stream_printf (out_stream, "recipients_cc: ["); extract_addresses(recipients, out_stream); g_mime_stream_printf (out_stream, "], "); recipients = g_mime_message_get_recipients(message, GMIME_RECIPIENT_TYPE_BCC); g_mime_stream_printf (out_stream, "recipients_bcc: ["); extract_addresses(recipients, out_stream); g_mime_stream_printf (out_stream, "] "); }
static void reply_headers_message_part (GMimeMessage *message) { 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)); }
static gboolean test_message (GMimeMessage *msg) { gchar *val; const gchar *str; g_print ("From : %s\n", g_mime_message_get_sender (msg)); val = get_recip (msg, GMIME_RECIPIENT_TYPE_TO); g_print ("To : %s\n", val ? val : "<none>" ); g_free (val); val = get_recip (msg, GMIME_RECIPIENT_TYPE_CC); g_print ("Cc : %s\n", val ? val : "<none>" ); g_free (val); val = get_recip (msg, GMIME_RECIPIENT_TYPE_BCC); g_print ("Bcc : %s\n", val ? val : "<none>" ); g_free (val); str = g_mime_message_get_subject (msg); g_print ("Subject: %s\n", str ? str : "<none>"); print_date (msg); str = g_mime_message_get_message_id (msg); g_print ("Msg-id : %s\n", str ? str : "<none>"); { gchar *refsstr; refsstr = get_refs_str (msg); g_print ("Refs : %s\n", refsstr ? refsstr : "<none>"); g_free (refsstr); } return TRUE; }
static void accumulate_text_message (MuMsg *msg, MuMsgPart *part, GString **gstrp) { const gchar *str; char *adrs; GMimeMessage *mimemsg; InternetAddressList *addresses; /* put sender, recipients and subject in the string, so they * can be indexed as well */ mimemsg = GMIME_MESSAGE (part->data); str = g_mime_message_get_sender (mimemsg); g_string_append_printf (*gstrp, "%s%s", str ? str : "", str ? "\n" : ""); str = g_mime_message_get_subject (mimemsg); g_string_append_printf (*gstrp, "%s%s", str ? str : "", str ? "\n" : ""); addresses = g_mime_message_get_all_recipients (mimemsg); adrs = internet_address_list_to_string (addresses, FALSE); g_object_unref (addresses); g_string_append_printf (*gstrp, "%s%s", adrs ? adrs : "", adrs ? "\n" : ""); g_free (adrs); }
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; }
document* parse_file(const char *file_name) { int tallied_length = 0; GMimeStream *stream; GMimeMessage *msg = 0; // struct stat stat_buf; const char *author, *subject, *xref, *xref_end; time_t date; int offset; int num_words = 0; int file; InternetAddress *iaddr; InternetAddressList *iaddr_list; char *address; #if DEBUG printf("%s\n", file_name); #endif /* if ((file = stat(file_name, &stat_buf)) == -1) { perror("tokenizer"); return NULL; } */ // |O_STREAMING if ((file = open(file_name, O_RDONLY|O_STREAMING)) == -1) { perror("tokenizer"); return NULL; } #ifdef POSIX_FADV_NOREUSE no_reuse(file); #endif stream = g_mime_stream_fs_new(file); msg = g_mime_parser_construct_message(stream); g_mime_stream_unref(stream); if (msg != 0) { table = g_hash_table_new(g_str_hash, g_str_equal); bufp = buffer; dword_table[0].word = NULL; bzero(saved_body, MAX_SAVED_BODY_LENGTH); saved_body_length = 0; author = g_mime_message_get_sender(msg); subject = g_mime_message_get_subject(msg); xref = g_mime_message_get_header(msg, "Xref"); g_mime_message_get_date(msg, &date, &offset); if (author != NULL && subject != NULL && xref != NULL) { tallied_length = tally_string(author, tallied_length); strncpy(doc.author, author, MAX_HEADER_LENGTH-1); /* Get the address from the From header. */ if ((iaddr_list = internet_address_parse_string(author)) != NULL) { iaddr = iaddr_list->address; internet_address_set_name(iaddr, NULL); address = internet_address_to_string(iaddr, FALSE); strncpy(doc.address, address, MAX_HEADER_LENGTH-1); downcase_string(doc.address); free(address); internet_address_list_destroy(iaddr_list); } else { *doc.address = 0; } tallied_length = tally_string(subject, tallied_length); strncpy(doc.subject, subject, MAX_HEADER_LENGTH-1); doc.time = date; if ((xref = strchr(xref, ' ')) != NULL) { xref++; xref_end = strchr(xref, ':'); *doc.group = 0; strncat(doc.group, xref, min(xref_end-xref, MAX_HEADER_LENGTH-1)); xref_end++; sscanf(xref_end, "%d", &doc.article); } g_mime_message_foreach_part(msg, partFound, (gpointer) &tallied_length); strncpy(doc.body, saved_body, MAX_SAVED_BODY_LENGTH); g_hash_table_foreach(table, add_word_to_table, (gpointer) &num_words); dword_table[num_words].word = NULL; g_hash_table_destroy(table); g_mime_object_unref(GMIME_OBJECT(msg)); } else { close(file); return NULL; } } close(file); doc.words = dword_table; doc.num_words = num_words; return &doc; }
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; }
static AddressesList *get_from_addresses(GMimeMessage *message) { const gchar *from_str = g_mime_message_get_sender(message); // transfer-none if (from_str) return collect_str_addresses(from_str); return NULL; }