Welcome to mirror list, hosted at ThFree Co, Russian Federation.

remote.py « gajim - dev.gajim.org/gajim/gajim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 521477b54fb3cddfce386fba2e5c0e93aa8e156d (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# This file is part of Gajim.
#
# Gajim 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; version 3 only.
#
# Gajim 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 Gajim. If not, see <http://www.gnu.org/licenses/>.

from typing import Any

import argparse
import logging
import sys

from gi.repository import Gio
from gi.repository import GLib

LOG_FORMAT = '%(asctime)s %(levelname)s %(message)s'
logging.basicConfig(level=logging.INFO, format=LOG_FORMAT)
log = logging.getLogger()


OBJ_PATH = '/org/gajim/dbus/RemoteObject'
INTERFACE = 'org.gajim.dbus.RemoteInterface'
SERVICE = 'org.gajim.Gajim'

SIGNATURES = {
    'list_contacts': '(s)',
    'list_accounts': '()',
    'change_status': '(sss)',
    'send_chat_message': '(sss)',
    'send_groupchat_message': '(sss)',
    'account_info': '(s)',
    'get_status': '(s)',
    'get_status_message': '(s)',
    'get_unread_msgs_number': '()',
}


def call_method(args: argparse.Namespace) -> Any:
    arg_dict = vars(args)
    app_id = arg_dict.pop('app_id')
    command = arg_dict.pop('command')

    proxy = Gio.DBusProxy.new_for_bus_sync(
        Gio.BusType.SESSION,
        Gio.DBusProxyFlags.NONE,
        None,
        app_id,
        OBJ_PATH,
        INTERFACE,
        None)

    arguments = tuple(arg_dict.values())
    signature = SIGNATURES[command]
    method = getattr(proxy, command)
    return method(signature, *arguments)


def create_arg_parser() -> argparse.ArgumentParser:

    account_help = 'The account the command is executed for'

    parser = argparse.ArgumentParser(prog='gajim-remote')
    parser.add_argument('--app-id', default=SERVICE)

    subparsers = parser.add_subparsers(required=True,
                                       metavar='commands',
                                       dest='command')

    subparser = subparsers.add_parser(
        'list_contacts',
        help='Get all roster contacts')
    subparser.add_argument('account', type=str)

    subparser = subparsers.add_parser(
        'list_accounts',
        help='Get the list of accounts')

    subparser = subparsers.add_parser(
        'change_status',
        help='Change the status')
    subparser.add_argument('status',
                           choices=['offline', 'online', 'away', 'xa', 'dnd'])
    subparser.add_argument('message', type=str)
    subparser.add_argument('account', type=str)

    subparser = subparsers.add_parser(
        'send_chat_message',
        help='Send a chat message to a contact')
    subparser.add_argument('address', type=str,
                           help='The XMPP address of the contact')
    subparser.add_argument('message', type=str,
                           help='The message to be sent')
    subparser.add_argument('account', type=str,
                           help=account_help)

    subparser = subparsers.add_parser(
        'send_groupchat_message',
        help='Send a chat message to a group chat')
    subparser.add_argument('address', type=str,
                           help='The XMPP address of the group chat')
    subparser.add_argument('message', type=str,
                           help='The message to be sent')
    subparser.add_argument('account', type=str,
                           help=account_help)

    subparser = subparsers.add_parser(
        'account_info',
        help='Get account details')
    subparser.add_argument('account', type=str, help=account_help)

    subparser = subparsers.add_parser(
        'get_status',
        help='Get the current status')
    subparser.add_argument('account', type=str, help=account_help)

    subparser = subparsers.add_parser(
        'get_status_message',
        help='Get the current status message')
    subparser.add_argument('account', type=str, help=account_help)

    subparser = subparsers.add_parser(
        'get_unread_msgs_number',
        help='Get the unread message count')

    return parser


def run() -> None:
    args = create_arg_parser().parse_args()
    try:
        result = call_method(args)
    except GLib.Error as error:
        quark = GLib.quark_try_string('g-dbus-error-quark')
        if error.matches(quark, Gio.DBusError.SERVICE_UNKNOWN):
            log.error('Service not found. Check if Gajim is running. '
                      'If Gajim is running under a custom profile use '
                      '--app-id=org.gajim.Gajim.myprofilename')

        else:
            log.exception('Failed to execute method')

        sys.exit(1)

    except Exception:
        log.exception('Failed to execute method')
        sys.exit(1)

    print(result)