File size: 3,926 Bytes
34a5084
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python3
"""Initialize Roundcube's SQLite database and the primary user's contacts."""

from __future__ import annotations

import json
import os
import sqlite3
from pathlib import Path


ASSET_DIR = Path(os.environ.get("SEED_ASSET_DIR", "/opt/thunderbird-seed/seed_assets"))
DATABASE = Path(os.environ.get("ROUNDCUBE_DB", "/var/lib/roundcube/roundcube.db"))
SCHEMA = Path(os.environ.get("ROUNDCUBE_SCHEMA", "/var/www/roundcube/SQL/sqlite.initial.sql"))
MAIL_HOST = "127.0.0.1"
CHANGED = "2026-07-10 17:00:00"


def vcard_escape(value: str) -> str:
    return (
        value.replace("\\", "\\\\")
        .replace(";", "\\;")
        .replace(",", "\\,")
        .replace("\n", "\\n")
    )


def vcard(contact: dict[str, str]) -> str:
    return "\r\n".join(
        [
            "BEGIN:VCARD",
            "VERSION:3.0",
            f"FN:{vcard_escape(contact['name'])}",
            f"N:{vcard_escape(contact['surname'])};{vcard_escape(contact['firstname'])};;;",
            f"EMAIL;TYPE=INTERNET:{contact['email']}",
            "REV:2026-07-10T17:00:00Z",
            "END:VCARD",
            "",
        ]
    )


def main() -> int:
    manifest = json.loads((ASSET_DIR / "manifest.json").read_text(encoding="utf-8"))
    contacts = json.loads((ASSET_DIR / "contacts.json").read_text(encoding="utf-8"))
    if len(contacts) != manifest["contact_count"]:
        raise RuntimeError("Contact count does not match seed manifest")

    DATABASE.parent.mkdir(parents=True, exist_ok=True)
    DATABASE.unlink(missing_ok=True)
    connection = sqlite3.connect(DATABASE)
    connection.execute("PRAGMA foreign_keys = ON")
    connection.executescript(SCHEMA.read_text(encoding="utf-8"))

    user_ids: dict[str, int] = {}
    for user in manifest["users"]:
        cursor = connection.execute(
            "INSERT INTO users (username, mail_host, created, language, preferences) VALUES (?, ?, ?, ?, ?)",
            (user["email"], MAIL_HOST, CHANGED, "en_US", "a:0:{}"),
        )
        user_id = int(cursor.lastrowid)
        user_ids[user["email"]] = user_id
        connection.execute(
            """
            INSERT INTO identities
                (user_id, changed, del, standard, name, organization, email, "reply-to", bcc, signature, html_signature)
            VALUES (?, ?, 0, 1, ?, 'TechCorp', ?, '', '', '', 0)
            """,
            (user_id, CHANGED, user["name"], user["email"]),
        )

    primary_user_id = user_ids[manifest["primary_user"]]
    group_cursor = connection.execute(
        "INSERT INTO contactgroups (user_id, changed, del, name) VALUES (?, ?, 0, ?)",
        (primary_user_id, CHANGED, "TechCorp Directory"),
    )
    group_id = int(group_cursor.lastrowid)

    for contact in contacts:
        words = " ".join(
            [
                contact["name"].casefold(),
                contact["firstname"].casefold(),
                contact["surname"].casefold(),
                contact["email"].casefold(),
            ]
        )
        cursor = connection.execute(
            """
            INSERT INTO contacts
                (user_id, changed, del, name, email, firstname, surname, vcard, words)
            VALUES (?, ?, 0, ?, ?, ?, ?, ?, ?)
            """,
            (
                primary_user_id,
                CHANGED,
                contact["name"],
                contact["email"],
                contact["firstname"],
                contact["surname"],
                vcard(contact),
                words,
            ),
        )
        connection.execute(
            "INSERT INTO contactgroupmembers (contactgroup_id, contact_id, created) VALUES (?, ?, ?)",
            (group_id, int(cursor.lastrowid), CHANGED),
        )

    connection.commit()
    connection.close()
    print(f"Seeded Roundcube users={len(user_ids)} contacts={len(contacts)}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())