Diff
Logged in as anonymous

Differences From Artifact [152822e4ec]:

To Artifact [1de828453b]:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//! 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::Result;
use async_std::{
	fs::metadata,
	io::Error,
	task,
};
use just_getopt::{
	OptFlags,
	OptSpecs,
	OptValueType,
};
use lazy_static::lazy_static;
use mailin_embedded::{
	Response,
	response::*,
};
use regex::Regex;













|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//! 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::Result;
use async_std::{
	fs::metadata,
	io::Error,
	task,
};
use just_getopt::{
	OptFlags,
	OptSpecs,
	OptValue,
};
use lazy_static::lazy_static;
use mailin_embedded::{
	Response,
	response::*,
};
use regex::Regex;
55
56
57
58
59
60
61


62
63
64
65
66
67
68
	NoHeaders,
	#[error("No recipient addresses")]
	NoRecipient,
	#[error("Failed to extract text from message")]
	NoText,
	#[error(transparent)]
	RequestError(#[from] teloxide::RequestError),


}

/// `SomeHeaders` object to store data through SMTP session
#[derive(Clone, Debug)]
struct SomeHeaders {
	from: String,
	to: Vec<String>,







>
>







55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
	NoHeaders,
	#[error("No recipient addresses")]
	NoRecipient,
	#[error("Failed to extract text from message")]
	NoText,
	#[error(transparent)]
	RequestError(#[from] teloxide::RequestError),
	#[error(transparent)]
	TryFromIntError(#[from] std::num::TryFromIntError),
}

/// `SomeHeaders` object to store data through SMTP session
#[derive(Clone, Debug)]
struct SomeHeaders {
	from: String,
	to: Vec<String>,
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
			if html_parts != text_parts {
				self.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 = "".into();
			/*
			 * actually I don't wanna parse that html stuff
			if html_parts > 0 {
				let text = mail.body_html(0).unwrap();
				if text.len() < 4096 - header_size {
					body = text;
					html_num = 1;
				}
			};
			*/
			if body == "" && text_parts > 0 {
				let text = mail.body_text(0)
					.ok_or(MyError::NoText)?;
				if text.len() < 4096 - header_size {
					body = text;
					text_num = 1;
				}
			};







|










|







215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
			if html_parts != text_parts {
				self.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();
			/*
			 * actually I don't wanna parse that html stuff
			if html_parts > 0 {
				let text = mail.body_html(0).unwrap();
				if text.len() < 4096 - header_size {
					body = text;
					html_num = 1;
				}
			};
			*/
			if body.is_empty() && text_parts > 0 {
				let text = mail.body_text(0)
					.ok_or(MyError::NoText)?;
				if text.len() < 4096 - header_size {
					body = text;
					text_num = 1;
				}
			};
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
			 * 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).unwrap());
				html_num += 1;
			}
			*/
			while text_num < text_parts {
				files_to_send.push(mail.text_part(text_num)
					.ok_or(MyError::NoText)?);
				text_num += 1;
			}
			while file_num < attachments {
				files_to_send.push(mail.attachment(file_num)
					.ok_or(MyError::NoText)?);
				file_num += 1;
			}

			let msg = reply.join("\n");
			for chat in rcpt {
				if !files_to_send.is_empty() {







|




|







248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
			 * 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).unwrap());
				html_num += 1;
			}
			*/
			while text_num < text_parts {
				files_to_send.push(mail.text_part(text_num.try_into()?)
					.ok_or(MyError::NoText)?);
				text_num += 1;
			}
			while file_num < attachments {
				files_to_send.push(mail.attachment(file_num.try_into()?)
					.ok_or(MyError::NoText)?);
				file_num += 1;
			}

			let msg = reply.join("\n");
			for chat in rcpt {
				if !files_to_send.is_empty() {
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
		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}");
	}







|
|
|
|







382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
		result
	}
}

#[async_std::main]
async fn main () -> Result<()> {
	let specs = OptSpecs::new()
		.option("help", "h", OptValue::None)
		.option("help", "help", OptValue::None)
		.option("config", "c", OptValue::Required)
		.option("config", "config", OptValue::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}");
	}