irc-fun-client-0.1.0.0: src/Network/IRC/Fun/Client/Util.hs
{- This file is part of irc-fun-client.
-
- Written in 2015 by fr33domlover <fr33domlover@rel4tion.org>.
-
- ♡ Copying is an act of love. Please copy, reuse and share.
-
- The author(s) have dedicated all copyright and related and neighboring
- rights to this software to the public domain worldwide. This software is
- distributed without any warranty.
-
- You should have received a copy of the CC0 Public Domain Dedication along
- with this software. If not, see
- <http://creativecommons.org/publicdomain/zero/1.0/>.
-}
module Network.IRC.Fun.Client.Util
( mentions
)
where
import Data.Char (isAlphaNum)
import Data.List (isPrefixOf)
isNameChar :: Char -> Bool
isNameChar c = isAlphaNum c || c `elem` "-_|"
-- | Check if a message mentions a given nickname.
--
-- This isn't the same as substring search, since the nickname could appear
-- inside a longer word. For example, \"ron\" isn't really mentioned in
-- \"asynchronic\". Therefore, this function is a bit smarter about this,
-- trying to capturing real mentions, and works quite well in practice.
mentions :: String -- ^ Message content
-> String -- ^ Nickname
-> Bool
msg `mentions` nick = m msg nick True
where
m "" _ _ = False
m msg@(c:cs) nick previnc =
if not previnc && nick `isPrefixOf` msg
then case drop (length nick) msg of
"" -> True
(d:ds) -> if not (isNameChar d)
then True
else m cs nick $ isNameChar c
else m cs nick $ isNameChar c