1use std::path::Path;
2
3use crate::config::Config;
4use crate::date::secs_to_iso;
5use crate::error::Result;
6use crate::headers::decode_mime_words;
7use crate::structs::EmailStore;
8
9pub fn write_haof(store: &EmailStore, config: &Config) -> Result<String> {
10 let dir = config.dir.as_deref().unwrap_or(".");
11 let haof_path = Path::new(dir).join("haof.xml");
12
13 let mut xml = String::from("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
14 xml.push_str("<haof version=\"1.0\">\n");
15 xml.push_str(&format!(
16 " <title>{}</title>\n",
17 escape_xml(config.label.as_deref().unwrap_or("Archive"))
18 ));
19 xml.push_str(" <generator>hypermail-rs</generator>\n");
20 xml.push_str(&format!(" <count>{}</count>\n", store.emails.len()));
21
22 for email in &store.emails {
23 xml.push_str(" <message>\n");
24 xml.push_str(&format!(" <id>{}</id>\n", escape_xml(&email.msgnum.to_string())));
25 xml.push_str(&format!(" <date>{}</date>\n", secs_to_iso(email.date)));
26 xml.push_str(&format!(
27 " <subject>{}</subject>\n",
28 escape_xml(&decode_mime_words(email.subject.as_deref().unwrap_or("(no subject)")))
29 ));
30 xml.push_str(&format!(
31 " <from>{}</from>\n",
32 escape_xml(&decode_mime_words(email.name.as_deref().unwrap_or("Unknown")))
33 ));
34 if let Some(ref addr) = email.email_addr {
35 xml.push_str(&format!(" <email>{}</email>\n", escape_xml(addr)));
36 }
37 if let Some(ref msgid) = email.msgid {
38 xml.push_str(&format!(" <msgid>{}</msgid>\n", escape_xml(msgid)));
39 }
40 if let Some(ref inreplyto) = email.inreplyto {
41 xml.push_str(&format!(" <inreplyto>{}</inreplyto>\n", escape_xml(inreplyto)));
42 }
43 xml.push_str(" </message>\n");
44 }
45
46 xml.push_str("</haof>\n");
47
48 std::fs::write(&haof_path, &xml)?;
49 crate::file_utils::apply_permissions(&haof_path, config.filemode);
50 Ok(xml)
51}
52
53fn escape_xml(s: &str) -> String {
54 let mut result = String::with_capacity(s.len());
55 for c in s.chars() {
56 match c {
57 '&' => result.push_str("&"),
58 '<' => result.push_str("<"),
59 '>' => result.push_str(">"),
60 '"' => result.push_str("""),
61 '\'' => result.push_str("'"),
62 c if !matches!(c, '\t' | '\n' | '\r' | '\u{20}'..='\u{d7ff}'
63 | '\u{e000}'..='\u{fffd}' | '\u{10000}'..='\u{10ffff}') => {},
64 c => result.push(c),
65 }
66 }
67 result
68}
69
70#[cfg(test)]
71mod tests {
72 use super::*;
73 use crate::config::Config;
74 use crate::message::EmailInfo;
75
76 #[test]
77 fn test_escape_xml() {
78 assert_eq!(escape_xml("<test&>"), "<test&>");
79 assert_eq!(escape_xml("plain"), "plain");
80 assert_eq!(escape_xml("a\u{1}b\u{1b}c\td"), "abc\td");
81 assert_eq!(escape_xml("a\u{fffe}\u{ffff}b"), "ab");
82 }
83
84 #[cfg(unix)]
85 #[test]
86 fn test_haof_uses_private_filemode() {
87 use std::os::unix::fs::PermissionsExt;
88 let dir = tempfile::tempdir().unwrap();
89 let config = Config {
90 dir: Some(dir.path().to_string_lossy().into_owned()),
91 filemode: 0o600,
92 ..Default::default()
93 };
94 write_haof(&EmailStore::new(), &config).unwrap();
95 let mode = std::fs::metadata(dir.path().join("haof.xml")).unwrap().permissions().mode();
96 assert_eq!(mode & 0o777, 0o600);
97 }
98
99 #[test]
100 fn test_write_haof_basic() {
101 let dir = tempfile::tempdir().unwrap();
102 let mut store = EmailStore::new();
103 let email = EmailInfo {
104 msgnum: 1,
105 name: Some("=?utf-8?Q?J=C3=BCrgen?=".to_string()),
106 email_addr: Some("alice@example.com".to_string()),
107 subject: Some("Hello".to_string()),
108 date: 1000000,
109 msgid: Some("<abc@e.com>".to_string()),
110 ..Default::default()
111 };
112 store.add_email(email);
113 let config =
114 Config { dir: Some(dir.path().to_string_lossy().into_owned()), ..Default::default() };
115 let xml = write_haof(&store, &config).unwrap();
116 assert!(xml.contains("<subject>Hello</subject>"));
117 assert!(xml.contains("<from>Jürgen</from>"), "{xml}");
118 assert!(xml.contains("<count>1</count>"));
119 }
120}