From 44d8c54867a52381ec741a3b4365a91447059502 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 20 Aug 2017 13:51:31 +0200 Subject: IRC: Chop messages longer than 400 characters The IRC protocol doesn't permit messages longer than 512 bytes (https://news.ycombinator.com/item?id=7991049). This includes server, channel, hostname, etc. information that's also included in the message. In order to be able to send longer messages, chop them at 400 characters (https://wholok.com/irc/), which seems like a "reasonable" hard-coded value. We already split our messages at newlines, so we need to effectuate a double split here. First we split at 400 characters because we might have a line that runs longer than that before encountering a newline. To do this, we use the `chunksOf` function (https://wiki.haskell.org/Data.List.Split). Then, when splitting on newlines, we take the list produced by splitting at 400 characters, and must split the elements inside that list. This creates a two-dimensional array. In order to flatten the array, we use the `concat` function (https://stackoverflow.com/questions/5994051/is-there-a-function-to-flatten-a-nested-list-of-elements). --- src/IRC.hs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/IRC.hs b/src/IRC.hs index 259772c..1c6a7f5 100644 --- a/src/IRC.hs +++ b/src/IRC.hs @@ -66,4 +66,10 @@ privmsgFromPlugin message = do Right r -> Just $ map (\r -> IRC.send $ IRC.Privmsg (channel message) (Right r) ) - (T.lines r) + (splitAtNewlines $ splitLongLines r) + where + -- IRC only permits 512 bytes per line. Use less to allow for protocol + -- information that gets sent in addition to the message content. + splitLongLines txt = T.chunksOf 400 txt + + splitAtNewlines lst = foldr (\s acc -> (T.lines s) ++ acc) [] lst -- cgit v1.2.3