- replaced a lot of strcat() calls with strlcat() which is more secure.
[ngircd-alex.git] / src / ngircd / irc-info.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
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  * IRC info commands
12  */
13
14
15 #include "portab.h"
16
17 static char UNUSED id[] = "$Id: irc-info.c,v 1.10 2002/12/26 16:48:14 alex Exp $";
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <errno.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 #include "ngircd.h"
26 #include "conn.h"
27 #include "client.h"
28 #include "channel.h"
29 #include "resolve.h"
30 #include "conf.h"
31 #include "defines.h"
32 #include "log.h"
33 #include "messages.h"
34 #include "tool.h"
35 #include "parse.h"
36 #include "irc-write.h"
37
38 #include "exp.h"
39 #include "irc-info.h"
40
41
42 GLOBAL BOOLEAN
43 IRC_ADMIN(CLIENT *Client, REQUEST *Req )
44 {
45         CLIENT *target, *prefix;
46
47         assert( Client != NULL );
48         assert( Req != NULL );
49
50         /* Falsche Anzahl Parameter? */
51         if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
52
53         /* Ziel suchen */
54         if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
55         else target = Client_ThisServer( );
56
57         /* Prefix ermitteln */
58         if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
59         else prefix = Client;
60         if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
61
62         /* An anderen Server weiterleiten? */
63         if( target != Client_ThisServer( ))
64         {
65                 if( ! target ) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
66
67                 /* forwarden */
68                 IRC_WriteStrClientPrefix( target, prefix, "ADMIN %s", Req->argv[0] );
69                 return CONNECTED;
70         }
71
72         /* mit Versionsinfo antworten */
73         if( ! IRC_WriteStrClient( Client, RPL_ADMINME_MSG, Client_ID( prefix ), Conf_ServerName )) return DISCONNECTED;
74         if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC1_MSG, Client_ID( prefix ), Conf_ServerAdmin1 )) return DISCONNECTED;
75         if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC2_MSG, Client_ID( prefix ), Conf_ServerAdmin2 )) return DISCONNECTED;
76         if( ! IRC_WriteStrClient( Client, RPL_ADMINEMAIL_MSG, Client_ID( prefix ), Conf_ServerAdminMail )) return DISCONNECTED;
77
78         return CONNECTED;
79 } /* IRC_ADMIN */
80
81
82 GLOBAL BOOLEAN
83 IRC_ISON( CLIENT *Client, REQUEST *Req )
84 {
85         CHAR rpl[COMMAND_LEN];
86         CLIENT *c;
87         CHAR *ptr;
88         INT i;
89
90         assert( Client != NULL );
91         assert( Req != NULL );
92
93         /* Falsche Anzahl Parameter? */
94         if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
95
96         strcpy( rpl, RPL_ISON_MSG );
97         for( i = 0; i < Req->argc; i++ )
98         {
99                 ptr = strtok( Req->argv[i], " " );
100                 while( ptr )
101                 {
102                         ngt_TrimStr( ptr );
103                         c = Client_Search( ptr );
104                         if( c && ( Client_Type( c ) == CLIENT_USER ))
105                         {
106                                 /* Dieser Nick ist "online" */
107                                 strlcat( rpl, ptr, sizeof( rpl ));
108                                 strlcat( rpl, " ", sizeof( rpl ));
109                         }
110                         ptr = strtok( NULL, " " );
111                 }
112         }
113         if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
114
115         return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
116 } /* IRC_ISON */
117
118
119 GLOBAL BOOLEAN
120 IRC_LINKS( CLIENT *Client, REQUEST *Req )
121 {
122         CLIENT *target, *from, *c;
123         CHAR *mask;
124
125         assert( Client != NULL );
126         assert( Req != NULL );
127
128         /* Falsche Anzahl Parameter? */
129         if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
130
131         /* Server-Mask ermitteln */
132         if( Req->argc > 0 ) mask = Req->argv[Req->argc - 1];
133         else mask = "*";
134
135         /* Absender ermitteln */
136         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
137         else from = Client;
138         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
139
140         /* An anderen Server forwarden? */
141         if( Req->argc == 2 )
142         {
143                 target = Client_Search( Req->argv[0] );
144                 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
145                 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LINKS %s %s", Req->argv[0], Req->argv[1] );
146         }
147
148         /* Wer ist der Absender? */
149         if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
150         else target = Client;
151         if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
152
153         c = Client_First( );
154         while( c )
155         {
156                 if( Client_Type( c ) == CLIENT_SERVER )
157                 {
158                         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;
159                 }
160                 c = Client_Next( c );
161         }
162
163         return IRC_WriteStrClient( target, RPL_ENDOFLINKS_MSG, Client_ID( target ), mask );
164 } /* IRC_LINKS */
165
166
167 GLOBAL BOOLEAN
168 IRC_LUSERS( CLIENT *Client, REQUEST *Req )
169 {
170         CLIENT *target, *from;
171
172         assert( Client != NULL );
173         assert( Req != NULL );
174
175         /* Falsche Anzahl Parameter? */
176         if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
177
178         /* Absender ermitteln */
179         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
180         else from = Client;
181         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
182
183         /* An anderen Server forwarden? */
184         if( Req->argc == 2 )
185         {
186                 target = Client_Search( Req->argv[1] );
187                 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
188                 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LUSERS %s %s", Req->argv[0], Req->argv[1] );
189         }
190
191         /* Wer ist der Absender? */
192         if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
193         else target = Client;
194         if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
195
196         IRC_Send_LUSERS( target );
197
198         return CONNECTED;
199 } /* IRC_LUSERS */
200
201
202 GLOBAL BOOLEAN
203 IRC_MOTD( CLIENT *Client, REQUEST *Req )
204 {
205         CLIENT *from, *target;
206
207         assert( Client != NULL );
208         assert( Req != NULL );
209
210         /* Falsche Anzahl Parameter? */
211         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
212
213         /* From aus Prefix ermitteln */
214         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
215         else from = Client;
216         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
217
218         if( Req->argc == 1 )
219         {
220                 /* an anderen Server forwarden */
221                 target = Client_Search( Req->argv[0] );
222                 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
223
224                 if( target != Client_ThisServer( ))
225                 {
226                         /* Ok, anderer Server ist das Ziel: forwarden */
227                         return IRC_WriteStrClientPrefix( target, from, "MOTD %s", Req->argv[0] );
228                 }
229         }
230
231         return IRC_Show_MOTD( from );
232 } /* IRC_MOTD */
233
234
235 GLOBAL BOOLEAN
236 IRC_NAMES( CLIENT *Client, REQUEST *Req )
237 {
238         CHAR rpl[COMMAND_LEN], *ptr;
239         CLIENT *target, *from, *c;
240         CHANNEL *chan;
241
242         assert( Client != NULL );
243         assert( Req != NULL );
244
245         /* Falsche Anzahl Parameter? */
246         if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
247
248         /* From aus Prefix ermitteln */
249         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
250         else from = Client;
251         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
252
253         if( Req->argc == 2 )
254         {
255                 /* an anderen Server forwarden */
256                 target = Client_Search( Req->argv[1] );
257                 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
258
259                 if( target != Client_ThisServer( ))
260                 {
261                         /* Ok, anderer Server ist das Ziel: forwarden */
262                         return IRC_WriteStrClientPrefix( target, from, "NAMES %s :%s", Req->argv[0], Req->argv[1] );
263                 }
264         }
265
266         if( Req->argc > 0 )
267         {
268                 /* bestimmte Channels durchgehen */
269                 ptr = strtok( Req->argv[0], "," );
270                 while( ptr )
271                 {
272                         chan = Channel_Search( ptr );
273                         if( chan )
274                         {
275                                 /* Namen ausgeben */
276                                 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
277                         }
278                         if( ! IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), ptr )) return DISCONNECTED;
279
280                         /* naechsten Namen ermitteln */
281                         ptr = strtok( NULL, "," );
282                 }
283                 return CONNECTED;
284         }
285
286         /* alle Channels durchgehen */
287         chan = Channel_First( );
288         while( chan )
289         {
290                 /* Namen ausgeben */
291                 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
292
293                 /* naechster Channel */
294                 chan = Channel_Next( chan );
295         }
296
297         /* Nun noch alle Clients ausgeben, die in keinem Channel sind */
298         c = Client_First( );
299         sprintf( rpl, RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
300         while( c )
301         {
302                 if(( Client_Type( c ) == CLIENT_USER ) && ( Channel_FirstChannelOf( c ) == NULL ) && ( ! strchr( Client_Modes( c ), 'i' )))
303                 {
304                         /* Okay, das ist ein User: anhaengen */
305                         if( rpl[strlen( rpl ) - 1] != ':' ) strlcat( rpl, " ", sizeof( rpl ));
306                         strlcat( rpl, Client_ID( c ), sizeof( rpl ));
307
308                         if( strlen( rpl ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
309                         {
310                                 /* Zeile wird zu lang: senden! */
311                                 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
312                                 sprintf( rpl, RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
313                         }
314                 }
315
316                 /* naechster Client */
317                 c = Client_Next( c );
318         }
319         if( rpl[strlen( rpl ) - 1] != ':')
320         {
321                 /* es wurden User gefunden */
322                 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
323         }
324
325         return IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), "*" );
326 } /* IRC_NAMES */
327
328
329 GLOBAL BOOLEAN
330 IRC_STATS( CLIENT *Client, REQUEST *Req )
331 {
332         CLIENT *from, *target, *cl;
333         CONN_ID con;
334         CHAR query;
335         COMMAND *cmd;
336
337         assert( Client != NULL );
338         assert( Req != NULL );
339
340         /* Falsche Anzahl Parameter? */
341         if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
342
343         /* From aus Prefix ermitteln */
344         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
345         else from = Client;
346         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
347
348         if( Req->argc == 2 )
349         {
350                 /* an anderen Server forwarden */
351                 target = Client_Search( Req->argv[1] );
352                 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
353
354                 if( target != Client_ThisServer( ))
355                 {
356                         /* Ok, anderer Server ist das Ziel: forwarden */
357                         return IRC_WriteStrClientPrefix( target, from, "STATS %s %s", Req->argv[0], Req->argv[1] );
358                 }
359         }
360
361         if( Req->argc > 0 ) query = Req->argv[0][0] ? Req->argv[0][0] : '*';
362         else query = '*';
363
364         switch ( query )
365         {
366                 case 'l':       /* Links */
367                 case 'L':
368                         con = Conn_First( );
369                         while( con != NONE )
370                         {
371                                 cl = Client_GetFromConn( con );
372                                 if( cl && (( Client_Type( cl ) == CLIENT_SERVER ) || ( cl == Client )))
373                                 {
374                                         /* Server link or our own connection */
375 #ifdef USE_ZLIB
376                                         if( Conn_Options( con ) & CONN_ZIP )
377                                         {
378                                                 if( ! IRC_WriteStrClient( from, RPL_STATSLINKINFOZIP_MSG, Client_ID( from ), Client_Mask( cl ), Conn_SendQ( con ), Conn_SendMsg( con ), Conn_SendBytesZip( con ), Conn_SendBytes( con ), Conn_RecvMsg( con ), Conn_RecvBytesZip( con ), Conn_RecvBytes( con ), (LONG)( time( NULL ) - Conn_StartTime( con )))) return DISCONNECTED;
379                                         }
380                                         else
381 #endif
382                                         {
383                                                 if( ! IRC_WriteStrClient( from, RPL_STATSLINKINFO_MSG, Client_ID( from ), Client_Mask( cl ), Conn_SendQ( con ), Conn_SendMsg( con ), Conn_SendBytes( con ), Conn_RecvMsg( con ), Conn_RecvBytes( con ), (LONG)( time( NULL ) - Conn_StartTime( con )))) return DISCONNECTED;
384                                         }
385                                 }
386                                 con = Conn_Next( con );
387                         }
388                         break;
389                 case 'm':       /* IRC-Befehle */
390                 case 'M':
391                         cmd = Parse_GetCommandStruct( );
392                         while( cmd->name )
393                         {
394                                 if( cmd->lcount > 0 || cmd->rcount > 0 )
395                                 {
396                                         if( ! IRC_WriteStrClient( from, RPL_STATSCOMMANDS_MSG, Client_ID( from ), cmd->name, cmd->lcount, cmd->bytes, cmd->rcount )) return DISCONNECTED;
397                                 }
398                                 cmd++;
399                         }
400                         break;
401         }
402
403         return IRC_WriteStrClient( from, RPL_ENDOFSTATS_MSG, Client_ID( from ), query );
404 } /* IRC_STATS */
405
406
407 GLOBAL BOOLEAN
408 IRC_TIME( CLIENT *Client, REQUEST *Req )
409 {
410         CLIENT *from, *target;
411         CHAR t_str[64];
412         time_t t;
413
414         assert( Client != NULL );
415         assert( Req != NULL );
416
417         /* Falsche Anzahl Parameter? */
418         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
419
420         /* From aus Prefix ermitteln */
421         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
422         else from = Client;
423         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
424
425         if( Req->argc == 1 )
426         {
427                 /* an anderen Server forwarden */
428                 target = Client_Search( Req->argv[0] );
429                 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
430
431                 if( target != Client_ThisServer( ))
432                 {
433                         /* Ok, anderer Server ist das Ziel: forwarden */
434                         return IRC_WriteStrClientPrefix( target, from, "TIME %s", Req->argv[0] );
435                 }
436         }
437
438         t = time( NULL );
439         (VOID)strftime( t_str, 60, "%A %B %d %Y -- %H:%M %z", localtime( &t ));
440         return IRC_WriteStrClient( from, RPL_TIME_MSG, Client_ID( from ), Client_ID( Client_ThisServer( )), t_str );
441 } /* IRC_TIME */
442
443
444 GLOBAL BOOLEAN
445 IRC_USERHOST( CLIENT *Client, REQUEST *Req )
446 {
447         CHAR rpl[COMMAND_LEN];
448         CLIENT *c;
449         INT max, i;
450
451         assert( Client != NULL );
452         assert( Req != NULL );
453
454         /* Falsche Anzahl Parameter? */
455         if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
456
457         if( Req->argc > 5 ) max = 5;
458         else max = Req->argc;
459
460         strcpy( rpl, RPL_USERHOST_MSG );
461         for( i = 0; i < max; i++ )
462         {
463                 c = Client_Search( Req->argv[i] );
464                 if( c && ( Client_Type( c ) == CLIENT_USER ))
465                 {
466                         /* Dieser Nick ist "online" */
467                         strlcat( rpl, Client_ID( c ), sizeof( rpl ));
468                         if( Client_HasMode( c, 'o' )) strlcat( rpl, "*", sizeof( rpl ));
469                         strlcat( rpl, "=", sizeof( rpl ));
470                         if( Client_HasMode( c, 'a' )) strlcat( rpl, "-", sizeof( rpl ));
471                         else strlcat( rpl, "+", sizeof( rpl ));
472                         strlcat( rpl, Client_User( c ), sizeof( rpl ));
473                         strlcat( rpl, "@", sizeof( rpl ));
474                         strlcat( rpl, Client_Hostname( c ), sizeof( rpl ));
475                         strlcat( rpl, " ", sizeof( rpl ));
476                 }
477         }
478         if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
479
480         return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
481 } /* IRC_USERHOST */
482
483
484 GLOBAL BOOLEAN
485 IRC_VERSION( CLIENT *Client, REQUEST *Req )
486 {
487         CLIENT *target, *prefix;
488
489         assert( Client != NULL );
490         assert( Req != NULL );
491
492         /* Falsche Anzahl Parameter? */
493         if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
494
495         /* Ziel suchen */
496         if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
497         else target = Client_ThisServer( );
498
499         /* Prefix ermitteln */
500         if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
501         else prefix = Client;
502         if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
503
504         /* An anderen Server weiterleiten? */
505         if( target != Client_ThisServer( ))
506         {
507                 if( ! target ) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
508
509                 /* forwarden */
510                 IRC_WriteStrClientPrefix( target, prefix, "VERSION %s", Req->argv[0] );
511                 return CONNECTED;
512         }
513
514         /* mit Versionsinfo antworten */
515         return IRC_WriteStrClient( Client, RPL_VERSION_MSG, Client_ID( prefix ), PACKAGE, VERSION, NGIRCd_DebugLevel, Conf_ServerName, NGIRCd_VersionAddition( ));
516 } /* IRC_VERSION */
517
518
519 GLOBAL BOOLEAN
520 IRC_WHO( CLIENT *Client, REQUEST *Req )
521 {
522         BOOLEAN ok, only_ops;
523         CHAR flags[8], *ptr;
524         CL2CHAN *cl2chan;
525         CHANNEL *chan;
526         CLIENT *c;
527
528         assert( Client != NULL );
529         assert( Req != NULL );
530
531         /* Falsche Anzahl Parameter? */
532         if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
533
534         only_ops = FALSE;
535         chan = NULL;
536
537         if( Req->argc == 2 )
538         {
539                 /* Nur OPs anzeigen? */
540                 if( strcmp( Req->argv[1], "o" ) == 0 ) only_ops = TRUE;
541 #ifdef STRICT_RFC
542                 else return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
543 #endif
544         }
545
546         if( Req->argc >= 1 )
547         {
548                 /* wurde ein Channel oder Nick-Mask angegeben? */
549                 chan = Channel_Search( Req->argv[0] );
550         }
551
552         if( chan )
553         {
554                 /* User eines Channels ausgeben */
555                 if( ! IRC_Send_WHO( Client, chan, only_ops )) return DISCONNECTED;
556         }
557
558         c = Client_First( );
559         while( c )
560         {
561                 if(( Client_Type( c ) == CLIENT_USER ) && ( ! strchr( Client_Modes( c ), 'i' )))
562                 {
563                         ok = FALSE;
564                         if( Req->argc == 0 ) ok = TRUE;
565                         else
566                         {
567                                 if( strcasecmp( Req->argv[0], Client_ID( c )) == 0 ) ok = TRUE;
568                                 else if( strcmp( Req->argv[0], "0" ) == 0 ) ok = TRUE;
569                         }
570
571                         if( ok && (( ! only_ops ) || ( strchr( Client_Modes( c ), 'o' ))))
572                         {
573                                 /* Flags zusammenbasteln */
574                                 strcpy( flags, "H" );
575                                 if( strchr( Client_Modes( c ), 'o' )) strlcat( flags, "*", sizeof( flags ));
576
577                                 /* ausgeben */
578                                 cl2chan = Channel_FirstChannelOf( c );
579                                 if( cl2chan ) ptr = Channel_Name( Channel_GetChannel( cl2chan ));
580                                 else ptr = "*";
581                                 if( ! IRC_WriteStrClient( Client, RPL_WHOREPLY_MSG, Client_ID( Client ), ptr, Client_User( c ), Client_Hostname( c ), Client_ID( Client_Introducer( c )), Client_ID( c ), flags, Client_Hops( c ), Client_Info( c ))) return DISCONNECTED;
582                         }
583                 }
584
585                 /* naechster Client */
586                 c = Client_Next( c );
587         }
588
589         if( chan ) return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), Channel_Name( chan ));
590         else if( Req->argc == 0 ) return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), "*" );
591         else return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), Req->argv[0] );
592 } /* IRC_WHO */
593
594
595 GLOBAL BOOLEAN
596 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
597 {
598         CLIENT *from, *target, *c;
599         CHAR str[LINE_LEN + 1], *ptr = NULL;
600         CL2CHAN *cl2chan;
601         CHANNEL *chan;
602
603         assert( Client != NULL );
604         assert( Req != NULL );
605
606         /* Falsche Anzahl Parameter? */
607         if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
608
609         /* Client suchen */
610         c = Client_Search( Req->argv[Req->argc - 1] );
611         if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[Req->argc - 1] );
612
613         /* Empfaenger des WHOIS suchen */
614         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
615         else from = Client;
616         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
617
618         /* Forwarden an anderen Server? */
619         if( Req->argc > 1 )
620         {
621                 /* angegebenen Ziel-Server suchen */
622                 target = Client_Search( Req->argv[1] );
623                 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
624                 ptr = Req->argv[1];
625         }
626         else target = Client_ThisServer( );
627
628         assert( target != NULL );
629
630         if(( Client_NextHop( target ) != Client_ThisServer( )) && ( Client_Type( Client_NextHop( target )) == CLIENT_SERVER )) return IRC_WriteStrClientPrefix( target, from, "WHOIS %s :%s", Req->argv[0], ptr );
631
632         /* Nick, User und Name */
633         if( ! IRC_WriteStrClient( from, RPL_WHOISUSER_MSG, Client_ID( from ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
634
635         /* Server */
636         if( ! IRC_WriteStrClient( from, RPL_WHOISSERVER_MSG, Client_ID( from ), Client_ID( c ), Client_ID( Client_Introducer( c )), Client_Info( Client_Introducer( c )))) return DISCONNECTED;
637
638         /* Channels */
639         sprintf( str, RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
640         cl2chan = Channel_FirstChannelOf( c );
641         while( cl2chan )
642         {
643                 chan = Channel_GetChannel( cl2chan );
644                 assert( chan != NULL );
645
646                 /* Channel-Name anhaengen */
647                 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
648                 if( strchr( Channel_UserModes( chan, c ), 'o' )) strlcat( str, "@", sizeof( str ));
649                 else if( strchr( Channel_UserModes( chan, c ), 'v' )) strlcat( str, "+", sizeof( str ));
650                 strlcat( str, Channel_Name( chan ), sizeof( str ));
651
652                 if( strlen( str ) > ( LINE_LEN - CHANNEL_NAME_LEN - 4 ))
653                 {
654                         /* Zeile wird zu lang: senden! */
655                         if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
656                         sprintf( str, RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
657                 }
658
659                 /* naechstes Mitglied suchen */
660                 cl2chan = Channel_NextChannelOf( c, cl2chan );
661         }
662         if( str[strlen( str ) - 1] != ':')
663         {
664                 /* Es sind noch Daten da, die gesendet werden muessen */
665                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
666         }
667
668         /* IRC-Operator? */
669         if( Client_HasMode( c, 'o' ))
670         {
671                 if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
672         }
673
674         /* Idle (nur lokale Clients) */
675         if( Client_Conn( c ) > NONE )
676         {
677                 if( ! IRC_WriteStrClient( from, RPL_WHOISIDLE_MSG, Client_ID( from ), Client_ID( c ), Conn_GetIdle( Client_Conn ( c )))) return DISCONNECTED;
678         }
679
680         /* Away? */
681         if( Client_HasMode( c, 'a' ))
682         {
683                 if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( c ), Client_Away( c ))) return DISCONNECTED;
684         }
685
686         /* End of Whois */
687         return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
688 } /* IRC_WHOIS */
689
690
691 GLOBAL BOOLEAN
692 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
693 {
694         assert( Client != NULL );
695         assert( Req != NULL );
696
697         /* Falsche Anzahl Parameter? */
698         if(( Req->argc < 1 ) || ( Req->argc > 3 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
699
700         /* ... */
701
702         return CONNECTED;
703 } /* IRC_WHOWAS */
704
705
706 GLOBAL BOOLEAN
707 IRC_Send_LUSERS( CLIENT *Client )
708 {
709         LONG cnt;
710
711         assert( Client != NULL );
712
713         /* Users, services and serevers in the network */
714         if( ! IRC_WriteStrClient( Client, RPL_LUSERCLIENT_MSG, Client_ID( Client ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
715
716         /* Number of IRC operators */
717         cnt = Client_OperCount( );
718         if( cnt > 0 )
719         {
720                 if( ! IRC_WriteStrClient( Client, RPL_LUSEROP_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
721         }
722
723         /* Unknown connections */
724         cnt = Client_UnknownCount( );
725         if( cnt > 0 )
726         {
727                 if( ! IRC_WriteStrClient( Client, RPL_LUSERUNKNOWN_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
728         }
729
730         /* Number of created channels */
731         if( ! IRC_WriteStrClient( Client, RPL_LUSERCHANNELS_MSG, Client_ID( Client ), Channel_Count( ))) return DISCONNECTED;
732
733         /* Number of local users, services and servers */
734         if( ! IRC_WriteStrClient( Client, RPL_LUSERME_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
735
736 #ifndef STRICT_RFC
737         /* Maximum number of local users */
738         if( ! IRC_WriteStrClient( Client, RPL_LOCALUSERS_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyMaxUserCount( ))) return DISCONNECTED;
739         /* Maximum number of users in the network */
740         if( ! IRC_WriteStrClient( Client, RPL_NETUSERS_MSG, Client_ID( Client ), Client_UserCount( ), Client_MaxUserCount( ))) return DISCONNECTED;
741 #endif
742         
743         return CONNECTED;
744 } /* IRC_Send_LUSERS */
745
746
747 GLOBAL BOOLEAN
748 IRC_Show_MOTD( CLIENT *Client )
749 {
750         BOOLEAN ok;
751         CHAR line[127];
752         FILE *fd;
753
754         assert( Client != NULL );
755
756         fd = fopen( Conf_MotdFile, "r" );
757         if( ! fd )
758         {
759                 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
760                 return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
761         }
762
763         IRC_WriteStrClient( Client, RPL_MOTDSTART_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )));
764         while( TRUE )
765         {
766                 if( ! fgets( line, 126, fd )) break;
767                 if( line[strlen( line ) - 1] == '\n' ) line[strlen( line ) - 1] = '\0';
768                 if( ! IRC_WriteStrClient( Client, RPL_MOTD_MSG, Client_ID( Client ), line ))
769                 {
770                         fclose( fd );
771                         return FALSE;
772                 }
773         }
774         ok = IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ) );
775
776         fclose( fd );
777
778         return ok;
779 } /* IRC_Show_MOTD */
780
781
782 GLOBAL BOOLEAN
783 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
784 {
785         BOOLEAN is_visible, is_member;
786         CHAR str[LINE_LEN + 1];
787         CL2CHAN *cl2chan;
788         CLIENT *cl;
789
790         assert( Client != NULL );
791         assert( Chan != NULL );
792
793         if( Channel_IsMemberOf( Chan, Client )) is_member = TRUE;
794         else is_member = FALSE;
795
796         /* Alle Mitglieder suchen */
797         sprintf( str, RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
798         cl2chan = Channel_FirstMember( Chan );
799         while( cl2chan )
800         {
801                 cl = Channel_GetClient( cl2chan );
802
803                 if( strchr( Client_Modes( cl ), 'i' )) is_visible = FALSE;
804                 else is_visible = TRUE;
805
806                 if( is_member || is_visible )
807                 {
808                         /* Nick anhaengen */
809                         if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
810                         if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
811                         else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
812                         strlcat( str, Client_ID( cl ), sizeof( str ));
813
814                         if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
815                         {
816                                 /* Zeile wird zu lang: senden! */
817                                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
818                                 sprintf( str, RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
819                         }
820                 }
821
822                 /* naechstes Mitglied suchen */
823                 cl2chan = Channel_NextMember( Chan, cl2chan );
824         }
825         if( str[strlen( str ) - 1] != ':')
826         {
827                 /* Es sind noch Daten da, die gesendet werden muessen */
828                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
829         }
830
831         return CONNECTED;
832 } /* IRC_Send_NAMES */
833
834
835 GLOBAL BOOLEAN
836 IRC_Send_WHO( CLIENT *Client, CHANNEL *Chan, BOOLEAN OnlyOps )
837 {
838         BOOLEAN is_visible, is_member;
839         CL2CHAN *cl2chan;
840         CHAR flags[8];
841         CLIENT *c;
842
843         assert( Client != NULL );
844         assert( Chan != NULL );
845
846         if( Channel_IsMemberOf( Chan, Client )) is_member = TRUE;
847         else is_member = FALSE;
848
849         /* Alle Mitglieder suchen */
850         cl2chan = Channel_FirstMember( Chan );
851         while( cl2chan )
852         {
853                 c = Channel_GetClient( cl2chan );
854
855                 if( strchr( Client_Modes( c ), 'i' )) is_visible = FALSE;
856                 else is_visible = TRUE;
857
858                 if( is_member || is_visible )
859                 {
860                         /* Flags zusammenbasteln */
861                         strcpy( flags, "H" );
862                         if( strchr( Client_Modes( c ), 'o' )) strlcat( flags, "*", sizeof( flags ));
863                         if( strchr( Channel_UserModes( Chan, c ), 'o' )) strlcat( flags, "@", sizeof( flags ));
864                         else if( strchr( Channel_UserModes( Chan, c ), 'v' )) strlcat( flags, "+", sizeof( flags ));
865
866                         /* ausgeben */
867                         if(( ! OnlyOps ) || ( strchr( Client_Modes( c ), 'o' )))
868                         {
869                                 if( ! IRC_WriteStrClient( Client, RPL_WHOREPLY_MSG, Client_ID( Client ), Channel_Name( Chan ), Client_User( c ), Client_Hostname( c ), Client_ID( Client_Introducer( c )), Client_ID( c ), flags, Client_Hops( c ), Client_Info( c ))) return DISCONNECTED;
870                         }
871                 }
872
873                 /* naechstes Mitglied suchen */
874                 cl2chan = Channel_NextMember( Chan, cl2chan );
875         }
876         return CONNECTED;
877 } /* IRC_Send_WHO */
878
879
880 /* -eof- */