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