]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-info.c
Change the certificate fingerprint digest to sha256
[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                         IRC_SetPenalty(Client, 2);
1151                         return IRC_WriteErrClient(Client,
1152                                                   ERR_NEEDMOREPARAMS_MSG,
1153                                                   Client_ID(Client),
1154                                                   Req->command);
1155                 }
1156 #endif
1157         }
1158
1159         if (Req->argc >= 1) {
1160                 /* Channel or mask given */
1161                 chan = Channel_Search(Req->argv[0]);
1162                 if (chan) {
1163                         /* Members of a channel have been requested */
1164                         return IRC_WHO_Channel(Client, chan, only_ops);
1165                 }
1166                 if (strcmp(Req->argv[0], "0") != 0) {
1167                         /* A mask has been given. But please note this RFC
1168                          * stupidity: "0" is same as no arguments ... */
1169                         return IRC_WHO_Mask(Client, Req->argv[0], only_ops);
1170                 }
1171         }
1172
1173         /* No channel or (valid) mask given */
1174         IRC_SetPenalty(Client, 2);
1175         return IRC_WHO_Mask(Client, NULL, only_ops);
1176 } /* IRC_WHO */
1177
1178 /**
1179  * Handler for the IRC "WHOIS" command.
1180  *
1181  * @param Client The client from which this command has been received.
1182  * @param Req Request structure with prefix and all parameters.
1183  * @return CONNECTED or DISCONNECTED.
1184  */
1185 GLOBAL bool
1186 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
1187 {
1188         CLIENT *from, *target, *c;
1189         unsigned int match_count = 0, found = 0;
1190         bool has_wildcards, is_remote;
1191         bool got_wildcard = false;
1192         char mask[COMMAND_LEN], *query;
1193
1194         assert( Client != NULL );
1195         assert( Req != NULL );
1196
1197         IRC_SetPenalty(Client, 1);
1198
1199         /* Wrong number of parameters? */
1200         if (Req->argc < 1)
1201                 return IRC_WriteErrClient(Client, ERR_NONICKNAMEGIVEN_MSG,
1202                                           Client_ID(Client));
1203
1204         _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
1205         _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
1206
1207         /* Get target server for this command */
1208         if (Req->argc > 1) {
1209                 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, Client)
1210         } else
1211                 target = Client_ThisServer();
1212
1213         assert(target != NULL);
1214
1215         /* Forward? */
1216         if (target != Client_ThisServer()) {
1217                 IRC_WriteStrClientPrefix(target, from, "WHOIS %s :%s",
1218                                          Req->argv[0], Req->argv[1]);
1219                 return CONNECTED;
1220         }
1221
1222         is_remote = Client_Conn(from) < 0;
1223         strlcpy(mask, Req->argv[Req->argc - 1], sizeof(mask));
1224         for (query = strtok(ngt_LowerStr(mask), ",");
1225                         query && found < 3;
1226                         query = strtok(NULL, ","), found++)
1227         {
1228                 has_wildcards = query[strcspn(query, "*?")] != 0;
1229                 /*
1230                  * follows ircd 2.10 implementation:
1231                  *  - handle up to 3 targets
1232                  *  - no wildcards for remote clients
1233                  *  - only one wildcard target per local client
1234                  *
1235                  *  Also, at most MAX_RPL_WHOIS matches are returned.
1236                  */
1237                 if (!has_wildcards || is_remote) {
1238                         c = Client_Search(query);
1239                         if (c && (Client_Type(c) == CLIENT_USER
1240                                   || Client_Type(c) == CLIENT_SERVICE)) {
1241                                 if (!IRC_WHOIS_SendReply(Client, from, c))
1242                                         return DISCONNECTED;
1243                         } else {
1244                                 if (!IRC_WriteErrClient(Client,
1245                                                         ERR_NOSUCHNICK_MSG,
1246                                                         Client_ID(Client),
1247                                                         query))
1248                                         return DISCONNECTED;
1249                         }
1250                         continue;
1251                 }
1252                 if (got_wildcard) {
1253                         /* we already handled one wildcard query */
1254                         if (!IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
1255                              Client_ID(Client), query))
1256                                 return DISCONNECTED;
1257                         continue;
1258                 }
1259                 got_wildcard = true;
1260                 IRC_SetPenalty(Client, 3);
1261
1262                 for (c = Client_First(); c; c = Client_Next(c)) {
1263                         if (IRC_CheckListTooBig(Client, match_count,
1264                                             MAX_RPL_WHOIS, "WHOIS"))
1265                                 break;
1266
1267                         if (Client_Type(c) != CLIENT_USER)
1268                                 continue;
1269                         if (!MatchCaseInsensitive(query, Client_ID(c)))
1270                                 continue;
1271                         if (!IRC_WHOIS_SendReply(Client, from, c))
1272                                 return DISCONNECTED;
1273
1274                         match_count++;
1275                 }
1276
1277                 if (match_count == 0)
1278                         IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
1279                                            Client_ID(Client),
1280                                            Req->argv[Req->argc - 1]);
1281         }
1282         return IRC_WriteStrClient(from, RPL_ENDOFWHOIS_MSG,
1283                                   Client_ID(from), Req->argv[Req->argc - 1]);
1284 } /* IRC_WHOIS */
1285
1286 /**
1287  * Handler for the IRC "WHOWAS" command.
1288  *
1289  * @param Client The client from which this command has been received.
1290  * @param Req Request structure with prefix and all parameters.
1291  * @return CONNECTED or DISCONNECTED.
1292  */
1293 GLOBAL bool
1294 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1295 {
1296         CLIENT *target, *prefix;
1297         WHOWAS *whowas;
1298         char tok_buf[COMMAND_LEN];
1299         int max, last, count, i, nc;
1300         const char *nick;
1301
1302         assert( Client != NULL );
1303         assert( Req != NULL );
1304
1305         /* Wrong number of parameters? */
1306         if (Req->argc < 1)
1307                 return IRC_WriteErrClient(Client, ERR_NONICKNAMEGIVEN_MSG,
1308                                           Client_ID(Client));
1309
1310         _IRC_ARGC_LE_OR_RETURN_(Client, Req, 3)
1311         _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
1312         _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 2, prefix)
1313
1314         /* Do not reveal any info on disconnected users? */
1315         if (Conf_MorePrivacy)
1316                 return CONNECTED;
1317
1318         /* Forward? */
1319         if (target != Client_ThisServer()) {
1320                 IRC_WriteStrClientPrefix(target, prefix, "WHOWAS %s %s %s",
1321                                          Req->argv[0], Req->argv[1],
1322                                          Client_ID(target));
1323                 return CONNECTED;
1324         }
1325
1326         whowas = Client_GetWhowas( );
1327         last = Client_GetLastWhowasIndex( );
1328         if (last < 0)
1329                 last = 0;
1330
1331         max = DEF_RPL_WHOWAS;
1332         if (Req->argc > 1) {
1333                 max = atoi(Req->argv[1]);
1334                 if (max < 1)
1335                         max = MAX_RPL_WHOWAS;
1336         }
1337
1338         /*
1339          * Break up the nick argument into a list of nicks, if applicable
1340          * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1341          */
1342         strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1343         nick = strtok(tok_buf, ",");
1344
1345         for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1346                 nc = 0;
1347                 do {
1348                         /* Used entry? */
1349                         if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1350                                 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1351                                         return DISCONNECTED;
1352                                 nc++;
1353                                 count++;
1354                         }
1355                         /* previous entry */
1356                         i--;
1357
1358                         /* "underflow", wrap around */
1359                         if (i < 0)
1360                                 i = MAX_WHOWAS - 1;
1361
1362                         if (nc && count >= max)
1363                                 break;
1364                 } while (i != last);
1365
1366                 if (nc == 0 && !IRC_WriteErrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1367                                                 Client_ID(prefix), nick))
1368                         return DISCONNECTED;
1369         }
1370         return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG,
1371                                   Client_ID(prefix), Req->argv[0]);
1372 } /* IRC_WHOWAS */
1373
1374 /**
1375  * Send LUSERS reply to a client.
1376  *
1377  * @param Client The receipient of the information.
1378  * @return CONNECTED or DISCONNECTED.
1379  */
1380 GLOBAL bool
1381 IRC_Send_LUSERS(CLIENT *Client)
1382 {
1383         unsigned long cnt;
1384 #ifndef STRICT_RFC
1385         unsigned long max;
1386 #endif
1387
1388         assert(Client != NULL);
1389
1390         /* Users, services and servers in the network */
1391         if (!IRC_WriteStrClient(Client, RPL_LUSERCLIENT_MSG, Client_ID(Client),
1392                                 Client_UserCount(), Client_ServiceCount(),
1393                                 Client_ServerCount()))
1394                 return DISCONNECTED;
1395
1396         /* Number of IRC operators */
1397         cnt = Client_OperCount( );
1398         if (cnt > 0) {
1399                 if (!IRC_WriteStrClient(Client, RPL_LUSEROP_MSG,
1400                                         Client_ID(Client), cnt))
1401                         return DISCONNECTED;
1402         }
1403
1404         /* Unknown connections */
1405         cnt = Client_UnknownCount( );
1406         if (cnt > 0) {
1407                 if (!IRC_WriteStrClient(Client, RPL_LUSERUNKNOWN_MSG,
1408                                         Client_ID(Client), cnt))
1409                         return DISCONNECTED;
1410         }
1411
1412         /* Number of created channels */
1413         if (!IRC_WriteStrClient(Client, RPL_LUSERCHANNELS_MSG,
1414                                 Client_ID(Client),
1415                                 Channel_CountVisible(Client)))
1416                 return DISCONNECTED;
1417
1418         /* Number of local users, services and servers */
1419         if (!IRC_WriteStrClient(Client, RPL_LUSERME_MSG, Client_ID(Client),
1420                                 Client_MyUserCount(), Client_MyServiceCount(),
1421                                 Client_MyServerCount()))
1422                 return DISCONNECTED;
1423
1424 #ifndef STRICT_RFC
1425         /* Maximum number of local users */
1426         cnt = Client_MyUserCount();
1427         max = Client_MyMaxUserCount();
1428         if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1429                         cnt, max, cnt, max))
1430                 return DISCONNECTED;
1431         /* Maximum number of users in the network */
1432         cnt = Client_UserCount();
1433         max = Client_MaxUserCount();
1434         if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1435                         cnt, max, cnt, max))
1436                 return DISCONNECTED;
1437         /* Connection counters */
1438         if (! IRC_WriteStrClient(Client, RPL_STATSCONN_MSG, Client_ID(Client),
1439                         Conn_CountMax(), Conn_CountAccepted()))
1440                 return DISCONNECTED;
1441 #endif
1442
1443         return CONNECTED;
1444 } /* IRC_Send_LUSERS */
1445
1446 GLOBAL bool
1447 IRC_Show_MOTD( CLIENT *Client )
1448 {
1449         const char *line;
1450         size_t len_tot, len_str;
1451
1452         assert( Client != NULL );
1453
1454         len_tot = array_bytes(&Conf_Motd);
1455         if (len_tot == 0 && !Conn_UsesSSL(Client_Conn(Client)))
1456                 return IRC_WriteErrClient(Client, ERR_NOMOTD_MSG, Client_ID(Client));
1457
1458         if (!IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG, Client_ID(Client),
1459                                 Client_ID(Client_ThisServer())))
1460                 return DISCONNECTED;
1461
1462         line = array_start(&Conf_Motd);
1463         while (len_tot > 0) {
1464                 len_str = strlen(line) + 1;
1465
1466                 assert(len_tot >= len_str);
1467                 len_tot -= len_str;
1468
1469                 if (!IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID(Client), line))
1470                         return DISCONNECTED;
1471                 line += len_str;
1472         }
1473
1474         if (!Show_MOTD_SSLInfo(Client))
1475                 return DISCONNECTED;
1476
1477         if (!IRC_WriteStrClient(Client, RPL_ENDOFMOTD_MSG, Client_ID(Client)))
1478                 return DISCONNECTED;
1479
1480         if (*Conf_CloakHost)
1481                 return IRC_WriteStrClient(Client, RPL_HOSTHIDDEN_MSG,
1482                                           Client_ID(Client),
1483                                           Client_Hostname(Client));
1484
1485         return CONNECTED;
1486 } /* IRC_Show_MOTD */
1487
1488 /**
1489  * Send NAMES reply for a specific client and channel.
1490  *
1491  * @param Client The client requesting the NAMES information.
1492  * @param Chan The channel
1493  * @return CONNECTED or DISCONNECTED.
1494  */
1495 GLOBAL bool
1496 IRC_Send_NAMES(CLIENT * Client, CHANNEL * Chan)
1497 {
1498         bool is_visible, is_member;
1499         char str[COMMAND_LEN];
1500         CL2CHAN *cl2chan;
1501         CLIENT *cl;
1502
1503         assert(Client != NULL);
1504         assert(Chan != NULL);
1505
1506         if (Channel_IsMemberOf(Chan, Client))
1507                 is_member = true;
1508         else
1509                 is_member = false;
1510
1511         /* Do not print info on channel memberships to anyone that is not member? */
1512         if (Conf_MorePrivacy && !is_member)
1513                 return CONNECTED;
1514
1515         /* Secret channel? */
1516         if (!is_member && Channel_HasMode(Chan, 's'))
1517                 return CONNECTED;
1518
1519         snprintf(str, sizeof(str), RPL_NAMREPLY_MSG, Client_ID(Client), "=",
1520                  Channel_Name(Chan));
1521         cl2chan = Channel_FirstMember(Chan);
1522         while (cl2chan) {
1523                 cl = Channel_GetClient(cl2chan);
1524
1525                 if (Client_HasMode(cl, 'i'))
1526                         is_visible = false;
1527                 else
1528                         is_visible = true;
1529
1530                 if (is_member || is_visible) {
1531                         if (str[strlen(str) - 1] != ':')
1532                                 strlcat(str, " ", sizeof(str));
1533
1534                         who_flags_qualifier(Client, Channel_UserModes(Chan, cl),
1535                                             str, sizeof(str));
1536                         strlcat(str, Client_ID(cl), sizeof(str));
1537
1538                         if (strlen(str) > (COMMAND_LEN - CLIENT_NICK_LEN - 4)) {
1539                                 if (!IRC_WriteStrClient(Client, "%s", str))
1540                                         return DISCONNECTED;
1541                                 snprintf(str, sizeof(str), RPL_NAMREPLY_MSG,
1542                                          Client_ID(Client), "=",
1543                                          Channel_Name(Chan));
1544                         }
1545                 }
1546
1547                 cl2chan = Channel_NextMember(Chan, cl2chan);
1548         }
1549         if (str[strlen(str) - 1] != ':') {
1550                 if (!IRC_WriteStrClient(Client, "%s", str))
1551                         return DISCONNECTED;
1552         }
1553
1554         return CONNECTED;
1555 } /* IRC_Send_NAMES */
1556
1557 /**
1558  * Send the ISUPPORT numeric (005).
1559  * This numeric indicates the features that are supported by this server.
1560  * See <http://www.irc.org/tech_docs/005.html> for details.
1561  */
1562 GLOBAL bool
1563 IRC_Send_ISUPPORT(CLIENT * Client)
1564 {
1565         if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1566                                 CHANTYPES, CHANTYPES, Conf_MaxJoins))
1567                 return DISCONNECTED;
1568         return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1569                                   CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1570                                   COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1571                                   COMMAND_LEN - 113, MAX_HNDL_MODES_ARG,
1572                                   MAX_HNDL_CHANNEL_LISTS);
1573 } /* IRC_Send_ISUPPORT */
1574
1575 /* -eof- */