Browse Source

IRC parser implemented

master
Wesley Kerfoot 6 years ago
parent
commit
881216975d
  1. 17
      app/Main.hs
  2. 1
      package.yaml
  3. 54
      src/IRCParser.hs

17
app/Main.hs

@ -2,6 +2,7 @@ module Main where
import Lib
import Types
import IRCParser (runParseMessage)
import Control.Applicative
import Network.Socket hiding (recv)
@ -9,14 +10,12 @@ import Network.Socket.ByteString as S
import qualified Data.ByteString.Lazy as L
import Control.Concurrent (threadDelay, forkIO)
channel = "#thisisatestwhatever"
channel = "#cockgangsters"
userCmd = buildCommand $ user "crapbot" "A crappy bot"
nickCmd = buildCommand $ nick "crapbot"
joinCmd = buildCommand $ join channel
initMsgCmd = buildCommand $ privmsg channel "Hello, my fellow humans!"
quitCmd = buildCommand quit
userCmd = user "crapbot" "A crappy bot"
nickCmd = nick "crapbot"
joinCmd = join channel
initMsgCmd = privmsg channel "Hello, my fellow humans!"
getAddress hname = head <$>
getAddrInfo (return defaultHints) (return hname) (return "6667")
@ -25,7 +24,7 @@ getIRCSock addr = socket (addrFamily addr) Stream defaultProtocol
recvLoop sock = do
msg <- recv sock 4096
print msg
print $ runParseMessage msg
threadDelay 1000000
recvLoop sock
@ -35,6 +34,6 @@ sendIRCConnect host = do
connect sock (addrAddress addr)
forkIO $ recvLoop sock
threadDelay 1000000
sendMany sock [userCmd, nickCmd, joinCmd, initMsgCmd, quitCmd]
sendMany sock (buildCommand <$> [userCmd, nickCmd, joinCmd, initMsgCmd])
main = undefined

1
package.yaml

@ -24,6 +24,7 @@ dependencies:
- text
- bytestring
- network
- attoparsec
library:
source-dirs: src

54
src/IRCParser.hs

@ -0,0 +1,54 @@
{-# LANGUAGE OverloadedStrings #-}
module IRCParser where
import Data.ByteString (pack)
import Data.Attoparsec.ByteString
import Control.Applicative
-- :nisstyre!wes@oftn/oswg-member/Nisstyre PRIVMSG #thisisatestwhatever :yay\r\n
-- :nisstyre!wes@oftn/oswg-member/Nisstyre PART #thisisatestwhatever :\"WeeChat 2.2\"\r\n
-- :nisstyre!wes@oftn/oswg-member/Nisstyre JOIN #thisisatestwhatever\r\n
-- :nisstyre!wes@oftn/oswg-member/Nisstyre QUIT :Quit: WeeChat 2.2\r\n
-- PING :asimov.freenode.net\r\n
space = word8 32
notSpace = notWord8 32
-- 13 = \r, 10 = \n
crlf c = c == 13 || c == 10
-- :
colon = word8 58
command = choice $ map string ["PRIVMSG", "PART", "JOIN", "QUIT", "PING"]
argument = takeWhile1 (\c -> c /= 58 && c /= 32)
parseFinalArg = do
_ <- colon
command <- takeWhile1 (not . crlf)
return command
parseArgs = do
args <- (argument `sepBy` space) <|> alwaysMatch []
_ <- space <|> (alwaysMatch 0)
finalArg <- parseFinalArg <|> (alwaysMatch "")
return (args, finalArg)
source = do
_ <- colon
source <- manyTill notSpace space
return source
-- Matches any character and does not consume input
-- Returns value passed in
alwaysMatch x = (maybe x (const x)) <$> peekWord8
parseMessage = do
sourceName <- source <|> (alwaysMatch [])
commandName <- command
_ <- space
arguments <- parseArgs
return (pack sourceName, commandName, arguments)
runParseMessage = parseOnly parseMessage
Loading…
Cancel
Save