├── rust-toolchain ├── sphinx ├── .gitignore ├── source │ ├── rustyknife.so │ ├── index.rst │ └── conf.py ├── Makefile └── make.bat ├── .gitignore ├── src ├── tests │ ├── mod.rs │ ├── test_headersection.rs │ ├── test_rfc5321.rs │ ├── test_rfc5322.rs │ └── test_rfc2231.rs ├── bin │ └── fuzz_mailbox.rs ├── rfc5234.rs ├── lib.rs ├── xforward.rs ├── rfc2047.rs ├── headersection.rs ├── util.rs ├── rfc3461.rs ├── pymod.rs ├── types.rs ├── rfc2231.rs ├── rfc5322.rs └── rfc5321.rs ├── setup.py ├── examples ├── parse_from.rs └── parse_smtp.rs ├── Cargo.toml ├── .travis.yml ├── README.md └── LICENSE /rust-toolchain: -------------------------------------------------------------------------------- 1 | stable 2 | -------------------------------------------------------------------------------- /sphinx/.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | -------------------------------------------------------------------------------- /sphinx/source/rustyknife.so: -------------------------------------------------------------------------------- 1 | ../../target/debug/librustyknife.so -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | /build 4 | /dist 5 | *.egg-info 6 | Cargo.lock 7 | -------------------------------------------------------------------------------- /src/tests/mod.rs: -------------------------------------------------------------------------------- 1 | mod test_headersection; 2 | mod test_rfc2231; 3 | mod test_rfc5321; 4 | mod test_rfc5322; 5 | -------------------------------------------------------------------------------- /src/bin/fuzz_mailbox.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate afl; 3 | 4 | fn main() { 5 | fuzz!(|data: &[u8]| { 6 | let _ = rustyknife::rfc5321::mailbox::(data); 7 | }); 8 | } 9 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup 2 | from setuptools_rust import Binding, RustExtension 3 | 4 | setup(name='rustyknife', 5 | version='0.1.3', 6 | rust_extensions=[RustExtension('rustyknife', 7 | 'Cargo.toml', binding=Binding.PyO3)], 8 | zip_safe=False 9 | ) 10 | -------------------------------------------------------------------------------- /examples/parse_from.rs: -------------------------------------------------------------------------------- 1 | extern crate rustyknife; 2 | 3 | use std::env; 4 | use std::os::unix::ffi::OsStringExt; 5 | 6 | use rustyknife::behaviour::Intl; 7 | use rustyknife::rfc5322::from; 8 | 9 | fn main() { 10 | let args : Vec<_> = env::args_os().skip(1).map(|x| x.into_vec()).collect(); 11 | let res = from::(&args[0]); 12 | println!("{:?}", res); 13 | let (rem, parsed) = res.unwrap(); 14 | 15 | println!("'{:?}'", parsed); 16 | println!("'{}'", String::from_utf8_lossy(rem)); 17 | } 18 | -------------------------------------------------------------------------------- /src/rfc5234.rs: -------------------------------------------------------------------------------- 1 | use nom::branch::alt; 2 | use nom::bytes::complete::tag; 3 | use nom::combinator::map; 4 | 5 | use crate::util::*; 6 | 7 | fn sp(input: &[u8]) -> NomResult<&[u8]> { 8 | tag(" ")(input) 9 | } 10 | 11 | fn htab(input: &[u8]) -> NomResult<&[u8]> { 12 | tag("\t")(input) 13 | } 14 | 15 | pub(crate) fn wsp(input: &[u8]) -> NomResult { 16 | map(alt((sp, htab)), |x| x[0])(input) 17 | } 18 | 19 | pub fn vchar(input: &[u8]) -> NomResult { 20 | map(take1_filter(|c| (0x21..=0x7e).contains(&c)), char::from)(input) 21 | } 22 | 23 | pub fn crlf(input: &[u8]) -> NomResult<&[u8]> { 24 | tag("\r\n")(input) 25 | } 26 | -------------------------------------------------------------------------------- /sphinx/Makefile: -------------------------------------------------------------------------------- 1 | # Minimal makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line. 5 | SPHINXOPTS = 6 | SPHINXBUILD = sphinx-build 7 | SPHINXPROJ = rustyknife 8 | SOURCEDIR = source 9 | BUILDDIR = build 10 | 11 | # Put it first so that "make" without argument is like "make help". 12 | help: 13 | @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) 14 | 15 | .PHONY: help Makefile 16 | 17 | # Catch-all target: route all unknown targets to Sphinx using the new 18 | # "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). 19 | %: Makefile 20 | @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) -------------------------------------------------------------------------------- /examples/parse_smtp.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | use std::os::unix::ffi::OsStrExt; 3 | 4 | use rustyknife::behaviour::Intl; 5 | use rustyknife::rfc5321::command; 6 | 7 | fn main() -> Result<(), String> { 8 | // Interpret each separate argument as a line ending in CRLF. 9 | let input : Vec = env::args_os().skip(1).fold(Vec::new(), |mut acc, x| { 10 | acc.extend(x.as_bytes()); 11 | acc.extend(b"\r\n"); 12 | acc 13 | }); 14 | 15 | println!("input: {:?}\n", String::from_utf8_lossy(&input)); 16 | 17 | let mut rem : &[u8] = &input; 18 | while !rem.is_empty() { 19 | let (res, parsed) = command::(rem).map_err(|e| format!("{:?}", e))?; 20 | 21 | rem = res; 22 | println!("{:?}", parsed); 23 | println!("remainder: {:?}\n", String::from_utf8_lossy(rem)); 24 | } 25 | 26 | 27 | Ok(()) 28 | } 29 | -------------------------------------------------------------------------------- /sphinx/make.bat: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | 3 | pushd %~dp0 4 | 5 | REM Command file for Sphinx documentation 6 | 7 | if "%SPHINXBUILD%" == "" ( 8 | set SPHINXBUILD=sphinx-build 9 | ) 10 | set SOURCEDIR=source 11 | set BUILDDIR=build 12 | set SPHINXPROJ=rustyknife 13 | 14 | if "%1" == "" goto help 15 | 16 | %SPHINXBUILD% >NUL 2>NUL 17 | if errorlevel 9009 ( 18 | echo. 19 | echo.The 'sphinx-build' command was not found. Make sure you have Sphinx 20 | echo.installed, then set the SPHINXBUILD environment variable to point 21 | echo.to the full path of the 'sphinx-build' executable. Alternatively you 22 | echo.may add the Sphinx directory to PATH. 23 | echo. 24 | echo.If you don't have Sphinx installed, grab it from 25 | echo.http://sphinx-doc.org/ 26 | exit /b 1 27 | ) 28 | 29 | %SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% 30 | goto end 31 | 32 | :help 33 | %SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% 34 | 35 | :end 36 | popd 37 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | #![cfg_attr(feature="nightly", feature(external_doc))] 2 | #![cfg_attr(feature="nightly", doc(include = "../README.md"))] 3 | 4 | #![warn(rust_2018_idioms)] 5 | #![allow(elided_lifetimes_in_paths)] 6 | #![warn(missing_docs)] 7 | 8 | #[macro_use] 9 | pub extern crate nom; 10 | 11 | /// Types used for varying parser behaviour. 12 | pub mod behaviour { 13 | /// Octets above 127 are replaced by a replacement character. 14 | pub struct Legacy; 15 | 16 | /// Octets above 127 are interpreted as UTF-8. 17 | /// 18 | /// * Activates message/global (RFC6532) support for message content. 19 | /// * Activates SMTPUTF8 support for SMTP. 20 | pub struct Intl; 21 | } 22 | 23 | #[macro_use] 24 | mod util; 25 | mod rfc5234; 26 | pub mod rfc2047; 27 | pub mod rfc2231; 28 | pub mod rfc5321; 29 | pub mod rfc5322; 30 | pub mod rfc3461; 31 | pub mod types; 32 | pub mod headersection; 33 | pub mod xforward; 34 | 35 | #[cfg(feature = "python")] 36 | mod pymod; 37 | 38 | #[cfg(test)] 39 | mod tests; 40 | 41 | pub use util::NomResult; 42 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rustyknife" 3 | description = "Fast, robust and safe email parsing library" 4 | version = "0.2.12" 5 | authors = ["Jonathan Bastien-Filiatrault "] 6 | edition = "2018" 7 | homepage = "https://github.com/zerospam/rustyknife" 8 | repository = "https://github.com/zerospam/rustyknife.git" 9 | license = "GPL-3.0+" 10 | readme = "README.md" 11 | categories = ["email", "parsing", "network-programming", "encoding"] 12 | exclude = ["sphinx/source/*.so"] 13 | 14 | [badges] 15 | travis-ci = { repository = "zerospam/rustyknife" } 16 | codecov = { repository = "zerospam/rustyknife", service = "github" } 17 | 18 | [features] 19 | default = ["quoted-string-rfc2047"] 20 | quoted-string-rfc2047 = [] 21 | python = ["memmap", "pyo3"] 22 | nightly = [] 23 | fuzz = ["afl"] 24 | 25 | [lib] 26 | crate-type = ["lib", "cdylib"] 27 | 28 | [dependencies] 29 | nom = "6.0" 30 | base64 = "0.13" 31 | idna = "0.2.0" 32 | serde = { version = "1.0", features = ["derive"], optional=true } 33 | 34 | memmap = { version = "0.7.0", optional=true } 35 | pyo3 = { version = "0.13", features = ["extension-module"], optional=true } 36 | afl = { version = "0.8", optional=true } 37 | encoding_rs = "0.8.33" 38 | charset = "0.1.3" 39 | 40 | [[bin]] 41 | name = "fuzz_mailbox" 42 | required-features = ["fuzz"] 43 | 44 | [profile.release] 45 | panic = "abort" 46 | 47 | [package.metadata.docs.rs] 48 | features = ["nightly"] 49 | -------------------------------------------------------------------------------- /sphinx/source/index.rst: -------------------------------------------------------------------------------- 1 | .. rustyknife documentation master file, created by 2 | sphinx-quickstart on Fri May 18 08:50:54 2018. 3 | You can adapt this file completely to your liking, but it should at least 4 | contain the root `toctree` directive. 5 | 6 | rustyknife: The quicker email chopper 7 | ===================================== 8 | 9 | .. toctree:: 10 | :maxdepth: 2 11 | :caption: Contents: 12 | 13 | .. automodule:: rustyknife 14 | :members: 15 | :exclude-members: mail_command, dsn_mail_params, rcpt_command, orcpt_address, xforward_params, from_, sender, reply_to, unstructured, content_type, content_transfer_encoding, content_disposition 16 | :undoc-members: 17 | :show-inheritance: 18 | 19 | MIME parameter parsing 20 | ====================== 21 | 22 | .. autofunction:: content_type 23 | .. autofunction:: content_transfer_encoding 24 | .. autofunction:: content_disposition 25 | 26 | RFC 5322 Email content parsing 27 | ============================== 28 | 29 | .. autofunction:: from_ 30 | .. autofunction:: sender 31 | .. autofunction:: reply_to 32 | .. autofunction:: unstructured 33 | 34 | SMTP command parsing 35 | ==================== 36 | 37 | .. autofunction:: mail_command 38 | .. autofunction:: dsn_mail_params 39 | .. autofunction:: rcpt_command 40 | .. autofunction:: orcpt_address 41 | .. autofunction:: xforward_params 42 | 43 | Indices and tables 44 | ================== 45 | 46 | * :ref:`genindex` 47 | * :ref:`modindex` 48 | * :ref:`search` 49 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: rust 2 | dist: xenial 3 | cache: cargo 4 | sudo: required 5 | 6 | rust: 7 | - stable 8 | 9 | env: 10 | matrix: 11 | - FEATURES="" 12 | - FEATURES="--no-default-features" 13 | - FEATURES="--no-default-features --features quoted-string-rfc2047" 14 | 15 | addons: 16 | apt: 17 | packages: 18 | # For codecov 19 | - libcurl4-openssl-dev 20 | - libelf-dev 21 | - libdw-dev 22 | - cmake 23 | - gcc 24 | - binutils-dev 25 | - libiberty-dev 26 | # For sphinx 27 | - python3-sphinx 28 | 29 | cache: 30 | directories: 31 | - /home/travis/.cargo 32 | 33 | before_cache: 34 | - rm -rf /home/travis/.cargo/registry 35 | 36 | script: 37 | - eval cargo build --verbose $FEATURES 38 | - eval cargo test --verbose $FEATURES 39 | 40 | # Coverage report 41 | after_success: 42 | - | 43 | if [[ "$TRAVIS_OS_NAME" == "linux" && "$TRAVIS_RUST_VERSION" == "stable" && "$FEATURES" == "" ]]; then 44 | wget https://github.com/SimonKagstrom/kcov/archive/master.tar.gz && 45 | tar xzf master.tar.gz && 46 | cd kcov-master && 47 | mkdir build && 48 | cd build && 49 | cmake .. && 50 | make && 51 | sudo make install && 52 | cd ../.. && 53 | rm -rf kcov-master && 54 | (GLOBIGNORE='*.d'; for file in target/debug/rustyknife-*; do mkdir -p "target/cov/$(basename $file)"; kcov --exclude-pattern=/.cargo,/usr/lib --verify "target/cov/$(basename $file)" "$file"; done) && 55 | bash <(curl -s https://codecov.io/bash) && 56 | echo "Uploaded code coverage" 57 | fi 58 | if [[ "$TRAVIS_OS_NAME" == "linux" && "$TRAVIS_RUST_VERSION" == "stable" && "$FEATURES" == "" ]]; then 59 | cargo doc --no-default-features --features quoted-string-rfc2047 --features nightly && 60 | /usr/share/sphinx/scripts/python3/sphinx-build sphinx/source target/doc/sphinx && 61 | sudo pip install ghp-import && 62 | ghp-import -n target/doc && 63 | git push -fq "https://${GH_TOKEN}@github.com/${TRAVIS_REPO_SLUG}.git" gh-pages 64 | fi 65 | -------------------------------------------------------------------------------- /src/xforward.rs: -------------------------------------------------------------------------------- 1 | //! Postfix [XFORWARD] SMTP extension parser 2 | //! 3 | //! [XFORWARD]: http://www.postfix.org/XFORWARD_README.html 4 | 5 | use charset::decode_ascii; 6 | 7 | use nom::branch::alt; 8 | use nom::bytes::complete::{tag, tag_no_case}; 9 | use nom::combinator::{opt, map}; 10 | use nom::multi::{many1}; 11 | use nom::sequence::{delimited, preceded, separated_pair}; 12 | 13 | use crate::rfc5234::{crlf, wsp}; 14 | use crate::rfc3461::xtext; 15 | use crate::util::*; 16 | 17 | /// XFORWARD parameter name and value. 18 | /// 19 | /// `"[UNAVAILABLE]"` is represented with a value of `None`. 20 | #[derive(Clone, Debug)] 21 | pub struct Param(pub &'static str, pub Option); 22 | 23 | fn command_name(input: &[u8]) -> NomResult<&'static str> { 24 | alt((map(tag_no_case("addr"), |_| "addr"), 25 | map(tag_no_case("helo"), |_| "helo"), 26 | map(tag_no_case("ident"), |_| "ident"), 27 | map(tag_no_case("name"), |_| "name"), 28 | map(tag_no_case("port"), |_| "port"), 29 | map(tag_no_case("proto"), |_| "proto"), 30 | map(tag_no_case("source"), |_| "source")))(input) 31 | } 32 | 33 | fn unavailable(input: &[u8]) -> NomResult> { 34 | map(tag_no_case("[unavailable]"), |_| None)(input) 35 | } 36 | 37 | fn value(input: &[u8]) -> NomResult> { 38 | alt((unavailable, map(xtext, |x| Some(decode_ascii(&x).into()))))(input) 39 | } 40 | 41 | fn param(input: &[u8]) -> NomResult { 42 | map(separated_pair(command_name, tag("="), value), 43 | |(c, v)| Param(c, v))(input) 44 | } 45 | 46 | /// Parse a XFORWARD b`"attr1=value attr2=value"` string. 47 | /// 48 | /// Returns a vector of [`Param`]. 49 | /// 50 | /// The parameter names must be valid and are normalized to 51 | /// lowercase. The values are xtext decoded and a value of 52 | /// `[UNAVAILABLE]` is translated to `None`. No other validation is 53 | /// done. 54 | pub fn xforward_params(input: &[u8]) -> NomResult> { 55 | fold_prefix0(preceded(opt(many1(wsp)), param), 56 | preceded(many1(wsp), param))(input) 57 | } 58 | 59 | pub fn command(input: &[u8]) -> NomResult> { 60 | delimited(tag_no_case("XFORWARD "), xforward_params, crlf)(input) 61 | } 62 | -------------------------------------------------------------------------------- /src/tests/test_headersection.rs: -------------------------------------------------------------------------------- 1 | use crate::headersection::*; 2 | 3 | fn hs(i: &[u8]) -> Vec { 4 | let (rem, parsed) = header_section(i).unwrap(); 5 | assert_eq!(rem.len(), 0); 6 | parsed 7 | } 8 | 9 | #[test] 10 | fn basic_line() { 11 | let parsed = hs(b"X-Mozilla-Status: 0001\r\nX-Mozilla-Status2: 00800000\r\n\r\n"); 12 | assert_eq!(parsed, [Ok((b"X-Mozilla-Status".as_ref(), b" 0001".as_ref())), 13 | Ok((b"X-Mozilla-Status2".as_ref(), b" 00800000".as_ref()))]); 14 | } 15 | 16 | #[test] 17 | fn bad_nl() { 18 | let parsed = hs(b"X-Mozilla-Status: 0001\r\nX-Mozilla-Status2: 00800000\nmore stuff\r\n\r\n".as_ref()); 19 | assert_eq!(parsed, [Ok((b"X-Mozilla-Status".as_ref(), b" 0001".as_ref())), 20 | Ok((b"X-Mozilla-Status2".as_ref(), b" 00800000\nmore stuff".as_ref()))]); 21 | } 22 | 23 | #[test] 24 | fn bad_cr() { 25 | let parsed = hs(b"X-Mozilla-Status: 0001\r\nX-Mozilla-Status2: 00800000\rmore stuff\r\n\r\n".as_ref()); 26 | assert_eq!(parsed, [Ok((b"X-Mozilla-Status".as_ref(), b" 0001".as_ref())), 27 | Ok((b"X-Mozilla-Status2".as_ref(), b" 00800000\rmore stuff".as_ref()))]); 28 | } 29 | 30 | #[test] 31 | fn folded_header() { 32 | let parsed = hs(b"X-Mozilla-Status: 0001\r\nContent-Type: multipart/alternative;\r\n boundary=\"------------000500020107050007070009\r\nX-Mozilla-Status2: 00800000\r\n\r\n"); 33 | assert_eq!(parsed, [Ok((b"X-Mozilla-Status".as_ref(), b" 0001".as_ref())), 34 | Ok((b"Content-Type".as_ref(), b" multipart/alternative;\r\n boundary=\"------------000500020107050007070009".as_ref())), 35 | Ok((b"X-Mozilla-Status2".as_ref(), b" 00800000".as_ref()))]); 36 | } 37 | 38 | #[test] 39 | fn big_garbage() { 40 | let parsed = hs(b"X-Mozilla-Status: 0001\r\nbad header 00800000\r\nX-Mozilla-Keys: badly\nformated\nstuff is should \r w\nork#!@#$%\r^&*()_|\"}{P?>< \r\nanother bad header <4F34184B.7040006@example.com>\r\nDate: Thu, 09 Feb 2012 14:02:35 -0500\r\n\r\n".as_ref()); 41 | assert_eq!(parsed, [Ok((b"X-Mozilla-Status".as_ref(), b" 0001".as_ref())), 42 | Err(b"bad header 00800000".as_ref()), 43 | Ok((b"X-Mozilla-Keys".as_ref(), b" badly\nformated\nstuff is should \r w\nork#!@#$%\r^&*()_|\"}{P?>< ".as_ref())), 44 | Err(b"another bad header <4F34184B.7040006@example.com>".as_ref()), 45 | Ok((b"Date".as_ref(), b" Thu, 09 Feb 2012 14:02:35 -0500".as_ref()))]); 46 | } 47 | -------------------------------------------------------------------------------- /src/rfc2047.rs: -------------------------------------------------------------------------------- 1 | //! [Header extensions for non-ASCII text] 2 | //! 3 | //! [Header extensions for non-ASCII text]: https://tools.ietf.org/html/rfc2047 4 | 5 | 6 | use std::borrow::Cow; 7 | 8 | use encoding_rs::{Encoding, UTF_8}; // TODO: was ASCII 9 | 10 | use nom::branch::alt; 11 | use nom::bytes::complete::{tag, take_while1}; 12 | use nom::combinator::{map, opt}; 13 | use nom::multi::many0; 14 | use nom::sequence::{delimited, preceded, terminated, tuple}; 15 | 16 | use crate::util::*; 17 | use crate::rfc3461::hexpair; 18 | 19 | fn token(input: &[u8]) -> NomResult<&[u8]> { 20 | take_while1(|c| (33..=126).contains(&c) && !b"()<>@,;:\\\"/[]?.=".contains(&c))(input) 21 | } 22 | 23 | fn encoded_text(input: &[u8]) -> NomResult<&[u8]> { 24 | take_while1(|c| match c {33..=62 | 64..=126 => true, _ => false})(input) 25 | } 26 | 27 | fn _qp_encoded_text(input: &[u8]) -> NomResult> { 28 | many0(alt(( 29 | preceded(tag("="), hexpair), 30 | map(tag("_"), |_| b' '), 31 | take1_filter(|_| true), 32 | )))(input) 33 | } 34 | 35 | // Decode the modified quoted-printable as defined by this RFC. 36 | fn decode_qp(input: &[u8]) -> Option> 37 | { 38 | exact!(input, _qp_encoded_text).ok().map(|(_, o)| o) 39 | } 40 | 41 | // Undoes the quoted-printable or base64 encoding. 42 | fn decode_text(encoding: &[u8], text: &[u8]) -> Option> 43 | { 44 | match &encoding.to_ascii_lowercase()[..] { 45 | b"q" => decode_qp(text), 46 | b"b" => base64::decode(text).ok(), 47 | _ => None, 48 | } 49 | } 50 | 51 | fn _encoded_word(input: &[u8]) -> NomResult<(Cow, Vec)> { 52 | map(tuple((preceded(tag("=?"), token), 53 | opt(preceded(tag("*"), token)), 54 | delimited(tag("?"), token, tag("?")), 55 | terminated(encoded_text, tag("?=")))), 56 | |(charset, _lang, encoding, text)| { 57 | (charset::decode_ascii(charset), decode_text(encoding, text).unwrap_or_else(|| text.to_vec())) 58 | })(input) 59 | } 60 | 61 | fn decode_charset((charset, bytes): (Cow, Vec)) -> String 62 | { 63 | Encoding::for_label(charset.as_bytes()).unwrap_or(UTF_8).decode_without_bom_handling(&bytes).0.to_string() 64 | } 65 | 66 | /// Decode an encoded word. 67 | /// 68 | /// # Examples 69 | /// ``` 70 | /// use rustyknife::rfc2047::encoded_word; 71 | /// 72 | /// let (_, decoded) = encoded_word(b"=?x-sjis?B?lEWWQI7Kg4GM9ZTygs6CtSiPzik=?=").unwrap(); 73 | /// assert_eq!(decoded, "忍法写メ光飛ばし(笑)"); 74 | /// ``` 75 | pub fn encoded_word(input: &[u8]) -> NomResult { 76 | map(_encoded_word, decode_charset)(input) 77 | } 78 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rustyknife 2 | 3 | [![crates.io](http://meritbadge.herokuapp.com/rustyknife)](https://crates.io/crates/rustyknife) 4 | [![Build Status](https://travis-ci.com/zerospam/rustyknife.svg?branch=master)](https://travis-ci.com/zerospam/rustyknife) 5 | [![codecov](https://codecov.io/gh/zerospam/rustyknife/branch/master/graph/badge.svg)](https://codecov.io/gh/zerospam/rustyknife) 6 | 7 | Email parsing library with a focus on reliably handling malformed data 8 | 9 | [Latest documentation] 10 | 11 | Features: 12 | * [Python module] 13 | * Email header parsing 14 | * ESMTP command parsing 15 | * Unit testing with a high coverage 16 | * Supports internationalized email headers through [RFC 2047] and [RFC 2231] decoding 17 | * Used to parse the content of millions of emails every day 18 | * [SMTPUTF8] support 19 | * [UTF-8 Internationalized Email Headers] support 20 | 21 | Roadmap: 22 | * Decoding of all common ESMTP extensions 23 | * Support more email content syntax 24 | 25 | # Examples 26 | ## Email header decoding 27 | ```rust 28 | use rustyknife::behaviour::Intl; 29 | use rustyknife::types::{DomainPart, DotAtom, Mailbox}; 30 | use rustyknife::rfc5322::{Address, Group, Mailbox as IMFMailbox}; 31 | use rustyknife::rfc5322::from; 32 | 33 | let (rem, parsed) = from::(b" A Group(Some people)\r 34 | :Chris Jones ,\r 35 | joe@example.org,\r 36 | John (my dear friend); (the end of the group)\r\n").unwrap(); 37 | 38 | // `rem` contains the unparsed remainder. 39 | assert!(rem.is_empty()); 40 | assert_eq!(parsed, [Address::Group(Group{ 41 | dname: "A Group".into(), 42 | members: vec![ 43 | IMFMailbox { dname: Some("Chris Jones".into()), 44 | address: Mailbox::from_imf(b"c@public.example").unwrap() }, 45 | IMFMailbox { dname: None, 46 | address: Mailbox::from_imf(b"joe@example.org").unwrap() }, 47 | IMFMailbox { dname: Some("John".into()), 48 | address: Mailbox::from_imf(b"jdoe@one.test").unwrap() } 49 | ] 50 | })]); 51 | ``` 52 | ## ESMTP command parsing 53 | ```rust 54 | use rustyknife::behaviour::Intl; 55 | use rustyknife::types::{Mailbox, QuotedString, Domain}; 56 | use rustyknife::rfc5321::{mail_command, Path, ReversePath, Param}; 57 | 58 | let (_, (path, params)) = mail_command::(b"MAIL FROM:<\"mr bob\"@example.com> RET=FULL ENVID=abc123\r\n").unwrap(); 59 | assert_eq!(path, ReversePath::Path( 60 | Path(Mailbox(QuotedString::from_smtp(b"\"mr bob\"").unwrap().into(), 61 | Domain::from_smtp(b"example.com").unwrap().into()), 62 | vec![]))); 63 | assert_eq!(params, [Param::new("RET", Some("FULL")).unwrap(), 64 | Param::new("ENVID", Some("abc123")).unwrap()]); 65 | ``` 66 | ## RFC 2047 encoded word decoding 67 | ```rust 68 | use rustyknife::rfc2047::encoded_word; 69 | let (_, decoded) = encoded_word(b"=?x-sjis?B?lEWWQI7Kg4GM9ZTygs6CtSiPzik=?=").unwrap(); 70 | assert_eq!(decoded, "忍法写メ光飛ばし(笑)"); 71 | ``` 72 | 73 | [RFC 2047]: https://tools.ietf.org/html/rfc2047 74 | [RFC 2231]: https://tools.ietf.org/html/rfc2231 75 | [SMTPUTF8]: https://tools.ietf.org/html/rfc6531 76 | [UTF-8 Internationalized Email Headers]: https://tools.ietf.org/html/rfc6532 77 | [Latest documentation]: https://zerospam.github.io/rustyknife/rustyknife/index.html 78 | [Python module]: https://zerospam.github.io/rustyknife/sphinx/index.html 79 | -------------------------------------------------------------------------------- /src/tests/test_rfc5321.rs: -------------------------------------------------------------------------------- 1 | use std::str::FromStr; 2 | use std::net::{IpAddr, Ipv4Addr}; 3 | 4 | use crate::behaviour::*; 5 | use crate::rfc5321::*; 6 | use crate::types::*; 7 | 8 | fn dp>(value: T) -> DomainPart { 9 | DomainPart::Domain(Domain(value.into())) 10 | } 11 | 12 | #[test] 13 | fn empty_from() { 14 | let (_, (path, params)) = mail_command::(b"MAIL FROM:<>\r\n").unwrap(); 15 | assert_eq!(path, ReversePath::Null); 16 | assert_eq!(params, []); 17 | } 18 | 19 | #[test] 20 | #[should_panic] 21 | fn empty_rcpt() { 22 | rcpt_command::(b"RCPT TO:<>\r\n").unwrap(); 23 | } 24 | 25 | #[test] 26 | #[should_panic] 27 | fn invalid_from() { 28 | mail_command::(b"MAIL FROM:\r\n").unwrap(); 29 | } 30 | 31 | #[test] 32 | #[should_panic] 33 | fn invalid_rcpt() { 34 | rcpt_command::(b"RCPT TO:\r\n").unwrap(); 35 | } 36 | 37 | #[test] 38 | fn esmtp_param() { 39 | let (_, (path, params)) = rcpt_command::(b"RCPT TO: ORCPT=rfc822;mrbob+AD@example.org\r\n").unwrap(); 40 | assert_eq!(path, ForwardPath::Path(Path(Mailbox(DotAtom("mrbob?".into()).into(), dp("example.org")), vec![]))); 41 | assert_eq!(params, [Param::new("ORCPT", Some("rfc822;mrbob+AD@example.org")).unwrap()]); 42 | } 43 | 44 | #[test] 45 | fn address_literal_domain() { 46 | let (_, (path, params)) = rcpt_command::(b"RCPT TO:\r\n").unwrap(); 47 | assert_eq!(path, ForwardPath::Path( 48 | Path(Mailbox(DotAtom("bob".into()).into(), 49 | DomainPart::Address(AddressLiteral::IP(IpAddr::V4(Ipv4Addr::from_str("127.0.0.1").unwrap())))), 50 | vec![]))); 51 | assert_eq!(params, []); 52 | } 53 | 54 | #[test] 55 | fn esmtp_from() { 56 | let (_, (path, params)) = mail_command::(b"MAIL FROM: RET=FULL ENVID=abc123\r\n").unwrap(); 57 | assert_eq!(path, ReversePath::Path( 58 | Path(Mailbox(DotAtom("bob".into()).into(), dp("example.com")), 59 | vec![]))); 60 | assert_eq!(params, [Param::new("RET", Some("FULL")).unwrap(), 61 | Param::new("ENVID", Some("abc123")).unwrap()]); 62 | } 63 | 64 | #[test] 65 | fn quoted_from() { 66 | let (_, (path, params)) = mail_command::(b"MAIL FROM:<\"bob the \\\"great \\\\ powerful\\\"\"@example.com>\r\n").unwrap(); 67 | assert_eq!(path, ReversePath::Path(Path( 68 | Mailbox(QuotedString("bob the \"great \\ powerful\"".into()).into(), dp("example.com")), 69 | vec![]))); 70 | assert_eq!(params, []); 71 | } 72 | 73 | #[test] 74 | fn postmaster_rcpt() { 75 | let (_, (path, params)) = rcpt_command::(b"RCPT TO:\r\n").unwrap(); 76 | assert_eq!(path, ForwardPath::PostMaster(None)); 77 | assert_eq!(params, []); 78 | 79 | let (_, (path, params)) = rcpt_command::(b"RCPT TO:\r\n").unwrap(); 80 | assert_eq!(path, ForwardPath::PostMaster(Some(Domain::from_smtp(b"Domain.example.org").unwrap()))); 81 | assert_eq!(params, []); 82 | } 83 | 84 | #[test] 85 | fn validate() { 86 | assert_eq!(validate_address::(b"mrbob@example.org"), true); 87 | assert_eq!(validate_address::(b"mrbob\"@example.org"), false); 88 | } 89 | 90 | #[test] 91 | fn overquoted_lp() { 92 | let mut lp = LocalPart::Quoted(QuotedString("a.b".into())); 93 | lp.smtp_try_unquote(); 94 | assert_eq!(lp, LocalPart::DotAtom(DotAtom("a.b".into()))); 95 | } 96 | 97 | #[test] 98 | fn normal_quoted_lp() { 99 | let mut lp = LocalPart::Quoted(QuotedString("a b".into())); 100 | lp.smtp_try_unquote(); 101 | assert_eq!(lp, LocalPart::Quoted(QuotedString("a b".into()))); 102 | } 103 | -------------------------------------------------------------------------------- /src/headersection.rs: -------------------------------------------------------------------------------- 1 | //! Robust parser for extracting a header section from a mail message 2 | //! 3 | //! Headers must be separated by CRLF. Loosely based on [RFC 5322] but 4 | //! tolerates bytes above 127. The header section is considered to be 5 | //! everything above a double CRLF. 6 | //! 7 | //! [RFC 5322]: https://tools.ietf.org/html/rfc5322 8 | 9 | use std::borrow::Cow; 10 | use std::str; 11 | 12 | use nom::branch::alt; 13 | use nom::bytes::streaming::{tag, take_while1, take_until}; 14 | use nom::combinator::{opt, map, map_opt, recognize}; 15 | use nom::multi::{many0, many1}; 16 | use nom::sequence::{pair, terminated, separated_pair}; 17 | 18 | use crate::util::*; 19 | 20 | fn fws(input: &[u8]) -> NomResult> { 21 | //CRLF is "semantically invisible" 22 | map(pair(opt(terminated(recognize_many0(wsp), crlf)), 23 | recognize_many1(wsp)), 24 | |(a, b)| { 25 | match a { 26 | Some(a) => { 27 | let mut out = String::from(str::from_utf8(a).unwrap()); 28 | out.push_str(str::from_utf8(b).unwrap()); 29 | Cow::from(out) 30 | }, 31 | None => Cow::from(str::from_utf8(b).unwrap()) 32 | } 33 | })(input) 34 | } 35 | 36 | fn ofws(input: &[u8]) -> NomResult> { 37 | map(opt(fws), |i| i.unwrap_or_else(|| Cow::from("")))(input) 38 | } 39 | 40 | fn sp(input: &[u8]) -> NomResult<&[u8]> { 41 | tag(" ")(input) 42 | } 43 | 44 | fn htab(input: &[u8]) -> NomResult<&[u8]> { 45 | tag("\t")(input) 46 | } 47 | 48 | fn wsp(input: &[u8]) -> NomResult { 49 | map(alt((sp, htab)), |x| x[0])(input) 50 | } 51 | 52 | fn vchar(input: &[u8]) -> NomResult { 53 | map(take1_filter(|c| (0x21..=0x7e).contains(&c)), char::from)(input) 54 | } 55 | 56 | fn crlf(input: &[u8]) -> NomResult<&[u8]> { 57 | tag("\r\n")(input) 58 | } 59 | 60 | /// Used to represent a split header. 61 | /// 62 | /// - The [`Ok`] variant is used when a valid header with a name was 63 | /// found. This variant contains a tuple with the header name and 64 | /// value. 65 | /// - The [`Err`] variant is returned when the the first line of a header 66 | /// does not contain a colon or contains 8bit bytes on the left hand 67 | /// side of the colon. 68 | pub type HeaderField<'a> = Result<(&'a[u8], &'a[u8]), &'a[u8]>; 69 | 70 | fn field_name(input: &[u8]) -> NomResult<&[u8]> { 71 | take_while1(|c| match c {33..=57 | 59..=126 => true, _ => false})(input) 72 | } 73 | 74 | fn until_crlf(input: &[u8]) -> NomResult<&[u8]> { 75 | map_opt(take_until("\r\n"), 76 | |i: &[u8]| if !i.is_empty() { 77 | Some(i) 78 | } else { 79 | None 80 | })(input) 81 | } 82 | 83 | fn unstructured(input: &[u8]) -> NomResult<&[u8]> { 84 | recognize(pair( 85 | many0(pair(ofws, alt((recognize(many1(vchar)), until_crlf)))), 86 | many0(wsp)))(input) 87 | } 88 | 89 | fn field(input: &[u8]) -> NomResult { 90 | map(terminated(separated_pair(field_name, tag(":"), unstructured), crlf), Ok)(input) 91 | } 92 | 93 | // Extension to be able to walk through crap. 94 | fn invalid_field(input: &[u8]) -> NomResult { 95 | map(terminated(until_crlf, crlf), Err)(input) 96 | } 97 | 98 | /// Zero copy mail message header splitter 99 | /// 100 | /// Returns the remaining input (the message body) and a vector of 101 | /// [HeaderField] on success. 102 | pub fn header_section(input: &[u8]) -> NomResult> { 103 | terminated(many0(alt((field, invalid_field))), 104 | opt(crlf))(input) 105 | } 106 | 107 | /// Parse a single header 108 | pub fn header(input: &[u8]) -> NomResult> { 109 | alt((map(alt((field, invalid_field)), Some), 110 | map(crlf, |_| None)))(input) 111 | } 112 | -------------------------------------------------------------------------------- /src/util.rs: -------------------------------------------------------------------------------- 1 | use nom::IResult; 2 | use nom::bytes::complete::take; 3 | use nom::combinator::{map, recognize, verify}; 4 | use nom::multi::{fold_many0, fold_many1}; 5 | // Change this to something else that implements ParseError to get a 6 | // different error type out of nom. 7 | pub(crate) type NomError<'a> = (); 8 | 9 | /// Shortcut type for taking in bytes and spitting out a success or NomError. 10 | pub type NomResult<'a, O, E=NomError<'a>> = IResult<&'a [u8], O, E>; 11 | 12 | macro_rules! nom_fromstr { 13 | ( $type:ty, $func:path ) => { 14 | impl std::str::FromStr for $type { 15 | type Err = (); 16 | 17 | fn from_str(s: &str) -> Result { 18 | exact!(s.as_bytes(), $func).map(|(_, r)| r).map_err(|_| ()) 19 | } 20 | } 21 | impl <'a> std::convert::TryFrom<&'a [u8]> for $type { 22 | type Error = nom::Err>; 23 | 24 | fn try_from(value: &'a [u8]) -> Result { 25 | exact!(value, $func).map(|(_, v)| v) 26 | } 27 | } 28 | impl <'a> std::convert::TryFrom<&'a str> for $type { 29 | type Error = nom::Err>; 30 | 31 | fn try_from(value: &'a str) -> Result { 32 | exact!(value.as_bytes(), $func).map(|(_, v)| v) 33 | } 34 | } 35 | } 36 | } 37 | 38 | macro_rules! nom_from_smtp { 39 | ( $smtp_func:path ) => { 40 | /// Parse using SMTP syntax. 41 | pub fn from_smtp(value: &[u8]) -> Result> { 42 | exact!(value, $smtp_func).map(|(_, v)| v) 43 | } 44 | } 45 | } 46 | macro_rules! nom_from_imf { 47 | ( $imf_func:path ) => { 48 | /// Parse using Internet Message Format syntax. 49 | pub fn from_imf(value: &[u8]) -> Result> { 50 | exact!(value, $imf_func).map(|(_, v)| v) 51 | } 52 | } 53 | } 54 | 55 | macro_rules! string_newtype { 56 | ( $type:ident ) => { 57 | impl std::fmt::Display for $type { 58 | fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { 59 | write!(f, "{}", self.0) 60 | } 61 | } 62 | impl std::convert::AsRef<[u8]> for $type { 63 | fn as_ref(&self) -> &[u8] { 64 | self.0.as_bytes() 65 | } 66 | } 67 | impl std::ops::Deref for $type { 68 | type Target = str; 69 | fn deref(&self) -> &Self::Target { 70 | &self.0 71 | } 72 | } 73 | impl From<$type> for String { 74 | fn from(value: $type) -> String { 75 | value.0 76 | } 77 | } 78 | 79 | impl std::fmt::Debug for $type { 80 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 81 | write!(f, "{:?}", self.0) 82 | } 83 | } 84 | } 85 | } 86 | 87 | pub(crate) fn fold_prefix0(mut prefix: F, mut cont: G) -> impl FnMut(I) -> IResult, E> 88 | where I: Clone + PartialEq, 89 | F: FnMut(I) -> IResult, 90 | G: FnMut(I) -> IResult, 91 | E: nom::error::ParseError::, 92 | Vec: Clone, 93 | { 94 | move |input: I| { 95 | let (rem, v1) = prefix(input)?; 96 | let out = vec![v1]; 97 | 98 | fold_many0(&mut cont, out, |mut acc, value| { 99 | acc.push(value); 100 | acc 101 | })(rem) 102 | } 103 | } 104 | 105 | pub(crate) fn recognize_many0(f: F) -> impl FnMut(I) -> IResult 106 | where I: Clone + PartialEq + nom::Slice> + nom::Offset, 107 | F: FnMut(I) -> IResult, 108 | E: nom::error::ParseError::, 109 | { 110 | recognize(fold_many0(f, (), |_, _| ())) 111 | } 112 | 113 | pub(crate) fn recognize_many1(f: F) -> impl FnMut(I) -> IResult 114 | where I: Clone + PartialEq + nom::Slice> + nom::Offset, 115 | F: FnMut(I) -> IResult, 116 | E: nom::error::ParseError::, 117 | { 118 | recognize(fold_many1(f, (), |_, _| ())) 119 | } 120 | 121 | pub(crate) fn take1_filter(pred: F) -> impl Fn(&[u8]) -> NomResult 122 | where F: Fn(u8) -> bool, 123 | { 124 | move |input| { 125 | verify(map(take(1usize), |c: &[u8]| c[0]), |c| pred(*c))(input) 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /src/tests/test_rfc5322.rs: -------------------------------------------------------------------------------- 1 | use crate::behaviour::{Intl, Legacy}; 2 | use crate::rfc5322::{Address, Group, Mailbox, from, reply_to, sender, unstructured}; 3 | use crate::types::{Mailbox as SMTPMailbox, *}; 4 | 5 | fn dp>(value: T) -> DomainPart { 6 | DomainPart::Domain(Domain(value.into())) 7 | } 8 | 9 | fn parse_single<'a, E, F>(syntax: F, input: &'a [u8]) -> Mailbox 10 | where F: Fn(&'a [u8]) -> Result<(&'a [u8], Vec
), E>, 11 | E: std::fmt::Debug 12 | { 13 | let (rem, mut parsed) = syntax(input).unwrap(); 14 | assert_eq!(rem.len(), 0); 15 | assert_eq!(parsed.len(), 1); 16 | 17 | match parsed.remove(0) { 18 | Address::Mailbox(mbox) => mbox, 19 | _ => unreachable!(), 20 | } 21 | } 22 | 23 | #[test] 24 | fn concat_atom() { 25 | assert_eq!(parse_single(from::, b" atom ").dname, Some("atom".into())); 26 | assert_eq!(parse_single(from::, b" atom atom ").dname, Some("atom atom".into())); 27 | assert_eq!(parse_single(from::, b" atom atom atom ").dname, Some("atom atom atom".into())); 28 | } 29 | 30 | #[test] 31 | fn concat_qs() { 32 | let parsed = parse_single(from::, b"\"no\" \"space\" space space \"two space\" \"end space \" \r\n"); 33 | assert_eq!(parsed.dname, Some("nospace space space two spaceend space ".into())); 34 | } 35 | 36 | #[test] 37 | fn simple_from() { 38 | let parsed = parse_single(from::, b"John Doe \r\n"); 39 | assert_eq!(parsed.dname, Some("John Doe".into())); 40 | assert_eq!(parsed.address, SMTPMailbox(DotAtom("jdoe".into()).into(), dp("machine.example"))) 41 | } 42 | 43 | #[test] 44 | fn simple_sender() { 45 | let (rem, parsed) = sender::(b"Michael Jones \r\n").unwrap(); 46 | assert_eq!(rem.len(), 0); 47 | if let Address::Mailbox(Mailbox{dname, address}) = parsed { 48 | assert_eq!(dname, Some("Michael Jones".into())); 49 | assert_eq!(address, SMTPMailbox(DotAtom("mjones".into()).into(), dp("machine.example"))) 50 | } else { 51 | unreachable!(); 52 | } 53 | } 54 | 55 | #[test] 56 | fn simple_reply_to() { 57 | let parsed = parse_single(reply_to::, b"\"Mary Smith: Personal Account\" \r\n"); 58 | assert_eq!(parsed.dname, Some("Mary Smith: Personal Account".into())); 59 | assert_eq!(parsed.address, SMTPMailbox(DotAtom("smith".into()).into(), dp("home.example"))) 60 | } 61 | 62 | #[test] 63 | fn group_reply_to() { 64 | let (rem, parsed) = reply_to::(b" A Group(Some people)\r\n :Chris Jones ,\r\n joe@example.org,\r\n John (my dear friend); (the end of the group)\r\n").unwrap(); 65 | assert_eq!(rem.len(), 0); 66 | assert_eq!(parsed, [Address::Group(Group{ 67 | dname: "A Group".into(), 68 | members: vec![ 69 | Mailbox { dname: Some("Chris Jones".into()), 70 | address: SMTPMailbox(DotAtom("c".into()).into(), dp("public.example"))}, 71 | Mailbox { dname: None, 72 | address: SMTPMailbox(DotAtom("joe".into()).into(), dp("example.org"))}, 73 | Mailbox { dname: Some("John".into()), 74 | address: SMTPMailbox(DotAtom("jdoe".into()).into(), dp("one.test"))}, 75 | ] 76 | })]); 77 | } 78 | 79 | #[test] 80 | fn multi_reply_to() { 81 | let (rem, parsed) = reply_to::(b"Mary Smith , jdoe@example.org, Who? \r\n").unwrap(); 82 | assert_eq!(rem.len(), 0); 83 | assert_eq!(parsed, [ 84 | Address::Mailbox(Mailbox { dname: Some("Mary Smith".into()), 85 | address: SMTPMailbox(DotAtom("mary".into()).into(), dp("x.test"))}), 86 | Address::Mailbox(Mailbox { dname: None, 87 | address: SMTPMailbox(DotAtom("jdoe".into()).into(), dp("example.org"))}), 88 | Address::Mailbox(Mailbox { dname: Some("Who?".into()), 89 | address: SMTPMailbox(DotAtom("one".into()).into(), dp("y.test"))}), 90 | ]); 91 | } 92 | 93 | #[test] 94 | fn folded_qs() { 95 | let parsed = parse_single(reply_to::, b"\"Mary\r\n Smith\"\r\n \r\n"); 96 | assert_eq!(parsed.dname, Some("Mary Smith".into())); 97 | assert_eq!(parsed.address, SMTPMailbox(DotAtom("mary".into()).into(), dp("x.test"))); 98 | } 99 | 100 | #[test] 101 | fn intl_subject() { 102 | let (rem, parsed) = unstructured::(b"=?x-sjis?B?lEWWQI7Kg4GM9ZTygs6CtSiPzik=?=").unwrap(); 103 | assert_eq!(rem.len(), 0); 104 | assert_eq!(parsed, "忍法写メ光飛ばし(笑)"); 105 | } 106 | 107 | #[test] 108 | fn direct_utf8() { 109 | let input = b"\xc3\xa9"; 110 | let (rem, parsed) = unstructured::(input).unwrap(); 111 | assert_eq!(rem.len(), 0); 112 | assert_eq!(parsed, "é"); 113 | 114 | let (rem, parsed) = unstructured::(input).unwrap(); 115 | assert_eq!(rem.len(), 0); 116 | assert_eq!(parsed, "\u{fffd}\u{fffd}"); 117 | } 118 | 119 | #[test] 120 | fn invalid_latin1() { 121 | let input = b"\xe9"; 122 | 123 | let (rem, parsed) = unstructured::(input).unwrap(); 124 | assert_eq!(rem.len(), 0); 125 | assert_eq!(parsed, "\u{fffd}"); 126 | 127 | let (rem, parsed) = unstructured::(input).unwrap(); 128 | assert_eq!(rem.len(), 0); 129 | assert_eq!(parsed, "\u{fffd}"); 130 | } 131 | -------------------------------------------------------------------------------- /sphinx/source/conf.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # Configuration file for the Sphinx documentation builder. 4 | # 5 | # This file does only contain a selection of the most common options. For a 6 | # full list see the documentation: 7 | # http://www.sphinx-doc.org/en/master/config 8 | 9 | # -- Path setup -------------------------------------------------------------- 10 | 11 | # If extensions (or modules to document with autodoc) are in another directory, 12 | # add these directories to sys.path here. If the directory is relative to the 13 | # documentation root, use os.path.abspath to make it absolute, like shown here. 14 | # 15 | import os 16 | import sys 17 | sys.path.insert(0, os.path.abspath('.')) 18 | 19 | 20 | # -- Project information ----------------------------------------------------- 21 | 22 | project = 'rustyknife' 23 | copyright = '2018, Jonathan Bastien-Filiatrault' 24 | author = 'Jonathan Bastien-Filiatrault' 25 | 26 | # The short X.Y version 27 | version = '' 28 | # The full version, including alpha/beta/rc tags 29 | release = '' 30 | 31 | 32 | # -- General configuration --------------------------------------------------- 33 | 34 | # If your documentation needs a minimal Sphinx version, state it here. 35 | # 36 | # needs_sphinx = '1.0' 37 | 38 | # Add any Sphinx extension module names here, as strings. They can be 39 | # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom 40 | # ones. 41 | extensions = [ 42 | 'sphinx.ext.autodoc', 43 | 'sphinx.ext.doctest', 44 | 'sphinx.ext.intersphinx', 45 | 'sphinx.ext.viewcode', 46 | ] 47 | 48 | # Add any paths that contain templates here, relative to this directory. 49 | templates_path = ['_templates'] 50 | 51 | # The suffix(es) of source filenames. 52 | # You can specify multiple suffix as a list of string: 53 | # 54 | # source_suffix = ['.rst', '.md'] 55 | source_suffix = '.rst' 56 | 57 | # The master toctree document. 58 | master_doc = 'index' 59 | 60 | # The language for content autogenerated by Sphinx. Refer to documentation 61 | # for a list of supported languages. 62 | # 63 | # This is also used if you do content translation via gettext catalogs. 64 | # Usually you set "language" from the command line for these cases. 65 | language = None 66 | 67 | # List of patterns, relative to source directory, that match files and 68 | # directories to ignore when looking for source files. 69 | # This pattern also affects html_static_path and html_extra_path . 70 | exclude_patterns = [] 71 | 72 | # The name of the Pygments (syntax highlighting) style to use. 73 | pygments_style = 'sphinx' 74 | 75 | 76 | # -- Options for HTML output ------------------------------------------------- 77 | 78 | # The theme to use for HTML and HTML Help pages. See the documentation for 79 | # a list of builtin themes. 80 | # 81 | html_theme = 'classic' 82 | 83 | # Theme options are theme-specific and customize the look and feel of a theme 84 | # further. For a list of options available for each theme, see the 85 | # documentation. 86 | # 87 | # html_theme_options = {} 88 | 89 | # Add any paths that contain custom static files (such as style sheets) here, 90 | # relative to this directory. They are copied after the builtin static files, 91 | # so a file named "default.css" will overwrite the builtin "default.css". 92 | html_static_path = ['_static'] 93 | 94 | # Custom sidebar templates, must be a dictionary that maps document names 95 | # to template names. 96 | # 97 | # The default sidebars (for documents that don't match any pattern) are 98 | # defined by theme itself. Builtin themes are using these templates by 99 | # default: ``['localtoc.html', 'relations.html', 'sourcelink.html', 100 | # 'searchbox.html']``. 101 | # 102 | # html_sidebars = {} 103 | 104 | 105 | # -- Options for HTMLHelp output --------------------------------------------- 106 | 107 | # Output file base name for HTML help builder. 108 | htmlhelp_basename = 'rustyknifedoc' 109 | 110 | 111 | # -- Options for LaTeX output ------------------------------------------------ 112 | 113 | latex_elements = { 114 | # The paper size ('letterpaper' or 'a4paper'). 115 | # 116 | # 'papersize': 'letterpaper', 117 | 118 | # The font size ('10pt', '11pt' or '12pt'). 119 | # 120 | # 'pointsize': '10pt', 121 | 122 | # Additional stuff for the LaTeX preamble. 123 | # 124 | # 'preamble': '', 125 | 126 | # Latex figure (float) alignment 127 | # 128 | # 'figure_align': 'htbp', 129 | } 130 | 131 | # Grouping the document tree into LaTeX files. List of tuples 132 | # (source start file, target name, title, 133 | # author, documentclass [howto, manual, or own class]). 134 | latex_documents = [ 135 | (master_doc, 'rustyknife.tex', 'rustyknife Documentation', 136 | 'Jonathan Bastien-Filiatrault', 'manual'), 137 | ] 138 | 139 | 140 | # -- Options for manual page output ------------------------------------------ 141 | 142 | # One entry per manual page. List of tuples 143 | # (source start file, name, description, authors, manual section). 144 | man_pages = [ 145 | (master_doc, 'rustyknife', 'rustyknife Documentation', 146 | [author], 1) 147 | ] 148 | 149 | 150 | # -- Options for Texinfo output ---------------------------------------------- 151 | 152 | # Grouping the document tree into Texinfo files. List of tuples 153 | # (source start file, target name, title, author, 154 | # dir menu entry, description, category) 155 | texinfo_documents = [ 156 | (master_doc, 'rustyknife', 'rustyknife Documentation', 157 | author, 'rustyknife', 'One line description of project.', 158 | 'Miscellaneous'), 159 | ] 160 | 161 | 162 | # -- Extension configuration ------------------------------------------------- 163 | 164 | # -- Options for intersphinx extension --------------------------------------- 165 | 166 | # Example configuration for intersphinx: refer to the Python standard library. 167 | intersphinx_mapping = {'https://docs.python.org/': None} 168 | -------------------------------------------------------------------------------- /src/rfc3461.rs: -------------------------------------------------------------------------------- 1 | //! [SMTP DSN] (delivery status notification) extension 2 | //! 3 | //! [SMTP DSN]: https://tools.ietf.org/html/rfc3461 4 | 5 | use std::borrow::Cow; 6 | use std::str; 7 | 8 | use crate::util::*; 9 | 10 | use charset::decode_ascii; 11 | 12 | use nom::branch::alt; 13 | use nom::bytes::complete::{take, tag, tag_no_case}; 14 | use nom::character::is_hex_digit; 15 | use nom::combinator::{map, map_res, verify}; 16 | use nom::multi::many0; 17 | use nom::sequence::{preceded, separated_pair}; 18 | 19 | use crate::rfc5322::atom; 20 | 21 | pub(crate) fn hexpair(input: &[u8]) -> NomResult { 22 | map_res(verify(take(2usize), |c: &[u8]| c.iter().cloned().all(is_hex_digit)), 23 | |x| u8::from_str_radix(str::from_utf8(x).unwrap(), 16))(input) 24 | } 25 | 26 | fn hexchar(input: &[u8]) -> NomResult { 27 | preceded(tag("+"), hexpair)(input) 28 | } 29 | 30 | fn xchar(input: &[u8]) -> NomResult { 31 | take1_filter(|c| match c { 33..=42 | 44..=60 | 62..=126 => true, _ => false})(input) 32 | } 33 | 34 | pub(crate) fn xtext(input: &[u8]) -> NomResult> { 35 | many0(alt((xchar, hexchar)))(input) 36 | } 37 | 38 | fn _printable_xtext(input: &[u8]) -> NomResult> { 39 | verify(xtext, |xtext: &[u8]| { 40 | xtext.iter().all(|c| match c { 9..=13 | 32..=126 => true, _ => false}) 41 | })(input) 42 | } 43 | 44 | /// Parse the ESMTP ORCPT parameter that may be present on a RCPT TO command. 45 | /// 46 | /// Returns the address type and the decoded original recipient address. 47 | /// # Examples 48 | /// ``` 49 | /// use rustyknife::rfc3461::orcpt_address; 50 | /// 51 | /// let (_, split) = orcpt_address(b"rfc822;bob@example.org").unwrap(); 52 | /// 53 | /// assert_eq!(split, ("rfc822".into(), "bob@example.org".into())); 54 | /// ``` 55 | pub fn orcpt_address(input: &[u8]) -> NomResult<(Cow, Cow)> { 56 | map(separated_pair(atom::, tag(";"), _printable_xtext), 57 | |(a, b)| (decode_ascii(a), Cow::Owned(decode_ascii(&b).into_owned())))(input) 58 | } 59 | 60 | /// The DSN return type desired by the sender. 61 | #[derive(Debug, PartialEq)] 62 | pub enum DSNRet { 63 | /// Return full the full message content. 64 | Full, 65 | /// Return only the email headers. 66 | Hdrs, 67 | } 68 | 69 | /// DSN parameters for the MAIL command. 70 | #[derive(Debug, PartialEq)] 71 | pub struct DSNMailParams { 72 | /// A mail transaction identifier provided by the sender. 73 | /// 74 | /// `None` if not specified. 75 | pub envid: Option, 76 | /// The DSN return type desired by the sender. 77 | /// 78 | /// `None` if not specified. 79 | pub ret: Option, 80 | } 81 | 82 | type Param<'a> = (&'a str, Option<&'a str>); 83 | 84 | /// Parse a list of ESMTP parameters on a MAIL FROM command into a 85 | /// [`DSNMailParams`] option block. 86 | /// 87 | /// Returns the option block and a vector of parameters that were not 88 | /// consumed. 89 | /// # Examples 90 | /// ``` 91 | /// use rustyknife::rfc3461::{dsn_mail_params, DSNRet, DSNMailParams}; 92 | /// let input = &[("RET", Some("HDRS")), 93 | /// ("OTHER", None)]; 94 | /// 95 | /// let (params, other) = dsn_mail_params(input).unwrap(); 96 | /// 97 | /// assert_eq!(params, DSNMailParams{ envid: None, ret: Some(DSNRet::Hdrs) }); 98 | /// assert_eq!(other, [("OTHER", None)]); 99 | /// ``` 100 | pub fn dsn_mail_params<'a>(input: &[Param<'a>]) -> Result<(DSNMailParams, Vec>), &'static str> 101 | { 102 | let mut out = Vec::new(); 103 | let mut envid_val : Option = None; 104 | let mut ret_val : Option = None; 105 | 106 | for (name, value) in input { 107 | match (name.to_lowercase().as_str(), value) { 108 | ("ret", Some(value)) => { 109 | if ret_val.is_some() { return Err("Duplicate RET"); } 110 | 111 | ret_val = match value.to_lowercase().as_str() { 112 | "full" => Some(DSNRet::Full), 113 | "hdrs" => Some(DSNRet::Hdrs), 114 | _ => return Err("Invalid RET") 115 | } 116 | }, 117 | 118 | ("envid", Some(value)) => { 119 | if envid_val.is_some() { return Err("Duplicate ENVID"); } 120 | let value = value.as_bytes(); 121 | if value.len() > 100 { 122 | return Err("ENVID over 100 bytes"); 123 | } 124 | if let Ok((_, parsed)) = exact!(value, _printable_xtext) { 125 | envid_val = Some(decode_ascii(&parsed).into()); 126 | } else { 127 | return Err("Invalid ENVID"); 128 | } 129 | }, 130 | ("ret", None) => { return Err("RET without value") }, 131 | ("envid", None) => { return Err("ENVID without value") }, 132 | _ => { 133 | out.push((*name, *value)) 134 | } 135 | } 136 | } 137 | 138 | Ok((DSNMailParams{envid: envid_val, ret: ret_val}, out)) 139 | } 140 | 141 | pub struct Notify { 142 | pub on_success: bool, 143 | pub on_failure: bool, 144 | pub delay: bool, 145 | } 146 | 147 | fn convert_notify_list(input: Vec<&str>) -> Notify { 148 | let mut on_success = false; 149 | let mut on_failure = false; 150 | let mut delay = false; 151 | 152 | for item in input { 153 | if item.eq_ignore_ascii_case("success") { 154 | on_success = true 155 | } else if item.eq_ignore_ascii_case("failure") { 156 | on_failure = true 157 | } else if item.eq_ignore_ascii_case("delay") { 158 | delay = true 159 | } 160 | } 161 | 162 | Notify { 163 | on_success, 164 | on_failure, 165 | delay, 166 | } 167 | } 168 | 169 | fn notify_item(input: &str) -> Result<(&str, &str), nom::Err<()>> { 170 | alt(( 171 | tag_no_case("success"), 172 | tag_no_case("failure"), 173 | tag_no_case("delay"), 174 | ))(input) 175 | } 176 | 177 | pub fn dsn_notify(input: &str) -> Result<(&str, Notify), nom::Err<()>> { 178 | alt(( 179 | map(tag_no_case("never"), |_| Notify { 180 | on_success: false, 181 | on_failure: false, 182 | delay: false, 183 | }), 184 | map( 185 | fold_prefix0(notify_item, preceded(tag(","), notify_item)), 186 | convert_notify_list, 187 | ), 188 | ))(input) 189 | } 190 | -------------------------------------------------------------------------------- /src/tests/test_rfc2231.rs: -------------------------------------------------------------------------------- 1 | use crate::rfc2231::*; 2 | use crate::rfc2231::{ContentTransferEncoding as CTE, ContentDisposition as CD}; 3 | 4 | 5 | #[cfg_attr(not(feature = "quoted-string-rfc2047"), should_panic)] 6 | #[test] 7 | fn rfc2047() { 8 | let (rem, (mtype, params)) = content_type(b" message/external-body; name=\"a =?utf-8?b?w6l0w6kgYmxvcXXDqQ==?= par ZEROSPAM.eml\"").unwrap(); 9 | assert_eq!(rem.len(), 0); 10 | assert_eq!(mtype, "message/external-body"); 11 | assert_eq!(params, [("name".into(), "a été bloqué par ZEROSPAM.eml".into())]); 12 | } 13 | 14 | 15 | #[test] 16 | #[should_panic] 17 | // I am not sure if this should be supported 18 | fn header_lf() { 19 | let (rem, (mtype, params)) = content_type(b"application/pdf; name=\n\t\"=?Windows-1252?Q?Fiche_d=92information_relative_=E0_la_garantie_facultati?=\n =?Windows-1252?Q?ve.pdf?=\"\n").unwrap(); 20 | assert_eq!(rem.len(), 0); 21 | assert_eq!(mtype, "application/pdf"); 22 | assert_eq!(params, [("name".into(), "Fiche d’information relative à la garantie facultative.pdf".into())]); 23 | } 24 | 25 | #[cfg_attr(not(feature = "quoted-string-rfc2047"), should_panic)] 26 | #[test] 27 | fn header_crlf() { 28 | let (rem, (mtype, params)) = content_type(b"application/pdf; name=\r\n\t\"=?Windows-1252?Q?Fiche_d=92information_relative_=E0_la_garantie_facultati?=\r\n =?Windows-1252?Q?ve.pdf?=\"\r\n").unwrap(); 29 | assert_eq!(rem.len(), 0); 30 | assert_eq!(mtype, "application/pdf"); 31 | assert_eq!(params, [("name".into(), "Fiche d’information relative à la garantie facultative.pdf".into())]); 32 | } 33 | 34 | #[cfg_attr(not(feature = "quoted-string-rfc2047"), should_panic)] 35 | #[test] 36 | fn attmsg1() { 37 | let (rem, (mtype, params)) = content_type(b"message/rfc822;\r\n name=\"=?windows-1252?Q?=5BThe_Listserve=5D_Have_you_ever_seen_somet?=\r\n =?windows-1252?Q?hing_you_couldn=92t_explain=3F=2Eeml?=\"").unwrap(); 38 | assert_eq!(rem.len(), 0); 39 | assert_eq!(mtype, "message/rfc822"); 40 | assert_eq!(params, [("name".into(), "[The Listserve] Have you ever seen something you couldn’t explain?.eml".into())]); 41 | } 42 | 43 | #[test] 44 | fn attmsg2() { 45 | let (rem, (disp, params)) = content_disposition(b" attachment;\r\n filename*0*=windows-1252''%5B%54%68%65%20%4C%69%73%74%73%65%72%76%65%5D%20;\r\n filename*1*=%48%61%76%65%20%79%6F%75%20%65%76%65%72%20%73%65%65%6E%20%73;\r\n filename*2*=%6F%6D%65%74%68%69%6E%67%20%79%6F%75%20%63%6F%75%6C%64%6E%92;\r\n filename*3*=%74%20%65%78%70%6C%61%69%6E%3F%2E%65%6D%6C").unwrap(); 46 | assert_eq!(rem.len(), 0); 47 | assert_eq!(disp, CD::Attachment); 48 | assert_eq!(params, [("filename".into(), "[The Listserve] Have you ever seen something you couldn’t explain?.eml".into())]); 49 | } 50 | 51 | #[cfg_attr(not(feature = "quoted-string-rfc2047"), should_panic)] 52 | #[test] 53 | fn attmsg3() { 54 | let (rem, (mtype, params)) = content_type(b"message/rfc822;\r\n name=\"[decoupe CNC] Re: H_S_ envoyer de =?windows-1252?Q?=AB_gros_=BB_fic?=\r\n =?windows-1252?Q?hiers=2Eeml?=\"").unwrap(); 55 | assert_eq!(rem.len(), 0); 56 | assert_eq!(mtype, "message/rfc822"); 57 | assert_eq!(params, [("name".into(), "[decoupe CNC] Re: H_S_ envoyer de « gros » fichiers.eml".into())]); 58 | } 59 | 60 | #[test] 61 | fn attmsg4() { 62 | let (rem, (disp, params)) = content_disposition(b"attachment;\r\n filename*0*=windows-1252''%5B%64%65%63%6F%75%70%65%20%43%4E%43%5D%20%52%65;\r\n filename*1*=%3A%20%48%5F%53%5F%20%65%6E%76%6F%79%65%72%20%64%65%20%AB%20;\r\n filename*2*=%67%72%6F%73%20%BB%20%66%69%63%68%69%65%72%73%2E%65%6D%6C").unwrap(); 63 | assert_eq!(rem.len(), 0); 64 | assert_eq!(disp, CD::Attachment); 65 | assert_eq!(params, [("filename".into(), "[decoupe CNC] Re: H_S_ envoyer de « gros » fichiers.eml".into())]); 66 | } 67 | 68 | // Cases from RFC2231 below 69 | #[test] 70 | fn simple_long() { 71 | let (rem, (mtype, mut params)) = content_type(b"message/external-body; access-type=URL;\r\n URL*0=\"ftp://\";\r\n URL*1=\"cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer.tar\"").unwrap(); 72 | assert_eq!(rem.len(), 0); 73 | assert_eq!(mtype, "message/external-body"); 74 | params.sort(); 75 | assert_eq!(params, [("access-type".into(), "URL".into()), 76 | ("url".into(), "ftp://cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer.tar".into())]); 77 | } 78 | 79 | #[test] 80 | fn encoded_single() { 81 | let (rem, (mtype, params)) = content_type(b"application/x-stuff;\r\n title*=us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A\r\n").unwrap(); 82 | assert_eq!(rem.len(), 0); 83 | assert_eq!(mtype, "application/x-stuff"); 84 | assert_eq!(params, [("title".into(), "This is ***fun***".into())]); 85 | } 86 | 87 | #[test] 88 | fn encoded_single_no_encoding() { 89 | let (rem, (mtype, params)) = content_type(b"application/x-stuff;\r\n title*='en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A\r\n").unwrap(); 90 | assert_eq!(rem.len(), 0); 91 | assert_eq!(mtype, "application/x-stuff"); 92 | assert_eq!(params, [("title".into(), "This is ***fun***".into())]); 93 | } 94 | 95 | #[test] 96 | fn cd_mixed() { 97 | const CASES : &[&[u8]] = &[b"inline", b"attachment", b"x-whatever"]; 98 | for input in CASES.iter() { 99 | let (rem, (disp, params)) = content_disposition(input).unwrap(); 100 | assert_eq!(rem.len(), 0); 101 | assert_eq!(disp.to_string(), std::str::from_utf8(*input).unwrap()); 102 | assert_eq!(params, []); 103 | } 104 | } 105 | 106 | #[test] 107 | fn cte_base64() { 108 | const CASES : &[&[u8]] = &[b"Base64", b"base64", b" base64 ", b" base64", b"base64 ", b" base64 \r\n "]; 109 | for input in CASES.iter() { 110 | let (rem, parsed) = content_transfer_encoding(input).unwrap(); 111 | assert_eq!(rem.len(), 0); 112 | assert_eq!(parsed, CTE::Base64); 113 | } 114 | 115 | let (rem, parsed) = content_transfer_encoding(b" base64 aoeu").unwrap(); 116 | assert_eq!(rem.len(), 4); 117 | assert_eq!(parsed, CTE::Base64); 118 | } 119 | 120 | #[test] 121 | fn cte_mixed() { 122 | const CASES : &[&[u8]] = &[b"7bit", b"8bit", b"binary", b"base64", b"quoted-printable", b"x-whatever"]; 123 | for input in CASES.iter() { 124 | let (rem, parsed) = content_transfer_encoding(input).unwrap(); 125 | assert_eq!(rem.len(), 0); 126 | assert_eq!(parsed.to_string(), std::str::from_utf8(*input).unwrap()); 127 | } 128 | } 129 | 130 | #[test] 131 | fn encoded_mixed() { 132 | let (rem, (mtype, params)) = content_type(b"application/x-stuff;\r\n title*0*=us-ascii'en'This%20is%20even%20more%20;\r\n title*1*=%2A%2A%2Afun%2A%2A%2A%20;\r\n title*2=\"isn\'t it!\"").unwrap(); 133 | assert_eq!(rem.len(), 0); 134 | assert_eq!(mtype, "application/x-stuff"); 135 | assert_eq!(params, [("title".into(), "This is even more ***fun*** isn't it!".into())]); 136 | } 137 | 138 | // Selected cases from http://test.greenbytes.de/tech/tc2231/ below 139 | 140 | macro_rules! green_tc { 141 | ($tname:ident, $input:expr, $disp:expr, $fname:expr) => ( 142 | #[test] 143 | fn $tname() { 144 | let (rem, (disp, params)) = content_disposition($input).unwrap(); 145 | assert_eq!(rem.len(), 0); 146 | assert_eq!(disp, $disp); 147 | assert_eq!(params, [("filename".into(), $fname.into())]); 148 | } 149 | ) 150 | } 151 | 152 | green_tc!(attfnboth, b"attachment; filename=\"foo-ae.html\"; filename*=UTF-8''foo-%c3%a4.html;", CD::Attachment, "foo-ä.html"); 153 | green_tc!(attfnboth2, b"attachment; filename*=UTF-8''foo-%c3%a4.html; filename=\"foo-ae.html\"", CD::Attachment, "foo-ä.html"); 154 | green_tc!(attfnboth3, b"attachment; filename*0*=ISO-8859-15''euro-sign%3d%a4; filename*=ISO-8859-1''currency-sign%3d%a4", CD::Attachment, "euro-sign=€"); 155 | green_tc!(attfncontenc, b"attachment; filename*0*=UTF-8''foo-%c3%a4; filename*1=\".html\"", CD::Attachment, "foo-ä.html"); 156 | green_tc!(attfncontord, b"attachment; filename*1=\"bar\"; filename*0=\"foo\"", CD::Attachment, "foobar"); 157 | green_tc!(attwithasciifnescapedchar, b"inline; filename=\"f\\oo.html\"", CD::Inline, "foo.html"); 158 | green_tc!(attwithfn2231abspathdisguised, b"attachment; filename*=UTF-8''%5cfoo.html",CD::Attachment, "\\foo.html"); 159 | green_tc!(attwithfn2231utf8, b"attachment; filename*=UTF-8''foo-%c3%a4-%e2%82%ac.html", CD::Attachment, "foo-ä-€.html"); 160 | green_tc!(attwithfntokensq, b"attachment; filename='foo.bar'", CD::Attachment, "'foo.bar'"); 161 | green_tc!(attwithisofnplain, b"attachment; filename=\"foo-\xe4.html\"", CD::Attachment, "foo-\u{fffd}.html"); 162 | green_tc!(attwithquotedsemicolon, b"attachment; filename=\"Here's a semicolon;.html\"", CD::Attachment, "Here's a semicolon;.html"); 163 | green_tc!(inlwithasciifilename, b"inline; filename=\"foo.html\"", CD::Inline, "foo.html"); 164 | 165 | #[test] 166 | #[should_panic] 167 | fn inlonlyquoted() { 168 | content_disposition(b"\"inline\"").unwrap(); 169 | } 170 | 171 | #[test] 172 | #[should_panic] 173 | fn attfnbrokentokenutf() { 174 | let (rem, _) = content_disposition(b"attachment; filename=foo-\xC3\xA4.html").unwrap(); 175 | assert_eq!(rem.len(), 0); 176 | } 177 | -------------------------------------------------------------------------------- /src/pymod.rs: -------------------------------------------------------------------------------- 1 | use std::fmt::Debug; 2 | use std::fs::File; 3 | 4 | use crate::behaviour::{Legacy, Intl}; 5 | use crate::rfc2231::{content_type, content_disposition, content_transfer_encoding}; 6 | use crate::rfc3461::{orcpt_address, dsn_mail_params, DSNMailParams, DSNRet}; 7 | use crate::rfc5321::{Param as ESMTPParam, mail_command, rcpt_command, validate_address, ForwardPath, ReversePath}; 8 | use crate::rfc5322::{Address, Mailbox, Group, from, sender, reply_to, unstructured}; 9 | use crate::headersection::{header_section}; 10 | use crate::xforward::{Param as XFORWARDParam, xforward_params}; 11 | use crate::util::NomResult; 12 | 13 | use memmap::Mmap; 14 | 15 | use pyo3::prelude::*; 16 | use pyo3::{self, Python, PyResult, PyObject, ToPyObject, PyErr}; 17 | use pyo3::types::{PyBytes, PyDict, PyTuple}; 18 | use pyo3::exceptions::PyValueError; 19 | 20 | impl IntoPy for Address { 21 | fn into_py(self, py: Python) -> PyObject { 22 | match self { 23 | Address::Mailbox(m) => m.into_py(py), 24 | Address::Group(g) => g.into_py(py), 25 | } 26 | } 27 | } 28 | 29 | impl IntoPy for Group { 30 | fn into_py(self, py: Python) -> PyObject { 31 | PyTuple::new(py, &[self.dname.to_object(py), self.members.into_py(py)]).to_object(py) 32 | } 33 | } 34 | impl IntoPy for Mailbox { 35 | fn into_py(self, py: Python) -> PyObject { 36 | PyTuple::new(py, &[self.dname.to_object(py), self.address.to_string().to_object(py)]).to_object(py) 37 | } 38 | } 39 | 40 | impl IntoPy for XFORWARDParam { 41 | fn into_py(self, py: Python) -> PyObject { 42 | PyTuple::new(py, &[self.0.to_object(py), 43 | self.1.to_object(py)]).to_object(py) 44 | } 45 | } 46 | 47 | impl IntoPy for ESMTPParam { 48 | fn into_py(self, py: Python) -> PyObject { 49 | PyTuple::new(py, &[self.0.to_object(py), 50 | self.1.as_ref().map(|v| &v.0).to_object(py)]).to_object(py) 51 | } 52 | } 53 | 54 | impl IntoPy for DSNMailParams { 55 | fn into_py(self, py: Python) -> PyObject { 56 | let out = PyDict::new(py); 57 | 58 | out.set_item("envid", self.envid.clone()).unwrap(); 59 | out.set_item("ret", match self.ret { 60 | Some(DSNRet::Hdrs) => Some("HDRS"), 61 | Some(DSNRet::Full) => Some("FULL"), 62 | None => None, 63 | }).unwrap(); 64 | out.to_object(py) 65 | } 66 | } 67 | 68 | impl IntoPy for ForwardPath { 69 | fn into_py(self, py: Python) -> PyObject { 70 | match self { 71 | ForwardPath::Path(p) => p.0.to_string().to_object(py), 72 | ForwardPath::PostMaster(None) => "postmaster".to_object(py), 73 | ForwardPath::PostMaster(Some(d)) => format!("postmaster@{}", d).to_object(py), 74 | } 75 | } 76 | } 77 | 78 | impl IntoPy for ReversePath { 79 | fn into_py(self, py: Python) -> PyObject { 80 | match self { 81 | ReversePath::Path(p) => p.0.to_string().to_object(py), 82 | ReversePath::Null => "".to_object(py), 83 | } 84 | } 85 | } 86 | 87 | fn convert_result (input: NomResult, match_all: bool) -> PyResult { 88 | match input { 89 | Ok((rem, out)) => { 90 | if match_all && !rem.is_empty() { 91 | Err(PyErr::new::("Whole input did not match")) 92 | } else { 93 | Ok(out) 94 | } 95 | } 96 | Err(err) => Err(PyErr::new::(format!("{:?}.", err))), 97 | } 98 | } 99 | 100 | fn header_section_slice(py: Python, input: &[u8]) -> PyResult { 101 | let (rem, out) = header_section(input) 102 | .map_err(|err| PyErr::new::(format!("{:?}.", err)))?; 103 | 104 | let header_end = input.len().checked_sub(rem.len()).unwrap(); 105 | let headers : Vec<_> = out.into_iter().map(|h| { 106 | match h { 107 | Ok((name, value)) => (PyBytes::new(py, name), PyBytes::new(py, value)).to_object(py), 108 | Err(invalid) => (py.None(), PyBytes::new(py, invalid)).to_object(py), 109 | } 110 | }).collect(); 111 | 112 | Ok((headers, header_end).to_object(py)) 113 | } 114 | 115 | #[pymodule] 116 | fn rustyknife(_py: Python, m: &PyModule) -> PyResult<()> { 117 | /// from_(input) 118 | #[pyfn(m, "from_")] 119 | fn py_from(input: &PyBytes) -> PyResult> { 120 | convert_result(from::(input.as_bytes()), true) 121 | } 122 | 123 | /// sender(input) 124 | #[pyfn(m, "sender")] 125 | fn py_sender(input: &PyBytes) -> PyResult
{ 126 | convert_result(sender::(input.as_bytes()), true) 127 | } 128 | 129 | /// reply_to(input) 130 | #[pyfn(m, "reply_to")] 131 | fn py_reply_to(input: &PyBytes) -> PyResult> { 132 | convert_result(reply_to::(input.as_bytes()), true) 133 | } 134 | 135 | /// header_section(input) -> ([headers...], end of headers position) 136 | /// 137 | /// :param input: Input string. 138 | /// :type input: bytes 139 | /// :return: A list of separated header (name, value) tuples with 140 | /// the exact byte position of the end of headers. 141 | /// :rtype: list of byte string tuples 142 | #[pyfn(m, "header_section")] 143 | fn py_header_section(py2: Python, input: &PyBytes) -> PyResult { 144 | header_section_slice(py2, input.as_bytes()) 145 | } 146 | 147 | /// header_section_file(fname) -> ([headers...], end of headers position) 148 | /// 149 | /// :param fname: File name to read. 150 | /// :type fname: str 151 | /// :return: Same as :meth:`header_section` 152 | #[pyfn(m, "header_section_file")] 153 | fn py_header_section_file(py2: Python, fname: &str) -> PyResult { 154 | let file = File::open(fname)?; 155 | let fmap = unsafe { Mmap::map(&file)? }; 156 | 157 | header_section_slice(py2, &fmap) 158 | } 159 | 160 | /// xforward_params(input) 161 | #[pyfn(m, "xforward_params")] 162 | fn py_xforward_params(input: &PyBytes) -> PyResult> { 163 | convert_result(xforward_params(input.as_bytes()), true) 164 | } 165 | 166 | /// orcpt_address(input) 167 | #[pyfn(m, "orcpt_address")] 168 | fn py_orcpt_address(input: &str) -> PyResult<(String, String)> { 169 | convert_result(orcpt_address(input.as_bytes()).map(|(rem, a)| (rem, (a.0.into(), a.1.into()))), true) 170 | } 171 | 172 | /// dsn_mail_params(input) 173 | #[pyfn(m, "dsn_mail_params")] 174 | fn py_dsn_mail_params(py2: Python, input: Vec<(&str, Option<&str>)>) -> PyResult<(PyObject, PyObject)> { 175 | dsn_mail_params(&input).map(|(parsed, rem)| (parsed.into_py(py2), rem.to_object(py2))).map_err(PyErr::new::) 176 | } 177 | 178 | /// mail_command(input) 179 | /// 180 | /// :param input: Full SMTP MAIL command 181 | /// 182 | /// b'MAIL FROM: BODY=7BIT\\\\r\\\\n' 183 | /// :type input: bytes 184 | /// :return: (address, [(param, param_value), ...]) 185 | #[pyfn(m, "mail_command")] 186 | pub fn py_mail_command(input: &PyBytes) -> PyResult<(ReversePath, Vec)> 187 | { 188 | convert_result(mail_command::(input.as_bytes()), true) 189 | } 190 | 191 | /// rcpt_command(input) 192 | /// 193 | /// :param input: Full SMTP RCPT command 194 | /// 195 | /// b'RCPT TO: ORCPT=rfc822;user@example.org\\\\r\\\\n' 196 | /// :type input: bytes 197 | /// :return: (address, [(param, param_value), ...]) 198 | #[pyfn(m, "rcpt_command")] 199 | pub fn py_rcpt_command(input: &PyBytes) -> PyResult<(ForwardPath, Vec)> 200 | { 201 | convert_result(rcpt_command::(input.as_bytes()), true) 202 | } 203 | 204 | /// validate_address(address) 205 | /// 206 | /// :param address: Non-empty address without <> brackets. 207 | /// :type address: str 208 | /// :rtype: bool 209 | #[pyfn(m, "validate_address")] 210 | pub fn py_validate_address(input: &str) -> bool 211 | { 212 | validate_address::(input.as_bytes()) 213 | } 214 | 215 | /// unstructured(input) 216 | /// 217 | /// Decode an unstructured email header. 218 | /// 219 | /// Useful for converting subject lines. 220 | /// 221 | /// :param input: Input string 222 | /// :type input: bytes 223 | /// :return: Decoded header 224 | /// :rtype: str 225 | #[pyfn(m, "unstructured")] 226 | fn py_unstructured(input: &PyBytes) -> PyResult { 227 | convert_result(unstructured::(input.as_bytes()), true) 228 | } 229 | 230 | /// content_type(input, all=False) 231 | #[pyfn(m, "content_type", input, all=false)] 232 | fn py_content_type(input: &PyBytes, all: bool) -> PyResult<(String, Vec<(String, String)>)> { 233 | convert_result(content_type(input.as_bytes()), all) 234 | } 235 | 236 | /// content_disposition(input, all=False) 237 | #[pyfn(m, "content_disposition", input, all=false)] 238 | fn py_content_disposition(input: &PyBytes, all: bool) -> PyResult<(String, Vec<(String, String)>)> { 239 | convert_result(content_disposition(input.as_bytes()), all).map(|(cd, params)| (cd.to_string().to_lowercase(), params)) 240 | } 241 | 242 | /// content_transfer_encoding(input, all=False) 243 | /// 244 | /// Parse a MIME Content-Transfer-Encoding header. 245 | /// 246 | /// Standard encodings such as 7bit, 8bit are accepted. Extended 247 | /// encodings starting with a 'x-' prefix are also accepted. 248 | /// 249 | /// :param input: Input string. 250 | /// :type input: bytes 251 | /// :return: Validated Content-Transfer-Encoding 252 | /// :rtype: str 253 | /// 254 | #[pyfn(m, "content_transfer_encoding", input, all=false)] 255 | fn py_content_transfer_encoding(input: &PyBytes, all: bool) -> PyResult { 256 | convert_result(content_transfer_encoding(input.as_bytes()), all).map(|cte| cte.to_string().to_lowercase()) 257 | } 258 | 259 | Ok(()) 260 | } 261 | -------------------------------------------------------------------------------- /src/types.rs: -------------------------------------------------------------------------------- 1 | //! Types shared by SMTP and Internet Message Format 2 | //! 3 | //! IMF allows comments and folding spaces in many places but is very 4 | //! close to the SMTP syntax. 5 | //! 6 | //! If glaring incompatibilites are found in practice, the [crate::rfc5321] and 7 | //! [crate::rfc5322] modules will get their own variants of these types. 8 | //! 9 | //! Structs such as [`types::Domain`] and [`types::QuotedString`] are 10 | //! newtypes around [`String`] to make sure they can only be constructed 11 | //! from valid values. 12 | use std::fmt::{self, Display}; 13 | 14 | use std::net::IpAddr; 15 | 16 | #[cfg(feature = "serde")] 17 | use serde::{Serialize, Deserialize}; 18 | 19 | use crate::behaviour::Intl; 20 | use crate::rfc5321 as smtp; 21 | use crate::rfc5322 as imf; 22 | use crate::util::*; 23 | 24 | /// A domain name such as used by DNS. 25 | #[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] 26 | pub struct Domain(pub(crate) String); 27 | string_newtype!(Domain); 28 | impl Domain { 29 | nom_from_smtp!(smtp::domain::); 30 | nom_from_imf!(imf::_domain::); 31 | } 32 | 33 | /// The local part of an address preceding the `"@"` in an email address. 34 | #[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] 35 | pub enum LocalPart { 36 | /// Simple local part with no spaces. 37 | DotAtom(DotAtom), 38 | /// Local part that may contain spaces and special characters. 39 | Quoted(QuotedString), 40 | } 41 | impl LocalPart { 42 | nom_from_smtp!(smtp::local_part::); 43 | nom_from_imf!(imf::local_part::); 44 | 45 | /// Unquote this local part if it is quoted needlessly. 46 | /// 47 | /// This is useful for normalization purposes. 48 | pub fn smtp_try_unquote(&mut self) { 49 | match self { 50 | LocalPart::Quoted(qs) => { 51 | if let Ok((b"", da)) = smtp::dot_string::(qs.0.as_bytes()) { 52 | *self = LocalPart::DotAtom(da); 53 | } 54 | } 55 | LocalPart::DotAtom(_) => (), 56 | } 57 | } 58 | } 59 | 60 | impl From for LocalPart { 61 | fn from(value: QuotedString) -> LocalPart { 62 | LocalPart::Quoted(value) 63 | } 64 | } 65 | 66 | impl From for LocalPart { 67 | fn from(value: DotAtom) -> LocalPart { 68 | LocalPart::DotAtom(value) 69 | } 70 | } 71 | 72 | /// A quoted string that may contain spaces. 73 | /// 74 | /// This is used in places such as SMTP local parts and IMF display 75 | /// names. 76 | #[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] 77 | pub struct QuotedString(pub(crate) String); 78 | string_newtype!(QuotedString); 79 | 80 | impl QuotedString { 81 | /// Returns this string enclosed in double quotes. 82 | /// 83 | /// Double quote and backslash characters are escaped with a 84 | /// backslash. 85 | /// 86 | /// No attempt is made to reencode values outside the ASCII range. 87 | pub fn quoted(&self) -> String { 88 | let mut out = String::with_capacity(self.len()+2); 89 | out.push('"'); 90 | 91 | for c in self.chars() { 92 | match c { 93 | '"' | '\\' => { 94 | out.push('\\'); 95 | out.push(c); 96 | } 97 | _ => out.push(c) 98 | } 99 | } 100 | out.push('"'); 101 | 102 | out 103 | } 104 | 105 | nom_from_smtp!(smtp::quoted_string::); 106 | nom_from_imf!(imf::quoted_string::); 107 | } 108 | 109 | /// A string consisting of atoms separated by periods. 110 | /// 111 | /// An atom is a string that may not contain spaces or some special 112 | /// characters such as `':'`. 113 | /// 114 | /// See [RFC 5322] for the full syntax. 115 | /// 116 | /// [RFC 5322]: https://tools.ietf.org/html/rfc5322#section-3.2.3 117 | #[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] 118 | pub struct DotAtom(pub(crate) String); 119 | string_newtype!(DotAtom); 120 | 121 | impl DotAtom { 122 | nom_from_smtp!(smtp::dot_string::); 123 | nom_from_imf!(imf::dot_atom::); 124 | } 125 | 126 | impl Display for LocalPart { 127 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 128 | match self { 129 | LocalPart::DotAtom(a) => write!(f, "{}", a), 130 | LocalPart::Quoted(q) => write!(f, "{}", q.quoted()), 131 | } 132 | } 133 | } 134 | 135 | /// The domain part of an address following the `"@"` in an email address. 136 | #[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] 137 | pub enum DomainPart { 138 | /// A DNS domain name such as `"example.org"`. 139 | Domain(Domain), 140 | /// A network address literal such as `"[192.0.2.1]"`. 141 | Address(AddressLiteral), 142 | } 143 | 144 | impl DomainPart { 145 | nom_from_smtp!(smtp::_domain_part::); 146 | nom_from_imf!(imf::domain::); 147 | } 148 | 149 | impl From for DomainPart { 150 | fn from(value: Domain) -> DomainPart { 151 | DomainPart::Domain(value) 152 | } 153 | } 154 | 155 | impl From for DomainPart { 156 | fn from(value: AddressLiteral) -> DomainPart { 157 | DomainPart::Address(value) 158 | } 159 | } 160 | 161 | impl Display for DomainPart { 162 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 163 | match self { 164 | DomainPart::Domain(d) => write!(f, "{}", d), 165 | DomainPart::Address(a) => write!(f, "{}", a), 166 | } 167 | } 168 | } 169 | 170 | /// A network address literal. 171 | #[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] 172 | pub enum AddressLiteral { 173 | /// An IPv4 or IPv6 address literal. 174 | /// # Examples 175 | /// ``` 176 | /// use std::convert::TryFrom; 177 | /// use std::net::{Ipv4Addr, Ipv6Addr}; 178 | /// use rustyknife::types::AddressLiteral; 179 | /// 180 | /// let ipv4 = AddressLiteral::from_smtp(b"[192.0.2.1]".as_ref()).unwrap(); 181 | /// let ipv6 = AddressLiteral::from_smtp(b"[IPv6:2001:db8::1]".as_ref()).unwrap(); 182 | /// 183 | /// assert_eq!(ipv4, AddressLiteral::IP("192.0.2.1".parse().unwrap())); 184 | /// assert_eq!(ipv6, AddressLiteral::IP("2001:db8::1".parse().unwrap())); 185 | /// ``` 186 | IP(IpAddr), 187 | /// An address literal in the form tag:value. 188 | /// # Examples 189 | /// ``` 190 | /// use std::convert::TryFrom; 191 | /// use rustyknife::types::AddressLiteral; 192 | /// 193 | /// let lit = AddressLiteral::from_smtp(b"[x400:cn=bob,dc=example,dc=org]".as_ref()).unwrap(); 194 | /// assert_eq!(lit, AddressLiteral::Tagged("x400".into(), "cn=bob,dc=example,dc=org".into())); 195 | /// ``` 196 | Tagged(String, String), 197 | /// A free form address literal. Generated only by the [crate::rfc5322] module. 198 | FreeForm(String), 199 | } 200 | 201 | impl AddressLiteral { 202 | /// Try to upgrade a [`AddressLiteral::FreeForm`] to the more formal subtypes. 203 | /// # Examples 204 | /// ``` 205 | /// use rustyknife::types::AddressLiteral; 206 | /// 207 | /// let valid = AddressLiteral::FreeForm("192.0.2.1".into()); 208 | /// let invalid = AddressLiteral::FreeForm("somewhere".into()); 209 | /// 210 | /// assert_eq!(valid.upgrade(), Ok(AddressLiteral::IP("192.0.2.1".parse().unwrap()))); 211 | /// assert_eq!(invalid.upgrade(), Err(())); 212 | /// ``` 213 | pub fn upgrade(&self) -> Result { 214 | if let AddressLiteral::FreeForm(s) = self { 215 | let (rem, parsed) = smtp::_inner_address_literal(s.as_bytes()).map_err(|_| ())?; 216 | 217 | if rem.is_empty() { 218 | Ok(parsed) 219 | } else { 220 | Err(()) 221 | } 222 | } else { 223 | Err(()) 224 | } 225 | } 226 | 227 | nom_from_smtp!(smtp::address_literal); 228 | nom_from_imf!(imf::domain_literal::); 229 | } 230 | 231 | 232 | impl Display for AddressLiteral { 233 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 234 | match self { 235 | AddressLiteral::IP(ip) => match ip { 236 | IpAddr::V4(ipv4) => write!(f, "[{}]", ipv4), 237 | IpAddr::V6(ipv6) => write!(f, "[IPv6:{}]", ipv6), 238 | }, 239 | AddressLiteral::Tagged(tag, value) => write!(f, "[{}:{}]", tag, value), 240 | AddressLiteral::FreeForm(value) => write!(f, "[{}]", value), 241 | } 242 | } 243 | } 244 | 245 | /// A valid email address. 246 | #[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] 247 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] 248 | #[cfg_attr(feature = "serde", serde(try_from="&str", into="String"))] 249 | pub struct Mailbox(pub(crate) LocalPart, pub(crate) DomainPart); 250 | 251 | impl Mailbox { 252 | /// Return the local part to the left of the "@". 253 | pub fn local_part(&self) -> &LocalPart { 254 | &self.0 255 | } 256 | 257 | /// Return the domain part to the right of the "@". 258 | pub fn domain_part(&self) -> &DomainPart { 259 | &self.1 260 | } 261 | 262 | /// Split the mailbox apart. 263 | pub fn into_parts(self) -> (LocalPart, DomainPart) { 264 | (self.0, self.1) 265 | } 266 | 267 | /// Build a mailbox from its parts. 268 | pub fn from_parts(local: LocalPart, domain: DomainPart) -> Self { 269 | Mailbox(local, domain) 270 | } 271 | 272 | /// Unquote this address' local part if it is quoted needlessly. 273 | /// 274 | /// This is useful for normalization purposes. 275 | pub fn smtp_try_unquote(&mut self) { 276 | self.0.smtp_try_unquote() 277 | } 278 | 279 | nom_from_smtp!(smtp::mailbox::); 280 | nom_from_imf!(imf::addr_spec::); 281 | } 282 | 283 | // FIXME: is type unification a good thing ? 284 | nom_fromstr!(Mailbox, smtp::mailbox::); 285 | 286 | impl Display for Mailbox { 287 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 288 | write!(f, "{}@{}", self.0, self.1) 289 | } 290 | } 291 | 292 | impl From for String { 293 | fn from(mailbox: Mailbox) -> String { 294 | mailbox.to_string() 295 | } 296 | } 297 | -------------------------------------------------------------------------------- /src/rfc2231.rs: -------------------------------------------------------------------------------- 1 | //! [Encoded MIME parameters] 2 | //! 3 | //! Implements [RFC 2045] syntax extended with RFC 2231 4 | //! 5 | //! [Encoded MIME parameters]: https://tools.ietf.org/html/rfc2231 6 | //! [RFC 2045]: https://tools.ietf.org/html/rfc2045 7 | 8 | 9 | use std::borrow::Cow; 10 | use std::fmt::{self, Display}; 11 | use std::str; 12 | use std::collections::HashMap; 13 | 14 | use charset::decode_ascii; 15 | 16 | use encoding_rs::Encoding; 17 | use encoding_rs::UTF_8; // TODO: was ASCII 18 | 19 | use nom::branch::alt; 20 | use nom::bytes::complete::{tag, tag_no_case, take_while1, take_while_m_n}; 21 | use nom::character::is_digit; 22 | use nom::combinator::{map, opt, recognize, verify}; 23 | use nom::multi::many0; 24 | use nom::sequence::{delimited, pair, preceded, separated_pair, terminated, tuple}; 25 | 26 | use crate::util::*; 27 | use crate::rfc3461::hexpair; 28 | use crate::rfc5234::crlf; 29 | use crate::rfc5322::{ofws, quoted_string}; 30 | 31 | #[derive(Debug)] 32 | struct Parameter<'a> { 33 | name: Name<'a>, 34 | value: Value<'a>, 35 | } 36 | 37 | #[derive(Debug)] 38 | struct Name<'a> { 39 | section: Option, 40 | name: &'a str, 41 | } 42 | 43 | #[derive(Debug)] 44 | enum Value<'a> { 45 | Regular(Cow<'a, str>), 46 | Extended(ExtendedValue<'a>), 47 | } 48 | 49 | #[derive(Debug)] 50 | enum ExtendedValue<'a> { 51 | Initial { encoding: Option<&'a [u8]>, language: Option<&'a [u8]>, value: Vec }, 52 | Other(Vec), 53 | } 54 | 55 | fn _equals(input: &[u8]) -> NomResult<()> { 56 | map(tuple((ofws, tag("="), ofws)), |_| ())(input) 57 | } 58 | 59 | fn parameter(input: &[u8]) -> NomResult { 60 | alt((regular_parameter, extended_parameter))(input) 61 | } 62 | 63 | fn regular_parameter(input: &[u8]) -> NomResult { 64 | map(separated_pair(regular_parameter_name, _equals, value), 65 | |(name, value)| Parameter{name, value: Value::Regular(value)})(input) 66 | } 67 | 68 | fn regular_parameter_name(input: &[u8]) -> NomResult { 69 | map(pair(attribute, opt(section)), 70 | |(name, section)| Name{name: std::str::from_utf8(name).unwrap(), section} 71 | )(input) 72 | } 73 | 74 | fn token(input: &[u8]) -> NomResult<&str> { 75 | map(take_while1(|c| (33..=126).contains(&c) && !b"()<>@,;:\\\"/[]?=".contains(&c)), 76 | |t| std::str::from_utf8(t).unwrap())(input) 77 | } 78 | 79 | fn is_attribute_char(c: u8) -> bool { 80 | (33..=126).contains(&c) && !b"*'%()<>@,;:\\\"/[]?=".contains(&c) 81 | } 82 | 83 | fn attribute_char(input: &[u8]) -> NomResult { 84 | take1_filter(is_attribute_char)(input) 85 | } 86 | 87 | fn attribute(input: &[u8]) -> NomResult<&[u8]> { 88 | take_while1(is_attribute_char)(input) 89 | } 90 | 91 | fn section(input: &[u8]) -> NomResult { 92 | alt((initial_section, other_sections))(input) 93 | } 94 | 95 | fn initial_section(input: &[u8]) -> NomResult { 96 | map(tag("*0"), |_| 0)(input) 97 | } 98 | 99 | fn other_sections(input: &[u8]) -> NomResult { 100 | map(preceded(tag("*"), verify(take_while_m_n(1, 8, is_digit), |x: &[u8]| x[0] != b'0')), 101 | |s| str::from_utf8(s).unwrap().parse().unwrap())(input) 102 | } 103 | 104 | fn extended_parameter(input: &[u8]) -> NomResult { 105 | alt(( 106 | map(separated_pair(extended_initial_name, 107 | _equals, 108 | extended_initial_value), 109 | |(name, value)| Parameter{name, value: Value::Extended(value)}), 110 | 111 | map(separated_pair(extended_other_names, 112 | _equals, 113 | extended_other_values), 114 | |(name, value)| Parameter{name, value: Value::Extended(ExtendedValue::Other(value))}), 115 | ))(input) 116 | } 117 | 118 | fn extended_initial_name(input: &[u8]) -> NomResult { 119 | map(terminated(pair(attribute, opt(initial_section)), tag("*")), 120 | |(name, section)| Name{name: str::from_utf8(&name).unwrap(), section})(input) 121 | } 122 | 123 | fn extended_other_names(input: &[u8]) -> NomResult { 124 | map(terminated(pair(attribute, other_sections), tag("*")), 125 | |(name, section)| Name{name: str::from_utf8(&name).unwrap(), section: Some(section)})(input) 126 | } 127 | 128 | fn extended_initial_value(input: &[u8]) -> NomResult { 129 | map(tuple((terminated(opt(attribute), tag("'")), 130 | terminated(opt(attribute), tag("'")), 131 | extended_other_values)), 132 | |(encoding, language, value)| ExtendedValue::Initial{encoding, language, value})(input) 133 | } 134 | 135 | fn ext_octet(input: &[u8]) -> NomResult { 136 | preceded(tag("%"), hexpair)(input) 137 | } 138 | 139 | fn extended_other_values(input: &[u8]) -> NomResult> { 140 | many0(alt((ext_octet, attribute_char)))(input) 141 | } 142 | 143 | fn value(input: &[u8]) -> NomResult> { 144 | alt((map(token, Cow::from), 145 | map(quoted_string::, |qs| Cow::from(qs.0))))(input) 146 | } 147 | 148 | fn _mime_type(input: &[u8]) -> NomResult<&[u8]> { 149 | recognize(tuple((token, tag("/"), token)))(input) 150 | } 151 | 152 | fn _parameter_list(input: &[u8]) -> NomResult> { 153 | terminated(many0(preceded(pair(tag(";"), ofws), parameter)), 154 | pair(opt(tag(";")), opt(crlf)))(input) 155 | } 156 | 157 | #[derive(Debug)] 158 | enum Segment<'a> { 159 | Encoded(Vec), 160 | Decoded(Cow<'a, str>), 161 | } 162 | 163 | fn decode_segments(mut input: Vec<(u32, Segment)>, encoding: &'static Encoding) -> String { 164 | input.sort_by(|a, b| a.0.cmp(&b.0)); 165 | let mut out = String::new(); 166 | let mut encoded = Vec::new(); 167 | 168 | let decode = |bytes: &mut Vec<_>, out: &mut String| { 169 | out.push_str(&encoding.decode_without_bom_handling(bytes).0); 170 | bytes.clear(); 171 | }; 172 | 173 | // Clump encoded segments together before decoding. Prevents partial UTF-8 sequences or similar with other encodings. 174 | for (_, segment) in input { 175 | match segment { 176 | Segment::Encoded(mut bytes) => encoded.append(&mut bytes), 177 | Segment::Decoded(s) => { decode(&mut encoded, &mut out); out.push_str(&s) } 178 | } 179 | } 180 | decode(&mut encoded, &mut out); 181 | 182 | out 183 | } 184 | 185 | fn decode_parameter_list(input: Vec) -> Vec<(String, String)> { 186 | let mut simple = HashMap::::new(); 187 | let mut simple_encoded = HashMap::::new(); 188 | let mut composite = HashMap::>::new(); 189 | let mut composite_encoding = HashMap::new(); 190 | 191 | for Parameter{name, value} in input { 192 | let name_norm = name.name.to_lowercase(); 193 | 194 | match name.section { 195 | None => { 196 | match value { 197 | Value::Regular(v) => { simple.insert(name_norm, v.into()); }, 198 | Value::Extended(ExtendedValue::Initial{value, encoding: encoding_name, ..}) => { 199 | let codec = match encoding_name { 200 | Some(encoding_name) => Encoding::for_label(decode_ascii(encoding_name).as_bytes()).unwrap_or(UTF_8), 201 | None => UTF_8, 202 | }; 203 | simple_encoded.insert(name_norm, codec.decode_without_bom_handling(value.as_slice()).0.to_string()); // TODO: eliminate to_string 204 | } 205 | Value::Extended(ExtendedValue::Other(..)) => unreachable!(), 206 | } 207 | }, 208 | Some(section) => { 209 | let ent = composite.entry(name_norm.clone()).or_default(); 210 | 211 | match value { 212 | Value::Regular(v) => ent.push((section, Segment::Decoded(v))), 213 | Value::Extended(ExtendedValue::Initial{value, encoding: encoding_name, ..}) => { 214 | if let Some(encoding_name) = encoding_name { 215 | if let Some(codec) = Encoding::for_label(decode_ascii(encoding_name).as_bytes()) { 216 | composite_encoding.insert(name_norm, codec); 217 | } 218 | } 219 | ent.push((section, Segment::Encoded(value.to_vec()))) 220 | } 221 | Value::Extended(ExtendedValue::Other(v)) => ent.push((section, Segment::Encoded(v))), 222 | } 223 | } 224 | } 225 | } 226 | 227 | let mut composite_out = Vec::new(); 228 | for (name, segments) in composite { 229 | let codec = composite_encoding.get(&name).cloned().unwrap_or(UTF_8); 230 | composite_out.push((name, decode_segments(segments, codec))); 231 | } 232 | 233 | for (name, value) in simple_encoded.into_iter().chain(composite_out.into_iter()) { 234 | simple.insert(name, value); 235 | } 236 | 237 | simple.into_iter().collect() 238 | } 239 | 240 | /// Parse a MIME `"Content-Type"` header. 241 | /// 242 | /// Returns a tuple of the MIME type and parameters. 243 | pub fn content_type(input: &[u8]) -> NomResult<(String, Vec<(String, String)>)> { 244 | map(pair(delimited(ofws, _mime_type, ofws), 245 | _parameter_list), 246 | |(mt, p)| (decode_ascii(mt).to_lowercase(), decode_parameter_list(p)))(input) 247 | } 248 | 249 | fn _x_token(input: &[u8]) -> NomResult<&str> { 250 | preceded(tag_no_case("x-"), token)(input) 251 | } 252 | 253 | /// Value from a MIME `"Content-Disposition"` header. 254 | #[derive(Debug, PartialEq)] 255 | pub enum ContentDisposition { 256 | /// "inline" 257 | Inline, 258 | /// "attachment" 259 | Attachment, 260 | /// Value prefixed with "X-". The prefix is not stored in the 261 | /// string. 262 | Extended(String), 263 | /// Any syntaxically valid token that is not any known disposition. 264 | Token(String), 265 | } 266 | 267 | impl Display for ContentDisposition { 268 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 269 | match self { 270 | ContentDisposition::Inline => write!(f, "inline"), 271 | ContentDisposition::Attachment => write!(f, "attachment"), 272 | ContentDisposition::Extended(s) => write!(f, "x-{}", s), 273 | ContentDisposition::Token(t) => write!(f, "{}", t), 274 | } 275 | } 276 | } 277 | 278 | fn _disposition(input: &[u8]) -> NomResult { 279 | alt(( 280 | map(tag_no_case("inline"), |_| ContentDisposition::Inline), 281 | map(tag_no_case("attachment"), |_| ContentDisposition::Attachment), 282 | map(_x_token, |x| ContentDisposition::Extended(x.into())), 283 | map(token, |t| ContentDisposition::Token(t.into())) 284 | ))(input) 285 | } 286 | 287 | /// Parse a MIME `"Content-Disposition"` header. 288 | /// 289 | /// Returns a tuple of [`ContentDisposition`] and parameters. 290 | pub fn content_disposition(input: &[u8]) -> NomResult<(ContentDisposition, Vec<(String, String)>)> { 291 | map(pair(delimited(ofws, _disposition, ofws), 292 | _parameter_list), 293 | |(disp, p)| (disp, decode_parameter_list(p)))(input) 294 | } 295 | 296 | /// Value from a MIME `"Content-Transfer-Encoding"` header. 297 | #[derive(Debug, PartialEq)] 298 | pub enum ContentTransferEncoding { 299 | /// "7bit" 300 | SevenBit, 301 | /// "8bit" 302 | EightBit, 303 | /// "binary" 304 | Binary, 305 | /// "base64" 306 | Base64, 307 | /// "quoted-printable" 308 | QuotedPrintable, 309 | /// Value prefixed with "X-". The prefix is not stored in the 310 | /// string. 311 | Extended(String), 312 | /// Any syntaxically valid token that is not any known encoding. 313 | Token(String), 314 | } 315 | 316 | impl Display for ContentTransferEncoding { 317 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 318 | match self { 319 | CTE::SevenBit => write!(f, "7bit"), 320 | CTE::EightBit => write!(f, "8bit"), 321 | CTE::Binary => write!(f, "binary"), 322 | CTE::Base64 => write!(f, "base64"), 323 | CTE::QuotedPrintable => write!(f, "quoted-printable"), 324 | CTE::Extended(s) => write!(f, "x-{}", s), 325 | CTE::Token(t) => write!(f, "{}", t), 326 | } 327 | } 328 | } 329 | 330 | use self::ContentTransferEncoding as CTE; 331 | 332 | /// Parse a MIME `"Content-Transfer-Encoding"` header. 333 | /// 334 | /// Returns a [`ContentTransferEncoding`]. 335 | pub fn content_transfer_encoding(input: &[u8]) -> NomResult { 336 | delimited(ofws, alt(( 337 | map(tag_no_case("7bit"), |_| CTE::SevenBit), 338 | map(tag_no_case("8bit"), |_| CTE::EightBit), 339 | map(tag_no_case("binary"), |_| CTE::Binary), 340 | map(tag_no_case("base64"), |_| CTE::Base64), 341 | map(tag_no_case("quoted-printable"), |_| CTE::QuotedPrintable), 342 | map(_x_token, |x| CTE::Extended(x.into())), 343 | map(token, |t| CTE::Token(t.into())) 344 | )), ofws)(input) 345 | } 346 | -------------------------------------------------------------------------------- /src/rfc5322.rs: -------------------------------------------------------------------------------- 1 | //! Parsers for [Internet Message Format] messages. 2 | //! 3 | //! Comments are ignored. [RFC 2047] decoding is applied where appropriate. 4 | //! 5 | //! [Internet Message Format]: https://tools.ietf.org/html/rfc5322 6 | //! [RFC 2047]: https://tools.ietf.org/html/rfc2047 7 | 8 | use std::borrow::Cow; 9 | use std::str; 10 | use std::mem; 11 | 12 | use nom::branch::alt; 13 | use nom::bytes::complete::{tag, take}; 14 | use nom::combinator::{map, map_opt, opt, recognize}; 15 | use nom::multi::{fold_many0, many0, many1}; 16 | use nom::sequence::{delimited, pair, preceded, separated_pair, terminated}; 17 | 18 | use crate::behaviour::*; 19 | use crate::rfc2047::encoded_word; 20 | use crate::rfc5234::*; 21 | use crate::types::{self, *}; 22 | use crate::util::*; 23 | 24 | #[allow(missing_docs)] // Mostly internal 25 | pub trait UTF8Policy { 26 | fn vchar(input: &[u8]) -> NomResult; 27 | fn ctext(input: &[u8]) -> NomResult; 28 | fn atext(input: &[u8]) -> NomResult; 29 | fn qtext(input: &[u8]) -> NomResult; 30 | fn dtext(input: &[u8]) -> NomResult; 31 | } 32 | 33 | impl UTF8Policy for Legacy { 34 | fn vchar(input: &[u8]) -> NomResult { 35 | crate::rfc5234::vchar(input) 36 | } 37 | 38 | fn ctext(input: &[u8]) -> NomResult { 39 | map(take1_filter(|c| match c {33..=39 | 42..=91 | 93..=126 => true, _ => false}), char::from)(input) 40 | } 41 | 42 | fn atext(input: &[u8]) -> NomResult { 43 | map(take1_filter(|c| b"!#$%&'*+-/=?^_`{|}~".contains(&c) || (b'0'..=b'9').contains(&c) 44 | || (b'A'..=b'Z').contains(&c) || (b'a'..=b'z').contains(&c)), char::from)(input) 45 | } 46 | 47 | fn qtext(input: &[u8]) -> NomResult { 48 | alt((map(take1_filter(|c| match c {33 | 35..=91 | 93..=126 => true, _ => false}), char::from), 49 | _8bit_char))(input) 50 | } 51 | 52 | fn dtext(input: &[u8]) -> NomResult { 53 | map(take1_filter(|c| match c {33..=90 | 94..=126 => true, _ => false}), char::from)(input) 54 | } 55 | } 56 | 57 | impl UTF8Policy for Intl { 58 | fn vchar(input: &[u8]) -> NomResult { 59 | alt((Legacy::vchar, utf8_non_ascii))(input) 60 | } 61 | 62 | fn ctext(input: &[u8]) -> NomResult { 63 | alt((Legacy::ctext, utf8_non_ascii))(input) 64 | } 65 | 66 | fn atext(input: &[u8]) -> NomResult { 67 | alt((Legacy::atext, utf8_non_ascii))(input) 68 | } 69 | 70 | fn qtext(input: &[u8]) -> NomResult { 71 | alt((map(take1_filter(|c| match c {33 | 35..=91 | 93..=126 => true, _ => false}), char::from), 72 | utf8_non_ascii, 73 | _8bit_char))(input) 74 | } 75 | 76 | fn dtext(input: &[u8]) -> NomResult { 77 | alt((Legacy::dtext, utf8_non_ascii))(input) 78 | } 79 | } 80 | 81 | fn quoted_pair(input: &[u8]) -> NomResult { 82 | preceded(tag("\\"), alt((P::vchar, map(wsp, char::from))))(input) 83 | } 84 | 85 | #[derive(Clone, Debug)] 86 | enum CommentContent<'a> { 87 | Text(Cow<'a, str>), 88 | Comment(Vec>), 89 | QP(char), 90 | } 91 | 92 | fn ccontent(input: &[u8]) -> NomResult { 93 | alt((alt((map(recognize_many1(P::ctext), |ct| CommentContent::Text(str::from_utf8(ct).unwrap().into())), 94 | map(quoted_pair::

