Philipp Hörist pushed to branch new-chat-commands at gajim / gajim
Commits:
-
edff9915
by lovetox at 2022-08-15T21:13:22+02:00
8 changed files:
- gajim/common/app.py
- gajim/common/application.py
- + gajim/common/commands.py
- gajim/common/modules/contacts.py
- gajim/gtk/chat_action_processor.py
- gajim/gtk/chat_stack.py
- gajim/gtk/message_actions_box.py
- pyrightconfig.json
Changes:
| ... | ... | @@ -63,6 +63,7 @@ |
| 63 | 63 | from gajim.common.call_manager import CallManager
|
| 64 | 64 | from gajim.common.preview import PreviewManager
|
| 65 | 65 | from gajim.common.task_manager import TaskManager
|
| 66 | + from gajim.common.commands import ChatCommands
|
|
| 66 | 67 | |
| 67 | 68 | |
| 68 | 69 | interface = cast(types.InterfaceT, None)
|
| ... | ... | @@ -77,6 +78,7 @@ |
| 77 | 78 | ipython_window = None
|
| 78 | 79 | app = None # type: GajimApplication
|
| 79 | 80 | window = None # type: MainWindow
|
| 81 | +commands = None # type: ChatCommands
|
|
| 80 | 82 | |
| 81 | 83 | ged = ged_module.GlobalEventsDispatcher() # Global Events Dispatcher
|
| 82 | 84 | plugin_manager = cast(types.PluginManagerT, None) # Plugins Manager
|
| ... | ... | @@ -37,6 +37,7 @@ |
| 37 | 37 | from gajim.common import configpaths
|
| 38 | 38 | from gajim.common import logging_helpers
|
| 39 | 39 | from gajim.common import passwords
|
| 40 | +from gajim.common.commands import ChatCommands
|
|
| 40 | 41 | from gajim.common.dbus import logind
|
| 41 | 42 | from gajim.common.events import AccountDisonnected
|
| 42 | 43 | from gajim.common.events import AllowGajimUpdateCheck
|
| ... | ... | @@ -68,6 +69,7 @@ def _init_core(self) -> None: |
| 68 | 69 | app.settings.init()
|
| 69 | 70 | |
| 70 | 71 | app.config = LegacyConfig()
|
| 72 | + app.commands = ChatCommands()
|
|
| 71 | 73 | |
| 72 | 74 | app.storage.cache = CacheStorage()
|
| 73 | 75 | app.storage.cache.init()
|
| 1 | +# This file is part of Gajim.
|
|
| 2 | +#
|
|
| 3 | +# Gajim is free software; you can redistribute it and/or modify
|
|
| 4 | +# it under the terms of the GNU General Public License as published
|
|
| 5 | +# by the Free Software Foundation; version 3 only.
|
|
| 6 | +#
|
|
| 7 | +# Gajim is distributed in the hope that it will be useful,
|
|
| 8 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
| 9 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
| 10 | +# GNU General Public License for more details.
|
|
| 11 | +#
|
|
| 12 | +# You should have received a copy of the GNU General Public License
|
|
| 13 | +# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
|
| 14 | + |
|
| 15 | +from typing import Any
|
|
| 16 | +from typing import NoReturn
|
|
| 17 | +from typing import Callable
|
|
| 18 | +from typing import Optional
|
|
| 19 | + |
|
| 20 | +import io
|
|
| 21 | +import argparse
|
|
| 22 | +import shlex
|
|
| 23 | +import operator
|
|
| 24 | + |
|
| 25 | +from nbxmpp.protocol import JID
|
|
| 26 | + |
|
| 27 | +from gajim.common import app
|
|
| 28 | +from gajim.common.i18n import _
|
|
| 29 | +from gajim.common.helpers import Observable, is_role_change_allowed
|
|
| 30 | +from gajim.common.helpers import is_affiliation_change_allowed
|
|
| 31 | +from gajim.common.modules.contacts import GroupchatContact
|
|
| 32 | + |
|
| 33 | + |
|
| 34 | +def split_argument_string(string: str) -> list[str]:
|
|
| 35 | + '''
|
|
| 36 | + Split a string with shlex.split
|
|
| 37 | + '''
|
|
| 38 | + |
|
| 39 | + lex = shlex.shlex(string, posix=True)
|
|
| 40 | + lex.whitespace_split = True
|
|
| 41 | + lex.commenters = ''
|
|
| 42 | + result: list[str] = []
|
|
| 43 | + |
|
| 44 | + try:
|
|
| 45 | + for token in lex:
|
|
| 46 | + result.append(token)
|
|
| 47 | + except ValueError:
|
|
| 48 | + # If end-of-string is reached and there is a invalid state
|
|
| 49 | + # ValueError is raised. Still add the partial token to the result.
|
|
| 50 | + result.append(lex.token)
|
|
| 51 | + |
|
| 52 | + return result
|
|
| 53 | + |
|
| 54 | + |
|
| 55 | +def get_usage_from_command(cmd: argparse.ArgumentParser) -> str:
|
|
| 56 | + with io.StringIO() as output:
|
|
| 57 | + cmd.print_usage(file=output)
|
|
| 58 | + usage = output.getvalue()
|
|
| 59 | + |
|
| 60 | + usage = usage.split('[-h] ')[1]
|
|
| 61 | + return usage.strip()
|
|
| 62 | + |
|
| 63 | + |
|
| 64 | +class ArgumentParserError(Exception):
|
|
| 65 | + pass
|
|
| 66 | + |
|
| 67 | + |
|
| 68 | +class CommandError(Exception):
|
|
| 69 | + pass
|
|
| 70 | + |
|
| 71 | + |
|
| 72 | +class CommandNotFoundError(Exception):
|
|
| 73 | + pass
|
|
| 74 | + |
|
| 75 | + |
|
| 76 | +class ArgumentParser(argparse.ArgumentParser):
|
|
| 77 | + def error(self, message: str) -> NoReturn:
|
|
| 78 | + raise ArgumentParserError(message)
|
|
| 79 | + |
|
| 80 | + |
|
| 81 | +class ChatCommands(Observable):
|
|
| 82 | + def __init__(self) -> None:
|
|
| 83 | + Observable.__init__(self)
|
|
| 84 | + self._parser = ArgumentParser(prog='ChatCommands')
|
|
| 85 | + self._sub_parser = self._parser.add_subparsers()
|
|
| 86 | + self._commands: dict[str, tuple[list[str], str]] = {}
|
|
| 87 | + |
|
| 88 | + self._create_commands()
|
|
| 89 | + |
|
| 90 | + def get_commands(self, used_in: str) -> list[tuple[str, str]]:
|
|
| 91 | + commands: list[tuple[str, str]] = []
|
|
| 92 | + for cmd_name, cmd in self._commands.items():
|
|
| 93 | + if used_in in cmd[0]:
|
|
| 94 | + commands.append((cmd_name, cmd[1]))
|
|
| 95 | + commands.sort(key=operator.itemgetter(0))
|
|
| 96 | + return commands
|
|
| 97 | + |
|
| 98 | + def make_parser(self,
|
|
| 99 | + command_name: str,
|
|
| 100 | + callback: Callable[..., Any],
|
|
| 101 | + **kwargs: Any) -> ArgumentParser:
|
|
| 102 | + |
|
| 103 | + '''Add and return a subparser and initialize
|
|
| 104 | + it with the command name.
|
|
| 105 | + '''
|
|
| 106 | + |
|
| 107 | + parser = self._sub_parser.add_parser(command_name, **kwargs)
|
|
| 108 | + parser.set_defaults(command_name=command_name)
|
|
| 109 | + self.connect(command_name, callback)
|
|
| 110 | + return parser
|
|
| 111 | + |
|
| 112 | + def add_command(self,
|
|
| 113 | + command_name: str,
|
|
| 114 | + used_in: list[str],
|
|
| 115 | + cmd: argparse.ArgumentParser
|
|
| 116 | + ) -> None:
|
|
| 117 | + |
|
| 118 | + usage = get_usage_from_command(cmd)
|
|
| 119 | + self._commands[command_name] = (used_in, usage)
|
|
| 120 | + |
|
| 121 | + def parse(self, type_: str, arg_string: str) -> None:
|
|
| 122 | + arg_list = split_argument_string(arg_string[1:])
|
|
| 123 | + |
|
| 124 | + command_name = arg_list[0]
|
|
| 125 | + command = self._commands.get(command_name)
|
|
| 126 | + if command is None or type_ not in command[0]:
|
|
| 127 | + raise CommandNotFoundError(_('Unknown command: %s' % command_name))
|
|
| 128 | + |
|
| 129 | + args = self._parser.parse_args(arg_list)
|
|
| 130 | + |
|
| 131 | + self.notify(args.command_name, args)
|
|
| 132 | + |
|
| 133 | + def _create_commands(self) -> None:
|
|
| 134 | + parser = self.make_parser('status', self._status_command)
|
|
| 135 | + parser.add_argument('status',
|
|
| 136 | + choices=['online', 'away', 'xa', 'dnd'])
|
|
| 137 | + parser.add_argument('message', default=None, nargs='?')
|
|
| 138 | + self.add_command('status', ['chat', 'groupchat', 'pm'], parser)
|
|
| 139 | + |
|
| 140 | + parser = self.make_parser('invite', self._invite_command)
|
|
| 141 | + parser.add_argument('address')
|
|
| 142 | + parser.add_argument('reason', default=None, nargs='?')
|
|
| 143 | + self.add_command('invite', ['groupchat'], parser)
|
|
| 144 | + |
|
| 145 | + parser = self.make_parser('ban', self._ban_command)
|
|
| 146 | + parser.add_argument('who')
|
|
| 147 | + parser.add_argument('reason', default=None, nargs='?')
|
|
| 148 | + self.add_command('ban', ['groupchat'], parser)
|
|
| 149 | + |
|
| 150 | + parser = self.make_parser('affiliate', self._affiliate_command)
|
|
| 151 | + parser.add_argument('who')
|
|
| 152 | + parser.add_argument('affiliation',
|
|
| 153 | + choices=['owner', 'admin', 'member', 'none'])
|
|
| 154 | + self.add_command('affiliate', ['groupchat'], parser)
|
|
| 155 | + |
|
| 156 | + parser = self.make_parser('kick', self._kick_command)
|
|
| 157 | + parser.add_argument('who')
|
|
| 158 | + parser.add_argument('reason', default=None, nargs='?')
|
|
| 159 | + self.add_command('kick', ['groupchat'], parser)
|
|
| 160 | + |
|
| 161 | + parser = self.make_parser('role', self._role_command)
|
|
| 162 | + parser.add_argument('who')
|
|
| 163 | + parser.add_argument('role',
|
|
| 164 | + choices=['moderator', 'participant', 'visitor'])
|
|
| 165 | + self.add_command('role', ['groupchat'], parser)
|
|
| 166 | + |
|
| 167 | + def _status_command(self,
|
|
| 168 | + chat_commands: Any,
|
|
| 169 | + signal_name: str,
|
|
| 170 | + args: Any
|
|
| 171 | + ) -> None:
|
|
| 172 | + |
|
| 173 | + for client in app.get_clients():
|
|
| 174 | + if not app.settings.get_account_setting(client.account,
|
|
| 175 | + 'sync_with_global_status'):
|
|
| 176 | + continue
|
|
| 177 | + |
|
| 178 | + if not client.state.is_available:
|
|
| 179 | + continue
|
|
| 180 | + |
|
| 181 | + client.change_status(args.status,
|
|
| 182 | + args.message or client.status_message)
|
|
| 183 | + |
|
| 184 | + def _check_if_joined(self) -> GroupchatContact:
|
|
| 185 | + contact = app.window.get_control().contact
|
|
| 186 | + assert isinstance(contact, GroupchatContact)
|
|
| 187 | + if not contact.is_joined:
|
|
| 188 | + raise CommandError(_('You are currently not '
|
|
| 189 | + 'joined this group chat'))
|
|
| 190 | + return contact
|
|
| 191 | + |
|
| 192 | + def _invite_command(self,
|
|
| 193 | + chat_commands: Any,
|
|
| 194 | + signal_name: str,
|
|
| 195 | + args: Any
|
|
| 196 | + ) -> None:
|
|
| 197 | + |
|
| 198 | + contact = self._check_if_joined()
|
|
| 199 | + |
|
| 200 | + try:
|
|
| 201 | + jid = JID.from_string(args.address)
|
|
| 202 | + except Exception:
|
|
| 203 | + raise CommandError(_('Invalid address: %s' % args.address))
|
|
| 204 | + |
|
| 205 | + if jid.is_full or jid.localpart is None:
|
|
| 206 | + raise CommandError(_('Invalid address: %s' % args.address))
|
|
| 207 | + |
|
| 208 | + client = app.get_client(contact.account)
|
|
| 209 | + client.get_module('MUC').invite(contact.jid, args.address, args.reason)
|
|
| 210 | + |
|
| 211 | + def _change_affiliation(self,
|
|
| 212 | + nick_or_address: str,
|
|
| 213 | + affiliation: str,
|
|
| 214 | + reason: Optional[str]) -> None:
|
|
| 215 | + |
|
| 216 | + contact = self._check_if_joined()
|
|
| 217 | + |
|
| 218 | + nick_list = contact.get_user_nicknames()
|
|
| 219 | + if nick_or_address in nick_list:
|
|
| 220 | + participant = contact.get_resource(nick_or_address)
|
|
| 221 | + self_contact = contact.get_self()
|
|
| 222 | + assert self_contact is not None
|
|
| 223 | + if not is_affiliation_change_allowed(self_contact,
|
|
| 224 | + participant,
|
|
| 225 | + 'outcast'):
|
|
| 226 | + raise CommandError(_('You have insufficient permissions'))
|
|
| 227 | + |
|
| 228 | + jid = participant.real_jid
|
|
| 229 | + |
|
| 230 | + else:
|
|
| 231 | + try:
|
|
| 232 | + jid = JID.from_string(nick_or_address)
|
|
| 233 | + except Exception:
|
|
| 234 | + raise CommandError(_('Invalid address: %s' % nick_or_address))
|
|
| 235 | + |
|
| 236 | + if jid.is_full or jid.localpart is None:
|
|
| 237 | + raise CommandError(_('Invalid address: %s' % nick_or_address))
|
|
| 238 | + |
|
| 239 | + client = app.get_client(contact.account)
|
|
| 240 | + client.get_module('MUC').set_affiliation(
|
|
| 241 | + contact.jid,
|
|
| 242 | + {jid: {'affiliation': affiliation,
|
|
| 243 | + 'reason': reason}})
|
|
| 244 | + |
|
| 245 | + def _ban_command(self,
|
|
| 246 | + chat_commands: Any,
|
|
| 247 | + signal_name: str,
|
|
| 248 | + args: Any
|
|
| 249 | + ) -> None:
|
|
| 250 | + |
|
| 251 | + self._change_affiliation(args.who, 'outcast', args.reason)
|
|
| 252 | + |
|
| 253 | + def _affiliate_command(self,
|
|
| 254 | + chat_commands: Any,
|
|
| 255 | + signal_name: str,
|
|
| 256 | + args: Any
|
|
| 257 | + ) -> None:
|
|
| 258 | + |
|
| 259 | + self._change_affiliation(args.who, args.affiliation, None)
|
|
| 260 | + |
|
| 261 | + def _change_role(self, nick: str, role: str, reason: Optional[str]) -> None:
|
|
| 262 | + |
|
| 263 | + contact = self._check_if_joined()
|
|
| 264 | + |
|
| 265 | + nick_list = contact.get_user_nicknames()
|
|
| 266 | + if nick not in nick_list:
|
|
| 267 | + raise CommandError(_('User %s not found' % nick))
|
|
| 268 | + |
|
| 269 | + participant = contact.get_resource(nick)
|
|
| 270 | + self_contact = contact.get_self()
|
|
| 271 | + assert self_contact is not None
|
|
| 272 | + if not is_role_change_allowed(self_contact, participant):
|
|
| 273 | + raise CommandError(_('You have insufficient permissions'))
|
|
| 274 | + |
|
| 275 | + client = app.get_client(contact.account)
|
|
| 276 | + client.get_module('MUC').set_role(contact.jid,
|
|
| 277 | + nick,
|
|
| 278 | + role,
|
|
| 279 | + reason)
|
|
| 280 | + |
|
| 281 | + def _kick_command(self,
|
|
| 282 | + chat_commands: Any,
|
|
| 283 | + signal_name: str,
|
|
| 284 | + args: Any
|
|
| 285 | + ) -> None:
|
|
| 286 | + |
|
| 287 | + self._change_role(args.who, 'none', args.reason)
|
|
| 288 | + |
|
| 289 | + def _role_command(self,
|
|
| 290 | + chat_commands: Any,
|
|
| 291 | + signal_name: str,
|
|
| 292 | + args: Any
|
|
| 293 | + ) -> None:
|
|
| 294 | + |
|
| 295 | + self._change_role(args.who, args.role, None) |
| ... | ... | @@ -308,6 +308,10 @@ def _supports(jid: JID, requested_feature: str) -> bool: |
| 308 | 308 | |
| 309 | 309 | return disco_info.supports(requested_feature)
|
| 310 | 310 | |
| 311 | + @property
|
|
| 312 | + def is_chat(self) -> bool:
|
|
| 313 | + return False
|
|
| 314 | + |
|
| 311 | 315 | @property
|
| 312 | 316 | def is_groupchat(self) -> bool:
|
| 313 | 317 | return False
|
| ... | ... | @@ -316,6 +320,10 @@ def is_groupchat(self) -> bool: |
| 316 | 320 | def is_pm_contact(self) -> bool:
|
| 317 | 321 | return False
|
| 318 | 322 | |
| 323 | + @property
|
|
| 324 | + def type_string(self) -> str:
|
|
| 325 | + raise NotImplementedError
|
|
| 326 | + |
|
| 319 | 327 | @property
|
| 320 | 328 | def is_jingle_available(self) -> bool:
|
| 321 | 329 | return False
|
| ... | ... | @@ -558,6 +566,14 @@ def is_jingle_available(self) -> bool: |
| 558 | 566 | return False
|
| 559 | 567 | return self.is_available
|
| 560 | 568 | |
| 569 | + @property
|
|
| 570 | + def is_chat(self) -> bool:
|
|
| 571 | + return True
|
|
| 572 | + |
|
| 573 | + @property
|
|
| 574 | + def type_string(self) -> str:
|
|
| 575 | + return 'chat'
|
|
| 576 | + |
|
| 561 | 577 | |
| 562 | 578 | class ResourceContact(CommonContact):
|
| 563 | 579 | def __init__(self, logger: LogAdapter, jid: JID, account: str) -> None:
|
| ... | ... | @@ -632,6 +648,10 @@ def update_presence(self, |
| 632 | 648 | if notify:
|
| 633 | 649 | self.notify('presence-update')
|
| 634 | 650 | |
| 651 | + @property
|
|
| 652 | + def type_string(self) -> str:
|
|
| 653 | + raise NotImplementedError
|
|
| 654 | + |
|
| 635 | 655 | |
| 636 | 656 | class GroupchatContact(CommonContact):
|
| 637 | 657 | def __init__(self, logger: LogAdapter, jid: JID, account: str) -> None:
|
| ... | ... | @@ -791,6 +811,10 @@ def can_notify(self) -> bool: |
| 791 | 811 | room = self.settings.get('notify_on_all_messages')
|
| 792 | 812 | return all_ or room
|
| 793 | 813 | |
| 814 | + @property
|
|
| 815 | + def type_string(self) -> str:
|
|
| 816 | + return 'groupchat'
|
|
| 817 | + |
|
| 794 | 818 | |
| 795 | 819 | class GroupchatParticipant(CommonContact):
|
| 796 | 820 | def __init__(self, logger: LogAdapter, jid: JID, account: str) -> None:
|
| ... | ... | @@ -936,6 +960,10 @@ def update_avatar(self, *args: Any) -> None: |
| 936 | 960 | app.app.avatar_storage.invalidate_cache(self._jid)
|
| 937 | 961 | self.notify('user-avatar-update')
|
| 938 | 962 | |
| 963 | + @property
|
|
| 964 | + def type_string(self) -> str:
|
|
| 965 | + return 'pm'
|
|
| 966 | + |
|
| 939 | 967 | |
| 940 | 968 | def can_add_to_roster(contact: Union[BareContact,
|
| 941 | 969 | GroupchatContact,
|
| ... | ... | @@ -121,17 +121,9 @@ def _replace_text(self, selected_action: str) -> None: |
| 121 | 121 | self._buf.delete(start_iter, self._current_iter)
|
| 122 | 122 | self._buf.insert(start_iter, selected_action)
|
| 123 | 123 | |
| 124 | - def _get_commands(self) -> list[str]:
|
|
| 125 | - # TODO
|
|
| 126 | - # commands: list[str] = []
|
|
| 127 | - # assert self._account
|
|
| 128 | - # assert self._contact
|
|
| 129 | - # control = app.window.get_control()
|
|
| 130 | - # for command in control.list_commands():
|
|
| 131 | - # for name in command.names:
|
|
| 132 | - # commands.append(name)
|
|
| 133 | - # return commands
|
|
| 134 | - return []
|
|
| 124 | + def _get_commands(self) -> list[tuple[str, str]]:
|
|
| 125 | + assert self._contact is not None
|
|
| 126 | + return app.commands.get_commands(self._contact.type_string)
|
|
| 135 | 127 | |
| 136 | 128 | def _on_changed(self, _textview: MessageInputTextView) -> None:
|
| 137 | 129 | insert = self._buf.get_insert()
|
| ... | ... | @@ -181,7 +173,7 @@ def _update_commands_menu(self, |
| 181 | 173 | self._menu.remove_all()
|
| 182 | 174 | command_list = self._get_commands()
|
| 183 | 175 | num_entries = 0
|
| 184 | - for command in command_list:
|
|
| 176 | + for command, usage in command_list:
|
|
| 185 | 177 | if not command.startswith(action_text[1:]):
|
| 186 | 178 | continue
|
| 187 | 179 | if num_entries >= MAX_ENTRIES:
|
| ... | ... | @@ -189,7 +181,7 @@ def _update_commands_menu(self, |
| 189 | 181 | |
| 190 | 182 | action_data = GLib.Variant('s', f'/{command}')
|
| 191 | 183 | menu_item = Gio.MenuItem()
|
| 192 | - menu_item.set_label(f'/{command}')
|
|
| 184 | + menu_item.set_label(f'/{command} {usage}')
|
|
| 193 | 185 | menu_item.set_attribute_value('action-data', action_data)
|
| 194 | 186 | self._menu.append_item(menu_item)
|
| 195 | 187 | num_entries += 1
|
| ... | ... | @@ -76,6 +76,10 @@ def __init__(self): |
| 76 | 76 | self._chat_banner = ChatBanner()
|
| 77 | 77 | self._chat_control = ChatControl()
|
| 78 | 78 | self._message_action_box = MessageActionsBox()
|
| 79 | + self._message_action_box.connect('command-error',
|
|
| 80 | + self._on_command_error)
|
|
| 81 | + self._message_action_box.connect('command-not-found',
|
|
| 82 | + self._on_command_not_found)
|
|
| 79 | 83 | |
| 80 | 84 | box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
|
| 81 | 85 | box.add(self._chat_banner)
|
| ... | ... | @@ -659,6 +663,9 @@ def _on_function_message(self, |
| 659 | 663 | def _on_send_message(self) -> None:
|
| 660 | 664 | self._message_action_box.msg_textview.replace_emojis()
|
| 661 | 665 | message = self._message_action_box.msg_textview.get_text()
|
| 666 | + if message.startswith('//'):
|
|
| 667 | + # Escape sequence for chat commands
|
|
| 668 | + message = message[1:]
|
|
| 662 | 669 | |
| 663 | 670 | contact = self._current_contact
|
| 664 | 671 | assert contact is not None
|
| ... | ... | @@ -683,9 +690,6 @@ def _on_send_message(self) -> None: |
| 683 | 690 | if message in ('', None, '\n'):
|
| 684 | 691 | return
|
| 685 | 692 | |
| 686 | - # if process_commands and self.process_as_command(message):
|
|
| 687 | - # return
|
|
| 688 | - |
|
| 689 | 693 | label = self._message_action_box.get_seclabel()
|
| 690 | 694 | |
| 691 | 695 | correct_id = None
|
| ... | ... | @@ -734,6 +738,19 @@ def clear(self) -> None: |
| 734 | 738 | self._message_action_box.clear()
|
| 735 | 739 | self._chat_control.clear()
|
| 736 | 740 | |
| 741 | + def _on_command_error(self,
|
|
| 742 | + message_actions_box: MessageActionsBox,
|
|
| 743 | + error: str) -> None:
|
|
| 744 | + |
|
| 745 | + self._chat_control.add_info_message(error)
|
|
| 746 | + |
|
| 747 | + def _on_command_not_found(self,
|
|
| 748 | + message_actions_box: MessageActionsBox,
|
|
| 749 | + error: str
|
|
| 750 | + ) -> None:
|
|
| 751 | + |
|
| 752 | + self._chat_control.add_info_message(error)
|
|
| 753 | + |
|
| 737 | 754 | |
| 738 | 755 | class ChatPlaceholderBox(Gtk.Box):
|
| 739 | 756 | def __init__(self):
|
| ... | ... | @@ -38,6 +38,9 @@ |
| 38 | 38 | from gajim.common import events
|
| 39 | 39 | from gajim.common import i18n
|
| 40 | 40 | from gajim.common import ged
|
| 41 | +from gajim.common.commands import ArgumentParserError
|
|
| 42 | +from gajim.common.commands import CommandError
|
|
| 43 | +from gajim.common.commands import CommandNotFoundError
|
|
| 41 | 44 | from gajim.common.i18n import _
|
| 42 | 45 | from gajim.common.client import Client
|
| 43 | 46 | from gajim.common.const import SimpleClientState
|
| ... | ... | @@ -67,6 +70,12 @@ |
| 67 | 70 | |
| 68 | 71 | |
| 69 | 72 | class MessageActionsBox(Gtk.Grid, ged.EventHelper):
|
| 73 | + |
|
| 74 | + __gsignals__ = {
|
|
| 75 | + 'command-error': (GObject.SignalFlags.RUN_FIRST, None, (str,)),
|
|
| 76 | + 'command-not-found': (GObject.SignalFlags.RUN_FIRST, None, (str,))
|
|
| 77 | + }
|
|
| 78 | + |
|
| 70 | 79 | def __init__(self) -> None:
|
| 71 | 80 | Gtk.Grid.__init__(self)
|
| 72 | 81 | ged.EventHelper.__init__(self)
|
| ... | ... | @@ -595,6 +604,21 @@ def _on_msg_textview_key_press_event(self, |
| 595 | 604 | return True
|
| 596 | 605 | |
| 597 | 606 | assert self._contact is not None
|
| 607 | + |
|
| 608 | + message = self.msg_textview.get_text()
|
|
| 609 | + if (message.startswith('/') and
|
|
| 610 | + not message.startswith('//') and
|
|
| 611 | + not message.startswith('/me ')):
|
|
| 612 | + try:
|
|
| 613 | + app.commands.parse(self._contact.type_string, message)
|
|
| 614 | + except (ArgumentParserError, CommandError) as error:
|
|
| 615 | + self.emit('command-error', error)
|
|
| 616 | + except CommandNotFoundError as error:
|
|
| 617 | + self.emit('command-not-found', error)
|
|
| 618 | + else:
|
|
| 619 | + self.msg_textview.clear()
|
|
| 620 | + return True
|
|
| 621 | + |
|
| 598 | 622 | if not app.account_is_available(self._contact.account):
|
| 599 | 623 | # we are not connected
|
| 600 | 624 | ErrorDialog(
|
| ... | ... | @@ -22,6 +22,7 @@ |
| 22 | 22 | "gajim/common/application.py",
|
| 23 | 23 | "gajim/common/call_manager.py",
|
| 24 | 24 | "gajim/common/cert_store.py",
|
| 25 | + "gajim/common/commands.py",
|
|
| 25 | 26 | "gajim/common/configpaths.py",
|
| 26 | 27 | "gajim/common/const.py",
|
| 27 | 28 | "gajim/common/events.py",
|