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.42 2008/02/11 11:06:31 fw Exp $";
28 #include "cvs-version.h"
29 #include "conn-func.h"
41 #include "irc-write.h"
48 IRC_ADMIN(CLIENT *Client, REQUEST *Req )
50 CLIENT *target, *prefix;
52 assert( Client != NULL );
53 assert( Req != NULL );
55 /* Falsche Anzahl Parameter? */
56 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
59 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
60 else target = Client_ThisServer( );
62 /* Prefix ermitteln */
63 if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
65 if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
67 /* An anderen Server weiterleiten? */
68 if( target != Client_ThisServer( ))
70 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
73 IRC_WriteStrClientPrefix( target, prefix, "ADMIN %s", Req->argv[0] );
77 /* mit Versionsinfo antworten */
78 if( ! IRC_WriteStrClient( Client, RPL_ADMINME_MSG, Client_ID( prefix ), Conf_ServerName )) return DISCONNECTED;
79 if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC1_MSG, Client_ID( prefix ), Conf_ServerAdmin1 )) return DISCONNECTED;
80 if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC2_MSG, Client_ID( prefix ), Conf_ServerAdmin2 )) return DISCONNECTED;
81 if( ! IRC_WriteStrClient( Client, RPL_ADMINEMAIL_MSG, Client_ID( prefix ), Conf_ServerAdminMail )) return DISCONNECTED;
83 IRC_SetPenalty( Client, 1 );
89 IRC_ISON( CLIENT *Client, REQUEST *Req )
91 char rpl[COMMAND_LEN];
96 assert( Client != NULL );
97 assert( Req != NULL );
99 /* Falsche Anzahl Parameter? */
100 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
102 strlcpy( rpl, RPL_ISON_MSG, sizeof rpl );
103 for( i = 0; i < Req->argc; i++ )
105 ptr = strtok( Req->argv[i], " " );
109 c = Client_Search( ptr );
110 if( c && ( Client_Type( c ) == CLIENT_USER ))
112 /* Dieser Nick ist "online" */
113 strlcat( rpl, ptr, sizeof( rpl ));
114 strlcat( rpl, " ", sizeof( rpl ));
116 ptr = strtok( NULL, " " );
119 ngt_TrimLastChr(rpl, ' ');
121 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
126 IRC_LINKS( CLIENT *Client, REQUEST *Req )
128 CLIENT *target, *from, *c;
131 assert( Client != NULL );
132 assert( Req != NULL );
134 /* Falsche Anzahl Parameter? */
135 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
137 /* Server-Mask ermitteln */
138 if( Req->argc > 0 ) mask = Req->argv[Req->argc - 1];
141 /* Absender ermitteln */
142 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
144 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
146 /* An anderen Server forwarden? */
149 target = Client_Search( Req->argv[0] );
150 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
151 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LINKS %s %s", Req->argv[0], Req->argv[1] );
154 /* Wer ist der Absender? */
155 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
156 else target = Client;
157 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
162 if( Client_Type( c ) == CLIENT_SERVER )
164 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;
166 c = Client_Next( c );
169 IRC_SetPenalty( target, 1 );
170 return IRC_WriteStrClient( target, RPL_ENDOFLINKS_MSG, Client_ID( target ), mask );
175 IRC_LUSERS( CLIENT *Client, REQUEST *Req )
177 CLIENT *target, *from;
179 assert( Client != NULL );
180 assert( Req != NULL );
182 /* Falsche Anzahl Parameter? */
183 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
185 /* Absender ermitteln */
186 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
188 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
190 /* An anderen Server forwarden? */
193 target = Client_Search( Req->argv[1] );
194 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
195 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LUSERS %s %s", Req->argv[0], Req->argv[1] );
198 /* Wer ist der Absender? */
199 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
200 else target = Client;
201 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
203 IRC_Send_LUSERS( target );
205 IRC_SetPenalty( target, 1 );
211 IRC_MOTD( CLIENT *Client, REQUEST *Req )
213 CLIENT *from, *target;
215 assert( Client != NULL );
216 assert( Req != NULL );
218 /* Falsche Anzahl Parameter? */
219 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
221 /* From aus Prefix ermitteln */
222 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
224 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
228 /* an anderen Server forwarden */
229 target = Client_Search( Req->argv[0] );
230 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
232 if( target != Client_ThisServer( ))
234 /* Ok, anderer Server ist das Ziel: forwarden */
235 return IRC_WriteStrClientPrefix( target, from, "MOTD %s", Req->argv[0] );
239 IRC_SetPenalty( from, 3 );
240 return IRC_Show_MOTD( from );
245 IRC_NAMES( CLIENT *Client, REQUEST *Req )
247 char rpl[COMMAND_LEN], *ptr;
248 CLIENT *target, *from, *c;
251 assert( Client != NULL );
252 assert( Req != NULL );
254 /* Falsche Anzahl Parameter? */
255 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
257 /* From aus Prefix ermitteln */
258 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
260 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
264 /* an anderen Server forwarden */
265 target = Client_Search( Req->argv[1] );
266 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
268 if( target != Client_ThisServer( ))
270 /* Ok, anderer Server ist das Ziel: forwarden */
271 return IRC_WriteStrClientPrefix( target, from, "NAMES %s :%s", Req->argv[0], Req->argv[1] );
277 /* bestimmte Channels durchgehen */
278 ptr = strtok( Req->argv[0], "," );
281 chan = Channel_Search( ptr );
285 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
287 if( ! IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), ptr )) return DISCONNECTED;
289 /* naechsten Namen ermitteln */
290 ptr = strtok( NULL, "," );
295 /* alle Channels durchgehen */
296 chan = Channel_First( );
300 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
302 /* naechster Channel */
303 chan = Channel_Next( chan );
306 /* Nun noch alle Clients ausgeben, die in keinem Channel sind */
308 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
311 if(( Client_Type( c ) == CLIENT_USER ) && ( Channel_FirstChannelOf( c ) == NULL ) && ( ! strchr( Client_Modes( c ), 'i' )))
313 /* Okay, das ist ein User: anhaengen */
314 if( rpl[strlen( rpl ) - 1] != ':' ) strlcat( rpl, " ", sizeof( rpl ));
315 strlcat( rpl, Client_ID( c ), sizeof( rpl ));
317 if( strlen( rpl ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
319 /* Zeile wird zu lang: senden! */
320 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
321 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
325 /* naechster Client */
326 c = Client_Next( c );
328 if( rpl[strlen( rpl ) - 1] != ':')
330 /* es wurden User gefunden */
331 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
334 IRC_SetPenalty( from, 1 );
335 return IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), "*" );
340 t_diff(time_t *t, const time_t div)
354 uptime_days(time_t *now)
356 return t_diff(now, 60 * 60 * 24);
361 uptime_hrs(time_t *now)
363 return t_diff(now, 60 * 60);
368 uptime_mins(time_t *now)
370 return t_diff(now, 60);
375 IRC_STATS( CLIENT *Client, REQUEST *Req )
377 CLIENT *from, *target, *cl;
382 unsigned int days, hrs, mins;
384 assert( Client != NULL );
385 assert( Req != NULL );
387 /* Falsche Anzahl Parameter? */
389 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
391 /* From aus Prefix ermitteln */
392 if (Client_Type(Client) == CLIENT_SERVER)
393 from = Client_Search(Req->prefix);
398 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix);
400 if (Req->argc == 2) {
401 /* an anderen Server forwarden */
402 target = Client_Search( Req->argv[1] );
403 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
404 return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
406 if( target != Client_ThisServer()) {
407 /* Ok, anderer Server ist das Ziel: forwarden */
408 return IRC_WriteStrClientPrefix( target, from, "STATS %s %s", Req->argv[0], Req->argv[1] );
413 query = Req->argv[0][0] ? Req->argv[0][0] : '*';
418 case 'l': /* Links */
420 time_now = time(NULL);
421 for (con = Conn_First(); con != NONE ;con = Conn_Next(con)) {
422 cl = Conn_GetClient(con);
425 if ((Client_Type(cl) == CLIENT_SERVER) || (cl == Client)) {
426 /* Server link or our own connection */
428 if (Conn_Options(con) & CONN_ZIP) {
429 if (!IRC_WriteStrClient(from, RPL_STATSLINKINFOZIP_MSG,
430 Client_ID(from), Client_Mask(cl), Conn_SendQ(con),
431 Conn_SendMsg(con), Zip_SendBytes(con), Conn_SendBytes(con),
432 Conn_RecvMsg(con), Zip_RecvBytes(con), Conn_RecvBytes(con), (long)(time_now - Conn_StartTime(con))))
437 if (!IRC_WriteStrClient(from, RPL_STATSLINKINFO_MSG, Client_ID(from),
438 Client_Mask(cl), Conn_SendQ(con), Conn_SendMsg(con), Conn_SendBytes(con),
439 Conn_RecvMsg(con), Conn_RecvBytes(con), (long)(time_now - Conn_StartTime(con))))
444 case 'm': /* IRC-Commands */
446 cmd = Parse_GetCommandStruct( );
447 for (; cmd->name ; cmd++) {
448 if (cmd->lcount == 0 && cmd->rcount == 0)
450 if (!IRC_WriteStrClient(from, RPL_STATSCOMMANDS_MSG, Client_ID(from),
451 cmd->name, cmd->lcount, cmd->bytes, cmd->rcount))
455 case 'u': /* server uptime */
457 time_now = time(NULL) - NGIRCd_Start;
458 days = uptime_days(&time_now);
459 hrs = uptime_hrs(&time_now);
460 mins = uptime_mins(&time_now);
461 if (!IRC_WriteStrClient(from, RPL_STATSUPTIME, Client_ID(from),
462 days, hrs, mins, (unsigned int) time_now))
467 IRC_SetPenalty(from, 2);
468 return IRC_WriteStrClient(from, RPL_ENDOFSTATS_MSG, Client_ID(from), query);
473 IRC_TIME( CLIENT *Client, REQUEST *Req )
475 CLIENT *from, *target;
479 assert( Client != NULL );
480 assert( Req != NULL );
482 /* Falsche Anzahl Parameter? */
483 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
485 /* From aus Prefix ermitteln */
486 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
488 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
492 /* an anderen Server forwarden */
493 target = Client_Search( Req->argv[0] );
494 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
496 if( target != Client_ThisServer( ))
498 /* Ok, anderer Server ist das Ziel: forwarden */
499 return IRC_WriteStrClientPrefix( target, from, "TIME %s", Req->argv[0] );
504 (void)strftime( t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime( &t ));
505 return IRC_WriteStrClient( from, RPL_TIME_MSG, Client_ID( from ), Client_ID( Client_ThisServer( )), t_str );
510 IRC_USERHOST( CLIENT *Client, REQUEST *Req )
512 char rpl[COMMAND_LEN];
516 assert( Client != NULL );
517 assert( Req != NULL );
519 /* Falsche Anzahl Parameter? */
520 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
522 if( Req->argc > 5 ) max = 5;
523 else max = Req->argc;
525 strlcpy( rpl, RPL_USERHOST_MSG, sizeof rpl );
526 for( i = 0; i < max; i++ )
528 c = Client_Search( Req->argv[i] );
529 if( c && ( Client_Type( c ) == CLIENT_USER ))
531 /* Dieser Nick ist "online" */
532 strlcat( rpl, Client_ID( c ), sizeof( rpl ));
533 if( Client_HasMode( c, 'o' )) strlcat( rpl, "*", sizeof( rpl ));
534 strlcat( rpl, "=", sizeof( rpl ));
535 if( Client_HasMode( c, 'a' )) strlcat( rpl, "-", sizeof( rpl ));
536 else strlcat( rpl, "+", sizeof( rpl ));
537 strlcat( rpl, Client_User( c ), sizeof( rpl ));
538 strlcat( rpl, "@", sizeof( rpl ));
539 strlcat( rpl, Client_Hostname( c ), sizeof( rpl ));
540 strlcat( rpl, " ", sizeof( rpl ));
543 ngt_TrimLastChr( rpl, ' ');
545 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
550 IRC_VERSION( CLIENT *Client, REQUEST *Req )
552 CLIENT *target, *prefix;
554 char ver[12], vertxt[30];
557 assert( Client != NULL );
558 assert( Req != NULL );
560 /* Falsche Anzahl Parameter? */
561 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
564 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
565 else target = Client_ThisServer( );
567 /* Prefix ermitteln */
568 if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
569 else prefix = Client;
570 if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
572 /* An anderen Server weiterleiten? */
573 if( target != Client_ThisServer( ))
575 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
578 IRC_WriteStrClientPrefix( target, prefix, "VERSION %s", Req->argv[0] );
582 /* mit Versionsinfo antworten */
583 IRC_SetPenalty( Client, 1 );
585 strlcpy( ver, CVSDATE, sizeof( ver ));
586 strncpy( ver + 4, ver + 5, 2 );
587 strncpy( ver + 6, ver + 8, 3 );
588 snprintf( vertxt, sizeof( vertxt ), "%s(%s)", PACKAGE_VERSION, ver );
589 return IRC_WriteStrClient( Client, RPL_VERSION_MSG, Client_ID( prefix ), PACKAGE_NAME, vertxt, NGIRCd_DebugLevel, Conf_ServerName, NGIRCd_VersionAddition );
591 return IRC_WriteStrClient( Client, RPL_VERSION_MSG, Client_ID( prefix ), PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel, Conf_ServerName, NGIRCd_VersionAddition );
598 write_whoreply(CLIENT *Client, CLIENT *c, const char *channelname, const char *flags)
600 return IRC_WriteStrClient(Client, RPL_WHOREPLY_MSG, Client_ID(Client), channelname,
601 Client_User(c), Client_Hostname(c), Client_ID(Client_Introducer(c)), Client_ID(c),
602 flags, Client_Hops(c), Client_Info(c));
607 IRC_Send_WHO(CLIENT *Client, CHANNEL *Chan, bool OnlyOps)
609 bool is_visible, is_member, is_ircop;
611 const char *client_modes;
612 const char *chan_user_modes;
616 assert( Client != NULL );
617 assert( Chan != NULL );
619 is_member = Channel_IsMemberOf(Chan, Client);
621 /* Secret channel? */
622 if (!is_member && strchr(Channel_Modes(Chan), 's'))
625 cl2chan = Channel_FirstMember(Chan);
626 for (; cl2chan ; cl2chan = Channel_NextMember(Chan, cl2chan)) {
627 c = Channel_GetClient(cl2chan);
629 client_modes = Client_Modes(c);
630 is_ircop = strchr(client_modes, 'o') != NULL;
631 if (OnlyOps && !is_ircop)
634 is_visible = strchr(client_modes, 'i') == NULL;
635 if (is_member || is_visible) {
636 if (strchr(client_modes, 'a'))
637 strcpy(flags, "G"); /* away */
641 strlcat(flags, "*", sizeof(flags));
643 chan_user_modes = Channel_UserModes(Chan, c);
644 if (strchr(chan_user_modes, 'o'))
645 strlcat(flags, "@", sizeof(flags));
646 else if (strchr(chan_user_modes, 'v'))
647 strlcat(flags, "+", sizeof(flags));
649 if (!write_whoreply(Client, c, Channel_Name(Chan), flags))
653 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), Channel_Name(Chan));
658 MatchCaseInsensitive(const char *pattern, const char *searchme)
660 char haystack[COMMAND_LEN];
662 strlcpy(haystack, searchme, sizeof(haystack));
664 ngt_LowerStr(haystack);
666 return Match(pattern, haystack);
671 IRC_WHO( CLIENT *Client, REQUEST *Req )
673 bool only_ops, have_arg, client_match;
674 const char *channelname, *client_modes;
675 char pattern[COMMAND_LEN];
681 assert( Client != NULL );
682 assert( Req != NULL );
685 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
690 if (Req->argc == 2) {
691 if (strcmp(Req->argv[1], "o") == 0)
694 else return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
698 IRC_SetPenalty(Client, 1);
699 if (Req->argc >= 1) { /* Channel or Mask. */
700 chan = Channel_Search(Req->argv[0]);
702 return IRC_Send_WHO(Client, chan, only_ops);
703 if (strcmp(Req->argv[0], "0") != 0) { /* RFC stupidity, same as no arguments */
705 strlcpy(pattern, Req->argv[0], sizeof(pattern));
706 ngt_LowerStr(pattern);
707 IRC_SetPenalty(Client, 3);
711 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
712 if (Client_Type(c) != CLIENT_USER)
716 * In the absence of the parameter, all visible (users who aren't
717 * invisible (user mode +i) and who don't have a common channel
718 * with the requesting client) are listed.
720 * The same result can be achieved by using a [sic] of "0"
721 * or any wildcard which will end up matching every visible user.
723 * The [sic] passed to WHO is matched against users' host, server, real name and
724 * nickname if the channel cannot be found.
726 client_modes = Client_Modes(c);
727 if (strchr(client_modes, 'i'))
730 if (only_ops && !strchr(client_modes, 'o'))
733 if (have_arg) { /* match pattern against user host/server/name/nick */
734 client_match = MatchCaseInsensitive(pattern, Client_Hostname(c)); /* user's host */
736 client_match = MatchCaseInsensitive(pattern, Client_ID(Client_Introducer(c))); /* server */
738 client_match = Match(Req->argv[0], Client_Info(c)); /* realname */
740 client_match = MatchCaseInsensitive(pattern, Client_ID(c)); /* nick name */
742 if (!client_match) /* This isn't the client you're looking for */
746 if (strchr(client_modes, 'a'))
747 strcpy(flags, "G"); /* user is away */
751 if (only_ops) /* this client is an operator */
752 strlcat(flags, "*", sizeof(flags));
754 /* Search suitable channel */
755 cl2chan = Channel_FirstChannelOf(c);
757 cn = Channel_GetChannel(cl2chan);
758 if (Channel_IsMemberOf(cn, Client) ||
759 !strchr(Channel_Modes(cn), 's'))
761 channelname = Channel_Name(cn);
764 cl2chan = Channel_NextChannelOf(c, cl2chan);
769 if (!write_whoreply(Client, c, channelname, flags))
774 channelname = Req->argv[0];
778 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), channelname);
783 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
785 CLIENT *from, *target, *c;
786 char str[LINE_LEN + 1];
790 assert( Client != NULL );
791 assert( Req != NULL );
793 /* Bad number of parameters? */
794 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
797 c = Client_Search( Req->argv[Req->argc - 1] );
798 if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[Req->argc - 1] );
800 /* Search sender of the WHOIS */
801 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
803 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
805 /* Forward to other server? */
808 /* Search target server (can be specified as nick of that server!) */
809 target = Client_Search( Req->argv[0] );
810 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
812 else target = Client_ThisServer( );
814 assert( target != NULL );
816 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] );
818 /* Nick, user and name */
819 if( ! IRC_WriteStrClient( from, RPL_WHOISUSER_MSG, Client_ID( from ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
822 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;
825 snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
826 cl2chan = Channel_FirstChannelOf( c );
829 chan = Channel_GetChannel( cl2chan );
830 assert( chan != NULL );
833 cl2chan = Channel_NextChannelOf( c, cl2chan );
835 /* Secret channel? */
836 if( strchr( Channel_Modes( chan ), 's' ) && ! Channel_IsMemberOf( chan, Client )) continue;
838 /* Concatenate channel names */
839 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
840 if( strchr( Channel_UserModes( chan, c ), 'o' )) strlcat( str, "@", sizeof( str ));
841 else if( strchr( Channel_UserModes( chan, c ), 'v' )) strlcat( str, "+", sizeof( str ));
842 strlcat( str, Channel_Name( chan ), sizeof( str ));
844 if( strlen( str ) > ( LINE_LEN - CHANNEL_NAME_LEN - 4 ))
846 /* Line becomes too long: send it! */
847 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
848 snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
851 if( str[strlen( str ) - 1] != ':')
853 /* There is data left to send: */
854 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
858 if( Client_HasMode( c, 'o' ))
860 if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
863 /* Idle and signon time (local clients only!) */
864 if (Client_Conn(c) > NONE ) {
865 if (! IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
866 Client_ID(from), Client_ID(c),
867 (unsigned long)Conn_GetIdle(Client_Conn(c)),
868 (unsigned long)Conn_GetSignon(Client_Conn(c))))
873 if( Client_HasMode( c, 'a' ))
875 if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( c ), Client_Away( c ))) return DISCONNECTED;
879 return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
884 * IRC "WHOWAS" function.
885 * This function implements the IRC command "WHOWHAS". It handles local
886 * requests and request that should be forwarded to other servers.
889 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
891 CLIENT *target, *prefix;
893 int max, last, count, i;
896 assert( Client != NULL );
897 assert( Req != NULL );
899 /* Wrong number of parameters? */
900 if(( Req->argc < 1 ) || ( Req->argc > 3 ))
901 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
902 Client_ID( Client ), Req->command );
906 target = Client_Search( Req->argv[2] );
908 target = Client_ThisServer( );
911 if( Client_Type( Client ) == CLIENT_SERVER )
912 prefix = Client_Search( Req->prefix );
917 return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG,
918 Client_ID( Client ), Req->prefix );
920 /* Forward to other server? */
921 if( target != Client_ThisServer( ))
923 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
924 return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG,
929 IRC_WriteStrClientPrefix( target, prefix, "WHOWAS %s %s %s",
930 Req->argv[0], Req->argv[1],
935 whowas = Client_GetWhowas( );
936 last = Client_GetLastWhowasIndex( );
937 if( last < 0 ) last = 0;
941 max = atoi( Req->argv[1] );
942 if( max < 1 ) max = MAX_WHOWAS;
945 max = DEFAULT_WHOWAS;
952 if( whowas[i].time > 0 &&
953 strcasecmp( Req->argv[0], whowas[i].id ) == 0 )
955 (void)strftime( t_str, sizeof(t_str),
956 "%a %b %d %H:%M:%S %Y",
957 localtime( &whowas[i].time ));
959 if( ! IRC_WriteStrClient( prefix, RPL_WHOWASUSER_MSG,
967 if( ! IRC_WriteStrClient( prefix, RPL_WHOISSERVER_MSG,
970 whowas[i].server, t_str ))
974 if( count >= max ) break;
980 /* "underflow", wrap around */
981 if( i < 0 ) i = MAX_WHOWAS - 1;
982 } while( i != last );
984 return IRC_WriteStrClient( prefix, RPL_ENDOFWHOWAS_MSG,
985 Client_ID( prefix ), Req->argv[0] );
990 IRC_Send_LUSERS( CLIENT *Client )
997 assert( Client != NULL );
999 /* Users, services and serevers in the network */
1000 if( ! IRC_WriteStrClient( Client, RPL_LUSERCLIENT_MSG, Client_ID( Client ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
1002 /* Number of IRC operators */
1003 cnt = Client_OperCount( );
1006 if( ! IRC_WriteStrClient( Client, RPL_LUSEROP_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
1009 /* Unknown connections */
1010 cnt = Client_UnknownCount( );
1013 if( ! IRC_WriteStrClient( Client, RPL_LUSERUNKNOWN_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
1016 /* Number of created channels */
1017 if( ! IRC_WriteStrClient( Client, RPL_LUSERCHANNELS_MSG, Client_ID( Client ), Channel_Count( ))) return DISCONNECTED;
1019 /* Number of local users, services and servers */
1020 if( ! IRC_WriteStrClient( Client, RPL_LUSERME_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
1023 /* Maximum number of local users */
1024 cnt = Client_MyUserCount();
1025 max = Client_MyMaxUserCount();
1026 if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1027 cnt, max, cnt, max))
1028 return DISCONNECTED;
1029 /* Maximum number of users in the network */
1030 cnt = Client_UserCount();
1031 max = Client_MaxUserCount();
1032 if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1033 cnt, max, cnt, max))
1034 return DISCONNECTED;
1038 } /* IRC_Send_LUSERS */
1042 Show_MOTD_Start(CLIENT *Client)
1044 return IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG,
1045 Client_ID( Client ), Client_ID( Client_ThisServer( )));
1049 Show_MOTD_Sendline(CLIENT *Client, const char *msg)
1051 return IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID( Client ), msg);
1055 Show_MOTD_End(CLIENT *Client)
1057 return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
1062 IRC_Show_MOTD( CLIENT *Client )
1067 assert( Client != NULL );
1069 if (Conf_MotdPhrase[0]) {
1070 if (!Show_MOTD_Start(Client))
1071 return DISCONNECTED;
1072 if (!Show_MOTD_Sendline(Client, Conf_MotdPhrase))
1073 return DISCONNECTED;
1075 return Show_MOTD_End(Client);
1078 fd = fopen( Conf_MotdFile, "r" );
1080 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
1081 return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
1084 if (!Show_MOTD_Start( Client )) {
1089 while (fgets( line, (int)sizeof line, fd )) {
1090 ngt_TrimLastChr( line, '\n');
1092 if( ! Show_MOTD_Sendline( Client, line)) {
1098 return Show_MOTD_End(Client);
1099 } /* IRC_Show_MOTD */
1103 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
1105 bool is_visible, is_member;
1106 char str[LINE_LEN + 1];
1110 assert( Client != NULL );
1111 assert( Chan != NULL );
1113 if( Channel_IsMemberOf( Chan, Client )) is_member = true;
1114 else is_member = false;
1116 /* Secret channel? */
1117 if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
1119 /* Alle Mitglieder suchen */
1120 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1121 cl2chan = Channel_FirstMember( Chan );
1124 cl = Channel_GetClient( cl2chan );
1126 if( strchr( Client_Modes( cl ), 'i' )) is_visible = false;
1127 else is_visible = true;
1129 if( is_member || is_visible )
1131 /* Nick anhaengen */
1132 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
1133 if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
1134 else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
1135 strlcat( str, Client_ID( cl ), sizeof( str ));
1137 if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
1139 /* Zeile wird zu lang: senden! */
1140 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1141 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1145 /* naechstes Mitglied suchen */
1146 cl2chan = Channel_NextMember( Chan, cl2chan );
1148 if( str[strlen( str ) - 1] != ':')
1150 /* Es sind noch Daten da, die gesendet werden muessen */
1151 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1155 } /* IRC_Send_NAMES */
1160 * Send the ISUPPORT numeric (005).
1161 * This numeric indicates the features that are supported by this server.
1162 * See <http://www.irc.org/tech_docs/005.html> for details.
1165 IRC_Send_ISUPPORT PARAMS((CLIENT * Client))
1167 if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1169 return DISCONNECTED;
1170 return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1171 CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1172 COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1174 } /* IRC_Send_ISUPPORT */