1 | ;;;; $Id: command.lisp 169 2007-01-06 11:08:56Z ehuelsmann $ |
---|
2 | ;;;; $URL: tags/0.8.0/command.lisp $ |
---|
3 | |
---|
4 | ;;;; See LICENSE for licensing information. |
---|
5 | |
---|
6 | (in-package :irc) |
---|
7 | |
---|
8 | (defgeneric pass (connection password)) |
---|
9 | (defgeneric nick (connection new-nickname)) |
---|
10 | (defgeneric user- (connection username mode &optional realname)) |
---|
11 | (defgeneric oper (connection name password)) |
---|
12 | (defgeneric mode (connection nickname mode)) |
---|
13 | (defgeneric op (connection channel nickname)) |
---|
14 | (defgeneric deop (connection channel nickname)) |
---|
15 | (defgeneric voice (connection channel user)) |
---|
16 | (defgeneric devoice (connection channel nickname)) |
---|
17 | (defgeneric ban (connection channel mask)) |
---|
18 | (defgeneric unban (connection channel mask)) |
---|
19 | (defgeneric service (connection nickname distribution info)) |
---|
20 | (defgeneric quit (connection &optional message)) |
---|
21 | (defgeneric squit (connection server comment)) |
---|
22 | (defgeneric join (connection channel &key password)) |
---|
23 | (defgeneric multi-join (connection channels)) |
---|
24 | (defgeneric part (connection channel &optional reason)) |
---|
25 | (defgeneric part-all (connection &optional reason)) |
---|
26 | (defgeneric topic- (connection channel topic)) |
---|
27 | (defgeneric names (connection channel &optional target)) |
---|
28 | (defgeneric list- (connection &optional channel target)) |
---|
29 | (defgeneric invite (connection user channel)) |
---|
30 | (defgeneric kick (connection channel user &optional comment)) |
---|
31 | (defgeneric privmsg (connection channel message)) |
---|
32 | (defgeneric notice (connection target message)) |
---|
33 | (defgeneric motd- (connection &optional target)) |
---|
34 | (defgeneric lusers (connection &optional mask target)) |
---|
35 | (defgeneric version (connection &optional target)) |
---|
36 | (defgeneric stats (connection &optional query target)) |
---|
37 | (defgeneric links (connection &optional remote-server server-mask)) |
---|
38 | (defgeneric time- (connection &optional target)) |
---|
39 | (defgeneric trace- (connection &optional target)) |
---|
40 | (defgeneric admin (connection &optional target)) |
---|
41 | (defgeneric info (connection &optional target)) |
---|
42 | (defgeneric servlist (connection &optional mask type)) |
---|
43 | (defgeneric squery (connection service-name text)) |
---|
44 | (defgeneric who (connection &optional mask o)) |
---|
45 | (defgeneric whois (connection mask &optional target)) |
---|
46 | (defgeneric whowas (connection nickname &optional count target)) |
---|
47 | (defgeneric kill (connection user &optional comment)) |
---|
48 | (defgeneric ping (connection server)) |
---|
49 | (defgeneric pong (connection server &optional server2)) |
---|
50 | (defgeneric error- (connection message)) |
---|
51 | (defgeneric away (connection message)) |
---|
52 | (defgeneric rehash (connection)) |
---|
53 | (defgeneric die (connection)) |
---|
54 | (defgeneric restart- (connection)) |
---|
55 | (defgeneric summon (connection nickname &optional target channel)) |
---|
56 | (defgeneric users- (connection &optional target)) |
---|
57 | (defgeneric wallops (connection message)) |
---|
58 | (defgeneric userhost (connection nickname)) |
---|
59 | (defgeneric ison (connection user)) |
---|
60 | (defgeneric ctcp (connection target message)) |
---|
61 | (defgeneric ctcp-chat-initiate (connection nickname)) |
---|
62 | |
---|
63 | |
---|
64 | (defmethod pass ((connection connection) (password string)) |
---|
65 | "A \"PASS\" command is not required for a client connection to be |
---|
66 | registered, but it MUST precede the latter of the NICK/USER |
---|
67 | combination (for a user connection) or the SERVICE command (for a |
---|
68 | service connection). The RECOMMENDED order for a client to register is |
---|
69 | as follows: |
---|
70 | |
---|
71 | 1. Pass message |
---|
72 | 2. Nick message 2. Service message |
---|
73 | 3. User message |
---|
74 | |
---|
75 | Upon success, the client will receive an RPL_WELCOME (for users) or |
---|
76 | RPL_YOURESERVICE (for services) message indicating that the connection |
---|
77 | is now registered and known the to the entire IRC network. The reply |
---|
78 | message MUST contain the full client identifier upon which it was |
---|
79 | registered." |
---|
80 | (send-irc-message connection :pass password)) |
---|
81 | |
---|
82 | (defmethod nick ((connection connection) (new-nickname string)) |
---|
83 | (send-irc-message connection :nick new-nickname)) |
---|
84 | |
---|
85 | (defmethod user- ((connection connection) (username string) |
---|
86 | (mode integer) &optional (realname "")) |
---|
87 | (send-irc-message connection :user username mode "*" realname)) |
---|
88 | |
---|
89 | (defmethod oper ((connection connection) (name string) (password string)) |
---|
90 | (send-irc-message connection :oper name password)) |
---|
91 | |
---|
92 | (defmethod mode ((connection connection) (nickname string) (mode string)) |
---|
93 | (send-irc-message connection :mode nickname mode)) |
---|
94 | |
---|
95 | ;; utility functions not part of the RFCs |
---|
96 | (defmethod op ((connection connection) (channel string) (nickname string)) |
---|
97 | (send-irc-message connection :mode channel "+o" nickname)) |
---|
98 | |
---|
99 | (defmethod op ((connection connection) (channel channel) (user user)) |
---|
100 | (op connection (name channel) (nickname user))) |
---|
101 | |
---|
102 | (defmethod deop ((connection connection) (channel string) (nickname string)) |
---|
103 | (send-irc-message connection :mode channel "-o" nickname)) |
---|
104 | |
---|
105 | (defmethod deop ((connection connection) (channel channel) (user user)) |
---|
106 | (deop connection (name channel) (nickname user))) |
---|
107 | |
---|
108 | (defmethod voice ((connection connection) (channel string) (nickname string)) |
---|
109 | (send-irc-message connection :mode channel "+v" nickname)) |
---|
110 | |
---|
111 | (defmethod voice ((connection connection) (channel channel) (user user)) |
---|
112 | (voice connection (name channel) (nickname user))) |
---|
113 | |
---|
114 | (defmethod devoice ((connection connection) (channel string) (nickname string)) |
---|
115 | (send-irc-message connection :mode channel "-v" nickname)) |
---|
116 | |
---|
117 | (defmethod devoice ((connection connection) (channel channel) (user user)) |
---|
118 | (devoice connection (name channel) (nickname user))) |
---|
119 | |
---|
120 | (defmethod ban ((connection connection) (channel string) (mask string)) |
---|
121 | (send-irc-message connection :mode channel "+b" mask)) |
---|
122 | |
---|
123 | (defmethod ban ((connection connection) (channel channel) (mask string)) |
---|
124 | (ban connection (name channel) mask)) |
---|
125 | |
---|
126 | ;; unban or deban? |
---|
127 | (defmethod unban ((connection connection) (channel string) (mask string)) |
---|
128 | (send-irc-message connection :mode channel "-b" mask)) |
---|
129 | |
---|
130 | (defmethod unban ((connection connection) (channel channel) (mask string)) |
---|
131 | (unban connection (name channel) mask)) |
---|
132 | |
---|
133 | (defmethod service ((connection connection) (nickname string) |
---|
134 | (distribution string) (info string)) |
---|
135 | (send-irc-message connection :service nickname "*" distribution 0 0 info)) |
---|
136 | |
---|
137 | (defmethod quit ((connection connection) &optional (message *default-quit-message*)) |
---|
138 | (remove-all-channels connection) |
---|
139 | (remove-all-users connection) |
---|
140 | (unwind-protect |
---|
141 | (send-irc-message connection :quit message) |
---|
142 | #+(and sbcl (not sb-thread)) |
---|
143 | (sb-sys:invalidate-descriptor (sb-sys:fd-stream-fd |
---|
144 | (network-stream connection))) |
---|
145 | (close (network-stream connection)))) |
---|
146 | |
---|
147 | (defmethod squit ((connection connection) (server string) (comment string)) |
---|
148 | (send-irc-message connection :squit server comment)) |
---|
149 | |
---|
150 | (defmethod join ((connection connection) (channel string) &key password) |
---|
151 | (apply #'send-irc-message |
---|
152 | connection :join channel (when password (list password)))) |
---|
153 | |
---|
154 | (defmethod join ((connection connection) (channel channel) &key password) |
---|
155 | (join connection (name channel) :password password)) |
---|
156 | |
---|
157 | ;; utility function not part of the RFC |
---|
158 | (defmethod multi-join ((connection connection) (channels list)) |
---|
159 | (dolist (channel channels) |
---|
160 | (join connection channel))) |
---|
161 | |
---|
162 | (defmethod part ((connection connection) (channel string) &optional reason) |
---|
163 | (apply #'send-irc-message |
---|
164 | connection :part channel (when reason (list reason)))) |
---|
165 | |
---|
166 | (defmethod part ((connection connection) (channel channel) &optional reason) |
---|
167 | (part connection (name channel) reason)) |
---|
168 | |
---|
169 | ;; utility function not part of the RFC |
---|
170 | (defmethod part-all ((connection connection) &optional reason) |
---|
171 | (dolist (channel (channels connection)) |
---|
172 | (part connection (name channel) reason))) |
---|
173 | |
---|
174 | (defmethod topic- ((connection connection) (channel string) (topic string)) |
---|
175 | (send-irc-message connection :topic channel topic)) |
---|
176 | |
---|
177 | (defmethod topic- ((connection connection) (channel channel) (topic string)) |
---|
178 | (topic- connection (name channel) topic)) |
---|
179 | |
---|
180 | (defmethod names ((connection connection) (channel string) |
---|
181 | &optional (target "")) |
---|
182 | (send-irc-message connection :names channel target)) |
---|
183 | |
---|
184 | (defmethod names ((connection connection) (channel channel) |
---|
185 | &optional (target "")) |
---|
186 | (names connection (name channel) target)) |
---|
187 | |
---|
188 | (defmethod list- ((connection connection) &optional |
---|
189 | (channel "") (target "")) |
---|
190 | (send-irc-message connection :list channel target)) |
---|
191 | |
---|
192 | (defmethod invite ((connection connection) (nickname string) (channel string)) |
---|
193 | (send-irc-message connection :invite nickname channel)) |
---|
194 | |
---|
195 | (defmethod invite ((connection connection) (user user) (channel channel)) |
---|
196 | (invite connection (nickname user) (name channel))) |
---|
197 | |
---|
198 | (defmethod kick ((connection connection) (channel string) |
---|
199 | (user string) &optional (comment "")) |
---|
200 | (send-irc-message connection :kick channel user comment)) |
---|
201 | |
---|
202 | (defmethod kick ((connection connection) (channel channel) |
---|
203 | (user user) &optional (comment "")) |
---|
204 | (kick connection (name channel) (nickname user) comment)) |
---|
205 | |
---|
206 | (defmethod privmsg ((connection connection) (target string) (message string)) |
---|
207 | (send-irc-message connection :privmsg target message)) |
---|
208 | |
---|
209 | (defmethod privmsg ((connection connection) (user user) (message string)) |
---|
210 | (privmsg connection (nickname user) message)) |
---|
211 | |
---|
212 | (defmethod privmsg ((connection connection) (channel channel) (message string)) |
---|
213 | (privmsg connection (name channel) message)) |
---|
214 | |
---|
215 | (defmethod notice ((connection connection) (target string) (message string)) |
---|
216 | (send-irc-message connection :notice target message)) |
---|
217 | |
---|
218 | (defmethod notice ((connection connection) (user user) (message string)) |
---|
219 | (notice connection (nickname user) message)) |
---|
220 | |
---|
221 | (defmethod notice ((connection connection) (channel channel) (message string)) |
---|
222 | (notice connection (name channel) message)) |
---|
223 | |
---|
224 | (defmethod motd- ((connection connection) &optional (target "")) |
---|
225 | (send-irc-message connection :motd target)) |
---|
226 | |
---|
227 | (defmethod lusers ((connection connection) &optional (mask "") (target "")) |
---|
228 | (send-irc-message connection :lusers mask target)) |
---|
229 | |
---|
230 | (defmethod version ((connection connection) &optional (target "")) |
---|
231 | (send-irc-message connection :version target)) |
---|
232 | |
---|
233 | (defmethod stats ((connection connection) &optional (query "") (target "")) |
---|
234 | (send-irc-message connection :stats query target)) |
---|
235 | |
---|
236 | (defmethod links ((connection connection) &optional (remote-server "") |
---|
237 | (server-mask "")) |
---|
238 | (send-irc-message connection :links remote-server server-mask)) |
---|
239 | |
---|
240 | (defmethod time- ((connection connection) &optional (target "")) |
---|
241 | (send-irc-message connection :time target)) |
---|
242 | |
---|
243 | (defun connect (&key (nickname *default-nickname*) |
---|
244 | (username nil) |
---|
245 | (realname nil) |
---|
246 | (password nil) |
---|
247 | (mode 0) |
---|
248 | (server *default-irc-server*) |
---|
249 | (port *default-irc-server-port*) |
---|
250 | (connection-type 'connection) |
---|
251 | (logging-stream t)) |
---|
252 | "Connect to server and return a connection object." |
---|
253 | (let* ((socket (usocket:socket-connect server port |
---|
254 | :element-type 'flexi-streams:octet)) |
---|
255 | (stream (usocket:socket-stream socket)) |
---|
256 | (connection (make-connection :connection-type connection-type |
---|
257 | :socket socket |
---|
258 | :network-stream stream |
---|
259 | :client-stream logging-stream |
---|
260 | :server-name server)) |
---|
261 | (user (make-user connection |
---|
262 | :nickname nickname |
---|
263 | :username username |
---|
264 | :realname realname))) |
---|
265 | (setf (user connection) user) |
---|
266 | (unless (null password) |
---|
267 | (pass connection password)) |
---|
268 | (nick connection nickname) |
---|
269 | (user- connection (or username nickname) mode (or realname nickname)) |
---|
270 | (add-default-hooks connection) |
---|
271 | connection)) |
---|
272 | |
---|
273 | (defmethod trace- ((connection connection) &optional (target "")) |
---|
274 | (send-irc-message connection :trace target)) |
---|
275 | |
---|
276 | (defmethod admin ((connection connection) &optional (target "")) |
---|
277 | (send-irc-message connection :admin target)) |
---|
278 | |
---|
279 | (defmethod info ((connection connection) &optional (target "")) |
---|
280 | (send-irc-message connection :info target)) |
---|
281 | |
---|
282 | (defmethod servlist ((connection connection) &optional (mask "") (type "")) |
---|
283 | (send-irc-message connection :servlist mask type)) |
---|
284 | |
---|
285 | (defmethod squery ((connection connection) (service-name string) (text string)) |
---|
286 | (send-irc-message connection :squery text service-name)) |
---|
287 | |
---|
288 | (defmethod who ((connection connection) &optional (mask "") (o "")) |
---|
289 | (send-irc-message connection :who mask o)) |
---|
290 | |
---|
291 | (defmethod whois ((connection connection) (mask string) &optional (target "")) |
---|
292 | (send-irc-message connection :whois target mask)) |
---|
293 | |
---|
294 | (defmethod whowas ((connection connection) (nickname string) |
---|
295 | &optional (count "") (target "")) |
---|
296 | (send-irc-message connection :whowas nickname count target)) |
---|
297 | |
---|
298 | (defmethod kill ((connection connection) (nickname string) &optional (comment "")) |
---|
299 | (send-irc-message connection :kill comment nickname)) |
---|
300 | |
---|
301 | (defmethod kill ((connection connection) (user user) &optional (comment "")) |
---|
302 | (kill connection (nickname user) comment)) |
---|
303 | |
---|
304 | (defmethod ping ((connection connection) (server string)) |
---|
305 | (send-irc-message connection :ping server)) |
---|
306 | |
---|
307 | (defmethod pong ((connection connection) (server string) &optional (server2 "")) |
---|
308 | (send-irc-message connection :pong server server2)) |
---|
309 | |
---|
310 | (defmethod error- ((connection connection) (message string)) |
---|
311 | (send-irc-message connection :error message)) |
---|
312 | |
---|
313 | (defmethod away ((connection connection) (message string)) |
---|
314 | (send-irc-message connection :away message)) |
---|
315 | |
---|
316 | (defmethod rehash ((connection connection)) |
---|
317 | (send-irc-message connection :rehash)) |
---|
318 | |
---|
319 | (defmethod die ((connection connection)) |
---|
320 | (send-irc-message connection :die)) |
---|
321 | |
---|
322 | (defmethod restart- ((connection connection)) |
---|
323 | (send-irc-message connection :restart)) |
---|
324 | |
---|
325 | (defmethod summon ((connection connection) (nickname string) |
---|
326 | &optional (target "") (channel "")) |
---|
327 | (send-irc-message connection :summon nickname target channel)) |
---|
328 | |
---|
329 | (defmethod users- ((connection connection) &optional (target "")) |
---|
330 | (send-irc-message connection :users target)) |
---|
331 | |
---|
332 | (defmethod wallops ((connection connection) (message string)) |
---|
333 | (send-irc-message connection :wallops message)) |
---|
334 | |
---|
335 | (defmethod userhost ((connection connection) (nickname string)) |
---|
336 | (send-irc-message connection :userhost nickname)) |
---|
337 | |
---|
338 | (defmethod userhost ((connection connection) (user user)) |
---|
339 | (userhost connection (nickname user))) |
---|
340 | |
---|
341 | (defmethod ison ((connection connection) (nickname string)) |
---|
342 | (send-irc-message connection :ison nickname)) |
---|
343 | |
---|
344 | (defmethod ison ((connection connection) (user user)) |
---|
345 | (ison connection (nickname user))) |
---|
346 | |
---|
347 | ;; utility functions not part of the RFC |
---|
348 | (defmethod ctcp ((connection connection) target message) |
---|
349 | (send-irc-message connection :privmsg target (make-ctcp-message message))) |
---|
350 | |
---|
351 | #| |
---|
352 | There's too much wrong with this method to fix it now. |
---|
353 | |
---|
354 | (defmethod ctcp-chat-initiate ((connection connection) (nickname string)) |
---|
355 | #+sbcl |
---|
356 | (let ((socket (sb-bsd-sockets:make-inet-socket :stream :tcp)) |
---|
357 | (port 44347)) |
---|
358 | (sb-bsd-sockets:socket-bind socket #(127 0 0 1) port) ; arbitrary port |
---|
359 | (sb-bsd-sockets:socket-listen socket 1) ; accept one connection |
---|
360 | (ctcp connection nickname |
---|
361 | (format nil "DCC CHAT chat ~A ~A" |
---|
362 | ; the use of hostname here is incorrect (it could be a firewall's IP) |
---|
363 | (host-byte-order (hostname (user connection))) port)) |
---|
364 | (make-dcc-connection :user (find-user connection nickname) |
---|
365 | :input-stream t |
---|
366 | :output-stream (sb-bsd-sockets:socket-make-stream socket :input t :output t :buffering :none) |
---|
367 | :socket socket)) |
---|
368 | #-sbcl (warn "ctcp-chat-initiate is not supported on this implementation.") |
---|
369 | ) |
---|
370 | |# |
---|