Last active
December 18, 2015 02:09
-
-
Save funrep/5708906 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module IRC where | |
import Network | |
import System.IO | |
import Control.Monad.Reader | |
import Data.List | |
import Text.Printf | |
import Config | |
type Net = ReaderT Bot IO | |
data Bot = Bot { socket :: Handle, | |
config :: Config } | |
connect :: Config -> IO Bot | |
connect conf = do | |
h <- connectTo (server conf) $ PortNumber $ fromIntegral $ port conf | |
hSetBuffering h NoBuffering | |
return $ Bot h conf | |
type Eval = ((Int, String) -> Net ()) | |
listen :: Eval -> Handle -> Net () | |
listen cmd h = do | |
s <- liftIO $ hGetLine h | |
let t = init s | |
liftIO $ putStrLn t | |
if ping t then pong t else cmd $ clean t | |
listen cmd h | |
where | |
clean x | (":" ++ (asks config >>= owner)) `isPrefixOf` x = (1, clean' x) | |
| otherwise = (0, clean' x) | |
clean' = drop 1 . dropWhile (/= ':') . drop 1 | |
ping x = "PING :" `isPrefixOf` x | |
pong x = write "PONG" $ ':' : drop 6 x | |
privmsg :: String -> Net () | |
privmsg s = write "PRIVMSG" $ (asks config >>= chan) ++ " :" ++ s | |
write :: String -> String -> Net () | |
write s t = do | |
h <- asks socket | |
liftIO $ hPrintf h "%s %s\n" s t | |
liftIO $ printf "> %s %s\n" s t | |
-- Config.hs | |
module Config where | |
import Data.ConfigFile | |
response = "End of /MOTD command." | |
data Config = | |
Config { server :: String, | |
port :: Int, | |
chan :: String, | |
nick :: String, | |
owner :: String } | |
deriving (Show) | |
getConfig :: FilePath -> IO Config | |
getConfig f = do | |
xs <- readFile f | |
let conf = do c <- readstring emptyCP xs | |
srv <- get c "DEFAULT" "server" | |
prt <- get c "DEFAULT" "port" | |
chn <- get c "DEFAULT" "chan" | |
nck <- get c "DEFAULT" "nick" | |
own <- get c "DEFAULT" "owner" | |
return Config { server = srv, | |
port = prt, | |
chan = "#" ++ chn, | |
nick = nck, | |
owner = own } | |
case conf of | |
Left err -> error $ show err | |
Right conf -> return conf |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[3 of 5] Compiling IRC ( src/IRC.hs, dist/build/tob/tob-tmp/IRC.o )
src/IRC.hs:38:32:
No instance for (MonadReader Bot [])
arising from a use of
asks' Possible fix: add an instance declaration for (MonadReader Bot []) In the first argument of
(>>=)', namelyasks config' In the first argument of
(++)', namely(asks config >>= chan)' In the second argument of
($)', namely`(asks config >>= chan) ++ " :" ++ s'