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