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