irc-fun-client-0.5.0.0: src/Network/IRC/Fun/Client/Util.hs
{- This file is part of irc-fun-client.
-
- Written in 2015, 2016 by fr33domlover <fr33domlover@riseup.net>.
-
- ♡ 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)
import Network.IRC.Fun.Types
import qualified Data.Text as T
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 :: MsgContent -> Nickname -> Bool
(MsgContent mc) `mentions` (Nickname n) = m mc n True
where
m msg nick previnc =
case T.uncons msg of
Nothing -> False
Just (c, r) ->
if not previnc && nick `T.isPrefixOf` msg
then case T.uncons $ T.drop (T.length nick) msg of
Nothing -> True
Just (d, s) ->
if isNameChar d
then m r nick $ isNameChar c
else True
else m r nick $ isNameChar c