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