, CommentContent::QP))), 95 | map(comment::

, CommentContent::Comment)))(input) 96 | } 97 | 98 | fn fws(input: &[u8]) -> NomResult> { 99 | //CRLF is "semantically invisible" 100 | map(pair(opt(terminated(recognize_many0(wsp), crlf)), 101 | recognize_many1(wsp)), 102 | |(a, b)| { 103 | match a { 104 | Some(a) => { 105 | let mut out = String::from(str::from_utf8(a).unwrap()); 106 | out.push_str(str::from_utf8(b).unwrap()); 107 | Cow::from(out) 108 | }, 109 | None => Cow::from(str::from_utf8(b).unwrap()) 110 | } 111 | })(input) 112 | } 113 | 114 | pub(crate) fn ofws(input: &[u8]) -> NomResult> { 115 | map(opt(fws), |i| i.unwrap_or_else(|| Cow::from("")))(input) 116 | } 117 | 118 | fn _concat_comment<'a, I: IntoIterator>>(comments: I) -> Vec> { 119 | let mut out = Vec::new(); 120 | let mut acc_text = String::new(); 121 | 122 | let push_text = |bytes: &mut String, out: &mut Vec| { 123 | if !bytes.is_empty() { 124 | out.push(CommentContent::Text(mem::replace(bytes, String::new()).into())) 125 | } 126 | }; 127 | 128 | for comment in comments.into_iter() { 129 | match comment { 130 | CommentContent::Text(text) => acc_text.push_str(&text), 131 | CommentContent::QP(qp) => acc_text.push(qp), 132 | _ => { push_text(&mut acc_text, &mut out); out.push(comment) } 133 | } 134 | } 135 | push_text(&mut acc_text, &mut out); 136 | 137 | out 138 | } 139 | 140 | fn comment(input: &[u8]) -> NomResult> { 141 | map(delimited(tag("("), 142 | pair(fold_many0(pair(ofws, ccontent::

), Vec::new(), |mut acc, (fws, cc)| { 143 | acc.push(CommentContent::Text(fws)); 144 | acc.push(cc); 145 | acc 146 | }), ofws), 147 | tag(")")), 148 | |(a, b)| _concat_comment(a.into_iter().chain(std::iter::once(CommentContent::Text(b)))))(input) 149 | } 150 | 151 | fn cfws(input: &[u8]) -> NomResult<&[u8]> { 152 | alt((recognize(pair(many1(pair(ofws, comment::

)), ofws)), recognize(fws)))(input) 153 | } 154 | 155 | #[cfg(feature = "quoted-string-rfc2047")] 156 | fn qcontent(input: &[u8]) -> NomResult { 157 | alt((map(encoded_word, QContent::EncodedWord), 158 | map(recognize_many1(P::qtext), |q| QContent::Literal(String::from_utf8_lossy(q))), 159 | map(quoted_pair::

, QContent::QP)) 160 | )(input) 161 | } 162 | 163 | #[cfg(not(feature = "quoted-string-rfc2047"))] 164 | fn qcontent(input: &[u8]) -> NomResult { 165 | alt((map(recognize_many1(P::qtext), |q| QContent::Literal(String::from_utf8_lossy(q))), 166 | map(quoted_pair::

, QContent::QP)) 167 | )(input) 168 | } 169 | 170 | // quoted-string not surrounded by CFWS 171 | fn _inner_quoted_string(input: &[u8]) -> NomResult> { 172 | map(delimited(tag("\""), 173 | pair(many0(pair(opt(fws), qcontent::

)), opt(fws)), 174 | tag("\"")), 175 | |(a, b)| { 176 | let mut out = Vec::with_capacity(a.len()*2+1); 177 | for (ws, cont) in a { 178 | match (ws, &cont, out.last()) { 179 | #[cfg(feature = "quoted-string-rfc2047")] 180 | (_, QContent::EncodedWord(_), Some(QContent::EncodedWord(_))) => (), 181 | (Some(ws),_, _) => { out.push(QContent::Literal(ws)); }, 182 | _ => (), 183 | } 184 | out.push(cont); 185 | } 186 | if let Some(x) = b { out.push(QContent::Literal(x)) } 187 | out 188 | })(input) 189 | } 190 | 191 | pub(crate) fn quoted_string(input: &[u8]) -> NomResult { 192 | map(delimited(opt(cfws::

), _inner_quoted_string::

, opt(cfws::

)), 193 | |qc| QuotedString(concat_qs(qc.into_iter())))(input) 194 | } 195 | 196 | /// A single mailbox with an optional display name. 197 | #[derive(Clone, Debug, PartialEq)] 198 | pub struct Mailbox { 199 | /// The display name. 200 | pub dname: Option, 201 | /// The address of this mailbox. 202 | pub address: types::Mailbox, 203 | } 204 | 205 | /// A group of many [`Mailbox`]. 206 | #[derive(Clone, Debug, PartialEq)] 207 | pub struct Group { 208 | /// This group's display name. 209 | pub dname: String, 210 | /// The members of this group. May be empty. 211 | pub members: Vec, 212 | } 213 | 214 | /// An address is either a single [`Mailbox`] or a [`Group`]. 215 | #[derive(Clone, Debug, PartialEq)] 216 | pub enum Address { 217 | /// Single [`Mailbox`]. 218 | Mailbox(Mailbox), 219 | /// [`Group`] of many [`Mailbox`]. 220 | Group(Group), 221 | } 222 | 223 | #[derive(Clone, Debug)] 224 | enum QContent<'a> { 225 | Literal(Cow<'a, str>), 226 | #[cfg(feature = "quoted-string-rfc2047")] 227 | EncodedWord(String), 228 | QP(char), 229 | } 230 | 231 | #[derive(Clone, Debug)] 232 | enum Text<'a> { 233 | Literal(String), 234 | Atom(&'a str), 235 | } 236 | 237 | impl <'a> From<&'a Text<'a>> for &'a str { 238 | fn from(t: &'a Text<'a>) -> &'a str { 239 | match t { 240 | Text::Literal(s) => s, 241 | Text::Atom(s) => s, 242 | } 243 | } 244 | } 245 | 246 | fn concat_qs<'a, A: Iterator>>(input: A) -> String { 247 | let mut out = String::new(); 248 | 249 | for qc in input { 250 | match qc { 251 | QContent::Literal(lit) => out.push_str(&lit), 252 | #[cfg(feature = "quoted-string-rfc2047")] 253 | QContent::EncodedWord(ew) => out.push_str(&ew), 254 | QContent::QP(c) => out.push(c), 255 | } 256 | } 257 | out 258 | } 259 | 260 | fn _single_char(len: usize) -> impl Fn(&[u8]) -> NomResult { 261 | move |input| { 262 | map_opt(take(len), |c| str::from_utf8(c).ok().and_then(|c| { 263 | if c.len() == len && c.chars().count() == 1 { 264 | c.chars().next() 265 | } else { 266 | None 267 | } 268 | }))(input) 269 | } 270 | } 271 | 272 | pub(crate) fn utf8_non_ascii(input: &[u8]) -> NomResult { 273 | alt((_single_char(4), _single_char(3), _single_char(2)))(input) 274 | } 275 | 276 | pub(crate) fn dot_atom(input: &[u8]) -> NomResult { 277 | map(delimited(opt(cfws::

), recognize(pair(recognize_many1(P::atext), recognize_many0(pair(tag("."), recognize_many1(P::atext))))), opt(cfws::

)), 278 | |a| (DotAtom(str::from_utf8(a).unwrap().into())))(input) 279 | } 280 | 281 | pub(crate) fn atom(input: &[u8]) -> NomResult<&[u8]> { 282 | delimited(opt(cfws::

), recognize_many1(P::atext), opt(cfws::

))(input) 283 | } 284 | 285 | pub(crate) fn _padded_encoded_word(input: &[u8]) -> NomResult { 286 | delimited(opt(cfws::

), encoded_word, opt(cfws::

))(input) 287 | } 288 | 289 | fn word(input: &[u8]) -> NomResult { 290 | alt(( 291 | map(_padded_encoded_word::

, Text::Literal), 292 | map(atom::

, |x| Text::Atom(str::from_utf8(&x).unwrap())), 293 | map(quoted_string::

, |qs| Text::Literal(qs.0)) 294 | ))(input) 295 | } 296 | 297 | fn _concat_atom_and_qs<'a, A>(input: A) -> String 298 | where A: Iterator>, 299 | { 300 | let mut iter = input.peekable(); 301 | let mut out = String::new(); 302 | 303 | while let Some(cur) = iter.next() { 304 | match (cur, iter.peek()) { 305 | (Text::Atom(v), Some(_)) => {out.push_str(&v); out.push(' ')}, 306 | (_, Some(Text::Atom(v))) => {out.push_str(&v); out.push(' ')}, 307 | (ref t1, _) => out.push_str(t1.into()), 308 | }; 309 | }; 310 | 311 | out 312 | } 313 | 314 | fn display_name(input: &[u8]) -> NomResult { 315 | map(many1(word::

), |words| _concat_atom_and_qs(words.into_iter().map(Into::into)))(input) 316 | } 317 | 318 | pub(crate) fn local_part(input: &[u8]) -> NomResult { 319 | alt((map(dot_atom::

, |a| a.into()), 320 | map(quoted_string::

, LocalPart::Quoted)))(input) 321 | } 322 | 323 | pub(crate) fn domain_literal(input: &[u8]) -> NomResult { 324 | map(delimited(pair(opt(cfws::

), tag("[")), 325 | pair(many0(pair(ofws, recognize_many1(P::dtext))), ofws), 326 | pair(tag("]"), opt(cfws::

))), 327 | |(a, b)| { 328 | let mut out: String = a.iter().flat_map(|(x, y)| x.chars().chain(str::from_utf8(y).unwrap().chars())).collect(); 329 | out.push_str(&b); 330 | let literal = AddressLiteral::FreeForm(out); 331 | literal.upgrade().unwrap_or(literal) 332 | })(input) 333 | } 334 | 335 | pub(crate) fn _domain(input: &[u8]) -> NomResult { 336 | map(dot_atom::

, |a| Domain(a.0))(input) 337 | } 338 | 339 | pub(crate) fn domain(input: &[u8]) -> NomResult { 340 | alt((map(_domain::

, DomainPart::Domain), 341 | map(domain_literal::

, DomainPart::Address)))(input) 342 | } 343 | 344 | pub(crate) fn addr_spec(input: &[u8]) -> NomResult { 345 | map(separated_pair(local_part::

, tag("@"), domain::

), 346 | |(lp, domain)| types::Mailbox(lp, domain))(input) 347 | } 348 | 349 | fn angle_addr(input: &[u8]) -> NomResult { 350 | delimited(pair(opt(cfws::

), tag("<")), 351 | addr_spec::

, 352 | pair(tag(">"), opt(cfws::

)))(input) 353 | } 354 | 355 | fn name_addr(input: &[u8]) -> NomResult { 356 | map(pair(opt(display_name::

), angle_addr::

), 357 | |(dname, address)| Mailbox{dname, address})(input) 358 | } 359 | 360 | fn mailbox(input: &[u8]) -> NomResult { 361 | alt((name_addr::

, 362 | map(addr_spec::

, |a| Mailbox{dname: None, address: a})))(input) 363 | } 364 | 365 | fn mailbox_list(input: &[u8]) -> NomResult> { 366 | fold_prefix0(mailbox::

, preceded(tag(","), mailbox::

))(input) 367 | } 368 | 369 | fn group_list(input: &[u8]) -> NomResult> { 370 | alt((mailbox_list::

, map(cfws::

, |_| vec![])))(input) 371 | } 372 | 373 | fn group(input: &[u8]) -> NomResult { 374 | map(pair(terminated(display_name::

, tag(":")), 375 | terminated(opt(group_list::

), pair(tag(";"), opt(cfws::

)))), 376 | |(dname, members)| Group{dname, members: members.unwrap_or_default()})(input) 377 | } 378 | 379 | fn address(input: &[u8]) -> NomResult

{ 380 | alt((map(mailbox::

, Address::Mailbox), 381 | map(group::

, Address::Group)))(input) 382 | } 383 | 384 | fn address_list(input: &[u8]) -> NomResult> { 385 | fold_prefix0(address::

, preceded(tag(","), address::

))(input) 386 | } 387 | 388 | fn address_list_crlf(input: &[u8]) -> NomResult> { 389 | terminated(address_list::

, opt(crlf))(input) 390 | } 391 | 392 | fn address_crlf(input: &[u8]) -> NomResult

{ 393 | terminated(address::

, opt(crlf))(input) 394 | } 395 | 396 | fn _8bit_char(input: &[u8]) -> NomResult { 397 | map(take1_filter(|c| (0x80..=0xff).contains(&c)), |_| '\u{fffd}')(input) 398 | } 399 | 400 | /// Parse an unstructured header such as `"Subject:"`. 401 | /// 402 | /// Returns a fully decoded string. 403 | pub fn unstructured(input: &[u8]) -> NomResult { 404 | map(pair( 405 | many0(alt(( 406 | pair(ofws, map(fold_prefix0(encoded_word, preceded(fws, encoded_word)), |ew| ew.into_iter().collect())), 407 | pair(ofws, map(many1(alt((P::vchar, _8bit_char))), |c| c.iter().collect::())) 408 | ))), 409 | many0(wsp)), 410 | |(words, ws)| { 411 | let mut out = String::new(); 412 | for (word_ws, word) in words { 413 | out.push_str(&word_ws); 414 | out.push_str(&word); 415 | } 416 | out.push_str(str::from_utf8(&ws).unwrap()); 417 | out 418 | })(input) 419 | } 420 | 421 | /// Parse the content of a `"From:"` header. 422 | /// 423 | /// Returns a list of addresses, since [RFC 6854] allows multiple mail 424 | /// authors. 425 | /// 426 | /// [RFC 6854]: https://tools.ietf.org/html/rfc6854 427 | pub fn from(i: &[u8]) -> NomResult> { 428 | address_list_crlf::

(i) 429 | } 430 | 431 | /// Parse the content of a `"Sender:"` header. 432 | /// 433 | /// Returns a single address. 434 | pub fn sender(i: &[u8]) -> NomResult

{ 435 | address_crlf::

(i) 436 | } 437 | 438 | /// Parse the content of a `"Reply-To:"` header. 439 | /// 440 | /// Returns a list of addresses. 441 | pub fn reply_to(i: &[u8]) -> NomResult> { 442 | address_list_crlf::

(i) 443 | } 444 | -------------------------------------------------------------------------------- /src/rfc5321.rs: -------------------------------------------------------------------------------- 1 | //! Parsers for [SMTP] command syntax 2 | //! 3 | //! [SMTP]: https://tools.ietf.org/html/rfc5321 4 | 5 | use std::convert::TryFrom; 6 | use std::fmt::{self, Display}; 7 | use std::net::{Ipv4Addr, Ipv6Addr}; 8 | use std::str::{self, FromStr}; 9 | 10 | #[cfg(feature = "serde")] 11 | use serde::{Serialize, Deserialize}; 12 | 13 | use nom::branch::alt; 14 | use nom::bytes::complete::{tag, tag_no_case, take_while1, take_while_m_n}; 15 | use nom::character::{is_alphanumeric, is_digit, is_hex_digit}; 16 | use nom::combinator::{map, map_res, opt, recognize, verify}; 17 | use nom::error::ParseError; 18 | use nom::multi::{many0, many1, many_m_n}; 19 | use nom::sequence::{delimited, pair, preceded, separated_pair, terminated}; 20 | 21 | use crate::behaviour::{Legacy, Intl}; 22 | use crate::rfc5322::utf8_non_ascii; 23 | use crate::rfc5234::{crlf, wsp}; 24 | use crate::types::*; 25 | use crate::util::*; 26 | 27 | #[allow(missing_docs)] // Mostly internal 28 | pub trait UTF8Policy { 29 | fn atext(input: &[u8]) -> NomResult; 30 | fn qtext_smtp(input: &[u8]) -> NomResult; 31 | fn esmtp_value_char(input: &[u8]) -> NomResult; 32 | fn sub_domain(input: &[u8]) -> NomResult<&[u8]>; 33 | } 34 | 35 | impl UTF8Policy for Legacy { 36 | fn atext(input: &[u8]) -> NomResult { 37 | ::atext(input) 38 | } 39 | 40 | fn qtext_smtp(input: &[u8]) -> NomResult { 41 | map(take1_filter(|c| match c {32..=33 | 35..=91 | 93..=126 => true, _ => false}), char::from)(input) 42 | } 43 | 44 | fn esmtp_value_char(input: &[u8]) -> NomResult { 45 | map(take1_filter(|c| match c {33..=60 | 62..=126 => true, _ => false}), char::from)(input) 46 | } 47 | 48 | fn sub_domain(input: &[u8]) -> NomResult<&[u8]> { 49 | recognize(pair(let_dig, opt(ldh_str)))(input) 50 | } 51 | } 52 | 53 | impl UTF8Policy for Intl { 54 | fn atext(input: &[u8]) -> NomResult { 55 | ::atext(input) 56 | } 57 | 58 | fn qtext_smtp(input: &[u8]) -> NomResult { 59 | alt((Legacy::qtext_smtp, utf8_non_ascii))(input) 60 | } 61 | 62 | fn esmtp_value_char(input: &[u8]) -> NomResult { 63 | alt((Legacy::esmtp_value_char, utf8_non_ascii))(input) 64 | } 65 | 66 | fn sub_domain(input: &[u8]) -> NomResult<&[u8]> { 67 | verify(recognize_many1(alt((map(take1_filter(_is_ldh), char::from), utf8_non_ascii))), |label| { 68 | idna::Config::default() 69 | .use_std3_ascii_rules(true) 70 | .verify_dns_length(true) 71 | .check_hyphens(true) 72 | .to_ascii(str::from_utf8(label).unwrap()) 73 | .is_ok() 74 | })(input) 75 | } 76 | } 77 | 78 | /// ESMTP parameter. 79 | /// 80 | /// Represents an ESMTP parameter. 81 | /// # Examples 82 | /// ``` 83 | /// use std::convert::TryFrom; 84 | /// use rustyknife::rfc5321::Param; 85 | /// 86 | /// // Parse a flag that may be present on a MAIL command. 87 | /// assert_eq!(Param::try_from(b"BODY=8BIT".as_ref()).unwrap(), 88 | /// Param::new("BODY", Some("8BIT")).unwrap()); 89 | /// 90 | /// // Parse a flag that may be present on an EXPN command. 91 | /// assert_eq!(Param::try_from(b"SMTPUTF8".as_ref()).unwrap(), 92 | /// Param::new("SMTPUTF8", None).unwrap()); 93 | /// ``` 94 | #[derive(Clone, Debug, PartialEq)] 95 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] 96 | pub struct Param(pub Keyword, pub Option); 97 | nom_fromstr!(Param, esmtp_param::); 98 | 99 | impl Param { 100 | /// Build a new parameter from string values with syntax checking. 101 | pub fn new>(keyword: T, value: Option) -> Result { 102 | let value = match value { 103 | Some(v) => Some(Value::try_from(v.as_ref()).map_err(|_| ())?), 104 | None => None, 105 | }; 106 | Ok(Param(Keyword::try_from(keyword.as_ref()).map_err(|_| ())?, value)) 107 | } 108 | } 109 | 110 | impl Display for Param { 111 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 112 | match &self.1 { 113 | Some(value) => write!(f, "{}={}", self.0, value), 114 | None => write!(f, "{}", self.0), 115 | } 116 | } 117 | } 118 | 119 | impl From for String { 120 | fn from(param: Param) -> String { 121 | param.to_string() 122 | } 123 | } 124 | 125 | /// Newtype over a slice of Param for display purposes. 126 | pub struct Params<'a>(pub &'a [Param]); 127 | 128 | impl<'a, T> From<&'a T> for Params<'a> 129 | where 130 | T: AsRef<[Param]> + 'a, 131 | { 132 | fn from(p: &'a T) -> Self { 133 | Params(p.as_ref()) 134 | } 135 | } 136 | 137 | impl<'a> Display for Params<'a> { 138 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 139 | for (i, param) in self.0.iter().enumerate() { 140 | if i < self.0.len() - 1 { 141 | write!(f, "{} ", param)?; 142 | } else { 143 | write!(f, "{}", param)?; 144 | } 145 | } 146 | Ok(()) 147 | } 148 | } 149 | 150 | /// ESMTP parameter keyword. 151 | /// 152 | /// Used as the left side in an ESMTP parameter. For example, it 153 | /// represents the "BODY" string in a parameter "BODY=8BIT". 154 | #[derive(Clone, PartialEq)] 155 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] 156 | pub struct Keyword(pub(crate) String); 157 | string_newtype!(Keyword); 158 | nom_fromstr!(Keyword, esmtp_keyword); 159 | 160 | /// ESMTP parameter value. 161 | /// 162 | /// Used as the right side in an ESMTP parameter. For example, it 163 | /// represents the "8BIT" string in a parameter "BODY=8BIT". 164 | #[derive(Clone, PartialEq)] 165 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] 166 | pub struct Value(pub(crate) String); 167 | string_newtype!(Value); 168 | nom_fromstr!(Value, esmtp_value::); 169 | 170 | /// Path with source route. 171 | /// 172 | /// The source route is absent when `self.1.is_empty()`. 173 | #[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] 174 | pub struct Path(pub Mailbox, pub Vec); 175 | nom_fromstr!(Path, path::); 176 | 177 | /// A generic SMTP string built from an atom or a quoted string 178 | #[derive(Clone, PartialEq)] 179 | pub struct SMTPString(pub(crate) String); 180 | string_newtype!(SMTPString); 181 | 182 | /// Represents a forward path from the `"RCPT TO"` command. 183 | #[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] 184 | pub enum ForwardPath { 185 | /// `""` 186 | Path(Path), 187 | /// - `PostMaster(None)` = `""` 188 | /// - `PostMaster(Some("domain.example.org"))` = `""` 189 | PostMaster(Option), 190 | } 191 | nom_fromstr!(ForwardPath, _forward_path::); 192 | 193 | impl ForwardPath { 194 | /// Convert this path into a mailbox. 195 | /// 196 | /// The postmaster domain must be provided since this path might 197 | /// be an unqualified address. 198 | pub fn into_mailbox(self, postmaster_domain: &DomainPart) -> Mailbox { 199 | match self { 200 | Self::Path(Path(mailbox, _)) => mailbox, 201 | Self::PostMaster(domain) => { 202 | let lp = LocalPart::from_smtp(b"postmaster").unwrap(); 203 | match domain { 204 | Some(domain) => Mailbox(lp, domain.into()), 205 | None => Mailbox(lp, postmaster_domain.clone()), 206 | } 207 | } 208 | } 209 | } 210 | } 211 | 212 | impl Display for ForwardPath { 213 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 214 | match self { 215 | ForwardPath::Path(p) => write!(f, "<{}>", p.0), 216 | ForwardPath::PostMaster(None) => write!(f, ""), 217 | ForwardPath::PostMaster(Some(d)) => write!(f, "", d), 218 | } 219 | } 220 | } 221 | 222 | /// Represents a reverse path from the `"MAIL FROM"` command. 223 | #[derive(Clone, Debug, PartialEq)] 224 | pub enum ReversePath { 225 | /// MAIL FROM: \ 226 | Path(Path), 227 | /// MAIL FROM: \<\> 228 | Null, 229 | } 230 | nom_fromstr!(ReversePath, reverse_path::); 231 | 232 | impl Display for ReversePath { 233 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 234 | match self { 235 | ReversePath::Path(p) => write!(f, "<{}>", p.0), 236 | ReversePath::Null => write!(f, "<>"), 237 | } 238 | } 239 | } 240 | 241 | impl From for Option { 242 | fn from(path: ReversePath) -> Option { 243 | match path { 244 | ReversePath::Path(Path(mailbox, _)) => Some(mailbox), 245 | ReversePath::Null => None, 246 | } 247 | } 248 | } 249 | 250 | fn _is_ldh(c: u8) -> bool { 251 | is_alphanumeric(c) || c == b'-' 252 | } 253 | 254 | fn esmtp_keyword(input: &[u8]) -> NomResult { 255 | map(recognize(pair(take1_filter(is_alphanumeric), recognize_many0(take1_filter(_is_ldh)))), 256 | |x| Keyword(std::str::from_utf8(x).unwrap().into()))(input) 257 | } 258 | 259 | fn esmtp_value(input: &[u8]) -> NomResult { 260 | map(recognize_many1(P::esmtp_value_char), 261 | |x| Value(std::str::from_utf8(x).unwrap().into()))(input) 262 | } 263 | 264 | fn esmtp_param(input: &[u8]) -> NomResult { 265 | map(pair(esmtp_keyword, opt(preceded(tag("="), esmtp_value::

))), 266 | |(n, v)| Param(n, v))(input) 267 | } 268 | 269 | fn _esmtp_params(input: &[u8]) -> NomResult> { 270 | fold_prefix0(esmtp_param::

, preceded(many1(wsp), esmtp_param::

))(input) 271 | } 272 | 273 | fn ldh_str(input: &[u8]) -> NomResult<&[u8]> { 274 | let (_, mut out) = take_while1(_is_ldh)(input)?; 275 | 276 | while out.last() == Some(&b'-') { 277 | out = &out[..out.len()-1]; 278 | } 279 | 280 | if out.is_empty() { 281 | #[allow(clippy::unit_arg)] 282 | Err(nom::Err::Error(NomError::from_error_kind(input, nom::error::ErrorKind::TakeWhile1))) 283 | } else { 284 | Ok((&input[out.len()..], out)) 285 | } 286 | } 287 | 288 | fn let_dig(input: &[u8]) -> NomResult { 289 | take1_filter(is_alphanumeric)(input) 290 | } 291 | 292 | pub(crate) fn domain(input: &[u8]) -> NomResult { 293 | map(recognize(pair(P::sub_domain, many0(pair(tag("."), P::sub_domain)))), 294 | |domain| Domain(str::from_utf8(domain).unwrap().into()))(input) 295 | } 296 | 297 | fn at_domain(input: &[u8]) -> NomResult { 298 | preceded(tag("@"), domain::

)(input) 299 | } 300 | 301 | fn a_d_l(input: &[u8]) -> NomResult> { 302 | fold_prefix0(at_domain::

, preceded(tag(","), at_domain::

))(input) 303 | } 304 | 305 | fn atom(input: &[u8]) -> NomResult<&[u8]> { 306 | recognize_many1(P::atext)(input) 307 | } 308 | 309 | pub(crate) fn dot_string(input: &[u8]) -> NomResult { 310 | map(recognize(pair(atom::

, many0(pair(tag("."), atom::

)))), 311 | |a| DotAtom(str::from_utf8(a).unwrap().into()))(input) 312 | } 313 | 314 | fn quoted_pair_smtp(input: &[u8]) -> NomResult { 315 | preceded(tag("\\"), map(take1_filter(|c| (32..=126).contains(&c)), char::from))(input) 316 | } 317 | 318 | fn qcontent_smtp(input: &[u8]) -> NomResult { 319 | alt((P::qtext_smtp, quoted_pair_smtp))(input) 320 | } 321 | 322 | pub(crate) fn quoted_string(input: &[u8]) -> NomResult { 323 | map(delimited( 324 | tag("\""), 325 | many0(qcontent_smtp::

), 326 | tag("\"")), 327 | |qs| QuotedString(qs.into_iter().collect()))(input) 328 | } 329 | 330 | pub(crate) fn local_part(input: &[u8]) -> NomResult { 331 | alt((map(dot_string::

, |s| s.into()), 332 | map(quoted_string::

, LocalPart::Quoted)))(input) 333 | } 334 | 335 | fn _ip_int(input: &[u8]) -> NomResult { 336 | map_res(take_while_m_n(1, 3, is_digit), 337 | |ip| str::from_utf8(ip).unwrap().parse())(input) 338 | } 339 | 340 | fn _ipv4_literal(input: &[u8]) -> NomResult { 341 | map(pair(_ip_int, many_m_n(3, 3, preceded(tag("."), _ip_int))), 342 | |(a, b)| (AddressLiteral::IP(Ipv4Addr::new(a, b[0], b[1], b[2]).into())))(input) 343 | } 344 | 345 | fn _ipv6_literal(input: &[u8]) -> NomResult { 346 | map_res(preceded(tag_no_case("IPv6:"), take_while1(|c| is_hex_digit(c) || c == b':' || c == b'.')), 347 | |addr| Ipv6Addr::from_str(str::from_utf8(addr).unwrap()).map(|ip| AddressLiteral::IP(ip.into())))(input) 348 | } 349 | 350 | fn dcontent(input: &[u8]) -> NomResult { 351 | take1_filter(|c| match c { 33..=90 | 94..=126 => true, _ => false})(input) 352 | } 353 | 354 | fn general_address_literal(input: &[u8]) -> NomResult { 355 | map(separated_pair(ldh_str, tag(":"), map(recognize_many1(dcontent), |d| str::from_utf8(d).unwrap())), 356 | |(tag, value)| AddressLiteral::Tagged(str::from_utf8(tag).unwrap().into(), value.into()) 357 | )(input) 358 | } 359 | 360 | pub(crate) fn _inner_address_literal(input: &[u8]) -> NomResult { 361 | alt((_ipv4_literal, _ipv6_literal, general_address_literal))(input) 362 | } 363 | 364 | pub(crate) fn address_literal(input: &[u8]) -> NomResult { 365 | delimited(tag("["), _inner_address_literal, tag("]"))(input) 366 | } 367 | 368 | pub(crate) fn _domain_part(input: &[u8]) -> NomResult { 369 | alt((map(domain::

, DomainPart::Domain), map(address_literal, DomainPart::Address)))(input) 370 | } 371 | 372 | pub fn mailbox(input: &[u8]) -> NomResult { 373 | map(separated_pair(local_part::

, tag("@"), _domain_part::

), 374 | |(lp, dp)| Mailbox(lp, dp))(input) 375 | } 376 | 377 | fn path(input: &[u8]) -> NomResult { 378 | map(delimited( 379 | tag("<"), 380 | pair(opt(terminated(a_d_l::

, tag(":"))), mailbox::

), 381 | tag(">")), 382 | |(path, m)| Path(m, path.unwrap_or_default()))(input) 383 | } 384 | 385 | fn reverse_path(input: &[u8]) -> NomResult { 386 | alt((map(path::

, ReversePath::Path), 387 | map(tag("<>"), |_| ReversePath::Null)))(input) 388 | } 389 | 390 | /// Parse an SMTP EHLO command. 391 | pub fn ehlo_command(input: &[u8]) -> NomResult { 392 | delimited(tag_no_case("EHLO "), _domain_part::

