1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
use std::io::{Read, Write};
use std::net::TcpStream;
use json::{JsonValue, object};
use crate::tools::{string, tools};

pub struct Pop3 {
    pop3_host: String,
    pop3_port: String,
    send_email: String,
    send_pass: String,
}

impl Pop3 {
    pub fn connect(config: JsonValue) -> Self {
        let pop3_host = config["pop3_host"].to_string();
        let pop3_port = config["pop3_port"].to_string();
        let send_email = config["send_email"].to_string();
        let send_pass = config["send_pass"].to_string();

        Self {
            pop3_host,
            pop3_port,
            send_email,
            send_pass,
        }
    }
    pub fn client(&mut self) {
        let mut stream = TcpStream::connect((self.pop3_host.as_str(), self.pop3_port.parse::<u16>().unwrap())).unwrap();

        let mut buffer = [0; 1024];
        stream.read(&mut buffer).unwrap();
        let text = std::str::from_utf8(&buffer).unwrap().trim_matches('\u{0}');
        println!("{}", text);

        stream.write(format!("USER {}\r\n", self.send_email).as_bytes()).unwrap();
        let mut buffer = [0; 1024 * 2];
        stream.read(&mut buffer).unwrap();
        let text = std::str::from_utf8(&buffer).unwrap().trim_matches('\u{0}');
        println!("{}", text);

        stream.write(format!("PASS {}\r\n", self.send_pass).as_bytes()).unwrap();
        let mut buffer = [0; 1024];
        stream.read(&mut buffer).unwrap();
        let text = std::str::from_utf8(&buffer).unwrap().trim_matches('\u{0}');
        println!("{}", text);

        stream.write(format!("STAT \r\n").as_bytes()).unwrap();
        let mut buffer = [0; 1024];
        stream.read(&mut buffer).unwrap();
        let text = std::str::from_utf8(&buffer).unwrap().trim_matches('\u{0}');
        println!(">>>{}", text);

        // stream.write(format!("LIST \r\n").as_bytes()).unwrap();
        // let mut buffer = [0; 1024 * 100];
        // stream.read(&mut buffer).unwrap();
        // let text = std::str::from_utf8(&buffer).unwrap().trim_matches('\u{0}');
        // println!("{}", text);

        // stream.write(format!("UIDL \r\n").as_bytes()).unwrap();
        // let mut buffer = [0; 1024];
        // let data = stream.read(&mut buffer).unwrap();
        // let text = std::str::from_utf8(&buffer).unwrap().trim_matches('\u{0}');
        // println!("{}", text);

        //10600
        //10635
        stream.write(format!("RETR {}\r\n", 10600).as_bytes()).unwrap();
        let mut buffer = [0; 1024 * 1024];
        stream.read(&mut buffer).unwrap();
        let text = std::str::from_utf8(&buffer).unwrap().trim_matches('\u{0}');
        let data = self._qq(text.to_string());
        println!("{:#}", data);
        // let lst1 = list[0].to_string()[4..list[0].to_string().len()].to_string();
        // println!("{:#}", lst1);
        // println!("{:#}", list);

        // println!("=======================================");
        // stream.write(format!("QUIT \r\n").as_bytes()).unwrap();
        // let mut buffer = [0; 1024];
        // let data = stream.read(&mut buffer).unwrap();
        // let text = std::str::from_utf8(&buffer).unwrap().trim_matches('\u{0}');
        // println!("{}", text);
    }
    fn _qq(&mut self, data: String) -> JsonValue {
        let data = string::split(data.clone(), "\r\n\r\n");
        let heads = data[0].clone();
        let mut body = String::new();
        for index in 1..data.len() {
            body = format!("{}{}", body.clone(), data[index]);
        }
        let body = string::split(body.clone(), "\r\n");
        let body = body[0].to_string();

        let mut list = string::split(heads.to_string().clone(), "\r\n");
        let size = list[0].to_string()[4..list[0].to_string().len()].to_string();
        let mut subject = String::new();
        let mut from_name = String::new();
        let mut from_mail = String::new();
        let mut sender = String::new();
        let mut encoding = String::new();

        let mut to_name = String::new();
        let mut to_mail = String::new();
        let mut reply_to_name = String::new();
        let mut reply_to_mail = String::new();
        let mut date = String::new();
        let mut content_type = String::new();

        for item in list.members_mut() {
            let text = item.to_string();

            let ttt = string::search(text.as_str(), "Content-Type:");
            if ttt {
                let ttt = string::split(text.clone(), ": ");
                let content_types = ttt[1].to_string();
                let ttt = string::search(content_types.as_str(), "multipart/alternative");
                if ttt {
                    content_type = "multipart/alternative".to_string();
                } else {
                    content_type = "base64".to_string();
                }
            }

            let ttt = string::search(text.as_str(), "Subject: ");
            if ttt {
                let ttt = string::split(text.clone(), ": ");
                subject = ttt[1].to_string();
                let ttt = string::search(text.as_str(), "b?");
                if ttt {
                    let ttt = string::split(subject.clone(), "?");
                    subject = ttt[3].to_string();
                    subject = tools::base64_decode(subject.to_string()).clone();
                }
            }

            if string::search(text.as_str().to_lowercase().as_str(), "sender: ") {
                let ttt = string::split(text.clone(), ": ");
                sender = ttt[1].to_string();
            }

            if string::search(text.as_str().to_lowercase().as_str(), "from: ") {
                let (name, mail) = self._from(text.clone());
                from_name = name.clone();
                from_mail = mail.clone();
            }

            if string::search(text.as_str().to_lowercase().as_str(), "to: ") {
                let (name, mail) = self._to(text.clone());
                to_name = name.clone();
                to_mail = mail.clone();
            }

            if string::search(text.as_str().to_lowercase().as_str(), "reply-to: ") {
                let (name, mail) = self._to(text.clone());
                reply_to_name = name.clone();
                reply_to_mail = mail.clone();
            }

            if string::search(text.as_str().to_lowercase().as_str(), "date: ") {
                let ttt = string::split(text.clone(), ": ");
                date = ttt[1].to_string();
            }

            if string::search(text.as_str().to_lowercase().as_str(), "Content-Transfer-Encoding: ") {
                let ttt = string::split(text.clone(), ": ");
                encoding = ttt[1].to_string();
            }
        }

        let info = object! {
            size:size.parse::<u32>().unwrap().clone(),
            subject:subject,
            from_name:from_name.clone(),
            from_mail:from_mail.clone(),
            to_name:to_name.clone(),
            to_mail:to_mail.clone(),
            reply_to_name:reply_to_name.clone(),
            reply_to_mail:reply_to_mail.clone(),
            date:date.clone(),
            body:tools::base64_decode(body.to_string()).clone().clone(),
            content_type:content_type.clone(),
            sender:sender.clone(),
            encoding:encoding.clone()
        };
        println!("{:#}", list);

        return info;
    }
    /// 来自地址
    fn _from(&mut self, data: String) -> (String, String) {
        let list = string::split(data.clone(), ": ");
        let mail = list[1].to_string();
        if string::search(mail.as_str(), "<") {
            let list = string::split(mail.clone(), "<");
            let from_name = list[0].to_string().trim().to_string();
            let from_email = string::trim_end(list[1].as_str().unwrap(), ">");
            (from_name, from_email)
        } else {
            ("".to_string(), mail.to_string())
        }
    }
    /// 收件人
    fn _to(&mut self, data: String) -> (String, String) {
        let list = string::split(data.clone(), ": ");
        let mail = list[1].to_string();
        if string::search(mail.as_str(), "<") {
            let list = string::split(mail.clone(), "<");
            let mut name = list[0].to_string().trim().to_string();
            let email = string::trim_end(list[1].as_str().unwrap(), ">");
            if string::search(name.as_str(), "=?") && string::search(name.as_str(), "?=") {
                let ttt = string::split(name.clone(), "?");
                name = ttt[3].to_string();
                name = tools::base64_decode(name).clone();
            }
            (name, email)
        } else {
            ("".to_string(), mail.to_string())
        }
    }
}