Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-message

Source of release 0.5.0

0.5.0 / src / mail_store.lucb

## The rows and folders the mail database holds (see mail_db), the store key of a
## folder, and making the app's directory. A folder's key is a hash of its server
## path, so any name makes a valid prism identity.
from luce_std import files
from luce_std.collections import Buffer

## One message's row in a folder index.
pub struct Row:
    pub var uid: u32
    ## imap.Flags bits.
    pub var flags: u16
    ## Seconds since 1970 UTC: the Date header, else the server's arrival time.
    pub var date: i64
    pub var size: u32
    pub var attachment: bool
    ## The sender as a list shows it: the display name, else the address.
    pub var sender: str
    pub var subject: str
    pub var message_id: str
    pub var in_reply_to: str
    ## The opening of the body; empty until the body is fetched.
    pub var preview: str

## A folder as the store lists it.
pub struct Folder:
    ## The server's name for it, used in commands.
    pub var path: str
    ## The name to show, UTF-8.
    pub var name: str
    pub var delimiter: u8
    ## imap.MailboxFlags bits.
    pub var flags: u32
    ## Its directory under the account.
    pub var key: str
    pub var total: u32
    pub var unseen: u32

## Append the store key of folder `path`: sixteen hex digits of its FNV-1a hash.
pub func put_key(path: const u8[], out: Buffer*) -> !:
    var digest: u64 = 0xcbf29ce484222325
    for byte in path:
        digest = (digest ^ byte) *% 0x100000001b3
    var shift: u64 = 60
    while true:
        try out.put_byte("0123456789abcdef".bytes[(digest >> shift) & 15])
        if shift == 0:
            break
        shift -= 4

## Make directory `path` and every missing directory above it.
pub func ensure_path(path: str) -> !:
    let data = path.bytes
    for at in 1..<data.length + 1:
        if at == data.length or data[at] == '/':
            try files.ensure_directory((str)data[0..<at])