[David Flatz] show transports even if show transports is false when they have unread...
[gajim.git] / src / gajim-remote.py
blob8926cf3417dacc97e76dc01fa67d8595e0a266c9
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-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
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'
64 class GajimRemote:
66 def __init__(self):
67 self.argv_len = len(sys.argv)
68 # define commands dict. Prototype :
69 # {
70 # 'command': [comment, [list of arguments] ]
71 # }
73 # each argument is defined as a tuple:
74 # (argument name, help on argument, is mandatory)
76 self.commands = {
77 'help':[
78 _('Shows a help on specific command'),
80 #User gets help for the command, specified by this parameter
81 (_('command'),
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'),
93 'list_contacts': [
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'),
98 False)
102 'list_accounts': [
103 _('Prints a list of registered accounts'),
106 'change_status': [
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)
117 'set_priority': [
118 _('Changes the priority of account or accounts'),
120 (_('priority'), _('priority you want to give to the account'),
121 True),
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)
127 'open_chat': [
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'),
131 True),
132 (_('account'), _('if specified, contact is taken from the '
133 'contact list of this account'), False),
134 (_('message'),
135 _('message content. The account must be specified or ""'),
136 False)
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),
175 'contact_info': [
176 _('Gets detailed info on a contact'),
178 ('jid', _('JID of the contact'), True)
181 'account_info': [
182 _('Gets detailed info on a account'),
184 ('account', _('Name of the account'), True)
187 'send_file': [
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 '
193 'account'), False)
196 'prefs_list': [
197 _('Lists all preferences and their values'),
200 'prefs_put': [
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)
207 'prefs_del': [
208 _('Deletes a preference item'),
210 (_('key'), _('name of the preference to be deleted'), True)
213 'prefs_store': [
214 _('Writes the current state of Gajim preferences to the .config '
215 'file'),
218 'remove_contact': [
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)
227 'add_contact': [
228 _('Adds contact to roster'),
230 (_('jid'), _('JID of the contact'), True),
231 (_('account'), _('Adds new contact to this account'), False)
235 'get_status': [
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'),
253 'start_chat': [
254 _('Opens \'Start Chat\' dialog'),
256 (_('account'), _('Starts chat, using this account'), True)
259 'send_xml': [
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'),
265 False)
268 'handle_uri': [
269 _('Handle a xmpp:/ uri'),
271 (_('uri'), _('URI to handle'), True),
272 (_('account'), _('Account in which you want to handle it'),
273 False),
274 (_('message'), _('Message content'), False)
277 'join_room': [
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 '
284 'room'), False)
287 'check_gajim_running':[
288 _('Check if Gajim is running'),
291 'toggle_ipython' : [
292 _('Shows or hides the ipython window'),
298 self.sbus = None
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)
306 else:
307 print self.compose_help().encode(PREFERRED_ENCODING)
308 sys.exit(0)
309 if self.command == 'handle_uri':
310 self.handle_uri()
311 if self.command == 'check_gajim_running':
312 print self.check_gajim_running()
313 sys.exit(0)
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"'))
321 try:
322 res = self.call_remote_method()
323 except exceptions.ServiceNotAvailable:
324 # At this point an error message has already been displayed
325 sys.exit(1)
326 else:
327 self.print_result(res)
329 def print_result(self, res):
330 ''' Print retrieved result to the output '''
331 if res is not None:
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'):
334 self.argv_len -= 2
336 if res is False:
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])
340 else:
341 send_error(_('You have no active account'))
342 elif self.command == 'list_accounts':
343 if isinstance(res, list):
344 for account in res:
345 if isinstance(account, unicode):
346 print account.encode(PREFERRED_ENCODING)
347 else:
348 print account
349 elif self.command == 'account_info':
350 if res:
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)
361 else:
362 print result
363 elif self.command == 'contact_info':
364 print self.print_info(0, res, True)
365 elif res:
366 print unicode(res).encode(PREFERRED_ENCODING)
368 def check_gajim_running(self):
369 if not self.sbus:
370 try:
371 self.sbus = dbus.SessionBus()
372 except Exception:
373 raise exceptions.SessionBusNotPresent
375 test = False
376 if hasattr(self.sbus, 'name_has_owner'):
377 if self.sbus.name_has_owner(SERVICE):
378 test = True
379 elif dbus.dbus_bindings.bus_name_has_owner(self.sbus.get_connection(),
380 SERVICE):
381 test = True
382 return test
384 def init_connection(self):
385 ''' create the onnection to the session dbus,
386 or exit if it is not possible '''
387 try:
388 self.sbus = dbus.SessionBus()
389 except Exception:
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 '[', ']' '''
403 s = ''
404 for arg in args:
405 if arg[2]:
406 s += ' <' + arg[0] + '>'
407 else:
408 s += ' [' + arg[0] + ']'
409 return s
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'
423 return str_
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):
430 s += ' ' + command
431 for arg in self.commands[command][1]:
432 if arg[2]:
433 s += ' <' + arg[0] + '>'
434 else:
435 s += ' [' + arg[0] + ']'
436 s += '\n'
437 return s
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)):
442 return ''
443 ret_str = ''
444 if isinstance(prop_dict, (list, tuple)):
445 ret_str = ''
446 spacing = ' ' * level * 4
447 for val in prop_dict:
448 if val is None:
449 ret_str +='\t'
450 elif isinstance(val, int):
451 ret_str +='\t' + str(val)
452 elif isinstance(val, (str, unicode)):
453 ret_str +='\t' + val
454 elif isinstance(val, (list, tuple)):
455 res = ''
456 for items in val:
457 res += self.print_info(level+1, items)
458 if res != '':
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():
465 val = prop_dict[key]
466 spacing = ' ' * level * 4
467 if isinstance(val, (unicode, int, str)):
468 if val is not None:
469 val = val.strip()
470 ret_str += '%s%-10s: %s\n' % (spacing, key, val)
471 elif isinstance(val, (list, tuple)):
472 res = ''
473 for items in val:
474 res += self.print_info(level+1, items)
475 if res != '':
476 ret_str += '%s%s: \n%s' % (spacing, key, res)
477 elif isinstance(val, dict):
478 res = self.print_info(level+1, val)
479 if res != '':
480 ret_str += '%s%s: \n%s' % (spacing, key, res)
481 if (encode_return):
482 try:
483 ret_str = ret_str.encode(PREFERRED_ENCODING)
484 except Exception:
485 pass
486 return ret_str
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})
502 self.arguments = []
503 i = 0
504 for arg in sys.argv[2:]:
505 i += 1
506 if i < len(args):
507 self.arguments.append(arg)
508 else:
509 # it's latest argument with spaces
510 self.arguments.append(' '.join(sys.argv[i+1:]))
511 break
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:]
519 uri = sys.argv[2]
520 if not '?' in uri:
521 self.command = sys.argv[1] = 'open_chat'
522 return
523 if 'body=' in uri:
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]
528 try:
529 message = urllib.unquote(message)
530 except UnicodeDecodeError:
531 pass
532 sys.argv[2] = uri.split('?')[0]
533 if len(sys.argv) == 4:
534 # jid in the sys.argv
535 sys.argv.append(message)
536 else:
537 sys.argv.append('')
538 sys.argv.append(message)
539 sys.argv[3] = ''
540 sys.argv[4] = message
541 return
542 (jid, action) = uri.split('?', 1)
543 try:
544 jid = urllib.unquote(jid)
545 except UnicodeDecodeError:
546 pass
547 sys.argv[2] = jid
548 if action == 'join':
549 self.command = sys.argv[1] = 'join_room'
550 # Move account parameter from position 3 to 5
551 sys.argv.append('')
552 sys.argv.append(sys.argv[3])
553 sys.argv[3] = ''
554 return
555 if action.startswith('roster'):
556 # Add contact to roster
557 self.command = sys.argv[1] = 'add_contact'
558 return
559 sys.exit(0)
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]
565 try:
566 res = self.method(*args)
567 return res
568 except Exception:
569 raise exceptions.ServiceNotAvailable
570 return None
572 if __name__ == '__main__':
573 GajimRemote()
575 # vim: se ts=3: