anchor.diff:               (client-delete client)))))
   
   (defun create-client (connection)
  +  "This procedure create a new client based on CONNECTION made by
  +  USOCKET:SOCKET-ACCEPT. This shit create a lot of side effects as messages
  +  if the debug is on because this makes all the log stuff to make analysis"
     (debug-format t "Incoming connection from ~a ~%" (socket-peer-address connection))
     (let ((client-stream (socket-stream connection)))
       (write-line "> Type your username: " client-stream)
  
positive.code: ;; Manoel Vilela
  
  (defpackage :lisp-chat-server
    (:use :usocket :cl :lisp-chat-config)
    (:export :main))
  
  (in-package :lisp-chat-server)
  
  ;; constants
  ;; (don't use defconstant because create a bug as ASDF system compile time)
  (defparameter +commands-names+ '("/users" "/help" "/log" "/quit" "/uptime"))
  (defparameter +day-names+ '("Monday" "Tuesday" "Wednesday"
                              "Thursday" "Friday" "Saturday" "Sunday"))
  (defparameter +uptime+ (multiple-value-list (get-decoded-time)))
  
  ;; global vars
  (defparameter *clients* nil)
  (defparameter *messages-stack* nil)
  (defparameter *messages-log* nil)
  
  
  ;; thread control
  (defvar *message-semaphore* (sb-thread:make-semaphore :name "message semaphore"
                                                        :count 0))
  (defvar *client-mutex* (sb-thread:make-mutex :name "client list mutex"))
  
  
  (defstruct message
    from content time)
  
  (defstruct client
    name socket address)
  
  
  (defun socket-peer-address (socket)
    (format nil "~{~a~^.~}\:~a"
            (map 'list #'identity (get-peer-address socket))
            (get-peer-port socket)))
  
  (defun client-stream (c)
    (socket-stream (client-socket c)))
  
  
  (defun debug-format (&rest args)
    (if *debug*
        (apply #'format args)))
  
  
  (defun get-time ()
    (multiple-value-bind (second minute hour)
        (get-decoded-time)
      (format nil "~2,'0d:~2,'0d:~2,'0d" hour minute second)))
  
  
  (defun formated-message (message)
    (format nil "|~a| [~a]: ~a"
            (message-time message)
            (message-from message)
            (message-content message)))
  
  (defun command-message (content)
    (let* ((from "@server")
           (time (get-time))
           (message (make-message :from from :content content :time time)))
      (formated-message message)))
  
  (defun call-command-by-name (string)
    (funcall (find-symbol (string-upcase string) :lisp-chat-server)))
  
  ;; user commands prefixed with /
  (defun /users ()
    (command-message (format nil "~{~a~^, ~}" (mapcar #'client-name *clients*))))
  
  
  (defun /help ()
    (command-message (format nil "~{~a~^, ~}" +commands-names+)))
  
  
  (defun /log (&optional (depth 20))
    (format nil "~{~a~^~%~}" (reverse (subseq *messages-log* 0
                                              (min depth (length *messages-log*))))))
  
  (defun /uptime ()
    (multiple-value-bind
          (second minute hour date month year day-of-week dst-p tz)
        (values-list +uptime+)
      (declare (ignore dst-p))
      (command-message
       (format nil
               "Server online since ~2,'0d:~2,'0d:~2,'0d of ~a, ~2,'0d/~2,'0d/~d (GMT~@d)"
               hour
               minute
               second
               (nth day-of-week +day-names+)
               month
               date
               year
               (- tz)))))
  
  
  (defun push-message (from content)
    (push (make-message :from from
                        :content content
                        :time (get-time))
          *messages-stack*)
    (sb-thread:signal-semaphore *message-semaphore*))
  
  (defun client-delete (client)
    (sb-thread:with-mutex (*client-mutex*)
      (setf *clients* (remove-if (lambda (c)
                                   (equal (client-address c)
                                          (client-address client)))
  
negative.code:     (let ((client (make-client :name (read-line client-stream)
                                 :socket connection
                                 :address (socket-peer-address connection))))
        (with-mutex (*client-mutex*)
          (debug-format t "Added new user ~a@~a ~%"
                        (client-name client)
                        (client-address client))
          (push client *clients*))
        (push-message "@server" (format nil "The user ~s joined to the party!" (client-name client)))
        (make-thread #'client-reader
                               :name (format nil "~a reader thread" (client-name client))
                               :arguments (list client)))))
  
  ;; a function defined to handle the errors of client thread
  (defun safe-client-thread (connection)
    "This function is a wrapper for CREATE-CLIENT treating the
  exceptions."
    (handler-case (create-client connection)
      (end-of-file () nil)
      (usocket:address-in-use-error () nil)))
  
  (defun message-broadcast ()
    "This procedure is a general independent thread to run brodcasting
     all the clients when a message is ping on this server"
    (loop when (wait-on-semaphore *message-semaphore*)
            do (let ((message (formated-message (pop *messages-stack*))))
                 (push message *messages-log*)
                 (loop for client in *clients*
  