2010-08-06 05:07:28 +02:00
|
|
|
# Copyright (c) 2009-2010, Alexander Cherniuk (ts33kr@gmail.com)
|
|
|
|
# All rights reserved.
|
2009-09-12 15:51:21 +02:00
|
|
|
#
|
2010-08-06 05:07:28 +02:00
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions
|
|
|
|
# are met:
|
2009-09-12 15:51:21 +02:00
|
|
|
#
|
2010-08-06 05:07:28 +02:00
|
|
|
# * Redistributions of source code must retain the above copyright
|
|
|
|
# notice, this list of conditions and the following disclaimer.
|
2009-09-12 15:51:21 +02:00
|
|
|
#
|
2010-08-06 05:07:28 +02:00
|
|
|
# * Redistributions in binary form must reproduce the above copyright
|
|
|
|
# notice, this list of conditions and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2009-09-12 15:51:21 +02:00
|
|
|
|
|
|
|
"""
|
2010-02-26 11:35:09 +01:00
|
|
|
Provides a glue to tie command system framework and the actual code
|
|
|
|
where it would be dropped in. Defines a little bit of scaffolding to
|
|
|
|
support interaction between the two and a few utility methods so you
|
2010-08-06 05:07:28 +02:00
|
|
|
don't need to dig up the code itself to write basic commands.
|
2009-09-12 15:51:21 +02:00
|
|
|
"""
|
2009-09-13 16:43:44 +02:00
|
|
|
|
2009-09-12 15:51:21 +02:00
|
|
|
from traceback import print_exc
|
|
|
|
|
2012-12-23 16:23:43 +01:00
|
|
|
from gi.repository import Pango
|
2009-10-02 22:57:11 +02:00
|
|
|
from common import gajim
|
|
|
|
|
|
|
|
from ..framework import CommandProcessor
|
2010-03-18 06:37:49 +01:00
|
|
|
from ..errors import CommandError, NoCommandError
|
2009-10-02 22:57:11 +02:00
|
|
|
|
|
|
|
class ChatCommandProcessor(CommandProcessor):
|
2009-09-12 15:51:21 +02:00
|
|
|
"""
|
2010-02-26 11:35:09 +01:00
|
|
|
A basic scaffolding to provide convenient interaction between the
|
2010-08-06 05:07:28 +02:00
|
|
|
command system and chat controls. It will be merged directly into
|
|
|
|
the controls, by ChatCommandProcessor being among superclasses of
|
|
|
|
the controls.
|
2009-09-12 15:51:21 +02:00
|
|
|
"""
|
|
|
|
|
2009-10-02 22:57:11 +02:00
|
|
|
def process_as_command(self, text):
|
2010-04-19 16:26:59 +02:00
|
|
|
self.command_succeeded = False
|
2010-08-06 05:07:28 +02:00
|
|
|
parents = super(ChatCommandProcessor, self)
|
|
|
|
flag = parents.process_as_command(text)
|
2010-04-19 16:26:59 +02:00
|
|
|
if flag and self.command_succeeded:
|
2009-10-02 22:57:11 +02:00
|
|
|
self.add_history(text)
|
|
|
|
self.clear_input()
|
|
|
|
return flag
|
|
|
|
|
|
|
|
def execute_command(self, name, arguments):
|
2009-09-12 15:51:21 +02:00
|
|
|
try:
|
2010-08-06 05:07:28 +02:00
|
|
|
parents = super(ChatCommandProcessor, self)
|
|
|
|
parents.execute_command(name, arguments)
|
2013-01-01 23:18:36 +01:00
|
|
|
except NoCommandError as error:
|
2010-03-18 06:37:49 +01:00
|
|
|
details = dict(name=error.name, message=error.message)
|
|
|
|
message = "%(name)s: %(message)s\n" % details
|
|
|
|
message += "Try using the //%(name)s or /say /%(name)s " % details
|
|
|
|
message += "construct if you intended to send it as a text."
|
2010-08-06 05:07:28 +02:00
|
|
|
self.echo_error(message)
|
2013-01-01 23:18:36 +01:00
|
|
|
except CommandError as error:
|
2010-08-06 05:07:28 +02:00
|
|
|
self.echo_error("%s: %s" % (error.name, error.message))
|
2009-09-12 15:51:21 +02:00
|
|
|
except Exception:
|
2012-08-11 12:59:05 +02:00
|
|
|
self.echo_error(_("Error during command execution!"))
|
2009-09-12 15:51:21 +02:00
|
|
|
print_exc()
|
2010-04-17 20:53:09 +02:00
|
|
|
else:
|
2010-04-19 16:26:59 +02:00
|
|
|
self.command_succeeded = True
|
2009-09-12 15:51:21 +02:00
|
|
|
|
2009-10-02 22:57:11 +02:00
|
|
|
def looks_like_command(self, text, body, name, arguments):
|
2017-03-07 16:36:43 +01:00
|
|
|
# Command escape stuff goes here. If text was prepended by the
|
2010-03-18 06:37:49 +01:00
|
|
|
# command prefix twice, like //not_a_command (if prefix is set
|
|
|
|
# to /) then it will be escaped, that is sent just as a regular
|
|
|
|
# message with one (only one) prefix removed, so message will be
|
|
|
|
# /not_a_command.
|
2009-10-02 22:57:11 +02:00
|
|
|
if body.startswith(self.COMMAND_PREFIX):
|
|
|
|
self.send(body)
|
2009-09-12 15:51:21 +02:00
|
|
|
return True
|
|
|
|
|
2009-10-02 22:57:11 +02:00
|
|
|
def command_preprocessor(self, command, name, arguments, args, kwargs):
|
2010-03-18 06:37:49 +01:00
|
|
|
# If command argument contain h or help option - forward it to
|
|
|
|
# the /help command. Dont forget to pass self, as all commands
|
|
|
|
# are unbound. And also don't forget to print output.
|
2009-09-12 15:51:21 +02:00
|
|
|
if 'h' in kwargs or 'help' in kwargs:
|
2009-10-02 22:57:11 +02:00
|
|
|
help = self.get_command('help')
|
|
|
|
self.echo(help(self, name))
|
2009-09-12 15:51:21 +02:00
|
|
|
return True
|
|
|
|
|
2009-10-02 22:57:11 +02:00
|
|
|
def command_postprocessor(self, command, name, arguments, args, kwargs, value):
|
2010-03-18 06:37:49 +01:00
|
|
|
# If command returns a string - print it to a user. A convenient
|
|
|
|
# and sufficient in most simple cases shortcut to a using echo.
|
2013-01-02 13:54:02 +01:00
|
|
|
if value and isinstance(value, str):
|
2009-09-12 15:51:21 +02:00
|
|
|
self.echo(value)
|
|
|
|
|
2009-10-02 22:57:11 +02:00
|
|
|
class CommandTools:
|
|
|
|
"""
|
2010-02-26 11:35:09 +01:00
|
|
|
Contains a set of basic tools and shortcuts you can use in your
|
2010-08-06 05:07:28 +02:00
|
|
|
commands to perform some simple operations. These will be merged
|
|
|
|
directly into the controls, by CommandTools being among superclasses
|
|
|
|
of the controls.
|
2009-10-02 22:57:11 +02:00
|
|
|
"""
|
|
|
|
|
2010-08-06 05:07:28 +02:00
|
|
|
def __init__(self):
|
|
|
|
self.install_tags()
|
|
|
|
|
|
|
|
def install_tags(self):
|
|
|
|
buffer = self.conv_textview.tv.get_buffer()
|
|
|
|
|
2015-10-04 21:22:17 +02:00
|
|
|
name = "Monospace"
|
2012-12-23 16:23:43 +01:00
|
|
|
font = Pango.FontDescription(name)
|
2010-08-06 05:07:28 +02:00
|
|
|
|
|
|
|
command_ok_tag = buffer.create_tag("command_ok")
|
|
|
|
command_ok_tag.set_property("font-desc", font)
|
|
|
|
command_ok_tag.set_property("foreground", "#3465A4")
|
|
|
|
|
|
|
|
command_error_tag = buffer.create_tag("command_error")
|
|
|
|
command_error_tag.set_property("font-desc", font)
|
|
|
|
command_error_tag.set_property("foreground", "#F57900")
|
|
|
|
|
|
|
|
def shift_line(self):
|
|
|
|
buffer = self.conv_textview.tv.get_buffer()
|
|
|
|
iter = buffer.get_end_iter()
|
|
|
|
if iter.ends_line() and not iter.is_start():
|
|
|
|
buffer.insert_with_tags_by_name(iter, "\n", "eol")
|
|
|
|
|
|
|
|
def append_with_tags(self, text, *tags):
|
|
|
|
buffer = self.conv_textview.tv.get_buffer()
|
|
|
|
iter = buffer.get_end_iter()
|
|
|
|
buffer.insert_with_tags_by_name(iter, text, *tags)
|
|
|
|
|
|
|
|
def echo(self, text, tag="command_ok"):
|
|
|
|
"""
|
|
|
|
Print given text to the user, as a regular command output.
|
|
|
|
"""
|
|
|
|
self.shift_line()
|
|
|
|
self.append_with_tags(text, tag)
|
|
|
|
|
|
|
|
def echo_error(self, text):
|
2009-09-12 15:51:21 +02:00
|
|
|
"""
|
2010-08-06 05:07:28 +02:00
|
|
|
Print given text to the user, as an error command output.
|
2009-09-12 15:51:21 +02:00
|
|
|
"""
|
2010-08-06 05:07:28 +02:00
|
|
|
self.echo(text, "command_error")
|
2009-09-12 15:51:21 +02:00
|
|
|
|
|
|
|
def send(self, text):
|
|
|
|
"""
|
|
|
|
Send a message to the contact.
|
|
|
|
"""
|
|
|
|
self.send_message(text, process_commands=False)
|
|
|
|
|
|
|
|
def set_input(self, text):
|
|
|
|
"""
|
|
|
|
Set given text into the input.
|
|
|
|
"""
|
2009-10-02 22:57:11 +02:00
|
|
|
buffer = self.msg_textview.get_buffer()
|
|
|
|
buffer.set_text(text)
|
2009-09-12 15:51:21 +02:00
|
|
|
|
|
|
|
def clear_input(self):
|
|
|
|
"""
|
|
|
|
Clear input.
|
|
|
|
"""
|
|
|
|
self.set_input(str())
|
|
|
|
|
|
|
|
def add_history(self, text):
|
|
|
|
"""
|
2010-02-26 11:35:09 +01:00
|
|
|
Add given text to the input history, so user can scroll through
|
|
|
|
it using ctrl + up/down arrow keys.
|
2009-09-12 15:51:21 +02:00
|
|
|
"""
|
2011-03-10 22:35:26 +01:00
|
|
|
self.save_message(text, 'sent')
|
2009-09-13 16:43:44 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def connection(self):
|
|
|
|
"""
|
|
|
|
Get the current connection object.
|
|
|
|
"""
|
2010-08-07 16:26:12 +02:00
|
|
|
return gajim.connections[self.account]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def full_jid(self):
|
|
|
|
"""
|
|
|
|
Get a full JID of the contact.
|
|
|
|
"""
|
2011-03-10 22:35:26 +01:00
|
|
|
return self.contact.get_full_jid()
|