]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-info.c
Remove unused "bool have_arg" from IRC_WHO()
[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;
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
948         if (Req->argc == 2) {
949                 if (strcmp(Req->argv[1], "o") == 0)
950                         only_ops = true;
951 #ifdef STRICT_RFC
952                 else
953                         return IRC_WriteStrClient(Client,
954                                                   ERR_NEEDMOREPARAMS_MSG,
955                                                   Client_ID(Client),
956                                                   Req->command);
957 #endif
958         }
959
960         IRC_SetPenalty(Client, 1);
961         if (Req->argc >= 1) {
962                 /* Channel or mask given */
963                 chan = Channel_Search(Req->argv[0]);
964                 if (chan) {
965                         /* Members of a channel have been requested */
966                         IRC_SetPenalty(Client, 1);
967                         return IRC_WHO_Channel(Client, chan, only_ops);
968                 }
969                 if (strcmp(Req->argv[0], "0") != 0) {
970                         /* A mask has been given. But please note this RFC
971                          * stupidity: "0" is same as no arguments ... */
972                         IRC_SetPenalty(Client, 3);
973                         return IRC_WHO_Mask(Client, Req->argv[0], only_ops);
974                 }
975         }
976
977         /* No channel or (valid) mask given */
978         IRC_SetPenalty(Client, 2);
979         return IRC_WHO_Mask(Client, NULL, only_ops);
980 } /* IRC_WHO */
981
982
983 /**
984  * Generate WHOIS reply of one actual client.
985  *
986  * @param Client        The client from which this command has been received.
987  * @param from          The client requesting the information ("originator").
988  * @param c             The client of which information should be returned.
989  * @returns             CONNECTED or DISCONNECTED.
990  */
991 static bool
992 IRC_WHOIS_SendReply(CLIENT *Client, CLIENT *from, CLIENT *c)
993 {
994         char str[LINE_LEN + 1];
995         CL2CHAN *cl2chan;
996         CHANNEL *chan;
997
998         /* Nick, user, hostname and client info */
999         if (!IRC_WriteStrClient(from, RPL_WHOISUSER_MSG, Client_ID(from),
1000                                 Client_ID(c), Client_User(c),
1001                                 Client_HostnameCloaked(c), Client_Info(c)))
1002                 return DISCONNECTED;
1003
1004         /* Server */
1005         if (!IRC_WriteStrClient(from, RPL_WHOISSERVER_MSG, Client_ID(from),
1006                                 Client_ID(c), Client_ID(Client_Introducer(c)),
1007                                 Client_Info(Client_Introducer(c))))
1008                 return DISCONNECTED;
1009
1010         /* Channels */
1011         snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
1012                  Client_ID(from), Client_ID(c));
1013         cl2chan = Channel_FirstChannelOf(c);
1014         while (cl2chan) {
1015                 chan = Channel_GetChannel(cl2chan);
1016                 assert(chan != NULL);
1017
1018                 /* next */
1019                 cl2chan = Channel_NextChannelOf(c, cl2chan);
1020
1021                 /* Secret channel? */
1022                 if (strchr(Channel_Modes(chan), 's')
1023                     && !Channel_IsMemberOf(chan, Client))
1024                         continue;
1025
1026                 /* Local channel and request is not from a user? */
1027                 if (Client_Type(Client) == CLIENT_SERVER
1028                     && Channel_IsLocal(chan))
1029                         continue;
1030
1031                 /* Concatenate channel names */
1032                 if (str[strlen(str) - 1] != ':')
1033                         strlcat(str, " ", sizeof(str));
1034
1035                 strlcat(str, who_flags_qualifier(Channel_UserModes(chan, c)),
1036                                                  sizeof(str));
1037                 strlcat(str, Channel_Name(chan), sizeof(str));
1038
1039                 if (strlen(str) > (LINE_LEN - CHANNEL_NAME_LEN - 4)) {
1040                         /* Line becomes too long: send it! */
1041                         if (!IRC_WriteStrClient(Client, "%s", str))
1042                                 return DISCONNECTED;
1043                         snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
1044                                  Client_ID(from), Client_ID(c));
1045                 }
1046         }
1047         if(str[strlen(str) - 1] != ':') {
1048                 /* There is data left to send: */
1049                 if (!IRC_WriteStrClient(Client, "%s", str))
1050                         return DISCONNECTED;
1051         }
1052
1053         /* IRC-Operator? */
1054         if (Client_HasMode(c, 'o') &&
1055                 !IRC_WriteStrClient(from, RPL_WHOISOPERATOR_MSG,
1056                                     Client_ID(from), Client_ID(c)))
1057                         return DISCONNECTED;
1058
1059         /* Connected using SSL? */
1060         if (Conn_UsesSSL(Client_Conn(c)) &&
1061                 !IRC_WriteStrClient(from, RPL_WHOISSSL_MSG,
1062                                     Client_ID(from), Client_ID(c)))
1063                         return DISCONNECTED;
1064
1065         /* Idle and signon time (local clients only!) */
1066         if (!Conf_MorePrivacy && Client_Conn(c) > NONE &&
1067                 !IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
1068                                     Client_ID(from), Client_ID(c),
1069                                     (unsigned long)Conn_GetIdle(Client_Conn(c)),
1070                                     (unsigned long)Conn_GetSignon(Client_Conn(c))))
1071                         return DISCONNECTED;
1072
1073         /* Away? */
1074         if (Client_HasMode(c, 'a') &&
1075                 !IRC_WriteStrClient(from, RPL_AWAY_MSG,
1076                                     Client_ID(from), Client_ID(c),
1077                                     Client_Away(c)))
1078                         return DISCONNECTED;
1079
1080         return IRC_WriteStrClient(from, RPL_ENDOFWHOIS_MSG,
1081                                   Client_ID(from), Client_ID(c));
1082 } /* IRC_WHOIS_SendReply */
1083
1084
1085 /**
1086  * Handler for the IRC "WHOIS" command.
1087  *
1088  * See RFC 2812, 3.6.2 "Whois query".
1089  *
1090  * @param Client        The client from which this command has been received.
1091  * @param Req           Request structure with prefix and all parameters.
1092  * @return              CONNECTED or DISCONNECTED.
1093  */
1094 GLOBAL bool
1095 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
1096 {
1097         CLIENT *from, *target, *c;
1098         unsigned int match_count = 0, found = 0;
1099         bool has_wildcards, is_remote;
1100         bool got_wildcard = false;
1101         const char *query;
1102
1103         assert( Client != NULL );
1104         assert( Req != NULL );
1105
1106         /* Bad number of parameters? */
1107         if (Req->argc < 1 || Req->argc > 2)
1108                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1109                                           Client_ID(Client), Req->command);
1110
1111         /* Search sender of the WHOIS */
1112         if (Client_Type(Client) == CLIENT_SERVER) {
1113                 from = Client_Search(Req->prefix);
1114         } else {
1115                 IRC_SetPenalty(Client, 1);
1116                 from = Client;
1117         }
1118         if (!from)
1119                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1120                                           Client_ID(Client), Req->prefix);
1121
1122         /* Get target server for this command */
1123         if (Req->argc > 1) {
1124                 /* Search the target server, which can be specified as a
1125                  * nick name on that server as well: */
1126                 target = Client_Search(Req->argv[0]);
1127                 if (!target)
1128                         return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
1129                                                 Client_ID(from), Req->argv[0]);
1130         } else
1131                 target = Client_ThisServer();
1132         assert(target != NULL);
1133
1134         /* Forward to other server? */
1135         if (Client_NextHop(target) != Client_ThisServer() &&
1136             Client_Type(Client_NextHop(target)) == CLIENT_SERVER)
1137                 return IRC_WriteStrClientPrefix(target, from,
1138                                                 "WHOIS %s :%s",
1139                                                 Req->argv[0], Req->argv[1]);
1140
1141         is_remote = Client_Conn(from) < 0;
1142         for (query = strtok(Req->argv[Req->argc - 1], ",");
1143                         query && found < 3;
1144                         query = strtok(NULL, ","), found++)
1145         {
1146                 has_wildcards = query[strcspn(query, "*?")] != 0;
1147                 /*
1148                  * follows ircd 2.10 implementation:
1149                  *  - handle up to 3 targets
1150                  *  - no wildcards for remote clients
1151                  *  - only one wildcard target per local client
1152                  *
1153                  *  also, at most ten matches are returned.
1154                  */
1155                 if (!has_wildcards || is_remote) {
1156                         c = Client_Search(query);
1157                         if (c) {
1158                                 if (!IRC_WHOIS_SendReply(Client, from, c))
1159                                         return DISCONNECTED;
1160                         } else {
1161                                 if (!IRC_WriteStrClient(Client,
1162                                                         ERR_NOSUCHNICK_MSG,
1163                                                         Client_ID(Client),
1164                                                         query))
1165                                         return DISCONNECTED;
1166                         }
1167                         continue;
1168                 }
1169                 if (got_wildcard) {
1170                         /* we already handled one wildcard query */
1171                         if (!IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1172                              Client_ID(Client), query))
1173                                 return DISCONNECTED;
1174                         continue;
1175                 }
1176                 got_wildcard = true;
1177                 IRC_SetPenalty(Client, 3);
1178
1179                 for (c = Client_First(); c && match_count < 10; c = Client_Next(c)) {
1180                         if (Client_Type(c) != CLIENT_USER)
1181                                 continue;
1182                         if (!MatchCaseInsensitive(query, Client_ID(c)))
1183                                 continue;
1184                         if (!IRC_WHOIS_SendReply(Client, from, c))
1185                                 return DISCONNECTED;
1186                         match_count++;
1187                 }
1188
1189                 if (match_count == 0)
1190                         return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1191                                 Client_ID(Client), Req->argv[Req->argc - 1]);
1192         }
1193         return CONNECTED;
1194 } /* IRC_WHOIS */
1195
1196
1197 static bool
1198 WHOWAS_EntryWrite(CLIENT *prefix, WHOWAS *entry)
1199 {
1200         char t_str[60];
1201
1202         (void)strftime(t_str, sizeof(t_str), "%a %b %d %H:%M:%S %Y",
1203                                         localtime(&entry->time));
1204
1205         if (!IRC_WriteStrClient(prefix, RPL_WHOWASUSER_MSG, Client_ID(prefix),
1206                         entry->id, entry->user, entry->host, entry->info))
1207                                 return DISCONNECTED;
1208
1209         return IRC_WriteStrClient(prefix, RPL_WHOISSERVER_MSG, Client_ID(prefix),
1210                   entry->id, entry->server, t_str);
1211 }
1212
1213 /**
1214  * IRC "WHOWAS" function.
1215  * This function implements the IRC command "WHOWHAS". It handles local
1216  * requests and request that should be forwarded to other servers.
1217  */
1218 GLOBAL bool
1219 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1220 {
1221         CLIENT *target, *prefix;
1222         WHOWAS *whowas;
1223         char tok_buf[COMMAND_LEN];
1224         int max, last, count, i, nc;
1225         const char *nick;
1226
1227         assert( Client != NULL );
1228         assert( Req != NULL );
1229
1230         /* Do not reveal any info on disconnected users? */
1231         if (Conf_MorePrivacy)
1232                 return CONNECTED;
1233
1234         /* Wrong number of parameters? */
1235         if (Req->argc > 3)
1236                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1237                                         Client_ID(Client), Req->command);
1238         if (Req->argc < 1)
1239                 return IRC_WriteStrClient(Client, ERR_NONICKNAMEGIVEN_MSG, Client_ID(Client));
1240
1241         /* Search target */
1242         if (Req->argc == 3)
1243                 target = Client_Search(Req->argv[2]);
1244         else
1245                 target = Client_ThisServer();
1246
1247         /* Get prefix */
1248         if (Client_Type(Client) == CLIENT_SERVER)
1249                 prefix = Client_Search(Req->prefix);
1250         else
1251                 prefix = Client;
1252
1253         if (!prefix)
1254                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1255                                                 Client_ID(Client), Req->prefix);
1256
1257         /* Forward to other server? */
1258         if (target != Client_ThisServer()) {
1259                 if (!target || (Client_Type(target) != CLIENT_SERVER))
1260                         return IRC_WriteStrClient(prefix, ERR_NOSUCHSERVER_MSG,
1261                                         Client_ID(prefix), Req->argv[2]);
1262
1263                 /* Forward */
1264                 IRC_WriteStrClientPrefix( target, prefix, "WHOWAS %s %s %s",
1265                                           Req->argv[0], Req->argv[1],
1266                                           Req->argv[2] );
1267                 return CONNECTED;
1268         }
1269
1270         whowas = Client_GetWhowas( );
1271         last = Client_GetLastWhowasIndex( );
1272         if (last < 0)
1273                 last = 0;
1274
1275         max = DEFAULT_WHOWAS;
1276         if (Req->argc > 1) {
1277                 max = atoi(Req->argv[1]);
1278                 if (max < 1)
1279                         max = MAX_WHOWAS;
1280         }
1281
1282         /*
1283          * Break up the nick argument into a list of nicks, if applicable
1284          * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1285          */
1286         strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1287         nick = strtok(tok_buf, ",");
1288
1289         for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1290                 nc = 0;
1291                 do {
1292                         /* Used entry? */
1293                         if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1294                                 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1295                                         return DISCONNECTED;
1296                                 nc++;
1297                                 count++;
1298                         }
1299                         /* previous entry */
1300                         i--;
1301
1302                         /* "underflow", wrap around */
1303                         if (i < 0)
1304                                 i = MAX_WHOWAS - 1;
1305
1306                         if (nc && count >= max)
1307                                 break;
1308                 } while (i != last);
1309
1310                 if (nc == 0 && !IRC_WriteStrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1311                                                 Client_ID(prefix), nick))
1312                         return DISCONNECTED;
1313         }
1314         return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG, Client_ID(prefix), Req->argv[0]);
1315 } /* IRC_WHOWAS */
1316
1317
1318 /**
1319  * Send LUSERS reply to a client.
1320  *
1321  * @param Client The receipient of the information.
1322  * @return CONNECTED or DISCONNECTED.
1323  */
1324 GLOBAL bool
1325 IRC_Send_LUSERS(CLIENT *Client)
1326 {
1327         unsigned long cnt;
1328 #ifndef STRICT_RFC
1329         unsigned long max;
1330 #endif
1331
1332         assert(Client != NULL);
1333
1334         /* Users, services and serevers in the network */
1335         if (!IRC_WriteStrClient(Client, RPL_LUSERCLIENT_MSG, Client_ID(Client),
1336                                 Client_UserCount(), Client_ServiceCount(),
1337                                 Client_ServerCount()))
1338                 return DISCONNECTED;
1339
1340         /* Number of IRC operators */
1341         cnt = Client_OperCount( );
1342         if (cnt > 0) {
1343                 if (!IRC_WriteStrClient(Client, RPL_LUSEROP_MSG,
1344                                         Client_ID(Client), cnt))
1345                         return DISCONNECTED;
1346         }
1347
1348         /* Unknown connections */
1349         cnt = Client_UnknownCount( );
1350         if (cnt > 0) {
1351                 if (!IRC_WriteStrClient(Client, RPL_LUSERUNKNOWN_MSG,
1352                                         Client_ID(Client), cnt))
1353                         return DISCONNECTED;
1354         }
1355
1356         /* Number of created channels */
1357         if (!IRC_WriteStrClient(Client, RPL_LUSERCHANNELS_MSG,
1358                                 Client_ID(Client),
1359                                 Channel_CountVisible(Client)))
1360                 return DISCONNECTED;
1361
1362         /* Number of local users, services and servers */
1363         if (!IRC_WriteStrClient(Client, RPL_LUSERME_MSG, Client_ID(Client),
1364                                 Client_MyUserCount(), Client_MyServiceCount(),
1365                                 Client_MyServerCount()))
1366                 return DISCONNECTED;
1367
1368 #ifndef STRICT_RFC
1369         /* Maximum number of local users */
1370         cnt = Client_MyUserCount();
1371         max = Client_MyMaxUserCount();
1372         if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1373                         cnt, max, cnt, max))
1374                 return DISCONNECTED;
1375         /* Maximum number of users in the network */
1376         cnt = Client_UserCount();
1377         max = Client_MaxUserCount();
1378         if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1379                         cnt, max, cnt, max))
1380                 return DISCONNECTED;
1381         /* Connection counters */
1382         if (! IRC_WriteStrClient(Client, RPL_STATSCONN_MSG, Client_ID(Client),
1383                         Conn_CountMax(), Conn_CountAccepted()))
1384                 return DISCONNECTED;
1385 #endif
1386         
1387         return CONNECTED;
1388 } /* IRC_Send_LUSERS */
1389
1390
1391 static bool
1392 Show_MOTD_Start(CLIENT *Client)
1393 {
1394         return IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG,
1395                 Client_ID( Client ), Client_ID( Client_ThisServer( )));
1396 }
1397
1398 static bool
1399 Show_MOTD_Sendline(CLIENT *Client, const char *msg)
1400 {
1401         return IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID( Client ), msg);
1402 }
1403
1404 static bool
1405 Show_MOTD_End(CLIENT *Client)
1406 {
1407         return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
1408 }
1409
1410 #ifdef SSL_SUPPORT
1411 static bool Show_MOTD_SSLInfo(CLIENT *Client)
1412 {
1413         bool ret = true;
1414         char buf[COMMAND_LEN] = "Connected using Cipher ";
1415
1416         if (!Conn_GetCipherInfo(Client_Conn(Client), buf + 23, sizeof buf - 23))
1417                 return true;
1418
1419         if (!Show_MOTD_Sendline(Client, buf))
1420                 ret = false;
1421
1422         return ret;
1423 }
1424 #else
1425 static inline bool
1426 Show_MOTD_SSLInfo(UNUSED CLIENT *c)
1427 { return true; }
1428 #endif
1429
1430 GLOBAL bool
1431 IRC_Show_MOTD( CLIENT *Client )
1432 {
1433         const char *line;
1434         size_t len_tot, len_str;
1435
1436         assert( Client != NULL );
1437
1438         len_tot = array_bytes(&Conf_Motd);
1439         if (len_tot == 0 && !Conn_UsesSSL(Client_Conn(Client)))
1440                 return IRC_WriteStrClient(Client, ERR_NOMOTD_MSG, Client_ID(Client));
1441
1442         if (!Show_MOTD_Start(Client))
1443                 return DISCONNECTED;
1444
1445         line = array_start(&Conf_Motd);
1446         while (len_tot > 0) {
1447                 len_str = strlen(line) + 1;
1448
1449                 assert(len_tot >= len_str);
1450                 len_tot -= len_str;
1451
1452                 if (!Show_MOTD_Sendline(Client, line))
1453                         return DISCONNECTED;
1454                 line += len_str;
1455         }
1456
1457         if (!Show_MOTD_SSLInfo(Client))
1458                 return DISCONNECTED;
1459         return Show_MOTD_End(Client);
1460 } /* IRC_Show_MOTD */
1461
1462
1463 GLOBAL bool
1464 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
1465 {
1466         bool is_visible, is_member;
1467         char str[LINE_LEN + 1];
1468         CL2CHAN *cl2chan;
1469         CLIENT *cl;
1470
1471         assert( Client != NULL );
1472         assert( Chan != NULL );
1473
1474         if( Channel_IsMemberOf( Chan, Client )) is_member = true;
1475         else is_member = false;
1476
1477         /* Do not print info on channel memberships to anyone that is not member? */
1478         if (Conf_MorePrivacy && !is_member)
1479                 return CONNECTED;
1480
1481         /* Secret channel? */
1482         if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
1483
1484         /* Alle Mitglieder suchen */
1485         snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1486         cl2chan = Channel_FirstMember( Chan );
1487         while( cl2chan )
1488         {
1489                 cl = Channel_GetClient( cl2chan );
1490
1491                 if( strchr( Client_Modes( cl ), 'i' )) is_visible = false;
1492                 else is_visible = true;
1493
1494                 if( is_member || is_visible )
1495                 {
1496                         /* Nick anhaengen */
1497                         if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
1498                         if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
1499                         else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
1500                         strlcat( str, Client_ID( cl ), sizeof( str ));
1501
1502                         if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
1503                         {
1504                                 /* Zeile wird zu lang: senden! */
1505                                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1506                                 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1507                         }
1508                 }
1509
1510                 /* naechstes Mitglied suchen */
1511                 cl2chan = Channel_NextMember( Chan, cl2chan );
1512         }
1513         if( str[strlen( str ) - 1] != ':')
1514         {
1515                 /* Es sind noch Daten da, die gesendet werden muessen */
1516                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1517         }
1518
1519         return CONNECTED;
1520 } /* IRC_Send_NAMES */
1521
1522
1523 /**
1524  * Send the ISUPPORT numeric (005).
1525  * This numeric indicates the features that are supported by this server.
1526  * See <http://www.irc.org/tech_docs/005.html> for details.
1527  */
1528 GLOBAL bool
1529 IRC_Send_ISUPPORT(CLIENT * Client)
1530 {
1531         if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1532                                 Conf_MaxJoins))
1533                 return DISCONNECTED;
1534         return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1535                                   CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1536                                   COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1537                                   COMMAND_LEN - 113);
1538 } /* IRC_Send_ISUPPORT */
1539
1540
1541 /* -eof- */