Skip to content

Instantly share code, notes, and snippets.

@scastillo
Created March 28, 2019 23:01
Show Gist options
  • Save scastillo/01d283da921943ffd9b54c5816408ca4 to your computer and use it in GitHub Desktop.
Save scastillo/01d283da921943ffd9b54c5816408ca4 to your computer and use it in GitHub Desktop.
(ns godot.smtp)
(use 'korma.core)
(require '[bote.core :refer [create-smtp-server]]
'[godot.models.transaction :as transaction])
;; We want to process an incomming mail checking for one of this categories:
;;
;; Payment: Bancolombia le informa Pago por $10,000.00 a PSE - ACH COLOMBIA desde cta *0840. 14/08/2015 15:13.
;; Purchase: Bancolombia le informa Compra por $10.000,00 en PRICESMART AMERICAS 20:10. 10/08/2015 T.Deb *9810.
;; Withdrawal: Bancolombia le informa Retiro por $10.000,00 en TEXACO 80. Hora 16:20 01/08/2015 T.Deb *9810.
;; CCPayment: Bancolombia le informa Pago de Tarjeta de Credito por $10,000 desde cta *0840 a la tarjeta *3291. 04/08/2015 14:22.
;; Transfer: Bancolombia le informa Transferencia por $10,000 desde cta *0840 a cta 10787060488. 15/08/2015 19:33.
;; Registration: Bancolombia le informa que realizo una inscripcion de cuentas de terceros a traves de la SUCURSAL VIRTUAL PERSONAS.
(defn process-new-mail [content]
(println "-- New mail --")
(prn content)
(let [purchase-re #"(?s).*Compra por \$([\d\.\,]*) en (.*) (\d|\d\d):(\d\d)\. (\d\d)\/(\d\d)\/(\d\d\d\d) T.Deb \*(\d\d\d\d).*"
payment-re #".*Pago por \$([\d\.\,]*) a (.*) desde cta \*(\d\d\d\d)\. (\d\d)\/(\d\d)\/(\d\d\d\d) (\d|\d\d):(\d\d).*"
transfer-re #".*Transferencia por \$([\d\.\,]*) desde cta \*(\d\d\d\d) a cta (\d*)\. (\d\d)\/(\d\d)\/(\d\d\d\d) (\d|\d\d):(\d\d).*"
withdrawal-re #".*Retiro por \$([\d\.\,]*) en (.*)\. Hora (\d|\d\d):(\d\d) (\d\d)\/(\d\d)\/(\d\d\d\d) T.Deb \*(\d\d\d\d).*"
[match value venue hour minutes day month year card] (re-matches purchase-re content)]
(println "Value: " value "Venue: " venue "Hour: " hour "Min: " minutes "Day: " day "Month: " month "Year: " year "Card: " card)
(insert transaction/transaction
(values {:value value :venue venue :created hour :card card}))
))
;; Fire a new email processing when smtp-server notifies an incomming-mail
;; TODO: Use -> macro (No matching field found)
;; SMELL: Atom is sync so maybe agent is a better solution.
;; Also atom is wierd with concurrency and the watchers but we dont need to preserver order processing incoming mails tho.
(defonce incomming-mail (atom nil))
(add-watch incomming-mail :process-new-mail-watcher
(fn [key atom old-state new-state]
;(process-new-mail (.toString (.getContent (.getBodyPart (:content new-state) 0))))
(let [alpha-state (-> new-state
(:content)
(.getBodyPart 0)
(.getContent)
(.toString))]
(process-new-mail alpha-state))
))
;; Create smtp server with received atom
(def smtp-server (create-smtp-server #(reset! incomming-mail %)
:port 2525))
(defn -main []
(println "-- Server Started --")
(.start smtp-server))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment