1 | ;;;; $Id: parse-message.lisp 74 2005-01-01 14:25:17Z ehuelsmann $ |
---|
2 | ;;;; $Source$ |
---|
3 | |
---|
4 | ;;;; See the LICENSE file for licensing information. |
---|
5 | |
---|
6 | (in-package :irc) |
---|
7 | |
---|
8 | (defun find-reply-name (reply-number &key (reply-names *reply-names*)) |
---|
9 | "Numeric replies in the IRC RFCs have more meaningful names. Given |
---|
10 | a numeric reply (`reply-number') this function will either return the |
---|
11 | symbol representing the reply or raise a continuable error |
---|
12 | (`no-such-reply') which gives you the opportunity to ignore the |
---|
13 | situation." |
---|
14 | (let ((name (assoc reply-number reply-names))) |
---|
15 | (if name |
---|
16 | (cadr name) |
---|
17 | (progn |
---|
18 | (cerror "Ignore unknown reply." |
---|
19 | 'no-such-reply :reply-number reply-number) |
---|
20 | :unknown-reply)))) |
---|
21 | |
---|
22 | (defun return-source (string &key (start 0)) |
---|
23 | "Assuming `string' is a valid IRC message this function returns the |
---|
24 | source part of the message. Returns nil if the source part is not |
---|
25 | present." |
---|
26 | (cut-between string #\: '(#\! #\Space) :start start)) |
---|
27 | |
---|
28 | (defun return-user (string &key (start 0)) |
---|
29 | "Assuming `string' is a valid IRC message this function returns the |
---|
30 | user part of the message. Returns nil if the user part is not |
---|
31 | present." |
---|
32 | (cut-between string #\! '(#\@ #\Space) :start start)) |
---|
33 | |
---|
34 | (defun return-host (string &key (start 0)) |
---|
35 | "Assuming `string' is a valid IRC message this function returns the |
---|
36 | host part of the message. Returns nil if the host part is not |
---|
37 | present." |
---|
38 | (cut-between string #\@ '(#\Space) :start start)) |
---|
39 | |
---|
40 | (defun return-command (string &key (start 0)) |
---|
41 | "Assuming `string' is a valid IRC message this function returns the |
---|
42 | command part of the message. Returns nil if the command part is not |
---|
43 | present." |
---|
44 | (if (eql (char string start) #\Space) |
---|
45 | (cut-between string #\Space '(#\Space) :start start) |
---|
46 | (cut-between string nil '(#\Space) :start start :cut-extra nil))) |
---|
47 | |
---|
48 | (defun return-arguments (string &key (start 0)) |
---|
49 | "Assuming `string' is a valid IRC message this function returns the |
---|
50 | arguments part of the message as a list. Returns nil if the arguments |
---|
51 | part is not present." |
---|
52 | (multiple-value-bind (end-position return-argument) |
---|
53 | (cut-before string " :" '(#\Return) :start start) |
---|
54 | (values end-position (tokenize-string return-argument |
---|
55 | :delimiters '(#\Space))))) |
---|
56 | |
---|
57 | (defun return-trailing-argument (string &key (start 0)) |
---|
58 | "Assuming `string' is a valid IRC message this function returns the |
---|
59 | trailing-argument part of the message. Returns nil if the |
---|
60 | trailing-argument part is not present." |
---|
61 | (cut-between string #\: '(#\Return) :start start)) |
---|
62 | |
---|
63 | (defun parse-raw-message (string &key (start 0)) |
---|
64 | "Assuming `string' is a valid IRC message, parse the message and |
---|
65 | return the values in the following order: |
---|
66 | |
---|
67 | - source |
---|
68 | - user |
---|
69 | - host |
---|
70 | - command |
---|
71 | - arguments |
---|
72 | - trailing-argument |
---|
73 | |
---|
74 | Any values not present will be represented as nil." |
---|
75 | (let ((index start) |
---|
76 | (returns nil)) |
---|
77 | (dolist (function '(return-source |
---|
78 | return-user |
---|
79 | return-host |
---|
80 | return-command |
---|
81 | return-arguments |
---|
82 | return-trailing-argument)) |
---|
83 | (multiple-value-bind (return-index return-string) |
---|
84 | (funcall function string :start index) |
---|
85 | (setf index return-index) |
---|
86 | (push return-string returns))) |
---|
87 | (apply #'values (reverse returns)))) |
---|
88 | |
---|
89 | (defun irc-error-reply-p (string) |
---|
90 | "Returns t if `string' is a string-representation of an IRC error |
---|
91 | reply message, nil otherwise." |
---|
92 | (unless (zerop (length string)) |
---|
93 | (if (and (every #'digit-char-p string) |
---|
94 | (member (char string 0) '(#\4 #\5))) |
---|
95 | t |
---|
96 | nil))) |
---|
97 | |
---|
98 | (defun numeric-reply-p (string) |
---|
99 | "Returns t if `string' is a string-representation of an IRC number |
---|
100 | reply, nil otherwise." |
---|
101 | (every #'digit-char-p string)) |
---|
102 | |
---|
103 | (defun ctcp-type-p (string type) |
---|
104 | "Is the `string' actually a representation of the CTCP `type'?" |
---|
105 | (if (string-equal (subseq string 1 (min (length string) |
---|
106 | (1+ (length (symbol-name type))))) |
---|
107 | type) |
---|
108 | type |
---|
109 | nil)) |
---|
110 | |
---|
111 | (defun dcc-type-p (string type) |
---|
112 | "Is the `string' actually a representation of the DCC `type'?" |
---|
113 | (case type |
---|
114 | (:dcc-chat-request |
---|
115 | (when (string-equal (char string 5) #\C) |
---|
116 | :dcc-chat-request)) |
---|
117 | (:dcc-send-request |
---|
118 | (when (string-equal (char string 5) #\S) |
---|
119 | :dcc-send-request)) |
---|
120 | (otherwise nil))) |
---|
121 | |
---|
122 | (defun ctcp-message-type (string) |
---|
123 | "If `string' is a CTCP message, return the type of the message or |
---|
124 | nil if this is a) not a CTCP message or b) a CTCP message we don't |
---|
125 | know about." |
---|
126 | (if (or (not (stringp string)) |
---|
127 | (zerop (length string)) |
---|
128 | (not (eql (char string 0) +soh+))) |
---|
129 | nil |
---|
130 | (case (char string 1) |
---|
131 | (#\A (ctcp-type-p string :action)) |
---|
132 | (#\C (ctcp-type-p string :clientinfo)) |
---|
133 | (#\D |
---|
134 | (or (dcc-type-p string :dcc-chat-request) |
---|
135 | (dcc-type-p string :dcc-send-request))) |
---|
136 | (#\F (ctcp-type-p string :finger)) |
---|
137 | (#\P (ctcp-type-p string :ping)) |
---|
138 | (#\S (ctcp-type-p string :source)) |
---|
139 | (#\T (ctcp-type-p string :time)) |
---|
140 | (#\U (ctcp-type-p string :userinfo)) |
---|
141 | (#\V (ctcp-type-p string :version)) |
---|
142 | (otherwise nil)))) |
---|
143 | |
---|
144 | (defun create-irc-message (string) |
---|
145 | "If `string' is a valid IRC message parse it and return an object of |
---|
146 | the correct type with its slots prefilled according to the information |
---|
147 | in the message." |
---|
148 | (multiple-value-bind (source user host command arguments trailing-argument) |
---|
149 | (parse-raw-message string) |
---|
150 | (let ((class 'irc-message) |
---|
151 | (ctcp (ctcp-message-type trailing-argument))) |
---|
152 | (when command |
---|
153 | (cond |
---|
154 | (nil ;(irc-error-reply-p command) |
---|
155 | ;; Disable for now, as it prevents adding hooks for some useful |
---|
156 | ;; error types |
---|
157 | (progn |
---|
158 | (setf command (find-reply-name (parse-integer command))) |
---|
159 | (setf class 'irc-error-reply))) |
---|
160 | ((numeric-reply-p command) |
---|
161 | (progn |
---|
162 | (setf command (find-reply-name (parse-integer command))) |
---|
163 | (setf class (find-irc-message-class command)))) |
---|
164 | (t |
---|
165 | (progn |
---|
166 | (setf command (intern (string-upcase command) |
---|
167 | (find-package :keyword))) |
---|
168 | (setf class (find-irc-message-class command)))))) |
---|
169 | (when ctcp |
---|
170 | (setf class (find-ctcp-message-class ctcp))) |
---|
171 | (let ((instance (make-instance class |
---|
172 | :source (or source "") |
---|
173 | :user (or user "") |
---|
174 | :host (or host "") |
---|
175 | :command (if command |
---|
176 | (string command) |
---|
177 | "") |
---|
178 | :arguments arguments |
---|
179 | :connection nil |
---|
180 | :trailing-argument (or trailing-argument "") |
---|
181 | :received-time (get-universal-time) |
---|
182 | :raw-message-string (or string "")))) |
---|
183 | (when ctcp |
---|
184 | (setf (ctcp-command instance) ctcp)) |
---|
185 | instance)))) |
---|