511d2a2340c7e16f79f87dcfcf9fc2ca6864f6c8
[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.7 2002/12/18 13:07:46 alex Exp $";
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <errno.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 #include "ngircd.h"
26 #include "conn.h"
27 #include "client.h"
28 #include "channel.h"
29 #include "resolve.h"
30 #include "conf.h"
31 #include "defines.h"
32 #include "log.h"
33 #include "messages.h"
34 #include "tool.h"
35 #include "parse.h"
36 #include "irc-write.h"
37
38 #include "exp.h"
39 #include "irc-info.h"
40
41
42 GLOBAL BOOLEAN
43 IRC_ADMIN(CLIENT *Client, REQUEST *Req )
44 {
45         CLIENT *target, *prefix;
46
47         assert( Client != NULL );
48         assert( Req != NULL );
49
50         /* Falsche Anzahl Parameter? */
51         if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
52
53         /* Ziel suchen */
54         if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
55         else target = Client_ThisServer( );
56
57         /* Prefix ermitteln */
58         if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
59         else prefix = Client;
60         if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
61
62         /* An anderen Server weiterleiten? */
63         if( target != Client_ThisServer( ))
64         {
65                 if( ! target ) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
66
67                 /* forwarden */
68                 IRC_WriteStrClientPrefix( target, prefix, "ADMIN %s", Req->argv[0] );
69                 return CONNECTED;
70         }
71
72         /* mit Versionsinfo antworten */
73         if( ! IRC_WriteStrClient( Client, RPL_ADMINME_MSG, Client_ID( prefix ), Conf_ServerName )) return DISCONNECTED;
74         if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC1_MSG, Client_ID( prefix ), Conf_ServerAdmin1 )) return DISCONNECTED;
75         if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC2_MSG, Client_ID( prefix ), Conf_ServerAdmin2 )) return DISCONNECTED;
76         if( ! IRC_WriteStrClient( Client, RPL_ADMINEMAIL_MSG, Client_ID( prefix ), Conf_ServerAdminMail )) return DISCONNECTED;
77
78         return CONNECTED;
79 } /* IRC_ADMIN */
80
81
82 GLOBAL BOOLEAN
83 IRC_ISON( CLIENT *Client, REQUEST *Req )
84 {
85         CHAR rpl[COMMAND_LEN];
86         CLIENT *c;
87         CHAR *ptr;
88         INT i;
89
90         assert( Client != NULL );
91         assert( Req != NULL );
92
93         /* Falsche Anzahl Parameter? */
94         if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
95
96         strcpy( rpl, RPL_ISON_MSG );
97         for( i = 0; i < Req->argc; i++ )
98         {
99                 ptr = strtok( Req->argv[i], " " );
100                 while( ptr )
101                 {
102                         ngt_TrimStr( ptr );
103                         c = Client_Search( ptr );
104                         if( c && ( Client_Type( c ) == CLIENT_USER ))
105                         {
106                                 /* Dieser Nick ist "online" */
107                                 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( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
145                 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LINKS %s %s", Req->argv[0], Req->argv[1] );
146         }
147
148         /* Wer ist der Absender? */
149         if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
150         else target = Client;
151         if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
152
153         c = Client_First( );
154         while( c )
155         {
156                 if( Client_Type( c ) == CLIENT_SERVER )
157                 {
158                         if( ! IRC_WriteStrClient( target, RPL_LINKS_MSG, Client_ID( target ), Client_ID( c ), Client_ID( Client_TopServer( c ) ? Client_TopServer( c ) : Client_ThisServer( )), Client_Hops( c ), Client_Info( c ))) return DISCONNECTED;
159                 }
160                 c = Client_Next( c );
161         }
162
163         return IRC_WriteStrClient( target, RPL_ENDOFLINKS_MSG, Client_ID( target ), mask );
164 } /* IRC_LINKS */
165
166
167 GLOBAL BOOLEAN
168 IRC_LUSERS( CLIENT *Client, REQUEST *Req )
169 {
170         CLIENT *target, *from;
171
172         assert( Client != NULL );
173         assert( Req != NULL );
174
175         /* Falsche Anzahl Parameter? */
176         if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
177
178         /* Absender ermitteln */
179         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
180         else from = Client;
181         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
182
183         /* An anderen Server forwarden? */
184         if( Req->argc == 2 )
185         {
186                 target = Client_Search( Req->argv[1] );
187                 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
188                 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LUSERS %s %s", Req->argv[0], Req->argv[1] );
189         }
190
191         /* Wer ist der Absender? */
192         if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
193         else target = Client;
194         if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
195
196         IRC_Send_LUSERS( target );
197
198         return CONNECTED;
199 } /* IRC_LUSERS */
200
201
202 GLOBAL BOOLEAN
203 IRC_MOTD( CLIENT *Client, REQUEST *Req )
204 {
205         CLIENT *from, *target;
206
207         assert( Client != NULL );
208         assert( Req != NULL );
209
210         /* Falsche Anzahl Parameter? */
211         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
212
213         /* From aus Prefix ermitteln */
214         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
215         else from = Client;
216         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
217
218         if( Req->argc == 1 )
219         {
220                 /* an anderen Server forwarden */
221                 target = Client_Search( Req->argv[0] );
222                 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
223
224                 if( target != Client_ThisServer( ))
225                 {
226                         /* Ok, anderer Server ist das Ziel: forwarden */
227                         return IRC_WriteStrClientPrefix( target, from, "MOTD %s", Req->argv[0] );
228                 }
229         }
230
231         return IRC_Show_MOTD( from );
232 } /* IRC_MOTD */
233
234
235 GLOBAL BOOLEAN
236 IRC_NAMES( CLIENT *Client, REQUEST *Req )
237 {
238         CHAR rpl[COMMAND_LEN], *ptr;
239         CLIENT *target, *from, *c;
240         CHANNEL *chan;
241
242         assert( Client != NULL );
243         assert( Req != NULL );
244
245         /* Falsche Anzahl Parameter? */
246         if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
247
248         /* From aus Prefix ermitteln */
249         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
250         else from = Client;
251         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
252
253         if( Req->argc == 2 )
254         {
255                 /* an anderen Server forwarden */
256                 target = Client_Search( Req->argv[1] );
257                 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
258
259                 if( target != Client_ThisServer( ))
260                 {
261                         /* Ok, anderer Server ist das Ziel: forwarden */
262                         return IRC_WriteStrClientPrefix( target, from, "NAMES %s :%s", Req->argv[0], Req->argv[1] );
263                 }
264         }
265
266         if( Req->argc > 0 )
267         {
268                 /* bestimmte Channels durchgehen */
269                 ptr = strtok( Req->argv[0], "," );
270                 while( ptr )
271                 {
272                         chan = Channel_Search( ptr );
273                         if( chan )
274                         {
275                                 /* Namen ausgeben */
276                                 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
277                         }
278                         if( ! IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), ptr )) return DISCONNECTED;
279
280                         /* naechsten Namen ermitteln */
281                         ptr = strtok( NULL, "," );
282                 }
283                 return CONNECTED;
284         }
285
286         /* alle Channels durchgehen */
287         chan = Channel_First( );
288         while( chan )
289         {
290                 /* Namen ausgeben */
291                 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
292
293                 /* naechster Channel */
294                 chan = Channel_Next( chan );
295         }
296
297         /* Nun noch alle Clients ausgeben, die in keinem Channel sind */
298         c = Client_First( );
299         sprintf( rpl, RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
300         while( c )
301         {
302                 if(( Client_Type( c ) == CLIENT_USER ) && ( Channel_FirstChannelOf( c ) == NULL ) && ( ! strchr( Client_Modes( c ), 'i' )))
303                 {
304                         /* Okay, das ist ein User: anhaengen */
305                         if( rpl[strlen( rpl ) - 1] != ':' ) 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_NOSUCHNICK_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( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), 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 %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 && (( Client_Type( cl ) == CLIENT_SERVER ) || ( cl == Client )))
372                                 {
373                                         /* Server link or our own connection */
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_NOSUCHNICK_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_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
493
494         /* An anderen Server weiterleiten? */
495         if( target != Client_ThisServer( ))
496         {
497                 if( ! target ) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), 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- */