From 6f5773590d2a1453aba3c47bcca286c4ffeb93bd Mon Sep 17 00:00:00 2001 From: pb Date: Sun, 28 Mar 2010 11:00:50 +0000 Subject: git-svn-id: https://ssl.buetow.org/repos/hsbot/trunk@48 9f8f72e9-4bf4-416e-b76e-7d4203597157 --- HsBot/General/Render.hs | 5 +++ HsBot/General/Tools.hs | 32 ++++++++++++++++ HsBot/IRC/Connection.hs | 96 ----------------------------------------------- HsBot/IRC/IRConnection.hs | 96 +++++++++++++++++++++++++++++++++++++++++++++++ HsBot/IRC/User.hs | 5 +-- HsBot/Logics.hs | 4 -- HsBot/Logics/IRCKarma.hs | 27 ------------- HsBot/Render.hs | 5 --- HsBot/Start.hs | 4 +- HsBot/State.hs | 2 +- HsBot/Tools.hs | 32 ---------------- 11 files changed, 138 insertions(+), 170 deletions(-) create mode 100644 HsBot/General/Render.hs create mode 100644 HsBot/General/Tools.hs delete mode 100644 HsBot/IRC/Connection.hs create mode 100644 HsBot/IRC/IRConnection.hs delete mode 100644 HsBot/Logics/IRCKarma.hs delete mode 100644 HsBot/Render.hs delete mode 100644 HsBot/Tools.hs (limited to 'HsBot') diff --git a/HsBot/General/Render.hs b/HsBot/General/Render.hs new file mode 100644 index 0000000..5768942 --- /dev/null +++ b/HsBot/General/Render.hs @@ -0,0 +1,5 @@ +module HsBot.General.Render where + +class Render a where + render :: a -> String + diff --git a/HsBot/General/Tools.hs b/HsBot/General/Tools.hs new file mode 100644 index 0000000..bd13271 --- /dev/null +++ b/HsBot/General/Tools.hs @@ -0,0 +1,32 @@ +module HsBot.General.Tools where + +uniq :: (Eq a) => [a] -> [a] +uniq list = + let r = u' list 0 + u' [] _ = [] + u' (x:list) n + | member x r n = u' list n + | otherwise = x:(u' list (n + 1)) + member e list 0 = False + member y (x:list) n = x == y || member y list (n - 1) + in r + +split :: String -> Char -> [String] +split [] delim = [""] +split (c:cs) delim + | c == delim = "" : rest + | otherwise = (c : head rest) : tail rest + where rest = split cs delim + +empty :: String -> Bool +empty str = length str == 0 + +contains :: String -> Char -> Bool +contains str char = takeWhile (/= char) str /= str + +isMultiline :: String -> Bool +isMultiline str = contains str '\n' + +showL :: Show a => [a] -> [String] +showL = map (\x -> show x ++ "\n") + diff --git a/HsBot/IRC/Connection.hs b/HsBot/IRC/Connection.hs deleted file mode 100644 index c4fa7a9..0000000 --- a/HsBot/IRC/Connection.hs +++ /dev/null @@ -1,96 +0,0 @@ -module HsBot.IRC.IRConnection (ircStart) where - -import IO -import List -import Network -import System -import System.IO -import Text.Printf - -import HsBot.Conf -import HsBot.Env -import HsBot.State -import HsBot.Tools -import HsBot.IRC.User - -data IrcMessage = IrcMessage { - raw :: String, - from :: String, - clean :: String, - isQuery :: Bool - } deriving Show - -ircWrite :: Handle -> String -> String -> IO () -ircWrite h s t = do - printf "> %s %s\n" s t - hPrintf h "%s %s\r\n" s t - -ircPrivMsg :: Handle -> IrcMessage -> Env -> String -> IO () -ircPrivMsg h msg env@(Env state _) s = do - ircPrivMsg' $ if isMultiline s then (lines s) else [s] - where - ircPrivMsg' [] = return () - ircPrivMsg' (x:xs) = - let maxMessageSize = - envGetInt "maxMessageSize" env - receiver = - if (isQuery msg) - then from msg - else currentChannel state - in if length x > maxMessageSize - then do - ircWrite h "PRIVMSG" (receiver ++ " :" - ++ (take maxMessageSize x) ++ "...") - ircWrite h "PRIVMSG" (receiver ++ " :" - ++ "...this message has been cut to " - ++ (show maxMessageSize) ++ " chars") - ircPrivMsg' xs - else do - ircWrite h "PRIVMSG" (receiver ++ " :" ++ x) - ircPrivMsg' xs - -ircStart :: Env -> IO () -ircStart (DispatchEnv state conf dispatch) = do - ircChannel <- get "ircChannel" conf - ircNick <- get "ircNick" conf - ircPort <- get "ircPort" conf - ircServer <- get "ircServer" conf - ircUser <- get "ircUser" conf - h <- connectTo ircServer (PortNumber $ fromIntegral (read ircPort :: Int)) - hSetBuffering h NoBuffering - ircWrite h "NICK" ircNick - ircWrite h "USER" $ ircNick ++ " 0 * :" ++ ircUser - ircEvalLoop h (DispatchEnv state { currentChannel = ircChannel } conf dispatch) - return () - -ircEvalLoop :: Handle -> Env -> IO () -ircEvalLoop h env = do - t <- hGetLine h - let s = init t - env' <- branch s - ircEvalLoop h env' - where - branch s - | ping s = do { pong s; return (env) } - | otherwise = ircEval h (msg s) env - ping x = "PING :" `isPrefixOf` x - pong x = ircWrite h "PONG" (':' : drop 6 x) - from = drop 1 . takeWhile (/= '!') - clean = drop 1 . dropWhile (/= ':') . drop 1 - isQuery x = split x ' ' !! 2 == (envGet "ircNick" env) - msg s = IrcMessage { - raw = s, from = from s, - clean = clean s, isQuery = isQuery s - } - -ircEval :: Handle -> IrcMessage -> Env -> IO Env -ircEval h msg env@(DispatchEnv state _ dispatch) = ircEval' (clean msg) - where - ircEval' "+x" = do - ircWrite h "JOIN" (currentChannel state) - return (env) - ircEval' cleanMsg = do - (Env s c) <- dispatch cleanMsg sendReplyMsg (castEnv env) - return (DispatchEnv s c dispatch) - sendReplyMsg = ircPrivMsg h msg (castEnv env) - diff --git a/HsBot/IRC/IRConnection.hs b/HsBot/IRC/IRConnection.hs new file mode 100644 index 0000000..610d333 --- /dev/null +++ b/HsBot/IRC/IRConnection.hs @@ -0,0 +1,96 @@ +module HsBot.IRC.IRConnection (ircStart) where + +import IO +import List +import Network +import System +import System.IO +import Text.Printf + +import HsBot.Conf +import HsBot.Env +import HsBot.State +import HsBot.General.Tools +import HsBot.IRC.User + +data IrcMessage = IrcMessage { + raw :: String, + from :: String, + clean :: String, + isQuery :: Bool + } deriving Show + +ircWrite :: Handle -> String -> String -> IO () +ircWrite h s t = do + printf "> %s %s\n" s t + hPrintf h "%s %s\r\n" s t + +ircPrivMsg :: Handle -> IrcMessage -> Env -> String -> IO () +ircPrivMsg h msg env@(Env state _) s = do + ircPrivMsg' $ if isMultiline s then (lines s) else [s] + where + ircPrivMsg' [] = return () + ircPrivMsg' (x:xs) = + let maxMessageSize = + envGetInt "maxMessageSize" env + receiver = + if (isQuery msg) + then from msg + else currentChannel state + in if length x > maxMessageSize + then do + ircWrite h "PRIVMSG" (receiver ++ " :" + ++ (take maxMessageSize x) ++ "...") + ircWrite h "PRIVMSG" (receiver ++ " :" + ++ "...this message has been cut to " + ++ (show maxMessageSize) ++ " chars") + ircPrivMsg' xs + else do + ircWrite h "PRIVMSG" (receiver ++ " :" ++ x) + ircPrivMsg' xs + +ircStart :: Env -> IO () +ircStart (DispatchEnv state conf dispatch) = do + ircChannel <- get "ircChannel" conf + ircNick <- get "ircNick" conf + ircPort <- get "ircPort" conf + ircServer <- get "ircServer" conf + ircUser <- get "ircUser" conf + h <- connectTo ircServer (PortNumber $ fromIntegral (read ircPort :: Int)) + hSetBuffering h NoBuffering + ircWrite h "NICK" ircNick + ircWrite h "USER" $ ircNick ++ " 0 * :" ++ ircUser + ircEvalLoop h (DispatchEnv state { currentChannel = ircChannel } conf dispatch) + return () + +ircEvalLoop :: Handle -> Env -> IO () +ircEvalLoop h env = do + t <- hGetLine h + let s = init t + env' <- branch s + ircEvalLoop h env' + where + branch s + | ping s = do { pong s; return (env) } + | otherwise = ircEval h (msg s) env + ping x = "PING :" `isPrefixOf` x + pong x = ircWrite h "PONG" (':' : drop 6 x) + from = drop 1 . takeWhile (/= '!') + clean = drop 1 . dropWhile (/= ':') . drop 1 + isQuery x = split x ' ' !! 2 == (envGet "ircNick" env) + msg s = IrcMessage { + raw = s, from = from s, + clean = clean s, isQuery = isQuery s + } + +ircEval :: Handle -> IrcMessage -> Env -> IO Env +ircEval h msg env@(DispatchEnv state _ dispatch) = ircEval' (clean msg) + where + ircEval' "+x" = do + ircWrite h "JOIN" (currentChannel state) + return (env) + ircEval' cleanMsg = do + (Env s c) <- dispatch cleanMsg sendReplyMsg (castEnv env) + return (DispatchEnv s c dispatch) + sendReplyMsg = ircPrivMsg h msg (castEnv env) + diff --git a/HsBot/IRC/User.hs b/HsBot/IRC/User.hs index cd6d55f..422f910 100644 --- a/HsBot/IRC/User.hs +++ b/HsBot/IRC/User.hs @@ -1,9 +1,8 @@ -module HsBot.User where +module HsBot.IRC.User where import List -import HsBot.Karma -import HsBot.Render +import HsBot.General.Render data User = User { userName :: String, diff --git a/HsBot/Logics.hs b/HsBot/Logics.hs index 5b534e5..01eda7c 100644 --- a/HsBot/Logics.hs +++ b/HsBot/Logics.hs @@ -1,10 +1,6 @@ module HsBot.Logics (logicsRun) where import HsBot.Env -import HsBot.Logics.IRCKarma - -class Logics where - logicRun :: String -> Env -> IO Env logicsRun :: String -> Env -> IO Env logicsRun str env = return (env) diff --git a/HsBot/Logics/IRCKarma.hs b/HsBot/Logics/IRCKarma.hs deleted file mode 100644 index 55c74d7..0000000 --- a/HsBot/Logics/IRCKarma.hs +++ /dev/null @@ -1,27 +0,0 @@ -module HsBot.Logics.IrcKarma where - -import HsBot.Env -import HsBot.Logics - -data Karma = Karma { - karmaName :: String, - minPts :: Int, - minPerc :: Float - } deriving (Show, Read) - -instance Eq Karma where - x == y = (minPerc x) == (minPerc y) && (minPts x == minPts y) - -instance Ord Karma where - x > y - | (minPerc x) > (minPerc y) = True - | otherwise = (minPts x) > (minPts y) - x < y - | (minPerc x) < (minPerc y) = True - | otherwise = (minPts x) < (minPts y) - x >= y = not (x < y) - x <= y = not (x > y) - -instance Logics Karma where - logicsRun str env = return (env) - diff --git a/HsBot/Render.hs b/HsBot/Render.hs deleted file mode 100644 index 1dc78ba..0000000 --- a/HsBot/Render.hs +++ /dev/null @@ -1,5 +0,0 @@ -module HsBot.Render where - -class Render a where - render :: a -> String - diff --git a/HsBot/Start.hs b/HsBot/Start.hs index 3ea4d1e..228092a 100644 --- a/HsBot/Start.hs +++ b/HsBot/Start.hs @@ -5,10 +5,10 @@ import System import HsBot.Cmd import HsBot.Conf import HsBot.Env -import HsBot.IRC.Connection +import HsBot.IRC.IRConnection import HsBot.Logics import HsBot.State -import HsBot.Tools +import HsBot.General.Tools start :: IO () start = do diff --git a/HsBot/State.hs b/HsBot/State.hs index 93ca7ef..9d1fa87 100644 --- a/HsBot/State.hs +++ b/HsBot/State.hs @@ -3,7 +3,7 @@ module HsBot.State where import qualified Data.Map as M import List -import HsBot.User +import HsBot.IRC.User data State = State { currentChannel :: String, diff --git a/HsBot/Tools.hs b/HsBot/Tools.hs deleted file mode 100644 index 6f66994..0000000 --- a/HsBot/Tools.hs +++ /dev/null @@ -1,32 +0,0 @@ -module HsBot.Tools where - -uniq :: (Eq a) => [a] -> [a] -uniq list = - let r = u' list 0 - u' [] _ = [] - u' (x:list) n - | member x r n = u' list n - | otherwise = x:(u' list (n + 1)) - member e list 0 = False - member y (x:list) n = x == y || member y list (n - 1) - in r - -split :: String -> Char -> [String] -split [] delim = [""] -split (c:cs) delim - | c == delim = "" : rest - | otherwise = (c : head rest) : tail rest - where rest = split cs delim - -empty :: String -> Bool -empty str = length str == 0 - -contains :: String -> Char -> Bool -contains str char = takeWhile (/= char) str /= str - -isMultiline :: String -> Bool -isMultiline str = contains str '\n' - -showL :: Show a => [a] -> [String] -showL = map (\x -> show x ++ "\n") - -- cgit v1.2.3