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