Blob


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 */
15 #include "portab.h"
17 static char UNUSED id[] = "$Id: irc-info.c,v 1.14 2003/01/01 13:30:35 alex Exp $";
19 #include "imp.h"
20 #include <assert.h>
21 #include <errno.h>
22 #include <stdio.h>
23 #include <string.h>
25 #include "ngircd.h"
26 #include "conn-func.h"
27 #include "conn-zip.h"
28 #include "client.h"
29 #include "channel.h"
30 #include "resolve.h"
31 #include "conf.h"
32 #include "defines.h"
33 #include "log.h"
34 #include "messages.h"
35 #include "tool.h"
36 #include "parse.h"
37 #include "irc-write.h"
39 #include "exp.h"
40 #include "irc-info.h"
43 GLOBAL BOOLEAN
44 IRC_ADMIN(CLIENT *Client, REQUEST *Req )
45 {
46 CLIENT *target, *prefix;
48 assert( Client != NULL );
49 assert( Req != NULL );
51 /* Falsche Anzahl Parameter? */
52 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
54 /* Ziel suchen */
55 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
56 else target = Client_ThisServer( );
58 /* Prefix ermitteln */
59 if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
60 else prefix = Client;
61 if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
63 /* An anderen Server weiterleiten? */
64 if( target != Client_ThisServer( ))
65 {
66 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
68 /* forwarden */
69 IRC_WriteStrClientPrefix( target, prefix, "ADMIN %s", Req->argv[0] );
70 return CONNECTED;
71 }
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;
79 return CONNECTED;
80 } /* IRC_ADMIN */
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;
91 assert( Client != NULL );
92 assert( Req != NULL );
94 /* Falsche Anzahl Parameter? */
95 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
97 strcpy( rpl, RPL_ISON_MSG );
98 for( i = 0; i < Req->argc; i++ )
99 {
100 ptr = strtok( Req->argv[i], " " );
101 while( ptr )
103 ngt_TrimStr( ptr );
104 c = Client_Search( ptr );
105 if( c && ( Client_Type( c ) == CLIENT_USER ))
107 /* Dieser Nick ist "online" */
108 strlcat( rpl, ptr, sizeof( rpl ));
109 strlcat( rpl, " ", sizeof( rpl ));
111 ptr = strtok( NULL, " " );
114 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
116 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
117 } /* IRC_ISON */
120 GLOBAL BOOLEAN
121 IRC_LINKS( CLIENT *Client, REQUEST *Req )
123 CLIENT *target, *from, *c;
124 CHAR *mask;
126 assert( Client != NULL );
127 assert( Req != NULL );
129 /* Falsche Anzahl Parameter? */
130 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
132 /* Server-Mask ermitteln */
133 if( Req->argc > 0 ) mask = Req->argv[Req->argc - 1];
134 else mask = "*";
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 );
141 /* An anderen Server forwarden? */
142 if( Req->argc == 2 )
144 target = Client_Search( Req->argv[0] );
145 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
146 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LINKS %s %s", Req->argv[0], Req->argv[1] );
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 );
154 c = Client_First( );
155 while( c )
157 if( Client_Type( c ) == CLIENT_SERVER )
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;
161 c = Client_Next( c );
164 return IRC_WriteStrClient( target, RPL_ENDOFLINKS_MSG, Client_ID( target ), mask );
165 } /* IRC_LINKS */
168 GLOBAL BOOLEAN
169 IRC_LUSERS( CLIENT *Client, REQUEST *Req )
171 CLIENT *target, *from;
173 assert( Client != NULL );
174 assert( Req != NULL );
176 /* Falsche Anzahl Parameter? */
177 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
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 );
184 /* An anderen Server forwarden? */
185 if( Req->argc == 2 )
187 target = Client_Search( Req->argv[1] );
188 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
189 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LUSERS %s %s", Req->argv[0], Req->argv[1] );
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 );
197 IRC_Send_LUSERS( target );
199 return CONNECTED;
200 } /* IRC_LUSERS */
203 GLOBAL BOOLEAN
204 IRC_MOTD( CLIENT *Client, REQUEST *Req )
206 CLIENT *from, *target;
208 assert( Client != NULL );
209 assert( Req != NULL );
211 /* Falsche Anzahl Parameter? */
212 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
214 /* From aus Prefix ermitteln */
215 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
216 else from = Client;
217 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
219 if( Req->argc == 1 )
221 /* an anderen Server forwarden */
222 target = Client_Search( Req->argv[0] );
223 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
225 if( target != Client_ThisServer( ))
227 /* Ok, anderer Server ist das Ziel: forwarden */
228 return IRC_WriteStrClientPrefix( target, from, "MOTD %s", Req->argv[0] );
232 return IRC_Show_MOTD( from );
233 } /* IRC_MOTD */
236 GLOBAL BOOLEAN
237 IRC_NAMES( CLIENT *Client, REQUEST *Req )
239 CHAR rpl[COMMAND_LEN], *ptr;
240 CLIENT *target, *from, *c;
241 CHANNEL *chan;
243 assert( Client != NULL );
244 assert( Req != NULL );
246 /* Falsche Anzahl Parameter? */
247 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
249 /* From aus Prefix ermitteln */
250 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
251 else from = Client;
252 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
254 if( Req->argc == 2 )
256 /* an anderen Server forwarden */
257 target = Client_Search( Req->argv[1] );
258 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
260 if( target != Client_ThisServer( ))
262 /* Ok, anderer Server ist das Ziel: forwarden */
263 return IRC_WriteStrClientPrefix( target, from, "NAMES %s :%s", Req->argv[0], Req->argv[1] );
267 if( Req->argc > 0 )
269 /* bestimmte Channels durchgehen */
270 ptr = strtok( Req->argv[0], "," );
271 while( ptr )
273 chan = Channel_Search( ptr );
274 if( chan )
276 /* Namen ausgeben */
277 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
279 if( ! IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), ptr )) return DISCONNECTED;
281 /* naechsten Namen ermitteln */
282 ptr = strtok( NULL, "," );
284 return CONNECTED;
287 /* alle Channels durchgehen */
288 chan = Channel_First( );
289 while( chan )
291 /* Namen ausgeben */
292 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
294 /* naechster Channel */
295 chan = Channel_Next( chan );
298 /* Nun noch alle Clients ausgeben, die in keinem Channel sind */
299 c = Client_First( );
300 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
301 while( c )
303 if(( Client_Type( c ) == CLIENT_USER ) && ( Channel_FirstChannelOf( c ) == NULL ) && ( ! strchr( Client_Modes( c ), 'i' )))
305 /* Okay, das ist ein User: anhaengen */
306 if( rpl[strlen( rpl ) - 1] != ':' ) strlcat( rpl, " ", sizeof( rpl ));
307 strlcat( rpl, Client_ID( c ), sizeof( rpl ));
309 if( strlen( rpl ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
311 /* Zeile wird zu lang: senden! */
312 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
313 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
317 /* naechster Client */
318 c = Client_Next( c );
320 if( rpl[strlen( rpl ) - 1] != ':')
322 /* es wurden User gefunden */
323 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
326 return IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), "*" );
327 } /* IRC_NAMES */
330 GLOBAL BOOLEAN
331 IRC_STATS( CLIENT *Client, REQUEST *Req )
333 CLIENT *from, *target, *cl;
334 CONN_ID con;
335 CHAR query;
336 COMMAND *cmd;
338 assert( Client != NULL );
339 assert( Req != NULL );
341 /* Falsche Anzahl Parameter? */
342 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
344 /* From aus Prefix ermitteln */
345 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
346 else from = Client;
347 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
349 if( Req->argc == 2 )
351 /* an anderen Server forwarden */
352 target = Client_Search( Req->argv[1] );
353 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
355 if( target != Client_ThisServer( ))
357 /* Ok, anderer Server ist das Ziel: forwarden */
358 return IRC_WriteStrClientPrefix( target, from, "STATS %s %s", Req->argv[0], Req->argv[1] );
362 if( Req->argc > 0 ) query = Req->argv[0][0] ? Req->argv[0][0] : '*';
363 else query = '*';
365 switch ( query )
367 case 'l': /* Links */
368 case 'L':
369 con = Conn_First( );
370 while( con != NONE )
372 cl = Client_GetFromConn( con );
373 if( cl && (( Client_Type( cl ) == CLIENT_SERVER ) || ( cl == Client )))
375 /* Server link or our own connection */
376 #ifdef USE_ZLIB
377 if( Conn_Options( con ) & CONN_ZIP )
379 if( ! IRC_WriteStrClient( from, RPL_STATSLINKINFOZIP_MSG, Client_ID( from ), Client_Mask( cl ), Conn_SendQ( con ), Conn_SendMsg( con ), Zip_SendBytes( con ), Conn_SendBytes( con ), Conn_RecvMsg( con ), Zip_RecvBytes( con ), Conn_RecvBytes( con ), (LONG)( time( NULL ) - Conn_StartTime( con )))) return DISCONNECTED;
381 else
382 #endif
384 if( ! IRC_WriteStrClient( from, RPL_STATSLINKINFO_MSG, Client_ID( from ), Client_Mask( cl ), Conn_SendQ( con ), Conn_SendMsg( con ), Conn_SendBytes( con ), Conn_RecvMsg( con ), Conn_RecvBytes( con ), (LONG)( time( NULL ) - Conn_StartTime( con )))) return DISCONNECTED;
387 con = Conn_Next( con );
389 break;
390 case 'm': /* IRC-Befehle */
391 case 'M':
392 cmd = Parse_GetCommandStruct( );
393 while( cmd->name )
395 if( cmd->lcount > 0 || cmd->rcount > 0 )
397 if( ! IRC_WriteStrClient( from, RPL_STATSCOMMANDS_MSG, Client_ID( from ), cmd->name, cmd->lcount, cmd->bytes, cmd->rcount )) return DISCONNECTED;
399 cmd++;
401 break;
404 return IRC_WriteStrClient( from, RPL_ENDOFSTATS_MSG, Client_ID( from ), query );
405 } /* IRC_STATS */
408 GLOBAL BOOLEAN
409 IRC_TIME( CLIENT *Client, REQUEST *Req )
411 CLIENT *from, *target;
412 CHAR t_str[64];
413 time_t t;
415 assert( Client != NULL );
416 assert( Req != NULL );
418 /* Falsche Anzahl Parameter? */
419 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
421 /* From aus Prefix ermitteln */
422 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
423 else from = Client;
424 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
426 if( Req->argc == 1 )
428 /* an anderen Server forwarden */
429 target = Client_Search( Req->argv[0] );
430 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
432 if( target != Client_ThisServer( ))
434 /* Ok, anderer Server ist das Ziel: forwarden */
435 return IRC_WriteStrClientPrefix( target, from, "TIME %s", Req->argv[0] );
439 t = time( NULL );
440 (VOID)strftime( t_str, 60, "%A %B %d %Y -- %H:%M %z", localtime( &t ));
441 return IRC_WriteStrClient( from, RPL_TIME_MSG, Client_ID( from ), Client_ID( Client_ThisServer( )), t_str );
442 } /* IRC_TIME */
445 GLOBAL BOOLEAN
446 IRC_USERHOST( CLIENT *Client, REQUEST *Req )
448 CHAR rpl[COMMAND_LEN];
449 CLIENT *c;
450 INT max, i;
452 assert( Client != NULL );
453 assert( Req != NULL );
455 /* Falsche Anzahl Parameter? */
456 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
458 if( Req->argc > 5 ) max = 5;
459 else max = Req->argc;
461 strcpy( rpl, RPL_USERHOST_MSG );
462 for( i = 0; i < max; i++ )
464 c = Client_Search( Req->argv[i] );
465 if( c && ( Client_Type( c ) == CLIENT_USER ))
467 /* Dieser Nick ist "online" */
468 strlcat( rpl, Client_ID( c ), sizeof( rpl ));
469 if( Client_HasMode( c, 'o' )) strlcat( rpl, "*", sizeof( rpl ));
470 strlcat( rpl, "=", sizeof( rpl ));
471 if( Client_HasMode( c, 'a' )) strlcat( rpl, "-", sizeof( rpl ));
472 else strlcat( rpl, "+", sizeof( rpl ));
473 strlcat( rpl, Client_User( c ), sizeof( rpl ));
474 strlcat( rpl, "@", sizeof( rpl ));
475 strlcat( rpl, Client_Hostname( c ), sizeof( rpl ));
476 strlcat( rpl, " ", sizeof( rpl ));
479 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
481 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
482 } /* IRC_USERHOST */
485 GLOBAL BOOLEAN
486 IRC_VERSION( CLIENT *Client, REQUEST *Req )
488 CLIENT *target, *prefix;
490 assert( Client != NULL );
491 assert( Req != NULL );
493 /* Falsche Anzahl Parameter? */
494 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
496 /* Ziel suchen */
497 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
498 else target = Client_ThisServer( );
500 /* Prefix ermitteln */
501 if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
502 else prefix = Client;
503 if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
505 /* An anderen Server weiterleiten? */
506 if( target != Client_ThisServer( ))
508 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
510 /* forwarden */
511 IRC_WriteStrClientPrefix( target, prefix, "VERSION %s", Req->argv[0] );
512 return CONNECTED;
515 /* mit Versionsinfo antworten */
516 return IRC_WriteStrClient( Client, RPL_VERSION_MSG, Client_ID( prefix ), PACKAGE, VERSION, NGIRCd_DebugLevel, Conf_ServerName, NGIRCd_VersionAddition( ));
517 } /* IRC_VERSION */
520 GLOBAL BOOLEAN
521 IRC_WHO( CLIENT *Client, REQUEST *Req )
523 BOOLEAN ok, only_ops;
524 CHAR flags[8], *ptr;
525 CL2CHAN *cl2chan;
526 CHANNEL *chan;
527 CLIENT *c;
529 assert( Client != NULL );
530 assert( Req != NULL );
532 /* Falsche Anzahl Parameter? */
533 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
535 only_ops = FALSE;
536 chan = NULL;
538 if( Req->argc == 2 )
540 /* Nur OPs anzeigen? */
541 if( strcmp( Req->argv[1], "o" ) == 0 ) only_ops = TRUE;
542 #ifdef STRICT_RFC
543 else return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
544 #endif
547 if( Req->argc >= 1 )
549 /* wurde ein Channel oder Nick-Mask angegeben? */
550 chan = Channel_Search( Req->argv[0] );
553 if( chan )
555 /* User eines Channels ausgeben */
556 if( ! IRC_Send_WHO( Client, chan, only_ops )) return DISCONNECTED;
559 c = Client_First( );
560 while( c )
562 if(( Client_Type( c ) == CLIENT_USER ) && ( ! strchr( Client_Modes( c ), 'i' )))
564 ok = FALSE;
565 if( Req->argc == 0 ) ok = TRUE;
566 else
568 if( strcasecmp( Req->argv[0], Client_ID( c )) == 0 ) ok = TRUE;
569 else if( strcmp( Req->argv[0], "0" ) == 0 ) ok = TRUE;
572 if( ok && (( ! only_ops ) || ( strchr( Client_Modes( c ), 'o' ))))
574 /* Flags zusammenbasteln */
575 strcpy( flags, "H" );
576 if( strchr( Client_Modes( c ), 'o' )) strlcat( flags, "*", sizeof( flags ));
578 /* ausgeben */
579 cl2chan = Channel_FirstChannelOf( c );
580 if( cl2chan ) ptr = Channel_Name( Channel_GetChannel( cl2chan ));
581 else ptr = "*";
582 if( ! IRC_WriteStrClient( Client, RPL_WHOREPLY_MSG, Client_ID( Client ), ptr, Client_User( c ), Client_Hostname( c ), Client_ID( Client_Introducer( c )), Client_ID( c ), flags, Client_Hops( c ), Client_Info( c ))) return DISCONNECTED;
586 /* naechster Client */
587 c = Client_Next( c );
590 if( chan ) return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), Channel_Name( chan ));
591 else if( Req->argc == 0 ) return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), "*" );
592 else return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), Req->argv[0] );
593 } /* IRC_WHO */
596 GLOBAL BOOLEAN
597 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
599 CLIENT *from, *target, *c;
600 CHAR str[LINE_LEN + 1];
601 CL2CHAN *cl2chan;
602 CHANNEL *chan;
604 assert( Client != NULL );
605 assert( Req != NULL );
607 /* Bad number of parameters? */
608 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
610 /* Search client */
611 c = Client_Search( Req->argv[Req->argc - 1] );
612 if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[Req->argc - 1] );
614 /* Search sender of the WHOIS */
615 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
616 else from = Client;
617 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
619 /* Forward to other server? */
620 if( Req->argc > 1 )
622 /* Search target server (can be specified as nick of that server!) */
623 target = Client_Search( Req->argv[0] );
624 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
626 else target = Client_ThisServer( );
628 assert( target != NULL );
630 if(( Client_NextHop( target ) != Client_ThisServer( )) && ( Client_Type( Client_NextHop( target )) == CLIENT_SERVER )) return IRC_WriteStrClientPrefix( target, from, "WHOIS %s :%s", Req->argv[0], Req->argv[1] );
632 /* Nick, user and name */
633 if( ! IRC_WriteStrClient( from, RPL_WHOISUSER_MSG, Client_ID( from ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
635 /* Server */
636 if( ! IRC_WriteStrClient( from, RPL_WHOISSERVER_MSG, Client_ID( from ), Client_ID( c ), Client_ID( Client_Introducer( c )), Client_Info( Client_Introducer( c )))) return DISCONNECTED;
638 /* Channels */
639 snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
640 cl2chan = Channel_FirstChannelOf( c );
641 while( cl2chan )
643 chan = Channel_GetChannel( cl2chan );
644 assert( chan != NULL );
646 /* Concatenate channel names */
647 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
648 if( strchr( Channel_UserModes( chan, c ), 'o' )) strlcat( str, "@", sizeof( str ));
649 else if( strchr( Channel_UserModes( chan, c ), 'v' )) strlcat( str, "+", sizeof( str ));
650 strlcat( str, Channel_Name( chan ), sizeof( str ));
652 if( strlen( str ) > ( LINE_LEN - CHANNEL_NAME_LEN - 4 ))
654 /* Line becomes too long: send it! */
655 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
656 snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
659 /* next */
660 cl2chan = Channel_NextChannelOf( c, cl2chan );
662 if( str[strlen( str ) - 1] != ':')
664 /* There is data left to send: */
665 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
668 /* IRC-Operator? */
669 if( Client_HasMode( c, 'o' ))
671 if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
674 /* Idle (only local clients) */
675 if( Client_Conn( c ) > NONE )
677 if( ! IRC_WriteStrClient( from, RPL_WHOISIDLE_MSG, Client_ID( from ), Client_ID( c ), Conn_GetIdle( Client_Conn ( c )))) return DISCONNECTED;
680 /* Away? */
681 if( Client_HasMode( c, 'a' ))
683 if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( c ), Client_Away( c ))) return DISCONNECTED;
686 /* End of Whois */
687 return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
688 } /* IRC_WHOIS */
691 GLOBAL BOOLEAN
692 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
694 assert( Client != NULL );
695 assert( Req != NULL );
697 /* Falsche Anzahl Parameter? */
698 if(( Req->argc < 1 ) || ( Req->argc > 3 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
700 /* ... */
702 return CONNECTED;
703 } /* IRC_WHOWAS */
706 GLOBAL BOOLEAN
707 IRC_Send_LUSERS( CLIENT *Client )
709 LONG cnt;
711 assert( Client != NULL );
713 /* Users, services and serevers in the network */
714 if( ! IRC_WriteStrClient( Client, RPL_LUSERCLIENT_MSG, Client_ID( Client ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
716 /* Number of IRC operators */
717 cnt = Client_OperCount( );
718 if( cnt > 0 )
720 if( ! IRC_WriteStrClient( Client, RPL_LUSEROP_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
723 /* Unknown connections */
724 cnt = Client_UnknownCount( );
725 if( cnt > 0 )
727 if( ! IRC_WriteStrClient( Client, RPL_LUSERUNKNOWN_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
730 /* Number of created channels */
731 if( ! IRC_WriteStrClient( Client, RPL_LUSERCHANNELS_MSG, Client_ID( Client ), Channel_Count( ))) return DISCONNECTED;
733 /* Number of local users, services and servers */
734 if( ! IRC_WriteStrClient( Client, RPL_LUSERME_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
736 #ifndef STRICT_RFC
737 /* Maximum number of local users */
738 if( ! IRC_WriteStrClient( Client, RPL_LOCALUSERS_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyMaxUserCount( ))) return DISCONNECTED;
739 /* Maximum number of users in the network */
740 if( ! IRC_WriteStrClient( Client, RPL_NETUSERS_MSG, Client_ID( Client ), Client_UserCount( ), Client_MaxUserCount( ))) return DISCONNECTED;
741 #endif
743 return CONNECTED;
744 } /* IRC_Send_LUSERS */
747 GLOBAL BOOLEAN
748 IRC_Show_MOTD( CLIENT *Client )
750 BOOLEAN ok;
751 CHAR line[127];
752 FILE *fd;
754 assert( Client != NULL );
756 fd = fopen( Conf_MotdFile, "r" );
757 if( ! fd )
759 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
760 return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
763 IRC_WriteStrClient( Client, RPL_MOTDSTART_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )));
764 while( TRUE )
766 if( ! fgets( line, 126, fd )) break;
767 if( line[strlen( line ) - 1] == '\n' ) line[strlen( line ) - 1] = '\0';
768 if( ! IRC_WriteStrClient( Client, RPL_MOTD_MSG, Client_ID( Client ), line ))
770 fclose( fd );
771 return FALSE;
774 ok = IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ) );
776 fclose( fd );
778 return ok;
779 } /* IRC_Show_MOTD */
782 GLOBAL BOOLEAN
783 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
785 BOOLEAN is_visible, is_member;
786 CHAR str[LINE_LEN + 1];
787 CL2CHAN *cl2chan;
788 CLIENT *cl;
790 assert( Client != NULL );
791 assert( Chan != NULL );
793 if( Channel_IsMemberOf( Chan, Client )) is_member = TRUE;
794 else is_member = FALSE;
796 /* Alle Mitglieder suchen */
797 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
798 cl2chan = Channel_FirstMember( Chan );
799 while( cl2chan )
801 cl = Channel_GetClient( cl2chan );
803 if( strchr( Client_Modes( cl ), 'i' )) is_visible = FALSE;
804 else is_visible = TRUE;
806 if( is_member || is_visible )
808 /* Nick anhaengen */
809 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
810 if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
811 else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
812 strlcat( str, Client_ID( cl ), sizeof( str ));
814 if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
816 /* Zeile wird zu lang: senden! */
817 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
818 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
822 /* naechstes Mitglied suchen */
823 cl2chan = Channel_NextMember( Chan, cl2chan );
825 if( str[strlen( str ) - 1] != ':')
827 /* Es sind noch Daten da, die gesendet werden muessen */
828 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
831 return CONNECTED;
832 } /* IRC_Send_NAMES */
835 GLOBAL BOOLEAN
836 IRC_Send_WHO( CLIENT *Client, CHANNEL *Chan, BOOLEAN OnlyOps )
838 BOOLEAN is_visible, is_member;
839 CL2CHAN *cl2chan;
840 CHAR flags[8];
841 CLIENT *c;
843 assert( Client != NULL );
844 assert( Chan != NULL );
846 if( Channel_IsMemberOf( Chan, Client )) is_member = TRUE;
847 else is_member = FALSE;
849 /* Alle Mitglieder suchen */
850 cl2chan = Channel_FirstMember( Chan );
851 while( cl2chan )
853 c = Channel_GetClient( cl2chan );
855 if( strchr( Client_Modes( c ), 'i' )) is_visible = FALSE;
856 else is_visible = TRUE;
858 if( is_member || is_visible )
860 /* Flags zusammenbasteln */
861 strcpy( flags, "H" );
862 if( strchr( Client_Modes( c ), 'o' )) strlcat( flags, "*", sizeof( flags ));
863 if( strchr( Channel_UserModes( Chan, c ), 'o' )) strlcat( flags, "@", sizeof( flags ));
864 else if( strchr( Channel_UserModes( Chan, c ), 'v' )) strlcat( flags, "+", sizeof( flags ));
866 /* ausgeben */
867 if(( ! OnlyOps ) || ( strchr( Client_Modes( c ), 'o' )))
869 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;
873 /* naechstes Mitglied suchen */
874 cl2chan = Channel_NextMember( Chan, cl2chan );
876 return CONNECTED;
877 } /* IRC_Send_WHO */
880 /* -eof- */