2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2010 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.
28 #include "conn-func.h"
38 #include "irc-write.h"
45 IRC_ADMIN(CLIENT *Client, REQUEST *Req )
47 CLIENT *target, *prefix;
49 assert( Client != NULL );
50 assert( Req != NULL );
52 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
55 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
56 else target = Client_ThisServer( );
59 if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
61 if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
63 /* forwad message to another server? */
64 if( target != Client_ThisServer( ))
66 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
69 IRC_WriteStrClientPrefix( target, prefix, "ADMIN %s", Req->argv[0] );
73 /* mit Versionsinfo antworten */
74 if( ! IRC_WriteStrClient( Client, RPL_ADMINME_MSG, Client_ID( prefix ), Conf_ServerName )) return DISCONNECTED;
75 if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC1_MSG, Client_ID( prefix ), Conf_ServerAdmin1 )) return DISCONNECTED;
76 if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC2_MSG, Client_ID( prefix ), Conf_ServerAdmin2 )) return DISCONNECTED;
77 if( ! IRC_WriteStrClient( Client, RPL_ADMINEMAIL_MSG, Client_ID( prefix ), Conf_ServerAdminMail )) return DISCONNECTED;
79 IRC_SetPenalty( Client, 1 );
85 * Handler for the IRC command "INFO".
86 * See RFC 2812 section 3.4.10.
89 IRC_INFO(CLIENT * Client, REQUEST * Req)
91 CLIENT *target, *prefix;
94 assert(Client != NULL);
97 /* Wrong number of parameters? */
99 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
100 Client_ID(Client), Req->command);
102 /* Determine prefix */
103 if (Client_Type(Client) == CLIENT_SERVER)
104 prefix = Client_Search(Req->prefix);
108 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
109 Client_ID(Client), Req->prefix);
111 /* Look for a target */
113 target = Client_Search(Req->argv[0]);
115 target = Client_ThisServer();
117 /* Make sure that the target is a server */
118 if (target && Client_Type(target) != CLIENT_SERVER)
119 target = Client_Introducer(target);
122 return IRC_WriteStrClient(prefix, ERR_NOSUCHSERVER_MSG,
123 Client_ID(prefix), Req->argv[0]);
125 /* Pass on to another server? */
126 if (target != Client_ThisServer()) {
127 IRC_WriteStrClientPrefix(target, prefix, "INFO %s",
132 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix),
136 #if defined(__DATE__) && defined(__TIME__)
137 snprintf(msg, sizeof(msg), "Birth Date: %s at %s", __DATE__, __TIME__);
138 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
142 strlcpy(msg, "On-line since ", sizeof(msg));
143 strlcat(msg, NGIRCd_StartStr, sizeof(msg));
144 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
147 if (!IRC_WriteStrClient(Client, RPL_ENDOFINFO_MSG, Client_ID(prefix)))
150 IRC_SetPenalty(Client, 2);
156 IRC_ISON( CLIENT *Client, REQUEST *Req )
158 char rpl[COMMAND_LEN];
163 assert( Client != NULL );
164 assert( Req != NULL );
166 /* Falsche Anzahl Parameter? */
167 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
169 strlcpy( rpl, RPL_ISON_MSG, sizeof rpl );
170 for( i = 0; i < Req->argc; i++ )
172 ptr = strtok( Req->argv[i], " " );
176 c = Client_Search( ptr );
177 if( c && ( Client_Type( c ) == CLIENT_USER ))
179 /* Dieser Nick ist "online" */
180 strlcat( rpl, ptr, sizeof( rpl ));
181 strlcat( rpl, " ", sizeof( rpl ));
183 ptr = strtok( NULL, " " );
186 ngt_TrimLastChr(rpl, ' ');
188 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
193 IRC_LINKS( CLIENT *Client, REQUEST *Req )
195 CLIENT *target, *from, *c;
198 assert( Client != NULL );
199 assert( Req != NULL );
201 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
203 /* Server-Mask ermitteln */
204 if( Req->argc > 0 ) mask = Req->argv[Req->argc - 1];
207 /* Absender ermitteln */
208 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
210 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
212 /* An anderen Server forwarden? */
215 target = Client_Search( Req->argv[0] );
216 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
217 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LINKS %s %s", Req->argv[0], Req->argv[1] );
220 /* Wer ist der Absender? */
221 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
222 else target = Client;
223 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
228 if( Client_Type( c ) == CLIENT_SERVER )
230 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;
232 c = Client_Next( c );
235 IRC_SetPenalty( target, 1 );
236 return IRC_WriteStrClient( target, RPL_ENDOFLINKS_MSG, Client_ID( target ), mask );
241 IRC_LUSERS( CLIENT *Client, REQUEST *Req )
243 CLIENT *target, *from;
245 assert( Client != NULL );
246 assert( Req != NULL );
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 * Handler for the IRC command "SERVLIST".
277 * List registered services, see RFC 2811, section 3.5.1: the syntax is
278 * "SERVLIST [<mask> [<type>]]".
281 IRC_SERVLIST(CLIENT *Client, REQUEST *Req)
285 assert(Client != NULL);
289 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
290 Client_ID(Client), Req->command);
292 if (Req->argc < 2 || strcmp(Req->argv[1], "0") == 0) {
293 for (c = Client_First(); c!= NULL; c = Client_Next(c)) {
294 if (Client_Type(c) != CLIENT_SERVICE)
296 if (Req->argc > 0 && !MatchCaseInsensitive(Req->argv[0],
299 if (!IRC_WriteStrClient(Client, RPL_SERVLIST_MSG,
300 Client_ID(Client), Client_Mask(c),
301 Client_Mask(Client_Introducer(c)), "*",
302 0, Client_Hops(c), Client_Info(c)))
307 return IRC_WriteStrClient(Client, RPL_SERVLISTEND_MSG, Client_ID(Client),
308 Req->argc > 0 ? Req->argv[0] : "*",
309 Req->argc > 1 ? Req->argv[1] : "0");
314 IRC_MOTD( CLIENT *Client, REQUEST *Req )
316 CLIENT *from, *target;
318 assert( Client != NULL );
319 assert( Req != NULL );
321 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
323 /* From aus Prefix ermitteln */
324 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
326 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
331 target = Client_Search( Req->argv[0] );
332 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
334 if( target != Client_ThisServer( ))
336 /* Ok, anderer Server ist das Ziel: forwarden */
337 return IRC_WriteStrClientPrefix( target, from, "MOTD %s", Req->argv[0] );
341 IRC_SetPenalty( from, 3 );
342 return IRC_Show_MOTD( from );
347 IRC_NAMES( CLIENT *Client, REQUEST *Req )
349 char rpl[COMMAND_LEN], *ptr;
350 CLIENT *target, *from, *c;
353 assert( Client != NULL );
354 assert( Req != NULL );
356 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
358 /* use prefix to determine "From" */
359 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
361 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
365 /* forward to another server? */
366 target = Client_Search( Req->argv[1] );
367 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
369 if( target != Client_ThisServer( )) {
370 /* target is another server, forward */
371 return IRC_WriteStrClientPrefix( target, from, "NAMES %s :%s", Req->argv[0], Req->argv[1] );
377 /* bestimmte Channels durchgehen */
378 ptr = strtok( Req->argv[0], "," );
381 chan = Channel_Search( ptr );
385 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
387 if( ! IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), ptr )) return DISCONNECTED;
389 /* get next channel name */
390 ptr = strtok( NULL, "," );
395 chan = Channel_First( );
398 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
400 chan = Channel_Next( chan );
403 /* Now print all clients which are not in any channel */
405 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
408 if(( Client_Type( c ) == CLIENT_USER ) && ( Channel_FirstChannelOf( c ) == NULL ) && ( ! strchr( Client_Modes( c ), 'i' )))
410 /* its a user, concatenate ... */
411 if( rpl[strlen( rpl ) - 1] != ':' ) strlcat( rpl, " ", sizeof( rpl ));
412 strlcat( rpl, Client_ID( c ), sizeof( rpl ));
414 if( strlen( rpl ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
416 /* Line is gwoing too long, send now */
417 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
418 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
422 c = Client_Next( c );
424 if( rpl[strlen( rpl ) - 1] != ':')
426 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
429 IRC_SetPenalty( from, 1 );
430 return IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), "*" );
435 t_diff(time_t *t, const time_t d)
443 return (unsigned int)diff;
448 uptime_days(time_t *now)
450 return t_diff(now, 60 * 60 * 24);
455 uptime_hrs(time_t *now)
457 return t_diff(now, 60 * 60);
462 uptime_mins(time_t *now)
464 return t_diff(now, 60);
469 * Handler for the IRC command "STATS".
470 * See RFC 2812 section 3.4.4.
473 IRC_STATS( CLIENT *Client, REQUEST *Req )
475 CLIENT *from, *target, *cl;
480 unsigned int days, hrs, mins;
482 assert(Client != NULL);
486 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
487 Client_ID(Client), Req->command);
489 /* use prefix to determine "From" */
490 if (Client_Type(Client) == CLIENT_SERVER)
491 from = Client_Search(Req->prefix);
496 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
497 Client_ID(Client), Req->prefix);
499 if (Req->argc == 2) {
500 /* forward to another server? */
501 target = Client_Search(Req->argv[1]);
502 if ((!target) || (Client_Type(target) != CLIENT_SERVER))
503 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
504 Client_ID(from), Req->argv[1]);
506 if (target != Client_ThisServer()) {
507 /* forward to another server */
508 return IRC_WriteStrClientPrefix(target, from,
509 "STATS %s %s", Req->argv[0], Req->argv[1]);
514 query = Req->argv[0][0] ? Req->argv[0][0] : '*';
519 case 'l': /* Link status (servers and own link) */
521 time_now = time(NULL);
522 for (con = Conn_First(); con != NONE; con = Conn_Next(con)) {
523 cl = Conn_GetClient(con);
526 if ((Client_Type(cl) == CLIENT_SERVER)
528 /* Server link or our own connection */
530 if (Conn_Options(con) & CONN_ZIP) {
531 if (!IRC_WriteStrClient
532 (from, RPL_STATSLINKINFOZIP_MSG,
533 Client_ID(from), Client_Mask(cl),
534 Conn_SendQ(con), Conn_SendMsg(con),
540 (long)(time_now - Conn_StartTime(con))))
545 if (!IRC_WriteStrClient
546 (from, RPL_STATSLINKINFO_MSG,
547 Client_ID(from), Client_Mask(cl),
548 Conn_SendQ(con), Conn_SendMsg(con),
549 Conn_SendBytes(con), Conn_RecvMsg(con),
551 (long)(time_now - Conn_StartTime(con))))
556 case 'm': /* IRC command status (usage count) */
558 cmd = Parse_GetCommandStruct();
559 for (; cmd->name; cmd++) {
560 if (cmd->lcount == 0 && cmd->rcount == 0)
562 if (!IRC_WriteStrClient
563 (from, RPL_STATSCOMMANDS_MSG, Client_ID(from),
564 cmd->name, cmd->lcount, cmd->bytes, cmd->rcount))
568 case 'u': /* Server uptime */
570 time_now = time(NULL) - NGIRCd_Start;
571 days = uptime_days(&time_now);
572 hrs = uptime_hrs(&time_now);
573 mins = uptime_mins(&time_now);
574 if (!IRC_WriteStrClient(from, RPL_STATSUPTIME, Client_ID(from),
575 days, hrs, mins, (unsigned int)time_now))
580 IRC_SetPenalty(from, 2);
581 return IRC_WriteStrClient(from, RPL_ENDOFSTATS_MSG,
582 Client_ID(from), query);
587 * Handler for the IRC command "SUMMON".
588 * See RFC 2812 section 4.5. ngIRCd doesn't implement this functionality and
589 * therefore answers with ERR_SUMMONDISABLED.
592 IRC_SUMMON(CLIENT * Client, REQUEST * Req)
594 return IRC_WriteStrClient(Client, ERR_SUMMONDISABLED_MSG,
595 Client_ID(Client), Req->command);
600 IRC_TIME( CLIENT *Client, REQUEST *Req )
602 CLIENT *from, *target;
606 assert( Client != NULL );
607 assert( Req != NULL );
609 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
611 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
613 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
617 target = Client_Search( Req->argv[0] );
618 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
620 if( target != Client_ThisServer( ))
622 return IRC_WriteStrClientPrefix( target, from, "TIME %s", Req->argv[0] );
627 (void)strftime( t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime( &t ));
628 return IRC_WriteStrClient( from, RPL_TIME_MSG, Client_ID( from ), Client_ID( Client_ThisServer( )), t_str );
633 * Handler for the IRC command "USERHOST".
634 * See RFC 2812 section 4.8.
637 IRC_USERHOST(CLIENT *Client, REQUEST *Req)
639 char rpl[COMMAND_LEN];
643 assert(Client != NULL);
647 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
648 Client_ID(Client), Req->command);
655 strlcpy(rpl, RPL_USERHOST_MSG, sizeof rpl);
656 for (i = 0; i < max; i++) {
657 c = Client_Search(Req->argv[i]);
658 if (c && (Client_Type(c) == CLIENT_USER)) {
659 /* This Nick is "online" */
660 strlcat(rpl, Client_ID(c), sizeof(rpl));
661 if (Client_HasMode(c, 'o'))
662 strlcat(rpl, "*", sizeof(rpl));
663 strlcat(rpl, "=", sizeof(rpl));
664 if (Client_HasMode(c, 'a'))
665 strlcat(rpl, "-", sizeof(rpl));
667 strlcat(rpl, "+", sizeof(rpl));
668 strlcat(rpl, Client_User(c), sizeof(rpl));
669 strlcat(rpl, "@", sizeof(rpl));
670 strlcat(rpl, Client_HostnameCloaked(c), sizeof(rpl));
671 strlcat(rpl, " ", sizeof(rpl));
674 ngt_TrimLastChr(rpl, ' ');
676 return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
681 * Handler for the IRC command "USERS".
682 * See RFC 2812 section 4.6. As suggested there the command is disabled.
685 IRC_USERS(CLIENT * Client, REQUEST * Req)
687 return IRC_WriteStrClient(Client, ERR_USERSDISABLED_MSG,
688 Client_ID(Client), Req->command);
693 IRC_VERSION( CLIENT *Client, REQUEST *Req )
695 CLIENT *target, *prefix;
697 assert( Client != NULL );
698 assert( Req != NULL );
700 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
703 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
704 else target = Client_ThisServer( );
706 /* Prefix ermitteln */
707 if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
708 else prefix = Client;
709 if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
711 /* An anderen Server weiterleiten? */
712 if( target != Client_ThisServer( ))
714 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
717 IRC_WriteStrClientPrefix( target, prefix, "VERSION %s", Req->argv[0] );
721 /* send version information */
722 IRC_SetPenalty(Client, 1);
723 return IRC_WriteStrClient(Client, RPL_VERSION_MSG, Client_ID(prefix),
724 PACKAGE_NAME, PACKAGE_VERSION,
725 NGIRCd_DebugLevel, Conf_ServerName,
726 NGIRCd_VersionAddition);
731 write_whoreply(CLIENT *Client, CLIENT *c, const char *channelname, const char *flags)
733 return IRC_WriteStrClient(Client, RPL_WHOREPLY_MSG, Client_ID(Client),
734 channelname, Client_User(c),
735 Client_HostnameCloaked(c),
736 Client_ID(Client_Introducer(c)), Client_ID(c),
737 flags, Client_Hops(c), Client_Info(c));
742 who_flags_status(const char *client_modes)
744 if (strchr(client_modes, 'a'))
745 return "G"; /* away */
751 who_flags_qualifier(const char *chan_user_modes)
753 if (strchr(chan_user_modes, 'o'))
755 else if (strchr(chan_user_modes, 'v'))
762 IRC_Send_WHO(CLIENT *Client, CHANNEL *Chan, bool OnlyOps)
764 bool is_visible, is_member, is_ircop;
766 const char *client_modes;
767 const char *chan_user_modes;
771 assert( Client != NULL );
772 assert( Chan != NULL );
774 is_member = Channel_IsMemberOf(Chan, Client);
776 /* Secret channel? */
777 if (!is_member && strchr(Channel_Modes(Chan), 's'))
778 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), Channel_Name(Chan));
780 cl2chan = Channel_FirstMember(Chan);
781 for (; cl2chan ; cl2chan = Channel_NextMember(Chan, cl2chan)) {
782 c = Channel_GetClient(cl2chan);
784 client_modes = Client_Modes(c);
785 is_ircop = strchr(client_modes, 'o') != NULL;
786 if (OnlyOps && !is_ircop)
789 is_visible = strchr(client_modes, 'i') == NULL;
790 if (is_member || is_visible) {
791 strcpy(flags, who_flags_status(client_modes));
793 strlcat(flags, "*", sizeof(flags));
795 chan_user_modes = Channel_UserModes(Chan, c);
796 strlcat(flags, who_flags_qualifier(chan_user_modes), sizeof(flags));
798 if (!write_whoreply(Client, c, Channel_Name(Chan), flags))
802 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), Channel_Name(Chan));
807 IRC_WHO( CLIENT *Client, REQUEST *Req )
809 bool only_ops, have_arg, client_match;
810 const char *channelname, *client_modes, *chan_user_modes;
811 char pattern[COMMAND_LEN];
817 assert( Client != NULL );
818 assert( Req != NULL );
821 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
826 if (Req->argc == 2) {
827 if (strcmp(Req->argv[1], "o") == 0)
830 else return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
834 IRC_SetPenalty(Client, 1);
835 if (Req->argc >= 1) { /* Channel or Mask. */
836 chan = Channel_Search(Req->argv[0]);
838 return IRC_Send_WHO(Client, chan, only_ops);
839 if (strcmp(Req->argv[0], "0") != 0) { /* RFC stupidity, same as no arguments */
841 strlcpy(pattern, Req->argv[0], sizeof(pattern));
842 ngt_LowerStr(pattern);
843 IRC_SetPenalty(Client, 3);
847 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
848 if (Client_Type(c) != CLIENT_USER)
852 * In the absence of the parameter, all visible (users who aren't
853 * invisible (user mode +i) and who don't have a common channel
854 * with the requesting client) are listed.
856 * The same result can be achieved by using a [sic] of "0"
857 * or any wildcard which will end up matching every visible user.
859 * The [sic] passed to WHO is matched against users' host, server, real name and
860 * nickname if the channel cannot be found.
862 client_modes = Client_Modes(c);
863 if (strchr(client_modes, 'i'))
866 if (only_ops && !strchr(client_modes, 'o'))
869 if (have_arg) { /* match pattern against user host/server/name/nick */
870 client_match = MatchCaseInsensitive(pattern, Client_Hostname(c)); /* user's host */
872 client_match = MatchCaseInsensitive(pattern, Client_ID(Client_Introducer(c))); /* server */
874 client_match = Match(Req->argv[0], Client_Info(c)); /* realname */
876 client_match = MatchCaseInsensitive(pattern, Client_ID(c)); /* nick name */
878 if (!client_match) /* This isn't the client you're looking for */
882 strcpy(flags, who_flags_status(client_modes));
884 if (strchr(client_modes, 'o')) /* this client is an operator */
885 strlcat(flags, "*", sizeof(flags));
887 /* Search suitable channel */
888 cl2chan = Channel_FirstChannelOf(c);
890 cn = Channel_GetChannel(cl2chan);
891 if (Channel_IsMemberOf(cn, Client) ||
892 !strchr(Channel_Modes(cn), 's'))
894 channelname = Channel_Name(cn);
897 cl2chan = Channel_NextChannelOf(c, cl2chan);
900 chan = Channel_GetChannel(cl2chan);
901 chan_user_modes = Channel_UserModes(chan, c);
902 strlcat(flags, who_flags_qualifier(chan_user_modes), sizeof(flags));
906 if (!write_whoreply(Client, c, channelname, flags))
911 channelname = Req->argv[0];
915 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), channelname);
920 * Handler for the IRC "WHOIS" command.
922 * See RFC 2812, 3.6.2 "Whois query".
924 * @param Client The client from which this command has been received.
925 * @param Req Request structure with prefix and all parameters.
926 * @return CONNECTED or DISCONNECTED.
929 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
931 CLIENT *from, *target, *c;
932 char str[LINE_LEN + 1];
936 assert( Client != NULL );
937 assert( Req != NULL );
939 /* Bad number of parameters? */
940 if (Req->argc < 1 || Req->argc > 2)
941 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
942 Client_ID(Client), Req->command);
945 c = Client_Search(Req->argv[Req->argc - 1]);
946 if (!c || (Client_Type(c) != CLIENT_USER
947 && Client_Type(c) != CLIENT_SERVICE))
948 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
950 Req->argv[Req->argc - 1]);
952 /* Search sender of the WHOIS */
953 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
955 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
957 /* Forward to other server? */
960 /* Search target server (can be specified as nick of that server!) */
961 target = Client_Search( Req->argv[0] );
962 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
964 else target = Client_ThisServer( );
966 assert( target != NULL );
968 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] );
970 /* Nick, user, hostname and client info */
971 if (!IRC_WriteStrClient(from, RPL_WHOISUSER_MSG, Client_ID(from),
972 Client_ID(c), Client_User(c),
973 Client_HostnameCloaked(c), Client_Info(c)))
977 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;
980 snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
981 cl2chan = Channel_FirstChannelOf( c );
984 chan = Channel_GetChannel( cl2chan );
985 assert( chan != NULL );
988 cl2chan = Channel_NextChannelOf( c, cl2chan );
990 /* Secret channel? */
991 if (strchr(Channel_Modes(chan), 's')
992 && !Channel_IsMemberOf(chan, Client))
995 /* Local channel and request is not from a user? */
996 if (Client_Type(Client) == CLIENT_SERVER
997 && Channel_IsLocal(chan))
1000 /* Concatenate channel names */
1001 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
1002 if( strchr( Channel_UserModes( chan, c ), 'o' )) strlcat( str, "@", sizeof( str ));
1003 else if( strchr( Channel_UserModes( chan, c ), 'v' )) strlcat( str, "+", sizeof( str ));
1004 strlcat( str, Channel_Name( chan ), sizeof( str ));
1006 if( strlen( str ) > ( LINE_LEN - CHANNEL_NAME_LEN - 4 ))
1008 /* Line becomes too long: send it! */
1009 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1010 snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
1013 if( str[strlen( str ) - 1] != ':')
1015 /* There is data left to send: */
1016 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1020 if( Client_HasMode( c, 'o' ))
1022 if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
1025 /* Connected using SSL? */
1026 if (Conn_UsesSSL(Client_Conn(c))) {
1027 if (!IRC_WriteStrClient
1028 (from, RPL_WHOISSSL_MSG, Client_ID(from), Client_ID(c)))
1029 return DISCONNECTED;
1032 /* Idle and signon time (local clients only!) */
1033 if (Client_Conn(c) > NONE ) {
1034 if (! IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
1035 Client_ID(from), Client_ID(c),
1036 (unsigned long)Conn_GetIdle(Client_Conn(c)),
1037 (unsigned long)Conn_GetSignon(Client_Conn(c))))
1038 return DISCONNECTED;
1042 if( Client_HasMode( c, 'a' ))
1044 if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( c ), Client_Away( c ))) return DISCONNECTED;
1048 return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
1053 WHOWAS_EntryWrite(CLIENT *prefix, WHOWAS *entry)
1057 (void)strftime(t_str, sizeof(t_str), "%a %b %d %H:%M:%S %Y",
1058 localtime(&entry->time));
1060 if (!IRC_WriteStrClient(prefix, RPL_WHOWASUSER_MSG, Client_ID(prefix),
1061 entry->id, entry->user, entry->host, entry->info))
1062 return DISCONNECTED;
1064 return IRC_WriteStrClient(prefix, RPL_WHOISSERVER_MSG, Client_ID(prefix),
1065 entry->id, entry->server, t_str);
1069 * IRC "WHOWAS" function.
1070 * This function implements the IRC command "WHOWHAS". It handles local
1071 * requests and request that should be forwarded to other servers.
1074 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1076 CLIENT *target, *prefix;
1078 char tok_buf[COMMAND_LEN];
1079 int max, last, count, i, nc;
1082 assert( Client != NULL );
1083 assert( Req != NULL );
1085 /* Wrong number of parameters? */
1087 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1088 Client_ID(Client), Req->command);
1090 return IRC_WriteStrClient(Client, ERR_NONICKNAMEGIVEN_MSG, Client_ID(Client));
1094 target = Client_Search(Req->argv[2]);
1096 target = Client_ThisServer();
1099 if (Client_Type(Client) == CLIENT_SERVER)
1100 prefix = Client_Search(Req->prefix);
1105 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1106 Client_ID(Client), Req->prefix);
1108 /* Forward to other server? */
1109 if (target != Client_ThisServer()) {
1110 if (!target || (Client_Type(target) != CLIENT_SERVER))
1111 return IRC_WriteStrClient(prefix, ERR_NOSUCHSERVER_MSG,
1112 Client_ID(prefix), Req->argv[2]);
1115 IRC_WriteStrClientPrefix( target, prefix, "WHOWAS %s %s %s",
1116 Req->argv[0], Req->argv[1],
1121 whowas = Client_GetWhowas( );
1122 last = Client_GetLastWhowasIndex( );
1126 max = DEFAULT_WHOWAS;
1127 if (Req->argc > 1) {
1128 max = atoi(Req->argv[1]);
1134 * Break up the nick argument into a list of nicks, if applicable
1135 * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1137 strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1138 nick = strtok(tok_buf, ",");
1140 for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1144 if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1145 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1146 return DISCONNECTED;
1150 /* previous entry */
1153 /* "underflow", wrap around */
1157 if (nc && count >= max)
1159 } while (i != last);
1161 if (nc == 0 && !IRC_WriteStrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1162 Client_ID(prefix), nick))
1163 return DISCONNECTED;
1165 return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG, Client_ID(prefix), Req->argv[0]);
1170 IRC_Send_LUSERS( CLIENT *Client )
1177 assert( Client != NULL );
1179 /* Users, services and serevers in the network */
1180 if( ! IRC_WriteStrClient( Client, RPL_LUSERCLIENT_MSG, Client_ID( Client ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
1182 /* Number of IRC operators */
1183 cnt = Client_OperCount( );
1186 if( ! IRC_WriteStrClient( Client, RPL_LUSEROP_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
1189 /* Unknown connections */
1190 cnt = Client_UnknownCount( );
1193 if( ! IRC_WriteStrClient( Client, RPL_LUSERUNKNOWN_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
1196 /* Number of created channels */
1197 if( ! IRC_WriteStrClient( Client, RPL_LUSERCHANNELS_MSG, Client_ID( Client ), Channel_Count( ))) return DISCONNECTED;
1199 /* Number of local users, services and servers */
1200 if( ! IRC_WriteStrClient( Client, RPL_LUSERME_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
1203 /* Maximum number of local users */
1204 cnt = Client_MyUserCount();
1205 max = Client_MyMaxUserCount();
1206 if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1207 cnt, max, cnt, max))
1208 return DISCONNECTED;
1209 /* Maximum number of users in the network */
1210 cnt = Client_UserCount();
1211 max = Client_MaxUserCount();
1212 if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1213 cnt, max, cnt, max))
1214 return DISCONNECTED;
1215 /* Connection counters */
1216 if (! IRC_WriteStrClient(Client, RPL_STATSCONN_MSG, Client_ID(Client),
1217 Conn_CountMax(), Conn_CountAccepted()))
1218 return DISCONNECTED;
1222 } /* IRC_Send_LUSERS */
1226 Show_MOTD_Start(CLIENT *Client)
1228 return IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG,
1229 Client_ID( Client ), Client_ID( Client_ThisServer( )));
1233 Show_MOTD_Sendline(CLIENT *Client, const char *msg)
1235 return IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID( Client ), msg);
1239 Show_MOTD_End(CLIENT *Client)
1241 return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
1245 static bool Show_MOTD_SSLInfo(CLIENT *Client)
1248 char buf[COMMAND_LEN] = "Connected using Cipher ";
1250 if (!Conn_GetCipherInfo(Client_Conn(Client), buf + 23, sizeof buf - 23))
1253 if (!Show_MOTD_Sendline(Client, buf))
1260 Show_MOTD_SSLInfo(UNUSED CLIENT *c)
1265 IRC_Show_MOTD( CLIENT *Client )
1268 size_t len_tot, len_str;
1270 assert( Client != NULL );
1272 len_tot = array_bytes(&Conf_Motd);
1273 if (len_tot == 0 && !Conn_UsesSSL(Client_Conn(Client)))
1274 return IRC_WriteStrClient(Client, ERR_NOMOTD_MSG, Client_ID(Client));
1276 if (!Show_MOTD_Start(Client))
1277 return DISCONNECTED;
1279 line = array_start(&Conf_Motd);
1280 while (len_tot > 0) {
1281 len_str = strlen(line) + 1;
1283 assert(len_tot >= len_str);
1286 if (!Show_MOTD_Sendline(Client, line))
1287 return DISCONNECTED;
1291 if (!Show_MOTD_SSLInfo(Client))
1292 return DISCONNECTED;
1293 return Show_MOTD_End(Client);
1294 } /* IRC_Show_MOTD */
1298 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
1300 bool is_visible, is_member;
1301 char str[LINE_LEN + 1];
1305 assert( Client != NULL );
1306 assert( Chan != NULL );
1308 if( Channel_IsMemberOf( Chan, Client )) is_member = true;
1309 else is_member = false;
1311 /* Secret channel? */
1312 if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
1314 /* Alle Mitglieder suchen */
1315 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1316 cl2chan = Channel_FirstMember( Chan );
1319 cl = Channel_GetClient( cl2chan );
1321 if( strchr( Client_Modes( cl ), 'i' )) is_visible = false;
1322 else is_visible = true;
1324 if( is_member || is_visible )
1326 /* Nick anhaengen */
1327 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
1328 if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
1329 else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
1330 strlcat( str, Client_ID( cl ), sizeof( str ));
1332 if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
1334 /* Zeile wird zu lang: senden! */
1335 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1336 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1340 /* naechstes Mitglied suchen */
1341 cl2chan = Channel_NextMember( Chan, cl2chan );
1343 if( str[strlen( str ) - 1] != ':')
1345 /* Es sind noch Daten da, die gesendet werden muessen */
1346 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1350 } /* IRC_Send_NAMES */
1354 * Send the ISUPPORT numeric (005).
1355 * This numeric indicates the features that are supported by this server.
1356 * See <http://www.irc.org/tech_docs/005.html> for details.
1359 IRC_Send_ISUPPORT(CLIENT * Client)
1361 if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1363 return DISCONNECTED;
1364 return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1365 CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1366 COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1368 } /* IRC_Send_ISUPPORT */