, crlf)(input) 393 | } 394 | 395 | /// Parse an SMTP HELO command. 396 | pub fn helo_command(input: &[u8]) -> NomResult { 397 | delimited(tag_no_case("HELO "), domain::

, crlf)(input) 398 | } 399 | 400 | /// Parse an SMTP MAIL FROM command. 401 | /// 402 | /// Returns a tuple with the reverse path and ESMTP parameters. 403 | /// # Examples 404 | /// ``` 405 | /// use rustyknife::behaviour::Intl; 406 | /// use rustyknife::rfc5321::{mail_command, Param}; 407 | /// 408 | /// let (_, (rp, params)) = mail_command::(b"MAIL FROM: BODY=8BIT\r\n").unwrap(); 409 | /// 410 | /// assert_eq!(rp.to_string(), ""); 411 | /// assert_eq!(params, [Param::new("BODY", Some("8BIT")).unwrap()]); 412 | /// ``` 413 | pub fn mail_command(input: &[u8]) -> NomResult<(ReversePath, Vec)> { 414 | map(delimited(tag_no_case("MAIL FROM:"), 415 | pair(reverse_path::

, opt(preceded(tag(" "), _esmtp_params::

))), 416 | crlf), 417 | |(addr, params)| (addr, params.unwrap_or_default()))(input) 418 | } 419 | 420 | fn _forward_path(input: &[u8]) -> NomResult { 421 | alt((map(tag_no_case(""), |_| ForwardPath::PostMaster(None)), 422 | map(delimited(tag_no_case(", tag(">")), |d| ForwardPath::PostMaster(Some(d))), 423 | map(path::

, ForwardPath::Path) 424 | ))(input) 425 | } 426 | 427 | /// Parse an SMTP RCPT TO command. 428 | /// 429 | /// Returns a tuple with the forward path and ESMTP parameters. 430 | /// # Examples 431 | /// ``` 432 | /// use rustyknife::behaviour::Intl; 433 | /// use rustyknife::rfc5321::{rcpt_command, Param}; 434 | /// 435 | /// let (_, (p, params)) = rcpt_command::(b"RCPT TO: NOTIFY=NEVER\r\n").unwrap(); 436 | /// 437 | /// assert_eq!(p.to_string(), ""); 438 | /// assert_eq!(params, [Param::new("NOTIFY", Some("NEVER")).unwrap()]); 439 | /// ``` 440 | pub fn rcpt_command(input: &[u8]) -> NomResult<(ForwardPath, Vec)> { 441 | map(delimited( 442 | tag_no_case("RCPT TO:"), 443 | pair(_forward_path::

, opt(preceded(tag(" "), _esmtp_params::

))), 444 | crlf, 445 | ), |(path, params)| (path, params.unwrap_or_default()))(input) 446 | } 447 | 448 | /// Parse an SMTP DATA command. 449 | pub fn data_command(input: &[u8]) -> NomResult<()> { 450 | map(tag_no_case("DATA\r\n"), |_| ())(input) 451 | } 452 | 453 | /// Parse an SMTP RSET command. 454 | pub fn rset_command(input: &[u8]) -> NomResult<()> { 455 | map(tag_no_case("RSET\r\n"), |_| ())(input) 456 | } 457 | 458 | fn _smtp_string(input: &[u8]) -> NomResult { 459 | alt((map(atom::

