Browse Source

simplified reading and writing of bytes to and from server

main
Jesse Laprade 2 months ago
parent
commit
f8a5068164
  1. 25
      src/ruth.rkt

25
src/ruth.rkt

@ -136,32 +136,30 @@ @@ -136,32 +136,30 @@
;; buffered data
;; Source:
;; https://docs.racket-lang.org/reference/port-buffers.html#%28def._%28%28quote._~23~25kernel%29._flush-output%29%29
(define (send-bytes/utf-8 a-string)
(let* ([string-rn (string-append a-string "\r\n")]
[string-as-bytes/utf-8 (string->bytes/utf-8 string-rn)])
(write-bytes string-as-bytes/utf-8 to-server)
(flush-output to-server)))
(define (send a-string)
(write-string (string-append a-string "\r\n") to-server)
(flush-output to-server))
(define (ping-check server-data-string)
(when (equal? "PING" (substring server-data-string 0 4))
(send-bytes/utf-8 "PONG :message")))
(send "PONG :message")))
(define (send-message channel message)
(send-bytes/utf-8 (format "PRIVMSG ~a :~a" channel message)))
(send (format "PRIVMSG ~a :~a" channel message)))
(define (send-action channel action)
(send-bytes/utf-8 (format "PRIVMSG ~a :\x01ACTION ~a\x01" channel action)))
(send (format "PRIVMSG ~a :\x01ACTION ~a\x01" channel action)))
(define (send-pass)
(send-bytes/utf-8
(send
(format "PASS ~a" (config-ref 'pass))))
(define (send-nick)
(send-bytes/utf-8
(send
(format "NICK ~a" (config-ref 'nickname))))
(define (send-user)
(send-bytes/utf-8
(send
(format "USER ~a 0.0.0.0 ~a :~a"
(config-ref 'nickname)
(config-ref 'username)
@ -169,7 +167,7 @@ @@ -169,7 +167,7 @@
(define (join-channels)
(for ([channel (config-ref 'channels)])
(send-bytes/utf-8 (format "JOIN ~a" channel))))
(send (format "JOIN ~a" channel))))
(define (check-for-e channel nickname message)
(let* ([oulipo-channel? (equal? channel "#oulipo")]
@ -257,8 +255,7 @@ @@ -257,8 +255,7 @@
;; (connection-loop) is outside of the (let ...), so each time the loop
;; is called, new values are assigned inside of the let statement
(define (connection-loop)
(let* ([lineof-bytes (read-bytes-line from-server)]
[server-data-string (bytes->string/utf-8 lineof-bytes)])
(let ([server-data-string (read-line from-server)])
(displayln server-data-string)
(ping-check server-data-string)
(commands-listen server-data-string)

Loading…
Cancel
Save