blob: ebf8f713cdf2060ca3aa33abfb498b58351d91ad (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
module Hsbot.IRCParser
( ParseError
, parseIrcMsg
, serializeIrcMsg
) where
import Control.Monad.Identity
import Text.Parsec
import Hsbot.Types
-- | Parses an IrcInput
parseIrcMsg :: String -> Either ParseError IrcMsg
parseIrcMsg line = parse pMsg "" line
pMsg :: ParsecT String u Identity IrcMsg
pMsg = do
pfx <- optionMaybe pPrefix
cmd <- pCommand
params <- many (char ' ' >> (pLongParam <|> pShortParam))
--char '\r'
eof
return $ IrcMsg pfx cmd params
pPrefix :: ParsecT String u Identity [Char]
pPrefix = do
char ':'
pfx <- many1 (noneOf " ")
space
return pfx
pCommand :: ParsecT String u Identity [Char]
pCommand = count 3 digit <|> many1 upper
pLongParam :: ParsecT String u Identity [Char]
pLongParam = char ':' >> (many1 (noneOf "\r"))
pShortParam :: ParsecT String u Identity [Char]
pShortParam = many1 (noneOf " \r")
-- |Serialize an IRC message to a string.
serializeIrcMsg :: IrcMsg -> String
serializeIrcMsg (IrcMsg pfx cmd params) = pfxStr ++ cmd ++ paramStr
where pfxStr = case pfx of
Nothing -> ""
Just pfx' -> ":" ++ pfx' ++ " "
paramStr = concat (map paramToStr (init params)
++ [lastParamToStr (last params)])
paramToStr p = " " ++ p
lastParamToStr p = " :" ++ p
|