;;;;Here there be aliens
(mapc 'require '(asdf usocket))

(setq *print-pretty* t)

(defvar *lf* nil)
(defvar *pc* 0)
(defvar *post* nil)
(defun alist-and-body (s c n)
 `(setq *post* (cons 
    (let ((utime (get-universal-time)))
     (multiple-value-bind
      (second minute hour date month year day)
      (decode-universal-time utime 0)
      (append (read ,s t nil t)
       (list
       (cons 'pc (incf *pc*))
       (cons 'second second)
       (cons 'minute minute)
       (cons 'hour hour)
       (cons 'date date)
       (cons 'month
        (cdr (assoc month '((1 . jan) (2 . feb) (3 . mar) (4 . apr) 
                           (5 . may) (6 . jun) (7 . jul) (8 . aug)
                           (9 . sep) (10 . oct) (11 . nov) (12 . dec)))))
       (cons 'year year)
       (cons 'day
        (cdr (assoc day '((0 . mon) (1 . tue) (2 . wed) (3 . thu)
                          (4 . fri) (5 . sat) (6 . sun)))))))))
   (loop for r = (read ,s t nil t)
    while (not (eq r '}))
    collect r
    finally (read-char-no-hang ,s t nil t)))))

(set-dispatch-macro-character #\# #\{ #'alist-and-body)

(defun post (&optional (msg nil) (server "127.0.0.1") (port 45678))
 (usocket:with-client-socket (socket stream server port :element-type 'character)
  (unwind-protect
   (let ((*read-eval* nil))
    (format stream "7~s~%~s~%" (car msg) (cdr msg))
    (loop for response = (read stream nil nil)
     while response collecting response))
   (usocket:socket-close socket))))
 
(defun strm2syms (stream)
  (let ((symbols (list))
        (curword (list)))
   (loop for ch = (read-char stream nil nil)
    do (if (or (null ch) (char= ch #\Space))
        (progn (push (with-input-from-string 
                       (s (format nil "|~a|" 
                           (coerce (nreverse curword) 'string)))
                       (read s)) symbols) (setf curword (list)))
        (when ch (push ch curword)))
    while ch finally (return-from strm2syms (reverse symbols)))))

(defun unjust (stream)
 (let ((string (format nil "~a" (strm2syms stream))))
  (values (subseq string 1 (1- (length string))))))

(defun linewise (filepath)
 (with-open-file (file filepath :direction :input)
  (loop for line = (read-line file nil nil) while line collecting
   (with-input-from-string (in line) (unjust in)))))