source: trunk/command.lisp

Last change on this file was 238, checked in by Erik Huelsmann, 11 years ago

Patch by Julien Danjou: MODE argument to mode command should be optional.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision URL
File size: 24.2 KB
Line 
1;;;; $Id: command.lisp 238 2013-01-13 18:50:37Z ehuelsmann $
2;;;; $URL: trunk/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 &optional 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 action (connection target message))
61(defgeneric ctcp (connection target message))
62(defgeneric ctcp-reply (connection target message))
63(defgeneric ctcp-chat-initiate (connection nickname &key passive)
64  (:documentation "Initiate a DCC chat session with `nickname' associated
65with `connection'.
66
67If `passive' is non-NIL, the remote is requested to serve as a DCC
68host. Otherwise, the local system will serve as a DCC host.  The
69latter may be a problem for firewalled or NATted hosts."))
70(defgeneric dcc-request-accept (message)
71  (:documentation ""))
72(defgeneric dcc-request-reject (message &optional reason)
73  (:documentation ""))
74(defgeneric dcc-request-cancel (connection token)
75  (:documentation ""))
76
77
78(defmethod pass ((connection connection) (password string))
79  "A \"PASS\" command is not required for a client connection to be
80registered, but it MUST precede the latter of the NICK/USER
81combination (for a user connection) or the SERVICE command (for a
82service connection). The RECOMMENDED order for a client to register is
83as follows:
84
85                           1. Pass message
86           2. Nick message                 2. Service message
87           3. User message
88
89Upon success, the client will receive an RPL_WELCOME (for users) or
90RPL_YOURESERVICE (for services) message indicating that the connection
91is now registered and known the to the entire IRC network.  The reply
92message MUST contain the full client identifier upon which it was
93registered."
94  (send-irc-message connection :pass password))
95
96(defmethod nick ((connection connection) (new-nickname string))
97  (send-irc-message connection :nick new-nickname))
98
99(defmethod user- ((connection connection) (username string)
100                  (mode integer) &optional (realname ""))
101  (send-irc-message connection :user username mode "*" realname))
102
103(defmethod oper ((connection connection) (name string) (password string))
104  (send-irc-message connection :oper name password))
105
106(defmethod mode ((connection connection) (nickname string) &optional mode)
107  (send-irc-message connection :mode nickname mode))
108
109;; utility functions not part of the RFCs
110(defmethod op ((connection connection) (channel string) (nickname string))
111  (send-irc-message connection :mode channel "+o" nickname))
112
113(defmethod op ((connection connection) (channel channel) (user user))
114  (op connection (name channel) (nickname user)))
115
116(defmethod deop ((connection connection) (channel string) (nickname string))
117  (send-irc-message connection :mode channel "-o" nickname))
118
119(defmethod deop ((connection connection) (channel channel) (user user))
120  (deop connection (name channel) (nickname user)))
121
122(defmethod voice ((connection connection) (channel string) (nickname string))
123  (send-irc-message connection :mode channel "+v" nickname))
124
125(defmethod voice ((connection connection) (channel channel) (user user))
126  (voice connection (name channel) (nickname user)))
127
128(defmethod devoice ((connection connection) (channel string) (nickname string))
129  (send-irc-message connection :mode channel "-v" nickname))
130
131(defmethod devoice ((connection connection) (channel channel) (user user))
132  (devoice connection (name channel) (nickname user)))
133
134(defmethod ban ((connection connection) (channel string) (mask string))
135  (send-irc-message connection :mode channel "+b" mask))
136
137(defmethod ban ((connection connection) (channel channel) (mask string))
138  (ban connection (name channel) mask))
139
140;; unban or deban?
141(defmethod unban ((connection connection) (channel string) (mask string))
142  (send-irc-message connection :mode channel "-b" mask))
143
144(defmethod unban ((connection connection) (channel channel) (mask string))
145  (unban connection (name channel) mask))
146
147(defmethod service ((connection connection) (nickname string)
148                    (distribution string) (info string))
149  (send-irc-message connection :service nickname "*" distribution 0 0 info))
150
151(defmethod quit ((connection connection) &optional (message *default-quit-message*))
152  (remove-all-channels connection)
153  (remove-all-users connection)
154  (dolist (dcc (dcc-connections connection))
155    (when (close-on-main dcc)
156      (quit dcc "Main IRC server connection lost.")))
157  (unwind-protect
158      (send-irc-message connection :quit message)
159    #+(and sbcl (not sb-thread))
160    (sb-sys:invalidate-descriptor (sb-sys:fd-stream-fd
161                                   (network-stream connection)))
162    (close (network-stream connection))))
163
164(defmethod squit ((connection connection) (server string) (comment string))
165  (send-irc-message connection :squit server comment))
166
167(defmethod join ((connection connection) (channel string) &key password)
168  (apply #'send-irc-message
169         connection :join channel (when password (list password))))
170
171(defmethod join ((connection connection) (channel channel) &key password)
172  (join connection (name channel) :password password))
173
174;; utility function not part of the RFC
175(defmethod multi-join ((connection connection) (channels list))
176  (dolist (channel channels)
177    (join connection channel)))
178
179(defmethod part ((connection connection) (channel string) &optional reason)
180  (apply #'send-irc-message
181         connection :part channel (when reason (list reason))))
182
183(defmethod part ((connection connection) (channel channel) &optional reason)
184  (part connection (name channel) reason))
185
186;; utility function not part of the RFC
187(defmethod part-all ((connection connection) &optional reason)
188  (maphash #'(lambda (chan obj)
189               (declare (ignore obj))
190               (part connection chan reason))
191           (channels connection)))
192
193(defmethod topic- ((connection connection) (channel string) (topic string))
194  (send-irc-message connection :topic channel topic))
195
196(defmethod topic- ((connection connection) (channel channel) (topic string))
197  (topic- connection (name channel) topic))
198
199(defmethod names ((connection connection) (channel string)
200                  &optional (target ""))
201  (send-irc-message connection :names channel target))
202
203(defmethod names ((connection connection) (channel channel)
204                  &optional (target ""))
205  (names connection (name channel) target))
206
207(defmethod list- ((connection connection) &optional
208                  (channel "") (target ""))
209  (send-irc-message connection :list channel target))
210
211(defmethod invite ((connection connection) (nickname string) (channel string))
212  (send-irc-message connection :invite nickname channel))
213
214(defmethod invite ((connection connection) (user user) (channel channel))
215  (invite connection (nickname user) (name channel)))
216
217(defmethod kick ((connection connection) (channel string)
218                 (user string) &optional (comment ""))
219  (send-irc-message connection :kick channel user comment))
220
221(defmethod kick ((connection connection) (channel channel)
222                 (user user) &optional (comment ""))
223  (kick connection (name channel) (nickname user) comment))
224
225(defmethod privmsg ((connection connection) (target string) (message string))
226  (send-irc-message connection :privmsg target message))
227
228(defmethod privmsg ((connection connection) (user user) (message string))
229  (privmsg connection (nickname user) message))
230
231(defmethod privmsg ((connection connection) (channel channel) (message string))
232  (privmsg connection (name channel) message))
233
234(defmethod privmsg ((connection dcc-chat-connection) target message)
235  (declare (ignore target))
236  (send-dcc-message connection message))
237
238(defmethod notice ((connection connection) (target string) (message string))
239  (send-irc-message connection :notice target message))
240
241(defmethod notice ((connection connection) (user user) (message string))
242  (notice connection (nickname user) message))
243
244(defmethod notice ((connection connection) (channel channel) (message string))
245  (notice connection (name channel) message))
246
247(defmethod motd- ((connection connection) &optional (target ""))
248  (send-irc-message connection :motd target))
249
250(defmethod lusers ((connection connection) &optional (mask "") (target ""))
251  (send-irc-message connection :lusers mask target))
252
253(defmethod version ((connection connection) &optional (target ""))
254  (send-irc-message connection :version target))
255
256(defmethod stats ((connection connection) &optional (query "") (target ""))
257  (send-irc-message connection :stats query target))
258
259(defmethod links ((connection connection) &optional (remote-server "")
260                  (server-mask ""))
261  (send-irc-message connection :links remote-server server-mask))
262
263(defmethod time- ((connection connection) &optional (target ""))
264  (send-irc-message connection :time target))
265
266(defun connect (&key (nickname *default-nickname*)
267                     (username nil)
268                     (realname nil)
269                     (password nil)
270                     (mode 0)
271                     (server *default-irc-server*)
272                     (port :default)
273                     (connection-type 'connection)
274                     (connection-security :none)
275                     (logging-stream t))
276  "Connect to server and return a connection object.
277
278`port' and `connection-security' have a relation: when `port' equals
279`:default' `*default-irc-server-port*' is used to find which port to
280connect to.  `connection-security' determines which port number is found.
281
282`connection-security' can be either `:none' or `:ssl'.  When passing
283`:ssl', the cl+ssl library must have been loaded by the caller.
284"
285  (let* ((port (if (eq port :default)
286                   ;; get the default port for this type of connection
287                   (getf *default-irc-server-port* connection-security)
288                 port))
289         (socket (usocket:socket-connect server port
290                                         :element-type 'flexi-streams:octet))
291         (stream (if (eq connection-security :ssl)
292                     (dynfound-funcall (make-ssl-client-stream :cl+ssl)
293                                       (usocket:socket-stream socket))
294                   (usocket:socket-stream socket)))
295         (connection (make-connection :connection-type connection-type
296                                      :network-stream stream
297                                      :client-stream logging-stream
298                                      :server-name server)))
299    #+sbcl (setf (sb-bsd-sockets::sockopt-keep-alive (usocket:socket socket)) t)
300    (unless (null password)
301      (pass connection password))
302    (nick connection nickname)
303    (user- connection (or username nickname) mode (or realname nickname))
304    (add-default-hooks connection)
305    connection))
306
307(defmethod trace- ((connection connection) &optional (target ""))
308  (send-irc-message connection :trace target))
309
310(defmethod admin ((connection connection) &optional (target ""))
311  (send-irc-message connection :admin target))
312
313(defmethod info ((connection connection) &optional (target ""))
314  (send-irc-message connection :info target))
315
316(defmethod servlist ((connection connection) &optional (mask "") (type ""))
317  (send-irc-message connection :servlist mask type))
318
319(defmethod squery ((connection connection) (service-name string) (text string))
320  (send-irc-message connection :squery text service-name))
321
322(defmethod who ((connection connection) &optional (mask "") (o ""))
323  (send-irc-message connection :who mask o))
324
325(defmethod whois ((connection connection) (mask string) &optional (target ""))
326  (send-irc-message connection :whois target mask))
327
328(defmethod whowas ((connection connection) (nickname string)
329                   &optional (count "") (target ""))
330  (send-irc-message connection :whowas nickname count target))
331
332(defmethod kill ((connection connection) (nickname string) &optional (comment ""))
333  (send-irc-message connection :kill comment nickname))
334
335(defmethod kill ((connection connection) (user user) &optional (comment ""))
336  (kill connection (nickname user) comment))
337
338(defmethod ping ((connection connection) (server string))
339  (send-irc-message connection :ping server))
340
341(defmethod pong ((connection connection) (server string) &optional server2)
342  (if server2
343      (send-irc-message connection :pong server server2)
344      (send-irc-message connection :pong server)))
345
346(defmethod error- ((connection connection) (message string))
347  (send-irc-message connection :error message))
348
349(defmethod away ((connection connection) (message string))
350  (send-irc-message connection :away message))
351
352(defmethod rehash ((connection connection))
353  (send-irc-message connection :rehash))
354
355(defmethod die ((connection connection))
356  (send-irc-message connection :die))
357
358(defmethod restart- ((connection connection))
359  (send-irc-message connection :restart))
360
361(defmethod summon ((connection connection) (nickname string)
362                   &optional (target "") (channel ""))
363  (send-irc-message connection :summon nickname target channel))
364
365(defmethod users- ((connection connection) &optional (target ""))
366  (send-irc-message connection :users target))
367
368(defmethod wallops ((connection connection) (message string))
369  (send-irc-message connection :wallops message))
370
371(defmethod userhost ((connection connection) (nickname string))
372  (send-irc-message connection :userhost nickname))
373
374(defmethod userhost ((connection connection) (user user))
375  (userhost connection (nickname user)))
376
377(defmethod ison ((connection connection) (nickname string))
378  (send-irc-message connection :ison nickname))
379
380(defmethod ison ((connection connection) (user user))
381  (ison connection (nickname user)))
382
383;; utility functions not part of the RFC
384(defmethod ctcp ((connection connection) target message)
385  (send-irc-message connection :privmsg target (make-ctcp-message message)))
386
387(defmethod ctcp-reply ((connection connection) target message)
388  (send-irc-message connection :notice target (make-ctcp-message message)))
389
390(defmethod action ((connection connection) (target string) (message string))
391  (ctcp connection target (concatenate 'string "ACTION " message)))
392
393(defmethod action ((connection connection) (user user) (message string))
394  (action connection (nickname user) message))
395
396(defmethod action ((connection connection) (channel channel) (message string))
397  (action connection (name channel) message))
398
399
400;; Intermezzo: Manage outstanding offers
401
402(defvar *passive-offer-sequence-token* 0)
403
404(defgeneric dcc-add-offer (connection nickname type token &optional proto)
405  (:documentation "Adds an offer to the list off outstanding offers list
406for `connection'."))
407
408(defgeneric dcc-remove-offer (connection token)
409  ;; Tokens are uniquely defined within the scope of the library,
410  ;; so we don't need anything but the token to actually remove an offer
411  (:documentation "Remove an offer from the list of outstanding offers
412for `connection'."))
413
414(defgeneric dcc-get-offer (connection token))
415(defgeneric dcc-get-offers (connection nickname &key type token))
416
417(defun matches-offer-by-token-p (offer token)
418  (equal (third offer) token))
419
420(defun matches-offer-by-user-p (offer user)
421  (equal (first offer) user))
422
423(defun offer-matches-message-p (offer message-nick message-type message-token)
424  (and (equal (first offer) message-nick)
425       (equal (second offer) message-type)
426       (equal (third offer) message-token)))
427
428(defmethod dcc-add-offer (connection nickname type token &optional proto)
429  (push (list nickname type token) (dcc-offers connection)))
430
431(defmethod dcc-remove-offer (connection token)
432  (setf (dcc-offers connection)
433        (remove-if #'(lambda (x)
434                       (matches-offer-by-token-p x token))
435                   (dcc-offers connection))))
436
437(defmethod dcc-get-offer (connection token)
438  (let ((offer-list (remove-if #'(lambda (x)
439                                   (not (equal (third x) token)))
440                               (dcc-offers connection))))
441    (first offer-list)))
442
443(defmethod dcc-get-offers (connection nickname &key type token)
444  (let* ((results (remove-if #'(lambda (x)
445                                 (not (matches-offer-by-user-p x nickname)))
446                             (dcc-offers connection)))
447         (results (if type
448                      (remove-if #'(lambda (x)
449                                     (not (equal type (second x)))) results)
450                    results))
451         (results (if token
452                      (remove-if #'(lambda (x)
453                                     (not (equal token (third x)))) results))))
454    results))
455
456;; End of intermezzo
457
458;;
459;; And we move on with the definitions required to manage the protocol
460;;
461
462(defmethod ctcp-chat-initiate ((connection connection) (nickname string)
463                               &key passive)
464  (if passive
465      ;; do passive request
466      (let ((token (princ-to-string (incf *passive-offer-sequence-token*))))
467        ;; tokens have been specified to be integer values,
468        (dcc-add-offer connection nickname "CHAT" token)
469        (ctcp connection nickname
470              (format nil "DCC CHAT CHAT ~A 0 ~A"
471                      (usocket:host-byte-order #(1 1 1 1))
472                      token))
473        token)
474    ;; or do active request
475    (error "Active DCC initiating not (yet) supported.")))
476
477(defmethod ctcp-chat-initiate ((connection dcc-chat-connection)
478                               nickname &key passive)
479  (declare (ignore nickname passive))
480  (error "Chat connection already in progress"))
481
482(defmethod dcc-request-cancel ((connection connection) token)
483  (dcc-remove-offer connection token)
484  (if (stringp token)
485      (let ((offer (dcc-get-offer connection token)))
486        ;; We have a passive request; active ones have an associated
487        ;; socket instead...
488        (ctcp-reply connection (first offer)
489                    (format nil "DCC REJECT ~A ~A" (second offer) token)))
490    (progn
491      ;; do something to close the socket here...
492      ;; OTOH, we don't support active sockets (yet), so, comment out.
493#|
494      (usocket:socket-close token)
495      (ctcp-reply connection nickname (format nil
496      "ERRMSG DCC ~A timed out" type))
497|#
498      )))
499
500(defmethod dcc-request-cancel ((connection dcc-chat-connection) token)
501  (dcc-request-cancel (irc-connection connection) token))
502
503(defmethod dcc-request-accept ((message ctcp-dcc-chat-request-message))
504  ;; There are 2 options here: it was an active dcc offer or a passive one
505  ;; For now, we'll support only active offers (where we act as a client)
506  (let* ((raw-offer (car (last (arguments message))))
507         (clean-offer (string-trim (list +soh+) raw-offer))
508         (args (tokenize-string clean-offer))
509         (remote-ip (ignore-errors (parse-integer (fourth args))))
510         (remote-port (ignore-errors (parse-integer (fifth args))))
511         (their-token (sixth args))
512         (irc-connection (connection message)))
513    (when (string= (string-upcase (third args)) "CHAT")
514      (if (= remote-port 0)
515          ;; a passive chat request, which we don't support (yet):
516          ;; we don't act as a server yet
517          (ctcp-reply irc-connection (source message)
518                      "ERRMSG DCC CHAT passive-CHAT unavailable")
519        (progn
520          (when their-token
521            (let ((offer (dcc-get-offer irc-connection their-token)))
522              (when (or (null offer)
523                        (not (offer-matches-message-p offer
524                                                      (source message)
525                                                      "CHAT" their-token)))
526                (ctcp-reply irc-connection (source message)
527                            (format nil
528                                    "ERRMSG DCC CHAT invalid token (~A)"
529                                    their-token))
530                (return-from dcc-request-accept))))
531          ;; ok, so either there was no token, or it matches
532          ;;
533          ;; When there was no token, but there was a chat request
534          ;; with the same nick and type, maybe we achieved the same
535          ;; in the end. (This would be caused by the other side
536          ;; initiating the request manually after the client blocked
537          ;; and automatic response.
538          (let ((offers (dcc-get-offers irc-connection (source message)
539                                        :type "CHAT")))
540            (when offers
541              ;; if there are more offers, consider the first fulfilled.
542              (dcc-remove-offer irc-connection (third (first offers)))))
543
544          (let ((socket (unless (or (null remote-ip)
545                                    (null remote-port)
546                                    (= 0 remote-port))
547                   (usocket:socket-connect
548                    remote-ip remote-port
549                    :element-type 'flexi-streams:octet))))
550            (dcc-remove-offer irc-connection their-token)
551            (make-dcc-chat-connection
552             :irc-connection irc-connection
553             :remote-user (find-user irc-connection (source message))
554             :network-stream (usocket:socket-stream socket))))))))
555
556(defmethod dcc-request-accept ((message dcc-ctcp-dcc-chat-request-message))
557  (error "DCC Chat already in progress"))
558
559(defmethod dcc-request-reject ((message ctcp-dcc-chat-request-message)
560                               &optional reason)
561  (ctcp-reply (connection message) (source message)
562              (format nil "ERRMSG DCC CHAT ~A" (if reason reason
563                                                 "rejected"))))
564
565(defmethod dcc-request-reject ((message dcc-ctcp-dcc-chat-request-message)
566                               &optional reason)
567  (ctcp-reply (irc-connection (connection message))
568              (nickname (user (connection message)))
569              (format nil "ERRMSG DCC CHAT ~A" (if reason reason
570                                                 "rejected"))))
571
572;;
573;; IRC commands which make some sence in a DCC CHAT context
574;;
575
576(defmethod quit ((connection dcc-chat-connection)
577                 &optional message)
578  (when message
579    (ignore-errors (send-dcc-message connection message)))
580  (ignore-errors
581    (dcc-close connection)))
582
583;;## TODO
584;; ctcp action, time, source, finger, ping+pong message generation
585;; btw: those could be defined for 'normal' IRC too; currently
586;; we only generate the responses to others' messages.
Note: See TracBrowser for help on using the repository browser.