]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/rfc2812.rb
758f574e0bde8a5ca076708196e6bffdfdd327ed
[user/henk/code/ruby/rbot.git] / lib / rbot / rfc2812.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: RFC 2821 Client Protocol module
5 #
6 # This module defines the Irc::Client class, a class that can handle and
7 # dispatch messages based on RFC 2821 (Internet Relay Chat: Client Protocol)
8
9 module Irc
10   # - The server sends Replies 001 to 004 to a user upon
11   #   successful registration.
12
13   # "Welcome to the Internet Relay Network
14   # <nick>!<user>@<host>"
15   #
16   RPL_WELCOME=001
17
18   # "Your host is <servername>, running version <ver>"
19   RPL_YOURHOST=002
20
21   # "This server was created <date>"
22   RPL_CREATED=003
23
24   # "<servername> <version> <available user modes> <available channel modes>"
25   RPL_MYINFO=004
26
27   # "005 nick PREFIX=(ov)@+ CHANTYPES=#& :are supported by this server"
28   #
29   # defines the capabilities supported by the server.
30   #
31   # Previous RFCs defined message 005 as follows:
32   #
33   # - Sent by the server to a user to suggest an alternative
34   #   server.  This is often used when the connection is
35   #   refused because the server is already full.
36   #
37   # # "Try server <server name>, port <port number>"
38   #
39   # RPL_BOUNCE=005
40   #
41   RPL_ISUPPORT=005
42
43   # ":*1<reply> *( " " <reply> )"
44   #
45   # - Reply format used by USERHOST to list replies to
46   #   the query list.  The reply string is composed as
47   #   follows:
48   #
49   # reply = nickname [ "*" ] "=" ( "+" / "-" ) hostname
50   #
51   # The '*' indicates whether the client has registered
52   # as an Operator.  The '-' or '+' characters represent
53   # whether the client has set an AWAY message or not
54   # respectively.
55   #
56   RPL_USERHOST=302
57
58   # ":*1<nick> *( " " <nick> )"
59   #
60   # - Reply format used by ISON to list replies to the
61   #   query list.
62   #
63   RPL_ISON=303
64
65   # - These replies are used with the AWAY command (if
66   #   allowed).  RPL_AWAY is sent to any client sending a
67   #   PRIVMSG to a client which is away.  RPL_AWAY is only
68   #   sent by the server to which the client is connected.
69   #   Replies RPL_UNAWAY and RPL_NOWAWAY are sent when the
70   #   client removes and sets an AWAY message.
71
72   # "<nick> :<away message>"
73   RPL_AWAY=301
74
75   # ":You are no longer marked as being away"
76   RPL_UNAWAY=305
77
78   # ":You have been marked as being away"
79   RPL_NOWAWAY=306
80
81   # - Replies 311 - 313, 317 - 319 are all replies
82   #   generated in response to a WHOIS message.  Given that
83   #   there are enough parameters present, the answering
84   #   server MUST either formulate a reply out of the above
85   #   numerics (if the query nick is found) or return an
86   #   error reply.  The '*' in RPL_WHOISUSER is there as
87   #   the literal character and not as a wild card.  For
88   #   each reply set, only RPL_WHOISCHANNELS may appear
89   #   more than once (for long lists of channel names).
90   #   The '@' and '+' characters next to the channel name
91   #   indicate whether a client is a channel operator or
92   #   has been granted permission to speak on a moderated
93   #   channel.  The RPL_ENDOFWHOIS reply is used to mark
94   #   the end of processing a WHOIS message.
95
96   # "<nick> <user> <host> * :<real name>"
97   RPL_WHOISUSER=311
98
99   # "<nick> <server> :<server info>"
100   RPL_WHOISSERVER=312
101
102   # "<nick> :is an IRC operator"
103   RPL_WHOISOPERATOR=313
104
105   # "<nick> <integer> :seconds idle"
106   RPL_WHOISIDLE=317
107
108   # "<nick> :End of WHOIS list"
109   RPL_ENDOFWHOIS=318
110
111   # "<nick> :*( ( "@" / "+" ) <channel> " " )"
112   RPL_WHOISCHANNELS=319
113
114   # - When replying to a WHOWAS message, a server MUST use
115   #   the replies RPL_WHOWASUSER, RPL_WHOISSERVER or
116   #   ERR_WASNOSUCHNICK for each nickname in the presented
117   #   list.  At the end of all reply batches, there MUST
118   #   be RPL_ENDOFWHOWAS (even if there was only one reply
119   #   and it was an error).
120
121   # "<nick> <user> <host> * :<real name>"
122   RPL_WHOWASUSER=314
123
124   # "<nick> :End of WHOWAS"
125   RPL_ENDOFWHOWAS=369
126
127   # - Replies RPL_LIST, RPL_LISTEND mark the actual replies
128   #   with data and end of the server's response to a LIST
129   #   command.  If there are no channels available to return,
130   #   only the end reply MUST be sent.
131
132   # Obsolete. Not used.
133   RPL_LISTSTART=321
134
135   # "<channel> <# visible> :<topic>"
136   RPL_LIST=322
137
138   # ":End of LIST"
139   RPL_LISTEND=323
140
141   # "<channel> <nickname>"
142   RPL_UNIQOPIS=325
143
144   # "<channel> <mode> <mode params>"
145   RPL_CHANNELMODEIS=324
146
147   # "<channel> <unixtime>"
148   RPL_CREATIONTIME=329
149
150   # "<channel> <url>"
151   RPL_CHANNEL_URL=328
152
153   # "<channel> :No topic is set"
154   RPL_NOTOPIC=331
155
156   # - When sending a TOPIC message to determine the
157   #   channel topic, one of two replies is sent.  If
158   #   the topic is set, RPL_TOPIC is sent back else
159   #   RPL_NOTOPIC.
160
161   # "<channel> :<topic>"
162   RPL_TOPIC=332
163
164   # <channel> <set by> <unixtime>
165   RPL_TOPIC_INFO=333
166
167   # "<channel> <nick>"
168   #
169   # - Returned by the server to indicate that the
170   #   attempted INVITE message was successful and is
171   #   being passed onto the end client.
172   #
173   RPL_INVITING=341
174
175   # "<user> :Summoning user to IRC"
176   #
177   # - Returned by a server answering a SUMMON message to
178   #   indicate that it is summoning that user.
179   #
180   RPL_SUMMONING=342
181
182   # "<channel> <invitemask>"
183   RPL_INVITELIST=346
184
185   # "<channel> :End of channel invite list"
186   #
187   # - When listing the 'invitations masks' for a given channel,
188   #   a server is required to send the list back using the
189   #   RPL_INVITELIST and RPL_ENDOFINVITELIST messages.  A
190   #   separate RPL_INVITELIST is sent for each active mask.
191   #   After the masks have been listed (or if none present) a
192   #   RPL_ENDOFINVITELIST MUST be sent.
193   #
194   RPL_ENDOFINVITELIST=347
195
196   # "<channel> <exceptionmask>"
197   RPL_EXCEPTLIST=348
198
199   # "<channel> :End of channel exception list"
200   #
201   # - When listing the 'exception masks' for a given channel,
202   #   a server is required to send the list back using the
203   #   RPL_EXCEPTLIST and RPL_ENDOFEXCEPTLIST messages.  A
204   #   separate RPL_EXCEPTLIST is sent for each active mask.
205   #   After the masks have been listed (or if none present)
206   #   a RPL_ENDOFEXCEPTLIST MUST be sent.
207   #
208   RPL_ENDOFEXCEPTLIST=349
209
210   # "<version>.<debuglevel> <server> :<comments>"
211   #
212   # - Reply by the server showing its version details.
213   #
214   # The <version> is the version of the software being
215   # used (including any patchlevel revisions) and the
216   # <debuglevel> is used to indicate if the server is
217   # running in "debug mode".
218   #
219   # The "comments" field may contain any comments about
220   # the version or further version details.
221   #
222   RPL_VERSION=351
223
224   # - The RPL_WHOREPLY and RPL_ENDOFWHO pair are used
225   #   to answer a WHO message.  The RPL_WHOREPLY is only
226   #   sent if there is an appropriate match to the WHO
227   #   query.  If there is a list of parameters supplied
228   #   with a WHO message, a RPL_ENDOFWHO MUST be sent
229   #   after processing each list item with <name> being
230   #   the item.
231
232   # "<channel> <user> <host> <server> <nick>
233   # ( "H" / "G" > ["*"] [ ( "@" / "+" ) ]
234   # :<hopcount> <real name>"
235   #
236   RPL_WHOREPLY=352
237
238   # "<name> :End of WHO list"
239   RPL_ENDOFWHO=315
240
241   # - To reply to a NAMES message, a reply pair consisting
242   #   of RPL_NAMREPLY and RPL_ENDOFNAMES is sent by the
243   #   server back to the client.  If there is no channel
244   #   found as in the query, then only RPL_ENDOFNAMES is
245   #   returned.  The exception to this is when a NAMES
246   #   message is sent with no parameters and all visible
247   #   channels and contents are sent back in a series of
248   #   RPL_NAMEREPLY messages with a RPL_ENDOFNAMES to mark
249   #   the end.
250
251   # "( "=" / "*" / "@" ) <channel>
252   # :[ "@" / "+" ] <nick> *( " " [ "@" / "+" ] <nick> )
253   # - "@" is used for secret channels, "*" for private
254   # channels, and "=" for others (public channels).
255   #
256   RPL_NAMREPLY=353
257
258   # "<channel> :End of NAMES list"
259   RPL_ENDOFNAMES=366
260
261   # - In replying to the LINKS message, a server MUST send
262   #   replies back using the RPL_LINKS numeric and mark the
263   #   end of the list using an RPL_ENDOFLINKS reply.
264
265   # "<mask> <server> :<hopcount> <server info>"
266   RPL_LINKS=364
267
268   # "<mask> :End of LINKS list"
269   RPL_ENDOFLINKS=365
270
271   # - When listing the active 'bans' for a given channel,
272   #   a server is required to send the list back using the
273   #   RPL_BANLIST and RPL_ENDOFBANLIST messages.  A separate
274   #   RPL_BANLIST is sent for each active banmask.  After the
275   #   banmasks have been listed (or if none present) a
276   #   RPL_ENDOFBANLIST MUST be sent.
277
278   # "<channel> <banmask>"
279   RPL_BANLIST=367
280
281   # "<channel> :End of channel ban list"
282   RPL_ENDOFBANLIST=368
283
284   # - A server responding to an INFO message is required to
285   #   send all its 'info' in a series of RPL_INFO messages
286   #   with a RPL_ENDOFINFO reply to indicate the end of the
287   #   replies.
288
289   # ":<string>"
290   RPL_INFO=371
291
292   # ":End of INFO list"
293   RPL_ENDOFINFO=374
294
295   # - When responding to the MOTD message and the MOTD file
296   # is found, the file is displayed line by line, with
297   # each line no longer than 80 characters, using
298   # RPL_MOTD format replies.  These MUST be surrounded
299   # by a RPL_MOTDSTART (before the RPL_MOTDs) and an
300   # RPL_ENDOFMOTD (after).
301
302   # ":- <server> Message of the day - "
303   RPL_MOTDSTART=375
304
305   # ":- <text>"
306   RPL_MOTD=372
307
308   # ":End of MOTD command"
309   RPL_ENDOFMOTD=376
310
311   # ":You are now an IRC operator"
312   #
313   # - RPL_YOUREOPER is sent back to a client which has
314   #   just successfully issued an OPER message and gained
315   #   operator status.
316   #
317   RPL_YOUREOPER=381
318
319   # "<config file> :Rehashing"
320   #
321   # - If the REHASH option is used and an operator sends
322   #   a REHASH message, an RPL_REHASHING is sent back to
323   #   the operator.
324   #
325   RPL_REHASHING=382
326
327   # "You are service <servicename>"
328   #
329   # - Sent by the server to a service upon successful
330   #   registration.
331   #
332   RPL_YOURESERVICE=383
333
334   # "<server> :<string showing server's local time>"
335   #
336   # - When replying to the TIME message, a server MUST send
337   #   the reply using the RPL_TIME format above.  The string
338   #   showing the time need only contain the correct day and
339   #   time there.  There is no further requirement for the
340   #   time string.
341   #
342   RPL_TIME=391
343
344   # - If the USERS message is handled by a server, the
345   #   replies RPL_USERSTART, RPL_USERS, RPL_ENDOFUSERS and
346   #   RPL_NOUSERS are used.  RPL_USERSSTART MUST be sent
347   #   first, following by either a sequence of RPL_USERS
348   #   or a single RPL_NOUSER.  Following this is
349   #   RPL_ENDOFUSERS.
350
351   # ":UserID   Terminal  Host"
352   RPL_USERSSTART=392
353
354   # ":<username> <ttyline> <hostname>"
355   RPL_USERS=393
356
357   # ":End of users"
358   RPL_ENDOFUSERS=394
359
360   # ":Nobody logged in"
361   RPL_NOUSERS=395
362
363   # - The RPL_TRACE* are all returned by the server in
364   #   response to the TRACE message.  How many are
365   #   returned is dependent on the TRACE message and
366   #   whether it was sent by an operator or not.  There
367   #   is no predefined order for which occurs first.
368   #   Replies RPL_TRACEUNKNOWN, RPL_TRACECONNECTING and
369   #   RPL_TRACEHANDSHAKE are all used for connections
370   #   which have not been fully established and are either
371   #   unknown, still attempting to connect or in the
372   #   process of completing the 'server handshake'.
373   #   RPL_TRACELINK is sent by any server which handles
374   #   a TRACE message and has to pass it on to another
375   #   server.  The list of RPL_TRACELINKs sent in
376   #   response to a TRACE command traversing the IRC
377   #   network should reflect the actual connectivity of
378   #   the servers themselves along that path.
379   #
380   # RPL_TRACENEWTYPE is to be used for any connection
381   # which does not fit in the other categories but is
382   # being displayed anyway.
383   # RPL_TRACEEND is sent to indicate the end of the list.
384
385   # "Link <version & debug level> <destination>
386   # <next server> V<protocol version>
387   # <link uptime in seconds> <backstream sendq>
388   # <upstream sendq>"
389   RPL_TRACELINK=200
390
391   # "Try. <class> <server>"
392   RPL_TRACECONNECTING=201
393
394   # "H.S. <class> <server>"
395   RPL_TRACEHANDSHAKE=202
396
397   # "???? <class> [<client IP address in dot form>]"
398   RPL_TRACEUNKNOWN=203
399
400   # "Oper <class> <nick>"
401   RPL_TRACEOPERATOR=204
402
403   # "User <class> <nick>"
404   RPL_TRACEUSER=205
405
406   # "Serv <class> <int>S <int>C <server>
407   # <nick!user|*!*>@<host|server> V<protocol version>"
408   RPL_TRACESERVER=206
409
410   # "Service <class> <name> <type> <active type>"
411   RPL_TRACESERVICE=207
412
413   # "<newtype> 0 <client name>"
414   RPL_TRACENEWTYPE=208
415
416   # "Class <class> <count>"
417   RPL_TRACECLASS=209
418
419   # Unused.
420   RPL_TRACERECONNECT=210
421
422   # "File <logfile> <debug level>"
423   RPL_TRACELOG=261
424
425   # "<server name> <version & debug level> :End of TRACE"
426   RPL_TRACEEND=262
427
428   # ":Current local  users: 3  Max: 4"
429   RPL_LOCALUSERS=265
430
431   # ":Current global users: 3  Max: 4"
432   RPL_GLOBALUSERS=266
433
434   # "::Highest connection count: 4 (4 clients) (251 since server was
435   # (re)started)"
436   RPL_STATSCONN=250
437
438   # "<linkname> <sendq> <sent messages>
439   # <sent Kbytes> <received messages>
440   # <received Kbytes> <time open>"
441   #
442   # - reports statistics on a connection.  <linkname>
443   #   identifies the particular connection, <sendq> is
444   #   the amount of data that is queued and waiting to be
445   #   sent <sent messages> the number of messages sent,
446   #   and <sent Kbytes> the amount of data sent, in
447   #   Kbytes. <received messages> and <received Kbytes>
448   #   are the equivalent of <sent messages> and <sent
449   #   Kbytes> for received data, respectively.  <time
450   #   open> indicates how long ago the connection was
451   #   opened, in seconds.
452   #
453   RPL_STATSLINKINFO=211
454
455   # "<command> <count> <byte count> <remote count>"
456   #
457   # - reports statistics on commands usage.
458   #
459   RPL_STATSCOMMANDS=212
460
461   # "<stats letter> :End of STATS report"
462   #
463   RPL_ENDOFSTATS=219
464
465   # ":Server Up %d days %d:%02d:%02d"
466   #
467   # - reports the server uptime.
468   #
469   RPL_STATSUPTIME=242
470
471   # "O <hostmask> * <name>"
472   #
473   # - reports the allowed hosts from where user may become IRC
474   #   operators.
475   #
476   RPL_STATSOLINE=243
477
478   # "<user mode string>"
479   #
480   # - To answer a query about a client's own mode,
481   #   RPL_UMODEIS is sent back.
482   #
483   RPL_UMODEIS=221
484
485   # - When listing services in reply to a SERVLIST message,
486   #   a server is required to send the list back using the
487   #   RPL_SERVLIST and RPL_SERVLISTEND messages.  A separate
488   #   RPL_SERVLIST is sent for each service.  After the
489   #   services have been listed (or if none present) a
490   #   RPL_SERVLISTEND MUST be sent.
491
492   # "<name> <server> <mask> <type> <hopcount> <info>"
493   RPL_SERVLIST=234
494
495   # "<mask> <type> :End of service listing"
496   RPL_SERVLISTEND=235
497
498   # - In processing an LUSERS message, the server
499   #   sends a set of replies from RPL_LUSERCLIENT,
500   #   RPL_LUSEROP, RPL_USERUNKNOWN,
501   #   RPL_LUSERCHANNELS and RPL_LUSERME.  When
502   #   replying, a server MUST send back
503   #   RPL_LUSERCLIENT and RPL_LUSERME.  The other
504   #   replies are only sent back if a non-zero count
505   #   is found for them.
506
507   # ":There are <integer> users and <integer>
508   # services on <integer> servers"
509   RPL_LUSERCLIENT=251
510
511   # "<integer> :operator(s) online"
512   RPL_LUSEROP=252
513
514   # "<integer> :unknown connection(s)"
515   RPL_LUSERUNKNOWN=253
516
517   # "<integer> :channels formed"
518   RPL_LUSERCHANNELS=254
519
520   # ":I have <integer> clients and <integer> servers"
521   RPL_LUSERME=255
522
523   # - When replying to an ADMIN message, a server
524   # is expected to use replies RPL_ADMINME
525   # through to RPL_ADMINEMAIL and provide a text
526   # message with each.  For RPL_ADMINLOC1 a
527   # description of what city, state and country
528   # the server is in is expected, followed by
529   # details of the institution (RPL_ADMINLOC2)
530   # and finally the administrative contact for the
531   # server (an email address here is REQUIRED)
532   # in RPL_ADMINEMAIL.
533
534   # "<server> :Administrative info"
535   RPL_ADMINME=256
536
537   # ":<admin info>"
538   RPL_ADMINLOC1=257
539
540   # ":<admin info>"
541   RPL_ADMINLOC2=258
542
543   # ":<admin info>"
544   RPL_ADMINEMAIL=259
545
546   # "<command> :Please wait a while and try again."
547   #
548   # - When a server drops a command without processing it,
549   #   it MUST use the reply RPL_TRYAGAIN to inform the
550   #   originating client.
551   RPL_TRYAGAIN=263
552
553   # 5.2 Error Replies
554   #
555   # Error replies are found in the range from 400 to 599.
556
557   # "<nickname> :No such nick/channel"
558   #
559   # - Used to indicate the nickname parameter supplied to a
560   #   command is currently unused.
561   #
562   ERR_NOSUCHNICK=401
563
564   # "<server name> :No such server"
565   #
566   # - Used to indicate the server name given currently
567   #   does not exist.
568   #
569   ERR_NOSUCHSERVER=402
570
571   # "<channel name> :No such channel"
572   #
573   # - Used to indicate the given channel name is invalid.
574   #
575   ERR_NOSUCHCHANNEL=403
576
577   # "<channel name> :Cannot send to channel"
578   #
579   # - Sent to a user who is either (a) not on a channel
580   #   which is mode +n or (b) not a chanop (or mode +v) on
581   #   a channel which has mode +m set or where the user is
582   #   banned and is trying to send a PRIVMSG message to
583   #   that channel.
584   #
585   ERR_CANNOTSENDTOCHAN=404
586
587   # "<channel name> :You have joined too many channels"
588   #
589   # - Sent to a user when they have joined the maximum
590   #   number of allowed channels and they try to join
591   #   another channel.
592   #
593   ERR_TOOMANYCHANNELS=405
594
595   # "<nickname> :There was no such nickname"
596   #
597   # - Returned by WHOWAS to indicate there is no history
598   #   information for that nickname.
599   #
600   ERR_WASNOSUCHNICK=406
601
602   # "<target> :<error code> recipients. <abort message>"
603   #
604   # - Returned to a client which is attempting to send a
605   #   PRIVMSG/NOTICE using the user@host destination format
606   #   and for a user@host which has several occurrences.
607   #
608   # - Returned to a client which trying to send a
609   #   PRIVMSG/NOTICE to too many recipients.
610   #
611   # - Returned to a client which is attempting to JOIN a safe
612   #   channel using the shortname when there are more than one
613   #   such channel.
614   #
615   ERR_TOOMANYTARGETS=407
616
617   # "<service name> :No such service"
618   #
619   # - Returned to a client which is attempting to send a SQUERY
620   #   to a service which does not exist.
621   #
622   ERR_NOSUCHSERVICE=408
623
624   # ":No origin specified"
625   #
626   # - PING or PONG message missing the originator parameter.
627   #
628   ERR_NOORIGIN=409
629
630   # ":No recipient given (<command>)"
631   ERR_NORECIPIENT=411
632
633   # - 412 - 415 are returned by PRIVMSG to indicate that
634   #   the message wasn't delivered for some reason.
635   #   ERR_NOTOPLEVEL and ERR_WILDTOPLEVEL are errors that
636   #   are returned when an invalid use of
637   #   "PRIVMSG $<server>" or "PRIVMSG #<host>" is attempted.
638
639   # ":No text to send"
640   ERR_NOTEXTTOSEND=412
641
642   # "<mask> :No toplevel domain specified"
643   ERR_NOTOPLEVEL=413
644
645   # "<mask> :Wildcard in toplevel domain"
646   ERR_WILDTOPLEVEL=414
647
648   # "<mask> :Bad Server/host mask"
649   ERR_BADMASK=415
650
651   # "<command> :Unknown command"
652   #
653   # - Returned to a registered client to indicate that the
654   #   command sent is unknown by the server.
655   #
656   ERR_UNKNOWNCOMMAND=421
657
658   # ":MOTD File is missing"
659   #
660   # - Server's MOTD file could not be opened by the server.
661   #
662   ERR_NOMOTD=422
663
664   # "<server> :No administrative info available"
665   #
666   # - Returned by a server in response to an ADMIN message
667   #   when there is an error in finding the appropriate
668   #   information.
669   #
670   ERR_NOADMININFO=423
671
672   # ":File error doing <file op> on <file>"
673   #
674   # - Generic error message used to report a failed file
675   #   operation during the processing of a message.
676   #
677   ERR_FILEERROR=424
678
679   # ":No nickname given"
680   #
681   # - Returned when a nickname parameter expected for a
682   #   command and isn't found.
683   #
684   ERR_NONICKNAMEGIVEN=431
685
686   # "<nick> :Erroneous nickname"
687   #
688   # - Returned after receiving a NICK message which contains
689   #   characters which do not fall in the defined set.  See
690   #   section 2.3.1 for details on valid nicknames.
691   #
692   ERR_ERRONEUSNICKNAME=432
693
694   # "<nick> :Nickname is already in use"
695   #
696   # - Returned when a NICK message is processed that results
697   #   in an attempt to change to a currently existing
698   #   nickname.
699   #
700   ERR_NICKNAMEINUSE=433
701
702   # "<nick> :Nickname collision KILL from <user>@<host>"
703   #
704   # - Returned by a server to a client when it detects a
705   #   nickname collision (registered of a NICK that
706   #   already exists by another server).
707   #
708   ERR_NICKCOLLISION=436
709
710   # "<nick/channel> :Nick/channel is temporarily unavailable"
711   #
712   # - Returned by a server to a user trying to join a channel
713   #   currently blocked by the channel delay mechanism.
714   #
715   # - Returned by a server to a user trying to change nickname
716   #   when the desired nickname is blocked by the nick delay
717   #   mechanism.
718   #
719   ERR_UNAVAILRESOURCE=437
720
721   # "<nick> <channel> :They aren't on that channel"
722   #
723   # - Returned by the server to indicate that the target
724   #   user of the command is not on the given channel.
725   #
726   ERR_USERNOTINCHANNEL=441
727
728   # "<channel> :You're not on that channel"
729   #
730   # - Returned by the server whenever a client tries to
731   #   perform a channel affecting command for which the
732   #   client isn't a member.
733   #
734   ERR_NOTONCHANNEL=442
735
736   # "<user> <channel> :is already on channel"
737   #
738   # - Returned when a client tries to invite a user to a
739   #   channel they are already on.
740   #
741   ERR_USERONCHANNEL=443
742
743   # "<user> :User not logged in"
744   #
745   # - Returned by the summon after a SUMMON command for a
746   #   user was unable to be performed since they were not
747   #   logged in.
748   #
749   ERR_NOLOGIN=444
750
751   # ":SUMMON has been disabled"
752   #
753   # - Returned as a response to the SUMMON command.  MUST be
754   #   returned by any server which doesn't implement it.
755   #
756   ERR_SUMMONDISABLED=445
757
758   # ":USERS has been disabled"
759   #
760   # - Returned as a response to the USERS command.  MUST be
761   #   returned by any server which does not implement it.
762   #
763   ERR_USERSDISABLED=446
764
765   # ":You have not registered"
766   #
767   # - Returned by the server to indicate that the client
768   #   MUST be registered before the server will allow it
769   #   to be parsed in detail.
770   #
771   ERR_NOTREGISTERED=451
772
773   # "<command> :Not enough parameters"
774   #
775   # - Returned by the server by numerous commands to
776   #   indicate to the client that it didn't supply enough
777   #   parameters.
778   #
779   ERR_NEEDMOREPARAMS=461
780
781   # ":Unauthorized command (already registered)"
782   #
783   # - Returned by the server to any link which tries to
784   #   change part of the registered details (such as
785   #   password or user details from second USER message).
786   #
787   ERR_ALREADYREGISTRED=462
788
789   # ":Your host isn't among the privileged"
790   #
791   # - Returned to a client which attempts to register with
792   #   a server which does not been setup to allow
793   #   connections from the host the attempted connection
794   #   is tried.
795   #
796   ERR_NOPERMFORHOST=463
797
798   # ":Password incorrect"
799   #
800   # - Returned to indicate a failed attempt at registering
801   #   a connection for which a password was required and
802   #   was either not given or incorrect.
803   #
804   ERR_PASSWDMISMATCH=464
805
806   # ":You are banned from this server"
807   #
808   # - Returned after an attempt to connect and register
809   #   yourself with a server which has been setup to
810   #   explicitly deny connections to you.
811   #
812   ERR_YOUREBANNEDCREEP=465
813
814   # - Sent by a server to a user to inform that access to the
815   #   server will soon be denied.
816   #
817   ERR_YOUWILLBEBANNED=466
818
819   # "<channel> :Channel key already set"
820   ERR_KEYSET=467
821
822   # "<channel> :Cannot join channel (+l)"
823   ERR_CHANNELISFULL=471
824
825   # "<char> :is unknown mode char to me for <channel>"
826   ERR_UNKNOWNMODE=472
827
828   # "<channel> :Cannot join channel (+i)"
829   ERR_INVITEONLYCHAN=473
830
831   # "<channel> :Cannot join channel (+b)"
832   ERR_BANNEDFROMCHAN=474
833
834   # "<channel> :Cannot join channel (+k)"
835   ERR_BADCHANNELKEY=475
836
837   # "<channel> :Bad Channel Mask"
838   ERR_BADCHANMASK=476
839
840   # "<channel> :Channel doesn't support modes"
841   ERR_NOCHANMODES=477
842
843   # "<channel> <char> :Channel list is full"
844   #
845   ERR_BANLISTFULL=478
846
847   # ":Permission Denied- You're not an IRC operator"
848   #
849   # - Any command requiring operator privileges to operate
850   #   MUST return this error to indicate the attempt was
851   #   unsuccessful.
852   #
853   ERR_NOPRIVILEGES=481
854
855   # "<channel> :You're not channel operator"
856   #
857   # - Any command requiring 'chanop' privileges (such as
858   #   MODE messages) MUST return this error if the client
859   #   making the attempt is not a chanop on the specified
860   #   channel.
861   #
862   #
863   ERR_CHANOPRIVSNEEDED=482
864
865   # ":You can't kill a server!"
866   #
867   # - Any attempts to use the KILL command on a server
868   #   are to be refused and this error returned directly
869   #   to the client.
870   #
871   ERR_CANTKILLSERVER=483
872
873   # ":Your connection is restricted!"
874   #
875   # - Sent by the server to a user upon connection to indicate
876   #   the restricted nature of the connection (user mode "+r").
877   #
878   ERR_RESTRICTED=484
879
880   # ":You're not the original channel operator"
881   #
882   # - Any MODE requiring "channel creator" privileges MUST
883   #   return this error if the client making the attempt is not
884   #   a chanop on the specified channel.
885   #
886   ERR_UNIQOPPRIVSNEEDED=485
887
888   # ":No O-lines for your host"
889   #
890   # - If a client sends an OPER message and the server has
891   #   not been configured to allow connections from the
892   #   client's host as an operator, this error MUST be
893   #   returned.
894   #
895   ERR_NOOPERHOST=491
896
897   # ":Unknown MODE flag"
898   #
899   # - Returned by the server to indicate that a MODE
900   #   message was sent with a nickname parameter and that
901   #   the a mode flag sent was not recognized.
902   #
903   ERR_UMODEUNKNOWNFLAG=501
904
905   # ":Cannot change mode for other users"
906   #
907   # - Error sent to any user trying to view or change the
908   #   user mode for a user other than themselves.
909   #
910   ERR_USERSDONTMATCH=502
911
912   # 5.3 Reserved numerics
913   #
914   # These numerics are not described above since they fall into one of
915   # the following categories:
916   #
917   # 1. no longer in use;
918   #
919   # 2. reserved for future planned use;
920   #
921   # 3. in current use but are part of a non-generic 'feature' of
922   #    the current IRC server.
923   #
924   RPL_SERVICEINFO=231
925   RPL_ENDOFSERVICES=232
926   RPL_SERVICE=233
927   RPL_NONE=300
928   RPL_WHOISCHANOP=316
929   RPL_KILLDONE=361
930   RPL_CLOSING=362
931   RPL_CLOSEEND=363
932   RPL_INFOSTART=373
933   RPL_MYPORTIS=384
934   RPL_STATSCLINE=213
935   RPL_STATSNLINE=214
936   RPL_STATSILINE=215
937   RPL_STATSKLINE=216
938   RPL_STATSQLINE=217
939   RPL_STATSYLINE=218
940   RPL_STATSVLINE=240
941   RPL_STATSLLINE=241
942   RPL_STATSHLINE=244
943   RPL_STATSSLINE=244
944   RPL_STATSPING=246
945   RPL_STATSBLINE=247
946   ERR_NOSERVICEHOST=492
947   RPL_DATASTR=290
948
949   # Implements RFC 2812 and prior IRC RFCs.
950   #
951   # Clients should register Proc{}s to handle the various server events, and
952   # the Client class will handle dispatch.
953   class Client
954
955     # the Server we're connected to
956     attr_reader :server
957     # the User representing us on that server
958     attr_reader :user
959
960     # Create a new Client instance
961     def initialize
962       @server = Server.new         # The Server
963       @user = @server.user("*!*@*")     # The User representing the client on this Server
964
965       @handlers = Hash.new
966
967       # This is used by some messages to build lists of users that
968       # will be delegated when the ENDOF... message is received
969       @tmpusers = []
970     end
971
972     # Clear the server and reset the user
973     def reset
974       @server.clear
975       @user = @server.user("*!*@*")
976     end
977
978     # key::   server event to handle
979     # value:: proc object called when event occurs
980     # set a handler for a server event
981     #
982     # ==server events currently supported:
983     #
984     # TODO handle errors ERR_NOSUCHNICK, ERR_NOSUCHCHANNEL
985     # TODO handle errors ERR_CHANOPRIVSNEEDED, ERR_CANNOTSENDTOCHAN
986     #
987     # welcome::     server welcome message on connect
988     # yourhost::    your host details (on connection)
989     # created::     when the server was started
990     # isupport::    information about what this server supports
991     # ping::        server pings you (default handler returns a pong)
992     # nicktaken::   you tried to change nick to one that's in use
993     # badnick::     you tried to change nick to one that's invalid
994     # topic::       someone changed the topic of a channel
995     # topicinfo::   on joining a channel or asking for the topic, tells you
996     #               who set it and when
997     # names::       server sends list of channel members when you join
998     # motd::        server message of the day
999     # privmsg::     privmsg, the core of IRC, a message to you from someone
1000     # public::      optionally instead of getting privmsg you can hook to only
1001     #               the public ones...
1002     # msg::         or only the private ones, or both
1003     # kick::        someone got kicked from a channel
1004     # part::        someone left a channel
1005     # quit::        someone quit IRC
1006     # join::        someone joined a channel
1007     # changetopic:: the topic of a channel changed
1008     # invite::      you are invited to a channel
1009     # nick::        someone changed their nick
1010     # mode::        a mode change
1011     # notice::      someone sends you a notice
1012     # unknown::     any other message not handled by the above
1013     def []=(key, value)
1014       @handlers[key] = value
1015     end
1016
1017     # key:: event name
1018     # remove a handler for a server event
1019     def deletehandler(key)
1020       @handlers.delete(key)
1021     end
1022
1023     # takes a server string, checks for PING, PRIVMSG, NOTIFY, etc, and parses
1024     # numeric server replies, calling the appropriate handler for each, and
1025     # sending it a hash containing the data from the server
1026     def process(serverstring)
1027       data = Hash.new
1028       data[:serverstring] = serverstring
1029
1030       unless serverstring.chomp =~ /^(:(\S+)\s)?(\S+)(\s(.*))?$/
1031         raise "Unparseable Server Message!!!: #{serverstring.inspect}"
1032       end
1033
1034       prefix, command, params = $2, $3, $5
1035
1036       if prefix != nil
1037         # Most servers will send a full nick!user@host prefix for
1038         # messages from users. Therefore, when the prefix doesn't match this
1039         # syntax it's usually the server hostname.
1040         #
1041         # This is not always true, though, since some servers do not send a
1042         # full hostmask for user messages.
1043         #
1044         if prefix =~ /^#{Regexp::Irc::BANG_AT}$/
1045           data[:source] = @server.user(prefix)
1046         else
1047           if @server.hostname
1048             if @server.hostname != prefix
1049               # TODO do we want to be able to differentiate messages that are passed on to us from /other/ servers?
1050               debug "Origin #{prefix} for message\n\t#{serverstring.inspect}\nis neither a user hostmask nor the server hostname\nI'll pretend that it's from the server anyway"
1051               data[:source] = @server
1052             else
1053               data[:source] = @server
1054             end
1055           else
1056             @server.instance_variable_set(:@hostname, prefix)
1057             data[:source] = @server
1058           end
1059         end
1060       end
1061
1062       # split parameters in an array
1063       argv = []
1064       params.scan(/(?!:)(\S+)|:(.*)/) { argv << ($1 || $2) } if params
1065
1066       if command =~ /^(\d+)$/ # Numeric replies
1067         data[:target] = argv[0]
1068         # A numeric reply /should/ be directed at the client, except when we're connecting with a used nick, in which case
1069         # it's directed at '*'
1070         not_us = !([@user.nick, '*'].include?(data[:target]))
1071         if not_us
1072           warning "Server reply #{serverstring.inspect} directed at #{data[:target]} instead of client (#{@user.nick})"
1073         end
1074
1075         num=command.to_i
1076         case num
1077         when RPL_WELCOME
1078           data[:message] = argv[1]
1079           # "Welcome to the Internet Relay Network
1080           # <nick>!<user>@<host>"
1081           if not_us
1082             warning "Server thinks client (#{@user.inspect}) has a different nick"
1083             @user.nick = data[:target]
1084           end
1085           if data[:message] =~ /([^@!\s]+)(?:!([^@!\s]+?))?@(\S+)/
1086             nick = $1
1087             user = $2
1088             host = $3
1089             warning "Welcome message nick mismatch (#{nick} vs #{data[:target]})" if nick != data[:target]
1090             @user.user = user if user
1091             @user.host = host if host
1092           end
1093           handle(:welcome, data)
1094         when RPL_YOURHOST
1095           # "Your host is <servername>, running version <ver>"
1096           data[:message] = argv[1]
1097           handle(:yourhost, data)
1098         when RPL_CREATED
1099           # "This server was created <date>"
1100           data[:message] = argv[1]
1101           handle(:created, data)
1102         when RPL_MYINFO
1103           # "<servername> <version> <available user modes>
1104           # <available channel modes>"
1105           @server.parse_my_info(params.split(' ', 2).last)
1106           data[:servername] = @server.hostname
1107           data[:version] = @server.version
1108           data[:usermodes] = @server.usermodes
1109           data[:chanmodes] = @server.chanmodes
1110           handle(:myinfo, data)
1111         when RPL_ISUPPORT
1112           # "PREFIX=(ov)@+ CHANTYPES=#& :are supported by this server"
1113           # "MODES=4 CHANLIMIT=#:20 NICKLEN=16 USERLEN=10 HOSTLEN=63
1114           # TOPICLEN=450 KICKLEN=450 CHANNELLEN=30 KEYLEN=23 CHANTYPES=#
1115           # PREFIX=(ov)@+ CASEMAPPING=ascii CAPAB IRCD=dancer :are available
1116           # on this server"
1117           #
1118           @server.parse_isupport(argv[1..-2].join(' '))
1119           handle(:isupport, data)
1120         when ERR_NICKNAMEINUSE
1121           # "* <nick> :Nickname is already in use"
1122           data[:nick] = argv[1]
1123           data[:message] = argv[2]
1124           handle(:nicktaken, data)
1125         when ERR_ERRONEUSNICKNAME
1126           # "* <nick> :Erroneous nickname"
1127           data[:nick] = argv[1]
1128           data[:message] = argv[2]
1129           handle(:badnick, data)
1130         when RPL_TOPIC
1131           data[:channel] = @server.channel(argv[1])
1132           data[:topic] = argv[2]
1133           data[:channel].topic.text = data[:topic]
1134
1135           handle(:topic, data)
1136         when RPL_TOPIC_INFO
1137           data[:nick] = @server.user(argv[0])
1138           data[:channel] = @server.channel(argv[1])
1139
1140           # This must not be an IRC::User because it might not be an actual User,
1141           # and we risk overwriting valid User data
1142           data[:source] = argv[2].to_irc_netmask(:server => @server)
1143
1144           data[:time] = Time.at(argv[3].to_i)
1145
1146           data[:channel].topic.set_by = data[:source]
1147           data[:channel].topic.set_on = data[:time]
1148
1149           handle(:topicinfo, data)
1150         when RPL_NAMREPLY
1151           # "( "=" / "*" / "@" ) <channel>
1152           # :[ "@" / "+" ] <nick> *( " " [ "@" / "+" ] <nick> )
1153           # - "@" is used for secret channels, "*" for private
1154           # channels, and "=" for others (public channels).
1155           data[:channeltype] = argv[1]
1156           data[:channel] = argv[2]
1157
1158           chan = @server.get_channel(data[:channel])
1159           unless chan
1160             warning "Received names #{data[:topic].inspect} for channel #{data[:channel].inspect} I was not on"
1161             return
1162           end
1163
1164           users = []
1165           argv[3].scan(/\S+/).each { |u|
1166             # FIXME beware of servers that allow multiple prefixes
1167             if(u =~ /^([#{@server.supports[:prefix][:prefixes].join}])?(.*)$/)
1168               umode = $1
1169               user = $2
1170               users << [user, umode]
1171             end
1172           }
1173
1174           users.each { |ar|
1175             u = @server.user(ar[0])
1176             chan.add_user(u, :silent => true)
1177             debug "Adding user #{u}"
1178             if ar[1]
1179               ms = @server.mode_for_prefix(ar[1].to_sym)
1180               debug "\twith mode #{ar[1]} (#{ms})"
1181               chan.mode[ms].set(u)
1182             end
1183           }
1184           @tmpusers += users
1185         when RPL_ENDOFNAMES
1186           data[:channel] = argv[1]
1187           data[:users] = @tmpusers
1188           handle(:names, data)
1189           @tmpusers = Array.new
1190         when RPL_LUSERCLIENT
1191           # ":There are <integer> users and <integer>
1192           # services on <integer> servers"
1193           data[:message] = argv[1]
1194           handle(:luserclient, data)
1195         when RPL_LUSEROP
1196           # "<integer> :operator(s) online"
1197           data[:ops] = argv[1].to_i
1198           handle(:luserop, data)
1199         when RPL_LUSERUNKNOWN
1200           # "<integer> :unknown connection(s)"
1201           data[:unknown] = argv[1].to_i
1202           handle(:luserunknown, data)
1203         when RPL_LUSERCHANNELS
1204           # "<integer> :channels formed"
1205           data[:channels] = argv[1].to_i
1206           handle(:luserchannels, data)
1207         when RPL_LUSERME
1208           # ":I have <integer> clients and <integer> servers"
1209           data[:message] = argv[1]
1210           handle(:luserme, data)
1211         when ERR_NOMOTD
1212           # ":MOTD File is missing"
1213           data[:message] = argv[1]
1214           handle(:motd_missing, data)
1215         when RPL_LOCALUSERS
1216           # ":Current local  users: 3  Max: 4"
1217           data[:message] = argv[1]
1218           handle(:localusers, data)
1219         when RPL_GLOBALUSERS
1220           # ":Current global users: 3  Max: 4"
1221           data[:message] = argv[1]
1222           handle(:globalusers, data)
1223         when RPL_STATSCONN
1224           # ":Highest connection count: 4 (4 clients) (251 since server was
1225           # (re)started)"
1226           data[:message] = argv[1]
1227           handle(:statsconn, data)
1228         when RPL_MOTDSTART
1229           # "<nick> :- <server> Message of the Day -"
1230           if argv[1] =~ /^-\s+(\S+)\s/
1231             server = $1
1232           else
1233             warning "Server doesn't have an RFC compliant MOTD start."
1234           end
1235           @motd = ""
1236         when RPL_MOTD
1237           if(argv[1] =~ /^-\s+(.*)$/)
1238             @motd << $1
1239             @motd << "\n"
1240           end
1241         when RPL_ENDOFMOTD
1242           data[:motd] = @motd
1243           handle(:motd, data)
1244         when RPL_DATASTR
1245           data[:text] = argv[1]
1246           handle(:datastr, data)
1247         when RPL_AWAY
1248           data[:nick] = argv[1]
1249           data[:message] = argv[-1]
1250           user = @server.get_user(data[:nick])
1251           user.away = data[:message]
1252           handle(:away, data)
1253         when RPL_WHOREPLY
1254           data[:channel] = argv[1]
1255           data[:user] = argv[2]
1256           data[:host] = argv[3]
1257           data[:userserver] = argv[4]
1258           data[:nick] = argv[5]
1259           if argv[6] =~ /^(H|G)(\*)?(.*)?$/
1260             data[:away] = ($1 == 'G')
1261             data[:ircop] = $2
1262             data[:modes] = $3.scan(/./).map { |mode|
1263               m = @server.supports[:prefix][:prefixes].index(mode.to_sym)
1264               @server.supports[:prefix][:modes][m]
1265             } rescue []
1266           else
1267             warning "Strange WHO reply: #{serverstring.inspect}"
1268           end
1269           data[:hopcount], data[:real_name] = argv[7].split(" ", 2)
1270
1271           user = @server.get_user(data[:nick])
1272
1273           user.user = data[:user]
1274           user.host = data[:host]
1275           user.away = data[:away] # FIXME doesn't provide the actual message
1276           # TODO ircop status
1277           # TODO userserver
1278           # TODO hopcount
1279           user.real_name = data[:real_name]
1280
1281           channel = @server.get_channel(data[:channel])
1282
1283           channel.add_user(user, :silent=>true)
1284           data[:modes].map { |mode|
1285             channel.mode[mode].set(user)
1286           }
1287
1288           handle(:who, data)
1289         when RPL_ENDOFWHO
1290           handle(:eowho, data)
1291         when RPL_CHANNELMODEIS
1292           parse_mode(serverstring, argv[1..-1], data)
1293           handle(:mode, data)
1294         when RPL_CREATIONTIME
1295           data[:channel] = argv[1]
1296           data[:time] = Time.at(argv[2].to_i)
1297           @server.get_channel(data[:channel]).creation_time=data[:time]
1298           handle(:creationtime, data)
1299         when RPL_CHANNEL_URL
1300           data[:channel] = argv[1]
1301           data[:url] = argv[2]
1302           @server.get_channel(data[:channel]).url=data[:url].dup
1303           handle(:channel_url, data)
1304         else
1305           warning "Unknown message #{serverstring.inspect}"
1306           handle(:unknown, data)
1307         end
1308         return # We've processed the numeric reply
1309       end
1310
1311       # Otherwise, the command should be a single word
1312       case command.to_sym
1313       when :PING
1314         data[:pingid] = argv[0]
1315         handle(:ping, data)
1316       when :PONG
1317         data[:pingid] = argv[0]
1318         handle(:pong, data)
1319       when :PRIVMSG
1320         # you can either bind to 'PRIVMSG', to get every one and
1321         # parse it yourself, or you can bind to 'MSG', 'PUBLIC',
1322         # etc and get it all nicely split up for you.
1323
1324         begin
1325           data[:target] = @server.user_or_channel(argv[0])
1326         rescue
1327           # The previous may fail e.g. when the target is a server or something
1328           # like that (e.g. $<mask>). In any of these cases, we just use the
1329           # String as a target
1330           # FIXME we probably want to explicitly check for the #<mask> $<mask>
1331           data[:target] = argv[0]
1332         end
1333         data[:message] = argv[1]
1334         handle(:privmsg, data)
1335
1336         # Now we split it
1337         if data[:target].kind_of?(Channel)
1338           handle(:public, data)
1339         else
1340           handle(:msg, data)
1341         end
1342       when :NOTICE
1343         begin
1344           data[:target] = @server.user_or_channel(argv[0])
1345         rescue
1346           # The previous may fail e.g. when the target is a server or something
1347           # like that (e.g. $<mask>). In any of these cases, we just use the
1348           # String as a target
1349           # FIXME we probably want to explicitly check for the #<mask> $<mask>
1350           data[:target] = argv[0]
1351         end
1352         data[:message] = argv[1]
1353         case data[:source]
1354         when User
1355           handle(:notice, data)
1356         else
1357           # "server notice" (not from user, noone to reply to)
1358           handle(:snotice, data)
1359         end
1360       when :KICK
1361         data[:channel] = @server.channel(argv[0])
1362         data[:target] = @server.user(argv[1])
1363         data[:message] = argv[2]
1364
1365         @server.delete_user_from_channel(data[:target], data[:channel])
1366         if data[:target] == @user
1367           @server.delete_channel(data[:channel])
1368         end
1369
1370         handle(:kick, data)
1371       when :PART
1372         data[:channel] = @server.channel(argv[0])
1373         data[:message] = argv[1]
1374
1375         @server.delete_user_from_channel(data[:source], data[:channel])
1376         if data[:source] == @user
1377           @server.delete_channel(data[:channel])
1378         end
1379
1380         handle(:part, data)
1381       when :QUIT
1382         data[:message] = argv[0]
1383         data[:was_on] = @server.channels.inject(ChannelList.new) { |list, ch|
1384           list << ch if ch.has_user?(data[:source])
1385           list
1386         }
1387
1388         @server.delete_user(data[:source])
1389
1390         handle(:quit, data)
1391       when :JOIN
1392         data[:channel] = @server.channel(argv[0])
1393         data[:channel].add_user(data[:source])
1394
1395         handle(:join, data)
1396       when :TOPIC
1397         data[:channel] = @server.channel(argv[0])
1398         data[:topic] = Channel::Topic.new(argv[1], data[:source], Time.new)
1399         data[:channel].topic.replace(data[:topic])
1400
1401         handle(:changetopic, data)
1402       when :INVITE
1403         data[:target] = @server.user(argv[0])
1404         data[:channel] = @server.channel(argv[1])
1405
1406         handle(:invite, data)
1407       when :NICK
1408         data[:is_on] = @server.channels.inject(ChannelList.new) { |list, ch|
1409           list << ch if ch.has_user?(data[:source])
1410           list
1411         }
1412
1413         data[:newnick] = argv[0]
1414         data[:oldnick] = data[:source].nick.dup
1415         data[:source].nick = data[:newnick]
1416
1417         debug "#{data[:oldnick]} (now #{data[:newnick]}) was on #{data[:is_on].join(', ')}"
1418
1419         handle(:nick, data)
1420       when :MODE
1421         parse_mode(serverstring, argv, data)
1422         handle(:mode, data)
1423       else
1424         warning "Unknown message #{serverstring.inspect}"
1425         handle(:unknown, data)
1426       end
1427     end
1428
1429     private
1430
1431     # key::  server event name
1432     # data:: hash containing data about the event, passed to the proc
1433     # call client's proc for an event, if they set one as a handler
1434     def handle(key, data)
1435       if(@handlers.has_key?(key))
1436         @handlers[key].call(data)
1437       end
1438     end
1439
1440     # RPL_CHANNELMODEIS
1441     # MODE ([+-]<modes> (<params>)*)*
1442     # When a MODE message is received by a server,
1443     # Type C will have parameters too, so we must
1444     # be able to consume parameters for all
1445     # but Type D modes
1446     def parse_mode(serverstring, argv, data)
1447       data[:target] = @server.user_or_channel(argv[0])
1448       data[:modestring] = argv[1..-1].join(" ")
1449       # data[:modes] is an array where each element
1450       # is an array with two elements, the first of which
1451       # is either :set or :reset, and the second symbol
1452       # is the mode letter. An optional third element
1453       # is present e.g. for channel modes that need
1454       # a parameter
1455       data[:modes] = []
1456       case data[:target]
1457       when User
1458         # User modes aren't currently handled internally,
1459         # but we still parse them and delegate to the client
1460         warning "Unhandled user mode message '#{serverstring}'"
1461         argv[1..-1].each { |arg|
1462           setting = arg[0].chr
1463           if "+-".include?(setting)
1464             setting = setting == "+" ? :set : :reset
1465             arg[1..-1].each_byte { |b|
1466               m = b.chr.intern
1467               data[:modes] << [setting, m]
1468             }
1469           else
1470             # Although typically User modes don't take an argument,
1471             # this is not true for all modes on all servers. Since
1472             # we have no knowledge of which modes take parameters
1473             # and which don't we just assign it to the last
1474             # mode. This is not going to do strange things often,
1475             # as usually User modes are only set one at a time
1476             warning "Unhandled user mode parameter #{arg} found"
1477             data[:modes].last << arg
1478           end
1479         }
1480       else
1481         # array of indices in data[:modes] where parameters
1482         # are needed
1483         who_wants_params = []
1484
1485         modes = argv[1..-1].dup
1486         debug modes
1487         getting_args = false
1488         while arg = modes.shift
1489           debug arg
1490           if getting_args
1491             # getting args for previously set modes
1492             idx = who_wants_params.shift
1493             if idx.nil?
1494               warning "Oops, problems parsing #{serverstring.inspect}"
1495               break
1496             end
1497             data[:modes][idx] << arg
1498             getting_args = false if who_wants_params.empty?
1499           else
1500             debug @server.supports[:chanmodes]
1501             setting = :set
1502             arg.each_byte do |c|
1503               m = c.chr.intern
1504               case m
1505               when :+
1506                 setting = :set
1507               when :-
1508                 setting = :reset
1509               else
1510                 data[:modes] << [setting, m]
1511                 case m
1512                 when *@server.supports[:chanmodes][:typea]
1513                   who_wants_params << data[:modes].length - 1
1514                 when *@server.supports[:chanmodes][:typeb]
1515                   who_wants_params << data[:modes].length - 1
1516                 when *@server.supports[:chanmodes][:typec]
1517                   if setting == :set
1518                     who_wants_params << data[:modes].length - 1
1519                   end
1520                 when *@server.supports[:chanmodes][:typed]
1521                   # Nothing to do
1522                 when *@server.supports[:prefix][:modes]
1523                   who_wants_params << data[:modes].length - 1
1524                 else
1525                   warning "Ignoring unknown mode #{m} in #{serverstring.inspect}"
1526                   data[:modes].pop
1527                 end
1528               end
1529             end
1530             getting_args = true unless who_wants_params.empty?
1531           end
1532         end
1533
1534         data[:modes].each { |mode|
1535           set, key, val = mode
1536           if val
1537             data[:target].mode[key].send(set, val)
1538           else
1539             data[:target].mode[key].send(set)
1540           end
1541         }
1542       end
1543     end
1544   end
1545 end