aboutsummaryrefslogtreecommitdiffstats
path: root/main.py
blob: 532e4af19fdf58964a3dc8f868c3ae39a3dcf542 (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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import logging

import ts3

from config import Config


def watcher(conn):
    # Register for events
    # https://yat.qa/ressourcen/server-query-notify/#server
    conn.servernotifyregister(event="server")

    while True:
        conn.send_keepalive()

        try:
            event = conn.wait_for_event(timeout=60)

        except ts3.query.TS3TimeoutError:
            pass

        else:
            # only parse entering clients info
            if event.event == "notifycliententerview":

                # skip query clients -- query client = 1 , voice client = 0
                if event[0]['client_type'] == '0':

                    # reasonid should be 0 not sure though
                    if event[0]["reasonid"] == "0":

                        user_grps = event.parsed[0]['client_servergroups'].split(sep=',')
                        gid = Config.GID

                        # only try to add nonmembers to group
                        if str(gid) not in user_grps:

                            cldbid = event.parsed[0]['client_database_id']

                            # https://yat.qa/ressourcen/server-query-kommentare/
                            # Usage: servergroupaddclient sgid={groupID} cldbid={clientDBID}
                            try:
                                cmd = conn.servergroupaddclient(sgid=gid, cldbid=cldbid)

                                if cmd.error['id'] != '0':
                                    logger.info(cmd.data[0].decode("utf-8"))

                                # log process
                                logmsg = '{client_nickname}:{client_database_id} - added to {gid}'
                                logging.info(logmsg.format(**event[0], gid=gid))

                            except KeyError:
                                logger.error(str(event.parsed))
                                pass


if __name__ == "__main__":
    # logging
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)

    logf = logging.FileHandler('info.log')
    logf.setFormatter(logging.Formatter("%(asctime)s - %(levelname)-8s - %(message)s"))

    logger.addHandler(logf)

    with ts3.query.TS3Connection(Config.HOST, Config.PORT) as ts3conn:
        ts3conn.login(client_login_name=Config.USER, client_login_password=Config.PW)
        ts3conn.use(sid=Config.SID)
        watcher(ts3conn)