From 09f21b7abfdbe78a515833760f7567bd23614757 Mon Sep 17 00:00:00 2001 From: ckonstanski Date: Wed, 23 Dec 2020 18:40:07 -0700 Subject: moved json functions to separate library --- file/file-utils.lisp | 110 --------------- http/html.lisp | 324 -------------------------------------------- http/httputils.lisp | 66 --------- json/json-utils.lisp | 72 ---------- ldapadmin.asd | 12 +- service/auth-service.lisp | 2 +- service/home-service.lisp | 6 +- service/login-service.lisp | 4 +- service/logout-service.lisp | 2 +- service/menu-service.lisp | 2 +- 10 files changed, 11 insertions(+), 589 deletions(-) delete mode 100644 file/file-utils.lisp delete mode 100644 http/html.lisp delete mode 100644 http/httputils.lisp delete mode 100644 json/json-utils.lisp diff --git a/file/file-utils.lisp b/file/file-utils.lisp deleted file mode 100644 index 934dc41..0000000 --- a/file/file-utils.lisp +++ /dev/null @@ -1,110 +0,0 @@ -;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*- -(declaim (optimize (speed 0) (safety 3) (debug 3))) - -(in-package #:ldapadmin) - -(defun compile-and-load (filename) - "Compiles and then loads a file. `filename' should not have an -extension, such as .fasl or .lisp." - (compile-file filename) - (load filename)) - -(defun write-pid-file () - (org-ckons-core::shell-wrapper (format nil "echo ~a >~a/~a.pid" (sb-posix:getpid) (sb-posix:getenv "HOME") (string-downcase (package-name *package*))))) - -(defun file-to-list (infile) - "Reads `infile' and returns a list, where each atom is a single -line of the file. `infile' can be a string or a pathname -object." - (let ((infile-list ())) - (with-open-file (filehandle infile :if-does-not-exist nil) - (if (streamp filehandle) - (progn - (loop for line = (read-line filehandle nil) - while line do - (push line infile-list)) - (nreverse infile-list)) - nil)))) - -(defun parse-csv-line (line) - "Parses a single line of CSV input into a list of string fields." - (let ((quoted-string-mode nil) - (line-list ()) - (field-collector "")) - (loop for this-char across (ppcre:regex-replace-all #\Return line "") do - (cond ((equal this-char #\") - (setf quoted-string-mode (not quoted-string-mode))) - ((and (equal this-char #\,) (not quoted-string-mode)) - (push field-collector line-list) - (setf field-collector "")) - (t - (setf field-collector (format nil "~a~a" field-collector this-char))))) - ;; get the field after the last comma - (push field-collector line-list) - (nreverse line-list))) - -(defmacro dofile ((line filename) &body body) - "Wrapper for the common task of opening a file and reading it one -line at a time." - (let ((stream (gensym))) - `(with-open-file (,stream ,filename) - (loop for ,line = (read-line ,stream nil nil) - while ,line do ,@body)))) - -(defun mkdir (path) - (uffi:run-shell-command (format nil "mkdir -p ~a" path))) - -(defun copy-file (source destination) - "Copies file from `source' to `destination'. If the destination -directory does not exist, it will be created." - (when (and (file-exists-p source) - (file-p source) - (not (org-ckons-core::null-or-empty-p destination))) - (let ((destination-parts (nreverse (remove-if #'org-ckons-core::null-or-empty-p (ppcre:split "/" destination)))) - (destination-file "") - (destination-directory "")) - (setf destination-file (pop destination-parts)) - (loop for part in (nreverse destination-parts) do - (setf destination-directory (format nil "~a/~a" destination-directory part))) - (mkdir destination-directory) - (uffi:run-shell-command (format nil "cp '~a' '~a'" source destination))))) - -(defun purge-old-files (directory-path) - "Deletes everything out of a directory that is older than 8 hours -old." - (uffi:run-shell-command (format nil "find ~a/* -mmin 480 |xargs rm -rf" directory-path))) - -(defun purge-files-regex (directory-path regex) - "Deletes everything out of a directory whose name matches the -`regex'." - (uffi:run-shell-command (format nil "find ~a/* -regex '~a' |xargs rm -rf" directory-path regex))) - -(defun file-exists-p (file-path) - (equal (car (org-ckons-core::shell-wrapper (format nil "if test -f '~a'; then echo 0; else echo 1; fi" file-path))) "0")) - -(defun file-mtime (file-path) - (let ((output (car (org-ckons-core::shell-wrapper (format nil "ls --full-time '~a' |awk '{ print $6,$7 }' |awk -F. '{ print $1; }'" file-path))))) - (if (not (org-ckons-core::match-it "^\\d\\d\\d\\d-\\d\\d-\\d\\d \\d\\d:\\d\\d:\\d\\d$" output)) - (error 'handled-error :text (format nil "Error in `file-mtime': ~a" output)) - output))) - -(defun directory-p (absolute-path) - (if (car (org-ckons-core::shell-wrapper (format nil "file '~a' |grep 'directory'" absolute-path))) t nil)) - -(defun symlink-p (absolute-path) - (if (car (org-ckons-core::shell-wrapper (format nil "file '~a' |grep 'symbolic link'" absolute-path))) t nil)) - -(defun file-p (absolute-path) - (if (or (directory-p absolute-path) (symlink-p absolute-path)) nil t)) - -(defun path-contained-p (root-path path-to-check) - "Returns `(,root-path) if `path-to-check' is contained within `root-path', -`nil' otherwise." - (org-ckons-core::match-it root-path path-to-check)) - -(defun find-files (working-dir base-dir pattern) - (org-ckons-core::shell-wrapper (format nil - "pushd ~a >/dev/null 2>&1 ; find ~a -iname '~a' ; popd >/dev/null 2>&1" - working-dir - base-dir - pattern))) diff --git a/http/html.lisp b/http/html.lisp deleted file mode 100644 index 99f0a4b..0000000 --- a/http/html.lisp +++ /dev/null @@ -1,324 +0,0 @@ -;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*- -(declaim (optimize (speed 0) (safety 3) (debug 3))) - -;; Lifted directly from araneida. - -(in-package :ldapadmin) - -;;; XXX fix this, it's not correct -(defun html-reserved-p (c) - (member c '(#\< #\" #\> #\&))) - -(defun html-escape (html-string) - (apply #'concatenate 'string - (loop for c across html-string - if (html-reserved-p c) - collect (format nil "&#~A;" (char-code c)) - else if (eql c #\Newline) collect "
" - else collect (string c)))) - -(defun s. (&rest args) - "Concatenate ARGS as strings" - (declare (optimize (speed 3))) - (let ((*print-pretty* nil)) - (with-output-to-string (out) - (dolist (arg args) - (princ arg out))))) - -(defun html-escape-tag (tag attrs content) - (declare (ignore tag attrs)) - (s. (mapcar #'html-escape content))) - -(setf (get 'escape 'html-converter) #'html-escape-tag) -(setf (get 'null 'html-converter) #'princ-to-string) - -(macrolet ((html-attr-body () - `(with-output-to-string (o) - (loop for (att val . rest) on attr by #'cddr do - (cond - #+parenscript((and (symbolp att) (equal (symbol-name 'css) (symbol-name att))) - (progn - (princ " " o) - (princ "style=\"" o) - (princ (val-printer (parenscript::css-inline-func val)) o) - (princ "\"" o))) - ((symbolp att) - (progn - (princ " " o) - (princ (symbol-name att) o) - (princ "=\"" o) - (princ (val-printer val) o) - (princ "\"" o))) - (t - (error "attribute ~S is not a symbol in attribute list ~S" att attr))))))) - (defun html-attr (attr) - (macrolet ((val-printer (val) - val)) - (html-attr-body))) - (defun html-attr-escaped (attr) - (macrolet ((val-printer (val) - `(html-escape ,val))) - (html-attr-body)))) - -(defun empty-element-p (tag) - (member (intern (symbol-name tag) #.*package*) '())) - -(defmacro defhtmltag (tag (attributes-var content-var) &body body) - "Define a custom HTML tag -Useful for custom phrases, or even special constructs. - -Example: -(defhtmltag coffee (attr content) - (declare (ignore attr content)) - \"c|_|\") - -So, saying -(span \"Nice hot \" (coffee)) - -Would produce: -Nice hot c|_| - -More in-depth use would be something such as: -(even-odd-list - (li \"one\") - (li \"two\") - (li \"three\")) - -Turning into: -(ul - ((li :class \"odd\") \"one\") - ((li :class \"even\") \"two\") - ((li :class \"odd\") \"odd\")) - -Your custom tag is expected to return either a string or -an html construct like you'd pass to HTML-STREAM." - (with-gensyms (throw-away) - `(setf (get ',tag :html-converter) - (lambda (,throw-away ,attributes-var ,content-var) - (declare (ignore ,throw-away)) - (funcall - (lambda (,attributes-var ,content-var) - ,@body) - ,attributes-var ,content-var))))) - -(defun htmlp (html) - "Returns t if html is a legal HTML list. -NB: HTML and friends print out a superset of legal html lists. - -'(ul (li \"yes\") (li \"no\")) is legal -3 is not - -But HTML will print both of them" - (and (consp html) - (not (stringp (car html))))) - -(defmacro destructure-html ((tag-sym attrs-sym content-sym) html &body body) - "Destructure an HTML construct. -(destructure-html (tag attrs content) '((span :class \"strange\") \"A strange span!\") - (format t \"<~A ~{~A~}>~{~A~}\" tag attrs content tag)) -If the construct is invalid, it will cause an error" - (org-ckons-core::once-only (html) - `(if (htmlp ,html) - (let ((,tag-sym (if (consp (car ,html)) - (caar ,html) - (car ,html))) - (,attrs-sym (if (consp (car ,html)) - (cdar ,html) - nil)) - (,content-sym (cdr ,html))) - ,@body)))) - -; I admit, this is a rather goofy way to write this. There's just so much code they have in common -; and this lets me modify them rather easily. -(macrolet ((html-body () - `(ret-block - (cond ((htmlp things) - (destructure-html (tag attrs content) things - (let ((special-effect (get tag :html-converter))) - (if special-effect - (if (not (functionp special-effect)) - (error "Tag ~A has :html-converter set, but NOT as a function." tag) - (call-self stream (funcall special-effect tag attrs content) inline-elements)) - (cond ((equal (symbol-name 'comment) (symbol-name tag)) - (printing - (format-out "~%"))) - #+parenscript - ((equal (symbol-name 'css) (symbol-name tag)) - (printing - (format-out ""))) - #+parenscript - ((equal (symbol-name 'js-script) (symbol-name tag)) - (printing - (format-out "