Moved out handlers from the core module to a module of their own

master
Abhinav Sarkar 2014-05-25 18:13:52 +05:30
parent 0de98e8f18
commit ca6ef0f166
15 changed files with 173 additions and 68 deletions

View File

@ -1,2 +0,0 @@
import Distribution.Simple
main = defaultMain

View File

@ -2,7 +2,7 @@
-- documentation, see http://haskell.org/cabal/users-guide/ -- documentation, see http://haskell.org/cabal/users-guide/
-- The name of the package. -- The name of the package.
name: hask-irc name: hask-irc-core
-- The package version. See the Haskell package versioning policy (PVP) -- The package version. See the Haskell package versioning policy (PVP)
-- for standards guiding when and how versions should be incremented. -- for standards guiding when and how versions should be incremented.
@ -56,41 +56,19 @@ library
build-depends: base >=4.5 && <4.8, build-depends: base >=4.5 && <4.8,
text >=0.11 && <0.12, text >=0.11 && <0.12,
mtl >=2.1 && <2.2, mtl >=2.1 && <2.2,
network >=2.3 && <2.5,
configurator >=0.2 && <0.3, configurator >=0.2 && <0.3,
time >=1.4 && <1.5, time >=1.4 && <1.5,
curl-aeson >=0.0.3 && <0.1,
aeson >=0.6.0.0 && <0.7,
HTTP >=4000 && <5000,
transformers >=0.3 && <0.4,
classy-prelude >=0.9 && <1.0, classy-prelude >=0.9 && <1.0,
text-format >=0.3 && <0.4, text-format >=0.3 && <0.4,
filepath >=1.3 && <1.4,
directory >=1.2 && <1.3,
lifted-base >=0.2 && <0.3, lifted-base >=0.2 && <0.3,
unix >=2.7 && <2.8,
convertible >=1.1 && <1.2,
hslogger >=1.2 && <1.3, hslogger >=1.2 && <1.3,
hslogger-template >=2.0 && <2.1, hslogger-template >=2.0 && <2.1,
ixset >=1.0 && <1.1, transformers-base >=0.4 && <0.5
acid-state >=0.12 && <0.13,
safecopy >=0.8 && <0.9,
uuid >=1.3 && <1.4,
transformers-base >=0.4 && <0.5,
unordered-containers >=0.2 && <0.3
exposed-modules: Network.IRC.Types, exposed-modules: Network.IRC.Types,
Network.IRC.Protocol, Network.IRC.Protocol,
Network.IRC.Util, Network.IRC.Util,
Network.IRC.Bot, Network.IRC.Bot
Network.IRC.Client,
Network.IRC.Handlers,
Network.IRC.Handlers.Auth,
Network.IRC.Handlers.Auth.Types,
Network.IRC.Handlers.MessageLogger,
Network.IRC.Handlers.NickTracker,
Network.IRC.Handlers.NickTracker.Types,
Network.IRC.Handlers.SongSearch
default-language: Haskell2010 default-language: Haskell2010

View File

