Artifact
8e25f3e7d7bc3b8306b5edc5a04285f1ad3c90d7632cbe0c32c573bb959a3d36:
0000: 75 73 65 20 63 72 61 74 65 3a 3a 43 75 72 73 6f use crate::Curso
0010: 72 3b 0a 0a 75 73 65 20 73 74 64 3a 3a 62 6f 72 r;..use std::bor
0020: 72 6f 77 3a 3a 43 6f 77 3b 0a 0a 75 73 65 20 68 row::Cow;..use h
0030: 74 6d 6c 5f 65 73 63 61 70 65 3a 3a 65 6e 63 6f tml_escape::enco
0040: 64 65 5f 74 65 78 74 3b 0a 75 73 65 20 6c 61 7a de_text;.use laz
0050: 79 5f 73 74 61 74 69 63 3a 3a 6c 61 7a 79 5f 73 y_static::lazy_s
0060: 74 61 74 69 63 3b 0a 75 73 65 20 72 65 67 65 78 tatic;.use regex
0070: 3a 3a 52 65 67 65 78 3b 0a 75 73 65 20 73 74 61 ::Regex;.use sta
0080: 63 6b 65 64 5f 65 72 72 6f 72 73 3a 3a 7b 0a 09 cked_errors::{..
0090: 62 61 69 6c 2c 0a 09 52 65 73 75 6c 74 2c 0a 7d bail,..Result,.}
00a0: 3b 0a 0a 6c 61 7a 79 5f 73 74 61 74 69 63 21 20 ;..lazy_static!
00b0: 7b 0a 09 70 75 62 20 73 74 61 74 69 63 20 72 65 {..pub static re
00c0: 66 20 52 45 5f 44 4f 4d 41 49 4e 3a 20 52 65 67 f RE_DOMAIN: Reg
00d0: 65 78 20 3d 20 52 65 67 65 78 3a 3a 6e 65 77 28 ex = Regex::new(
00e0: 72 22 5e 5b 61 2d 7a 30 2d 39 5d 28 5b 2d 61 2d r"^[a-z0-9]([-a-
00f0: 7a 30 2d 39 5d 2a 5b 61 2d 7a 30 2d 39 5d 29 3f z0-9]*[a-z0-9])?
0100: 28 5c 2e 5b 61 2d 7a 30 2d 39 5d 28 5b 2d 61 2d (\.[a-z0-9]([-a-
0110: 7a 30 2d 39 5d 2a 5b 61 2d 7a 30 2d 39 5d 29 3f z0-9]*[a-z0-9])?
0120: 29 2a 24 22 29 2e 65 78 70 65 63 74 28 22 49 6e )*$").expect("In
0130: 76 61 6c 69 64 20 64 6f 6d 61 69 6e 20 72 65 67 valid domain reg
0140: 65 78 22 29 3b 0a 09 70 75 62 20 73 74 61 74 69 ex");..pub stati
0150: 63 20 72 65 66 20 52 45 5f 43 4c 4f 53 49 4e 47 c ref RE_CLOSING
0160: 3a 20 52 65 67 65 78 20 3d 20 52 65 67 65 78 3a : Regex = Regex:
0170: 3a 6e 65 77 28 72 22 3c 2f 5b 20 5c 74 5d 2a 28 :new(r"</[ \t]*(
0180: 70 72 65 7c 63 6f 64 65 29 5b 20 5c 74 5d 2a 3e pre|code)[ \t]*>
0190: 22 29 2e 65 78 70 65 63 74 28 22 49 6e 76 61 6c ").expect("Inval
01a0: 69 64 20 63 6c 6f 64 69 6e 67 20 74 61 67 20 72 id cloding tag r
01b0: 65 67 65 78 22 29 3b 0a 7d 0a 0a 2f 2f 2f 20 53 egex");.}../// S
01c0: 74 6f 72 65 73 20 62 69 6e 61 72 79 20 61 74 74 tores binary att
01d0: 61 63 68 6d 65 6e 74 20 64 61 74 61 20 61 6e 64 achment data and
01e0: 20 6d 65 74 61 64 61 74 61 20 66 6f 72 20 54 65 metadata for Te
01f0: 6c 65 67 72 61 6d 20 6d 65 73 73 61 67 65 73 2e legram messages.
0200: 0a 2f 2f 2f 20 54 68 65 20 64 61 74 61 20 69 73 ./// The data is
0210: 20 77 72 61 70 70 65 64 20 69 6e 20 61 20 60 43 wrapped in a `C
0220: 75 72 73 6f 72 3c 56 65 63 3c 75 38 3e 3e 60 20 ursor<Vec<u8>>`
0230: 66 6f 72 20 65 66 66 69 63 69 65 6e 74 20 73 74 for efficient st
0240: 72 65 61 6d 69 6e 67 2c 0a 2f 2f 2f 20 77 68 69 reaming,./// whi
0250: 6c 65 20 60 6e 61 6d 65 60 20 68 6f 6c 64 73 20 le `name` holds
0260: 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 6f 72 20 the filename or
0270: 64 69 73 70 6c 61 79 20 6e 61 6d 65 20 6f 66 20 display name of
0280: 74 68 65 20 61 74 74 61 63 68 6d 65 6e 74 2e 0a the attachment..
0290: 23 5b 64 65 72 69 76 65 28 44 65 62 75 67 29 5d #[derive(Debug)]
02a0: 0a 70 75 62 20 73 74 72 75 63 74 20 41 74 74 61 .pub struct Atta
02b0: 63 68 6d 65 6e 74 20 7b 0a 09 70 75 62 20 64 61 chment {..pub da
02c0: 74 61 3a 20 43 75 72 73 6f 72 3c 56 65 63 3c 75 ta: Cursor<Vec<u
02d0: 38 3e 3e 2c 0a 09 70 75 62 20 6e 61 6d 65 3a 20 8>>,..pub name:
02e0: 53 74 72 69 6e 67 2c 0a 7d 0a 0a 2f 2f 2f 20 50 String,.}../// P
02f0: 61 73 73 20 61 6e 79 20 74 65 78 74 20 68 65 72 ass any text her
0300: 65 20 74 6f 20 62 65 20 76 61 6c 69 64 61 74 65 e to be validate
0310: 64 20 61 73 20 6e 6f 74 20 62 72 65 61 6b 69 6e d as not breakin
0320: 67 20 66 72 6f 6d 20 54 65 6c 65 67 72 61 6d 20 g from Telegram
0330: 70 72 65 66 6f 72 6d 61 74 74 65 64 20 62 6c 6f preformatted blo
0340: 63 6b 73 0a 2f 2f 2f 20 65 73 63 61 70 65 20 61 cks./// escape a
0350: 6c 6c 20 48 54 4d 4c 20 63 68 61 72 73 20 61 66 ll HTML chars af
0360: 74 65 72 77 61 72 64 73 0a 70 75 62 20 66 6e 20 terwards.pub fn
0370: 76 61 6c 69 64 61 74 65 20 3c 27 61 3e 28 74 65 validate <'a>(te
0380: 78 74 3a 20 26 27 61 20 73 74 72 29 20 2d 3e 20 xt: &'a str) ->
0390: 52 65 73 75 6c 74 3c 43 6f 77 3c 27 61 2c 20 73 Result<Cow<'a, s
03a0: 74 72 3e 3e 20 7b 0a 09 69 66 20 52 45 5f 43 4c tr>> {..if RE_CL
03b0: 4f 53 49 4e 47 2e 69 73 5f 6d 61 74 63 68 28 74 OSING.is_match(t
03c0: 65 78 74 29 20 7b 0a 09 09 62 61 69 6c 21 28 22 ext) {...bail!("
03d0: 54 65 6c 65 67 72 61 6d 20 63 6c 6f 73 69 6e 67 Telegram closing
03e0: 20 74 61 67 20 66 6f 75 6e 64 2e 22 29 3b 0a 09 tag found.");..
03f0: 7d 20 65 6c 73 65 20 7b 0a 09 09 4f 6b 28 65 6e } else {...Ok(en
0400: 63 6f 64 65 5f 74 65 78 74 28 74 65 78 74 29 29 code_text(text))
0410: 0a 09 7d 0a 7d 0a ..}.}.