2006-09-28 13:29:05 +02:00
|
|
|
#!/usr/bin/env python
|
2008-08-15 19:31:51 +02:00
|
|
|
# -*- coding:utf-8 -*-
|
2008-08-15 05:20:23 +02:00
|
|
|
## src/gajim-remote.py
|
2005-08-01 17:23:45 +02:00
|
|
|
##
|
2008-08-15 05:20:23 +02:00
|
|
|
## Copyright (C) 2005-2006 Dimitur Kirov <dkirov AT gmail.com>
|
|
|
|
## Nikos Kouremenos <kourem AT gmail.com>
|
|
|
|
## Copyright (C) 2005-2008 Yann Leboulanger <asterix AT lagaule.org>
|
|
|
|
## Copyright (C) 2006 Junglecow <junglecow AT gmail.com>
|
|
|
|
## Travis Shirk <travis AT pobox.com>
|
|
|
|
## Copyright (C) 2006-2008 Jean-Marie Traissard <jim AT lapin.org>
|
|
|
|
## Copyright (C) 2007 Julien Pivotto <roidelapluie AT gmail.com>
|
2005-08-01 17:23:45 +02:00
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## This file is part of Gajim.
|
|
|
|
##
|
|
|
|
## Gajim is free software; you can redistribute it and/or modify
|
2005-08-01 17:23:45 +02:00
|
|
|
## it under the terms of the GNU General Public License as published
|
2007-10-22 13:13:13 +02:00
|
|
|
## by the Free Software Foundation; version 3 only.
|
2005-08-01 17:23:45 +02:00
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## Gajim is distributed in the hope that it will be useful,
|
2005-08-01 17:23:45 +02:00
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2008-08-15 05:20:23 +02:00
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2005-08-01 17:23:45 +02:00
|
|
|
## GNU General Public License for more details.
|
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## You should have received a copy of the GNU General Public License
|
2008-08-15 05:20:23 +02:00
|
|
|
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
2007-10-22 13:13:13 +02:00
|
|
|
##
|
2005-08-01 17:23:45 +02:00
|
|
|
|
2006-09-28 13:29:05 +02:00
|
|
|
# gajim-remote help will show you the D-BUS API of Gajim
|
2005-08-01 17:23:45 +02:00
|
|
|
|
|
|
|
import sys
|
2008-04-14 17:19:09 +02:00
|
|
|
import os
|
2006-02-09 00:51:46 +01:00
|
|
|
import locale
|
2005-08-01 17:23:45 +02:00
|
|
|
import signal
|
|
|
|
signal.signal(signal.SIGINT, signal.SIG_DFL) # ^C exits the application
|
|
|
|
|
2005-12-01 18:16:53 +01:00
|
|
|
from common import exceptions
|
2005-09-04 13:33:14 +02:00
|
|
|
from common import i18n
|
2005-08-01 17:23:45 +02:00
|
|
|
|
2006-03-13 19:20:08 +01:00
|
|
|
try:
|
|
|
|
PREFERRED_ENCODING = locale.getpreferredencoding()
|
2008-10-11 11:37:13 +02:00
|
|
|
except Exception:
|
2006-03-13 19:20:08 +01:00
|
|
|
PREFERRED_ENCODING = 'UTF-8'
|
2006-02-14 19:44:51 +01:00
|
|
|
|
2005-09-04 12:03:29 +02:00
|
|
|
def send_error(error_message):
|
2006-02-14 19:44:51 +01:00
|
|
|
'''Writes error message to stderr and exits'''
|
|
|
|
print >> sys.stderr, error_message.encode(PREFERRED_ENCODING)
|
2006-04-01 07:01:42 +02:00
|
|
|
sys.exit(1)
|
2005-09-06 15:17:10 +02:00
|
|
|
|
2005-08-01 17:23:45 +02:00
|
|
|
try:
|
2007-11-14 20:47:52 +01:00
|
|
|
if sys.platform == 'darwin':
|
|
|
|
import osx.dbus
|
|
|
|
osx.dbus.load(False)
|
2005-08-01 17:23:45 +02:00
|
|
|
import dbus
|
|
|
|
import dbus.service
|
|
|
|
import dbus.glib
|
2008-10-11 11:37:13 +02:00
|
|
|
except Exception:
|
2006-10-10 20:20:09 +02:00
|
|
|
print str(exceptions.DbusNotSupported())
|
|
|
|
sys.exit(1)
|
2005-08-01 17:23:45 +02:00
|
|
|
|
|
|
|
OBJ_PATH = '/org/gajim/dbus/RemoteObject'
|
|
|
|
INTERFACE = 'org.gajim.dbus.RemoteInterface'
|
|
|
|
SERVICE = 'org.gajim.dbus'
|
|
|
|
BASENAME = 'gajim-remote'
|
|
|
|
|
2006-02-14 19:44:51 +01:00
|
|
|
|
2005-08-01 17:23:45 +02:00
|
|
|
class GajimRemote:
|
2006-08-03 12:47:14 +02:00
|
|
|
|
2005-08-01 17:23:45 +02:00
|
|
|
def __init__(self):
|
2008-02-05 23:13:16 +01:00
|
|
|
self.argv_len = len(sys.argv)
|
2005-08-01 17:23:45 +02:00
|
|
|
# define commands dict. Prototype :
|
|
|
|
# {
|
|
|
|
# 'command': [comment, [list of arguments] ]
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# each argument is defined as a tuple:
|
|
|
|
# (argument name, help on argument, is mandatory)
|
|
|
|
#
|
|
|
|
self.commands = {
|
|
|
|
'help':[
|
2006-09-10 15:14:13 +02:00
|
|
|
_('Shows a help on specific command'),
|
2005-08-01 17:23:45 +02:00
|
|
|
[
|
2005-09-17 14:18:47 +02:00
|
|
|
#User gets help for the command, specified by this parameter
|
2008-02-05 23:13:16 +01:00
|
|
|
(_('command'),
|
2005-08-09 15:39:31 +02:00
|
|
|
_('show help on command'), False)
|
2005-08-01 17:23:45 +02:00
|
|
|
]
|
2008-02-05 23:13:16 +01:00
|
|
|
],
|
2005-08-01 17:23:45 +02:00
|
|
|
'toggle_roster_appearance' : [
|
|
|
|
_('Shows or hides the roster window'),
|
|
|
|
[]
|
2008-02-05 23:13:16 +01:00
|
|
|
],
|
2006-10-20 11:37:24 +02:00
|
|
|
'show_next_pending_event': [
|
2007-01-02 14:36:54 +01:00
|
|
|
_('Pops up a window with the next pending event'),
|
2005-08-01 17:23:45 +02:00
|
|
|
[]
|
|
|
|
],
|
|
|
|
'list_contacts': [
|
2007-01-02 14:36:54 +01:00
|
|
|
_('Prints a list of all contacts in the roster. Each contact '
|
|
|
|
'appears on a separate line'),
|
2005-08-01 17:23:45 +02:00
|
|
|
[
|
2007-01-02 14:36:54 +01:00
|
|
|
(_('account'), _('show only contacts of the given account'),
|
|
|
|
False)
|
2005-08-01 17:23:45 +02:00
|
|
|
]
|
2006-08-03 12:47:14 +02:00
|
|
|
|
2008-02-05 23:13:16 +01:00
|
|
|
],
|
2005-08-01 17:23:45 +02:00
|
|
|
'list_accounts': [
|
2005-11-30 16:26:08 +01:00
|
|
|
_('Prints a list of registered accounts'),
|
2005-08-01 17:23:45 +02:00
|
|
|
[]
|
2008-02-05 23:13:16 +01:00
|
|
|
],
|
2005-08-01 17:23:45 +02:00
|
|
|
'change_status': [
|
2005-11-30 16:26:08 +01:00
|
|
|
_('Changes the status of account or accounts'),
|
2005-08-01 17:23:45 +02:00
|
|
|
[
|
2006-06-09 15:42:33 +02:00
|
|
|
#offline, online, chat, away, xa, dnd, invisible should not be translated
|
2008-02-05 23:13:16 +01:00
|
|
|
(_('status'), _('one of: offline, online, chat, away, xa, dnd, invisible '), True),
|
|
|
|
(_('message'), _('status message'), False),
|
2005-11-30 16:26:08 +01:00
|
|
|
(_('account'), _('change status of account "account". '
|
|
|
|
'If not specified, try to change status of all accounts that have '
|
|
|
|
'"sync with global status" option set'), False)
|
2005-08-01 17:23:45 +02:00
|
|
|
]
|
|
|
|
],
|
2008-02-05 23:13:16 +01:00
|
|
|
'open_chat': [
|
|
|
|
_('Shows the chat dialog so that you can send messages to a contact'),
|
2005-08-01 17:23:45 +02:00
|
|
|
[
|
2005-11-30 16:26:08 +01:00
|
|
|
('jid', _('JID of the contact that you want to chat with'),
|
2008-02-05 23:13:16 +01:00
|
|
|
True),
|
2005-11-30 16:26:08 +01:00
|
|
|
(_('account'), _('if specified, contact is taken from the '
|
|
|
|
'contact list of this account'), False)
|
2005-08-01 17:23:45 +02:00
|
|
|
]
|
|
|
|
],
|
2006-11-21 22:04:28 +01:00
|
|
|
'send_chat_message':[
|
2006-06-16 18:09:46 +02:00
|
|
|
_('Sends new chat message to a contact in the roster. Both OpenPGP key '
|
2005-11-30 16:26:08 +01:00
|
|
|
'and account are optional. If you want to set only \'account\', '
|
2008-02-05 23:13:16 +01:00
|
|
|
'without \'OpenPGP key\', just set \'OpenPGP key\' to \'\'.'),
|
2005-08-01 17:23:45 +02:00
|
|
|
[
|
2005-11-30 16:26:08 +01:00
|
|
|
('jid', _('JID of the contact that will receive the message'), True),
|
2005-08-01 17:23:45 +02:00
|
|
|
(_('message'), _('message contents'), True),
|
2005-11-30 16:26:08 +01:00
|
|
|
(_('pgp key'), _('if specified, the message will be encrypted '
|
|
|
|
'using this public key'), False),
|
|
|
|
(_('account'), _('if specified, the message will be sent '
|
|
|
|
'using this account'), False),
|
2005-08-01 17:23:45 +02:00
|
|
|
]
|
2006-06-16 18:09:46 +02:00
|
|
|
],
|
|
|
|
'send_single_message':[
|
|
|
|
_('Sends new single message to a contact in the roster. Both OpenPGP key '
|
|
|
|
'and account are optional. If you want to set only \'account\', '
|
2008-02-05 23:13:16 +01:00
|
|
|
'without \'OpenPGP key\', just set \'OpenPGP key\' to \'\'.'),
|
2006-06-16 18:09:46 +02:00
|
|
|
[
|
|
|
|
('jid', _('JID of the contact that will receive the message'), True),
|
|
|
|
(_('subject'), _('message subject'), True),
|
|
|
|
(_('message'), _('message contents'), True),
|
|
|
|
(_('pgp key'), _('if specified, the message will be encrypted '
|
|
|
|
'using this public key'), False),
|
|
|
|
(_('account'), _('if specified, the message will be sent '
|
|
|
|
'using this account'), False),
|
|
|
|
]
|
2008-02-05 23:13:16 +01:00
|
|
|
],
|
2007-07-18 14:07:21 +02:00
|
|
|
'send_groupchat_message':[
|
2008-02-05 23:13:16 +01:00
|
|
|
_('Sends new message to a groupchat you\'ve joined.'),
|
2007-07-18 14:07:21 +02:00
|
|
|
[
|
|
|
|
('room_jid', _('JID of the room that will receive the message'), True),
|
|
|
|
(_('message'), _('message contents'), True),
|
|
|
|
(_('account'), _('if specified, the message will be sent '
|
|
|
|
'using this account'), False),
|
|
|
|
]
|
|
|
|
],
|
2005-08-01 17:23:45 +02:00
|
|
|
'contact_info': [
|
2008-02-05 23:13:16 +01:00
|
|
|
_('Gets detailed info on a contact'),
|
2005-08-01 17:23:45 +02:00
|
|
|
[
|
2005-08-19 13:33:58 +02:00
|
|
|
('jid', _('JID of the contact'), True)
|
2005-08-01 17:23:45 +02:00
|
|
|
]
|
2005-08-24 01:41:23 +02:00
|
|
|
],
|
2006-02-12 20:07:38 +01:00
|
|
|
'account_info': [
|
2008-02-05 23:13:16 +01:00
|
|
|
_('Gets detailed info on a account'),
|
2006-02-12 20:07:38 +01:00
|
|
|
[
|
|
|
|
('account', _('Name of the account'), True)
|
|
|
|
]
|
|
|
|
],
|
2005-08-24 01:41:23 +02:00
|
|
|
'send_file': [
|
2005-11-30 16:26:08 +01:00
|
|
|
_('Sends file to a contact'),
|
2005-08-24 01:41:23 +02:00
|
|
|
[
|
|
|
|
(_('file'), _('File path'), True),
|
|
|
|
('jid', _('JID of the contact'), True),
|
2005-11-30 16:26:08 +01:00
|
|
|
(_('account'), _('if specified, file will be sent using this '
|
|
|
|
'account'), False)
|
2005-08-24 01:41:23 +02:00
|
|
|
]
|
2005-09-09 17:29:32 +02:00
|
|
|
],
|
|
|
|
'prefs_list': [
|
2005-11-30 16:26:08 +01:00
|
|
|
_('Lists all preferences and their values'),
|
2005-09-09 17:29:32 +02:00
|
|
|
[ ]
|
|
|
|
],
|
|
|
|
'prefs_put': [
|
2005-11-30 16:26:08 +01:00
|
|
|
_('Sets value of \'key\' to \'value\'.'),
|
2005-09-09 17:29:32 +02:00
|
|
|
[
|
2005-11-30 16:26:08 +01:00
|
|
|
(_('key=value'), _('\'key\' is the name of the preference, '
|
|
|
|
'\'value\' is the value to set it to'), True)
|
2005-09-09 17:29:32 +02:00
|
|
|
]
|
|
|
|
],
|
|
|
|
'prefs_del': [
|
2005-11-30 16:26:08 +01:00
|
|
|
_('Deletes a preference item'),
|
2008-02-05 23:13:16 +01:00
|
|
|
[
|
|
|
|
(_('key'), _('name of the preference to be deleted'), True)
|
2005-09-09 17:29:32 +02:00
|
|
|
]
|
|
|
|
],
|
|
|
|
'prefs_store': [
|
2005-11-30 16:26:08 +01:00
|
|
|
_('Writes the current state of Gajim preferences to the .config '
|
|
|
|
'file'),
|
2005-09-09 17:29:32 +02:00
|
|
|
[ ]
|
2005-09-10 00:22:05 +02:00
|
|
|
],
|
|
|
|
'remove_contact': [
|
2005-11-30 16:26:08 +01:00
|
|
|
_('Removes contact from roster'),
|
2008-02-05 23:13:16 +01:00
|
|
|
[
|
2005-09-10 00:22:05 +02:00
|
|
|
('jid', _('JID of the contact'), True),
|
2005-11-30 16:26:08 +01:00
|
|
|
(_('account'), _('if specified, contact is taken from the '
|
|
|
|
'contact list of this account'), False)
|
2006-08-03 12:47:14 +02:00
|
|
|
|
2005-09-10 00:22:05 +02:00
|
|
|
]
|
|
|
|
],
|
|
|
|
'add_contact': [
|
2005-11-30 16:26:08 +01:00
|
|
|
_('Adds contact to roster'),
|
2008-02-05 23:13:16 +01:00
|
|
|
[
|
2006-05-19 23:13:45 +02:00
|
|
|
(_('jid'), _('JID of the contact'), True),
|
|
|
|
(_('account'), _('Adds new contact to this account'), False)
|
2005-09-10 00:22:05 +02:00
|
|
|
]
|
2005-11-30 16:26:08 +01:00
|
|
|
],
|
2006-08-03 12:47:14 +02:00
|
|
|
|
2005-11-30 16:26:08 +01:00
|
|
|
'get_status': [
|
2005-11-30 17:17:06 +01:00
|
|
|
_('Returns current status (the global one unless account is specified)'),
|
2005-11-30 16:26:08 +01:00
|
|
|
[
|
2008-08-01 14:05:01 +02:00
|
|
|
(_('account'), '', False)
|
2005-11-30 16:26:08 +01:00
|
|
|
]
|
|
|
|
],
|
2006-08-03 12:47:14 +02:00
|
|
|
|
2005-12-30 22:37:36 +01:00
|
|
|
'get_status_message': [
|
2008-09-01 19:35:59 +02:00
|
|
|
_('Returns current status message (the global one unless account is specified)'),
|
2005-12-30 22:37:36 +01:00
|
|
|
[
|
2008-08-01 14:05:01 +02:00
|
|
|
(_('account'), '', False)
|
2005-12-30 22:37:36 +01:00
|
|
|
]
|
2008-02-05 23:13:16 +01:00
|
|
|
],
|
2006-02-20 18:25:26 +01:00
|
|
|
|
|
|
|
'get_unread_msgs_number': [
|
2007-01-02 13:17:51 +01:00
|
|
|
_('Returns number of unread messages'),
|
2006-02-20 18:25:26 +01:00
|
|
|
[ ]
|
|
|
|
],
|
2006-05-10 00:13:46 +02:00
|
|
|
'start_chat': [
|
2006-09-10 15:14:13 +02:00
|
|
|
_('Opens \'Start Chat\' dialog'),
|
2006-05-10 00:13:46 +02:00
|
|
|
[
|
|
|
|
(_('account'), _('Starts chat, using this account'), True)
|
|
|
|
]
|
|
|
|
],
|
2006-09-09 20:25:49 +02:00
|
|
|
'send_xml': [
|
2008-02-05 23:13:16 +01:00
|
|
|
_('Sends custom XML'),
|
2006-09-09 20:25:49 +02:00
|
|
|
[
|
|
|
|
('xml', _('XML to send'), True),
|
2006-09-10 15:19:42 +02:00
|
|
|
('account', _('Account in which the xml will be sent; '
|
|
|
|
'if not specified, xml will be sent to all accounts'),
|
|
|
|
False)
|
2006-09-09 20:25:49 +02:00
|
|
|
]
|
|
|
|
],
|
2007-01-02 03:45:43 +01:00
|
|
|
'handle_uri': [
|
|
|
|
_('Handle a xmpp:/ uri'),
|
|
|
|
[
|
2008-08-01 14:05:01 +02:00
|
|
|
(_('uri'), _('URI to handle'), True),
|
|
|
|
(_('account'), _('Account in which you want to handle it'),
|
|
|
|
False)
|
2007-01-02 03:45:43 +01:00
|
|
|
]
|
|
|
|
],
|
|
|
|
'join_room': [
|
|
|
|
_('Join a MUC room'),
|
|
|
|
[
|
2008-08-01 14:05:01 +02:00
|
|
|
(_('room'), _('Room JID'), True),
|
|
|
|
(_('nick'), _('Nickname to use'), False),
|
|
|
|
(_('password'), _('Password to enter the room'), False),
|
|
|
|
(_('account'), _('Account from which you want to enter the '
|
|
|
|
'room'), False)
|
2007-01-02 03:45:43 +01:00
|
|
|
]
|
|
|
|
],
|
2008-02-05 23:13:16 +01:00
|
|
|
'check_gajim_running':[
|
|
|
|
_('Check if Gajim is running'),
|
|
|
|
[]
|
|
|
|
],
|
2008-04-14 17:19:09 +02:00
|
|
|
'toggle_ipython' : [
|
|
|
|
_('Shows or hides the ipython window'),
|
|
|
|
[]
|
|
|
|
],
|
2007-01-02 03:45:43 +01:00
|
|
|
|
2005-08-01 17:23:45 +02:00
|
|
|
}
|
2008-04-14 17:19:09 +02:00
|
|
|
|
2008-02-05 23:13:16 +01:00
|
|
|
self.sbus = None
|
2008-04-14 17:19:09 +02:00
|
|
|
if self.argv_len < 2 or sys.argv[1] not in self.commands.keys():
|
|
|
|
# no args or bad args
|
2005-09-04 12:03:29 +02:00
|
|
|
send_error(self.compose_help())
|
2005-08-01 17:23:45 +02:00
|
|
|
self.command = sys.argv[1]
|
|
|
|
if self.command == 'help':
|
|
|
|
if self.argv_len == 3:
|
2006-02-14 19:44:51 +01:00
|
|
|
print self.help_on_command(sys.argv[2]).encode(PREFERRED_ENCODING)
|
2005-08-01 17:23:45 +02:00
|
|
|
else:
|
2006-02-14 19:44:51 +01:00
|
|
|
print self.compose_help().encode(PREFERRED_ENCODING)
|
2006-04-01 07:01:42 +02:00
|
|
|
sys.exit(0)
|
2007-01-02 03:45:43 +01:00
|
|
|
if self.command == 'handle_uri':
|
|
|
|
self.handle_uri()
|
2008-02-05 23:13:16 +01:00
|
|
|
if self.command == 'check_gajim_running':
|
|
|
|
print self.check_gajim_running()
|
|
|
|
sys.exit(0)
|
2005-08-01 17:23:45 +02:00
|
|
|
self.init_connection()
|
2005-07-21 23:00:18 +02:00
|
|
|
self.check_arguments()
|
2006-08-03 12:47:14 +02:00
|
|
|
|
2005-08-01 17:23:45 +02:00
|
|
|
if self.command == 'contact_info':
|
|
|
|
if self.argv_len < 3:
|
2005-09-04 12:03:29 +02:00
|
|
|
send_error(_('Missing argument "contact_jid"'))
|
2006-08-03 12:47:14 +02:00
|
|
|
|
2006-04-01 07:01:42 +02:00
|
|
|
try:
|
|
|
|
res = self.call_remote_method()
|
|
|
|
except exceptions.ServiceNotAvailable:
|
|
|
|
# At this point an error message has already been displayed
|
|
|
|
sys.exit(1)
|
|
|
|
else:
|
|
|
|
self.print_result(res)
|
2006-08-03 12:47:14 +02:00
|
|
|
|
2005-07-26 17:04:40 +02:00
|
|
|
def print_result(self, res):
|
|
|
|
''' Print retrieved result to the output '''
|
|
|
|
if res is not None:
|
2006-11-22 18:19:50 +01:00
|
|
|
if self.command in ('open_chat', 'send_chat_message', 'send_single_message', 'start_chat'):
|
2006-06-16 18:09:46 +02:00
|
|
|
if self.command in ('send_message', 'send_single_message'):
|
2005-07-26 17:04:40 +02:00
|
|
|
self.argv_len -= 2
|
2006-08-03 12:47:14 +02:00
|
|
|
|
2005-09-07 23:12:30 +02:00
|
|
|
if res is False:
|
2005-07-26 17:04:40 +02:00
|
|
|
if self.argv_len < 4:
|
2005-11-30 16:26:08 +01:00
|
|
|
send_error(_('\'%s\' is not in your roster.\n'
|
|
|
|
'Please specify account for sending the message.') % sys.argv[2])
|
2005-07-26 17:04:40 +02:00
|
|
|
else:
|
2005-09-04 12:03:29 +02:00
|
|
|
send_error(_('You have no active account'))
|
2005-07-26 17:04:40 +02:00
|
|
|
elif self.command == 'list_accounts':
|
2005-08-26 14:35:00 +02:00
|
|
|
if isinstance(res, list):
|
2005-07-26 17:04:40 +02:00
|
|
|
for account in res:
|
2006-02-27 19:07:38 +01:00
|
|
|
if isinstance(account, unicode):
|
|
|
|
print account.encode(PREFERRED_ENCODING)
|
|
|
|
else:
|
|
|
|
print account
|
2006-02-12 20:07:38 +01:00
|
|
|
elif self.command == 'account_info':
|
|
|
|
if res:
|
2006-02-18 09:59:32 +01:00
|
|
|
print self.print_info(0, res, True)
|
2005-07-26 17:04:40 +02:00
|
|
|
elif self.command == 'list_contacts':
|
2006-02-18 09:59:32 +01:00
|
|
|
for account_dict in res:
|
|
|
|
print self.print_info(0, account_dict, True)
|
2005-09-09 17:29:32 +02:00
|
|
|
elif self.command == 'prefs_list':
|
2008-10-11 11:59:52 +02:00
|
|
|
pref_keys = sorted(res.keys())
|
2005-09-09 17:29:32 +02:00
|
|
|
for pref_key in pref_keys:
|
2006-02-27 18:14:41 +01:00
|
|
|
result = '%s = %s' % (pref_key, res[pref_key])
|
2008-02-05 23:13:16 +01:00
|
|
|
if isinstance(result, unicode):
|
2006-02-27 19:07:38 +01:00
|
|
|
print result.encode(PREFERRED_ENCODING)
|
|
|
|
else:
|
|
|
|
print result
|
2006-02-18 09:59:32 +01:00
|
|
|
elif self.command == 'contact_info':
|
|
|
|
print self.print_info(0, res, True)
|
2005-07-26 17:04:40 +02:00
|
|
|
elif res:
|
2006-02-27 18:14:41 +01:00
|
|
|
print unicode(res).encode(PREFERRED_ENCODING)
|
2006-08-03 12:47:14 +02:00
|
|
|
|
2008-02-05 23:13:16 +01:00
|
|
|
def check_gajim_running(self):
|
|
|
|
if not self.sbus:
|
|
|
|
try:
|
|
|
|
self.sbus = dbus.SessionBus()
|
2008-10-11 11:37:13 +02:00
|
|
|
except Exception:
|
2008-02-05 23:13:16 +01:00
|
|
|
raise exceptions.SessionBusNotPresent
|
|
|
|
|
|
|
|
test = False
|
|
|
|
if hasattr(self.sbus, 'name_has_owner'):
|
|
|
|
if self.sbus.name_has_owner(SERVICE):
|
|
|
|
test = True
|
|
|
|
elif dbus.dbus_bindings.bus_name_has_owner(self.sbus.get_connection(),
|
|
|
|
SERVICE):
|
|
|
|
test = True
|
|
|
|
return test
|
|
|
|
|
2005-07-26 17:04:40 +02:00
|
|
|
def init_connection(self):
|
|
|
|
''' create the onnection to the session dbus,
|
|
|
|
or exit if it is not possible '''
|
|
|
|
try:
|
|
|
|
self.sbus = dbus.SessionBus()
|
2008-10-11 11:37:13 +02:00
|
|
|
except Exception:
|
2005-12-01 18:16:53 +01:00
|
|
|
raise exceptions.SessionBusNotPresent
|
2006-08-03 12:47:14 +02:00
|
|
|
|
2008-02-05 23:13:16 +01:00
|
|
|
if not self.check_gajim_running():
|
|
|
|
send_error(_('It seems Gajim is not running. So you can\'t use gajim-remote.'))
|
2006-09-25 14:38:42 +02:00
|
|
|
obj = self.sbus.get_object(SERVICE, OBJ_PATH)
|
|
|
|
interface = dbus.Interface(obj, INTERFACE)
|
2006-08-03 12:47:14 +02:00
|
|
|
|
2005-07-26 17:04:40 +02:00
|
|
|
# get the function asked
|
2005-08-12 01:20:24 +02:00
|
|
|
self.method = interface.__getattr__(self.command)
|
2006-08-03 12:47:14 +02:00
|
|
|
|
2005-08-01 17:23:45 +02:00
|
|
|
def make_arguments_row(self, args):
|
|
|
|
''' return arguments list. Mandatory arguments are enclosed with:
|
|
|
|
'<', '>', optional arguments - with '[', ']' '''
|
|
|
|
str = ''
|
|
|
|
for argument in args:
|
|
|
|
str += ' '
|
|
|
|
if argument[2]:
|
|
|
|
str += '<'
|
|
|
|
else:
|
|
|
|
str += '['
|
|
|
|
str += argument[0]
|
|
|
|
if argument[2]:
|
|
|
|
str += '>'
|
|
|
|
else:
|
|
|
|
str += ']'
|
|
|
|
return str
|
2006-08-03 12:47:14 +02:00
|
|
|
|
2005-08-01 17:23:45 +02:00
|
|
|
def help_on_command(self, command):
|
|
|
|
''' return help message for a given command '''
|
|
|
|
if command in self.commands:
|
|
|
|
command_props = self.commands[command]
|
|
|
|
arguments_str = self.make_arguments_row(command_props[1])
|
2008-08-01 15:03:23 +02:00
|
|
|
str = _('Usage: %(basename)s %(command)s %(arguments)s \n\t %(help)s')\
|
|
|
|
% {'basename': BASENAME, 'command': command,
|
|
|
|
'arguments': arguments_str, 'help': command_props[0]}
|
2005-09-09 17:29:32 +02:00
|
|
|
if len(command_props[1]) > 0:
|
2006-02-12 17:04:45 +01:00
|
|
|
str += '\n\n' + _('Arguments:') + '\n'
|
2005-09-09 17:29:32 +02:00
|
|
|
for argument in command_props[1]:
|
|
|
|
str += ' ' + argument[0] + ' - ' + argument[1] + '\n'
|
2005-08-01 17:23:45 +02:00
|
|
|
return str
|
2005-09-04 12:03:29 +02:00
|
|
|
send_error(_('%s not found') % command)
|
2006-08-03 12:47:14 +02:00
|
|
|
|
2005-08-01 17:23:45 +02:00
|
|
|
def compose_help(self):
|
|
|
|
''' print usage, and list available commands '''
|
|
|
|
str = _('Usage: %s command [arguments]\nCommand is one of:\n' ) % BASENAME
|
2008-10-11 11:59:52 +02:00
|
|
|
commands = sorted(self.commands.keys())
|
2005-09-09 17:29:32 +02:00
|
|
|
for command in commands:
|
2008-02-05 23:13:16 +01:00
|
|
|
str += ' ' + command
|
2005-08-01 17:23:45 +02:00
|
|
|
for argument in self.commands[command][1]:
|
|
|
|
str += ' '
|
|
|
|
if argument[2]:
|
|
|
|
str += '<'
|
|
|
|
else:
|
|
|
|
str += '['
|
|
|
|
str += argument[0]
|
|
|
|
if argument[2]:
|
|
|
|
str += '>'
|
|
|
|
else:
|
|
|
|
str += ']'
|
|
|
|
str += '\n'
|
|
|
|
return str
|
2006-08-03 12:47:14 +02:00
|
|
|
|
2006-02-18 09:59:32 +01:00
|
|
|
def print_info(self, level, prop_dict, encode_return = False):
|
|
|
|
''' return formated string from data structure '''
|
2006-02-12 16:33:45 +01:00
|
|
|
if prop_dict is None or not isinstance(prop_dict, (dict, list, tuple)):
|
2005-08-01 17:23:45 +02:00
|
|
|
return ''
|
2005-07-26 17:04:40 +02:00
|
|
|
ret_str = ''
|
2006-02-12 16:33:45 +01:00
|
|
|
if isinstance(prop_dict, (list, tuple)):
|
2005-07-26 17:04:40 +02:00
|
|
|
ret_str = ''
|
|
|
|
spacing = ' ' * level * 4
|
|
|
|
for val in prop_dict:
|
|
|
|
if val is None:
|
|
|
|
ret_str +='\t'
|
2006-02-18 09:59:32 +01:00
|
|
|
elif isinstance(val, int):
|
2005-07-26 17:04:40 +02:00
|
|
|
ret_str +='\t' + str(val)
|
2006-02-18 09:59:32 +01:00
|
|
|
elif isinstance(val, (str, unicode)):
|
2006-02-12 17:04:45 +01:00
|
|
|
ret_str +='\t' + val
|
2006-02-12 16:33:45 +01:00
|
|
|
elif isinstance(val, (list, tuple)):
|
2005-07-26 17:04:40 +02:00
|
|
|
res = ''
|
|
|
|
for items in val:
|
|
|
|
res += self.print_info(level+1, items)
|
|
|
|
if res != '':
|
|
|
|
ret_str += '\t' + res
|
2006-02-09 01:00:17 +01:00
|
|
|
elif isinstance(val, dict):
|
2005-09-06 15:17:10 +02:00
|
|
|
ret_str += self.print_info(level+1, val)
|
2005-07-26 17:04:40 +02:00
|
|
|
ret_str = '%s(%s)\n' % (spacing, ret_str[1:])
|
2005-08-26 14:35:00 +02:00
|
|
|
elif isinstance(prop_dict, dict):
|
2005-08-01 17:23:45 +02:00
|
|
|
for key in prop_dict.keys():
|
|
|
|
val = prop_dict[key]
|
|
|
|
spacing = ' ' * level * 4
|
2006-02-12 16:33:45 +01:00
|
|
|
if isinstance(val, (unicode, int, str)):
|
2005-08-01 17:23:45 +02:00
|
|
|
if val is not None:
|
|
|
|
val = val.strip()
|
|
|
|
ret_str += '%s%-10s: %s\n' % (spacing, key, val)
|
2006-02-12 16:33:45 +01:00
|
|
|
elif isinstance(val, (list, tuple)):
|
2005-08-01 17:23:45 +02:00
|
|
|
res = ''
|
|
|
|
for items in val:
|
|
|
|
res += self.print_info(level+1, items)
|
|
|
|
if res != '':
|
|
|
|
ret_str += '%s%s: \n%s' % (spacing, key, res)
|
2005-08-26 14:35:00 +02:00
|
|
|
elif isinstance(val, dict):
|
2005-08-01 17:23:45 +02:00
|
|
|
res = self.print_info(level+1, val)
|
|
|
|
if res != '':
|
|
|
|
ret_str += '%s%s: \n%s' % (spacing, key, res)
|
2006-02-18 09:59:32 +01:00
|
|
|
if (encode_return):
|
|
|
|
try:
|
|
|
|
ret_str = ret_str.encode(PREFERRED_ENCODING)
|
2008-10-11 11:37:13 +02:00
|
|
|
except Exception:
|
2006-02-18 09:59:32 +01:00
|
|
|
pass
|
|
|
|
return ret_str
|
2006-08-03 12:47:14 +02:00
|
|
|
|
2005-08-01 17:23:45 +02:00
|
|
|
def check_arguments(self):
|
|
|
|
''' Make check if all necessary arguments are given '''
|
|
|
|
argv_len = self.argv_len - 2
|
|
|
|
args = self.commands[self.command][1]
|
2007-01-11 18:58:44 +01:00
|
|
|
if len(args) < argv_len:
|
|
|
|
send_error(_('Too many arguments. \n'
|
2008-08-01 15:03:23 +02:00
|
|
|
'Type "%(basename)s help %(command)s" for more info') % {
|
|
|
|
'basename': BASENAME, 'command': self.command})
|
2005-08-01 17:23:45 +02:00
|
|
|
if len(args) > argv_len:
|
|
|
|
if args[argv_len][2]:
|
2008-08-01 15:03:23 +02:00
|
|
|
send_error(_('Argument "%(arg)s" is not specified. \n'
|
|
|
|
'Type "%(basename)s help %(command)s" for more info') %
|
|
|
|
{'arg': args[argv_len][0], 'basename': BASENAME,
|
|
|
|
'command': self.command})
|
2007-01-11 18:58:44 +01:00
|
|
|
self.arguments = []
|
|
|
|
i = 0
|
|
|
|
for arg in sys.argv[2:]:
|
|
|
|
i += 1
|
|
|
|
if i < len(args):
|
|
|
|
self.arguments.append(arg)
|
|
|
|
else:
|
|
|
|
# it's latest argument with spaces
|
|
|
|
self.arguments.append(' '.join(sys.argv[i+1:]))
|
|
|
|
break
|
|
|
|
# add empty string for missing args
|
|
|
|
self.arguments += ['']*(len(args)-i)
|
2006-08-03 12:47:14 +02:00
|
|
|
|
2007-01-02 03:45:43 +01:00
|
|
|
def handle_uri(self):
|
2008-08-26 14:08:36 +02:00
|
|
|
if not sys.argv[2].startswith('xmpp:'):
|
2007-01-02 03:45:43 +01:00
|
|
|
send_error(_('Wrong uri'))
|
|
|
|
sys.argv[2] = sys.argv[2][5:]
|
2008-08-26 14:08:36 +02:00
|
|
|
uri = sys.argv[2]
|
2007-01-02 03:45:43 +01:00
|
|
|
if not '?' in uri:
|
|
|
|
self.command = sys.argv[1] = 'open_chat'
|
|
|
|
return
|
|
|
|
(jid, action) = uri.split('?', 1)
|
|
|
|
sys.argv[2] = jid
|
|
|
|
if action == 'join':
|
|
|
|
self.command = sys.argv[1] = 'join_room'
|
2008-05-27 18:47:33 +02:00
|
|
|
# Move account parameter from position 3 to 5
|
|
|
|
sys.argv.append('')
|
|
|
|
sys.argv.append(sys.argv[3])
|
|
|
|
sys.argv[3] = ''
|
2007-01-02 03:45:43 +01:00
|
|
|
return
|
2008-02-05 23:13:16 +01:00
|
|
|
|
2007-01-02 03:45:43 +01:00
|
|
|
sys.exit(0)
|
|
|
|
|
2005-07-21 23:00:18 +02:00
|
|
|
def call_remote_method(self):
|
2005-08-01 17:23:45 +02:00
|
|
|
''' calls self.method with arguments from sys.argv[2:] '''
|
2007-01-11 18:58:44 +01:00
|
|
|
args = [i.decode(PREFERRED_ENCODING) for i in self.arguments]
|
2006-09-25 14:38:42 +02:00
|
|
|
args = [dbus.String(i) for i in args]
|
2006-02-18 17:34:38 +01:00
|
|
|
try:
|
2006-02-18 09:59:32 +01:00
|
|
|
res = self.method(*args)
|
2005-08-01 17:23:45 +02:00
|
|
|
return res
|
2006-02-18 09:59:32 +01:00
|
|
|
except Exception:
|
2005-12-01 18:16:53 +01:00
|
|
|
raise exceptions.ServiceNotAvailable
|
2005-08-01 17:23:45 +02:00
|
|
|
return None
|
|
|
|
|
2005-07-21 23:00:18 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
GajimRemote()
|
2008-07-29 21:49:31 +02:00
|
|
|
|
2008-08-01 14:05:01 +02:00
|
|
|
# vim: se ts=3:
|