@ -0,0 +1,35 @@
{-# LANGUAGE FlexibleContexts #-}
module Network.IRC.Handlers (coreMsgHandlerNames, mkMsgHandler) where
import qualified Network.IRC.Handlers.Auth as Auth
import qualified Network.IRC.Handlers.Core as Core
import qualified Network.IRC.Handlers.Greet as Greet
import qualified Network.IRC.Handlers.MessageLogger as Logger
import qualified Network.IRC.Handlers.NickTracker as NickTracker
import qualified Network.IRC.Handlers.SongSearch as SongSearch
import ClassyPrelude
import Control.Concurrent.Lifted (Chan)
import Network.IRC.Types
coreMsgHandlerNames :: [Text]
coreMsgHandlerNames = ["pingpong", "help"]
mkMsgHandler :: BotConfig -> Chan SomeEvent -> MsgHandlerName -> IO (Maybe MsgHandler)
mkMsgHandler botConfig eventChan name =
flip (`foldM` Nothing) handlerMakers $ \finalHandler handler ->
case finalHandler of
Just _ -> return finalHandler
Nothing -> handler botConfig eventChan name
where
handlerMakers = [
Auth.mkMsgHandler
, Core.mkMsgHandler
, Greet.mkMsgHandler
, Logger.mkMsgHandler
, NickTracker.mkMsgHandler
, SongSearch.mkMsgHandler
]

View File

@ -1,11 +1,4 @@
{-# LANGUAGE FlexibleContexts #-} module Network.IRC.Handlers.Core (mkMsgHandler) where
module Network.IRC.Handlers (coreMsgHandlerNames, mkMsgHandler) where
import qualified Network.IRC.Handlers.MessageLogger as Logger
import qualified Network.IRC.Handlers.SongSearch as SongSearch
import qualified Network.IRC.Handlers.Auth as Auth
import qualified Network.IRC.Handlers.NickTracker as NickTracker
import ClassyPrelude import ClassyPrelude
import Control.Concurrent.Lifted (Chan) import Control.Concurrent.Lifted (Chan)
@ -16,12 +9,7 @@ import Data.Time (addUTCTime)
import Network.IRC.Types import Network.IRC.Types
import Network.IRC.Util import Network.IRC.Util
coreMsgHandlerNames :: [Text]
coreMsgHandlerNames = ["pingpong", "messagelogger", "help"]
mkMsgHandler :: BotConfig -> Chan SomeEvent -> MsgHandlerName -> IO (Maybe MsgHandler) mkMsgHandler :: BotConfig -> Chan SomeEvent -> MsgHandlerName -> IO (Maybe MsgHandler)
mkMsgHandler _ _ "greeter" = return . Just $ newMsgHandler { onMessage = greeter }
mkMsgHandler _ _ "welcomer" = return . Just $ newMsgHandler { onMessage = welcomer }
mkMsgHandler _ _ "pingpong" = do mkMsgHandler _ _ "pingpong" = do
state <- getCurrentTime >>= newIORef state <- getCurrentTime >>= newIORef
return . Just $ newMsgHandler { onMessage = pingPong state } return . Just $ newMsgHandler { onMessage = pingPong state }
@ -30,15 +18,7 @@ mkMsgHandler _ _ "help" =
onHelp = return $ singletonMap "!help" helpMsg } onHelp = return $ singletonMap "!help" helpMsg }
where where
helpMsg = "Get help. !help or !help <command>" helpMsg = "Get help. !help or !help <command>"
mkMsgHandler _ _ _ = return Nothing
mkMsgHandler botConfig eventChan name =
flip (`foldM` Nothing) [ Logger.mkMsgHandler
, SongSearch.mkMsgHandler
, Auth.mkMsgHandler
, NickTracker.mkMsgHandler ] $ \finalHandler handler ->
case finalHandler of
Just _ -> return finalHandler
Nothing -> handler botConfig eventChan name
pingPong :: MonadMsgHandler m => IORef UTCTime -> Message -> m (Maybe Command) pingPong :: MonadMsgHandler m => IORef UTCTime -> Message -> m (Maybe Command)
pingPong state Message { msgDetails = PingMsg { .. }, .. } = do pingPong state Message { msgDetails = PingMsg { .. }, .. } = do
@ -59,23 +39,6 @@ pingPong state Message { msgDetails = IdleMsg { .. }, .. }
pingPong _ _ = return Nothing pingPong _ _ = return Nothing
greeter :: MonadMsgHandler m => Message -> m (Maybe Command)
greeter Message { msgDetails = ChannelMsg { .. }, .. } =
return . map (ChannelMsgReply . (++ userNick user) . (++ " ")) . find (== clean msg) $ greetings
where
greetings = [ "hi", "hello", "hey", "sup", "bye"
, "good morning", "good evening", "good night" ]
greeter _ = return Nothing
welcomer :: MonadMsgHandler m => Message -> m (Maybe Command)
welcomer Message { msgDetails = JoinMsg { .. }, .. } = do
BotConfig { .. } <- ask
if userNick user /= botNick
then return . Just . ChannelMsgReply $ "welcome back " ++ userNick user
else return Nothing
welcomer _ = return Nothing
help :: MonadMsgHandler m => Message -> m (Maybe Command) help :: MonadMsgHandler m => Message -> m (Maybe Command)
help Message { msgDetails = ChannelMsg { .. }, .. } help Message { msgDetails = ChannelMsg { .. }, .. }
| "!help" == clean msg = do | "!help" == clean msg = do

View File

@ -0,0 +1,32 @@
module Network.IRC.Handlers.Greet (mkMsgHandler) where
import ClassyPrelude
import Control.Concurrent.Lifted (Chan)
import Control.Monad.Reader (ask)
import Network.IRC.Types
import Network.IRC.Util
mkMsgHandler :: BotConfig -> Chan SomeEvent -> MsgHandlerName -> IO (Maybe MsgHandler)
mkMsgHandler _ _ "greeter" = return . Just $ newMsgHandler { onMessage = greeter }
mkMsgHandler _ _ "welcomer" = return . Just $ newMsgHandler { onMessage = welcomer }
mkMsgHandler _ _ _ = return Nothing
greeter :: MonadMsgHandler m => Message -> m (Maybe Command)
greeter Message { msgDetails = ChannelMsg { .. }, .. } =
return . map (ChannelMsgReply . (++ userNick user) . (++ " ")) . find (== clean msg) $ greetings
where
greetings = [ "hi", "hello", "hey", "sup", "bye"
, "good morning", "good evening", "good night" ]
greeter _ = return Nothing
welcomer :: MonadMsgHandler m => Message -> m (Maybe Command)
welcomer Message { msgDetails = JoinMsg { .. }, .. } = do
BotConfig { .. } <- ask
if userNick user /= botNick
then return . Just . ChannelMsgReply $ "welcome back " ++ userNick user
else return Nothing
welcomer _ = return Nothing

