From 209f8ef79f7844b7e883cf145515a16e44487ed6 Mon Sep 17 00:00:00 2001 From: eta Date: Tue, 14 Jul 2020 18:37:36 +0100 Subject: feat(owothia): make more things configurable, better - You can now configure server, port, password, nickserv password, and nick. - eta doing haskell: https://theta.eu.org/lx/selif/8pu34rll.jpg - (props to grfn for basically writing half this CL) Change-Id: I128ae01f5879df730a6404402ef16bf1f3429f98 Reviewed-on: https://cl.tvl.fyi/c/depot/+/1164 Tested-by: BuildkiteCI Reviewed-by: glittershark --- users/glittershark/owothia/src/Main.hs | 49 ++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 20 deletions(-) (limited to 'users/glittershark/owothia/src') diff --git a/users/glittershark/owothia/src/Main.hs b/users/glittershark/owothia/src/Main.hs index f0212881dbc1..5e0f18a924c8 100644 --- a/users/glittershark/owothia/src/Main.hs +++ b/users/glittershark/owothia/src/Main.hs @@ -11,16 +11,29 @@ import NLP.Corpora.Conll (Tag) import qualified Data.ByteString as BS import System.Random import System.Envy +import Data.Maybe -------------------------------------------------------------------------------- data Config = Config - { _nickservPassword :: Text - , _owoChance :: Int + { _owoChance :: Int + , _ircServer :: ByteString + , _ircPort :: Int + , _ircServerPassword :: Maybe Text + , _nickservPassword :: Maybe Text + , _ircNick :: Maybe Text } deriving stock (Show, Eq, Generic) - deriving anyclass (FromEnv) makeLenses ''Config +instance FromEnv Config where + fromEnv _ = + Config <$> env "OWO_CHANCE" + <*> env "IRC_SERVER" + <*> env "IRC_PORT" + <*> envMaybe "IRC_SERVER_PASSWORD" + <*> envMaybe "NICKSERV_PASSWORD" + <*> envMaybe "IRC_NICK" + stopWord :: Text -> Bool stopWord "'s" = True stopWord "is" = True @@ -59,8 +72,8 @@ doOwo conf = do liftIO $ putStrLn $ "rolled " <> show n pure $ n == 0 -owothiaHandler :: Config -> IORef Bool -> POSTagger Tag -> EventHandler s -owothiaHandler conf state tagger = EventHandler Just $ \src ev -> do +owothiaHandler :: Config -> Text -> IORef Bool -> POSTagger Tag -> EventHandler s +owothiaHandler conf nick state tagger = EventHandler Just $ \src ev -> do hasIdentified <- readIORef state when (not hasIdentified) $ do nickservAuth @@ -82,26 +95,22 @@ owothiaHandler conf state tagger = EventHandler Just $ \src ev -> do owoMessage m = do mVerb <- liftIO $ randomVerb tagger m for_ mVerb $ \verb -> send $ Privmsg "##tvl" $ Right $ owo verb - nickservAuthMsg = "IDENTIFY " <> myNick <> " " <> conf ^. nickservPassword + nickservAuthMsg = "IDENTIFY " <> nick <> " " <> fromJust (conf ^. nickservPassword) nickservAuth = send $ Privmsg "NickServ" $ Right nickservAuthMsg -myNick :: Text -myNick = "owothia" - -run :: ByteString -> Int -> IO () -run host port = do - Right conf <- decodeEnv +main :: IO () +main = do + conf <- either fail pure =<< decodeEnv tagger <- defaultTagger - state <- newIORef False - let conn = - plainConnection host port + state <- newIORef $ not . isJust $ (conf ^. nickservPassword) + let nick = fromMaybe "owothia" (conf ^. ircNick) + conn = + plainConnection (conf ^. ircServer) (conf ^. ircPort) & realname .~ "Owothia Revströwö" + & password .~ (conf ^. ircServerPassword) & logfunc .~ stdoutLogger cfg = - defaultInstanceConfig myNick + defaultInstanceConfig nick & channels .~ ["##tvl"] - & handlers %~ (owothiaHandler conf state tagger : ) + & handlers %~ (owothiaHandler conf nick state tagger : ) runClient conn cfg () - -main :: IO () -main = run "irc.freenode.net" 6667 -- cgit 1.4.1