index.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. (add-to-load-path (dirname (current-filename)))
  2. (use-modules
  3. (ice-9 textual-ports)
  4. (ice-9 regex)
  5. (decode)
  6. (email)
  7. (web server)
  8. (web request)
  9. (web response)
  10. (web uri)
  11. (oop goops)
  12. (sxml simple)
  13. ;; (srfi srfi-19)
  14. )
  15. (define* (templatize title body
  16. #:key
  17. (js-files #f))
  18. `(html (head (title ,title)
  19. (head
  20. (link (@ (type "text/css") (href "css/form.min.css") (rel "stylesheet")) "")
  21. ,(if js-files
  22. `(script (@ (async) (src ,(string-append "js/" js-files))) " ")
  23. `(link (@ (type "text/css") (href "#")) ""))
  24. )
  25. (body ,@body))
  26. ))
  27. (define* (respond #:optional body #:key
  28. (status 200)
  29. (title "form")
  30. (doctype "<!DOCTYPE html>\n")
  31. (content-type-params '((charset . "utf-8")))
  32. (content-type 'text/html)
  33. (extra-headers '())
  34. (js-files #f)
  35. (sxml (and body (if js-files
  36. (templatize title body #:js-files js-files)
  37. (templatize title body)))))
  38. (values (build-response
  39. #:code status
  40. #:headers `((content-type
  41. . (,content-type ,@content-type-params))
  42. ,@extra-headers))
  43. (lambda (port)
  44. (if sxml
  45. (begin
  46. (if doctype (display doctype port))
  47. (sxml->xml sxml port))))))
  48. (define (request-path-components request)
  49. (split-and-decode-uri-path (uri-path (request-uri request))))
  50. ;; Paste this in your REPL
  51. (define (not-found request)
  52. (values (build-response #:code 404)
  53. (string-append "Resource not found: "
  54. (uri->string (request-uri request)))))
  55. (define (output-file file-name)
  56. (call-with-input-file file-name
  57. (lambda (port)
  58. (string-append
  59. ""
  60. (let loop ([string (get-line port)])
  61. (if (eof-object? string)
  62. ""
  63. (string-append
  64. string "\n" (loop (get-line port)))))))))
  65. (define (submit-page bv)
  66. (respond
  67. `((div (@ (class "container"))
  68. (div (@ (class "row"))
  69. (div (@ (class "col-md-12"))
  70. ,(let* ([alist (decode bv)]
  71. ;;(if (and (not null? (assoc-ref alist "timestamp"))))
  72. ;; [form-timestamp-pair
  73. ;; (string-split (car (assoc-ref alist "timestamp")) #\space)]
  74. ;; [form-timestamp-seconds (car form-timestamp-pair)]
  75. ;; [form-timestamp-microseconds (car (cdr form-timestamp-pair))]
  76. ;; [timestamp (gettimeofday)]
  77. ;; [timestamp-seconds (car timestamp)]
  78. ;; [timestamp-microseconds (cdr timestamp)]
  79. ;; [form-completion-time-seconds
  80. ;; (- (current-time) (string->number form-timestamp-seconds))]
  81. ;; [form-completion-time-microseconds
  82. ;; (abs (- timestamp-microseconds
  83. ;; (string->number form-timestamp-microseconds)))]
  84. [name (car (assoc-ref alist "name"))]
  85. [phone (car (assoc-ref alist "phone"))]
  86. [domain (car (assoc-ref alist "domain"))]
  87. [email (car (assoc-ref alist "email"))]
  88. [message (car (assoc-ref alist "message"))]
  89. [honeypot (car (assoc-ref alist "address"))]
  90. [service1 (if (assoc-ref alist "service1")
  91. (car (assoc-ref alist "service1"))
  92. #f)]
  93. [service2 (if (assoc-ref alist "service2")
  94. (car (assoc-ref alist "service2"))
  95. #f)]
  96. [service3 (if (assoc-ref alist "service3")
  97. (car (assoc-ref alist "service3"))
  98. #f)]
  99. [service4 (if (assoc-ref alist "service4")
  100. (car (assoc-ref alist "service4"))
  101. #f)]
  102. [service5 (if (assoc-ref alist "service5")
  103. (car (assoc-ref alist "service5"))
  104. #f)]
  105. [service6 (if (assoc-ref alist "service6")
  106. (car (assoc-ref alist "service6"))
  107. #f)]
  108. [service7 (if (assoc-ref alist "service7")
  109. (car (assoc-ref alist "service7"))
  110. #f)]
  111. [service8 (if (assoc-ref alist "service8")
  112. (car (assoc-ref alist "service8"))
  113. #f)]
  114. ;;[service9 (car (assoc-ref alist "service9"))]
  115. [service9 (if (assoc-ref alist "service9")
  116. (car (assoc-ref alist "service9"))
  117. #f)]
  118. )
  119. ;; if this form was completed in under 200000 microseconds, then this may be a computer trying to log in
  120. ;; don't let them log in. OR if the honeypot had any value in it...
  121. ;; (if (or (and (= 0 form-completion-time-seconds)
  122. ;; (> 200000 form-completion-time-microseconds))
  123. ;; (not (string= honeypot "")))
  124. ;; '(p "We're having issues...please try again later."))
  125. ;; ,(string-append
  126. ;; "It took you "
  127. ;; ;;(number->string form-completion-time-seconds)
  128. ;; " seconds and "
  129. ;; ;;(number->string form-completion-time-microseconds)
  130. ;; " microseconds to complete the form.\n")
  131. (if (string= honeypot "")
  132. (begin
  133. (send-email email "Hosting at GNUcode.me"
  134. (string-append
  135. "You have decided to use
  136. some of the services provided by gnucode.me! Josh will contact you
  137. will the payment details, and we'll have your services set up in no time."))
  138. (send-email "jbranso+services@dismail.de"
  139. (string-append "GNUcode.me Supporter:" name)
  140. (string-append name " wants to host their site :"
  141. domain
  142. " with you! Their number is "
  143. phone
  144. ".\nTheir email is "
  145. email
  146. ".\n"
  147. "Their message is:\n"
  148. message
  149. "Their services are:"
  150. service1 "\n"
  151. service2 "\n"
  152. service3 "\n"
  153. service4 "\n"
  154. service5 "\n"
  155. service6 "\n"
  156. service7 "\n"
  157. service8 "\n"
  158. service9 "\n"
  159. ))
  160. `(p "Awesome! You'll be getting an email
  161. from me soon! I'm glad that you are looking forward to supporting me!"))
  162. `(p "You must be a robot.")))
  163. ))))))
  164. (define (main-page)
  165. (respond
  166. `((form (@ (action "submit.scm") (method "post"))
  167. (div
  168. (label (@ (for "name")) "Name")
  169. (input (@ (id "name") (type "text") (name "name") (placeholder "Greg Jones"))))
  170. (div
  171. (label (@ (for "email")) "Email")
  172. (input (@ (id "email") (type "email") (name "email") (placeholder "email@example.com"))))
  173. (div
  174. ;; this is a honeypot. It's an hidden input, that users will leave empty, but computer programs
  175. ;; will put in a value.
  176. (label (@ (for "address") (class "hidden")) "address")
  177. (input (@ (id "address") (type "text") (class "hidden") (name "address"))))
  178. (div
  179. (label (@ (for "timestamp") (class "hidden")) "timestamp")
  180. (input (@ (id "timestamp") (type "number") (class "hidden") (name "timestamp")
  181. (value ,(string-append
  182. (number->string (car (gettimeofday)))
  183. " "
  184. (number->string (cdr (gettimeofday))))
  185. ))))
  186. (div
  187. (label (@ (id "phone") (for "phone")) "Phone")
  188. (input (@ (type "tel") (name "phone") (placeholder "224-930-0493"))))
  189. (div
  190. (label (@ (id "subject") (for "subject")) "Subject")
  191. (input (@ (type "text") (name "subject") (placeholder "Website Hosting"))))
  192. (div
  193. (label (@ (id "domain") (for "domain")) "Domain")
  194. (input (@ (type "text") (name "domain") (placeholder "gregsblog.com"))))
  195. (div
  196. (label (@ (id "message") (for "message")) "Message")
  197. (textarea (@ (name "message") (cols "26") (rows "6")
  198. (placeholder "Will you host my website?"))
  199. ;;necessary to properly close <textarea></textarea> tag.
  200. ()))
  201. (div
  202. (button (@ (type "submit")) "Submit"))))))
  203. (define (run-page request body)
  204. ;;(display (request-path-components request))
  205. (let ([current-page (request-path-components request)])
  206. (cond
  207. [(equal? current-page '("form" "css" "form.min.css"))
  208. (values `((content-type . (text/css)))
  209. (output-file "css/form.min.css"))]
  210. [(equal? current-page '("form" "index.scm"))
  211. (main-page)]
  212. [(equal? current-page '("form" "submit.scm"))
  213. (submit-page body)]
  214. [else
  215. (respond `((h1 "Page not found.")
  216. (h1 ,(let loop ([current-page current-page])
  217. (if (null? current-page) ""
  218. (string-append (car current-page) "/"
  219. (loop (cdr current-page))))))
  220. (h2 ,current-page)))])))
  221. (run-server run-page 'http '(#:port 8081))