View File

@ -0,0 +1,90 @@
-- Initial hask-irc.cabal generated by cabal init. For further
-- documentation, see http://haskell.org/cabal/users-guide/
-- The name of the package.
name: hask-irc-handlers
-- The package version. See the Haskell package versioning policy (PVP)
-- for standards guiding when and how versions should be incremented.
-- http://www.haskell.org/haskellwiki/Package_versioning_policy
-- PVP summary: +-+------- breaking API changes
-- | | +----- non-breaking API additions
-- | | | +--- code changes with no API change
version: 0.1.0
-- A short (one-line) description of the package.
synopsis: A simple extensible IRC bot
-- A longer description of the package.
-- description:
-- URL for the project homepage or repository.
homepage: https://github.com/abhin4v/hask-irc
-- The license under which the package is released.
license: Apache-2.0
-- The file containing the license text.
license-file: ../LICENSE
-- The package author(s).
author: Abhinav Sarkar
-- An email address to which users can send suggestions, bug reports, and
-- patches.
maintainer: abhinav@abhinavsarkar.net
-- A copyright notice.
-- copyright:
category: Network, IRC
build-type: Simple
-- Extra files to be distributed with the package, such as examples or a
-- README.
-- extra-source-files:
-- Constraint on the version of Cabal needed to build this package.
cabal-version: >=1.10
library
default-extensions: NoImplicitPrelude, OverloadedStrings, RecordWildCards, ScopedTypeVariables,
BangPatterns, TupleSections, NamedFieldPuns, GeneralizedNewtypeDeriving,
DeriveDataTypeable
build-depends: base >=4.5 && <4.8,
hask-irc-core ==0.1.0,
text >=0.11 && <0.12,
mtl >=2.1 && <2.2,
configurator >=0.2 && <0.3,
time >=1.4 && <1.5,
curl-aeson >=0.0.3 && <0.1,
aeson >=0.6.0.0 && <0.7,
HTTP >=4000 && <5000,
classy-prelude >=0.9 && <1.0,
text-format >=0.3 && <0.4,
filepath >=1.3 && <1.4,
directory >=1.2 && <1.3,
lifted-base >=0.2 && <0.3,
convertible >=1.1 && <1.2,
hslogger >=1.2 && <1.3,
hslogger-template >=2.0 && <2.1,
ixset >=1.0 && <1.1,
acid-state >=0.12 && <0.13,
safecopy >=0.8 && <0.9,
uuid >=1.3 && <1.4
exposed-modules: Network.IRC.Handlers,
Network.IRC.Handlers.Auth,
Network.IRC.Handlers.Auth.Types,
Network.IRC.Handlers.Core,
Network.IRC.Handlers.Greet,
Network.IRC.Handlers.MessageLogger,
Network.IRC.Handlers.NickTracker,
Network.IRC.Handlers.NickTracker.Types,
Network.IRC.Handlers.SongSearch
default-language: Haskell2010
ghc-options: -Wall -fno-warn-unused-do-bind -O2 -funbox-strict-fields -fno-warn-orphans

View File

@ -61,12 +61,15 @@ executable hask-irc
-- Other library packages from which modules are imported. -- Other library packages from which modules are imported.
build-depends: base >=4.5 && <4.8, build-depends: base >=4.5 && <4.8,
hask-irc ==0.1.0, hask-irc-core ==0.1.0,
hask-irc-handlers ==0.1.0,
configurator >=0.2 && <0.3, configurator >=0.2 && <0.3,
classy-prelude >=0.9 && <1.0, classy-prelude >=0.9 && <1.0,
network >=2.3 && <2.5,
lifted-base >=0.2 && <0.3, lifted-base >=0.2 && <0.3,
unix >=2.7 && <2.8, unix >=2.7 && <2.8,
hslogger >=1.2 && <1.3 hslogger >=1.2 && <1.3,
hslogger-template >=2.0 && <2.1
-- Directories containing source files. -- Directories containing source files.
-- hs-source-dirs: -- hs-source-dirs:

View File

@ -6,9 +6,15 @@ cd hask-irc-core
cabal sandbox init --sandbox=../.cabal-sandbox cabal sandbox init --sandbox=../.cabal-sandbox
cd .. cd ..
cd hask-irc-handlers
cabal sandbox init --sandbox=../.cabal-sandbox
cabal sandbox add-source ../hask-irc-core/
cd ..
cd hask-irc-runner cd hask-irc-runner
cabal sandbox init --sandbox=../.cabal-sandbox cabal sandbox init --sandbox=../.cabal-sandbox
cabal sandbox add-source ../hask-irc-core/ cabal sandbox add-source ../hask-irc-core/
cabal sandbox add-source ../hask-irc-handlers/
cd .. cd ..
cd hask-irc-core cd hask-irc-core