2005-07-17 23:41:54 +02:00
#!/usr/bin/env python
## scripts/gajim-remote.py
##
## Gajim Team:
## - Yann Le Boulanger <asterix@lagaule.org>
## - Vincent Hanquez <tab@snarc.org>
## - Nikos Kouremenos <kourem@gmail.com>
2005-07-20 12:08:11 +02:00
## - Dimitur Kirov <dkirov@gmail.com>
2005-07-17 23:41:54 +02:00
##
## This file was initially written by Dimitur Kirov
##
## Copyright (C) 2003-2005 Gajim Team
##
## 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; version 2 only.
##
## 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.
##
2005-07-18 00:00:40 +02:00
# gajim-remote help will show you the DBUS API of Gajim
2005-07-17 23:41:54 +02:00
import sys
import gtk
import gobject
2005-07-18 00:38:38 +02:00
import signal
signal . signal ( signal . SIGINT , signal . SIG_DFL ) # ^C exits the application
2005-07-20 02:27:40 +02:00
import i18n
_ = i18n . _
i18n . init ( )
2005-07-20 10:09:10 +02:00
2005-07-17 23:41:54 +02:00
def send_error ( error_message ) :
2005-07-18 00:00:40 +02:00
sys . stderr . write ( error_message + ' \n ' )
2005-07-17 23:41:54 +02:00
sys . stderr . flush ( )
sys . exit ( 1 )
try :
import dbus
except :
send_error ( ' Dbus is not supported. \n ' )
_version = getattr ( dbus , ' version ' , ( 0 , 20 , 0 ) )
2005-07-20 02:27:40 +02:00
if _version [ 1 ] > = 41 :
import dbus . service
import dbus . glib
2005-07-17 23:41:54 +02:00
OBJ_PATH = ' /org/gajim/dbus/RemoteObject '
INTERFACE = ' org.gajim.dbus.RemoteInterface '
SERVICE = ' org.gajim.dbus '
2005-07-20 09:56:30 +02:00
BASENAME = ' gajim-remote '
2005-07-20 03:29:40 +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)
#
2005-07-20 02:27:40 +02:00
commands = {
' help ' : [
_ ( ' show a help on specific command ' ) ,
[
( _ ( ' on_command ' ) , _ ( ' show help on command ' ) , False )
]
] ,
' toggle_roster_appearance ' : [
_ ( ' Shows or hides the roster window ' ) ,
[ ]
] ,
' show_next_unread ' : [
_ ( ' Popup a window with the next unread message ' ) ,
[ ]
] ,
' list_contacts ' : [
_ ( ' Print a list of all contacts in the roster. \
Each contact appear on a separate line ' ),
[
2005-07-20 12:08:11 +02:00
( _ ( ' account ' ) , _ ( ' show only contacts of the given account ' ) ,
False )
2005-07-20 02:27:40 +02:00
]
] ,
' list_accounts ' : [
_ ( ' Print a list of registered accounts ' ) ,
[ ]
] ,
' change_status ' : [
_ ( ' Change ' ) ,
[
( _ ( ' status ' ) , _ ( ' one of: offline, online, chat, \
away , xa , dnd , invisible ' ), True),
( _ ( ' message ' ) , _ ( ' status message ' ) , False ) ,
2005-07-20 12:08:11 +02:00
( _ ( ' account ' ) , _ ( ' change status of the account " accounts " . \
If not specified try to change status of all accounts that \
have " sync with global status " option set ' ), False)
2005-07-20 02:27:40 +02:00
]
] ,
2005-07-20 15:27:15 +02:00
' open_chat ' : [
2005-07-20 02:27:40 +02:00
_ ( ' Show the chat dialog so that you can send message to a contact ' ) ,
[
2005-07-20 15:27:15 +02:00
( ' jid ' , _ ( ' jid of the contact that you want to chat with ' ) ,
2005-07-20 12:08:11 +02:00
True ) ,
2005-07-20 15:27:15 +02:00
( _ ( ' account ' ) , _ ( ' if specified contact is taken from the contact \
list of this account ' ), False)
2005-07-20 02:27:40 +02:00
]
] ,
' send_message ' : [
2005-07-20 17:45:56 +02:00
_ ( ' Send new message to a contact in the roster. Both pgp key and account are optional. If you want to set only \' account \' , whitout \' pgp key \' , just set \' pgp key \' to \' \' . ' ) ,
2005-07-20 02:27:40 +02:00
[
2005-07-20 12:08:11 +02:00
( ' jid ' , _ ( ' jid of the contact that will receive the message ' ) , True ) ,
2005-07-20 02:27:40 +02:00
( _ ( ' message ' ) , _ ( ' message contents ' ) , True ) ,
2005-07-20 17:45:56 +02:00
( _ ( ' pgp key ' ) , _ ( ' if specified the message will be encrypted using \
2005-07-20 02:27:40 +02:00
this pulic key ' ), False),
2005-07-20 12:08:11 +02:00
( _ ( ' account ' ) , _ ( ' if specified the message will be sent using this account ' ) , False ) ,
2005-07-20 02:27:40 +02:00
]
] ,
' contact_info ' : [
_ ( ' Get detailed info on a contact ' ) ,
[
2005-07-20 12:08:11 +02:00
( ' jid ' , _ ( ' jid of the contact ' ) , True )
2005-07-20 02:27:40 +02:00
]
]
}
2005-07-17 23:41:54 +02:00
2005-07-20 02:27:40 +02:00
def make_arguments_row ( args ) :
2005-07-20 03:29:40 +02:00
''' return arguments list. Mandatory arguments are enclosed with:
' < ' , ' > ' , optional arguments - with ' [ ' , ' ] ' '''
2005-07-20 02:27:40 +02:00
str = ' '
for argument in args :
str + = ' '
if argument [ 2 ] :
str + = ' < '
else :
str + = ' [ '
str + = argument [ 0 ]
if argument [ 2 ] :
str + = ' > '
else :
str + = ' ] '
return str
2005-07-20 03:29:40 +02:00
2005-07-20 02:27:40 +02:00
def help_on_command ( command ) :
2005-07-20 03:29:40 +02:00
''' return help message for a given command '''
2005-07-20 02:27:40 +02:00
if command in commands :
2005-07-20 12:08:11 +02:00
str = _ ( ' Usage: %s %s %s \n \t ' ) % ( BASENAME , command ,
make_arguments_row ( commands [ command ] [ 1 ] ) )
2005-07-20 02:27:40 +02:00
str + = commands [ command ] [ 0 ] + ' \n \n Arguments: \n '
for argument in commands [ command ] [ 1 ] :
str + = ' ' + argument [ 0 ] + ' - ' + argument [ 1 ] + ' \n '
return str
send_error ( _ ( ' %s not found ' ) % command )
2005-07-17 23:41:54 +02:00
def compose_help ( ) :
2005-07-20 03:29:40 +02:00
''' print usage, and list available commands '''
2005-07-20 09:56:30 +02:00
str = _ ( ' Usage: %s command [arguments] \n Command is one of: \n ' ) % BASENAME
2005-07-20 02:27:40 +02:00
for command in commands . keys ( ) :
str + = ' ' + command
for argument in commands [ command ] [ 1 ] :
str + = ' '
if argument [ 2 ] :
str + = ' < '
else :
str + = ' [ '
str + = argument [ 0 ]
if argument [ 2 ] :
str + = ' > '
else :
str + = ' ] '
str + = ' \n '
2005-07-17 23:41:54 +02:00
return str
def show_vcard_info ( * args , * * keyword ) :
2005-07-20 03:29:40 +02:00
# FIXME: more cleaner output
2005-07-17 23:41:54 +02:00
if _version [ 1 ] > = 30 :
print args [ 0 ]
else :
if args and len ( args ) > = 5 :
print args [ 4 ] . get_args_list ( )
2005-07-20 02:27:40 +02:00
# remove_signal_receiver is broken in lower versions (< 0.35),
2005-07-17 23:41:54 +02:00
# so we leave the leak - nothing can be done
if _version [ 1 ] > = 41 :
sbus . remove_signal_receiver ( show_vcard_info , ' VcardInfo ' , INTERFACE ,
SERVICE , OBJ_PATH )
gtk . main_quit ( )
2005-07-20 03:29:40 +02:00
def check_arguments ( command ) :
''' Make check if all necessary arguments are given '''
argv_len = len ( sys . argv ) - 2
args = commands [ command ] [ 1 ]
if len ( args ) > argv_len :
if args [ argv_len ] [ 2 ] :
2005-07-20 12:08:11 +02:00
send_error ( _ ( ' Argument " %s " is not specified. \n \
Type " %s help %s " for more info ' ) % \
2005-07-20 09:56:30 +02:00
( args [ argv_len ] [ 0 ] , BASENAME , command ) )
2005-07-20 03:29:40 +02:00
2005-07-17 23:41:54 +02:00
def gtk_quit ( ) :
if _version [ 1 ] > = 41 :
sbus . remove_signal_receiver ( show_vcard_info , ' VcardInfo ' , INTERFACE ,
SERVICE , OBJ_PATH )
gtk . main_quit ( )
2005-07-20 03:29:40 +02:00
#FIXME - didn't find more clever way for the below 8 lines of code.
# method(sys.argv[2:]) doesn't work, cos sys.argv[2:] is a tuple
def call_remote_method ( method ) :
argv_len = len ( sys . argv )
2005-07-20 19:51:31 +02:00
try :
if argv_len == 2 :
res = method ( )
elif argv_len == 3 :
res = method ( sys . argv [ 2 ] )
elif argv_len == 4 :
res = method ( sys . argv [ 2 ] , sys . argv [ 3 ] )
elif argv_len == 5 :
res = method ( sys . argv [ 2 ] , sys . argv [ 3 ] , sys . argv [ 4 ] )
elif argv_len == 6 :
res = method ( sys . argv [ 2 ] , sys . argv [ 3 ] , sys . argv [ 4 ] , sys . argv [ 5 ] )
return res
except :
send_error ( _ ( ' Service not available ' ) )
2005-07-20 03:29:40 +02:00
return None
2005-07-17 23:41:54 +02:00
argv_len = len ( sys . argv )
2005-07-20 02:27:40 +02:00
if argv_len < 2 or sys . argv [ 1 ] not in commands . keys ( ) : # no args or bad args
2005-07-18 11:53:19 +02:00
send_error ( compose_help ( ) )
2005-07-17 23:41:54 +02:00
command = sys . argv [ 1 ]
if command == ' help ' :
2005-07-20 02:27:40 +02:00
if argv_len == 3 :
print help_on_command ( sys . argv [ 2 ] )
else :
print compose_help ( )
2005-07-17 23:41:54 +02:00
sys . exit ( )
try :
sbus = dbus . SessionBus ( )
except :
2005-07-20 03:29:40 +02:00
send_error ( _ ( ' Session bus is not available. \n ' ) )
2005-07-17 23:41:54 +02:00
if _version [ 1 ] > = 30 and _version [ 1 ] < = 42 :
object = sbus . get_object ( SERVICE , OBJ_PATH )
interface = dbus . Interface ( object , INTERFACE )
elif _version [ 1 ] < 30 :
service = sbus . get_service ( SERVICE )
interface = service . get_object ( OBJ_PATH , INTERFACE )
else :
2005-07-20 03:29:40 +02:00
send_error ( _ ( ' Unknow dbus version: %s ' ) % _version )
2005-07-17 23:41:54 +02:00
method = interface . __getattr__ ( sys . argv [ 1 ] ) # get the function asked
2005-07-20 03:29:40 +02:00
check_arguments ( command )
2005-07-17 23:41:54 +02:00
if command == ' contact_info ' :
if argv_len < 3 :
2005-07-20 12:08:11 +02:00
send_error ( _ ( ' Missing argument " contact_jid " ' ) )
2005-07-17 23:41:54 +02:00
try :
id = sbus . add_signal_receiver ( show_vcard_info , ' VcardInfo ' ,
INTERFACE , SERVICE , OBJ_PATH )
2005-07-20 02:27:40 +02:00
except :
send_error ( _ ( ' Service not available ' ) )
res = call_remote_method ( method )
2005-07-20 17:45:56 +02:00
if res is not None :
if command in [ ' open_chat ' , ' send_message ' ] :
if command == ' send_message ' :
argv_len - = 2
if res == False :
if argv_len < 4 :
send_error ( _ ( ' \' %s \' is not in your roster. \n \
Please specify account for sending the message . ' ) % s ys.argv[2])
else :
send_error ( _ ( ' You have no active account ' ) )
2005-07-20 18:32:31 +02:00
elif res :
2005-07-20 17:45:56 +02:00
print res
2005-07-18 00:00:40 +02:00
if command == ' contact_info ' :
gobject . timeout_add ( 5000 , gtk_quit ) # wait 5 sec maximum
gtk . main ( )