[kepi] ability to use subkeys. Fixes #6051
[gajim.git] / src / gajim-remote.py
blob96895561ea927982f3525ba47290883affb8184f
1 # -*- coding:utf-8 -*-
2 ## src/gajim-remote.py
3 ##
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
29 import sys
30 import locale
31 import urllib
32 import signal
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
38 try:
39 PREFERRED_ENCODING = locale.getpreferredencoding()
40 except Exception:
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)
46 sys.exit(1)
48 try:
49 import dbus
50 import dbus.service
51 import dbus.glib
52 # test if dbus-x11 is installed
53 bus = dbus.SessionBus()
54 except Exception:
55 print str(exceptions.DbusNotSupported())
56 sys.exit(1)
58 OBJ_PATH = '/org/gajim/dbus/RemoteObject'
59 INTERFACE = 'org.gajim.dbus.RemoteInterface'
60 SERVICE = 'org.gajim.dbus'
61 BASENAME = 'gajim-remote'
63 class GajimRemote:
65 def __init__(self):
66 self.argv_len = len(sys.argv)
67 # define commands dict. Prototype :
68 # {
69 # 'command': [comment, [list of arguments] ]
70 # }
72 # each argument is defined as a tuple:
73 # (argument name, help on argument, is mandatory)
75 self.commands = {
76 'help': [
77 _('Shows a help on specific command'),
79 #User gets help for the command, specified by this parameter
80 (_('command'),
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'),
92 'list_contacts': [
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'),
97 False)
101 'list_accounts': [
102 _('Prints a list of registered accounts'),
105 'change_status': [
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)
116 'set_priority': [
117 _('Changes the priority of account or accounts'),
119 (_('priority'), _('priority you want to give to the account'),
120 True),
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)
126 'open_chat': [
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'),
130 True),
131 (_('account'), _('if specified, contact is taken from the '
132 'contact list of this account'), False),
133 (_('message'),
134 _('message content. The account must be specified or ""'),
135 False)
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),
174 'contact_info': [
175 _('Gets detailed info on a contact'),
177 ('jid', _('JID of the contact'), True)
180 'account_info': [
181 _('Gets detailed info on a account'),
183 ('account', _('Name of the account'), True)
186 'send_file': [
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 '
192 'account'), False)
195 'prefs_list': [
196 _('Lists all preferences and their values'),
199 'prefs_put': [
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)
206 'prefs_del': [
207 _('Deletes a preference item'),
209 (_('key'), _('name of the preference to be deleted'), True)
212 'prefs_store': [
213 _('Writes the current state of Gajim preferences to the .config '
214 'file'),
217 'remove_contact': [
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)
226 'add_contact': [
227 _('Adds contact to roster'),
229 (_('jid'), _('JID of the contact'), True),
230 (_('account'), _('Adds new contact to this account'), False)
234 'get_status': [
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'),
252 'start_chat': [
253 _('Opens \'Start Chat\' dialog'),
255 (_('account'), _('Starts chat, using this account'), True)
258 'send_xml': [
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'),
264 False)
267 'change_avatar': [
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'),
273 False)
276 'handle_uri': [
277 _('Handle a xmpp:/ uri'),
279 (_('uri'), _('URI to handle'), True),
280 (_('account'), _('Account in which you want to handle it'),
281 False),
282 (_('message'), _('Message content'), False)
285 'join_room': [
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 '
292 'room'), False)
295 'check_gajim_running': [
296 _('Check if Gajim is running'),
299 'toggle_ipython': [
300 _('Shows or hides the ipython window'),
306 self.sbus = None
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)
314 else:
315 print self.compose_help().encode(PREFERRED_ENCODING)
316 sys.exit(0)
317 if self.command == 'handle_uri':
318 self.handle_uri()
319 if self.command == 'check_gajim_running':
320 print self.check_gajim_running()
321 sys.exit(0)
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"'))
329 try:
330 res = self.call_remote_method()
331 except exceptions.ServiceNotAvailable:
332 # At this point an error message has already been displayed
333 sys.exit(1)
334 else:
335 self.print_result(res)
337 def print_result(self, res):
339 Print retrieved result to the output
341 if res is not None:
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'):
344 self.argv_len -= 2
346 if res is False:
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])
350 else:
351 send_error(_('You have no active account'))
352 elif self.command == 'list_accounts':
353 if isinstance(res, list):
354 for account in res:
355 if isinstance(account, unicode):
356 print account.encode(PREFERRED_ENCODING)
357 else:
358 print account
359 elif self.command == 'account_info':
360 if res:
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)
371 else:
372 print result
373 elif self.command == 'contact_info':
374 print self.print_info(0, res, True)
375 elif res:
376 print unicode(res).encode(PREFERRED_ENCODING)
378 def check_gajim_running(self):
379 if not self.sbus:
380 try:
381 self.sbus = dbus.SessionBus()
382 except Exception:
383 raise exceptions.SessionBusNotPresent
385 test = False
386 if hasattr(self.sbus, 'name_has_owner'):
387 if self.sbus.name_has_owner(SERVICE):
388 test = True
389 elif dbus.dbus_bindings.bus_name_has_owner(self.sbus.get_connection(),
390 SERVICE):
391 test = True
392 return test
394 def init_connection(self):
396 Create the onnection to the session dbus, or exit if it is not possible
398 try:
399 self.sbus = dbus.SessionBus()
400 except Exception:
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 '[', ']'
416 s = ''
417 for arg in args:
418 if arg[2]:
419 s += ' <' + arg[0] + '>'
420 else:
421 s += ' [' + arg[0] + ']'
422 return s
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'
438 return str_
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):
447 s += ' ' + command
448 for arg in self.commands[command][1]:
449 if arg[2]:
450 s += ' <' + arg[0] + '>'
451 else:
452 s += ' [' + arg[0] + ']'
453 s += '\n'
454 return s
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)):
461 return ''
462 ret_str = ''
463 if isinstance(prop_dict, (list, tuple)):
464 ret_str = ''
465 spacing = ' ' * level * 4
466 for val in prop_dict:
467 if val is None:
468 ret_str +='\t'
469 elif isinstance(val, int):
470 ret_str +='\t' + str(val)
471 elif isinstance(val, (str, unicode)):
472 ret_str +='\t' + val
473 elif isinstance(val, (list, tuple)):
474 res = ''
475 for items in val:
476 res += self.print_info(level+1, items)
477 if res != '':
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():
484 val = prop_dict[key]
485 spacing = ' ' * level * 4
486 if isinstance(val, (unicode, int, str)):
487 if val is not None:
488 val = val.strip()
489 ret_str += '%s%-10s: %s\n' % (spacing, key, val)
490 elif isinstance(val, (list, tuple)):
491 res = ''
492 for items in val:
493 res += self.print_info(level+1, items)
494 if res != '':
495 ret_str += '%s%s: \n%s' % (spacing, key, res)
496 elif isinstance(val, dict):
497 res = self.print_info(level+1, val)
498 if res != '':
499 ret_str += '%s%s: \n%s' % (spacing, key, res)
500 if (encode_return):
501 try:
502 ret_str = ret_str.encode(PREFERRED_ENCODING)
503 except Exception:
504 pass
505 return ret_str
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})
523 self.arguments = []
524 i = 0
525 for arg in sys.argv[2:]:
526 i += 1
527 if i < len(args):
528 self.arguments.append(arg)
529 else:
530 # it's latest argument with spaces
531 self.arguments.append(' '.join(sys.argv[i+1:]))
532 break
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:]
542 uri = sys.argv[2]
543 if not '?' in uri:
544 self.command = sys.argv[1] = 'open_chat'
545 return
546 if 'body=' in uri:
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]
551 try:
552 message = urllib.unquote(message)
553 except UnicodeDecodeError:
554 pass
555 sys.argv[2] = uri.split('?')[0]
556 if len(sys.argv) == 4:
557 # jid in the sys.argv
558 sys.argv.append(message)
559 else:
560 sys.argv.append('')
561 sys.argv.append(message)
562 sys.argv[3] = ''
563 sys.argv[4] = message
564 return
565 (jid, action) = uri.split('?', 1)
566 try:
567 jid = urllib.unquote(jid)
568 except UnicodeDecodeError:
569 pass
570 sys.argv[2] = jid
571 if action == 'join':
572 self.command = sys.argv[1] = 'join_room'
573 # Move account parameter from position 3 to 5
574 sys.argv.append('')
575 sys.argv.append(sys.argv[3])
576 sys.argv[3] = ''
577 return
578 if action.startswith('roster'):
579 # Add contact to roster
580 self.command = sys.argv[1] = 'add_contact'
581 return
582 sys.exit(0)
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]
590 try:
591 res = self.method(*args)
592 return res
593 except Exception:
594 raise exceptions.ServiceNotAvailable
595 return None
597 if __name__ == '__main__':
598 GajimRemote()