Skip to content

Commit

Permalink
Wrote message parsing code
Browse files Browse the repository at this point in the history
  • Loading branch information
ori-sky committed Aug 1, 2014
1 parent ddec737 commit 8fbeede
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/IRCD/Logic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ import Control.Monad.State
import System.IO (hPutStrLn)
import qualified IRCD.TS6 as TS6
import IRCD.Types.Server
import IRCD.Message

doLogic :: Client -> String -> StateT Env IO ()
doLogic client line = do
handles' <- gets (byUid . envClients) >>= return . map (handle . snd) . IM.toList
liftIO (mapM_ f handles')
liftIO $ print (parseMessage line)
where f Nothing = return ()
f (Just handle') = hPutStrLn handle' $ "[::" ++ uidString ++ "] " ++ line
uidString = maybe "*" TS6.intToID (uid client)
39 changes: 39 additions & 0 deletions src/IRCD/Message.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{- Copyright 2014 David Farrell <[email protected]>
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
-}

module IRCD.Message (parseMessage) where

import Data.Char (isSpace, toUpper)
import IRCD.Types.Message
import IRCD.Types.Prefix

parseMessage :: String -> Message
parseMessage "" = Message () Nothing "" []
parseMessage (':':xs)
| null prefix' = msg {prefix=Nothing}
| otherwise = msg {prefix=Just (StringPrefix prefix')}
where (prefix', rest) = break isSpace xs
msg = parseMessage rest
parseMessage (' ':':':xs) = parseMessage xs
parseMessage (' ':xs) = parseMessage xs
parseMessage line = Message () Nothing (map toUpper cmd) (parseParams rest)
where (cmd, rest) = break isSpace line

parseParams :: String -> [String]
parseParams "" = []
parseParams (' ':xs) = parseParams xs
parseParams (':':xs) = [xs]
parseParams str = x : parseParams (drop 1 xs)
where (x, xs) = break isSpace str

0 comments on commit 8fbeede

Please sign in to comment.