~reedwade/wee-matter

ref: adfdd6516b39eb05eb19a92a2d9bbbf244444683 wee-matter/wee_matter/commands.py -rw-r--r-- 7.4 KiB
adfdd651Reed Wade fix a bug when download file fail 9 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
216
217
218
219
220
221
222
223
224
225
226
import weechat
import wee_matter

server_default_config = {
    "username": "",
    "password": "",
    "port": "443",
    "protocol": "https",
}

def setup_server_config(server_name, key, value, override=False):
    config_key = "server." + server_name + "." + key

    if not weechat.config_is_set_plugin(config_key) or override:
        weechat.config_set_plugin(
            config_key,
            value
        )

def server_add_command_usage(buffer):
    weechat.prnt(buffer, "Usage: /matter server add <server-name> <server-domain>")

def server_add_command(args, buffer):
    if 2 != len(args.split()):
        server_add_command_usage(buffer)
        return weechat.WEECHAT_RC_ERROR

    server_name, _, server_url = args.partition(" ")

    for config, default_value in server_default_config.items():
        setup_server_config(server_name, config, default_value)

    setup_server_config(server_name, "address", server_url)

    weechat.prnt(buffer, "Server added. You should now configure your username and password.")
    weechat.prnt(buffer, "/set plugins.var.python.wee-matter.server.%s.*" % server_name)

    return weechat.WEECHAT_RC_OK

def connect_command_usage(buffer):
    weechat.prnt(buffer, "Usage: /matter connect <server-name>")

def connect_command(args, buffer):
    if 1 != len(args.split()):
        connect_command_usage(buffer)
        return weechat.WEECHAT_RC_ERROR
    return wee_matter.server.connect_server(args)

def disconnect_command_usage(buffer):
    weechat.prnt(buffer, "Usage: /matter connect <server-name>")

def disconnect_command(args, buffer):
    if 1 != len(args.split()):
        disconnect_command_usage(buffer)
        return weechat.WEECHAT_RC_ERROR
    return wee_matter.server.disconnect_server(args)

def matter_server_command_usage(buffer):
    weechat.prnt(buffer,
        (
            "Usage: /matter server add <server-name> <server-domain>"
        )
    )

def server_command(args, buffer):
    if 0 == len(args.split()):
        matter_server_command_usage(buffer)
        return weechat.WEECHAT_RC_ERROR

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

    if command == "add":
        server_add_command(args, buffer)

    return weechat.WEECHAT_RC_OK

def matter_command_usage(buffer):
    weechat.prnt(buffer,
        (
            "Usage: \n"
            "    /matter server add <server-name> <server-domain>\n"
            "    /matter connect <server-name>\n"
            "    /matter disconnect <server-name>\n"
        )
    )

def matter_command_cb(data, buffer, args):
    if 0 == len(args.split()):
        matter_command_usage(buffer)
        return weechat.WEECHAT_RC_ERROR

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

    if command == "server":
        server_command(args, buffer)
    if command == "connect":
        connect_command(args, buffer)
    if command == "disconnect":
        disconnect_command(args, buffer)

    return weechat.WEECHAT_RC_OK

def reply_command_usage(buffer):
    weechat.prnt(buffer, "Usage: /reply <post-id> <message>")

def reply_command_cb(data, buffer, args):
    if 2 != len(args.split(' ', 1)):
        reply_command_usage(buffer)
        return weechat.WEECHAT_RC_ERROR

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

    line_data = wee_matter.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_matter.post.get_line_data_tags(line_data)

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

    post = wee_matter.post.build_post_from_input_data(buffer, message)
    post = post._replace(parent_id=post_id)

    server = wee_matter.server.get_server_from_buffer(buffer)

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

    return weechat.WEECHAT_RC_OK

def react_command_usage(buffer):
    weechat.prnt(buffer, "Usage: /react <post-id> <emoji-name>")

def react_command_cb(data, buffer, args):
    if 2 != len(args.split()):
        react_command_usage(buffer)
        return weechat.WEECHAT_RC_ERROR

    short_post_id, _, emoji_name = args.partition(" ")
    post_id = wee_matter.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_matter.server.get_server_from_buffer(buffer)
    wee_matter.http.run_post_reaction(emoji_name, post_id, server, "singularity_cb", buffer)

    return weechat.WEECHAT_RC_OK

def unreact_command_usage(buffer):
    weechat.prnt(buffer, "Usage: /unreact <post-id> <emoji-name>")

def unreact_command_cb(data, buffer, args):
    if 2 != len(args.split()):
        unreact_command_usage(buffer)
        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_matter.server.get_server_from_buffer(buffer)
    wee_matter.http.run_delete_reaction(emoji_name, post_id, server, "singularity_cb", buffer)

    return weechat.WEECHAT_RC_OK

def delete_post_command_cb_usage(buffer):
    weechat.prnt(buffer, "Usage: /delete <post-id>")

def delete_post_command_cb(data, buffer, args):
    if 1 != len(args.split()):
        delete_post_command_usage(buffer)
        return weechat.WEECHAT_RC_ERROR

    post_id = wee_matter.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_matter.server.get_server_from_buffer(buffer)

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

    return weechat.WEECHAT_RC_OK

def setup_commands():
    weechat.hook_command(
        "matter",
        "Mattermost chat protocol command",
         # Synopsis
        (
            "server add <server-name> <hostname> ||"
            "connect <server-name> ||"
            "disconnect <server-name> ||"
        ),
        # Description
        (
            "server: add Matrix servers\n"
            "connect Matrix servers\n"
            "disconnect Matrix servers\n"
        ),
        # Completions
        (
            "server add ||"
            "connect ||"
            "disconnect %(mattermost_server_commands) ||"
        ),
        "matter_command_cb",
        ""
    )

    weechat.hook_command("reply", "Reply to a post", "<post-id> <message>", "Reply to a post", "", "reply_command_cb", "")
    weechat.hook_command("react", "React to a post", "<post-id> <emoji-name>", "React to a post", "", "react_command_cb", "")
    weechat.hook_command("unreact", "Unreact to a post", "<post-id> <emoji-name>", "Unreact to a post", "", "unreact_command_cb", "")
    weechat.hook_command("delete", "Delete a post", "<post-id>", "Delete a post", "", "delete_post_command_cb", "")

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

    weechat.hook_completion("irc_channels", "complete channels for mattermost", "channel_completion_cb", "")
    weechat.hook_completion("irc_privates", "complete dms/mpdms for mattermost", "private_completion_cb", "")
    weechat.hook_completion("mattermost_server_commands", "complete server names for mattermost", "server_completion_cb", "")