add scheme evaluation

master
opfez 2021-10-11 19:24:59 +02:00
parent be728dde67
commit 292c39cd9f
1 changed files with 15 additions and 0 deletions

15
Anna.hs
View File

@ -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 '<timezone here>' > ~/.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