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