4 ## Copyright (C) 2005-2006 Dimitur Kirov <dkirov AT gmail.com>
5 ## Nikos Kouremenos <kourem AT gmail.com>
6 ## Copyright (C) 2005-2010 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-plugin'
66 self
.argv_len
= len(sys
.argv
)
67 # define commands dict. Prototype :
69 # 'command': [comment, [list of arguments] ]
72 # each argument is defined as a tuple:
73 # (argument name, help on argument, is mandatory)
77 _('Shows a help on specific command'),
79 #User gets help for the command, specified by this parameter
81 _('show help on command'), False)
84 'toggle_roster_appearance': [
85 _('Shows or hides the roster window'),
88 'show_next_pending_event': [
89 _('Pops up a window with the next pending event'),
93 _('Prints a list of all contacts in the roster. Each contact '
94 'appears on a separate line'),
96 (_('account'), _('show only contacts of the given account'),
102 _('Prints a list of registered accounts'),
106 _('Changes the status of account or accounts'),
108 #offline, online, chat, away, xa, dnd, invisible should not be translated
109 (_('status'), _('one of: offline, online, chat, away, xa, dnd, invisible. If not set, use account\'s previous status'), False),
110 (_('message'), _('status message'), False),
111 (_('account'), _('change status of account "account". '
112 'If not specified, try to change status of all accounts that have '
113 '"sync with global status" option set'), False)
117 _('Changes the priority of account or accounts'),
119 (_('priority'), _('priority you want to give to the account'),
121 (_('account'), _('change the priority of the given account. '
122 'If not specified, change status of all accounts that have'
123 ' "sync with global status" option set'), False)
127 _('Shows the chat dialog so that you can send messages to a contact'),
129 ('jid', _('JID of the contact that you want to chat with'),
131 (_('account'), _('if specified, contact is taken from the '
132 'contact list of this account'), False),
134 _('message content. The account must be specified or ""'),
138 'send_chat_message': [
139 _('Sends new chat message to a contact in the roster. Both OpenPGP key '
140 'and account are optional. If you want to set only \'account\', '
141 'without \'OpenPGP key\', just set \'OpenPGP key\' to \'\'.'),
143 ('jid', _('JID of the contact that will receive the message'), True),
144 (_('message'), _('message contents'), True),
145 (_('pgp key'), _('if specified, the message will be encrypted '
146 'using this public key'), False),
147 (_('account'), _('if specified, the message will be sent '
148 'using this account'), False),
151 'send_single_message': [
152 _('Sends new single message to a contact in the roster. Both OpenPGP key '
153 'and account are optional. If you want to set only \'account\', '
154 'without \'OpenPGP key\', just set \'OpenPGP key\' to \'\'.'),
156 ('jid', _('JID of the contact that will receive the message'), True),
157 (_('subject'), _('message subject'), True),
158 (_('message'), _('message contents'), True),
159 (_('pgp key'), _('if specified, the message will be encrypted '
160 'using this public key'), False),
161 (_('account'), _('if specified, the message will be sent '
162 'using this account'), False),
165 'send_groupchat_message': [
166 _('Sends new message to a groupchat you\'ve joined.'),
168 ('room_jid', _('JID of the room that will receive the message'), True),
169 (_('message'), _('message contents'), True),
170 (_('account'), _('if specified, the message will be sent '
171 'using this account'), False),
175 _('Gets detailed info on a contact'),
177 ('jid', _('JID of the contact'), True)
181 _('Gets detailed info on a account'),
183 ('account', _('Name of the account'), True)
187 _('Sends file to a contact'),
189 (_('file'), _('File path'), True),
190 ('jid', _('JID of the contact'), True),
191 (_('account'), _('if specified, file will be sent using this '
196 _('Lists all preferences and their values'),
200 _('Sets value of \'key\' to \'value\'.'),
202 (_('key=value'), _('\'key\' is the name of the preference, '
203 '\'value\' is the value to set it to'), True)
207 _('Deletes a preference item'),
209 (_('key'), _('name of the preference to be deleted'), True)
213 _('Writes the current state of Gajim preferences to the .config '
218 _('Removes contact from roster'),
220 ('jid', _('JID of the contact'), True),
221 (_('account'), _('if specified, contact is taken from the '
222 'contact list of this account'), False)
227 _('Adds contact to roster'),
229 (_('jid'), _('JID of the contact'), True),
230 (_('account'), _('Adds new contact to this account'), False)
235 _('Returns current status (the global one unless account is specified)'),
237 (_('account'), '', False)
241 'get_status_message': [
242 _('Returns current status message (the global one unless account is specified)'),
244 (_('account'), '', False)
248 'get_unread_msgs_number': [
249 _('Returns number of unread messages'),
253 _('Opens \'Start Chat\' dialog'),
255 (_('account'), _('Starts chat, using this account'), True)
259 _('Sends custom XML'),
261 ('xml', _('XML to send'), True),
262 ('account', _('Account in which the xml will be sent; '
263 'if not specified, xml will be sent to all accounts'),
268 _('Change the avatar'),
270 ('picture', _('Picture to use'), True),
271 ('account', _('Account in which the avatar will be set; '
272 'if not specified, the avatar will be set for all accounts'),
277 _('Handle a xmpp:/ uri'),
279 (_('uri'), _('URI to handle'), True),
280 (_('account'), _('Account in which you want to handle it'),
282 (_('message'), _('Message content'), False)
286 _('Join a MUC room'),
288 (_('room'), _('Room JID'), True),
289 (_('nick'), _('Nickname to use'), False),
290 (_('password'), _('Password to enter the room'), False),
291 (_('account'), _('Account from which you want to enter the '
295 'check_gajim_running': [
296 _('Check if Gajim is running'),
300 _('Shows or hides the ipython window'),
307 if self
.argv_len
< 2 or sys
.argv
[1] not in self
.commands
.keys():
308 # no args or bad args
309 send_error(self
.compose_help())
310 self
.command
= sys
.argv
[1]
311 if self
.command
== 'help':
312 if self
.argv_len
== 3:
313 print self
.help_on_command(sys
.argv
[2]).encode(PREFERRED_ENCODING
)
315 print self
.compose_help().encode(PREFERRED_ENCODING
)
317 if self
.command
== 'handle_uri':
319 if self
.command
== 'check_gajim_running':
320 print self
.check_gajim_running()
322 self
.init_connection()
323 self
.check_arguments()
325 if self
.command
== 'contact_info':
326 if self
.argv_len
< 3:
327 send_error(_('Missing argument "contact_jid"'))
330 res
= self
.call_remote_method()
331 except exceptions
.ServiceNotAvailable
:
332 # At this point an error message has already been displayed
335 self
.print_result(res
)
337 def print_result(self
, res
):
339 Print retrieved result to the output
342 if self
.command
in ('open_chat', 'send_chat_message', 'send_single_message', 'start_chat'):
343 if self
.command
in ('send_message', 'send_single_message'):
347 if self
.argv_len
< 4:
348 send_error(_('\'%s\' is not in your roster.\n'
349 'Please specify account for sending the message.') % sys
.argv
[2])
351 send_error(_('You have no active account'))
352 elif self
.command
== 'list_accounts':
353 if isinstance(res
, list):
355 if isinstance(account
, unicode):
356 print account
.encode(PREFERRED_ENCODING
)
359 elif self
.command
== 'account_info':
361 print self
.print_info(0, res
, True)
362 elif self
.command
== 'list_contacts':
363 for account_dict
in res
:
364 print self
.print_info(0, account_dict
, True)
365 elif self
.command
== 'prefs_list':
366 pref_keys
= sorted(res
.keys())
367 for pref_key
in pref_keys
:
368 result
= '%s = %s' % (pref_key
, res
[pref_key
])
369 if isinstance(result
, unicode):
370 print result
.encode(PREFERRED_ENCODING
)
373 elif self
.command
== 'contact_info':
374 print self
.print_info(0, res
, True)
376 print unicode(res
).encode(PREFERRED_ENCODING
)
378 def check_gajim_running(self
):
381 self
.sbus
= dbus
.SessionBus()
383 raise exceptions
.SessionBusNotPresent
386 if hasattr(self
.sbus
, 'name_has_owner'):
387 if self
.sbus
.name_has_owner(SERVICE
):
389 elif dbus
.dbus_bindings
.bus_name_has_owner(self
.sbus
.get_connection(),
394 def init_connection(self
):
396 Create the onnection to the session dbus, or exit if it is not possible
399 self
.sbus
= dbus
.SessionBus()
401 raise exceptions
.SessionBusNotPresent
403 if not self
.check_gajim_running():
404 send_error(_('It seems Gajim is not running. So you can\'t use gajim-remote.'))
405 obj
= self
.sbus
.get_object(SERVICE
, OBJ_PATH
)
406 interface
= dbus
.Interface(obj
, INTERFACE
)
408 # get the function asked
409 self
.method
= interface
.__getattr
__(self
.command
)
411 def make_arguments_row(self
, args
):
413 Return arguments list. Mandatory arguments are enclosed with:
414 '<', '>', optional arguments - with '[', ']'
419 s
+= ' <' + arg
[0] + '>'
421 s
+= ' [' + arg
[0] + ']'
424 def help_on_command(self
, command
):
426 Return help message for a given command
428 if command
in self
.commands
:
429 command_props
= self
.commands
[command
]
430 arguments_str
= self
.make_arguments_row(command_props
[1])
431 str_
= _('Usage: %(basename)s %(command)s %(arguments)s \n\t %(help)s')\
432 % {'basename': BASENAME
, 'command': command
,
433 'arguments': arguments_str
, 'help': command_props
[0]}
434 if len(command_props
[1]) > 0:
435 str_
+= '\n\n' + _('Arguments:') + '\n'
436 for argument
in command_props
[1]:
437 str_
+= ' ' + argument
[0] + ' - ' + argument
[1] + '\n'
439 send_error(_('%s not found') % command
)
441 def compose_help(self
):
443 Print usage, and list available commands
445 s
= _('Usage: %s command [arguments]\nCommand is one of:\n' ) % BASENAME
446 for command
in sorted(self
.commands
):
448 for arg
in self
.commands
[command
][1]:
450 s
+= ' <' + arg
[0] + '>'
452 s
+= ' [' + arg
[0] + ']'
456 def print_info(self
, level
, prop_dict
, encode_return
= False):
458 Return formated string from data structure
460 if prop_dict
is None or not isinstance(prop_dict
, (dict, list, tuple)):
463 if isinstance(prop_dict
, (list, tuple)):
465 spacing
= ' ' * level
* 4
466 for val
in prop_dict
:
469 elif isinstance(val
, int):
470 ret_str
+='\t' + str(val
)
471 elif isinstance(val
, (str, unicode)):
473 elif isinstance(val
, (list, tuple)):
476 res
+= self
.print_info(level
+1, items
)
478 ret_str
+= '\t' + res
479 elif isinstance(val
, dict):
480 ret_str
+= self
.print_info(level
+1, val
)
481 ret_str
= '%s(%s)\n' % (spacing
, ret_str
[1:])
482 elif isinstance(prop_dict
, dict):
483 for key
in prop_dict
.keys():
485 spacing
= ' ' * level
* 4
486 if isinstance(val
, (unicode, int, str)):
489 ret_str
+= '%s%-10s: %s\n' % (spacing
, key
, val
)
490 elif isinstance(val
, (list, tuple)):
493 res
+= self
.print_info(level
+1, items
)
495 ret_str
+= '%s%s: \n%s' % (spacing
, key
, res
)
496 elif isinstance(val
, dict):
497 res
= self
.print_info(level
+1, val
)
499 ret_str
+= '%s%s: \n%s' % (spacing
, key
, res
)
502 ret_str
= ret_str
.encode(PREFERRED_ENCODING
)
507 def check_arguments(self
):
509 Make check if all necessary arguments are given
511 argv_len
= self
.argv_len
- 2
512 args
= self
.commands
[self
.command
][1]
513 if len(args
) < argv_len
:
514 send_error(_('Too many arguments. \n'
515 'Type "%(basename)s help %(command)s" for more info') % {
516 'basename': BASENAME
, 'command': self
.command
})
517 if len(args
) > argv_len
:
518 if args
[argv_len
][2]:
519 send_error(_('Argument "%(arg)s" is not specified. \n'
520 'Type "%(basename)s help %(command)s" for more info') %
521 {'arg': args
[argv_len
][0], 'basename': BASENAME
,
522 'command': self
.command
})
525 for arg
in sys
.argv
[2:]:
528 self
.arguments
.append(arg
)
530 # it's latest argument with spaces
531 self
.arguments
.append(' '.join(sys
.argv
[i
+1:]))
533 # add empty string for missing args
534 self
.arguments
+= ['']*(len(args
)-i
)
536 def handle_uri(self
):
537 if len(sys
.argv
) < 3:
538 send_error(_('No uri given'))
539 if not sys
.argv
[2].startswith('xmpp:'):
540 send_error(_('Wrong uri'))
541 sys
.argv
[2] = sys
.argv
[2][5:]
544 self
.command
= sys
.argv
[1] = 'open_chat'
547 # Open chat window and paste the text in the input message dialog
548 self
.command
= sys
.argv
[1] = 'open_chat'
549 message
= uri
.split('body=')
550 message
= message
[1].split(';')[0]
552 message
= urllib
.unquote(message
)
553 except UnicodeDecodeError:
555 sys
.argv
[2] = uri
.split('?')[0]
556 if len(sys
.argv
) == 4:
557 # jid in the sys.argv
558 sys
.argv
.append(message
)
561 sys
.argv
.append(message
)
563 sys
.argv
[4] = message
565 (jid
, action
) = uri
.split('?', 1)
567 jid
= urllib
.unquote(jid
)
568 except UnicodeDecodeError:
572 self
.command
= sys
.argv
[1] = 'join_room'
573 # Move account parameter from position 3 to 5
575 sys
.argv
.append(sys
.argv
[3])
578 if action
.startswith('roster'):
579 # Add contact to roster
580 self
.command
= sys
.argv
[1] = 'add_contact'
584 def call_remote_method(self
):
586 Calls self.method with arguments from sys.argv[2:]
588 args
= [i
.decode(PREFERRED_ENCODING
) for i
in self
.arguments
]
589 args
= [dbus
.String(i
) for i
in args
]
591 res
= self
.method(*args
)
594 raise exceptions
.ServiceNotAvailable
597 if __name__
== '__main__':