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