From 673788bf46c71a9945d65b91bb1ba03e463ea31e Mon Sep 17 00:00:00 2001 From: mathieui Date: Sat, 5 Apr 2014 17:50:50 +0200 Subject: Split the Core class Although the logic stays the same, and everything is put back together in a single class. --- src/core/__init__.py | 8 + src/core/commands.py | 892 ++++++++++++++++++++++++ src/core/completions.py | 381 +++++++++++ src/core/core.py | 1733 +++++++++++++++++++++++++++++++++++++++++++++++ src/core/handlers.py | 1022 ++++++++++++++++++++++++++++ src/core/structs.py | 50 ++ 6 files changed, 4086 insertions(+) create mode 100644 src/core/__init__.py create mode 100644 src/core/commands.py create mode 100644 src/core/completions.py create mode 100644 src/core/core.py create mode 100644 src/core/handlers.py create mode 100644 src/core/structs.py (limited to 'src/core') diff --git a/src/core/__init__.py b/src/core/__init__.py new file mode 100644 index 00000000..6a82e2bb --- /dev/null +++ b/src/core/__init__.py @@ -0,0 +1,8 @@ +""" +Core class, splitted into smaller chunks +""" + +from . core import Core +from . structs import Command, Status, possible_show, DEPRECATED_ERRORS, \ + ERROR_AND_STATUS_CODES + diff --git a/src/core/commands.py b/src/core/commands.py new file mode 100644 index 00000000..fb8dc2eb --- /dev/null +++ b/src/core/commands.py @@ -0,0 +1,892 @@ +""" +Global commands which are to be linked to the Core class +""" + +import logging + +log = logging.getLogger(__name__) + +import os +import sys +from datetime import datetime +from gettext import gettext as _ +from xml.etree import cElementTree as ET + +from sleekxmpp.xmlstream.stanzabase import StanzaBase +from sleekxmpp.xmlstream.handler import Callback +from sleekxmpp.xmlstream.matcher import StanzaPath + +import bookmark +import common +import fixes +import pep +import pubsub +import tabs +import theming +from common import safeJID +from config import config, options as config_opts +import multiuserchat as muc +from roster import roster +from theming import dump_tuple, get_theme + +from . structs import Command, possible_show + + +def command_help(self, arg): + """ + /help + """ + args = arg.split() + if not args: + color = dump_tuple(get_theme().COLOR_HELP_COMMANDS) + acc = [] + buff = ['Global commands:'] + for command in self.commands: + if isinstance(self.commands[command], Command): + acc.append(' \x19%s}%s\x19o - %s' % (color, command, self.commands[command].short)) + else: + acc.append(' \x19%s}%s\x19o' % (color, command)) + acc = sorted(acc) + buff.extend(acc) + acc = [] + buff.append('Tab-specific commands:') + commands = self.current_tab().commands + for command in commands: + if isinstance(commands[command], Command): + acc.append(' \x19%s}%s\x19o - %s' % (color, command, commands[command].short)) + else: + acc.append(' \x19%s}%s\x19o' % (color, command)) + acc = sorted(acc) + buff.extend(acc) + + msg = '\n'.join(buff) + msg += _("\nType /help to know what each command does") + if args: + command = args[0].lstrip('/').strip() + + if command in self.current_tab().commands: + tup = self.current_tab().commands[command] + elif command in self.commands: + tup = self.commands[command] + else: + self.information(_('Unknown command: %s') % command, 'Error') + return + if isinstance(tup, Command): + msg = _('Usage: /%s %s\n' % (command, tup.usage)) + msg += tup.desc + else: + msg = tup[1] + self.information(msg, 'Help') + +def command_runkey(self, arg): + """ + /runkey + """ + def replace_line_breaks(key): + if key == '^J': + return '\n' + return key + char = arg.strip() + func = self.key_func.get(char, None) + if func: + func() + else: + res = self.do_command(replace_line_breaks(char), False) + if res: + self.refresh_window() + +def command_status(self, arg): + """ + /status [msg] + """ + args = common.shell_split(arg) + if len(args) < 1: + return + if not args[0] in possible_show.keys(): + self.command_help('status') + return + show = possible_show[args[0]] + if len(args) == 2: + msg = args[1] + else: + msg = None + pres = self.xmpp.make_presence() + if msg: + pres['status'] = msg + pres['type'] = show + self.events.trigger('send_normal_presence', pres) + pres.send() + current = self.current_tab() + if isinstance(current, tabs.MucTab) and current.joined and show in ('away', 'xa'): + current.send_chat_state('inactive') + for tab in self.tabs: + if isinstance(tab, tabs.MucTab) and tab.joined: + muc.change_show(self.xmpp, tab.name, tab.own_nick, show, msg) + if hasattr(tab, 'directed_presence'): + del tab.directed_presence + self.set_status(show, msg) + if isinstance(current, tabs.MucTab) and current.joined and show not in ('away', 'xa'): + current.send_chat_state('active') + +def command_presence(self, arg): + """ + /presence [type] [status] + """ + args = common.shell_split(arg) + if len(args) == 1: + jid, type, status = args[0], None, None + elif len(args) == 2: + jid, type, status = args[0], args[1], None + elif len(args) == 3: + jid, type, status = args[0], args[1], args[2] + else: + return + if jid == '.' and isinstance(self.current_tab(), tabs.ChatTab): + jid = self.current_tab().get_name() + if type == 'available': + type = None + try: + pres = self.xmpp.make_presence(pto=jid, ptype=type, pstatus=status) + self.events.trigger('send_normal_presence', pres) + pres.send() + except: + self.information(_('Could not send directed presence'), 'Error') + log.debug('Could not send directed presence to %s', jid, exc_info=True) + return + tab = self.get_tab_by_name(jid) + if tab: + if type in ('xa', 'away'): + tab.directed_presence = False + chatstate = 'inactive' + else: + tab.directed_presence = True + chatstate = 'active' + if tab == self.current_tab(): + tab.send_chat_state(chatstate, True) + if isinstance(tab, tabs.MucTab): + for private in tab.privates: + private.directed_presence = tab.directed_presence + if self.current_tab() in tab.privates: + self.current_tab().send_chat_state(chatstate, True) + +def command_theme(self, arg=''): + """/theme """ + args = arg.split() + if args: + self.command_set('theme %s' % (args[0],)) + warning = theming.reload_theme() + if warning: + self.information(warning, 'Warning') + self.refresh_window() + +def command_win(self, arg): + """ + /win + """ + arg = arg.strip() + if not arg: + self.command_help('win') + return + try: + nb = int(arg.split()[0]) + except ValueError: + nb = arg + if self.current_tab_nb == nb: + return + self.previous_tab_nb = self.current_tab_nb + old_tab = self.current_tab() + if isinstance(nb, int): + if 0 <= nb < len(self.tabs): + if not self.tabs[nb]: + return + self.current_tab_nb = nb + else: + matchs = [] + for tab in self.tabs: + for name in tab.matching_names(): + if nb.lower() in name[1].lower(): + matchs.append((name[0], tab)) + self.current_tab_nb = tab.nb + if not matchs: + return + tab = min(matchs, key=lambda m: m[0])[1] + self.current_tab_nb = tab.nb + old_tab.on_lose_focus() + self.current_tab().on_gain_focus() + self.refresh_window() + +def command_move_tab(self, arg): + """ + /move_tab old_pos new_pos + """ + args = common.shell_split(arg) + current_tab = self.current_tab() + if len(args) != 2: + return self.command_help('move_tab') + def get_nb_from_value(value): + ref = None + try: + ref = int(value) + except ValueError: + old_tab = None + for tab in self.tabs: + if not old_tab and value == tab.get_name(): + old_tab = tab + if not old_tab: + self.information("Tab %s does not exist" % args[0], "Error") + return None + ref = old_tab.nb + return ref + old = get_nb_from_value(args[0]) + new = get_nb_from_value(args[1]) + if new is None or old is None: + return self.information('Unable to move the tab.', 'Info') + result = self.insert_tab(old, new) + if not result: + self.information('Unable to move the tab.', 'Info') + else: + self.current_tab_nb = self.tabs.index(current_tab) + self.refresh_window() + +def command_list(self, arg): + """ + /list + Opens a MucListTab containing the list of the room in the specified server + """ + arg = arg.split() + if len(arg) > 1: + return self.command_help('list') + elif arg: + server = safeJID(arg[0]).server + else: + if not isinstance(self.current_tab(), tabs.MucTab): + return self.information('Please provide a server', 'Error') + server = safeJID(self.current_tab().get_name()).server + list_tab = tabs.MucListTab(server) + self.add_tab(list_tab, True) + self.xmpp.plugin['xep_0030'].get_items(jid=server, block=False, callback=list_tab.on_muc_list_item_received) + +def command_version(self, arg): + """ + /version + """ + def callback(res): + if not res: + return self.information('Could not get the software version from %s' % (jid,), 'Warning') + version = '%s is running %s version %s on %s' % (jid, + res.get('name') or _('an unknown software'), + res.get('version') or _('unknown'), + res.get('os') or _('an unknown platform')) + self.information(version, 'Info') + + args = common.shell_split(arg) + if len(args) < 1: + return self.command_help('version') + jid = safeJID(args[0]) + if jid.resource or jid not in roster: + fixes.get_version(self.xmpp, jid, callback=callback) + elif jid in roster: + for resource in roster[jid].resources: + fixes.get_version(self.xmpp, resource.jid, callback=callback) + else: + fixes.get_version(self.xmpp, jid, callback=callback) + +def command_join(self, arg, histo_length=None): + """ + /join [room][/nick] [password] + """ + args = common.shell_split(arg) + password = None + if len(args) == 0: + tab = self.current_tab() + if not isinstance(tab, tabs.MucTab) and not isinstance(tab, tabs.PrivateTab): + return + room = safeJID(tab.get_name()).bare + nick = tab.own_nick + else: + if args[0].startswith('@'): # we try to join a server directly + server_root = True + info = safeJID(args[0][1:]) + else: + info = safeJID(args[0]) + server_root = False + if info == '' and len(args[0]) > 1 and args[0][0] == '/': + nick = args[0][1:] + elif info.resource == '': + default = os.environ.get('USER') if os.environ.get('USER') else 'poezio' + nick = config.get('default_nick', '') + if nick == '': + nick = default + else: + nick = info.resource + if info.bare == '': # happens with /join /nickname, which is OK + tab = self.current_tab() + if not isinstance(tab, tabs.MucTab): + return + room = tab.get_name() + if nick == '': + nick = tab.own_nick + else: + room = info.bare + if room.find('@') == -1 and not server_root: # no server is provided, like "/join hello" + # use the server of the current room if available + # check if the current room's name has a server + if isinstance(self.current_tab(), tabs.MucTab) and\ + self.current_tab().get_name().find('@') != -1: + room += '@%s' % safeJID(self.current_tab().get_name()).domain + else: + room = args[0] + room = room.lower() + if room in self.pending_invites: + del self.pending_invites[room] + tab = self.get_tab_by_name(room, tabs.MucTab) + if len(args) == 2: # a password is provided + password = args[1] + if tab and tab.joined: # if we are already in the room + self.focus_tab_named(tab.name) + if tab.own_nick == nick: + self.information('/join: Nothing to do.', 'Info') + else: + tab.own_nick = nick + tab.command_cycle('') + return + + if room.startswith('@'): + room = room[1:] + current_status = self.get_status() + if not histo_length: + histo_length = config.get('muc_history_length', 20) + if histo_length == -1: + histo_length = None + if histo_length is not None: + histo_length = str(histo_length) + if password is None: # try to use a saved password + password = config.get_by_tabname('password', None, room, fallback=False) + if tab and not tab.joined: + if tab.last_connection: + delta = datetime.now() - tab.last_connection + seconds = delta.seconds + delta.days * 24 * 3600 if tab.last_connection is not None else 0 + seconds = int(seconds) + else: + seconds = 0 + muc.join_groupchat(self, room, nick, password, + histo_length, current_status.message, current_status.show, seconds=seconds) + if not tab: + self.open_new_room(room, nick) + muc.join_groupchat(self, room, nick, password, + histo_length, current_status.message, current_status.show) + else: + tab.own_nick = nick + tab.users = [] + if tab and tab.joined: + self.enable_private_tabs(room) + tab.state = "normal" + if tab == self.current_tab(): + tab.refresh() + self.doupdate() + +def command_bookmark_local(self, arg=''): + """ + /bookmark_local [room][/nick] [password] + """ + args = common.shell_split(arg) + nick = None + password = None + if not args and not isinstance(self.current_tab(), tabs.MucTab): + return + if not args: + tab = self.current_tab() + roomname = tab.get_name() + if tab.joined and tab.own_nick != self.own_nick: + nick = tab.own_nick + elif args[0] == '*': + new_bookmarks = [] + for tab in self.get_tabs(tabs.MucTab): + b = bookmark.get_by_jid(tab.get_name()) + if not b: + b = bookmark.Bookmark(tab.get_name(), autojoin=True, method="local") + new_bookmarks.append(b) + else: + b.method = "local" + new_bookmarks.append(b) + bookmark.bookmarks.remove(b) + new_bookmarks.extend(bookmark.bookmarks) + bookmark.bookmarks = new_bookmarks + bookmark.save_local() + bookmark.save_remote(self.xmpp) + self.information('Bookmarks added and saved.', 'Info') + return + else: + info = safeJID(args[0]) + if info.resource != '': + nick = info.resource + roomname = info.bare + if not roomname: + if not isinstance(self.current_tab(), tabs.MucTab): + return + roomname = self.current_tab().get_name() + if len(args) > 1: + password = args[1] + + bm = bookmark.get_by_jid(roomname) + if not bm: + bm = bookmark.Bookmark(jid=roomname) + bookmark.bookmarks.append(bm) + self.information('Bookmark added.', 'Info') + else: + self.information('Bookmark updated.', 'Info') + if nick: + bm.nick = nick + bm.autojoin = True + bm.password = password + bm.method = "local" + bookmark.save_local() + self.information(_('Your local bookmarks are now: %s') % + [b for b in bookmark.bookmarks if b.method == 'local'], 'Info') + +def command_bookmark(self, arg=''): + """ + /bookmark [room][/nick] [autojoin] [password] + """ + + if not config.get('use_remote_bookmarks', True): + self.command_bookmark_local(arg) + return + args = common.shell_split(arg) + nick = None + if not args and not isinstance(self.current_tab(), tabs.MucTab): + return + if not args: + tab = self.current_tab() + roomname = tab.get_name() + if tab.joined: + nick = tab.own_nick + autojoin = True + password = None + elif args[0] == '*': + if len(args) > 1: + autojoin = False if args[1].lower() != 'true' else True + else: + autojoin = True + new_bookmarks = [] + for tab in self.get_tabs(tabs.MucTab): + b = bookmark.get_by_jid(tab.get_name()) + if not b: + b = bookmark.Bookmark(tab.get_name(), autojoin=autojoin, + method=bookmark.preferred) + new_bookmarks.append(b) + else: + b.method = bookmark.preferred + bookmark.bookmarks.remove(b) + new_bookmarks.append(b) + new_bookmarks.extend(bookmark.bookmarks) + bookmark.bookmarks = new_bookmarks + + if bookmark.save_remote(self.xmpp): + bookmark.save_local() + self.information("Bookmarks added.", "Info") + else: + self.information("Could not add the bookmarks.", "Info") + return + else: + info = safeJID(args[0]) + if info.resource != '': + nick = info.resource + roomname = info.bare + if roomname == '': + if not isinstance(self.current_tab(), tabs.MucTab): + return + roomname = self.current_tab().get_name() + if len(args) > 1: + autojoin = False if args[1].lower() != 'true' else True + else: + autojoin = True + if len(args) > 2: + password = args[2] + else: + password = None + bm = bookmark.get_by_jid(roomname) + if not bm: + bm = bookmark.Bookmark(roomname) + bookmark.bookmarks.append(bm) + bm.method = config.get('use_bookmarks_method', 'pep') + if nick: + bm.nick = nick + if password: + bm.password = password + bm.autojoin = autojoin + if bookmark.save_remote(self.xmpp): + self.information('Bookmark added.', 'Info') + self.information(_('Your remote bookmarks are now: %s') % + [b for b in bookmark.bookmarks if b.method in ('pep', 'privatexml')], 'Info') + +def command_bookmarks(self, arg=''): + """/bookmarks""" + self.information(_('Your remote bookmarks are: %s') % + [b for b in bookmark.bookmarks if b.method in ('pep', 'privatexml')], 'Info') + self.information(_('Your local bookmarks are: %s') % + [b for b in bookmark.bookmarks if b.method is 'local'], 'Info') + +def command_remove_bookmark(self, arg=''): + """/remove_bookmark [jid]""" + args = common.shell_split(arg) + if not args: + tab = self.current_tab() + if isinstance(tab, tabs.MucTab) and bookmark.get_by_jid(tab.get_name()): + bookmark.remove(tab.get_name()) + bookmark.save(self.xmpp) + if bookmark.save(self.xmpp): + self.information('Bookmark deleted', 'Info') + else: + self.information('No bookmark to remove', 'Info') + else: + if bookmark.get_by_jid(args[0]): + bookmark.remove(args[0]) + if bookmark.save(self.xmpp): + self.information('Bookmark deleted', 'Info') + + else: + self.information('No bookmark to remove', 'Info') + +def command_set(self, arg): + """ + /set [module|][section]