Hex Artifact Content
Logged in as anonymous

Artifact 88ec92aaa8a74bc8cd81097234ae45901ce5b91c08d5bba65097c6bcb650f902:


0000: 75 73 65 20 63 72 61 74 65 3a 3a 7b 0a 09 43 75  use crate::{..Cu
0010: 72 73 6f 72 2c 0a 09 74 65 6c 65 67 72 61 6d 3a  rsor,..telegram:
0020: 3a 7b 0a 09 09 65 6e 63 6f 64 65 2c 0a 09 09 54  :{...encode,...T
0030: 65 6c 65 67 72 61 6d 54 72 61 6e 73 70 6f 72 74  elegramTransport
0040: 2c 0a 09 7d 2c 0a 09 75 74 69 6c 73 3a 3a 7b 0a  ,..},..utils::{.
0050: 09 09 41 74 74 61 63 68 6d 65 6e 74 2c 0a 09 09  ..Attachment,...
0060: 52 45 5f 44 4f 4d 41 49 4e 2c 0a 09 7d 2c 0a 7d  RE_DOMAIN,..},.}
0070: 3b 0a 0a 75 73 65 20 73 74 64 3a 3a 7b 0a 09 62  ;..use std::{..b
0080: 6f 72 72 6f 77 3a 3a 43 6f 77 2c 0a 09 63 6f 6c  orrow::Cow,..col
0090: 6c 65 63 74 69 6f 6e 73 3a 3a 7b 0a 09 09 48 61  lections::{...Ha
00a0: 73 68 4d 61 70 2c 0a 09 09 48 61 73 68 53 65 74  shMap,...HashSet
00b0: 2c 0a 09 7d 2c 0a 09 69 6f 3a 3a 45 72 72 6f 72  ,..},..io::Error
00c0: 2c 0a 7d 3b 0a 0a 75 73 65 20 61 73 79 6e 63 5f  ,.};..use async_
00d0: 73 74 64 3a 3a 7b 0a 09 73 79 6e 63 3a 3a 41 72  std::{..sync::Ar
00e0: 63 2c 0a 09 74 61 73 6b 2c 0a 7d 3b 0a 75 73 65  c,..task,.};.use
00f0: 20 6d 61 69 6c 69 6e 5f 65 6d 62 65 64 64 65 64   mailin_embedded
0100: 3a 3a 7b 0a 09 52 65 73 70 6f 6e 73 65 2c 0a 09  ::{..Response,..
0110: 72 65 73 70 6f 6e 73 65 3a 3a 7b 0a 09 09 49 4e  response::{...IN
0120: 54 45 52 4e 41 4c 5f 45 52 52 4f 52 2c 0a 09 09  TERNAL_ERROR,...
0130: 49 4e 56 41 4c 49 44 5f 43 52 45 44 45 4e 54 49  INVALID_CREDENTI
0140: 41 4c 53 2c 0a 09 09 4e 4f 5f 4d 41 49 4c 42 4f  ALS,...NO_MAILBO
0150: 58 2c 0a 09 09 4f 4b 0a 09 7d 2c 0a 7d 3b 0a 75  X,...OK..},.};.u
0160: 73 65 20 72 65 67 65 78 3a 3a 7b 0a 09 52 65 67  se regex::{..Reg
0170: 65 78 2c 0a 09 65 73 63 61 70 65 2c 0a 7d 3b 0a  ex,..escape,.};.
0180: 75 73 65 20 73 74 61 63 6b 65 64 5f 65 72 72 6f  use stacked_erro
0190: 72 73 3a 3a 7b 0a 09 52 65 73 75 6c 74 2c 0a 09  rs::{..Result,..
01a0: 53 74 61 63 6b 61 62 6c 65 45 72 72 2c 0a 09 62  StackableErr,..b
01b0: 61 69 6c 2c 0a 7d 3b 0a 75 73 65 20 74 67 62 6f  ail,.};.use tgbo
01c0: 74 3a 3a 74 79 70 65 73 3a 3a 43 68 61 74 50 65  t::types::ChatPe
01d0: 65 72 49 64 3b 0a 0a 2f 2f 2f 20 60 53 6f 6d 65  erId;../// `Some
01e0: 48 65 61 64 65 72 73 60 20 6f 62 6a 65 63 74 20  Headers` object 
01f0: 74 6f 20 73 74 6f 72 65 20 64 61 74 61 20 74 68  to store data th
0200: 72 6f 75 67 68 20 53 4d 54 50 20 73 65 73 73 69  rough SMTP sessi
0210: 6f 6e 0a 23 5b 64 65 72 69 76 65 28 43 6c 6f 6e  on.#[derive(Clon
0220: 65 2c 20 44 65 62 75 67 29 5d 0a 73 74 72 75 63  e, Debug)].struc
0230: 74 20 53 6f 6d 65 48 65 61 64 65 72 73 20 7b 0a  t SomeHeaders {.
0240: 09 66 72 6f 6d 3a 20 53 74 72 69 6e 67 2c 0a 09  .from: String,..
0250: 74 6f 3a 20 56 65 63 3c 53 74 72 69 6e 67 3e 2c  to: Vec<String>,
0260: 0a 7d 0a 0a 2f 2f 2f 20 60 4d 61 69 6c 53 65 72  .}../// `MailSer
0270: 76 65 72 60 20 43 65 6e 74 72 61 6c 20 6f 62 6a  ver` Central obj
0280: 65 63 74 20 77 69 74 68 20 54 47 20 61 70 69 20  ect with TG api 
0290: 61 6e 64 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  and configuratio
02a0: 6e 0a 23 5b 64 65 72 69 76 65 28 43 6c 6f 6e 65  n.#[derive(Clone
02b0: 2c 20 44 65 62 75 67 29 5d 0a 70 75 62 20 73 74  , Debug)].pub st
02c0: 72 75 63 74 20 4d 61 69 6c 53 65 72 76 65 72 20  ruct MailServer 
02d0: 7b 0a 09 64 61 74 61 3a 20 56 65 63 3c 75 38 3e  {..data: Vec<u8>
02e0: 2c 0a 09 68 65 61 64 65 72 73 3a 20 4f 70 74 69  ,..headers: Opti
02f0: 6f 6e 3c 53 6f 6d 65 48 65 61 64 65 72 73 3e 2c  on<SomeHeaders>,
0300: 0a 09 72 65 6c 61 79 3a 20 62 6f 6f 6c 2c 0a 09  ..relay: bool,..
0310: 74 67 3a 20 41 72 63 3c 54 65 6c 65 67 72 61 6d  tg: Arc<Telegram
0320: 54 72 61 6e 73 70 6f 72 74 3e 2c 0a 09 66 69 65  Transport>,..fie
0330: 6c 64 73 3a 20 48 61 73 68 53 65 74 3c 53 74 72  lds: HashSet<Str
0340: 69 6e 67 3e 2c 0a 09 61 64 64 72 65 73 73 3a 20  ing>,..address: 
0350: 52 65 67 65 78 2c 0a 7d 0a 0a 69 6d 70 6c 20 4d  Regex,.}..impl M
0360: 61 69 6c 53 65 72 76 65 72 20 7b 0a 09 2f 2f 2f  ailServer {..///
0370: 20 49 6e 69 74 69 61 6c 69 7a 65 20 41 50 49 20   Initialize API 
0380: 61 6e 64 20 72 65 61 64 20 63 6f 6e 66 69 67 75  and read configu
0390: 72 61 74 69 6f 6e 0a 09 70 75 62 20 66 6e 20 6e  ration..pub fn n
03a0: 65 77 28 73 65 74 74 69 6e 67 73 3a 20 63 6f 6e  ew(settings: con
03b0: 66 69 67 3a 3a 43 6f 6e 66 69 67 29 20 2d 3e 20  fig::Config) -> 
03c0: 52 65 73 75 6c 74 3c 4d 61 69 6c 53 65 72 76 65  Result<MailServe
03d0: 72 3e 20 7b 0a 09 09 6c 65 74 20 61 70 69 5f 6b  r> {...let api_k
03e0: 65 79 20 3d 20 73 65 74 74 69 6e 67 73 2e 67 65  ey = settings.ge
03f0: 74 5f 73 74 72 69 6e 67 28 22 61 70 69 5f 6b 65  t_string("api_ke
0400: 79 22 29 0a 09 09 09 2e 63 6f 6e 74 65 78 74 28  y").....context(
0410: 22 5b 73 6d 74 70 32 74 67 2e 74 6f 6d 6c 5d 20  "[smtp2tg.toml] 
0420: 6d 69 73 73 69 6e 67 20 5c 22 61 70 69 5f 6b 65  missing \"api_ke
0430: 79 5c 22 20 70 61 72 61 6d 65 74 65 72 2e 5c 6e  y\" parameter.\n
0440: 22 29 3f 3b 0a 09 09 6c 65 74 20 6d 75 74 20 72  ")?;...let mut r
0450: 65 63 69 70 69 65 6e 74 73 20 3d 20 48 61 73 68  ecipients = Hash
0460: 4d 61 70 3a 3a 6e 65 77 28 29 3b 0a 09 09 66 6f  Map::new();...fo
0470: 72 20 28 6e 61 6d 65 2c 20 76 61 6c 75 65 29 20  r (name, value) 
0480: 69 6e 20 73 65 74 74 69 6e 67 73 2e 67 65 74 5f  in settings.get_
0490: 74 61 62 6c 65 28 22 72 65 63 69 70 69 65 6e 74  table("recipient
04a0: 73 22 29 0a 09 09 09 2e 65 78 70 65 63 74 28 22  s").....expect("
04b0: 5b 73 6d 74 70 32 74 67 2e 74 6f 6d 6c 5d 20 6d  [smtp2tg.toml] m
04c0: 69 73 73 69 6e 67 20 74 61 62 6c 65 20 5c 22 72  issing table \"r
04d0: 65 63 69 70 69 65 6e 74 73 5c 22 2e 5c 6e 22 29  ecipients\".\n")
04e0: 0a 09 09 7b 0a 09 09 09 6c 65 74 20 76 61 6c 75  ...{....let valu
04f0: 65 20 3d 20 76 61 6c 75 65 2e 69 6e 74 6f 5f 69  e = value.into_i
0500: 6e 74 28 29 0a 09 09 09 09 2e 63 6f 6e 74 65 78  nt()......contex
0510: 74 28 22 5b 73 6d 74 70 32 74 67 2e 74 6f 6d 6c  t("[smtp2tg.toml
0520: 5d 20 5c 22 72 65 63 69 70 69 65 6e 74 5c 22 20  ] \"recipient\" 
0530: 74 61 62 6c 65 20 76 61 6c 75 65 73 20 73 68 6f  table values sho
0540: 75 6c 64 20 62 65 20 69 6e 74 65 67 65 72 73 2e  uld be integers.
0550: 5c 6e 22 29 3f 3b 0a 09 09 09 72 65 63 69 70 69  \n")?;....recipi
0560: 65 6e 74 73 2e 69 6e 73 65 72 74 28 6e 61 6d 65  ents.insert(name
0570: 2c 20 76 61 6c 75 65 29 3b 0a 09 09 7d 0a 09 09  , value);...}...
0580: 6c 65 74 20 64 65 66 61 75 6c 74 20 3d 20 73 65  let default = se
0590: 74 74 69 6e 67 73 2e 67 65 74 5f 69 6e 74 28 22  ttings.get_int("
05a0: 64 65 66 61 75 6c 74 22 29 0a 09 09 09 2e 63 6f  default").....co
05b0: 6e 74 65 78 74 28 22 5b 73 6d 74 70 32 74 67 2e  ntext("[smtp2tg.
05c0: 74 6f 6d 6c 5d 20 6d 69 73 73 69 6e 67 20 5c 22  toml] missing \"
05d0: 64 65 66 61 75 6c 74 5c 22 20 72 65 63 69 70 69  default\" recipi
05e0: 65 6e 74 2e 5c 6e 22 29 3f 3b 0a 0a 09 09 6c 65  ent.\n")?;....le
05f0: 74 20 74 67 20 3d 20 41 72 63 3a 3a 6e 65 77 28  t tg = Arc::new(
0600: 54 65 6c 65 67 72 61 6d 54 72 61 6e 73 70 6f 72  TelegramTranspor
0610: 74 3a 3a 6e 65 77 28 61 70 69 5f 6b 65 79 2c 20  t::new(api_key, 
0620: 72 65 63 69 70 69 65 6e 74 73 2c 20 64 65 66 61  recipients, defa
0630: 75 6c 74 29 3f 29 3b 0a 09 09 6c 65 74 20 66 69  ult)?);...let fi
0640: 65 6c 64 73 20 3d 20 48 61 73 68 53 65 74 3a 3a  elds = HashSet::
0650: 3c 53 74 72 69 6e 67 3e 3a 3a 66 72 6f 6d 5f 69  <String>::from_i
0660: 74 65 72 28 73 65 74 74 69 6e 67 73 2e 67 65 74  ter(settings.get
0670: 5f 61 72 72 61 79 28 22 66 69 65 6c 64 73 22 29  _array("fields")
0680: 0a 09 09 09 2e 65 78 70 65 63 74 28 22 5b 73 6d  .....expect("[sm
0690: 74 70 32 74 67 2e 74 6f 6d 6c 5d 20 5c 22 66 69  tp2tg.toml] \"fi
06a0: 65 6c 64 73 5c 22 20 73 68 6f 75 6c 64 20 62 65  elds\" should be
06b0: 20 61 6e 20 61 72 72 61 79 22 29 0a 09 09 09 2e   an array").....
06c0: 69 74 65 72 28 29 2e 6d 61 70 28 7c 78 7c 20 78  iter().map(|x| x
06d0: 2e 63 6c 6f 6e 65 28 29 2e 69 6e 74 6f 5f 73 74  .clone().into_st
06e0: 72 69 6e 67 28 29 2e 65 78 70 65 63 74 28 22 73  ring().expect("s
06f0: 68 6f 75 6c 64 20 62 65 20 73 74 72 69 6e 67 73  hould be strings
0700: 22 29 29 29 3b 0a 09 09 6c 65 74 20 6d 75 74 20  ")));...let mut 
0710: 64 6f 6d 61 69 6e 73 3a 20 48 61 73 68 53 65 74  domains: HashSet
0720: 3c 53 74 72 69 6e 67 3e 20 3d 20 48 61 73 68 53  <String> = HashS
0730: 65 74 3a 3a 6e 65 77 28 29 3b 0a 09 09 6c 65 74  et::new();...let
0740: 20 65 78 74 72 61 5f 64 6f 6d 61 69 6e 73 20 3d   extra_domains =
0750: 20 73 65 74 74 69 6e 67 73 2e 67 65 74 5f 61 72   settings.get_ar
0760: 72 61 79 28 22 64 6f 6d 61 69 6e 73 22 29 2e 73  ray("domains").s
0770: 74 61 63 6b 28 29 3f 3b 0a 09 09 66 6f 72 20 64  tack()?;...for d
0780: 6f 6d 61 69 6e 20 69 6e 20 65 78 74 72 61 5f 64  omain in extra_d
0790: 6f 6d 61 69 6e 73 20 7b 0a 09 09 09 6c 65 74 20  omains {....let 
07a0: 64 6f 6d 61 69 6e 20 3d 20 64 6f 6d 61 69 6e 2e  domain = domain.
07b0: 74 6f 5f 73 74 72 69 6e 67 28 29 2e 74 6f 5f 6c  to_string().to_l
07c0: 6f 77 65 72 63 61 73 65 28 29 3b 0a 09 09 09 69  owercase();....i
07d0: 66 20 52 45 5f 44 4f 4d 41 49 4e 2e 69 73 5f 6d  f RE_DOMAIN.is_m
07e0: 61 74 63 68 28 26 64 6f 6d 61 69 6e 29 20 7b 0a  atch(&domain) {.
07f0: 09 09 09 09 64 6f 6d 61 69 6e 73 2e 69 6e 73 65  ....domains.inse
0800: 72 74 28 64 6f 6d 61 69 6e 29 3b 0a 09 09 09 7d  rt(domain);....}
0810: 20 65 6c 73 65 20 7b 0a 09 09 09 09 70 61 6e 69   else {.....pani
0820: 63 21 28 22 5b 73 6d 74 70 32 74 67 2e 74 6f 6d  c!("[smtp2tg.tom
0830: 6c 5d 20 63 61 6e 27 74 20 63 68 65 63 6b 20 6f  l] can't check o
0840: 66 20 64 6f 6d 61 69 6e 73 20 69 6e 20 5c 22 64  f domains in \"d
0850: 6f 6d 61 69 6e 73 5c 22 3a 20 7b 64 6f 6d 61 69  omains\": {domai
0860: 6e 7d 22 29 3b 0a 09 09 09 7d 0a 09 09 7d 0a 09  n}");....}...}..
0870: 09 6c 65 74 20 64 6f 6d 61 69 6e 73 20 3d 20 64  .let domains = d
0880: 6f 6d 61 69 6e 73 2e 69 6e 74 6f 5f 69 74 65 72  omains.into_iter
0890: 28 29 2e 6d 61 70 28 7c 73 7c 20 65 73 63 61 70  ().map(|s| escap
08a0: 65 28 26 73 29 29 0a 09 09 09 2e 63 6f 6c 6c 65  e(&s)).....colle
08b0: 63 74 3a 3a 3c 56 65 63 3c 53 74 72 69 6e 67 3e  ct::<Vec<String>
08c0: 3e 28 29 2e 6a 6f 69 6e 28 22 7c 22 29 3b 0a 09  >().join("|");..
08d0: 09 6c 65 74 20 61 64 64 72 65 73 73 20 3d 20 52  .let address = R
08e0: 65 67 65 78 3a 3a 6e 65 77 28 26 66 6f 72 6d 61  egex::new(&forma
08f0: 74 21 28 22 5e 28 3f 50 3c 75 73 65 72 3e 5b 61  t!("^(?P<user>[a
0900: 2d 7a 30 2d 39 5d 5b 2d 61 2d 7a 30 2d 39 5d 29  -z0-9][-a-z0-9])
0910: 28 40 28 7b 64 6f 6d 61 69 6e 73 7d 29 29 24 22  (@({domains}))$"
0920: 29 29 2e 73 74 61 63 6b 28 29 3f 3b 0a 09 09 6c  )).stack()?;...l
0930: 65 74 20 72 65 6c 61 79 20 3d 20 6d 61 74 63 68  et relay = match
0940: 20 73 65 74 74 69 6e 67 73 2e 67 65 74 5f 73 74   settings.get_st
0950: 72 69 6e 67 28 22 75 6e 6b 6e 6f 77 6e 22 29 0a  ring("unknown").
0960: 09 09 09 2e 63 6f 6e 74 65 78 74 28 22 5b 73 6d  ....context("[sm
0970: 74 70 32 74 67 2e 74 6f 6d 6c 5d 20 63 61 6e 27  tp2tg.toml] can'
0980: 74 20 67 65 74 20 5c 22 75 6e 6b 6e 6f 77 6e 5c  t get \"unknown\
0990: 22 20 70 6f 6c 69 63 79 2e 5c 6e 22 29 3f 2e 61  " policy.\n")?.a
09a0: 73 5f 73 74 72 28 29 0a 09 09 7b 0a 09 09 09 22  s_str()...{...."
09b0: 72 65 6c 61 79 22 20 3d 3e 20 74 72 75 65 2c 0a  relay" => true,.
09c0: 09 09 09 22 64 65 6e 79 22 20 3d 3e 20 66 61 6c  ..."deny" => fal
09d0: 73 65 2c 0a 09 09 09 5f 20 3d 3e 20 7b 0a 09 09  se,...._ => {...
09e0: 09 09 62 61 69 6c 21 28 22 5b 73 6d 74 70 32 74  ..bail!("[smtp2t
09f0: 67 2e 74 6f 6d 6c 5d 20 5c 22 75 6e 6b 6e 6f 77  g.toml] \"unknow
0a00: 6e 5c 22 20 73 68 6f 75 6c 64 20 62 65 20 65 69  n\" should be ei
0a10: 74 68 65 72 20 5c 22 72 65 6c 61 79 5c 22 20 6f  ther \"relay\" o
0a20: 72 20 5c 22 64 65 6e 79 5c 22 2e 5c 6e 22 29 3b  r \"deny\".\n");
0a30: 0a 09 09 09 7d 2c 0a 09 09 7d 3b 0a 0a 09 09 4f  ....},...};....O
0a40: 6b 28 4d 61 69 6c 53 65 72 76 65 72 20 7b 0a 09  k(MailServer {..
0a50: 09 09 64 61 74 61 3a 20 76 65 63 21 28 29 2c 0a  ..data: vec!(),.
0a60: 09 09 09 68 65 61 64 65 72 73 3a 20 4e 6f 6e 65  ...headers: None
0a70: 2c 0a 09 09 09 72 65 6c 61 79 2c 0a 09 09 09 74  ,....relay,....t
0a80: 67 2c 0a 09 09 09 66 69 65 6c 64 73 2c 0a 09 09  g,....fields,...
0a90: 09 61 64 64 72 65 73 73 2c 0a 09 09 7d 29 0a 09  .address,...})..
0aa0: 7d 0a 0a 09 2f 2f 2f 20 52 65 74 75 72 6e 73 20  }.../// Returns 
0ab0: 69 64 20 66 6f 72 20 70 72 6f 76 69 64 65 64 20  id for provided 
0ac0: 65 6d 61 69 6c 20 61 64 64 72 65 73 73 0a 09 66  email address..f
0ad0: 6e 20 67 65 74 5f 69 64 20 28 26 73 65 6c 66 2c  n get_id (&self,
0ae0: 20 6e 61 6d 65 3a 20 26 73 74 72 29 20 2d 3e 20   name: &str) -> 
0af0: 52 65 73 75 6c 74 3c 26 43 68 61 74 50 65 65 72  Result<&ChatPeer
0b00: 49 64 3e 20 7b 0a 09 09 2f 2f 20 68 65 72 65 20  Id> {...// here 
0b10: 77 65 20 6e 65 65 64 20 74 6f 20 73 74 6f 72 65  we need to store
0b20: 20 53 74 72 69 6e 67 20 6c 6f 63 61 6c 6c 79 20   String locally 
0b30: 74 6f 20 62 6f 72 72 6f 77 20 69 74 20 61 66 74  to borrow it aft
0b40: 65 72 0a 09 09 6c 65 74 20 6d 75 74 20 6c 69 6e  er...let mut lin
0b50: 6b 20 3d 20 6e 61 6d 65 3b 0a 09 09 6c 65 74 20  k = name;...let 
0b60: 6e 61 6d 65 3a 20 53 74 72 69 6e 67 3b 0a 09 09  name: String;...
0b70: 69 66 20 6c 65 74 20 53 6f 6d 65 28 63 61 70 73  if let Some(caps
0b80: 29 20 3d 20 73 65 6c 66 2e 61 64 64 72 65 73 73  ) = self.address
0b90: 2e 63 61 70 74 75 72 65 73 28 6c 69 6e 6b 29 20  .captures(link) 
0ba0: 7b 0a 09 09 09 6e 61 6d 65 20 3d 20 63 61 70 73  {....name = caps
0bb0: 5b 22 6e 61 6d 65 22 5d 2e 74 6f 5f 73 74 72 69  ["name"].to_stri
0bc0: 6e 67 28 29 3b 0a 09 09 09 6c 69 6e 6b 20 3d 20  ng();....link = 
0bd0: 26 6e 61 6d 65 3b 0a 09 09 7d 0a 09 09 6d 61 74  &name;...}...mat
0be0: 63 68 20 73 65 6c 66 2e 74 67 2e 67 65 74 28 6c  ch self.tg.get(l
0bf0: 69 6e 6b 29 20 7b 0a 09 09 09 4f 6b 28 61 64 64  ink) {....Ok(add
0c00: 72 29 20 3d 3e 20 4f 6b 28 61 64 64 72 29 2c 0a  r) => Ok(addr),.
0c10: 09 09 09 45 72 72 28 5f 29 20 3d 3e 20 4f 6b 28  ...Err(_) => Ok(
0c20: 26 73 65 6c 66 2e 74 67 2e 64 65 66 61 75 6c 74  &self.tg.default
0c30: 29 2c 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2f 2f 20  ),...}..}.../// 
0c40: 41 74 74 65 6d 70 74 20 74 6f 20 64 65 6c 69 76  Attempt to deliv
0c50: 65 72 20 6f 6e 65 20 6d 65 73 73 61 67 65 0a 09  er one message..
0c60: 61 73 79 6e 63 20 66 6e 20 72 65 6c 61 79 5f 6d  async fn relay_m
0c70: 61 69 6c 20 28 26 73 65 6c 66 29 20 2d 3e 20 52  ail (&self) -> R
0c80: 65 73 75 6c 74 3c 28 29 3e 20 7b 0a 09 09 69 66  esult<()> {...if
0c90: 20 6c 65 74 20 53 6f 6d 65 28 68 65 61 64 65 72   let Some(header
0ca0: 73 29 20 3d 20 26 73 65 6c 66 2e 68 65 61 64 65  s) = &self.heade
0cb0: 72 73 20 7b 0a 09 09 09 6c 65 74 20 6d 61 69 6c  rs {....let mail
0cc0: 20 3d 20 6d 61 69 6c 5f 70 61 72 73 65 72 3a 3a   = mail_parser::
0cd0: 4d 65 73 73 61 67 65 50 61 72 73 65 72 3a 3a 6e  MessageParser::n
0ce0: 65 77 28 29 2e 70 61 72 73 65 28 26 73 65 6c 66  ew().parse(&self
0cf0: 2e 64 61 74 61 29 0a 09 09 09 09 2e 63 6f 6e 74  .data)......cont
0d00: 65 78 74 28 22 46 61 69 6c 65 64 20 74 6f 20 70  ext("Failed to p
0d10: 61 72 73 65 20 6d 61 69 6c 2e 22 29 3f 3b 0a 0a  arse mail.")?;..
0d20: 09 09 09 2f 2f 20 41 64 64 69 6e 67 20 61 6c 6c  ...// Adding all
0d30: 20 6b 6e 6f 77 6e 20 61 64 64 72 65 73 73 65 73   known addresses
0d40: 20 74 6f 20 72 65 63 69 70 69 65 6e 74 20 6c 69   to recipient li
0d50: 73 74 2c 20 66 6f 72 20 61 6e 79 6f 6e 65 20 65  st, for anyone e
0d60: 6c 73 65 20 61 64 64 69 6e 67 20 64 65 66 61 75  lse adding defau
0d70: 6c 74 0a 09 09 09 2f 2f 20 41 6c 73 6f 20 69 66  lt....// Also if
0d80: 20 6c 69 73 74 20 69 73 20 65 6d 70 74 79 20 61   list is empty a
0d90: 6c 73 6f 20 61 64 64 69 6e 67 20 64 65 66 61 75  lso adding defau
0da0: 6c 74 0a 09 09 09 6c 65 74 20 6d 75 74 20 72 63  lt....let mut rc
0db0: 70 74 3a 20 48 61 73 68 53 65 74 3c 26 43 68 61  pt: HashSet<&Cha
0dc0: 74 50 65 65 72 49 64 3e 20 3d 20 48 61 73 68 53  tPeerId> = HashS
0dd0: 65 74 3a 3a 6e 65 77 28 29 3b 0a 09 09 09 69 66  et::new();....if
0de0: 20 68 65 61 64 65 72 73 2e 74 6f 2e 69 73 5f 65   headers.to.is_e
0df0: 6d 70 74 79 28 29 20 26 26 20 21 73 65 6c 66 2e  mpty() && !self.
0e00: 72 65 6c 61 79 20 7b 0a 09 09 09 09 62 61 69 6c  relay {.....bail
0e10: 21 28 22 52 65 6c 61 79 69 6e 67 20 69 73 20 64  !("Relaying is d
0e20: 69 73 61 62 6c 65 64 2c 20 61 6e 64 20 74 68 65  isabled, and the
0e30: 72 65 27 73 20 6e 6f 20 64 65 73 74 69 6e 61 74  re's no destinat
0e40: 69 6f 6e 20 61 64 64 72 65 73 73 22 29 3b 0a 09  ion address");..
0e50: 09 09 7d 0a 09 09 09 66 6f 72 20 69 74 65 6d 20  ..}....for item 
0e60: 69 6e 20 26 68 65 61 64 65 72 73 2e 74 6f 20 7b  in &headers.to {
0e70: 0a 09 09 09 09 72 63 70 74 2e 69 6e 73 65 72 74  .....rcpt.insert
0e80: 28 73 65 6c 66 2e 67 65 74 5f 69 64 28 69 74 65  (self.get_id(ite
0e90: 6d 29 3f 29 3b 0a 09 09 09 7d 3b 0a 09 09 09 69  m)?);....};....i
0ea0: 66 20 72 63 70 74 2e 69 73 5f 65 6d 70 74 79 28  f rcpt.is_empty(
0eb0: 29 20 7b 0a 09 09 09 09 73 65 6c 66 2e 74 67 2e  ) {.....self.tg.
0ec0: 64 65 62 75 67 28 22 4e 6f 20 72 65 63 69 70 69  debug("No recipi
0ed0: 65 6e 74 20 6f 72 20 65 6e 76 65 6c 6f 70 65 20  ent or envelope 
0ee0: 61 64 64 72 65 73 73 2e 22 29 2e 61 77 61 69 74  address.").await
0ef0: 3f 3b 0a 09 09 09 09 72 63 70 74 2e 69 6e 73 65  ?;.....rcpt.inse
0f00: 72 74 28 26 73 65 6c 66 2e 74 67 2e 64 65 66 61  rt(&self.tg.defa
0f10: 75 6c 74 29 3b 0a 09 09 09 7d 3b 0a 0a 09 09 09  ult);....};.....
0f20: 2f 2f 20 70 72 65 70 61 74 69 6e 67 20 6d 65 73  // prepating mes
0f30: 73 61 67 65 20 68 65 61 64 65 72 0a 09 09 09 6c  sage header....l
0f40: 65 74 20 6d 75 74 20 72 65 70 6c 79 3a 20 56 65  et mut reply: Ve
0f50: 63 3c 53 74 72 69 6e 67 3e 20 3d 20 76 65 63 21  c<String> = vec!
0f60: 5b 5d 3b 0a 09 09 09 69 66 20 73 65 6c 66 2e 66  [];....if self.f
0f70: 69 65 6c 64 73 2e 63 6f 6e 74 61 69 6e 73 28 22  ields.contains("
0f80: 73 75 62 6a 65 63 74 22 29 20 7b 0a 09 09 09 09  subject") {.....
0f90: 69 66 20 6c 65 74 20 53 6f 6d 65 28 73 75 62 6a  if let Some(subj
0fa0: 65 63 74 29 20 3d 20 6d 61 69 6c 2e 73 75 62 6a  ect) = mail.subj
0fb0: 65 63 74 28 29 20 7b 0a 09 09 09 09 09 72 65 70  ect() {......rep
0fc0: 6c 79 2e 70 75 73 68 28 66 6f 72 6d 61 74 21 28  ly.push(format!(
0fd0: 22 5f 5f 2a 53 75 62 6a 65 63 74 3a 2a 5f 5f e2  "__*Subject:*__â
0fe0: 80 87 60 7b 7d 60 22 2c 20 65 6e 63 6f 64 65 28  €‡`{}`", encode(
0ff0: 73 75 62 6a 65 63 74 29 29 29 3b 0a 09 09 09 09  subject)));.....
1000: 7d 20 65 6c 73 65 20 69 66 20 6c 65 74 20 53 6f  } else if let So
1010: 6d 65 28 74 68 72 65 61 64 29 20 3d 20 6d 61 69  me(thread) = mai
1020: 6c 2e 74 68 72 65 61 64 5f 6e 61 6d 65 28 29 20  l.thread_name() 
1030: 7b 0a 09 09 09 09 09 72 65 70 6c 79 2e 70 75 73  {......reply.pus
1040: 68 28 66 6f 72 6d 61 74 21 28 22 5f 5f 2a 54 68  h(format!("__*Th
1050: 72 65 61 64 3a 2a 5f 5f e2 80 87 60 7b 7d 60 22  read:*__ `{}`"
1060: 2c 20 65 6e 63 6f 64 65 28 74 68 72 65 61 64 29  , encode(thread)
1070: 29 29 3b 0a 09 09 09 09 7d 0a 09 09 09 7d 0a 09  ));.....}....}..
1080: 09 09 6c 65 74 20 6d 75 74 20 73 68 6f 72 74 5f  ..let mut short_
1090: 68 65 61 64 65 72 73 3a 20 56 65 63 3c 53 74 72  headers: Vec<Str
10a0: 69 6e 67 3e 20 3d 20 76 65 63 21 5b 5d 3b 0a 09  ing> = vec![];..
10b0: 09 09 2f 2f 20 64 6f 20 77 65 20 6e 65 65 64 20  ..// do we need 
10c0: 74 6f 20 72 65 70 6c 61 63 65 20 73 70 61 63 65  to replace space
10d0: 73 20 68 65 72 65 3f 0a 09 09 09 69 66 20 73 65  s here?....if se
10e0: 6c 66 2e 66 69 65 6c 64 73 2e 63 6f 6e 74 61 69  lf.fields.contai
10f0: 6e 73 28 22 66 72 6f 6d 22 29 20 7b 0a 09 09 09  ns("from") {....
1100: 09 73 68 6f 72 74 5f 68 65 61 64 65 72 73 2e 70  .short_headers.p
1110: 75 73 68 28 66 6f 72 6d 61 74 21 28 22 5f 5f 2a  ush(format!("__*
1120: 46 72 6f 6d 3a 2a 5f 5f e2 80 87 60 7b 7d 60 22  From:*__ `{}`"
1130: 2c 20 65 6e 63 6f 64 65 28 26 68 65 61 64 65 72  , encode(&header
1140: 73 2e 66 72 6f 6d 29 29 29 3b 0a 09 09 09 7d 0a  s.from)));....}.
1150: 09 09 09 69 66 20 73 65 6c 66 2e 66 69 65 6c 64  ...if self.field
1160: 73 2e 63 6f 6e 74 61 69 6e 73 28 22 64 61 74 65  s.contains("date
1170: 22 29 20 7b 0a 09 09 09 09 69 66 20 6c 65 74 20  ") {.....if let 
1180: 53 6f 6d 65 28 64 61 74 65 29 20 3d 20 6d 61 69  Some(date) = mai
1190: 6c 2e 64 61 74 65 28 29 20 7b 0a 09 09 09 09 09  l.date() {......
11a0: 73 68 6f 72 74 5f 68 65 61 64 65 72 73 2e 70 75  short_headers.pu
11b0: 73 68 28 66 6f 72 6d 61 74 21 28 22 5f 5f 2a 44  sh(format!("__*D
11c0: 61 74 65 3a 2a 5f 5f e2 80 87 60 7b 64 61 74 65  ate:*__ `{date
11d0: 7d 60 22 29 29 3b 0a 09 09 09 09 7d 0a 09 09 09  }`"));.....}....
11e0: 7d 0a 09 09 09 72 65 70 6c 79 2e 70 75 73 68 28  }....reply.push(
11f0: 73 68 6f 72 74 5f 68 65 61 64 65 72 73 2e 6a 6f  short_headers.jo
1200: 69 6e 28 22 20 22 29 29 3b 0a 09 09 09 6c 65 74  in(" "));....let
1210: 20 68 65 61 64 65 72 5f 73 69 7a 65 20 3d 20 72   header_size = r
1220: 65 70 6c 79 2e 6a 6f 69 6e 28 22 20 22 29 2e 6c  eply.join(" ").l
1230: 65 6e 28 29 20 2b 20 31 3b 0a 0a 09 09 09 6c 65  en() + 1;.....le
1240: 74 20 68 74 6d 6c 5f 70 61 72 74 73 20 3d 20 6d  t html_parts = m
1250: 61 69 6c 2e 68 74 6d 6c 5f 62 6f 64 79 5f 63 6f  ail.html_body_co
1260: 75 6e 74 28 29 3b 0a 09 09 09 6c 65 74 20 74 65  unt();....let te
1270: 78 74 5f 70 61 72 74 73 20 3d 20 6d 61 69 6c 2e  xt_parts = mail.
1280: 74 65 78 74 5f 62 6f 64 79 5f 63 6f 75 6e 74 28  text_body_count(
1290: 29 3b 0a 09 09 09 6c 65 74 20 61 74 74 61 63 68  );....let attach
12a0: 6d 65 6e 74 73 20 3d 20 6d 61 69 6c 2e 61 74 74  ments = mail.att
12b0: 61 63 68 6d 65 6e 74 5f 63 6f 75 6e 74 28 29 3b  achment_count();
12c0: 0a 09 09 09 69 66 20 68 74 6d 6c 5f 70 61 72 74  ....if html_part
12d0: 73 20 21 3d 20 74 65 78 74 5f 70 61 72 74 73 20  s != text_parts 
12e0: 7b 0a 09 09 09 09 73 65 6c 66 2e 74 67 2e 64 65  {.....self.tg.de
12f0: 62 75 67 28 26 66 6f 72 6d 61 74 21 28 22 48 6d  bug(&format!("Hm
1300: 2c 20 77 65 20 68 61 76 65 20 7b 68 74 6d 6c 5f  , we have {html_
1310: 70 61 72 74 73 7d 20 48 54 4d 4c 20 70 61 72 74  parts} HTML part
1320: 73 20 61 6e 64 20 7b 74 65 78 74 5f 70 61 72 74  s and {text_part
1330: 73 7d 20 74 65 78 74 20 70 61 72 74 73 2e 22 29  s} text parts.")
1340: 29 2e 61 77 61 69 74 3f 3b 0a 09 09 09 7d 0a 09  ).await?;....}..
1350: 09 09 2f 2f 6c 65 74 20 6d 75 74 20 68 74 6d 6c  ..//let mut html
1360: 5f 6e 75 6d 20 3d 20 30 3b 0a 09 09 09 6c 65 74  _num = 0;....let
1370: 20 6d 75 74 20 74 65 78 74 5f 6e 75 6d 20 3d 20   mut text_num = 
1380: 30 3b 0a 09 09 09 6c 65 74 20 6d 75 74 20 66 69  0;....let mut fi
1390: 6c 65 5f 6e 75 6d 20 3d 20 30 3b 0a 09 09 09 2f  le_num = 0;..../
13a0: 2f 20 6c 65 74 27 73 20 64 69 73 70 6c 61 79 20  / let's display 
13b0: 66 69 72 73 74 20 68 74 6d 6c 20 6f 72 20 74 65  first html or te
13c0: 78 74 20 70 61 72 74 20 61 73 20 62 6f 64 79 0a  xt part as body.
13d0: 09 09 09 6c 65 74 20 6d 75 74 20 62 6f 64 79 3a  ...let mut body:
13e0: 20 43 6f 77 3c 27 5f 2c 20 73 74 72 3e 20 3d 20   Cow<'_, str> = 
13f0: 22 22 2e 69 6e 74 6f 28 29 3b 0a 09 09 09 2f 2a  "".into();..../*
1400: 0a 09 09 09 20 2a 20 61 63 74 75 61 6c 6c 79 20  .... * actually 
1410: 49 20 64 6f 6e 27 74 20 77 61 6e 6e 61 20 70 61  I don't wanna pa
1420: 72 73 65 20 74 68 61 74 20 68 74 6d 6c 20 73 74  rse that html st
1430: 75 66 66 0a 09 09 09 69 66 20 68 74 6d 6c 5f 70  uff....if html_p
1440: 61 72 74 73 20 3e 20 30 20 7b 0a 09 09 09 09 6c  arts > 0 {.....l
1450: 65 74 20 74 65 78 74 20 3d 20 6d 61 69 6c 2e 62  et text = mail.b
1460: 6f 64 79 5f 68 74 6d 6c 28 30 29 2e 73 74 61 63  ody_html(0).stac
1470: 6b 28 29 3f 3b 0a 09 09 09 09 69 66 20 74 65 78  k()?;.....if tex
1480: 74 2e 6c 65 6e 28 29 20 3c 20 34 30 39 36 20 2d  t.len() < 4096 -
1490: 20 68 65 61 64 65 72 5f 73 69 7a 65 20 7b 0a 09   header_size {..
14a0: 09 09 09 09 62 6f 64 79 20 3d 20 74 65 78 74 3b  ....body = text;
14b0: 0a 09 09 09 09 09 68 74 6d 6c 5f 6e 75 6d 20 3d  ......html_num =
14c0: 20 31 3b 0a 09 09 09 09 7d 0a 09 09 09 7d 3b 0a   1;.....}....};.
14d0: 09 09 09 2a 2f 0a 09 09 09 69 66 20 62 6f 64 79  ...*/....if body
14e0: 2e 69 73 5f 65 6d 70 74 79 28 29 20 26 26 20 74  .is_empty() && t
14f0: 65 78 74 5f 70 61 72 74 73 20 3e 20 30 20 7b 0a  ext_parts > 0 {.
1500: 09 09 09 09 6c 65 74 20 74 65 78 74 20 3d 20 6d  ....let text = m
1510: 61 69 6c 2e 62 6f 64 79 5f 74 65 78 74 28 30 29  ail.body_text(0)
1520: 0a 09 09 09 09 09 2e 63 6f 6e 74 65 78 74 28 22  .......context("
1530: 46 61 69 6c 65 64 20 74 6f 20 65 78 74 72 61 63  Failed to extrac
1540: 74 20 74 65 78 74 20 66 72 6f 6d 20 6d 65 73 73  t text from mess
1550: 61 67 65 22 29 3f 3b 0a 09 09 09 09 69 66 20 74  age")?;.....if t
1560: 65 78 74 2e 6c 65 6e 28 29 20 3c 20 34 30 39 36  ext.len() < 4096
1570: 20 2d 20 68 65 61 64 65 72 5f 73 69 7a 65 20 7b   - header_size {
1580: 0a 09 09 09 09 09 62 6f 64 79 20 3d 20 74 65 78  ......body = tex
1590: 74 3b 0a 09 09 09 09 09 74 65 78 74 5f 6e 75 6d  t;......text_num
15a0: 20 3d 20 31 3b 0a 09 09 09 09 7d 0a 09 09 09 7d   = 1;.....}....}
15b0: 3b 0a 09 09 09 72 65 70 6c 79 2e 70 75 73 68 28  ;....reply.push(
15c0: 22 60 60 60 22 2e 69 6e 74 6f 28 29 29 3b 0a 09  "```".into());..
15d0: 09 09 72 65 70 6c 79 2e 65 78 74 65 6e 64 28 62  ..reply.extend(b
15e0: 6f 64 79 2e 6c 69 6e 65 73 28 29 2e 6d 61 70 28  ody.lines().map(
15f0: 7c 78 7c 20 78 2e 69 6e 74 6f 28 29 29 29 3b 0a  |x| x.into()));.
1600: 09 09 09 72 65 70 6c 79 2e 70 75 73 68 28 22 60  ...reply.push("`
1610: 60 60 22 2e 69 6e 74 6f 28 29 29 3b 0a 0a 09 09  ``".into());....
1620: 09 2f 2f 20 61 6e 64 20 6c 65 74 27 73 20 63 6f  .// and let's co
1630: 6c 6c 65 63 74 20 61 6c 6c 20 6f 74 68 65 72 20  llect all other 
1640: 61 74 74 61 63 68 6d 65 6e 74 20 70 61 72 74 73  attachment parts
1650: 0a 09 09 09 6c 65 74 20 6d 75 74 20 66 69 6c 65  ....let mut file
1660: 73 5f 74 6f 5f 73 65 6e 64 20 3d 20 76 65 63 21  s_to_send = vec!
1670: 5b 5d 3b 0a 09 09 09 2f 2a 0a 09 09 09 20 2a 20  [];..../*.... * 
1680: 6c 65 74 27 73 20 6a 75 73 74 20 73 6b 69 70 20  let's just skip 
1690: 68 74 6d 6c 20 70 61 72 74 73 20 66 6f 72 20 6e  html parts for n
16a0: 6f 77 2c 20 74 68 65 79 20 6a 75 73 74 20 64 75  ow, they just du
16b0: 70 6c 69 63 61 74 65 20 74 65 78 74 3f 0a 09 09  plicate text?...
16c0: 09 77 68 69 6c 65 20 68 74 6d 6c 5f 6e 75 6d 20  .while html_num 
16d0: 3c 20 68 74 6d 6c 5f 70 61 72 74 73 20 7b 0a 09  < html_parts {..
16e0: 09 09 09 66 69 6c 65 73 5f 74 6f 5f 73 65 6e 64  ...files_to_send
16f0: 2e 70 75 73 68 28 6d 61 69 6c 2e 68 74 6d 6c 5f  .push(mail.html_
1700: 70 61 72 74 28 68 74 6d 6c 5f 6e 75 6d 29 2e 73  part(html_num).s
1710: 74 61 63 6b 28 29 3f 29 3b 0a 09 09 09 09 68 74  tack()?);.....ht
1720: 6d 6c 5f 6e 75 6d 20 2b 3d 20 31 3b 0a 09 09 09  ml_num += 1;....
1730: 7d 0a 09 09 09 2a 2f 0a 09 09 09 77 68 69 6c 65  }....*/....while
1740: 20 74 65 78 74 5f 6e 75 6d 20 3c 20 74 65 78 74   text_num < text
1750: 5f 70 61 72 74 73 20 7b 0a 09 09 09 09 66 69 6c  _parts {.....fil
1760: 65 73 5f 74 6f 5f 73 65 6e 64 2e 70 75 73 68 28  es_to_send.push(
1770: 6d 61 69 6c 2e 74 65 78 74 5f 70 61 72 74 28 74  mail.text_part(t
1780: 65 78 74 5f 6e 75 6d 2e 74 72 79 5f 69 6e 74 6f  ext_num.try_into
1790: 28 29 2e 73 74 61 63 6b 28 29 3f 29 0a 09 09 09  ().stack()?)....
17a0: 09 09 2e 63 6f 6e 74 65 78 74 28 22 46 61 69 6c  ...context("Fail
17b0: 65 64 20 74 6f 20 67 65 74 20 74 65 78 74 20 70  ed to get text p
17c0: 61 72 74 20 66 72 6f 6d 20 6d 65 73 73 61 67 65  art from message
17d0: 2e 22 29 3f 29 3b 0a 09 09 09 09 74 65 78 74 5f  .")?);.....text_
17e0: 6e 75 6d 20 2b 3d 20 31 3b 0a 09 09 09 7d 0a 09  num += 1;....}..
17f0: 09 09 77 68 69 6c 65 20 66 69 6c 65 5f 6e 75 6d  ..while file_num
1800: 20 3c 20 61 74 74 61 63 68 6d 65 6e 74 73 20 7b   < attachments {
1810: 0a 09 09 09 09 66 69 6c 65 73 5f 74 6f 5f 73 65  .....files_to_se
1820: 6e 64 2e 70 75 73 68 28 6d 61 69 6c 2e 61 74 74  nd.push(mail.att
1830: 61 63 68 6d 65 6e 74 28 66 69 6c 65 5f 6e 75 6d  achment(file_num
1840: 2e 74 72 79 5f 69 6e 74 6f 28 29 2e 73 74 61 63  .try_into().stac
1850: 6b 28 29 3f 29 0a 09 09 09 09 09 2e 63 6f 6e 74  k()?).......cont
1860: 65 78 74 28 22 46 61 69 6c 65 64 20 74 6f 20 67  ext("Failed to g
1870: 65 74 20 66 69 6c 65 20 70 61 72 74 20 66 72 6f  et file part fro
1880: 6d 20 6d 65 73 73 61 67 65 2e 22 29 3f 29 3b 0a  m message.")?);.
1890: 09 09 09 09 66 69 6c 65 5f 6e 75 6d 20 2b 3d 20  ....file_num += 
18a0: 31 3b 0a 09 09 09 7d 0a 0a 09 09 09 6c 65 74 20  1;....}.....let 
18b0: 6d 73 67 20 3d 20 72 65 70 6c 79 2e 6a 6f 69 6e  msg = reply.join
18c0: 28 22 5c 6e 22 29 3b 0a 09 09 09 66 6f 72 20 63  ("\n");....for c
18d0: 68 61 74 20 69 6e 20 72 63 70 74 20 7b 0a 09 09  hat in rcpt {...
18e0: 09 09 69 66 20 21 66 69 6c 65 73 5f 74 6f 5f 73  ..if !files_to_s
18f0: 65 6e 64 2e 69 73 5f 65 6d 70 74 79 28 29 20 7b  end.is_empty() {
1900: 0a 09 09 09 09 09 6c 65 74 20 6d 75 74 20 66 69  ......let mut fi
1910: 6c 65 73 20 3d 20 76 65 63 21 5b 5d 3b 0a 09 09  les = vec![];...
1920: 09 09 09 2f 2f 20 6c 65 74 20 6d 75 74 20 66 69  ...// let mut fi
1930: 72 73 74 5f 6f 6e 65 20 3d 20 74 72 75 65 3b 0a  rst_one = true;.
1940: 09 09 09 09 09 66 6f 72 20 63 68 75 6e 6b 20 69  .....for chunk i
1950: 6e 20 26 66 69 6c 65 73 5f 74 6f 5f 73 65 6e 64  n &files_to_send
1960: 20 7b 0a 09 09 09 09 09 09 6c 65 74 20 64 61 74   {.......let dat
1970: 61 3a 20 56 65 63 3c 75 38 3e 20 3d 20 63 68 75  a: Vec<u8> = chu
1980: 6e 6b 2e 63 6f 6e 74 65 6e 74 73 28 29 2e 74 6f  nk.contents().to
1990: 5f 76 65 63 28 29 3b 0a 09 09 09 09 09 09 6c 65  _vec();.......le
19a0: 74 20 6d 75 74 20 66 69 6c 65 6e 61 6d 65 3a 20  t mut filename: 
19b0: 4f 70 74 69 6f 6e 3c 53 74 72 69 6e 67 3e 20 3d  Option<String> =
19c0: 20 4e 6f 6e 65 3b 0a 09 09 09 09 09 09 66 6f 72   None;.......for
19d0: 20 68 65 61 64 65 72 20 69 6e 20 63 68 75 6e 6b   header in chunk
19e0: 2e 68 65 61 64 65 72 73 28 29 20 7b 0a 09 09 09  .headers() {....
19f0: 09 09 09 09 69 66 20 68 65 61 64 65 72 2e 6e 61  ....if header.na
1a00: 6d 65 28 29 20 3d 3d 20 22 43 6f 6e 74 65 6e 74  me() == "Content
1a10: 2d 54 79 70 65 22 20 7b 0a 09 09 09 09 09 09 09  -Type" {........
1a20: 09 6d 61 74 63 68 20 68 65 61 64 65 72 2e 76 61  .match header.va
1a30: 6c 75 65 28 29 20 7b 0a 09 09 09 09 09 09 09 09  lue() {.........
1a40: 09 6d 61 69 6c 5f 70 61 72 73 65 72 3a 3a 48 65  .mail_parser::He
1a50: 61 64 65 72 56 61 6c 75 65 3a 3a 43 6f 6e 74 65  aderValue::Conte
1a60: 6e 74 54 79 70 65 28 63 6f 6e 74 65 6e 74 74 79  ntType(contentty
1a70: 70 65 29 20 3d 3e 20 7b 0a 09 09 09 09 09 09 09  pe) => {........
1a80: 09 09 09 69 66 20 6c 65 74 20 53 6f 6d 65 28 66  ...if let Some(f
1a90: 6e 61 6d 65 29 20 3d 20 63 6f 6e 74 65 6e 74 74  name) = contentt
1aa0: 79 70 65 2e 61 74 74 72 69 62 75 74 65 28 22 6e  ype.attribute("n
1ab0: 61 6d 65 22 29 20 7b 0a 09 09 09 09 09 09 09 09  ame") {.........
1ac0: 09 09 09 66 69 6c 65 6e 61 6d 65 20 3d 20 53 6f  ...filename = So
1ad0: 6d 65 28 66 6e 61 6d 65 2e 74 6f 5f 6f 77 6e 65  me(fname.to_owne
1ae0: 64 28 29 29 3b 0a 09 09 09 09 09 09 09 09 09 09  d());...........
1af0: 7d 0a 09 09 09 09 09 09 09 09 09 7d 2c 0a 09 09  }..........},...
1b00: 09 09 09 09 09 09 09 5f 20 3d 3e 20 7b 0a 09 09  ......._ => {...
1b10: 09 09 09 09 09 09 09 09 73 65 6c 66 2e 74 67 2e  ........self.tg.
1b20: 64 65 62 75 67 28 22 41 74 74 61 63 68 6d 65 6e  debug("Attachmen
1b30: 74 20 68 61 73 20 62 61 64 20 43 6f 6e 74 65 6e  t has bad Conten
1b40: 74 54 79 70 65 20 68 65 61 64 65 72 2e 22 29 2e  tType header.").
1b50: 61 77 61 69 74 3f 3b 0a 09 09 09 09 09 09 09 09  await?;.........
1b60: 09 7d 2c 0a 09 09 09 09 09 09 09 09 7d 3b 0a 09  .},.........};..
1b70: 09 09 09 09 09 09 7d 3b 0a 09 09 09 09 09 09 7d  ......};.......}
1b80: 3b 0a 09 09 09 09 09 09 6c 65 74 20 66 69 6c 65  ;.......let file
1b90: 6e 61 6d 65 20 3d 20 69 66 20 6c 65 74 20 53 6f  name = if let So
1ba0: 6d 65 28 66 6e 61 6d 65 29 20 3d 20 66 69 6c 65  me(fname) = file
1bb0: 6e 61 6d 65 20 7b 0a 09 09 09 09 09 09 09 66 6e  name {........fn
1bc0: 61 6d 65 0a 09 09 09 09 09 09 7d 20 65 6c 73 65  ame.......} else
1bd0: 20 7b 0a 09 09 09 09 09 09 09 22 41 74 74 61 63   {........"Attac
1be0: 68 6d 65 6e 74 2e 74 78 74 22 2e 69 6e 74 6f 28  hment.txt".into(
1bf0: 29 0a 09 09 09 09 09 09 7d 3b 0a 09 09 09 09 09  ).......};......
1c00: 09 66 69 6c 65 73 2e 70 75 73 68 28 41 74 74 61  .files.push(Atta
1c10: 63 68 6d 65 6e 74 20 7b 0a 09 09 09 09 09 09 09  chment {........
1c20: 64 61 74 61 3a 20 43 75 72 73 6f 72 3a 3a 6e 65  data: Cursor::ne
1c30: 77 28 64 61 74 61 29 2c 0a 09 09 09 09 09 09 09  w(data),........
1c40: 6e 61 6d 65 3a 20 66 69 6c 65 6e 61 6d 65 2c 0a  name: filename,.
1c50: 09 09 09 09 09 09 7d 29 3b 0a 09 09 09 09 09 7d  ......});......}
1c60: 0a 09 09 09 09 09 73 65 6c 66 2e 74 67 2e 73 65  ......self.tg.se
1c70: 6e 64 67 72 6f 75 70 28 63 68 61 74 2c 20 66 69  ndgroup(chat, fi
1c80: 6c 65 73 2c 20 26 6d 73 67 29 2e 61 77 61 69 74  les, &msg).await
1c90: 3f 3b 0a 09 09 09 09 7d 20 65 6c 73 65 20 7b 0a  ?;.....} else {.
1ca0: 09 09 09 09 09 73 65 6c 66 2e 74 67 2e 73 65 6e  .....self.tg.sen
1cb0: 64 28 63 68 61 74 2c 20 26 6d 73 67 29 2e 61 77  d(chat, &msg).aw
1cc0: 61 69 74 3f 3b 0a 09 09 09 09 7d 0a 09 09 09 7d  ait?;.....}....}
1cd0: 0a 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09 62  ...} else {....b
1ce0: 61 69 6c 21 28 22 52 65 71 75 69 72 65 64 20 68  ail!("Required h
1cf0: 65 61 64 65 72 73 20 77 65 72 65 20 6e 6f 74 20  eaders were not 
1d00: 66 6f 75 6e 64 2e 22 29 3b 0a 09 09 7d 0a 09 09  found.");...}...
1d10: 4f 6b 28 28 29 29 0a 09 7d 0a 7d 0a 0a 69 6d 70  Ok(())..}.}..imp
1d20: 6c 20 6d 61 69 6c 69 6e 5f 65 6d 62 65 64 64 65  l mailin_embedde
1d30: 64 3a 3a 48 61 6e 64 6c 65 72 20 66 6f 72 20 4d  d::Handler for M
1d40: 61 69 6c 53 65 72 76 65 72 20 7b 0a 09 2f 2f 2f  ailServer {..///
1d50: 20 4a 75 73 74 20 64 65 6e 79 20 6c 6f 67 69 6e   Just deny login
1d60: 20 61 75 74 68 0a 09 66 6e 20 61 75 74 68 5f 6c   auth..fn auth_l
1d70: 6f 67 69 6e 20 28 26 6d 75 74 20 73 65 6c 66 2c  ogin (&mut self,
1d80: 20 5f 75 73 65 72 6e 61 6d 65 3a 20 26 73 74 72   _username: &str
1d90: 2c 20 5f 70 61 73 73 77 6f 72 64 3a 20 26 73 74  , _password: &st
1da0: 72 29 20 2d 3e 20 52 65 73 70 6f 6e 73 65 20 7b  r) -> Response {
1db0: 0a 09 09 49 4e 56 41 4c 49 44 5f 43 52 45 44 45  ...INVALID_CREDE
1dc0: 4e 54 49 41 4c 53 0a 09 7d 0a 0a 09 2f 2f 2f 20  NTIALS..}.../// 
1dd0: 4a 75 73 74 20 64 65 6e 79 20 70 6c 61 69 6e 20  Just deny plain 
1de0: 61 75 74 68 0a 09 66 6e 20 61 75 74 68 5f 70 6c  auth..fn auth_pl
1df0: 61 69 6e 20 28 26 6d 75 74 20 73 65 6c 66 2c 20  ain (&mut self, 
1e00: 5f 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 5f 69  _authorization_i
1e10: 64 3a 20 26 73 74 72 2c 20 5f 61 75 74 68 65 6e  d: &str, _authen
1e20: 74 69 63 61 74 69 6f 6e 5f 69 64 3a 20 26 73 74  tication_id: &st
1e30: 72 2c 20 5f 70 61 73 73 77 6f 72 64 3a 20 26 73  r, _password: &s
1e40: 74 72 29 20 2d 3e 20 52 65 73 70 6f 6e 73 65 20  tr) -> Response 
1e50: 7b 0a 09 09 49 4e 56 41 4c 49 44 5f 43 52 45 44  {...INVALID_CRED
1e60: 45 4e 54 49 41 4c 53 0a 09 7d 0a 0a 09 2f 2f 2f  ENTIALS..}...///
1e70: 20 56 65 72 69 66 79 20 77 68 65 74 68 65 72 20   Verify whether 
1e80: 61 64 64 72 65 73 73 20 69 73 20 64 65 6c 69 76  address is deliv
1e90: 65 72 61 62 6c 65 0a 09 66 6e 20 72 63 70 74 20  erable..fn rcpt 
1ea0: 28 26 6d 75 74 20 73 65 6c 66 2c 20 74 6f 3a 20  (&mut self, to: 
1eb0: 26 73 74 72 29 20 2d 3e 20 52 65 73 70 6f 6e 73  &str) -> Respons
1ec0: 65 20 7b 0a 09 09 69 66 20 73 65 6c 66 2e 72 65  e {...if self.re
1ed0: 6c 61 79 20 7b 0a 09 09 09 4f 4b 0a 09 09 7d 20  lay {....OK...} 
1ee0: 65 6c 73 65 20 7b 0a 09 09 09 6d 61 74 63 68 20  else {....match 
1ef0: 73 65 6c 66 2e 67 65 74 5f 69 64 28 74 6f 29 20  self.get_id(to) 
1f00: 7b 0a 09 09 09 09 4f 6b 28 5f 29 20 3d 3e 20 4f  {.....Ok(_) => O
1f10: 4b 2c 0a 09 09 09 09 45 72 72 28 5f 29 20 3d 3e  K,.....Err(_) =>
1f20: 20 7b 0a 09 09 09 09 09 69 66 20 73 65 6c 66 2e   {......if self.
1f30: 72 65 6c 61 79 20 7b 0a 09 09 09 09 09 09 4f 4b  relay {.......OK
1f40: 0a 09 09 09 09 09 7d 20 65 6c 73 65 20 7b 0a 09  ......} else {..
1f50: 09 09 09 09 09 4e 4f 5f 4d 41 49 4c 42 4f 58 0a  .....NO_MAILBOX.
1f60: 09 09 09 09 09 7d 0a 09 09 09 09 7d 0a 09 09 09  .....}.....}....
1f70: 7d 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2f 2f 20 53  }...}..}.../// S
1f80: 61 76 65 20 68 65 61 64 65 72 73 20 77 65 20 6e  ave headers we n
1f90: 65 65 64 0a 09 66 6e 20 64 61 74 61 5f 73 74 61  eed..fn data_sta
1fa0: 72 74 20 28 26 6d 75 74 20 73 65 6c 66 2c 20 5f  rt (&mut self, _
1fb0: 64 6f 6d 61 69 6e 3a 20 26 73 74 72 2c 20 66 72  domain: &str, fr
1fc0: 6f 6d 3a 20 26 73 74 72 2c 20 5f 69 73 38 62 69  om: &str, _is8bi
1fd0: 74 3a 20 62 6f 6f 6c 2c 20 74 6f 3a 20 26 5b 53  t: bool, to: &[S
1fe0: 74 72 69 6e 67 5d 29 20 2d 3e 20 52 65 73 70 6f  tring]) -> Respo
1ff0: 6e 73 65 20 7b 0a 09 09 73 65 6c 66 2e 68 65 61  nse {...self.hea
2000: 64 65 72 73 20 3d 20 53 6f 6d 65 28 53 6f 6d 65  ders = Some(Some
2010: 48 65 61 64 65 72 73 7b 0a 09 09 09 66 72 6f 6d  Headers{....from
2020: 3a 20 66 72 6f 6d 2e 74 6f 5f 73 74 72 69 6e 67  : from.to_string
2030: 28 29 2c 0a 09 09 09 74 6f 3a 20 74 6f 2e 74 6f  (),....to: to.to
2040: 5f 76 65 63 28 29 2c 0a 09 09 7d 29 3b 0a 09 09  _vec(),...});...
2050: 4f 4b 0a 09 7d 0a 0a 09 2f 2f 2f 20 53 61 76 65  OK..}.../// Save
2060: 20 63 68 75 6e 6b 28 3f 29 20 6f 66 20 64 61 74   chunk(?) of dat
2070: 61 0a 09 66 6e 20 64 61 74 61 20 28 26 6d 75 74  a..fn data (&mut
2080: 20 73 65 6c 66 2c 20 62 75 66 3a 20 26 5b 75 38   self, buf: &[u8
2090: 5d 29 20 2d 3e 20 73 74 64 3a 3a 72 65 73 75 6c  ]) -> std::resul
20a0: 74 3a 3a 52 65 73 75 6c 74 3c 28 29 2c 20 45 72  t::Result<(), Er
20b0: 72 6f 72 3e 20 7b 0a 09 09 73 65 6c 66 2e 64 61  ror> {...self.da
20c0: 74 61 2e 61 70 70 65 6e 64 28 62 75 66 2e 74 6f  ta.append(buf.to
20d0: 5f 76 65 63 28 29 2e 61 73 5f 6d 75 74 28 29 29  _vec().as_mut())
20e0: 3b 0a 09 09 4f 6b 28 28 29 29 0a 09 7d 0a 0a 09  ;...Ok(())..}...
20f0: 2f 2f 2f 20 41 74 74 65 6d 70 74 20 74 6f 20 73  /// Attempt to s
2100: 65 6e 64 20 65 6d 61 69 6c 2c 20 72 65 74 75 72  end email, retur
2110: 6e 20 74 65 6d 70 6f 72 61 72 79 20 65 72 72 6f  n temporary erro
2120: 72 20 69 66 20 74 68 61 74 20 66 61 69 6c 73 0a  r if that fails.
2130: 09 66 6e 20 64 61 74 61 5f 65 6e 64 20 28 26 6d  .fn data_end (&m
2140: 75 74 20 73 65 6c 66 29 20 2d 3e 20 52 65 73 70  ut self) -> Resp
2150: 6f 6e 73 65 20 7b 0a 09 09 6c 65 74 20 6d 75 74  onse {...let mut
2160: 20 72 65 73 75 6c 74 20 3d 20 4f 4b 3b 0a 09 09   result = OK;...
2170: 74 61 73 6b 3a 3a 62 6c 6f 63 6b 5f 6f 6e 28 61  task::block_on(a
2180: 73 79 6e 63 20 7b 0a 09 09 09 2f 2f 20 72 65 6c  sync {....// rel
2190: 61 79 20 6d 61 69 6c 0a 09 09 09 69 66 20 6c 65  ay mail....if le
21a0: 74 20 45 72 72 28 65 72 72 29 20 3d 20 73 65 6c  t Err(err) = sel
21b0: 66 2e 72 65 6c 61 79 5f 6d 61 69 6c 28 29 2e 61  f.relay_mail().a
21c0: 77 61 69 74 20 7b 0a 09 09 09 09 72 65 73 75 6c  wait {.....resul
21d0: 74 20 3d 20 49 4e 54 45 52 4e 41 4c 5f 45 52 52  t = INTERNAL_ERR
21e0: 4f 52 3b 0a 09 09 09 09 2f 2f 20 69 6e 20 63 61  OR;.....// in ca
21f0: 73 65 20 74 68 61 74 20 66 61 69 6c 73 20 2d 20  se that fails - 
2200: 69 6e 66 6f 72 6d 20 64 65 66 61 75 6c 74 20 72  inform default r
2210: 65 63 69 70 69 65 6e 74 0a 09 09 09 09 69 66 20  ecipient.....if 
2220: 6c 65 74 20 45 72 72 28 65 72 72 29 20 3d 20 73  let Err(err) = s
2230: 65 6c 66 2e 74 67 2e 64 65 62 75 67 28 26 66 6f  elf.tg.debug(&fo
2240: 72 6d 61 74 21 28 22 53 65 6e 64 69 6e 67 20 65  rmat!("Sending e
2250: 6d 61 69 6c 73 20 66 61 69 6c 65 64 3a 5c 6e 7b  mails failed:\n{
2260: 65 72 72 3a 3f 7d 22 29 29 2e 61 77 61 69 74 20  err:?}")).await 
2270: 7b 0a 09 09 09 09 09 2f 2f 20 69 6e 20 63 61 73  {......// in cas
2280: 65 20 74 68 61 74 20 61 6c 73 6f 20 66 61 69 6c  e that also fail
2290: 73 20 2d 20 77 72 69 74 65 20 73 6f 6d 65 20 6c  s - write some l
22a0: 6f 67 73 20 61 6e 64 20 62 61 69 6c 0a 09 09 09  ogs and bail....
22b0: 09 09 65 70 72 69 6e 74 6c 6e 21 28 22 7b 65 72  ..eprintln!("{er
22c0: 72 3a 3f 7d 22 29 3b 0a 09 09 09 09 7d 3b 0a 09  r:?}");.....};..
22d0: 09 09 7d 3b 0a 09 09 7d 29 3b 0a 09 09 2f 2f 20  ..};...});...// 
22e0: 63 6c 65 61 72 20 2d 20 6a 75 73 74 20 69 6e 20  clear - just in 
22f0: 63 61 73 65 0a 09 09 73 65 6c 66 2e 64 61 74 61  case...self.data
2300: 20 3d 20 76 65 63 21 5b 5d 3b 0a 09 09 73 65 6c   = vec![];...sel
2310: 66 2e 68 65 61 64 65 72 73 20 3d 20 4e 6f 6e 65  f.headers = None
2320: 3b 0a 09 09 72 65 73 75 6c 74 0a 09 7d 0a 7d 0a  ;...result..}.}.