kovi 0.13.0-rc1

A OneBot V11 bot plugin framework
Documentation
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
use super::RuntimeBot;
use crate::bot::BotInformation;
use crate::error::BotError;
use crate::event::id::ID;
use crate::plugin::PluginInfo;
use crate::types::ApiAndOptOneshot;
use crate::{Bot, PluginBuilder};
use parking_lot::RwLock;
use std::path::PathBuf;
use std::sync::Arc;
use tokio::sync::mpsc;

#[cfg(feature = "plugin-access-control")]
pub use crate::plugin::SetAccessControlList;

#[cfg(feature = "plugin-access-control")]
use ahash::HashSet;
#[cfg(feature = "plugin-access-control")]
use serde::{Deserialize, Serialize};

#[deprecated(since = "0.11.0", note = "弃用,直接删掉就好了")]
pub trait KoviApi {}

#[derive(Debug, Clone)]
pub enum SetAdmin {
    /// 增加一个管理员
    Add(ID),
    /// 增加多个管理员
    Adds(Vec<ID>),
    /// 移除一个管理员
    Remove(ID),
    /// 移除多个管理员
    Removes(Vec<ID>),
    /// 替换管理员成此管理员
    Changes(Vec<ID>),
}

#[cfg(feature = "plugin-access-control")]
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct AccessList {
    pub friends: HashSet<ID>,
    pub groups: HashSet<ID>,
}

#[cfg(feature = "plugin-access-control")]
#[derive(Debug, Clone, Copy, Deserialize, Serialize)]
pub enum AccessControlMode {
    BlackList,
    WhiteList,
}

/// 黑白名单
#[cfg(feature = "plugin-access-control")]
impl RuntimeBot {
    /// 为某一插件启动名单
    ///
    /// # error
    ///
    /// 如果寻找不到插件,会返回Err `BotError::PluginNotFound`
    ///
    /// 如果此 `RuntimeBot` 实例内部的 `Bot` 中已经不存在,将会返回Err `BotError::RefExpired` 。
    /// 这通常出现在 `Bot` 已经关闭,可有个不受 Kovi 管理的线程仍然拥有此 `RuntimeBot`。
    pub fn set_plugin_access_control<T: AsRef<str>>(
        &self,
        plugin_name: T,
        enable: bool,
    ) -> Result<(), BotError> {
        let bot = match self.bot.upgrade() {
            Some(b) => b,
            None => return Err(BotError::RefExpired),
        };

        let mut bot = bot.write();

        let plugin_name = plugin_name.as_ref();

        let plugin = match bot.plugins.get_mut(plugin_name) {
            Some(v) => v,
            None => return Err(BotError::PluginNotFound(plugin_name.to_string())),
        };

        plugin.set_access_control(enable);

        Ok(())
    }

    /// 更改名单为其他模式,插件默认为白名单模式
    ///
    /// # error
    ///
    /// 如果寻找不到插件,会返回Err `BotError::PluginNotFound`
    ///
    /// 如果此 `RuntimeBot` 实例内部的 `Bot` 中已经不存在,将会返回Err `BotError::RefExpired` 。
    /// 这通常出现在 `Bot` 已经关闭,可有个不受 Kovi 管理的线程仍然拥有此 `RuntimeBot`。
    pub fn set_plugin_access_control_mode<T: AsRef<str>>(
        &self,
        plugin_name: T,
        access_control_mode: AccessControlMode,
    ) -> Result<(), BotError> {
        let bot = match self.bot.upgrade() {
            Some(b) => b,
            None => return Err(BotError::RefExpired),
        };

        let mut bot = bot.write();

        let plugin_name = plugin_name.as_ref();

        let plugin = match bot.plugins.get_mut(plugin_name) {
            Some(v) => v,
            None => return Err(BotError::PluginNotFound(plugin_name.to_string())),
        };

        plugin.set_access_control_mode(access_control_mode);

        Ok(())
    }

