From 7a75544e46a5e4972675ff02e39d1637dd98b74a Mon Sep 17 00:00:00 2001 From: opfez Date: Thu, 8 Apr 2021 14:57:05 +0000 Subject: [PATCH] change scdr, add scar, add multiple channel support --- anna.scm | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/anna.scm b/anna.scm index bac523f..e954ec2 100644 --- a/anna.scm +++ b/anna.scm @@ -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 .")) + (send-message stream chn "Hello! I respond to !rollcall, !anna, and !eval .")) ((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)))