fin-lisp.lisp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. ;;;(ql:quickload "cl-store")
  2. (ql:quickload "yason")
  3. (ql:quickload "cl-ppcre")
  4. (ql:quickload "unix-opts")
  5. (ql:quickload "ironclad")
  6. (ql:quickload "dexador")
  7. ;;; Features
  8. ;;; - Import records from old .txt format
  9. ;;; - Removed as I'll never use it again
  10. ;;; - Added in again to retest some things
  11. ;;; - Interactive prompt to manage expenses
  12. ;;; - Generic expense handling
  13. ;;; TODO
  14. ;;; - Non interactive CLI Interface
  15. ;;; - Upload/download support like perl version
  16. ;;; - Should support encryption/decryption of records - DONE
  17. ;;; - Interface is not good, and doesn't protect the user from
  18. ;;; data entry mistakes
  19. ;;;; Reimplementation of my bills tracker in Lisp
  20. ;;; All records exist in this data structure
  21. ;;; nil on start and loaded in from file
  22. ;;; *records* represents as hash of months,
  23. ;;; where the key is the month stamp, eg 20210701
  24. ;;; and the value is the monthly expenses hash
  25. (defvar *records* (make-hash-table :test 'equalp))
  26. (defvar *api-config-path* "./auth.json")
  27. (defvar *api-url* NIL)
  28. (defvar *api-key* NIL)
  29. (defun file-test (filename)
  30. (if (probe-file filename) filename (print "Couldn't find filename")))
  31. ;;; Used by "print-month" arg to validate
  32. ;;; the user provided a valid key
  33. (defun check-month (month-key)
  34. (if (stringp month-key) month-key))
  35. (opts:define-opts
  36. (:name :help
  37. :description "Print help text"
  38. :short #\h
  39. :long "help")
  40. (:name :read
  41. :description "Read serialized records file"
  42. :short #\r
  43. :long "read"
  44. :arg-parser #'file-test)
  45. (:name :print-month
  46. :description "Print records for given month"
  47. :short #\p
  48. :long "print-month"
  49. :arg-parser #'check-month)
  50. (:name :interactive-mode
  51. :description "Run in interactive mode"
  52. :short #\i
  53. :long "interactive"))
  54. ;; See: https://github.com/libre-man/unix-opts/blob/master/example/example.lisp
  55. (defmacro when-option ((options opt) &body body)
  56. `(let ((it (getf ,options ,opt)))
  57. (when it
  58. ,@body)))
  59. (defun reload ()
  60. (load "~/Repos/fin-lisp/fin-lisp.lisp"))
  61. (defun wfile (file-content file-path)
  62. (alexandria:write-string-into-file
  63. (concatenate 'string file-content) file-path :if-exists :overwrite
  64. :if-does-not-exist :create))
  65. ;;;;;;;;;;;;;;;;;;;;;;;;
  66. ;;; Encryption stuff ;;;
  67. ;;;;;;;;;;;;;;;;;;;;;;;;
  68. ;;; See: https://www.cliki.net/Ironclad
  69. ;;; Return cipher when provided key
  70. ;;; Currently, this is 'insecure' as we are using a string
  71. ;;; coerced into a byte array as the key, aka a non-random secret.
  72. ;;; Should use twofish
  73. (defun get-cipher (key)
  74. (ironclad:make-cipher
  75. :blowfish
  76. :mode :ecb
  77. :key (ironclad:ascii-string-to-byte-array key)))
  78. ;;; First serialize the file,
  79. ;;; then encrypt it from disk
  80. (defun encrypt-records (key filename)
  81. (let ((cipher (get-cipher key))
  82. (file-content (uiop:read-file-string filename)))
  83. (let ((content (ironclad:ascii-string-to-byte-array file-content)))
  84. (ironclad:encrypt-in-place cipher content)
  85. (wfile
  86. (write-to-string (ironclad:octets-to-integer content))
  87. (concatenate 'string filename ".enc")))))
  88. (defun decrypt-records (key filename)
  89. (let ((cipher (get-cipher key))
  90. (file-content (uiop:read-file-string filename)))
  91. (let ((content (ironclad:integer-to-octets (parse-integer file-content))))
  92. (ironclad:decrypt-in-place cipher content)
  93. (coerce (mapcar #'code-char (coerce content 'list)) 'string))))
  94. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  95. ;;; End Encryption Stuff ;;;
  96. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  97. ;;; Can only be called from the REPL,
  98. ;;; used for importing according to the old schema
  99. (defun import-records (filename)
  100. (let ((old-file-lines
  101. (with-open-file (stream filename)
  102. (loop for line = (read-line stream nil)
  103. while line
  104. collect line)))
  105. (mre (ppcre:create-scanner "^(.*)[0-9]{4}$"))
  106. (ere (ppcre:create-scanner "^([A-Z].*)\ -\ \\\$([0-9]{1,4}) - PAID"))
  107. (cur-mon)
  108. (cur-exp))
  109. (loop for line in old-file-lines
  110. do (progn
  111. (if (ppcre:scan mre line) (setf cur-mon line))
  112. (if (ppcre:scan ere line)
  113. (progn
  114. (setf cur-exp (ppcre:register-groups-bind (first second) (ere line) :sharedp t (list first second)))
  115. (print cur-exp)
  116. (if (gethash cur-mon *records*)
  117. (let ((innerhash (gethash cur-mon *records*)))
  118. (setf (gethash (first cur-exp) innerhash) (second cur-exp))))
  119. (if (not (gethash cur-mon *records*))
  120. (progn
  121. (add-month cur-mon)
  122. (let ((innerhash (gethash cur-mon *records*)))
  123. (setf (gethash (first cur-exp) innerhash) (second cur-exp)))))))))))
  124. (defun reset-records ()
  125. (setf *records* (make-hash-table :test 'equal)))
  126. ;; Called like: (add-month '202107)
  127. (defun add-month (month-key)
  128. (setf (gethash month-key *records*) (make-hash-table :test 'equalp))
  129. month-key)
  130. ;;; Taken from practical common lisp
  131. (defun prompt-read (prompt)
  132. (format *query-io* "~a: " prompt)
  133. (force-output *query-io*)
  134. (read-line *query-io*))
  135. (defun prompt-for-expense ()
  136. (list
  137. (prompt-read "Enter expense name")
  138. (parse-integer
  139. (prompt-read "Enter expense value"))))
  140. (defun add-expense-to-month (month)
  141. (if (gethash month *records*)
  142. (let ((innerhash (gethash month *records*))
  143. (exp-l (prompt-for-expense)))
  144. (setf (gethash (first exp-l) innerhash) (second exp-l)))
  145. ;;NIL))
  146. (add-expense-to-month (add-month month))))
  147. ;;; Given key for *records* hash,
  148. ;;; print expenses/values for month
  149. (defun dump-month (month-key)
  150. (format t "~a~C" month-key #\linefeed)
  151. (let ((month-hash)
  152. (exp-keys))
  153. (setf month-hash (gethash month-key *records*))
  154. (setf exp-keys (loop for key being the hash-keys of month-hash collect key))
  155. (dolist (exp-key exp-keys)
  156. (format t "~a : ~a~C" exp-key (gethash exp-key month-hash) #\linefeed))))
  157. ;;; Dump all records.
  158. (defun dump-records ()
  159. (let ((record-key-list (loop for key being the hash-keys of *records* collect key)))
  160. (dolist (month-key record-key-list) (dump-month month-key))))
  161. ;;; Serialization and communicating with the web API
  162. (defun serialize-records (key filename)
  163. (with-open-file (stream filename
  164. :direction :output
  165. :if-exists :overwrite
  166. :if-does-not-exist :create)
  167. (yason:encode *records* stream))
  168. (encrypt-records key filename)
  169. (delete-file filename))
  170. (defun deserialize-records (key filename)
  171. (setf *records* (yason:parse (decrypt-records key filename))))
  172. (defun parse-api-config (path)
  173. (let ((api-config-hash (yason:parse (uiop:read-file-string path)))
  174. (ret-tuple '()))
  175. (push (gethash "token" api-config-hash) ret-tuple)
  176. (push (gethash "url" api-config-hash) ret-tuple)
  177. ret-tuple))
  178. (defun download-records ()
  179. (let* ((api-config (parse-api-config *api-config-path*))
  180. (dl-records (yason:parse (dex:get (concatenate 'string (first api-config) "download")
  181. :headers (list (cons "X-Token" (second api-config)))))))
  182. dl-records))
  183. (defun upload-records (records-file))
  184. (defmacro generic-handler (form error-string)
  185. `(handler-case ,form
  186. (error (e)
  187. (format t "Invalid input: ~a ~%" ,error-string)
  188. (values 0 e))))
  189. ;; Util screen clearer
  190. (defun cls()
  191. (format t "~A[H~@*~A[J" #\escape))
  192. (defun interactive-mode ()
  193. (format t "~%")
  194. (format t "Available options:~%")
  195. (format t "1. Enter expense~%")
  196. (format t "2. Display month~%")
  197. (format t "3. Write records~%")
  198. (format t "4. Read records~%")
  199. (format t "5. Quit~%")
  200. (let
  201. ((answer (prompt-read "Select an option")))
  202. (if (string= answer "1")
  203. (generic-handler
  204. (add-expense-to-month (prompt-read "Enter month"))
  205. "Invalid Input"))
  206. (if (string= answer "2")
  207. (generic-handler
  208. (dump-month (prompt-read "Enter month"))
  209. "Invalid month"))
  210. (if (string= answer "3")
  211. (generic-handler
  212. (serialize-records (prompt-read "Enter encryption key")
  213. (prompt-read "Enter filename"))
  214. "Serialization error or invalid filename"))
  215. (if (string= answer "4")
  216. (generic-handler
  217. (deserialize-records (prompt-read "Enter decryption key")
  218. (prompt-read "Enter filename"))
  219. "Deserialization error or invalid filename"))
  220. (if (string= answer "5")
  221. (quit)))
  222. (interactive-mode))
  223. (defun display-help ()
  224. (format t "foo ~%")
  225. (opts:describe
  226. :prefix "fin-lisp.lisp - Basic expense tracker in lisp"
  227. :usage-of "fin-lisp.lisp"
  228. :args "[FREE-ARGS]")
  229. (quit))
  230. ;; Entry point
  231. (defun main ()
  232. (if (= 1 (length sb-ext:*posix-argv*)) (interactive-mode))
  233. (let ((matches (opts:get-opts)))
  234. (format t "~a ~%" matches)
  235. (when-option (matches :help)
  236. (display-help))
  237. (when-option (matches :print-month)
  238. (when-option (matches :interactive-mode)
  239. (progn
  240. (interactive-mode)
  241. (quit))))))