#ifndef __SMTPSyntax_h__ # define __SMTPSyntax_h__ # include # include # include namespace mail { /// /// SMTPSyntax /// /// Parsing and validation shared by the SMTP command handlers, kept in /// one place so every command applies the same grammar (RFC 5321 /// 4.1.2) rather than each handler doing its own partial version. /// Pure functions - no server or session state. /// namespace SMTPSyntax { /// /// The parsed argument of "MAIL FROM: params" or /// "RCPT TO: params". On failure `reply` holds the complete /// reply line to send (code, enhanced code and text, no CRLF). /// struct PathArgument { bool ok = false; std::string reply; std::string address; // contents of <...>, may be empty for MAIL FROM:<> std::vector> params; // keyword upper-cased }; /// /// The argument text after a command verb: everything past the /// first run of spaces. "" when there is none. /// std::string argument(const std::string &line); /// /// Parse the argument of MAIL ("FROM:") or RCPT ("TO:"). The /// keyword is matched case-insensitively (RFC 5321 2.4); a single /// space after the colon is tolerated, as most servers do, though /// the grammar has none. Parameters are checked for esmtp-param /// syntax only; deciding which ones are supported is the caller's. /// PathArgument parsePath(const std::string &argument, const std::string &keyword, bool allowEmpty); /// /// RFC 5321 4.1.2 Mailbox (Local-part "@" ( Domain / address-literal )) /// with the 4.5.3.1 size limits (local-part 64, domain 255). /// bool validMailbox(const std::string &address); /// /// Split "local@domain" at the LAST "@" (a quoted local-part may /// contain one). Domain is lower-cased - domains are case-insensitive /// (RFC 5321 2.4) and the mail store keys directories on them. /// void splitAddress(const std::string &address, std::string &local, std::string &domain); /// /// True when a local-part/domain can be used as a path component /// under the mail store without escaping it ("/" and NUL are legal /// in an RFC 5321 local-part, but not as a directory name, and "."/ /// ".." would walk the tree). /// bool safePathComponent(const std::string &s); bool iequals(const std::string &a, const std::string &b); std::string upper(std::string s); std::string lower(std::string s); /// /// Strict RFC 4648 base64 decode, as RFC 4954 4 requires for SASL /// responses: returns false for any character outside the alphabet, /// bad padding, or a length that isn't a multiple of 4. /// bool decodeBase64(const std::string &in, std::string &out); /// /// The queue file name is "||" (see /// __SMTP_DATA::queueMail()). "/" and "|" are both legal in an /// address, and either one breaks that name - a "/" turns it into /// a path into some other directory, a "|" shifts the fields - so /// the address parts are %-encoded ("%", "/", "|") on the way in /// and decoded by SMTPServer::inCreate() on the way out. /// std::string encodeQueueField(const std::string &s); std::string decodeQueueField(const std::string &s); } } #endif