hask-irc/Network/IRC/Protocol.hs

53 lines
2.0 KiB
Haskell
Raw Normal View History

2014-05-04 07:43:37 +05:30
{-# LANGUAGE RecordWildCards, OverloadedStrings #-}
2014-05-04 02:57:43 +05:30
2014-05-04 07:43:37 +05:30
module Network.IRC.Protocol (msgFromLine, lineFromCommand) where
2014-05-04 02:57:43 +05:30
2014-05-04 07:43:37 +05:30
import qualified Data.List as L
import Data.Text
import Prelude hiding (drop, unwords, takeWhile, (++))
2014-05-04 02:57:43 +05:30
import System.Time
import Network.IRC.Types
2014-05-04 07:43:37 +05:30
msgFromLine :: BotConfig -> ClockTime -> Text -> Message
2014-05-04 04:28:44 +05:30
msgFromLine (BotConfig { .. }) time line
2014-05-04 02:57:43 +05:30
| "PING :" `isPrefixOf` line = Ping time . drop 6 $ line
| otherwise = case command of
"JOIN" -> JoinMsg time user
"QUIT" -> QuitMsg time user message
"PART" -> PartMsg time user message
2014-05-06 03:08:09 +05:30
"KICK" -> KickMsg time user kicked kickReason
2014-05-04 02:57:43 +05:30
"MODE" -> if source == botNick
then ModeMsg time Self target message []
else ModeMsg time user target mode modeArgs
"NICK" -> NickMsg time user (drop 1 target)
"PRIVMSG" -> if target == channel
then ChannelMsg time user message
else PrivMsg time user message
_ -> OtherMsg time source command target message
where
isSpc = (== ' ')
isNotSpc = not . isSpc
2014-05-04 07:43:37 +05:30
splits = split isSpc line
source = drop 1 . takeWhile isNotSpc $ line
target = splits !! 2
command = splits !! 1
2014-05-04 07:43:37 +05:30
message = drop 1 . unwords . L.drop 3 $ splits
user = let u = split (== '!') source in User (u !! 0) (u !! 1)
mode = splits !! 3
2014-05-04 07:43:37 +05:30
modeArgs = L.drop 4 splits
2014-05-06 03:08:09 +05:30
kicked = splits !! 3
2014-05-04 07:43:37 +05:30
kickReason = drop 1 . unwords . L.drop 4 $ splits
2014-05-04 02:57:43 +05:30
2014-05-04 07:43:37 +05:30
lineFromCommand :: BotConfig -> Command -> Text
2014-05-04 04:28:44 +05:30
lineFromCommand (BotConfig { .. }) reply = case reply of
2014-05-04 02:57:43 +05:30
Pong { .. } -> "PONG :" ++ rmsg
NickCmd -> "NICK " ++ botNick
UserCmd -> "USER " ++ botNick ++ " 0 * :" ++ botNick
JoinCmd -> "JOIN " ++ channel
ChannelMsgReply { .. } -> "PRIVMSG " ++ channel ++ " :" ++ rmsg
PrivMsgReply (User { .. }) rmsg -> "PRIVMSG " ++ botNick ++ " :" ++ rmsg
2014-05-07 14:35:25 +05:30
where
(++) = append