| 1 | //! Implementation of IRC codec for Tokio. |
| 2 | use bytes::BytesMut; |
| 3 | use tokio_util::codec::{Decoder, Encoder}; |
| 4 | |
| 5 | use crate::error; |
| 6 | use crate::line::LineCodec; |
| 7 | use crate::message::Message; |
| 8 | |
| 9 | /// An IRC codec built around an inner codec. |
| 10 | pub struct IrcCodec { |
| 11 | inner: LineCodec, |
| 12 | } |
| 13 | |
| 14 | impl IrcCodec { |
| 15 | /// Creates a new instance of IrcCodec wrapping a LineCodec with the specific encoding. |
| 16 | pub fn new(label: &str) -> error::Result<IrcCodec> { |
| 17 | LineCodec::new(label).map(|codec| IrcCodec { inner: codec }) |
| 18 | } |
| 19 | |
| 20 | /// Sanitizes the input string by cutting up to (and including) the first occurence of a line |
| 21 | /// terminiating phrase (`\r\n`, `\r`, or `\n`). This is used in sending messages through the |
| 22 | /// codec to prevent the injection of additional commands. |
| 23 | pub fn sanitize(mut data: String) -> String { |
| 24 | // n.b. ordering matters here to prefer "\r\n" over "\r" |
| 25 | if let Some((pos, len)) = ["\r\n", "\r", "\n"] |
| 26 | .iter() |
| 27 | .flat_map(|needle| data.find(needle).map(|pos| (pos, needle.len()))) |
| 28 | .min_by_key(|&(pos, _)| pos) |
| 29 | { |
| 30 | data.truncate(pos + len); |
| 31 | } |
| 32 | data |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | impl Decoder for IrcCodec { |
| 37 | type Item = Message; |
| 38 | type Error = error::ProtocolError; |
| 39 | |
| 40 | fn decode(&mut self, src: &mut BytesMut) -> error::Result<Option<Message>> { |
| 41 | self.inner |
| 42 | .decode(src) |
| 43 | .and_then(|res| res.map_or(Ok(None), |msg| msg.parse::<Message>().map(Some))) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | impl Encoder<Message> for IrcCodec { |
| 48 | type Error = error::ProtocolError; |
| 49 | |
| 50 | fn encode(&mut self, msg: Message, dst: &mut BytesMut) -> error::Result<()> { |
| 51 | self.inner.encode(IrcCodec::sanitize(msg.to_string()), dst) |
| 52 | } |
| 53 | } |