0000: 2f 2f 21 20 53 4d 54 50 20 73 65 72 76 65 72 20 //! SMTP server
0010: 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 implementation f
0020: 6f 72 20 72 65 63 65 69 76 69 6e 67 20 61 6e 64 or receiving and
0030: 20 70 72 6f 63 65 73 73 69 6e 67 20 65 6d 61 69 processing emai
0040: 6c 73 2e 0a 2f 2f 21 0a 2f 2f 21 20 54 68 69 73 ls..//!.//! This
0050: 20 6d 6f 64 75 6c 65 20 68 61 6e 64 6c 65 73 20 module handles
0060: 53 4d 54 50 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 SMTP connections
0070: 2c 20 65 6d 61 69 6c 20 70 61 72 73 69 6e 67 2c , email parsing,
0080: 20 61 6e 64 20 66 6f 72 77 61 72 64 69 6e 67 20 and forwarding
0090: 74 6f 0a 2f 2f 21 20 54 65 6c 65 67 72 61 6d 2e to.//! Telegram.
00a0: 0a 0a 75 73 65 20 63 72 61 74 65 3a 3a 7b 0a 09 ..use crate::{..
00b0: 43 75 72 73 6f 72 2c 0a 09 74 65 6c 65 67 72 61 Cursor,..telegra
00c0: 6d 3a 3a 54 65 6c 65 67 72 61 6d 54 72 61 6e 73 m::TelegramTrans
00d0: 70 6f 72 74 2c 0a 09 75 74 69 6c 73 3a 3a 7b 0a port,..utils::{.
00e0: 09 09 41 74 74 61 63 68 6d 65 6e 74 2c 0a 09 09 ..Attachment,...
00f0: 52 45 5f 44 4f 4d 41 49 4e 2c 0a 09 09 76 61 6c RE_DOMAIN,...val
0100: 69 64 61 74 65 2c 0a 09 7d 2c 0a 7d 3b 0a 0a 75 idate,..},.};..u
0110: 73 65 20 73 74 64 3a 3a 7b 0a 09 63 6f 6c 6c 65 se std::{..colle
0120: 63 74 69 6f 6e 73 3a 3a 7b 0a 09 09 48 61 73 68 ctions::{...Hash
0130: 4d 61 70 2c 0a 09 09 48 61 73 68 53 65 74 2c 0a Map,...HashSet,.
0140: 09 7d 2c 0a 09 69 6f 3a 3a 45 72 72 6f 72 2c 0a .},..io::Error,.
0150: 09 73 79 6e 63 3a 3a 41 72 63 2c 0a 7d 3b 0a 0a .sync::Arc,.};..
0160: 75 73 65 20 61 73 79 6e 63 5f 63 6f 6d 70 61 74 use async_compat
0170: 3a 3a 43 6f 6d 70 61 74 3b 0a 75 73 65 20 6d 61 ::Compat;.use ma
0180: 69 6c 69 6e 5f 65 6d 62 65 64 64 65 64 3a 3a 7b ilin_embedded::{
0190: 0a 09 52 65 73 70 6f 6e 73 65 2c 0a 09 72 65 73 ..Response,..res
01a0: 70 6f 6e 73 65 3a 3a 7b 0a 09 09 49 4e 54 45 52 ponse::{...INTER
01b0: 4e 41 4c 5f 45 52 52 4f 52 2c 0a 09 09 49 4e 56 NAL_ERROR,...INV
01c0: 41 4c 49 44 5f 43 52 45 44 45 4e 54 49 41 4c 53 ALID_CREDENTIALS
01d0: 2c 0a 09 09 4e 4f 5f 4d 41 49 4c 42 4f 58 2c 0a ,...NO_MAILBOX,.
01e0: 09 09 4f 4b 0a 09 7d 2c 0a 7d 3b 0a 75 73 65 20 ..OK..},.};.use
01f0: 72 65 67 65 78 3a 3a 7b 0a 09 52 65 67 65 78 2c regex::{..Regex,
0200: 0a 09 52 65 67 65 78 42 75 69 6c 64 65 72 2c 0a ..RegexBuilder,.
0210: 09 65 73 63 61 70 65 2c 0a 7d 3b 0a 75 73 65 20 .escape,.};.use
0220: 73 74 61 63 6b 65 64 5f 65 72 72 6f 72 73 3a 3a stacked_errors::
0230: 7b 0a 09 52 65 73 75 6c 74 2c 0a 09 53 74 61 63 {..Result,..Stac
0240: 6b 61 62 6c 65 45 72 72 2c 0a 09 62 61 69 6c 2c kableErr,..bail,
0250: 0a 7d 3b 0a 75 73 65 20 74 67 62 6f 74 3a 3a 74 .};.use tgbot::t
0260: 79 70 65 73 3a 3a 43 68 61 74 50 65 65 72 49 64 ypes::ChatPeerId
0270: 3b 0a 0a 2f 2f 2f 20 60 53 6f 6d 65 48 65 61 64 ;../// `SomeHead
0280: 65 72 73 60 20 6f 62 6a 65 63 74 20 74 6f 20 73 ers` object to s
0290: 74 6f 72 65 20 64 61 74 61 20 74 68 72 6f 75 67 tore data throug
02a0: 68 20 53 4d 54 50 20 73 65 73 73 69 6f 6e 0a 23 h SMTP session.#
02b0: 5b 64 65 72 69 76 65 28 43 6c 6f 6e 65 2c 20 44 [derive(Clone, D
02c0: 65 62 75 67 29 5d 0a 73 74 72 75 63 74 20 53 6f ebug)].struct So
02d0: 6d 65 48 65 61 64 65 72 73 20 7b 0a 09 66 72 6f meHeaders {..fro
02e0: 6d 3a 20 53 74 72 69 6e 67 2c 0a 09 74 6f 3a 20 m: String,..to:
02f0: 56 65 63 3c 53 74 72 69 6e 67 3e 2c 0a 7d 0a 0a Vec<String>,.}..
0300: 2f 2f 2f 20 60 4d 61 69 6c 53 65 72 76 65 72 60 /// `MailServer`
0310: 20 43 65 6e 74 72 61 6c 20 6f 62 6a 65 63 74 20 Central object
0320: 77 69 74 68 20 54 47 20 61 70 69 20 61 6e 64 20 with TG api and
0330: 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 0a 23 5b configuration.#[
0340: 64 65 72 69 76 65 28 43 6c 6f 6e 65 2c 20 44 65 derive(Clone, De
0350: 62 75 67 29 5d 0a 70 75 62 20 73 74 72 75 63 74 bug)].pub struct
0360: 20 4d 61 69 6c 53 65 72 76 65 72 20 7b 0a 09 64 MailServer {..d
0370: 61 74 61 3a 20 56 65 63 3c 75 38 3e 2c 0a 09 68 ata: Vec<u8>,..h
0380: 65 61 64 65 72 73 3a 20 4f 70 74 69 6f 6e 3c 53 eaders: Option<S
0390: 6f 6d 65 48 65 61 64 65 72 73 3e 2c 0a 09 74 67 omeHeaders>,..tg
03a0: 3a 20 41 72 63 3c 54 65 6c 65 67 72 61 6d 54 72 : Arc<TelegramTr
03b0: 61 6e 73 70 6f 72 74 3e 2c 0a 09 66 69 65 6c 64 ansport>,..field
03c0: 73 3a 20 48 61 73 68 53 65 74 3c 53 74 72 69 6e s: HashSet<Strin
03d0: 67 3e 2c 0a 09 61 64 64 72 65 73 73 3a 20 52 65 g>,..address: Re
03e0: 67 65 78 2c 0a 7d 0a 0a 69 6d 70 6c 20 4d 61 69 gex,.}..impl Mai
03f0: 6c 53 65 72 76 65 72 20 7b 0a 09 2f 2f 2f 20 49 lServer {../// I
0400: 6e 69 74 69 61 6c 69 7a 65 73 20 74 68 65 20 6d nitializes the m
0410: 61 69 6c 20 73 65 72 76 65 72 3a 20 73 65 74 73 ail server: sets
0420: 20 75 70 20 74 68 65 20 54 65 6c 65 67 72 61 6d up the Telegram
0430: 20 41 50 49 20 63 6c 69 65 6e 74 20 61 6e 64 0a API client and.
0440: 09 2f 2f 2f 20 76 61 6c 69 64 61 74 65 73 20 61 ./// validates a
0450: 6c 6c 20 72 65 71 75 69 72 65 64 20 63 6f 6e 66 ll required conf
0460: 69 67 75 72 61 74 69 6f 6e 20 76 61 6c 75 65 73 iguration values
0470: 2e 0a 09 2f 2f 2f 0a 09 2f 2f 2f 20 23 20 41 72 ...///../// # Ar
0480: 67 75 6d 65 6e 74 73 0a 09 2f 2f 2f 20 2a 20 60 guments../// * `
0490: 73 65 74 74 69 6e 67 73 60 20 2d 20 50 61 72 73 settings` - Pars
04a0: 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 63 ed application c
04b0: 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2e 0a 09 2f onfiguration.../
04c0: 2f 2f 0a 09 2f 2f 2f 20 23 20 45 72 72 6f 72 73 //../// # Errors
04d0: 0a 09 2f 2f 2f 20 52 65 74 75 72 6e 73 20 61 6e ../// Returns an
04e0: 20 65 72 72 6f 72 20 69 66 20 72 65 71 75 69 72 error if requir
04f0: 65 64 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e ed configuration
0500: 20 76 61 6c 75 65 73 20 61 72 65 20 6d 69 73 73 values are miss
0510: 69 6e 67 20 6f 72 20 69 6e 76 61 6c 69 64 2c 0a ing or invalid,.
0520: 09 2f 2f 2f 20 54 65 6c 65 67 72 61 6d 20 41 50 ./// Telegram AP
0530: 49 20 63 6c 69 65 6e 74 20 63 72 65 61 74 69 6f I client creatio
0540: 6e 20 66 61 69 6c 73 2c 20 6f 72 20 74 68 65 20 n fails, or the
0550: 61 64 64 72 65 73 73 20 72 65 67 65 78 20 63 61 address regex ca
0560: 6e 6e 6f 74 20 62 65 20 62 75 69 6c 74 2e 0a 09 nnot be built...
0570: 70 75 62 20 66 6e 20 6e 65 77 20 28 73 65 74 74 pub fn new (sett
0580: 69 6e 67 73 3a 20 63 6f 6e 66 69 67 3a 3a 43 6f ings: config::Co
0590: 6e 66 69 67 29 20 2d 3e 20 52 65 73 75 6c 74 3c nfig) -> Result<
05a0: 4d 61 69 6c 53 65 72 76 65 72 3e 20 7b 0a 09 09 MailServer> {...
05b0: 6c 65 74 20 61 70 69 5f 6b 65 79 20 3d 20 73 65 let api_key = se
05c0: 74 74 69 6e 67 73 2e 67 65 74 5f 73 74 72 69 6e ttings.get_strin
05d0: 67 28 22 61 70 69 5f 6b 65 79 22 29 0a 09 09 09 g("api_key")....
05e0: 2e 63 6f 6e 74 65 78 74 28 22 5b 73 6d 74 70 32 .context("[smtp2
05f0: 74 67 2e 74 6f 6d 6c 5d 20 6d 69 73 73 69 6e 67 tg.toml] missing
0600: 20 5c 22 61 70 69 5f 6b 65 79 5c 22 20 70 61 72 \"api_key\" par
0610: 61 6d 65 74 65 72 2e 5c 6e 22 29 3f 3b 0a 09 09 ameter.\n")?;...
0620: 6c 65 74 20 6d 75 74 20 72 65 63 69 70 69 65 6e let mut recipien
0630: 74 73 20 3d 20 48 61 73 68 4d 61 70 3a 3a 6e 65 ts = HashMap::ne
0640: 77 28 29 3b 0a 09 09 66 6f 72 20 28 6e 61 6d 65 w();...for (name
0650: 2c 20 76 61 6c 75 65 29 20 69 6e 20 73 65 74 74 , value) in sett
0660: 69 6e 67 73 2e 67 65 74 5f 74 61 62 6c 65 28 22 ings.get_table("
0670: 72 65 63 69 70 69 65 6e 74 73 22 29 0a 09 09 09 recipients")....
0680: 2e 63 6f 6e 74 65 78 74 28 22 5b 73 6d 74 70 32 .context("[smtp2
0690: 74 67 2e 74 6f 6d 6c 5d 20 6d 69 73 73 69 6e 67 tg.toml] missing
06a0: 20 74 61 62 6c 65 20 5c 22 72 65 63 69 70 69 65 table \"recipie
06b0: 6e 74 73 5c 22 2e 5c 6e 22 29 3f 0a 09 09 7b 0a nts\".\n")?...{.
06c0: 09 09 09 6c 65 74 20 76 61 6c 75 65 20 3d 20 76 ...let value = v
06d0: 61 6c 75 65 2e 69 6e 74 6f 5f 69 6e 74 28 29 0a alue.into_int().
06e0: 09 09 09 09 2e 63 6f 6e 74 65 78 74 28 22 5b 73 .....context("[s
06f0: 6d 74 70 32 74 67 2e 74 6f 6d 6c 5d 20 5c 22 72 mtp2tg.toml] \"r
0700: 65 63 69 70 69 65 6e 74 5c 22 20 74 61 62 6c 65 ecipient\" table
0710: 20 76 61 6c 75 65 73 20 73 68 6f 75 6c 64 20 62 values should b
0720: 65 20 69 6e 74 65 67 65 72 73 2e 5c 6e 22 29 3f e integers.\n")?
0730: 3b 0a 09 09 09 72 65 63 69 70 69 65 6e 74 73 2e ;....recipients.
0740: 69 6e 73 65 72 74 28 6e 61 6d 65 2e 74 6f 5f 6c insert(name.to_l
0750: 6f 77 65 72 63 61 73 65 28 29 2c 20 76 61 6c 75 owercase(), valu
0760: 65 29 3b 0a 09 09 7d 0a 0a 09 09 6c 65 74 20 74 e);...}....let t
0770: 67 20 3d 20 41 72 63 3a 3a 6e 65 77 28 54 65 6c g = Arc::new(Tel
0780: 65 67 72 61 6d 54 72 61 6e 73 70 6f 72 74 3a 3a egramTransport::
0790: 6e 65 77 28 61 70 69 5f 6b 65 79 2c 20 72 65 63 new(api_key, rec
07a0: 69 70 69 65 6e 74 73 2c 20 26 73 65 74 74 69 6e ipients, &settin
07b0: 67 73 29 3f 29 3b 0a 09 09 6c 65 74 20 66 69 65 gs)?);...let fie
07c0: 6c 64 73 20 3d 20 48 61 73 68 53 65 74 3a 3a 3c lds = HashSet::<
07d0: 53 74 72 69 6e 67 3e 3a 3a 66 72 6f 6d 5f 69 74 String>::from_it
07e0: 65 72 28 73 65 74 74 69 6e 67 73 2e 67 65 74 5f er(settings.get_
07f0: 61 72 72 61 79 28 22 66 69 65 6c 64 73 22 29 0a array("fields").
0800: 09 09 09 2e 63 6f 6e 74 65 78 74 28 22 5b 73 6d ....context("[sm
0810: 74 70 32 74 67 2e 74 6f 6d 6c 5d 20 5c 22 66 69 tp2tg.toml] \"fi
0820: 65 6c 64 73 5c 22 20 73 68 6f 75 6c 64 20 62 65 elds\" should be
0830: 20 61 6e 20 61 72 72 61 79 22 29 3f 0a 09 09 09 an array")?....
0840: 2e 69 74 65 72 28 29 2e 6d 61 70 28 7c 78 7c 20 .iter().map(|x|
0850: 78 2e 63 6c 6f 6e 65 28 29 2e 69 6e 74 6f 5f 73 x.clone().into_s
0860: 74 72 69 6e 67 28 29 2e 63 6f 6e 74 65 78 74 28 tring().context(
0870: 22 73 68 6f 75 6c 64 20 62 65 20 73 74 72 69 6e "should be strin
0880: 67 73 22 29 29 0a 09 09 09 2e 63 6f 6c 6c 65 63 gs")).....collec
0890: 74 3a 3a 3c 52 65 73 75 6c 74 3c 56 65 63 3c 53 t::<Result<Vec<S
08a0: 74 72 69 6e 67 3e 3e 3e 28 29 3f 29 3b 0a 09 09 tring>>>()?);...
08b0: 6c 65 74 20 6d 75 74 20 64 6f 6d 61 69 6e 73 3a let mut domains:
08c0: 20 48 61 73 68 53 65 74 3c 53 74 72 69 6e 67 3e HashSet<String>
08d0: 20 3d 20 48 61 73 68 53 65 74 3a 3a 6e 65 77 28 = HashSet::new(
08e0: 29 3b 0a 09 09 6c 65 74 20 65 78 74 72 61 5f 64 );...let extra_d
08f0: 6f 6d 61 69 6e 73 20 3d 20 73 65 74 74 69 6e 67 omains = setting
0900: 73 2e 67 65 74 5f 61 72 72 61 79 28 22 64 6f 6d s.get_array("dom
0910: 61 69 6e 73 22 29 2e 73 74 61 63 6b 28 29 3f 3b ains").stack()?;
0920: 0a 09 09 66 6f 72 20 64 6f 6d 61 69 6e 20 69 6e ...for domain in
0930: 20 65 78 74 72 61 5f 64 6f 6d 61 69 6e 73 20 7b extra_domains {
0940: 0a 09 09 09 6c 65 74 20 64 6f 6d 61 69 6e 20 3d ....let domain =
0950: 20 64 6f 6d 61 69 6e 2e 74 6f 5f 73 74 72 69 6e domain.to_strin
0960: 67 28 29 2e 74 6f 5f 6c 6f 77 65 72 63 61 73 65 g().to_lowercase
0970: 28 29 3b 0a 09 09 09 69 66 20 52 45 5f 44 4f 4d ();....if RE_DOM
0980: 41 49 4e 2e 69 73 5f 6d 61 74 63 68 28 26 64 6f AIN.is_match(&do
0990: 6d 61 69 6e 29 20 7b 0a 09 09 09 09 64 6f 6d 61 main) {.....doma
09a0: 69 6e 73 2e 69 6e 73 65 72 74 28 64 6f 6d 61 69 ins.insert(domai
09b0: 6e 29 3b 0a 09 09 09 7d 20 65 6c 73 65 20 7b 0a n);....} else {.
09c0: 09 09 09 09 62 61 69 6c 21 28 22 49 6e 76 61 6c ....bail!("Inval
09d0: 69 64 20 64 6f 6d 61 69 6e 20 69 6e 20 63 6f 6e id domain in con
09e0: 66 69 67 75 72 61 74 69 6f 6e 3a 20 27 7b 64 6f figuration: '{do
09f0: 6d 61 69 6e 7d 27 5c 6e 5c 0a 09 09 09 09 09 44 main}'\n\......D
0a00: 6f 6d 61 69 6e 73 20 6d 75 73 74 20 62 65 20 76 omains must be v
0a10: 61 6c 69 64 20 28 65 2e 67 2e 2c 20 27 65 78 61 alid (e.g., 'exa
0a20: 6d 70 6c 65 2e 63 6f 6d 27 2c 20 27 6c 6f 63 61 mple.com', 'loca
0a30: 6c 68 6f 73 74 27 29 2e 5c 6e 5c 0a 09 09 09 09 lhost').\n\.....
0a40: 09 43 68 65 63 6b 20 27 64 6f 6d 61 69 6e 73 27 .Check 'domains'
0a50: 20 61 72 72 61 79 20 69 6e 20 73 6d 74 70 32 74 array in smtp2t
0a60: 67 2e 74 6f 6d 6c 2e 22 29 3b 0a 09 09 7d 09 7d g.toml.");...}.}
0a70: 0a 09 09 69 66 20 64 6f 6d 61 69 6e 73 2e 69 73 ...if domains.is
0a80: 5f 65 6d 70 74 79 28 29 20 7b 0a 09 09 09 62 61 _empty() {....ba
0a90: 69 6c 21 28 22 4e 6f 20 64 6f 6d 61 69 6e 73 2c il!("No domains,
0aa0: 20 6e 65 65 64 20 61 74 20 6c 65 61 73 74 20 6f need at least o
0ab0: 6e 65 3a 20 64 65 66 61 75 6c 74 20 60 6c 6f 63 ne: default `loc
0ac0: 61 6c 68 6f 73 74 60 20 77 6f 75 6c 64 20 64 6f alhost` would do
0ad0: 2e 22 29 3b 0a 09 09 7d 0a 09 09 6c 65 74 20 72 .");...}...let r
0ae0: 65 5f 64 6f 6d 61 69 6e 73 20 3d 20 64 6f 6d 61 e_domains = doma
0af0: 69 6e 73 2e 69 74 65 72 28 29 2e 6d 61 70 28 7c ins.iter().map(|
0b00: 73 7c 20 65 73 63 61 70 65 28 73 29 29 0a 09 09 s| escape(s))...
0b10: 09 2e 63 6f 6c 6c 65 63 74 3a 3a 3c 56 65 63 3c ..collect::<Vec<
0b20: 53 74 72 69 6e 67 3e 3e 28 29 2e 6a 6f 69 6e 28 String>>().join(
0b30: 22 7c 22 29 3b 0a 09 09 6c 65 74 20 61 64 64 72 "|");...let addr
0b40: 65 73 73 20 3d 20 52 65 67 65 78 42 75 69 6c 64 ess = RegexBuild
0b50: 65 72 3a 3a 6e 65 77 28 26 66 6f 72 6d 61 74 21 er::new(&format!
0b60: 28 22 5e 5b 61 2d 7a 30 2d 39 5d 5b 61 2d 7a 30 ("^[a-z0-9][a-z0
0b70: 2d 39 2e 2d 5d 2a 28 40 28 7b 72 65 5f 64 6f 6d -9.-]*(@({re_dom
0b80: 61 69 6e 73 7d 29 29 3f 24 22 29 29 0a 09 09 09 ains}))?$"))....
0b90: 2e 63 61 73 65 5f 69 6e 73 65 6e 73 69 74 69 76 .case_insensitiv
0ba0: 65 28 74 72 75 65 29 2e 62 75 69 6c 64 28 29 2e e(true).build().
0bb0: 73 74 61 63 6b 28 29 3f 3b 0a 0a 09 09 4f 6b 28 stack()?;....Ok(
0bc0: 4d 61 69 6c 53 65 72 76 65 72 20 7b 0a 09 09 09 MailServer {....
0bd0: 64 61 74 61 3a 20 76 65 63 21 5b 5d 2c 0a 09 09 data: vec![],...
0be0: 09 68 65 61 64 65 72 73 3a 20 4e 6f 6e 65 2c 0a .headers: None,.
0bf0: 09 09 09 74 67 2c 0a 09 09 09 66 69 65 6c 64 73 ...tg,....fields
0c00: 2c 0a 09 09 09 61 64 64 72 65 73 73 2c 0a 09 09 ,....address,...
0c10: 7d 29 0a 09 7d 0a 0a 09 2f 2f 2f 20 52 65 74 72 })..}.../// Retr
0c20: 69 65 76 65 73 20 74 68 65 20 54 65 6c 65 67 72 ieves the Telegr
0c30: 61 6d 20 63 68 61 74 20 49 44 20 66 6f 72 20 61 am chat ID for a
0c40: 20 67 69 76 65 6e 20 65 6d 61 69 6c 20 61 64 64 given email add
0c50: 72 65 73 73 2c 20 63 68 65 63 6b 73 20 74 68 61 ress, checks tha
0c60: 74 0a 09 2f 2f 2f 20 75 73 65 64 20 64 6f 6d 61 t../// used doma
0c70: 69 6e 20 69 73 20 61 6c 6c 6f 77 65 64 2e 0a 09 in is allowed...
0c80: 2f 2f 2f 0a 09 2f 2f 2f 20 23 20 41 72 67 75 6d ///../// # Argum
0c90: 65 6e 74 73 0a 09 2f 2f 2f 20 2a 20 60 6e 61 6d ents../// * `nam
0ca0: 65 60 20 2d 20 45 6d 61 69 6c 20 61 64 64 72 65 e` - Email addre
0cb0: 73 73 20 6f 72 20 75 73 65 72 6e 61 6d 65 20 74 ss or username t
0cc0: 6f 20 6c 6f 6f 6b 20 75 70 2e 0a 09 2f 2f 2f 0a o look up...///.
0cd0: 09 2f 2f 2f 20 23 20 52 65 74 75 72 6e 73 0a 09 ./// # Returns..
0ce0: 2f 2f 2f 20 2a 20 60 52 65 73 75 6c 74 3c 43 68 /// * `Result<Ch
0cf0: 61 74 50 65 65 72 49 64 3e 60 20 2d 20 54 65 6c atPeerId>` - Tel
0d00: 65 67 72 61 6d 20 63 68 61 74 20 49 44 20 66 6f egram chat ID fo
0d10: 72 20 74 68 65 20 61 64 64 72 65 73 73 2c 20 6f r the address, o
0d20: 72 20 64 65 66 61 75 6c 74 20 69 66 0a 09 2f 2f r default if..//
0d30: 2f 20 20 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 09 / not found...
0d40: 70 75 62 20 66 6e 20 67 65 74 5f 69 64 20 28 26 pub fn get_id (&
0d50: 73 65 6c 66 2c 20 6e 61 6d 65 3a 20 26 73 74 72 self, name: &str
0d60: 29 20 2d 3e 20 52 65 73 75 6c 74 3c 26 43 68 61 ) -> Result<&Cha
0d70: 74 50 65 65 72 49 64 3e 20 7b 0a 09 09 69 66 20 tPeerId> {...if
0d80: 73 65 6c 66 2e 61 64 64 72 65 73 73 2e 69 73 5f self.address.is_
0d90: 6d 61 74 63 68 28 6e 61 6d 65 29 20 7b 0a 09 09 match(name) {...
0da0: 09 4f 6b 28 73 65 6c 66 2e 74 67 2e 67 65 74 28 .Ok(self.tg.get(
0db0: 6e 61 6d 65 29 2e 75 6e 77 72 61 70 5f 6f 72 28 name).unwrap_or(
0dc0: 26 73 65 6c 66 2e 74 67 2e 64 65 66 61 75 6c 74 &self.tg.default
0dd0: 29 29 0a 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09 ))...} else {...
0de0: 09 62 61 69 6c 21 28 22 45 6d 61 69 6c 20 61 64 .bail!("Email ad
0df0: 64 72 65 73 73 20 7b 6e 61 6d 65 3a 3f 7d 20 69 dress {name:?} i
0e00: 73 20 6e 6f 74 20 66 72 6f 6d 20 61 6e 20 61 6c s not from an al
0e10: 6c 6f 77 65 64 20 64 6f 6d 61 69 6e 2e 22 29 3b lowed domain.");
0e20: 0a 09 7d 09 7d 0a 0a 09 2f 2f 2f 20 46 6f 72 77 ..}.}.../// Forw
0e30: 61 72 64 73 20 74 68 65 20 62 75 66 66 65 72 65 ards the buffere
0e40: 64 20 65 6d 61 69 6c 20 74 6f 20 65 61 63 68 20 d email to each
0e50: 64 69 73 74 69 6e 63 74 20 64 65 73 74 69 6e 61 distinct destina
0e60: 74 69 6f 6e 20 63 68 61 74 2c 20 75 73 69 6e 67 tion chat, using
0e70: 20 74 68 65 0a 09 2f 2f 2f 20 64 65 66 61 75 6c the../// defaul
0e80: 74 20 63 68 61 74 20 66 6f 72 20 61 6c 6c 6f 77 t chat for allow
0e90: 65 64 20 61 64 64 72 65 73 73 65 73 20 77 69 74 ed addresses wit
0ea0: 68 6f 75 74 20 61 20 63 6f 6e 66 69 67 75 72 65 hout a configure
0eb0: 64 20 72 65 63 69 70 69 65 6e 74 2e 0a 09 2f 2f d recipient...//
0ec0: 2f 0a 09 2f 2f 2f 20 49 6e 6c 69 6e 65 73 20 74 /../// Inlines t
0ed0: 68 65 20 66 69 72 73 74 20 70 6c 61 69 6e 2d 74 he first plain-t
0ee0: 65 78 74 20 62 6f 64 79 20 69 66 20 74 68 65 20 ext body if the
0ef0: 65 73 63 61 70 65 64 20 6d 65 73 73 61 67 65 20 escaped message
0f00: 69 73 20 62 65 6c 6f 77 20 34 30 39 36 0a 09 2f is below 4096../
0f10: 2f 2f 20 62 79 74 65 73 2e 20 53 65 6e 64 73 20 // bytes. Sends
0f20: 72 65 6d 61 69 6e 69 6e 67 20 74 65 78 74 20 70 remaining text p
0f30: 61 72 74 73 20 61 6e 64 20 64 65 63 6f 64 65 64 arts and decoded
0f40: 20 61 74 74 61 63 68 6d 65 6e 74 73 20 61 73 20 attachments as
0f50: 64 6f 63 75 6d 65 6e 74 73 3b 0a 09 2f 2f 2f 20 documents;..///
0f60: 48 54 4d 4c 20 62 6f 64 79 20 70 61 72 74 73 20 HTML body parts
0f70: 61 72 65 20 6f 6d 69 74 74 65 64 2e 20 4c 65 61 are omitted. Lea
0f80: 76 65 73 20 74 68 65 20 62 75 66 66 65 72 65 64 ves the buffered
0f90: 20 64 61 74 61 20 61 6e 64 20 65 6e 76 65 6c 6f data and envelo
0fa0: 70 65 20 69 6e 74 61 63 74 2e 0a 09 2f 2f 2f 0a pe intact...///.
0fb0: 09 2f 2f 2f 20 23 20 45 72 72 6f 72 73 0a 09 2f ./// # Errors../
0fc0: 2f 2f 20 52 65 74 75 72 6e 73 20 61 6e 20 65 72 // Returns an er
0fd0: 72 6f 72 20 66 6f 72 20 61 20 6d 69 73 73 69 6e ror for a missin
0fe0: 67 20 65 6e 76 65 6c 6f 70 65 2c 20 6e 6f 20 72 g envelope, no r
0ff0: 65 63 69 70 69 65 6e 74 73 2c 20 61 20 64 69 73 ecipients, a dis
1000: 61 6c 6c 6f 77 65 64 0a 09 2f 2f 2f 20 61 64 64 allowed../// add
1010: 72 65 73 73 2c 20 6d 61 69 6c 20 70 61 72 73 69 ress, mail parsi
1020: 6e 67 20 6f 72 20 70 61 72 74 20 65 78 74 72 61 ng or part extra
1030: 63 74 69 6f 6e 20 66 61 69 6c 75 72 65 73 2c 20 ction failures,
1040: 6f 72 20 72 65 6a 65 63 74 65 64 20 63 6c 6f 73 or rejected clos
1050: 69 6e 67 0a 09 2f 2f 2f 20 60 70 72 65 60 2f 60 ing../// `pre`/`
1060: 63 6f 64 65 60 20 74 61 67 73 20 69 6e 20 74 65 code` tags in te
1070: 78 74 20 70 61 73 73 65 64 20 74 6f 20 76 61 6c xt passed to val
1080: 69 64 61 74 69 6f 6e 2e 20 50 72 6f 70 61 67 61 idation. Propaga
1090: 74 65 73 20 6d 65 64 69 61 2d 67 72 6f 75 70 0a tes media-group.
10a0: 09 2f 2f 2f 20 63 6f 6e 73 74 72 75 63 74 69 6f ./// constructio
10b0: 6e 20 61 6e 64 20 54 65 6c 65 67 72 61 6d 20 72 n and Telegram r
10c0: 65 71 75 65 73 74 20 65 72 72 6f 72 73 2c 20 69 equest errors, i
10d0: 6e 63 6c 75 64 69 6e 67 20 64 69 61 67 6e 6f 73 ncluding diagnos
10e0: 74 69 63 20 73 65 6e 64 73 2e 0a 09 2f 2f 2f 20 tic sends...///
10f0: 44 65 6c 69 76 65 72 79 20 74 6f 20 65 61 72 6c Delivery to earl
1100: 69 65 72 20 63 68 61 74 73 20 69 73 20 6e 6f 74 ier chats is not
1110: 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 69 66 20 rolled back if
1120: 61 20 6c 61 74 65 72 20 73 65 6e 64 20 66 61 69 a later send fai
1130: 6c 73 2e 0a 09 61 73 79 6e 63 20 66 6e 20 72 65 ls...async fn re
1140: 6c 61 79 5f 6d 61 69 6c 20 28 26 73 65 6c 66 29 lay_mail (&self)
1150: 20 2d 3e 20 52 65 73 75 6c 74 3c 28 29 3e 20 7b -> Result<()> {
1160: 0a 09 09 69 66 20 6c 65 74 20 53 6f 6d 65 28 68 ...if let Some(h
1170: 65 61 64 65 72 73 29 20 3d 20 26 73 65 6c 66 2e eaders) = &self.
1180: 68 65 61 64 65 72 73 20 7b 0a 09 09 09 6c 65 74 headers {....let
1190: 20 6d 61 69 6c 20 3d 20 6d 61 69 6c 5f 70 61 72 mail = mail_par
11a0: 73 65 72 3a 3a 4d 65 73 73 61 67 65 50 61 72 73 ser::MessagePars
11b0: 65 72 3a 3a 6e 65 77 28 29 2e 70 61 72 73 65 28 er::new().parse(
11c0: 26 73 65 6c 66 2e 64 61 74 61 29 0a 09 09 09 09 &self.data).....
11d0: 2e 63 6f 6e 74 65 78 74 28 22 46 61 69 6c 65 64 .context("Failed
11e0: 20 74 6f 20 70 61 72 73 65 20 6d 61 69 6c 2e 22 to parse mail."
11f0: 29 3f 3b 0a 0a 09 09 09 2f 2f 20 41 64 64 69 6e )?;.....// Addin
1200: 67 20 61 6c 6c 20 6b 6e 6f 77 6e 20 61 64 64 72 g all known addr
1210: 65 73 73 65 73 20 74 6f 20 72 65 63 69 70 69 65 esses to recipie
1220: 6e 74 20 6c 69 73 74 2c 20 66 6f 72 20 61 6e 79 nt list, for any
1230: 6f 6e 65 20 65 6c 73 65 20 61 64 64 69 6e 67 20 one else adding
1240: 64 65 66 61 75 6c 74 0a 09 09 09 2f 2f 20 41 6c default....// Al
1250: 73 6f 20 69 66 20 6c 69 73 74 20 69 73 20 65 6d so if list is em
1260: 70 74 79 20 61 6c 73 6f 20 61 64 64 69 6e 67 20 pty also adding
1270: 64 65 66 61 75 6c 74 0a 09 09 09 6c 65 74 20 6d default....let m
1280: 75 74 20 72 63 70 74 3a 20 48 61 73 68 53 65 74 ut rcpt: HashSet
1290: 3c 26 43 68 61 74 50 65 65 72 49 64 3e 20 3d 20 <&ChatPeerId> =
12a0: 48 61 73 68 53 65 74 3a 3a 6e 65 77 28 29 3b 0a HashSet::new();.
12b0: 09 09 09 69 66 20 68 65 61 64 65 72 73 2e 74 6f ...if headers.to
12c0: 2e 69 73 5f 65 6d 70 74 79 28 29 20 7b 0a 09 09 .is_empty() {...
12d0: 09 09 62 61 69 6c 21 28 22 52 65 6c 61 79 69 6e ..bail!("Relayin
12e0: 67 20 69 73 20 64 69 73 61 62 6c 65 64 2c 20 61 g is disabled, a
12f0: 6e 64 20 74 68 65 72 65 27 73 20 6e 6f 20 64 65 nd there's no de
1300: 73 74 69 6e 61 74 69 6f 6e 20 61 64 64 72 65 73 stination addres
1310: 73 22 29 3b 0a 09 09 09 7d 0a 09 09 09 66 6f 72 s");....}....for
1320: 20 69 74 65 6d 20 69 6e 20 26 68 65 61 64 65 72 item in &header
1330: 73 2e 74 6f 20 7b 0a 09 09 09 09 72 63 70 74 2e s.to {.....rcpt.
1340: 69 6e 73 65 72 74 28 73 65 6c 66 2e 67 65 74 5f insert(self.get_
1350: 69 64 28 69 74 65 6d 29 3f 29 3b 0a 09 09 09 7d id(item)?);....}
1360: 3b 0a 09 09 09 69 66 20 72 63 70 74 2e 69 73 5f ;....if rcpt.is_
1370: 65 6d 70 74 79 28 29 20 7b 0a 09 09 09 09 73 65 empty() {.....se
1380: 6c 66 2e 74 67 2e 64 65 62 75 67 28 22 4e 6f 20 lf.tg.debug("No
1390: 72 65 63 69 70 69 65 6e 74 20 6f 72 20 65 6e 76 recipient or env
13a0: 65 6c 6f 70 65 20 61 64 64 72 65 73 73 2e 22 29 elope address.")
13b0: 2e 61 77 61 69 74 3f 3b 0a 09 09 09 09 72 63 70 .await?;.....rcp
13c0: 74 2e 69 6e 73 65 72 74 28 26 73 65 6c 66 2e 74 t.insert(&self.t
13d0: 67 2e 64 65 66 61 75 6c 74 29 3b 0a 09 09 09 7d g.default);....}
13e0: 3b 0a 0a 09 09 09 2f 2f 20 70 72 65 70 61 72 69 ;.....// prepari
13f0: 6e 67 20 6d 65 73 73 61 67 65 20 68 65 61 64 65 ng message heade
1400: 72 0a 09 09 09 6c 65 74 20 6d 75 74 20 72 65 70 r....let mut rep
1410: 6c 79 3a 20 56 65 63 3c 53 74 72 69 6e 67 3e 20 ly: Vec<String>
1420: 3d 20 76 65 63 21 5b 22 3c 62 6c 6f 63 6b 71 75 = vec!["<blockqu
1430: 6f 74 65 20 65 78 70 61 6e 64 61 62 6c 65 3e 22 ote expandable>"
1440: 2e 69 6e 74 6f 28 29 5d 3b 0a 09 09 09 69 66 20 .into()];....if
1450: 73 65 6c 66 2e 66 69 65 6c 64 73 2e 63 6f 6e 74 self.fields.cont
1460: 61 69 6e 73 28 22 73 75 62 6a 65 63 74 22 29 20 ains("subject")
1470: 7b 0a 09 09 09 09 69 66 20 6c 65 74 20 53 6f 6d {.....if let Som
1480: 65 28 73 75 62 6a 65 63 74 29 20 3d 20 6d 61 69 e(subject) = mai
1490: 6c 2e 73 75 62 6a 65 63 74 28 29 20 7b 0a 09 09 l.subject() {...
14a0: 09 09 09 72 65 70 6c 79 2e 70 75 73 68 28 66 6f ...reply.push(fo
14b0: 72 6d 61 74 21 28 22 3c 75 3e 3c 69 3e 53 75 62 rmat!("<u><i>Sub
14c0: 6a 65 63 74 3a 3c 2f 69 3e 3c 2f 75 3e e2 80 87 ject:</i></u> 
14d0: 3c 63 6f 64 65 3e 7b 7d 3c 2f 63 6f 64 65 3e 22 <code>{}</code>"
14e0: 2c 20 76 61 6c 69 64 61 74 65 28 73 75 62 6a 65 , validate(subje
14f0: 63 74 29 2e 73 74 61 63 6b 28 29 3f 29 29 3b 0a ct).stack()?));.
1500: 09 09 09 09 7d 20 65 6c 73 65 20 69 66 20 6c 65 ....} else if le
1510: 74 20 53 6f 6d 65 28 74 68 72 65 61 64 29 20 3d t Some(thread) =
1520: 20 6d 61 69 6c 2e 74 68 72 65 61 64 5f 6e 61 6d mail.thread_nam
1530: 65 28 29 20 7b 0a 09 09 09 09 09 72 65 70 6c 79 e() {......reply
1540: 2e 70 75 73 68 28 66 6f 72 6d 61 74 21 28 22 3c .push(format!("<
1550: 75 3e 3c 69 3e 54 68 72 65 61 64 3a 3c 2f 69 3e u><i>Thread:</i>
1560: 3c 2f 75 3e e2 80 87 3c 63 6f 64 65 3e 7b 7d 3c </u> <code>{}<
1570: 2f 63 6f 64 65 3e 22 2c 20 76 61 6c 69 64 61 74 /code>", validat
1580: 65 28 74 68 72 65 61 64 29 2e 73 74 61 63 6b 28 e(thread).stack(
1590: 29 3f 29 29 3b 0a 09 09 09 09 7d 0a 09 09 09 7d )?));.....}....}
15a0: 0a 09 09 09 2f 2f 20 64 6f 20 77 65 20 6e 65 65 ....// do we nee
15b0: 64 20 74 6f 20 72 65 70 6c 61 63 65 20 73 70 61 d to replace spa
15c0: 63 65 73 20 68 65 72 65 3f 0a 09 09 09 69 66 20 ces here?....if
15d0: 73 65 6c 66 2e 66 69 65 6c 64 73 2e 63 6f 6e 74 self.fields.cont
15e0: 61 69 6e 73 28 22 66 72 6f 6d 22 29 20 7b 0a 09 ains("from") {..
15f0: 09 09 09 72 65 70 6c 79 2e 70 75 73 68 28 66 6f ...reply.push(fo
1600: 72 6d 61 74 21 28 22 3c 75 3e 3c 69 3e 46 72 6f rmat!("<u><i>Fro
1610: 6d 3a 3c 2f 69 3e 3c 2f 75 3e e2 80 87 3c 63 6f m:</i></u> <co
1620: 64 65 3e 7b 7d 3c 2f 63 6f 64 65 3e 22 2c 20 76 de>{}</code>", v
1630: 61 6c 69 64 61 74 65 28 26 68 65 61 64 65 72 73 alidate(&headers
1640: 2e 66 72 6f 6d 29 2e 73 74 61 63 6b 28 29 3f 29 .from).stack()?)
1650: 29 3b 0a 09 09 09 7d 0a 09 09 09 69 66 20 73 65 );....}....if se
1660: 6c 66 2e 66 69 65 6c 64 73 2e 63 6f 6e 74 61 69 lf.fields.contai
1670: 6e 73 28 22 64 61 74 65 22 29 0a 09 09 09 09 26 ns("date").....&
1680: 26 20 6c 65 74 20 53 6f 6d 65 28 64 61 74 65 29 & let Some(date)
1690: 20 3d 20 6d 61 69 6c 2e 64 61 74 65 28 29 0a 09 = mail.date()..
16a0: 09 09 7b 0a 09 09 09 09 72 65 70 6c 79 2e 70 75 ..{.....reply.pu
16b0: 73 68 28 66 6f 72 6d 61 74 21 28 22 3c 75 3e 3c sh(format!("<u><
16c0: 69 3e 44 61 74 65 3a 3c 2f 69 3e 3c 2f 75 3e e2 i>Date:</i></u>â
16d0: 80 87 3c 63 6f 64 65 3e 7b 64 61 74 65 7d 3c 2f €‡<code>{date}</
16e0: 63 6f 64 65 3e 22 29 29 3b 0a 09 09 09 7d 0a 09 code>"));....}..
16f0: 09 09 72 65 70 6c 79 2e 70 75 73 68 28 22 3c 2f ..reply.push("</
1700: 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e blockquote><pre>
1710: 22 2e 69 6e 74 6f 28 29 29 3b 0a 09 09 09 6c 65 ".into());....le
1720: 74 20 72 65 70 6c 79 20 3d 20 72 65 70 6c 79 2e t reply = reply.
1730: 6a 6f 69 6e 28 22 5c 6e 22 29 3b 0a 0a 09 09 09 join("\n");.....
1740: 6c 65 74 20 74 65 78 74 5f 70 61 72 74 73 20 3d let text_parts =
1750: 20 6d 61 69 6c 2e 74 65 78 74 5f 62 6f 64 79 28 mail.text_body(
1760: 29 2e 6c 65 6e 28 29 3b 0a 09 09 09 6c 65 74 20 ).len();....let
1770: 68 74 6d 6c 5f 70 61 72 74 73 20 3d 20 6d 61 69 html_parts = mai
1780: 6c 2e 68 74 6d 6c 5f 62 6f 64 79 28 29 2e 6c 65 l.html_body().le
1790: 6e 28 29 3b 0a 09 09 09 6c 65 74 20 61 74 74 61 n();....let atta
17a0: 63 68 6d 65 6e 74 73 20 3d 20 6d 61 69 6c 2e 61 chments = mail.a
17b0: 74 74 61 63 68 6d 65 6e 74 73 28 29 2e 6c 65 6e ttachments().len
17c0: 28 29 3b 0a 09 09 09 69 66 20 68 74 6d 6c 5f 70 ();....if html_p
17d0: 61 72 74 73 20 21 3d 20 74 65 78 74 5f 70 61 72 arts != text_par
17e0: 74 73 20 7b 0a 09 09 09 09 73 65 6c 66 2e 74 67 ts {.....self.tg
17f0: 2e 64 65 62 75 67 28 26 66 6f 72 6d 61 74 21 28 .debug(&format!(
1800: 22 48 6d 2c 20 77 65 20 68 61 76 65 20 7b 68 74 "Hm, we have {ht
1810: 6d 6c 5f 70 61 72 74 73 7d 20 48 54 4d 4c 20 70 ml_parts} HTML p
1820: 61 72 74 73 20 61 6e 64 20 7b 74 65 78 74 5f 70 arts and {text_p
1830: 61 72 74 73 7d 20 74 65 78 74 20 70 61 72 74 73 arts} text parts
1840: 2e 22 29 29 2e 61 77 61 69 74 3f 3b 0a 09 09 09 .")).await?;....
1850: 7d 0a 09 09 09 2f 2f 6c 65 74 20 6d 75 74 20 68 }....//let mut h
1860: 74 6d 6c 5f 6e 75 6d 20 3d 20 30 3b 0a 09 09 09 tml_num = 0;....
1870: 6c 65 74 20 6d 75 74 20 74 65 78 74 5f 6e 75 6d let mut text_num
1880: 20 3d 20 30 3b 0a 09 09 09 6c 65 74 20 6d 75 74 = 0;....let mut
1890: 20 66 69 6c 65 5f 6e 75 6d 20 3d 20 30 3b 0a 09 file_num = 0;..
18a0: 09 09 2f 2f 20 6c 65 74 27 73 20 64 69 73 70 6c ..// let's displ
18b0: 61 79 20 66 69 72 73 74 20 68 74 6d 6c 20 6f 72 ay first html or
18c0: 20 74 65 78 74 20 70 61 72 74 20 61 73 20 62 6f text part as bo
18d0: 64 79 0a 09 09 09 6c 65 74 20 6d 75 74 20 62 6f dy....let mut bo
18e0: 64 79 3a 20 53 74 72 69 6e 67 20 3d 20 22 22 2e dy: String = "".
18f0: 69 6e 74 6f 28 29 3b 0a 09 09 09 2f 2a 0a 09 09 into();..../*...
1900: 09 20 2a 20 61 63 74 75 61 6c 6c 79 20 49 20 64 . * actually I d
1910: 6f 6e 27 74 20 77 61 6e 6e 61 20 70 61 72 73 65 on't wanna parse
1920: 20 74 68 61 74 20 68 74 6d 6c 20 73 74 75 66 66 that html stuff
1930: 0a 09 09 09 69 66 20 68 74 6d 6c 5f 70 61 72 74 ....if html_part
1940: 73 20 3e 20 30 20 7b 0a 09 09 09 09 6c 65 74 20 s > 0 {.....let
1950: 74 65 78 74 20 3d 20 6d 61 69 6c 2e 62 6f 64 79 text = mail.body
1960: 5f 68 74 6d 6c 28 30 29 2e 73 74 61 63 6b 28 29 _html(0).stack()
1970: 3f 3b 0a 09 09 09 09 69 66 20 74 65 78 74 2e 6c ?;.....if text.l
1980: 65 6e 28 29 20 3c 20 34 30 39 36 20 2d 20 68 65 en() < 4096 - he
1990: 61 64 65 72 5f 73 69 7a 65 20 7b 0a 09 09 09 09 ader_size {.....
19a0: 09 62 6f 64 79 20 3d 20 74 65 78 74 3b 0a 09 09 .body = text;...
19b0: 09 09 09 68 74 6d 6c 5f 6e 75 6d 20 3d 20 31 3b ...html_num = 1;
19c0: 0a 09 09 09 09 7d 0a 09 09 09 7d 3b 0a 09 09 09 .....}....};....
19d0: 2a 2f 0a 09 09 09 69 66 20 62 6f 64 79 2e 69 73 */....if body.is
19e0: 5f 65 6d 70 74 79 28 29 20 26 26 20 74 65 78 74 _empty() && text
19f0: 5f 70 61 72 74 73 20 3e 20 30 20 7b 0a 09 09 09 _parts > 0 {....
1a00: 09 6c 65 74 20 74 65 78 74 20 3d 20 6d 61 69 6c .let text = mail
1a10: 2e 62 6f 64 79 5f 74 65 78 74 28 30 29 0a 09 09 .body_text(0)...
1a20: 09 09 09 2e 63 6f 6e 74 65 78 74 28 22 46 61 69 ....context("Fai
1a30: 6c 65 64 20 74 6f 20 65 78 74 72 61 63 74 20 74 led to extract t
1a40: 65 78 74 20 66 72 6f 6d 20 6d 65 73 73 61 67 65 ext from message
1a50: 22 29 3f 0a 09 09 09 09 09 2e 72 65 70 6c 61 63 ")?.......replac
1a60: 65 28 22 5c 72 5c 6e 22 2c 20 22 5c 6e 22 29 3b e("\r\n", "\n");
1a70: 0a 09 09 09 09 6c 65 74 20 74 65 78 74 20 3d 20 .....let text =
1a80: 76 61 6c 69 64 61 74 65 28 26 74 65 78 74 29 2e validate(&text).
1a90: 73 74 61 63 6b 28 29 3f 3b 0a 09 09 09 09 2f 2f stack()?;.....//
1aa0: 20 36 3a 0a 09 09 09 09 2f 2f 20 2d 20 28 68 65 6:.....// - (he
1ab0: 61 64 65 72 73 29 0a 09 09 09 09 2f 2f 20 2d 20 aders).....// -
1ac0: 28 6d 61 69 6c 20 74 65 78 74 29 0a 09 09 09 09 (mail text).....
1ad0: 2f 2f 20 2d 20 36 3a 20 3c 2f 70 72 65 3e 0a 09 // - 6: </pre>..
1ae0: 09 09 09 69 66 20 74 65 78 74 2e 6c 65 6e 28 29 ...if text.len()
1af0: 20 3c 20 34 30 39 36 20 2d 20 28 20 72 65 70 6c < 4096 - ( repl
1b00: 79 2e 6c 65 6e 28 29 20 2b 20 36 20 29 20 7b 0a y.len() + 6 ) {.
1b10: 09 09 09 09 09 62 6f 64 79 20 3d 20 74 65 78 74 .....body = text
1b20: 2e 74 6f 5f 73 74 72 69 6e 67 28 29 3b 0a 09 09 .to_string();...
1b30: 09 09 09 74 65 78 74 5f 6e 75 6d 20 3d 20 31 3b ...text_num = 1;
1b40: 0a 09 09 09 09 7d 0a 09 09 09 7d 3b 0a 09 09 09 .....}....};....
1b50: 6c 65 74 20 6d 73 67 20 3d 20 66 6f 72 6d 61 74 let msg = format
1b60: 21 28 22 7b 7d 7b 7d 3c 2f 70 72 65 3e 22 2c 20 !("{}{}</pre>",
1b70: 72 65 70 6c 79 2c 20 62 6f 64 79 29 3b 0a 0a 09 reply, body);...
1b80: 09 09 2f 2f 20 61 6e 64 20 6c 65 74 27 73 20 63 ..// and let's c
1b90: 6f 6c 6c 65 63 74 20 61 6c 6c 20 6f 74 68 65 72 ollect all other
1ba0: 20 61 74 74 61 63 68 6d 65 6e 74 20 70 61 72 74 attachment part
1bb0: 73 0a 09 09 09 6c 65 74 20 6d 75 74 20 66 69 6c s....let mut fil
1bc0: 65 73 5f 74 6f 5f 73 65 6e 64 20 3d 20 76 65 63 es_to_send = vec
1bd0: 21 5b 5d 3b 0a 09 09 09 2f 2a 0a 09 09 09 20 2a ![];..../*.... *
1be0: 20 6c 65 74 27 73 20 6a 75 73 74 20 73 6b 69 70 let's just skip
1bf0: 20 68 74 6d 6c 20 70 61 72 74 73 20 66 6f 72 20 html parts for
1c00: 6e 6f 77 2c 20 74 68 65 79 20 6a 75 73 74 20 64 now, they just d
1c10: 75 70 6c 69 63 61 74 65 20 74 65 78 74 3f 0a 09 uplicate text?..
1c20: 09 09 77 68 69 6c 65 20 68 74 6d 6c 5f 6e 75 6d ..while html_num
1c30: 20 3c 20 68 74 6d 6c 5f 70 61 72 74 73 20 7b 0a < html_parts {.
1c40: 09 09 09 09 66 69 6c 65 73 5f 74 6f 5f 73 65 6e ....files_to_sen
1c50: 64 2e 70 75 73 68 28 6d 61 69 6c 2e 68 74 6d 6c d.push(mail.html
1c60: 5f 70 61 72 74 28 68 74 6d 6c 5f 6e 75 6d 29 2e _part(html_num).
1c70: 73 74 61 63 6b 28 29 3f 29 3b 0a 09 09 09 09 68 stack()?);.....h
1c80: 74 6d 6c 5f 6e 75 6d 20 2b 3d 20 31 3b 0a 09 09 tml_num += 1;...
1c90: 09 7d 0a 09 09 09 2a 2f 0a 09 09 09 77 68 69 6c .}....*/....whil
1ca0: 65 20 74 65 78 74 5f 6e 75 6d 20 3c 20 74 65 78 e text_num < tex
1cb0: 74 5f 70 61 72 74 73 20 7b 0a 09 09 09 09 66 69 t_parts {.....fi
1cc0: 6c 65 73 5f 74 6f 5f 73 65 6e 64 2e 70 75 73 68 les_to_send.push
1cd0: 28 6d 61 69 6c 2e 74 65 78 74 5f 62 6f 64 79 28 (mail.text_body(
1ce0: 29 2e 6e 74 68 28 74 65 78 74 5f 6e 75 6d 29 0a ).nth(text_num).
1cf0: 09 09 09 09 09 2e 63 6f 6e 74 65 78 74 28 22 46 ......context("F
1d00: 61 69 6c 65 64 20 74 6f 20 67 65 74 20 74 65 78 ailed to get tex
1d10: 74 20 70 61 72 74 20 66 72 6f 6d 20 6d 65 73 73 t part from mess
1d20: 61 67 65 2e 22 29 3f 29 3b 0a 09 09 09 09 74 65 age.")?);.....te
1d30: 78 74 5f 6e 75 6d 20 2b 3d 20 31 3b 0a 09 09 09 xt_num += 1;....
1d40: 7d 0a 09 09 09 77 68 69 6c 65 20 66 69 6c 65 5f }....while file_
1d50: 6e 75 6d 20 3c 20 61 74 74 61 63 68 6d 65 6e 74 num < attachment
1d60: 73 20 7b 0a 09 09 09 09 66 69 6c 65 73 5f 74 6f s {.....files_to
1d70: 5f 73 65 6e 64 2e 70 75 73 68 28 6d 61 69 6c 2e _send.push(mail.
1d80: 61 74 74 61 63 68 6d 65 6e 74 73 28 29 2e 6e 74 attachments().nt
1d90: 68 28 66 69 6c 65 5f 6e 75 6d 29 0a 09 09 09 09 h(file_num).....
1da0: 09 2e 63 6f 6e 74 65 78 74 28 22 46 61 69 6c 65 ..context("Faile
1db0: 64 20 74 6f 20 67 65 74 20 66 69 6c 65 20 70 61 d to get file pa
1dc0: 72 74 20 66 72 6f 6d 20 6d 65 73 73 61 67 65 2e rt from message.
1dd0: 22 29 3f 29 3b 0a 09 09 09 09 66 69 6c 65 5f 6e ")?);.....file_n
1de0: 75 6d 20 2b 3d 20 31 3b 0a 09 09 09 7d 0a 0a 09 um += 1;....}...
1df0: 09 09 66 6f 72 20 63 68 61 74 20 69 6e 20 72 63 ..for chat in rc
1e00: 70 74 20 7b 0a 09 09 09 09 69 66 20 21 66 69 6c pt {.....if !fil
1e10: 65 73 5f 74 6f 5f 73 65 6e 64 2e 69 73 5f 65 6d es_to_send.is_em
1e20: 70 74 79 28 29 20 7b 0a 09 09 09 09 09 6c 65 74 pty() {......let
1e30: 20 6d 75 74 20 66 69 6c 65 73 20 3d 20 76 65 63 mut files = vec
1e40: 21 5b 5d 3b 0a 09 09 09 09 09 2f 2f 20 6c 65 74 ![];......// let
1e50: 20 6d 75 74 20 66 69 72 73 74 5f 6f 6e 65 20 3d mut first_one =
1e60: 20 74 72 75 65 3b 0a 09 09 09 09 09 66 6f 72 20 true;......for
1e70: 63 68 75 6e 6b 20 69 6e 20 26 66 69 6c 65 73 5f chunk in &files_
1e80: 74 6f 5f 73 65 6e 64 20 7b 0a 09 09 09 09 09 09 to_send {.......
1e90: 6c 65 74 20 64 61 74 61 3a 20 56 65 63 3c 75 38 let data: Vec<u8
1ea0: 3e 20 3d 20 63 68 75 6e 6b 2e 64 65 63 6f 64 65 > = chunk.decode
1eb0: 64 28 29 2e 74 6f 5f 76 65 63 28 29 3b 0a 09 09 d().to_vec();...
1ec0: 09 09 09 09 6c 65 74 20 6d 75 74 20 66 69 6c 65 ....let mut file
1ed0: 6e 61 6d 65 3a 20 4f 70 74 69 6f 6e 3c 53 74 72 name: Option<Str
1ee0: 69 6e 67 3e 20 3d 20 4e 6f 6e 65 3b 0a 09 09 09 ing> = None;....
1ef0: 09 09 09 66 6f 72 20 68 65 61 64 65 72 20 69 6e ...for header in
1f00: 20 63 68 75 6e 6b 2e 68 65 61 64 65 72 73 28 29 chunk.headers()
1f10: 20 7b 0a 09 09 09 09 09 09 09 69 66 20 68 65 61 {........if hea
1f20: 64 65 72 2e 72 61 77 5f 6e 61 6d 65 28 29 20 3d der.raw_name() =
1f30: 3d 20 22 43 6f 6e 74 65 6e 74 2d 54 79 70 65 22 = "Content-Type"
1f40: 20 7b 0a 09 09 09 09 09 09 09 09 6d 61 74 63 68 {.........match
1f50: 20 68 65 61 64 65 72 2e 76 61 6c 75 65 28 29 20 header.value()
1f60: 7b 0a 09 09 09 09 09 09 09 09 09 6d 61 69 6c 5f {..........mail_
1f70: 70 61 72 73 65 72 3a 3a 48 65 61 64 65 72 56 61 parser::HeaderVa
1f80: 6c 75 65 3a 3a 43 6f 6e 74 65 6e 74 54 79 70 65 lue::ContentType
1f90: 28 63 6f 6e 74 65 6e 74 74 79 70 65 29 20 3d 3e (contenttype) =>
1fa0: 20 7b 0a 09 09 09 09 09 09 09 09 09 09 69 66 20 {...........if
1fb0: 6c 65 74 20 53 6f 6d 65 28 66 6e 61 6d 65 29 20 let Some(fname)
1fc0: 3d 20 63 6f 6e 74 65 6e 74 74 79 70 65 2e 61 74 = contenttype.at
1fd0: 74 72 69 62 75 74 65 28 22 6e 61 6d 65 22 29 20 tribute("name")
1fe0: 7b 0a 09 09 09 09 09 09 09 09 09 09 09 66 69 6c {............fil
1ff0: 65 6e 61 6d 65 20 3d 20 53 6f 6d 65 28 66 6e 61 ename = Some(fna
2000: 6d 65 2e 74 6f 5f 6f 77 6e 65 64 28 29 29 3b 0a me.to_owned());.
2010: 09 09 09 09 09 09 09 09 09 09 7d 0a 09 09 09 09 ..........}.....
2020: 09 09 09 09 09 7d 2c 0a 09 09 09 09 09 09 09 09 .....},.........
2030: 09 5f 20 3d 3e 20 7b 0a 09 09 09 09 09 09 09 09 ._ => {.........
2040: 09 09 73 65 6c 66 2e 74 67 2e 64 65 62 75 67 28 ..self.tg.debug(
2050: 22 41 74 74 61 63 68 6d 65 6e 74 20 68 61 73 20 "Attachment has
2060: 62 61 64 20 43 6f 6e 74 65 6e 74 54 79 70 65 20 bad ContentType
2070: 68 65 61 64 65 72 2e 22 29 2e 61 77 61 69 74 3f header.").await?
2080: 3b 0a 09 09 09 09 09 09 09 09 09 7d 2c 0a 09 09 ;..........},...
2090: 09 09 09 09 09 09 7d 3b 0a 09 09 09 09 09 09 09 ......};........
20a0: 7d 3b 0a 09 09 09 09 09 09 7d 3b 0a 09 09 09 09 };.......};.....
20b0: 09 09 6c 65 74 20 66 69 6c 65 6e 61 6d 65 20 3d ..let filename =
20c0: 20 69 66 20 6c 65 74 20 53 6f 6d 65 28 66 6e 61 if let Some(fna
20d0: 6d 65 29 20 3d 20 66 69 6c 65 6e 61 6d 65 20 7b me) = filename {
20e0: 0a 09 09 09 09 09 09 09 66 6e 61 6d 65 0a 09 09 ........fname...
20f0: 09 09 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09 ....} else {....
2100: 09 09 09 09 22 41 74 74 61 63 68 6d 65 6e 74 2e ...."Attachment.
2110: 74 78 74 22 2e 69 6e 74 6f 28 29 0a 09 09 09 09 txt".into().....
2120: 09 09 7d 3b 0a 09 09 09 09 09 09 66 69 6c 65 73 ..};.......files
2130: 2e 70 75 73 68 28 41 74 74 61 63 68 6d 65 6e 74 .push(Attachment
2140: 20 7b 0a 09 09 09 09 09 09 09 64 61 74 61 3a 20 {........data:
2150: 43 75 72 73 6f 72 3a 3a 6e 65 77 28 64 61 74 61 Cursor::new(data
2160: 29 2c 0a 09 09 09 09 09 09 09 6e 61 6d 65 3a 20 ),........name:
2170: 66 69 6c 65 6e 61 6d 65 2c 0a 09 09 09 09 09 09 filename,.......
2180: 7d 29 3b 0a 09 09 09 09 09 7d 0a 09 09 09 09 09 });......}......
2190: 73 65 6c 66 2e 74 67 2e 73 65 6e 64 67 72 6f 75 self.tg.sendgrou
21a0: 70 28 63 68 61 74 2c 20 66 69 6c 65 73 2c 20 26 p(chat, files, &
21b0: 6d 73 67 29 2e 61 77 61 69 74 3f 3b 0a 09 09 09 msg).await?;....
21c0: 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09 09 09 73 .} else {......s
21d0: 65 6c 66 2e 74 67 2e 73 65 6e 64 28 63 68 61 74 elf.tg.send(chat
21e0: 2c 20 26 6d 73 67 29 2e 61 77 61 69 74 3f 3b 0a , &msg).await?;.
21f0: 09 09 09 09 7d 0a 09 09 09 7d 0a 09 09 7d 20 65 ....}....}...} e
2200: 6c 73 65 20 7b 0a 09 09 09 62 61 69 6c 21 28 22 lse {....bail!("
2210: 52 65 71 75 69 72 65 64 20 68 65 61 64 65 72 73 Required headers
2220: 20 77 65 72 65 20 6e 6f 74 20 66 6f 75 6e 64 2e were not found.
2230: 22 29 3b 0a 09 09 7d 0a 09 09 4f 6b 28 28 29 29 ");...}...Ok(())
2240: 0a 09 7d 0a 7d 0a 0a 2f 2f 2f 20 53 4d 54 50 20 ..}.}../// SMTP
2250: 68 61 6e 64 6c 65 72 20 69 6d 70 6c 65 6d 65 6e handler implemen
2260: 74 61 74 69 6f 6e 20 66 6f 72 20 6d 61 69 6c 69 tation for maili
2270: 6e 2d 65 6d 62 65 64 64 65 64 2e 0a 69 6d 70 6c n-embedded..impl
2280: 20 6d 61 69 6c 69 6e 5f 65 6d 62 65 64 64 65 64 mailin_embedded
2290: 3a 3a 48 61 6e 64 6c 65 72 20 66 6f 72 20 4d 61 ::Handler for Ma
22a0: 69 6c 53 65 72 76 65 72 20 7b 0a 09 2f 2f 2f 20 ilServer {..///
22b0: 4a 75 73 74 20 64 65 6e 79 20 6c 6f 67 69 6e 20 Just deny login
22c0: 61 75 74 68 0a 09 66 6e 20 61 75 74 68 5f 6c 6f auth..fn auth_lo
22d0: 67 69 6e 20 28 26 6d 75 74 20 73 65 6c 66 2c 20 gin (&mut self,
22e0: 5f 75 73 65 72 6e 61 6d 65 3a 20 26 73 74 72 2c _username: &str,
22f0: 20 5f 70 61 73 73 77 6f 72 64 3a 20 26 73 74 72 _password: &str
2300: 29 20 2d 3e 20 52 65 73 70 6f 6e 73 65 20 7b 0a ) -> Response {.
2310: 09 09 49 4e 56 41 4c 49 44 5f 43 52 45 44 45 4e ..INVALID_CREDEN
2320: 54 49 41 4c 53 0a 09 7d 0a 0a 09 2f 2f 2f 20 4a TIALS..}.../// J
2330: 75 73 74 20 64 65 6e 79 20 70 6c 61 69 6e 20 61 ust deny plain a
2340: 75 74 68 0a 09 66 6e 20 61 75 74 68 5f 70 6c 61 uth..fn auth_pla
2350: 69 6e 20 28 26 6d 75 74 20 73 65 6c 66 2c 20 5f in (&mut self, _
2360: 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 5f 69 64 authorization_id
2370: 3a 20 26 73 74 72 2c 20 5f 61 75 74 68 65 6e 74 : &str, _authent
2380: 69 63 61 74 69 6f 6e 5f 69 64 3a 20 26 73 74 72 ication_id: &str
2390: 2c 20 5f 70 61 73 73 77 6f 72 64 3a 20 26 73 74 , _password: &st
23a0: 72 29 20 2d 3e 20 52 65 73 70 6f 6e 73 65 20 7b r) -> Response {
23b0: 0a 09 09 49 4e 56 41 4c 49 44 5f 43 52 45 44 45 ...INVALID_CREDE
23c0: 4e 54 49 41 4c 53 0a 09 7d 0a 0a 09 2f 2f 2f 20 NTIALS..}...///
23d0: 56 65 72 69 66 79 20 77 68 65 74 68 65 72 20 61 Verify whether a
23e0: 64 64 72 65 73 73 20 69 73 20 64 65 6c 69 76 65 ddress is delive
23f0: 72 61 62 6c 65 0a 09 66 6e 20 72 63 70 74 20 28 rable..fn rcpt (
2400: 26 6d 75 74 20 73 65 6c 66 2c 20 74 6f 3a 20 26 &mut self, to: &
2410: 73 74 72 29 20 2d 3e 20 52 65 73 70 6f 6e 73 65 str) -> Response
2420: 20 7b 0a 09 09 69 66 20 73 65 6c 66 2e 67 65 74 {...if self.get
2430: 5f 69 64 28 74 6f 29 2e 69 73 5f 6f 6b 28 29 20 _id(to).is_ok()
2440: 7b 0a 09 09 09 4f 4b 0a 09 09 7d 20 65 6c 73 65 {....OK...} else
2450: 20 7b 0a 09 09 09 4e 4f 5f 4d 41 49 4c 42 4f 58 {....NO_MAILBOX
2460: 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2f 2f 20 53 61 ...}..}.../// Sa
2470: 76 65 20 68 65 61 64 65 72 73 20 77 65 20 6e 65 ve headers we ne
2480: 65 64 0a 09 66 6e 20 64 61 74 61 5f 73 74 61 72 ed..fn data_star
2490: 74 20 28 26 6d 75 74 20 73 65 6c 66 2c 20 5f 64 t (&mut self, _d
24a0: 6f 6d 61 69 6e 3a 20 26 73 74 72 2c 20 66 72 6f omain: &str, fro
24b0: 6d 3a 20 26 73 74 72 2c 20 5f 69 73 38 62 69 74 m: &str, _is8bit
24c0: 3a 20 62 6f 6f 6c 2c 20 74 6f 3a 20 26 5b 53 74 : bool, to: &[St
24d0: 72 69 6e 67 5d 29 20 2d 3e 20 52 65 73 70 6f 6e ring]) -> Respon
24e0: 73 65 20 7b 0a 09 09 73 65 6c 66 2e 68 65 61 64 se {...self.head
24f0: 65 72 73 20 3d 20 53 6f 6d 65 28 53 6f 6d 65 48 ers = Some(SomeH
2500: 65 61 64 65 72 73 7b 0a 09 09 09 66 72 6f 6d 3a eaders{....from:
2510: 20 66 72 6f 6d 2e 74 6f 5f 73 74 72 69 6e 67 28 from.to_string(
2520: 29 2c 0a 09 09 09 74 6f 3a 20 74 6f 2e 74 6f 5f ),....to: to.to_
2530: 76 65 63 28 29 2c 0a 09 09 7d 29 3b 0a 09 09 4f vec(),...});...O
2540: 4b 0a 09 7d 0a 0a 09 2f 2f 2f 20 53 61 76 65 20 K..}.../// Save
2550: 63 68 75 6e 6b 28 3f 29 20 6f 66 20 64 61 74 61 chunk(?) of data
2560: 0a 09 66 6e 20 64 61 74 61 20 28 26 6d 75 74 20 ..fn data (&mut
2570: 73 65 6c 66 2c 20 62 75 66 3a 20 26 5b 75 38 5d self, buf: &[u8]
2580: 29 20 2d 3e 20 73 74 64 3a 3a 72 65 73 75 6c 74 ) -> std::result
2590: 3a 3a 52 65 73 75 6c 74 3c 28 29 2c 20 45 72 72 ::Result<(), Err
25a0: 6f 72 3e 20 7b 0a 09 09 73 65 6c 66 2e 64 61 74 or> {...self.dat
25b0: 61 2e 61 70 70 65 6e 64 28 62 75 66 2e 74 6f 5f a.append(buf.to_
25c0: 76 65 63 28 29 2e 61 73 5f 6d 75 74 28 29 29 3b vec().as_mut());
25d0: 0a 09 09 4f 6b 28 28 29 29 0a 09 7d 0a 0a 09 2f ...Ok(())..}.../
25e0: 2f 2f 20 41 74 74 65 6d 70 74 20 74 6f 20 73 65 // Attempt to se
25f0: 6e 64 20 65 6d 61 69 6c 2c 20 72 65 74 75 72 6e nd email, return
2600: 20 74 65 6d 70 6f 72 61 72 79 20 65 72 72 6f 72 temporary error
2610: 20 69 66 20 74 68 61 74 20 66 61 69 6c 73 0a 09 if that fails..
2620: 66 6e 20 64 61 74 61 5f 65 6e 64 20 28 26 6d 75 fn data_end (&mu
2630: 74 20 73 65 6c 66 29 20 2d 3e 20 52 65 73 70 6f t self) -> Respo
2640: 6e 73 65 20 7b 0a 09 09 6c 65 74 20 6d 75 74 20 nse {...let mut
2650: 72 65 73 75 6c 74 20 3d 20 4f 4b 3b 0a 09 09 73 result = OK;...s
2660: 6d 6f 6c 3a 3a 62 6c 6f 63 6b 5f 6f 6e 28 43 6f mol::block_on(Co
2670: 6d 70 61 74 3a 3a 6e 65 77 28 61 73 79 6e 63 20 mpat::new(async
2680: 7b 0a 09 09 09 2f 2f 20 72 65 6c 61 79 20 6d 61 {....// relay ma
2690: 69 6c 0a 09 09 09 69 66 20 6c 65 74 20 45 72 72 il....if let Err
26a0: 28 65 72 72 29 20 3d 20 73 65 6c 66 2e 72 65 6c (err) = self.rel
26b0: 61 79 5f 6d 61 69 6c 28 29 2e 61 77 61 69 74 20 ay_mail().await
26c0: 7b 0a 09 09 09 09 72 65 73 75 6c 74 20 3d 20 49 {.....result = I
26d0: 4e 54 45 52 4e 41 4c 5f 45 52 52 4f 52 3b 0a 09 NTERNAL_ERROR;..
26e0: 09 09 09 2f 2f 20 69 6e 20 63 61 73 65 20 74 68 ...// in case th
26f0: 61 74 20 66 61 69 6c 73 20 2d 20 69 6e 66 6f 72 at fails - infor
2700: 6d 20 64 65 66 61 75 6c 74 20 72 65 63 69 70 69 m default recipi
2710: 65 6e 74 0a 09 09 09 09 69 66 20 6c 65 74 20 45 ent.....if let E
2720: 72 72 28 65 72 72 29 20 3d 20 73 65 6c 66 2e 74 rr(err) = self.t
2730: 67 2e 64 65 62 75 67 28 26 66 6f 72 6d 61 74 21 g.debug(&format!
2740: 28 22 53 65 6e 64 69 6e 67 20 65 6d 61 69 6c 73 ("Sending emails
2750: 20 66 61 69 6c 65 64 3a 5c 6e 7b 65 72 72 3a 7d failed:\n{err:}
2760: 22 29 29 2e 61 77 61 69 74 20 7b 0a 09 09 09 09 ")).await {.....
2770: 09 2f 2f 20 69 6e 20 63 61 73 65 20 74 68 61 74 .// in case that
2780: 20 61 6c 73 6f 20 66 61 69 6c 73 20 2d 20 77 72 also fails - wr
2790: 69 74 65 20 73 6f 6d 65 20 6c 6f 67 73 20 61 6e ite some logs an
27a0: 64 20 62 61 69 6c 0a 09 09 09 09 09 65 70 72 69 d bail......epri
27b0: 6e 74 6c 6e 21 28 22 7b 65 72 72 3a 3f 7d 22 29 ntln!("{err:?}")
27c0: 3b 0a 09 09 09 09 7d 3b 0a 09 09 09 7d 3b 0a 09 ;.....};....};..
27d0: 09 7d 29 29 3b 0a 09 09 2f 2f 20 63 6c 65 61 72 .}));...// clear
27e0: 20 2d 20 6a 75 73 74 20 69 6e 20 63 61 73 65 0a - just in case.
27f0: 09 09 73 65 6c 66 2e 64 61 74 61 20 3d 20 76 65 ..self.data = ve
2800: 63 21 5b 5d 3b 0a 09 09 73 65 6c 66 2e 68 65 61 c![];...self.hea
2810: 64 65 72 73 20 3d 20 4e 6f 6e 65 3b 0a 09 09 72 ders = None;...r
2820: 65 73 75 6c 74 0a 09 7d 0a 7d 0a esult..}.}.