4 ## Copyright (C) 2005-2006 Dimitur Kirov <dkirov AT gmail.com>
5 ## Nikos Kouremenos <kourem AT gmail.com>
6 ## Copyright (C) 2005-2008 Yann Leboulanger <asterix AT lagaule.org>
7 ## Copyright (C) 2006 Junglecow <junglecow AT gmail.com>
8 ## Travis Shirk <travis AT pobox.com>
9 ## Copyright (C) 2006-2008 Jean-Marie Traissard <jim AT lapin.org>
10 ## Copyright (C) 2007 Julien Pivotto <roidelapluie AT gmail.com>
12 ## This file is part of Gajim.
14 ## Gajim is free software; you can redistribute it and/or modify
15 ## it under the terms of the GNU General Public License as published
16 ## by the Free Software Foundation; version 3 only.
18 ## Gajim is distributed in the hope that it will be useful,
19 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ## GNU General Public License for more details.
23 ## You should have received a copy of the GNU General Public License
24 ## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
27 # gajim-remote help will show you the D-BUS API of Gajim
33 signal
.signal(signal
.SIGINT
, signal
.SIG_DFL
) # ^C exits the application
35 from common
import exceptions
36 from common
import i18n
# This installs _() function
39 PREFERRED_ENCODING
= locale
.getpreferredencoding()
41 PREFERRED_ENCODING
= 'UTF-8'
43 def send_error(error_message
):
44 '''Writes error message to stderr and exits'''
45 print >> sys
.stderr
, error_message
.encode(PREFERRED_ENCODING
)
52 # test if dbus-x11 is installed
53 bus
= dbus
.SessionBus()
55 print str(exceptions
.DbusNotSupported())
58 OBJ_PATH
= '/org/gajim/dbus/RemoteObject'
59 INTERFACE
= 'org.gajim.dbus.RemoteInterface'
60 SERVICE
= 'org.gajim.dbus'
61 BASENAME
= 'gajim-remote'
67 self
.argv_len
= len(sys
.argv
)
68 # define commands dict. Prototype :
70 # 'command': [comment, [list of arguments] ]
73 # each argument is defined as a tuple:
74 # (argument name, help on argument, is mandatory)
78 _('Shows a help on specific command'),
80 #User gets help for the command, specified by this parameter
82 _('show help on command'), False)
85 'toggle_roster_appearance' : [
86 _('Shows or hides the roster window'),
89 'show_next_pending_event': [
90 _('Pops up a window with the next pending event'),
94 _('Prints a list of all contacts in the roster. Each contact '
95 'appears on a separate line'),
97 (_('account'), _('show only contacts of the given account'),
103 _('Prints a list of registered accounts'),
107 _('Changes the status of account or accounts'),
109 #offline, online, chat, away, xa, dnd, invisible should not be translated
110 (_('status'), _('one of: offline, online, chat, away, xa, dnd, invisible '), True),
111 (_('message'), _('status message'), False),
112 (_('account'), _('change status of account "account". '
113 'If not specified, try to change status of all accounts that have '
114 '"sync with global status" option set'), False)
118 _('Changes the priority of account or accounts'),
120 (_('priority'), _('priority you want to give to the account'),
122 (_('account'), _('change the priority of the given account. '
123 'If not specified, change status of all accounts that have'
124 ' "sync with global status" option set'), False)
128 _('Shows the chat dialog so that you can send messages to a contact'),
130 ('jid', _('JID of the contact that you want to chat with'),
132 (_('account'), _('if specified, contact is taken from the '
133 'contact list of this account'), False),
135 _('message content. The account must be specified or ""'),
139 'send_chat_message':[
140 _('Sends new chat message to a contact in the roster. Both OpenPGP key '
141 'and account are optional. If you want to set only \'account\', '
142 'without \'OpenPGP key\', just set \'OpenPGP key\' to \'\'.'),
144 ('jid', _('JID of the contact that will receive the message'), True),
145 (_('message'), _('message contents'), True),
146 (_('pgp key'), _('if specified, the message will be encrypted '
147 'using this public key'), False),
148 (_('account'), _('if specified, the message will be sent '
149 'using this account'), False),
152 'send_single_message':[
153 _('Sends new single message to a contact in the roster. Both OpenPGP key '
154 'and account are optional. If you want to set only \'account\', '
155 'without \'OpenPGP key\', just set \'OpenPGP key\' to \'\'.'),
157 ('jid', _('JID of the contact that will receive the message'), True),
158 (_('subject'), _('message subject'), True),
159 (_('message'), _('message contents'), True),
160 (_('pgp key'), _('if specified, the message will be encrypted '
161 'using this public key'), False),
162 (_('account'), _('if specified, the message will be sent '
163 'using this account'), False),
166 'send_groupchat_message':[
167 _('Sends new message to a groupchat you\'ve joined.'),
169 ('room_jid', _('JID of the room that will receive the message'), True),
170 (_('message'), _('message contents'), True),
171 (_('account'), _('if specified, the message will be sent '
172 'using this account'), False),
176 _('Gets detailed info on a contact'),
178 ('jid', _('JID of the contact'), True)
182 _('Gets detailed info on a account'),
184 ('account', _('Name of the account'), True)
188 _('Sends file to a contact'),
190 (_('file'), _('File path'), True),
191 ('jid', _('JID of the contact'), True),
192 (_('account'), _('if specified, file will be sent using this '
197 _('Lists all preferences and their values'),
201 _('Sets value of \'key\' to \'value\'.'),
203 (_('key=value'), _('\'key\' is the name of the preference, '
204 '\'value\' is the value to set it to'), True)
208 _('Deletes a preference item'),
210 (_('key'), _('name of the preference to be deleted'), True)
214 _('Writes the current state of Gajim preferences to the .config '
219 _('Removes contact from roster'),
221 ('jid', _('JID of the contact'), True),
222 (_('account'), _('if specified, contact is taken from the '
223 'contact list of this account'), False)
228 _('Adds contact to roster'),
230 (_('jid'), _('JID of the contact'), True),
231 (_('account'), _('Adds new contact to this account'), False)
236 _('Returns current status (the global one unless account is specified)'),
238 (_('account'), '', False)
242 'get_status_message': [
243 _('Returns current status message (the global one unless account is specified)'),
245 (_('account'), '', False)
249 'get_unread_msgs_number': [
250 _('Returns number of unread messages'),
254 _('Opens \'Start Chat\' dialog'),
256 (_('account'), _('Starts chat, using this account'), True)
260 _('Sends custom XML'),
262 ('xml', _('XML to send'), True),
263 ('account', _('Account in which the xml will be sent; '
264 'if not specified, xml will be sent to all accounts'),
269 _('Handle a xmpp:/ uri'),
271 (_('uri'), _('URI to handle'), True),
272 (_('account'), _('Account in which you want to handle it'),
274 (_('message'), _('Message content'), False)
278 _('Join a MUC room'),
280 (_('room'), _('Room JID'), True),
281 (_('nick'), _('Nickname to use'), False),
282 (_('password'), _('Password to enter the room'), False),
283 (_('account'), _('Account from which you want to enter the '
287 'check_gajim_running':[
288 _('Check if Gajim is running'),
292 _('Shows or hides the ipython window'),
299 if self
.argv_len
< 2 or sys
.argv
[1] not in self
.commands
.keys():
300 # no args or bad args
301 send_error(self
.compose_help())
302 self
.command
= sys
.argv
[1]
303 if self
.command
== 'help':
304 if self
.argv_len
== 3:
305 print self
.help_on_command(sys
.argv
[2]).encode(PREFERRED_ENCODING
)
307 print self
.compose_help().encode(PREFERRED_ENCODING
)
309 if self
.command
== 'handle_uri':
311 if self
.command
== 'check_gajim_running':
312 print self
.check_gajim_running()
314 self
.init_connection()
315 self
.check_arguments()
317 if self
.command
== 'contact_info':
318 if self
.argv_len
< 3:
319 send_error(_('Missing argument "contact_jid"'))
322 res
= self
.call_remote_method()
323 except exceptions
.ServiceNotAvailable
:
324 # At this point an error message has already been displayed
327 self
.print_result(res
)
329 def print_result(self
, res
):
330 ''' Print retrieved result to the output '''
332 if self
.command
in ('open_chat', 'send_chat_message', 'send_single_message', 'start_chat'):
333 if self
.command
in ('send_message', 'send_single_message'):
337 if self
.argv_len
< 4:
338 send_error(_('\'%s\' is not in your roster.\n'
339 'Please specify account for sending the message.') % sys
.argv
[2])
341 send_error(_('You have no active account'))
342 elif self
.command
== 'list_accounts':
343 if isinstance(res
, list):
345 if isinstance(account
, unicode):
346 print account
.encode(PREFERRED_ENCODING
)
349 elif self
.command
== 'account_info':
351 print self
.print_info(0, res
, True)
352 elif self
.command
== 'list_contacts':
353 for account_dict
in res
:
354 print self
.print_info(0, account_dict
, True)
355 elif self
.command
== 'prefs_list':
356 pref_keys
= sorted(res
.keys())
357 for pref_key
in pref_keys
:
358 result
= '%s = %s' % (pref_key
, res
[pref_key
])
359 if isinstance(result
, unicode):
360 print result
.encode(PREFERRED_ENCODING
)
363 elif self
.command
== 'contact_info':
364 print self
.print_info(0, res
, True)
366 print unicode(res
).encode(PREFERRED_ENCODING
)
368 def check_gajim_running(self
):
371 self
.sbus
= dbus
.SessionBus()
373 raise exceptions
.SessionBusNotPresent
376 if hasattr(self
.sbus
, 'name_has_owner'):
377 if self
.sbus
.name_has_owner(SERVICE
):
379 elif dbus
.dbus_bindings
.bus_name_has_owner(self
.sbus
.get_connection(),
384 def init_connection(self
):
385 ''' create the onnection to the session dbus,
386 or exit if it is not possible '''
388 self
.sbus
= dbus
.SessionBus()
390 raise exceptions
.SessionBusNotPresent
392 if not self
.check_gajim_running():
393 send_error(_('It seems Gajim is not running. So you can\'t use gajim-remote.'))
394 obj
= self
.sbus
.get_object(SERVICE
, OBJ_PATH
)
395 interface
= dbus
.Interface(obj
, INTERFACE
)
397 # get the function asked
398 self
.method
= interface
.__getattr
__(self
.command
)
400 def make_arguments_row(self
, args
):
401 ''' return arguments list. Mandatory arguments are enclosed with:
402 '<', '>', optional arguments - with '[', ']' '''
406 s
+= ' <' + arg
[0] + '>'
408 s
+= ' [' + arg
[0] + ']'
411 def help_on_command(self
, command
):
412 ''' return help message for a given command '''
413 if command
in self
.commands
:
414 command_props
= self
.commands
[command
]
415 arguments_str
= self
.make_arguments_row(command_props
[1])
416 str_
= _('Usage: %(basename)s %(command)s %(arguments)s \n\t %(help)s')\
417 % {'basename': BASENAME
, 'command': command
,
418 'arguments': arguments_str
, 'help': command_props
[0]}
419 if len(command_props
[1]) > 0:
420 str_
+= '\n\n' + _('Arguments:') + '\n'
421 for argument
in command_props
[1]:
422 str_
+= ' ' + argument
[0] + ' - ' + argument
[1] + '\n'
424 send_error(_('%s not found') % command
)
426 def compose_help(self
):
427 ''' print usage, and list available commands '''
428 s
= _('Usage: %s command [arguments]\nCommand is one of:\n' ) % BASENAME
429 for command
in sorted(self
.commands
):
431 for arg
in self
.commands
[command
][1]:
433 s
+= ' <' + arg
[0] + '>'
435 s
+= ' [' + arg
[0] + ']'
439 def print_info(self
, level
, prop_dict
, encode_return
= False):
440 ''' return formated string from data structure '''
441 if prop_dict
is None or not isinstance(prop_dict
, (dict, list, tuple)):
444 if isinstance(prop_dict
, (list, tuple)):
446 spacing
= ' ' * level
* 4
447 for val
in prop_dict
:
450 elif isinstance(val
, int):
451 ret_str
+='\t' + str(val
)
452 elif isinstance(val
, (str, unicode)):
454 elif isinstance(val
, (list, tuple)):
457 res
+= self
.print_info(level
+1, items
)
459 ret_str
+= '\t' + res
460 elif isinstance(val
, dict):
461 ret_str
+= self
.print_info(level
+1, val
)
462 ret_str
= '%s(%s)\n' % (spacing
, ret_str
[1:])
463 elif isinstance(prop_dict
, dict):
464 for key
in prop_dict
.keys():
466 spacing
= ' ' * level
* 4
467 if isinstance(val
, (unicode, int, str)):
470 ret_str
+= '%s%-10s: %s\n' % (spacing
, key
, val
)
471 elif isinstance(val
, (list, tuple)):
474 res
+= self
.print_info(level
+1, items
)
476 ret_str
+= '%s%s: \n%s' % (spacing
, key
, res
)
477 elif isinstance(val
, dict):
478 res
= self
.print_info(level
+1, val
)
480 ret_str
+= '%s%s: \n%s' % (spacing
, key
, res
)
483 ret_str
= ret_str
.encode(PREFERRED_ENCODING
)
488 def check_arguments(self
):
489 ''' Make check if all necessary arguments are given '''
490 argv_len
= self
.argv_len
- 2
491 args
= self
.commands
[self
.command
][1]
492 if len(args
) < argv_len
:
493 send_error(_('Too many arguments. \n'
494 'Type "%(basename)s help %(command)s" for more info') % {
495 'basename': BASENAME
, 'command': self
.command
})
496 if len(args
) > argv_len
:
497 if args
[argv_len
][2]:
498 send_error(_('Argument "%(arg)s" is not specified. \n'
499 'Type "%(basename)s help %(command)s" for more info') %
500 {'arg': args
[argv_len
][0], 'basename': BASENAME
,
501 'command': self
.command
})
504 for arg
in sys
.argv
[2:]:
507 self
.arguments
.append(arg
)
509 # it's latest argument with spaces
510 self
.arguments
.append(' '.join(sys
.argv
[i
+1:]))
512 # add empty string for missing args
513 self
.arguments
+= ['']*(len(args
)-i
)
515 def handle_uri(self
):
516 if not sys
.argv
[2].startswith('xmpp:'):
517 send_error(_('Wrong uri'))
518 sys
.argv
[2] = sys
.argv
[2][5:]
521 self
.command
= sys
.argv
[1] = 'open_chat'
524 # Open chat window and paste the text in the input message dialog
525 self
.command
= sys
.argv
[1] = 'open_chat'
526 message
= uri
.split('body=')
527 message
= message
[1].split(';')[0]
529 message
= urllib
.unquote(message
)
530 except UnicodeDecodeError:
532 sys
.argv
[2] = uri
.split('?')[0]
533 if len(sys
.argv
) == 4:
534 # jid in the sys.argv
535 sys
.argv
.append(message
)
538 sys
.argv
.append(message
)
540 sys
.argv
[4] = message
542 (jid
, action
) = uri
.split('?', 1)
544 jid
= urllib
.unquote(jid
)
545 except UnicodeDecodeError:
549 self
.command
= sys
.argv
[1] = 'join_room'
550 # Move account parameter from position 3 to 5
552 sys
.argv
.append(sys
.argv
[3])
555 if action
.startswith('roster'):
556 # Add contact to roster
557 self
.command
= sys
.argv
[1] = 'add_contact'
561 def call_remote_method(self
):
562 ''' calls self.method with arguments from sys.argv[2:] '''
563 args
= [i
.decode(PREFERRED_ENCODING
) for i
in self
.arguments
]
564 args
= [dbus
.String(i
) for i
in args
]
566 res
= self
.method(*args
)
569 raise exceptions
.ServiceNotAvailable
572 if __name__
== '__main__':