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.44 2008/02/17 13:26:42 alex 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 * Handler for the IRC command "INFO".
90 * See RFC 2812 section 3.4.10.
93 IRC_INFO(CLIENT * Client, REQUEST * Req)
95 CLIENT *target, *prefix;
98 assert(Client != NULL);
101 /* Wrong number of parameters? */
103 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
104 Client_ID(Client), Req->command);
106 /* Determine prefix */
107 if (Client_Type(Client) == CLIENT_SERVER)
108 prefix = Client_Search(Req->prefix);
112 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
113 Client_ID(Client), Req->prefix);
115 /* Look for a target */
117 target = Client_Search(Req->argv[0]);
119 target = Client_ThisServer();
121 /* Make sure that the target is a server */
122 if (target && Client_Type(target) != CLIENT_SERVER)
123 target = Client_Introducer(target);
126 return IRC_WriteStrClient(prefix, ERR_NOSUCHSERVER_MSG,
127 Client_ID(prefix), Req->argv[0]);
129 /* Pass on to another server? */
130 if (target != Client_ThisServer()) {
131 IRC_WriteStrClientPrefix(target, prefix, "INFO %s",
136 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix),
140 strlcpy(msg, "Server has been started ", sizeof(msg));
141 strlcat(msg, NGIRCd_StartStr, sizeof(msg));
142 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
145 if (!IRC_WriteStrClient(Client, RPL_ENDOFINFO_MSG, Client_ID(prefix)))
148 IRC_SetPenalty(Client, 2);
154 IRC_ISON( CLIENT *Client, REQUEST *Req )
156 char rpl[COMMAND_LEN];
161 assert( Client != NULL );
162 assert( Req != NULL );
164 /* Falsche Anzahl Parameter? */
165 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
167 strlcpy( rpl, RPL_ISON_MSG, sizeof rpl );
168 for( i = 0; i < Req->argc; i++ )
170 ptr = strtok( Req->argv[i], " " );
174 c = Client_Search( ptr );
175 if( c && ( Client_Type( c ) == CLIENT_USER ))
177 /* Dieser Nick ist "online" */
178 strlcat( rpl, ptr, sizeof( rpl ));
179 strlcat( rpl, " ", sizeof( rpl ));
181 ptr = strtok( NULL, " " );
184 ngt_TrimLastChr(rpl, ' ');
186 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
191 IRC_LINKS( CLIENT *Client, REQUEST *Req )
193 CLIENT *target, *from, *c;
196 assert( Client != NULL );
197 assert( Req != NULL );
199 /* Falsche Anzahl Parameter? */
200 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
202 /* Server-Mask ermitteln */
203 if( Req->argc > 0 ) mask = Req->argv[Req->argc - 1];
206 /* Absender ermitteln */
207 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
209 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
211 /* An anderen Server forwarden? */
214 target = Client_Search( Req->argv[0] );
215 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
216 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LINKS %s %s", Req->argv[0], Req->argv[1] );
219 /* Wer ist der Absender? */
220 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
221 else target = Client;
222 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
227 if( Client_Type( c ) == CLIENT_SERVER )
229 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;
231 c = Client_Next( c );
234 IRC_SetPenalty( target, 1 );
235 return IRC_WriteStrClient( target, RPL_ENDOFLINKS_MSG, Client_ID( target ), mask );
240 IRC_LUSERS( CLIENT *Client, REQUEST *Req )
242 CLIENT *target, *from;
244 assert( Client != NULL );
245 assert( Req != NULL );
247 /* Falsche Anzahl Parameter? */
248 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
250 /* Absender ermitteln */
251 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
253 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
255 /* An anderen Server forwarden? */
258 target = Client_Search( Req->argv[1] );
259 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
260 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LUSERS %s %s", Req->argv[0], Req->argv[1] );
263 /* Wer ist der Absender? */
264 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
265 else target = Client;
266 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
268 IRC_Send_LUSERS( target );
270 IRC_SetPenalty( target, 1 );
276 IRC_MOTD( CLIENT *Client, REQUEST *Req )
278 CLIENT *from, *target;
280 assert( Client != NULL );
281 assert( Req != NULL );
283 /* Falsche Anzahl Parameter? */
284 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
286 /* From aus Prefix ermitteln */
287 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
289 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
293 /* an anderen Server forwarden */
294 target = Client_Search( Req->argv[0] );
295 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
297 if( target != Client_ThisServer( ))
299 /* Ok, anderer Server ist das Ziel: forwarden */
300 return IRC_WriteStrClientPrefix( target, from, "MOTD %s", Req->argv[0] );
304 IRC_SetPenalty( from, 3 );
305 return IRC_Show_MOTD( from );
310 IRC_NAMES( CLIENT *Client, REQUEST *Req )
312 char rpl[COMMAND_LEN], *ptr;
313 CLIENT *target, *from, *c;
316 assert( Client != NULL );
317 assert( Req != NULL );
319 /* Falsche Anzahl Parameter? */
320 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
322 /* From aus Prefix ermitteln */
323 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
325 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
329 /* an anderen Server forwarden */
330 target = Client_Search( Req->argv[1] );
331 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
333 if( target != Client_ThisServer( ))
335 /* Ok, anderer Server ist das Ziel: forwarden */
336 return IRC_WriteStrClientPrefix( target, from, "NAMES %s :%s", Req->argv[0], Req->argv[1] );
342 /* bestimmte Channels durchgehen */
343 ptr = strtok( Req->argv[0], "," );
346 chan = Channel_Search( ptr );
350 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
352 if( ! IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), ptr )) return DISCONNECTED;
354 /* naechsten Namen ermitteln */
355 ptr = strtok( NULL, "," );
360 /* alle Channels durchgehen */
361 chan = Channel_First( );
365 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
367 /* naechster Channel */
368 chan = Channel_Next( chan );
371 /* Nun noch alle Clients ausgeben, die in keinem Channel sind */
373 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
376 if(( Client_Type( c ) == CLIENT_USER ) && ( Channel_FirstChannelOf( c ) == NULL ) && ( ! strchr( Client_Modes( c ), 'i' )))
378 /* Okay, das ist ein User: anhaengen */
379 if( rpl[strlen( rpl ) - 1] != ':' ) strlcat( rpl, " ", sizeof( rpl ));
380 strlcat( rpl, Client_ID( c ), sizeof( rpl ));
382 if( strlen( rpl ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
384 /* Zeile wird zu lang: senden! */
385 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
386 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
390 /* naechster Client */
391 c = Client_Next( c );
393 if( rpl[strlen( rpl ) - 1] != ':')
395 /* es wurden User gefunden */
396 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
399 IRC_SetPenalty( from, 1 );
400 return IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), "*" );
405 t_diff(time_t *t, const time_t div)
419 uptime_days(time_t *now)
421 return t_diff(now, 60 * 60 * 24);
426 uptime_hrs(time_t *now)
428 return t_diff(now, 60 * 60);
433 uptime_mins(time_t *now)
435 return t_diff(now, 60);
440 IRC_STATS( CLIENT *Client, REQUEST *Req )
442 CLIENT *from, *target, *cl;
447 unsigned int days, hrs, mins;
449 assert( Client != NULL );
450 assert( Req != NULL );
452 /* Falsche Anzahl Parameter? */
454 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
456 /* From aus Prefix ermitteln */
457 if (Client_Type(Client) == CLIENT_SERVER)
458 from = Client_Search(Req->prefix);
463 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix);
465 if (Req->argc == 2) {
466 /* an anderen Server forwarden */
467 target = Client_Search( Req->argv[1] );
468 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
469 return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
471 if( target != Client_ThisServer()) {
472 /* Ok, anderer Server ist das Ziel: forwarden */
473 return IRC_WriteStrClientPrefix( target, from, "STATS %s %s", Req->argv[0], Req->argv[1] );
478 query = Req->argv[0][0] ? Req->argv[0][0] : '*';
483 case 'l': /* Links */
485 time_now = time(NULL);
486 for (con = Conn_First(); con != NONE ;con = Conn_Next(con)) {
487 cl = Conn_GetClient(con);
490 if ((Client_Type(cl) == CLIENT_SERVER) || (cl == Client)) {
491 /* Server link or our own connection */
493 if (Conn_Options(con) & CONN_ZIP) {
494 if (!IRC_WriteStrClient(from, RPL_STATSLINKINFOZIP_MSG,
495 Client_ID(from), Client_Mask(cl), Conn_SendQ(con),
496 Conn_SendMsg(con), Zip_SendBytes(con), Conn_SendBytes(con),
497 Conn_RecvMsg(con), Zip_RecvBytes(con), Conn_RecvBytes(con), (long)(time_now - Conn_StartTime(con))))
502 if (!IRC_WriteStrClient(from, RPL_STATSLINKINFO_MSG, Client_ID(from),
503 Client_Mask(cl), Conn_SendQ(con), Conn_SendMsg(con), Conn_SendBytes(con),
504 Conn_RecvMsg(con), Conn_RecvBytes(con), (long)(time_now - Conn_StartTime(con))))
509 case 'm': /* IRC-Commands */
511 cmd = Parse_GetCommandStruct( );
512 for (; cmd->name ; cmd++) {
513 if (cmd->lcount == 0 && cmd->rcount == 0)
515 if (!IRC_WriteStrClient(from, RPL_STATSCOMMANDS_MSG, Client_ID(from),
516 cmd->name, cmd->lcount, cmd->bytes, cmd->rcount))
520 case 'u': /* server uptime */
522 time_now = time(NULL) - NGIRCd_Start;
523 days = uptime_days(&time_now);
524 hrs = uptime_hrs(&time_now);
525 mins = uptime_mins(&time_now);
526 if (!IRC_WriteStrClient(from, RPL_STATSUPTIME, Client_ID(from),
527 days, hrs, mins, (unsigned int) time_now))
532 IRC_SetPenalty(from, 2);
533 return IRC_WriteStrClient(from, RPL_ENDOFSTATS_MSG, Client_ID(from), query);
538 * Handler for the IRC command "SUMMON".
539 * See RFC 2812 section 4.5. ngIRCd doesn't implement this functionality and
540 * therefore answers with ERR_SUMMONDISABLED.
543 IRC_SUMMON(CLIENT * Client, REQUEST * Req)
545 return IRC_WriteStrClient(Client, ERR_SUMMONDISABLED_MSG,
546 Client_ID(Client), Req->command);
551 IRC_TIME( CLIENT *Client, REQUEST *Req )
553 CLIENT *from, *target;
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 );
563 /* From aus Prefix ermitteln */
564 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
566 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
570 /* an anderen Server forwarden */
571 target = Client_Search( Req->argv[0] );
572 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
574 if( target != Client_ThisServer( ))
576 /* Ok, anderer Server ist das Ziel: forwarden */
577 return IRC_WriteStrClientPrefix( target, from, "TIME %s", Req->argv[0] );
582 (void)strftime( t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime( &t ));
583 return IRC_WriteStrClient( from, RPL_TIME_MSG, Client_ID( from ), Client_ID( Client_ThisServer( )), t_str );
588 IRC_USERHOST( CLIENT *Client, REQUEST *Req )
590 char rpl[COMMAND_LEN];
594 assert( Client != NULL );
595 assert( Req != NULL );
597 /* Falsche Anzahl Parameter? */
598 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
600 if( Req->argc > 5 ) max = 5;
601 else max = Req->argc;
603 strlcpy( rpl, RPL_USERHOST_MSG, sizeof rpl );
604 for( i = 0; i < max; i++ )
606 c = Client_Search( Req->argv[i] );
607 if( c && ( Client_Type( c ) == CLIENT_USER ))
609 /* Dieser Nick ist "online" */
610 strlcat( rpl, Client_ID( c ), sizeof( rpl ));
611 if( Client_HasMode( c, 'o' )) strlcat( rpl, "*", sizeof( rpl ));
612 strlcat( rpl, "=", sizeof( rpl ));
613 if( Client_HasMode( c, 'a' )) strlcat( rpl, "-", sizeof( rpl ));
614 else strlcat( rpl, "+", sizeof( rpl ));
615 strlcat( rpl, Client_User( c ), sizeof( rpl ));
616 strlcat( rpl, "@", sizeof( rpl ));
617 strlcat( rpl, Client_Hostname( c ), sizeof( rpl ));
618 strlcat( rpl, " ", sizeof( rpl ));
621 ngt_TrimLastChr( rpl, ' ');
623 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
628 * Handler for the IRC command "USERS".
629 * See RFC 2812 section 4.6. As suggested there the command is disabled.
632 IRC_USERS(CLIENT * Client, REQUEST * Req)
634 return IRC_WriteStrClient(Client, ERR_USERSDISABLED_MSG,
635 Client_ID(Client), Req->command);
640 IRC_VERSION( CLIENT *Client, REQUEST *Req )
642 CLIENT *target, *prefix;
644 char ver[12], vertxt[30];
647 assert( Client != NULL );
648 assert( Req != NULL );
650 /* Falsche Anzahl Parameter? */
651 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
654 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
655 else target = Client_ThisServer( );
657 /* Prefix ermitteln */
658 if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
659 else prefix = Client;
660 if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
662 /* An anderen Server weiterleiten? */
663 if( target != Client_ThisServer( ))
665 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
668 IRC_WriteStrClientPrefix( target, prefix, "VERSION %s", Req->argv[0] );
672 /* mit Versionsinfo antworten */
673 IRC_SetPenalty( Client, 1 );
675 strlcpy( ver, CVSDATE, sizeof( ver ));
676 strncpy( ver + 4, ver + 5, 2 );
677 strncpy( ver + 6, ver + 8, 3 );
678 snprintf( vertxt, sizeof( vertxt ), "%s(%s)", PACKAGE_VERSION, ver );
679 return IRC_WriteStrClient( Client, RPL_VERSION_MSG, Client_ID( prefix ), PACKAGE_NAME, vertxt, NGIRCd_DebugLevel, Conf_ServerName, NGIRCd_VersionAddition );
681 return IRC_WriteStrClient( Client, RPL_VERSION_MSG, Client_ID( prefix ), PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel, Conf_ServerName, NGIRCd_VersionAddition );
688 write_whoreply(CLIENT *Client, CLIENT *c, const char *channelname, const char *flags)
690 return IRC_WriteStrClient(Client, RPL_WHOREPLY_MSG, Client_ID(Client), channelname,
691 Client_User(c), Client_Hostname(c), Client_ID(Client_Introducer(c)), Client_ID(c),
692 flags, Client_Hops(c), Client_Info(c));
697 who_flags_status(const char *client_modes)
699 if (strchr(client_modes, 'a'))
700 return "G"; /* away */
706 who_flags_qualifier(const char *chan_user_modes)
708 if (strchr(chan_user_modes, 'o'))
710 else if (strchr(chan_user_modes, 'v'))
717 IRC_Send_WHO(CLIENT *Client, CHANNEL *Chan, bool OnlyOps)
719 bool is_visible, is_member, is_ircop;
721 const char *client_modes;
722 const char *chan_user_modes;
726 assert( Client != NULL );
727 assert( Chan != NULL );
729 is_member = Channel_IsMemberOf(Chan, Client);
731 /* Secret channel? */
732 if (!is_member && strchr(Channel_Modes(Chan), 's'))
735 cl2chan = Channel_FirstMember(Chan);
736 for (; cl2chan ; cl2chan = Channel_NextMember(Chan, cl2chan)) {
737 c = Channel_GetClient(cl2chan);
739 client_modes = Client_Modes(c);
740 is_ircop = strchr(client_modes, 'o') != NULL;
741 if (OnlyOps && !is_ircop)
744 is_visible = strchr(client_modes, 'i') == NULL;
745 if (is_member || is_visible) {
746 strcpy(flags, who_flags_status(client_modes));
748 strlcat(flags, "*", sizeof(flags));
750 chan_user_modes = Channel_UserModes(Chan, c);
751 strlcat(flags, who_flags_qualifier(chan_user_modes), sizeof(flags));
753 if (!write_whoreply(Client, c, Channel_Name(Chan), flags))
757 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), Channel_Name(Chan));
763 MatchCaseInsensitive(const char *pattern, const char *searchme)
765 char haystack[COMMAND_LEN];
767 strlcpy(haystack, searchme, sizeof(haystack));
769 ngt_LowerStr(haystack);
771 return Match(pattern, haystack);
776 IRC_WHO( CLIENT *Client, REQUEST *Req )
778 bool only_ops, have_arg, client_match;
779 const char *channelname, *client_modes, *chan_user_modes;
780 char pattern[COMMAND_LEN];
786 assert( Client != NULL );
787 assert( Req != NULL );
790 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
795 if (Req->argc == 2) {
796 if (strcmp(Req->argv[1], "o") == 0)
799 else return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
803 IRC_SetPenalty(Client, 1);
804 if (Req->argc >= 1) { /* Channel or Mask. */
805 chan = Channel_Search(Req->argv[0]);
807 return IRC_Send_WHO(Client, chan, only_ops);
808 if (strcmp(Req->argv[0], "0") != 0) { /* RFC stupidity, same as no arguments */
810 strlcpy(pattern, Req->argv[0], sizeof(pattern));
811 ngt_LowerStr(pattern);
812 IRC_SetPenalty(Client, 3);
816 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
817 if (Client_Type(c) != CLIENT_USER)
821 * In the absence of the parameter, all visible (users who aren't
822 * invisible (user mode +i) and who don't have a common channel
823 * with the requesting client) are listed.
825 * The same result can be achieved by using a [sic] of "0"
826 * or any wildcard which will end up matching every visible user.
828 * The [sic] passed to WHO is matched against users' host, server, real name and
829 * nickname if the channel cannot be found.
831 client_modes = Client_Modes(c);
832 if (strchr(client_modes, 'i'))
835 if (only_ops && !strchr(client_modes, 'o'))
838 if (have_arg) { /* match pattern against user host/server/name/nick */
839 client_match = MatchCaseInsensitive(pattern, Client_Hostname(c)); /* user's host */
841 client_match = MatchCaseInsensitive(pattern, Client_ID(Client_Introducer(c))); /* server */
843 client_match = Match(Req->argv[0], Client_Info(c)); /* realname */
845 client_match = MatchCaseInsensitive(pattern, Client_ID(c)); /* nick name */
847 if (!client_match) /* This isn't the client you're looking for */
851 strcpy(flags, who_flags_status(client_modes));
853 if (strchr(client_modes, 'o')) /* this client is an operator */
854 strlcat(flags, "*", sizeof(flags));
856 /* Search suitable channel */
857 cl2chan = Channel_FirstChannelOf(c);
859 cn = Channel_GetChannel(cl2chan);
860 if (Channel_IsMemberOf(cn, Client) ||
861 !strchr(Channel_Modes(cn), 's'))
863 channelname = Channel_Name(cn);
866 cl2chan = Channel_NextChannelOf(c, cl2chan);
869 chan = Channel_GetChannel(cl2chan);
870 chan_user_modes = Channel_UserModes(chan, c);
871 strlcat(flags, who_flags_qualifier(chan_user_modes), sizeof(flags));
875 if (!write_whoreply(Client, c, channelname, flags))
880 channelname = Req->argv[0];
884 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), channelname);
889 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
891 CLIENT *from, *target, *c;
892 char str[LINE_LEN + 1];
896 assert( Client != NULL );
897 assert( Req != NULL );
899 /* Bad number of parameters? */
900 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
903 c = Client_Search( Req->argv[Req->argc - 1] );
904 if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[Req->argc - 1] );
906 /* Search sender of the WHOIS */
907 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
909 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
911 /* Forward to other server? */
914 /* Search target server (can be specified as nick of that server!) */
915 target = Client_Search( Req->argv[0] );
916 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
918 else target = Client_ThisServer( );
920 assert( target != NULL );
922 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] );
924 /* Nick, user and name */
925 if( ! IRC_WriteStrClient( from, RPL_WHOISUSER_MSG, Client_ID( from ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
928 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;
931 snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
932 cl2chan = Channel_FirstChannelOf( c );
935 chan = Channel_GetChannel( cl2chan );
936 assert( chan != NULL );
939 cl2chan = Channel_NextChannelOf( c, cl2chan );
941 /* Secret channel? */
942 if( strchr( Channel_Modes( chan ), 's' ) && ! Channel_IsMemberOf( chan, Client )) continue;
944 /* Concatenate channel names */
945 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
946 if( strchr( Channel_UserModes( chan, c ), 'o' )) strlcat( str, "@", sizeof( str ));
947 else if( strchr( Channel_UserModes( chan, c ), 'v' )) strlcat( str, "+", sizeof( str ));
948 strlcat( str, Channel_Name( chan ), sizeof( str ));
950 if( strlen( str ) > ( LINE_LEN - CHANNEL_NAME_LEN - 4 ))
952 /* Line becomes too long: send it! */
953 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
954 snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
957 if( str[strlen( str ) - 1] != ':')
959 /* There is data left to send: */
960 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
964 if( Client_HasMode( c, 'o' ))
966 if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
969 /* Idle and signon time (local clients only!) */
970 if (Client_Conn(c) > NONE ) {
971 if (! IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
972 Client_ID(from), Client_ID(c),
973 (unsigned long)Conn_GetIdle(Client_Conn(c)),
974 (unsigned long)Conn_GetSignon(Client_Conn(c))))
979 if( Client_HasMode( c, 'a' ))
981 if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( c ), Client_Away( c ))) return DISCONNECTED;
985 return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
990 * IRC "WHOWAS" function.
991 * This function implements the IRC command "WHOWHAS". It handles local
992 * requests and request that should be forwarded to other servers.
995 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
997 CLIENT *target, *prefix;
999 int max, last, count, i;
1002 assert( Client != NULL );
1003 assert( Req != NULL );
1005 /* Wrong number of parameters? */
1006 if(( Req->argc < 1 ) || ( Req->argc > 3 ))
1007 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
1008 Client_ID( Client ), Req->command );
1011 if( Req->argc == 3 )
1012 target = Client_Search( Req->argv[2] );
1014 target = Client_ThisServer( );
1017 if( Client_Type( Client ) == CLIENT_SERVER )
1018 prefix = Client_Search( Req->prefix );
1023 return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG,
1024 Client_ID( Client ), Req->prefix );
1026 /* Forward to other server? */
1027 if( target != Client_ThisServer( ))
1029 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
1030 return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG,
1031 Client_ID( prefix ),
1035 IRC_WriteStrClientPrefix( target, prefix, "WHOWAS %s %s %s",
1036 Req->argv[0], Req->argv[1],
1041 whowas = Client_GetWhowas( );
1042 last = Client_GetLastWhowasIndex( );
1043 if( last < 0 ) last = 0;
1047 max = atoi( Req->argv[1] );
1048 if( max < 1 ) max = MAX_WHOWAS;
1051 max = DEFAULT_WHOWAS;
1058 if( whowas[i].time > 0 &&
1059 strcasecmp( Req->argv[0], whowas[i].id ) == 0 )
1061 (void)strftime( t_str, sizeof(t_str),
1062 "%a %b %d %H:%M:%S %Y",
1063 localtime( &whowas[i].time ));
1065 if( ! IRC_WriteStrClient( prefix, RPL_WHOWASUSER_MSG,
1066 Client_ID( prefix ),
1071 return DISCONNECTED;
1073 if( ! IRC_WriteStrClient( prefix, RPL_WHOISSERVER_MSG,
1074 Client_ID( prefix ),
1076 whowas[i].server, t_str ))
1077 return DISCONNECTED;
1080 if( count >= max ) break;
1086 /* "underflow", wrap around */
1087 if( i < 0 ) i = MAX_WHOWAS - 1;
1088 } while( i != last );
1090 return IRC_WriteStrClient( prefix, RPL_ENDOFWHOWAS_MSG,
1091 Client_ID( prefix ), Req->argv[0] );
1096 IRC_Send_LUSERS( CLIENT *Client )
1103 assert( Client != NULL );
1105 /* Users, services and serevers in the network */
1106 if( ! IRC_WriteStrClient( Client, RPL_LUSERCLIENT_MSG, Client_ID( Client ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
1108 /* Number of IRC operators */
1109 cnt = Client_OperCount( );
1112 if( ! IRC_WriteStrClient( Client, RPL_LUSEROP_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
1115 /* Unknown connections */
1116 cnt = Client_UnknownCount( );
1119 if( ! IRC_WriteStrClient( Client, RPL_LUSERUNKNOWN_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
1122 /* Number of created channels */
1123 if( ! IRC_WriteStrClient( Client, RPL_LUSERCHANNELS_MSG, Client_ID( Client ), Channel_Count( ))) return DISCONNECTED;
1125 /* Number of local users, services and servers */
1126 if( ! IRC_WriteStrClient( Client, RPL_LUSERME_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
1129 /* Maximum number of local users */
1130 cnt = Client_MyUserCount();
1131 max = Client_MyMaxUserCount();
1132 if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1133 cnt, max, cnt, max))
1134 return DISCONNECTED;
1135 /* Maximum number of users in the network */
1136 cnt = Client_UserCount();
1137 max = Client_MaxUserCount();
1138 if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1139 cnt, max, cnt, max))
1140 return DISCONNECTED;
1144 } /* IRC_Send_LUSERS */
1148 Show_MOTD_Start(CLIENT *Client)
1150 return IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG,
1151 Client_ID( Client ), Client_ID( Client_ThisServer( )));
1155 Show_MOTD_Sendline(CLIENT *Client, const char *msg)
1157 return IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID( Client ), msg);
1161 Show_MOTD_End(CLIENT *Client)
1163 return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
1168 IRC_Show_MOTD( CLIENT *Client )
1173 assert( Client != NULL );
1175 if (Conf_MotdPhrase[0]) {
1176 if (!Show_MOTD_Start(Client))
1177 return DISCONNECTED;
1178 if (!Show_MOTD_Sendline(Client, Conf_MotdPhrase))
1179 return DISCONNECTED;
1181 return Show_MOTD_End(Client);
1184 fd = fopen( Conf_MotdFile, "r" );
1186 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
1187 return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
1190 if (!Show_MOTD_Start( Client )) {
1195 while (fgets( line, (int)sizeof line, fd )) {
1196 ngt_TrimLastChr( line, '\n');
1198 if( ! Show_MOTD_Sendline( Client, line)) {
1204 return Show_MOTD_End(Client);
1205 } /* IRC_Show_MOTD */
1209 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
1211 bool is_visible, is_member;
1212 char str[LINE_LEN + 1];
1216 assert( Client != NULL );
1217 assert( Chan != NULL );
1219 if( Channel_IsMemberOf( Chan, Client )) is_member = true;
1220 else is_member = false;
1222 /* Secret channel? */
1223 if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
1225 /* Alle Mitglieder suchen */
1226 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1227 cl2chan = Channel_FirstMember( Chan );
1230 cl = Channel_GetClient( cl2chan );
1232 if( strchr( Client_Modes( cl ), 'i' )) is_visible = false;
1233 else is_visible = true;
1235 if( is_member || is_visible )
1237 /* Nick anhaengen */
1238 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
1239 if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
1240 else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
1241 strlcat( str, Client_ID( cl ), sizeof( str ));
1243 if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
1245 /* Zeile wird zu lang: senden! */
1246 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1247 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1251 /* naechstes Mitglied suchen */
1252 cl2chan = Channel_NextMember( Chan, cl2chan );
1254 if( str[strlen( str ) - 1] != ':')
1256 /* Es sind noch Daten da, die gesendet werden muessen */
1257 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1261 } /* IRC_Send_NAMES */
1266 * Send the ISUPPORT numeric (005).
1267 * This numeric indicates the features that are supported by this server.
1268 * See <http://www.irc.org/tech_docs/005.html> for details.
1271 IRC_Send_ISUPPORT PARAMS((CLIENT * Client))
1273 if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1275 return DISCONNECTED;
1276 return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1277 CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1278 COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1280 } /* IRC_Send_ISUPPORT */