source: tags/cl-irc_upstream_version_0_5/parse-message.lisp

Last change on this file was 2, checked in by Erik Enge, 20 years ago

Initial revision

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.8 KB
Line 
1;;;; $Id: parse-message.lisp 2 2004-01-05 14:13:03Z eenge $
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
10a numeric reply (`reply-number') this function will either return the
11symbol representing the reply or raise a continuable error
12(`no-such-reply') which gives you the opportunity to ignore the
13situation."
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
24source part of the message.  Returns nil if the source part is not
25present."
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
30user part of the message.  Returns nil if the user part is not
31present."
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
36host part of the message.  Returns nil if the host part is not
37present."
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
42command part of the message.  Returns nil if the command part is not
43present."
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
50arguments part of the message as a list.  Returns nil if the arguments
51part is not present."
52  (multiple-value-bind (end-position return-argument)
53      (cut-between string nil '(#\: #\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
59trailing-argument part of the message.  Returns nil if the
60trailing-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
65return the values in the following order:
66
67  - source
68  - user
69  - host
70  - command
71  - arguments
72  - trailing-argument
73
74Any 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
91reply 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
100reply, 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
124nil if this is a) not a CTCP message or b) a CTCP message we don't
125know 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
146the correct type with its slots prefilled according to the information
147in 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          ((irc-error-reply-p command)
155           (progn
156             (setf command (find-reply-name (parse-integer command)))
157             (setf class 'irc-error-reply)))
158          ((numeric-reply-p command)
159           (progn
160             (setf command (find-reply-name (parse-integer command)))
161             (setf class (find-irc-message-class command))))
162          (t
163           (progn
164             (setf command (intern (string-upcase command)
165                                   (find-package :keyword)))
166             (setf class (find-irc-message-class command))))))
167      (when ctcp
168        (setf class (find-ctcp-message-class ctcp)))
169      (let ((instance (make-instance class
170                                     :source source
171                                     :user user
172                                     :host host
173                                     :command command
174                                     :arguments arguments
175                                     :connection nil
176                                     :trailing-argument trailing-argument
177                                     :received-time (get-universal-time)
178                                     :raw-message-string string)))
179        (when ctcp
180          (setf (ctcp-command instance) ctcp))
181        instance))))
Note: See TracBrowser for help on using the repository browser.