hask-irc/Network/IRC/Protocol.hs

54 lines
2.2 KiB
Haskell
Raw Normal View History

2014-05-13 00:00:33 +05:30
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
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-10 21:45:16 +05:30
import ClassyPrelude
import Data.List ((!!))
import Data.Text (split)
2014-05-04 02:57:43 +05:30
import Network.IRC.Types
2014-05-10 21:45:16 +05:30
msgFromLine :: BotConfig -> UTCTime -> Text -> Message
2014-05-04 04:28:44 +05:30
msgFromLine (BotConfig { .. }) time line
| "PING :" `isPrefixOf` line = Ping time (drop 6 line) line
2014-05-04 02:57:43 +05:30
| otherwise = case command of
"JOIN" -> JoinMsg time user line
"QUIT" -> QuitMsg time user quitMessage line
"PART" -> PartMsg time user message line
"KICK" -> KickMsg time user kicked kickReason line
2014-05-04 02:57:43 +05:30
"MODE" -> if source == botNick
then ModeMsg time Self target message [] line
else ModeMsg time user target mode modeArgs line
"NICK" -> NickMsg time user (drop 1 target) line
2014-05-04 02:57:43 +05:30
"PRIVMSG" -> if target == channel
then if "\x01" `isPrefixOf` message && "ACTION" `isPrefixOf` drop 1 message
then ActionMsg time user (initDef . drop 8 $ message) line
else ChannelMsg time user message line
else PrivMsg time user message line
_ -> OtherMsg time source command target message line
2014-05-04 02:57:43 +05:30
where
isSpc = (== ' ')
isNotSpc = not . isSpc
splits = split isSpc line
source = drop 1 . takeWhile isNotSpc $ line
target = splits !! 2
command = splits !! 1
message = drop 1 . unwords . drop 3 $ splits
quitMessage = drop 1 . unwords . drop 2 $ splits
2014-05-15 12:02:31 +05:30
user = uncurry User . break (== '!') $ source
mode = splits !! 3
modeArgs = drop 4 splits
kicked = splits !! 3
kickReason = drop 1 . unwords . 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