1use crate::message::BodyChain;
2use crate::structs::EmailStore;
3use std::collections::HashMap;
4
5pub fn link_quotes(store: &mut EmailStore, ignore_types: bool, linkquotes: bool) {
15 if !linkquotes || ignore_types {
16 return;
17 }
18
19 let msgid_to_msgnum: HashMap<&str, i32> = store
20 .emails
21 .iter()
22 .filter_map(|e| e.msgid.as_deref().map(|mid| (mid, e.msgnum)))
23 .collect();
24
25 let mut new_replies = Vec::new();
26 for email in &store.emails {
27 let to_msgnum = email.msgnum;
28 let from_text = collect_body_text(&email.bodylist);
29 for candidate in extract_angle_bracket_tokens(&from_text) {
30 if let Some(&from_msgnum) = msgid_to_msgnum.get(candidate) {
31 if from_msgnum != to_msgnum {
32 new_replies.push(crate::message::Reply {
33 from_msgnum,
34 msgnum: to_msgnum,
35 data: None,
36 maybe_reply: 1,
37 });
38 }
39 }
40 }
41 }
42 store.replylist.extend(new_replies);
43}
44
45fn extract_angle_bracket_tokens(text: &str) -> impl Iterator<Item = &str> {
47 let mut rest = text;
48 std::iter::from_fn(move || {
49 let start = rest.find('<')?;
50 let after_start = &rest[start + 1..];
51 match after_start.find('>') {
52 Some(end) => {
53 let consumed = start + 1 + end + 1;
54 let token = &rest[start..consumed];
55 rest = &rest[consumed..];
56 Some(token)
57 },
58 None => None,
59 }
60 })
61}
62
63fn collect_body_text(body_chain: &BodyChain) -> String {
64 let mut text = String::new();
65 for body in &body_chain.bodies {
66 if !body.attached && !body.header {
67 text.push_str(&body.line);
68 text.push(' ');
69 }
70 }
71 text
72}
73
74#[cfg(test)]
75mod tests {
76 use super::*;
77 use crate::message::{Body, BodyChain, EmailInfo};
78
79 #[test]
80 fn test_link_quotes_empty_store() {
81 let mut store = EmailStore::new();
82 link_quotes(&mut store, false, true);
83 assert!(store.replylist.is_empty());
84 }
85
86 #[test]
87 fn test_link_quotes_disabled() {
88 let mut store = EmailStore::new();
89 let e1 = EmailInfo {
90 msgnum: 1,
91 msgid: Some("<a@b>".to_string()),
92 bodylist: BodyChain {
93 bodies: vec![Body {
94 line: "reply to <a@b>".to_string(),
95 html: false,
96 header: false,
97 parsed_header: false,
98 attached: false,
99 demimed: false,
100 converted: false,
101 msgnum: 0,
102 }],
103 },
104 ..Default::default()
105 };
106 let e2 = EmailInfo {
107 msgnum: 2,
108 msgid: Some("<b@c>".to_string()),
109 bodylist: BodyChain { bodies: Vec::new() },
110 ..Default::default()
111 };
112 store.add_email(e1);
113 store.add_email(e2);
114 link_quotes(&mut store, false, false);
115 assert!(store.replylist.is_empty());
116 }
117
118 #[test]
119 fn test_link_quotes_finds_quoted_msgid() {
120 let mut store = EmailStore::new();
121 let e1 = EmailInfo {
122 msgnum: 1,
123 msgid: Some("<a@b>".to_string()),
124 bodylist: BodyChain { bodies: Vec::new() },
125 ..Default::default()
126 };
127 let e2 = EmailInfo {
128 msgnum: 2,
129 msgid: Some("<c@d>".to_string()),
130 bodylist: BodyChain {
131 bodies: vec![Body {
132 line: "On Mon, someone wrote: <a@b>".to_string(),
133 html: false,
134 header: false,
135 parsed_header: false,
136 attached: false,
137 demimed: false,
138 converted: false,
139 msgnum: 0,
140 }],
141 },
142 ..Default::default()
143 };
144 store.add_email(e1);
145 store.add_email(e2);
146 link_quotes(&mut store, false, true);
147 assert_eq!(store.replylist.len(), 1);
148 assert_eq!(store.replylist[0].from_msgnum, 1);
149 assert_eq!(store.replylist[0].msgnum, 2);
150 }
151
152 #[test]
153 fn test_extract_angle_bracket_tokens() {
154 let text = "reply to <a@b.com> and also <c@d.com> end";
155 let tokens: Vec<&str> = extract_angle_bracket_tokens(text).collect();
156 assert_eq!(tokens, vec!["<a@b.com>", "<c@d.com>"]);
157 }
158
159 #[test]
160 fn test_extract_angle_bracket_tokens_unclosed() {
161 let text = "no closing bracket <a@b.com";
162 let tokens: Vec<&str> = extract_angle_bracket_tokens(text).collect();
163 assert!(tokens.is_empty());
164 }
165
166 #[test]
167 fn test_collect_body_text() {
168 let chain = BodyChain {
169 bodies: vec![
170 Body {
171 line: "hello".to_string(),
172 html: false,
173 header: false,
174 parsed_header: false,
175 attached: false,
176 demimed: false,
177 converted: false,
178 msgnum: 0,
179 },
180 Body {
181 line: "world".to_string(),
182 html: false,
183 header: false,
184 parsed_header: false,
185 attached: true,
186 demimed: false,
187 converted: false,
188 msgnum: 0,
189 },
190 ],
191 };
192 let text = collect_body_text(&chain);
193 assert!(!text.contains("world"));
194 assert!(text.contains("hello"));
195 }
196}