-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathaccount.lisp
175 lines (137 loc) · 6.13 KB
/
account.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
163
164
165
166
167
168
169
170
171
172
173
174
175
(in-package #:org.shirakumo.maiden.agents.accounts)
(defun charr (from to)
(loop for i from from to to collect (code-char i)))
(defun chars (&rest chars)
(mapcar #'code-char chars))
(defparameter *illegal-account-name-chars*
(append ;; Disallow things problematic for filenames
(list #\\ #\/ #\~ #\. #\' #\")
;; We disallow all spacing and all unicode control characters.
(charr #x0000 #x0020)
(charr #x0080 #x009F)
(charr #x2000 #x200D)
(charr #x2028 #x202F)
(charr #xFFF0 #xFFFF)
(chars #x007F #x061C #x1680 #x180E #x205F #x2060 #x3000 #xFEFF)))
(defun normalize-account-name (name)
(remove-if (lambda (c) (find c *illegal-account-name-chars*))
(string-downcase (etypecase name
(string name)
(account (name name))))))
(defvar *accounts* (make-hash-table :test 'equal))
(defvar *identity-account-map* (make-hash-table :test 'equalp))
(defclass account (named-entity data-entity)
((identities :initform () :accessor identities)
(password :initarg :password :accessor password))
(:default-initargs
:password (error "PASSWORD required.")))
(defmethod initialize-instance :after ((account account) &key)
(setf (name account) (normalize-account-name (name account)))
(when (gethash (name account) *accounts*)
(error "An account with the name ~s already exists."
(name account)))
(setf (account (name account)) account))
(defmethod offload ((account account))
(let ((*package* #.*package*))
(ubiquitous:offload (account-pathname account) :lisp account)))
(defmethod offload (account-ish)
(offload (account account-ish)))
(defmethod (setf data-value) :after (value field (account account))
(offload account))
(defmethod (setf password) :after (value (account account))
(offload account))
(defmethod (setf identities) :after (value (account account))
(offload account))
(defmethod data-value ((field symbol) (account account))
(data-value (normalize-field-name field) account))
(defmethod identity ((event user-event))
(identity (user event)))
(defmethod identity ((user user))
(cons (name (client user)) (name user)))
(defmethod identity ((cons cons))
(when (typep (car cons) 'client)
(setf (car cons) (name (car cons))))
(when (typep (cdr cons) 'user)
(setf (cdr cons) (name (cdr cons))))
cons)
(defmethod add-identity (account-ish identity-ish)
(add-identity account-ish (identity identity-ish)))
(defmethod add-identity (account-ish (identity cons))
(add-identity (account account-ish) identity))
(defmethod add-identity ((account account) (identity cons))
(let ((identity (identity identity)))
(when (gethash identity *identity-account-map*)
(error "The identity ~s is already linked to an account!" identity))
(pushnew identity (identities account) :test #'equalp)
(setf (gethash identity *identity-account-map*) account)))
(defmethod remove-identity (account-ish identity-ish)
(remove-identity account-ish (identity identity-ish)))
(defmethod remove-identity (account-ish (identity cons))
(remove-identity (account account-ish) identity))
(defmethod remove-identity ((account account) (identity cons))
(let ((identity (identity identity)))
(setf (identities account) (remove identity (identities account) :test #'equalp))
(remhash identity *identity-account-map*)))
(defmethod identity-p (account-ish identity-ish)
(identity-p account-ish (identity identity-ish)))
(defmethod identity-p (account-ish (identity cons))
(identity-p (account account-ish) identity))
(defmethod identity-p ((account account) (identity cons))
(find (identity identity) (identities account) :test #'equalp))
(defun account-pathname (name)
(maiden-storage:config-pathname
(make-pathname :name (normalize-account-name name)
:type "lisp"
:directory '(:relative "account"))))
(defmethod maiden-storage:config-pathname ((account account))
(account-pathname (name account)))
(defmethod account ((account account) &key error)
(declare (ignore error))
account)
(defmethod account ((user user) &key (error T))
(or (data-value 'account user)
(and (authenticated-p user)
(setf (account user) (account (identity user) :error error)))))
(defmethod account ((identity cons) &key (error T))
(let ((identity (identity identity)))
(or (gethash identity *identity-account-map*)
(when error (error 'no-account-for-identity :identity identity)))))
(defmethod account ((name symbol) &key (error T))
(cond (name
(account (string name) :error error))
(error
(error 'account-not-found :name name))))
(defmethod account ((name string) &key (error T))
(let ((name (normalize-account-name name))
(*package* #.*package*))
(or (gethash name *accounts*)
(handler-case
(setf (account name) (ubiquitous:restore (account-pathname name)))
(ubiquitous:no-storage-file () NIL))
(when error (error 'account-not-found :name name)))))
(defmethod (setf account) (account (user user))
(setf (data-value 'account user) account))
(defmethod (setf account) (account (name symbol))
(setf (account (string name)) account))
(defmethod (setf account) (account (name string))
(dolist (identity (identities account))
(setf (account identity) account))
(setf (gethash name *accounts*) account)
(offload account))
(defmethod (setf account) (account (identity cons))
(add-identity account identity))
(defun delete-account (account-ish)
(let ((account (account account-ish)))
(uiop:delete-file-if-exists (account-pathname account))
(remhash (name account) *accounts*)
account))
(defun load-all-accounts ()
(dolist (pathname (directory
(maiden-storage:config-pathname
(make-pathname :name pathname-utils:*wild-component*
:type "lisp"
:directory '(:relative "account")))))
(with-simple-restart (abort "Abort restoring the account from ~a." pathname)
(let* ((*package* #.*package*)
(account (ubiquitous:restore pathname)))
(setf (account (name account)) account)))))