#lang imperative-syndicate

(require "protocol.rkt")
(require "message.rkt")

(require/activate imperative-syndicate/reload)

(spawn #:name 'channel-factory
       (stop-when-reloaded)

       (during (ircd-channel-member $Ch _)
         (assert (ircd-channel Ch)))

       (during/spawn (ircd-channel $Ch)
         #:name (ircd-channel Ch)
         (field [topic #f])
         (assert (ircd-channel-topic Ch (topic)))

         (define/query-count user-count (ircd-channel-member Ch $who) 'any)
         (assert (ircd-channel-user-count Ch (hash-ref (user-count) 'any 0)))

         (on (message (ircd-action $who (irc-message _ "MODE" (list Ch "b") _)))
             (send! (ircd-event who (irc-message server-prefix 368 (list (lookup-nick who) Ch)
                                                 "End of Channel Ban List"))))

         (on (message (ircd-action $who (irc-message _ "MODE" (list Ch) _)))
             (send! (ircd-event who (irc-message server-prefix 324
                                                 (list (lookup-nick who) Ch "+") #f))))

         (on (message (ircd-action _ (irc-message _ "TOPIC" (list Ch) $new-topic)))
             (topic new-topic))))
