|
| 1 | +///! # format=flowed support |
| 2 | +///! |
| 3 | +///! Format=flowed is defined in |
| 4 | +///! [RFC 3676](https://tools.ietf.org/html/rfc3676). |
| 5 | +///! |
| 6 | +///! Older [RFC 2646](https://tools.ietf.org/html/rfc2646) is used |
| 7 | +///! during formatting, i.e., DelSp parameter introduced in RFC 3676 |
| 8 | +///! is assumed to be set to "no". |
| 9 | +///! |
| 10 | +///! For received messages, DelSp parameter is honoured. |
| 11 | +
|
| 12 | +/// Wraps line to 72 characters using format=flowed soft breaks. |
| 13 | +/// |
| 14 | +/// 72 characters is the limit recommended by RFC 3676. |
| 15 | +/// |
| 16 | +/// The function breaks line only after SP and before non-whitespace |
| 17 | +/// characters. It also does not insert breaks before ">" to avoid the |
| 18 | +/// need to do space stuffing (see RFC 3676) for quotes. |
| 19 | +/// |
| 20 | +/// If there are long words, line may still exceed the limits on line |
| 21 | +/// length. However, this should be rare and should not result in |
| 22 | +/// immediate mail rejection: SMTP (RFC 2821) limit is 998 characters, |
| 23 | +/// and Spam Assassin limit is 78 characters. |
| 24 | +fn format_line_flowed(line: &str) -> String { |
| 25 | + let mut result = String::new(); |
| 26 | + let mut buffer = String::new(); |
| 27 | + let mut after_space = false; |
| 28 | + |
| 29 | + for c in line.chars() { |
| 30 | + if c == ' ' { |
| 31 | + buffer.push(c); |
| 32 | + after_space = true; |
| 33 | + } else { |
| 34 | + if after_space && buffer.len() >= 72 && !c.is_whitespace() && c != '>' { |
| 35 | + // Flush the buffer and insert soft break (SP CRLF). |
| 36 | + result += &buffer; |
| 37 | + result += "\r\n"; |
| 38 | + buffer = String::new(); |
| 39 | + } |
| 40 | + buffer.push(c); |
| 41 | + after_space = false; |
| 42 | + } |
| 43 | + } |
| 44 | + result + &buffer |
| 45 | +} |
| 46 | + |
| 47 | +/// Returns text formatted according to RFC 3767 (format=flowed). |
| 48 | +/// |
| 49 | +/// This function accepts text separated by LF, but returns text |
| 50 | +/// separated by CRLF. |
| 51 | +/// |
| 52 | +/// RFC 2646 technique is used to insert soft line breaks, so DelSp |
| 53 | +/// SHOULD be set to "no" when sending. |
| 54 | +pub fn format_flowed(text: &str) -> String { |
| 55 | + let mut result = String::new(); |
| 56 | + |
| 57 | + for line in text.split('\n') { |
| 58 | + if !result.is_empty() { |
| 59 | + result += "\r\n"; |
| 60 | + } |
| 61 | + let line = line.trim_end(); |
| 62 | + if line.len() > 78 { |
| 63 | + result += &format_line_flowed(line); |
| 64 | + } else { |
| 65 | + result += line; |
| 66 | + } |
| 67 | + } |
| 68 | + result |
| 69 | +} |
| 70 | + |
| 71 | +/// Joins lines in format=flowed text. |
| 72 | +/// |
| 73 | +/// Lines must be separated by single LF. |
| 74 | +/// |
| 75 | +/// Quote processing is not supported, it is assumed that they are |
| 76 | +/// deleted during simplification. |
| 77 | +/// |
| 78 | +/// Signature separator line is not processed here, it is assumed to |
| 79 | +/// be stripped beforehand. |
| 80 | +pub fn unformat_flowed(text: &str, delsp: bool) -> String { |
| 81 | + let mut result = String::new(); |
| 82 | + let mut skip_newline = true; |
| 83 | + |
| 84 | + for line in text.split('\n') { |
| 85 | + // Revert space-stuffing |
| 86 | + let line = line.strip_prefix(" ").unwrap_or(line); |
| 87 | + |
| 88 | + if !skip_newline { |
| 89 | + result.push('\n'); |
| 90 | + } |
| 91 | + |
| 92 | + if let Some(line) = line.strip_suffix(" ") { |
| 93 | + // Flowed line |
| 94 | + result += line; |
| 95 | + if !delsp { |
| 96 | + result.push(' '); |
| 97 | + } |
| 98 | + skip_newline = true; |
| 99 | + } else { |
| 100 | + // Fixed line |
| 101 | + result += line; |
| 102 | + skip_newline = false; |
| 103 | + } |
| 104 | + } |
| 105 | + result |
| 106 | +} |
| 107 | + |
| 108 | +#[cfg(test)] |
| 109 | +mod tests { |
| 110 | + use super::*; |
| 111 | + |
| 112 | + #[test] |
| 113 | + fn test_format_flowed() { |
| 114 | + let text = "Foo bar baz"; |
| 115 | + assert_eq!(format_flowed(text), "Foo bar baz"); |
| 116 | + |
| 117 | + let text = "This is the Autocrypt Setup Message used to transfer your key between clients.\n\ |
| 118 | + \n\ |
| 119 | + To decrypt and use your key, open the message in an Autocrypt-compliant client and enter the setup code presented on the generating device."; |
| 120 | + let expected = "This is the Autocrypt Setup Message used to transfer your key between clients.\r\n\ |
| 121 | + \r\n\ |
| 122 | + To decrypt and use your key, open the message in an Autocrypt-compliant \r\n\ |
| 123 | + client and enter the setup code presented on the generating device."; |
| 124 | + assert_eq!(format_flowed(text), expected); |
| 125 | + } |
| 126 | + |
| 127 | + #[test] |
| 128 | + fn test_unformat_flowed() { |
| 129 | + let text = "this is a very long message that should be wrapped using format=flowed and \n\ |
| 130 | + unwrapped on the receiver"; |
| 131 | + let expected = |
| 132 | + "this is a very long message that should be wrapped using format=flowed and \ |
| 133 | + unwrapped on the receiver"; |
| 134 | + assert_eq!(unformat_flowed(text, false), expected); |
| 135 | + } |
| 136 | +} |
0 commit comments