summaryrefslogtreecommitdiff
path: root/Hsbot/IRCParser.hs
blob: a76e22aaf05e55d3b8d58d080aa2cd330bdfd98f (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
module Hsbot.IRCParser
    ( IrcMsg (..)
    , ircParser
    ) where

--import Text.Parsec
import Text.ParserCombinators.Parsec

-- |An IRC message.
data IrcMsg = IrcMsg (Maybe String) String [String] -- (Maybe first statement) cmd [chan, params/sentence]
    deriving (Show)

--ircParser :: String -> IrcInput
ircParser :: String -> Either ParseError IrcMsg
ircParser str = parse pMsg "" str

pMsg = do
    pfx <- optionMaybe pPrefix
    cmd <- pCommand
    params <- many (char ' ' >> (pLongParam <|> pShortParam))
    char '\r'
    eof
    return $ IrcMsg pfx cmd params

pPrefix = do
    char ':'
    pfx <- many1 (noneOf " ")
    space
    return pfx

pCommand = count 3 digit <|> many1 upper

pLongParam = char ':' >> (many1 (noneOf "\r"))

pShortParam = many1 (noneOf " \r")