From 93f0540991809d459cfc3061c88fc07d1cfc948c Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Thu, 13 Jul 2017 13:37:24 +0200 Subject: [PATCH] add nickserv and a cli. Fix #2 --- .gitignore | 2 +- Makefile | 3 +- README.md | 4 +++ cloogle.icl => cloogleirc.icl | 67 +++++++++++++++++++++++++++++++---- run.sh | 2 +- 5 files changed, 67 insertions(+), 11 deletions(-) rename cloogle.icl => cloogleirc.icl (69%) diff --git a/.gitignore b/.gitignore index 4288e9d..29de276 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ test Clean System Files -cloogle +cloogleirc IRC diff --git a/Makefile b/Makefile index 4be80d8..bd1f7b7 100644 --- 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 ~/projects/gast/Libraries\ -I ./libcloogle -BINARIES:=IRC cloogle #test +BINARIES:=IRC cloogleirc #test all: $(BINARIES) diff --git a/README.md b/README.md index b2bf47f..dcc383f 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,9 @@ # 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 diff --git a/cloogle.icl b/cloogleirc.icl similarity index 69% rename from cloogle.icl rename to cloogleirc.icl index b2c7bdf..69be839 100644 --- a/cloogle.icl +++ b/cloogleirc.icl @@ -1,4 +1,4 @@ -module cloogle +module cloogleirc 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 -from Control.Monad import class Monad, instance Monad Maybe +from Control.Monad import class Monad, instance Monad Maybe, >>= from Text.Encodings.UrlEncoding import urlEncode +import System.CommandLine import Internet.HTTP import Data.Error import Data.List @@ -97,15 +98,67 @@ cloogle data w | 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 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 + 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} - 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) diff --git a/run.sh b/run.sh index 83639f6..bf33636 100644 --- a/run.sh +++ b/run.sh @@ -3,6 +3,6 @@ while true do git pull origin master make - ./cloogle + ./cloogleirc "$@" sleep 5s done -- 2.20.1