From 1311706d7cca3cd67af5c974c763aba53b0011a9 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 16 Aug 2017 20:25:03 +0200 Subject: handlePrivmsg: Move duplicated code to new function We have some code that's duplicated in both branches of the `Either` case statement that sends a response over IRC. Extract that to a new function that can be shared between the two case statements to try to reduce repetition. Took me a while to wrangle the monads to get this, but it's in working condition now. Need to add the commented `Nothing` branch in so I'm handling all the cases. --- src/IRC.hs | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/IRC.hs b/src/IRC.hs index 427fac7..253d51f 100644 --- a/src/IRC.hs +++ b/src/IRC.hs @@ -48,8 +48,24 @@ handlePrivmsg = IRC.EventHandler , channel = T.unpack chan , nick = T.unpack nick } - Just plugin = matchPlugin message - response <- liftIO $ performPlugin plugin message - IRC.send $ case response of - Left err -> IRC.Privmsg chan (Right (T.pack err)) - Right r -> IRC.Privmsg chan (Right (T.pack r)) + -- case privmsgFromPlugin message of + -- () -> return () + -- msg -> IRC.send msg + -- response <- privmsgFromPlugin message + response <- liftIO $ privmsgFromPlugin message + IRC.send response + +privmsgFromPlugin :: Message -> IO (IRC.Message T.Text) +privmsgFromPlugin message = do + case matchPlugin message of + -- Nothing -> + Just plugin -> do + -- let response = liftIO $ performPlugin plugin message in + response <- liftIO $ performPlugin plugin message + return $ case response of + Left err -> IRC.Privmsg + (T.pack (channel message)) + (Right (T.pack err)) + Right r -> IRC.Privmsg + (T.pack (channel message)) + (Right (T.pack r)) -- cgit v1.2.3