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