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.37 2006/10/07 10:40:52 fw 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 strlcpy( rpl, RPL_ISON_MSG, sizeof rpl );
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 = Conn_GetClient( 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 strlcpy( rpl, RPL_USERHOST_MSG, sizeof rpl );
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 )
551 assert( Client != NULL );
552 assert( Req != NULL );
554 /* Falsche Anzahl Parameter? */
555 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
562 /* Nur OPs anzeigen? */
563 if( strcmp( Req->argv[1], "o" ) == 0 ) only_ops = true;
565 else return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
571 /* wurde ein Channel oder Nick-Mask angegeben? */
572 chan = Channel_Search( Req->argv[0] );
577 /* User eines Channels ausgeben */
578 if( ! IRC_Send_WHO( Client, chan, only_ops )) return DISCONNECTED;
584 if(( Client_Type( c ) == CLIENT_USER ) && ( ! strchr( Client_Modes( c ), 'i' )))
587 if( Req->argc == 0 ) ok = true;
590 if( strcasecmp( Req->argv[0], Client_ID( c )) == 0 ) ok = true;
591 else if( strcmp( Req->argv[0], "0" ) == 0 ) ok = true;
594 if( ok && (( ! only_ops ) || ( strchr( Client_Modes( c ), 'o' ))))
597 strcpy( flags, "H" );
598 if( strchr( Client_Modes( c ), 'o' )) strlcat( flags, "*", sizeof( flags ));
600 /* Search suitable channel */
601 cl2chan = Channel_FirstChannelOf( c );
604 cn = Channel_GetChannel( cl2chan );
605 if( Channel_IsMemberOf( cn, Client ) ||
606 ! strchr( Channel_Modes( cn ), 's' ))
608 ptr = Channel_Name( cn );
611 cl2chan = Channel_NextChannelOf( c, cl2chan );
613 if( ! cl2chan ) ptr = "*";
615 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;
619 /* naechster Client */
620 c = Client_Next( c );
623 if( chan ) return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), Channel_Name( chan ));
624 else if( Req->argc == 0 ) return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), "*" );
625 else return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), Req->argv[0] );
630 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
632 CLIENT *from, *target, *c;
633 char str[LINE_LEN + 1];
637 assert( Client != NULL );
638 assert( Req != NULL );
640 /* Bad number of parameters? */
641 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
644 c = Client_Search( Req->argv[Req->argc - 1] );
645 if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[Req->argc - 1] );
647 /* Search sender of the WHOIS */
648 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
650 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
652 /* Forward to other server? */
655 /* Search target server (can be specified as nick of that server!) */
656 target = Client_Search( Req->argv[0] );
657 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
659 else target = Client_ThisServer( );
661 assert( target != NULL );
663 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] );
665 /* Nick, user and name */
666 if( ! IRC_WriteStrClient( from, RPL_WHOISUSER_MSG, Client_ID( from ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
669 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;
672 snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
673 cl2chan = Channel_FirstChannelOf( c );
676 chan = Channel_GetChannel( cl2chan );
677 assert( chan != NULL );
680 cl2chan = Channel_NextChannelOf( c, cl2chan );
682 /* Secret channel? */
683 if( strchr( Channel_Modes( chan ), 's' ) && ! Channel_IsMemberOf( chan, Client )) continue;
685 /* Concatenate channel names */
686 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
687 if( strchr( Channel_UserModes( chan, c ), 'o' )) strlcat( str, "@", sizeof( str ));
688 else if( strchr( Channel_UserModes( chan, c ), 'v' )) strlcat( str, "+", sizeof( str ));
689 strlcat( str, Channel_Name( chan ), sizeof( str ));
691 if( strlen( str ) > ( LINE_LEN - CHANNEL_NAME_LEN - 4 ))
693 /* Line becomes too long: send it! */
694 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
695 snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
698 if( str[strlen( str ) - 1] != ':')
700 /* There is data left to send: */
701 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
705 if( Client_HasMode( c, 'o' ))
707 if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
710 /* Idle (only local clients) */
711 if( Client_Conn( c ) > NONE )
713 if( ! IRC_WriteStrClient( from, RPL_WHOISIDLE_MSG, Client_ID( from ), Client_ID( c ), Conn_GetIdle( Client_Conn ( c )))) return DISCONNECTED;
717 if( Client_HasMode( c, 'a' ))
719 if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( c ), Client_Away( c ))) return DISCONNECTED;
723 return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
728 * IRC "WHOWAS" function.
729 * This function implements the IRC command "WHOWHAS". It handles local
730 * requests and request that should be forwarded to other servers.
733 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
735 CLIENT *target, *prefix;
737 int max, last, count, i;
740 assert( Client != NULL );
741 assert( Req != NULL );
743 /* Wrong number of parameters? */
744 if(( Req->argc < 1 ) || ( Req->argc > 3 ))
745 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
746 Client_ID( Client ), Req->command );
750 target = Client_Search( Req->argv[2] );
752 target = Client_ThisServer( );
755 if( Client_Type( Client ) == CLIENT_SERVER )
756 prefix = Client_Search( Req->prefix );
761 return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG,
762 Client_ID( Client ), Req->prefix );
764 /* Forward to other server? */
765 if( target != Client_ThisServer( ))
767 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
768 return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG,
773 IRC_WriteStrClientPrefix( target, prefix, "WHOWAS %s %s %s",
774 Req->argv[0], Req->argv[1],
779 whowas = Client_GetWhowas( );
780 last = Client_GetLastWhowasIndex( );
781 if( last < 0 ) last = 0;
785 max = atoi( Req->argv[1] );
786 if( max < 1 ) max = MAX_WHOWAS;
789 max = DEFAULT_WHOWAS;
796 if( whowas[i].time > 0 &&
797 strcasecmp( Req->argv[0], whowas[i].id ) == 0 )
799 (void)strftime( t_str, sizeof(t_str),
800 "%a %b %d %H:%M:%S %Y",
801 localtime( &whowas[i].time ));
803 if( ! IRC_WriteStrClient( prefix, RPL_WHOWASUSER_MSG,
811 if( ! IRC_WriteStrClient( prefix, RPL_WHOISSERVER_MSG,
814 whowas[i].server, t_str ))
818 if( count >= max ) break;
824 /* "underflow", wrap around */
825 if( i < 0 ) i = MAX_WHOWAS - 1;
826 } while( i != last );
828 return IRC_WriteStrClient( prefix, RPL_ENDOFWHOWAS_MSG,
829 Client_ID( prefix ), Req->argv[0] );
834 IRC_Send_LUSERS( CLIENT *Client )
841 assert( Client != NULL );
843 /* Users, services and serevers in the network */
844 if( ! IRC_WriteStrClient( Client, RPL_LUSERCLIENT_MSG, Client_ID( Client ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
846 /* Number of IRC operators */
847 cnt = Client_OperCount( );
850 if( ! IRC_WriteStrClient( Client, RPL_LUSEROP_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
853 /* Unknown connections */
854 cnt = Client_UnknownCount( );
857 if( ! IRC_WriteStrClient( Client, RPL_LUSERUNKNOWN_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
860 /* Number of created channels */
861 if( ! IRC_WriteStrClient( Client, RPL_LUSERCHANNELS_MSG, Client_ID( Client ), Channel_Count( ))) return DISCONNECTED;
863 /* Number of local users, services and servers */
864 if( ! IRC_WriteStrClient( Client, RPL_LUSERME_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
867 /* Maximum number of local users */
868 cnt = Client_MyUserCount();
869 max = Client_MyMaxUserCount();
870 if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
873 /* Maximum number of users in the network */
874 cnt = Client_UserCount();
875 max = Client_MaxUserCount();
876 if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
882 } /* IRC_Send_LUSERS */
885 static bool Show_MOTD_Start(CLIENT *Client)
887 return IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG,
888 Client_ID( Client ), Client_ID( Client_ThisServer( )));
891 static bool Show_MOTD_Sendline(CLIENT *Client, const char *msg)
893 return IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID( Client ), msg);
896 static bool Show_MOTD_End(CLIENT *Client)
898 return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
903 IRC_Show_MOTD( CLIENT *Client )
908 assert( Client != NULL );
910 if (Conf_MotdPhrase[0]) {
911 if (!Show_MOTD_Start(Client))
913 if (!Show_MOTD_Sendline(Client, Conf_MotdPhrase))
916 return Show_MOTD_End(Client);
919 fd = fopen( Conf_MotdFile, "r" );
921 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
922 return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
925 if (!Show_MOTD_Start( Client )) {
930 while (fgets( line, (int)sizeof line, fd )) {
931 ngt_TrimLastChr( line, '\n');
933 if( ! Show_MOTD_Sendline( Client, line)) {
939 return Show_MOTD_End(Client);
940 } /* IRC_Show_MOTD */
944 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
946 bool is_visible, is_member;
947 char str[LINE_LEN + 1];
951 assert( Client != NULL );
952 assert( Chan != NULL );
954 if( Channel_IsMemberOf( Chan, Client )) is_member = true;
955 else is_member = false;
957 /* Secret channel? */
958 if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
960 /* Alle Mitglieder suchen */
961 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
962 cl2chan = Channel_FirstMember( Chan );
965 cl = Channel_GetClient( cl2chan );
967 if( strchr( Client_Modes( cl ), 'i' )) is_visible = false;
968 else is_visible = true;
970 if( is_member || is_visible )
973 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
974 if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
975 else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
976 strlcat( str, Client_ID( cl ), sizeof( str ));
978 if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
980 /* Zeile wird zu lang: senden! */
981 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
982 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
986 /* naechstes Mitglied suchen */
987 cl2chan = Channel_NextMember( Chan, cl2chan );
989 if( str[strlen( str ) - 1] != ':')
991 /* Es sind noch Daten da, die gesendet werden muessen */
992 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
996 } /* IRC_Send_NAMES */
1000 IRC_Send_WHO( CLIENT *Client, CHANNEL *Chan, bool OnlyOps )
1002 bool is_visible, is_member;
1007 assert( Client != NULL );
1008 assert( Chan != NULL );
1010 if( Channel_IsMemberOf( Chan, Client )) is_member = true;
1011 else is_member = false;
1013 /* Secret channel? */
1014 if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
1016 /* Alle Mitglieder suchen */
1017 cl2chan = Channel_FirstMember( Chan );
1020 c = Channel_GetClient( cl2chan );
1022 if( strchr( Client_Modes( c ), 'i' )) is_visible = false;
1023 else is_visible = true;
1025 if( is_member || is_visible )
1027 /* Flags zusammenbasteln */
1028 strcpy( flags, "H" );
1029 if( strchr( Client_Modes( c ), 'o' )) strlcat( flags, "*", sizeof( flags ));
1030 if( strchr( Channel_UserModes( Chan, c ), 'o' )) strlcat( flags, "@", sizeof( flags ));
1031 else if( strchr( Channel_UserModes( Chan, c ), 'v' )) strlcat( flags, "+", sizeof( flags ));
1034 if(( ! OnlyOps ) || ( strchr( Client_Modes( c ), 'o' )))
1036 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;
1040 /* naechstes Mitglied suchen */
1041 cl2chan = Channel_NextMember( Chan, cl2chan );
1044 } /* IRC_Send_WHO */