Skip to content

Instantly share code, notes, and snippets.

@funrep
Created October 1, 2013 19:00
Show Gist options
  • Save funrep/6783356 to your computer and use it in GitHub Desktop.
Save funrep/6783356 to your computer and use it in GitHub Desktop.
#lang racket
(define config
(hash "server" "irc.codetalk.io"
"port" 6667
"chan" "#lobby"
"nick" "tob"
"name" "tob the bot"))
(define (connect)
(let-values ([(in out) (tcp-connect (hash-ref config "server")
(hash-ref config "port"))])
(login out)
(listen in out)))
(define (listen in out)
(let ([msg (get-msg in)])
(when (equal? "PING" (caddr msg)) (begin (write-msg out "PONG" (string-append ":" (cadddr msg))) (displayln "ponging")))
(when (equal? "266" (caddr msg)) (begin (write-msg out "JOIN" (hash-ref config "chan")) (displayln "joining")))
(displayln msg)
(listen in out)))
(define (get-msg in)
(let ([s (read-line in)])
(if (eof-object? s)
(error "connection failed")
(regexp-match #px"^(?:[:](\\S+) )?(\\S+)(?: (?!:)(.+?))?(?: [:](.+))?$" s))))
(define (write-msg out cmd s)
(let ([x (string-append cmd " " s "\r\n")])
(display x out)
(displayln (string-append "> " x))))
(define (login out)
(write-msg out "NICK" (hash-ref config "nick"))
(write-msg out "USER" (string-append (hash-ref config "nick") " 0 * :" (hash-ref config "name"))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment