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