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

master
Abhinav Sarkar 9 years ago
parent 0de98e8f18
commit ca6ef0f166
  1. 2
      Setup.hs
  2. 28
      hask-irc-core/hask-irc-core.cabal
  3. 35
      hask-irc-handlers/Network/IRC/Handlers.hs
  4. 0
      hask-irc-handlers/Network/IRC/Handlers/Auth.hs
  5. 0
      hask-irc-handlers/Network/IRC/Handlers/Auth/Types.hs
  6. 41
      hask-irc-handlers/Network/IRC/Handlers/Core.hs
  7. 32
      hask-irc-handlers/Network/IRC/Handlers/Greet.hs
  8. 0
      hask-irc-handlers/Network/IRC/Handlers/MessageLogger.hs
  9. 0
      hask-irc-handlers/Network/IRC/Handlers/NickTracker.hs
  10. 0
      hask-irc-handlers/Network/IRC/Handlers/NickTracker/Types.hs
  11. 0
      hask-irc-handlers/Network/IRC/Handlers/SongSearch.hs
  12. 90
      hask-irc-handlers/hask-irc-handlers.cabal
  13. 0
      hask-irc-runner/Network/IRC/Client.hs
  14. 7
      hask-irc-runner/hask-irc-runner.cabal
  15. 6
      init_sanbox.sh

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

@ -2,7 +2,7 @@
-- documentation, see http://haskell.org/cabal/users-guide/
-- The name of the package.
name: hask-irc
name: hask-irc-core
-- The package version. See the Haskell package versioning policy (PVP)
-- for standards guiding when and how versions should be incremented.
@ -56,41 +56,19 @@ library
build-depends: base >=4.5 && <4.8,
text >=0.11 && <0.12,
mtl >=2.1 && <2.2,
network >=2.3 && <2.5,
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,
transformers >=0.3 && <0.4,
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,
unix >=2.7 && <2.8,
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,
transformers-base >=0.4 && <0.5,
unordered-containers >=0.2 && <0.3
transformers-base >=0.4 && <0.5
exposed-modules: Network.IRC.Types,
Network.IRC.Protocol,
Network.IRC.Util,
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
Network.IRC.Bot
default-language: Haskell2010

@ -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
]

@ -1,11 +1,4 @@
{-# LANGUAGE FlexibleContexts #-}
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
module Network.IRC.Handlers.Core (mkMsgHandler) where
import ClassyPrelude
import Control.Concurrent.Lifted (Chan)
@ -16,12 +9,7 @@ import Data.Time (addUTCTime)
import Network.IRC.Types
import Network.IRC.Util
coreMsgHandlerNames :: [Text]
coreMsgHandlerNames = ["pingpong", "messagelogger", "help"]
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
state <- getCurrentTime >>= newIORef
return . Just $ newMsgHandler { onMessage = pingPong state }
@ -30,15 +18,7 @@ mkMsgHandler _ _ "help" =
onHelp = return $ singletonMap "!help" helpMsg }
where
helpMsg = "Get help. !help or !help <command>"
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
mkMsgHandler _ _ _ = return Nothing
pingPong :: MonadMsgHandler m => IORef UTCTime -> Message -> m (Maybe Command)
pingPong state Message { msgDetails = PingMsg { .. }, .. } = do
@ -59,23 +39,6 @@ pingPong state Message { msgDetails = IdleMsg { .. }, .. }
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 Message { msgDetails = ChannelMsg { .. }, .. }
| "!help" == clean msg = do

@ -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

@ -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

@ -61,12 +61,15 @@ executable hask-irc
-- Other library packages from which modules are imported.
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,
classy-prelude >=0.9 && <1.0,
network >=2.3 && <2.5,
lifted-base >=0.2 && <0.3,
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.
-- hs-source-dirs:

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

Loading…
Cancel
Save