1use crate::config::BotConfig;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4pub enum TalkReason {
5 AllowedUser,
6 Oncall,
7 Peer,
8 AllowedChatGroup,
9 ChatGrant,
10 GlobalGrant,
11 Open,
12 None,
13}
14
15#[derive(Debug, Clone)]
16pub struct TalkEvaluation {
17 pub allowed: bool,
18 pub reason: TalkReason,
19 pub quota_key: Option<String>,
20}
21
22pub fn evaluate_talk(
23 bot: &BotConfig,
24 chat_id: &str,
25 sender_open_id: &str,
26 resolved_allowed_users: &[String],
27 peer_bot_open_ids: &[String],
28) -> TalkEvaluation {
29 let sender_is_allowed = resolved_allowed_users.iter().any(|id| id == sender_open_id);
30
31 if sender_is_allowed {
32 return TalkEvaluation {
33 allowed: true,
34 reason: TalkReason::AllowedUser,
35 quota_key: None,
36 };
37 }
38
39 let oncall_match = bot.oncall_chats.iter().any(|oc| oc.chat_id == chat_id);
40 if oncall_match {
41 return TalkEvaluation {
42 allowed: true,
43 reason: TalkReason::Oncall,
44 quota_key: None,
45 };
46 }
47
48 let peer_match = peer_bot_open_ids.iter().any(|id| id == sender_open_id);
49 if peer_match {
50 return TalkEvaluation {
51 allowed: true,
52 reason: TalkReason::Peer,
53 quota_key: None,
54 };
55 }
56
57 let chat_group_match = bot.allowed_chat_groups.iter().any(|cg| cg == chat_id);
58 if chat_group_match {
59 return TalkEvaluation {
60 allowed: true,
61 reason: TalkReason::AllowedChatGroup,
62 quota_key: None,
63 };
64 }
65
66 if let Some(granted) = bot.chat_grants.get(chat_id) {
67 if granted.iter().any(|id| id == sender_open_id) {
68 return TalkEvaluation {
69 allowed: true,
70 reason: TalkReason::ChatGrant,
71 quota_key: Some(format!("chat:{}:{}", chat_id, sender_open_id)),
72 };
73 }
74 }
75
76 if bot.global_grants.iter().any(|id| id == sender_open_id) {
77 return TalkEvaluation {
78 allowed: true,
79 reason: TalkReason::GlobalGrant,
80 quota_key: Some(format!("global:{}", sender_open_id)),
81 };
82 }
83
84 if bot.allowed_users.is_empty()
85 && bot.allowed_chat_groups.is_empty()
86 && bot.chat_grants.is_empty()
87 && bot.global_grants.is_empty()
88 && bot.oncall_chats.is_empty()
89 {
90 return TalkEvaluation {
91 allowed: true,
92 reason: TalkReason::Open,
93 quota_key: None,
94 };
95 }
96
97 TalkEvaluation {
98 allowed: false,
99 reason: TalkReason::None,
100 quota_key: None,
101 }
102}
103
104pub fn can_operate(
105 bot: &BotConfig,
106 sender_open_id: &str,
107 resolved_allowed_users: &[String],
108 peer_bot_open_ids: &[String],
109) -> bool {
110 let sender_is_allowed = resolved_allowed_users.iter().any(|id| id == sender_open_id);
111
112 if sender_is_allowed {
113 return true;
114 }
115
116 let peer_match = peer_bot_open_ids.iter().any(|id| id == sender_open_id);
117 if peer_match {
118 return true;
119 }
120
121 let has_allowlist = !bot.allowed_users.is_empty()
122 || !bot.allowed_chat_groups.is_empty()
123 || !bot.chat_grants.is_empty()
124 || !bot.global_grants.is_empty()
125 || !bot.oncall_chats.is_empty();
126
127 if !has_allowlist {
128 return true;
129 }
130
131 false
132}
133
134pub fn is_owner(open_id: &str, resolved_allowed_users: &[String]) -> bool {
135 resolved_allowed_users
136 .first()
137 .map(|owner| owner == open_id)
138 .unwrap_or(false)
139}
140
141pub fn get_owner_open_id(resolved_allowed_users: &[String]) -> Option<String> {
142 resolved_allowed_users.first().cloned()
143}
144
145pub fn grant_restricted(talk_eval: &TalkEvaluation, restrict_grant_commands: bool) -> bool {
146 if !restrict_grant_commands {
147 return false;
148 }
149 matches!(
150 talk_eval.reason,
151 TalkReason::ChatGrant | TalkReason::GlobalGrant
152 )
153}
154
155#[cfg(test)]
156mod tests {
157 use super::*;
158 use crate::config::{BotConfig, OncallChatBinding};
159 use std::collections::HashMap;
160
161 fn default_bot() -> BotConfig {
162 BotConfig {
163 name: None,
164 lark_app_id: "app-1".to_string(),
165 lark_app_secret: "secret".to_string(),
166 cli_id: "codex".to_string(),
167 cli_bin: None,
168 model: None,
169 working_dir: None,
170 backend_type: None,
171 lark_encrypt_key: None,
172 lark_verification_token: None,
173 allowed_users: vec![],
174 private_card: false,
175 allowed_chat_groups: vec![],
176 chat_grants: HashMap::new(),
177 global_grants: vec![],
178 oncall_chats: vec![],
179 restrict_grant_commands: false,
180 message_quota: None,
181 quota_state: HashMap::new(),
182 }
183 }
184
185 #[test]
186 fn evaluate_talk_open_when_no_restrictions() {
187 let bot = default_bot();
188 let eval = evaluate_talk(&bot, "chat-1", "ou_user", &[], &[]);
189 assert!(eval.allowed);
190 assert_eq!(eval.reason, TalkReason::Open);
191 }
192
193 #[test]
194 fn evaluate_talk_allowed_user() {
195 let bot = BotConfig {
196 allowed_users: vec!["ou_owner".to_string()],
197 ..default_bot()
198 };
199 let eval = evaluate_talk(&bot, "chat-1", "ou_owner", &["ou_owner".to_string()], &[]);
200 assert!(eval.allowed);
201 assert_eq!(eval.reason, TalkReason::AllowedUser);
202 }
203
204 #[test]
205 fn evaluate_talk_denies_non_allowed_user() {
206 let bot = BotConfig {
207 allowed_users: vec!["ou_owner".to_string()],
208 ..default_bot()
209 };
210 let eval = evaluate_talk(&bot, "chat-1", "ou_other", &["ou_owner".to_string()], &[]);
211 assert!(!eval.allowed);
212 assert_eq!(eval.reason, TalkReason::None);
213 }
214
215 #[test]
216 fn evaluate_talk_allowed_chat_group() {
217 let bot = BotConfig {
218 allowed_chat_groups: vec!["chat-open".to_string()],
219 ..default_bot()
220 };
221 let eval = evaluate_talk(&bot, "chat-open", "ou_anyone", &[], &[]);
222 assert!(eval.allowed);
223 assert_eq!(eval.reason, TalkReason::AllowedChatGroup);
224 }
225
226 #[test]
227 fn evaluate_talk_oncall_chat() {
228 let bot = BotConfig {
229 oncall_chats: vec![OncallChatBinding {
230 chat_id: "oncall-1".to_string(),
231 working_dir: None,
232 }],
233 ..default_bot()
234 };
235 let eval = evaluate_talk(&bot, "oncall-1", "ou_member", &[], &[]);
236 assert!(eval.allowed);
237 assert_eq!(eval.reason, TalkReason::Oncall);
238 }
239
240 #[test]
241 fn evaluate_talk_chat_grant_with_quota_key() {
242 let mut chat_grants = HashMap::new();
243 chat_grants.insert("chat-1".to_string(), vec!["ou_granted".to_string()]);
244 let bot = BotConfig {
245 allowed_users: vec!["ou_owner".to_string()],
246 chat_grants,
247 ..default_bot()
248 };
249 let eval = evaluate_talk(&bot, "chat-1", "ou_granted", &["ou_owner".to_string()], &[]);
250 assert!(eval.allowed);
251 assert_eq!(eval.reason, TalkReason::ChatGrant);
252 assert_eq!(eval.quota_key, Some("chat:chat-1:ou_granted".to_string()));
253 }
254
255 #[test]
256 fn evaluate_talk_global_grant() {
257 let bot = BotConfig {
258 allowed_users: vec!["ou_owner".to_string()],
259 global_grants: vec!["ou_global".to_string()],
260 ..default_bot()
261 };
262 let eval = evaluate_talk(
263 &bot,
264 "any-chat",
265 "ou_global",
266 &["ou_owner".to_string()],
267 &[],
268 );
269 assert!(eval.allowed);
270 assert_eq!(eval.reason, TalkReason::GlobalGrant);
271 assert_eq!(eval.quota_key, Some("global:ou_global".to_string()));
272 }
273
274 #[test]
275 fn can_operate_empty_allowlist() {
276 let bot = default_bot();
277 assert!(can_operate(&bot, "ou_any", &[], &[]));
278 }
279
280 #[test]
281 fn can_operate_respects_allowlist() {
282 let bot = BotConfig {
283 allowed_users: vec!["ou_owner".to_string()],
284 ..default_bot()
285 };
286 assert!(can_operate(
287 &bot,
288 "ou_owner",
289 &["ou_owner".to_string()],
290 &[]
291 ));
292 assert!(!can_operate(
293 &bot,
294 "ou_other",
295 &["ou_owner".to_string()],
296 &[]
297 ));
298 }
299
300 #[test]
301 fn can_operate_is_locked_by_talk_only_grants() {
302 let bot = BotConfig {
303 allowed_chat_groups: vec!["chat-open".to_string()],
304 ..default_bot()
305 };
306 assert!(!can_operate(&bot, "ou_any", &[], &[]));
307 }
308
309 #[test]
310 fn grant_restricted_blocks_chat_grant_when_enabled() {
311 let eval = TalkEvaluation {
312 allowed: true,
313 reason: TalkReason::ChatGrant,
314 quota_key: Some("chat:chat-1:ou_user".to_string()),
315 };
316 assert!(grant_restricted(&eval, true));
317 assert!(!grant_restricted(&eval, false));
318 }
319
320 #[test]
321 fn grant_restricted_allows_allowed_user_even_when_enabled() {
322 let eval = TalkEvaluation {
323 allowed: true,
324 reason: TalkReason::AllowedUser,
325 quota_key: None,
326 };
327 assert!(!grant_restricted(&eval, true));
328 }
329}