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

# This file is part of Remote Support Desktop
# https://gitlab.das-netzwerkteam.de/RemoteWebApp/rwa.support.sessionservice
# Copyright 2020, 2021 Jonathan Weth <dev@jonathanweth.de>
# Copyright 2020, 2021 Daniel Teichmann <daniel.teichmann@das-netzwerkteam.de>
# Copyright 2020 Mike Gabriel <mike.gabriel@das-netzwerkteam.de>
# SPDX-License-Identifier: GPL-2.0-or-later
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

import argparse
from typing import Union

import dbus


def str2bool(v: Union[str, bool, int]) -> bool:
    """Return true or false if the given string can be interpreted as a boolean.

    If it fails, raise an exception.
    """
    if isinstance(v, bool):
        return v
    if v.lower() in ("yes", "true", "t", "y", "1", 1):
        return True
    elif v.lower() in ("no", "false", "f", "n", "0", 0):
        return False
    else:
        raise argparse.ArgumentTypeError("Boolean value expected.")


def main():
    parser = argparse.ArgumentParser(description="D-Bus Session Service API test client")
    arguments = parser.add_mutually_exclusive_group()
    arguments.add_argument(
        "-s",
        "--start",
        const=True,
        nargs="?",
        default=True,
        help="Starts a new support session by calling the D-Bus Session Service (default)",
    )
    arguments.add_argument("-x", "--stop", help="Stops <PID>, an existing support session.")
    arguments.add_argument(
        "-r", "--refresh_status", help="Refreshes the status of <PID>, an existing support session."
    )
    arguments.add_argument(
        "-i", "--status", help="Gets the status of <PID>, an existing support session."
    )
    args = parser.parse_args()

    start = args.start
    stop_pid = args.stop
    status_pid = args.status
    refresh_pid = args.refresh_status

    bus = dbus.SessionBus()
    req = bus.get_object("org.ArcticaProject.RWASupportSessionService", "/RWASupportSessionService")

    if stop_pid:
        print(f"Sending D-Bus request 'stop': {stop_pid}")
        response = req.stop(stop_pid)
        print(f"Your response is: {response}")
    elif status_pid:
        print(f"Sending D-Bus request 'status': {status_pid}")
        response = req.status(status_pid)
        print(f"Your response is: {response}")
    elif refresh_pid:
        print(f"Sending D-Bus request 'refresh_status': {refresh_pid}")
        response = req.refresh_status(refresh_pid)
        print(f"Your response is: {response}")
    elif start:
        print("Sending D-Bus request 'start'")
        response = req.start()
        print(f"Your response is: {response}")
    else:
        print("No valid input!")
        return


if __name__ == "__main__":
    main()