summaryrefslogtreecommitdiff
path: root/miaowe.hs
diff options
context:
space:
mode:
Diffstat (limited to 'miaowe.hs')
-rw-r--r--miaowe.hs87
1 files changed, 60 insertions, 27 deletions
diff --git a/miaowe.hs b/miaowe.hs
index cdd5b2b..f26f69d 100644
--- a/miaowe.hs
+++ b/miaowe.hs
@@ -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