From 46ff474bfdb9c987f22059e43680de0c0b36ecc8 Mon Sep 17 00:00:00 2001 From: Jeena Paradies Date: Mon, 27 Jun 2011 12:30:40 +0200 Subject: [PATCH] initial commit --- .gitignore | 3 +++ Makefile | 2 ++ README | 1 + cnb.hs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 README create mode 100644 cnb.hs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eba1e11 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.hi +*.o +cnb diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..597f58a --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +all: + ghc --make cnb.hs -o cnb diff --git a/README b/README new file mode 100644 index 0000000..4f34145 --- /dev/null +++ b/README @@ -0,0 +1 @@ +A simple IRC bot. diff --git a/cnb.hs b/cnb.hs new file mode 100644 index 0000000..d508797 --- /dev/null +++ b/cnb.hs @@ -0,0 +1,48 @@ +import Network +import System.IO +import Text.Printf +import Data.List +import System.Exit + +server = "irc.epd-me.net" +port = 6667 +chan = "#jeena" +nick = "cnb" + +main = do + h <- connectTo server (PortNumber (fromIntegral port)) + hSetBuffering h NoBuffering + write h "NICK" nick + write h "USER" (nick ++ " 0 * :clynx Nerv-Bot") + write h "JOIN" chan + listen h + +write :: Handle -> String -> String -> IO () +write h s t = do + hPrintf h "%s %s\r\n" s t + printf "> %s %s\n" s t + +listen :: Handle -> IO () +listen h = forever $ do + t <- hGetLine h + let s = init t + if ping s then pong s else eval h (clean s) + putStrLn s + where + forever a = a >> forever a + clean = drop 1 . dropWhile (/= ':') . drop 1 + ping x = "PING :" `isPrefixOf` x + pong x = write h "PONG" (':' : drop 6 x) + +eval :: Handle -> String -> IO () +eval h "!quit" = write h "QUIT" ":Exiting" >> exitWith ExitSuccess +eval h "!jump" = privmsg h "The quick brown clynx jumps over the lazy oak." +eval h "!implode" = write h "QUIT" ":whhooooooshhhh ..." +eval h "!explode" = write h "QUIT" ":kaBOOooOOOOoommm ..." +eval h x | (nick ++ ":") `isPrefixOf` x = privmsg h "Halt die Fresse!" +eval h x | and [(not (isInfixOf "@clynx" x)), (isInfixOf "clynx" x)] = + privmsg h "Der clynx kann nix!" +eval _ _ = return () + +privmsg :: Handle -> String -> IO () +privmsg h s = write h "PRIVMSG" (chan ++ " :" ++s)