2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2005 Alexander Barton (alex@barton.de)
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.
17 static char UNUSED id[] = "$Id: irc-info.c,v 1.29 2005/05/16 12:25:15 alex Exp $";
28 #include "cvs-version.h"
29 #include "conn-func.h"
40 #include "irc-write.h"
47 IRC_ADMIN(CLIENT *Client, REQUEST *Req )
49 CLIENT *target, *prefix;
51 assert( Client != NULL );
52 assert( Req != NULL );
54 /* Falsche Anzahl Parameter? */
55 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
58 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
59 else target = Client_ThisServer( );
61 /* Prefix ermitteln */
62 if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
64 if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
66 /* An anderen Server weiterleiten? */
67 if( target != Client_ThisServer( ))
69 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
72 IRC_WriteStrClientPrefix( target, prefix, "ADMIN %s", Req->argv[0] );
76 /* mit Versionsinfo antworten */
77 if( ! IRC_WriteStrClient( Client, RPL_ADMINME_MSG, Client_ID( prefix ), Conf_ServerName )) return DISCONNECTED;
78 if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC1_MSG, Client_ID( prefix ), Conf_ServerAdmin1 )) return DISCONNECTED;
79 if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC2_MSG, Client_ID( prefix ), Conf_ServerAdmin2 )) return DISCONNECTED;
80 if( ! IRC_WriteStrClient( Client, RPL_ADMINEMAIL_MSG, Client_ID( prefix ), Conf_ServerAdminMail )) return DISCONNECTED;
82 IRC_SetPenalty( Client, 1 );
88 IRC_ISON( CLIENT *Client, REQUEST *Req )
90 char rpl[COMMAND_LEN];
95 assert( Client != NULL );
96 assert( Req != NULL );
98 /* Falsche Anzahl Parameter? */
99 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
101 strcpy( rpl, RPL_ISON_MSG );
102 for( i = 0; i < Req->argc; i++ )
104 ptr = strtok( Req->argv[i], " " );
108 c = Client_Search( ptr );
109 if( c && ( Client_Type( c ) == CLIENT_USER ))
111 /* Dieser Nick ist "online" */
112 strlcat( rpl, ptr, sizeof( rpl ));
113 strlcat( rpl, " ", sizeof( rpl ));
115 ptr = strtok( NULL, " " );
118 ngt_TrimLastChr(rpl, ' ');
120 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
125 IRC_LINKS( CLIENT *Client, REQUEST *Req )
127 CLIENT *target, *from, *c;
130 assert( Client != NULL );
131 assert( Req != NULL );
133 /* Falsche Anzahl Parameter? */
134 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
136 /* Server-Mask ermitteln */
137 if( Req->argc > 0 ) mask = Req->argv[Req->argc - 1];
140 /* Absender ermitteln */
141 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
143 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
145 /* An anderen Server forwarden? */
148 target = Client_Search( Req->argv[0] );
149 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
150 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LINKS %s %s", Req->argv[0], Req->argv[1] );
153 /* Wer ist der Absender? */
154 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
155 else target = Client;
156 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
161 if( Client_Type( c ) == CLIENT_SERVER )
163 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;
165 c = Client_Next( c );
168 IRC_SetPenalty( target, 1 );
169 return IRC_WriteStrClient( target, RPL_ENDOFLINKS_MSG, Client_ID( target ), mask );
174 IRC_LUSERS( CLIENT *Client, REQUEST *Req )
176 CLIENT *target, *from;
178 assert( Client != NULL );
179 assert( Req != NULL );
181 /* Falsche Anzahl Parameter? */
182 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
184 /* Absender ermitteln */
185 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
187 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
189 /* An anderen Server forwarden? */
192 target = Client_Search( Req->argv[1] );
193 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
194 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LUSERS %s %s", Req->argv[0], Req->argv[1] );
197 /* Wer ist der Absender? */
198 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
199 else target = Client;
200 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
202 IRC_Send_LUSERS( target );
204 IRC_SetPenalty( target, 1 );
210 IRC_MOTD( CLIENT *Client, REQUEST *Req )
212 CLIENT *from, *target;
214 assert( Client != NULL );
215 assert( Req != NULL );
217 /* Falsche Anzahl Parameter? */
218 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
220 /* From aus Prefix ermitteln */
221 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
223 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
227 /* an anderen Server forwarden */
228 target = Client_Search( Req->argv[0] );
229 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
231 if( target != Client_ThisServer( ))
233 /* Ok, anderer Server ist das Ziel: forwarden */
234 return IRC_WriteStrClientPrefix( target, from, "MOTD %s", Req->argv[0] );
238 IRC_SetPenalty( from, 3 );
239 return IRC_Show_MOTD( from );
244 IRC_NAMES( CLIENT *Client, REQUEST *Req )
246 char rpl[COMMAND_LEN], *ptr;
247 CLIENT *target, *from, *c;
250 assert( Client != NULL );
251 assert( Req != NULL );
253 /* Falsche Anzahl Parameter? */
254 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
256 /* From aus Prefix ermitteln */
257 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
259 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
263 /* an anderen Server forwarden */
264 target = Client_Search( Req->argv[1] );
265 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
267 if( target != Client_ThisServer( ))
269 /* Ok, anderer Server ist das Ziel: forwarden */
270 return IRC_WriteStrClientPrefix( target, from, "NAMES %s :%s", Req->argv[0], Req->argv[1] );
276 /* bestimmte Channels durchgehen */
277 ptr = strtok( Req->argv[0], "," );
280 chan = Channel_Search( ptr );
284 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
286 if( ! IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), ptr )) return DISCONNECTED;
288 /* naechsten Namen ermitteln */
289 ptr = strtok( NULL, "," );
294 /* alle Channels durchgehen */
295 chan = Channel_First( );
299 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
301 /* naechster Channel */
302 chan = Channel_Next( chan );
305 /* Nun noch alle Clients ausgeben, die in keinem Channel sind */
307 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
310 if(( Client_Type( c ) == CLIENT_USER ) && ( Channel_FirstChannelOf( c ) == NULL ) && ( ! strchr( Client_Modes( c ), 'i' )))
312 /* Okay, das ist ein User: anhaengen */
313 if( rpl[strlen( rpl ) - 1] != ':' ) strlcat( rpl, " ", sizeof( rpl ));
314 strlcat( rpl, Client_ID( c ), sizeof( rpl ));
316 if( strlen( rpl ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
318 /* Zeile wird zu lang: senden! */
319 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
320 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
324 /* naechster Client */
325 c = Client_Next( c );
327 if( rpl[strlen( rpl ) - 1] != ':')
329 /* es wurden User gefunden */
330 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
333 IRC_SetPenalty( from, 1 );
334 return IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), "*" );
339 IRC_STATS( CLIENT *Client, REQUEST *Req )
341 CLIENT *from, *target, *cl;
346 assert( Client != NULL );
347 assert( Req != NULL );
349 /* Falsche Anzahl Parameter? */
350 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
352 /* From aus Prefix ermitteln */
353 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
355 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
359 /* an anderen Server forwarden */
360 target = Client_Search( Req->argv[1] );
361 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
363 if( target != Client_ThisServer( ))
365 /* Ok, anderer Server ist das Ziel: forwarden */
366 return IRC_WriteStrClientPrefix( target, from, "STATS %s %s", Req->argv[0], Req->argv[1] );
370 if( Req->argc > 0 ) query = Req->argv[0][0] ? Req->argv[0][0] : '*';
375 case 'l': /* Links */
380 cl = Client_GetFromConn( con );
381 if( cl && (( Client_Type( cl ) == CLIENT_SERVER ) || ( cl == Client )))
383 /* Server link or our own connection */
385 if( Conn_Options( con ) & CONN_ZIP )
387 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;
392 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;
395 con = Conn_Next( con );
398 case 'm': /* IRC-Befehle */
400 cmd = Parse_GetCommandStruct( );
403 if( cmd->lcount > 0 || cmd->rcount > 0 )
405 if( ! IRC_WriteStrClient( from, RPL_STATSCOMMANDS_MSG, Client_ID( from ), cmd->name, cmd->lcount, cmd->bytes, cmd->rcount )) return DISCONNECTED;
412 IRC_SetPenalty( from, 2 );
413 return IRC_WriteStrClient( from, RPL_ENDOFSTATS_MSG, Client_ID( from ), query );
418 IRC_TIME( CLIENT *Client, REQUEST *Req )
420 CLIENT *from, *target;
424 assert( Client != NULL );
425 assert( Req != NULL );
427 /* Falsche Anzahl Parameter? */
428 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
430 /* From aus Prefix ermitteln */
431 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
433 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
437 /* an anderen Server forwarden */
438 target = Client_Search( Req->argv[0] );
439 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
441 if( target != Client_ThisServer( ))
443 /* Ok, anderer Server ist das Ziel: forwarden */
444 return IRC_WriteStrClientPrefix( target, from, "TIME %s", Req->argv[0] );
449 (void)strftime( t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime( &t ));
450 return IRC_WriteStrClient( from, RPL_TIME_MSG, Client_ID( from ), Client_ID( Client_ThisServer( )), t_str );
455 IRC_USERHOST( CLIENT *Client, REQUEST *Req )
457 char rpl[COMMAND_LEN];
461 assert( Client != NULL );
462 assert( Req != NULL );
464 /* Falsche Anzahl Parameter? */
465 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
467 if( Req->argc > 5 ) max = 5;
468 else max = Req->argc;
470 strcpy( rpl, RPL_USERHOST_MSG );
471 for( i = 0; i < max; i++ )
473 c = Client_Search( Req->argv[i] );
474 if( c && ( Client_Type( c ) == CLIENT_USER ))
476 /* Dieser Nick ist "online" */
477 strlcat( rpl, Client_ID( c ), sizeof( rpl ));
478 if( Client_HasMode( c, 'o' )) strlcat( rpl, "*", sizeof( rpl ));
479 strlcat( rpl, "=", sizeof( rpl ));
480 if( Client_HasMode( c, 'a' )) strlcat( rpl, "-", sizeof( rpl ));
481 else strlcat( rpl, "+", sizeof( rpl ));
482 strlcat( rpl, Client_User( c ), sizeof( rpl ));
483 strlcat( rpl, "@", sizeof( rpl ));
484 strlcat( rpl, Client_Hostname( c ), sizeof( rpl ));
485 strlcat( rpl, " ", sizeof( rpl ));
488 ngt_TrimLastChr( rpl, ' ');
490 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
495 IRC_VERSION( CLIENT *Client, REQUEST *Req )
497 CLIENT *target, *prefix;
499 char ver[12], vertxt[30];
502 assert( Client != NULL );
503 assert( Req != NULL );
505 /* Falsche Anzahl Parameter? */
506 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
509 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
510 else target = Client_ThisServer( );
512 /* Prefix ermitteln */
513 if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
514 else prefix = Client;
515 if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
517 /* An anderen Server weiterleiten? */
518 if( target != Client_ThisServer( ))
520 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
523 IRC_WriteStrClientPrefix( target, prefix, "VERSION %s", Req->argv[0] );
527 /* mit Versionsinfo antworten */
528 IRC_SetPenalty( Client, 1 );
530 strlcpy( ver, CVSDATE, sizeof( ver ));
531 strncpy( ver + 4, ver + 5, 2 );
532 strncpy( ver + 6, ver + 8, 3 );
533 snprintf( vertxt, sizeof( vertxt ), "%s(%s)", PACKAGE_VERSION, ver );
534 return IRC_WriteStrClient( Client, RPL_VERSION_MSG, Client_ID( prefix ), PACKAGE_NAME, vertxt, NGIRCd_DebugLevel, Conf_ServerName, NGIRCd_VersionAddition );
536 return IRC_WriteStrClient( Client, RPL_VERSION_MSG, Client_ID( prefix ), PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel, Conf_ServerName, NGIRCd_VersionAddition );
542 IRC_WHO( CLIENT *Client, REQUEST *Req )
550 assert( Client != NULL );
551 assert( Req != NULL );
553 /* Falsche Anzahl Parameter? */
554 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
561 /* Nur OPs anzeigen? */
562 if( strcmp( Req->argv[1], "o" ) == 0 ) only_ops = true;
564 else return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
570 /* wurde ein Channel oder Nick-Mask angegeben? */
571 chan = Channel_Search( Req->argv[0] );
576 /* User eines Channels ausgeben */
577 if( ! IRC_Send_WHO( Client, chan, only_ops )) return DISCONNECTED;
583 if(( Client_Type( c ) == CLIENT_USER ) && ( ! strchr( Client_Modes( c ), 'i' )))
586 if( Req->argc == 0 ) ok = true;
589 if( strcasecmp( Req->argv[0], Client_ID( c )) == 0 ) ok = true;
590 else if( strcmp( Req->argv[0], "0" ) == 0 ) ok = true;
593 if( ok && (( ! only_ops ) || ( strchr( Client_Modes( c ), 'o' ))))
596 strcpy( flags, "H" );
597 if( strchr( Client_Modes( c ), 'o' )) strlcat( flags, "*", sizeof( flags ));
599 /* Search suitable channel */
600 cl2chan = Channel_FirstChannelOf( c );
603 cn = Channel_GetChannel( cl2chan );
604 if( Channel_IsMemberOf( cn, Client ) ||
605 ! strchr( Channel_Modes( cn ), 's' ))
607 ptr = Channel_Name( cn );
610 cl2chan = Channel_NextChannelOf( c, cl2chan );
612 if( ! cl2chan ) ptr = "*";
614 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;
618 /* naechster Client */
619 c = Client_Next( c );
622 if( chan ) return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), Channel_Name( chan ));
623 else if( Req->argc == 0 ) return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), "*" );
624 else return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), Req->argv[0] );
629 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
631 CLIENT *from, *target, *c;
632 char str[LINE_LEN + 1];
636 assert( Client != NULL );
637 assert( Req != NULL );
639 /* Bad number of parameters? */
640 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
643 c = Client_Search( Req->argv[Req->argc - 1] );
644 if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[Req->argc - 1] );
646 /* Search sender of the WHOIS */
647 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
649 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
651 /* Forward to other server? */
654 /* Search target server (can be specified as nick of that server!) */
655 target = Client_Search( Req->argv[0] );
656 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
658 else target = Client_ThisServer( );
660 assert( target != NULL );
662 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] );
664 /* Nick, user and name */
665 if( ! IRC_WriteStrClient( from, RPL_WHOISUSER_MSG, Client_ID( from ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
668 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;
671 snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
672 cl2chan = Channel_FirstChannelOf( c );
675 chan = Channel_GetChannel( cl2chan );
676 assert( chan != NULL );
679 cl2chan = Channel_NextChannelOf( c, cl2chan );
681 /* Secret channel? */
682 if( strchr( Channel_Modes( chan ), 's' ) && ! Channel_IsMemberOf( chan, Client )) continue;
684 /* Concatenate channel names */
685 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
686 if( strchr( Channel_UserModes( chan, c ), 'o' )) strlcat( str, "@", sizeof( str ));
687 else if( strchr( Channel_UserModes( chan, c ), 'v' )) strlcat( str, "+", sizeof( str ));
688 strlcat( str, Channel_Name( chan ), sizeof( str ));
690 if( strlen( str ) > ( LINE_LEN - CHANNEL_NAME_LEN - 4 ))
692 /* Line becomes too long: send it! */
693 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
694 snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
697 if( str[strlen( str ) - 1] != ':')
699 /* There is data left to send: */
700 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
704 if( Client_HasMode( c, 'o' ))
706 if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
709 /* Idle (only local clients) */
710 if( Client_Conn( c ) > NONE )
712 if( ! IRC_WriteStrClient( from, RPL_WHOISIDLE_MSG, Client_ID( from ), Client_ID( c ), Conn_GetIdle( Client_Conn ( c )))) return DISCONNECTED;
716 if( Client_HasMode( c, 'a' ))
718 if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( c ), Client_Away( c ))) return DISCONNECTED;
722 return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
727 * IRC "WHOWAS" function.
728 * This function implements the IRC command "WHOWHAS". It handles local
729 * requests and request that should be forwarded to other servers.
732 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
734 CLIENT *target, *prefix;
736 int max, last, count, i;
739 assert( Client != NULL );
740 assert( Req != NULL );
742 /* Wrong number of parameters? */
743 if(( Req->argc < 1 ) || ( Req->argc > 3 ))
744 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
745 Client_ID( Client ), Req->command );
749 target = Client_Search( Req->argv[2] );
751 target = Client_ThisServer( );
754 if( Client_Type( Client ) == CLIENT_SERVER )
755 prefix = Client_Search( Req->prefix );
760 return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG,
761 Client_ID( Client ), Req->prefix );
763 /* Forward to other server? */
764 if( target != Client_ThisServer( ))
766 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
767 return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG,
772 IRC_WriteStrClientPrefix( target, prefix, "WHOWAS %s %s %s",
773 Req->argv[0], Req->argv[1],
778 whowas = Client_GetWhowas( );
779 last = Client_GetLastWhowasIndex( );
780 if( last < 0 ) last = 0;
784 max = atoi( Req->argv[1] );
785 if( max < 1 ) max = MAX_WHOWAS;
788 max = DEFAULT_WHOWAS;
795 if( whowas[i].time > 0 &&
796 strcasecmp( Req->argv[0], whowas[i].id ) == 0 )
798 (void)strftime( t_str, sizeof(t_str),
799 "%a %b %d %H:%M:%S %Y",
800 localtime( &whowas[i].time ));
802 if( ! IRC_WriteStrClient( prefix, RPL_WHOWASUSER_MSG,
810 if( ! IRC_WriteStrClient( prefix, RPL_WHOISSERVER_MSG,
813 whowas[i].server, t_str ))
817 if( count >= max ) break;
823 /* "underflow", wrap around */
824 if( i < 0 ) i = MAX_WHOWAS - 1;
825 } while( i != last );
827 return IRC_WriteStrClient( prefix, RPL_ENDOFWHOWAS_MSG,
828 Client_ID( prefix ), Req->argv[0] );
833 IRC_Send_LUSERS( CLIENT *Client )
837 assert( Client != NULL );
839 /* Users, services and serevers in the network */
840 if( ! IRC_WriteStrClient( Client, RPL_LUSERCLIENT_MSG, Client_ID( Client ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
842 /* Number of IRC operators */
843 cnt = Client_OperCount( );
846 if( ! IRC_WriteStrClient( Client, RPL_LUSEROP_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
849 /* Unknown connections */
850 cnt = Client_UnknownCount( );
853 if( ! IRC_WriteStrClient( Client, RPL_LUSERUNKNOWN_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
856 /* Number of created channels */
857 if( ! IRC_WriteStrClient( Client, RPL_LUSERCHANNELS_MSG, Client_ID( Client ), Channel_Count( ))) return DISCONNECTED;
859 /* Number of local users, services and servers */
860 if( ! IRC_WriteStrClient( Client, RPL_LUSERME_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
863 /* Maximum number of local users */
864 if( ! IRC_WriteStrClient( Client, RPL_LOCALUSERS_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyMaxUserCount( ))) return DISCONNECTED;
865 /* Maximum number of users in the network */
866 if( ! IRC_WriteStrClient( Client, RPL_NETUSERS_MSG, Client_ID( Client ), Client_UserCount( ), Client_MaxUserCount( ))) return DISCONNECTED;
870 } /* IRC_Send_LUSERS */
874 IRC_Show_MOTD( CLIENT *Client )
880 assert( Client != NULL );
882 if( Conf_MotdPhrase[0] )
884 if( ! IRC_WriteStrClient( Client, RPL_MOTDSTART_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )))) return DISCONNECTED;
885 if( ! IRC_WriteStrClient( Client, RPL_MOTD_MSG, Client_ID( Client ), Conf_MotdPhrase )) return DISCONNECTED;
886 return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
889 fd = fopen( Conf_MotdFile, "r" );
892 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
893 return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
896 if( ! IRC_WriteStrClient( Client, RPL_MOTDSTART_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )))) return DISCONNECTED;
899 if( ! fgets( line, sizeof( line ), fd )) break;
901 ngt_TrimLastChr( line, '\n');
903 if( ! IRC_WriteStrClient( Client, RPL_MOTD_MSG, Client_ID( Client ), line ))
909 ok = IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ) );
914 } /* IRC_Show_MOTD */
918 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
920 bool is_visible, is_member;
921 char str[LINE_LEN + 1];
925 assert( Client != NULL );
926 assert( Chan != NULL );
928 if( Channel_IsMemberOf( Chan, Client )) is_member = true;
929 else is_member = false;
931 /* Secret channel? */
932 if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
934 /* Alle Mitglieder suchen */
935 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
936 cl2chan = Channel_FirstMember( Chan );
939 cl = Channel_GetClient( cl2chan );
941 if( strchr( Client_Modes( cl ), 'i' )) is_visible = false;
942 else is_visible = true;
944 if( is_member || is_visible )
947 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
948 if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
949 else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
950 strlcat( str, Client_ID( cl ), sizeof( str ));
952 if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
954 /* Zeile wird zu lang: senden! */
955 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
956 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
960 /* naechstes Mitglied suchen */
961 cl2chan = Channel_NextMember( Chan, cl2chan );
963 if( str[strlen( str ) - 1] != ':')
965 /* Es sind noch Daten da, die gesendet werden muessen */
966 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
970 } /* IRC_Send_NAMES */
974 IRC_Send_WHO( CLIENT *Client, CHANNEL *Chan, bool OnlyOps )
976 bool is_visible, is_member;
981 assert( Client != NULL );
982 assert( Chan != NULL );
984 if( Channel_IsMemberOf( Chan, Client )) is_member = true;
985 else is_member = false;
987 /* Secret channel? */
988 if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
990 /* Alle Mitglieder suchen */
991 cl2chan = Channel_FirstMember( Chan );
994 c = Channel_GetClient( cl2chan );
996 if( strchr( Client_Modes( c ), 'i' )) is_visible = false;
997 else is_visible = true;
999 if( is_member || is_visible )
1001 /* Flags zusammenbasteln */
1002 strcpy( flags, "H" );
1003 if( strchr( Client_Modes( c ), 'o' )) strlcat( flags, "*", sizeof( flags ));
1004 if( strchr( Channel_UserModes( Chan, c ), 'o' )) strlcat( flags, "@", sizeof( flags ));
1005 else if( strchr( Channel_UserModes( Chan, c ), 'v' )) strlcat( flags, "+", sizeof( flags ));
1008 if(( ! OnlyOps ) || ( strchr( Client_Modes( c ), 'o' )))
1010 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;
1014 /* naechstes Mitglied suchen */
1015 cl2chan = Channel_NextMember( Chan, cl2chan );
1018 } /* IRC_Send_WHO */