diff options
author | Teddy Wing | 2017-08-20 13:51:31 +0200 |
---|---|---|
committer | Teddy Wing | 2017-08-20 15:15:14 +0200 |
commit | 44d8c54867a52381ec741a3b4365a91447059502 (patch) | |
tree | 058372ba413be0e993c18803686d3ff305c486c4 /src/IRC.hs | |
parent | c543eebfcbd92bb261003dbfd658487b59362a5b (diff) | |
download | sorbot-44d8c54867a52381ec741a3b4365a91447059502.tar.bz2 |
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).
Diffstat (limited to 'src/IRC.hs')
-rw-r--r-- | src/IRC.hs | 8 |
1 files changed, 7 insertions, 1 deletions
@@ -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 |