;;;(ql:quickload "cl-store") (ql:quickload "yason") (ql:quickload "cl-ppcre") (ql:quickload "unix-opts") (ql:quickload "ironclad") (ql:quickload "dexador") ;;; Features ;;; - Import records from old .txt format ;;; - Removed as I'll never use it again ;;; - Added in again to retest some things ;;; - Interactive prompt to manage expenses ;;; - Generic expense handling ;;; - Encryption/decryption of old records ;;; - Upload/download support like perl version ;;; TODO ;;; - Non interactive CLI Interface ;;; - Interface is not good, and doesn't protect the user from ;;; data entry mistakes ;;;; Reimplementation of my bills tracker in Lisp ;;; All records exist in this data structure ;;; nil on start and loaded in from file ;;; *records* represents as hash of months, ;;; where the key is the month stamp, eg 20210701 ;;; and the value is the monthly expenses hash (defvar *records* (make-hash-table :test 'equalp)) (defvar *api-config-path* "./auth.json") (defvar *api-url* NIL) (defvar *api-key* NIL) ;;; Taken from: https://gist.github.com/WetHat/a49e6f2140b401a190d45d31e052af8f ;;; Used for pretty printing output (defconstant +CELL-FORMATS+ '(:left "~vA" :center "~v:@<~A~>" :right "~v@A")) (defun format-table (stream data &key (column-label (loop for i from 1 to (length (car data)) collect (format nil "COL~D" i))) (column-align (loop for i from 1 to (length (car data)) collect :left))) (let* ((col-count (length column-label)) (strtable (cons column-label ; table header (loop for row in data ; table body with all cells as strings collect (loop for cell in row collect (if (stringp cell) cell ;else (format nil "~A" cell)))))) (col-widths (loop with widths = (make-array col-count :initial-element 0) for row in strtable do (loop for cell in row for i from 0 do (setf (aref widths i) (max (aref widths i) (length cell)))) finally (return widths)))) ;------------------------------------------------------------------------------------ ; splice in the header separator (setq strtable (nconc (list (car strtable) ; table header (loop for align in column-align ; generate separator for width across col-widths collect (case align (:left (format nil ":~v@{~A~:*~}" (1- width) "-")) (:right (format nil "~v@{~A~:*~}:" (1- width) "-")) (:center (format nil ":~v@{~A~:*~}:" (- width 2) "-"))))) (cdr strtable))) ; table body ;------------------------------------------------------------------------------------ ; Generate the formatted table (let ((row-fmt (format nil "| ~{~A~^ | ~} |~~%" ; compile the row format (loop for align in column-align collect (getf +CELL-FORMATS+ align)))) (widths (loop for w across col-widths collect w))) ; write each line to the given stream (dolist (row strtable) (apply #'format stream row-fmt (mapcan #'list widths row)))))) ;;; Used by "print-month" arg to validate ;;; the user provided a valid key (defun check-month (month-key) (if (stringp month-key) month-key)) (opts:define-opts (:name :help :description "Print help text" :short #\h :long "help") (:name :print-month :description "Print records for given month" :shora #\p :long "print-month" :arg-parser #'check-month) (:name :interactive-mode :description "Run in interactive mode" :short #\i :long "interactive")) ;; See: https://github.com/libre-man/unix-opts/blob/master/example/example.lisp (defmacro when-option ((options opt) &body body) `(let ((it (getf ,options ,opt))) (when it ,@body))) (defun reload () (load "~/Repos/fin-lisp/fin-lisp.lisp")) (defun parse-api-config (path) (let ((api-config-hash (yason:parse (uiop:read-file-string path))) (ret-tuple '())) (push (gethash "token" api-config-hash) ret-tuple) (push (gethash "url" api-config-hash) ret-tuple) ret-tuple)) ;;;;;;;;;;;;;;;;;;;;;;;; ;;; Encryption stuff ;;; ;;;;;;;;;;;;;;;;;;;;;;;; ;;; See: https://www.cliki.net/Ironclad ;;; Return cipher when provided key ;;; Currently, this is 'insecure' as we are using a string ;;; coerced into a byte array as the key, aka a non-random secret. ;;; Should use twofish (defun get-cipher (key) (ironclad:make-cipher :blowfish :mode :ecb :key (ironclad:ascii-string-to-byte-array key))) (defun encrypt-records (key) (let* ((cipher (get-cipher key)) (content (ironclad:ascii-string-to-byte-array (with-output-to-string (json) (yason:encode *records* json))))) (ironclad:encrypt-in-place cipher content) (write-to-string (ironclad:octets-to-integer content)))) (defun decrypt-records (key enc-record-string) (let ((cipher (get-cipher key))) (let ((content (ironclad:integer-to-octets (parse-integer enc-record-string)))) (ironclad:decrypt-in-place cipher content) (coerce (mapcar #'code-char (coerce content 'list)) 'string)))) (defun download-records () (let* ((api-config (parse-api-config *api-config-path*)) (dl-records (yason:parse (dex:get (concatenate 'string (first api-config) "download") :headers (list (cons "X-Token" (second api-config))))))) dl-records)) (defun upload-records (enc-records-string) (let* ((api-config (parse-api-config *api-config-path*)) (result (dex:post (concatenate 'string (first api-config) "upload") :headers (list (cons "X-Token" (second api-config)) (cons "Content-Type" "application/json")) :content (concatenate 'string "{\"content\": \"" enc-records-string "\"}")))) result)) ;;; Serialization and communicating with the web API (defun push-records (key) "Upload records to remote server" (upload-records (encrypt-records key))) (defun get-records (key) "Get records from remote server" (setf *records* (yason:parse (decrypt-records key (gethash "content" (download-records)))))) ;; Called like: (add-month '202107) (defun add-month (month-key) (setf (gethash month-key *records*) (make-hash-table :test 'equalp)) month-key) ;;; Taken from practical common lisp (defun prompt-read (prompt) (format *query-io* "~a: " prompt) (force-output *query-io*) (read-line *query-io*)) (defun prompt-for-expense () (list (prompt-read "Enter expense name") (parse-integer (prompt-read "Enter expense value")))) (defun add-expense-to-month (month) (if (gethash month *records*) (let ((innerhash (gethash month *records*)) (exp-l (prompt-for-expense))) (setf (gethash (first exp-l) innerhash) (second exp-l))) ;;NIL)) (add-expense-to-month (add-month month)))) ;;; Given key for *records* hash, ;;; print expenses/values for month (defun dump-month (month-key) (let ((month-hash) (exp-keys)) (setf month-hash (gethash month-key *records*)) (setf exp-keys (loop for key being the hash-keys of month-hash collect key)) (format t "~C" #\linefeed) (format t "~a~C" month-key #\linefeed) (dolist (exp-key exp-keys) (format t "~a : ~a~C" exp-key (gethash exp-key month-hash) #\linefeed)) (format t "~C" #\linefeed))) ;;; Given key for *records* hash, ;;; print expenses/values for month (defun dump-month-table (month-key) (let* ((month-hash (gethash month-key *records*)) (exp-keys (loop for key being the hash-keys of month-hash collect key)) (flist)) (dolist (exp-key exp-keys) (setq flist (append flist (list (list exp-key (gethash exp-key month-hash)))))) (format-table T flist :column-label '("Expense" "Amount")))) ;;; Dump all records. (defun dump-records () (let ((record-key-list (loop for key being the hash-keys of *records* collect key))) (dolist (month-key record-key-list) (dump-month month-key)))) (defmacro generic-handler (form error-string) `(handler-case ,form (error (e) (format t "Invalid input: ~a ~%" ,error-string) (values 0 e)))) ;; Util screen clearer (defun cls() (format t "~A[H~@*~A[J" #\escape)) (defun interactive-mode () (format t "~%") (format t "Available options:~%") (format t "1. Enter expense~%") (format t "2. Display month~%") (format t "3. Push records~%") (format t "4. Get records~%") (format t "5. Quit~%") (let ((answer (prompt-read "Select an option"))) (if (string= answer "1") (generic-handler (add-expense-to-month (prompt-read "Enter month")) "Invalid Input")) (if (string= answer "2") (generic-handler (dump-month-table (prompt-read "Enter month")) "Invalid month")) (if (string= answer "3") (generic-handler (push-records (prompt-read "Enter encryption key")) "Serialization error or invalid filename")) (if (string= answer "4") (generic-handler (get-records (prompt-read "Enter decryption key")) "Deserialization error or invalid filename")) (if (string= answer "5") (return-from interactive-mode nil))) (interactive-mode)) (defun display-help () (format t "foo ~%") (opts:describe :prefix "fin-lisp.lisp - Basic expense tracker in lisp" :usage-of "fin-lisp.lisp" :args "[FREE-ARGS]") (quit)) ;; Entry point (defun main () (if (= 1 (length sb-ext:*posix-argv*)) (interactive-mode)) (let ((matches (opts:get-opts))) (format t "~a ~%" matches) (when-option (matches :help) (display-help)) (when-option (matches :print-month) (when-option (matches :interactive-mode) (progn (interactive-mode) (quit)))))) ;;; Can only be called from the REPL, ;;; used for importing according to the old schema (defun import-records (filename) (let ((old-file-lines (with-open-file (stream filename) (loop for line = (read-line stream nil) while line collect line))) (mre (ppcre:create-scanner "^(.*)[0-9]{4}$")) (ere (ppcre:create-scanner "^([A-Z].*)\ -\ \\\$([0-9]{1,4}) - PAID")) (cur-mon) (cur-exp)) (loop for line in old-file-lines do (progn (if (ppcre:scan mre line) (setf cur-mon line)) (if (ppcre:scan ere line) (progn (setf cur-exp (ppcre:register-groups-bind (first second) (ere line) :sharedp t (list first second))) (print cur-exp) (if (gethash cur-mon *records*) (let ((innerhash (gethash cur-mon *records*))) (setf (gethash (first cur-exp) innerhash) (second cur-exp)))) (if (not (gethash cur-mon *records*)) (progn (add-month cur-mon) (let ((innerhash (gethash cur-mon *records*))) (setf (gethash (first cur-exp) innerhash) (second cur-exp))))))))))) (defun reset-records () "Used for debugging, just resets the *records* hash to NIL" (setf *records* (make-hash-table :test 'equal)))