]> git.netwichtig.de Git - user/henk/code/haskell/birch.git/commitdiff
Completed first steps
authorHendrik Jaeger <git-commit@henk.geekmail.org>
Mon, 21 Oct 2019 16:34:41 +0000 (18:34 +0200)
committerHendrik Jaeger <git-commit@henk.geekmail.org>
Mon, 21 Oct 2019 16:34:41 +0000 (18:34 +0200)
app/Main.hs
package.yaml
src/Lib.hs

index de1c1ab35c4ad21e14ec824121b9710418b75c39..46af6cf53cb98c4a5327a6728a78746ee3026663 100644 (file)
@@ -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
index 75db005b7bc0802d645f40df5eaaf54f92776cb0..1acd14ebbb24071f38a615a4925ab9ac4bc1d39d 100644 (file)
@@ -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 <https://github.com/githubuser/birch#readme>
+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:
index d36ff2714d5b36319784afa370e0f0d111d57ef1..12d4dbcb5e36ca56d1c77f0194b248cabac60c1e 100644 (file)
@@ -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