remove inboxes table and add arrived_at column

there is no current need to have a separate table for keeping track of
the inboxes, since all emails are getting stored upon arrival and anyone
can access all mails from any rcpt address.

also, added a arrived_at column, as a utc unix timestamp. This serves
the obvious reason to have a ts on an email, but also allows this
service to delete old mail.
This commit is contained in:
Guilherme Rugai Freire 2024-02-20 00:42:00 -03:00
parent 369115d781
commit 7e8327fd0d
No known key found for this signature in database
GPG Key ID: AC1D9B6E48E16AC1
2 changed files with 12 additions and 36 deletions

View File

@ -28,7 +28,7 @@ func (backend *Backend) NewSession(c *smtp.Conn) (smtp.Session, error) {
type Session struct {
tx *sql.Tx
from, rcpt string
rcpt_addr_id int64
arrived_at int64
}
func (session *Session) AuthPlain(username, password string) error {
@ -36,6 +36,8 @@ func (session *Session) AuthPlain(username, password string) error {
}
func (session *Session) Mail(from string, opts *smtp.MailOptions) error {
session.arrived_at = time.Now().UTC().Unix()
session.from = from
return nil
}
@ -43,28 +45,6 @@ func (session *Session) Mail(from string, opts *smtp.MailOptions) error {
func (session *Session) Rcpt(to string, opts *smtp.RcptOptions) error {
session.rcpt = to
stmt, err := session.tx.Prepare("INSERT OR IGNORE INTO inboxes (addr) VALUES (?) RETURNING id")
if err != nil {
return err
}
defer stmt.Close()
res, err := stmt.Exec(to)
if err != nil {
return err
}
// sometimes does not work and returns 0
// see https://github.com/mattn/go-sqlite3/issues/1140
id, err := res.LastInsertId()
if err != nil {
return err
}
log.Println("Last id: ", id)
session.rcpt_addr_id = id
return nil
}
@ -73,12 +53,13 @@ func (session *Session) Data(reader io.Reader) error {
return err
} else {
stmt, err := session.tx.Prepare("INSERT INTO mails (inbox_id, from_addr, data) VALUES (?, ?, ?)")
stmt, err := session.tx.Prepare("INSERT INTO mails (arrived_at, rcpt_addr, from_addr, data) VALUES (?, ?, ?, ?)")
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(session.rcpt_addr_id, session.from, bytes)
_, err = stmt.Exec(session.arrived_at, session.rcpt, session.from, bytes)
if err != nil {
return err
}

View File

@ -1,12 +1,7 @@
CREATE TABLE inboxes (
id integer not null primary key,
addr text unique
);
CREATE TABLE mails (
id integer not null primary key,
inbox_id id,
from_addr text,
data blob,
FOREIGN KEY(inbox_id) REFERENCES inboxes(id)
arrived_at integer not null,
rcpt_addr text not null,
from_addr text not null,
data blob not null
);