#!/usr/bin/env sh exec guile -l $BIZ_ROOT/Biz/Bot.scm -e '(@ (Biz Bot) main)' -s "$0" "$@" !# ;; Usage with ii: ;; ;; tail -f \#biz/out | guile -L $BIZ_ROOT -s Biz/Bot.scm (define-module (Biz Bot) #:use-module (ice-9 rdelim) #:use-module (ice-9 match) #:use-module (ice-9 regex) #:use-module (ice-9 receive) #:use-module (bs core) #:use-module ((bs string) #:prefix string.) #:export (main)) (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)))))) (define (main args) (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)))