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.2.1 2008/02/26 12:06:57 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 if (strchr(Client_Modes( c ), 'a'))
652 strcpy(flags, "G"); /* away */
656 if( strchr( Client_Modes( c ), 'o' )) strlcat( flags, "*", sizeof( flags ));
658 /* Search suitable channel */
659 cl2chan = Channel_FirstChannelOf( c );
662 cn = Channel_GetChannel( cl2chan );
663 if( Channel_IsMemberOf( cn, Client ) ||
664 ! strchr( Channel_Modes( cn ), 's' ))
666 ptr = Channel_Name( cn );
669 cl2chan = Channel_NextChannelOf( c, cl2chan );
671 if( ! cl2chan ) ptr = "*";
673 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;
677 /* naechster Client */
678 c = Client_Next( c );
681 if( chan ) return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), Channel_Name( chan ));
682 else if( Req->argc == 0 ) return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), "*" );
683 else return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), Req->argv[0] );
688 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
690 CLIENT *from, *target, *c;
691 char str[LINE_LEN + 1];
695 assert( Client != NULL );
696 assert( Req != NULL );
698 /* Bad number of parameters? */
699 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
702 c = Client_Search( Req->argv[Req->argc - 1] );
703 if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[Req->argc - 1] );
705 /* Search sender of the WHOIS */
706 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
708 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
710 /* Forward to other server? */
713 /* Search target server (can be specified as nick of that server!) */
714 target = Client_Search( Req->argv[0] );
715 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
717 else target = Client_ThisServer( );
719 assert( target != NULL );
721 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] );
723 /* Nick, user and name */
724 if( ! IRC_WriteStrClient( from, RPL_WHOISUSER_MSG, Client_ID( from ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
727 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;
730 snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
731 cl2chan = Channel_FirstChannelOf( c );
734 chan = Channel_GetChannel( cl2chan );
735 assert( chan != NULL );
738 cl2chan = Channel_NextChannelOf( c, cl2chan );
740 /* Secret channel? */
741 if( strchr( Channel_Modes( chan ), 's' ) && ! Channel_IsMemberOf( chan, Client )) continue;
743 /* Concatenate channel names */
744 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
745 if( strchr( Channel_UserModes( chan, c ), 'o' )) strlcat( str, "@", sizeof( str ));
746 else if( strchr( Channel_UserModes( chan, c ), 'v' )) strlcat( str, "+", sizeof( str ));
747 strlcat( str, Channel_Name( chan ), sizeof( str ));
749 if( strlen( str ) > ( LINE_LEN - CHANNEL_NAME_LEN - 4 ))
751 /* Line becomes too long: send it! */
752 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
753 snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
756 if( str[strlen( str ) - 1] != ':')
758 /* There is data left to send: */
759 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
763 if( Client_HasMode( c, 'o' ))
765 if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
768 /* Idle and signon time (local clients only!) */
769 if (Client_Conn(c) > NONE ) {
770 if (! IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
771 Client_ID(from), Client_ID(c),
772 (unsigned long)Conn_GetIdle(Client_Conn(c)),
773 (unsigned long)Conn_GetSignon(Client_Conn(c))))
778 if( Client_HasMode( c, 'a' ))
780 if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( c ), Client_Away( c ))) return DISCONNECTED;
784 return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
789 * IRC "WHOWAS" function.
790 * This function implements the IRC command "WHOWHAS". It handles local
791 * requests and request that should be forwarded to other servers.
794 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
796 CLIENT *target, *prefix;
798 int max, last, count, i;
801 assert( Client != NULL );
802 assert( Req != NULL );
804 /* Wrong number of parameters? */
805 if(( Req->argc < 1 ) || ( Req->argc > 3 ))
806 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
807 Client_ID( Client ), Req->command );
811 target = Client_Search( Req->argv[2] );
813 target = Client_ThisServer( );
816 if( Client_Type( Client ) == CLIENT_SERVER )
817 prefix = Client_Search( Req->prefix );
822 return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG,
823 Client_ID( Client ), Req->prefix );
825 /* Forward to other server? */
826 if( target != Client_ThisServer( ))
828 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
829 return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG,
834 IRC_WriteStrClientPrefix( target, prefix, "WHOWAS %s %s %s",
835 Req->argv[0], Req->argv[1],
840 whowas = Client_GetWhowas( );
841 last = Client_GetLastWhowasIndex( );
842 if( last < 0 ) last = 0;
846 max = atoi( Req->argv[1] );
847 if( max < 1 ) max = MAX_WHOWAS;
850 max = DEFAULT_WHOWAS;
857 if( whowas[i].time > 0 &&
858 strcasecmp( Req->argv[0], whowas[i].id ) == 0 )
860 (void)strftime( t_str, sizeof(t_str),
861 "%a %b %d %H:%M:%S %Y",
862 localtime( &whowas[i].time ));
864 if( ! IRC_WriteStrClient( prefix, RPL_WHOWASUSER_MSG,
872 if( ! IRC_WriteStrClient( prefix, RPL_WHOISSERVER_MSG,
875 whowas[i].server, t_str ))
879 if( count >= max ) break;
885 /* "underflow", wrap around */
886 if( i < 0 ) i = MAX_WHOWAS - 1;
887 } while( i != last );
889 return IRC_WriteStrClient( prefix, RPL_ENDOFWHOWAS_MSG,
890 Client_ID( prefix ), Req->argv[0] );
895 IRC_Send_LUSERS( CLIENT *Client )
902 assert( Client != NULL );
904 /* Users, services and serevers in the network */
905 if( ! IRC_WriteStrClient( Client, RPL_LUSERCLIENT_MSG, Client_ID( Client ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
907 /* Number of IRC operators */
908 cnt = Client_OperCount( );
911 if( ! IRC_WriteStrClient( Client, RPL_LUSEROP_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
914 /* Unknown connections */
915 cnt = Client_UnknownCount( );
918 if( ! IRC_WriteStrClient( Client, RPL_LUSERUNKNOWN_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
921 /* Number of created channels */
922 if( ! IRC_WriteStrClient( Client, RPL_LUSERCHANNELS_MSG, Client_ID( Client ), Channel_Count( ))) return DISCONNECTED;
924 /* Number of local users, services and servers */
925 if( ! IRC_WriteStrClient( Client, RPL_LUSERME_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
928 /* Maximum number of local users */
929 cnt = Client_MyUserCount();
930 max = Client_MyMaxUserCount();
931 if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
934 /* Maximum number of users in the network */
935 cnt = Client_UserCount();
936 max = Client_MaxUserCount();
937 if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
943 } /* IRC_Send_LUSERS */
947 Show_MOTD_Start(CLIENT *Client)
949 return IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG,
950 Client_ID( Client ), Client_ID( Client_ThisServer( )));
954 Show_MOTD_Sendline(CLIENT *Client, const char *msg)
956 return IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID( Client ), msg);
960 Show_MOTD_End(CLIENT *Client)
962 return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
967 IRC_Show_MOTD( CLIENT *Client )
972 assert( Client != NULL );
974 if (Conf_MotdPhrase[0]) {
975 if (!Show_MOTD_Start(Client))
977 if (!Show_MOTD_Sendline(Client, Conf_MotdPhrase))
980 return Show_MOTD_End(Client);
983 fd = fopen( Conf_MotdFile, "r" );
985 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
986 return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
989 if (!Show_MOTD_Start( Client )) {
994 while (fgets( line, (int)sizeof line, fd )) {
995 ngt_TrimLastChr( line, '\n');
997 if( ! Show_MOTD_Sendline( Client, line)) {
1003 return Show_MOTD_End(Client);
1004 } /* IRC_Show_MOTD */
1008 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
1010 bool is_visible, is_member;
1011 char str[LINE_LEN + 1];
1015 assert( Client != NULL );
1016 assert( Chan != NULL );
1018 if( Channel_IsMemberOf( Chan, Client )) is_member = true;
1019 else is_member = false;
1021 /* Secret channel? */
1022 if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
1024 /* Alle Mitglieder suchen */
1025 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1026 cl2chan = Channel_FirstMember( Chan );
1029 cl = Channel_GetClient( cl2chan );
1031 if( strchr( Client_Modes( cl ), 'i' )) is_visible = false;
1032 else is_visible = true;
1034 if( is_member || is_visible )
1036 /* Nick anhaengen */
1037 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
1038 if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
1039 else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
1040 strlcat( str, Client_ID( cl ), sizeof( str ));
1042 if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
1044 /* Zeile wird zu lang: senden! */
1045 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1046 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1050 /* naechstes Mitglied suchen */
1051 cl2chan = Channel_NextMember( Chan, cl2chan );
1053 if( str[strlen( str ) - 1] != ':')
1055 /* Es sind noch Daten da, die gesendet werden muessen */
1056 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1060 } /* IRC_Send_NAMES */
1064 IRC_Send_WHO( CLIENT *Client, CHANNEL *Chan, bool OnlyOps )
1066 bool is_visible, is_member;
1071 assert( Client != NULL );
1072 assert( Chan != NULL );
1074 if( Channel_IsMemberOf( Chan, Client )) is_member = true;
1075 else is_member = false;
1077 /* Secret channel? */
1078 if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
1080 /* Alle Mitglieder suchen */
1081 cl2chan = Channel_FirstMember( Chan );
1084 c = Channel_GetClient( cl2chan );
1086 if( strchr( Client_Modes( c ), 'i' )) is_visible = false;
1087 else is_visible = true;
1089 if( is_member || is_visible ) {
1090 if (strchr(Client_Modes( c ), 'a'))
1091 strcpy(flags, "G"); /* away */
1095 if( strchr( Client_Modes( c ), 'o' )) strlcat( flags, "*", sizeof( flags ));
1096 if( strchr( Channel_UserModes( Chan, c ), 'o' )) strlcat( flags, "@", sizeof( flags ));
1097 else if( strchr( Channel_UserModes( Chan, c ), 'v' )) strlcat( flags, "+", sizeof( flags ));
1100 if(( ! OnlyOps ) || ( strchr( Client_Modes( c ), 'o' )))
1102 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;
1106 /* naechstes Mitglied suchen */
1107 cl2chan = Channel_NextMember( Chan, cl2chan );
1110 } /* IRC_Send_WHO */
1114 * Send the ISUPPORT numeric (005).
1115 * This numeric indicates the features that are supported by this server.
1116 * See <http://www.irc.org/tech_docs/005.html> for details.
1119 IRC_Send_ISUPPORT PARAMS((CLIENT * Client))
1121 if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1123 return DISCONNECTED;
1124 return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1125 CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1126 COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1128 } /* IRC_Send_ISUPPORT */