Diff
Logged in as anonymous

Differences From Artifact [a10b8e47fa]:

To Artifact [64a6b71a91]:


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
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







+


















+
+
+







#[derive(Clone)]
struct TelegramTransport {
	data: Vec<u8>,
	headers: Option<SomeHeaders>,
	recipients: HashMap<String, ChatId>,
	relay: bool,
	tg: teloxide::adaptors::DefaultParseMode<teloxide::adaptors::Throttle<Bot>>,
	fields: HashSet<String>,
}

impl TelegramTransport {
	/// Initialize API and read configuration
	fn new(settings: config::Config) -> TelegramTransport {
		let tg = Bot::new(settings.get_string("api_key")
			.expect("[smtp2tg.toml] missing \"api_key\" parameter.\n"))
			.throttle(teloxide::adaptors::throttle::Limits::default())
			.parse_mode(MarkdownV2);
		let recipients: HashMap<String, ChatId> = settings.get_table("recipients")
			.expect("[smtp2tg.toml] missing table \"recipients\".\n")
			.into_iter().map(|(a, b)| (a, ChatId (b.into_int()
				.expect("[smtp2tg.toml] \"recipient\" table values should be integers.\n")
				))).collect();
		if !recipients.contains_key("_") {
			eprintln!("[smtp2tg.toml] \"recipient\" table misses \"default_recipient\".\n");
			panic!("no default recipient");
		}
		let fields = HashSet::<String>::from_iter(settings.get_array("fields")
			.expect("[smtp2tg.toml] \"fields\" should be an array")
			.iter().map(|x| x.clone().into_string().expect("should be strings")));
		let value = settings.get_string("unknown");
		let relay = match value {
			Ok(value) => {
				match value.as_str() {
					"relay" => true,
					"deny" => false,
					_ => {
91
92
93
94
95
96
97

98
99
100
101
102
103
104
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109







+








		TelegramTransport {
			data: vec!(),
			headers: None,
			recipients,
			relay,
			tg,
			fields,
		}
	}

	/// Send message to default user, used for debug/log/info purposes
	async fn debug<'b, S>(&self, msg: S) -> Result<Message>
	where S: Into<String> {
		Ok(self.tg.send_message(*self.recipients.get("_").unwrap(), msg).await?)
136
137
138
139
140
141
142

143
144
145
146
147
148














149
150
151
152
153
154
155
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







+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+







				self.debug("No recipient or envelope address\\.").await?;
				rcpt.insert(self.recipients.get("_")
					.ok_or(anyhow!("Missing default address in recipient table."))?);
			};

			// prepating message header
			let mut reply: Vec<Cow<'_, str>> = vec![];
			if self.fields.contains("subject") {
			if let Some(subject) = mail.subject() {
				reply.push(format!("**Subject:** `{}`", subject).into());
			} else if let Some(thread) = mail.thread_name() {
				reply.push(format!("**Thread:** `{}`", thread).into());
			}
			reply.push(format!("**From:** `{}`", headers.from).into());
				if let Some(subject) = mail.subject() {
					reply.push(format!("**Subject:** `{}`", subject).into());
				} else if let Some(thread) = mail.thread_name() {
					reply.push(format!("**Thread:** `{}`", thread).into());
				}
			}
			if self.fields.contains("from") {
				reply.push(format!("**From:** `{}`", headers.from).into());
			}
			if self.fields.contains("date") {
				if let Some(date) = mail.date() {
					reply.push(format!("**Date:** `{}`", date).into());
				}
			}
			reply.push("".into());
			let header_size = reply.join("\n").len() + 1;

			let html_parts = mail.html_body_count();
			let text_parts = mail.text_body_count();
			let attachments = mail.attachment_count();
			if html_parts != text_parts {
322
323
324
325
326
327
328
329

330

331
332
333
334
335
336
337
336
337
338
339
340
341
342

343
344
345
346
347
348
349
350
351
352







-
+

+







		result
	}
}

#[async_std::main]
async fn main() -> Result<()> {
	let settings: config::Config = config::Config::builder()
		.set_default("listen_on", "0.0.0.0:1025").unwrap()
		.set_default("fields", vec!["date", "from", "subject"]).unwrap()
		.set_default("hostname", "smtp.2.tg").unwrap()
		.set_default("listen_on", "0.0.0.0:1025").unwrap()
		.set_default("unknown", "relay").unwrap()
		.add_source(config::File::with_name("smtp2tg.toml"))
		.build()
		.expect("[smtp2tg.toml] there was an error reading config\n\
			\tplease consult \"smtp2tg.toml.example\" for details");

	let listen_on = settings.get_string("listen_on")?;