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); if (!GMIME_IS_PART(part) || GMIME_IS_MESSAGE_PART(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); }
gchar* mu_msg_part_get_path (MuMsg *msg, MuMsgOptions opts, const char* targetdir, unsigned index, GError **err) { char *fname, *filepath; GMimeObject* mobj; g_return_val_if_fail (msg, NULL); if (!mu_msg_load_msg_file (msg, NULL)) return NULL; mobj = get_mime_object_at_index (msg, opts, index); if (!mobj){ mu_util_g_set_error (err, MU_ERROR_GMIME, "cannot find part %u", index); return NULL; } fname = mime_part_get_filename (mobj, index, TRUE); filepath = g_build_path (G_DIR_SEPARATOR_S, targetdir ? targetdir : "", fname, NULL); g_free (fname); return filepath; }
gchar* mu_msg_part_get_cache_path (MuMsg *msg, MuMsgOptions opts, guint partid, GError **err) { char *dirname, *filepath; const char* path; g_return_val_if_fail (msg, NULL); if (!mu_msg_load_msg_file (msg, NULL)) return NULL; path = mu_msg_get_path (msg); /* g_compute_checksum_for_string may be better, but requires * rel. new glib (2.16) */ dirname = g_strdup_printf ("%s%c%x%c%u", mu_util_cache_dir(), G_DIR_SEPARATOR, g_str_hash (path), G_DIR_SEPARATOR, partid); if (!mu_util_create_dir_maybe (dirname, 0700, FALSE)) { mu_util_g_set_error (err, MU_ERROR_FILE, "failed to create dir %s", dirname); g_free (dirname); return NULL; } filepath = mu_msg_part_get_path (msg, opts, dirname, partid, err); g_free (dirname); return filepath; }
static const char* get_str_field (MuMsg *self, MuMsgFieldId mfid) { char *val; gboolean do_free; do_free = TRUE; val = NULL; if (self->_doc && mu_msg_field_xapian_value (mfid)) val = mu_msg_doc_get_str_field (self->_doc, mfid); else if (mu_msg_field_gmime (mfid)) { /* if we don't have a file object yet, we need to * create it from the file on disk */ if (!mu_msg_load_msg_file (self, NULL)) return NULL; val = mu_msg_file_get_str_field (self->_file, mfid, &do_free); } else { g_warning ("%s: cannot retrieve field", __FUNCTION__); val = NULL; } return do_free ? free_later_str (self, val) : val; }
static void /* ie., parts that require opening the message file */ add_file_parts (JsonBuilder *bob, MuMsg *msg, MuMsgOptions opts) { const char *str; GError *err; err = NULL; if (!mu_msg_load_msg_file (msg, &err)) { g_warning ("failed to load message file: %s", err ? err->message : "some error occured"); g_clear_error (&err); return; } add_parts (bob, msg, opts); add_contacts (bob, msg); /* add the user-agent / x-mailer */ str = mu_msg_get_header (msg, "User-Agent"); if (!str) str = mu_msg_get_header (msg, "X-Mailer"); add_string_member (bob, "user-agent", str); add_body_txt_params (bob, msg, opts); add_string_member (bob, "body-txt", mu_msg_get_body_text(msg, opts)); add_string_member (bob, "body-html", mu_msg_get_body_html(msg, opts)); }
const char* mu_msg_get_header (MuMsg *self, const char *header) { g_return_val_if_fail (self, NULL); g_return_val_if_fail (header, NULL); /* if we don't have a file object yet, we need to * create it from the file on disk */ if (!mu_msg_load_msg_file (self, NULL)) return NULL; return free_later_str (self, mu_msg_file_get_header (self->_file, header)); }
gboolean mu_msg_part_foreach (MuMsg *msg, MuMsgOptions opts, MuMsgPartForeachFunc func, gpointer user_data) { GMimeMessage *mime_msg; unsigned idx; g_return_val_if_fail (msg, FALSE); if (!mu_msg_load_msg_file (msg, NULL)) return FALSE; idx = 0; mime_msg = GMIME_MESSAGE(msg->_file->_mime_msg); return handle_children (msg, mime_msg, opts, idx, func, user_data); }
int mu_msg_find_index_for_cid (MuMsg *msg, MuMsgOptions opts, const char *sought_cid) { const char* cid; g_return_val_if_fail (msg, -1); g_return_val_if_fail (sought_cid, -1); if (!mu_msg_load_msg_file (msg, NULL)) return -1; cid = g_str_has_prefix (sought_cid, "cid:") ? sought_cid + 4 : sought_cid; return get_matching_part_index (msg, opts, (MuMsgPartMatchFunc)match_cid, (gpointer)(char*)cid); }
static gint64 get_num_field (MuMsg *self, MuMsgFieldId mfid) { guint64 val; val = -1; if (self->_doc && mu_msg_field_xapian_value (mfid)) val = mu_msg_doc_get_num_field (self->_doc, mfid); else { /* if we don't have a file object yet, we need to * create it from the file on disk */ if (!mu_msg_load_msg_file (self, NULL)) return -1; val = mu_msg_file_get_num_field (self->_file, mfid); } return val; }
static const GSList* get_str_list_field (MuMsg *self, MuMsgFieldId mfid) { GSList *val; val = NULL; if (self->_doc && mu_msg_field_xapian_value (mfid)) val = mu_msg_doc_get_str_list_field (self->_doc, mfid); else if (mu_msg_field_gmime (mfid)) { /* if we don't have a file object yet, we need to * create it from the file on disk */ if (!mu_msg_load_msg_file (self, NULL)) return NULL; val = mu_msg_file_get_str_list_field (self->_file, mfid); } return free_later_lst (self, val); }
GSList* mu_msg_find_files (MuMsg *msg, MuMsgOptions opts, const GRegex *pattern) { RxMatchData mdata; g_return_val_if_fail (msg, NULL); g_return_val_if_fail (pattern, NULL); if (!mu_msg_load_msg_file (msg, NULL)) return NULL; mdata._lst = NULL; mdata._rx = pattern; mdata._idx = 0; mu_msg_part_foreach (msg, opts, (MuMsgPartForeachFunc)match_filename_rx, &mdata); return mdata._lst; }
gboolean mu_msg_part_foreach (MuMsg *msg, MuMsgOptions opts, MuMsgPartForeachFunc func, gpointer user_data) { unsigned index; index = 1; g_return_val_if_fail (msg, FALSE); if (!mu_msg_load_msg_file (msg, NULL)) return FALSE; return handle_mime_object (msg, msg->_file->_mime_msg->mime_part, (GMimeObject *) msg->_file->_mime_msg, opts, &index, FALSE, func, user_data); }