about summary refs log tree commit diff
path: root/users
diff options
context:
space:
mode:
Diffstat (limited to 'users')
-rw-r--r--users/glittershark/owothia/src/Main.hs49
1 files changed, 29 insertions, 20 deletions
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