add nickserv and a cli. Fix #2
authorMart Lubbers <mart@martlubbers.net>
Thu, 13 Jul 2017 11:37:24 +0000 (13:37 +0200)
committerMart Lubbers <mart@martlubbers.net>
Thu, 13 Jul 2017 11:37:24 +0000 (13:37 +0200)
.gitignore
Makefile
README.md
cloogleirc.icl [moved from cloogle.icl with 69% similarity]
run.sh

index 4288e9d..29de276 100644 (file)
@@ -1,4 +1,4 @@
 test
 Clean System Files
 test
 Clean System Files
-cloogle
+cloogleirc
 IRC
 IRC
index 4be80d8..bd1f7b7 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -13,10 +13,9 @@ CLMLIBS:=\
        -I $(CLEAN_HOME)/lib/Generics\
        -I $(CLEAN_HOME)/lib/TCPIP\
        -I $(CLEAN_HOME)/lib/Dynamics\
        -I $(CLEAN_HOME)/lib/Generics\
        -I $(CLEAN_HOME)/lib/TCPIP\
        -I $(CLEAN_HOME)/lib/Dynamics\
-       -I ~/projects/gast/Libraries\
        -I ./libcloogle
 
        -I ./libcloogle
 
-BINARIES:=IRC cloogle #test
+BINARIES:=IRC cloogleirc #test
 
 all: $(BINARIES)
 
 
 all: $(BINARIES)
 
index b2bf47f..dcc383f 100644 (file)
--- a/README.md
+++ b/README.md
@@ -1,5 +1,9 @@
 # clean-irc
 IRC library for Clean
 
 # clean-irc
 IRC library for Clean
 
+## Example
+As a proof of concept, a cloogle bot is created. Run `./cloogle-irc --help` for
+more info. `run.sh` contains a script suitable for running it continously.
+
 ## Todo
 - Check the commands one more time on syntax
 ## Todo
 - Check the commands one more time on syntax
similarity index 69%
rename from cloogle.icl
rename to cloogleirc.icl
index b2c7bdf..69be839 100644 (file)
@@ -1,4 +1,4 @@
-module cloogle
+module cloogleirc
 
 import Cloogle
 import GenPrint
 
 import Cloogle
 import GenPrint
@@ -19,8 +19,9 @@ import Text.URI
 import Control.Applicative
 import qualified Control.Monad as CM
 import qualified Data.Map as DM
 import Control.Applicative
 import qualified Control.Monad as CM
 import qualified Data.Map as DM
-from Control.Monad import class Monad, instance Monad Maybe
+from Control.Monad import class Monad, instance Monad Maybe, >>=
 from Text.Encodings.UrlEncoding import urlEncode
 from Text.Encodings.UrlEncoding import urlEncode
+import System.CommandLine
 import Internet.HTTP
 import Data.Error
 import Data.List
 import Internet.HTTP
 import Data.Error
 import Data.List
@@ -97,15 +98,67 @@ cloogle data w
                | size s > 80 = subString 0 77 s + "..."
                = s
 
                | size s > 80 = subString 0 77 s + "..."
                = s
 
+:: BotSettings =
+               { bs_nick     :: String
+               , bs_nickserv :: Maybe String
+               , bs_autojoin :: [String]
+               , bs_port     :: Int
+               , bs_server   :: String
+               }
 
 Start :: *World -> (MaybeErrorString (), *World)
 
 Start :: *World -> (MaybeErrorString (), *World)
-Start w = bot ("irc.freenode.net", 6667) startup shutdown () process w
+Start w
+# ([arg0:args], w) = getCommandLine w
+# bs = parseCLI args 
+| isError bs = (Error $ "\n" +++ fromError bs +++ "\n", w)
+# (Ok bs) = bs
+= bot (bs.bs_server, bs.bs_port) (startup bs) shutdown () process w
        where
        where
+               parseCLI :: [String] -> MaybeErrorString BotSettings
+               parseCLI [] = Ok
+                       { bs_nick     = "clooglebot"
+                       , bs_nickserv = Nothing
+                       , bs_autojoin = []
+                       , bs_port     = 6667
+                       , bs_server   = "irc.freenode.net"
+                       }
+               parseCLI [a:as]
+               | a == "-n" || a == "--nick"
+                       = arg1 "--nick" as \a c->{c & bs_nick=a}
+               | a == "-ns" || a == "--nickserv"
+                       = arg1 "--nickserv" as \a c->{c & bs_nickserv=Just a}
+               | a == "-a" || a == "--autojoin"
+                       = arg1 "--autojoin" as \a c->{c & bs_autojoin=c.bs_autojoin ++ [a]}
+               | a == "-p" || a == "--port"
+                       = arg1 "--port" as \a c->{c & bs_port=toInt a}
+               | a == "-s" || a == "--server"
+                       = arg1 "--port" as \a c->{c & bs_server=a}
+               | a == "-h" || a == "--help" = Error $ join "\n" $
+                       [ "Usage: cloogle [OPTS]"
+                       , "Options:"
+                       , "\t--nick/-n NICKNAME     Use the given nickname instead of clooglebot"
+                       , "\t--nickserv/-ns PW      Identify via the given password with NickServ"
+                       , "\t--port/-p PORT         Use the given port instead of port 6667"
+                       , "\t--server/-s SERVER     Use the given server instead of irc.freenode.net"
+                       , "\t--autojoin/-a CHANNEL  Add CHANNEL to the autojoin list. This command "
+                       , "\t                       can be called multiple times. Beware that #"
+                       , "\t                       has to be escaped in most shells"
+                       ]
+               = Error $ "Unknown option: " +++ a
+
+               arg1 name [] _ = Error $ name +++ " requires an argument"
+               arg1 name [a:as] f = parseCLI as >>= Ok o f a
+
+               nickserv pw = PRIVMSG (CSepList ["NickServ"]) $ "IDENTIFY " +++ pw
+
                toPrefix c = {irc_prefix=Nothing,irc_command=Right c}
                toPrefix c = {irc_prefix=Nothing,irc_command=Right c}
-               startup = map toPrefix
-                       [NICK "clooglebot" Nothing
-                       ,USER "cloogle" "cloogle" "cloogle" "Cloogle bot"
-                       ,JOIN (CSepList ["#cloogle", "#cleanlang"]) Nothing]
+
+               startup bs = map toPrefix $
+                       [   NICK bs.bs_nick Nothing
+                       ,   USER "cloogle" "cloogle" "cloogle" "Cloogle bot"
+                       ]++ maybe [] (pure o nickserv) bs.bs_nickserv
+                        ++ if (isEmpty bs.bs_autojoin) []
+                               [JOIN (CSepList bs.bs_autojoin) Nothing]
                shutdown = map toPrefix [QUIT $ Just "Bye"]
 
                process :: IRCMessage () *World -> (Maybe [IRCMessage], (), *World)
                shutdown = map toPrefix [QUIT $ Just "Bye"]
 
                process :: IRCMessage () *World -> (Maybe [IRCMessage], (), *World)
diff --git a/run.sh b/run.sh
index 83639f6..bf33636 100644 (file)
--- a/run.sh
+++ b/run.sh
@@ -3,6 +3,6 @@ while true
 do
        git pull origin master
        make
 do
        git pull origin master
        make
-       ./cloogle
+       ./cloogleirc "$@"
        sleep 5s
 done
        sleep 5s
 done