add timezone command
parent
b7599c627a
commit
be728dde67
28
Anna.hs
28
Anna.hs
|
@ -2,6 +2,7 @@ module Main where
|
||||||
|
|
||||||
import Data.List
|
import Data.List
|
||||||
import Data.Char (isSpace)
|
import Data.Char (isSpace)
|
||||||
|
import qualified Data.Text as T
|
||||||
import System.IO
|
import System.IO
|
||||||
import System.Directory
|
import System.Directory
|
||||||
import Control.Monad
|
import Control.Monad
|
||||||
|
@ -15,15 +16,15 @@ port = 6667
|
||||||
name = "anna"
|
name = "anna"
|
||||||
channels = ["#tildetown", "#bots"]
|
channels = ["#tildetown", "#bots"]
|
||||||
|
|
||||||
data Bot = Bot { socket :: Handle }
|
data Socket = Socket { socket :: Handle }
|
||||||
type Net = ReaderT Bot IO
|
type Net = ReaderT Socket IO
|
||||||
|
|
||||||
-- Entrypoint
|
-- Entrypoint
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
handle <- connectTo hostname port
|
handle <- connectTo hostname port
|
||||||
hSetBuffering stdout NoBuffering
|
hSetBuffering stdout NoBuffering
|
||||||
runReaderT run Bot { socket = handle }
|
runReaderT run Socket { socket = handle }
|
||||||
|
|
||||||
run :: Net ()
|
run :: Net ()
|
||||||
run = do
|
run = do
|
||||||
|
@ -40,8 +41,8 @@ listen = forever $ do
|
||||||
-- Debug print
|
-- Debug print
|
||||||
liftIO $ putStrLn line
|
liftIO $ putStrLn line
|
||||||
tokenDispatch (parseUser $ tail line) (parseChannel tokens) tokens
|
tokenDispatch (parseUser $ tail line) (parseChannel tokens) tokens
|
||||||
where parseUser str = userName
|
where parseUser str = username
|
||||||
where userName = getName str
|
where username = getName str
|
||||||
|
|
||||||
getName :: String -> Maybe String
|
getName :: String -> Maybe String
|
||||||
getName s
|
getName s
|
||||||
|
@ -60,7 +61,7 @@ listen = forever $ do
|
||||||
tokenDispatch :: Maybe String -> Maybe String -> [String] -> Net ()
|
tokenDispatch :: Maybe String -> Maybe String -> [String] -> Net ()
|
||||||
tokenDispatch _ _ ("PING":_) = pong
|
tokenDispatch _ _ ("PING":_) = pong
|
||||||
tokenDispatch _ _ [] = return ()
|
tokenDispatch _ _ [] = return ()
|
||||||
tokenDispatch (Just userName) (Just chn) ts = if elem "PRIVMSG" ts
|
tokenDispatch (Just username) (Just chn) ts = if elem "PRIVMSG" ts
|
||||||
then helper
|
then helper
|
||||||
else return ()
|
else return ()
|
||||||
where helper
|
where helper
|
||||||
|
@ -70,6 +71,9 @@ tokenDispatch (Just userName) (Just chn) ts = if elem "PRIVMSG" ts
|
||||||
-- TODO: make reader monad for this? passing chn is a little tedious
|
-- TODO: make reader monad for this? passing chn is a little tedious
|
||||||
"!greets" -> greets chn
|
"!greets" -> greets chn
|
||||||
"!qotd" -> qotd chn
|
"!qotd" -> qotd chn
|
||||||
|
"!tz" -> if "!tz" == last tokens
|
||||||
|
then timezone chn username
|
||||||
|
else timezone chn $ head $ tail tokens
|
||||||
"!rollcall" -> rollcall chn
|
"!rollcall" -> rollcall chn
|
||||||
"!anna" -> anna chn
|
"!anna" -> anna chn
|
||||||
_ -> return ()
|
_ -> return ()
|
||||||
|
@ -115,9 +119,17 @@ qotd chn = do
|
||||||
mapM_ (sendMessage chn) textLines
|
mapM_ (sendMessage chn) textLines
|
||||||
return ()
|
return ()
|
||||||
|
|
||||||
-- TODO
|
|
||||||
timezone :: String -> String -> Net ()
|
timezone :: String -> String -> Net ()
|
||||||
timezone chn username = undefined
|
timezone chn username = do
|
||||||
|
let filepath = "/home/" ++ username ++ "/.tz"
|
||||||
|
existence <- liftIO $ doesFileExist filepath
|
||||||
|
if existence
|
||||||
|
then do
|
||||||
|
contents <- liftIO $ readFile filepath
|
||||||
|
let cleanContents = sanitize contents
|
||||||
|
sendMessage chn cleanContents
|
||||||
|
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
|
||||||
|
|
||||||
rollcall :: String -> Net ()
|
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"
|
rollcall chn = sendMessage chn "Hello! I respond to !anna, !qotd, and !greets. My source code is available at https://git.tilde.town/opfez/anna2"
|
||||||
|
|
Loading…
Reference in New Issue