Check-in [c996f5c871]
Logged in as anonymous
Overview
Comment:minor typos, add back tests, change formatting a lot
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: c996f5c87175fa5c6c83ad67a20742f6bc0f43bbcf5759fbed08fcbba9a31200
User & Date: arcade on 2026-01-12 13:41:17.558
Other Links: manifest | tags
Context
2026-01-12
14:46
fix typo, fix comments, drop unneeded constant check-in: 6ce625a569 user: arcade tags: trunk
13:41
minor typos, add back tests, change formatting a lot check-in: c996f5c871 user: arcade tags: trunk
13:39
add action for pull requests, ass sccache check-in: 4448c56dd2 user: arcade tags: trunk
Changes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1
2
3
4
5
6
7
8
9
10
11

12
13
14
15
16
17
18











-







use crate::{
	Cursor,
	telegram::TelegramTransport,
	utils::{
		Attachment,
		RE_DOMAIN,
		validate,
	},
};

use std::{
	borrow::Cow,
	collections::{
		HashMap,
		HashSet,
	},
	io::Error,
	sync::Arc,
};
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
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







-
+
-
-
-
-











-
+












-
-
+
+
+
+

-

-
+




-
-
+







			}
			if self.fields.contains("date")
				&& let Some(date) = mail.date()
			{
				reply.push(format!("<u><i>Date:</i></u> <code>{date}</code>"));
			}
			reply.push("</blockquote><pre>".into());
			//let header_size = reply.join(" ").len();
			let reply = reply.join("\n");
			let mut header_size = 0;
			for i in reply.iter() {
				header_size += i.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 {
				self.tg.debug(&format!("Hm, we have {html_parts} HTML parts and {text_parts} text parts.")).await?;
			}
			//let mut html_num = 0;
			let mut text_num = 0;
			let mut file_num = 0;
			// let's display first html or text part as body
			let mut body: Cow<'_, str> = "".into();
			let mut body: String = "".into();
			/*
			 * actually I don't wanna parse that html stuff
			if html_parts > 0 {
				let text = mail.body_html(0).stack()?;
				if text.len() < 4096 - header_size {
					body = text;
					html_num = 1;
				}
			};
			*/
			if body.is_empty() && text_parts > 0 {
				let text = mail.body_text(0)
					.context("Failed to extract text from message")?;
				// 7:
					.context("Failed to extract text from message")?
					.replace("\r\n", "\n");
				// 6:
				// - (headers)
				// - (mail text)
				// - 1 trailing newline
				// - 6: </pre>
				if text.len() < 4096 - ( header_size + 7 ) {
				if text.len() < 4096 - ( reply.len() + 7 ) {
					body = text;
					text_num = 1;
				}
			};
			reply.extend(body.lines().map(|x| x.into()));
			reply.push("</pre>".into());
			let msg = format!("{}{}</pre>", reply, validate(&body).stack()?);

			// and let's collect all other attachment parts
			let mut files_to_send = vec![];
			/*
			 * let's just skip html parts for now, they just duplicate text?
			while html_num < html_parts {
				files_to_send.push(mail.html_part(html_num).stack()?);
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
215
216
217
218
219
220
221

222
223
224
225
226
227
228







-







			}
			while file_num < attachments {
				files_to_send.push(mail.attachment(file_num.try_into().stack()?)
					.context("Failed to get file part from message.")?);
				file_num += 1;
			}

			let msg = reply.join("\n");
			for chat in rcpt {
				if !files_to_send.is_empty() {
					let mut files = vec![];
					// let mut first_one = true;
					for chunk in &files_to_send {
						let data: Vec<u8> = chunk.contents().to_vec();
						let mut filename: Option<String> = None;
1
2
3
4
5
6
7



8
9
10
11
12
13
14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17







+
+
+







//! Simple SMTP-to-Telegram gateway. Can parse email and send them as telegram
//! messages to specified chats, generally you specify which email address is
//! available in configuration, everything else is sent to default address.

mod mail;
mod telegram;
mod utils;

#[cfg(test)]
mod tests;

use crate::mail::MailServer;

use async_compat::Compat;
use just_getopt::{
	OptFlags,
	OptSpecs,
48
49
50
51
52
53
54
55

56
57
58
59
60
61
62
51
52
53
54
55
56
57

58
59
60
61
62
63
64
65







-
+







	let mut args = std::env::args();
	args.next();
	let parsed = specs.getopt(args);
	for u in &parsed.unknown {
		println!("Unknown option: {u}");
	}
	if !(parsed.unknown.is_empty()) || parsed.options_first("help").is_some() {
		println!("SMTP2TG v{}, (C) 2024 - 2025\n\n\
		println!("SMTP2TG v{}, (C) 2024 - 2026\n\n\
			\t-h|--help\tDisplay this help\n\
			\t-c|--config …\tSet configuration file location.",
			env!("CARGO_PKG_VERSION"));
		return Ok(());
	};
	let config_file = Path::new(if let Some(path) = parsed.options_value_last("config") {
		&path[..]
1




2
3
4
5
6
7
8

1
2
3
4
5
6
7
8
9
10
11
-
+
+
+
+







use crate::utils::Attachment;
use crate::utils::{
	Attachment,
	validate,
};

use std::{
	collections::HashMap,
	fmt::Debug,
};

use stacked_errors::{
53
54
55
56
57
58
59
60

61
62
63
64
65
66
67
56
57
58
59
60
61
62

63
64
65
66
67
68
69
70







-
+







			recipients,
			default,
		})
	}

	/// Send message to default user, used for debug/log/info purposes
	pub async fn debug (&self, msg: &str) -> Result<Message> {
		self.send(&self.default, format!("<pre>{msg}</pre>")).await
		self.send(&self.default, format!("<pre>{}</pre>", validate(msg).stack()?)).await
	}

	/// Get recipient by address
	pub fn get (&self, name: &str) -> Result<&ChatPeerId> {
		self.recipients.get(name)
			.with_context(|| format!("Recipient \"{name}\" not found in configuration"))
	}
Added src/tests.rs version [4300f88482].
21
22
23
24
25
26
27
28
29
30

31
21
22
23
24
25
26
27


28
29
30







-
-

+

}

/// Pass any text here to be validated as HTML, breaks on validation errors
pub fn validate (text: &str) -> Result<&str> {
	let fragment = Html::parse_fragment(text);
	if !fragment.errors.is_empty() {
		bail!(fragment.errors.join("\n"));
	} else {
		Ok(text)
	}
	Ok(text)
}