ircd.rkt 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #lang racket
  2. ;; single room IRC server
  3. (require "private/irc-functions.rkt"
  4. (prefix-in ws: "api.rkt")
  5. (prefix-in movie-night: "chat.rkt"))
  6. (provide (all-defined-out))
  7. (define movie-night-ws-url (make-parameter "wss://stream.ihatebeinga.live/ws"))
  8. (define users '())
  9. (define channel "#chats")
  10. ;; Main entry point
  11. ;; Returns the main server loop thread (for synchronizing) and
  12. ;; a function for killing the server.
  13. (define (serve #:port port-no #:hostname host)
  14. (define serve-cust (make-custodian))
  15. (parameterize ([current-custodian serve-cust])
  16. (define listener (tcp-listen port-no 5 #t host))
  17. (define (loop)
  18. (accept-and-handle listener)
  19. (loop))
  20. (define t (thread loop))
  21. (values t
  22. (lambda ()
  23. (custodian-shutdown-all serve-cust)))))
  24. ;; Accepting new clients
  25. (define (accept-and-handle listener)
  26. (define cust (make-custodian))
  27. (parameterize ([current-custodian cust])
  28. (define-values (in out) (tcp-accept listener))
  29. ;; once the ports are bound we spawn a new thread
  30. ;; in oreder to allow the main server loop to handler
  31. ;; other connections
  32. (thread
  33. (lambda ()
  34. (define user-conn (accept-irc-connection in out))
  35. (thread (lambda ()
  36. (handle-user-messages user-conn cust)))))))
  37. (define/contract (accept-irc-connection in out)
  38. (-> input-port? output-port? irc-connection?)
  39. (define fake-conn (irc-connection in out #f #f #f))
  40. (define nick
  41. (let loop ()
  42. (match (read-from-client fake-conn)
  43. [(irc-message _ "NICK" params)
  44. (car params)]
  45. [_ (loop)])))
  46. (log-info (format "~a connected" nick))
  47. (define user
  48. (let loop ()
  49. (match (read-from-client fake-conn)
  50. [(irc-message _ "USER" params)
  51. (car params)]
  52. [_ (loop)])))
  53. (file-stream-buffer-mode out 'line)
  54. (define conn (irc-connection in out nick user #f))
  55. (define ws-c
  56. (movie-night:make-connection
  57. (movie-night-ws-url)
  58. nick
  59. #:on-join (lambda (n) (on-join conn n))
  60. #:on-leave (lambda (n) (on-leave conn n))
  61. #:on-name-change (lambda (n1 n2) (on-name-change conn n1 n2))
  62. #:on-users (lambda (l) (set! users l))
  63. #:on-chat (lambda (from msg) (on-chat conn from msg))
  64. #:on-response (lambda (msg) (on-response conn msg))
  65. #:on-notify (lambda (msg) (on-response conn msg))))
  66. (set-irc-connection-ws-conn! conn ws-c)
  67. (welcome-user conn)
  68. conn)
  69. ;; Callbacks for the MoveNight chat api
  70. (define (on-chat conn from message)
  71. (unless (equal? from (irc-connection-nick conn))
  72. (send-to-client
  73. conn
  74. (irc-message (format "~a!~a@lolcathost" from from)
  75. "PRIVMSG"
  76. (list channel (format ":~a" message))))))
  77. (define (on-response conn message)
  78. (send-to-client
  79. conn
  80. (irc-message "OwO!SERVER@lolcathost"
  81. "PRIVMSG"
  82. (list (irc-connection-nick conn)
  83. (format ":!!! [ ~a ]" message)))))
  84. (define (on-join conn nick)
  85. (unless (equal? nick (irc-connection-nick conn))
  86. (send-to-client
  87. conn
  88. (irc-message nick
  89. "JOIN"
  90. (list channel)))))
  91. (define (on-leave conn nick)
  92. (unless (equal? nick (irc-connection-nick conn))
  93. (send-to-client
  94. conn
  95. (irc-message nick
  96. "PART"
  97. (list channel)))))
  98. (define (on-name-change conn old-nick new-nick)
  99. (unless (equal? old-nick (irc-connection-nick conn))
  100. (send-to-client
  101. conn
  102. (irc-message old-nick
  103. "NICK"
  104. (list new-nick)))))
  105. ;; The loop for handling commands from the client
  106. (define (handle-user-messages conn custodian)
  107. (define nick (irc-connection-nick conn))
  108. (match (read-from-client conn)
  109. [(irc-message _ "PING" params)
  110. (send-to-client conn (irc-message #f "PONG" params))]
  111. [(irc-message _ "NICK" params)
  112. ;; TODO: propagate this info along the WS
  113. (set-irc-connection-nick! conn (car params))]
  114. [(irc-message _ "JOIN" (list chan))
  115. #:when (equal? chan channel)
  116. (send-to-client conn (irc-message
  117. (format "~a!~a@lolcathost"
  118. (irc-connection-nick conn)
  119. (irc-connection-user conn))
  120. "JOIN"
  121. (list channel)))
  122. (send-to-client conn (irc-message
  123. ":lolcathost"
  124. RPL_TOPIC
  125. (list nick channel ":chatting hard")))
  126. ;; TODO: proper user list
  127. (send-to-client conn (irc-message
  128. ":lolcathost"
  129. RPL_NAMEREPLY
  130. (list nick "@" channel
  131. (format ":~a" nick))))
  132. (send-to-client conn (irc-message
  133. ":lolcathost"
  134. RPL_ENDOFNAMES
  135. (list nick channel ":End of /NAMES list.")))]
  136. [(irc-message _ "MODE" (cons chan _))
  137. #:when (equal? chan channel)
  138. (send-to-client conn (irc-message
  139. ":lolcathost"
  140. RPL_CHANNELMODEIS
  141. (list nick channel "+OwO")))]
  142. [(irc-message _ "LIST" _)
  143. (send-to-client conn (irc-message
  144. ":lolcathost"
  145. "002"
  146. (list nick " /list not implemented ")))
  147. ]
  148. [(irc-message _ "WHO" (list chan))
  149. #:when (equal? chan channel)
  150. (send-to-client conn (irc-message
  151. ":lolcathost"
  152. RPL_WHOREPLY
  153. (list nick channel
  154. (irc-connection-user conn)
  155. "lolcathost"
  156. "lolcathost"
  157. nick
  158. "H"
  159. ":0")))
  160. (send-to-client conn (irc-message
  161. ":lolcathost"
  162. RPL_ENDOFWHO
  163. (list nick channel ":End of /WHO list.")))]
  164. [(irc-message _ "PRIVMSG" (list chan msg))
  165. #:when (equal? chan channel)
  166. (send-ws-message conn msg)]
  167. [(or #f
  168. (? eof-object?)
  169. (irc-message _ "QUIT" _))
  170. ;; TODO remove from the user list
  171. ;; somehow attach this to a custodian?
  172. (custodian-shutdown-all custodian)]
  173. [(var msg)
  174. (log-warning (format "handle-user-message: unknown message: ~a" msg))])
  175. (handle-user-messages conn custodian))
  176. ;; Utils
  177. (define (send-ws-message conn msg)
  178. (ws:send-message (irc-connection-ws-conn conn) msg))
  179. (define (welcome-user conn)
  180. (define nick (irc-connection-nick conn))
  181. (define (notify-nick msg)
  182. (send-to-client conn
  183. (irc-message "lolcathost" "002" (list nick msg))))
  184. ;; "001" has to be a string, otherwise it's converted to 1
  185. (send-to-client conn (irc-message "lolcathost" "001" (list nick "OwO")))
  186. (notify-nick ":-----------------------------------------------------------------------------")
  187. (notify-nick ":If you encounter an error, try reconnecting!")
  188. (notify-nick ":This IRCd does not support many features, like user to user messages or channel lists.")
  189. (notify-nick ":[Feb 2020] NEW! features: support for HexChat, JOINs & PARTs.")
  190. (notify-nick ":A lot of things are broken, please submit an issues to ")
  191. (notify-nick ": --> <https://notabug.org/epi/movie-night-chat> <--")
  192. (notify-nick ":This pwogwam comes with ABSOWUTEWY NyO WAWWANTY!11oneone")
  193. (notify-nick ":This is fwee softwawe, and you awe wewcome to wedistwibute it")
  194. (notify-nick ":undew cewtain conditions; see the LICENSE file for details UwU :3")
  195. (notify-nick ":-----------------------------------------------------------------------------")
  196. (for ([x cofe])
  197. (notify-nick (string-append ":" x)))
  198. (notify-nick ":Welcome nyaa")
  199. (notify-nick (format ":Please join the channel ~a nyaa" channel)))
  200. (define cofe
  201. '(" ,. ,."
  202. " || ||"
  203. " ,''--''. ON THIS SERVER"
  204. " : (.)(.) : WE #cofe"
  205. " ,' `. "
  206. " : : "
  207. " : : hash tag IHBA gang"
  208. " -ctr- `._m____m_,' "))