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