aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/notmuch/.local/bin/notmuch-notify
blob: 18c6c83789707cf4f16245d315bc82dbe35f34f0 (plain)
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
#!/usr/bin/env python3

import os

import notmuch
from jeepney import DBusAddress, new_method_call
from jeepney.io.blocking import open_dbus_connection


DBUS_ADDRESS = DBusAddress(
    "/org/freedesktop/Notifications",
    bus_name="org.freedesktop.Notifications",
    interface="org.freedesktop.Notifications",
)


def default_config() -> str:
    return os.path.join(
        os.environ.get(
            "XDG_CONFIG_HOME", os.path.join(os.path.expanduser("~"), ".config")
        ),
        "notmuch",
        os.environ.get("NOTMUCH_PROFILE", "default"),
        "config",
    )


def notify(summary: str, body: str) -> int | None:
    msg = new_method_call(
        DBUS_ADDRESS,
        "Notify",
        "susssasa{sv}i",
        (
            "notmuch-notify",
            0,
            "mail-unread",
            summary,
            body,
            [],
            {},
            -1,
        ),
    )

    id = open_dbus_connection(bus="SESSION").send_and_get_reply(msg)
    if isinstance(id, int):
        return id


def main():
    if os.environ.get("NOTMUCH_CONFIG") is None:
        os.environ["NOTMUCH_CONFIG"] = default_config()

    database = notmuch.Database(mode=notmuch.Database.MODE.READ_WRITE)

    for message in database.create_query("tag:notify").search_messages():
        notify(message.get_header("From"), message.get_header("Subject"))
        print(message)
        message.remove_tag("notify")
        del message


if __name__ == "__main__":
    main()