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