Skip to main content

br_email/
lib.rs

1use crate::analyze::AnalyzeEmails;
2#[cfg(feature = "imap")]
3use crate::imap::Imap;
4#[cfg(feature = "pop3")]
5use crate::pop3::Pop3;
6#[cfg(feature = "smtp")]
7use crate::smtp::Smtp;
8use serde::{Deserialize, Serialize};
9use std::collections::BTreeMap;
10use std::io::Error;
11use std::path::PathBuf;
12use std::{fs, io};
13
14#[cfg(feature = "imap")]
15mod imap;
16#[cfg(feature = "pop3")]
17mod pop3;
18#[cfg(feature = "smtp")]
19mod smtp;
20
21pub mod analyze;
22pub mod pools;
23
24#[derive(Clone, Debug)]
25pub struct Mail {
26    /// 协议
27    protocol: Protocol,
28    /// 邮件地址
29    host: String,
30    /// 邮件端口
31    port: u16,
32    /// 邮件名称
33    name: String,
34    /// 邮件账号
35    mail: String,
36    /// 邮件密码
37    pass: String,
38    /// 邮件类型
39    email_type: EmailType,
40}
41
42impl Mail {
43    pub fn new_new(config: Config) -> Result<Self, String> {
44        let default = config.default.clone();
45        let connection = config
46            .connections
47            .get(&default)
48            .ok_or_else(|| format!("默认配置不存在: {default}"))?
49            .clone();
50        Ok(Self {
51            protocol: connection.protocol,
52            host: connection.host,
53            port: connection.port,
54            name: connection.name,
55            mail: connection.mail,
56            pass: connection.pass,
57            email_type: connection.email_type,
58        })
59    }
60    pub fn new(protocol: &str, host: &str, port: u16, name: &str, mail: &str, pass: &str) -> Self {
61        Self {
62            protocol: Protocol::from(protocol),
63            host: host.to_string(),
64            port,
65            name: name.to_string(),
66            mail: mail.to_string(),
67            pass: pass.to_string(),
68            email_type: EmailType::from(mail),
69        }
70    }
71    /// 解析邮件
72    pub fn analyze(data: Vec<u8>, debug: bool) -> io::Result<AnalyzeEmails> {
73        AnalyzeEmails::new(data, debug)
74    }
75    /// 接收
76    pub fn receive(&mut self) -> Box<dyn Receive> {
77        match self.protocol {
78            #[cfg(feature = "pop3")]
79            Protocol::POP3 => Box::new(Pop3::new(
80                self.host.clone(),
81                self.port,
82                self.name.clone(),
83                self.mail.clone(),
84                self.pass.clone(),
85                self.email_type.clone(),
86            )),
87            #[cfg(feature = "smtp")]
88            Protocol::Smtp => Box::new(Smtp::new(
89                self.host.clone(),
90                self.port,
91                self.name.clone(),
92                self.mail.clone(),
93                self.pass.clone(),
94                self.email_type.clone(),
95            )),
96            #[cfg(feature = "imap")]
97            Protocol::Imap => Box::new(Imap::new(
98                self.host.clone(),
99                self.port,
100                self.name.clone(),
101                self.mail.clone(),
102                self.pass.clone(),
103                self.email_type.clone(),
104            )),
105            _ => Box::new(UnsupportedReceive {
106                protocol: self.protocol.clone(),
107            }),
108        }
109    }
110    /// 发送
111    pub fn sender(&mut self) -> Box<dyn Sender> {
112        match self.protocol {
113            #[cfg(feature = "pop3")]
114            Protocol::POP3 => Box::new(Pop3::new(
115                self.host.clone(),
116                self.port,
117                self.name.clone(),
118                self.mail.clone(),
119                self.pass.clone(),
120                self.email_type.clone(),
121            )),
122            #[cfg(feature = "smtp")]
123            Protocol::Smtp => Box::new(Smtp::new(
124                self.host.clone(),
125                self.port,
126                self.name.clone(),
127                self.mail.clone(),
128                self.pass.clone(),
129                self.email_type.clone(),
130            )),
131            #[cfg(feature = "imap")]
132            Protocol::Imap => Box::new(Imap::new(
133                self.host.clone(),
134                self.port,
135                self.name.clone(),
136                self.mail.clone(),
137                self.pass.clone(),
138                self.email_type.clone(),
139            )),
140            _ => Box::new(UnsupportedSender {
141                protocol: self.protocol.clone(),
142            }),
143        }
144    }
145}
146
147pub trait Receive {
148    /// 获取邮件总数
149    fn get_total(&mut self) -> Result<usize, Error>;
150    /// 获取指定量ID列表
151    fn get_id_list(&mut self) -> Result<Vec<String>, Error>;
152    /// 获取指定ID邮件
153    /// 返回 (邮件内容,邮件尺寸,邮件指纹)
154    fn get_id_info(&mut self, id: &str) -> Result<(Vec<u8>, usize, String), Error>;
155    /// 删除指定邮件
156    fn del_id(&mut self, id: &str) -> Result<bool, Error>;
157
158    /// 获取唯一标识符列表
159    /// IMAP: 返回 UID 列表
160    /// POP3: 返回 (序号, UIDL) 列表中的 UIDL
161    fn get_uid_list(&mut self) -> Result<Vec<String>, Error> {
162        self.get_id_list()
163    }
164
165    /// 获取指定 UID 之后的新邮件列表 (增量查询)
166    /// IMAP: 使用 UID SEARCH
167    /// POP3: 需要配合本地存储比对
168    fn get_new_since(&mut self, _last_uid: &str) -> Result<Vec<String>, Error> {
169        Err(Error::other("此协议不支持增量查询"))
170    }
171
172    /// 获取邮箱有效性标识
173    /// IMAP: 返回 UIDVALIDITY
174    /// POP3: 返回 None (POP3 无此概念)
175    fn get_validity(&mut self) -> Result<Option<u64>, Error> {
176        Ok(None)
177    }
178
179    /// 获取最大 UID (用于记录同步点)
180    fn get_max_uid(&mut self) -> Result<Option<u64>, Error> {
181        Ok(None)
182    }
183
184    /// 获取 UIDL 映射表 (仅 POP3)
185    /// 返回 Vec<(序号, UIDL)>
186    fn get_uidl_map(&mut self) -> Result<Vec<(String, String)>, Error> {
187        Err(Error::other("此协议不支持 UIDL"))
188    }
189}
190
191pub trait Sender {
192    /// 发送邮件
193    /// 返回 (邮件内容,邮件尺寸,邮件指纹)
194    fn send(&mut self) -> Result<(Vec<u8>, usize, String), Error>;
195
196    /// 设置发件人信息
197    /// * name 发件人名称
198    /// * mail 发件箱
199    fn set_from(&mut self, name: &str) -> &mut dyn Sender;
200
201    /// 收件人
202    /// * name 收件人名称
203    /// * mail 收件箱
204    fn set_to(&mut self, name: &str, mail: &str) -> &mut dyn Sender;
205    /// 抄送人
206    /// * name 抄送人名称
207    /// * mail 收件箱
208    fn set_cc(&mut self, name: &str, mail: &str) -> &mut dyn Sender;
209    /// 设置主题
210    fn set_subject(&mut self, subject: &str) -> &mut dyn Sender;
211    /// 设置内容
212    fn set_body(&mut self, text: &str, html: &str) -> &mut dyn Sender;
213    /// 设置文件
214    fn set_file(&mut self, name: &str, data: Vec<u8>, content_type: &str) -> &mut dyn Sender;
215}
216
217struct UnsupportedReceive {
218    protocol: Protocol,
219}
220
221impl UnsupportedReceive {
222    fn err(&self, action: &str) -> Error {
223        Error::other(format!("{action} 不支持协议: {:?}", self.protocol))
224    }
225}
226
227impl Receive for UnsupportedReceive {
228    fn get_total(&mut self) -> Result<usize, Error> {
229        Err(self.err("获取邮件总数"))
230    }
231
232    fn get_id_list(&mut self) -> Result<Vec<String>, Error> {
233        Err(self.err("获取邮件ID列表"))
234    }
235
236    fn get_id_info(&mut self, _id: &str) -> Result<(Vec<u8>, usize, String), Error> {
237        Err(self.err("获取邮件内容"))
238    }
239
240    fn del_id(&mut self, _id: &str) -> Result<bool, Error> {
241        Err(self.err("删除邮件"))
242    }
243}
244
245struct UnsupportedSender {
246    protocol: Protocol,
247}
248
249impl UnsupportedSender {
250    fn err(&self, action: &str) -> Error {
251        Error::other(format!("{action} 不支持协议: {:?}", self.protocol))
252    }
253}
254
255impl Sender for UnsupportedSender {
256    fn send(&mut self) -> Result<(Vec<u8>, usize, String), Error> {
257        Err(self.err("发送邮件"))
258    }
259
260    fn set_from(&mut self, _name: &str) -> &mut dyn Sender {
261        self
262    }
263
264    fn set_to(&mut self, _name: &str, _mail: &str) -> &mut dyn Sender {
265        self
266    }
267
268    fn set_cc(&mut self, _name: &str, _mail: &str) -> &mut dyn Sender {
269        self
270    }
271
272    fn set_subject(&mut self, _subject: &str) -> &mut dyn Sender {
273        self
274    }
275
276    fn set_body(&mut self, _text: &str, _html: &str) -> &mut dyn Sender {
277        self
278    }
279
280    fn set_file(&mut self, _name: &str, _data: Vec<u8>, _content_type: &str) -> &mut dyn Sender {
281        self
282    }
283}
284
285#[derive(Clone, Debug, Deserialize, Serialize)]
286enum Protocol {
287    POP3,
288    Smtp,
289    Imap,
290    None,
291}
292
293impl Protocol {
294    pub fn from(name: &str) -> Self {
295        let name = name.to_lowercase();
296        match name.as_str() {
297            "pop3" => Protocol::POP3,
298            "imap" => Protocol::Imap,
299            "smtp" => Protocol::Smtp,
300            _ => Protocol::None,
301        }
302    }
303}
304
305#[derive(Clone, Debug, Deserialize, Serialize)]
306enum EmailType {
307    Google,
308    Qq,
309    None,
310}
311impl EmailType {
312    pub fn from(email: &str) -> Self {
313        match email {
314            x if x.contains("@gmail.com") => EmailType::Google,
315            x if x.contains("@qq.com") => EmailType::Qq,
316            _ => EmailType::None,
317        }
318    }
319}
320
321#[derive(Clone, Debug, Deserialize, Serialize)]
322pub struct Config {
323    default: String,
324    connections: BTreeMap<String, Connection>,
325}
326impl Default for Config {
327    fn default() -> Self {
328        Self::new()
329    }
330}
331
332impl Config {
333    pub fn create(config_file: PathBuf, pkg_name: bool) -> Config {
334        #[derive(Clone, Debug, Deserialize, Serialize)]
335        pub struct ConfigNew {
336            pub br_email: Config,
337        }
338        impl ConfigNew {
339            pub fn new() -> ConfigNew {
340                let mut connections = BTreeMap::new();
341                connections.insert("my_name".to_string(), Connection::default());
342                Self {
343                    br_email: Config {
344                        default: "my_name".to_string(),
345                        connections,
346                    },
347                }
348            }
349        }
350        match fs::read_to_string(config_file.clone()) {
351            Ok(e) => {
352                if pkg_name {
353                    let data = ConfigNew::new();
354                    toml::from_str::<ConfigNew>(&e)
355                        .unwrap_or_else(|_| {
356                            let toml = toml::to_string(&data).unwrap();
357                            let toml = format!("{e}\r\n{toml}");
358                            let _ = fs::write(config_file.to_str().unwrap(), toml);
359                            data
360                        })
361                        .br_email
362                } else {
363                    let data = Config::new();
364                    toml::from_str::<Config>(&e).unwrap_or_else(|_| {
365                        let toml = toml::to_string(&data).unwrap();
366                        let toml = format!("{e}\r\n{toml}");
367                        let _ = fs::write(config_file.to_str().unwrap(), toml);
368                        data
369                    })
370                }
371            }
372            Err(_) => {
373                if pkg_name {
374                    let data = ConfigNew::new();
375                    fs::create_dir_all(config_file.parent().unwrap()).unwrap();
376                    let toml = toml::to_string(&data).unwrap();
377                    let _ = fs::write(config_file.to_str().unwrap(), toml);
378                    data.br_email
379                } else {
380                    let data = Config::new();
381                    fs::create_dir_all(config_file.parent().unwrap()).unwrap();
382                    let toml = toml::to_string(&data).unwrap();
383                    let _ = fs::write(config_file.to_str().unwrap(), toml);
384                    data
385                }
386            }
387        }
388    }
389    pub fn new() -> Self {
390        let mut connections = BTreeMap::new();
391        connections.insert("my_name".to_string(), Connection::default());
392        Self {
393            default: "my_name".to_string(),
394            connections,
395        }
396    }
397}
398#[derive(Clone, Debug, Deserialize, Serialize)]
399struct Connection {
400    /// 协议
401    protocol: Protocol,
402    /// 邮件地址
403    host: String,
404    /// 邮件端口
405    port: u16,
406    /// 邮件名称
407    name: String,
408    /// 邮件账号
409    mail: String,
410    /// 邮件密码
411    pass: String,
412    /// 邮件类型
413    email_type: EmailType,
414}
415impl Connection {
416    pub fn default() -> Connection {
417        Self {
418            protocol: Protocol::Smtp,
419            host: "smtp.exmail.qq.com".to_string(),
420            port: 25,
421            name: "发件人名".to_string(),
422            mail: "xxx@qq.com".to_string(),
423            pass: "*******".to_string(),
424            email_type: EmailType::Qq,
425        }
426    }
427}
428
429#[cfg(test)]
430mod tests {
431    use super::*;
432
433    #[test]
434    fn test_protocol_from() {
435        assert!(matches!(Protocol::from("pop3"), Protocol::POP3));
436        assert!(matches!(Protocol::from("POP3"), Protocol::POP3));
437        assert!(matches!(Protocol::from("imap"), Protocol::Imap));
438        assert!(matches!(Protocol::from("IMAP"), Protocol::Imap));
439        assert!(matches!(Protocol::from("smtp"), Protocol::Smtp));
440        assert!(matches!(Protocol::from("SMTP"), Protocol::Smtp));
441        assert!(matches!(Protocol::from("unknown"), Protocol::None));
442    }
443
444    #[test]
445    fn test_email_type_from() {
446        assert!(matches!(
447            EmailType::from("user@gmail.com"),
448            EmailType::Google
449        ));
450        assert!(matches!(EmailType::from("user@qq.com"), EmailType::Qq));
451        assert!(matches!(
452            EmailType::from("user@outlook.com"),
453            EmailType::None
454        ));
455    }
456
457    #[test]
458    fn test_mail_new() {
459        let mail = Mail::new(
460            "imap",
461            "imap.gmail.com",
462            993,
463            "test",
464            "user@gmail.com",
465            "pass",
466        );
467        assert!(matches!(mail.protocol, Protocol::Imap));
468        assert_eq!(mail.host, "imap.gmail.com");
469        assert_eq!(mail.port, 993);
470        assert_eq!(mail.name, "test");
471        assert_eq!(mail.mail, "user@gmail.com");
472        assert_eq!(mail.pass, "pass");
473    }
474
475    #[test]
476    fn test_config_new() {
477        let config = Config::new();
478        assert_eq!(config.default, "my_name");
479        assert!(config.connections.contains_key("my_name"));
480    }
481
482    #[test]
483    fn test_config_default() {
484        let config = Config::default();
485        assert_eq!(config.default, "my_name");
486    }
487
488    #[test]
489    fn test_connection_default() {
490        let conn = Connection::default();
491        assert!(matches!(conn.protocol, Protocol::Smtp));
492        assert_eq!(conn.host, "smtp.exmail.qq.com");
493        assert_eq!(conn.port, 25);
494    }
495
496    #[test]
497    fn test_mail_new_new_success() {
498        let mut config = Config::new();
499        config.default = "test_conn".to_string();
500        config.connections.insert(
501            "test_conn".to_string(),
502            Connection {
503                protocol: Protocol::Imap,
504                host: "imap.test.com".to_string(),
505                port: 993,
506                name: "Test".to_string(),
507                mail: "test@test.com".to_string(),
508                pass: "password".to_string(),
509                email_type: EmailType::None,
510            },
511        );
512
513        let mail = Mail::new_new(config).unwrap();
514        assert_eq!(mail.host, "imap.test.com");
515        assert_eq!(mail.port, 993);
516    }
517
518    #[test]
519    fn test_mail_new_new_missing_default() {
520        let mut config = Config::new();
521        config.default = "nonexistent".to_string();
522
523        let result = Mail::new_new(config);
524        assert!(result.is_err());
525    }
526
527    #[test]
528    fn test_unsupported_receive() {
529        let mut mail = Mail::new("unknown", "host", 123, "name", "mail", "pass");
530        let mut receiver = mail.receive();
531
532        assert!(receiver.get_total().is_err());
533        assert!(receiver.get_id_list().is_err());
534        assert!(receiver.get_id_info("1").is_err());
535        assert!(receiver.del_id("1").is_err());
536    }
537
538    #[test]
539    fn test_unsupported_sender() {
540        let mut mail = Mail::new("unknown", "host", 123, "name", "mail", "pass");
541        let mut sender = mail.sender();
542
543        assert!(sender.send().is_err());
544        sender.set_from("test");
545        sender.set_to("name", "mail");
546        sender.set_cc("name", "mail");
547        sender.set_subject("subject");
548        sender.set_body("text", "html");
549        sender.set_file("name", vec![], "type");
550    }
551
552    #[test]
553    fn test_receive_trait_default_methods() {
554        let mut mail = Mail::new("unknown", "host", 123, "name", "mail", "pass");
555        let mut receiver = mail.receive();
556
557        assert!(receiver.get_new_since("100").is_err());
558        assert!(receiver.get_validity().unwrap().is_none());
559        assert!(receiver.get_max_uid().unwrap().is_none());
560        assert!(receiver.get_uidl_map().is_err());
561    }
562
563    #[test]
564    fn test_mail_analyze_valid() {
565        let email_data = b"From: sender@example.com\r\n\
566To: receiver@example.com\r\n\
567Subject: Test\r\n\
568Content-Type: text/plain\r\n\
569Date: 01 Jan 2024 12:00:00 +0000\r\n\
570\r\n\
571Body"
572            .to_vec();
573
574        let result = Mail::analyze(email_data, false);
575        assert!(result.is_ok());
576        let email = result.unwrap();
577        assert_eq!(email.subject, "Test");
578    }
579
580    #[test]
581    fn test_mail_analyze_invalid() {
582        let invalid_data = b"invalid email".to_vec();
583        let result = Mail::analyze(invalid_data, false);
584        assert!(result.is_err());
585    }
586
587    #[test]
588    fn test_mail_with_different_protocols() {
589        let pop3_mail = Mail::new("pop3", "pop.test.com", 995, "name", "user@test.com", "pass");
590        assert!(matches!(pop3_mail.protocol, Protocol::POP3));
591
592        let smtp_mail = Mail::new(
593            "smtp",
594            "smtp.test.com",
595            587,
596            "name",
597            "user@test.com",
598            "pass",
599        );
600        assert!(matches!(smtp_mail.protocol, Protocol::Smtp));
601
602        let imap_mail = Mail::new(
603            "imap",
604            "imap.test.com",
605            993,
606            "name",
607            "user@test.com",
608            "pass",
609        );
610        assert!(matches!(imap_mail.protocol, Protocol::Imap));
611    }
612
613    #[test]
614    fn test_email_type_detection() {
615        let gmail = Mail::new(
616            "imap",
617            "imap.gmail.com",
618            993,
619            "name",
620            "user@gmail.com",
621            "pass",
622        );
623        assert!(matches!(gmail.email_type, EmailType::Google));
624
625        let qq = Mail::new("imap", "imap.qq.com", 993, "name", "user@qq.com", "pass");
626        assert!(matches!(qq.email_type, EmailType::Qq));
627
628        let other = Mail::new(
629            "imap",
630            "imap.other.com",
631            993,
632            "name",
633            "user@other.com",
634            "pass",
635        );
636        assert!(matches!(other.email_type, EmailType::None));
637    }
638}