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