correctly set transient window for muc error dialogs. Fixes #6943
[gajim.git] / src / gajim-remote.py
blob7ad39f064c741d381f94836ab8c00e39dde84a42
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 _('D-Bus is not present on this machine or python module is missing')
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',
343 'send_single_message', 'start_chat'):
344 if self.command in ('send_message', 'send_single_message'):
345 self.argv_len -= 2
347 if res is False:
348 if self.argv_len < 4:
349 send_error(_('\'%s\' is not in your roster.\n'
350 'Please specify account for sending the message.') % sys.argv[2])
351 else:
352 send_error(_('You have no active account'))
353 elif self.command == 'list_accounts':
354 if isinstance(res, list):
355 for account in res:
356 if isinstance(account, unicode):
357 print account.encode(PREFERRED_ENCODING)
358 else:
359 print account
360 elif self.command == 'account_info':
361 if res:
362 print self.print_info(0, res, True)
363 elif self.command == 'list_contacts':
364 for account_dict in res:
365 print self.print_info(0, account_dict, True)
366 elif self.command == 'prefs_list':
367 pref_keys = sorted(res.keys())
368 for pref_key in pref_keys:
369 result = '%s = %s' % (pref_key, res[pref_key])
370 if isinstance(result, unicode):
371 print result.encode(PREFERRED_ENCODING)
372 else:
373 print result
374 elif self.command == 'contact_info':
375 print self.print_info(0, res, True)
376 elif res:
377 print unicode(res).encode(PREFERRED_ENCODING)
379 def check_gajim_running(self):
380 if not self.sbus:
381 try:
382 self.sbus = dbus.SessionBus()
383 except Exception:
384 raise exceptions.SessionBusNotPresent
386 test = False
387 if hasattr(self.sbus, 'name_has_owner'):
388 if self.sbus.name_has_owner(SERVICE):
389 test = True
390 elif dbus.dbus_bindings.bus_name_has_owner(self.sbus.get_connection(),
391 SERVICE):
392 test = True
393 return test
395 def init_connection(self):
397 Create the onnection to the session dbus, or exit if it is not possible
399 try:
400 self.sbus = dbus.SessionBus()
401 except Exception:
402 raise exceptions.SessionBusNotPresent
404 if not self.check_gajim_running():
405 send_error(_('It seems Gajim is not running. So you can\'t use gajim-remote.'))
406 obj = self.sbus.get_object(SERVICE, OBJ_PATH)
407 interface = dbus.Interface(obj, INTERFACE)
409 # get the function asked
410 self.method = interface.__getattr__(self.command)
412 def make_arguments_row(self, args):
414 Return arguments list. Mandatory arguments are enclosed with:
415 '<', '>', optional arguments - with '[', ']'
417 s = ''
418 for arg in args:
419 if arg[2]:
420 s += ' <' + arg[0] + '>'
421 else:
422 s += ' [' + arg[0] + ']'
423 return s
425 def help_on_command(self, command):
427 Return help message for a given command
429 if command in self.commands:
430 command_props = self.commands[command]
431 arguments_str = self.make_arguments_row(command_props[1])
432 str_ = _('Usage: %(basename)s %(command)s %(arguments)s \n\t %(help)s')\
433 % {'basename': BASENAME, 'command': command,
434 'arguments': arguments_str, 'help': command_props[0]}
435 if len(command_props[1]) > 0:
436 str_ += '\n\n' + _('Arguments:') + '\n'
437 for argument in command_props[1]:
438 str_ += ' ' + argument[0] + ' - ' + argument[1] + '\n'
439 return str_
440 send_error(_('%s not found') % command)
442 def compose_help(self):
444 Print usage, and list available commands
446 s = _('Usage: %s command [arguments]\nCommand is one of:\n' ) % BASENAME
447 for command in sorted(self.commands):
448 s += ' ' + command
449 for arg in self.commands[command][1]:
450 if arg[2]:
451 s += ' <' + arg[0] + '>'
452 else:
453 s += ' [' + arg[0] + ']'
454 s += '\n'
455 return s
457 def print_info(self, level, prop_dict, encode_return = False):
459 Return formated string from data structure
461 if prop_dict is None or not isinstance(prop_dict, (dict, list, tuple)):
462 return ''
463 ret_str = ''
464 if isinstance(prop_dict, (list, tuple)):
465 ret_str = ''
466 spacing = ' ' * level * 4
467 for val in prop_dict:
468 if val is None:
469 ret_str +='\t'
470 elif isinstance(val, int):
471 ret_str +='\t' + str(val)
472 elif isinstance(val, (str, unicode)):
473 ret_str +='\t' + val
474 elif isinstance(val, (list, tuple)):
475 res = ''
476 for items in val:
477 res += self.print_info(level+1, items)
478 if res != '':
479 ret_str += '\t' + res
480 elif isinstance(val, dict):
481 ret_str += self.print_info(level+1, val)
482 ret_str = '%s(%s)\n' % (spacing, ret_str[1:])
483 elif isinstance(prop_dict, dict):
484 for key in prop_dict.keys():
485 val = prop_dict[key]
486 spacing = ' ' * level * 4
487 if isinstance(val, (unicode, int, str)):
488 if val is not None:
489 val = val.strip()
490 ret_str += '%s%-10s: %s\n' % (spacing, key, val)
491 elif isinstance(val, (list, tuple)):
492 res = ''
493 for items in val:
494 res += self.print_info(level+1, items)
495 if res != '':
496 ret_str += '%s%s: \n%s' % (spacing, key, res)
497 elif isinstance(val, dict):
498 res = self.print_info(level+1, val)
499 if res != '':
500 ret_str += '%s%s: \n%s' % (spacing, key, res)
501 if (encode_return):
502 try:
503 ret_str = ret_str.encode(PREFERRED_ENCODING)
504 except Exception:
505 pass
506 return ret_str
508 def check_arguments(self):
510 Make check if all necessary arguments are given
512 argv_len = self.argv_len - 2
513 args = self.commands[self.command][1]
514 if len(args) < argv_len:
515 send_error(_('Too many arguments. \n'
516 'Type "%(basename)s help %(command)s" for more info') % {
517 'basename': BASENAME, 'command': self.command})
518 if len(args) > argv_len:
519 if args[argv_len][2]:
520 send_error(_('Argument "%(arg)s" is not specified. \n'
521 'Type "%(basename)s help %(command)s" for more info') %
522 {'arg': args[argv_len][0], 'basename': BASENAME,
523 'command': self.command})
524 self.arguments = []
525 i = 0
526 for arg in sys.argv[2:]:
527 i += 1
528 if i < len(args):
529 self.arguments.append(arg)
530 else:
531 # it's latest argument with spaces
532 self.arguments.append(' '.join(sys.argv[i+1:]))
533 break
534 # add empty string for missing args
535 self.arguments += ['']*(len(args)-i)
537 def handle_uri(self):
538 if len(sys.argv) < 3:
539 send_error(_('No uri given'))
540 if not sys.argv[2].startswith('xmpp:'):
541 send_error(_('Wrong uri'))
542 sys.argv[2] = sys.argv[2][5:]
543 uri = sys.argv[2]
544 if not '?' in uri:
545 self.command = sys.argv[1] = 'open_chat'
546 return
547 if 'body=' in uri:
548 # Open chat window and paste the text in the input message dialog
549 self.command = sys.argv[1] = 'open_chat'
550 message = uri.split('body=')
551 message = message[1].split(';')[0]
552 try:
553 message = urllib.unquote(message)
554 except UnicodeDecodeError:
555 pass
556 sys.argv[2] = uri.split('?')[0]
557 if len(sys.argv) == 4:
558 # jid in the sys.argv
559 sys.argv.append(message)
560 else:
561 sys.argv.append('')
562 sys.argv.append(message)
563 sys.argv[3] = ''
564 sys.argv[4] = message
565 return
566 (jid, action) = uri.split('?', 1)
567 try:
568 jid = urllib.unquote(jid)
569 except UnicodeDecodeError:
570 pass
571 sys.argv[2] = jid
572 if action == 'join':
573 self.command = sys.argv[1] = 'join_room'
574 # Move account parameter from position 3 to 5
575 sys.argv.append('')
576 sys.argv.append(sys.argv[3])
577 sys.argv[3] = ''
578 return
579 if action.startswith('roster'):
580 # Add contact to roster
581 self.command = sys.argv[1] = 'add_contact'
582 return
583 sys.exit(0)
585 def call_remote_method(self):
587 Calls self.method with arguments from sys.argv[2:]
589 args = [i.decode(PREFERRED_ENCODING) for i in self.arguments]
590 args = [dbus.String(i) for i in args]
591 try:
592 res = self.method(*args)
593 return res
594 except Exception:
595 raise exceptions.ServiceNotAvailable
596 return None
598 if __name__ == '__main__':
599 GajimRemote()