Compare commits

...

3 Commits

Author SHA1 Message Date
opfez 2b2f86d164 Merge branch 'master' of http://git.tilde.town/opfez/anna 2021-04-08 14:16:12 +00:00
opfez 85b3fe1e30 remove fezbot.scm 2021-04-08 14:15:49 +00:00
opfez 9625aea82d rename to anna, add .eval command 2021-04-08 14:15:14 +00:00
2 changed files with 94 additions and 73 deletions

94
anna.scm 100644
View File

@ -0,0 +1,94 @@
#!/usr/bin/guile
!#
(use-modules (ice-9 rdelim)
(ice-9 textual-ports)
(ice-9 format))
(define +channel+ "#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)
lst
(cdr lst)))
(define (slist-ref lst index)
"Safe list-ref, see scdr"
(if (< (length lst) (+ index 1))
'()
(list-ref lst index)))
(define (slist-tail lst index)
"Safe list-tail, see scdr"
(if (< (length lst) (+ index 1))
'()
(list-tail lst index)))
(define (string-tail str index)
"List-tail for strings."
(list->string (list-tail (string->list str) index)))
(define (intersperse sep lst)
"Returns a new list with sep between each element of lst."
(cond
((or (null? lst)
(null? (cdr lst))) lst)
(else (cons (car lst)
(cons sep
(intersperse sep (cdr lst)))))))
(define (make-connection host port)
"Connect to irc server on host:port, returns a socket for reading and writing."
(let* ((sock (socket PF_INET SOCK_STREAM 0))
(con (connect sock
AF_INET
(car (vector-ref (gethost host) 4))
port)))
sock))
(define (pong stream)
(format stream "PONG :anna\r\n"))
(define (send-nick stream)
(format stream "NICK anna\r\n"))
(define (send-user stream)
(format stream "USER anna 0.0.0.0 anna :Anna\r\n"))
(define (join-channel stream)
(format stream "JOIN ~a\r\n" +channel+))
(define (send-message stream chn msg)
(format stream "PRIVMSG ~a :~a\r\n" chn msg))
(define (send-action stream chn action)
(format stream "PRIVMSG ~a :\x01ACTION ~a\x01\r\n" chn action))
(define (main-loop stream)
(let* ((inl (read-line stream))
(data (string-tokenize inl)))
;; debug print
(format #t "~s~%" data)
(cond
((equal? (car data) "PING")
(pong stream))
((equal? ":.hello" (slist-ref data 3))
(send-message stream +channel+ "hello, world"))
((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))
(interaction-environment)))))
((and (= 7 (length data))
(equal? (list-tail data 4) '("ACTION" "shoots" "anna")))
(send-action stream +channel+ "dies")
(close-port stream)
(exit)))
(main-loop stream)))
(let* ((io (make-connection "127.0.0.1" 6667)))
(send-nick io)
(send-user io)
(join-channel io)
(main-loop io))

View File

@ -1,73 +0,0 @@
#!/usr/bin/guile
!#
(use-modules (ice-9 rdelim)
(ice-9 textual-ports))
(define *channel* "#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)
lst
(cdr lst)))
(define (slist-ref lst index)
"Safe list-ref, see scdr"
(if (< (length lst) (+ index 1))
'()
(list-ref lst index)))
(define (slist-tail lst index)
"Safe list-tail, see scdr"
(if (< (length lst) (+ index 1))
'()
(list-tail lst index)))
(define (make-connection host port)
"Connect to irc server on host:port, returns a socket for reading and writing."
(let* ((sock (socket PF_INET SOCK_STREAM 0))
(con (connect sock
AF_INET
(car (vector-ref (gethost host) 4))
port)))
sock))
(define (send-nick stream)
(put-string stream "NICK fezbot\r\n"))
(define (send-user stream)
(put-string stream "USER fezbot 0.0.0.0 fezbot :Fez Bottingson\r\n"))
(define (join-channel stream)
(put-string stream (string-append "JOIN " *channel* "\r\n")))
(define (send-message stream chn msg)
(put-string stream (string-append "PRIVMSG " chn " :" msg "\r\n")))
(define (send-action stream chn action)
(put-string stream (string-append "PRIVMSG " chn " :\x01ACTION " action "\x01\r\n")))
(define (main-loop stream)
(let* ((inl (read-line stream))
(data (string-tokenize inl)))
(display data)
(newline)
(cond
((equal? (car data) "PING")
(send-message stream *channel* "PONG :foo"))
((equal? ":hello" (slist-ref data 3))
(send-message stream *channel* "hello, world"))
((and (= 7 (length data))
(equal? (list-tail data 4) '("ACTION" "shoots" "fezbot")))
(send-action stream *channel* "dies")
(close-port stream)
(exit)))
(main-loop stream)))
(let* ((io (make-connection "127.0.0.1" 6667)))
(send-nick io)
(send-user io)
(join-channel io)
(main-loop io))