From e3a813d72449e738bd95323511ad0696719bf396 Mon Sep 17 00:00:00 2001 From: Ben Sima Date: Fri, 2 Apr 2021 21:39:47 -0400 Subject: Init bizbot --- Biz/Bild/ShellHook.sh | 2 ++ Biz/Bot.nix | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ Biz/Bot.scm | 53 ++++++++++++++++++++++++++++++++++++++- bs/re.scm | 6 +++-- bs/string.scm | 4 +-- 5 files changed, 128 insertions(+), 5 deletions(-) create mode 100644 Biz/Bot.nix diff --git a/Biz/Bild/ShellHook.sh b/Biz/Bild/ShellHook.sh index f2c2470..5be96ae 100644 --- a/Biz/Bild/ShellHook.sh +++ b/Biz/Bild/ShellHook.sh @@ -21,6 +21,8 @@ NC='\033[0m' # No Color alias runghc="runghc --ghc-arg=-i$BIZ_ROOT" +alias guile="guile -L $BIZ_ROOT" + alias tree="tree -I _" # link git hooks diff --git a/Biz/Bot.nix b/Biz/Bot.nix new file mode 100644 index 0000000..93aa2e6 --- /dev/null +++ b/Biz/Bot.nix @@ -0,0 +1,68 @@ +{ options +, lib +, config +, pkgs +, modulesPath +}: + +let + cfg = config.services.bizbot; +in +{ + options.services.bizbot = { + enable = lib.mkEnableOption "Enable the bizbot service"; + package = lib.mkOption { + type = lib.types.package; + description = "bizbot package to use"; + }; + server = lib.mkOption { + type = lib.types.str; + description = "server address to connect to"; + }; + channel = lib.mkOption { + type = lib.types.str; + description = "channel to join"; + }; + dataDir = lib.mkOption { + type = lib.types.str; + description = "where bizbot will start it's ii tree"; + }; + }; + config = lib.mkIf cfg.enable { + systemd.services = { + bizbot-${cfg.server}-${cfg.channel}-ii = { + wantedBy = [ "multi-user.target" ]; + path = [ pkgs.ii ]; + script = '' + ${pkgs.ii}/bin/ii ${cfg.dataDir}/irc -s ${cfg.server} -p 6667 -n bizbot + ''; + postStart = '' + echo "/join ${cfg.channel}" > ${cfg.dataDir}/irc/${cfg.server}/in + ''; + description = '' + bizbot ii tree for ${cfg.server}/${cfg.channel} + ''; + + }; + bizbot-${cfg.server}-${cfg.channel} = { + path = [ cfg.package ]; + wantedBy = [ "multi-user.target" ]; + after = [ "bizbot-${cfg.server}-${cfg.channel}-ii.service" ]; + script = '' + tail -f ${cfg.dataDir}/irc/${cfg.server}/${cfg.channel}/out \ + | ${cfg.package}/bin/bizbot \ + | tee -a ${cfg.dataDir}/irc/${cfg.server}/${cfg.channel}/in + ''; + description = '' + bizbot for ${cfg.server}/${cfg.channel} + ''; + serviceConfig = { + KillSignal = "INT"; + Type = "simple"; + Restart = "on-abort"; + RestartSec = "1"; + }; + }; + }; + }; +} diff --git a/Biz/Bot.scm b/Biz/Bot.scm index 7ed413a..dec654c 100644 --- a/Biz/Bot.scm +++ b/Biz/Bot.scm @@ -1,4 +1,55 @@ +;; Usage with ii: +;; +;; tail -f \#biz/out | guile -L $BIZ_ROOT -s Biz/Bot.scm +(import (ice-9 rdelim)) +(import (ice-9 match)) +(import (ice-9 regex)) +(import (ice-9 receive)) +(import (bs core)) +(import (prefix (bs string) string.)) -(display "hello") +(define (log msg) + (display msg (current-error-port))) + +(define (is-command? msg) + (string.prefix? msg "bizbot:")) + +(define (parse-line line) + (if (eof-object? line) + (exit) + (let ([matches (regexp-exec + (make-regexp "<(\\S*)>(.*)" regexp/extended) + (string-drop line 11))]) + (if matches + `(user + ,(match:substring matches 1) + ,(string.lstrip (match:substring matches 2) #\space)) + `(system ,(string-drop line 11)))))) + +(define (dispatch user msg) + (let ([msg (-> msg + (string-drop (string-length "bizbot:")) + (string.lstrip #\space))]) + (cond + ((equal? msg "hi") + (display (fmt "~a: well, hello!" user))) + + (else + (display (fmt "command not understood: ~a" msg)))))) + +(while #t + (match (parse-line (read-line)) + [('user user msg) + (if (is-command? msg) + (dispatch user msg) + (begin + (log (fmt "user: ~a " user)) + (log (fmt "message: ~a" msg))))] + + [('system msg) + (log (fmt "system: ~a" msg))]) + + (newline) + (force-output)) diff --git a/bs/re.scm b/bs/re.scm index 2b5752b..a325249 100644 --- a/bs/re.scm +++ b/bs/re.scm @@ -19,8 +19,10 @@ (export match group sub search compile ;; I IGNORECASE M MULTILINE ) - (import (rnrs base (6)) - (ice-9 regex)) + (import (rnrs base) + ;; TODO: port to srfi-115 + (only (ice-9 regex) + make-regexp)) ;; (define I regexp/icase) ;; (define IGNORECASE regexp/icase) diff --git a/bs/string.scm b/bs/string.scm index 963525c..c6e6e31 100644 --- a/bs/string.scm +++ b/bs/string.scm @@ -19,8 +19,8 @@ (only (ice-9 ports) with-output-to-string)) ;; Is `pre` a prefix of `s`? - (define (prefix? s sub) - (string-prefix? sub s)) + (define (prefix? s pre) + (string-prefix? pre s)) ;; Is `suf` a suffix of `s`? (define (suffix? s suf) -- cgit v1.2.3