     (let [login (:data packet) id (- @max-id 20)]
       (swap! clients         assoc channel login)
       (swap! clients-last-id assoc channel id)
-      (println packet @clients-last-id)
       (send! channel (json-str {:status "ok"}))
-      (dosync (ref-set all-msgs (conj @all-msgs {:id (next-id) :time (now) :msg (format "Пришел: %s" (@clients channel)) :author "system"}))))))
+      (let [author (@clients channel)]
+        (if (not-nil? author)(dosync (ref-set all-msgs (conj @all-msgs {:id (next-id)
+                                                                        :time (now)
+                                                                        :msg (format "Пришел: %s" author)
+                                                                        :author "system"}))))))))
 ; logout version 1
 (defn logout [channel]
   (do
-    (dosync (ref-set all-msgs (conj @all-msgs {:id (next-id) :time (now) :msg (format "Ушел: %s" (@clients channel)) :author "system"})))
+    (let [author (@clients channel)]
+      (if (not-nil? author) (dosync (ref-set all-msgs (conj @all-msgs {:id (next-id)
+                                                                       :time (now)
+                                                                       :msg (format "Ушел: %s" author)
+                                                                       :author "system"})))))
     (swap! clients dissoc channel)
     (swap! clients-last-id dissoc channel)
-    (println @clients-last-id)
     (clear-old-msgs)))
 (defmethod process-packet ["logout" "1"] [packet channel]
   (do

  (apply min (map (fn [msg] (:id msg)) @all-msgs)))
(defn clear-old-msgs []
  (let [min-id (min-clients-last-id)]
    (dosync (ref-set all-msgs (filter (fn [msg] (>= (:id msg) (- min-id 10))) @all-msgs)))))
; multi method, dispatch by command and version
(defn dispatcher [packet channel] [(:command packet) (:version packet)])
(defmulti  process-packet dispatcher)

    (let [author (@clients channel) text (strip-html-tags (:data packet))]
      (do
        (dosync (ref-set all-msgs (conj @all-msgs {:id (next-id) :time (now) :msg text :author author})))
        (send! channel (json-str {:status "ok"}))
        (clear-old-msgs)))))
; fetch version 1
(defmethod process-packet ["fetch" "1"] [packet channel]
  (let [last-id (@clients-last-id channel) cur-id (+ @max-id 1)]
    (do
      (if (not-nil? last-id)
        (do
          (send! channel
            (json-str {:status "ok" :data (sort-by :id < (filter (fn [msg] (>= (:id msg) last-id)) @all-msgs))}))
          (swap! clients-last-id assoc channel cur-id)
          (clear-old-msgs))))))
; perfomance dump version 1
