 ; login version 1
 (defmethod process-packet ["login" "1"] [packet channel]
   (do
-    (let [login (:data packet)]
+    (let [login (:data packet) id (- @max-id 20)]
       (swap! clients         assoc channel login)
-      (swap! clients-last-id assoc channel (- max-id 20)
+      (swap! clients-last-id assoc channel id)
       (println packet @clients-last-id)
-      (send! channel (json-str {:status "ok"})))))
+      (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"}))))))
 ; 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"})))
     (swap! clients dissoc channel)
     (swap! clients-last-id dissoc channel)
     (println @clients-last-id)

(defmethod process-packet ["logout" "1"] [packet channel]
  (do
    (logout channel)
    (println packet)
    (send! channel (json-str {:status "ok"}))))
; send version 1
(defmethod process-packet ["send" "1"] [packet channel]

  (let [last-id (@clients-last-id channel) cur-id (+ @max-id 1)]
    (do
      (println packet @clients-last-id)
      (if (not-nil? last-id)
        (do
          ;(println (reverse (filter (fn [msg] (>= (:id msg) last-id)) @all-msgs)))
          ;(println (filter (fn [msg] (>= (:id msg) last-id)) @all-msgs))
          (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
(defmethod process-packet ["dump" "1"] [packet channel] (dump))
; default method - simple print packet
