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.41 2007/12/11 11:29:44 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 t_diff(time_t *t, const time_t div)
353 uptime_days(time_t *now)
355 return t_diff(now, 60 * 60 * 24);
360 uptime_hrs(time_t *now)
362 return t_diff(now, 60 * 60);
367 uptime_mins(time_t *now)
369 return t_diff(now, 60);
374 IRC_STATS( CLIENT *Client, REQUEST *Req )
376 CLIENT *from, *target, *cl;
381 unsigned int days, hrs, mins;
383 assert( Client != NULL );
384 assert( Req != NULL );
386 /* Falsche Anzahl Parameter? */
388 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
390 /* From aus Prefix ermitteln */
391 if (Client_Type(Client) == CLIENT_SERVER)
392 from = Client_Search(Req->prefix);
397 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix);
399 if (Req->argc == 2) {
400 /* an anderen Server forwarden */
401 target = Client_Search( Req->argv[1] );
402 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
403 return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
405 if( target != Client_ThisServer()) {
406 /* Ok, anderer Server ist das Ziel: forwarden */
407 return IRC_WriteStrClientPrefix( target, from, "STATS %s %s", Req->argv[0], Req->argv[1] );
412 query = Req->argv[0][0] ? Req->argv[0][0] : '*';
417 case 'l': /* Links */
419 time_now = time(NULL);
420 for (con = Conn_First(); con != NONE ;con = Conn_Next(con)) {
421 cl = Conn_GetClient(con);
424 if ((Client_Type(cl) == CLIENT_SERVER) || (cl == Client)) {
425 /* Server link or our own connection */
427 if (Conn_Options(con) & CONN_ZIP) {
428 if (!IRC_WriteStrClient(from, RPL_STATSLINKINFOZIP_MSG,
429 Client_ID(from), Client_Mask(cl), Conn_SendQ(con),
430 Conn_SendMsg(con), Zip_SendBytes(con), Conn_SendBytes(con),
431 Conn_RecvMsg(con), Zip_RecvBytes(con), Conn_RecvBytes(con), (long)(time_now - Conn_StartTime(con))))
436 if (!IRC_WriteStrClient(from, RPL_STATSLINKINFO_MSG, Client_ID(from),
437 Client_Mask(cl), Conn_SendQ(con), Conn_SendMsg(con), Conn_SendBytes(con),
438 Conn_RecvMsg(con), Conn_RecvBytes(con), (long)(time_now - Conn_StartTime(con))))
443 case 'm': /* IRC-Commands */
445 cmd = Parse_GetCommandStruct( );
446 for (; cmd->name ; cmd++) {
447 if (cmd->lcount == 0 && cmd->rcount == 0)
449 if (!IRC_WriteStrClient(from, RPL_STATSCOMMANDS_MSG, Client_ID(from),
450 cmd->name, cmd->lcount, cmd->bytes, cmd->rcount))
454 case 'u': /* server uptime */
456 time_now = time(NULL) - NGIRCd_Start;
457 days = uptime_days(&time_now);
458 hrs = uptime_hrs(&time_now);
459 mins = uptime_mins(&time_now);
460 if (!IRC_WriteStrClient(from, RPL_STATSUPTIME, Client_ID(from),
461 days, hrs, mins, (unsigned int) time_now))
466 IRC_SetPenalty(from, 2);
467 return IRC_WriteStrClient(from, RPL_ENDOFSTATS_MSG, Client_ID(from), query);
472 IRC_TIME( CLIENT *Client, REQUEST *Req )
474 CLIENT *from, *target;
478 assert( Client != NULL );
479 assert( Req != NULL );
481 /* Falsche Anzahl Parameter? */
482 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
484 /* From aus Prefix ermitteln */
485 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
487 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
491 /* an anderen Server forwarden */
492 target = Client_Search( Req->argv[0] );
493 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
495 if( target != Client_ThisServer( ))
497 /* Ok, anderer Server ist das Ziel: forwarden */
498 return IRC_WriteStrClientPrefix( target, from, "TIME %s", Req->argv[0] );
503 (void)strftime( t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime( &t ));
504 return IRC_WriteStrClient( from, RPL_TIME_MSG, Client_ID( from ), Client_ID( Client_ThisServer( )), t_str );
509 IRC_USERHOST( CLIENT *Client, REQUEST *Req )
511 char rpl[COMMAND_LEN];
515 assert( Client != NULL );
516 assert( Req != NULL );
518 /* Falsche Anzahl Parameter? */
519 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
521 if( Req->argc > 5 ) max = 5;
522 else max = Req->argc;
524 strlcpy( rpl, RPL_USERHOST_MSG, sizeof rpl );
525 for( i = 0; i < max; i++ )
527 c = Client_Search( Req->argv[i] );
528 if( c && ( Client_Type( c ) == CLIENT_USER ))
530 /* Dieser Nick ist "online" */
531 strlcat( rpl, Client_ID( c ), sizeof( rpl ));
532 if( Client_HasMode( c, 'o' )) strlcat( rpl, "*", sizeof( rpl ));
533 strlcat( rpl, "=", sizeof( rpl ));
534 if( Client_HasMode( c, 'a' )) strlcat( rpl, "-", sizeof( rpl ));
535 else strlcat( rpl, "+", sizeof( rpl ));
536 strlcat( rpl, Client_User( c ), sizeof( rpl ));
537 strlcat( rpl, "@", sizeof( rpl ));
538 strlcat( rpl, Client_Hostname( c ), sizeof( rpl ));
539 strlcat( rpl, " ", sizeof( rpl ));
542 ngt_TrimLastChr( rpl, ' ');
544 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
549 IRC_VERSION( CLIENT *Client, REQUEST *Req )
551 CLIENT *target, *prefix;
553 char ver[12], vertxt[30];
556 assert( Client != NULL );
557 assert( Req != NULL );
559 /* Falsche Anzahl Parameter? */
560 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
563 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
564 else target = Client_ThisServer( );
566 /* Prefix ermitteln */
567 if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
568 else prefix = Client;
569 if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
571 /* An anderen Server weiterleiten? */
572 if( target != Client_ThisServer( ))
574 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
577 IRC_WriteStrClientPrefix( target, prefix, "VERSION %s", Req->argv[0] );
581 /* mit Versionsinfo antworten */
582 IRC_SetPenalty( Client, 1 );
584 strlcpy( ver, CVSDATE, sizeof( ver ));
585 strncpy( ver + 4, ver + 5, 2 );
586 strncpy( ver + 6, ver + 8, 3 );
587 snprintf( vertxt, sizeof( vertxt ), "%s(%s)", PACKAGE_VERSION, ver );
588 return IRC_WriteStrClient( Client, RPL_VERSION_MSG, Client_ID( prefix ), PACKAGE_NAME, vertxt, NGIRCd_DebugLevel, Conf_ServerName, NGIRCd_VersionAddition );
590 return IRC_WriteStrClient( Client, RPL_VERSION_MSG, Client_ID( prefix ), PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel, Conf_ServerName, NGIRCd_VersionAddition );
596 IRC_WHO( CLIENT *Client, REQUEST *Req )
605 assert( Client != NULL );
606 assert( Req != NULL );
608 /* Falsche Anzahl Parameter? */
609 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
616 /* Nur OPs anzeigen? */
617 if( strcmp( Req->argv[1], "o" ) == 0 ) only_ops = true;
619 else return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
625 /* wurde ein Channel oder Nick-Mask angegeben? */
626 chan = Channel_Search( Req->argv[0] );
631 /* User eines Channels ausgeben */
632 if( ! IRC_Send_WHO( Client, chan, only_ops )) return DISCONNECTED;
638 if(( Client_Type( c ) == CLIENT_USER ) && ( ! strchr( Client_Modes( c ), 'i' )))
641 if( Req->argc == 0 ) ok = true;
644 if( strcasecmp( Req->argv[0], Client_ID( c )) == 0 ) ok = true;
645 else if( strcmp( Req->argv[0], "0" ) == 0 ) ok = true;
648 if( ok && (( ! only_ops ) || ( strchr( Client_Modes( c ), 'o' ))))
651 strcpy( flags, "H" );
652 if( strchr( Client_Modes( c ), 'o' )) strlcat( flags, "*", sizeof( flags ));
654 /* Search suitable channel */
655 cl2chan = Channel_FirstChannelOf( c );
658 cn = Channel_GetChannel( cl2chan );
659 if( Channel_IsMemberOf( cn, Client ) ||
660 ! strchr( Channel_Modes( cn ), 's' ))
662 ptr = Channel_Name( cn );
665 cl2chan = Channel_NextChannelOf( c, cl2chan );
667 if( ! cl2chan ) ptr = "*";
669 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;
673 /* naechster Client */
674 c = Client_Next( c );
677 if( chan ) return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), Channel_Name( chan ));
678 else if( Req->argc == 0 ) return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), "*" );
679 else return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), Req->argv[0] );
684 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
686 CLIENT *from, *target, *c;
687 char str[LINE_LEN + 1];
691 assert( Client != NULL );
692 assert( Req != NULL );
694 /* Bad number of parameters? */
695 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
698 c = Client_Search( Req->argv[Req->argc - 1] );
699 if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[Req->argc - 1] );
701 /* Search sender of the WHOIS */
702 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
704 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
706 /* Forward to other server? */
709 /* Search target server (can be specified as nick of that server!) */
710 target = Client_Search( Req->argv[0] );
711 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
713 else target = Client_ThisServer( );
715 assert( target != NULL );
717 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] );
719 /* Nick, user and name */
720 if( ! IRC_WriteStrClient( from, RPL_WHOISUSER_MSG, Client_ID( from ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
723 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;
726 snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
727 cl2chan = Channel_FirstChannelOf( c );
730 chan = Channel_GetChannel( cl2chan );
731 assert( chan != NULL );
734 cl2chan = Channel_NextChannelOf( c, cl2chan );
736 /* Secret channel? */
737 if( strchr( Channel_Modes( chan ), 's' ) && ! Channel_IsMemberOf( chan, Client )) continue;
739 /* Concatenate channel names */
740 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
741 if( strchr( Channel_UserModes( chan, c ), 'o' )) strlcat( str, "@", sizeof( str ));
742 else if( strchr( Channel_UserModes( chan, c ), 'v' )) strlcat( str, "+", sizeof( str ));
743 strlcat( str, Channel_Name( chan ), sizeof( str ));
745 if( strlen( str ) > ( LINE_LEN - CHANNEL_NAME_LEN - 4 ))
747 /* Line becomes too long: send it! */
748 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
749 snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
752 if( str[strlen( str ) - 1] != ':')
754 /* There is data left to send: */
755 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
759 if( Client_HasMode( c, 'o' ))
761 if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
764 /* Idle and signon time (local clients only!) */
765 if (Client_Conn(c) > NONE ) {
766 if (! IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
767 Client_ID(from), Client_ID(c),
768 (unsigned long)Conn_GetIdle(Client_Conn(c)),
769 (unsigned long)Conn_GetSignon(Client_Conn(c))))
774 if( Client_HasMode( c, 'a' ))
776 if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( c ), Client_Away( c ))) return DISCONNECTED;
780 return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
785 * IRC "WHOWAS" function.
786 * This function implements the IRC command "WHOWHAS". It handles local
787 * requests and request that should be forwarded to other servers.
790 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
792 CLIENT *target, *prefix;
794 int max, last, count, i;
797 assert( Client != NULL );
798 assert( Req != NULL );
800 /* Wrong number of parameters? */
801 if(( Req->argc < 1 ) || ( Req->argc > 3 ))
802 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
803 Client_ID( Client ), Req->command );
807 target = Client_Search( Req->argv[2] );
809 target = Client_ThisServer( );
812 if( Client_Type( Client ) == CLIENT_SERVER )
813 prefix = Client_Search( Req->prefix );
818 return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG,
819 Client_ID( Client ), Req->prefix );
821 /* Forward to other server? */
822 if( target != Client_ThisServer( ))
824 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
825 return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG,
830 IRC_WriteStrClientPrefix( target, prefix, "WHOWAS %s %s %s",
831 Req->argv[0], Req->argv[1],
836 whowas = Client_GetWhowas( );
837 last = Client_GetLastWhowasIndex( );
838 if( last < 0 ) last = 0;
842 max = atoi( Req->argv[1] );
843 if( max < 1 ) max = MAX_WHOWAS;
846 max = DEFAULT_WHOWAS;
853 if( whowas[i].time > 0 &&
854 strcasecmp( Req->argv[0], whowas[i].id ) == 0 )
856 (void)strftime( t_str, sizeof(t_str),
857 "%a %b %d %H:%M:%S %Y",
858 localtime( &whowas[i].time ));
860 if( ! IRC_WriteStrClient( prefix, RPL_WHOWASUSER_MSG,
868 if( ! IRC_WriteStrClient( prefix, RPL_WHOISSERVER_MSG,
871 whowas[i].server, t_str ))
875 if( count >= max ) break;
881 /* "underflow", wrap around */
882 if( i < 0 ) i = MAX_WHOWAS - 1;
883 } while( i != last );
885 return IRC_WriteStrClient( prefix, RPL_ENDOFWHOWAS_MSG,
886 Client_ID( prefix ), Req->argv[0] );
891 IRC_Send_LUSERS( CLIENT *Client )
898 assert( Client != NULL );
900 /* Users, services and serevers in the network */
901 if( ! IRC_WriteStrClient( Client, RPL_LUSERCLIENT_MSG, Client_ID( Client ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
903 /* Number of IRC operators */
904 cnt = Client_OperCount( );
907 if( ! IRC_WriteStrClient( Client, RPL_LUSEROP_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
910 /* Unknown connections */
911 cnt = Client_UnknownCount( );
914 if( ! IRC_WriteStrClient( Client, RPL_LUSERUNKNOWN_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
917 /* Number of created channels */
918 if( ! IRC_WriteStrClient( Client, RPL_LUSERCHANNELS_MSG, Client_ID( Client ), Channel_Count( ))) return DISCONNECTED;
920 /* Number of local users, services and servers */
921 if( ! IRC_WriteStrClient( Client, RPL_LUSERME_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
924 /* Maximum number of local users */
925 cnt = Client_MyUserCount();
926 max = Client_MyMaxUserCount();
927 if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
930 /* Maximum number of users in the network */
931 cnt = Client_UserCount();
932 max = Client_MaxUserCount();
933 if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
939 } /* IRC_Send_LUSERS */
943 Show_MOTD_Start(CLIENT *Client)
945 return IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG,
946 Client_ID( Client ), Client_ID( Client_ThisServer( )));
950 Show_MOTD_Sendline(CLIENT *Client, const char *msg)
952 return IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID( Client ), msg);
956 Show_MOTD_End(CLIENT *Client)
958 return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
963 IRC_Show_MOTD( CLIENT *Client )
968 assert( Client != NULL );
970 if (Conf_MotdPhrase[0]) {
971 if (!Show_MOTD_Start(Client))
973 if (!Show_MOTD_Sendline(Client, Conf_MotdPhrase))
976 return Show_MOTD_End(Client);
979 fd = fopen( Conf_MotdFile, "r" );
981 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
982 return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
985 if (!Show_MOTD_Start( Client )) {
990 while (fgets( line, (int)sizeof line, fd )) {
991 ngt_TrimLastChr( line, '\n');
993 if( ! Show_MOTD_Sendline( Client, line)) {
999 return Show_MOTD_End(Client);
1000 } /* IRC_Show_MOTD */
1004 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
1006 bool is_visible, is_member;
1007 char str[LINE_LEN + 1];
1011 assert( Client != NULL );
1012 assert( Chan != NULL );
1014 if( Channel_IsMemberOf( Chan, Client )) is_member = true;
1015 else is_member = false;
1017 /* Secret channel? */
1018 if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
1020 /* Alle Mitglieder suchen */
1021 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1022 cl2chan = Channel_FirstMember( Chan );
1025 cl = Channel_GetClient( cl2chan );
1027 if( strchr( Client_Modes( cl ), 'i' )) is_visible = false;
1028 else is_visible = true;
1030 if( is_member || is_visible )
1032 /* Nick anhaengen */
1033 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
1034 if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
1035 else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
1036 strlcat( str, Client_ID( cl ), sizeof( str ));
1038 if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
1040 /* Zeile wird zu lang: senden! */
1041 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1042 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1046 /* naechstes Mitglied suchen */
1047 cl2chan = Channel_NextMember( Chan, cl2chan );
1049 if( str[strlen( str ) - 1] != ':')
1051 /* Es sind noch Daten da, die gesendet werden muessen */
1052 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1056 } /* IRC_Send_NAMES */
1060 IRC_Send_WHO( CLIENT *Client, CHANNEL *Chan, bool OnlyOps )
1062 bool is_visible, is_member;
1067 assert( Client != NULL );
1068 assert( Chan != NULL );
1070 if( Channel_IsMemberOf( Chan, Client )) is_member = true;
1071 else is_member = false;
1073 /* Secret channel? */
1074 if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
1076 /* Alle Mitglieder suchen */
1077 cl2chan = Channel_FirstMember( Chan );
1080 c = Channel_GetClient( cl2chan );
1082 if( strchr( Client_Modes( c ), 'i' )) is_visible = false;
1083 else is_visible = true;
1085 if( is_member || is_visible )
1087 /* Flags zusammenbasteln */
1088 strcpy( flags, "H" );
1089 if( strchr( Client_Modes( c ), 'o' )) strlcat( flags, "*", sizeof( flags ));
1090 if( strchr( Channel_UserModes( Chan, c ), 'o' )) strlcat( flags, "@", sizeof( flags ));
1091 else if( strchr( Channel_UserModes( Chan, c ), 'v' )) strlcat( flags, "+", sizeof( flags ));
1094 if(( ! OnlyOps ) || ( strchr( Client_Modes( c ), 'o' )))
1096 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;
1100 /* naechstes Mitglied suchen */
1101 cl2chan = Channel_NextMember( Chan, cl2chan );
1104 } /* IRC_Send_WHO */
1108 * Send the ISUPPORT numeric (005).
1109 * This numeric indicates the features that are supported by this server.
1110 * See <http://www.irc.org/tech_docs/005.html> for details.
1113 IRC_Send_ISUPPORT PARAMS((CLIENT * Client))
1115 if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1117 return DISCONNECTED;
1118 return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1119 CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1120 COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1122 } /* IRC_Send_ISUPPORT */