Hex Artifact Content
Logged in as anonymous

Artifact 420cd8d808b512d4c1bc437a6a8120913afae190aa63dd001e6cbcda2e54d405:


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 6c 65  ", "\n");.....le
1530: 74 20 74 65 78 74 20 3d 20 76 61 6c 69 64 61 74  t text = validat
1540: 65 28 26 74 65 78 74 29 2e 73 74 61 63 6b 28 29  e(&text).stack()
1550: 3f 3b 0a 09 09 09 09 2f 2f 20 36 3a 0a 09 09 09  ?;.....// 6:....
1560: 09 2f 2f 20 2d 20 28 68 65 61 64 65 72 73 29 0a  .// - (headers).
1570: 09 09 09 09 2f 2f 20 2d 20 28 6d 61 69 6c 20 74  ....// - (mail t
1580: 65 78 74 29 0a 09 09 09 09 2f 2f 20 2d 20 36 3a  ext).....// - 6:
1590: 20 3c 2f 70 72 65 3e 0a 09 09 09 09 69 66 20 74   </pre>.....if t
15a0: 65 78 74 2e 6c 65 6e 28 29 20 3c 20 34 30 39 36  ext.len() < 4096
15b0: 20 2d 20 28 20 72 65 70 6c 79 2e 6c 65 6e 28 29   - ( reply.len()
15c0: 20 2b 20 36 20 29 20 7b 0a 09 09 09 09 09 62 6f   + 6 ) {......bo
15d0: 64 79 20 3d 20 74 65 78 74 2e 74 6f 5f 73 74 72  dy = text.to_str
15e0: 69 6e 67 28 29 3b 0a 09 09 09 09 09 74 65 78 74  ing();......text
15f0: 5f 6e 75 6d 20 3d 20 31 3b 0a 09 09 09 09 7d 0a  _num = 1;.....}.
1600: 09 09 09 7d 3b 0a 09 09 09 6c 65 74 20 6d 73 67  ...};....let msg
1610: 20 3d 20 66 6f 72 6d 61 74 21 28 22 7b 7d 7b 7d   = format!("{}{}
1620: 3c 2f 70 72 65 3e 22 2c 20 72 65 70 6c 79 2c 20  </pre>", reply, 
1630: 62 6f 64 79 29 3b 0a 0a 09 09 09 2f 2f 20 61 6e  body);.....// an
1640: 64 20 6c 65 74 27 73 20 63 6f 6c 6c 65 63 74 20  d let's collect 
1650: 61 6c 6c 20 6f 74 68 65 72 20 61 74 74 61 63 68  all other attach
1660: 6d 65 6e 74 20 70 61 72 74 73 0a 09 09 09 6c 65  ment parts....le
1670: 74 20 6d 75 74 20 66 69 6c 65 73 5f 74 6f 5f 73  t mut files_to_s
1680: 65 6e 64 20 3d 20 76 65 63 21 5b 5d 3b 0a 09 09  end = vec![];...
1690: 09 2f 2a 0a 09 09 09 20 2a 20 6c 65 74 27 73 20  ./*.... * let's 
16a0: 6a 75 73 74 20 73 6b 69 70 20 68 74 6d 6c 20 70  just skip html p
16b0: 61 72 74 73 20 66 6f 72 20 6e 6f 77 2c 20 74 68  arts for now, th
16c0: 65 79 20 6a 75 73 74 20 64 75 70 6c 69 63 61 74  ey just duplicat
16d0: 65 20 74 65 78 74 3f 0a 09 09 09 77 68 69 6c 65  e text?....while
16e0: 20 68 74 6d 6c 5f 6e 75 6d 20 3c 20 68 74 6d 6c   html_num < html
16f0: 5f 70 61 72 74 73 20 7b 0a 09 09 09 09 66 69 6c  _parts {.....fil
1700: 65 73 5f 74 6f 5f 73 65 6e 64 2e 70 75 73 68 28  es_to_send.push(
1710: 6d 61 69 6c 2e 68 74 6d 6c 5f 70 61 72 74 28 68  mail.html_part(h
1720: 74 6d 6c 5f 6e 75 6d 29 2e 73 74 61 63 6b 28 29  tml_num).stack()
1730: 3f 29 3b 0a 09 09 09 09 68 74 6d 6c 5f 6e 75 6d  ?);.....html_num
1740: 20 2b 3d 20 31 3b 0a 09 09 09 7d 0a 09 09 09 2a   += 1;....}....*
1750: 2f 0a 09 09 09 77 68 69 6c 65 20 74 65 78 74 5f  /....while text_
1760: 6e 75 6d 20 3c 20 74 65 78 74 5f 70 61 72 74 73  num < text_parts
1770: 20 7b 0a 09 09 09 09 66 69 6c 65 73 5f 74 6f 5f   {.....files_to_
1780: 73 65 6e 64 2e 70 75 73 68 28 6d 61 69 6c 2e 74  send.push(mail.t
1790: 65 78 74 5f 70 61 72 74 28 74 65 78 74 5f 6e 75  ext_part(text_nu
17a0: 6d 2e 74 72 79 5f 69 6e 74 6f 28 29 2e 73 74 61  m.try_into().sta
17b0: 63 6b 28 29 3f 29 0a 09 09 09 09 09 2e 63 6f 6e  ck()?).......con
17c0: 74 65 78 74 28 22 46 61 69 6c 65 64 20 74 6f 20  text("Failed to 
17d0: 67 65 74 20 74 65 78 74 20 70 61 72 74 20 66 72  get text part fr
17e0: 6f 6d 20 6d 65 73 73 61 67 65 2e 22 29 3f 29 3b  om message.")?);
17f0: 0a 09 09 09 09 74 65 78 74 5f 6e 75 6d 20 2b 3d  .....text_num +=
1800: 20 31 3b 0a 09 09 09 7d 0a 09 09 09 77 68 69 6c   1;....}....whil
1810: 65 20 66 69 6c 65 5f 6e 75 6d 20 3c 20 61 74 74  e file_num < att
1820: 61 63 68 6d 65 6e 74 73 20 7b 0a 09 09 09 09 66  achments {.....f
1830: 69 6c 65 73 5f 74 6f 5f 73 65 6e 64 2e 70 75 73  iles_to_send.pus
1840: 68 28 6d 61 69 6c 2e 61 74 74 61 63 68 6d 65 6e  h(mail.attachmen
1850: 74 28 66 69 6c 65 5f 6e 75 6d 2e 74 72 79 5f 69  t(file_num.try_i
1860: 6e 74 6f 28 29 2e 73 74 61 63 6b 28 29 3f 29 0a  nto().stack()?).
1870: 09 09 09 09 09 2e 63 6f 6e 74 65 78 74 28 22 46  ......context("F
1880: 61 69 6c 65 64 20 74 6f 20 67 65 74 20 66 69 6c  ailed to get fil
1890: 65 20 70 61 72 74 20 66 72 6f 6d 20 6d 65 73 73  e part from mess
18a0: 61 67 65 2e 22 29 3f 29 3b 0a 09 09 09 09 66 69  age.")?);.....fi
18b0: 6c 65 5f 6e 75 6d 20 2b 3d 20 31 3b 0a 09 09 09  le_num += 1;....
18c0: 7d 0a 0a 09 09 09 66 6f 72 20 63 68 61 74 20 69  }.....for chat i
18d0: 6e 20 72 63 70 74 20 7b 0a 09 09 09 09 69 66 20  n rcpt {.....if 
18e0: 21 66 69 6c 65 73 5f 74 6f 5f 73 65 6e 64 2e 69  !files_to_send.i
18f0: 73 5f 65 6d 70 74 79 28 29 20 7b 0a 09 09 09 09  s_empty() {.....
1900: 09 6c 65 74 20 6d 75 74 20 66 69 6c 65 73 20 3d  .let mut files =
1910: 20 76 65 63 21 5b 5d 3b 0a 09 09 09 09 09 2f 2f   vec![];......//
1920: 20 6c 65 74 20 6d 75 74 20 66 69 72 73 74 5f 6f   let mut first_o
1930: 6e 65 20 3d 20 74 72 75 65 3b 0a 09 09 09 09 09  ne = true;......
1940: 66 6f 72 20 63 68 75 6e 6b 20 69 6e 20 26 66 69  for chunk in &fi
1950: 6c 65 73 5f 74 6f 5f 73 65 6e 64 20 7b 0a 09 09  les_to_send {...
1960: 09 09 09 09 6c 65 74 20 64 61 74 61 3a 20 56 65  ....let data: Ve
1970: 63 3c 75 38 3e 20 3d 20 63 68 75 6e 6b 2e 63 6f  c<u8> = chunk.co
1980: 6e 74 65 6e 74 73 28 29 2e 74 6f 5f 76 65 63 28  ntents().to_vec(
1990: 29 3b 0a 09 09 09 09 09 09 6c 65 74 20 6d 75 74  );.......let mut
19a0: 20 66 69 6c 65 6e 61 6d 65 3a 20 4f 70 74 69 6f   filename: Optio
19b0: 6e 3c 53 74 72 69 6e 67 3e 20 3d 20 4e 6f 6e 65  n<String> = None
19c0: 3b 0a 09 09 09 09 09 09 66 6f 72 20 68 65 61 64  ;.......for head
19d0: 65 72 20 69 6e 20 63 68 75 6e 6b 2e 68 65 61 64  er in chunk.head
19e0: 65 72 73 28 29 20 7b 0a 09 09 09 09 09 09 09 69  ers() {........i
19f0: 66 20 68 65 61 64 65 72 2e 6e 61 6d 65 28 29 20  f header.name() 
1a00: 3d 3d 20 22 43 6f 6e 74 65 6e 74 2d 54 79 70 65  == "Content-Type
1a10: 22 20 7b 0a 09 09 09 09 09 09 09 09 6d 61 74 63  " {.........matc
1a20: 68 20 68 65 61 64 65 72 2e 76 61 6c 75 65 28 29  h header.value()
1a30: 20 7b 0a 09 09 09 09 09 09 09 09 09 6d 61 69 6c   {..........mail
1a40: 5f 70 61 72 73 65 72 3a 3a 48 65 61 64 65 72 56  _parser::HeaderV
1a50: 61 6c 75 65 3a 3a 43 6f 6e 74 65 6e 74 54 79 70  alue::ContentTyp
1a60: 65 28 63 6f 6e 74 65 6e 74 74 79 70 65 29 20 3d  e(contenttype) =
1a70: 3e 20 7b 0a 09 09 09 09 09 09 09 09 09 09 69 66  > {...........if
1a80: 20 6c 65 74 20 53 6f 6d 65 28 66 6e 61 6d 65 29   let Some(fname)
1a90: 20 3d 20 63 6f 6e 74 65 6e 74 74 79 70 65 2e 61   = contenttype.a
1aa0: 74 74 72 69 62 75 74 65 28 22 6e 61 6d 65 22 29  ttribute("name")
1ab0: 20 7b 0a 09 09 09 09 09 09 09 09 09 09 09 66 69   {............fi
1ac0: 6c 65 6e 61 6d 65 20 3d 20 53 6f 6d 65 28 66 6e  lename = Some(fn
1ad0: 61 6d 65 2e 74 6f 5f 6f 77 6e 65 64 28 29 29 3b  ame.to_owned());
1ae0: 0a 09 09 09 09 09 09 09 09 09 09 7d 0a 09 09 09  ...........}....
1af0: 09 09 09 09 09 09 7d 2c 0a 09 09 09 09 09 09 09  ......},........
1b00: 09 09 5f 20 3d 3e 20 7b 0a 09 09 09 09 09 09 09  .._ => {........
1b10: 09 09 09 73 65 6c 66 2e 74 67 2e 64 65 62 75 67  ...self.tg.debug
1b20: 28 22 41 74 74 61 63 68 6d 65 6e 74 20 68 61 73  ("Attachment has
1b30: 20 62 61 64 20 43 6f 6e 74 65 6e 74 54 79 70 65   bad ContentType
1b40: 20 68 65 61 64 65 72 2e 22 29 2e 61 77 61 69 74   header.").await
1b50: 3f 3b 0a 09 09 09 09 09 09 09 09 09 7d 2c 0a 09  ?;..........},..
1b60: 09 09 09 09 09 09 09 7d 3b 0a 09 09 09 09 09 09  .......};.......
1b70: 09 7d 3b 0a 09 09 09 09 09 09 7d 3b 0a 09 09 09  .};.......};....
1b80: 09 09 09 6c 65 74 20 66 69 6c 65 6e 61 6d 65 20  ...let filename 
1b90: 3d 20 69 66 20 6c 65 74 20 53 6f 6d 65 28 66 6e  = if let Some(fn
1ba0: 61 6d 65 29 20 3d 20 66 69 6c 65 6e 61 6d 65 20  ame) = filename 
1bb0: 7b 0a 09 09 09 09 09 09 09 66 6e 61 6d 65 0a 09  {........fname..
1bc0: 09 09 09 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09  .....} else {...
1bd0: 09 09 09 09 09 22 41 74 74 61 63 68 6d 65 6e 74  ....."Attachment
1be0: 2e 74 78 74 22 2e 69 6e 74 6f 28 29 0a 09 09 09  .txt".into()....
1bf0: 09 09 09 7d 3b 0a 09 09 09 09 09 09 66 69 6c 65  ...};.......file
1c00: 73 2e 70 75 73 68 28 41 74 74 61 63 68 6d 65 6e  s.push(Attachmen
1c10: 74 20 7b 0a 09 09 09 09 09 09 09 64 61 74 61 3a  t {........data:
1c20: 20 43 75 72 73 6f 72 3a 3a 6e 65 77 28 64 61 74   Cursor::new(dat
1c30: 61 29 2c 0a 09 09 09 09 09 09 09 6e 61 6d 65 3a  a),........name:
1c40: 20 66 69 6c 65 6e 61 6d 65 2c 0a 09 09 09 09 09   filename,......
1c50: 09 7d 29 3b 0a 09 09 09 09 09 7d 0a 09 09 09 09  .});......}.....
1c60: 09 73 65 6c 66 2e 74 67 2e 73 65 6e 64 67 72 6f  .self.tg.sendgro
1c70: 75 70 28 63 68 61 74 2c 20 66 69 6c 65 73 2c 20  up(chat, files, 
1c80: 26 6d 73 67 29 2e 61 77 61 69 74 3f 3b 0a 09 09  &msg).await?;...
1c90: 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09 09 09  ..} else {......
1ca0: 73 65 6c 66 2e 74 67 2e 73 65 6e 64 28 63 68 61  self.tg.send(cha
1cb0: 74 2c 20 26 6d 73 67 29 2e 61 77 61 69 74 3f 3b  t, &msg).await?;
1cc0: 0a 09 09 09 09 7d 0a 09 09 09 7d 0a 09 09 7d 20  .....}....}...} 
1cd0: 65 6c 73 65 20 7b 0a 09 09 09 62 61 69 6c 21 28  else {....bail!(
1ce0: 22 52 65 71 75 69 72 65 64 20 68 65 61 64 65 72  "Required header
1cf0: 73 20 77 65 72 65 20 6e 6f 74 20 66 6f 75 6e 64  s were not found
1d00: 2e 22 29 3b 0a 09 09 7d 0a 09 09 4f 6b 28 28 29  .");...}...Ok(()
1d10: 29 0a 09 7d 0a 7d 0a 0a 69 6d 70 6c 20 6d 61 69  )..}.}..impl mai
1d20: 6c 69 6e 5f 65 6d 62 65 64 64 65 64 3a 3a 48 61  lin_embedded::Ha
1d30: 6e 64 6c 65 72 20 66 6f 72 20 4d 61 69 6c 53 65  ndler for MailSe
1d40: 72 76 65 72 20 7b 0a 09 2f 2f 2f 20 4a 75 73 74  rver {../// Just
1d50: 20 64 65 6e 79 20 6c 6f 67 69 6e 20 61 75 74 68   deny login auth
1d60: 0a 09 66 6e 20 61 75 74 68 5f 6c 6f 67 69 6e 20  ..fn auth_login 
1d70: 28 26 6d 75 74 20 73 65 6c 66 2c 20 5f 75 73 65  (&mut self, _use
1d80: 72 6e 61 6d 65 3a 20 26 73 74 72 2c 20 5f 70 61  rname: &str, _pa
1d90: 73 73 77 6f 72 64 3a 20 26 73 74 72 29 20 2d 3e  ssword: &str) ->
1da0: 20 52 65 73 70 6f 6e 73 65 20 7b 0a 09 09 49 4e   Response {...IN
1db0: 56 41 4c 49 44 5f 43 52 45 44 45 4e 54 49 41 4c  VALID_CREDENTIAL
1dc0: 53 0a 09 7d 0a 0a 09 2f 2f 2f 20 4a 75 73 74 20  S..}.../// Just 
1dd0: 64 65 6e 79 20 70 6c 61 69 6e 20 61 75 74 68 0a  deny plain auth.
1de0: 09 66 6e 20 61 75 74 68 5f 70 6c 61 69 6e 20 28  .fn auth_plain (
1df0: 26 6d 75 74 20 73 65 6c 66 2c 20 5f 61 75 74 68  &mut self, _auth
1e00: 6f 72 69 7a 61 74 69 6f 6e 5f 69 64 3a 20 26 73  orization_id: &s
1e10: 74 72 2c 20 5f 61 75 74 68 65 6e 74 69 63 61 74  tr, _authenticat
1e20: 69 6f 6e 5f 69 64 3a 20 26 73 74 72 2c 20 5f 70  ion_id: &str, _p
1e30: 61 73 73 77 6f 72 64 3a 20 26 73 74 72 29 20 2d  assword: &str) -
1e40: 3e 20 52 65 73 70 6f 6e 73 65 20 7b 0a 09 09 49  > Response {...I
1e50: 4e 56 41 4c 49 44 5f 43 52 45 44 45 4e 54 49 41  NVALID_CREDENTIA
1e60: 4c 53 0a 09 7d 0a 0a 09 2f 2f 2f 20 56 65 72 69  LS..}.../// Veri
1e70: 66 79 20 77 68 65 74 68 65 72 20 61 64 64 72 65  fy whether addre
1e80: 73 73 20 69 73 20 64 65 6c 69 76 65 72 61 62 6c  ss is deliverabl
1e90: 65 0a 09 66 6e 20 72 63 70 74 20 28 26 6d 75 74  e..fn rcpt (&mut
1ea0: 20 73 65 6c 66 2c 20 74 6f 3a 20 26 73 74 72 29   self, to: &str)
1eb0: 20 2d 3e 20 52 65 73 70 6f 6e 73 65 20 7b 0a 09   -> Response {..
1ec0: 09 69 66 20 73 65 6c 66 2e 72 65 6c 61 79 20 7b  .if self.relay {
1ed0: 0a 09 09 09 4f 4b 0a 09 09 7d 20 65 6c 73 65 20  ....OK...} else 
1ee0: 7b 0a 09 09 09 6d 61 74 63 68 20 73 65 6c 66 2e  {....match self.
1ef0: 67 65 74 5f 69 64 28 74 6f 29 20 7b 0a 09 09 09  get_id(to) {....
1f00: 09 4f 6b 28 5f 29 20 3d 3e 20 4f 4b 2c 0a 09 09  .Ok(_) => OK,...
1f10: 09 09 45 72 72 28 5f 29 20 3d 3e 20 7b 0a 09 09  ..Err(_) => {...
1f20: 09 09 09 69 66 20 73 65 6c 66 2e 72 65 6c 61 79  ...if self.relay
1f30: 20 7b 0a 09 09 09 09 09 09 4f 4b 0a 09 09 09 09   {.......OK.....
1f40: 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09 09 09 09  .} else {.......
1f50: 4e 4f 5f 4d 41 49 4c 42 4f 58 0a 09 09 09 09 09  NO_MAILBOX......
1f60: 7d 0a 09 09 09 09 7d 0a 09 09 09 7d 0a 09 09 7d  }.....}....}...}
1f70: 0a 09 7d 0a 0a 09 2f 2f 2f 20 53 61 76 65 20 68  ..}.../// Save h
1f80: 65 61 64 65 72 73 20 77 65 20 6e 65 65 64 0a 09  eaders we need..
1f90: 66 6e 20 64 61 74 61 5f 73 74 61 72 74 20 28 26  fn data_start (&
1fa0: 6d 75 74 20 73 65 6c 66 2c 20 5f 64 6f 6d 61 69  mut self, _domai
1fb0: 6e 3a 20 26 73 74 72 2c 20 66 72 6f 6d 3a 20 26  n: &str, from: &
1fc0: 73 74 72 2c 20 5f 69 73 38 62 69 74 3a 20 62 6f  str, _is8bit: bo
1fd0: 6f 6c 2c 20 74 6f 3a 20 26 5b 53 74 72 69 6e 67  ol, to: &[String
1fe0: 5d 29 20 2d 3e 20 52 65 73 70 6f 6e 73 65 20 7b  ]) -> Response {
1ff0: 0a 09 09 73 65 6c 66 2e 68 65 61 64 65 72 73 20  ...self.headers 
2000: 3d 20 53 6f 6d 65 28 53 6f 6d 65 48 65 61 64 65  = Some(SomeHeade
2010: 72 73 7b 0a 09 09 09 66 72 6f 6d 3a 20 66 72 6f  rs{....from: fro
2020: 6d 2e 74 6f 5f 73 74 72 69 6e 67 28 29 2c 0a 09  m.to_string(),..
2030: 09 09 74 6f 3a 20 74 6f 2e 74 6f 5f 76 65 63 28  ..to: to.to_vec(
2040: 29 2c 0a 09 09 7d 29 3b 0a 09 09 4f 4b 0a 09 7d  ),...});...OK..}
2050: 0a 0a 09 2f 2f 2f 20 53 61 76 65 20 63 68 75 6e  .../// Save chun
2060: 6b 28 3f 29 20 6f 66 20 64 61 74 61 0a 09 66 6e  k(?) of data..fn
2070: 20 64 61 74 61 20 28 26 6d 75 74 20 73 65 6c 66   data (&mut self
2080: 2c 20 62 75 66 3a 20 26 5b 75 38 5d 29 20 2d 3e  , buf: &[u8]) ->
2090: 20 73 74 64 3a 3a 72 65 73 75 6c 74 3a 3a 52 65   std::result::Re
20a0: 73 75 6c 74 3c 28 29 2c 20 45 72 72 6f 72 3e 20  sult<(), Error> 
20b0: 7b 0a 09 09 73 65 6c 66 2e 64 61 74 61 2e 61 70  {...self.data.ap
20c0: 70 65 6e 64 28 62 75 66 2e 74 6f 5f 76 65 63 28  pend(buf.to_vec(
20d0: 29 2e 61 73 5f 6d 75 74 28 29 29 3b 0a 09 09 4f  ).as_mut());...O
20e0: 6b 28 28 29 29 0a 09 7d 0a 0a 09 2f 2f 2f 20 41  k(())..}.../// A
20f0: 74 74 65 6d 70 74 20 74 6f 20 73 65 6e 64 20 65  ttempt to send e
2100: 6d 61 69 6c 2c 20 72 65 74 75 72 6e 20 74 65 6d  mail, return tem
2110: 70 6f 72 61 72 79 20 65 72 72 6f 72 20 69 66 20  porary error if 
2120: 74 68 61 74 20 66 61 69 6c 73 0a 09 66 6e 20 64  that fails..fn d
2130: 61 74 61 5f 65 6e 64 20 28 26 6d 75 74 20 73 65  ata_end (&mut se
2140: 6c 66 29 20 2d 3e 20 52 65 73 70 6f 6e 73 65 20  lf) -> Response 
2150: 7b 0a 09 09 6c 65 74 20 6d 75 74 20 72 65 73 75  {...let mut resu
2160: 6c 74 20 3d 20 4f 4b 3b 0a 09 09 73 6d 6f 6c 3a  lt = OK;...smol:
2170: 3a 62 6c 6f 63 6b 5f 6f 6e 28 43 6f 6d 70 61 74  :block_on(Compat
2180: 3a 3a 6e 65 77 28 61 73 79 6e 63 20 7b 0a 09 09  ::new(async {...
2190: 09 2f 2f 20 72 65 6c 61 79 20 6d 61 69 6c 0a 09  .// relay mail..
21a0: 09 09 69 66 20 6c 65 74 20 45 72 72 28 65 72 72  ..if let Err(err
21b0: 29 20 3d 20 73 65 6c 66 2e 72 65 6c 61 79 5f 6d  ) = self.relay_m
21c0: 61 69 6c 28 29 2e 61 77 61 69 74 20 7b 0a 09 09  ail().await {...
21d0: 09 09 72 65 73 75 6c 74 20 3d 20 49 4e 54 45 52  ..result = INTER
21e0: 4e 41 4c 5f 45 52 52 4f 52 3b 0a 09 09 09 09 2f  NAL_ERROR;...../
21f0: 2f 20 69 6e 20 63 61 73 65 20 74 68 61 74 20 66  / in case that f
2200: 61 69 6c 73 20 2d 20 69 6e 66 6f 72 6d 20 64 65  ails - inform de
2210: 66 61 75 6c 74 20 72 65 63 69 70 69 65 6e 74 0a  fault recipient.
2220: 09 09 09 09 69 66 20 6c 65 74 20 45 72 72 28 65  ....if let Err(e
2230: 72 72 29 20 3d 20 73 65 6c 66 2e 74 67 2e 64 65  rr) = self.tg.de
2240: 62 75 67 28 26 66 6f 72 6d 61 74 21 28 22 53 65  bug(&format!("Se
2250: 6e 64 69 6e 67 20 65 6d 61 69 6c 73 20 66 61 69  nding emails fai
2260: 6c 65 64 3a 5c 6e 7b 65 72 72 3a 7d 22 29 29 2e  led:\n{err:}")).
2270: 61 77 61 69 74 20 7b 0a 09 09 09 09 09 2f 2f 20  await {......// 
2280: 69 6e 20 63 61 73 65 20 74 68 61 74 20 61 6c 73  in case that als
2290: 6f 20 66 61 69 6c 73 20 2d 20 77 72 69 74 65 20  o fails - write 
22a0: 73 6f 6d 65 20 6c 6f 67 73 20 61 6e 64 20 62 61  some logs and ba
22b0: 69 6c 0a 09 09 09 09 09 65 70 72 69 6e 74 6c 6e  il......eprintln
22c0: 21 28 22 7b 65 72 72 3a 3f 7d 22 29 3b 0a 09 09  !("{err:?}");...
22d0: 09 09 7d 3b 0a 09 09 09 7d 3b 0a 09 09 7d 29 29  ..};....};...}))
22e0: 3b 0a 09 09 2f 2f 20 63 6c 65 61 72 20 2d 20 6a  ;...// clear - j
22f0: 75 73 74 20 69 6e 20 63 61 73 65 0a 09 09 73 65  ust in case...se
2300: 6c 66 2e 64 61 74 61 20 3d 20 76 65 63 21 5b 5d  lf.data = vec![]
2310: 3b 0a 09 09 73 65 6c 66 2e 68 65 61 64 65 72 73  ;...self.headers
2320: 20 3d 20 4e 6f 6e 65 3b 0a 09 09 72 65 73 75 6c   = None;...resul
2330: 74 0a 09 7d 0a 7d 0a                             t..}.}.