-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmail-app.lisp
162 lines (141 loc) · 5.77 KB
/
mail-app.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
(defpackage :msgraph.demo.mail
(:use :cl :msgraph :odata/lang :easy-routes :cl-who :arrows :access)
(:export :start-app :stop-app))
(in-package :msgraph.demo.mail)
(defvar *html*)
(defmacro with-html-page (&body body)
`(who:with-html-output-to-string (*html*)
(:html
(:head
(:title "Messages")
(:link :rel "stylesheet" :href "https://unpkg.com/[email protected]/build/pure-min.css" :integrity "sha384-oAOxQR6DkCoMliIh8yFnu25d7Eq/PHS21PClpwjOTeU2jRSq11vu66rf90/cZr47" :crossorigin"anonymous"))
(:body
,@body))))
(defun get-user (id)
(-> msgraph::+msgraph+
(collection "users")
(id id)
(fetch)))
(defparameter +appuser+ (get-user "77d37ed0-173e-474e-a477-371f4bbdd1a2"))
(defun get-messages (user)
(-> msgraph::+msgraph+
(collection "users")
(id (access user :id))
(collection "messages")
(fetch :collection)))
(defun get-message (user id)
(-> msgraph::+msgraph+
(collection "users")
(id (access user :id))
(collection "messages")
(id id)
(fetch)))
(defun create-message (user message)
(-> msgraph::+msgraph+
(collection "users")
(id (access user :id))
(collection "messages")
(post message)))
(defun send-message (user id)
(-> msgraph::+msgraph+
(collection "users")
(id (access user :id))
(collection "messages")
(id id)
(fcall "send")
(post)))
(defroute home ("/" :acceptor-name msgraph-mail)
()
(with-html-page
(show-messages +appuser+)
(:a :href (genurl 'create-message-page :acceptor-name 'msgraph-mail)
(str "New message"))))
(defroute show-message ("/messages/:id" :acceptor-name msgraph-mail)
(&path (id 'string))
(let ((message (get-message +appuser+ id)))
(access:with-dot ()
(with-html-page
(:form :class "pure-form pure-form-stacked"
(:fieldset
(:label "From") (:label
(who:str message.from.email-address.name)
(:a :href (format nil "mailto:~a" message.from.email-address.address)
(who:fmt "<~a>" message.from.email-address.address)))
(:label "To")
(:label (loop for recipient in message.to-recipients
do
(who:htm
(who:str recipient.email-address.name)
(:a :href (format nil "mailto:~a" recipient.email-address.address)
(who:fmt "<~a>" recipient.email-address.address)))))
(:label "Subject")
(:label (str message.subject))
(:label "Body")
(write-string message.body.content *html*)))
(when message.is-draft
(who:htm
(:p
(who:str "This message is a DRAFT")
(:form :action (genurl 'send-message-action :id id :acceptor-name 'msgraph-mail)
:method :post
(:input :type "submit" :value "Send")))))
))))
(defroute send-message-action ("/messages/:id/send" :method :post :acceptor-name msgraph-mail)
()
(send-message +appuser+ id)
(redirect (genurl 'home :acceptor-name 'msgraph-mail)))
(defroute create-message-page ("/messages/new" :acceptor-name msgraph-mail)
()
(access:with-dot ()
(with-html-page
(:form :class "pure-form pure-form-aligned"
:method "POST"
(:legend "Create message")
(:fieldset
(:div :class "pure-control-group"
(:label "From") (:input :name "from"
:readonly t
:value +appuser+.mail))
(:div :class "pure-control-group"
(:label "To") (:input :name "to"))
(:div :class "pure-control-group"
(:label "Subject") (:input :name "subject"))
(:div :class "pure-control-group"
(:label "Body") (:textarea :name "body"))
(:input :type "submit" :value "Create"))))))
(defun parse-recipient (str)
(multiple-value-bind (address at name)
(darts.lib.email-address:parse-rfc5322-mailbox str)
`((:email-address . ((:address . ,(format nil "~a@~a" address at))
(:name . ,(or name address)))))))
(defun parse-recipients (str)
(loop for rstr in (split-sequence:split-sequence #\, str)
collect (parse-recipient rstr)))
(defroute save-message ("/messages/new" :method :post :acceptor-name msgraph-mail)
(&post from to subject body)
(create-message +appuser+
`((:subject . ,subject)
(:body . ((:content-type . "text")
(:content . ,body)))
(:from . ,(first (parse-recipients from)))
(:to-recipients . ,(parse-recipients to))))
(redirect (genurl 'home :acceptor-name 'msgraph-mail)))
(defun show-messages (user)
(access:with-dot ()
(who:with-html-output (*html*)
(:ul
(loop
for message in (get-messages user)
do
(who:htm (:li (:a :href (genurl 'show-message
:id (access message :id)
:acceptor-name 'msgraph-mail)
(who:str message.subject))
(when message.is-draft
(who:str "(DRAFT)")))))))))
(defparameter *acceptor* nil)
(defun start-app (&key (port 0))
"When port is zero, a random one is selected."
(setf *acceptor* (hunchentoot:start (make-instance 'easy-routes-acceptor :port port :name 'msgraph-mail))))
(defun stop-app ()
(hunchentoot:stop *acceptor*))