about summary refs log tree commit diff
path: root/fun
diff options
context:
space:
mode:
authormulti <depot@in-addr.xyz>2021-05-22T13·42+0100
committertazjin <mail@tazj.in>2021-05-22T15·10+0000
commitfd6ce088bac05eb8f826bc189a6bcacd189a01c1 (patch)
treef7fd1c09bf66bbed6c8b5cf88a9a77038ec4802d /fun
parent48b052c1e485e97d7e77abdef44b69b4967faada (diff)
feat(fun/owothia): add configurable join channels. r/2601
owothia is hardcoded to join ##tvl, which is a remnant of when TVL was on
freenode, and on hackint the IRC channel is single-hash #tvl instead. Instead
of hardcoding another channel name, let's make this configurable, so we don't
need to recompile owothia for every different channel we want her in.

It's now possible to set IRC_CHANNELS in owothia's environment to '["#foo",
"#bar"]' to make her join both #foo and #bar automatically.

Additionally IRC_IDENT can now be set to configure owothia's ident,
which is required for ZNC compatibility.

Change-Id: I0fc0856f4ea35f59255b76ae0e594325f18ef993
Reviewed-on: https://cl.tvl.fyi/c/depot/+/3130
Tested-by: BuildkiteCI
Reviewed-by: sterni <sternenseemann@systemli.org>
Diffstat (limited to 'fun')
-rw-r--r--fun/owothia/src/Main.hs28
1 files changed, 20 insertions, 8 deletions
diff --git a/fun/owothia/src/Main.hs b/fun/owothia/src/Main.hs
index 65578f258c1a..3bf5e51dba2f 100644
--- a/fun/owothia/src/Main.hs
+++ b/fun/owothia/src/Main.hs
@@ -12,7 +12,9 @@ import           NLP.Corpora.Conll (Tag)
 import qualified Data.ByteString as BS
 import           System.Random
 import           System.Envy
+import           System.IO as S
 import           Data.Maybe
+import           Data.Foldable (traverse_)
 import qualified Data.Text
 --------------------------------------------------------------------------------
 
@@ -23,10 +25,16 @@ data Config = Config
   , _ircServerPassword :: Maybe Text
   , _nickservPassword :: Maybe Text
   , _ircNick :: Maybe Text
+  , _ircIdent :: Maybe Text
+  , _ircChannels :: [Text]
   }
   deriving stock (Show, Eq, Generic)
 makeLenses ''Config
 
+instance Var [Text] where
+  toVar ts = show ts
+  fromVar s = readMaybe s >>= (pure . map Data.Text.pack)
+
 instance FromEnv Config where
   fromEnv _ =
     Config <$> env "OWO_CHANCE"
@@ -35,6 +43,8 @@ instance FromEnv Config where
        <*> envMaybe "IRC_SERVER_PASSWORD"
        <*> envMaybe "NICKSERV_PASSWORD"
        <*> envMaybe "IRC_NICK"
+       <*> envMaybe "IRC_IDENT"
+       <*> env "IRC_CHANNELS"
 
 stopWord :: Text -> Bool
 stopWord "'s"   = True
@@ -116,25 +126,25 @@ owothiaHandler conf nick state tagger = EventHandler Just $ \src ev -> do
   hasIdentified <- readIORef state
   when (not hasIdentified) $ do
     nickservAuth
-    send $ Join "##tvl"
+    traverse_ (send . Join) (conf ^. ircChannels)
     writeIORef state True
 
   when ("You are now identified" `BS.isInfixOf` (ev ^. raw)) $
-    send $ Join "##tvl"
+    traverse_ (send . Join) (conf ^. ircChannels)
 
   case (src, ev ^. message) of
-    (Channel "##tvl" nick, Privmsg _ (Right m)) -> do
+    (Channel chan nick, Privmsg _ (Right m)) -> do
       willOwo <- doOwo conf
-      when willOwo $ owoMessage m
-    _ -> pure ()
+      when willOwo $ owoMessage chan m
+    _ -> pure()
 
   pure ()
 
   where
-    owoMessage m = do
+    owoMessage chan m = do
       owoType <- liftIO randomIO
       mWord <- liftIO $ randomOwo owoType tagger m
-      for_ mWord $ \word -> send $ Privmsg "##tvl" $ Right $ owo owoType word
+      for_ mWord $ \word -> send $ Privmsg chan $ Right $ owo owoType word
     nickservAuthMsg = "IDENTIFY " <> nick <> " " <> fromJust (conf ^. nickservPassword)
     nickservAuth = send $ Privmsg "NickServ" $ Right nickservAuthMsg
 
@@ -143,14 +153,16 @@ main = do
   conf <- either fail pure =<< decodeEnv
   tagger <- defaultTagger
   state <- newIORef $ not . isJust $ (conf ^. nickservPassword)
+  S.hSetBuffering stdout LineBuffering
   let nick = fromMaybe "owothia" (conf ^. ircNick)
       conn =
         plainConnection (conf ^. ircServer) (conf ^. ircPort)
           & realname .~ "Owothia Revströwö"
           & password .~ (conf ^. ircServerPassword)
+          & username .~ fromMaybe "owothia" (conf ^. ircIdent)
           & logfunc .~ stdoutLogger
       cfg =
         defaultInstanceConfig nick
-          & channels .~ ["##tvl"]
+          & channels .~ (conf ^. ircChannels)
           & handlers %~ (owothiaHandler conf nick state tagger : )
   runClient conn cfg ()