forked from TeMPOraL/alice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.lisp
137 lines (105 loc) · 5.01 KB
/
server.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
(in-package #:alice)
;;; State
(defvar *hunchentoot-acceptor* nil)
(defparameter *remote-admin-allowed-users-alist* '())
;;; Routes
;;; NOTE we could (probably should) consider moving the routes and associated functionality to Alice's Grimoire.
(hunchentoot:define-easy-handler (index :uri "") ()
(http-get-index-page))
(hunchentoot:define-easy-handler (status :uri "/status") ()
(http-get-status-page))
(hunchentoot:define-easy-handler (relay-message :uri "/message/relay") (to message lang)
(http-relay-message to message lang))
(hunchentoot:define-easy-handler (relay-canned-message :uri "/message/relay-canned") (to message lang)
(http-relay-canned-message to message lang))
(hunchentoot:define-easy-handler (test-pushover :uri "/test/pushover" :default-request-type :POST) ()
(http-test-pushover))
;;; Webserver management interface
(defun start-server (&key (port 12321))
"Start Alice's management HTTP server on `PORT'."
(unless *hunchentoot-acceptor*
(setf *hunchentoot-acceptor* (make-instance 'hunchentoot:easy-acceptor :port port))
(hunchentoot:start *hunchentoot-acceptor*)))
(defun stop-server ()
"Stop previously started Alice's HTTP server.
Do NOT call this from a request handler, or it might deadlock."
(when *hunchentoot-acceptor*
(hunchentoot:stop *hunchentoot-acceptor* :soft t)
(setf *hunchentoot-acceptor* nil)))
(defun request-stop-server ()
"Request Alice's server to be stopped at some time in the nearby future.
Like `STOP-SERVER', but for use from within request handlers."
;; TODO
)
(defun force-stop-server ()
"Force stop Alice's HTTP server and clear its connection state. Use if server broke
in such a way you can't stop or start it normally with `STOP-SERVER' or `START-SERVER'."
(when *hunchentoot-acceptor*
(Hunchentoot:stop *hunchentoot-acceptor* :soft nil))
(setf *hunchentoot-acceptor* nil))
(defun server-debug-mode (mode)
(setf hunchentoot:*show-lisp-errors-p* mode
hunchentoot:*show-lisp-backtraces-p* mode))
;;; Implementation of remote management actions
(defparameter *alice-remote-canned-messages-pl* '((":ok" . "przekazuje: OK")
(":not-ok" . "przekazuje: Nie OK")
(":???" . "nie wie o co chodzi...")
(":call-me" . "prosi, żeby ktoś do niego zadzwonił.")))
(defparameter *alice-remote-canned-messages-en* '((":ok" . "relays: OK")
(":not-ok" . "relays: not OK")
(":???" . "doesn't know what this is about.")
(":call-me" . "asks for someone to call him.")))
(defun canned-message (msg lang)
(cdr (assoc msg
(if (string-equal lang "pl_PL")
*alice-remote-canned-messages-pl*
*alice-remote-canned-messages-en*)
:test #'string-equal)))
(defun canned-message-preamble (from lang)
(declare (ignore lang))
from)
(defun relay-message-preamble (from lang)
(concatenate 'string
from
" "
(if (string= lang "pl_PL")
"kazał przekazać:"
"asked to say:")))
;;; Route handlers
(defun http-get-index-page ()
(setf (hunchentoot:content-type*) "text/html")
(who:with-html-output-to-string (*standard-output* nil :prologue t :indent t)
(:html
(:head (:title "Alice Margatroid")) ;TODO use parametric name
(:body
(:h1 "Why, hello there!")))))
(defun http-get-status-page ()
(setf (hunchentoot:content-type*) "text/html")
(who:with-html-output-to-string (*standard-output* nil :prologue t :indent t)
(:html
(:head (:title "Status | Alice Margatroid")) ;TODO use parametric name
(:body
(:h1 "Status page.")
(:h2 "Connected channels:")
(:ul (dolist (channel *connected-channels*)
(who:htm (:li (who:str channel)))))))))
(defun http-relay-message (to message lang)
(setf (hunchentoot:content-type*) "application/json")
(when-let ((user (http-authenticated-user)))
(say to (concatenate 'string (relay-message-preamble user lang) " " message))))
(defun http-relay-canned-message (to message lang)
(setf (hunchentoot:content-type*) "application/json")
(when-let ((user (http-authenticated-user)))
(say to (concatenate 'string (canned-message-preamble user lang) " " (canned-message message lang)))))
(defun http-test-pushover ()
(setf (hunchentoot:content-type*) "application/json")
(when (http-authenticated-user)
(send-pushover (concatenate 'string "A test Pushover sent at: " (local-time:format-timestring nil (local-time:now)) ".")
*pushover-admin-user*
"Alice Testing Service"))
"ok")
;;; Additional utils
(defun http-authenticated-user ()
(multiple-value-bind (user pass) (hunchentoot:authorization)
(when (string= pass (cdr (assoc user *remote-admin-allowed-users-alist* :test #'string=)))
user)))