    /// 为某一插件添加名单
    ///
    /// is_group为true时,为群组名单,为false时为好友名单
    ///
    /// # error
    ///
    /// 如果寻找不到插件,会返回Err `BotError::PluginNotFound`
    ///
    /// 如果此 `RuntimeBot` 实例内部的 `Bot` 中已经不存在,将会返回Err `BotError::RefExpired` 。
    /// 这通常出现在 `Bot` 已经关闭,可有个不受 Kovi 管理的线程仍然拥有此 `RuntimeBot`。
    pub fn set_plugin_access_control_list<T: AsRef<str>>(
        &self,
        plugin_name: T,
        is_group: bool,
        change: SetAccessControlList,
    ) -> Result<(), BotError> {
        let bot = match self.bot.upgrade() {
            Some(b) => b,
            None => return Err(BotError::RefExpired),
        };

        let mut bot = bot.write();

        let plugin_name = plugin_name.as_ref();

        let plugin = match bot.plugins.get_mut(plugin_name) {
            Some(v) => v,
            None => return Err(BotError::PluginNotFound(plugin_name.to_string())),
        };

        plugin.set_access_control_list(is_group, change);

        Ok(())
    }
}

/// 管理员控制
impl RuntimeBot {
    /// 修改Bot的管理员
    ///
    /// # Error
    ///
    /// 如果此 `RuntimeBot` 实例内部的 `Bot` 中已经不存在,将会返回 `BotError::RefExpired` 错误。
    /// 这通常出现在Bot已经关闭,可有个不受Kovi管理的线程仍然拥有此 RuntimeBot。
    pub fn set_deputy_admins(&self, change: SetAdmin) -> Result<(), BotError> {
        let bot = match self.bot.upgrade() {
            Some(b) => b,
            None => return Err(BotError::RefExpired),
        };
        let bot = bot.read();
        let mut bot_info_lock = bot.information.write();
        let main_admin = bot_info_lock.get_main_admin().clone();
        let mut deputy_admins = bot_info_lock.get_deputy_admins().clone();
        match change {
            SetAdmin::Add(id) => {
                deputy_admins.insert(id);
            }
            SetAdmin::Adds(ids) => {
                deputy_admins.extend(ids);
            }
            SetAdmin::Remove(id) => {
                deputy_admins.remove(&id);
            }
            SetAdmin::Removes(ids) => {
                deputy_admins.retain(|x| !ids.contains(x));
            }
            SetAdmin::Changes(ids) => {
                deputy_admins = ids.into_iter().collect();
            }
        }

        *bot_info_lock = BotInformation::build(main_admin, deputy_admins);

        Ok(())
    }

    /// 获取Bot的主管理员
    ///
    /// # Error
    ///
    /// 如果此 `RuntimeBot` 实例内部的 `Bot` 中已经不存在,将会返回 `BotError::RefExpired` 错误。
    /// 这通常出现在Bot已经关闭,可有个不受Kovi管理的线程仍然拥有此 RuntimeBot。
    pub fn get_main_admin(&self) -> Result<ID, BotError> {
        let bot = match self.bot.upgrade() {
            Some(b) => b,
            None => return Err(BotError::RefExpired),
        };

        let id = bot.read().information.read().get_main_admin().clone();
        Ok(id)
    }

    /// 获取Bot的副管理员
    ///
    /// # Error
    ///
    /// 如果此 `RuntimeBot` 实例内部的 `Bot` 中已经不存在,将会返回 `BotError::RefExpired` 错误。
    /// 这通常出现在Bot已经关闭,可有个不受Kovi管理的线程仍然拥有此 RuntimeBot。
    pub fn get_deputy_admins(&self) -> Result<Vec<ID>, BotError> {
        let bot = match self.bot.upgrade() {
            Some(b) => b,
            None => return Err(BotError::RefExpired),
        };

        let ids = bot.read().information.read().get_deputy_admins().clone();
        Ok(ids.into_iter().collect())
    }

