Browse Source

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

Abhinav Sarkar 6 years ago
parent
commit
ca6ef0f166

+ 0
- 2
Setup.hs View File

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

+ 3
- 25
hask-irc-core/hask-irc-core.cabal View File

@@ -2,7 +2,7 @@
2 2
 -- documentation, see http://haskell.org/cabal/users-guide/
3 3
 
4 4
 -- The name of the package.
5
-name:                hask-irc
5
+name:                hask-irc-core
6 6
 
7 7
 -- The package version.  See the Haskell package versioning policy (PVP)
8 8
 -- for standards guiding when and how versions should be incremented.
@@ -56,41 +56,19 @@ library
56 56
   build-depends:       base                 >=4.5     && <4.8,
57 57
                        text                 >=0.11    && <0.12,
58 58
                        mtl                  >=2.1     && <2.2,
59
-                       network              >=2.3     && <2.5,
60 59
                        configurator         >=0.2     && <0.3,
61 60
                        time                 >=1.4     && <1.5,
62
-                       curl-aeson           >=0.0.3   && <0.1,
63
-                       aeson                >=0.6.0.0 && <0.7,
64
-                       HTTP                 >=4000    && <5000,
65
-                       transformers         >=0.3     && <0.4,
66 61
                        classy-prelude       >=0.9     && <1.0,
67 62
                        text-format          >=0.3     && <0.4,
68
-                       filepath             >=1.3     && <1.4,
69
-                       directory            >=1.2     && <1.3,
70 63
                        lifted-base          >=0.2     && <0.3,
71
-                       unix                 >=2.7     && <2.8,
72
-                       convertible          >=1.1     && <1.2,
73 64
                        hslogger             >=1.2     && <1.3,
74 65
                        hslogger-template    >=2.0     && <2.1,
75
-                       ixset                >=1.0     && <1.1,
76
-                       acid-state           >=0.12    && <0.13,
77
-                       safecopy             >=0.8     && <0.9,
78
-                       uuid                 >=1.3     && <1.4,
79
-                       transformers-base    >=0.4     && <0.5,
80
-                       unordered-containers >=0.2     && <0.3
66
+                       transformers-base    >=0.4     && <0.5
81 67
 
82 68
   exposed-modules:     Network.IRC.Types,
83 69
                        Network.IRC.Protocol,
84 70
                        Network.IRC.Util,
85
-                       Network.IRC.Bot,
86
-                       Network.IRC.Client,
87
-                       Network.IRC.Handlers,
88
-                       Network.IRC.Handlers.Auth,
89
-                       Network.IRC.Handlers.Auth.Types,
90
-                       Network.IRC.Handlers.MessageLogger,
91
-                       Network.IRC.Handlers.NickTracker,
92
-                       Network.IRC.Handlers.NickTracker.Types,
93
-                       Network.IRC.Handlers.SongSearch
71
+                       Network.IRC.Bot
94 72
 
95 73
   default-language:    Haskell2010
96 74
 

+ 35
- 0
hask-irc-handlers/Network/IRC/Handlers.hs View File

