summaryrefslogtreecommitdiff
path: root/alot/commandfactory.py
blob: 01a52412ec6a522087972809c8ed360bf1237088 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""
This file is part of alot.

Alot 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.

Alot 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 notmuch.  If not, see <http://www.gnu.org/licenses/>.

Copyright (C) 2011 Patrick Totzke <patricktotzke@gmail.com>
"""
import logging
import os


from settings import get_hook
import commands

COMMANDS = {
        'bufferlist': (commands.OpenBufferListCommand, {}),
        'buffer close': (commands.BufferCloseCommand, {}),
        'buffer next': (commands.BufferFocusCommand, {'offset': 1}),
        'buffer refresh': (commands.RefreshCommand, {}),
        'buffer previous': (commands.BufferFocusCommand, {'offset': -1}),
        'exit': (commands.ExitCommand, {}),
        'flush': (commands.FlushCommand, {}),
        'pyshell': (commands.PythonShellCommand, {}),
        'search': (commands.SearchCommand, {}),
        'shellescape': (commands.ExternalCommand, {}),
        'taglist': (commands.TagListCommand, {}),
        'edit': (commands.EditCommand, {}),
        'commandprompt': (commands.CommandPromptCommand, {}),
        'openthread': (commands.OpenThreadCommand, {}),
        'refine': (commands.RefineSearchPromptCommand, {}),
        'toggletag': (commands.ToggleThreadTagCommand, {'tag': 'inbox'}),
        'buffer focus': (commands.BufferFocusCommand, {}),
        'buffer close focussed': (commands.BufferCloseCommand, {'focussed': True}),

        'compose': (commands.ComposeCommand, {}),
        'open_envelope': (commands.OpenEnvelopeCommand, {}),
        'send': (commands.SendMailCommand, {}),
        'retag': (commands.ThreadTagCommand, {}),
        }


def commandfactory(cmdname, **kwargs):
    if cmdname in COMMANDS:
        (cmdclass, parms) = COMMANDS[cmdname]
        parms = parms.copy()
        parms.update(kwargs)
        for (key, value) in kwargs.items():
            if callable(value):
                parms[key] = value()
            else:
                parms[key] = value

        parms['prehook'] = get_hook('pre_' + cmdname)
        parms['posthook'] = get_hook('post_' + cmdname)

        logging.debug('cmd parms %s' % parms)
        return cmdclass(**parms)
    else:
        logging.error('there is no command %s' % cmdname)


aliases = {'bc': 'buffer close',
           'bn': 'buffer next',
           'bp': 'buffer previous',
           'br': 'buffer refresh',
           'bcf': 'buffer close focussed',
           'refresh': 'buffer refresh',
           'ls': 'bufferlist',
           'quit': 'exit',
}

globalcomands = [
    'buffer close',
    'buffer next',
    'buffer previous',
    'buffer refresh',
    'bufferlist',
    'edit',
    'exit',
    'flush',
    'pyshell',
    'search',
    'shellescape',
    'taglist',
]

ALLOWED_COMMANDS = {
    'search': ['refine', 'toggletag', 'openthread', 'retag'] + globalcomands,
    'envelope': ['send'] + globalcomands,
    'bufferlist': ['buffer focus', 'buffer close focussed'] + globalcomands,
    'taglist': globalcomands,
    'thread': ['toggletag'] + globalcomands,
}

def interpret_commandline(cmdline, mode):
    if not cmdline:
        return None
    logging.debug('mode:%s got commandline "%s"' % (mode, cmdline))
    args = cmdline.strip().split(' ', 1)
    cmd = args[0]
    params = args[1:]

    # unfold aliases
    if cmd in aliases:
        cmd = aliases[cmd]

    # buffer commands depend on first parameter only
    if cmd == 'buffer' and len(params) == 1:
        cmd = cmd + ' ' +params[0]
        params = []
    # allow to shellescape without a space after '!'
    if cmd.startswith('!'):
        params = cmd[1:] + ''.join(params)
        cmd = 'shellescape'

    # check if this command makes sense in current mode
    if cmd not in ALLOWED_COMMANDS[mode]:
        logging.debug('not allowed in mode %s: %s' % (mode,cmd))
        return None

    if not params:  # commands that don't accept parameter
        if cmd in ['exit', 'flush', 'pyshell', 'taglist', 'buffer close',
                   'buffer close focussed', 'buffer next', 'buffer previous',
                   'buffer refresh', 'bufferlist', 'refine', 'openthread',
                   'buffer focus', 'retag']:
            return commandfactory(cmd)
        else:
            return None
    else:
        if cmd == 'search':
            return commandfactory(cmd, query=params[0])
        elif cmd == 'refine':
            return commandfactory(cmd, query=params[0])
        elif cmd == 'shellescape':
            return commandfactory(cmd, commandstring=params)
        elif cmd == 'toggletag':
            return commandfactory(cmd, tag=params[0])
        elif cmd == 'retag':
            return commandfactory(cmd, tagsstring=params[0])
        elif cmd == 'edit':
            filepath = params[0]
            if os.path.isfile(filepath):
                return commandfactory(cmd, path=filepath)
        else:
            return None