    /// 获取Bot的所有管理员
    ///
    /// # Error
    ///
    /// 如果此 `RuntimeBot` 实例内部的 `Bot` 中已经不存在,将会返回 `BotError::RefExpired` 错误。
    /// 这通常出现在Bot已经关闭,可有个不受Kovi管理的线程仍然拥有此 RuntimeBot。
    pub fn get_all_admin(&self) -> Result<Vec<ID>, BotError> {
        let bot = match self.bot.upgrade() {
            Some(b) => b,
            None => return Err(BotError::RefExpired),
        };

        let mut admins = Vec::with_capacity(1);

        let bot = bot.read();

        admins.push(bot.information.read().get_main_admin().clone());

        admins.extend(bot.information.read().get_deputy_admins().clone());

        Ok(admins)
    }
}

/// 工具
impl RuntimeBot {
    /// 获取插件自己的路径
    ///
    /// # panic
    ///
    /// 可能会 panic 的情况:
    ///  - 当前运行目录不存在,这种情况很少见。
    ///  - 权限不足,无法访问当前目录,这样肯定不能运行插件。
    pub fn get_data_path(&self) -> PathBuf {
        let data_root_path = crate::utils::get_data_root_path();
        data_root_path.join(self.plugin_name.clone())
    }
}

/// 插件控制
impl RuntimeBot {
    /// 获取Bot的插件信息。
    ///
    /// # Error
    ///
    /// 如果此 `RuntimeBot` 实例内部的 `Bot` 中已经不存在,将会返回 `BotError::RefExpired` 错误。
    /// 这通常出现在Bot已经关闭,可有个不受Kovi管理的线程仍然拥有此 RuntimeBot。
    pub fn get_plugin_info(&self) -> Result<Vec<PluginInfo>, BotError> {
        let bot = match self.bot.upgrade() {
            Some(b) => b,
            None => return Err(BotError::RefExpired),
        };

        let bot = bot.read();

        let plugins_info: Vec<PluginInfo> = bot
            .plugins
            .iter()
            .map(|(name, plugin)| PluginInfo {
                name: name.clone(),
                version: plugin.version.clone(),
                enabled: *plugin.enabled.borrow(),
                enable_on_startup: plugin.enable_on_startup,
                #[cfg(feature = "plugin-access-control")]
                access_control: plugin.access_control,
                #[cfg(feature = "plugin-access-control")]
                list_mode: plugin.list_mode,
                #[cfg(feature = "plugin-access-control")]
                access_list: plugin.access_list.clone(),
            })
            .collect();

        Ok(plugins_info)
    }

    /// 重载传入的插件
    ///
    /// # error
    ///
    /// 如果寻找不到插件,会返回Err `BotError::PluginNotFound`
    ///
    /// 如果此 `RuntimeBot` 实例内部的 `Bot` 中已经不存在,将会返回Err `BotError::RefExpired` 。
    /// 这通常出现在 `Bot` 已经关闭,可有个不受 Kovi 管理的线程仍然拥有此 `RuntimeBot`。
    ///
    /// # panic
    ///
    /// 如果插件的 Drop 闭包发生 panic ,此函数也会 panic
    pub async fn restart_plugin<T: AsRef<str>>(&self, plugin_name: T) -> Result<(), BotError> {
        if self.is_plugin_enable(&plugin_name)? {
            let join = self.disable_plugin(&plugin_name)?;

            if let Some(join) = join {
                join.await.expect("Internal thread panic")
            }
        }

        self.enable_plugin(plugin_name)
    }

