|
@@ -17,10 +17,10 @@
|
|
|
;; Main entry point
|
|
|
;; Returns the main server loop thread (for synchronizing) and
|
|
|
;; a function for killing the server.
|
|
|
-(: serve (->* (#:port Integer #:hostname (U False String))
|
|
|
- ()
|
|
|
+(: serve (->* ()
|
|
|
+ (#:port Integer #:hostname (U False String))
|
|
|
(Values Thread (-> Void))))
|
|
|
-(define (serve #:port port-no #:hostname host)
|
|
|
+(define (serve #:port [port-no 6667] #:hostname [host #f])
|
|
|
(define serve-cust (make-custodian))
|
|
|
(parameterize ([current-custodian serve-cust])
|
|
|
(define listener (tcp-listen port-no 5 #t host))
|
|
@@ -48,7 +48,12 @@
|
|
|
(thread (lambda ()
|
|
|
(handle-user-messages user-conn cust)))))))
|
|
|
|
|
|
-
|
|
|
+;; When we accept a new IRC connection we need to do several things:
|
|
|
+;; 1. Receive the user nick/user information
|
|
|
+;; 2. Prepare the IO ports
|
|
|
+;; 3. Create a new WebSocket connection through chat.rkt
|
|
|
+;; 4. Set up a ping thread
|
|
|
+;; 5. Send the MOTD to the user
|
|
|
(: accept-irc-connection (-> Input-Port Output-Port Custodian irc-connection))
|
|
|
(define (accept-irc-connection in out cust)
|
|
|
(define nick
|
|
@@ -93,8 +98,19 @@
|
|
|
(define conn (irc-connection in out nick user ws-c))
|
|
|
;;(set-irc-connection-ws-conn! conn ws-c)
|
|
|
(welcome-user conn)
|
|
|
+ (void (thread (lambda () (ping-pong-thread conn))))
|
|
|
conn)
|
|
|
|
|
|
+
|
|
|
+(: ping-pong-thread (-> irc-connection Void))
|
|
|
+(define (ping-pong-thread conn)
|
|
|
+ (sleep 120)
|
|
|
+ (send-to-client conn (irc-message
|
|
|
+ ":lolcathost"
|
|
|
+ "PING"
|
|
|
+ '("fffffffffffffffffffffff")))
|
|
|
+ (ping-pong-thread conn))
|
|
|
+
|
|
|
;; Callbacks for the MoveNight chat api
|
|
|
|
|
|
(: notify-users (-> irc-connection Void))
|
|
@@ -166,10 +182,14 @@
|
|
|
;; The loop for handling commands from the client
|
|
|
|
|
|
;; return type is Nothing ==> the function does not terminate
|
|
|
+;; NB: we read from the client with the timeout
|
|
|
+;; of 333 > the frequency of PINGs
|
|
|
+;; so the client should respond to the PING within
|
|
|
+;; some number of seconds in order to keep the connection alive
|
|
|
(: handle-user-messages (-> irc-connection Custodian Nothing))
|
|
|
(define (handle-user-messages conn custodian)
|
|
|
(define nick (irc-connection-nick conn))
|
|
|
- (define msg (read-from-client conn))
|
|
|
+ (define msg (read-from-client conn #:timeout 333))
|
|
|
(match msg
|
|
|
[(irc-message _ "PING" (list ping))
|
|
|
(send-to-client conn
|
|
@@ -177,6 +197,8 @@
|
|
|
"PONG"
|
|
|
(list "lolcathost"
|
|
|
(string-append ":" ping))))]
|
|
|
+ [(irc-message _ "PONG" (list pong))
|
|
|
+ (void)]
|
|
|
[(irc-message _ "NICK" params)
|
|
|
;; TODO: propagate this info along the WS
|
|
|
(set-irc-connection-nick! conn (car params))]
|
|
@@ -242,15 +264,17 @@
|
|
|
[(irc-message _ "PRIVMSG" (list chan msg))
|
|
|
#:when (equal? chan channel)
|
|
|
(send-ws-message conn msg)]
|
|
|
+ [(irc-message _ "STATS" '())
|
|
|
+ (send-ws-message conn "/STATS")]
|
|
|
[(or (? eof-object?)
|
|
|
(irc-message _ "QUIT" _))
|
|
|
- ;; TODO remove from the user list
|
|
|
;; somehow attach this to a custodian?
|
|
|
- (log-info (format "Closing socket for ~a" (irc-connection-nick conn)))
|
|
|
+ (log-warning (format "ircd.rkt: Closing socket for ~a" (irc-connection-nick conn)))
|
|
|
(custodian-shutdown-all custodian)]
|
|
|
- [#f (void)]
|
|
|
+ [#f ;;; were unable to parse the string correctly
|
|
|
+ (void)]
|
|
|
[(var msg)
|
|
|
- (log-warning (format "handle-user-message: unknown message: ~a" msg))])
|
|
|
+ (log-warning (format "ircd.rkt/handle-user-message: unknown message: ~a" msg))])
|
|
|
(handle-user-messages conn custodian))
|
|
|
|
|
|
|
|
@@ -268,7 +292,8 @@
|
|
|
(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 ":[Mar 2020] NEW! bug fixes (erc support), displaying correct topic")
|
|
|
+ (notify-nick ":[Mar 2020] NEW! /STATS command & timeouts for broken connections")
|
|
|
+ (notify-nick ":[Mar 2020] bug fixes (erc support), displaying correct topic")
|
|
|
(notify-nick ":[Feb 2020] support for HexChat, JOIN & PARTs, html encoding of symbols")
|
|
|
(notify-nick ":-----------------------------------------------------------------------------")
|
|
|
(notify-nick ":If you encounter an error, try reconnecting!")
|