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 <grfn@gws.fyi>
This commit is contained in:
eta 2020-07-14 18:37:36 +01:00
parent 7986b93a35
commit 209f8ef79f

View file

@ -11,16 +11,29 @@ import NLP.Corpora.Conll (Tag)
import qualified Data.ByteString as BS import qualified Data.ByteString as BS
import System.Random import System.Random
import System.Envy import System.Envy
import Data.Maybe
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
data Config = Config 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 stock (Show, Eq, Generic)
deriving anyclass (FromEnv)
makeLenses ''Config 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 :: Text -> Bool
stopWord "'s" = True stopWord "'s" = True
stopWord "is" = True stopWord "is" = True
@ -59,8 +72,8 @@ doOwo conf = do
liftIO $ putStrLn $ "rolled " <> show n liftIO $ putStrLn $ "rolled " <> show n
pure $ n == 0 pure $ n == 0
owothiaHandler :: Config -> IORef Bool -> POSTagger Tag -> EventHandler s owothiaHandler :: Config -> Text -> IORef Bool -> POSTagger Tag -> EventHandler s
owothiaHandler conf state tagger = EventHandler Just $ \src ev -> do owothiaHandler conf nick state tagger = EventHandler Just $ \src ev -> do
hasIdentified <- readIORef state hasIdentified <- readIORef state
when (not hasIdentified) $ do when (not hasIdentified) $ do
nickservAuth nickservAuth
@ -82,26 +95,22 @@ owothiaHandler conf state tagger = EventHandler Just $ \src ev -> do
owoMessage m = do owoMessage m = do
mVerb <- liftIO $ randomVerb tagger m mVerb <- liftIO $ randomVerb tagger m
for_ mVerb $ \verb -> send $ Privmsg "##tvl" $ Right $ owo verb 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 nickservAuth = send $ Privmsg "NickServ" $ Right nickservAuthMsg
myNick :: Text main :: IO ()
myNick = "owothia" main = do
conf <- either fail pure =<< decodeEnv
run :: ByteString -> Int -> IO ()
run host port = do
Right conf <- decodeEnv
tagger <- defaultTagger tagger <- defaultTagger
state <- newIORef False state <- newIORef $ not . isJust $ (conf ^. nickservPassword)
let conn = let nick = fromMaybe "owothia" (conf ^. ircNick)
plainConnection host port conn =
plainConnection (conf ^. ircServer) (conf ^. ircPort)
& realname .~ "Owothia Revströwö" & realname .~ "Owothia Revströwö"
& password .~ (conf ^. ircServerPassword)
& logfunc .~ stdoutLogger & logfunc .~ stdoutLogger
cfg = cfg =
defaultInstanceConfig myNick defaultInstanceConfig nick
& channels .~ ["##tvl"] & channels .~ ["##tvl"]
& handlers %~ (owothiaHandler conf state tagger : ) & handlers %~ (owothiaHandler conf nick state tagger : )
runClient conn cfg () runClient conn cfg ()
main :: IO ()
main = run "irc.freenode.net" 6667