Commit f808f0cf authored by mathieui's avatar mathieui

core: move initial commands in a separate files

(improve core.py SNR)
parent 08f48b0c
from poezio.core.commands import CommandCore
from poezio.core.completions import CompletionCore
from poezio.plugin_manager import PluginManager
from typing import TypedDict, Callable, List
CommandDict = TypedDict(
"CommandDict",
{
"name": str,
"func": Callable,
"shortdesc": str,
"desc": str,
"completion": Callable,
},
total=False,
)
def get_commands(commands: CommandCore, completions: CompletionCore, plugin_manager: PluginManager) -> List[CommandDict]:
"""
Get the set of default poezio commands.
"""
return [
{
"name": "help",
"func": commands.help,
"usage": "[command]",
"shortdesc": "\\_o< KOIN KOIN KOIN",
"completion": completions.help,
},
{
"name": "join",
"func": commands.join,
"usage": "[room_name][@server][/nick] [password]",
"desc": (
"Join the specified room. You can specify a nickname "
"after a slash (/). If no nickname is specified, you will"
" use the default_nick in the configuration file. You can"
" omit the room name: you will then join the room you're"
" looking at (useful if you were kicked). You can also "
"provide a room_name without specifying a server, the "
"server of the room you're currently in will be used. You"
" can also provide a password to join the room.\nExamples"
":\n/join room@server.tld\n/join room@server.tld/John\n"
"/join room2\n/join /me_again\n/join\n/join room@server"
".tld/my_nick password\n/join / password"
),
"shortdesc": "Join a room",
"completion": completions.join,
},
{
"name": "exit",
"func": commands.quit,
"desc": "Just disconnect from the server and exit poezio.",
"shortdesc": "Exit poezio.",
},
{
"name": "quit",
"func": commands.quit,
"desc": "Just disconnect from the server and exit poezio.",
"shortdesc": "Exit poezio.",
},
{
"name": "next",
"func": commands.rotate_rooms_right,
"shortdesc": "Go to the next room.",
},
{
"name": "prev",
"func": commands.rotate_rooms_left,
"shortdesc": "Go to the previous room.",
},
{
"name": "win",
"func": commands.win,
"usage": "<number or name>",
"shortdesc": "Go to the specified room",
"completion": completions.win,
},
{
"name": "w",
"func": commands.win,
"usage": "<number or name>",
"shortdesc": "Go to the specified room",
"completion": completions.win,
},
{
"name": "wup",
"func": commands.wup,
"usage": "<prefix>",
"shortdesc": "Go to the tab whose name uniquely starts with prefix",
"completion": completions.win,
},
{
"name": "move_tab",
"func": commands.move_tab,
"usage": "<source> <destination>",
"desc": (
"Insert the <source> tab at the position of "
"<destination>. This will make the following tabs shift in"
" some cases (refer to the documentation). A tab can be "
"designated by its number or by the beginning of its "
'address. You can use "." as a shortcut for the current '
"tab."
),
"shortdesc": "Move a tab.",
"completion": completions.move_tab,
},
{
"name": "destroy_room",
"func": commands.destroy_room,
"usage": "[room JID]",
"desc": (
"Try to destroy the room [room JID], or the current"
" tab if it is a multi-user chat and [room JID] is "
"not given."
),
"shortdesc": "Destroy a room.",
"completion": None,
},
{
"name": "status",
"func": commands.status,
"usage": "<availability> [status message]",
"desc": (
"Sets your availability and (optionally) your status "
'message. The <availability> argument is one of "available'
', chat, away, afk, dnd, busy, xa" and the optional '
"[status message] argument will be your status message."
),
"shortdesc": "Change your availability.",
"completion": completions.status,
},
{
"name": "show",
"func": commands.status,
"usage": "<availability> [status message]",
"desc": (
"Sets your availability and (optionally) your status "
'message. The <availability> argument is one of "available'
', chat, away, afk, dnd, busy, xa" and the optional '
"[status message] argument will be your status message."
),
"shortdesc": "Change your availability.",
"completion": completions.status,
},
{
"name": "bookmark_local",
"func": commands.bookmark_local,
"usage": "[roomname][/nick] [password]",
"desc": (
"Bookmark Local: Bookmark locally the specified room "
"(you will then auto-join it on each poezio start). This"
" commands uses almost the same syntaxe as /join. Type "
"/help join for syntax examples. Note that when typing "
'"/bookmark" on its own, the room will be bookmarked '
"with the nickname you're currently using in this room "
"(instead of default_nick)"
),
"shortdesc": "Bookmark a room locally.",
"completion": completions.bookmark_local,
},
{
"name": "bookmark",
"func": commands.bookmark,
"usage": "[roomname][/nick] [autojoin] [password]",
"desc": (
"Bookmark: Bookmark online the specified room (you "
"will then auto-join it on each poezio start if autojoin"
" is specified and is 'true'). This commands uses almost"
" the same syntax as /join. Type /help join for syntax "
'examples. Note that when typing "/bookmark" alone, the'
" room will be bookmarked with the nickname you're "
"currently using in this room (instead of default_nick)."
),
"shortdesc": "Bookmark a room online.",
"completion": completions.bookmark,
},
{
"name": "accept",
"func": commands.accept,
"usage": "[jid]",
"desc": (
"Allow the provided JID (or the selected contact "
"in your roster), to see your presence."
),
"shortdesc": "Allow a user your presence.",
"completion": completions.roster_barejids,
},
{
"name": "add",
"func": commands.add,
"usage": "<jid>",
"desc": (
"Add the specified JID to your roster, ask them to"
" allow you to see his presence, and allow them to"
" see your presence."
),
"shortdesc": "Add a user to your roster.",
},
{
"name": "deny",
"func": commands.deny,
"usage": "[jid]",
"desc": (
"Deny your presence to the provided JID (or the "
"selected contact in your roster), who is asking"
"you to be in their roster."
),
"shortdesc": "Deny a user your presence.",
"completion": completions.roster_barejids,
},
{
"name": "remove",
"func": commands.remove,
"usage": "[jid]",
"desc": (
"Remove the specified JID from your roster. This "
"will unsubscribe you from its presence, cancel "
"its subscription to yours, and remove the item "
"from your roster."
),
"shortdesc": "Remove a user from your roster.",
"completion": completions.remove,
},
{
"name": "reconnect",
"func": commands.command_reconnect,
"usage": "[reconnect]",
"desc": (
"Disconnect from the remote server if you are "
"currently connected and then connect to it again."
),
"shortdesc": "Disconnect and reconnect to the server.",
},
{
"name": "set",
"func": commands.set,
"usage": "[plugin|][section] <option> [value]",
"desc": (
"Set the value of an option in your configuration file."
" You can, for example, change your default nickname by "
"doing `/set default_nick toto` or your resource with `/set"
" resource blabla`. You can also set options in specific "
"sections with `/set bindings M-i ^i` or in specific plugin"
" with `/set mpd_client| host 127.0.0.1`. `toggle` can be "
"used as a special value to toggle a boolean option."
),
"shortdesc": "Set the value of an option",
"completion": completions.set,
},
{
"name": "set_default",
"func": commands.set_default,
"usage": "[section] <option>",
"desc": (
"Set the default value of an option. For example, "
"`/set_default resource` will reset the resource "
"option. You can also reset options in specific "
"sections by doing `/set_default section option`."
),
"shortdesc": "Set the default value of an option",
"completion": completions.set_default,
},
{
"name": "toggle",
"func": commands.toggle,
"usage": "<option>",
"desc": "Shortcut for /set <option> toggle",
"shortdesc": "Toggle an option",
"completion": completions.toggle,
},
{
"name": "theme",
"func": commands.theme,
"usage": "[theme name]",
"desc": (
"Reload the theme defined in the config file. If theme"
"_name is provided, set that theme before reloading it."
),
"shortdesc": "Load a theme",
"completion": completions.theme,
},
{
"name": "list",
"func": commands.list,
"usage": "[server]",
"desc": "Get the list of public rooms" " on the specified server.",
"shortdesc": "List the rooms.",
"completion": completions.list,
},
{
"name": "message",
"func": commands.message,
"usage": "<jid> [optional message]",
"desc": (
"Open a conversation with the specified JID (even if it"
" is not in our roster), and send a message to it, if the "
"message is specified."
),
"shortdesc": "Send a message",
"completion": completions.message,
},
{
"name": "version",
"func": commands.version,
"usage": "<jid>",
"desc": (
"Get the software version of the given JID (usually its"
" XMPP client and Operating System)."
),
"shortdesc": "Get the software version of a JID.",
"completion": completions.version,
},
{
"name": "server_cycle",
"func": commands.server_cycle,
"usage": "[domain] [message]",
"desc": "Disconnect and reconnect in all the rooms in domain.",
"shortdesc": "Cycle a range of rooms",
"completion": completions.server_cycle,
},
{
"name": "bind",
"func": commands.bind,
"usage": "<key> <equ>",
"desc": (
"Bind a key to another key or to a “command”. For "
'example "/bind ^H KEY_UP" makes Control + h do the'
" same same as the Up key."
),
"completion": completions.bind,
"shortdesc": "Bind a key to another key.",
},
{
"name": "load",
"func": commands.load,
"usage": "<plugin> [<otherplugin> …]",
"shortdesc": "Load the specified plugin(s)",
"completion": plugin_manager.completion_load,
},
{
"name": "unload",
"func": commands.unload,
"usage": "<plugin> [<otherplugin> …]",
"shortdesc": "Unload the specified plugin(s)",
"completion": plugin_manager.completion_unload,
},
{
"name": "plugins",
"func": commands.plugins,
"shortdesc": "Show the plugins in use.",
},
{
"name": "presence",
"func": commands.presence,
"usage": "<JID> [type] [status]",
"desc": "Send a directed presence to <JID> and using"
" [type] and [status] if provided.",
"shortdesc": "Send a directed presence.",
"completion": completions.presence,
},
{
"name": "rawxml",
"func": commands.rawxml,
"usage": "<xml>",
"shortdesc": "Send a custom xml stanza.",
},
{
"name": "invite",
"func": commands.invite,
"usage": "<jid> <room> [reason]",
"desc": "Invite jid in room with reason.",
"shortdesc": "Invite someone in a room.",
"completion": completions.invite,
},
{
"name": "impromptu",
"func": commands.impromptu,
"usage": "<jid> [jid ...]",
"desc": "Invite specified JIDs into a newly created room.",
"shortdesc": "Invite specified JIDs into newly created room.",
"completion": completions.impromptu,
},
{
"name": "invitations",
"func": commands.invitations,
"shortdesc": "Show the pending invitations.",
},
{
"name": "bookmarks",
"func": commands.bookmarks,
"shortdesc": "Show the current bookmarks.",
},
{
"name": "remove_bookmark",
"func": commands.remove_bookmark,
"usage": "[jid]",
"desc": "Remove the specified bookmark, or the "
"bookmark on the current tab, if any.",
"shortdesc": "Remove a bookmark",
"completion": completions.remove_bookmark,
},
{
"name": "xml_tab",
"func": commands.xml_tab,
"shortdesc": "Open an XML tab.",
},
{
"name": "runkey",
"func": commands.runkey,
"usage": "<key>",
"shortdesc": "Execute the action defined for <key>.",
"completion": completions.runkey,
},
{
"name": "self",
"func": commands.self_,
"shortdesc": "Remind you of who you are.",
},
{
"name": "last_activity",
"func": commands.last_activity,
"usage": "<jid>",
"desc": "Informs you of the last activity of a JID.",
"shortdesc": "Get the activity of someone.",
"completion": completions.last_activity,
},
{
"name": "ad-hoc",
"func": commands.adhoc,
"usage": "<jid>",
"shortdesc": "List available ad-hoc commands on the given jid",
},
{
"name": "reload",
"func": commands.reload,
"shortdesc": "Reload the config. You can achieve the same by "
"sending SIGUSR1 to poezio.",
},
]
...@@ -37,6 +37,14 @@ class CommandCore: ...@@ -37,6 +37,14 @@ class CommandCore:
def __init__(self, core): def __init__(self, core):
self.core = core self.core = core
@command_args_parser.ignored
def rotate_rooms_left(self, args=None):
self.core.rotate_rooms_left()
@command_args_parser.ignored
def rotate_rooms_right(self, args=None):
self.core.rotate_rooms_right()
@command_args_parser.quoted(0, 1) @command_args_parser.quoted(0, 1)
def help(self, args): def help(self, args):
""" """
......
...@@ -63,6 +63,7 @@ from poezio import keyboard, xdg ...@@ -63,6 +63,7 @@ from poezio import keyboard, xdg
from poezio.core.completions import CompletionCore from poezio.core.completions import CompletionCore
from poezio.core.tabs import Tabs from poezio.core.tabs import Tabs
from poezio.core.commands import CommandCore from poezio.core.commands import CommandCore
from poezio.core.command_defs import get_commands
from poezio.core.handlers import HandlerCore from poezio.core.handlers import HandlerCore
from poezio.core.structs import ( from poezio.core.structs import (
Command, Command,
...@@ -1053,13 +1054,13 @@ class Core: ...@@ -1053,13 +1054,13 @@ class Core:
### Move actions (e.g. go to next room) ### ### Move actions (e.g. go to next room) ###
def rotate_rooms_right(self, args=None) -> None: def rotate_rooms_right(self) -> None:
""" """
rotate the rooms list to the right rotate the rooms list to the right
""" """
self.tabs.next() self.tabs.next()
def rotate_rooms_left(self, args=None) -> None: def rotate_rooms_left(self) -> None:
""" """
rotate the rooms list to the right rotate the rooms list to the right
""" """
...@@ -1675,323 +1676,8 @@ class Core: ...@@ -1675,323 +1676,8 @@ class Core:
""" """
Register the commands when poezio starts Register the commands when poezio starts
""" """
self.register_command( for command in get_commands(self.command, self.completion, self.plugin_manager):
'help', self.register_command(**command)
self.command.help,
usage='[command]',
shortdesc='\\_o< KOIN KOIN KOIN',
completion=self.completion.help)
self.register_command(
'join',
self.command.join,
usage="[room_name][@server][/nick] [password]",
desc="Join the specified room. You can specify a nickname "
"after a slash (/). If no nickname is specified, you will"
" use the default_nick in the configuration file. You can"
" omit the room name: you will then join the room you\'re"
" looking at (useful if you were kicked). You can also "
"provide a room_name without specifying a server, the "
"server of the room you're currently in will be used. You"
" can also provide a password to join the room.\nExamples"
":\n/join room@server.tld\n/join room@server.tld/John\n"
"/join room2\n/join /me_again\n/join\n/join room@server"
".tld/my_nick password\n/join / password",
shortdesc='Join a room',
completion=self.completion.join)
self.register_command(
'exit',
self.command.quit,
desc='Just disconnect from the server and exit poezio.',
shortdesc='Exit poezio.')
self.register_command(
'quit',
self.command.quit,
desc='Just disconnect from the server and exit poezio.',
shortdesc='Exit poezio.')
self.register_command(
'next', self.rotate_rooms_right, shortdesc='Go to the next room.')
self.register_command(
'prev',
self.rotate_rooms_left,
shortdesc='Go to the previous room.')
self.register_command(
'win',
self.command.win,
usage='<number or name>',
shortdesc='Go to the specified room',
completion=self.completion.win)
self.register_command(
'wup',
self.command.wup,
usage='<prefix>',
shortdesc='Go to the tab whose name uniquely starts with prefix',
completion=self.completion.win)
self.commands['w'] = self.commands['win']
self.register_command(
'move_tab',
self.command.move_tab,
usage='<source> <destination>',
desc="Insert the <source> tab at the position of "
"<destination>. This will make the following tabs shift in"
" some cases (refer to the documentation). A tab can be "
"designated by its number or by the beginning of its "
"address. You can use \".\" as a shortcut for the current "
"tab.",
shortdesc='Move a tab.',
completion=self.completion.move_tab)
self.register_command(
'destroy_room',
self.command.destroy_room,
usage='[room JID]',
desc='Try to destroy the room [room JID], or the current'
' tab if it is a multi-user chat and [room JID] is '
'not given.',
shortdesc='Destroy a room.',
completion=None)
self.register_command(
'show',
self.command.status,
usage='<availability> [status message]',
desc="Sets your availability and (optionally) your status "
"message. The <availability> argument is one of \"available"
", chat, away, afk, dnd, busy, xa\" and the optional "
"[status message] argument will be your status message.",
shortdesc='Change your availability.',
completion=self.completion.status)
self.commands['status'] = self.commands['show']
self.register_command(
'bookmark_local',
self.command.bookmark_local,
usage="[roomname][/nick] [password]",
desc="Bookmark Local: Bookmark locally the specified room "
"(you will then auto-join it on each poezio start). This"
" commands uses almost the same syntaxe as /join. Type "
"/help join for syntax examples. Note that when typing "
"\"/bookmark\" on its own, the room will be bookmarked "
"with the nickname you\'re currently using in this room "
"(instead of default_nick)",
shortdesc='Bookmark a room locally.',
completion=self.completion.bookmark_local)
self.register_command(
'bookmark',
self.command.bookmark,
usage="[roomname][/nick] [autojoin] [password]",
desc="Bookmark: Bookmark online the specified room (you "
"will then auto-join it on each poezio start if autojoin"
" is specified and is 'true'). This commands uses almost"
" the same syntax as /join. Type /help join for syntax "
"examples. Note that when typing \"/bookmark\" alone, the"
" room will be bookmarked with the nickname you\'re "
"currently using in this room (instead of default_nick).",
shortdesc="Bookmark a room online.",
completion=self.completion.bookmark)
self.register_command(
'accept',
self.command.accept,
usage='[jid]',
desc='Allow the provided JID (or the selected contact '
'in your roster), to see your presence.',
shortdesc='Allow a user your presence.',
completion=self.completion.roster_barejids)
self.register_command(
'add',
self.command.add,
usage='<jid>',
desc='Add the specified JID to your roster, ask them to'
' allow you to see his presence, and allow them to'
' see your presence.',
shortdesc='Add a user to your roster.')
self.register_command(
'deny',
self.command.deny,
usage='[jid]',
desc='Deny your presence to the provided JID (or the '
'selected contact in your roster), who is asking'
'you to be in their roster.',
shortdesc='Deny a user your presence.',
completion=self.completion.roster_barejids)
self.register_command(
'remove',
self.command.remove,
usage='[jid]',
desc='Remove the specified JID from your roster. This '
'will unsubscribe you from its presence, cancel '
'its subscription to yours, and remove the item '
'from your roster.',
shortdesc='Remove a user from your roster.',
completion=self.completion.remove)