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