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
use crate::channel::{ClosedChannel, Sender};
use crate::interface::InputConnector;
use crate::message::Message;
use async_trait::async_trait;
use imap::{error::Error, Session};
use mailparse::{DispositionType, MailHeaderMap, ParsedMail};
use native_tls::{TlsConnector, TlsStream};
use std::collections::HashMap;
use std::io::{Read, Write};
use std::net::TcpStream;
use std::time::Duration;
#[derive(Default, Clone)]
pub struct ImapClient {
imap_domain: String,
email: String,
password: String,
polling_time: Duration,
}
impl ImapClient {
pub fn domain(mut self, value: impl Into<String>) -> Self {
self.imap_domain = value.into();
self
}
pub fn email(mut self, value: impl Into<String>) -> Self {
self.email = value.into();
self
}
pub fn password(mut self, value: impl Into<String>) -> Self {
self.password = value.into();
self
}
pub fn polling_time(mut self, duration: Duration) -> Self {
self.polling_time = duration;
self
}
fn connect(&self) -> Result<Session<TlsStream<TcpStream>>, Error> {
let tls = TlsConnector::builder().build().unwrap();
let client = imap::connect(
(self.imap_domain.as_str(), 993),
self.imap_domain.as_str(),
&tls,
)?;
client.login(&self.email, &self.password).map_err(|e| e.0)
}
}
#[async_trait]
impl InputConnector for ImapClient {
async fn run(mut self: Box<Self>, sender: Sender) -> Result<(), ClosedChannel> {
tokio::task::spawn_blocking(move || {
let mut session = self.connect().unwrap();
loop {
std::thread::sleep(self.polling_time);
match read_inbox(&mut session) {
Ok(Some(message)) => sender.blocking_send(message)?,
Ok(None) => (),
Err(err) => {
log::warn!("{}", err);
session = match self.connect() {
Ok(session) => {
log::info!("Connection restored");
session
}
Err(err) => {
log::error!("{}", err);
continue;
}
}
}
}
}
})
.await
.unwrap()
}
}
fn read_inbox<T: Read + Write>(session: &mut Session<T>) -> Result<Option<Message>, Error> {
session.select("INBOX")?;
let emails = session.fetch("1", "RFC822")?;
if let Some(email) = emails.iter().next() {
session.store(format!("{}", email.message), "+FLAGS (\\Deleted)")?;
session.expunge()?;
if let Some(body) = email.body() {
log::trace!(
"Raw email:\n{}",
std::str::from_utf8(body).unwrap_or("No utf8")
);
match mailparse::parse_mail(body) {
Ok(parsed) => return Ok(Some(email_to_message(parsed))),
Err(err) => log::error!("{}", err),
}
}
}
Ok(None)
}
fn email_to_message(email: ParsedMail) -> Message {
let subject = email.headers.get_first_value("Subject").unwrap_or_default();
let mut subject_args = subject.split_whitespace().map(|s| s.to_owned());
let mut body = String::default();
for part in &email.subparts {
if part.ctype.mimetype.starts_with("text/plain") {
body = part.get_body().unwrap_or_default();
}
}
let mut files = HashMap::default();
for part in &email.subparts {
let content_disposition = part.get_content_disposition();
if let DispositionType::Attachment = content_disposition.disposition {
if let Some(filename) = content_disposition.params.get("filename") {
files.insert(filename.into(), part.get_body_raw().unwrap_or_default());
}
}
}
Message {
user: email
.headers
.get_first_value("From")
.map(|from_list| {
mailparse::addrparse(&from_list)
.expect("Have a 'From' email")
.extract_single_info()
.expect("Have at least one 'From' email")
.addr
})
.unwrap_or_default(),
service_name: subject_args.next().unwrap_or_default(),
args: subject_args.collect(),
body,
attached_data: files,
}
}