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