, |a| SMTPString(str::from_utf8(a).unwrap().into())), 460 | map(quoted_string::

, |qs| SMTPString(qs.into()))))(input) 461 | } 462 | 463 | /// Parse an SMTP NOOP command. 464 | pub fn noop_command(input: &[u8]) -> NomResult> { 465 | delimited(tag_no_case("NOOP"), 466 | opt(preceded(tag(" "), _smtp_string::

)), 467 | crlf)(input) 468 | } 469 | 470 | /// Parse an SMTP QUIT command. 471 | pub fn quit_command(input: &[u8]) -> NomResult<()> { 472 | map(tag_no_case("QUIT\r\n"), |_| ())(input) 473 | } 474 | 475 | /// Parse an SMTP VRFY command. 476 | pub fn vrfy_command(input: &[u8]) -> NomResult { 477 | delimited(tag_no_case("VRFY "), _smtp_string::

, crlf)(input) 478 | } 479 | 480 | /// Parse an SMTP EXPN command. 481 | pub fn expn_command(input: &[u8]) -> NomResult { 482 | delimited(tag_no_case("EXPN "), _smtp_string::

, crlf)(input) 483 | } 484 | 485 | /// Parse an SMTP HELP command. 486 | pub fn help_command(input: &[u8]) -> NomResult> { 487 | delimited(tag_no_case("HELP"), 488 | opt(preceded(tag(" "), _smtp_string::

)), 489 | crlf)(input) 490 | } 491 | 492 | /// The base SMTP command set 493 | /// 494 | /// The data on each variant corresponds to the return type of the 495 | /// *_command functions. 496 | #[derive(Debug)] 497 | #[allow(missing_docs)] 498 | pub enum Command { 499 | EHLO(DomainPart), 500 | HELO(Domain), 501 | MAIL(ReversePath, Vec), 502 | RCPT(ForwardPath, Vec), 503 | DATA, 504 | RSET, 505 | NOOP(Option), 506 | QUIT, 507 | VRFY(SMTPString), 508 | EXPN(SMTPString), 509 | HELP(Option), 510 | } 511 | 512 | /// Parse any basic SMTP command. 513 | pub fn command(input: &[u8]) -> NomResult { 514 | alt(( 515 | map(ehlo_command::

