summaryrefslogtreecommitdiff
path: root/Biz/Bot.scm
blob: af0658592704f8a11a3630faf3fe5d9bfd30ff60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
;; : out bizbot
;;
;; Usage with ii:
;;
;;     tail -f \#biz/out | guile -L $BIZ_ROOT -s Biz/Bot.scm
;;
(define-module (Biz Bot) #:export (main))

(import (ice-9 rdelim))
(import (ice-9 match))
(import (ice-9 regex))
(import (ice-9 receive))
(import (bs core))
(import (prefix (bs string)  string.))

(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)))