email.lisp

;; Copyright (c) 2024, SWGY, Inc. <ron@sw.gy>
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3 of the License, or (at
;; your option) any later version.
;;
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software Foundation, Inc.,
;; 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
;;
(in-package :swtx)

(defparameter auth-email "auth@tix.sw.gy"
  "The email address that will send authentication communications.")

(defparameter info-email "info@tix.sw.gy"
  "The email address that will send general information.")

(defun valid-recipient-p (address)
  "Returns T if the recipient address looks good, nil otherwise"
  (and (< (length address)
          *max-email-length*)
       (> (length address)
          *min-email-length*)
       (or (ppcre:scan "^.*@sw\\.gy$" address)
           (ppcre:scan "^ricmatt\\+.*@gmail\\.com$" address)
           (ppcre:scan "^k3wang\\+.*@gmail\\.com$" address))))

(defun send-auth-email (recipient-address response-code)
  "Send a standard auth email to the indicated recipient"
  (when (valid-recipient-p recipient-address)
    (let ((message (format nil
                           "Hello,

Your authentication response code is:

~6,'0d

Best regards,
SwiggyTix Support Team" response-code)))


    (format T "Sending to ~A: ~%~A~%" recipient-address message)
    (cl-smtp:send-email "localhost" auth-email recipient-address
                        "SwiggyTix Authentication Code" message))))

(defun send-account-listing-email (recipient-address accounts-list)
  "Sends an account listing email to the recipient. Address must pass
the valid-recipient-p address filter.

accounts-list : A list of cons cells of (account-type . account-id) where
    both are strings."
  (when (valid-recipient-p recipient-address)
    (let ((message
            (with-output-to-string (s)
              (format s "Hello,
A list of accounts associated with your email address was requested. Here is a
list that includes the type of account:

")
              (dolist (c accounts-list)
                (let ((account-type (car c))
                      (account-id (cdr c)))
                  (format s "Account type: ~A, account id: ~A~%"
                          account-type account-id))))))
      (format T "Sending to ~A: ~%~A~%" recipient-address message)
      (cl-smtp:send-email "localhost" info-email recipient-address
                          "SwiggyTix Account Listing" message))))