aboutsummaryrefslogtreecommitdiffstats
path: root/slack/config.py
blob: a3b01ce2ed835b6298b7ee014356f9f71f795a09 (plain) (blame)
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
from __future__ import annotations

from typing import Optional

import weechat

from slack.log import print_error
from slack.shared import shared
from slack.slack_conversation import invalidate_nicklists
from slack.slack_workspace import SlackWorkspace
from slack.util import get_callback_name
from slack.weechat_config import (
    WeeChatColor,
    WeeChatConfig,
    WeeChatOption,
    WeeChatOptionType,
    WeeChatSection,
)


class SlackConfigSectionColor:
    def __init__(self, weechat_config: WeeChatConfig):
        self._section = WeeChatSection(weechat_config, "color")

        self.channel_mention_color = WeeChatOption(
            self._section,
            "channel_mention_color",
            "text color for mentioned channel names in the chat",
            WeeChatColor("blue"),
        )

        self.deleted_message = WeeChatOption(
            self._section,
            "deleted_message",
            "text color for a deleted message",
            WeeChatColor("red"),
        )

        self.disconnected = WeeChatOption(
            self._section,
            "disconnected",
            "text color for the disconnected text",
            WeeChatColor("red"),
        )

        self.edited_message_suffix = WeeChatOption(
            self._section,
            "edited_message_suffix",
            "text color for the suffix after an edited message",
            WeeChatColor("095"),
        )

        self.loading = WeeChatOption(
            self._section,
            "loading",
            "text color for the loading text",
            WeeChatColor("yellow"),
        )

        self.message_join = WeeChatOption(
            self._section,
            "message_join",
            "color for text in join messages",
            WeeChatColor("green"),
            parent_option="irc.color.message_join",
        )

        self.message_quit = WeeChatOption(
            self._section,
            "message_quit",
            "color for text in part messages",
            WeeChatColor("red"),
            parent_option="irc.color.message_quit",
        )

        self.reaction_suffix = WeeChatOption(
            self._section,
            "reaction_suffix",
            "color for the reactions after a message",
            WeeChatColor("darkgray"),
        )

        self.reaction_self_suffix = WeeChatOption(
            self._section,
            "reaction_self_suffix",
            "color for the reactions after a message, for reactions you have added",
            WeeChatColor("blue"),
        )

        self.user_mention_color = WeeChatOption(
            self._section,
            "user_mention_color",
            "text color for mentioned user names in the chat",
            WeeChatColor("blue"),
        )

        self.usergroup_mention_color = WeeChatOption(
            self._section,
            "usergroup_mention_color",
            "text color for mentioned user group names in the chat",
            WeeChatColor("blue"),
        )


class SlackConfigSectionLook:
    def __init__(self, weechat_config: WeeChatConfig):
        self._section = WeeChatSection(weechat_config, "look")

        self.bot_user_suffix = WeeChatOption(
            self._section,
            "bot_user_suffix",
            "the suffix appended to nicks to indicate a bot",
            " :]",
        )

        self.color_nicks_in_nicklist = WeeChatOption(
            self._section,
            "color_nicks_in_nicklist",
            "use nick color in nicklist",
            False,
            parent_option="irc.look.color_nicks_in_nicklist",
            callback_change=self.config_change_color_nicks_in_nicklist_cb,
        )

        self.display_reaction_nicks = WeeChatOption(
            self._section,
            "display_reaction_nicks",
            "display the name of the reacting user(s) after each reaction",
            False,
        )

        self.external_user_suffix = WeeChatOption(
            self._section,
            "external_user_suffix",
            "the suffix appended to nicks to indicate external users",
            "*",
        )

        self.typing_status_nicks = WeeChatOption(
            self._section,
            "typing_status_nicks",
            'display nicks typing on the channel in bar item "typing" (option typing.look.enabled_nicks must be enabled)',
            True,
        )

        self.typing_status_self = WeeChatOption(
            self._section,
            "typing_status_self",
            "send self typing status to channels so that other users see when you are typing a message (option typing.look.enabled_self must be enabled)",
            True,
        )

        weechat.hook_config(
            "weechat.look.nick_color_*",
            get_callback_name(self.config_change_nick_colors_cb),
            "",
        )
        weechat.hook_config(
            "weechat.color.chat_nick_colors",
            get_callback_name(self.config_change_nick_colors_cb),
            "",
        )

    def config_change_color_nicks_in_nicklist_cb(
        self, option: WeeChatOption[WeeChatOptionType], parent_changed: bool
    ):
        invalidate_nicklists()

    def config_change_nick_colors_cb(self, data: str, option: str, value: str):
        invalidate_nicklists()
        return weechat.WEECHAT_RC_OK


class SlackConfigSectionWorkspace:
    def __init__(
        self,
        section: WeeChatSection,
        workspace_name: Optional[str],
        parent_config: Optional[SlackConfigSectionWorkspace],
    ):
        self._section = section
        self._workspace_name = workspace_name
        self._parent_config = parent_config

        self.api_token = self._create_option(
            "api_token",
            "",
            "",
        )

        self.api_cookies = self._create_option(
            "api_cookies",
            "",
            "",
        )

        self.autoconnect = self._create_option(
            "autoconnect",
            "automatically connect to workspace when WeeChat is starting",
            False,
        )

        self.network_timeout = self._create_option(
            "network_timeout",
            "timeout (in seconds) for network requests",
            30,
        )

        self.use_real_names = self._create_option(
            "use_real_names",
            "use real names as the nicks for all users. When this is"
            " false, display names will be used if set, with a fallback"
            " to the real name if display name is not set",
            False,
        )

    def _create_option(
        self,
        name: str,
        description: str,
        default_value: WeeChatOptionType,
        min_value: Optional[int] = None,
        max_value: Optional[int] = None,
        string_values: Optional[str] = None,
    ) -> WeeChatOption[WeeChatOptionType]:
        if self._workspace_name:
            option_name = f"{self._workspace_name}.{name}"
        else:
            option_name = name

        if self._parent_config:
            parent_option = getattr(self._parent_config, name, None)
        else:
            parent_option = None

        return WeeChatOption(
            self._section,
            option_name,
            description,
            default_value,
            min_value,
            max_value,
            string_values,
            parent_option,
        )


def config_section_workspace_read_cb(
    data: str, config_file: str, section: str, option_name: str, value: Optional[str]
) -> int:
    option_split = option_name.split(".", 1)
    if len(option_split) < 2:
        return weechat.WEECHAT_CONFIG_OPTION_SET_ERROR
    workspace_name, name = option_split
    if not workspace_name or not name:
        return weechat.WEECHAT_CONFIG_OPTION_SET_ERROR

    if workspace_name not in shared.workspaces:
        shared.workspaces[workspace_name] = SlackWorkspace(workspace_name)

    option = getattr(shared.workspaces[workspace_name].config, name, None)
    if option is None:
        return weechat.WEECHAT_CONFIG_OPTION_SET_OPTION_NOT_FOUND
    if not isinstance(option, WeeChatOption):
        return weechat.WEECHAT_CONFIG_OPTION_SET_ERROR

    if value is None or (
        shared.weechat_version < 0x03080000
        and value == ""
        and option.weechat_type != "string"
    ):
        rc = option.value_set_null()
    else:
        rc = option.value_set_as_str(value)
    if rc == weechat.WEECHAT_CONFIG_OPTION_SET_ERROR:
        print_error(f'error creating workspace option "{option_name}"')
    return rc


def config_section_workspace_write_for_old_weechat_cb(
    data: str, config_file: str, section_name: str
) -> int:
    if not weechat.config_write_line(config_file, section_name, ""):
        return weechat.WEECHAT_CONFIG_WRITE_ERROR

    for workspace in shared.workspaces.values():
        for option in vars(workspace.config).values():
            if isinstance(option, WeeChatOption):
                if (
                    option.weechat_type != "string"
                    or not weechat.config_option_is_null(
                        option._pointer  # pyright: ignore [reportPrivateUsage]
                    )
                ):
                    if not weechat.config_write_option(
                        config_file,
                        option._pointer,  # pyright: ignore [reportPrivateUsage]
                    ):
                        return weechat.WEECHAT_CONFIG_WRITE_ERROR

    return weechat.WEECHAT_CONFIG_WRITE_OK


class SlackConfig:
    def __init__(self):
        self.weechat_config = WeeChatConfig("slack")
        self.color = SlackConfigSectionColor(self.weechat_config)
        self.look = SlackConfigSectionLook(self.weechat_config)
        self._section_workspace_default = WeeChatSection(
            self.weechat_config, "workspace_default"
        )
        # WeeChat < 3.8 sends null as an empty string to callback_read, so in
        # order to distinguish them, don't write the null values to the config
        # See https://github.com/weechat/weechat/pull/1843
        callback_write = (
            get_callback_name(config_section_workspace_write_for_old_weechat_cb)
            if shared.weechat_version < 0x03080000
            else ""
        )
        self._section_workspace = WeeChatSection(
            self.weechat_config,
            "workspace",
            callback_read=get_callback_name(config_section_workspace_read_cb),
            callback_write=callback_write,
        )
        self._workspace_default = SlackConfigSectionWorkspace(
            self._section_workspace_default, None, None
        )

    def config_read(self):
        weechat.config_read(self.weechat_config.pointer)

    def create_workspace_config(self, workspace_name: str):
        if workspace_name in shared.workspaces:
            raise Exception(
                f"Failed to create workspace config, already exists: {workspace_name}"
            )
        return SlackConfigSectionWorkspace(
            self._section_workspace, workspace_name, self._workspace_default
        )