~tardypad/wee-most

ref: 9d0406e52b5fd04b941c86c8489fd5626304d7ef wee-most/wee_most/commands.py -rw-r--r-- 6.9 KiB
9d0406e5 — Damien Tardy-Panis Move completion functions and setup to new completions file 2 months ago
                                                                                
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import weechat
import wee_most
from collections import namedtuple
from wee_most.globals import config

Command = namedtuple("Command", ["name", "args", "description", "completion"])

commands = [
    Command(
        name = "server add",
        args = "<server-name>",
        description = "add a server",
        completion = "",
    ),
    Command(
        name = "connect",
        args = "<server-name>",
        description = "connect to a server",
        completion = "",
    ),
    Command(
        name = "disconnect",
        args = "<server-name>",
        description = "disconnect from a server",
        completion = "%(mattermost_server_commands)",
    ),
    Command(
        name = "slash",
        args = "<mattermost-command>",
        description = "send a plain slash command",
        completion = "%(mattermost_slash_commands)",
    ),
    Command(
        name = "reply",
        args = "<post-id> <message>",
        description = "reply to a post",
        completion = "",
    ),
    Command(
        name = "react",
        args = "<post-id> <emoji-name>",
        description = "react to a post",
        completion = "",
    ),
    Command(
        name = "unreact",
        args = "<post-id> <emoji-name>",
        description = "remove a reaction to a post",
        completion = "",
    ),
    Command(
        name = "delete",
        args = "<post-id>",
        description = "delete a post",
        completion = "",
    ),
]

def command_server_add(args, buffer):
    if 1 != len(args.split()):
        write_command_error("server add " + args, "Error with subcommand arguments")
        return weechat.WEECHAT_RC_ERROR

    config.add_server_options(args)

    weechat.prnt(buffer, "Server \"%s\" added. You should now configure it." % args)
    weechat.prnt(buffer, "/set plugins.var.python.wee-most.server.%s.*" % args)

    return weechat.WEECHAT_RC_OK

def command_connect(args, buffer):
    if 1 != len(args.split()):
        write_command_error("connect " + args, "Error with subcommand arguments")
        return weechat.WEECHAT_RC_ERROR
    return wee_most.server.connect_server(args)

def command_disconnect(args, buffer):
    if 1 != len(args.split()):
        write_command_error("disconnect " + args, "Error with subcommand arguments")
        return weechat.WEECHAT_RC_ERROR
    return wee_most.server.disconnect_server(args)

def command_server(args, buffer):
    if 0 == len(args.split()):
        write_command_error("server " + args, "Error with subcommand arguments")
        return weechat.WEECHAT_RC_ERROR

    command, _, args = args.partition(" ")

    if command == "add":
        return command_server_add(args, buffer)

    write_command_error("server " + command + " " + args, "Invalid server subcommand")
    return weechat.WEECHAT_RC_ERROR

def command_slash(args, buffer):
    if 0 == len(args.split()):
        write_command_error("slash " + args, "Error with subcommand arguments")
        return weechat.WEECHAT_RC_ERROR

    server = wee_most.server.get_server_from_buffer(buffer)
    channel_id = weechat.buffer_get_string(buffer, "localvar_channel_id")

    wee_most.http.run_post_command(channel_id, "/{}".format(args), server, "singularity_cb", buffer)

    return weechat.WEECHAT_RC_OK

def mattermost_command_cb(data, buffer, command):
    if 0 == len(command.split()):
        write_command_error("", "Missing subcommand")
        return weechat.WEECHAT_RC_ERROR

    prefix, _, args = command.partition(" ")

    if prefix not in [c.name for c in commands]:
        write_command_error(command, "Invalid subcommand")
        return weechat.WEECHAT_RC_ERROR

    return globals()['command_' + prefix](args, buffer)

def command_reply(args, buffer):
    if 2 != len(args.split(' ', 1)):
        write_command_error("reply " + args, "Error with subcommand arguments")
        return weechat.WEECHAT_RC_ERROR

    short_post_id, _, message = args.partition(" ")

    line_data = wee_most.post.find_buffer_last_post_line_data(buffer, short_post_id)
    if not line_data:
        weechat.prnt(buffer, "Can't find post id for \"%s\"" % short_post_id)
        return weechat.WEECHAT_RC_ERROR

    tags = wee_most.post.get_line_data_tags(line_data)

    post_id = wee_most.post.find_reply_to_in_tags(tags)
    if not post_id:
        post_id = wee_most.post.find_post_id_in_tags(tags)

    post = {
        'channel_id': weechat.buffer_get_string(buffer, "localvar_channel_id"),
        'message': message,
        'parent_id': post_id,
    }

    server = wee_most.server.get_server_from_buffer(buffer)

    wee_most.http.run_post_post(post, server, "post_post_cb", buffer)

    return weechat.WEECHAT_RC_OK

def command_react(args, buffer):
    if 2 != len(args.split()):
        write_command_error("react " + args, "Error with subcommand arguments")
        return weechat.WEECHAT_RC_ERROR

    short_post_id, _, emoji_name = args.partition(" ")
    post_id = wee_most.post.find_full_post_id(buffer, short_post_id)
    if not post_id:
        weechat.prnt(buffer, "Can't find post id for \"%s\"" % short_post_id)
        return weechat.WEECHAT_RC_ERROR

    server = wee_most.server.get_server_from_buffer(buffer)
    wee_most.http.run_post_reaction(emoji_name, post_id, server, "singularity_cb", buffer)

    return weechat.WEECHAT_RC_OK

def command_unreact(args, buffer):
    if 2 != len(args.split()):
        write_command_error("unreact " + args, "Error with subcommand arguments")
        return weechat.WEECHAT_RC_ERROR

    short_post_id, _, emoji_name = args.partition(" ")
    post_id = find_full_post_id(buffer, short_post_id)
    if not post_id:
        weechat.prnt(buffer, "Can't find post id for \"%s\"" % short_post_id)
        return weechat.WEECHAT_RC_ERROR

    server = wee_most.server.get_server_from_buffer(buffer)
    wee_most.http.run_delete_reaction(emoji_name, post_id, server, "singularity_cb", buffer)

    return weechat.WEECHAT_RC_OK

def command_delete(args, buffer):
    if 1 != len(args.split()):
        write_command_error("delete " + args, "Error with subcommand arguments")
        return weechat.WEECHAT_RC_ERROR

    post_id = wee_most.post.find_full_post_id(buffer, args)
    if not post_id:
        weechat.prnt(buffer, "Can't find post id for \"%s\"" % args)
        return weechat.WEECHAT_RC_ERROR

    server = wee_most.server.get_server_from_buffer(buffer)

    wee_most.http.run_delete_post(post_id, server, "singularity_cb", buffer)

    return weechat.WEECHAT_RC_OK

def write_command_error(args, message):
    buffer = weechat.buffer_search_main()
    weechat.prnt(buffer, message + " \"/mattermost " + args + "\" (help on command: /help mattermost)")

def setup_commands():
    weechat.hook_command(
        "mattermost",
        "Mattermost commands",
        "||".join([c.name + " " + c.args for c in commands]),
        "\n".join([c.name.rjust(10) + ": " + c.description for c in commands]),
        "||".join([c.name + " " + c.completion for c in commands]),
        "mattermost_command_cb",
        ""
    )

    weechat.hook_focus("chat", "channel_click_cb", "")