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 && granted.iter().any(|id| id == sender_open_id)
68 {
69 return TalkEvaluation {
70 allowed: true,
71 reason: TalkReason::ChatGrant,
72 quota_key: Some(format!("chat:{}:{}", chat_id, sender_open_id)),
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 cli_args: Vec::new(),
169 skip_working_dir_prompt: false,
170 model: None,
171 working_dir: None,
172 lark_encrypt_key: None,
173 lark_verification_token: None,
174 allowed_users: vec![],
175 private_card: false,
176 allowed_chat_groups: vec![],
177 chat_grants: HashMap::new(),
178 global_grants: vec![],
179 oncall_chats: vec![],
180 restrict_grant_commands: false,
181 message_quota: None,
182 quota_state: HashMap::new(),
183 custom_triggers: Vec::new(),
184 }
185 }
186
187 #[test]
188 fn evaluate_talk_open_when_no_restrictions() {
189 let bot = default_bot();
190 let eval = evaluate_talk(&bot, "chat-1", "ou_user", &[], &[]);
191 assert!(eval.allowed);
192 assert_eq!(eval.reason, TalkReason::Open);
193 }
194
195 #[test]
196 fn evaluate_talk_allowed_user() {
197 let bot = BotConfig {
198 allowed_users: vec!["ou_owner".to_string()],
199 ..default_bot()
200 };
201 let eval = evaluate_talk(&bot, "chat-1", "ou_owner", &["ou_owner".to_string()], &[]);
202 assert!(eval.allowed);
203 assert_eq!(eval.reason, TalkReason::AllowedUser);
204 }
205
206 #[test]
207 fn evaluate_talk_denies_non_allowed_user() {
208 let bot = BotConfig {
209 allowed_users: vec!["ou_owner".to_string()],
210 ..default_bot()
211 };
212 let eval = evaluate_talk(&bot, "chat-1", "ou_other", &["ou_owner".to_string()], &[]);
213 assert!(!eval.allowed);
214 assert_eq!(eval.reason, TalkReason::None);
215 }
216
217 #[test]
218 fn evaluate_talk_allowed_chat_group() {
219 let bot = BotConfig {
220 allowed_chat_groups: vec!["chat-open".to_string()],
221 ..default_bot()
222 };
223 let eval = evaluate_talk(&bot, "chat-open", "ou_anyone", &[], &[]);
224 assert!(eval.allowed);
225 assert_eq!(eval.reason, TalkReason::AllowedChatGroup);
226 }
227
228 #[test]
229 fn evaluate_talk_oncall_chat() {
230 let bot = BotConfig {
231 oncall_chats: vec![OncallChatBinding {
232 chat_id: "oncall-1".to_string(),
233 working_dir: None,
234 }],
235 ..default_bot()
236 };
237 let eval = evaluate_talk(&bot, "oncall-1", "ou_member", &[], &[]);
238 assert!(eval.allowed);
239 assert_eq!(eval.reason, TalkReason::Oncall);
240 }
241
242 #[test]
243 fn evaluate_talk_chat_grant_with_quota_key() {
244 let mut chat_grants = HashMap::new();
245 chat_grants.insert("chat-1".to_string(), vec!["ou_granted".to_string()]);
246 let bot = BotConfig {
247 allowed_users: vec!["ou_owner".to_string()],
248 chat_grants,
249 ..default_bot()
250 };
251 let eval = evaluate_talk(&bot, "chat-1", "ou_granted", &["ou_owner".to_string()], &[]);
252 assert!(eval.allowed);
253 assert_eq!(eval.reason, TalkReason::ChatGrant);
254 assert_eq!(eval.quota_key, Some("chat:chat-1:ou_granted".to_string()));
255 }
256
257 #[test]
258 fn evaluate_talk_global_grant() {
259 let bot = BotConfig {
260 allowed_users: vec!["ou_owner".to_string()],
261 global_grants: vec!["ou_global".to_string()],
262 ..default_bot()
263 };
264 let eval = evaluate_talk(
265 &bot,
266 "any-chat",
267 "ou_global",
268 &["ou_owner".to_string()],
269 &[],
270 );
271 assert!(eval.allowed);
272 assert_eq!(eval.reason, TalkReason::GlobalGrant);
273 assert_eq!(eval.quota_key, Some("global:ou_global".to_string()));
274 }
275
276 #[test]
277 fn can_operate_empty_allowlist() {
278 let bot = default_bot();
279 assert!(can_operate(&bot, "ou_any", &[], &[]));
280 }
281
282 #[test]
283 fn can_operate_respects_allowlist() {
284 let bot = BotConfig {
285 allowed_users: vec!["ou_owner".to_string()],
286 ..default_bot()
287 };
288 assert!(can_operate(
289 &bot,
290 "ou_owner",
291 &["ou_owner".to_string()],
292 &[]
293 ));
294 assert!(!can_operate(
295 &bot,
296 "ou_other",
297 &["ou_owner".to_string()],
298 &[]
299 ));
300 }
301
302 #[test]
303 fn can_operate_is_locked_by_talk_only_grants() {
304 let bot = BotConfig {
305 allowed_chat_groups: vec!["chat-open".to_string()],
306 ..default_bot()
307 };
308 assert!(!can_operate(&bot, "ou_any", &[], &[]));
309 }
310
311 #[test]
312 fn grant_restricted_blocks_chat_grant_when_enabled() {
313 let eval = TalkEvaluation {
314 allowed: true,
315 reason: TalkReason::ChatGrant,
316 quota_key: Some("chat:chat-1:ou_user".to_string()),
317 };
318 assert!(grant_restricted(&eval, true));
319 assert!(!grant_restricted(&eval, false));
320 }
321
322 #[test]
323 fn grant_restricted_allows_allowed_user_even_when_enabled() {
324 let eval = TalkEvaluation {
325 allowed: true,
326 reason: TalkReason::AllowedUser,
327 quota_key: None,
328 };
329 assert!(!grant_restricted(&eval, true));
330 }
331}