aboutsummaryrefslogtreecommitdiff
path: root/matrix-mcnt.py
blob: f3cca07f44acf77800b370ec30aa8d680628467c (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
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
#!/usr/bin/env python3

import asyncio
import argparse
import getpass
from nio import (
        AsyncClient,
        MatrixRoom,
        RoomMessageText,
        LoginResponse,
        RoomInfo
)


HOME = "matrix.org"
HOME_SERVER = f"https://{HOME}"
USERNAME = None
USER_ID = f"@{USERNAME}:{HOME}"
ROOM_IDS = []
EXCLUDE_ROOM_IDS = []
ACCESS_TOKEN = None


async def client_login() -> AsyncClient:
    client = AsyncClient(HOME_SERVER, USER_ID)
    #client = AsyncClient(f"https://{HOME}", f"@{USERNAME}:{HOME}") # need to decide on username format
    if ACCESS_TOKEN:
        client.access_token = ACCESS_TOKEN
        # TODO: implement try-exception
        return client
    passwd = getpass.getpass()
    if isinstance(await client.login(passwd), LoginResponse):
        return client

    return None

async def main() -> None:
    client = await client_login()

    if not client:
        print(f"Could not log on to {USERNAME}")
        exit()
    else:
        print(f"Logged on as {USERNAME}.")

    await client.sync_forever(timeout=30000)

async def fetch_unread(client: AsyncClient):
    exit() # TODO:

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
            description="matrix-mcnt: Matrix Unread Message Count"
    )

    parser.add_argument(
            "--username",
            required=True,
            help="eg. 'myusername' not '@myusername:matrix.org'"
            # otherwise conflict arises if --homeserver is supplied
    )

    parser.add_argument(
            "--access-token",
            help="Supply an access token to prevent password prompting"
    )

    parser.add_argument(
            "--homeserver",
            default=HOME,
            help="Supply homeserver domain (eg. 'matrix.org' or 'matrix.server.com')"
    )

    parser.add_argument(
            "--rooms",
            help="Room ID(s) (eg. '!Abcdefghijklmnopqr' '!2Abcdefghijklmnopq')." +
                 "matrix-mcnt will only count the supplied ID(s).",
            default=[],
            action="append",
    )

    parser.add_argument(
            "--exclude-rooms",
            help="A list of Room(s) ID(s) to exclude (eg. '!Abcdefghijklmnopqr')",
            default=[],
            action="append",
    )

    parser.add_argument(
            "--print-rooms",
            help="Print all available rooms"
    )

    args = parser.parse_args()

    HOME = args.homeserver
    USERNAME = args.username
    ROOM_IDS = args.rooms
    EXCLUDE_ROOM_IDS = args.exclude_rooms
    ACCESS_TOKEN = args.access_token

    asyncio.run(main())