Hex Artifact Content
Logged in as anonymous

Artifact 7b99a09150e15eaae92a9ff01fff135d228dc73ede392201e503ffbdd1a491cf:


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 54 65 6c 65 67 72 61 6d 54 72 61 6e 73 70 6f  :TelegramTranspo
0030: 72 74 2c 0a 09 75 74 69 6c 73 3a 3a 7b 0a 09 09  rt,..utils::{...
0040: 41 74 74 61 63 68 6d 65 6e 74 2c 0a 09 09 52 45  Attachment,...RE
0050: 5f 44 4f 4d 41 49 4e 2c 0a 09 09 76 61 6c 69 64  _DOMAIN,...valid
0060: 61 74 65 2c 0a 09 7d 2c 0a 7d 3b 0a 0a 75 73 65  ate,..},.};..use
0070: 20 73 74 64 3a 3a 7b 0a 09 63 6f 6c 6c 65 63 74   std::{..collect
0080: 69 6f 6e 73 3a 3a 7b 0a 09 09 48 61 73 68 4d 61  ions::{...HashMa
0090: 70 2c 0a 09 09 48 61 73 68 53 65 74 2c 0a 09 7d  p,...HashSet,..}
00a0: 2c 0a 09 69 6f 3a 3a 45 72 72 6f 72 2c 0a 09 73  ,..io::Error,..s
00b0: 79 6e 63 3a 3a 41 72 63 2c 0a 7d 3b 0a 0a 75 73  ync::Arc,.};..us
00c0: 65 20 61 73 79 6e 63 5f 63 6f 6d 70 61 74 3a 3a  e async_compat::
00d0: 43 6f 6d 70 61 74 3b 0a 75 73 65 20 6d 61 69 6c  Compat;.use mail
00e0: 69 6e 5f 65 6d 62 65 64 64 65 64 3a 3a 7b 0a 09  in_embedded::{..
00f0: 52 65 73 70 6f 6e 73 65 2c 0a 09 72 65 73 70 6f  Response,..respo
0100: 6e 73 65 3a 3a 7b 0a 09 09 49 4e 54 45 52 4e 41  nse::{...INTERNA
0110: 4c 5f 45 52 52 4f 52 2c 0a 09 09 49 4e 56 41 4c  L_ERROR,...INVAL
0120: 49 44 5f 43 52 45 44 45 4e 54 49 41 4c 53 2c 0a  ID_CREDENTIALS,.
0130: 09 09 4e 4f 5f 4d 41 49 4c 42 4f 58 2c 0a 09 09  ..NO_MAILBOX,...
0140: 4f 4b 0a 09 7d 2c 0a 7d 3b 0a 75 73 65 20 72 65  OK..},.};.use re
0150: 67 65 78 3a 3a 7b 0a 09 52 65 67 65 78 2c 0a 09  gex::{..Regex,..
0160: 65 73 63 61 70 65 2c 0a 7d 3b 0a 75 73 65 20 73  escape,.};.use s
0170: 74 61 63 6b 65 64 5f 65 72 72 6f 72 73 3a 3a 7b  tacked_errors::{
0180: 0a 09 52 65 73 75 6c 74 2c 0a 09 53 74 61 63 6b  ..Result,..Stack
0190: 61 62 6c 65 45 72 72 2c 0a 09 62 61 69 6c 2c 0a  ableErr,..bail,.
01a0: 7d 3b 0a 75 73 65 20 74 67 62 6f 74 3a 3a 74 79  };.use tgbot::ty
01b0: 70 65 73 3a 3a 43 68 61 74 50 65 65 72 49 64 3b  pes::ChatPeerId;
01c0: 0a 0a 2f 2f 2f 20 60 53 6f 6d 65 48 65 61 64 65  ../// `SomeHeade
01d0: 72 73 60 20 6f 62 6a 65 63 74 20 74 6f 20 73 74  rs` object to st
01e0: 6f 72 65 20 64 61 74 61 20 74 68 72 6f 75 67 68  ore data through
01f0: 20 53 4d 54 50 20 73 65 73 73 69 6f 6e 0a 23 5b   SMTP session.#[
0200: 64 65 72 69 76 65 28 43 6c 6f 6e 65 2c 20 44 65  derive(Clone, De
0210: 62 75 67 29 5d 0a 73 74 72 75 63 74 20 53 6f 6d  bug)].struct Som
0220: 65 48 65 61 64 65 72 73 20 7b 0a 09 66 72 6f 6d  eHeaders {..from
0230: 3a 20 53 74 72 69 6e 67 2c 0a 09 74 6f 3a 20 56  : String,..to: V
0240: 65 63 3c 53 74 72 69 6e 67 3e 2c 0a 7d 0a 0a 2f  ec<String>,.}../
0250: 2f 2f 20 60 4d 61 69 6c 53 65 72 76 65 72 60 20  // `MailServer` 
0260: 43 65 6e 74 72 61 6c 20 6f 62 6a 65 63 74 20 77  Central object w
0270: 69 74 68 20 54 47 20 61 70 69 20 61 6e 64 20 63  ith TG api and c
0280: 6f 6e 66 69 67 75 72 61 74 69 6f 6e 0a 23 5b 64  onfiguration.#[d
0290: 65 72 69 76 65 28 43 6c 6f 6e 65 2c 20 44 65 62  erive(Clone, Deb
02a0: 75 67 29 5d 0a 70 75 62 20 73 74 72 75 63 74 20  ug)].pub struct 
02b0: 4d 61 69 6c 53 65 72 76 65 72 20 7b 0a 09 64 61  MailServer {..da
02c0: 74 61 3a 20 56 65 63 3c 75 38 3e 2c 0a 09 68 65  ta: Vec<u8>,..he
02d0: 61 64 65 72 73 3a 20 4f 70 74 69 6f 6e 3c 53 6f  aders: Option<So
02e0: 6d 65 48 65 61 64 65 72 73 3e 2c 0a 09 72 65 6c  meHeaders>,..rel
02f0: 61 79 3a 20 62 6f 6f 6c 2c 0a 09 74 67 3a 20 41  ay: bool,..tg: A
0300: 72 63 3c 54 65 6c 65 67 72 61 6d 54 72 61 6e 73  rc<TelegramTrans
0310: 70 6f 72 74 3e 2c 0a 09 66 69 65 6c 64 73 3a 20  port>,..fields: 
0320: 48 61 73 68 53 65 74 3c 53 74 72 69 6e 67 3e 2c  HashSet<String>,
0330: 0a 09 61 64 64 72 65 73 73 3a 20 52 65 67 65 78  ..address: Regex
0340: 2c 0a 7d 0a 0a 69 6d 70 6c 20 4d 61 69 6c 53 65  ,.}..impl MailSe
0350: 72 76 65 72 20 7b 0a 09 2f 2f 2f 20 49 6e 69 74  rver {../// Init
0360: 69 61 6c 69 7a 65 20 41 50 49 20 61 6e 64 20 72  ialize API and r
0370: 65 61 64 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  ead configuratio
0380: 6e 0a 09 70 75 62 20 66 6e 20 6e 65 77 28 73 65  n..pub fn new(se
0390: 74 74 69 6e 67 73 3a 20 63 6f 6e 66 69 67 3a 3a  ttings: config::
03a0: 43 6f 6e 66 69 67 29 20 2d 3e 20 52 65 73 75 6c  Config) -> Resul
03b0: 74 3c 4d 61 69 6c 53 65 72 76 65 72 3e 20 7b 0a  t<MailServer> {.
03c0: 09 09 6c 65 74 20 61 70 69 5f 6b 65 79 20 3d 20  ..let api_key = 
03d0: 73 65 74 74 69 6e 67 73 2e 67 65 74 5f 73 74 72  settings.get_str
03e0: 69 6e 67 28 22 61 70 69 5f 6b 65 79 22 29 0a 09  ing("api_key")..
03f0: 09 09 2e 63 6f 6e 74 65 78 74 28 22 5b 73 6d 74  ...context("[smt
0400: 70 32 74 67 2e 74 6f 6d 6c 5d 20 6d 69 73 73 69  p2tg.toml] missi
0410: 6e 67 20 5c 22 61 70 69 5f 6b 65 79 5c 22 20 70  ng \"api_key\" p
0420: 61 72 61 6d 65 74 65 72 2e 5c 6e 22 29 3f 3b 0a  arameter.\n")?;.
0430: 09 09 6c 65 74 20 6d 75 74 20 72 65 63 69 70 69  ..let mut recipi
0440: 65 6e 74 73 20 3d 20 48 61 73 68 4d 61 70 3a 3a  ents = HashMap::
0450: 6e 65 77 28 29 3b 0a 09 09 66 6f 72 20 28 6e 61  new();...for (na
0460: 6d 65 2c 20 76 61 6c 75 65 29 20 69 6e 20 73 65  me, value) in se
0470: 74 74 69 6e 67 73 2e 67 65 74 5f 74 61 62 6c 65  ttings.get_table
0480: 28 22 72 65 63 69 70 69 65 6e 74 73 22 29 0a 09  ("recipients")..
0490: 09 09 2e 65 78 70 65 63 74 28 22 5b 73 6d 74 70  ...expect("[smtp
04a0: 32 74 67 2e 74 6f 6d 6c 5d 20 6d 69 73 73 69 6e  2tg.toml] missin
04b0: 67 20 74 61 62 6c 65 20 5c 22 72 65 63 69 70 69  g table \"recipi
04c0: 65 6e 74 73 5c 22 2e 5c 6e 22 29 0a 09 09 7b 0a  ents\".\n")...{.
04d0: 09 09 09 6c 65 74 20 76 61 6c 75 65 20 3d 20 76  ...let value = v
04e0: 61 6c 75 65 2e 69 6e 74 6f 5f 69 6e 74 28 29 0a  alue.into_int().
04f0: 09 09 09 09 2e 63 6f 6e 74 65 78 74 28 22 5b 73  .....context("[s
0500: 6d 74 70 32 74 67 2e 74 6f 6d 6c 5d 20 5c 22 72  mtp2tg.toml] \"r
0510: 65 63 69 70 69 65 6e 74 5c 22 20 74 61 62 6c 65  ecipient\" table
0520: 20 76 61 6c 75 65 73 20 73 68 6f 75 6c 64 20 62   values should b
0530: 65 20 69 6e 74 65 67 65 72 73 2e 5c 6e 22 29 3f  e integers.\n")?
0540: 3b 0a 09 09 09 72 65 63 69 70 69 65 6e 74 73 2e  ;....recipients.
0550: 69 6e 73 65 72 74 28 6e 61 6d 65 2c 20 76 61 6c  insert(name, val
0560: 75 65 29 3b 0a 09 09 7d 0a 0a 09 09 6c 65 74 20  ue);...}....let 
0570: 74 67 20 3d 20 41 72 63 3a 3a 6e 65 77 28 54 65  tg = Arc::new(Te
0580: 6c 65 67 72 61 6d 54 72 61 6e 73 70 6f 72 74 3a  legramTransport:
0590: 3a 6e 65 77 28 61 70 69 5f 6b 65 79 2c 20 72 65  :new(api_key, re
05a0: 63 69 70 69 65 6e 74 73 2c 20 26 73 65 74 74 69  cipients, &setti
05b0: 6e 67 73 29 3f 29 3b 0a 09 09 6c 65 74 20 66 69  ngs)?);...let fi
05c0: 65 6c 64 73 20 3d 20 48 61 73 68 53 65 74 3a 3a  elds = HashSet::
05d0: 3c 53 74 72 69 6e 67 3e 3a 3a 66 72 6f 6d 5f 69  <String>::from_i
05e0: 74 65 72 28 73 65 74 74 69 6e 67 73 2e 67 65 74  ter(settings.get
05f0: 5f 61 72 72 61 79 28 22 66 69 65 6c 64 73 22 29  _array("fields")
0600: 0a 09 09 09 2e 65 78 70 65 63 74 28 22 5b 73 6d  .....expect("[sm
0610: 74 70 32 74 67 2e 74 6f 6d 6c 5d 20 5c 22 66 69  tp2tg.toml] \"fi
0620: 65 6c 64 73 5c 22 20 73 68 6f 75 6c 64 20 62 65  elds\" should be
0630: 20 61 6e 20 61 72 72 61 79 22 29 0a 09 09 09 2e   an array").....
0640: 69 74 65 72 28 29 2e 6d 61 70 28 7c 78 7c 20 78  iter().map(|x| x
0650: 2e 63 6c 6f 6e 65 28 29 2e 69 6e 74 6f 5f 73 74  .clone().into_st
0660: 72 69 6e 67 28 29 2e 65 78 70 65 63 74 28 22 73  ring().expect("s
0670: 68 6f 75 6c 64 20 62 65 20 73 74 72 69 6e 67 73  hould be strings
0680: 22 29 29 29 3b 0a 09 09 6c 65 74 20 6d 75 74 20  ")));...let mut 
0690: 64 6f 6d 61 69 6e 73 3a 20 48 61 73 68 53 65 74  domains: HashSet
06a0: 3c 53 74 72 69 6e 67 3e 20 3d 20 48 61 73 68 53  <String> = HashS
06b0: 65 74 3a 3a 6e 65 77 28 29 3b 0a 09 09 6c 65 74  et::new();...let
06c0: 20 65 78 74 72 61 5f 64 6f 6d 61 69 6e 73 20 3d   extra_domains =
06d0: 20 73 65 74 74 69 6e 67 73 2e 67 65 74 5f 61 72   settings.get_ar
06e0: 72 61 79 28 22 64 6f 6d 61 69 6e 73 22 29 2e 73  ray("domains").s
06f0: 74 61 63 6b 28 29 3f 3b 0a 09 09 66 6f 72 20 64  tack()?;...for d
0700: 6f 6d 61 69 6e 20 69 6e 20 65 78 74 72 61 5f 64  omain in extra_d
0710: 6f 6d 61 69 6e 73 20 7b 0a 09 09 09 6c 65 74 20  omains {....let 
0720: 64 6f 6d 61 69 6e 20 3d 20 64 6f 6d 61 69 6e 2e  domain = domain.
0730: 74 6f 5f 73 74 72 69 6e 67 28 29 2e 74 6f 5f 6c  to_string().to_l
0740: 6f 77 65 72 63 61 73 65 28 29 3b 0a 09 09 09 69  owercase();....i
0750: 66 20 52 45 5f 44 4f 4d 41 49 4e 2e 69 73 5f 6d  f RE_DOMAIN.is_m
0760: 61 74 63 68 28 26 64 6f 6d 61 69 6e 29 20 7b 0a  atch(&domain) {.
0770: 09 09 09 09 64 6f 6d 61 69 6e 73 2e 69 6e 73 65  ....domains.inse
0780: 72 74 28 64 6f 6d 61 69 6e 29 3b 0a 09 09 09 7d  rt(domain);....}
0790: 20 65 6c 73 65 20 7b 0a 09 09 09 09 70 61 6e 69   else {.....pani
07a0: 63 21 28 22 5b 73 6d 74 70 32 74 67 2e 74 6f 6d  c!("[smtp2tg.tom
07b0: 6c 5d 20 63 61 6e 27 74 20 63 68 65 63 6b 20 6f  l] can't check o
07c0: 66 20 64 6f 6d 61 69 6e 73 20 69 6e 20 5c 22 64  f domains in \"d
07d0: 6f 6d 61 69 6e 73 5c 22 3a 20 7b 64 6f 6d 61 69  omains\": {domai
07e0: 6e 7d 22 29 3b 0a 09 09 09 7d 0a 09 09 7d 0a 09  n}");....}...}..
07f0: 09 6c 65 74 20 64 6f 6d 61 69 6e 73 20 3d 20 64  .let domains = d
0800: 6f 6d 61 69 6e 73 2e 69 6e 74 6f 5f 69 74 65 72  omains.into_iter
0810: 28 29 2e 6d 61 70 28 7c 73 7c 20 65 73 63 61 70  ().map(|s| escap
0820: 65 28 26 73 29 29 0a 09 09 09 2e 63 6f 6c 6c 65  e(&s)).....colle
0830: 63 74 3a 3a 3c 56 65 63 3c 53 74 72 69 6e 67 3e  ct::<Vec<String>
0840: 3e 28 29 2e 6a 6f 69 6e 28 22 7c 22 29 3b 0a 09  >().join("|");..
0850: 09 6c 65 74 20 61 64 64 72 65 73 73 20 3d 20 52  .let address = R
0860: 65 67 65 78 3a 3a 6e 65 77 28 26 66 6f 72 6d 61  egex::new(&forma
0870: 74 21 28 22 5e 28 3f 50 3c 75 73 65 72 3e 5b 61  t!("^(?P<user>[a
0880: 2d 7a 30 2d 39 5d 5b 2d 61 2d 7a 30 2d 39 5d 29  -z0-9][-a-z0-9])
0890: 28 40 28 7b 64 6f 6d 61 69 6e 73 7d 29 29 24 22  (@({domains}))$"
08a0: 29 29 2e 73 74 61 63 6b 28 29 3f 3b 0a 09 09 6c  )).stack()?;...l
08b0: 65 74 20 72 65 6c 61 79 20 3d 20 6d 61 74 63 68  et relay = match
08c0: 20 73 65 74 74 69 6e 67 73 2e 67 65 74 5f 73 74   settings.get_st
08d0: 72 69 6e 67 28 22 75 6e 6b 6e 6f 77 6e 22 29 0a  ring("unknown").
08e0: 09 09 09 2e 63 6f 6e 74 65 78 74 28 22 5b 73 6d  ....context("[sm
08f0: 74 70 32 74 67 2e 74 6f 6d 6c 5d 20 63 61 6e 27  tp2tg.toml] can'
0900: 74 20 67 65 74 20 5c 22 75 6e 6b 6e 6f 77 6e 5c  t get \"unknown\
0910: 22 20 70 6f 6c 69 63 79 2e 5c 6e 22 29 3f 2e 61  " policy.\n")?.a
0920: 73 5f 73 74 72 28 29 0a 09 09 7b 0a 09 09 09 22  s_str()...{...."
0930: 72 65 6c 61 79 22 20 3d 3e 20 74 72 75 65 2c 0a  relay" => true,.
0940: 09 09 09 22 64 65 6e 79 22 20 3d 3e 20 66 61 6c  ..."deny" => fal
0950: 73 65 2c 0a 09 09 09 5f 20 3d 3e 20 7b 0a 09 09  se,...._ => {...
0960: 09 09 62 61 69 6c 21 28 22 5b 73 6d 74 70 32 74  ..bail!("[smtp2t
0970: 67 2e 74 6f 6d 6c 5d 20 5c 22 75 6e 6b 6e 6f 77  g.toml] \"unknow
0980: 6e 5c 22 20 73 68 6f 75 6c 64 20 62 65 20 65 69  n\" should be ei
0990: 74 68 65 72 20 5c 22 72 65 6c 61 79 5c 22 20 6f  ther \"relay\" o
09a0: 72 20 5c 22 64 65 6e 79 5c 22 2e 5c 6e 22 29 3b  r \"deny\".\n");
09b0: 0a 09 09 09 7d 2c 0a 09 09 7d 3b 0a 0a 09 09 4f  ....},...};....O
09c0: 6b 28 4d 61 69 6c 53 65 72 76 65 72 20 7b 0a 09  k(MailServer {..
09d0: 09 09 64 61 74 61 3a 20 76 65 63 21 28 29 2c 0a  ..data: vec!(),.
09e0: 09 09 09 68 65 61 64 65 72 73 3a 20 4e 6f 6e 65  ...headers: None
09f0: 2c 0a 09 09 09 72 65 6c 61 79 2c 0a 09 09 09 74  ,....relay,....t
0a00: 67 2c 0a 09 09 09 66 69 65 6c 64 73 2c 0a 09 09  g,....fields,...
0a10: 09 61 64 64 72 65 73 73 2c 0a 09 09 7d 29 0a 09  .address,...})..
0a20: 7d 0a 0a 09 2f 2f 2f 20 52 65 74 75 72 6e 73 20  }.../// Returns 
0a30: 69 64 20 66 6f 72 20 70 72 6f 76 69 64 65 64 20  id for provided 
0a40: 65 6d 61 69 6c 20 61 64 64 72 65 73 73 0a 09 66  email address..f
0a50: 6e 20 67 65 74 5f 69 64 20 28 26 73 65 6c 66 2c  n get_id (&self,
0a60: 20 6e 61 6d 65 3a 20 26 73 74 72 29 20 2d 3e 20   name: &str) -> 
0a70: 52 65 73 75 6c 74 3c 26 43 68 61 74 50 65 65 72  Result<&ChatPeer
0a80: 49 64 3e 20 7b 0a 09 09 2f 2f 20 68 65 72 65 20  Id> {...// here 
0a90: 77 65 20 6e 65 65 64 20 74 6f 20 73 74 6f 72 65  we need to store
0aa0: 20 53 74 72 69 6e 67 20 6c 6f 63 61 6c 6c 79 20   String locally 
0ab0: 74 6f 20 62 6f 72 72 6f 77 20 69 74 20 61 66 74  to borrow it aft
0ac0: 65 72 0a 09 09 6c 65 74 20 6d 75 74 20 6c 69 6e  er...let mut lin
0ad0: 6b 20 3d 20 6e 61 6d 65 3b 0a 09 09 6c 65 74 20  k = name;...let 
0ae0: 6e 61 6d 65 3a 20 53 74 72 69 6e 67 3b 0a 09 09  name: String;...
0af0: 69 66 20 6c 65 74 20 53 6f 6d 65 28 63 61 70 73  if let Some(caps
0b00: 29 20 3d 20 73 65 6c 66 2e 61 64 64 72 65 73 73  ) = self.address
0b10: 2e 63 61 70 74 75 72 65 73 28 6c 69 6e 6b 29 20  .captures(link) 
0b20: 7b 0a 09 09 09 6e 61 6d 65 20 3d 20 63 61 70 73  {....name = caps
0b30: 5b 22 6e 61 6d 65 22 5d 2e 74 6f 5f 73 74 72 69  ["name"].to_stri
0b40: 6e 67 28 29 3b 0a 09 09 09 6c 69 6e 6b 20 3d 20  ng();....link = 
0b50: 26 6e 61 6d 65 3b 0a 09 09 7d 0a 09 09 6d 61 74  &name;...}...mat
0b60: 63 68 20 73 65 6c 66 2e 74 67 2e 67 65 74 28 6c  ch self.tg.get(l
0b70: 69 6e 6b 29 20 7b 0a 09 09 09 4f 6b 28 61 64 64  ink) {....Ok(add
0b80: 72 29 20 3d 3e 20 4f 6b 28 61 64 64 72 29 2c 0a  r) => Ok(addr),.
0b90: 09 09 09 45 72 72 28 5f 29 20 3d 3e 20 4f 6b 28  ...Err(_) => Ok(
0ba0: 26 73 65 6c 66 2e 74 67 2e 64 65 66 61 75 6c 74  &self.tg.default
0bb0: 29 2c 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2f 2f 20  ),...}..}.../// 
0bc0: 41 74 74 65 6d 70 74 20 74 6f 20 64 65 6c 69 76  Attempt to deliv
0bd0: 65 72 20 6f 6e 65 20 6d 65 73 73 61 67 65 0a 09  er one message..
0be0: 61 73 79 6e 63 20 66 6e 20 72 65 6c 61 79 5f 6d  async fn relay_m
0bf0: 61 69 6c 20 28 26 73 65 6c 66 29 20 2d 3e 20 52  ail (&self) -> R
0c00: 65 73 75 6c 74 3c 28 29 3e 20 7b 0a 09 09 69 66  esult<()> {...if
0c10: 20 6c 65 74 20 53 6f 6d 65 28 68 65 61 64 65 72   let Some(header
0c20: 73 29 20 3d 20 26 73 65 6c 66 2e 68 65 61 64 65  s) = &self.heade
0c30: 72 73 20 7b 0a 09 09 09 6c 65 74 20 6d 61 69 6c  rs {....let mail
0c40: 20 3d 20 6d 61 69 6c 5f 70 61 72 73 65 72 3a 3a   = mail_parser::
0c50: 4d 65 73 73 61 67 65 50 61 72 73 65 72 3a 3a 6e  MessageParser::n
0c60: 65 77 28 29 2e 70 61 72 73 65 28 26 73 65 6c 66  ew().parse(&self
0c70: 2e 64 61 74 61 29 0a 09 09 09 09 2e 63 6f 6e 74  .data)......cont
0c80: 65 78 74 28 22 46 61 69 6c 65 64 20 74 6f 20 70  ext("Failed to p
0c90: 61 72 73 65 20 6d 61 69 6c 2e 22 29 3f 3b 0a 0a  arse mail.")?;..
0ca0: 09 09 09 2f 2f 20 41 64 64 69 6e 67 20 61 6c 6c  ...// Adding all
0cb0: 20 6b 6e 6f 77 6e 20 61 64 64 72 65 73 73 65 73   known addresses
0cc0: 20 74 6f 20 72 65 63 69 70 69 65 6e 74 20 6c 69   to recipient li
0cd0: 73 74 2c 20 66 6f 72 20 61 6e 79 6f 6e 65 20 65  st, for anyone e
0ce0: 6c 73 65 20 61 64 64 69 6e 67 20 64 65 66 61 75  lse adding defau
0cf0: 6c 74 0a 09 09 09 2f 2f 20 41 6c 73 6f 20 69 66  lt....// Also if
0d00: 20 6c 69 73 74 20 69 73 20 65 6d 70 74 79 20 61   list is empty a
0d10: 6c 73 6f 20 61 64 64 69 6e 67 20 64 65 66 61 75  lso adding defau
0d20: 6c 74 0a 09 09 09 6c 65 74 20 6d 75 74 20 72 63  lt....let mut rc
0d30: 70 74 3a 20 48 61 73 68 53 65 74 3c 26 43 68 61  pt: HashSet<&Cha
0d40: 74 50 65 65 72 49 64 3e 20 3d 20 48 61 73 68 53  tPeerId> = HashS
0d50: 65 74 3a 3a 6e 65 77 28 29 3b 0a 09 09 09 69 66  et::new();....if
0d60: 20 68 65 61 64 65 72 73 2e 74 6f 2e 69 73 5f 65   headers.to.is_e
0d70: 6d 70 74 79 28 29 20 26 26 20 21 73 65 6c 66 2e  mpty() && !self.
0d80: 72 65 6c 61 79 20 7b 0a 09 09 09 09 62 61 69 6c  relay {.....bail
0d90: 21 28 22 52 65 6c 61 79 69 6e 67 20 69 73 20 64  !("Relaying is d
0da0: 69 73 61 62 6c 65 64 2c 20 61 6e 64 20 74 68 65  isabled, and the
0db0: 72 65 27 73 20 6e 6f 20 64 65 73 74 69 6e 61 74  re's no destinat
0dc0: 69 6f 6e 20 61 64 64 72 65 73 73 22 29 3b 0a 09  ion address");..
0dd0: 09 09 7d 0a 09 09 09 66 6f 72 20 69 74 65 6d 20  ..}....for item 
0de0: 69 6e 20 26 68 65 61 64 65 72 73 2e 74 6f 20 7b  in &headers.to {
0df0: 0a 09 09 09 09 72 63 70 74 2e 69 6e 73 65 72 74  .....rcpt.insert
0e00: 28 73 65 6c 66 2e 67 65 74 5f 69 64 28 69 74 65  (self.get_id(ite
0e10: 6d 29 3f 29 3b 0a 09 09 09 7d 3b 0a 09 09 09 69  m)?);....};....i
0e20: 66 20 72 63 70 74 2e 69 73 5f 65 6d 70 74 79 28  f rcpt.is_empty(
0e30: 29 20 7b 0a 09 09 09 09 73 65 6c 66 2e 74 67 2e  ) {.....self.tg.
0e40: 64 65 62 75 67 28 22 4e 6f 20 72 65 63 69 70 69  debug("No recipi
0e50: 65 6e 74 20 6f 72 20 65 6e 76 65 6c 6f 70 65 20  ent or envelope 
0e60: 61 64 64 72 65 73 73 2e 22 29 2e 61 77 61 69 74  address.").await
0e70: 3f 3b 0a 09 09 09 09 72 63 70 74 2e 69 6e 73 65  ?;.....rcpt.inse
0e80: 72 74 28 26 73 65 6c 66 2e 74 67 2e 64 65 66 61  rt(&self.tg.defa
0e90: 75 6c 74 29 3b 0a 09 09 09 7d 3b 0a 0a 09 09 09  ult);....};.....
0ea0: 2f 2f 20 70 72 65 70 61 72 69 6e 67 20 6d 65 73  // preparing mes
0eb0: 73 61 67 65 20 68 65 61 64 65 72 0a 09 09 09 6c  sage header....l
0ec0: 65 74 20 6d 75 74 20 72 65 70 6c 79 3a 20 56 65  et mut reply: Ve
0ed0: 63 3c 53 74 72 69 6e 67 3e 20 3d 20 76 65 63 21  c<String> = vec!
0ee0: 5b 22 3c 62 6c 6f 63 6b 71 75 6f 74 65 20 65 78  ["<blockquote ex
0ef0: 70 61 6e 64 61 62 6c 65 3e 22 2e 69 6e 74 6f 28  pandable>".into(
0f00: 29 5d 3b 0a 09 09 09 69 66 20 73 65 6c 66 2e 66  )];....if self.f
0f10: 69 65 6c 64 73 2e 63 6f 6e 74 61 69 6e 73 28 22  ields.contains("
0f20: 73 75 62 6a 65 63 74 22 29 20 7b 0a 09 09 09 09  subject") {.....
0f30: 69 66 20 6c 65 74 20 53 6f 6d 65 28 73 75 62 6a  if let Some(subj
0f40: 65 63 74 29 20 3d 20 6d 61 69 6c 2e 73 75 62 6a  ect) = mail.subj
0f50: 65 63 74 28 29 20 7b 0a 09 09 09 09 09 72 65 70  ect() {......rep
0f60: 6c 79 2e 70 75 73 68 28 66 6f 72 6d 61 74 21 28  ly.push(format!(
0f70: 22 3c 75 3e 3c 69 3e 53 75 62 6a 65 63 74 3a 3c  "<u><i>Subject:<
0f80: 2f 69 3e 3c 2f 75 3e e2 80 87 3c 63 6f 64 65 3e  /i></u> <code>
0f90: 7b 7d 3c 2f 63 6f 64 65 3e 22 2c 20 76 61 6c 69  {}</code>", vali
0fa0: 64 61 74 65 28 73 75 62 6a 65 63 74 29 2e 73 74  date(subject).st
0fb0: 61 63 6b 28 29 3f 29 29 3b 0a 09 09 09 09 7d 20  ack()?));.....} 
0fc0: 65 6c 73 65 20 69 66 20 6c 65 74 20 53 6f 6d 65  else if let Some
0fd0: 28 74 68 72 65 61 64 29 20 3d 20 6d 61 69 6c 2e  (thread) = mail.
0fe0: 74 68 72 65 61 64 5f 6e 61 6d 65 28 29 20 7b 0a  thread_name() {.
0ff0: 09 09 09 09 09 72 65 70 6c 79 2e 70 75 73 68 28  .....reply.push(
1000: 66 6f 72 6d 61 74 21 28 22 3c 75 3e 3c 69 3e 54  format!("<u><i>T
1010: 68 72 65 61 64 3a 3c 2f 69 3e 3c 2f 75 3e e2 80  hread:</i></u>â€
1020: 87 3c 63 6f 64 65 3e 7b 7d 3c 2f 63 6f 64 65 3e  ‡<code>{}</code>
1030: 22 2c 20 76 61 6c 69 64 61 74 65 28 74 68 72 65  ", validate(thre
1040: 61 64 29 2e 73 74 61 63 6b 28 29 3f 29 29 3b 0a  ad).stack()?));.
1050: 09 09 09 09 7d 0a 09 09 09 7d 0a 09 09 09 2f 2f  ....}....}....//
1060: 20 64 6f 20 77 65 20 6e 65 65 64 20 74 6f 20 72   do we need to r
1070: 65 70 6c 61 63 65 20 73 70 61 63 65 73 20 68 65  eplace spaces he
1080: 72 65 3f 0a 09 09 09 69 66 20 73 65 6c 66 2e 66  re?....if self.f
1090: 69 65 6c 64 73 2e 63 6f 6e 74 61 69 6e 73 28 22  ields.contains("
10a0: 66 72 6f 6d 22 29 20 7b 0a 09 09 09 09 72 65 70  from") {.....rep
10b0: 6c 79 2e 70 75 73 68 28 66 6f 72 6d 61 74 21 28  ly.push(format!(
10c0: 22 3c 75 3e 3c 69 3e 46 72 6f 6d 3a 3c 2f 69 3e  "<u><i>From:</i>
10d0: 3c 2f 75 3e e2 80 87 3c 63 6f 64 65 3e 7b 7d 3c  </u> <code>{}<
10e0: 2f 63 6f 64 65 3e 22 2c 20 76 61 6c 69 64 61 74  /code>", validat
10f0: 65 28 26 68 65 61 64 65 72 73 2e 66 72 6f 6d 29  e(&headers.from)
1100: 2e 73 74 61 63 6b 28 29 3f 29 29 3b 0a 09 09 09  .stack()?));....
1110: 7d 0a 09 09 09 69 66 20 73 65 6c 66 2e 66 69 65  }....if self.fie
1120: 6c 64 73 2e 63 6f 6e 74 61 69 6e 73 28 22 64 61  lds.contains("da
1130: 74 65 22 29 0a 09 09 09 09 26 26 20 6c 65 74 20  te").....&& let 
1140: 53 6f 6d 65 28 64 61 74 65 29 20 3d 20 6d 61 69  Some(date) = mai
1150: 6c 2e 64 61 74 65 28 29 0a 09 09 09 7b 0a 09 09  l.date()....{...
1160: 09 09 72 65 70 6c 79 2e 70 75 73 68 28 66 6f 72  ..reply.push(for
1170: 6d 61 74 21 28 22 3c 75 3e 3c 69 3e 44 61 74 65  mat!("<u><i>Date
1180: 3a 3c 2f 69 3e 3c 2f 75 3e e2 80 87 3c 63 6f 64  :</i></u> <cod
1190: 65 3e 7b 64 61 74 65 7d 3c 2f 63 6f 64 65 3e 22  e>{date}</code>"
11a0: 29 29 3b 0a 09 09 09 7d 0a 09 09 09 72 65 70 6c  ));....}....repl
11b0: 79 2e 70 75 73 68 28 22 3c 2f 62 6c 6f 63 6b 71  y.push("</blockq
11c0: 75 6f 74 65 3e 3c 70 72 65 3e 22 2e 69 6e 74 6f  uote><pre>".into
11d0: 28 29 29 3b 0a 09 09 09 6c 65 74 20 72 65 70 6c  ());....let repl
11e0: 79 20 3d 20 72 65 70 6c 79 2e 6a 6f 69 6e 28 22  y = reply.join("
11f0: 5c 6e 22 29 3b 0a 0a 09 09 09 6c 65 74 20 68 74  \n");.....let ht
1200: 6d 6c 5f 70 61 72 74 73 20 3d 20 6d 61 69 6c 2e  ml_parts = mail.
1210: 68 74 6d 6c 5f 62 6f 64 79 5f 63 6f 75 6e 74 28  html_body_count(
1220: 29 3b 0a 09 09 09 6c 65 74 20 74 65 78 74 5f 70  );....let text_p
1230: 61 72 74 73 20 3d 20 6d 61 69 6c 2e 74 65 78 74  arts = mail.text
1240: 5f 62 6f 64 79 5f 63 6f 75 6e 74 28 29 3b 0a 09  _body_count();..
1250: 09 09 6c 65 74 20 61 74 74 61 63 68 6d 65 6e 74  ..let attachment
1260: 73 20 3d 20 6d 61 69 6c 2e 61 74 74 61 63 68 6d  s = mail.attachm
1270: 65 6e 74 5f 63 6f 75 6e 74 28 29 3b 0a 09 09 09  ent_count();....
1280: 69 66 20 68 74 6d 6c 5f 70 61 72 74 73 20 21 3d  if html_parts !=
1290: 20 74 65 78 74 5f 70 61 72 74 73 20 7b 0a 09 09   text_parts {...
12a0: 09 09 73 65 6c 66 2e 74 67 2e 64 65 62 75 67 28  ..self.tg.debug(
12b0: 26 66 6f 72 6d 61 74 21 28 22 48 6d 2c 20 77 65  &format!("Hm, we
12c0: 20 68 61 76 65 20 7b 68 74 6d 6c 5f 70 61 72 74   have {html_part
12d0: 73 7d 20 48 54 4d 4c 20 70 61 72 74 73 20 61 6e  s} HTML parts an
12e0: 64 20 7b 74 65 78 74 5f 70 61 72 74 73 7d 20 74  d {text_parts} t
12f0: 65 78 74 20 70 61 72 74 73 2e 22 29 29 2e 61 77  ext parts.")).aw
1300: 61 69 74 3f 3b 0a 09 09 09 7d 0a 09 09 09 2f 2f  ait?;....}....//
1310: 6c 65 74 20 6d 75 74 20 68 74 6d 6c 5f 6e 75 6d  let mut html_num
1320: 20 3d 20 30 3b 0a 09 09 09 6c 65 74 20 6d 75 74   = 0;....let mut
1330: 20 74 65 78 74 5f 6e 75 6d 20 3d 20 30 3b 0a 09   text_num = 0;..
1340: 09 09 6c 65 74 20 6d 75 74 20 66 69 6c 65 5f 6e  ..let mut file_n
1350: 75 6d 20 3d 20 30 3b 0a 09 09 09 2f 2f 20 6c 65  um = 0;....// le
1360: 74 27 73 20 64 69 73 70 6c 61 79 20 66 69 72 73  t's display firs
1370: 74 20 68 74 6d 6c 20 6f 72 20 74 65 78 74 20 70  t html or text p
1380: 61 72 74 20 61 73 20 62 6f 64 79 0a 09 09 09 6c  art as body....l
1390: 65 74 20 6d 75 74 20 62 6f 64 79 3a 20 53 74 72  et mut body: Str
13a0: 69 6e 67 20 3d 20 22 22 2e 69 6e 74 6f 28 29 3b  ing = "".into();
13b0: 0a 09 09 09 2f 2a 0a 09 09 09 20 2a 20 61 63 74  ..../*.... * act
13c0: 75 61 6c 6c 79 20 49 20 64 6f 6e 27 74 20 77 61  ually I don't wa
13d0: 6e 6e 61 20 70 61 72 73 65 20 74 68 61 74 20 68  nna parse that h
13e0: 74 6d 6c 20 73 74 75 66 66 0a 09 09 09 69 66 20  tml stuff....if 
13f0: 68 74 6d 6c 5f 70 61 72 74 73 20 3e 20 30 20 7b  html_parts > 0 {
1400: 0a 09 09 09 09 6c 65 74 20 74 65 78 74 20 3d 20  .....let text = 
1410: 6d 61 69 6c 2e 62 6f 64 79 5f 68 74 6d 6c 28 30  mail.body_html(0
1420: 29 2e 73 74 61 63 6b 28 29 3f 3b 0a 09 09 09 09  ).stack()?;.....
1430: 69 66 20 74 65 78 74 2e 6c 65 6e 28 29 20 3c 20  if text.len() < 
1440: 34 30 39 36 20 2d 20 68 65 61 64 65 72 5f 73 69  4096 - header_si
1450: 7a 65 20 7b 0a 09 09 09 09 09 62 6f 64 79 20 3d  ze {......body =
1460: 20 74 65 78 74 3b 0a 09 09 09 09 09 68 74 6d 6c   text;......html
1470: 5f 6e 75 6d 20 3d 20 31 3b 0a 09 09 09 09 7d 0a  _num = 1;.....}.
1480: 09 09 09 7d 3b 0a 09 09 09 2a 2f 0a 09 09 09 69  ...};....*/....i
1490: 66 20 62 6f 64 79 2e 69 73 5f 65 6d 70 74 79 28  f body.is_empty(
14a0: 29 20 26 26 20 74 65 78 74 5f 70 61 72 74 73 20  ) && text_parts 
14b0: 3e 20 30 20 7b 0a 09 09 09 09 6c 65 74 20 74 65  > 0 {.....let te
14c0: 78 74 20 3d 20 6d 61 69 6c 2e 62 6f 64 79 5f 74  xt = mail.body_t
14d0: 65 78 74 28 30 29 0a 09 09 09 09 09 2e 63 6f 6e  ext(0).......con
14e0: 74 65 78 74 28 22 46 61 69 6c 65 64 20 74 6f 20  text("Failed to 
14f0: 65 78 74 72 61 63 74 20 74 65 78 74 20 66 72 6f  extract text fro
1500: 6d 20 6d 65 73 73 61 67 65 22 29 3f 0a 09 09 09  m message")?....
1510: 09 09 2e 72 65 70 6c 61 63 65 28 22 5c 72 5c 6e  ...replace("\r\n
1520: 22 2c 20 22 5c 6e 22 29 3b 0a 09 09 09 09 2f 2f  ", "\n");.....//
1530: 20 36 3a 0a 09 09 09 09 2f 2f 20 2d 20 28 68 65   6:.....// - (he
1540: 61 64 65 72 73 29 0a 09 09 09 09 2f 2f 20 2d 20  aders).....// - 
1550: 28 6d 61 69 6c 20 74 65 78 74 29 0a 09 09 09 09  (mail text).....
1560: 2f 2f 20 2d 20 36 3a 20 3c 2f 70 72 65 3e 0a 09  // - 6: </pre>..
1570: 09 09 09 69 66 20 74 65 78 74 2e 6c 65 6e 28 29  ...if text.len()
1580: 20 3c 20 34 30 39 36 20 2d 20 28 20 72 65 70 6c   < 4096 - ( repl
1590: 79 2e 6c 65 6e 28 29 20 2b 20 36 20 29 20 7b 0a  y.len() + 6 ) {.
15a0: 09 09 09 09 09 62 6f 64 79 20 3d 20 74 65 78 74  .....body = text
15b0: 3b 0a 09 09 09 09 09 74 65 78 74 5f 6e 75 6d 20  ;......text_num 
15c0: 3d 20 31 3b 0a 09 09 09 09 7d 0a 09 09 09 7d 3b  = 1;.....}....};
15d0: 0a 09 09 09 6c 65 74 20 6d 73 67 20 3d 20 66 6f  ....let msg = fo
15e0: 72 6d 61 74 21 28 22 7b 7d 7b 7d 3c 2f 70 72 65  rmat!("{}{}</pre
15f0: 3e 22 2c 20 72 65 70 6c 79 2c 20 76 61 6c 69 64  >", reply, valid
1600: 61 74 65 28 26 62 6f 64 79 29 2e 73 74 61 63 6b  ate(&body).stack
1610: 28 29 3f 29 3b 0a 0a 09 09 09 2f 2f 20 61 6e 64  ()?);.....// and
1620: 20 6c 65 74 27 73 20 63 6f 6c 6c 65 63 74 20 61   let's collect a
1630: 6c 6c 20 6f 74 68 65 72 20 61 74 74 61 63 68 6d  ll other attachm
1640: 65 6e 74 20 70 61 72 74 73 0a 09 09 09 6c 65 74  ent parts....let
1650: 20 6d 75 74 20 66 69 6c 65 73 5f 74 6f 5f 73 65   mut files_to_se
1660: 6e 64 20 3d 20 76 65 63 21 5b 5d 3b 0a 09 09 09  nd = vec![];....
1670: 2f 2a 0a 09 09 09 20 2a 20 6c 65 74 27 73 20 6a  /*.... * let's j
1680: 75 73 74 20 73 6b 69 70 20 68 74 6d 6c 20 70 61  ust skip html pa
1690: 72 74 73 20 66 6f 72 20 6e 6f 77 2c 20 74 68 65  rts for now, the
16a0: 79 20 6a 75 73 74 20 64 75 70 6c 69 63 61 74 65  y just duplicate
16b0: 20 74 65 78 74 3f 0a 09 09 09 77 68 69 6c 65 20   text?....while 
16c0: 68 74 6d 6c 5f 6e 75 6d 20 3c 20 68 74 6d 6c 5f  html_num < html_
16d0: 70 61 72 74 73 20 7b 0a 09 09 09 09 66 69 6c 65  parts {.....file
16e0: 73 5f 74 6f 5f 73 65 6e 64 2e 70 75 73 68 28 6d  s_to_send.push(m
16f0: 61 69 6c 2e 68 74 6d 6c 5f 70 61 72 74 28 68 74  ail.html_part(ht
1700: 6d 6c 5f 6e 75 6d 29 2e 73 74 61 63 6b 28 29 3f  ml_num).stack()?
1710: 29 3b 0a 09 09 09 09 68 74 6d 6c 5f 6e 75 6d 20  );.....html_num 
1720: 2b 3d 20 31 3b 0a 09 09 09 7d 0a 09 09 09 2a 2f  += 1;....}....*/
1730: 0a 09 09 09 77 68 69 6c 65 20 74 65 78 74 5f 6e  ....while text_n
1740: 75 6d 20 3c 20 74 65 78 74 5f 70 61 72 74 73 20  um < text_parts 
1750: 7b 0a 09 09 09 09 66 69 6c 65 73 5f 74 6f 5f 73  {.....files_to_s
1760: 65 6e 64 2e 70 75 73 68 28 6d 61 69 6c 2e 74 65  end.push(mail.te
1770: 78 74 5f 70 61 72 74 28 74 65 78 74 5f 6e 75 6d  xt_part(text_num
1780: 2e 74 72 79 5f 69 6e 74 6f 28 29 2e 73 74 61 63  .try_into().stac
1790: 6b 28 29 3f 29 0a 09 09 09 09 09 2e 63 6f 6e 74  k()?).......cont
17a0: 65 78 74 28 22 46 61 69 6c 65 64 20 74 6f 20 67  ext("Failed to g
17b0: 65 74 20 74 65 78 74 20 70 61 72 74 20 66 72 6f  et text part fro
17c0: 6d 20 6d 65 73 73 61 67 65 2e 22 29 3f 29 3b 0a  m message.")?);.
17d0: 09 09 09 09 74 65 78 74 5f 6e 75 6d 20 2b 3d 20  ....text_num += 
17e0: 31 3b 0a 09 09 09 7d 0a 09 09 09 77 68 69 6c 65  1;....}....while
17f0: 20 66 69 6c 65 5f 6e 75 6d 20 3c 20 61 74 74 61   file_num < atta
1800: 63 68 6d 65 6e 74 73 20 7b 0a 09 09 09 09 66 69  chments {.....fi
1810: 6c 65 73 5f 74 6f 5f 73 65 6e 64 2e 70 75 73 68  les_to_send.push
1820: 28 6d 61 69 6c 2e 61 74 74 61 63 68 6d 65 6e 74  (mail.attachment
1830: 28 66 69 6c 65 5f 6e 75 6d 2e 74 72 79 5f 69 6e  (file_num.try_in
1840: 74 6f 28 29 2e 73 74 61 63 6b 28 29 3f 29 0a 09  to().stack()?)..
1850: 09 09 09 09 2e 63 6f 6e 74 65 78 74 28 22 46 61  .....context("Fa
1860: 69 6c 65 64 20 74 6f 20 67 65 74 20 66 69 6c 65  iled to get file
1870: 20 70 61 72 74 20 66 72 6f 6d 20 6d 65 73 73 61   part from messa
1880: 67 65 2e 22 29 3f 29 3b 0a 09 09 09 09 66 69 6c  ge.")?);.....fil
1890: 65 5f 6e 75 6d 20 2b 3d 20 31 3b 0a 09 09 09 7d  e_num += 1;....}
18a0: 0a 0a 09 09 09 66 6f 72 20 63 68 61 74 20 69 6e  .....for chat in
18b0: 20 72 63 70 74 20 7b 0a 09 09 09 09 69 66 20 21   rcpt {.....if !
18c0: 66 69 6c 65 73 5f 74 6f 5f 73 65 6e 64 2e 69 73  files_to_send.is
18d0: 5f 65 6d 70 74 79 28 29 20 7b 0a 09 09 09 09 09  _empty() {......
18e0: 6c 65 74 20 6d 75 74 20 66 69 6c 65 73 20 3d 20  let mut files = 
18f0: 76 65 63 21 5b 5d 3b 0a 09 09 09 09 09 2f 2f 20  vec![];......// 
1900: 6c 65 74 20 6d 75 74 20 66 69 72 73 74 5f 6f 6e  let mut first_on
1910: 65 20 3d 20 74 72 75 65 3b 0a 09 09 09 09 09 66  e = true;......f
1920: 6f 72 20 63 68 75 6e 6b 20 69 6e 20 26 66 69 6c  or chunk in &fil
1930: 65 73 5f 74 6f 5f 73 65 6e 64 20 7b 0a 09 09 09  es_to_send {....
1940: 09 09 09 6c 65 74 20 64 61 74 61 3a 20 56 65 63  ...let data: Vec
1950: 3c 75 38 3e 20 3d 20 63 68 75 6e 6b 2e 63 6f 6e  <u8> = chunk.con
1960: 74 65 6e 74 73 28 29 2e 74 6f 5f 76 65 63 28 29  tents().to_vec()
1970: 3b 0a 09 09 09 09 09 09 6c 65 74 20 6d 75 74 20  ;.......let mut 
1980: 66 69 6c 65 6e 61 6d 65 3a 20 4f 70 74 69 6f 6e  filename: Option
1990: 3c 53 74 72 69 6e 67 3e 20 3d 20 4e 6f 6e 65 3b  <String> = None;
19a0: 0a 09 09 09 09 09 09 66 6f 72 20 68 65 61 64 65  .......for heade
19b0: 72 20 69 6e 20 63 68 75 6e 6b 2e 68 65 61 64 65  r in chunk.heade
19c0: 72 73 28 29 20 7b 0a 09 09 09 09 09 09 09 69 66  rs() {........if
19d0: 20 68 65 61 64 65 72 2e 6e 61 6d 65 28 29 20 3d   header.name() =
19e0: 3d 20 22 43 6f 6e 74 65 6e 74 2d 54 79 70 65 22  = "Content-Type"
19f0: 20 7b 0a 09 09 09 09 09 09 09 09 6d 61 74 63 68   {.........match
1a00: 20 68 65 61 64 65 72 2e 76 61 6c 75 65 28 29 20   header.value() 
1a10: 7b 0a 09 09 09 09 09 09 09 09 09 6d 61 69 6c 5f  {..........mail_
1a20: 70 61 72 73 65 72 3a 3a 48 65 61 64 65 72 56 61  parser::HeaderVa
1a30: 6c 75 65 3a 3a 43 6f 6e 74 65 6e 74 54 79 70 65  lue::ContentType
1a40: 28 63 6f 6e 74 65 6e 74 74 79 70 65 29 20 3d 3e  (contenttype) =>
1a50: 20 7b 0a 09 09 09 09 09 09 09 09 09 09 69 66 20   {...........if 
1a60: 6c 65 74 20 53 6f 6d 65 28 66 6e 61 6d 65 29 20  let Some(fname) 
1a70: 3d 20 63 6f 6e 74 65 6e 74 74 79 70 65 2e 61 74  = contenttype.at
1a80: 74 72 69 62 75 74 65 28 22 6e 61 6d 65 22 29 20  tribute("name") 
1a90: 7b 0a 09 09 09 09 09 09 09 09 09 09 09 66 69 6c  {............fil
1aa0: 65 6e 61 6d 65 20 3d 20 53 6f 6d 65 28 66 6e 61  ename = Some(fna
1ab0: 6d 65 2e 74 6f 5f 6f 77 6e 65 64 28 29 29 3b 0a  me.to_owned());.
1ac0: 09 09 09 09 09 09 09 09 09 09 7d 0a 09 09 09 09  ..........}.....
1ad0: 09 09 09 09 09 7d 2c 0a 09 09 09 09 09 09 09 09  .....},.........
1ae0: 09 5f 20 3d 3e 20 7b 0a 09 09 09 09 09 09 09 09  ._ => {.........
1af0: 09 09 73 65 6c 66 2e 74 67 2e 64 65 62 75 67 28  ..self.tg.debug(
1b00: 22 41 74 74 61 63 68 6d 65 6e 74 20 68 61 73 20  "Attachment has 
1b10: 62 61 64 20 43 6f 6e 74 65 6e 74 54 79 70 65 20  bad ContentType 
1b20: 68 65 61 64 65 72 2e 22 29 2e 61 77 61 69 74 3f  header.").await?
1b30: 3b 0a 09 09 09 09 09 09 09 09 09 7d 2c 0a 09 09  ;..........},...
1b40: 09 09 09 09 09 09 7d 3b 0a 09 09 09 09 09 09 09  ......};........
1b50: 7d 3b 0a 09 09 09 09 09 09 7d 3b 0a 09 09 09 09  };.......};.....
1b60: 09 09 6c 65 74 20 66 69 6c 65 6e 61 6d 65 20 3d  ..let filename =
1b70: 20 69 66 20 6c 65 74 20 53 6f 6d 65 28 66 6e 61   if let Some(fna
1b80: 6d 65 29 20 3d 20 66 69 6c 65 6e 61 6d 65 20 7b  me) = filename {
1b90: 0a 09 09 09 09 09 09 09 66 6e 61 6d 65 0a 09 09  ........fname...
1ba0: 09 09 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09  ....} else {....
1bb0: 09 09 09 09 22 41 74 74 61 63 68 6d 65 6e 74 2e  ...."Attachment.
1bc0: 74 78 74 22 2e 69 6e 74 6f 28 29 0a 09 09 09 09  txt".into().....
1bd0: 09 09 7d 3b 0a 09 09 09 09 09 09 66 69 6c 65 73  ..};.......files
1be0: 2e 70 75 73 68 28 41 74 74 61 63 68 6d 65 6e 74  .push(Attachment
1bf0: 20 7b 0a 09 09 09 09 09 09 09 64 61 74 61 3a 20   {........data: 
1c00: 43 75 72 73 6f 72 3a 3a 6e 65 77 28 64 61 74 61  Cursor::new(data
1c10: 29 2c 0a 09 09 09 09 09 09 09 6e 61 6d 65 3a 20  ),........name: 
1c20: 66 69 6c 65 6e 61 6d 65 2c 0a 09 09 09 09 09 09  filename,.......
1c30: 7d 29 3b 0a 09 09 09 09 09 7d 0a 09 09 09 09 09  });......}......
1c40: 73 65 6c 66 2e 74 67 2e 73 65 6e 64 67 72 6f 75  self.tg.sendgrou
1c50: 70 28 63 68 61 74 2c 20 66 69 6c 65 73 2c 20 26  p(chat, files, &
1c60: 6d 73 67 29 2e 61 77 61 69 74 3f 3b 0a 09 09 09  msg).await?;....
1c70: 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09 09 09 73  .} else {......s
1c80: 65 6c 66 2e 74 67 2e 73 65 6e 64 28 63 68 61 74  elf.tg.send(chat
1c90: 2c 20 26 6d 73 67 29 2e 61 77 61 69 74 3f 3b 0a  , &msg).await?;.
1ca0: 09 09 09 09 7d 0a 09 09 09 7d 0a 09 09 7d 20 65  ....}....}...} e
1cb0: 6c 73 65 20 7b 0a 09 09 09 62 61 69 6c 21 28 22  lse {....bail!("
1cc0: 52 65 71 75 69 72 65 64 20 68 65 61 64 65 72 73  Required headers
1cd0: 20 77 65 72 65 20 6e 6f 74 20 66 6f 75 6e 64 2e   were not found.
1ce0: 22 29 3b 0a 09 09 7d 0a 09 09 4f 6b 28 28 29 29  ");...}...Ok(())
1cf0: 0a 09 7d 0a 7d 0a 0a 69 6d 70 6c 20 6d 61 69 6c  ..}.}..impl mail
1d00: 69 6e 5f 65 6d 62 65 64 64 65 64 3a 3a 48 61 6e  in_embedded::Han
1d10: 64 6c 65 72 20 66 6f 72 20 4d 61 69 6c 53 65 72  dler for MailSer
1d20: 76 65 72 20 7b 0a 09 2f 2f 2f 20 4a 75 73 74 20  ver {../// Just 
1d30: 64 65 6e 79 20 6c 6f 67 69 6e 20 61 75 74 68 0a  deny login auth.
1d40: 09 66 6e 20 61 75 74 68 5f 6c 6f 67 69 6e 20 28  .fn auth_login (
1d50: 26 6d 75 74 20 73 65 6c 66 2c 20 5f 75 73 65 72  &mut self, _user
1d60: 6e 61 6d 65 3a 20 26 73 74 72 2c 20 5f 70 61 73  name: &str, _pas
1d70: 73 77 6f 72 64 3a 20 26 73 74 72 29 20 2d 3e 20  sword: &str) -> 
1d80: 52 65 73 70 6f 6e 73 65 20 7b 0a 09 09 49 4e 56  Response {...INV
1d90: 41 4c 49 44 5f 43 52 45 44 45 4e 54 49 41 4c 53  ALID_CREDENTIALS
1da0: 0a 09 7d 0a 0a 09 2f 2f 2f 20 4a 75 73 74 20 64  ..}.../// Just d
1db0: 65 6e 79 20 70 6c 61 69 6e 20 61 75 74 68 0a 09  eny plain auth..
1dc0: 66 6e 20 61 75 74 68 5f 70 6c 61 69 6e 20 28 26  fn auth_plain (&
1dd0: 6d 75 74 20 73 65 6c 66 2c 20 5f 61 75 74 68 6f  mut self, _autho
1de0: 72 69 7a 61 74 69 6f 6e 5f 69 64 3a 20 26 73 74  rization_id: &st
1df0: 72 2c 20 5f 61 75 74 68 65 6e 74 69 63 61 74 69  r, _authenticati
1e00: 6f 6e 5f 69 64 3a 20 26 73 74 72 2c 20 5f 70 61  on_id: &str, _pa
1e10: 73 73 77 6f 72 64 3a 20 26 73 74 72 29 20 2d 3e  ssword: &str) ->
1e20: 20 52 65 73 70 6f 6e 73 65 20 7b 0a 09 09 49 4e   Response {...IN
1e30: 56 41 4c 49 44 5f 43 52 45 44 45 4e 54 49 41 4c  VALID_CREDENTIAL
1e40: 53 0a 09 7d 0a 0a 09 2f 2f 2f 20 56 65 72 69 66  S..}.../// Verif
1e50: 79 20 77 68 65 74 68 65 72 20 61 64 64 72 65 73  y whether addres
1e60: 73 20 69 73 20 64 65 6c 69 76 65 72 61 62 6c 65  s is deliverable
1e70: 0a 09 66 6e 20 72 63 70 74 20 28 26 6d 75 74 20  ..fn rcpt (&mut 
1e80: 73 65 6c 66 2c 20 74 6f 3a 20 26 73 74 72 29 20  self, to: &str) 
1e90: 2d 3e 20 52 65 73 70 6f 6e 73 65 20 7b 0a 09 09  -> Response {...
1ea0: 69 66 20 73 65 6c 66 2e 72 65 6c 61 79 20 7b 0a  if self.relay {.
1eb0: 09 09 09 4f 4b 0a 09 09 7d 20 65 6c 73 65 20 7b  ...OK...} else {
1ec0: 0a 09 09 09 6d 61 74 63 68 20 73 65 6c 66 2e 67  ....match self.g
1ed0: 65 74 5f 69 64 28 74 6f 29 20 7b 0a 09 09 09 09  et_id(to) {.....
1ee0: 4f 6b 28 5f 29 20 3d 3e 20 4f 4b 2c 0a 09 09 09  Ok(_) => OK,....
1ef0: 09 45 72 72 28 5f 29 20 3d 3e 20 7b 0a 09 09 09  .Err(_) => {....
1f00: 09 09 69 66 20 73 65 6c 66 2e 72 65 6c 61 79 20  ..if self.relay 
1f10: 7b 0a 09 09 09 09 09 09 4f 4b 0a 09 09 09 09 09  {.......OK......
1f20: 7d 20 65 6c 73 65 20 7b 0a 09 09 09 09 09 09 4e  } else {.......N
1f30: 4f 5f 4d 41 49 4c 42 4f 58 0a 09 09 09 09 09 7d  O_MAILBOX......}
1f40: 0a 09 09 09 09 7d 0a 09 09 09 7d 0a 09 09 7d 0a  .....}....}...}.
1f50: 09 7d 0a 0a 09 2f 2f 2f 20 53 61 76 65 20 68 65  .}.../// Save he
1f60: 61 64 65 72 73 20 77 65 20 6e 65 65 64 0a 09 66  aders we need..f
1f70: 6e 20 64 61 74 61 5f 73 74 61 72 74 20 28 26 6d  n data_start (&m
1f80: 75 74 20 73 65 6c 66 2c 20 5f 64 6f 6d 61 69 6e  ut self, _domain
1f90: 3a 20 26 73 74 72 2c 20 66 72 6f 6d 3a 20 26 73  : &str, from: &s
1fa0: 74 72 2c 20 5f 69 73 38 62 69 74 3a 20 62 6f 6f  tr, _is8bit: boo
1fb0: 6c 2c 20 74 6f 3a 20 26 5b 53 74 72 69 6e 67 5d  l, to: &[String]
1fc0: 29 20 2d 3e 20 52 65 73 70 6f 6e 73 65 20 7b 0a  ) -> Response {.
1fd0: 09 09 73 65 6c 66 2e 68 65 61 64 65 72 73 20 3d  ..self.headers =
1fe0: 20 53 6f 6d 65 28 53 6f 6d 65 48 65 61 64 65 72   Some(SomeHeader
1ff0: 73 7b 0a 09 09 09 66 72 6f 6d 3a 20 66 72 6f 6d  s{....from: from
2000: 2e 74 6f 5f 73 74 72 69 6e 67 28 29 2c 0a 09 09  .to_string(),...
2010: 09 74 6f 3a 20 74 6f 2e 74 6f 5f 76 65 63 28 29  .to: to.to_vec()
2020: 2c 0a 09 09 7d 29 3b 0a 09 09 4f 4b 0a 09 7d 0a  ,...});...OK..}.
2030: 0a 09 2f 2f 2f 20 53 61 76 65 20 63 68 75 6e 6b  ../// Save chunk
2040: 28 3f 29 20 6f 66 20 64 61 74 61 0a 09 66 6e 20  (?) of data..fn 
2050: 64 61 74 61 20 28 26 6d 75 74 20 73 65 6c 66 2c  data (&mut self,
2060: 20 62 75 66 3a 20 26 5b 75 38 5d 29 20 2d 3e 20   buf: &[u8]) -> 
2070: 73 74 64 3a 3a 72 65 73 75 6c 74 3a 3a 52 65 73  std::result::Res
2080: 75 6c 74 3c 28 29 2c 20 45 72 72 6f 72 3e 20 7b  ult<(), Error> {
2090: 0a 09 09 73 65 6c 66 2e 64 61 74 61 2e 61 70 70  ...self.data.app
20a0: 65 6e 64 28 62 75 66 2e 74 6f 5f 76 65 63 28 29  end(buf.to_vec()
20b0: 2e 61 73 5f 6d 75 74 28 29 29 3b 0a 09 09 4f 6b  .as_mut());...Ok
20c0: 28 28 29 29 0a 09 7d 0a 0a 09 2f 2f 2f 20 41 74  (())..}.../// At
20d0: 74 65 6d 70 74 20 74 6f 20 73 65 6e 64 20 65 6d  tempt to send em
20e0: 61 69 6c 2c 20 72 65 74 75 72 6e 20 74 65 6d 70  ail, return temp
20f0: 6f 72 61 72 79 20 65 72 72 6f 72 20 69 66 20 74  orary error if t
2100: 68 61 74 20 66 61 69 6c 73 0a 09 66 6e 20 64 61  hat fails..fn da
2110: 74 61 5f 65 6e 64 20 28 26 6d 75 74 20 73 65 6c  ta_end (&mut sel
2120: 66 29 20 2d 3e 20 52 65 73 70 6f 6e 73 65 20 7b  f) -> Response {
2130: 0a 09 09 6c 65 74 20 6d 75 74 20 72 65 73 75 6c  ...let mut resul
2140: 74 20 3d 20 4f 4b 3b 0a 09 09 73 6d 6f 6c 3a 3a  t = OK;...smol::
2150: 62 6c 6f 63 6b 5f 6f 6e 28 43 6f 6d 70 61 74 3a  block_on(Compat:
2160: 3a 6e 65 77 28 61 73 79 6e 63 20 7b 0a 09 09 09  :new(async {....
2170: 2f 2f 20 72 65 6c 61 79 20 6d 61 69 6c 0a 09 09  // relay mail...
2180: 09 69 66 20 6c 65 74 20 45 72 72 28 65 72 72 29  .if let Err(err)
2190: 20 3d 20 73 65 6c 66 2e 72 65 6c 61 79 5f 6d 61   = self.relay_ma
21a0: 69 6c 28 29 2e 61 77 61 69 74 20 7b 0a 09 09 09  il().await {....
21b0: 09 72 65 73 75 6c 74 20 3d 20 49 4e 54 45 52 4e  .result = INTERN
21c0: 41 4c 5f 45 52 52 4f 52 3b 0a 09 09 09 09 2f 2f  AL_ERROR;.....//
21d0: 20 69 6e 20 63 61 73 65 20 74 68 61 74 20 66 61   in case that fa
21e0: 69 6c 73 20 2d 20 69 6e 66 6f 72 6d 20 64 65 66  ils - inform def
21f0: 61 75 6c 74 20 72 65 63 69 70 69 65 6e 74 0a 09  ault recipient..
2200: 09 09 09 69 66 20 6c 65 74 20 45 72 72 28 65 72  ...if let Err(er
2210: 72 29 20 3d 20 73 65 6c 66 2e 74 67 2e 64 65 62  r) = self.tg.deb
2220: 75 67 28 26 66 6f 72 6d 61 74 21 28 22 53 65 6e  ug(&format!("Sen
2230: 64 69 6e 67 20 65 6d 61 69 6c 73 20 66 61 69 6c  ding emails fail
2240: 65 64 3a 5c 6e 7b 65 72 72 3a 3f 7d 22 29 29 2e  ed:\n{err:?}")).
2250: 61 77 61 69 74 20 7b 0a 09 09 09 09 09 2f 2f 20  await {......// 
2260: 69 6e 20 63 61 73 65 20 74 68 61 74 20 61 6c 73  in case that als
2270: 6f 20 66 61 69 6c 73 20 2d 20 77 72 69 74 65 20  o fails - write 
2280: 73 6f 6d 65 20 6c 6f 67 73 20 61 6e 64 20 62 61  some logs and ba
2290: 69 6c 0a 09 09 09 09 09 65 70 72 69 6e 74 6c 6e  il......eprintln
22a0: 21 28 22 7b 65 72 72 3a 3f 7d 22 29 3b 0a 09 09  !("{err:?}");...
22b0: 09 09 7d 3b 0a 09 09 09 7d 3b 0a 09 09 7d 29 29  ..};....};...}))
22c0: 3b 0a 09 09 2f 2f 20 63 6c 65 61 72 20 2d 20 6a  ;...// clear - j
22d0: 75 73 74 20 69 6e 20 63 61 73 65 0a 09 09 73 65  ust in case...se
22e0: 6c 66 2e 64 61 74 61 20 3d 20 76 65 63 21 5b 5d  lf.data = vec![]
22f0: 3b 0a 09 09 73 65 6c 66 2e 68 65 61 64 65 72 73  ;...self.headers
2300: 20 3d 20 4e 6f 6e 65 3b 0a 09 09 72 65 73 75 6c   = None;...resul
2310: 74 0a 09 7d 0a 7d 0a                             t..}.}.