]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-info.c
f87714fc90187927db3caf3d4e7e7024aa7642fd
[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), Client_Hostname(c),
411                                         Conn_GetIPAInfo(Client_Conn(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_ARGC_LE_OR_RETURN_(Client, Req, 1)
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_ARGC_LE_OR_RETURN_(Client, Req, 1)
550         _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
551         _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
552
553         /* Forward? */
554         if (target != Client_ThisServer()) {
555                 IRC_WriteStrClientPrefix(target, prefix, "INFO %s",
556                                          Client_ID(target));
557                 return CONNECTED;
558         }
559
560         if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix),
561                                 NGIRCd_Version))
562                 return DISCONNECTED;
563
564 #if defined(__DATE__) && defined(__TIME__)
565         snprintf(msg, sizeof(msg), "Birth Date: %s at %s", __DATE__, __TIME__);
566         if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
567                 return DISCONNECTED;
568 #endif
569
570         strlcpy(msg, "On-line since ", sizeof(msg));
571         strlcat(msg, NGIRCd_StartStr, sizeof(msg));
572         if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
573                 return DISCONNECTED;
574
575         if (!IRC_WriteStrClient(Client, RPL_ENDOFINFO_MSG, Client_ID(prefix)))
576                 return DISCONNECTED;
577
578         return CONNECTED;
579 } /* IRC_INFO */
580
581 /**
582  * Handler for the IRC "ISON" command.
583  *
584  * @param Client The client from which this command has been received.
585  * @param Req Request structure with prefix and all parameters.
586  * @return CONNECTED or DISCONNECTED.
587  */
588 GLOBAL bool
589 IRC_ISON( CLIENT *Client, REQUEST *Req )
590 {
591         char rpl[COMMAND_LEN];
592         CLIENT *c;
593         char *ptr;
594         int i;
595
596         assert(Client != NULL);
597         assert(Req != NULL);
598
599         _IRC_ARGC_GE_OR_RETURN_(Client, Req, 1)
600
601         strlcpy(rpl, RPL_ISON_MSG, sizeof rpl);
602         for (i = 0; i < Req->argc; i++) {
603                 /* "All" ircd even parse ":<x> <y> ..." arguments and split
604                  * them up; so we do the same ... */
605                 ptr = strtok(Req->argv[i], " ");
606                 while (ptr) {
607                         ngt_TrimStr(ptr);
608                         c = Client_Search(ptr);
609                         if (c && Client_Type(c) == CLIENT_USER) {
610                                 strlcat(rpl, Client_ID(c), sizeof(rpl));
611                                 strlcat(rpl, " ", sizeof(rpl));
612                         }
613                         ptr = strtok(NULL, " ");
614                 }
615         }
616         ngt_TrimLastChr(rpl, ' ');
617
618         return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
619 } /* IRC_ISON */
620
621 /**
622  * Handler for the IRC "LINKS" command.
623  *
624  * @param Client The client from which this command has been received.
625  * @param Req Request structure with prefix and all parameters.
626  * @return CONNECTED or DISCONNECTED.
627  */
628 GLOBAL bool
629 IRC_LINKS(CLIENT *Client, REQUEST *Req)
630 {
631         CLIENT *target, *from, *c;
632         char *mask;
633
634         assert(Client != NULL);
635         assert(Req != NULL);
636
637         IRC_SetPenalty(Client, 1);
638
639         _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
640         _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
641
642         /* Get pointer to server mask or "*", if none given */
643         if (Req->argc > 0)
644                 mask = Req->argv[Req->argc - 1];
645         else
646                 mask = "*";
647
648         /* Forward? */
649         if (Req->argc == 2) {
650                 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
651                 if (target != Client_ThisServer()) {
652                         IRC_WriteStrClientPrefix(target, from,
653                                         "LINKS %s %s", Client_ID(target),
654                                         Req->argv[1]);
655                         return CONNECTED;
656                 }
657         }
658
659         c = Client_First();
660         while (c) {
661                 if (Client_Type(c) == CLIENT_SERVER
662                     && MatchCaseInsensitive(mask, Client_ID(c))) {
663                         if (!IRC_WriteStrClient(from, RPL_LINKS_MSG,
664                                         Client_ID(from), Client_ID(c),
665                                         Client_ID(Client_TopServer(c)
666                                                   ? Client_TopServer(c)
667                                                   : Client_ThisServer()),
668                                         Client_Hops(c), Client_Info(c)))
669                                 return DISCONNECTED;
670                 }
671                 c = Client_Next(c);
672         }
673         return IRC_WriteStrClient(from, RPL_ENDOFLINKS_MSG,
674                                   Client_ID(from), mask);
675 } /* IRC_LINKS */
676
677 /**
678  * Handler for the IRC "LUSERS" command.
679  *
680  * @param Client The client from which this command has been received.
681  * @param Req Request structure with prefix and all parameters.
682  * @return CONNECTED or DISCONNECTED.
683  */
684 GLOBAL bool
685 IRC_LUSERS( CLIENT *Client, REQUEST *Req )
686 {
687         CLIENT *target, *from;
688
689         assert( Client != NULL );
690         assert( Req != NULL );
691
692         IRC_SetPenalty(Client, 1);
693
694         _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
695         _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
696         _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
697
698         /* Forward? */
699         if (target != Client_ThisServer()) {
700                 IRC_WriteStrClientPrefix(target, from,
701                                          "LUSERS %s %s", Req->argv[0],
702                                          Client_ID(target));
703                 return CONNECTED;
704         }
705
706         return IRC_Send_LUSERS(from);
707 } /* IRC_LUSERS */
708
709 /**
710  * Handler for the IRC command "SERVLIST".
711  *
712  * @param Client The client from which this command has been received.
713  * @param Req Request structure with prefix and all parameters.
714  * @return CONNECTED or DISCONNECTED.
715  */
716 GLOBAL bool
717 IRC_SERVLIST(CLIENT *Client, REQUEST *Req)
718 {
719         CLIENT *c;
720
721         IRC_SetPenalty(Client, 1);
722
723         assert(Client != NULL);
724         assert(Req != NULL);
725
726         _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
727
728         if (Req->argc < 2 || strcmp(Req->argv[1], "0") == 0) {
729                 for (c = Client_First(); c!= NULL; c = Client_Next(c)) {
730                         if (Client_Type(c) != CLIENT_SERVICE)
731                                 continue;
732                         if (Req->argc > 0 && !MatchCaseInsensitive(Req->argv[0],
733                                                                   Client_ID(c)))
734                                 continue;
735                         if (!IRC_WriteStrClient(Client, RPL_SERVLIST_MSG,
736                                         Client_ID(Client), Client_Mask(c),
737                                         Client_Mask(Client_Introducer(c)), "*",
738                                         0, Client_Hops(c), Client_Info(c)))
739                                 return DISCONNECTED;
740                 }
741         }
742
743         return IRC_WriteStrClient(Client, RPL_SERVLISTEND_MSG, Client_ID(Client),
744                                   Req->argc > 0 ? Req->argv[0] : "*",
745                                   Req->argc > 1 ? Req->argv[1] : "0");
746 } /* IRC_SERVLIST */
747
748 /**
749  * Handler for the IRC command "MOTD".
750  *
751  * @param Client The client from which this command has been received.
752  * @param Req Request structure with prefix and all parameters.
753  * @return CONNECTED or DISCONNECTED.
754  */
755 GLOBAL bool
756 IRC_MOTD( CLIENT *Client, REQUEST *Req )
757 {
758         CLIENT *from, *target;
759
760         assert( Client != NULL );
761         assert( Req != NULL );
762
763         IRC_SetPenalty(Client, 3);
764
765         _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
766         _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
767         _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
768
769         /* Forward? */
770         if (target != Client_ThisServer()) {
771                 IRC_WriteStrClientPrefix(target, from, "MOTD %s",
772                                          Client_ID(target));
773                 return CONNECTED;
774         }
775
776         return IRC_Show_MOTD(from);
777 } /* IRC_MOTD */
778
779 /**
780  * Handler for the IRC command "NAMES".
781  *
782  * @param Client The client from which this command has been received.
783  * @param Req Request structure with prefix and all parameters.
784  * @return CONNECTED or DISCONNECTED.
785  */
786 GLOBAL bool
787 IRC_NAMES( CLIENT *Client, REQUEST *Req )
788 {
789         char rpl[COMMAND_LEN], *ptr;
790         CLIENT *target, *from, *c;
791         CHANNEL *chan;
792
793         assert( Client != NULL );
794         assert( Req != NULL );
795
796         IRC_SetPenalty(Client, 1);
797
798         _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
799         _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
800         _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
801
802         /* Forward? */
803         if (target != Client_ThisServer()) {
804                 IRC_WriteStrClientPrefix(target, from, "NAMES %s :%s",
805                                          Req->argv[0], Client_ID(target));
806                 return CONNECTED;
807         }
808
809         if (Req->argc > 0) {
810                 /* Return NAMES list for specific channels */
811                 ptr = strtok(Req->argv[0], ",");
812                 while(ptr) {
813                         chan = Channel_Search(ptr);
814                         if (chan && !IRC_Send_NAMES(from, chan))
815                                 return DISCONNECTED;
816                         if (!IRC_WriteStrClient(from, RPL_ENDOFNAMES_MSG,
817                                                 Client_ID(from), ptr))
818                                 return DISCONNECTED;
819                         ptr = strtok( NULL, "," );
820                 }
821                 return CONNECTED;
822         }
823
824         chan = Channel_First();
825         while (chan) {
826                 if (!IRC_Send_NAMES(from, chan))
827                         return DISCONNECTED;
828                 chan = Channel_Next(chan);
829         }
830
831         /* Now print all clients which are not in any channel */
832         c = Client_First();
833         snprintf(rpl, sizeof(rpl), RPL_NAMREPLY_MSG, Client_ID(from), "*", "*");
834         while (c) {
835                 if (Client_Type(c) == CLIENT_USER
836                     && Channel_FirstChannelOf(c) == NULL
837                     && !Client_HasMode(c, 'i'))
838                 {
839                         /* its a user, concatenate ... */
840                         if (rpl[strlen(rpl) - 1] != ':')
841                                 strlcat(rpl, " ", sizeof(rpl));
842                         strlcat(rpl, Client_ID(c), sizeof(rpl));
843
844                         if (strlen(rpl) > COMMAND_LEN - CLIENT_NICK_LEN - 4) {
845                                 /* Line is gwoing too long, send now */
846                                 if (!IRC_WriteStrClient(from, "%s", rpl))
847                                         return DISCONNECTED;
848                                 snprintf(rpl, sizeof(rpl), RPL_NAMREPLY_MSG,
849                                          Client_ID(from), "*", "*");
850                         }
851                 }
852                 c = Client_Next(c);
853         }
854         if (rpl[strlen(rpl) - 1] != ':' && !IRC_WriteStrClient(from, "%s", rpl))
855                 return DISCONNECTED;
856
857         return IRC_WriteStrClient(from, RPL_ENDOFNAMES_MSG, Client_ID(from), "*");
858 } /* IRC_NAMES */
859
860 /**
861  * Handler for the IRC command "STATS".
862  *
863  * @param Client The client from which this command has been received.
864  * @param Req Request structure with prefix and all parameters.
865  * @return CONNECTED or DISCONNECTED.
866  */
867 GLOBAL bool
868 IRC_STATS( CLIENT *Client, REQUEST *Req )
869 {
870         CLIENT *from, *target, *cl;
871         CONN_ID con;
872         char query;
873         COMMAND *cmd;
874         time_t time_now;
875         unsigned int days, hrs, mins;
876         struct list_head *list;
877         struct list_elem *list_item;
878
879         assert(Client != NULL);
880         assert(Req != NULL);
881
882         IRC_SetPenalty(Client, 2);
883
884         _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
885         _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
886         _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
887
888         /* Forward? */
889         if (target != Client_ThisServer()) {
890                 IRC_WriteStrClientPrefix(target, from, "STATS %s %s",
891                                          Req->argv[0], Client_ID(target));
892                 return CONNECTED;
893         }
894
895         if (Req->argc > 0)
896                 query = Req->argv[0][0] ? Req->argv[0][0] : '*';
897         else
898                 query = '*';
899
900         switch (query) {
901         case 'g':       /* Network-wide bans ("G-Lines") */
902         case 'G':
903         case 'k':       /* Server-local bans ("K-Lines") */
904         case 'K':
905                 if (!Client_HasMode(from, 'o'))
906                     return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG,
907                                               Client_ID(from));
908                 if (query == 'g' || query == 'G')
909                         list = Class_GetList(CLASS_GLINE);
910                 else
911                         list = Class_GetList(CLASS_KLINE);
912                         list_item = Lists_GetFirst(list);
913                         while (list_item) {
914                                 if (!IRC_WriteStrClient(from, RPL_STATSXLINE_MSG,
915                                                 Client_ID(from), query,
916                                                 Lists_GetMask(list_item),
917                                                 Lists_GetValidity(list_item),
918                                                 Lists_GetReason(list_item)))
919                                         return DISCONNECTED;
920                                 list_item = Lists_GetNext(list_item);
921                         }
922                 break;
923         case 'l':       /* Link status (servers and own link) */
924         case 'L':
925                 time_now = time(NULL);
926                 for (con = Conn_First(); con != NONE; con = Conn_Next(con)) {
927                         cl = Conn_GetClient(con);
928                         if (!cl)
929                                 continue;
930                         if ((Client_Type(cl) == CLIENT_SERVER)
931                             || (cl == Client)) {
932                                 /* Server link or our own connection */
933 #ifdef ZLIB
934                                 if (Conn_Options(con) & CONN_ZIP) {
935                                         if (!IRC_WriteStrClient
936                                             (from, RPL_STATSLINKINFOZIP_MSG,
937                                              Client_ID(from), Client_Mask(cl),
938                                              Conn_SendQ(con), Conn_SendMsg(con),
939                                              Zip_SendBytes(con),
940                                              Conn_SendBytes(con),
941                                              Conn_RecvMsg(con),
942                                              Zip_RecvBytes(con),
943                                              Conn_RecvBytes(con),
944                                              (long)(time_now - Conn_StartTime(con))))
945                                                 return DISCONNECTED;
946                                         continue;
947                                 }
948 #endif
949                                 if (!IRC_WriteStrClient
950                                     (from, RPL_STATSLINKINFO_MSG,
951                                      Client_ID(from), Client_Mask(cl),
952                                      Conn_SendQ(con), Conn_SendMsg(con),
953                                      Conn_SendBytes(con), Conn_RecvMsg(con),
954                                      Conn_RecvBytes(con),
955                                      (long)(time_now - Conn_StartTime(con))))
956                                         return DISCONNECTED;
957                         }
958                 }
959                 break;
960         case 'm':       /* IRC command status (usage count) */
961         case 'M':
962                 cmd = Parse_GetCommandStruct();
963                 for (; cmd->name; cmd++) {
964                         if (cmd->lcount == 0 && cmd->rcount == 0)
965                                 continue;
966                         if (!IRC_WriteStrClient
967                             (from, RPL_STATSCOMMANDS_MSG, Client_ID(from),
968                              cmd->name, cmd->lcount, cmd->bytes, cmd->rcount))
969                                 return DISCONNECTED;
970                 }
971                 break;
972         case 'u':       /* Server uptime */
973         case 'U':
974                 time_now = time(NULL) - NGIRCd_Start;
975                 days = uptime_days(&time_now);
976                 hrs = uptime_hrs(&time_now);
977                 mins = uptime_mins(&time_now);
978                 if (!IRC_WriteStrClient(from, RPL_STATSUPTIME, Client_ID(from),
979                                        days, hrs, mins, (unsigned int)time_now))
980                         return DISCONNECTED;
981                 break;
982         }
983
984         return IRC_WriteStrClient(from, RPL_ENDOFSTATS_MSG,
985                                   Client_ID(from), query);
986 } /* IRC_STATS */
987
988 /**
989  * Handler for the IRC command "SUMMON".
990  *
991  * @param Client The client from which this command has been received.
992  * @param Req Request structure with prefix and all parameters.
993  * @return CONNECTED or DISCONNECTED.
994  */
995 GLOBAL bool
996 IRC_SUMMON(CLIENT * Client, UNUSED REQUEST * Req)
997 {
998         assert(Client != NULL);
999
1000         IRC_SetPenalty(Client, 1);
1001
1002         return IRC_WriteStrClient(Client, ERR_SUMMONDISABLED_MSG,
1003                                   Client_ID(Client));
1004 } /* IRC_SUMMON */
1005
1006 /**
1007  * Handler for the IRC command "TIME".
1008  *
1009  * @param Client The client from which this command has been received.
1010  * @param Req Request structure with prefix and all parameters.
1011  * @return CONNECTED or DISCONNECTED.
1012  */
1013 GLOBAL bool
1014 IRC_TIME( CLIENT *Client, REQUEST *Req )
1015 {
1016         CLIENT *from, *target;
1017         char t_str[64];
1018         time_t t;
1019
1020         assert(Client != NULL);
1021         assert(Req != NULL);
1022
1023         IRC_SetPenalty(Client, 1);
1024
1025         _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
1026         _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
1027         _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
1028
1029         /* Forward? */
1030         if (target != Client_ThisServer()) {
1031                 IRC_WriteStrClientPrefix(target, from, "TIME %s",
1032                                          Client_ID(target));
1033                 return CONNECTED;
1034         }
1035
1036         t = time( NULL );
1037         (void)strftime(t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime(&t));
1038         return IRC_WriteStrClient(from, RPL_TIME_MSG, Client_ID(from),
1039                                   Client_ID(Client_ThisServer()), t_str);
1040 } /* IRC_TIME */
1041
1042 /**
1043  * Handler for the IRC command "USERHOST".
1044  *
1045  * @param Client The client from which this command has been received.
1046  * @param Req Request structure with prefix and all parameters.
1047  * @return CONNECTED or DISCONNECTED.
1048  */
1049 GLOBAL bool
1050 IRC_USERHOST(CLIENT *Client, REQUEST *Req)
1051 {
1052         char rpl[COMMAND_LEN];
1053         CLIENT *c;
1054         int max, i;
1055
1056         assert(Client != NULL);
1057         assert(Req != NULL);
1058
1059         IRC_SetPenalty(Client, 1);
1060
1061         _IRC_ARGC_GE_OR_RETURN_(Client, Req, 1)
1062
1063         if (Req->argc > 5)
1064                 max = 5;
1065         else
1066                 max = Req->argc;
1067
1068         strlcpy(rpl, RPL_USERHOST_MSG, sizeof rpl);
1069         for (i = 0; i < max; i++) {
1070                 c = Client_Search(Req->argv[i]);
1071                 if (c && (Client_Type(c) == CLIENT_USER)) {
1072                         /* This Nick is "online" */
1073                         strlcat(rpl, Client_ID(c), sizeof(rpl));
1074                         if (Client_HasMode(c, 'o'))
1075                                 strlcat(rpl, "*", sizeof(rpl));
1076                         strlcat(rpl, "=", sizeof(rpl));
1077                         if (Client_HasMode(c, 'a'))
1078                                 strlcat(rpl, "-", sizeof(rpl));
1079                         else
1080                                 strlcat(rpl, "+", sizeof(rpl));
1081                         strlcat(rpl, Client_User(c), sizeof(rpl));
1082                         strlcat(rpl, "@", sizeof(rpl));
1083                         strlcat(rpl, Client_HostnameDisplayed(c), sizeof(rpl));
1084                         strlcat(rpl, " ", sizeof(rpl));
1085                 }
1086         }
1087         ngt_TrimLastChr(rpl, ' ');
1088
1089         return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
1090 } /* IRC_USERHOST */
1091
1092 /**
1093  * Handler for the IRC command "USERS".
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_USERS(CLIENT * Client, UNUSED REQUEST * Req)
1101 {
1102         assert(Client != NULL);
1103
1104         IRC_SetPenalty(Client, 1);
1105
1106         return IRC_WriteStrClient(Client, ERR_USERSDISABLED_MSG,
1107                                   Client_ID(Client));
1108 } /* IRC_USERS */
1109
1110 /**
1111  * Handler for the IRC command "VERSION".
1112  *
1113  * @param Client The client from which this command has been received.
1114  * @param Req Request structure with prefix and all parameters.
1115  * @return CONNECTED or DISCONNECTED.
1116  */
1117 GLOBAL bool
1118 IRC_VERSION( CLIENT *Client, REQUEST *Req )
1119 {
1120         CLIENT *target, *prefix;
1121
1122         assert( Client != NULL );
1123         assert( Req != NULL );
1124
1125         IRC_SetPenalty(Client, 1);
1126
1127         _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
1128         _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
1129         _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
1130
1131         /* Forward? */
1132         if (target != Client_ThisServer()) {
1133                 IRC_WriteStrClientPrefix(target, prefix, "VERSION %s",
1134                                          Client_ID(target));
1135                 return CONNECTED;
1136         }
1137
1138         /* send version information */
1139         return IRC_WriteStrClient(Client, RPL_VERSION_MSG, Client_ID(prefix),
1140                                   PACKAGE_NAME, PACKAGE_VERSION,
1141                                   NGIRCd_DebugLevel, Conf_ServerName,
1142                                   NGIRCd_VersionAddition);
1143 } /* IRC_VERSION */
1144
1145 /**
1146  * Handler for the IRC "WHO" command.
1147  *
1148  * @param Client The client from which this command has been received.
1149  * @param Req Request structure with prefix and all parameters.
1150  * @return CONNECTED or DISCONNECTED.
1151  */
1152 GLOBAL bool
1153 IRC_WHO(CLIENT *Client, REQUEST *Req)
1154 {
1155         bool only_ops;
1156         CHANNEL *chan;
1157
1158         assert (Client != NULL);
1159         assert (Req != NULL);
1160
1161         IRC_SetPenalty(Client, 1);
1162
1163         _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
1164
1165         only_ops = false;
1166         if (Req->argc == 2) {
1167                 if (strcmp(Req->argv[1], "o") == 0)
1168                         only_ops = true;
1169 #ifdef STRICT_RFC
1170                 else
1171                         return IRC_WriteStrClient(Client,
1172                                                   ERR_NEEDMOREPARAMS_MSG,
1173                                                   Client_ID(Client),
1174                                                   Req->command);
1175 #endif
1176         }
1177
1178         if (Req->argc >= 1) {
1179                 /* Channel or mask given */
1180                 chan = Channel_Search(Req->argv[0]);
1181                 if (chan) {
1182                         /* Members of a channel have been requested */
1183                         return IRC_WHO_Channel(Client, chan, only_ops);
1184                 }
1185                 if (strcmp(Req->argv[0], "0") != 0) {
1186                         /* A mask has been given. But please note this RFC
1187                          * stupidity: "0" is same as no arguments ... */
1188                         return IRC_WHO_Mask(Client, Req->argv[0], only_ops);
1189                 }
1190         }
1191
1192         /* No channel or (valid) mask given */
1193         IRC_SetPenalty(Client, 2);
1194         return IRC_WHO_Mask(Client, NULL, only_ops);
1195 } /* IRC_WHO */
1196
1197 /**
1198  * Handler for the IRC "WHOIS" command.
1199  *
1200  * @param Client The client from which this command has been received.
1201  * @param Req Request structure with prefix and all parameters.
1202  * @return CONNECTED or DISCONNECTED.
1203  */
1204 GLOBAL bool
1205 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
1206 {
1207         CLIENT *from, *target, *c;
1208         unsigned int match_count = 0, found = 0;
1209         bool has_wildcards, is_remote;
1210         bool got_wildcard = false;
1211         char mask[COMMAND_LEN], *query;
1212
1213         assert( Client != NULL );
1214         assert( Req != NULL );
1215
1216         IRC_SetPenalty(Client, 1);
1217
1218         /* Bad number of parameters? */
1219         if (Req->argc < 1 || Req->argc > 2)
1220                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1221                                           Client_ID(Client), Req->command);
1222
1223         _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
1224
1225         /* Get target server for this command */
1226         if (Req->argc > 1) {
1227                 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, Client)
1228         } else
1229                 target = Client_ThisServer();
1230
1231         assert(target != NULL);
1232
1233         /* Forward? */
1234         if (target != Client_ThisServer()) {
1235                 IRC_WriteStrClientPrefix(target, from, "WHOIS %s :%s",
1236                                          Req->argv[0], Req->argv[1]);
1237                 return CONNECTED;
1238         }
1239
1240         is_remote = Client_Conn(from) < 0;
1241         strlcpy(mask, Req->argv[Req->argc - 1], sizeof(mask));
1242         for (query = strtok(ngt_LowerStr(mask), ",");
1243                         query && found < 3;
1244                         query = strtok(NULL, ","), found++)
1245         {
1246                 has_wildcards = query[strcspn(query, "*?")] != 0;
1247                 /*
1248                  * follows ircd 2.10 implementation:
1249                  *  - handle up to 3 targets
1250                  *  - no wildcards for remote clients
1251                  *  - only one wildcard target per local client
1252                  *
1253                  *  Also, at most MAX_RPL_WHOIS matches are returned.
1254                  */
1255                 if (!has_wildcards || is_remote) {
1256                         c = Client_Search(query);
1257                         if (c && (Client_Type(c) == CLIENT_USER
1258                                   || Client_Type(c) == CLIENT_SERVICE)) {
1259                                 if (!IRC_WHOIS_SendReply(Client, from, c))
1260                                         return DISCONNECTED;
1261                         } else {
1262                                 if (!IRC_WriteStrClient(Client,
1263                                                         ERR_NOSUCHNICK_MSG,
1264                                                         Client_ID(Client),
1265                                                         query))
1266                                         return DISCONNECTED;
1267                         }
1268                         continue;
1269                 }
1270                 if (got_wildcard) {
1271                         /* we already handled one wildcard query */
1272                         if (!IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1273                              Client_ID(Client), query))
1274                                 return DISCONNECTED;
1275                         continue;
1276                 }
1277                 got_wildcard = true;
1278                 IRC_SetPenalty(Client, 3);
1279
1280                 for (c = Client_First(); c; c = Client_Next(c)) {
1281                         if (IRC_CheckListTooBig(Client, match_count,
1282                                             MAX_RPL_WHOIS, "WHOIS"))
1283                                 break;
1284
1285                         if (Client_Type(c) != CLIENT_USER)
1286                                 continue;
1287                         if (!MatchCaseInsensitive(query, Client_ID(c)))
1288                                 continue;
1289                         if (!IRC_WHOIS_SendReply(Client, from, c))
1290                                 return DISCONNECTED;
1291
1292                         match_count++;
1293                 }
1294
1295                 if (match_count == 0)
1296                         IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1297                                            Client_ID(Client),
1298                                            Req->argv[Req->argc - 1]);
1299         }
1300         return IRC_WriteStrClient(from, RPL_ENDOFWHOIS_MSG,
1301                                   Client_ID(from), Req->argv[Req->argc - 1]);
1302 } /* IRC_WHOIS */
1303
1304 /**
1305  * Handler for the IRC "WHOWAS" command.
1306  *
1307  * @param Client The client from which this command has been received.
1308  * @param Req Request structure with prefix and all parameters.
1309  * @return CONNECTED or DISCONNECTED.
1310  */
1311 GLOBAL bool
1312 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1313 {
1314         CLIENT *target, *prefix;
1315         WHOWAS *whowas;
1316         char tok_buf[COMMAND_LEN];
1317         int max, last, count, i, nc;
1318         const char *nick;
1319
1320         assert( Client != NULL );
1321         assert( Req != NULL );
1322
1323         /* Do not reveal any info on disconnected users? */
1324         if (Conf_MorePrivacy)
1325                 return CONNECTED;
1326
1327         /* Wrong number of parameters? */
1328         if (Req->argc < 1)
1329                 return IRC_WriteStrClient(Client, ERR_NONICKNAMEGIVEN_MSG,
1330                                           Client_ID(Client));
1331
1332         _IRC_ARGC_LE_OR_RETURN_(Client, Req, 3)
1333         _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
1334         _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 2, prefix)
1335
1336         /* Forward? */
1337         if (target != Client_ThisServer()) {
1338                 IRC_WriteStrClientPrefix(target, prefix, "WHOWAS %s %s %s",
1339                                          Req->argv[0], Req->argv[1],
1340                                          Client_ID(target));
1341                 return CONNECTED;
1342         }
1343
1344         whowas = Client_GetWhowas( );
1345         last = Client_GetLastWhowasIndex( );
1346         if (last < 0)
1347                 last = 0;
1348
1349         max = DEF_RPL_WHOWAS;
1350         if (Req->argc > 1) {
1351                 max = atoi(Req->argv[1]);
1352                 if (max < 1)
1353                         max = MAX_RPL_WHOWAS;
1354         }
1355
1356         /*
1357          * Break up the nick argument into a list of nicks, if applicable
1358          * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1359          */
1360         strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1361         nick = strtok(tok_buf, ",");
1362
1363         for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1364                 nc = 0;
1365                 do {
1366                         /* Used entry? */
1367                         if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1368                                 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1369                                         return DISCONNECTED;
1370                                 nc++;
1371                                 count++;
1372                         }
1373                         /* previous entry */
1374                         i--;
1375
1376                         /* "underflow", wrap around */
1377                         if (i < 0)
1378                                 i = MAX_WHOWAS - 1;
1379
1380                         if (nc && count >= max)
1381                                 break;
1382                 } while (i != last);
1383
1384                 if (nc == 0 && !IRC_WriteStrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1385                                                 Client_ID(prefix), nick))
1386                         return DISCONNECTED;
1387         }
1388         return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG,
1389                                   Client_ID(prefix), Req->argv[0]);
1390 } /* IRC_WHOWAS */
1391
1392 /**
1393  * Send LUSERS reply to a client.
1394  *
1395  * @param Client The receipient of the information.
1396  * @return CONNECTED or DISCONNECTED.
1397  */
1398 GLOBAL bool
1399 IRC_Send_LUSERS(CLIENT *Client)
1400 {
1401         unsigned long cnt;
1402 #ifndef STRICT_RFC
1403         unsigned long max;
1404 #endif
1405
1406         assert(Client != NULL);
1407
1408         /* Users, services and servers in the network */
1409         if (!IRC_WriteStrClient(Client, RPL_LUSERCLIENT_MSG, Client_ID(Client),
1410                                 Client_UserCount(), Client_ServiceCount(),
1411                                 Client_ServerCount()))
1412                 return DISCONNECTED;
1413
1414         /* Number of IRC operators */
1415         cnt = Client_OperCount( );
1416         if (cnt > 0) {
1417                 if (!IRC_WriteStrClient(Client, RPL_LUSEROP_MSG,
1418                                         Client_ID(Client), cnt))
1419                         return DISCONNECTED;
1420         }
1421
1422         /* Unknown connections */
1423         cnt = Client_UnknownCount( );
1424         if (cnt > 0) {
1425                 if (!IRC_WriteStrClient(Client, RPL_LUSERUNKNOWN_MSG,
1426                                         Client_ID(Client), cnt))
1427                         return DISCONNECTED;
1428         }
1429
1430         /* Number of created channels */
1431         if (!IRC_WriteStrClient(Client, RPL_LUSERCHANNELS_MSG,
1432                                 Client_ID(Client),
1433                                 Channel_CountVisible(Client)))
1434                 return DISCONNECTED;
1435
1436         /* Number of local users, services and servers */
1437         if (!IRC_WriteStrClient(Client, RPL_LUSERME_MSG, Client_ID(Client),
1438                                 Client_MyUserCount(), Client_MyServiceCount(),
1439                                 Client_MyServerCount()))
1440                 return DISCONNECTED;
1441
1442 #ifndef STRICT_RFC
1443         /* Maximum number of local users */
1444         cnt = Client_MyUserCount();
1445         max = Client_MyMaxUserCount();
1446         if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1447                         cnt, max, cnt, max))
1448                 return DISCONNECTED;
1449         /* Maximum number of users in the network */
1450         cnt = Client_UserCount();
1451         max = Client_MaxUserCount();
1452         if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1453                         cnt, max, cnt, max))
1454                 return DISCONNECTED;
1455         /* Connection counters */
1456         if (! IRC_WriteStrClient(Client, RPL_STATSCONN_MSG, Client_ID(Client),
1457                         Conn_CountMax(), Conn_CountAccepted()))
1458                 return DISCONNECTED;
1459 #endif
1460
1461         return CONNECTED;
1462 } /* IRC_Send_LUSERS */
1463
1464 GLOBAL bool
1465 IRC_Show_MOTD( CLIENT *Client )
1466 {
1467         const char *line;
1468         size_t len_tot, len_str;
1469
1470         assert( Client != NULL );
1471
1472         len_tot = array_bytes(&Conf_Motd);
1473         if (len_tot == 0 && !Conn_UsesSSL(Client_Conn(Client)))
1474                 return IRC_WriteStrClient(Client, ERR_NOMOTD_MSG, Client_ID(Client));
1475
1476         if (!IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG, Client_ID(Client),
1477                                 Client_ID(Client_ThisServer())))
1478                 return DISCONNECTED;
1479
1480         line = array_start(&Conf_Motd);
1481         while (len_tot > 0) {
1482                 len_str = strlen(line) + 1;
1483
1484                 assert(len_tot >= len_str);
1485                 len_tot -= len_str;
1486
1487                 if (!IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID(Client), line))
1488                         return DISCONNECTED;
1489                 line += len_str;
1490         }
1491
1492         if (!Show_MOTD_SSLInfo(Client))
1493                 return DISCONNECTED;
1494
1495         if (!IRC_WriteStrClient(Client, RPL_ENDOFMOTD_MSG, Client_ID(Client)))
1496                 return DISCONNECTED;
1497
1498         if (*Conf_CloakHost)
1499                 return IRC_WriteStrClient(Client, RPL_HOSTHIDDEN_MSG,
1500                                           Client_ID(Client),
1501                                           Client_Hostname(Client));
1502
1503         return CONNECTED;
1504 } /* IRC_Show_MOTD */
1505
1506 /**
1507  * Send NAMES reply for a specific client and channel.
1508  *
1509  * @param Client The client requesting the NAMES information.
1510  * @param Chan The channel
1511  * @return CONNECTED or DISCONNECTED.
1512  */
1513 GLOBAL bool
1514 IRC_Send_NAMES(CLIENT * Client, CHANNEL * Chan)
1515 {
1516         bool is_visible, is_member;
1517         char str[COMMAND_LEN];
1518         CL2CHAN *cl2chan;
1519         CLIENT *cl;
1520
1521         assert(Client != NULL);
1522         assert(Chan != NULL);
1523
1524         if (Channel_IsMemberOf(Chan, Client))
1525                 is_member = true;
1526         else
1527                 is_member = false;
1528
1529         /* Do not print info on channel memberships to anyone that is not member? */
1530         if (Conf_MorePrivacy && !is_member)
1531                 return CONNECTED;
1532
1533         /* Secret channel? */
1534         if (!is_member && Channel_HasMode(Chan, 's'))
1535                 return CONNECTED;
1536
1537         snprintf(str, sizeof(str), RPL_NAMREPLY_MSG, Client_ID(Client), "=",
1538                  Channel_Name(Chan));
1539         cl2chan = Channel_FirstMember(Chan);
1540         while (cl2chan) {
1541                 cl = Channel_GetClient(cl2chan);
1542
1543                 if (Client_HasMode(cl, 'i'))
1544                         is_visible = false;
1545                 else
1546                         is_visible = true;
1547
1548                 if (is_member || is_visible) {
1549                         if (str[strlen(str) - 1] != ':')
1550                                 strlcat(str, " ", sizeof(str));
1551
1552                         who_flags_qualifier(Client, Channel_UserModes(Chan, cl),
1553                                             str, sizeof(str));
1554                         strlcat(str, Client_ID(cl), sizeof(str));
1555
1556                         if (strlen(str) > (COMMAND_LEN - CLIENT_NICK_LEN - 4)) {
1557                                 if (!IRC_WriteStrClient(Client, "%s", str))
1558                                         return DISCONNECTED;
1559                                 snprintf(str, sizeof(str), RPL_NAMREPLY_MSG,
1560                                          Client_ID(Client), "=",
1561                                          Channel_Name(Chan));
1562                         }
1563                 }
1564
1565                 cl2chan = Channel_NextMember(Chan, cl2chan);
1566         }
1567         if (str[strlen(str) - 1] != ':') {
1568                 if (!IRC_WriteStrClient(Client, "%s", str))
1569                         return DISCONNECTED;
1570         }
1571
1572         return CONNECTED;
1573 } /* IRC_Send_NAMES */
1574
1575 /**
1576  * Send the ISUPPORT numeric (005).
1577  * This numeric indicates the features that are supported by this server.
1578  * See <http://www.irc.org/tech_docs/005.html> for details.
1579  */
1580 GLOBAL bool
1581 IRC_Send_ISUPPORT(CLIENT * Client)
1582 {
1583         if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1584                                 CHANTYPES, CHANTYPES, Conf_MaxJoins))
1585                 return DISCONNECTED;
1586         return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1587                                   CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1588                                   COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1589                                   COMMAND_LEN - 113, MAX_HNDL_MODES_ARG,
1590                                   MAX_HNDL_CHANNEL_LISTS);
1591 } /* IRC_Send_ISUPPORT */
1592
1593 /* -eof- */