aboutsummaryrefslogtreecommitdiff
path: root/src/IRC.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/IRC.hs')
-rw-r--r--src/IRC.hs62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/IRC.hs b/src/IRC.hs
new file mode 100644
index 0000000..36cb1c3
--- /dev/null
+++ b/src/IRC.hs
@@ -0,0 +1,62 @@
+module IRC where
+
+import Control.Monad (forM_)
+import Control.Monad.IO.Class (liftIO)
+import qualified Data.ByteString.Char8 as Char8
+import Data.Char
+import Network.IRC.Client
+-- import Network.IRC.Client.Events
+import Lens.Micro
+import Data.Text (Text)
+import qualified Data.Text as T
+
+
+connectIRC :: (Text -> Bool) -> (Text -> IO [Text]) -> IO ()
+connectIRC commandDetect msgFun = do
+ pass <- readFile "irc-password.txt"
+ let trim = reverse . dropWhile isSpace . reverse . dropWhile isSpace
+
+ let conn = tlsConnection (WithDefaultConfig (Char8.pack "irc.libera.chat") 6697)
+ & username .~ T.pack "yahb2"
+ & realname .~ T.pack "Bot operated by tomsmeding"
+ & password .~ Just (T.pack (trim pass))
+ & logfunc .~ stdoutLogger
+
+ let cfg = defaultInstanceConfig (T.pack "yahb2")
+ & handlers .~
+ (-- some of the standard handlers
+ [pingHandler
+ ,kickHandler
+ ,ctcpPingHandler
+ ,ctcpTimeHandler
+ ,ctcpVersionHandler
+ ,welcomeNick
+ ,joinHandler]
+ ++
+ -- our custom handlers
+ [noticeHandler
+ ,privmsgHandler commandDetect msgFun]
+ )
+
+ runClient conn cfg ()
+
+noticeHandler :: EventHandler s
+noticeHandler = EventHandler
+ (\ev -> case ev ^. message of
+ Notice _ (Right text)
+ | T.pack "now identified for" `T.isInfixOf` text
+ -> Just ()
+ _ -> Nothing)
+ (\_ () -> do
+ send $ Join (T.pack "#haskell"))
+
+privmsgHandler :: (Text -> Bool) -> (Text -> IO [Text]) -> EventHandler s
+privmsgHandler commandDetect msgFun = EventHandler
+ (\ev -> case ev ^. message of
+ Privmsg target (Right text)
+ | commandDetect text -> Just (target, text)
+ _ -> Nothing)
+ (\_ (target, text) -> do
+ msgs <- liftIO $ msgFun text
+ forM_ msgs $ \msg ->
+ send $ Privmsg target (Right msg))