change scdr, add scar, add multiple channel support

master
opfez 2021-04-08 14:57:05 +00:00
parent 022a90254f
commit 7a75544e46
1 changed files with 19 additions and 13 deletions

View File

@ -5,23 +5,28 @@
(ice-9 textual-ports) (ice-9 textual-ports)
(ice-9 format)) (ice-9 format))
(define +channel+ "#bots")
(define +channels+ '("#tildetown" "#bots")) (define +channels+ '("#tildetown" "#bots"))
(define (scdr lst) (define (scdr lst)
"Safe CDR which will return the empty list if it is passed the empty list instead of throwing an error." "Safe CDR. Returns CDR of lst, if it is a list. Returns lst otherwise."
(if (null? lst) (if (not (pair? lst))
lst lst
(cdr lst))) (cdr lst)))
(define (scar lst)
"Safe CAR, see scdr."
(if (not (pair? lst))
lst
(car lst)))
(define (slist-ref lst index) (define (slist-ref lst index)
"Safe list-ref, see scdr" "Safe list-ref, see scdr."
(if (< (length lst) (+ index 1)) (if (< (length lst) (+ index 1))
'() '()
(list-ref lst index))) (list-ref lst index)))
(define (slist-tail lst index) (define (slist-tail lst index)
"Safe list-tail, see scdr" "Safe list-tail, see scdr."
(if (< (length lst) (+ index 1)) (if (< (length lst) (+ index 1))
'() '()
(list-tail lst index))) (list-tail lst index)))
@ -58,10 +63,10 @@
(format stream "USER anna 0.0.0.0 anna :Anna\r\n")) (format stream "USER anna 0.0.0.0 anna :Anna\r\n"))
(define (join-channels stream) (define (join-channels stream)
;; (for-each (lambda (chn) (for-each (lambda (chn)
;; (format stream "JOIN ~a\r\n" chn)) (format stream "JOIN ~a\r\n" chn))
;; +channels+)) +channels+))
(format stream "JOIN ~a\r\n" "#bots"))
(define (send-message stream chn msg) (define (send-message stream chn msg)
(format stream "PRIVMSG ~a :~a\r\n" chn msg)) (format stream "PRIVMSG ~a :~a\r\n" chn msg))
@ -71,7 +76,8 @@
(define (main-loop stream) (define (main-loop stream)
(let* ((inl (read-line stream)) (let* ((inl (read-line stream))
(data (string-tokenize inl))) (data (string-tokenize inl))
(chn (scar (scdr (member "PRIVMSG" data)))))
;; debug print ;; debug print
(format #t "~s~%" data) (format #t "~s~%" data)
(cond (cond
@ -79,14 +85,14 @@
(pong stream)) (pong stream))
((or (equal? ":!rollcall" (slist-ref data 3)) ((or (equal? ":!rollcall" (slist-ref data 3))
(equal? ":!anna" (slist-ref data 3))) (equal? ":!anna" (slist-ref data 3)))
(send-message stream +channel+ "Hello! I respond to !rollcall, !anna, and !eval <s-expr>.")) (send-message stream chn "Hello! I respond to !rollcall, !anna, and !eval <s-expr>."))
((equal? ":!eval" (slist-ref data 3)) ((equal? ":!eval" (slist-ref data 3))
(let* ((expr (string-concatenate (intersperse " " (cdr (member ":!eval" data)))))) (let* ((expr (string-concatenate (intersperse " " (cdr (member ":!eval" data))))))
(send-message stream +channel+ (eval (read (open-input-string expr)) (send-message stream chn (eval (read (open-input-string expr))
(interaction-environment))))) (interaction-environment)))))
((and (= 7 (length data)) ((and (= 7 (length data))
(equal? (list-tail data 4) '("ACTION" "shoots" "anna"))) (equal? (list-tail data 4) '("ACTION" "shoots" "anna")))
(send-action stream +channel+ "dies") (send-action stream chn "dies")
(close-port stream) (close-port stream)
(exit))) (exit)))
(main-loop stream))) (main-loop stream)))