diff options
author | Laura <the-ceo-of-antifa@protonmail.com> | 2023-01-22 13:59:35 +0100 |
---|---|---|
committer | Laura <the-ceo-of-antifa@protonmail.com> | 2023-01-22 13:59:35 +0100 |
commit | 037561fd2e2caf0ddb894cac05ca19b5840d4ae2 (patch) | |
tree | 7cb396428960dba58a78b04c7399a2df4d8cb141 | |
parent | 7cd4f6ea6999f8b88a03c93d8d9ef306d4356065 (diff) | |
download | miaowe-037561fd2e2caf0ddb894cac05ca19b5840d4ae2.tar.gz miaowe-037561fd2e2caf0ddb894cac05ca19b5840d4ae2.zip |
less hard-coding
-rw-r--r-- | README.md | 4 | ||||
-rw-r--r-- | miaowe.hs | 87 |
2 files changed, 63 insertions, 28 deletions
@@ -10,7 +10,9 @@ To build: The resulting executable will be called "miaowe". -Miaowe includes no networking code of its own, but assumes stdin and stdout will be connected to an IRC server. A zsh script, nya.sh, is included to help with this. Users of other shells may need to adjust the script to work with their shell. NickServ login credentials are placed in a file called "conn" (in the parent of miaowe's PWD), on the first line and separated by a space. Image files are read from the working directory. If nya.sh is used, image_directory becomes miaowe's PWD. +Miaowe includes no networking code of its own, but assumes stdin and stdout will be connected to an IRC server. A zsh script, nya.sh, is included to help with this. Users of other shells may need to adjust the script to work with their shell. Image files are read from the working directory. If nya.sh is used, image_directory becomes miaowe's PWD. + +Miaowe expects to find a configuration file with the name "conf" in its PWD's parent directory. This should be in a key-value format, one entry per line. Strings should not be quoted; anything after the first space is considered part of the value. It should include the following keys: "nick", "username", "realname", "credentials". "Credentials" should be the NickServ password, or username and password separated by a space. Available commands: * List -- lists all files in PWD @@ -48,12 +48,21 @@ data Image = Image { imgHeight :: Int } +data UserInfo = UserInfo { + userNick :: String, + userName :: String, + userRealname :: String, + userCredentials :: String + } + codeUrl = "https://gitlab.com/laleanor/miaowe" #ifdef FORTUNE -helpString = "Commands available: Code; Fortune; Help; List. Anything else is interpreted as a file name." +helpString = "Commands available: Code; Fortune; Help; List. \ +Anything else is interpreted as a file name." #else -helpString = "Commands available: Code; Help; List. Anything else is interpreted as a file name." +helpString = "Commands available: Code; Help; List. \ +Anything else is interpreted as a file name." #endif ctrlC = [toEnum 03] :: String @@ -138,10 +147,29 @@ ppmToImage ppm = Image ((w8Split (S.Internal.c2w ' ') ((w8Split (S.Internal.c2w '\n') ppm) !! 1)) !! 1))) -ircConnect :: String -> MircString -ircConnect credentials = printf "NICK miaowe\r\n\ -USER nya 0 * :mew mew\r\n\ -PRIVMSG NickServ :IDENTIFY %s\r\n" credentials +readFromConf :: String -> String -> String +readFromConf conf key = (intercalate " " . + drop 1 . + words . + (!!0) . + filter (\l -> (words l)!!0 == key)) + (lines conf) + +readConf :: String -> UserInfo +readConf conf = UserInfo + (readFromConf conf "nick") + (readFromConf conf "username") + (readFromConf conf "realname") + (readFromConf conf "credentials") + +ircConnect :: UserInfo -> MircString +ircConnect u = printf "NICK %s\r\n\ +USER %s 0 * :%s\r\n\ +PRIVMSG NickServ :IDENTIFY %s\r\n" + (userNick u) + (userName u) + (userRealname u) + (userCredentials u) waitForMessage :: (String -> Bool) -> IO String waitForMessage p = getLine >>= \l -> if p l @@ -202,27 +230,32 @@ getFortune :: IO String getFortune = readCreateProcess ((shell "fortune | cowsay -b")) "" #endif -processCommand :: IrcChannel -> MircString -> IO () -processCommand channel command | length (words command) < 5 = sendToChannel channel - "You need to specify a command." - | otherwise = case map toLower ((words command)!!4) of - "quit" -> exitSuccess - "list" -> listDirectory "." - >>= sendToChannel channel . - intercalate "\n" - "code" -> sendToChannel channel codeUrl - "help" -> sendToChannel channel helpString +doCommand :: IrcChannel -> MircString -> IO () +doCommand channel command = + case map toLower ((words command)!!4) of + "quit" -> exitSuccess + "list" -> listDirectory "." + >>= sendToChannel channel . + intercalate "\n" + "code" -> sendToChannel channel codeUrl + "help" -> sendToChannel channel helpString #ifdef FORTUNE - "fortune" -> getFortune - >>= sendToChannel channel + "fortune" -> getFortune + >>= sendToChannel channel #endif - _ -> (if isInfixOf "/" ((words command)!!4) - then - sendToChannel channel - "Only PWD please." - else - trySendFile channel - ((words command)!!4)) + _ -> (if isInfixOf "/" ((words command)!!4) + then + sendToChannel channel + "Only PWD please." + else + trySendFile channel + ((words command)!!4)) + +processCommand :: IrcChannel -> MircString -> IO () +processCommand channel command | length (words command) < 5 = sendToChannel channel + "You need to specify \ +a command." + | otherwise = doCommand channel command mainLoop :: IrcChannel -> IO () mainLoop channel = waitForCommand @@ -235,8 +268,8 @@ main :: IO () main = openFile "../conf" ReadMode >>= (\handle -> - hGetLine handle - >>= putStr . ircConnect + hGetContents handle + >>= putStr . ircConnect . readConf >> hClose handle) >> hFlush stdout >> waitForCloak |