diff --git a/Network/IRC/Client.hs b/Network/IRC/Client.hs index 4160300..ebdb350 100644 --- a/Network/IRC/Client.hs +++ b/Network/IRC/Client.hs @@ -51,9 +51,9 @@ listen = do debug $ "< " ++ line let message = msgFromLine botConfig now line - case message of + nStatus <- case message of JoinMsg { .. } | userNick user == nick -> debug "Joined" >> return Joined - KickMsg { .. } | kicked == nick -> debug "Kicked" >> return Kicked + KickMsg { .. } | kickedNick == nick -> debug "Kicked" >> return Kicked ModeMsg { user = Self, .. } -> sendCommand bot JoinCmd >> return status _ -> return status @@ -67,7 +67,8 @@ listen = do case mCmd of Nothing -> return () Just cmd -> sendCommand bot cmd - return status + + return nStatus put nStatus when (nStatus /= Disconnected) listen diff --git a/Network/IRC/Handlers/Core.hs b/Network/IRC/Handlers/Core.hs index 2613853..f97e346 100644 --- a/Network/IRC/Handlers/Core.hs +++ b/Network/IRC/Handlers/Core.hs @@ -46,10 +46,37 @@ exitMessageLogger = do Nothing -> return () Just logFileHandle -> liftIO $ hClose logFileHandle -messageLogger :: MonadMsgHandler m => Message -> m (Maybe Command) -messageLogger ChannelMsg { .. } = do +withLogFile :: MonadMsgHandler m => (Handle -> IO ()) -> m (Maybe Command) +withLogFile action = do logFileHandle <- map (`fromDyn` error "No log file set") get - let time = formatTime defaultTimeLocale "%F %T" msgTime - liftIO $ TF.hprint logFileHandle "[{}] {}: {}\n" $ TF.buildParams (time, userNick user, msg) + liftIO $ action logFileHandle return Nothing + +fmtTime :: UTCTime -> String +fmtTime = formatTime defaultTimeLocale "%F %T" + +messageLogger :: MonadMsgHandler m => Message -> m (Maybe Command) +messageLogger ChannelMsg { .. } = withLogFile $ \logFileHandle -> + TF.hprint logFileHandle "[{}] {}: {}\n" $ TF.buildParams (fmtTime msgTime, userNick user, msg) + +messageLogger KickMsg { .. } = withLogFile $ \logFileHandle -> + TF.hprint logFileHandle "[{}] ** {} KICKED {} :{}\n" $ + TF.buildParams (fmtTime msgTime, userNick user, kickedNick, msg) + +messageLogger JoinMsg { .. } = withLogFile $ \logFileHandle -> + TF.hprint logFileHandle "[{}] ** {} JOINED\n" $ + TF.buildParams (fmtTime msgTime, userNick user) + +messageLogger PartMsg { .. } = withLogFile $ \logFileHandle -> + TF.hprint logFileHandle "[{}] ** {} PARTED :{}\n" $ + TF.buildParams (fmtTime msgTime, userNick user, msg) + +messageLogger QuitMsg { .. } = withLogFile $ \logFileHandle -> + TF.hprint logFileHandle "[{}] ** {} QUIT :{}\n" $ + TF.buildParams (fmtTime msgTime, userNick user, msg) + +messageLogger NickMsg { .. } = withLogFile $ \logFileHandle -> + TF.hprint logFileHandle "[{}] ** {} CHANGED NICK TO {}\n" $ + TF.buildParams (fmtTime msgTime, userNick user, nick) + messageLogger _ = return Nothing diff --git a/Network/IRC/Types.hs b/Network/IRC/Types.hs index 5e4df74..4196200 100644 --- a/Network/IRC/Types.hs +++ b/Network/IRC/Types.hs @@ -24,18 +24,18 @@ data User = Self | User { userNick :: !Nick, userServer :: !Text } deriving (Show, Eq) data Message = - ChannelMsg { msgTime :: !UTCTime, user :: !User, msg :: !Text, msgLine :: !Text } - | PrivMsg { msgTime :: !UTCTime, user :: !User, msg :: !Text, msgLine :: !Text } - | Ping { msgTime :: !UTCTime, msg :: !Text, msgLine :: !Text } - | JoinMsg { msgTime :: !UTCTime, user :: !User, msgLine :: !Text } - | ModeMsg { msgTime :: !UTCTime, user :: !User, target :: !Text, mode :: !Text + ChannelMsg { msgTime :: !UTCTime, user :: !User, msg :: !Text, msgLine :: !Text } + | PrivMsg { msgTime :: !UTCTime, user :: !User, msg :: !Text, msgLine :: !Text } + | Ping { msgTime :: !UTCTime, msg :: !Text, msgLine :: !Text } + | JoinMsg { msgTime :: !UTCTime, user :: !User, msgLine :: !Text } + | QuitMsg { msgTime :: !UTCTime, user :: !User, msg :: !Text, msgLine :: !Text } + | PartMsg { msgTime :: !UTCTime, user :: !User, msg :: !Text, msgLine :: !Text } + | ModeMsg { msgTime :: !UTCTime, user :: !User, target :: !Text, mode :: !Text , modeArgs :: ![Text], msgLine :: !Text } - | NickMsg { msgTime :: !UTCTime, user :: !User, nick :: !Text, msgLine :: !Text } - | QuitMsg { msgTime :: !UTCTime, user :: !User, msg :: !Text, msgLine :: !Text } - | PartMsg { msgTime :: !UTCTime, user :: !User, msg :: !Text, msgLine :: !Text } - | KickMsg { msgTime :: !UTCTime, user :: !User, kicked :: !Text, msg :: !Text + | NickMsg { msgTime :: !UTCTime, user :: !User, nick :: !Text, msgLine :: !Text } + | KickMsg { msgTime :: !UTCTime, user :: !User, kickedNick :: !Text, msg :: !Text , msgLine :: !Text } - | OtherMsg { msgTime :: !UTCTime, source :: !Text, command :: !Text, target :: !Text + | OtherMsg { msgTime :: !UTCTime, source :: !Text, command :: !Text, target :: !Text , msg :: !Text, msgLine :: !Text } deriving (Show, Eq) diff --git a/config.cfg.template b/config.cfg.template index 5bc2131..1f7cdcc 100644 --- a/config.cfg.template +++ b/config.cfg.template @@ -7,3 +7,7 @@ msghandlers = ["greeter", "welcomer", "songsearch"] songsearch { tinysong_apikey = "xxxyyyzzz" } + +messagelogger { + logdir = "./logs/" +}