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