123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- #lang racket
- ;; single room IRC server
- (require "private/irc-functions.rkt"
- (prefix-in ws: "api.rkt")
- (prefix-in movie-night: "chat.rkt"))
- (provide (all-defined-out))
- (define movie-night-ws-url (make-parameter "wss://stream.ihatebeinga.live/ws"))
- (define users '())
- (define channel "#chats")
- ;; Main entry point
- ;; Returns the main server loop thread (for synchronizing) and
- ;; a function for killing the server.
- (define (serve #:port port-no #:hostname host)
- (define serve-cust (make-custodian))
- (parameterize ([current-custodian serve-cust])
- (define listener (tcp-listen port-no 5 #t host))
- (define (loop)
- (accept-and-handle listener)
- (loop))
- (define t (thread loop))
- (values t
- (lambda ()
- (custodian-shutdown-all serve-cust)))))
- ;; Accepting new clients
- (define (accept-and-handle listener)
- (define cust (make-custodian))
- (parameterize ([current-custodian cust])
- (define-values (in out) (tcp-accept listener))
- ;; once the ports are bound we spawn a new thread
- ;; in oreder to allow the main server loop to handler
- ;; other connections
- (thread
- (lambda ()
- (define user-conn (accept-irc-connection in out))
- (thread (lambda ()
- (handle-user-messages user-conn cust)))))))
-
- (define/contract (accept-irc-connection in out)
- (-> input-port? output-port? irc-connection?)
- (define fake-conn (irc-connection in out #f #f #f))
- (define nick
- (let loop ()
- (match (read-from-client fake-conn)
- [(irc-message _ "NICK" params)
- (car params)]
- [_ (loop)])))
- (log-info (format "~a connected" nick))
- (define user
- (let loop ()
- (match (read-from-client fake-conn)
- [(irc-message _ "USER" params)
- (car params)]
- [_ (loop)])))
- (file-stream-buffer-mode out 'line)
- (define conn (irc-connection in out nick user #f))
- (define ws-c
- (movie-night:make-connection
- (movie-night-ws-url)
- nick
- #:on-join (lambda (n) (on-join conn n))
- #:on-leave (lambda (n) (on-leave conn n))
- #:on-name-change (lambda (n1 n2) (on-name-change conn n1 n2))
- #:on-users (lambda (l) (set! users l) (notify-users conn))
- #:on-chat (lambda (from msg) (on-chat conn from msg))
- #:on-response (lambda (msg) (on-response conn msg))
- #:on-notify (lambda (msg) (on-response conn msg))))
- (set-irc-connection-ws-conn! conn ws-c)
- (welcome-user conn)
- conn)
- ;; Callbacks for the MoveNight chat api
- (define (notify-users conn)
- (send-to-client conn (irc-message
- ":lolcathost"
- RPL_NAMEREPLY
- (list (irc-connection-nick conn) "@" channel
- (format ":~a" (string-join users)))))
- (send-to-client conn (irc-message
- ":lolcathost"
- RPL_ENDOFNAMES
- (list (irc-connection-nick conn) channel ":End of /NAMES list."))))
- (define (on-chat conn from message)
- (unless (equal? from (irc-connection-nick conn))
- (send-to-client
- conn
- (irc-message (format "~a!~a@lolcathost" from from)
- "PRIVMSG"
- (list channel (format ":~a" message))))))
- (define (on-response conn message)
- (send-to-client
- conn
- (irc-message "OwO!SERVER@lolcathost"
- "NOTICE"
- (list channel
- (format ":!!! [ ~a ]" message)))))
- (define (on-join conn nick)
- (unless (equal? nick (irc-connection-nick conn))
- (send-to-client
- conn
- (irc-message (format "~a!~a@lolcathost" nick nick)
- "JOIN"
- (list channel)))))
- (define (on-leave conn nick)
- (unless (equal? nick (irc-connection-nick conn))
- (send-to-client
- conn
- (irc-message (format "~a!~a@lolcathost" nick nick)
- "PART"
- (list channel)))))
- (define (on-name-change conn old-nick new-nick)
- (unless (equal? old-nick (irc-connection-nick conn))
- (send-to-client
- conn
- (irc-message (format "~a!~a@lolcathost" old-nick old-nick)
- "NICK"
- (list new-nick)))))
- ;; The loop for handling commands from the client
- (define (handle-user-messages conn custodian)
- (define nick (irc-connection-nick conn))
- (match (read-from-client conn)
- [(irc-message _ "PING" params)
- (send-to-client conn (irc-message #f "PONG" params))]
- [(irc-message _ "NICK" params)
- ;; TODO: propagate this info along the WS
- (set-irc-connection-nick! conn (car params))]
- [(irc-message _ "JOIN" (list chan))
- #:when (equal? chan channel)
- (define c (irc-connection-ws-conn conn))
- (ws:send-join c (irc-connection-nick conn) "#00FFAA")
- (send-to-client conn (irc-message
- (format "~a!~a@lolcathost"
- (irc-connection-nick conn)
- (irc-connection-user conn))
- "JOIN"
- (list channel)))
- (send-to-client conn (irc-message
- ":lolcathost"
- RPL_TOPIC
- (list nick channel ":chatting hard")))
- (sleep 1.5) ;; is there a way around going to sleep? :-<
- (ws:send-users c)]
- [(irc-message _ "MODE" (cons chan _))
- #:when (equal? chan channel)
- (send-to-client conn (irc-message
- ":lolcathost"
- RPL_CHANNELMODEIS
- (list nick channel "+OwO")))]
- [(irc-message _ "LIST" _)
- (send-to-client conn (irc-message
- ":lolcathost"
- "002"
- (list nick " /list not implemented ")))
- ]
- [(irc-message _ "WHO" (list chan))
- #:when (equal? chan channel)
- (send-to-client conn (irc-message
- ":lolcathost"
- RPL_WHOREPLY
- (list nick channel
- (irc-connection-user conn)
- "lolcathost"
- "lolcathost"
- nick
- "H"
- ":0")))
- (send-to-client conn (irc-message
- ":lolcathost"
- RPL_ENDOFWHO
- (list nick channel ":End of /WHO list.")))]
- [(irc-message _ "PRIVMSG" (list chan msg))
- #:when (equal? chan channel)
- (send-ws-message conn msg)]
- [(or #f
- (? eof-object?)
- (irc-message _ "QUIT" _))
- ;; TODO remove from the user list
- ;; somehow attach this to a custodian?
- (custodian-shutdown-all custodian)]
- [(var msg)
- (log-warning (format "handle-user-message: unknown message: ~a" msg))])
- (handle-user-messages conn custodian))
- ;; Utils
- (define (send-ws-message conn msg)
- (ws:send-message (irc-connection-ws-conn conn) msg))
- (define (welcome-user conn)
- (define nick (irc-connection-nick conn))
- (define (notify-nick msg)
- (send-to-client conn
- (irc-message "lolcathost" "002" (list nick msg))))
- ;; "001" has to be a string, otherwise it's converted to 1
- (send-to-client conn (irc-message "lolcathost" "001" (list nick "OwO")))
- (notify-nick ":-----------------------------------------------------------------------------")
- (notify-nick ":If you encounter an error, try reconnecting!")
- (notify-nick ":This IRCd does not support many features, like user to user messages or channel lists.")
- (notify-nick ":[Feb 2020] NEW! features: support for HexChat, JOINs & PARTs.")
- (notify-nick ":A lot of things are broken, please submit an issues to ")
- (notify-nick ": --> <https://notabug.org/epi/movie-night-chat> <--")
- (notify-nick ":This pwogwam comes with ABSOWUTEWY NyO WAWWANTY!11oneone")
- (notify-nick ":This is fwee softwawe, and you awe wewcome to wedistwibute it")
- (notify-nick ":undew cewtain conditions; see the LICENSE file for details UwU :3")
- (notify-nick ":-----------------------------------------------------------------------------")
- (for ([x cofe])
- (notify-nick (string-append ":" x)))
- (notify-nick ":Welcome nyaa")
- (notify-nick (format ":Please join the channel ~a nyaa" channel)))
- (define cofe
- '(" ,. ,."
- " || ||"
- " ,''--''. ON THIS SERVER"
- " : (.)(.) : WE #cofe"
- " ,' `. "
- " : : "
- " : : hash tag IHBA gang"
- " -ctr- `._m____m_,' "))
|