1 | ;;;; $Id: utility.lisp 88 2005-03-20 16:55:43Z ehuelsmann $ |
---|
2 | ;;;; $Source$ |
---|
3 | |
---|
4 | ;;;; See the LICENSE file for licensing information. |
---|
5 | |
---|
6 | (in-package :irc) |
---|
7 | |
---|
8 | (defun get-day-name (day-number) |
---|
9 | "Given a number, such as 1, return the appropriate day name, |
---|
10 | abbrevated, such as \"Tue\". Index 0 is Monday." |
---|
11 | (case day-number |
---|
12 | (0 "Mon") |
---|
13 | (1 "Tue") |
---|
14 | (2 "Wed") |
---|
15 | (3 "Thu") |
---|
16 | (4 "Fri") |
---|
17 | (5 "Sat") |
---|
18 | (6 "Sun") |
---|
19 | (otherwise |
---|
20 | (error "Unknown day ~A." day-number)))) |
---|
21 | |
---|
22 | (defun get-month-name (month-number) |
---|
23 | "Index 1 is January." |
---|
24 | (case month-number |
---|
25 | (1 "Jan") |
---|
26 | (2 "Feb") |
---|
27 | (3 "Mar") |
---|
28 | (4 "Apr") |
---|
29 | (5 "May") |
---|
30 | (6 "Jun") |
---|
31 | (7 "Jul") |
---|
32 | (8 "Aug") |
---|
33 | (9 "Sep") |
---|
34 | (10 "Oct") |
---|
35 | (11 "Nov") |
---|
36 | (12 "Dec") |
---|
37 | (otherwise |
---|
38 | (error "Unknown month ~A." month-number)))) |
---|
39 | |
---|
40 | (defun make-time-message (second minute hour date month year day) |
---|
41 | "Returns a string composed of the input parameters so that it |
---|
42 | represents a time message as by the IRC protocol." |
---|
43 | (format nil "~A ~A ~2D ~2,'0D:~2,'0D:~2,'0D ~D" |
---|
44 | (get-day-name day) |
---|
45 | (get-month-name month) |
---|
46 | date |
---|
47 | hour |
---|
48 | minute |
---|
49 | second |
---|
50 | year)) |
---|
51 | |
---|
52 | (defun make-irc-message (command &key (arguments nil) |
---|
53 | (trailing-argument nil)) |
---|
54 | "Return a valid IRC message, as a string, composed of the input |
---|
55 | parameters." |
---|
56 | (let ((*print-circle* nil)) |
---|
57 | (format nil "~A~{ ~A~}~A~A~A~A" command arguments |
---|
58 | (if trailing-argument |
---|
59 | " :" |
---|
60 | "") |
---|
61 | (or trailing-argument "") |
---|
62 | #\Return |
---|
63 | #\Linefeed))) |
---|
64 | |
---|
65 | (defun make-ctcp-message (string) |
---|
66 | "Return a valid IRC CTCP message, as a string, composed by |
---|
67 | `string'." |
---|
68 | (format nil "~A~A~A" +soh+ string +soh+)) |
---|
69 | |
---|
70 | (defun tokenize-string (string &key |
---|
71 | (delimiters '(#\Space #\Return #\Linefeed #\Newline))) |
---|
72 | "Split string into a list, splitting on `delimiters' and removing any |
---|
73 | empty subsequences." |
---|
74 | (split-sequence:split-sequence-if #'(lambda (character) |
---|
75 | (member character delimiters)) |
---|
76 | string :remove-empty-subseqs t)) |
---|
77 | |
---|
78 | (defun list-of-strings-to-integers (list) |
---|
79 | "Take a list of strings and return a new list of integers (from |
---|
80 | parse-integer) on each of the string elements." |
---|
81 | (let ((new-list nil)) |
---|
82 | (dolist (element (reverse list)) |
---|
83 | (push (parse-integer element) new-list)) |
---|
84 | new-list)) |
---|
85 | |
---|
86 | (defun host-byte-order (string) |
---|
87 | "Convert a string, such as 192.168.1.1, to host-byte-order, such as |
---|
88 | 3232235777." |
---|
89 | (let ((list (list-of-strings-to-integers (split-sequence:split-sequence #\. string)))) |
---|
90 | (+ (* (first list) 256 256 256) (* (second list) 256 256) |
---|
91 | (* (third list) 256) (fourth list)))) |
---|
92 | |
---|
93 | (defun hbo-to-dotted-quad (integer) |
---|
94 | "Host-byte-order integer to dotted-quad string conversion utility." |
---|
95 | (let ((first (ldb (byte 8 24) integer)) |
---|
96 | (second (ldb (byte 8 16) integer)) |
---|
97 | (third (ldb (byte 8 8) integer)) |
---|
98 | (fourth (ldb (byte 8 0) integer))) |
---|
99 | (format nil "~A.~A.~A.~A" first second third fourth))) |
---|
100 | |
---|
101 | (defun hbo-to-vector-quad (integer) |
---|
102 | "Host-byte-order integer to dotted-quad string conversion utility." |
---|
103 | (let ((first (ldb (byte 8 24) integer)) |
---|
104 | (second (ldb (byte 8 16) integer)) |
---|
105 | (third (ldb (byte 8 8) integer)) |
---|
106 | (fourth (ldb (byte 8 0) integer))) |
---|
107 | (vector first second third fourth))) |
---|
108 | |
---|
109 | (defun cut-between (string start-char end-chars &key (start 0) (cut-extra t)) |
---|
110 | "If `start-char' is not nil, cut string between `start-char' and any |
---|
111 | of the `end-chars', from `start'. If `start-char' is nil, cut from |
---|
112 | `start' until any of the `end-chars'. |
---|
113 | |
---|
114 | If `cut-extra' is t, we will cut from start + 1 instead of just |
---|
115 | `start'. |
---|
116 | |
---|
117 | When there is no string matching the input parameters `start' and nil |
---|
118 | will be returned, otherwise `end-position' and the string are |
---|
119 | returned." |
---|
120 | (let ((end-position (position-if #'(lambda (char) |
---|
121 | (member char end-chars)) |
---|
122 | string :start (1+ start))) |
---|
123 | (cut-from (if cut-extra |
---|
124 | (1+ start) |
---|
125 | start))) |
---|
126 | (if (and end-position start-char) |
---|
127 | (if (eql (char string start) start-char) |
---|
128 | (values end-position |
---|
129 | (subseq string cut-from end-position)) |
---|
130 | (values start nil)) |
---|
131 | (if end-position |
---|
132 | (values end-position |
---|
133 | (subseq string cut-from end-position)) |
---|
134 | (values start nil))))) |
---|
135 | |
---|
136 | (defun cut-before (string substring end-chars &key (start 0) (cut-extra t)) |
---|
137 | "Cut `string' before `substring' or any of the `end-chars', from `start'. |
---|
138 | |
---|
139 | If `cut-extra' is t, we will cut from start + 1 instead of just |
---|
140 | `start'. |
---|
141 | |
---|
142 | When there is no string matching the input parameters `start' and nil |
---|
143 | will be returned, otherwise `end-position' and the string are |
---|
144 | returned." |
---|
145 | (let ((end-position (search substring string :start2 start))) |
---|
146 | (if end-position |
---|
147 | (values (+ end-position (1- (length substring))) |
---|
148 | (subseq string (if (and cut-extra |
---|
149 | (< start end-position)) |
---|
150 | (1+ start) start) end-position)) |
---|
151 | (let ((end-position (position-if #'(lambda (x) |
---|
152 | (member x end-chars)) |
---|
153 | string :start (1+ start))) |
---|
154 | (cut-from (if cut-extra (1+ start) start))) |
---|
155 | (if end-position |
---|
156 | (values end-position |
---|
157 | (subseq string cut-from end-position)) |
---|
158 | (values start nil)))))) |
---|
159 | |
---|
160 | (defun parse-isupport-prefix-argument (prefix) |
---|
161 | (declare (type string prefix)) |
---|
162 | (let ((closing-paren-pos (position #\) prefix))) |
---|
163 | (when (and (eq (elt prefix 0) #\( ) |
---|
164 | closing-paren-pos) |
---|
165 | (let ((prefixes (subseq prefix (1+ closing-paren-pos))) |
---|
166 | (modes (subseq prefix 1 closing-paren-pos))) |
---|
167 | (when (= (length prefixes) |
---|
168 | (length modes)) |
---|
169 | (values prefixes modes)))))) |
---|
170 | |
---|
171 | (defun nick-prefixes-from-isupport (isupport-arguments) |
---|
172 | "Returns an assoc list associating prefix characters with mode characters." |
---|
173 | (multiple-value-bind |
---|
174 | (prefixes modes) |
---|
175 | (parse-isupport-prefix-argument (second (assoc "PREFIX" |
---|
176 | isupport-arguments |
---|
177 | :test #'string=))) |
---|
178 | (let ((rv)) |
---|
179 | (dotimes (i (length modes) |
---|
180 | rv) |
---|
181 | (setf (getf rv (char prefixes i)) |
---|
182 | (char modes i)))))) |
---|
183 | |
---|
184 | (defun chanmode-descs-from-isupport (isupport-arguments |
---|
185 | &optional |
---|
186 | (mode-symbols |
---|
187 | *default-char-to-channel-modes-map*)) |
---|
188 | "Parses a string describing channel modes conforming to |
---|
189 | http://www.irc.org/tech_docs/draft-brocklesby-irc-isupport-03.txt |
---|
190 | paragraph 3.3. |
---|
191 | |
---|
192 | It returns a list of mode-description records." |
---|
193 | (let* ((mode-desc-recs) |
---|
194 | (pref (second (assoc "PREFIX" isupport-arguments :test #'string=))) |
---|
195 | (chanmodes (second (assoc "CHANMODES" isupport-arguments |
---|
196 | :test #'string=))) |
---|
197 | (modes-list |
---|
198 | (cons (second (multiple-value-list |
---|
199 | (parse-isupport-prefix-argument pref))) |
---|
200 | (split-sequence:split-sequence #\, chanmodes))) |
---|
201 | (mode-descs '(;; B type mode from PREFIX with nick argument |
---|
202 | (t t t list-value-mode) |
---|
203 | ;; A type mode |
---|
204 | (:optional-for-server |
---|
205 | :optional-for-server nil list-value-mode) |
---|
206 | ;; B type mode from CHANMODES |
---|
207 | (t t nil single-value-mode) |
---|
208 | ;; C type mode from CHANMODES |
---|
209 | (t nil nil single-value-mode) |
---|
210 | ;; D type mode from CHANMODES |
---|
211 | (nil nil nil single-value-mode)))) |
---|
212 | (do ((mode (pop modes-list) (pop modes-list)) |
---|
213 | (mode-desc (pop mode-descs) (pop mode-descs))) |
---|
214 | ((null mode-desc) mode-desc-recs) |
---|
215 | (when (< 0 (length mode)) |
---|
216 | (let ((mode-struct |
---|
217 | (make-mode-description :param-on-set-p (first mode-desc) |
---|
218 | :param-on-unset-p (second mode-desc) |
---|
219 | :nick-param-p (third mode-desc) |
---|
220 | :class (fourth mode-desc)))) |
---|
221 | (dotimes (j (length mode)) |
---|
222 | (let ((mode-rec (copy-structure mode-struct)) |
---|
223 | (mode-char (elt mode j))) |
---|
224 | (setf (mode-desc-char mode-rec) mode-char |
---|
225 | (mode-desc-symbol mode-rec) (cdr (assoc mode-char |
---|
226 | mode-symbols))) |
---|
227 | (push mode-rec mode-desc-recs)))))))) |
---|
228 | |
---|
229 | (defmacro do-property-list ((prop val list) &body body) |
---|
230 | (let ((lsym (gensym))) |
---|
231 | `(let ((,lsym ,list)) |
---|
232 | (do* ((,prop (pop ,lsym) (pop ,lsym)) |
---|
233 | (,val (pop ,lsym) (pop ,lsym))) |
---|
234 | ((and (null ,lsym) |
---|
235 | (null ,prop) |
---|
236 | (null ,val))) |
---|
237 | ,@body)))) |
---|
238 | |
---|
239 | (defgeneric irc-string-downcase (map-name string &key start end)) |
---|
240 | |
---|
241 | (defmethod irc-string-downcase (map-name |
---|
242 | string &key (start 0) end) |
---|
243 | (declare (ignore map-name)) |
---|
244 | (let* ((new-string (substitute #\[ #\{ string :start start :end end)) |
---|
245 | (new-string (substitute #\] #\} new-string :start start :end end)) |
---|
246 | (new-string (substitute #\\ #\| new-string :start start :end end)) |
---|
247 | (new-string (substitute #\~ #\^ new-string :start start :end end))) |
---|
248 | (string-downcase new-string :start start :end end))) |
---|
249 | |
---|
250 | (defmethod irc-string-downcase ((map-name (eql :ascii)) |
---|
251 | string &key (start 0) end) |
---|
252 | (declare (ignore map-name)) |
---|
253 | (string-downcase string :start start :end end)) |
---|
254 | |
---|
255 | (defun parse-isupport-multivalue-argument (argument) |
---|
256 | (declare (type string argument)) |
---|
257 | (mapcar #'(lambda (x) |
---|
258 | (split-sequence:split-sequence #\: x)) |
---|
259 | (split-sequence:split-sequence #\, argument))) |
---|
260 | |
---|
261 | (defun parse-mode-arguments (connection target arguments &key server-p) |
---|
262 | "Create a list of mode changes with their arguments for `target' |
---|
263 | from `mode-string' and `arguments'. |
---|
264 | |
---|
265 | Throw nil to the UNKNOWN-MODE symbol if any of the mode chars are unknown." |
---|
266 | (catch 'illegal-mode-spec |
---|
267 | (if (and (= 1 (length arguments)) |
---|
268 | (null (position (char (first arguments) 0) "+-"))) |
---|
269 | ;; type 1 mode specification; only allowed on servers |
---|
270 | (when server-p |
---|
271 | (let ((ops) |
---|
272 | (arg (car arguments))) |
---|
273 | (dotimes (i (length arg) (reverse ops)) |
---|
274 | (push (char arg i) ops)))) |
---|
275 | ;; type 2 mode specification; clients and servers |
---|
276 | (let ((ops)) |
---|
277 | (do ((changes (pop arguments) (pop arguments))) |
---|
278 | ((null changes) (values ops nil)) |
---|
279 | (let* ((this-op (char changes 0)) |
---|
280 | (modes (subseq changes 1)) |
---|
281 | (param-req (if (char= this-op #\+) |
---|
282 | #'mode-desc-param-on-set-p |
---|
283 | #'mode-desc-param-on-unset-p))) |
---|
284 | (unless (position this-op "+-") |
---|
285 | (throw 'illegal-mode-spec nil)) |
---|
286 | (dotimes (i (length modes)) |
---|
287 | (let* ((mode-rec |
---|
288 | (mode-description connection target |
---|
289 | (mode-name-from-char connection target |
---|
290 | (char modes i)))) |
---|
291 | (param-p (funcall param-req mode-rec))) |
---|
292 | (when (and param-p |
---|
293 | (= 0 (length arguments))) |
---|
294 | (throw 'illegal-mode-spec nil)) |
---|
295 | (push (list this-op |
---|
296 | (mode-desc-symbol mode-rec) |
---|
297 | (when param-p |
---|
298 | (if (mode-desc-nick-param-p mode-rec) |
---|
299 | (find-user connection (pop arguments)) |
---|
300 | (pop arguments)))) ops))))))))) |
---|
301 | |
---|