, Command::EHLO), 516 | map(helo_command::

, Command::HELO), 517 | map(mail_command::

, |(a, p)| Command::MAIL(a, p)), 518 | map(rcpt_command::

, |(a, p)| Command::RCPT(a, p)), 519 | map(data_command, |_| Command::DATA), 520 | map(rset_command, |_| Command::RSET), 521 | map(noop_command::

, Command::NOOP), 522 | map(quit_command, |_| Command::QUIT), 523 | map(vrfy_command::

, Command::VRFY), 524 | map(expn_command::

, Command::EXPN), 525 | map(help_command::

, Command::HELP), 526 | ))(input) 527 | } 528 | 529 | /// Validates an email address. 530 | /// 531 | /// Does not accept the empty address. 532 | /// # Examples 533 | /// ``` 534 | /// use rustyknife::behaviour::Intl; 535 | /// use rustyknife::rfc5321::validate_address; 536 | /// 537 | /// assert!(validate_address::(b"bob@example.org")); 538 | /// assert!(validate_address::(b"bob@[aoeu:192.0.2.1]")); 539 | /// assert!(!validate_address::(b"")); 540 | /// ``` 541 | pub fn validate_address(i: &[u8]) -> bool { 542 | exact!(i, mailbox::

).is_ok() 543 | } 544 | 545 | /// Parse a STARTTLS command from RFC 3207 546 | pub fn starttls_command(input: &[u8]) -> NomResult<()> { 547 | map(tag_no_case("STARTTLS\r\n"), |_| ())(input) 548 | } 549 | 550 | /// Parse a BDAT command from RFC 3030 551 | pub fn bdat_command(input: &[u8]) -> NomResult<(u64, bool)> { 552 | terminated( 553 | pair( 554 | preceded(tag_no_case("BDAT "), bdat_chunk_size), 555 | map(opt(tag_no_case(" LAST")), |l| l.is_some()), 556 | ), 557 | crlf, 558 | )(input) 559 | } 560 | 561 | fn bdat_chunk_size(input: &[u8]) -> NomResult { 562 | map_res(take_while_m_n(1, 20, is_digit), |s| { 563 | std::str::from_utf8(s).unwrap().parse() 564 | })(input) 565 | } 566 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------