mail-storage.h revision 236bedf76e31651ea9fca63fbdc25be673819526
#ifndef MAIL_STORAGE_H
#define MAIL_STORAGE_H
struct message_size;
#include "seq-range-array.h"
#include "file-lock.h"
#include "mail-types.h"
#include "mail-error.h"
#include "mailbox-list.h"
/* If some operation is taking long, call notify_ok every n seconds. */
#define MAIL_STORAGE_STAYALIVE_SECS 15
enum mail_storage_flags {
/* Print debugging information while initializing the storage */
MAIL_STORAGE_FLAG_DEBUG = 0x01,
/* Allow full filesystem access with absolute or relative paths. */
MAIL_STORAGE_FLAG_FULL_FS_ACCESS = 0x02,
/* Don't try to mmap() files */
MAIL_STORAGE_FLAG_MMAP_DISABLE = 0x04,
/* Don't try to write() to mmap()ed files. Required for the few
OSes that don't have unified buffer cache
(currently OpenBSD <= 3.5) */
MAIL_STORAGE_FLAG_MMAP_NO_WRITE = 0x08,
/* Remember message headers' MD5 sum */
/* Use CRLF linefeeds when saving mails. */
MAIL_STORAGE_FLAG_SAVE_CRLF = 0x40,
/* Don't try to autodetect anything, require that the given data
contains all the necessary information. */
MAIL_STORAGE_FLAG_NO_AUTODETECTION = 0x100,
/* Don't autocreate any directories. If they don't exist,
fail to create the storage. */
MAIL_STORAGE_FLAG_NO_AUTOCREATE = 0x200,
/* Rely on O_EXCL when creating dotlocks */
MAIL_STORAGE_FLAG_DOTLOCK_USE_EXCL = 0x400,
/* Flush NFS caches for mail storage / index */
MAIL_STORAGE_FLAG_NFS_FLUSH_STORAGE = 0x800,
MAIL_STORAGE_FLAG_NFS_FLUSH_INDEX = 0x1000,
/* Don't use fsync() or fdatasync() */
MAIL_STORAGE_FLAG_FSYNC_DISABLE = 0x2000
};
enum mailbox_open_flags {
/* Mailbox must not be modified even if asked */
MAILBOX_OPEN_READONLY = 0x01,
MAILBOX_OPEN_SAVEONLY = 0x02,
/* Any extra time consuming operations shouldn't be performed
(eg. when opening mailbox just for STATUS). */
MAILBOX_OPEN_FAST = 0x04,
/* Don't reset MAIL_RECENT flags when syncing */
MAILBOX_OPEN_KEEP_RECENT = 0x08,
/* Don't create index files for the mailbox */
MAILBOX_OPEN_NO_INDEX_FILES = 0x10,
/* Keep mailbox exclusively locked all the time while it's open */
MAILBOX_OPEN_KEEP_LOCKED = 0x20,
/* Enable if mailbox is used for serving POP3. This allows making
better caching decisions. */
MAILBOX_OPEN_POP3_SESSION = 0x40
};
enum mailbox_feature {
/* Enable tracking modsequences */
MAILBOX_FEATURE_CONDSTORE = 0x01,
/* Enable tracking expunge modsequences */
MAILBOX_FEATURE_QRESYNC = 0x02
};
enum mailbox_status_items {
STATUS_MESSAGES = 0x01,
STATUS_RECENT = 0x02,
STATUS_UIDNEXT = 0x04,
STATUS_UIDVALIDITY = 0x08,
STATUS_UNSEEN = 0x10,
STATUS_FIRST_UNSEEN_SEQ = 0x20,
STATUS_KEYWORDS = 0x40,
STATUS_HIGHESTMODSEQ = 0x80
};
enum mailbox_search_result_flags {
/* Update search results whenever the mailbox view is synced.
Expunged messages are removed even without this flag. */
/* Queue changes so _sync() can be used. */
};
enum mail_sort_type {
/* Maximum size for sort program (each one separately + END) */
MAIL_SORT_ARRIVAL = 0x0001,
MAIL_SORT_CC = 0x0002,
MAIL_SORT_DATE = 0x0004,
MAIL_SORT_FROM = 0x0008,
MAIL_SORT_SIZE = 0x0010,
MAIL_SORT_SUBJECT = 0x0020,
MAIL_SORT_TO = 0x0040,
MAIL_SORT_SEARCH_SCORE = 0x0080,
MAIL_SORT_MASK = 0x0fff,
};
enum mail_fetch_field {
MAIL_FETCH_FLAGS = 0x00000001,
MAIL_FETCH_MESSAGE_PARTS = 0x00000002,
MAIL_FETCH_STREAM_HEADER = 0x00000004,
MAIL_FETCH_STREAM_BODY = 0x00000008,
MAIL_FETCH_DATE = 0x00000010,
MAIL_FETCH_RECEIVED_DATE = 0x00000020,
MAIL_FETCH_SAVE_DATE = 0x00000040,
MAIL_FETCH_PHYSICAL_SIZE = 0x00000080,
MAIL_FETCH_VIRTUAL_SIZE = 0x00000100,
/* Set has_nuls / has_no_nuls fields */
MAIL_FETCH_NUL_STATE = 0x00000200,
/* specials: */
MAIL_FETCH_IMAP_BODY = 0x00001000,
MAIL_FETCH_IMAP_BODYSTRUCTURE = 0x00002000,
MAIL_FETCH_IMAP_ENVELOPE = 0x00004000,
MAIL_FETCH_FROM_ENVELOPE = 0x00008000,
MAIL_FETCH_HEADER_MD5 = 0x00010000,
MAIL_FETCH_UIDL_FILE_NAME = 0x00020000,
MAIL_FETCH_UIDL_BACKEND = 0x00040000,
MAIL_FETCH_MAILBOX_NAME = 0x00080000,
MAIL_FETCH_SEARCH_SCORE = 0x00100000,
MAIL_FETCH_GUID = 0x00200000
};
enum mailbox_transaction_flags {
/* Hide changes done in this transaction from next view sync */
MAILBOX_TRANSACTION_FLAG_HIDE = 0x01,
/* External transaction. Should be used for copying and appends,
but nothing else. */
is done only if the mailbox is synced, or if dest_mail parameter
was non-NULL to mailbox_save_init() or mailbox_copy() */
};
enum mailbox_sync_flags {
/* Make sure we sync all external changes done to mailbox */
MAILBOX_SYNC_FLAG_FULL_READ = 0x01,
/* Make sure we write all our internal changes into the mailbox */
MAILBOX_SYNC_FLAG_FULL_WRITE = 0x02,
/* If it's not too much trouble, check if there are some changes */
MAILBOX_SYNC_FLAG_FAST = 0x04,
/* Don't sync expunges from our view */
MAILBOX_SYNC_FLAG_NO_EXPUNGES = 0x08,
/* Stop auto syncing */
MAILBOX_SYNC_AUTO_STOP = 0x20,
/* If mailbox is currently inconsistent, fix it instead of failing. */
/* Syncing after an EXPUNGE command. This is just an informational
flag for plugins. */
MAILBOX_SYNC_FLAG_EXPUNGE = 0x80
};
enum mailbox_sync_type {
MAILBOX_SYNC_TYPE_EXPUNGE = 0x01,
MAILBOX_SYNC_TYPE_FLAGS = 0x02,
MAILBOX_SYNC_TYPE_MODSEQ = 0x04
};
struct message_part;
struct mail_namespace;
struct mail_storage;
struct mail_search_args;
struct mail_search_result;
struct mail_keywords;
struct mail_save_context;
struct mailbox;
struct mailbox_transaction_context;
struct mailbox_status {
/* There are expunges that haven't been synced yet */
unsigned int sync_delayed_expunges:1;
/* Modseqs aren't permanent (index is in memory) */
unsigned int nonpermanent_modseqs:1;
};
struct mailbox_sync_rec {
enum mailbox_sync_type type;
};
struct mail {
/* always set */
struct mailbox_transaction_context *transaction;
unsigned int expunged:1;
};
struct mail_storage_callbacks {
/* "* OK <text>" */
void *context);
/* "* NO <text>" */
void *context);
};
void mail_storage_init(void);
void mail_storage_deinit(void);
/* register all mail storages */
void mail_storage_register_all(void);
/* Register mail storage class with given name - all methods that are NULL
are set to default methods */
/* Find mail storage class by name */
/* Returns flags and lock_method based on environment settings. */
enum file_lock_method *lock_method_r);
/* Create a new instance of registered mail storage class with given
storage-specific data. If driver is NULL, it's tried to be autodetected
from data. If data is NULL, it uses the first storage that exists.
The storage is put into ns->storage. */
enum file_lock_method lock_method,
const char **error_r);
struct mailbox_list *
struct mail_namespace *
/* Set storage callback functions to use. */
struct mail_storage_callbacks *callbacks,
void *context);
/* name is allowed to contain multiple new hierarchy levels.
If directory is TRUE, the mailbox should be created so that it
can contain children. The mailbox itself doesn't have to be
created as long as it shows in LIST. */
bool directory);
/* Returns the error message of last occurred error. */
enum mail_error *error_r);
/* Returns path to the given mailbox, or NULL if mailbox doesn't exist in
filesystem. is_file_r is set to TRUE if returned path points to a file,
and FALSE if it points to a directory. If name is "", the root storage
directory is returned. */
/* Returns path to the control directory of the mailbox, or NULL if mailbox
doesn't exist in filesystem. */
const char *name);
/* Returns path to the index directory of the mailbox, or NULL if using
in-memory indexes or mailbox doesn't exist. */
const char *name);
/* Open a mailbox. If input stream is given, mailbox is opened read-only
using it as a backend. If storage doesn't support stream backends and its
tried to be used, NULL is returned.
Note that append and copy may open the selected mailbox again
with possibly different readonly-state. */
enum mailbox_open_flags flags);
/* Close the box. Returns -1 if some cleanup errors occurred, but
the mailbox was closed anyway. */
/* Enable the given feature for the mailbox. */
/* Returns storage of given mailbox */
/* Returns name of given mailbox */
/* Returns TRUE if mailbox is read-only. */
/* Returns TRUE if mailbox currently supports adding keywords. */
/* Gets the mailbox status information. */
struct mailbox_status *status_r);
/* Synchronize the mailbox. */
struct mailbox_sync_context *
struct mailbox_sync_rec *sync_rec_r);
struct mailbox_status *status_r);
/* One-step mailbox synchronization. Use this if you don't care about
changes. */
struct mailbox_status *status_r);
/* Call given callback function when something changes in the mailbox. */
#ifdef CONTEXT_TYPE_SAFETY
#else
#endif
struct mailbox_transaction_context *
enum mailbox_transaction_flags flags);
int mailbox_transaction_commit(struct mailbox_transaction_context **t);
/* If no messages were saved/copied, first/last_saved_uid_r are 0. */
int mailbox_transaction_commit_get_uids(struct mailbox_transaction_context **t,
void mailbox_transaction_rollback(struct mailbox_transaction_context **t);
/* Return the number of active transactions for the mailbox. */
modseq is larger than max_modseq. Save those messages' sequences to the
given array. */
void mailbox_transaction_set_max_modseq(struct mailbox_transaction_context *t,
struct mailbox *
mailbox_transaction_get_mailbox(const struct mailbox_transaction_context *t)
/* Build mail_keywords from NULL-terminated keywords list.
Returns 0 if successful, -1 if there are invalid keywords (error is set). */
struct mail_keywords **keywords_r);
/* Like mailbox_keywords_create(), except ignore invalid keywords. */
struct mail_keywords *
const char *const keywords[]);
struct mail_keywords **keywords);
/* Returns TRUE if keyword is valid, FALSE and error if not. */
const char **error_r);
/* Convert uid range to sequence range. */
/* Convert sequence range to uid range. If sequences contain
(uint32_t)-1 to specify "*", they're preserved. */
/* Get list of UIDs expunged after modseq and within the given range.
UIDs that have been expunged after the last mailbox sync aren't returned.
Returns TRUE if ok, FALSE if modseq is lower than we can check for. */
/* Initialize header lookup for given headers. */
struct mailbox_header_lookup_ctx *
/* Initialize new search request. charset specifies the character set used in
the search argument strings. If sort_program is non-NULL, the messages are
returned in the requested order, otherwise from first to last. */
struct mail_search_context *
mailbox_search_init(struct mailbox_transaction_context *t,
struct mail_search_args *args,
const enum mail_sort_type *sort_program);
/* Deinitialize search request. */
/* Search the next message. Returns 1 if found, 0 if not, -1 if failure. */
/* Like mailbox_search_next(), but don't spend too much time searching.
Returns 1 if found, -1 if failure or 0 with tryagain_r=FALSE if
finished, and TRUE if more results will by calling the function again. */
/* Remember the search result for future use. This must be called before the
first mailbox_search_next*() call. */
struct mail_search_result *
enum mailbox_search_result_flags flags);
/* Free memory used by search result. */
/* A simplified API for searching and saving the result. */
int mailbox_search_result_build(struct mailbox_transaction_context *t,
struct mail_search_args *args,
struct mail_search_result **result_r);
/* Return all messages' UIDs in the search result. */
const ARRAY_TYPE(seq_range) *
/* Return messages that have been removed and added since the last sync call.
This function must not be called if search result wasn't saved with
_QUEUE_SYNC flag. */
/* Initialize saving a new mail. You must not try to save more than one mail
at a time. */
struct mail_save_context *
mailbox_save_alloc(struct mailbox_transaction_context *t);
/* Set the flags and keywords. Nothing is set by default. */
enum mail_flags flags,
struct mail_keywords *keywords);
/* If received date isn't specified the current time is used. timezone_offset
specifies the preferred timezone in minutes, but it may be ignored if
backend doesn't support storing it. */
/* Set the envelope sender. This is currently used only with mbox files to
specify the address in From_-line. */
const char *envelope);
/* Set globally unique ID for the saved mail. A new GUID is generated by
default. This function should usually be called only when copying an
existing mail (or restoring a mail from backup). */
/* If dest_mail is set, the saved message can be accessed using it. Note that
setting it may require mailbox syncing, so don't set it unless you need
it. Also you shouldn't try to access it before mailbox_save_finish() is
called. */
/* Begin saving the message. All mail_save_set_*() calls must have been called
before this function. If the save initialization fails, the context is freed
and -1 is returned. After beginning the save you should keep calling
i_stream_read() and calling mailbox_save_continue() as long as there's
more input. */
/* Copy given message. If dest_mail is non-NULL, the copied message can be
accessed using it. Note that setting it non-NULL may require mailbox
syncing, so don't give give it unless you need it. */
/* Returns TRUE if mailbox is now in inconsistent state, meaning that
the message IDs etc. may have changed - only way to recover this
would be to fully close the mailbox and reopen it. With IMAP
connection this would mean a forced disconnection since we can't
do forced CLOSE. */
struct mailbox_header_lookup_ctx *wanted_headers);
/* Returns TRUE if successful, FALSE if message doesn't exist.
mail_*() functions shouldn't be called if FALSE is returned. */
/* Returns message's flags */
/* Returns message's keywords */
/* Returns message's keywords */
/* Returns message's modseq */
/* Returns message's MIME parts */
/* Get the Date-header of the mail. Timezone is in minutes. date=0 if it
wasn't found or it was invalid. */
/* Get the time when the mail was received (IMAP INTERNALDATE). */
/* Get the time when the mail was saved into this mailbox. This time may not
always be entirely reliable. */
/* Get the space used by the mail as seen by the reader. Linefeeds are always
counted as being CR+LF. */
/* Get the size of the stream returned by mail_get_stream(). */
/* Get value for single header field, or NULL if header wasn't found.
Returns 1 if header was found, 0 if not, -1 if error. */
const char **value_r);
/* Like mail_get_first_header(), but decode MIME encoded words to UTF-8 */
const char **value_r);
/* Return a NULL-terminated list of values for each found field. */
const char *const **value_r);
/* Like mail_get_headers(), but decode MIME encoded words to UTF-8 */
const char *const **value_r);
/* Returns stream containing specified headers. */
struct mailbox_header_lookup_ctx *headers,
/* Returns input stream pointing to beginning of message header.
hdr_size and body_size are updated unless they're NULL. */
/* Get any of the "special" fields. */
const char **value_r);
/* Update message flags. */
enum mail_flags flags);
/* Update message keywords. */
struct mail_keywords *keywords);
/* Expunge this message. Sequence numbers don't change until commit. */
/* Mark a cached field corrupted and have it recalculated. */
#endif