 destructors fry io io.encodings.utf8 kernel managed-server
 namespaces parser sequences sorting splitting strings.parser
 unicode.case unicode.categories calendar calendar.format
-locals multiline ;
+locals multiline io.encodings.binary io.encodings.string
+prettyprint ;
 IN: managed-server.chat
 
 TUPLE: chat-server < managed-server ;

Displays the documentation for a command.">
"help" add-command

[ drop clients keys ", " join print flush ]
<" Syntax: /who
Shows the list of connected users.">
"who" add-command

        [ "Unknown command: " prepend print flush ] if
    ] if-empty ;

: usage ( string -- )
    chat-docs get at print flush ;

: username-taken-string ( username -- string )
    "The username ``" "'' is already in use; try again." surround ;

: warn-name-changed ( old new -- )
    [
        [ line-beginning "``" ] 2dip
        [ "'' is now known as ``" ] dip "''"
    ] "" append-outputs-as send-everyone ;

: handle-nick ( string -- )
    [
        "nick" usage
    ] [
        dup clients key? [
            username-taken-string print flush
        ] [
            [ username swap warn-name-changed ]
            [ username clients rename-at ]
            [ client (>>username) ] tri
        ] if
    ] if-empty ;

:: add-command ( quot docs key -- )
    quot key commands get set-at
    docs key chat-docs get set-at ;
