Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-message

Source of release 0.5.0

0.5.0 / src / mail_sync / module.lucb

## The mail engine for one email account, on its own thread. It keeps one IMAP
## session: it lists the folders, brings each folder's index up to date (new messages'
## headers, everyone's flags, expunges), prefetches the newest INBOX bodies, then
## waits in IDLE on INBOX until the server has news or the window has a command.
## Commands (fetch a body, change flags, move, send) run between those steps. Every
## change is written to the store (mail_db) first and then announced as an event, so the
## window only ever reads the store.
##
## Commands, tab-separated:   sync | body PATH UID | flags PATH UIDS ADD BITS |
##                            move PATH UIDS TARGET | send QUEUED FROM RECIPIENTS | draft QUEUED |
##                            search PATH QUERY
## Events:                    online | offline TEXT | status TEXT | folders |
##                            folder PATH | body PATH UID | sent QUEUED | unsent QUEUED TEXT |
##                            saved QUEUED | unsaved QUEUED TEXT | found PATH UIDS |
##                            error TEXT
##
## Fragments, in ORDER: module (imports, limits, the account), engine (the loop,
## commands, paths, selecting, waiting), folders (listing and syncing folders, bodies),
## changes (flags, moves, sending).
import c
from luce_imap import imap
import memory
from luce_mime import mime
from luce_std import net
from luce_smtp import smtp
import thread
import time
from luce_tls import stream as tls_stream
from luce_prism import prism
from luce_std.collections import Buffer, TextPool, List
from engine_text import Fields, put_field
from mail_store import Row, Folder, put_key
from mail_queue import Mailroom
import mail_db

## The headers a message list needs, fetched for every new message.
let summary_fields: str = "DATE FROM SUBJECT MESSAGE-ID IN-REPLY-TO CONTENT-TYPE"
## How many messages' headers one FETCH asks for.
let batch_size: usize = 400
## How many of INBOX's newest bodies are fetched ahead of being opened.
let prefetch_count: usize = 25
## A full sync of every folder at least this often, in nanoseconds.
let full_sync_interval: u64 = 10 * 60 * 1000000000
## One slice of an IDLE wait, between looks at the mailroom.
let idle_slice: u64 = 500000000
## How long to wait before reconnecting after a failure.
let retry_delay: u64 = 30 * 1000000000

## The account the engine serves, read from its configuration line.
struct Account:
    var id: str
    var imap_host: str
    var imap_port: u16
    var imap_security: imap.Security
    var user: str
    var password: str
    var smtp_host: str
    var smtp_port: u16
    var smtp_security: smtp.Security
    var smtp_user: str
    var smtp_password: str

## The security named `name`: plain, starttls or tls.
func imap_security(name: const u8[]) -> imap.Security:
    if name == b"plain":
        return imap.Security.plain
    if name == b"starttls":
        return imap.Security.starttls
    return imap.Security.tls

func smtp_security(name: const u8[]) -> smtp.Security:
    if name == b"plain":
        return smtp.Security.plain
    if name == b"tls":
        return smtp.Security.tls
    return smtp.Security.starttls