    /// 卸载传入的插件
    ///
    /// # error
    ///
    /// 如果寻找不到插件,会返回Err `BotError::PluginNotFound`
    ///
    /// 如果此 `RuntimeBot` 实例内部的 `Bot` 中已经不存在,将会返回Err `BotError::RefExpired` 。
    /// 这通常出现在 `Bot` 已经关闭,可有个不受 Kovi 管理的线程仍然拥有此 `RuntimeBot`。
    pub fn disable_plugin<T: AsRef<str>>(
        &self,
        plugin_name: T,
    ) -> Result<Option<tokio::task::JoinHandle<()>>, BotError> {
        if !self.is_plugin_enable(&plugin_name)? {
            return Ok(None);
        }

        let bot = match self.bot.upgrade() {
            Some(b) => b,
            None => return Err(BotError::RefExpired),
        };

        Ok(Some(disable_plugin(bot, plugin_name)?))
    }

    /// 启用传入的插件
    ///
    /// # error
    ///
    /// 如果寻找不到插件,会返回Err `BotError::PluginNotFound`
    ///
    /// 如果此 `RuntimeBot` 实例内部的 `Bot` 中已经不存在,将会返回Err `BotError::RefExpired` 。
    /// 这通常出现在 `Bot` 已经关闭,可有个不受 Kovi 管理的线程仍然拥有此 `RuntimeBot`。
    pub fn enable_plugin<T: AsRef<str>>(&self, plugin_name: T) -> Result<(), BotError> {
        if self.is_plugin_enable(&plugin_name)? {
            return Ok(());
        }

        let bot = match self.bot.upgrade() {
            Some(b) => b,
            None => return Err(BotError::RefExpired),
        };

        enable_plugin(bot, plugin_name, self.api_tx.clone())
    }

    /// 插件是否开启
    ///
    /// # error
    ///
    /// 如果寻找不到插件,会返回Err `BotError::PluginNotFound`
    ///
    /// 如果此 `RuntimeBot` 实例内部的 `Bot` 中已经不存在,将会返回Err `BotError::RefExpired` 。
    /// 这通常出现在 `Bot` 已经关闭,可有个不受 Kovi 管理的线程仍然拥有此 `RuntimeBot`。
    pub fn is_plugin_enable<T: AsRef<str>>(&self, plugin_name: T) -> Result<bool, BotError> {
        let bot = match self.bot.upgrade() {
            Some(b) => b,
            None => return Err(BotError::RefExpired),
        };

        let bot = bot.read();
        let plugin_name = plugin_name.as_ref();

        let bot_plugin = match bot.plugins.get(plugin_name) {
            Some(v) => v,
            None => return Err(BotError::PluginNotFound(plugin_name.to_string())),
        };
        let bool_ = *bot_plugin.enabled.borrow();
        Ok(bool_)
    }
}

pub(crate) fn disable_plugin<T: AsRef<str>>(
    bot: Arc<RwLock<Bot>>,
    plugin_name: T,
) -> Result<tokio::task::JoinHandle<()>, BotError> {
    let join;
    {
        let mut bot = bot.write();

        let plugin_name = plugin_name.as_ref();

        let bot_plugin = match bot.plugins.get_mut(plugin_name) {
            Some(v) => v,
            None => return Err(BotError::PluginNotFound(plugin_name.to_string())),
        };
        join = bot_plugin.shutdown();
    }

    Ok(join)
}

fn enable_plugin<T: AsRef<str>>(
    bot: Arc<RwLock<Bot>>,
    plugin_name: T,
    api_tx: mpsc::Sender<ApiAndOptOneshot>,
) -> Result<(), BotError> {
    let bot_read = bot.read();
    let plugin_name = plugin_name.as_ref();

    // let (host, port) = {
    //     let info = bot_read.information.read();
    //     (info.server.host.clone(), info.server.port)
    // };

    let Some(bot_plugin) = bot_read.plugins.get(plugin_name) else {
        return Err(BotError::PluginNotFound(plugin_name.to_string()));
    };

    bot_plugin.enabled.send_modify(|v| {
        *v = true;
    });

    let plugin_ = bot_plugin.clone();

    let plugin_builder = PluginBuilder::new(plugin_name.to_string(), bot.clone(), api_tx);

    tokio::spawn(async move { plugin_.run(plugin_builder) });

    Ok(())
}