]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-info.c
Move the IRC_SetPenalty() call after the asserts
[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_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         assert(Client != NULL);
722         assert(Req != NULL);
723
724         IRC_SetPenalty(Client, 1);
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_WriteErrClient(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         return IRC_WriteErrClient(Client, ERR_SUMMONDISABLED_MSG,
1001                                   Client_ID(Client));
1002 } /* IRC_SUMMON */
1003
1004 /**
1005  * Handler for the IRC command "TIME".
1006  *
1007  * @param Client The client from which this command has been received.
1008  * @param Req Request structure with prefix and all parameters.
1009  * @return CONNECTED or DISCONNECTED.
1010  */
1011 GLOBAL bool
1012 IRC_TIME( CLIENT *Client, REQUEST *Req )
1013 {
1014         CLIENT *from, *target;
1015         char t_str[64];
1016         time_t t;
1017
1018         assert(Client != NULL);
1019         assert(Req != NULL);
1020
1021         IRC_SetPenalty(Client, 1);
1022
1023         _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
1024         _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
1025         _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
1026
1027         /* Forward? */
1028         if (target != Client_ThisServer()) {
1029                 IRC_WriteStrClientPrefix(target, from, "TIME %s",
1030                                          Client_ID(target));
1031                 return CONNECTED;
1032         }
1033
1034         t = time( NULL );
1035         (void)strftime(t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime(&t));
1036         return IRC_WriteStrClient(from, RPL_TIME_MSG, Client_ID(from),
1037                                   Client_ID(Client_ThisServer()), t_str);
1038 } /* IRC_TIME */
1039
1040 /**
1041  * Handler for the IRC command "USERHOST".
1042  *
1043  * @param Client The client from which this command has been received.
1044  * @param Req Request structure with prefix and all parameters.
1045  * @return CONNECTED or DISCONNECTED.
1046  */
1047 GLOBAL bool
1048 IRC_USERHOST(CLIENT *Client, REQUEST *Req)
1049 {
1050         char rpl[COMMAND_LEN];
1051         CLIENT *c;
1052         int max, i;
1053
1054         assert(Client != NULL);
1055         assert(Req != NULL);
1056
1057         IRC_SetPenalty(Client, 1);
1058
1059         _IRC_ARGC_GE_OR_RETURN_(Client, Req, 1)
1060
1061         if (Req->argc > 5)
1062                 max = 5;
1063         else
1064                 max = Req->argc;
1065
1066         strlcpy(rpl, RPL_USERHOST_MSG, sizeof rpl);
1067         for (i = 0; i < max; i++) {
1068                 c = Client_Search(Req->argv[i]);
1069                 if (c && (Client_Type(c) == CLIENT_USER)) {
1070                         /* This Nick is "online" */
1071                         strlcat(rpl, Client_ID(c), sizeof(rpl));
1072                         if (Client_HasMode(c, 'o'))
1073                                 strlcat(rpl, "*", sizeof(rpl));
1074                         strlcat(rpl, "=", sizeof(rpl));
1075                         if (Client_HasMode(c, 'a'))
1076                                 strlcat(rpl, "-", sizeof(rpl));
1077                         else
1078                                 strlcat(rpl, "+", sizeof(rpl));
1079                         strlcat(rpl, Client_User(c), sizeof(rpl));
1080                         strlcat(rpl, "@", sizeof(rpl));
1081                         strlcat(rpl, Client_HostnameDisplayed(c), sizeof(rpl));
1082                         strlcat(rpl, " ", sizeof(rpl));
1083                 }
1084         }
1085         ngt_TrimLastChr(rpl, ' ');
1086
1087         return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
1088 } /* IRC_USERHOST */
1089
1090 /**
1091  * Handler for the IRC command "USERS".
1092  *
1093  * @param Client The client from which this command has been received.
1094  * @param Req Request structure with prefix and all parameters.
1095  * @return CONNECTED or DISCONNECTED.
1096  */
1097 GLOBAL bool
1098 IRC_USERS(CLIENT * Client, UNUSED REQUEST * Req)
1099 {
1100         assert(Client != NULL);
1101
1102         return IRC_WriteErrClient(Client, ERR_USERSDISABLED_MSG,
1103                                   Client_ID(Client));
1104 } /* IRC_USERS */
1105
1106 /**
1107  * Handler for the IRC command "VERSION".
1108  *
1109  * @param Client The client from which this command has been received.
1110  * @param Req Request structure with prefix and all parameters.
1111  * @return CONNECTED or DISCONNECTED.
1112  */
1113 GLOBAL bool
1114 IRC_VERSION( CLIENT *Client, REQUEST *Req )
1115 {
1116         CLIENT *target, *prefix;
1117
1118         assert( Client != NULL );
1119         assert( Req != NULL );
1120
1121         IRC_SetPenalty(Client, 1);
1122
1123         _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
1124         _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
1125         _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
1126
1127         /* Forward? */
1128         if (target != Client_ThisServer()) {
1129                 IRC_WriteStrClientPrefix(target, prefix, "VERSION %s",
1130                                          Client_ID(target));
1131                 return CONNECTED;
1132         }
1133
1134         /* send version information */
1135         return IRC_WriteStrClient(Client, RPL_VERSION_MSG, Client_ID(prefix),
1136                                   PACKAGE_NAME, PACKAGE_VERSION,
1137                                   NGIRCd_DebugLevel, Conf_ServerName,
1138                                   NGIRCd_VersionAddition);
1139 } /* IRC_VERSION */
1140
1141 /**
1142  * Handler for the IRC "WHO" command.
1143  *
1144  * @param Client The client from which this command has been received.
1145  * @param Req Request structure with prefix and all parameters.
1146  * @return CONNECTED or DISCONNECTED.
1147  */
1148 GLOBAL bool
1149 IRC_WHO(CLIENT *Client, REQUEST *Req)
1150 {
1151         bool only_ops;
1152         CHANNEL *chan;
1153
1154         assert (Client != NULL);
1155         assert (Req != NULL);
1156
1157         IRC_SetPenalty(Client, 1);
1158
1159         _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
1160
1161         only_ops = false;
1162         if (Req->argc == 2) {
1163                 if (strcmp(Req->argv[1], "o") == 0)
1164                         only_ops = true;
1165 #ifdef STRICT_RFC
1166                 else
1167                         return IRC_WriteErrClient(Client,
1168                                                   ERR_NEEDMOREPARAMS_MSG,
1169                                                   Client_ID(Client),
1170                                                   Req->command);
1171 #endif
1172         }
1173
1174         if (Req->argc >= 1) {
1175                 /* Channel or mask given */
1176                 chan = Channel_Search(Req->argv[0]);
1177                 if (chan) {
1178                         /* Members of a channel have been requested */
1179                         return IRC_WHO_Channel(Client, chan, only_ops);
1180                 }
1181                 if (strcmp(Req->argv[0], "0") != 0) {
1182                         /* A mask has been given. But please note this RFC
1183                          * stupidity: "0" is same as no arguments ... */
1184                         return IRC_WHO_Mask(Client, Req->argv[0], only_ops);
1185                 }
1186         }
1187
1188         /* No channel or (valid) mask given */
1189         IRC_SetPenalty(Client, 2);
1190         return IRC_WHO_Mask(Client, NULL, only_ops);
1191 } /* IRC_WHO */
1192
1193 /**
1194  * Handler for the IRC "WHOIS" command.
1195  *
1196  * @param Client The client from which this command has been received.
1197  * @param Req Request structure with prefix and all parameters.
1198  * @return CONNECTED or DISCONNECTED.
1199  */
1200 GLOBAL bool
1201 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
1202 {
1203         CLIENT *from, *target, *c;
1204         unsigned int match_count = 0, found = 0;
1205         bool has_wildcards, is_remote;
1206         bool got_wildcard = false;
1207         char mask[COMMAND_LEN], *query;
1208
1209         assert( Client != NULL );
1210         assert( Req != NULL );
1211
1212         IRC_SetPenalty(Client, 1);
1213
1214         /* Wrong number of parameters? */
1215         if (Req->argc < 1)
1216                 return IRC_WriteErrClient(Client, ERR_NONICKNAMEGIVEN_MSG,
1217                                           Client_ID(Client));
1218
1219         _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
1220         _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
1221
1222         /* Get target server for this command */
1223         if (Req->argc > 1) {
1224                 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, Client)
1225         } else
1226                 target = Client_ThisServer();
1227
1228         assert(target != NULL);
1229
1230         /* Forward? */
1231         if (target != Client_ThisServer()) {
1232                 IRC_WriteStrClientPrefix(target, from, "WHOIS %s :%s",
1233                                          Req->argv[0], Req->argv[1]);
1234                 return CONNECTED;
1235         }
1236
1237         is_remote = Client_Conn(from) < 0;
1238         strlcpy(mask, Req->argv[Req->argc - 1], sizeof(mask));
1239         for (query = strtok(ngt_LowerStr(mask), ",");
1240                         query && found < 3;
1241                         query = strtok(NULL, ","), found++)
1242         {
1243                 has_wildcards = query[strcspn(query, "*?")] != 0;
1244                 /*
1245                  * follows ircd 2.10 implementation:
1246                  *  - handle up to 3 targets
1247                  *  - no wildcards for remote clients
1248                  *  - only one wildcard target per local client
1249                  *
1250                  *  Also, at most MAX_RPL_WHOIS matches are returned.
1251                  */
1252                 if (!has_wildcards || is_remote) {
1253                         c = Client_Search(query);
1254                         if (c && (Client_Type(c) == CLIENT_USER
1255                                   || Client_Type(c) == CLIENT_SERVICE)) {
1256                                 if (!IRC_WHOIS_SendReply(Client, from, c))
1257                                         return DISCONNECTED;
1258                         } else {
1259                                 if (!IRC_WriteErrClient(Client,
1260                                                         ERR_NOSUCHNICK_MSG,
1261                                                         Client_ID(Client),
1262                                                         query))
1263                                         return DISCONNECTED;
1264                         }
1265                         continue;
1266                 }
1267                 if (got_wildcard) {
1268                         /* we already handled one wildcard query */
1269                         if (!IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
1270                              Client_ID(Client), query))
1271                                 return DISCONNECTED;
1272                         continue;
1273                 }
1274                 got_wildcard = true;
1275                 IRC_SetPenalty(Client, 3);
1276
1277                 for (c = Client_First(); c; c = Client_Next(c)) {
1278                         if (IRC_CheckListTooBig(Client, match_count,
1279                                             MAX_RPL_WHOIS, "WHOIS"))
1280                                 break;
1281
1282                         if (Client_Type(c) != CLIENT_USER)
1283                                 continue;
1284                         if (!MatchCaseInsensitive(query, Client_ID(c)))
1285                                 continue;
1286                         if (!IRC_WHOIS_SendReply(Client, from, c))
1287                                 return DISCONNECTED;
1288
1289                         match_count++;
1290                 }
1291
1292                 if (match_count == 0)
1293                         IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
1294                                            Client_ID(Client),
1295                                            Req->argv[Req->argc - 1]);
1296         }
1297         return IRC_WriteStrClient(from, RPL_ENDOFWHOIS_MSG,
1298                                   Client_ID(from), Req->argv[Req->argc - 1]);
1299 } /* IRC_WHOIS */
1300
1301 /**
1302  * Handler for the IRC "WHOWAS" command.
1303  *
1304  * @param Client The client from which this command has been received.
1305  * @param Req Request structure with prefix and all parameters.
1306  * @return CONNECTED or DISCONNECTED.
1307  */
1308 GLOBAL bool
1309 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1310 {
1311         CLIENT *target, *prefix;
1312         WHOWAS *whowas;
1313         char tok_buf[COMMAND_LEN];
1314         int max, last, count, i, nc;
1315         const char *nick;
1316
1317         assert( Client != NULL );
1318         assert( Req != NULL );
1319
1320         /* Do not reveal any info on disconnected users? */
1321         if (Conf_MorePrivacy)
1322                 return CONNECTED;
1323
1324         /* Wrong number of parameters? */
1325         if (Req->argc < 1)
1326                 return IRC_WriteErrClient(Client, ERR_NONICKNAMEGIVEN_MSG,
1327                                           Client_ID(Client));
1328
1329         _IRC_ARGC_LE_OR_RETURN_(Client, Req, 3)
1330         _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
1331         _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 2, prefix)
1332
1333         /* Forward? */
1334         if (target != Client_ThisServer()) {
1335                 IRC_WriteStrClientPrefix(target, prefix, "WHOWAS %s %s %s",
1336                                          Req->argv[0], Req->argv[1],
1337                                          Client_ID(target));
1338                 return CONNECTED;
1339         }
1340
1341         whowas = Client_GetWhowas( );
1342         last = Client_GetLastWhowasIndex( );
1343         if (last < 0)
1344                 last = 0;
1345
1346         max = DEF_RPL_WHOWAS;
1347         if (Req->argc > 1) {
1348                 max = atoi(Req->argv[1]);
1349                 if (max < 1)
1350                         max = MAX_RPL_WHOWAS;
1351         }
1352
1353         /*
1354          * Break up the nick argument into a list of nicks, if applicable
1355          * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1356          */
1357         strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1358         nick = strtok(tok_buf, ",");
1359
1360         for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1361                 nc = 0;
1362                 do {
1363                         /* Used entry? */
1364                         if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1365                                 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1366                                         return DISCONNECTED;
1367                                 nc++;
1368                                 count++;
1369                         }
1370                         /* previous entry */
1371                         i--;
1372
1373                         /* "underflow", wrap around */
1374                         if (i < 0)
1375                                 i = MAX_WHOWAS - 1;
1376
1377                         if (nc && count >= max)
1378                                 break;
1379                 } while (i != last);
1380
1381                 if (nc == 0 && !IRC_WriteErrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1382                                                 Client_ID(prefix), nick))
1383                         return DISCONNECTED;
1384         }
1385         return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG,
1386                                   Client_ID(prefix), Req->argv[0]);
1387 } /* IRC_WHOWAS */
1388
1389 /**
1390  * Send LUSERS reply to a client.
1391  *
1392  * @param Client The receipient of the information.
1393  * @return CONNECTED or DISCONNECTED.
1394  */
1395 GLOBAL bool
1396 IRC_Send_LUSERS(CLIENT *Client)
1397 {
1398         unsigned long cnt;
1399 #ifndef STRICT_RFC
1400         unsigned long max;
1401 #endif
1402
1403         assert(Client != NULL);
1404
1405         /* Users, services and servers in the network */
1406         if (!IRC_WriteStrClient(Client, RPL_LUSERCLIENT_MSG, Client_ID(Client),
1407                                 Client_UserCount(), Client_ServiceCount(),
1408                                 Client_ServerCount()))
1409                 return DISCONNECTED;
1410
1411         /* Number of IRC operators */
1412         cnt = Client_OperCount( );
1413         if (cnt > 0) {
1414                 if (!IRC_WriteStrClient(Client, RPL_LUSEROP_MSG,
1415                                         Client_ID(Client), cnt))
1416                         return DISCONNECTED;
1417         }
1418
1419         /* Unknown connections */
1420         cnt = Client_UnknownCount( );
1421         if (cnt > 0) {
1422                 if (!IRC_WriteStrClient(Client, RPL_LUSERUNKNOWN_MSG,
1423                                         Client_ID(Client), cnt))
1424                         return DISCONNECTED;
1425         }
1426
1427         /* Number of created channels */
1428         if (!IRC_WriteStrClient(Client, RPL_LUSERCHANNELS_MSG,
1429                                 Client_ID(Client),
1430                                 Channel_CountVisible(Client)))
1431                 return DISCONNECTED;
1432
1433         /* Number of local users, services and servers */
1434         if (!IRC_WriteStrClient(Client, RPL_LUSERME_MSG, Client_ID(Client),
1435                                 Client_MyUserCount(), Client_MyServiceCount(),
1436                                 Client_MyServerCount()))
1437                 return DISCONNECTED;
1438
1439 #ifndef STRICT_RFC
1440         /* Maximum number of local users */
1441         cnt = Client_MyUserCount();
1442         max = Client_MyMaxUserCount();
1443         if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1444                         cnt, max, cnt, max))
1445                 return DISCONNECTED;
1446         /* Maximum number of users in the network */
1447         cnt = Client_UserCount();
1448         max = Client_MaxUserCount();
1449         if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1450                         cnt, max, cnt, max))
1451                 return DISCONNECTED;
1452         /* Connection counters */
1453         if (! IRC_WriteStrClient(Client, RPL_STATSCONN_MSG, Client_ID(Client),
1454                         Conn_CountMax(), Conn_CountAccepted()))
1455                 return DISCONNECTED;
1456 #endif
1457
1458         return CONNECTED;
1459 } /* IRC_Send_LUSERS */
1460
1461 GLOBAL bool
1462 IRC_Show_MOTD( CLIENT *Client )
1463 {
1464         const char *line;
1465         size_t len_tot, len_str;
1466
1467         assert( Client != NULL );
1468
1469         len_tot = array_bytes(&Conf_Motd);
1470         if (len_tot == 0 && !Conn_UsesSSL(Client_Conn(Client)))
1471                 return IRC_WriteErrClient(Client, ERR_NOMOTD_MSG, Client_ID(Client));
1472
1473         if (!IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG, Client_ID(Client),
1474                                 Client_ID(Client_ThisServer())))
1475                 return DISCONNECTED;
1476
1477         line = array_start(&Conf_Motd);
1478         while (len_tot > 0) {
1479                 len_str = strlen(line) + 1;
1480
1481                 assert(len_tot >= len_str);
1482                 len_tot -= len_str;
1483
1484                 if (!IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID(Client), line))
1485                         return DISCONNECTED;
1486                 line += len_str;
1487         }
1488
1489         if (!Show_MOTD_SSLInfo(Client))
1490                 return DISCONNECTED;
1491
1492         if (!IRC_WriteStrClient(Client, RPL_ENDOFMOTD_MSG, Client_ID(Client)))
1493                 return DISCONNECTED;
1494
1495         if (*Conf_CloakHost)
1496                 return IRC_WriteStrClient(Client, RPL_HOSTHIDDEN_MSG,
1497                                           Client_ID(Client),
1498                                           Client_Hostname(Client));
1499
1500         return CONNECTED;
1501 } /* IRC_Show_MOTD */
1502
1503 /**
1504  * Send NAMES reply for a specific client and channel.
1505  *
1506  * @param Client The client requesting the NAMES information.
1507  * @param Chan The channel
1508  * @return CONNECTED or DISCONNECTED.
1509  */
1510 GLOBAL bool
1511 IRC_Send_NAMES(CLIENT * Client, CHANNEL * Chan)
1512 {
1513         bool is_visible, is_member;
1514         char str[COMMAND_LEN];
1515         CL2CHAN *cl2chan;
1516         CLIENT *cl;
1517
1518         assert(Client != NULL);
1519         assert(Chan != NULL);
1520
1521         if (Channel_IsMemberOf(Chan, Client))
1522                 is_member = true;
1523         else
1524                 is_member = false;
1525
1526         /* Do not print info on channel memberships to anyone that is not member? */
1527         if (Conf_MorePrivacy && !is_member)
1528                 return CONNECTED;
1529
1530         /* Secret channel? */
1531         if (!is_member && Channel_HasMode(Chan, 's'))
1532                 return CONNECTED;
1533
1534         snprintf(str, sizeof(str), RPL_NAMREPLY_MSG, Client_ID(Client), "=",
1535                  Channel_Name(Chan));
1536         cl2chan = Channel_FirstMember(Chan);
1537         while (cl2chan) {
1538                 cl = Channel_GetClient(cl2chan);
1539
1540                 if (Client_HasMode(cl, 'i'))
1541                         is_visible = false;
1542                 else
1543                         is_visible = true;
1544
1545                 if (is_member || is_visible) {
1546                         if (str[strlen(str) - 1] != ':')
1547                                 strlcat(str, " ", sizeof(str));
1548
1549                         who_flags_qualifier(Client, Channel_UserModes(Chan, cl),
1550                                             str, sizeof(str));
1551                         strlcat(str, Client_ID(cl), sizeof(str));
1552
1553                         if (strlen(str) > (COMMAND_LEN - CLIENT_NICK_LEN - 4)) {
1554                                 if (!IRC_WriteStrClient(Client, "%s", str))
1555                                         return DISCONNECTED;
1556                                 snprintf(str, sizeof(str), RPL_NAMREPLY_MSG,
1557                                          Client_ID(Client), "=",
1558                                          Channel_Name(Chan));
1559                         }
1560                 }
1561
1562                 cl2chan = Channel_NextMember(Chan, cl2chan);
1563         }
1564         if (str[strlen(str) - 1] != ':') {
1565                 if (!IRC_WriteStrClient(Client, "%s", str))
1566                         return DISCONNECTED;
1567         }
1568
1569         return CONNECTED;
1570 } /* IRC_Send_NAMES */
1571
1572 /**
1573  * Send the ISUPPORT numeric (005).
1574  * This numeric indicates the features that are supported by this server.
1575  * See <http://www.irc.org/tech_docs/005.html> for details.
1576  */
1577 GLOBAL bool
1578 IRC_Send_ISUPPORT(CLIENT * Client)
1579 {
1580         if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1581                                 CHANTYPES, CHANTYPES, Conf_MaxJoins))
1582                 return DISCONNECTED;
1583         return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1584                                   CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1585                                   COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1586                                   COMMAND_LEN - 113, MAX_HNDL_MODES_ARG,
1587                                   MAX_HNDL_CHANNEL_LISTS);
1588 } /* IRC_Send_ISUPPORT */
1589
1590 /* -eof- */