This repository has been archived on 2023-09-20. You can view files and clone it, but cannot push or open issues or pull requests.
rust-opensmtpd/src/entry.rs

182 lines
4.1 KiB
Rust
Raw Normal View History

use crate::errors::Error;
use nom::{alt, alt_complete, call, complete, cond, do_parse, error_position, map_res, named, opt,
tag, take_until, take_while};
#[derive(Clone, Debug, PartialEq)]
pub enum Kind {
Report,
Filter,
}
#[derive(Clone, Debug, PartialEq)]
pub enum Subsystem {
SmtpIn,
2018-12-29 18:34:23 +01:00
SmtpOut,
}
#[derive(Clone, Debug, PartialEq)]
pub enum Event {
2018-12-29 18:34:23 +01:00
LinkConnect,
LinkDisconnect,
LinkIdentify,
LinkTls,
TxBegin,
TxMail,
TxRcpt,
2018-12-29 18:34:23 +01:00
TxEnvelope,
TxData,
TxCommit,
TxRollback,
ProtocolClient,
ProtocolServer,
Timeout,
2018-12-29 18:34:23 +01:00
FilterResponse,
}
#[derive(Debug)]
pub struct Entry {
pub kind: Kind,
pub version: u8,
pub timestamp: u64,
pub subsystem: Subsystem,
pub event: Event,
pub token: Option<u64>,
pub session_id: u64,
pub params: Option<String>,
}
fn is_ascii_digit(c: char) -> bool {
c.is_ascii_digit()
}
fn is_ascii_hexdigit(c: char) -> bool {
c.is_ascii_hexdigit()
}
fn to_u8(s: &str) -> Result<u8, std::num::ParseIntError> {
s.parse()
}
fn to_i32(s: &str) -> Result<i32, std::num::ParseIntError> {
s.parse()
}
fn to_u64(s: &str) -> Result<u64, std::num::ParseIntError> {
s.parse()
}
fn to_u64_hex(s: &str) -> Result<u64, std::num::ParseIntError> {
u64::from_str_radix(s, 16)
}
named!(parse_kind<&str, Kind>,
2018-12-29 18:34:23 +01:00
alt_complete!(
tag!("report") => { |_| Kind::Report } |
tag!("filter") => { |_| Kind::Filter }
)
);
named!(parse_subsystem<&str, Subsystem>,
2018-12-29 18:34:23 +01:00
alt_complete! (
tag!("smtp-in") => { |_| Subsystem::SmtpIn } |
tag!("smtp-out") => { |_| Subsystem::SmtpOut }
)
);
named!(parse_event<&str, Event>,
2018-12-29 18:34:23 +01:00
alt_complete!(
tag!("link-connect") => { |_| Event::LinkConnect } |
tag!("link-disconnect") => { |_| Event::LinkDisconnect } |
tag!("link-identify") => { |_| Event::LinkIdentify } |
tag!("link-tls") => { |_| Event::LinkTls } |
tag!("tx-begin") => { |_| Event::TxBegin } |
tag!("tx-mail") => { |_| Event::TxMail } |
2018-12-29 18:34:23 +01:00
tag!("tx-rcpt") => { |_| Event::TxRcpt } |
tag!("tx-envelope") => { |_| Event::TxEnvelope } |
tag!("tx-data") => { |_| Event::TxData } |
tag!("tx-commit") => { |_| Event::TxCommit } |
tag!("tx-rollback") => { |_| Event::TxRollback } |
tag!("protocol-client") => { |_| Event::ProtocolClient } |
tag!("protocol-server") => { |_| Event::ProtocolServer } |
tag!("timeout") => { |_| Event::Timeout } |
2018-12-29 18:34:23 +01:00
tag!("filter-response") => { |_| Event::FilterResponse }
)
);
named!(parse_timestamp<&str, u64>,
do_parse!(
sec: parse_u64 >>
tag!(".") >>
nsec: parse_i32 >>
(sec)
)
);
named!(parse_version<&str, u8>,
map_res!(take_while!(is_ascii_digit), to_u8)
);
named!(parse_i32<&str, i32>,
map_res!(take_while!(is_ascii_digit), to_i32)
);
named!(parse_u64<&str, u64>,
map_res!(take_while!(is_ascii_digit), to_u64)
);
named!(parse_u64_hex<&str, u64>,
map_res!(take_while!(is_ascii_hexdigit), to_u64_hex)
);
named!(parse_token<&str, u64>,
do_parse!(
token: parse_u64_hex >>
tag!("|") >>
(token)
)
);
named!(parse_params<&str, String>,
do_parse!(
tag!("|") >>
s: take_until!("\n") >>
(s.to_string())
)
);
named!(
parse_entry<&str, Entry>,
do_parse!(
kind: parse_kind >>
tag!("|") >>
version: parse_version >>
tag!("|") >>
timestamp: parse_timestamp >>
tag!("|") >>
subsystem: parse_subsystem >>
tag!("|") >>
event: parse_event >>
tag!("|") >>
token: cond!(kind == Kind::Filter, parse_token) >>
session_id: parse_u64_hex >>
params: opt!(parse_params) >>
(Entry {
kind,
version,
timestamp,
subsystem,
event,
token,
session_id,
params: params,
})
)
);
impl Entry {
pub fn from_str(entry: &str) -> Result<Entry, Error> {
let (_, res) = parse_entry(entry)?;
Ok(res)
}
}