Initial commit for mail-sr

This commit is contained in:
zeer
2026-04-25 20:51:08 +08:00
commit 37907dd2f5
14 changed files with 4628 additions and 0 deletions

93
db.js Normal file
View File

@@ -0,0 +1,93 @@
const path = require('path');
const Database = require('better-sqlite3');
const dbPath = process.env.DB_PATH
? path.resolve(process.env.DB_PATH)
: path.join(__dirname, 'mail.db');
const db = new Database(dbPath);
db.pragma('journal_mode = WAL');
db.exec(`
CREATE TABLE IF NOT EXISTS channels (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
code TEXT NOT NULL UNIQUE,
imap_host TEXT NOT NULL,
imap_port INTEGER NOT NULL,
imap_secure INTEGER NOT NULL DEFAULT 1,
smtp_host TEXT NOT NULL,
smtp_port INTEGER NOT NULL,
smtp_secure INTEGER NOT NULL DEFAULT 1,
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS accounts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
channel_id INTEGER NOT NULL,
email TEXT NOT NULL UNIQUE,
auth_code TEXT NOT NULL,
display_name TEXT,
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY(channel_id) REFERENCES channels(id)
);
CREATE TABLE IF NOT EXISTS mail_cache (
id INTEGER PRIMARY KEY AUTOINCREMENT,
account_id INTEGER NOT NULL,
uid TEXT NOT NULL,
folder TEXT NOT NULL DEFAULT 'INBOX',
subject TEXT,
from_name TEXT,
from_address TEXT,
to_addresses TEXT,
sent_at TEXT,
text_content TEXT,
html_content TEXT,
raw_json TEXT NOT NULL,
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE(account_id, uid),
FOREIGN KEY(account_id) REFERENCES accounts(id)
);
`);
const seedChannels = [
{
name: 'QQ邮箱',
code: 'qq',
imap_host: 'imap.qq.com',
imap_port: 993,
imap_secure: 1,
smtp_host: 'smtp.qq.com',
smtp_port: 465,
smtp_secure: 1,
},
{
name: '163邮箱',
code: '163',
imap_host: 'imap.163.com',
imap_port: 993,
imap_secure: 1,
smtp_host: 'smtp.163.com',
smtp_port: 465,
smtp_secure: 1,
},
];
const channelExistsStmt = db.prepare('SELECT id FROM channels WHERE code = ?');
const insertChannelStmt = db.prepare(`
INSERT INTO channels (
name, code, imap_host, imap_port, imap_secure, smtp_host, smtp_port, smtp_secure
) VALUES (
@name, @code, @imap_host, @imap_port, @imap_secure, @smtp_host, @smtp_port, @smtp_secure
)
`);
for (const channel of seedChannels) {
if (!channelExistsStmt.get(channel.code)) {
insertChannelStmt.run(channel);
}
}
module.exports = db;