From 5530bd9742bc73c2c6631c20c942d3ecd4b81505 Mon Sep 17 00:00:00 2001 From: Hendrik Jaeger Date: Mon, 21 Oct 2019 18:34:41 +0200 Subject: [PATCH 1/1] Completed first steps --- app/Main.hs | 8 +++++++- package.yaml | 9 +++++---- src/Lib.hs | 40 +++++++++++++++++++++++++++++++++++++--- 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index de1c1ab..46af6cf 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -1,6 +1,12 @@ module Main where import Lib +import qualified Network.Socket as N -- network + +myServer = "irc.netwichtig.de" :: String +myPort = 6667 :: N.PortNumber +myChannels = ["#shelly-testing", "#shelly"] :: [String] +myNick = "shelly" :: String main :: IO () -main = someFunc +main = runBirch myServer myPort myNick myChannels diff --git a/package.yaml b/package.yaml index 75db005..1acd14e 100644 --- a/package.yaml +++ b/package.yaml @@ -1,10 +1,10 @@ name: birch version: 0.1.0.0 github: "githubuser/birch" -license: BSD3 +license: GPL author: "Hendrik Jaeger" maintainer: "code@henk.geekmail.org" -copyright: "GPL" +copyright: "2019" extra-source-files: - README.md @@ -17,16 +17,17 @@ extra-source-files: # To avoid duplicated efforts in documentation and dealing with the # complications of embedding Haddock markup inside cabal files, it is # common to point users to the README.md file. -description: Please see the README on GitHub at +description: bot for IRC in Haskell dependencies: - base >= 4.7 && < 5 +- network library: source-dirs: src executables: - birch-exe: + birch: main: Main.hs source-dirs: app ghc-options: diff --git a/src/Lib.hs b/src/Lib.hs index d36ff27..12d4dbc 100644 --- a/src/Lib.hs +++ b/src/Lib.hs @@ -1,6 +1,40 @@ module Lib - ( someFunc + ( runBirch ) where -someFunc :: IO () -someFunc = putStrLn "someFunc" +import System.IO -- base +import qualified Network.Socket as N -- network + +-- Toplevel program +runBirch :: String -> N.PortNumber -> String -> [String] -> IO () +runBirch myServer myPort myNick myChannels = do + h <- connectTo myServer myPort + write h "NICK" myNick + write h "USER" (myNick ++ " 0 * :tutorial bot") +-- map (write h "JOIN") myChannels + write h "JOIN" (head myChannels) + listen h + +-- Connect to a server given its name and port number +connectTo :: N.HostName -> N.PortNumber -> IO Handle +connectTo host port = do + addr : _ <- N.getAddrInfo Nothing (Just host) (Just (show port)) + sock <- N.socket (N.addrFamily addr) (N.addrSocketType addr) (N.addrProtocol addr) + N.connect sock (N.addrAddress addr) + N.socketToHandle sock ReadWriteMode + +-- Send a message to a handle +write :: Handle -> String -> String -> IO () +write h cmd args = do + let msg = cmd ++ " " ++ args ++ "\r\n" + hPutStr h msg -- Send message on the wire + putStr ("> " ++ msg) -- Show sent message on the command line + +-- Process each line from the server +listen :: Handle -> IO () +listen h = forever $ do + line <- hGetLine h + putStrLn line + where + forever :: IO () -> IO () + forever a = do a; forever a -- 2.39.2