@@ -0,0 +1,35 @@
1
+{-# LANGUAGE FlexibleContexts #-}
2
+
3
+module Network.IRC.Handlers (coreMsgHandlerNames, mkMsgHandler) where
4
+
5
+import qualified Network.IRC.Handlers.Auth          as Auth
6
+import qualified Network.IRC.Handlers.Core          as Core
7
+import qualified Network.IRC.Handlers.Greet         as Greet
8
+import qualified Network.IRC.Handlers.MessageLogger as Logger
9
+import qualified Network.IRC.Handlers.NickTracker   as NickTracker
10
+import qualified Network.IRC.Handlers.SongSearch    as SongSearch
11
+
12
+import ClassyPrelude
13
+import Control.Concurrent.Lifted  (Chan)
14
+
15
+import Network.IRC.Types
16
+
17
+coreMsgHandlerNames :: [Text]
18
+coreMsgHandlerNames = ["pingpong", "help"]
19
+
20
+mkMsgHandler :: BotConfig -> Chan SomeEvent -> MsgHandlerName -> IO (Maybe MsgHandler)
21
+mkMsgHandler botConfig eventChan name =
22
+  flip (`foldM` Nothing) handlerMakers $ \finalHandler handler ->
23
+    case finalHandler of
24
+      Just _  -> return finalHandler
25
+      Nothing -> handler botConfig eventChan name
26
+
27
+  where
28
+    handlerMakers = [
29
+        Auth.mkMsgHandler
30
+      , Core.mkMsgHandler
31
+      , Greet.mkMsgHandler
32
+      , Logger.mkMsgHandler
33
+      , NickTracker.mkMsgHandler
34
+      , SongSearch.mkMsgHandler
35
+      ]

hask-irc-core/Network/IRC/Handlers/Auth.hs → hask-irc-handlers/Network/IRC/Handlers/Auth.hs View File


hask-irc-core/Network/IRC/Handlers/Auth/Types.hs → hask-irc-handlers/Network/IRC/Handlers/Auth/Types.hs View File


hask-irc-core/Network/IRC/Handlers.hs → hask-irc-handlers/Network/IRC/Handlers/Core.hs View File

@@ -1,11 +1,4 @@
1
-{-# LANGUAGE FlexibleContexts #-}
2
-
3
-module Network.IRC.Handlers (coreMsgHandlerNames, mkMsgHandler) where
4
-
5
-import qualified Network.IRC.Handlers.MessageLogger as Logger
6
-import qualified Network.IRC.Handlers.SongSearch    as SongSearch
7
-import qualified Network.IRC.Handlers.Auth          as Auth
8
-import qualified Network.IRC.Handlers.NickTracker   as NickTracker
1
+module Network.IRC.Handlers.Core (mkMsgHandler) where
9 2
 
10 3
 import ClassyPrelude
11 4
 import Control.Concurrent.Lifted  (Chan)
@@ -16,12 +9,7 @@ import Data.Time                  (addUTCTime)
16 9
 import Network.IRC.Types
17 10
 import Network.IRC.Util
18 11
 
19
-coreMsgHandlerNames :: [Text]
20
-coreMsgHandlerNames = ["pingpong", "messagelogger", "help"]
21
-
22 12
 mkMsgHandler :: BotConfig -> Chan SomeEvent -> MsgHandlerName -> IO (Maybe MsgHandler)
23
-mkMsgHandler _ _ "greeter"  = return . Just $ newMsgHandler { onMessage = greeter }
24
-mkMsgHandler _ _ "welcomer" = return . Just $ newMsgHandler { onMessage = welcomer }
25 13
 mkMsgHandler _ _ "pingpong" = do
26 14
   state <- getCurrentTime >>= newIORef
27 15
   return . Just $ newMsgHandler { onMessage = pingPong state }
@@ -30,15 +18,7 @@ mkMsgHandler _ _ "help"     =
30 18
                                     onHelp    = return $ singletonMap "!help" helpMsg }
31 19
   where
32 20
     helpMsg = "Get help. !help or !help <command>"
33
-
34
-mkMsgHandler botConfig eventChan name =
35
-  flip (`foldM` Nothing) [ Logger.mkMsgHandler
36
-                         , SongSearch.mkMsgHandler
37
-                         , Auth.mkMsgHandler
38
-                         , NickTracker.mkMsgHandler ] $ \finalHandler handler ->
39
-    case finalHandler of
40
-      Just _  -> return finalHandler
41
-      Nothing -> handler botConfig eventChan name
21
+mkMsgHandler _ _ _          = return Nothing
42 22
 
43 23
 pingPong :: MonadMsgHandler m => IORef UTCTime -> Message -> m (Maybe Command)
44 24
 pingPong state Message { msgDetails = PingMsg { .. }, .. } = do
@@ -59,23 +39,6 @@ pingPong state Message { msgDetails = IdleMsg { .. }, .. }
59 39
 
60 40
 pingPong _ _ = return Nothing
61 41
 
62
-greeter ::  MonadMsgHandler m => Message -> m (Maybe Command)
63
-greeter Message { msgDetails = ChannelMsg { .. }, .. } =
64
-  return . map (ChannelMsgReply . (++ userNick user) . (++ " ")) . find (== clean msg) $ greetings
65
-  where
66
-    greetings = [ "hi", "hello", "hey", "sup", "bye"
67
-                , "good morning", "good evening", "good night" ]
68
-greeter _ = return Nothing
69
-
70
-welcomer :: MonadMsgHandler m => Message -> m (Maybe Command)
71
-welcomer Message { msgDetails = JoinMsg { .. }, .. } = do
72
-  BotConfig { .. } <- ask
73
-  if userNick user /= botNick
74
-    then return . Just . ChannelMsgReply $ "welcome back " ++ userNick user
75
-    else return Nothing
76
-
77
-welcomer _ = return Nothing
78
-
79 42
 help :: MonadMsgHandler m => Message -> m (Maybe Command)
80 43
 help Message { msgDetails = ChannelMsg { .. }, .. }
81 44
   | "!help" == clean msg = do

+ 32
- 0
hask-irc-handlers/Network/IRC/Handlers/Greet.hs View File

@@ -0,0 +1,32 @@
1
+module Network.IRC.Handlers.Greet (mkMsgHandler) where
2
+
3
+import ClassyPrelude
4
+import Control.Concurrent.Lifted  (Chan)
5
+import Control.Monad.Reader       (ask)
6
+
7
+import Network.IRC.Types
8
+import Network.IRC.Util
9
+
10
+mkMsgHandler :: BotConfig -> Chan SomeEvent -> MsgHandlerName -> IO (Maybe MsgHandler)
11
+mkMsgHandler _ _ "greeter"  = return . Just $ newMsgHandler { onMessage = greeter }
12
+mkMsgHandler _ _ "welcomer" = return . Just $ newMsgHandler { onMessage = welcomer }
13
+mkMsgHandler _ _ _          = return Nothing
14
+
15
+greeter ::  MonadMsgHandler m => Message -> m (Maybe Command)
16
+greeter Message { msgDetails = ChannelMsg { .. }, .. } =
17
+  return . map (ChannelMsgReply . (++ userNick user) . (++ " ")) . find (== clean msg) $ greetings
18
+  where
19
+    greetings = [ "hi", "hello", "hey", "sup", "bye"
20
+                , "good morning", "good evening", "good night" ]
21
+greeter _ = return Nothing
22
+
23
+welcomer :: MonadMsgHandler m => Message -> m (Maybe Command)
24
+welcomer Message { msgDetails = JoinMsg { .. }, .. } = do
25
+  BotConfig { .. } <- ask
26
+  if userNick user /= botNick
27
+    then return . Just . ChannelMsgReply $ "welcome back " ++ userNick user
28
+    else return Nothing
29
+
30
+welcomer _ = return Nothing
31
+
32
+

hask-irc-core/Network/IRC/Handlers/MessageLogger.hs → hask-irc-handlers/Network/IRC/Handlers/MessageLogger.hs View File


hask-irc-core/Network/IRC/Handlers/NickTracker.hs → hask-irc-handlers/Network/IRC/Handlers/NickTracker.hs View File


hask-irc-core/Network/IRC/Handlers/NickTracker/Types.hs → hask-irc-handlers/Network/IRC/Handlers/NickTracker/Types.hs View File


hask-irc-core/Network/IRC/Handlers/SongSearch.hs → hask-irc-handlers/Network/IRC/Handlers/SongSearch.hs View File


+ 90
- 0
hask-irc-handlers/hask-irc-handlers.cabal View File

@@ -0,0 +1,90 @@
1
+-- Initial hask-irc.cabal generated by cabal init.  For further
2
+-- documentation, see http://haskell.org/cabal/users-guide/
3
+
4
+-- The name of the package.
5
+name:                hask-irc-handlers
6
+
7
+-- The package version.  See the Haskell package versioning policy (PVP)
8
+-- for standards guiding when and how versions should be incremented.
9
+-- http://www.haskell.org/haskellwiki/Package_versioning_policy
10
+-- PVP summary:      +-+------- breaking API changes
11
+--                   | | +----- non-breaking API additions
12
+--                   | | | +--- code changes with no API change
13
+version:             0.1.0
14
+
15
+-- A short (one-line) description of the package.
16
+synopsis:            A simple extensible IRC bot
17
+
18
+-- A longer description of the package.
19
+-- description:
20
+
21
+-- URL for the project homepage or repository.
22
+homepage:            https://github.com/abhin4v/hask-irc
23
+
24
+-- The license under which the package is released.
25
+license:             Apache-2.0
26
+
27
+-- The file containing the license text.
28
+license-file:        ../LICENSE
29
+
30
+-- The package author(s).
31
+author:              Abhinav Sarkar
32
+
33
+-- An email address to which users can send suggestions, bug reports, and
34
+-- patches.
35
+maintainer:          abhinav@abhinavsarkar.net
36
+
37
+-- A copyright notice.
38
+-- copyright:
39
+
40
+category:            Network, IRC
41
+
42
+build-type:          Simple
43
+
44
+-- Extra files to be distributed with the package, such as examples or a
45
+-- README.
46
+-- extra-source-files:
47
+
48
+-- Constraint on the version of Cabal needed to build this package.
49
+cabal-version:       >=1.10
50
+
51
+library
52
+  default-extensions:  NoImplicitPrelude, OverloadedStrings, RecordWildCards, ScopedTypeVariables,
53
+                       BangPatterns, TupleSections, NamedFieldPuns, GeneralizedNewtypeDeriving,
54
+                       DeriveDataTypeable
55
+
56
+  build-depends:       base                 >=4.5     && <4.8,
57
+                       hask-irc-core        ==0.1.0,
58
+                       text                 >=0.11    && <0.12,
59
+                       mtl                  >=2.1     && <2.2,
60
+                       configurator         >=0.2     && <0.3,
61
+                       time                 >=1.4     && <1.5,
62
+                       curl-aeson           >=0.0.3   && <0.1,
63
+                       aeson                >=0.6.0.0 && <0.7,
64
+                       HTTP                 >=4000    && <5000,
65
+                       classy-prelude       >=0.9     && <1.0,
66
+                       text-format          >=0.3     && <0.4,
67
+                       filepath             >=1.3     && <1.4,
68
+                       directory            >=1.2     && <1.3,
69
+                       lifted-base          >=0.2     && <0.3,
70
+                       convertible          >=1.1     && <1.2,
71
+                       hslogger             >=1.2     && <1.3,
72
+                       hslogger-template    >=2.0     && <2.1,
73
+                       ixset                >=1.0     && <1.1,
74
+                       acid-state           >=0.12    && <0.13,
75
+                       safecopy             >=0.8     && <0.9,
76
+                       uuid                 >=1.3     && <1.4
77
+
78
+  exposed-modules:     Network.IRC.Handlers,
79
+                       Network.IRC.Handlers.Auth,
80
+                       Network.IRC.Handlers.Auth.Types,
81
+                       Network.IRC.Handlers.Core,
82
+                       Network.IRC.Handlers.Greet,
83
+                       Network.IRC.Handlers.MessageLogger,
84
+                       Network.IRC.Handlers.NickTracker,
85
+                       Network.IRC.Handlers.NickTracker.Types,
86
+                       Network.IRC.Handlers.SongSearch
87
+
88
+  default-language:    Haskell2010
89
+
90
+  ghc-options:         -Wall -fno-warn-unused-do-bind -O2 -funbox-strict-fields -fno-warn-orphans

hask-irc-core/Network/IRC/Client.hs → hask-irc-runner/Network/IRC/Client.hs View File


+ 5
- 2
hask-irc-runner/hask-irc-runner.cabal View File

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

+ 6
- 0
init_sanbox.sh View File

@@ -6,9 +6,15 @@ cd hask-irc-core
6 6
 cabal sandbox init --sandbox=../.cabal-sandbox
7 7
 cd ..
8 8
 
9
+cd hask-irc-handlers
10
+cabal sandbox init --sandbox=../.cabal-sandbox
11
+cabal sandbox add-source ../hask-irc-core/
12
+cd ..
13
+
9 14
 cd hask-irc-runner
10 15
 cabal sandbox init --sandbox=../.cabal-sandbox
11 16
 cabal sandbox add-source ../hask-irc-core/
17
+cabal sandbox add-source ../hask-irc-handlers/
12 18
 cd ..
13 19
 
14 20
 cd hask-irc-core

Loading…
Cancel
Save