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