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