anchor.diff:  ;; Manoel Vilela
   
   (defpackage :lisp-chat-server
  -  (:use :usocket :cl :lisp-chat-config)
  +  (:use :usocket :cl :lisp-chat-config :sb-thread)
     (: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
  +(defvar *day-names* '("Monday" "Tuesday" "Wednesday"
  +                      "Thursday" "Friday" "Saturday" "Sunday"))
  +(defvar *uptime* (multiple-value-list (get-decoded-time)))
  +(defparameter *commands-names* '("/users" "/help" "/log" "/quit" "/uptime"))
   (defparameter *clients* nil)
   (defparameter *messages-stack* nil)
   (defparameter *messages-log* nil)
  -
  +(defparameter *server-nickname* "@server")
   
   ;; thread control
  -(defvar *message-semaphore* (sb-thread:make-semaphore :name "message semaphore"
  +(defvar *message-semaphore* (make-semaphore :name "message semaphore"
                                                         :count 0))
  -(defvar *client-mutex* (sb-thread:make-mutex :name "client list mutex"))
  +(defvar *client-mutex* (make-mutex :name "client list mutex"))
  +
   
   
   (defstruct message
  -  from content time)
  +  "This structure abstract the type message with is saved
  +   into *messages-log* and until consumed, temporally pushed
  +   to *messages-stack*. FROM, CONTENT and TIME has type string"
  +  from
  +  content
  +  time )
   
   (defstruct client
  -  name socket address)
  +  "This structure handle the creation/control of the clients of the server.
  +   NAME is a string. Socket is a USOCKET:SOCKET and address is a ipv4 encoded
  +   string. "
  +  name
  +  socket
  +  address)
   
   
   (defun socket-peer-address (socket)
  +  "Given a USOCKET:SOCKET instance return a ipv4 encoded IP string"
     (format nil "~{~a~^.~}\:~a"
             (map 'list #'identity (get-peer-address socket))
             (get-peer-port socket)))
   
   (defun client-stream (c)
  +  "Select the stream IO from the client"
     (socket-stream (client-socket c)))
   
   
   (defun debug-format (&rest args)
  +  "If *debug* from lisp-chat-config is true, print debug info on
  +   running based on ARGS"
     (if *debug*
         (apply #'format args)))
   
   
   (defun get-time ()
  +  "Return a encoded string as HH:MM:SS based on the current timestamp."
     (multiple-value-bind (second minute hour)
         (get-decoded-time)
       (format nil "~2,'0d:~2,'0d:~2,'0d" hour minute second)))
   
   
   (defun formated-message (message)
  +  "The default message format of this server. MESSAGE is a string
  +   Changing this reflects all the layout from client/server.
  +   Probably this would be the MFRP: Manoel Fucking Raw Protocol.
  +   Because this we can still use netcat as client for lisp-chat."
     (format nil "|~a| [~a]: ~a"
             (message-time message)
             (message-from message)
             (message-content message)))
   
   (defun command-message (content)
  -  (let* ((from "@server")
  +  "This function prepare the CONTENT as a message by the @server"
  +  (let* ((from *server-nickname*)
            (time (get-time))
            (message (make-message :from from :content content :time time)))
       (formated-message message)))
   
   (defun call-command-by-name (string)
  +  "Wow, this is a horrible hack to get a string as symbol for functions/command
  +  like /help /users /log and so on."
     (funcall (find-symbol (string-upcase string) :lisp-chat-server)))
   
   ;; user commands prefixed with /
   (defun /users ()
  +  "Return a list separated by commas of the currently logged users"
     (command-message (format nil "~{~a~^, ~}" (mapcar #'client-name *clients*))))
   
   
   (defun /help ()
  -  (command-message (format nil "~{~a~^, ~}" +commands-names+)))
  +  "Show a list of the available commands of lisp-chat"
  +  (command-message (format nil "~{~a~^, ~}" *commands-names*)))
   
   
   (defun /log (&optional (depth 20))
  +  "Show the last messages typed on the server.
  +   DEPTH is optional number of messages frames from log"
     (format nil "~{~a~^~%~}" (reverse (subseq *messages-log* 0
                                               (min depth (length *messages-log*))))))
   
   (defun /uptime ()
  +  "Return a string nice encoded to preset the uptime since the server started."
     (multiple-value-bind
           (second minute hour date month year day-of-week dst-p tz)
  -      (values-list +uptime+)
  +      (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
  +             hour minute second
  +             (nth day-of-week *day-names*)
  +             month date year
                (- tz)))))
   
   
   (defun push-message (from content)
  +  "Push a messaged FROM as CONTENT into the *messages-stack*"
     (push (make-message :from from
                         :content content
                         :time (get-time))
           *messages-stack*)
  -  (sb-thread:signal-semaphore *message-semaphore*))
  +  (signal-semaphore *message-semaphore*))
   
   (defun client-delete (client)
  -  (sb-thread:with-mutex (*client-mutex*)
  +  "Delete a CLIENT from the list *clients*"
  +  (with-mutex (*client-mutex*)
       (setf *clients* (remove-if (lambda (c)
                                    (equal (client-address c)
                                           (client-address client)))
  
positive.code:   (socket-close (client-socket client)))
  
  (defun send-message (client message)
    (let ((stream (client-stream client)))
      (write-line message stream)
      (finish-output stream)))
  
  (defun client-reader-routine (client)
    (loop for message = (read-line (client-stream client))
          while (not (equal message "/quit"))
          if (member message +commands-names+ :test #'equal)
            do (send-message client (call-command-by-name message))
          else
            when (> (length message) 0)
  
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*
  