]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-info.c
fe7246bd19e9d6b436e0172aaf0b38f8ee628d7d
[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  * Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
6  * der GNU General Public License (GPL), wie von der Free Software Foundation
7  * herausgegeben, weitergeben und/oder modifizieren, entweder unter Version 2
8  * der Lizenz oder (wenn Sie es wuenschen) jeder spaeteren Version.
9  * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
10  * der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS.
11  *
12  * $Id: irc-info.c,v 1.4 2002/12/02 13:45:51 alex Exp $
13  *
14  * irc-info.c: IRC-Info-Befehle
15  */
16
17
18 #include "portab.h"
19
20 #include "imp.h"
21 #include <assert.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <string.h>
25
26 #include "ngircd.h"
27 #include "conn.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_NOSUCHSERVER_MSG, Client_ID( Client ), Req->prefix );
62
63         /* An anderen Server weiterleiten? */
64         if( target != Client_ThisServer( ))
65         {
66                 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), 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                                 strcat( rpl, ptr );
109                                 strcat( 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( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), 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( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), 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_NOSUCHSERVER_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( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), 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_NOSUCHSERVER_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( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), 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         sprintf( 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] != ':' ) strcat( rpl, " " );
307                         strcat( rpl, Client_ID( c ));
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                                 sprintf( 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
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_NOSUCHSERVER_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( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), 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", 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 )
373                                 {
374 #ifdef USE_ZLIB
375                                         if( Conn_Options( con ) & CONN_ZIP )
376                                         {
377                                                 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;
378                                         }
379                                         else
380 #endif
381                                         {
382                                                 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;
383                                         }
384                                 }
385                                 con = Conn_Next( con );
386                         }
387                         break;
388                 case 'm':       /* IRC-Befehle */
389                 case 'M':
390                         break;
391         }
392
393         return IRC_WriteStrClient( from, RPL_ENDOFSTATS_MSG, Client_ID( from ), query );
394 } /* IRC_STATS */
395
396
397 GLOBAL BOOLEAN
398 IRC_TIME( CLIENT *Client, REQUEST *Req )
399 {
400         CLIENT *from, *target;
401         CHAR t_str[64];
402         time_t t;
403
404         assert( Client != NULL );
405         assert( Req != NULL );
406
407         /* Falsche Anzahl Parameter? */
408         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
409
410         /* From aus Prefix ermitteln */
411         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
412         else from = Client;
413         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->prefix );
414
415         if( Req->argc == 1 )
416         {
417                 /* an anderen Server forwarden */
418                 target = Client_Search( Req->argv[0] );
419                 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
420
421                 if( target != Client_ThisServer( ))
422                 {
423                         /* Ok, anderer Server ist das Ziel: forwarden */
424                         return IRC_WriteStrClientPrefix( target, from, "TIME %s", Req->argv[0] );
425                 }
426         }
427
428         t = time( NULL );
429         (VOID)strftime( t_str, 60, "%A %B %d %Y -- %H:%M %z", localtime( &t ));
430         return IRC_WriteStrClient( from, RPL_TIME_MSG, Client_ID( from ), Client_ID( Client_ThisServer( )), t_str );
431 } /* IRC_TIME */
432
433
434 GLOBAL BOOLEAN
435 IRC_USERHOST( CLIENT *Client, REQUEST *Req )
436 {
437         CHAR rpl[COMMAND_LEN];
438         CLIENT *c;
439         INT max, i;
440
441         assert( Client != NULL );
442         assert( Req != NULL );
443
444         /* Falsche Anzahl Parameter? */
445         if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
446
447         if( Req->argc > 5 ) max = 5;
448         else max = Req->argc;
449
450         strcpy( rpl, RPL_USERHOST_MSG );
451         for( i = 0; i < max; i++ )
452         {
453                 c = Client_Search( Req->argv[i] );
454                 if( c && ( Client_Type( c ) == CLIENT_USER ))
455                 {
456                         /* Dieser Nick ist "online" */
457                         strcat( rpl, Client_ID( c ));
458                         if( Client_HasMode( c, 'o' )) strcat( rpl, "*" );
459                         strcat( rpl, "=" );
460                         if( Client_HasMode( c, 'a' )) strcat( rpl, "-" );
461                         else strcat( rpl, "+" );
462                         strcat( rpl, Client_User( c ));
463                         strcat( rpl, "@" );
464                         strcat( rpl, Client_Hostname( c ));
465                         strcat( rpl, " " );
466                 }
467         }
468         if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
469
470         return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
471 } /* IRC_USERHOST */
472
473
474 GLOBAL BOOLEAN
475 IRC_VERSION( CLIENT *Client, REQUEST *Req )
476 {
477         CLIENT *target, *prefix;
478
479         assert( Client != NULL );
480         assert( Req != NULL );
481
482         /* Falsche Anzahl Parameter? */
483         if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
484
485         /* Ziel suchen */
486         if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
487         else target = Client_ThisServer( );
488
489         /* Prefix ermitteln */
490         if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
491         else prefix = Client;
492         if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->prefix );
493
494         /* An anderen Server weiterleiten? */
495         if( target != Client_ThisServer( ))
496         {
497                 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
498
499                 /* forwarden */
500                 IRC_WriteStrClientPrefix( target, prefix, "VERSION %s", Req->argv[0] );
501                 return CONNECTED;
502         }
503
504         /* mit Versionsinfo antworten */
505         return IRC_WriteStrClient( Client, RPL_VERSION_MSG, Client_ID( prefix ), PACKAGE, VERSION, NGIRCd_DebugLevel, Conf_ServerName, NGIRCd_VersionAddition( ));
506 } /* IRC_VERSION */
507
508
509 GLOBAL BOOLEAN
510 IRC_WHO( CLIENT *Client, REQUEST *Req )
511 {
512         BOOLEAN ok, only_ops;
513         CHAR flags[8], *ptr;
514         CL2CHAN *cl2chan;
515         CHANNEL *chan;
516         CLIENT *c;
517
518         assert( Client != NULL );
519         assert( Req != NULL );
520
521         /* Falsche Anzahl Parameter? */
522         if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
523
524         only_ops = FALSE;
525         chan = NULL;
526
527         if( Req->argc == 2 )
528         {
529                 /* Nur OPs anzeigen? */
530                 if( strcmp( Req->argv[1], "o" ) == 0 ) only_ops = TRUE;
531 #ifdef STRICT_RFC
532                 else return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
533 #endif
534         }
535
536         if( Req->argc >= 1 )
537         {
538                 /* wurde ein Channel oder Nick-Mask angegeben? */
539                 chan = Channel_Search( Req->argv[0] );
540         }
541
542         if( chan )
543         {
544                 /* User eines Channels ausgeben */
545                 if( ! IRC_Send_WHO( Client, chan, only_ops )) return DISCONNECTED;
546         }
547
548         c = Client_First( );
549         while( c )
550         {
551                 if(( Client_Type( c ) == CLIENT_USER ) && ( ! strchr( Client_Modes( c ), 'i' )))
552                 {
553                         ok = FALSE;
554                         if( Req->argc == 0 ) ok = TRUE;
555                         else
556                         {
557                                 if( strcasecmp( Req->argv[0], Client_ID( c )) == 0 ) ok = TRUE;
558                                 else if( strcmp( Req->argv[0], "0" ) == 0 ) ok = TRUE;
559                         }
560
561                         if( ok && (( ! only_ops ) || ( strchr( Client_Modes( c ), 'o' ))))
562                         {
563                                 /* Flags zusammenbasteln */
564                                 strcpy( flags, "H" );
565                                 if( strchr( Client_Modes( c ), 'o' )) strcat( flags, "*" );
566
567                                 /* ausgeben */
568                                 cl2chan = Channel_FirstChannelOf( c );
569                                 if( cl2chan ) ptr = Channel_Name( Channel_GetChannel( cl2chan ));
570                                 else ptr = "*";
571                                 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;
572                         }
573                 }
574
575                 /* naechster Client */
576                 c = Client_Next( c );
577         }
578
579         if( chan ) return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), Channel_Name( chan ));
580         else if( Req->argc == 0 ) return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), "*" );
581         else return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), Req->argv[0] );
582 } /* IRC_WHO */
583
584
585 GLOBAL BOOLEAN
586 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
587 {
588         CLIENT *from, *target, *c;
589         CHAR str[LINE_LEN + 1], *ptr = NULL;
590         CL2CHAN *cl2chan;
591         CHANNEL *chan;
592
593         assert( Client != NULL );
594         assert( Req != NULL );
595
596         /* Falsche Anzahl Parameter? */
597         if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
598
599         /* Client suchen */
600         c = Client_Search( Req->argv[Req->argc - 1] );
601         if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[Req->argc - 1] );
602
603         /* Empfaenger des WHOIS suchen */
604         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
605         else from = Client;
606         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
607
608         /* Forwarden an anderen Server? */
609         if( Req->argc > 1 )
610         {
611                 /* angegebenen Ziel-Server suchen */
612                 target = Client_Search( Req->argv[1] );
613                 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
614                 ptr = Req->argv[1];
615         }
616         else target = Client_ThisServer( );
617
618         assert( target != NULL );
619
620         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 );
621
622         /* Nick, User und Name */
623         if( ! IRC_WriteStrClient( from, RPL_WHOISUSER_MSG, Client_ID( from ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
624
625         /* Server */
626         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;
627
628         /* Channels */
629         sprintf( str, RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
630         cl2chan = Channel_FirstChannelOf( c );
631         while( cl2chan )
632         {
633                 chan = Channel_GetChannel( cl2chan );
634                 assert( chan != NULL );
635
636                 /* Channel-Name anhaengen */
637                 if( str[strlen( str ) - 1] != ':' ) strcat( str, " " );
638                 if( strchr( Channel_UserModes( chan, c ), 'o' )) strcat( str, "@" );
639                 else if( strchr( Channel_UserModes( chan, c ), 'v' )) strcat( str, "+" );
640                 strcat( str, Channel_Name( chan ));
641
642                 if( strlen( str ) > ( LINE_LEN - CHANNEL_NAME_LEN - 4 ))
643                 {
644                         /* Zeile wird zu lang: senden! */
645                         if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
646                         sprintf( str, RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
647                 }
648
649                 /* naechstes Mitglied suchen */
650                 cl2chan = Channel_NextChannelOf( c, cl2chan );
651         }
652         if( str[strlen( str ) - 1] != ':')
653         {
654                 /* Es sind noch Daten da, die gesendet werden muessen */
655                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
656         }
657
658         /* IRC-Operator? */
659         if( Client_HasMode( c, 'o' ))
660         {
661                 if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
662         }
663
664         /* Idle (nur lokale Clients) */
665         if( Client_Conn( c ) > NONE )
666         {
667                 if( ! IRC_WriteStrClient( from, RPL_WHOISIDLE_MSG, Client_ID( from ), Client_ID( c ), Conn_GetIdle( Client_Conn ( c )))) return DISCONNECTED;
668         }
669
670         /* Away? */
671         if( Client_HasMode( c, 'a' ))
672         {
673                 if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( c ), Client_Away( c ))) return DISCONNECTED;
674         }
675
676         /* End of Whois */
677         return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
678 } /* IRC_WHOIS */
679
680
681 GLOBAL BOOLEAN
682 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
683 {
684         assert( Client != NULL );
685         assert( Req != NULL );
686
687         /* Falsche Anzahl Parameter? */
688         if(( Req->argc < 1 ) || ( Req->argc > 3 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
689
690         /* ... */
691
692         return CONNECTED;
693 } /* IRC_WHOWAS */
694
695
696 GLOBAL BOOLEAN
697 IRC_Send_LUSERS( CLIENT *Client )
698 {
699         LONG cnt;
700
701         assert( Client != NULL );
702
703         /* Users, Services und Serevr im Netz */
704         if( ! IRC_WriteStrClient( Client, RPL_LUSERCLIENT_MSG, Client_ID( Client ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
705
706         /* IRC-Operatoren im Netz */
707         cnt = Client_OperCount( );
708         if( cnt > 0 )
709         {
710                 if( ! IRC_WriteStrClient( Client, RPL_LUSEROP_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
711         }
712
713         /* Unbekannt Verbindungen */
714         cnt = Client_UnknownCount( );
715         if( cnt > 0 )
716         {
717                 if( ! IRC_WriteStrClient( Client, RPL_LUSERUNKNOWN_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
718         }
719
720         /* Channels im Netz */
721         if( ! IRC_WriteStrClient( Client, RPL_LUSERCHANNELS_MSG, Client_ID( Client ), Channel_Count( ))) return DISCONNECTED;
722
723         /* Channels im Netz */
724         if( ! IRC_WriteStrClient( Client, RPL_LUSERME_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
725
726         return CONNECTED;
727 } /* IRC_Send_LUSERS */
728
729
730 GLOBAL BOOLEAN
731 IRC_Show_MOTD( CLIENT *Client )
732 {
733         BOOLEAN ok;
734         CHAR line[127];
735         FILE *fd;
736
737         assert( Client != NULL );
738
739         fd = fopen( Conf_MotdFile, "r" );
740         if( ! fd )
741         {
742                 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
743                 return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
744         }
745
746         IRC_WriteStrClient( Client, RPL_MOTDSTART_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )));
747         while( TRUE )
748         {
749                 if( ! fgets( line, 126, fd )) break;
750                 if( line[strlen( line ) - 1] == '\n' ) line[strlen( line ) - 1] = '\0';
751                 if( ! IRC_WriteStrClient( Client, RPL_MOTD_MSG, Client_ID( Client ), line ))
752                 {
753                         fclose( fd );
754                         return FALSE;
755                 }
756         }
757         ok = IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ) );
758
759         fclose( fd );
760
761         return ok;
762 } /* IRC_Show_MOTD */
763
764
765 GLOBAL BOOLEAN
766 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
767 {
768         BOOLEAN is_visible, is_member;
769         CHAR str[LINE_LEN + 1];
770         CL2CHAN *cl2chan;
771         CLIENT *cl;
772
773         assert( Client != NULL );
774         assert( Chan != NULL );
775
776         if( Channel_IsMemberOf( Chan, Client )) is_member = TRUE;
777         else is_member = FALSE;
778
779         /* Alle Mitglieder suchen */
780         sprintf( str, RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
781         cl2chan = Channel_FirstMember( Chan );
782         while( cl2chan )
783         {
784                 cl = Channel_GetClient( cl2chan );
785
786                 if( strchr( Client_Modes( cl ), 'i' )) is_visible = FALSE;
787                 else is_visible = TRUE;
788
789                 if( is_member || is_visible )
790                 {
791                         /* Nick anhaengen */
792                         if( str[strlen( str ) - 1] != ':' ) strcat( str, " " );
793                         if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strcat( str, "@" );
794                         else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strcat( str, "+" );
795                         strcat( str, Client_ID( cl ));
796
797                         if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
798                         {
799                                 /* Zeile wird zu lang: senden! */
800                                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
801                                 sprintf( str, RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
802                         }
803                 }
804
805                 /* naechstes Mitglied suchen */
806                 cl2chan = Channel_NextMember( Chan, cl2chan );
807         }
808         if( str[strlen( str ) - 1] != ':')
809         {
810                 /* Es sind noch Daten da, die gesendet werden muessen */
811                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
812         }
813
814         return CONNECTED;
815 } /* IRC_Send_NAMES */
816
817
818 GLOBAL BOOLEAN
819 IRC_Send_WHO( CLIENT *Client, CHANNEL *Chan, BOOLEAN OnlyOps )
820 {
821         BOOLEAN is_visible, is_member;
822         CL2CHAN *cl2chan;
823         CHAR flags[8];
824         CLIENT *c;
825
826         assert( Client != NULL );
827         assert( Chan != NULL );
828
829         if( Channel_IsMemberOf( Chan, Client )) is_member = TRUE;
830         else is_member = FALSE;
831
832         /* Alle Mitglieder suchen */
833         cl2chan = Channel_FirstMember( Chan );
834         while( cl2chan )
835         {
836                 c = Channel_GetClient( cl2chan );
837
838                 if( strchr( Client_Modes( c ), 'i' )) is_visible = FALSE;
839                 else is_visible = TRUE;
840
841                 if( is_member || is_visible )
842                 {
843                         /* Flags zusammenbasteln */
844                         strcpy( flags, "H" );
845                         if( strchr( Client_Modes( c ), 'o' )) strcat( flags, "*" );
846                         if( strchr( Channel_UserModes( Chan, c ), 'o' )) strcat( flags, "@" );
847                         else if( strchr( Channel_UserModes( Chan, c ), 'v' )) strcat( flags, "+" );
848
849                         /* ausgeben */
850                         if(( ! OnlyOps ) || ( strchr( Client_Modes( c ), 'o' )))
851                         {
852                                 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;
853                         }
854                 }
855
856                 /* naechstes Mitglied suchen */
857                 cl2chan = Channel_NextMember( Chan, cl2chan );
858         }
859         return CONNECTED;
860 } /* IRC_Send_WHO */
861
862
863 /* -eof- */