Diff
Logged in as anonymous

Differences From Artifact [9c6050fb77]:

To Artifact [1f629cd869]:


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
19
20
21
22
23
24












+
+
+
+
+







//! 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.

use anyhow::{
	anyhow,
	bail,
	Result,
};
use async_std::{
	io::Error,
	task,
};
use just_getopt::{
	OptFlags,
	OptSpecs,
	OptValueType,
};
use mailin_embedded::{
	Response,
	response::*,
};
use teloxide::{
	Bot,
31
32
33
34
35
36
37

38
39
40
41
42
43
44
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50







+








use std::{
	borrow::Cow,
	collections::{
		HashMap,
		HashSet,
	},
	path::Path,
	vec::Vec,
};

/// `SomeHeaders` object to store data through SMTP session
#[derive(Clone, Debug)]
struct SomeHeaders {
	from: String,
337
338
339
340
341
342
343




























344
345
346
347
348
349

350
351
352



353
354
355
356
357
358
359
360
361
362
363
364
365
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382

383
384


385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+





-
+

-
-
+
+
+













		self.headers = None;
		result
	}
}

#[async_std::main]
async fn main() -> Result<()> {
	let specs = OptSpecs::new()
		.option("help", "h", OptValueType::None)
		.option("help", "help", OptValueType::None)
		.option("config", "c", OptValueType::Required)
		.option("config", "config", OptValueType::Required)
		.flag(OptFlags::OptionsEverywhere);
	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\
			\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[..]
	} else {
		"smtp2tg.toml"
	});
	if !config_file.exists() {
		eprintln!("Error: can't read configuration from {:?}", config_file);
		std::process::exit(1);
	};
	let settings: config::Config = config::Config::builder()
		.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"))
		.add_source(config::File::from(config_file))
		.build()
		.expect("[smtp2tg.toml] there was an error reading config\n\
			\tplease consult \"smtp2tg.toml.example\" for details");
		.expect(&format!("[{:?}] there was an error reading config\n\
			\tplease consult \"smtp2tg.toml.example\" for details",
			config_file)[..]);

	let listen_on = settings.get_string("listen_on")?;
	let server_name = settings.get_string("hostname")?;
	let core = TelegramTransport::new(settings);
	let mut server = mailin_embedded::Server::new(core);

	server.with_name(server_name)
		.with_ssl(mailin_embedded::SslConfig::None).unwrap()
		.with_addr(listen_on).unwrap();
	server.serve().unwrap();

	Ok(())
}