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