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