]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-info.c
Introduce "CHANTYPES" #define
[ngircd-alex.git] / src / ngircd / irc-info.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2013 Alexander Barton (alex@barton.de) and Contributors.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * Please read the file COPYING, README and AUTHORS for more information.
10  */
11
12 #include "portab.h"
13
14 /**
15  * @file
16  * IRC info commands
17  */
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <strings.h>
26
27 #include "ngircd.h"
28 #include "conn-func.h"
29 #include "conn-zip.h"
30 #include "channel.h"
31 #include "class.h"
32 #include "conf.h"
33 #include "defines.h"
34 #include "lists.h"
35 #include "log.h"
36 #include "messages.h"
37 #include "match.h"
38 #include "tool.h"
39 #include "parse.h"
40 #include "irc.h"
41 #include "irc-macros.h"
42 #include "irc-write.h"
43 #include "client-cap.h"
44
45 #include "exp.h"
46 #include "irc-info.h"
47
48 /* Local functions */
49
50 static unsigned int
51 t_diff(time_t *t, const time_t d)
52 {
53         time_t diff, remain;
54
55         diff = *t / d;
56         remain = diff * d;
57         *t -= remain;
58
59         return (unsigned int)diff;
60 }
61
62 static unsigned int
63 uptime_days(time_t *now)
64 {
65         return t_diff(now, 60 * 60 * 24);
66 }
67
68 static unsigned int
69 uptime_hrs(time_t *now)
70 {
71         return t_diff(now, 60 * 60);
72 }
73
74 static unsigned int
75 uptime_mins(time_t *now)
76 {
77         return t_diff(now, 60);
78 }
79
80 static bool
81 write_whoreply(CLIENT *Client, CLIENT *c, const char *channelname, const char *flags)
82 {
83         return IRC_WriteStrClient(Client, RPL_WHOREPLY_MSG, Client_ID(Client),
84                                   channelname, Client_User(c),
85                                   Client_HostnameDisplayed(c),
86                                   Client_ID(Client_Introducer(c)), Client_ID(c),
87                                   flags, Client_Hops(c), Client_Info(c));
88 }
89
90 static const char *
91 who_flags_status(const char *client_modes)
92 {
93         if (strchr(client_modes, 'a'))
94                 return "G"; /* away */
95         return "H";
96 }
97
98 /**
99  * Return channel user mode prefix(es).
100  *
101  * @param Client The client requesting the mode prefixes.
102  * @param chan_user_modes String with channel user modes.
103  * @param str String buffer to which the prefix(es) will be appended.
104  * @param len Size of "str" buffer.
105  * @return Pointer to "str".
106  */
107 static char *
108 who_flags_qualifier(CLIENT *Client, const char *chan_user_modes,
109                     char *str, size_t len)
110 {
111         assert(Client != NULL);
112
113         if (Client_Cap(Client) & CLIENT_CAP_MULTI_PREFIX) {
114                 if (strchr(chan_user_modes, 'q'))
115                         strlcat(str, "~", len);
116                 if (strchr(chan_user_modes, 'a'))
117                         strlcat(str, "&", len);
118                 if (strchr(chan_user_modes, 'o'))
119                         strlcat(str, "@", len);
120                 if (strchr(chan_user_modes, 'h'))
121                         strlcat(str, "%", len);
122                 if (strchr(chan_user_modes, 'v'))
123                         strlcat(str, "+", len);
124
125                 return str;
126         }
127
128         if (strchr(chan_user_modes, 'q'))
129                 strlcat(str, "~", len);
130         else if (strchr(chan_user_modes, 'a'))
131                 strlcat(str, "&", len);
132         else if (strchr(chan_user_modes, 'o'))
133                 strlcat(str, "@", len);
134         else if (strchr(chan_user_modes, 'h'))
135                 strlcat(str, "%", len);
136         else if (strchr(chan_user_modes, 'v'))
137                 strlcat(str, "+", len);
138
139         return str;
140 }
141
142 /**
143  * Send WHO reply for a "channel target" ("WHO #channel").
144  *
145  * @param Client Client requesting the information.
146  * @param Chan Channel being requested.
147  * @param OnlyOps Only display IRC operators.
148  * @return CONNECTED or DISCONNECTED.
149  */
150 static bool
151 IRC_WHO_Channel(CLIENT *Client, CHANNEL *Chan, bool OnlyOps)
152 {
153         bool is_visible, is_member, is_ircop;
154         CL2CHAN *cl2chan;
155         const char *client_modes;
156         char flags[10];
157         CLIENT *c;
158         int count = 0;
159
160         assert( Client != NULL );
161         assert( Chan != NULL );
162
163         IRC_SetPenalty(Client, 1);
164
165         is_member = Channel_IsMemberOf(Chan, Client);
166
167         /* Secret channel? */
168         if (!is_member && strchr(Channel_Modes(Chan), 's'))
169                 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG,
170                                           Client_ID(Client), Channel_Name(Chan));
171
172         cl2chan = Channel_FirstMember(Chan);
173         for (; cl2chan ; cl2chan = Channel_NextMember(Chan, cl2chan)) {
174                 c = Channel_GetClient(cl2chan);
175
176                 client_modes = Client_Modes(c);
177                 is_ircop = strchr(client_modes, 'o') != NULL;
178                 if (OnlyOps && !is_ircop)
179                         continue;
180
181                 is_visible = strchr(client_modes, 'i') == NULL;
182                 if (is_member || is_visible) {
183                         strlcpy(flags, who_flags_status(client_modes),
184                                 sizeof(flags));
185                         if (is_ircop)
186                                 strlcat(flags, "*", sizeof(flags));
187
188                         who_flags_qualifier(Client, Channel_UserModes(Chan, c),
189                                             flags, sizeof(flags));
190
191                         if (!write_whoreply(Client, c, Channel_Name(Chan),
192                                             flags))
193                                 return DISCONNECTED;
194                         count++;
195                 }
196         }
197
198         /* If there are a lot of clients, augment penalty a bit */
199         if (count > MAX_RPL_WHO)
200                 IRC_SetPenalty(Client, 1);
201
202         return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client),
203                                   Channel_Name(Chan));
204 }
205
206 /**
207  * Send WHO reply for a "mask target" ("WHO m*sk").
208  *
209  * @param Client Client requesting the information.
210  * @param Mask Mask being requested or NULL for "all" clients.
211  * @param OnlyOps Only display IRC operators.
212  * @return CONNECTED or DISCONNECTED.
213  */
214 static bool
215 IRC_WHO_Mask(CLIENT *Client, char *Mask, bool OnlyOps)
216 {
217         CLIENT *c;
218         CL2CHAN *cl2chan;
219         CHANNEL *chan;
220         bool client_match, is_visible;
221         char flags[4];
222         int count = 0;
223
224         assert (Client != NULL);
225
226         if (Mask)
227                 ngt_LowerStr(Mask);
228
229         IRC_SetPenalty(Client, 3);
230         for (c = Client_First(); c != NULL; c = Client_Next(c)) {
231                 if (Client_Type(c) != CLIENT_USER)
232                         continue;
233
234                 if (OnlyOps && !Client_HasMode(c, 'o'))
235                         continue;
236
237                 if (Mask) {
238                         /* Match pattern against user host/server/name/nick */
239                         client_match = MatchCaseInsensitive(Mask,
240                                                             Client_Hostname(c));
241                         if (!client_match)
242                                 client_match = MatchCaseInsensitive(Mask,
243                                                                     Client_ID(Client_Introducer(c)));
244                         if (!client_match)
245                                 client_match = MatchCaseInsensitive(Mask,
246                                                                     Client_Info(c));
247                         if (!client_match)
248                                 client_match = MatchCaseInsensitive(Mask,
249                                                                     Client_ID(c));
250                         if (!client_match)
251                                 continue;       /* no match: skip this client */
252                 }
253
254                 is_visible = !Client_HasMode(c, 'i');
255
256                 /* Target client is invisible, but mask matches exactly? */
257                 if (!is_visible && Mask && strcasecmp(Client_ID(c), Mask) == 0)
258                         is_visible = true;
259
260                 /* Target still invisible, but are both on the same channel? */
261                 if (!is_visible) {
262                         cl2chan = Channel_FirstChannelOf(Client);
263                         while (cl2chan && !is_visible) {
264                                 chan = Channel_GetChannel(cl2chan);
265                                 if (Channel_IsMemberOf(chan, c))
266                                         is_visible = true;
267                                 cl2chan = Channel_NextChannelOf(Client, cl2chan);
268                         }
269                 }
270
271                 if (!is_visible)        /* target user is not visible */
272                         continue;
273
274                 if (IRC_CheckListTooBig(Client, count, MAX_RPL_WHO, "WHO"))
275                         break;
276
277                 strlcpy(flags, who_flags_status(Client_Modes(c)), sizeof(flags));
278                 if (strchr(Client_Modes(c), 'o'))
279                         strlcat(flags, "*", sizeof(flags));
280
281                 if (!write_whoreply(Client, c, "*", flags))
282                         return DISCONNECTED;
283                 count++;
284         }
285
286         return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client),
287                                   Mask ? Mask : "*");
288 }
289
290 /**
291  * Generate WHOIS reply of one actual client.
292  *
293  * @param Client The client from which this command has been received.
294  * @param from The client requesting the information ("originator").
295  * @param c The client of which information should be returned.
296  * @return CONNECTED or DISCONNECTED.
297  */
298 static bool
299 IRC_WHOIS_SendReply(CLIENT *Client, CLIENT *from, CLIENT *c)
300 {
301         char str[COMMAND_LEN];
302         CL2CHAN *cl2chan;
303         CHANNEL *chan;
304
305         assert(Client != NULL);
306         assert(from != NULL);
307         assert(c != NULL);
308
309         /* Nick, user, hostname and client info */
310         if (!IRC_WriteStrClient(from, RPL_WHOISUSER_MSG, Client_ID(from),
311                                 Client_ID(c), Client_User(c),
312                                 Client_HostnameDisplayed(c), Client_Info(c)))
313                 return DISCONNECTED;
314
315         /* Server */
316         if (!IRC_WriteStrClient(from, RPL_WHOISSERVER_MSG, Client_ID(from),
317                                 Client_ID(c), Client_ID(Client_Introducer(c)),
318                                 Client_Info(Client_Introducer(c))))
319                 return DISCONNECTED;
320
321         /* Channels */
322         snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
323                  Client_ID(from), Client_ID(c));
324         cl2chan = Channel_FirstChannelOf(c);
325         while (cl2chan) {
326                 chan = Channel_GetChannel(cl2chan);
327                 assert(chan != NULL);
328
329                 /* next */
330                 cl2chan = Channel_NextChannelOf(c, cl2chan);
331
332                 /* Secret channel? */
333                 if (strchr(Channel_Modes(chan), 's')
334                     && !Channel_IsMemberOf(chan, Client))
335                         continue;
336
337                 /* Local channel and request is not from a user? */
338                 if (Client_Type(Client) == CLIENT_SERVER
339                     && Channel_IsLocal(chan))
340                         continue;
341
342                 /* Concatenate channel names */
343                 if (str[strlen(str) - 1] != ':')
344                         strlcat(str, " ", sizeof(str));
345
346                 who_flags_qualifier(Client, Channel_UserModes(chan, c),
347                                     str, sizeof(str));
348                 strlcat(str, Channel_Name(chan), sizeof(str));
349
350                 if (strlen(str) > (COMMAND_LEN - CHANNEL_NAME_LEN - 4)) {
351                         /* Line becomes too long: send it! */
352                         if (!IRC_WriteStrClient(Client, "%s", str))
353                                 return DISCONNECTED;
354                         snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
355                                  Client_ID(from), Client_ID(c));
356                 }
357         }
358         if(str[strlen(str) - 1] != ':') {
359                 /* There is data left to send: */
360                 if (!IRC_WriteStrClient(Client, "%s", str))
361                         return DISCONNECTED;
362         }
363
364         /* IRC-Services? */
365         if (Client_Type(c) == CLIENT_SERVICE &&
366             !IRC_WriteStrClient(from, RPL_WHOISSERVICE_MSG,
367                                 Client_ID(from), Client_ID(c)))
368                 return DISCONNECTED;
369
370         /* IRC-Operator? */
371         if (Client_Type(c) != CLIENT_SERVICE &&
372             Client_HasMode(c, 'o') &&
373             !IRC_WriteStrClient(from, RPL_WHOISOPERATOR_MSG,
374                                 Client_ID(from), Client_ID(c)))
375                 return DISCONNECTED;
376
377         /* IRC-Bot? */
378         if (Client_HasMode(c, 'B') &&
379             !IRC_WriteStrClient(from, RPL_WHOISBOT_MSG,
380                                 Client_ID(from), Client_ID(c)))
381                 return DISCONNECTED;
382
383         /* Connected using SSL? */
384         if (Conn_UsesSSL(Client_Conn(c))) {
385                 if (!IRC_WriteStrClient(from, RPL_WHOISSSL_MSG, Client_ID(from),
386                                         Client_ID(c)))
387                         return DISCONNECTED;
388
389                 /* Certificate fingerprint? */
390                 if (Conn_GetFingerprint(Client_Conn(c)) &&
391                     from == c &&
392                     !IRC_WriteStrClient(from, RPL_WHOISCERTFP_MSG,
393                                         Client_ID(from), Client_ID(c),
394                                         Conn_GetFingerprint(Client_Conn(c))))
395                         return DISCONNECTED;
396         }
397
398         /* Registered nickname? */
399         if (Client_HasMode(c, 'R') &&
400             !IRC_WriteStrClient(from, RPL_WHOISREGNICK_MSG,
401                                 Client_ID(from), Client_ID(c)))
402                 return DISCONNECTED;
403
404         /* Local client and requester is the user itself or an IRC Op? */
405         if (Client_Conn(c) > NONE &&
406             (from == c || (!Conf_MorePrivacy && Client_HasMode(from, 'o')))) {
407                 /* Client hostname */
408                 if (!IRC_WriteStrClient(from, RPL_WHOISHOST_MSG,
409                                         Client_ID(from), Client_ID(c), Client_Hostname(c),
410                                         Conn_GetIPAInfo(Client_Conn(c))))
411                         return DISCONNECTED;
412                 /* Client modes */
413                 if (!IRC_WriteStrClient(from, RPL_WHOISMODES_MSG,
414                                         Client_ID(from), Client_ID(c), Client_Modes(c)))
415                         return DISCONNECTED;
416         }
417
418         /* Idle and signon time (local clients only!) */
419         if (!Conf_MorePrivacy && Client_Conn(c) > NONE &&
420             !IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
421                                 Client_ID(from), Client_ID(c),
422                                 (unsigned long)Conn_GetIdle(Client_Conn(c)),
423                                 (unsigned long)Conn_GetSignon(Client_Conn(c))))
424                 return DISCONNECTED;
425
426         /* Away? */
427         if (Client_HasMode(c, 'a') &&
428             !IRC_WriteStrClient(from, RPL_AWAY_MSG,
429                                 Client_ID(from), Client_ID(c), Client_Away(c)))
430                 return DISCONNECTED;
431
432         return CONNECTED;
433 }
434
435 static bool
436 WHOWAS_EntryWrite(CLIENT *prefix, WHOWAS *entry)
437 {
438         char t_str[60];
439
440         (void)strftime(t_str, sizeof(t_str), "%a %b %d %H:%M:%S %Y",
441                        localtime(&entry->time));
442
443         if (!IRC_WriteStrClient(prefix, RPL_WHOWASUSER_MSG, Client_ID(prefix),
444                                 entry->id, entry->user, entry->host, entry->info))
445                 return DISCONNECTED;
446
447         return IRC_WriteStrClient(prefix, RPL_WHOISSERVER_MSG, Client_ID(prefix),
448                                   entry->id, entry->server, t_str);
449 }
450
451 #ifdef SSL_SUPPORT
452 static bool
453 Show_MOTD_SSLInfo(CLIENT *Client)
454 {
455         char buf[COMMAND_LEN];
456         char c_str[128];
457
458         if (Conn_GetCipherInfo(Client_Conn(Client), c_str, sizeof(c_str))) {
459                 snprintf(buf, sizeof(buf), "Connected using Cipher %s", c_str);
460                 if (!IRC_WriteStrClient(Client, RPL_MOTD_MSG,
461                                         Client_ID(Client), buf))
462                         return false;
463         }
464
465         if (Conn_GetFingerprint(Client_Conn(Client))) {
466                 snprintf(buf, sizeof(buf),
467                          "Your client certificate fingerprint is: %s",
468                          Conn_GetFingerprint(Client_Conn(Client)));
469                 if (!IRC_WriteStrClient(Client, RPL_MOTD_MSG,
470                                         Client_ID(Client), buf))
471                         return false;
472         }
473
474         return true;
475 }
476 #else
477 static bool
478 Show_MOTD_SSLInfo(UNUSED CLIENT *c)
479 {
480         return true;
481 }
482 #endif
483
484 /* Global functions */
485
486 /**
487  * Handler for the IRC command "ADMIN".
488  *
489  * @param Client The client from which this command has been received.
490  * @param Req Request structure with prefix and all parameters.
491  * @return CONNECTED or DISCONNECTED.
492  */
493 GLOBAL bool
494 IRC_ADMIN(CLIENT *Client, REQUEST *Req )
495 {
496         CLIENT *target, *prefix;
497
498         assert( Client != NULL );
499         assert( Req != NULL );
500
501         IRC_SetPenalty(Client, 1);
502
503         _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
504         _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
505         _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
506
507         /* Forward? */
508         if(target != Client_ThisServer()) {
509                 IRC_WriteStrClientPrefix(target, prefix,
510                                          "ADMIN %s", Client_ID(target));
511                 return CONNECTED;
512         }
513
514         if (!IRC_WriteStrClient(Client, RPL_ADMINME_MSG, Client_ID(prefix),
515                                 Conf_ServerName))
516                 return DISCONNECTED;
517         if (!IRC_WriteStrClient(Client, RPL_ADMINLOC1_MSG, Client_ID(prefix),
518                                 Conf_ServerAdmin1))
519                 return DISCONNECTED;
520         if (!IRC_WriteStrClient(Client, RPL_ADMINLOC2_MSG, Client_ID(prefix),
521                                 Conf_ServerAdmin2))
522                 return DISCONNECTED;
523         if (!IRC_WriteStrClient(Client, RPL_ADMINEMAIL_MSG, Client_ID(prefix),
524                                 Conf_ServerAdminMail))
525                 return DISCONNECTED;
526
527         return CONNECTED;
528 } /* IRC_ADMIN */
529
530 /**
531  * Handler for the IRC command "INFO".
532  *
533  * @param Client The client from which this command has been received.
534  * @param Req Request structure with prefix and all parameters.
535  * @return CONNECTED or DISCONNECTED.
536  */
537 GLOBAL bool
538 IRC_INFO(CLIENT * Client, REQUEST * Req)
539 {
540         CLIENT *target, *prefix;
541         char msg[510];
542
543         assert(Client != NULL);
544         assert(Req != NULL);
545
546         IRC_SetPenalty(Client, 2);
547
548         _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
549         _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
550         _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
551
552         /* Forward? */
553         if (target != Client_ThisServer()) {
554                 IRC_WriteStrClientPrefix(target, prefix, "INFO %s",
555                                          Client_ID(target));
556                 return CONNECTED;
557         }
558
559         if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix),
560                                 NGIRCd_Version))
561                 return DISCONNECTED;
562
563 #if defined(__DATE__) && defined(__TIME__)
564         snprintf(msg, sizeof(msg), "Birth Date: %s at %s", __DATE__, __TIME__);
565         if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
566                 return DISCONNECTED;
567 #endif
568
569         strlcpy(msg, "On-line since ", sizeof(msg));
570         strlcat(msg, NGIRCd_StartStr, sizeof(msg));
571         if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
572                 return DISCONNECTED;
573
574         if (!IRC_WriteStrClient(Client, RPL_ENDOFINFO_MSG, Client_ID(prefix)))
575                 return DISCONNECTED;
576
577         return CONNECTED;
578 } /* IRC_INFO */
579
580 /**
581  * Handler for the IRC "ISON" command.
582  *
583  * @param Client The client from which this command has been received.
584  * @param Req Request structure with prefix and all parameters.
585  * @return CONNECTED or DISCONNECTED.
586  */
587 GLOBAL bool
588 IRC_ISON( CLIENT *Client, REQUEST *Req )
589 {
590         char rpl[COMMAND_LEN];
591         CLIENT *c;
592         char *ptr;
593         int i;
594
595         assert(Client != NULL);
596         assert(Req != NULL);
597
598         _IRC_ARGC_GE_OR_RETURN_(Client, Req, 1)
599
600         strlcpy(rpl, RPL_ISON_MSG, sizeof rpl);
601         for (i = 0; i < Req->argc; i++) {
602                 /* "All" ircd even parse ":<x> <y> ..." arguments and split
603                  * them up; so we do the same ... */
604                 ptr = strtok(Req->argv[i], " ");
605                 while (ptr) {
606                         ngt_TrimStr(ptr);
607                         c = Client_Search(ptr);
608                         if (c && Client_Type(c) == CLIENT_USER) {
609                                 strlcat(rpl, Client_ID(c), sizeof(rpl));
610                                 strlcat(rpl, " ", sizeof(rpl));
611                         }
612                         ptr = strtok(NULL, " ");
613                 }
614         }
615         ngt_TrimLastChr(rpl, ' ');
616
617         return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
618 } /* IRC_ISON */
619
620 /**
621  * Handler for the IRC "LINKS" command.
622  *
623  * @param Client The client from which this command has been received.
624  * @param Req Request structure with prefix and all parameters.
625  * @return CONNECTED or DISCONNECTED.
626  */
627 GLOBAL bool
628 IRC_LINKS(CLIENT *Client, REQUEST *Req)
629 {
630         CLIENT *target, *from, *c;
631         char *mask;
632
633         assert(Client != NULL);
634         assert(Req != NULL);
635
636         IRC_SetPenalty(Client, 1);
637
638         _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
639         _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
640
641         /* Get pointer to server mask or "*", if none given */
642         if (Req->argc > 0)
643                 mask = Req->argv[Req->argc - 1];
644         else
645                 mask = "*";
646
647         /* Forward? */
648         if (Req->argc == 2) {
649                 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
650                 if (target != Client_ThisServer()) {
651                         IRC_WriteStrClientPrefix(target, from,
652                                         "LINKS %s %s", Client_ID(target),
653                                         Req->argv[1]);
654                         return CONNECTED;
655                 }
656         }
657
658         c = Client_First();
659         while (c) {
660                 if (Client_Type(c) == CLIENT_SERVER
661                     && MatchCaseInsensitive(mask, Client_ID(c))) {
662                         if (!IRC_WriteStrClient(from, RPL_LINKS_MSG,
663                                         Client_ID(from), Client_ID(c),
664                                         Client_ID(Client_TopServer(c)
665                                                   ? Client_TopServer(c)
666                                                   : Client_ThisServer()),
667                                         Client_Hops(c), Client_Info(c)))
668                                 return DISCONNECTED;
669                 }
670                 c = Client_Next(c);
671         }
672         return IRC_WriteStrClient(from, RPL_ENDOFLINKS_MSG,
673                                   Client_ID(from), mask);
674 } /* IRC_LINKS */
675
676 /**
677  * Handler for the IRC "LUSERS" command.
678  *
679  * @param Client The client from which this command has been received.
680  * @param Req Request structure with prefix and all parameters.
681  * @return CONNECTED or DISCONNECTED.
682  */
683 GLOBAL bool
684 IRC_LUSERS( CLIENT *Client, REQUEST *Req )
685 {
686         CLIENT *target, *from;
687
688         assert( Client != NULL );
689         assert( Req != NULL );
690
691         IRC_SetPenalty(Client, 1);
692
693         _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
694         _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
695         _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
696
697         /* Forward? */
698         if (target != Client_ThisServer()) {
699                 IRC_WriteStrClientPrefix(target, from,
700                                          "LUSERS %s %s", Req->argv[0],
701                                          Client_ID(target));
702                 return CONNECTED;
703         }
704
705         return IRC_Send_LUSERS(from);
706 } /* IRC_LUSERS */
707
708 /**
709  * Handler for the IRC command "SERVLIST".
710  *
711  * @param Client The client from which this command has been received.
712  * @param Req Request structure with prefix and all parameters.
713  * @return CONNECTED or DISCONNECTED.
714  */
715 GLOBAL bool
716 IRC_SERVLIST(CLIENT *Client, REQUEST *Req)
717 {
718         CLIENT *c;
719
720         IRC_SetPenalty(Client, 1);
721
722         assert(Client != NULL);
723         assert(Req != NULL);
724
725         _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
726
727         if (Req->argc < 2 || strcmp(Req->argv[1], "0") == 0) {
728                 for (c = Client_First(); c!= NULL; c = Client_Next(c)) {
729                         if (Client_Type(c) != CLIENT_SERVICE)
730                                 continue;
731                         if (Req->argc > 0 && !MatchCaseInsensitive(Req->argv[0],
732                                                                   Client_ID(c)))
733                                 continue;
734                         if (!IRC_WriteStrClient(Client, RPL_SERVLIST_MSG,
735                                         Client_ID(Client), Client_Mask(c),
736                                         Client_Mask(Client_Introducer(c)), "*",
737                                         0, Client_Hops(c), Client_Info(c)))
738                                 return DISCONNECTED;
739                 }
740         }
741
742         return IRC_WriteStrClient(Client, RPL_SERVLISTEND_MSG, Client_ID(Client),
743                                   Req->argc > 0 ? Req->argv[0] : "*",
744                                   Req->argc > 1 ? Req->argv[1] : "0");
745 } /* IRC_SERVLIST */
746
747 /**
748  * Handler for the IRC command "MOTD".
749  *
750  * @param Client The client from which this command has been received.
751  * @param Req Request structure with prefix and all parameters.
752  * @return CONNECTED or DISCONNECTED.
753  */
754 GLOBAL bool
755 IRC_MOTD( CLIENT *Client, REQUEST *Req )
756 {
757         CLIENT *from, *target;
758
759         assert( Client != NULL );
760         assert( Req != NULL );
761
762         IRC_SetPenalty(Client, 3);
763
764         _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
765         _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
766         _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
767
768         /* Forward? */
769         if (target != Client_ThisServer()) {
770                 IRC_WriteStrClientPrefix(target, from, "MOTD %s",
771                                          Client_ID(target));
772                 return CONNECTED;
773         }
774
775         return IRC_Show_MOTD(from);
776 } /* IRC_MOTD */
777
778 /**
779  * Handler for the IRC command "NAMES".
780  *
781  * @param Client The client from which this command has been received.
782  * @param Req Request structure with prefix and all parameters.
783  * @return CONNECTED or DISCONNECTED.
784  */
785 GLOBAL bool
786 IRC_NAMES( CLIENT *Client, REQUEST *Req )
787 {
788         char rpl[COMMAND_LEN], *ptr;
789         CLIENT *target, *from, *c;
790         CHANNEL *chan;
791
792         assert( Client != NULL );
793         assert( Req != NULL );
794
795         IRC_SetPenalty(Client, 1);
796
797         _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
798         _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
799         _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
800
801         /* Forward? */
802         if (target != Client_ThisServer()) {
803                 IRC_WriteStrClientPrefix(target, from, "NAMES %s :%s",
804                                          Req->argv[0], Client_ID(target));
805                 return CONNECTED;
806         }
807
808         if (Req->argc > 0) {
809                 /* Return NAMES list for specific channels */
810                 ptr = strtok(Req->argv[0], ",");
811                 while(ptr) {
812                         chan = Channel_Search(ptr);
813                         if (chan && !IRC_Send_NAMES(from, chan))
814                                 return DISCONNECTED;
815                         if (!IRC_WriteStrClient(from, RPL_ENDOFNAMES_MSG,
816                                                 Client_ID(from), ptr))
817                                 return DISCONNECTED;
818                         ptr = strtok( NULL, "," );
819                 }
820                 return CONNECTED;
821         }
822
823         chan = Channel_First();
824         while (chan) {
825                 if (!IRC_Send_NAMES(from, chan))
826                         return DISCONNECTED;
827                 chan = Channel_Next(chan);
828         }
829
830         /* Now print all clients which are not in any channel */
831         c = Client_First();
832         snprintf(rpl, sizeof(rpl), RPL_NAMREPLY_MSG, Client_ID(from), "*", "*");
833         while (c) {
834                 if (Client_Type(c) == CLIENT_USER
835                     && Channel_FirstChannelOf(c) == NULL
836                     && !strchr(Client_Modes(c), 'i'))
837                 {
838                         /* its a user, concatenate ... */
839                         if (rpl[strlen(rpl) - 1] != ':')
840                                 strlcat(rpl, " ", sizeof(rpl));
841                         strlcat(rpl, Client_ID(c), sizeof(rpl));
842
843                         if (strlen(rpl) > COMMAND_LEN - CLIENT_NICK_LEN - 4) {
844                                 /* Line is gwoing too long, send now */
845                                 if (!IRC_WriteStrClient(from, "%s", rpl))
846                                         return DISCONNECTED;
847                                 snprintf(rpl, sizeof(rpl), RPL_NAMREPLY_MSG,
848                                          Client_ID(from), "*", "*");
849                         }
850                 }
851                 c = Client_Next(c);
852         }
853         if (rpl[strlen(rpl) - 1] != ':' && !IRC_WriteStrClient(from, "%s", rpl))
854                 return DISCONNECTED;
855
856         return IRC_WriteStrClient(from, RPL_ENDOFNAMES_MSG, Client_ID(from), "*");
857 } /* IRC_NAMES */
858
859 /**
860  * Handler for the IRC command "STATS".
861  *
862  * @param Client The client from which this command has been received.
863  * @param Req Request structure with prefix and all parameters.
864  * @return CONNECTED or DISCONNECTED.
865  */
866 GLOBAL bool
867 IRC_STATS( CLIENT *Client, REQUEST *Req )
868 {
869         CLIENT *from, *target, *cl;
870         CONN_ID con;
871         char query;
872         COMMAND *cmd;
873         time_t time_now;
874         unsigned int days, hrs, mins;
875         struct list_head *list;
876         struct list_elem *list_item;
877
878         assert(Client != NULL);
879         assert(Req != NULL);
880
881         IRC_SetPenalty(Client, 2);
882
883         _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
884         _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
885         _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
886
887         /* Forward? */
888         if (target != Client_ThisServer()) {
889                 IRC_WriteStrClientPrefix(target, from, "STATS %s %s",
890                                          Req->argv[0], Client_ID(target));
891                 return CONNECTED;
892         }
893
894         if (Req->argc > 0)
895                 query = Req->argv[0][0] ? Req->argv[0][0] : '*';
896         else
897                 query = '*';
898
899         switch (query) {
900         case 'g':       /* Network-wide bans ("G-Lines") */
901         case 'G':
902         case 'k':       /* Server-local bans ("K-Lines") */
903         case 'K':
904                 if (!Client_HasMode(from, 'o'))
905                     return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG,
906                                               Client_ID(from));
907                 if (query == 'g' || query == 'G')
908                         list = Class_GetList(CLASS_GLINE);
909                 else
910                         list = Class_GetList(CLASS_KLINE);
911                         list_item = Lists_GetFirst(list);
912                         while (list_item) {
913                                 if (!IRC_WriteStrClient(from, RPL_STATSXLINE_MSG,
914                                                 Client_ID(from), query,
915                                                 Lists_GetMask(list_item),
916                                                 Lists_GetValidity(list_item),
917                                                 Lists_GetReason(list_item)))
918                                         return DISCONNECTED;
919                                 list_item = Lists_GetNext(list_item);
920                         }
921                 break;
922         case 'l':       /* Link status (servers and own link) */
923         case 'L':
924                 time_now = time(NULL);
925                 for (con = Conn_First(); con != NONE; con = Conn_Next(con)) {
926                         cl = Conn_GetClient(con);
927                         if (!cl)
928                                 continue;
929                         if ((Client_Type(cl) == CLIENT_SERVER)
930                             || (cl == Client)) {
931                                 /* Server link or our own connection */
932 #ifdef ZLIB
933                                 if (Conn_Options(con) & CONN_ZIP) {
934                                         if (!IRC_WriteStrClient
935                                             (from, RPL_STATSLINKINFOZIP_MSG,
936                                              Client_ID(from), Client_Mask(cl),
937                                              Conn_SendQ(con), Conn_SendMsg(con),
938                                              Zip_SendBytes(con),
939                                              Conn_SendBytes(con),
940                                              Conn_RecvMsg(con),
941                                              Zip_RecvBytes(con),
942                                              Conn_RecvBytes(con),
943                                              (long)(time_now - Conn_StartTime(con))))
944                                                 return DISCONNECTED;
945                                         continue;
946                                 }
947 #endif
948                                 if (!IRC_WriteStrClient
949                                     (from, RPL_STATSLINKINFO_MSG,
950                                      Client_ID(from), Client_Mask(cl),
951                                      Conn_SendQ(con), Conn_SendMsg(con),
952                                      Conn_SendBytes(con), Conn_RecvMsg(con),
953                                      Conn_RecvBytes(con),
954                                      (long)(time_now - Conn_StartTime(con))))
955                                         return DISCONNECTED;
956                         }
957                 }
958                 break;
959         case 'm':       /* IRC command status (usage count) */
960         case 'M':
961                 cmd = Parse_GetCommandStruct();
962                 for (; cmd->name; cmd++) {
963                         if (cmd->lcount == 0 && cmd->rcount == 0)
964                                 continue;
965                         if (!IRC_WriteStrClient
966                             (from, RPL_STATSCOMMANDS_MSG, Client_ID(from),
967                              cmd->name, cmd->lcount, cmd->bytes, cmd->rcount))
968                                 return DISCONNECTED;
969                 }
970                 break;
971         case 'u':       /* Server uptime */
972         case 'U':
973                 time_now = time(NULL) - NGIRCd_Start;
974                 days = uptime_days(&time_now);
975                 hrs = uptime_hrs(&time_now);
976                 mins = uptime_mins(&time_now);
977                 if (!IRC_WriteStrClient(from, RPL_STATSUPTIME, Client_ID(from),
978                                        days, hrs, mins, (unsigned int)time_now))
979                         return DISCONNECTED;
980                 break;
981         }
982
983         return IRC_WriteStrClient(from, RPL_ENDOFSTATS_MSG,
984                                   Client_ID(from), query);
985 } /* IRC_STATS */
986
987 /**
988  * Handler for the IRC command "SUMMON".
989  *
990  * @param Client The client from which this command has been received.
991  * @param Req Request structure with prefix and all parameters.
992  * @return CONNECTED or DISCONNECTED.
993  */
994 GLOBAL bool
995 IRC_SUMMON(CLIENT * Client, UNUSED REQUEST * Req)
996 {
997         assert(Client != NULL);
998
999         IRC_SetPenalty(Client, 1);
1000
1001         return IRC_WriteStrClient(Client, ERR_SUMMONDISABLED_MSG,
1002                                   Client_ID(Client));
1003 } /* IRC_SUMMON */
1004
1005 /**
1006  * Handler for the IRC command "TIME".
1007  *
1008  * @param Client The client from which this command has been received.
1009  * @param Req Request structure with prefix and all parameters.
1010  * @return CONNECTED or DISCONNECTED.
1011  */
1012 GLOBAL bool
1013 IRC_TIME( CLIENT *Client, REQUEST *Req )
1014 {
1015         CLIENT *from, *target;
1016         char t_str[64];
1017         time_t t;
1018
1019         assert(Client != NULL);
1020         assert(Req != NULL);
1021
1022         IRC_SetPenalty(Client, 1);
1023
1024         _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
1025         _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
1026         _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
1027
1028         /* Forward? */
1029         if (target != Client_ThisServer()) {
1030                 IRC_WriteStrClientPrefix(target, from, "TIME %s",
1031                                          Client_ID(target));
1032                 return CONNECTED;
1033         }
1034
1035         t = time( NULL );
1036         (void)strftime(t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime(&t));
1037         return IRC_WriteStrClient(from, RPL_TIME_MSG, Client_ID(from),
1038                                   Client_ID(Client_ThisServer()), t_str);
1039 } /* IRC_TIME */
1040
1041 /**
1042  * Handler for the IRC command "USERHOST".
1043  *
1044  * @param Client The client from which this command has been received.
1045  * @param Req Request structure with prefix and all parameters.
1046  * @return CONNECTED or DISCONNECTED.
1047  */
1048 GLOBAL bool
1049 IRC_USERHOST(CLIENT *Client, REQUEST *Req)
1050 {
1051         char rpl[COMMAND_LEN];
1052         CLIENT *c;
1053         int max, i;
1054
1055         assert(Client != NULL);
1056         assert(Req != NULL);
1057
1058         IRC_SetPenalty(Client, 1);
1059
1060         _IRC_ARGC_GE_OR_RETURN_(Client, Req, 1)
1061
1062         if (Req->argc > 5)
1063                 max = 5;
1064         else
1065                 max = Req->argc;
1066
1067         strlcpy(rpl, RPL_USERHOST_MSG, sizeof rpl);
1068         for (i = 0; i < max; i++) {
1069                 c = Client_Search(Req->argv[i]);
1070                 if (c && (Client_Type(c) == CLIENT_USER)) {
1071                         /* This Nick is "online" */
1072                         strlcat(rpl, Client_ID(c), sizeof(rpl));
1073                         if (Client_HasMode(c, 'o'))
1074                                 strlcat(rpl, "*", sizeof(rpl));
1075                         strlcat(rpl, "=", sizeof(rpl));
1076                         if (Client_HasMode(c, 'a'))
1077                                 strlcat(rpl, "-", sizeof(rpl));
1078                         else
1079                                 strlcat(rpl, "+", sizeof(rpl));
1080                         strlcat(rpl, Client_User(c), sizeof(rpl));
1081                         strlcat(rpl, "@", sizeof(rpl));
1082                         strlcat(rpl, Client_HostnameDisplayed(c), sizeof(rpl));
1083                         strlcat(rpl, " ", sizeof(rpl));
1084                 }
1085         }
1086         ngt_TrimLastChr(rpl, ' ');
1087
1088         return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
1089 } /* IRC_USERHOST */
1090
1091 /**
1092  * Handler for the IRC command "USERS".
1093  *
1094  * @param Client The client from which this command has been received.
1095  * @param Req Request structure with prefix and all parameters.
1096  * @return CONNECTED or DISCONNECTED.
1097  */
1098 GLOBAL bool
1099 IRC_USERS(CLIENT * Client, UNUSED REQUEST * Req)
1100 {
1101         assert(Client != NULL);
1102
1103         IRC_SetPenalty(Client, 1);
1104
1105         return IRC_WriteStrClient(Client, ERR_USERSDISABLED_MSG,
1106                                   Client_ID(Client));
1107 } /* IRC_USERS */
1108
1109 /**
1110  * Handler for the IRC command "VERSION".
1111  *
1112  * @param Client The client from which this command has been received.
1113  * @param Req Request structure with prefix and all parameters.
1114  * @return CONNECTED or DISCONNECTED.
1115  */
1116 GLOBAL bool
1117 IRC_VERSION( CLIENT *Client, REQUEST *Req )
1118 {
1119         CLIENT *target, *prefix;
1120
1121         assert( Client != NULL );
1122         assert( Req != NULL );
1123
1124         IRC_SetPenalty(Client, 1);
1125
1126         _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
1127         _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
1128         _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
1129
1130         /* Forward? */
1131         if (target != Client_ThisServer()) {
1132                 IRC_WriteStrClientPrefix(target, prefix, "VERSION %s",
1133                                          Client_ID(target));
1134                 return CONNECTED;
1135         }
1136
1137         /* send version information */
1138         return IRC_WriteStrClient(Client, RPL_VERSION_MSG, Client_ID(prefix),
1139                                   PACKAGE_NAME, PACKAGE_VERSION,
1140                                   NGIRCd_DebugLevel, Conf_ServerName,
1141                                   NGIRCd_VersionAddition);
1142 } /* IRC_VERSION */
1143
1144 /**
1145  * Handler for the IRC "WHO" command.
1146  *
1147  * @param Client The client from which this command has been received.
1148  * @param Req Request structure with prefix and all parameters.
1149  * @return CONNECTED or DISCONNECTED.
1150  */
1151 GLOBAL bool
1152 IRC_WHO(CLIENT *Client, REQUEST *Req)
1153 {
1154         bool only_ops;
1155         CHANNEL *chan;
1156
1157         assert (Client != NULL);
1158         assert (Req != NULL);
1159
1160         IRC_SetPenalty(Client, 1);
1161
1162         _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
1163
1164         only_ops = false;
1165         if (Req->argc == 2) {
1166                 if (strcmp(Req->argv[1], "o") == 0)
1167                         only_ops = true;
1168 #ifdef STRICT_RFC
1169                 else
1170                         return IRC_WriteStrClient(Client,
1171                                                   ERR_NEEDMOREPARAMS_MSG,
1172                                                   Client_ID(Client),
1173                                                   Req->command);
1174 #endif
1175         }
1176
1177         if (Req->argc >= 1) {
1178                 /* Channel or mask given */
1179                 chan = Channel_Search(Req->argv[0]);
1180                 if (chan) {
1181                         /* Members of a channel have been requested */
1182                         return IRC_WHO_Channel(Client, chan, only_ops);
1183                 }
1184                 if (strcmp(Req->argv[0], "0") != 0) {
1185                         /* A mask has been given. But please note this RFC
1186                          * stupidity: "0" is same as no arguments ... */
1187                         return IRC_WHO_Mask(Client, Req->argv[0], only_ops);
1188                 }
1189         }
1190
1191         /* No channel or (valid) mask given */
1192         IRC_SetPenalty(Client, 2);
1193         return IRC_WHO_Mask(Client, NULL, only_ops);
1194 } /* IRC_WHO */
1195
1196 /**
1197  * Handler for the IRC "WHOIS" command.
1198  *
1199  * @param Client The client from which this command has been received.
1200  * @param Req Request structure with prefix and all parameters.
1201  * @return CONNECTED or DISCONNECTED.
1202  */
1203 GLOBAL bool
1204 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
1205 {
1206         CLIENT *from, *target, *c;
1207         unsigned int match_count = 0, found = 0;
1208         bool has_wildcards, is_remote;
1209         bool got_wildcard = false;
1210         char mask[COMMAND_LEN], *query;
1211
1212         assert( Client != NULL );
1213         assert( Req != NULL );
1214
1215         IRC_SetPenalty(Client, 1);
1216
1217         /* Bad number of parameters? */
1218         if (Req->argc < 1 || Req->argc > 2)
1219                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1220                                           Client_ID(Client), Req->command);
1221
1222         _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
1223
1224         /* Get target server for this command */
1225         if (Req->argc > 1) {
1226                 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, Client)
1227         } else
1228                 target = Client_ThisServer();
1229
1230         assert(target != NULL);
1231
1232         /* Forward? */
1233         if (target != Client_ThisServer()) {
1234                 IRC_WriteStrClientPrefix(target, from, "WHOIS %s :%s",
1235                                          Req->argv[0], Req->argv[1]);
1236                 return CONNECTED;
1237         }
1238
1239         is_remote = Client_Conn(from) < 0;
1240         strlcpy(mask, Req->argv[Req->argc - 1], sizeof(mask));
1241         for (query = strtok(ngt_LowerStr(mask), ",");
1242                         query && found < 3;
1243                         query = strtok(NULL, ","), found++)
1244         {
1245                 has_wildcards = query[strcspn(query, "*?")] != 0;
1246                 /*
1247                  * follows ircd 2.10 implementation:
1248                  *  - handle up to 3 targets
1249                  *  - no wildcards for remote clients
1250                  *  - only one wildcard target per local client
1251                  *
1252                  *  Also, at most MAX_RPL_WHOIS matches are returned.
1253                  */
1254                 if (!has_wildcards || is_remote) {
1255                         c = Client_Search(query);
1256                         if (c && (Client_Type(c) == CLIENT_USER
1257                                   || Client_Type(c) == CLIENT_SERVICE)) {
1258                                 if (!IRC_WHOIS_SendReply(Client, from, c))
1259                                         return DISCONNECTED;
1260                         } else {
1261                                 if (!IRC_WriteStrClient(Client,
1262                                                         ERR_NOSUCHNICK_MSG,
1263                                                         Client_ID(Client),
1264                                                         query))
1265                                         return DISCONNECTED;
1266                         }
1267                         continue;
1268                 }
1269                 if (got_wildcard) {
1270                         /* we already handled one wildcard query */
1271                         if (!IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1272                              Client_ID(Client), query))
1273                                 return DISCONNECTED;
1274                         continue;
1275                 }
1276                 got_wildcard = true;
1277                 IRC_SetPenalty(Client, 3);
1278
1279                 for (c = Client_First(); c; c = Client_Next(c)) {
1280                         if (IRC_CheckListTooBig(Client, match_count,
1281                                             MAX_RPL_WHOIS, "WHOIS"))
1282                                 break;
1283
1284                         if (Client_Type(c) != CLIENT_USER)
1285                                 continue;
1286                         if (!MatchCaseInsensitive(query, Client_ID(c)))
1287                                 continue;
1288                         if (!IRC_WHOIS_SendReply(Client, from, c))
1289                                 return DISCONNECTED;
1290
1291                         match_count++;
1292                 }
1293
1294                 if (match_count == 0)
1295                         IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1296                                            Client_ID(Client),
1297                                            Req->argv[Req->argc - 1]);
1298         }
1299         return IRC_WriteStrClient(from, RPL_ENDOFWHOIS_MSG,
1300                                   Client_ID(from), Req->argv[Req->argc - 1]);
1301 } /* IRC_WHOIS */
1302
1303 /**
1304  * Handler for the IRC "WHOWAS" command.
1305  *
1306  * @param Client The client from which this command has been received.
1307  * @param Req Request structure with prefix and all parameters.
1308  * @return CONNECTED or DISCONNECTED.
1309  */
1310 GLOBAL bool
1311 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1312 {
1313         CLIENT *target, *prefix;
1314         WHOWAS *whowas;
1315         char tok_buf[COMMAND_LEN];
1316         int max, last, count, i, nc;
1317         const char *nick;
1318
1319         assert( Client != NULL );
1320         assert( Req != NULL );
1321
1322         /* Do not reveal any info on disconnected users? */
1323         if (Conf_MorePrivacy)
1324                 return CONNECTED;
1325
1326         /* Wrong number of parameters? */
1327         if (Req->argc < 1)
1328                 return IRC_WriteStrClient(Client, ERR_NONICKNAMEGIVEN_MSG,
1329                                           Client_ID(Client));
1330
1331         _IRC_ARGC_LE_OR_RETURN_(Client, Req, 3)
1332         _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
1333         _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 2, prefix)
1334
1335         /* Forward? */
1336         if (target != Client_ThisServer()) {
1337                 IRC_WriteStrClientPrefix(target, prefix, "WHOWAS %s %s %s",
1338                                          Req->argv[0], Req->argv[1],
1339                                          Client_ID(target));
1340                 return CONNECTED;
1341         }
1342
1343         whowas = Client_GetWhowas( );
1344         last = Client_GetLastWhowasIndex( );
1345         if (last < 0)
1346                 last = 0;
1347
1348         max = DEF_RPL_WHOWAS;
1349         if (Req->argc > 1) {
1350                 max = atoi(Req->argv[1]);
1351                 if (max < 1)
1352                         max = MAX_RPL_WHOWAS;
1353         }
1354
1355         /*
1356          * Break up the nick argument into a list of nicks, if applicable
1357          * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1358          */
1359         strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1360         nick = strtok(tok_buf, ",");
1361
1362         for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1363                 nc = 0;
1364                 do {
1365                         /* Used entry? */
1366                         if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1367                                 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1368                                         return DISCONNECTED;
1369                                 nc++;
1370                                 count++;
1371                         }
1372                         /* previous entry */
1373                         i--;
1374
1375                         /* "underflow", wrap around */
1376                         if (i < 0)
1377                                 i = MAX_WHOWAS - 1;
1378
1379                         if (nc && count >= max)
1380                                 break;
1381                 } while (i != last);
1382
1383                 if (nc == 0 && !IRC_WriteStrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1384                                                 Client_ID(prefix), nick))
1385                         return DISCONNECTED;
1386         }
1387         return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG,
1388                                   Client_ID(prefix), Req->argv[0]);
1389 } /* IRC_WHOWAS */
1390
1391 /**
1392  * Send LUSERS reply to a client.
1393  *
1394  * @param Client The receipient of the information.
1395  * @return CONNECTED or DISCONNECTED.
1396  */
1397 GLOBAL bool
1398 IRC_Send_LUSERS(CLIENT *Client)
1399 {
1400         unsigned long cnt;
1401 #ifndef STRICT_RFC
1402         unsigned long max;
1403 #endif
1404
1405         assert(Client != NULL);
1406
1407         /* Users, services and serevers in the network */
1408         if (!IRC_WriteStrClient(Client, RPL_LUSERCLIENT_MSG, Client_ID(Client),
1409                                 Client_UserCount(), Client_ServiceCount(),
1410                                 Client_ServerCount()))
1411                 return DISCONNECTED;
1412
1413         /* Number of IRC operators */
1414         cnt = Client_OperCount( );
1415         if (cnt > 0) {
1416                 if (!IRC_WriteStrClient(Client, RPL_LUSEROP_MSG,
1417                                         Client_ID(Client), cnt))
1418                         return DISCONNECTED;
1419         }
1420
1421         /* Unknown connections */
1422         cnt = Client_UnknownCount( );
1423         if (cnt > 0) {
1424                 if (!IRC_WriteStrClient(Client, RPL_LUSERUNKNOWN_MSG,
1425                                         Client_ID(Client), cnt))
1426                         return DISCONNECTED;
1427         }
1428
1429         /* Number of created channels */
1430         if (!IRC_WriteStrClient(Client, RPL_LUSERCHANNELS_MSG,
1431                                 Client_ID(Client),
1432                                 Channel_CountVisible(Client)))
1433                 return DISCONNECTED;
1434
1435         /* Number of local users, services and servers */
1436         if (!IRC_WriteStrClient(Client, RPL_LUSERME_MSG, Client_ID(Client),
1437                                 Client_MyUserCount(), Client_MyServiceCount(),
1438                                 Client_MyServerCount()))
1439                 return DISCONNECTED;
1440
1441 #ifndef STRICT_RFC
1442         /* Maximum number of local users */
1443         cnt = Client_MyUserCount();
1444         max = Client_MyMaxUserCount();
1445         if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1446                         cnt, max, cnt, max))
1447                 return DISCONNECTED;
1448         /* Maximum number of users in the network */
1449         cnt = Client_UserCount();
1450         max = Client_MaxUserCount();
1451         if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1452                         cnt, max, cnt, max))
1453                 return DISCONNECTED;
1454         /* Connection counters */
1455         if (! IRC_WriteStrClient(Client, RPL_STATSCONN_MSG, Client_ID(Client),
1456                         Conn_CountMax(), Conn_CountAccepted()))
1457                 return DISCONNECTED;
1458 #endif
1459
1460         return CONNECTED;
1461 } /* IRC_Send_LUSERS */
1462
1463 GLOBAL bool
1464 IRC_Show_MOTD( CLIENT *Client )
1465 {
1466         const char *line;
1467         size_t len_tot, len_str;
1468
1469         assert( Client != NULL );
1470
1471         len_tot = array_bytes(&Conf_Motd);
1472         if (len_tot == 0 && !Conn_UsesSSL(Client_Conn(Client)))
1473                 return IRC_WriteStrClient(Client, ERR_NOMOTD_MSG, Client_ID(Client));
1474
1475         if (!IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG, Client_ID(Client),
1476                                 Client_ID(Client_ThisServer())))
1477                 return DISCONNECTED;
1478
1479         line = array_start(&Conf_Motd);
1480         while (len_tot > 0) {
1481                 len_str = strlen(line) + 1;
1482
1483                 assert(len_tot >= len_str);
1484                 len_tot -= len_str;
1485
1486                 if (!IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID(Client), line))
1487                         return DISCONNECTED;
1488                 line += len_str;
1489         }
1490
1491         if (!Show_MOTD_SSLInfo(Client))
1492                 return DISCONNECTED;
1493
1494         if (!IRC_WriteStrClient(Client, RPL_ENDOFMOTD_MSG, Client_ID(Client)))
1495                 return DISCONNECTED;
1496
1497         if (*Conf_CloakHost)
1498                 return IRC_WriteStrClient(Client, RPL_HOSTHIDDEN_MSG,
1499                                           Client_ID(Client),
1500                                           Client_Hostname(Client));
1501
1502         return CONNECTED;
1503 } /* IRC_Show_MOTD */
1504
1505 /**
1506  * Send NAMES reply for a specific client and channel.
1507  *
1508  * @param Client The client requesting the NAMES information.
1509  * @param Chan The channel
1510  * @return CONNECTED or DISCONNECTED.
1511  */
1512 GLOBAL bool
1513 IRC_Send_NAMES(CLIENT * Client, CHANNEL * Chan)
1514 {
1515         bool is_visible, is_member;
1516         char str[COMMAND_LEN];
1517         CL2CHAN *cl2chan;
1518         CLIENT *cl;
1519
1520         assert(Client != NULL);
1521         assert(Chan != NULL);
1522
1523         if (Channel_IsMemberOf(Chan, Client))
1524                 is_member = true;
1525         else
1526                 is_member = false;
1527
1528         /* Do not print info on channel memberships to anyone that is not member? */
1529         if (Conf_MorePrivacy && !is_member)
1530                 return CONNECTED;
1531
1532         /* Secret channel? */
1533         if (!is_member && strchr(Channel_Modes(Chan), 's'))
1534                 return CONNECTED;
1535
1536         snprintf(str, sizeof(str), RPL_NAMREPLY_MSG, Client_ID(Client), "=",
1537                  Channel_Name(Chan));
1538         cl2chan = Channel_FirstMember(Chan);
1539         while (cl2chan) {
1540                 cl = Channel_GetClient(cl2chan);
1541
1542                 if (strchr(Client_Modes(cl), 'i'))
1543                         is_visible = false;
1544                 else
1545                         is_visible = true;
1546
1547                 if (is_member || is_visible) {
1548                         if (str[strlen(str) - 1] != ':')
1549                                 strlcat(str, " ", sizeof(str));
1550
1551                         who_flags_qualifier(Client, Channel_UserModes(Chan, cl),
1552                                             str, sizeof(str));
1553                         strlcat(str, Client_ID(cl), sizeof(str));
1554
1555                         if (strlen(str) > (COMMAND_LEN - CLIENT_NICK_LEN - 4)) {
1556                                 if (!IRC_WriteStrClient(Client, "%s", str))
1557                                         return DISCONNECTED;
1558                                 snprintf(str, sizeof(str), RPL_NAMREPLY_MSG,
1559                                          Client_ID(Client), "=",
1560                                          Channel_Name(Chan));
1561                         }
1562                 }
1563
1564                 cl2chan = Channel_NextMember(Chan, cl2chan);
1565         }
1566         if (str[strlen(str) - 1] != ':') {
1567                 if (!IRC_WriteStrClient(Client, "%s", str))
1568                         return DISCONNECTED;
1569         }
1570
1571         return CONNECTED;
1572 } /* IRC_Send_NAMES */
1573
1574 /**
1575  * Send the ISUPPORT numeric (005).
1576  * This numeric indicates the features that are supported by this server.
1577  * See <http://www.irc.org/tech_docs/005.html> for details.
1578  */
1579 GLOBAL bool
1580 IRC_Send_ISUPPORT(CLIENT * Client)
1581 {
1582         if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1583                                 CHANTYPES, CHANTYPES, Conf_MaxJoins))
1584                 return DISCONNECTED;
1585         return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1586                                   CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1587                                   COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1588                                   COMMAND_LEN - 113, MAX_HNDL_MODES_ARG,
1589                                   MAX_HNDL_CHANNEL_LISTS);
1590 } /* IRC_Send_ISUPPORT */
1591
1592 /* -eof- */