Welcome to mirror list, hosted at ThFree Co, Russian Federation.

middleware.py « implementation « command_system « src - dev.gajim.org/gajim/gajim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9ef4bea298572cd2fe17a67df396a7e843c86c86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Copyright (C) 2009  red-agent <hell.director@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""
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 don't need to dig up the code
itself code to write basic commands.
"""

from types import StringTypes
from traceback import print_exc

from common import gajim

from ..framework import CommandProcessor
from ..errors import CommandError

class ChatCommandProcessor(CommandProcessor):
    """
    A basic scaffolding to provide convenient interaction between the command
    system and chat controls.
    """

    def process_as_command(self, text):
        flag = super(ChatCommandProcessor, self).process_as_command(text)
        if flag:
            self.add_history(text)
            self.clear_input()
        return flag

    def execute_command(self, name, arguments):
        try:
            super(ChatCommandProcessor, self).execute_command(name, arguments)
        except CommandError, error:
            self.echo("%s: %s" %(error.name, error.message), 'error')
        except Exception:
            self.echo("An error occured while trying to execute the command", 'error')
            print_exc()

    def looks_like_command(self, text, body, name, arguments):
        # Command escape stuff ggoes here. If text was prepended by the 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.
        if body.startswith(self.COMMAND_PREFIX):
            self.send(body)
            return True

    def command_preprocessor(self, command, name, arguments, args, kwargs):
        # 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.
        if 'h' in kwargs or 'help' in kwargs:
            help = self.get_command('help')
            self.echo(help(self, name))
            return True

    def command_postprocessor(self, command, name, arguments, args, kwargs, value):
        # If command returns a string - print it to a user. A convenient and
        # sufficient in most simple cases shortcut to a using echo.
        if value and isinstance(value, StringTypes):
            self.echo(value)

class CommandTools:
    """
    Contains a set of basic tools and shortcuts you can use in your commands to
    performe some simple operations.
    """

    def echo(self, text, kind='info'):
        """
        Print given text to the user.
        """
        self.print_conversation(str(text), kind)

    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.
        """
        buffer = self.msg_textview.get_buffer()
        buffer.set_text(text)

    def clear_input(self):
        """
        Clear input.
        """
        self.set_input(str())

    def add_history(self, text):
        """
        Add given text to the input history, so user can scroll through it using
        ctrl + up/down arrow keys.
        """
        self.save_sent_message(text)

    @property
    def connection(self):
        """
        Get the current connection object.
        """
        return gajim.connections[self.account]