From 292c39cd9fb2ad7889e99c9254d355d9d8efd7f8 Mon Sep 17 00:00:00 2001 From: opfez Date: Mon, 11 Oct 2021 19:24:59 +0200 Subject: [PATCH] add scheme evaluation --- Anna.hs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Anna.hs b/Anna.hs index 9bc922b..3c17682 100644 --- a/Anna.hs +++ b/Anna.hs @@ -5,6 +5,7 @@ import Data.Char (isSpace) import qualified Data.Text as T import System.IO import System.Directory +import System.Process (readProcess) import Control.Monad import Control.Monad.IO.Class import qualified Network.Socket as N @@ -74,6 +75,7 @@ tokenDispatch (Just username) (Just chn) ts = if elem "PRIVMSG" ts "!tz" -> if "!tz" == last tokens then timezone chn username else timezone chn $ head $ tail tokens + "!eval" -> evalScheme chn $ join $ intersperse " " $ tail tokens "!rollcall" -> rollcall chn "!anna" -> anna chn _ -> return () @@ -131,6 +133,19 @@ timezone chn username = do else sendMessage chn $ username ++ " has not set their timezone. Use `echo '' > ~/.tz' to add your timezone." where sanitize = T.unpack . T.replace (T.pack "\n") T.empty . T.pack +evalScheme :: String -> String -> Net () +evalScheme chn form = do + liftIO $ putStrLn form + result <- liftIO $ (\s -> s ++ "\n") <$> readProcess "guile" ["-c", expr] "" + sendMessage chn result + where expr = "(use-modules (ice-9 sandbox) \ + \ (rnrs exceptions)) \ + \ (display (guard (ex (else 'error)) \ + \ (eval-in-sandbox (read (open-input-string \"" ++ form ++ "\")) \ + \ #:bindings all-pure-and-impure-bindings)))" + + + rollcall :: String -> Net () rollcall chn = sendMessage chn "Hello! I respond to !anna, !qotd, and !greets. My source code is available at https://git.tilde.town/opfez/anna2" anna = rollcall