|
| 1 | +use super::{ |
| 2 | + util::{parse_timezone_offset, split2_at_space}, |
| 3 | + Error, |
| 4 | +}; |
| 5 | +use crate::{parsed::Signature, Time}; |
| 6 | +use bstr::{BStr, ByteSlice}; |
| 7 | +use btoi::btoi; |
| 8 | +use hex::FromHex; |
| 9 | + |
| 10 | +#[derive(PartialEq, Eq, Debug, Hash)] |
| 11 | +pub struct Tag<'data> { |
| 12 | + pub target: &'data BStr, |
| 13 | + pub name: &'data BStr, |
| 14 | + pub target_kind: crate::Kind, |
| 15 | + pub message: Option<&'data BStr>, |
| 16 | + pub signature: Signature<'data>, |
| 17 | + pub pgp_signature: Option<&'data BStr>, |
| 18 | +} |
| 19 | + |
| 20 | +fn parse_signature(d: &[u8]) -> Result<Signature, Error> { |
| 21 | + const ONE_SPACE: usize = 1; |
| 22 | + let email_begin = d |
| 23 | + .iter() |
| 24 | + .position(|&b| b == b'<') |
| 25 | + .ok_or_else(|| { |
| 26 | + Error::ParseError( |
| 27 | + "Could not find beginning of email marked by '<'", |
| 28 | + d.to_owned(), |
| 29 | + ) |
| 30 | + }) |
| 31 | + .and_then(|pos| { |
| 32 | + if pos == 0 { |
| 33 | + Err(Error::ParseError( |
| 34 | + "Email found in place of author name", |
| 35 | + d.to_owned(), |
| 36 | + )) |
| 37 | + } else { |
| 38 | + Ok(pos) |
| 39 | + } |
| 40 | + })?; |
| 41 | + let email_end = email_begin |
| 42 | + + d.iter() |
| 43 | + .skip(email_begin) |
| 44 | + .position(|&b| b == b'>') |
| 45 | + .ok_or_else(|| { |
| 46 | + Error::ParseError("Could not find end of email marked by '>'", d.to_owned()) |
| 47 | + }) |
| 48 | + .and_then(|pos| { |
| 49 | + if pos >= d.len() - 1 - ONE_SPACE { |
| 50 | + Err(Error::ParseError( |
| 51 | + "There is no time after email", |
| 52 | + d.to_owned(), |
| 53 | + )) |
| 54 | + } else { |
| 55 | + Ok(pos) |
| 56 | + } |
| 57 | + })?; |
| 58 | + let (time_in_seconds, tzofz) = split2_at_space(&d[email_end + ONE_SPACE + 1..], |_, _| true)?; |
| 59 | + let (offset, sign) = parse_timezone_offset(tzofz)?; |
| 60 | + |
| 61 | + Ok(Signature { |
| 62 | + name: (&d[..email_begin - ONE_SPACE]).as_bstr(), |
| 63 | + email: (&d[email_begin + 1..email_end]).as_bstr(), |
| 64 | + time: Time { |
| 65 | + time: btoi::<u32>(time_in_seconds).map_err(|e| { |
| 66 | + Error::ParseIntegerError("Could parse to seconds", time_in_seconds.to_owned(), e) |
| 67 | + })?, |
| 68 | + offset, |
| 69 | + sign, |
| 70 | + }, |
| 71 | + }) |
| 72 | +} |
| 73 | + |
| 74 | +fn parse_message<'data>( |
| 75 | + d: &'data [u8], |
| 76 | + mut lines: impl Iterator<Item = &'data [u8]>, |
| 77 | +) -> Result<(Option<&'data BStr>, Option<&'data BStr>), Error> { |
| 78 | + const PGP_SIGNATURE_BEGIN: &[u8] = b"-----BEGIN PGP SIGNATURE-----"; |
| 79 | + const PGP_SIGNATURE_END: &[u8] = b"-----END PGP SIGNATURE-----"; |
| 80 | + |
| 81 | + Ok(match lines.next() { |
| 82 | + Some(l) if l.is_empty() => { |
| 83 | + let msg_begin = 0; // TODO: use nom to parse this or do it without needing nightly |
| 84 | + if msg_begin >= d.len() { |
| 85 | + return Err(Error::ParseError( |
| 86 | + "Message separator was not followed by message", |
| 87 | + d.to_owned(), |
| 88 | + )); |
| 89 | + } |
| 90 | + let mut msg_end = d.len(); |
| 91 | + let mut pgp_signature = None; |
| 92 | + if let Some(_pgp_begin_line) = lines.find(|l| l.starts_with(PGP_SIGNATURE_BEGIN)) { |
| 93 | + match lines.find(|l| l.starts_with(PGP_SIGNATURE_END)) { |
| 94 | + None => { |
| 95 | + return Err(Error::ParseError( |
| 96 | + "Didn't find end of signature marker", |
| 97 | + d.to_owned(), |
| 98 | + )) |
| 99 | + } |
| 100 | + Some(_) => { |
| 101 | + msg_end = d.len(); // TODO: use nom to parse this or do it without needing nightly |
| 102 | + pgp_signature = Some((&d[msg_end..]).as_bstr()) |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + (Some((&d[msg_begin..msg_end]).as_bstr()), pgp_signature) |
| 107 | + } |
| 108 | + Some(l) => { |
| 109 | + return Err(Error::ParseError( |
| 110 | + "Expected empty newline to separate message", |
| 111 | + l.to_owned(), |
| 112 | + )) |
| 113 | + } |
| 114 | + None => (None, None), |
| 115 | + }) |
| 116 | +} |
| 117 | + |
| 118 | +impl<'data> Tag<'data> { |
| 119 | + pub fn target(&self) -> crate::Id { |
| 120 | + <[u8; 20]>::from_hex(self.target).expect("prior validation") |
| 121 | + } |
| 122 | + pub fn from_bytes(d: &'data [u8]) -> Result<Tag<'data>, Error> { |
| 123 | + let mut lines = d.split(|&b| b == b'\n'); |
| 124 | + let (target, target_kind, name, signature) = |
| 125 | + match (lines.next(), lines.next(), lines.next(), lines.next()) { |
| 126 | + (Some(target), Some(kind), Some(name), Some(tagger)) => { |
| 127 | + let (_, target) = split2_at_space(target, |f, v| { |
| 128 | + f == b"object" && v.len() == 40 && <[u8; 20]>::from_hex(v).is_ok() |
| 129 | + })?; |
| 130 | + let kind = split2_at_space(kind, |f, _v| f == b"type") |
| 131 | + .and_then(|(_, kind)| crate::Kind::from_bytes(kind).map_err(Into::into))?; |
| 132 | + let (_, name) = split2_at_space(name, |f, _v| f == b"tag")?; |
| 133 | + let (_, rest) = split2_at_space(tagger, |f, _v| f == b"tagger")?; |
| 134 | + (target.as_bstr(), kind, name.as_bstr(), parse_signature(rest)?) |
| 135 | + } |
| 136 | + _ => { |
| 137 | + return Err(Error::ParseError( |
| 138 | + "Expected four lines: target, type, tag and tagger", |
| 139 | + d.to_owned(), |
| 140 | + )) |
| 141 | + } |
| 142 | + }; |
| 143 | + |
| 144 | + let (message, pgp_signature) = parse_message(d, &mut lines)?; |
| 145 | + |
| 146 | + Ok(Tag { |
| 147 | + target, |
| 148 | + name, |
| 149 | + target_kind, |
| 150 | + message, |
| 151 | + signature, |
| 152 | + pgp_signature, |
| 153 | + }) |
| 154 | + } |
| 155 | +} |
0 commit comments