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.
29 #include "conn-func.h"
39 #include "irc-write.h"
46 IRC_ADMIN(CLIENT *Client, REQUEST *Req )
48 CLIENT *target, *prefix;
50 assert( Client != NULL );
51 assert( Req != NULL );
53 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
56 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
57 else target = Client_ThisServer( );
60 if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
62 if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
64 /* forwad message to another server? */
65 if( target != Client_ThisServer( ))
67 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
70 IRC_WriteStrClientPrefix( target, prefix, "ADMIN %s", Req->argv[0] );
74 /* mit Versionsinfo antworten */
75 if( ! IRC_WriteStrClient( Client, RPL_ADMINME_MSG, Client_ID( prefix ), Conf_ServerName )) return DISCONNECTED;
76 if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC1_MSG, Client_ID( prefix ), Conf_ServerAdmin1 )) return DISCONNECTED;
77 if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC2_MSG, Client_ID( prefix ), Conf_ServerAdmin2 )) return DISCONNECTED;
78 if( ! IRC_WriteStrClient( Client, RPL_ADMINEMAIL_MSG, Client_ID( prefix ), Conf_ServerAdminMail )) return DISCONNECTED;
80 IRC_SetPenalty( Client, 1 );
86 * Handler for the IRC command "INFO".
87 * See RFC 2812 section 3.4.10.
90 IRC_INFO(CLIENT * Client, REQUEST * Req)
92 CLIENT *target, *prefix;
95 assert(Client != NULL);
98 /* Wrong number of parameters? */
100 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
101 Client_ID(Client), Req->command);
103 /* Determine prefix */
104 if (Client_Type(Client) == CLIENT_SERVER)
105 prefix = Client_Search(Req->prefix);
109 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
110 Client_ID(Client), Req->prefix);
112 /* Look for a target */
114 target = Client_Search(Req->argv[0]);
116 target = Client_ThisServer();
118 /* Make sure that the target is a server */
119 if (target && Client_Type(target) != CLIENT_SERVER)
120 target = Client_Introducer(target);
123 return IRC_WriteStrClient(prefix, ERR_NOSUCHSERVER_MSG,
124 Client_ID(prefix), Req->argv[0]);
126 /* Pass on to another server? */
127 if (target != Client_ThisServer()) {
128 IRC_WriteStrClientPrefix(target, prefix, "INFO %s",
133 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix),
137 #if defined(__DATE__) && defined(__TIME__)
138 snprintf(msg, sizeof(msg), "Birth Date: %s at %s", __DATE__, __TIME__);
139 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
143 strlcpy(msg, "On-line since ", sizeof(msg));
144 strlcat(msg, NGIRCd_StartStr, sizeof(msg));
145 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
148 if (!IRC_WriteStrClient(Client, RPL_ENDOFINFO_MSG, Client_ID(prefix)))
151 IRC_SetPenalty(Client, 2);
157 IRC_ISON( CLIENT *Client, REQUEST *Req )
159 char rpl[COMMAND_LEN];
164 assert( Client != NULL );
165 assert( Req != NULL );
167 /* Falsche Anzahl Parameter? */
168 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
170 strlcpy( rpl, RPL_ISON_MSG, sizeof rpl );
171 for( i = 0; i < Req->argc; i++ )
173 ptr = strtok( Req->argv[i], " " );
177 c = Client_Search( ptr );
178 if( c && ( Client_Type( c ) == CLIENT_USER ))
180 /* Dieser Nick ist "online" */
181 strlcat( rpl, ptr, sizeof( rpl ));
182 strlcat( rpl, " ", sizeof( rpl ));
184 ptr = strtok( NULL, " " );
187 ngt_TrimLastChr(rpl, ' ');
189 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
194 IRC_LINKS( CLIENT *Client, REQUEST *Req )
196 CLIENT *target, *from, *c;
199 assert( Client != NULL );
200 assert( Req != NULL );
202 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
204 /* Server-Mask ermitteln */
205 if( Req->argc > 0 ) mask = Req->argv[Req->argc - 1];
208 /* Absender ermitteln */
209 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
211 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
213 /* An anderen Server forwarden? */
216 target = Client_Search( Req->argv[0] );
217 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
218 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LINKS %s %s", Req->argv[0], Req->argv[1] );
221 /* Wer ist der Absender? */
222 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
223 else target = Client;
224 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
229 if( Client_Type( c ) == CLIENT_SERVER )
231 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;
233 c = Client_Next( c );
236 IRC_SetPenalty( target, 1 );
237 return IRC_WriteStrClient( target, RPL_ENDOFLINKS_MSG, Client_ID( target ), mask );
242 IRC_LUSERS( CLIENT *Client, REQUEST *Req )
244 CLIENT *target, *from;
246 assert( Client != NULL );
247 assert( Req != NULL );
249 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
251 /* Absender ermitteln */
252 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
254 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
256 /* An anderen Server forwarden? */
259 target = Client_Search( Req->argv[1] );
260 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
261 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LUSERS %s %s", Req->argv[0], Req->argv[1] );
264 /* Wer ist der Absender? */
265 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
266 else target = Client;
267 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
269 IRC_Send_LUSERS( target );
271 IRC_SetPenalty( target, 1 );
277 * Handler for the IRC command "SERVLIST".
278 * List registered services, see RFC 2811, section 3.5.1: the syntax is
279 * "SERVLIST [<mask> [<type>]]".
282 IRC_SERVLIST(CLIENT *Client, REQUEST *Req)
286 assert(Client != NULL);
290 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
291 Client_ID(Client), Req->command);
293 if (Req->argc < 2 || strcmp(Req->argv[1], "0") == 0) {
294 for (c = Client_First(); c!= NULL; c = Client_Next(c)) {
295 if (Client_Type(c) != CLIENT_SERVICE)
297 if (Req->argc > 0 && !MatchCaseInsensitive(Req->argv[0],
300 if (!IRC_WriteStrClient(Client, RPL_SERVLIST_MSG,
301 Client_ID(Client), Client_Mask(c),
302 Client_Mask(Client_Introducer(c)), "*",
303 0, Client_Hops(c), Client_Info(c)))
308 return IRC_WriteStrClient(Client, RPL_SERVLISTEND_MSG, Client_ID(Client),
309 Req->argc > 0 ? Req->argv[0] : "*",
310 Req->argc > 1 ? Req->argv[1] : "0");
315 IRC_MOTD( CLIENT *Client, REQUEST *Req )
317 CLIENT *from, *target;
319 assert( Client != NULL );
320 assert( Req != NULL );
322 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
324 /* From aus Prefix ermitteln */
325 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
327 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
332 target = Client_Search( Req->argv[0] );
333 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
335 if( target != Client_ThisServer( ))
337 /* Ok, anderer Server ist das Ziel: forwarden */
338 return IRC_WriteStrClientPrefix( target, from, "MOTD %s", Req->argv[0] );
342 IRC_SetPenalty( from, 3 );
343 return IRC_Show_MOTD( from );
348 IRC_NAMES( CLIENT *Client, REQUEST *Req )
350 char rpl[COMMAND_LEN], *ptr;
351 CLIENT *target, *from, *c;
354 assert( Client != NULL );
355 assert( Req != NULL );
357 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
359 /* use prefix to determine "From" */
360 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
362 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
366 /* forward to another server? */
367 target = Client_Search( Req->argv[1] );
368 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
370 if( target != Client_ThisServer( )) {
371 /* target is another server, forward */
372 return IRC_WriteStrClientPrefix( target, from, "NAMES %s :%s", Req->argv[0], Req->argv[1] );
378 /* bestimmte Channels durchgehen */
379 ptr = strtok( Req->argv[0], "," );
382 chan = Channel_Search( ptr );
386 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
388 if( ! IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), ptr )) return DISCONNECTED;
390 /* get next channel name */
391 ptr = strtok( NULL, "," );
396 chan = Channel_First( );
399 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
401 chan = Channel_Next( chan );
404 /* Now print all clients which are not in any channel */
406 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
409 if(( Client_Type( c ) == CLIENT_USER ) && ( Channel_FirstChannelOf( c ) == NULL ) && ( ! strchr( Client_Modes( c ), 'i' )))
411 /* its a user, concatenate ... */
412 if( rpl[strlen( rpl ) - 1] != ':' ) strlcat( rpl, " ", sizeof( rpl ));
413 strlcat( rpl, Client_ID( c ), sizeof( rpl ));
415 if( strlen( rpl ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
417 /* Line is gwoing too long, send now */
418 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
419 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
423 c = Client_Next( c );
425 if( rpl[strlen( rpl ) - 1] != ':')
427 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
430 IRC_SetPenalty( from, 1 );
431 return IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), "*" );
436 t_diff(time_t *t, const time_t d)
444 return (unsigned int)diff;
449 uptime_days(time_t *now)
451 return t_diff(now, 60 * 60 * 24);
456 uptime_hrs(time_t *now)
458 return t_diff(now, 60 * 60);
463 uptime_mins(time_t *now)
465 return t_diff(now, 60);
470 * Handler for the IRC command "STATS".
471 * See RFC 2812 section 3.4.4.
474 IRC_STATS( CLIENT *Client, REQUEST *Req )
476 CLIENT *from, *target, *cl;
481 unsigned int days, hrs, mins;
483 assert(Client != NULL);
487 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
488 Client_ID(Client), Req->command);
490 /* use prefix to determine "From" */
491 if (Client_Type(Client) == CLIENT_SERVER)
492 from = Client_Search(Req->prefix);
497 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
498 Client_ID(Client), Req->prefix);
500 if (Req->argc == 2) {
501 /* forward to another server? */
502 target = Client_Search(Req->argv[1]);
503 if ((!target) || (Client_Type(target) != CLIENT_SERVER))
504 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
505 Client_ID(from), Req->argv[1]);
507 if (target != Client_ThisServer()) {
508 /* forward to another server */
509 return IRC_WriteStrClientPrefix(target, from,
510 "STATS %s %s", Req->argv[0], Req->argv[1]);
515 query = Req->argv[0][0] ? Req->argv[0][0] : '*';
520 case 'l': /* Link status (servers and own link) */
522 time_now = time(NULL);
523 for (con = Conn_First(); con != NONE; con = Conn_Next(con)) {
524 cl = Conn_GetClient(con);
527 if ((Client_Type(cl) == CLIENT_SERVER)
529 /* Server link or our own connection */
531 if (Conn_Options(con) & CONN_ZIP) {
532 if (!IRC_WriteStrClient
533 (from, RPL_STATSLINKINFOZIP_MSG,
534 Client_ID(from), Client_Mask(cl),
535 Conn_SendQ(con), Conn_SendMsg(con),
541 (long)(time_now - Conn_StartTime(con))))
546 if (!IRC_WriteStrClient
547 (from, RPL_STATSLINKINFO_MSG,
548 Client_ID(from), Client_Mask(cl),
549 Conn_SendQ(con), Conn_SendMsg(con),
550 Conn_SendBytes(con), Conn_RecvMsg(con),
552 (long)(time_now - Conn_StartTime(con))))
557 case 'm': /* IRC command status (usage count) */
559 cmd = Parse_GetCommandStruct();
560 for (; cmd->name; cmd++) {
561 if (cmd->lcount == 0 && cmd->rcount == 0)
563 if (!IRC_WriteStrClient
564 (from, RPL_STATSCOMMANDS_MSG, Client_ID(from),
565 cmd->name, cmd->lcount, cmd->bytes, cmd->rcount))
569 case 'u': /* Server uptime */
571 time_now = time(NULL) - NGIRCd_Start;
572 days = uptime_days(&time_now);
573 hrs = uptime_hrs(&time_now);
574 mins = uptime_mins(&time_now);
575 if (!IRC_WriteStrClient(from, RPL_STATSUPTIME, Client_ID(from),
576 days, hrs, mins, (unsigned int)time_now))
581 IRC_SetPenalty(from, 2);
582 return IRC_WriteStrClient(from, RPL_ENDOFSTATS_MSG,
583 Client_ID(from), query);
588 * Handler for the IRC command "SUMMON".
589 * See RFC 2812 section 4.5. ngIRCd doesn't implement this functionality and
590 * therefore answers with ERR_SUMMONDISABLED.
593 IRC_SUMMON(CLIENT * Client, REQUEST * Req)
595 return IRC_WriteStrClient(Client, ERR_SUMMONDISABLED_MSG,
596 Client_ID(Client), Req->command);
601 IRC_TIME( CLIENT *Client, REQUEST *Req )
603 CLIENT *from, *target;
607 assert( Client != NULL );
608 assert( Req != NULL );
610 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
612 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
614 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
618 target = Client_Search( Req->argv[0] );
619 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
621 if( target != Client_ThisServer( ))
623 return IRC_WriteStrClientPrefix( target, from, "TIME %s", Req->argv[0] );
628 (void)strftime( t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime( &t ));
629 return IRC_WriteStrClient( from, RPL_TIME_MSG, Client_ID( from ), Client_ID( Client_ThisServer( )), t_str );
634 * Handler for the IRC command "USERHOST".
635 * See RFC 2812 section 4.8.
638 IRC_USERHOST(CLIENT *Client, REQUEST *Req)
640 char rpl[COMMAND_LEN];
644 assert(Client != NULL);
648 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
649 Client_ID(Client), Req->command);
656 strlcpy(rpl, RPL_USERHOST_MSG, sizeof rpl);
657 for (i = 0; i < max; i++) {
658 c = Client_Search(Req->argv[i]);
659 if (c && (Client_Type(c) == CLIENT_USER)) {
660 /* This Nick is "online" */
661 strlcat(rpl, Client_ID(c), sizeof(rpl));
662 if (Client_HasMode(c, 'o'))
663 strlcat(rpl, "*", sizeof(rpl));
664 strlcat(rpl, "=", sizeof(rpl));
665 if (Client_HasMode(c, 'a'))
666 strlcat(rpl, "-", sizeof(rpl));
668 strlcat(rpl, "+", sizeof(rpl));
669 strlcat(rpl, Client_User(c), sizeof(rpl));
670 strlcat(rpl, "@", sizeof(rpl));
671 strlcat(rpl, Client_HostnameCloaked(c), sizeof(rpl));
672 strlcat(rpl, " ", sizeof(rpl));
675 ngt_TrimLastChr(rpl, ' ');
677 return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
682 * Handler for the IRC command "USERS".
683 * See RFC 2812 section 4.6. As suggested there the command is disabled.
686 IRC_USERS(CLIENT * Client, REQUEST * Req)
688 return IRC_WriteStrClient(Client, ERR_USERSDISABLED_MSG,
689 Client_ID(Client), Req->command);
694 IRC_VERSION( CLIENT *Client, REQUEST *Req )
696 CLIENT *target, *prefix;
698 assert( Client != NULL );
699 assert( Req != NULL );
701 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
704 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
705 else target = Client_ThisServer( );
707 /* Prefix ermitteln */
708 if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
709 else prefix = Client;
710 if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
712 /* An anderen Server weiterleiten? */
713 if( target != Client_ThisServer( ))
715 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
718 IRC_WriteStrClientPrefix( target, prefix, "VERSION %s", Req->argv[0] );
722 /* send version information */
723 IRC_SetPenalty(Client, 1);
724 return IRC_WriteStrClient(Client, RPL_VERSION_MSG, Client_ID(prefix),
725 PACKAGE_NAME, PACKAGE_VERSION,
726 NGIRCd_DebugLevel, Conf_ServerName,
727 NGIRCd_VersionAddition);
732 write_whoreply(CLIENT *Client, CLIENT *c, const char *channelname, const char *flags)
734 return IRC_WriteStrClient(Client, RPL_WHOREPLY_MSG, Client_ID(Client),
735 channelname, Client_User(c),
736 Client_HostnameCloaked(c),
737 Client_ID(Client_Introducer(c)), Client_ID(c),
738 flags, Client_Hops(c), Client_Info(c));
743 who_flags_status(const char *client_modes)
745 if (strchr(client_modes, 'a'))
746 return "G"; /* away */
752 who_flags_qualifier(const char *chan_user_modes)
754 if (strchr(chan_user_modes, 'o'))
756 else if (strchr(chan_user_modes, 'v'))
763 IRC_Send_WHO(CLIENT *Client, CHANNEL *Chan, bool OnlyOps)
765 bool is_visible, is_member, is_ircop;
767 const char *client_modes;
768 const char *chan_user_modes;
772 assert( Client != NULL );
773 assert( Chan != NULL );
775 is_member = Channel_IsMemberOf(Chan, Client);
777 /* Secret channel? */
778 if (!is_member && strchr(Channel_Modes(Chan), 's'))
779 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), Channel_Name(Chan));
781 cl2chan = Channel_FirstMember(Chan);
782 for (; cl2chan ; cl2chan = Channel_NextMember(Chan, cl2chan)) {
783 c = Channel_GetClient(cl2chan);
785 client_modes = Client_Modes(c);
786 is_ircop = strchr(client_modes, 'o') != NULL;
787 if (OnlyOps && !is_ircop)
790 is_visible = strchr(client_modes, 'i') == NULL;
791 if (is_member || is_visible) {
792 strcpy(flags, who_flags_status(client_modes));
794 strlcat(flags, "*", sizeof(flags));
796 chan_user_modes = Channel_UserModes(Chan, c);
797 strlcat(flags, who_flags_qualifier(chan_user_modes), sizeof(flags));
799 if (!write_whoreply(Client, c, Channel_Name(Chan), flags))
803 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), Channel_Name(Chan));
808 IRC_WHO( CLIENT *Client, REQUEST *Req )
810 bool only_ops, have_arg, client_match;
811 const char *channelname, *client_modes, *chan_user_modes;
812 char pattern[COMMAND_LEN];
818 assert( Client != NULL );
819 assert( Req != NULL );
822 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
827 if (Req->argc == 2) {
828 if (strcmp(Req->argv[1], "o") == 0)
831 else return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
835 IRC_SetPenalty(Client, 1);
836 if (Req->argc >= 1) { /* Channel or Mask. */
837 chan = Channel_Search(Req->argv[0]);
839 return IRC_Send_WHO(Client, chan, only_ops);
840 if (strcmp(Req->argv[0], "0") != 0) { /* RFC stupidity, same as no arguments */
842 strlcpy(pattern, Req->argv[0], sizeof(pattern));
843 ngt_LowerStr(pattern);
844 IRC_SetPenalty(Client, 3);
848 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
849 if (Client_Type(c) != CLIENT_USER)
853 * In the absence of the parameter, all visible (users who aren't
854 * invisible (user mode +i) and who don't have a common channel
855 * with the requesting client) are listed.
857 * The same result can be achieved by using a [sic] of "0"
858 * or any wildcard which will end up matching every visible user.
860 * The [sic] passed to WHO is matched against users' host, server, real name and
861 * nickname if the channel cannot be found.
863 client_modes = Client_Modes(c);
864 if (strchr(client_modes, 'i'))
867 if (only_ops && !strchr(client_modes, 'o'))
870 if (have_arg) { /* match pattern against user host/server/name/nick */
871 client_match = MatchCaseInsensitive(pattern, Client_Hostname(c)); /* user's host */
873 client_match = MatchCaseInsensitive(pattern, Client_ID(Client_Introducer(c))); /* server */
875 client_match = Match(Req->argv[0], Client_Info(c)); /* realname */
877 client_match = MatchCaseInsensitive(pattern, Client_ID(c)); /* nick name */
879 if (!client_match) /* This isn't the client you're looking for */
883 strcpy(flags, who_flags_status(client_modes));
885 if (strchr(client_modes, 'o')) /* this client is an operator */
886 strlcat(flags, "*", sizeof(flags));
888 /* Search suitable channel */
889 cl2chan = Channel_FirstChannelOf(c);
891 cn = Channel_GetChannel(cl2chan);
892 if (Channel_IsMemberOf(cn, Client) ||
893 !strchr(Channel_Modes(cn), 's'))
895 channelname = Channel_Name(cn);
898 cl2chan = Channel_NextChannelOf(c, cl2chan);
901 chan = Channel_GetChannel(cl2chan);
902 chan_user_modes = Channel_UserModes(chan, c);
903 strlcat(flags, who_flags_qualifier(chan_user_modes), sizeof(flags));
907 if (!write_whoreply(Client, c, channelname, flags))
912 channelname = Req->argv[0];
916 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), channelname);
921 * Handler for the IRC "WHOIS" command.
923 * See RFC 2812, 3.6.2 "Whois query".
925 * @param Client The client from which this command has been received.
926 * @param Req Request structure with prefix and all parameters.
927 * @return CONNECTED or DISCONNECTED.
930 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
932 CLIENT *from, *target, *c;
933 char str[LINE_LEN + 1];
937 assert( Client != NULL );
938 assert( Req != NULL );
940 /* Bad number of parameters? */
941 if (Req->argc < 1 || Req->argc > 2)
942 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
943 Client_ID(Client), Req->command);
946 c = Client_Search(Req->argv[Req->argc - 1]);
947 if (!c || (Client_Type(c) != CLIENT_USER
948 && Client_Type(c) != CLIENT_SERVICE))
949 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
951 Req->argv[Req->argc - 1]);
953 /* Search sender of the WHOIS */
954 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
956 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
958 /* Forward to other server? */
961 /* Search target server (can be specified as nick of that server!) */
962 target = Client_Search( Req->argv[0] );
963 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
965 else target = Client_ThisServer( );
967 assert( target != NULL );
969 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] );
971 /* Nick, user, hostname and client info */
972 if (!IRC_WriteStrClient(from, RPL_WHOISUSER_MSG, Client_ID(from),
973 Client_ID(c), Client_User(c),
974 Client_HostnameCloaked(c), Client_Info(c)))
978 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;
981 snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
982 cl2chan = Channel_FirstChannelOf( c );
985 chan = Channel_GetChannel( cl2chan );
986 assert( chan != NULL );
989 cl2chan = Channel_NextChannelOf( c, cl2chan );
991 /* Secret channel? */
992 if (strchr(Channel_Modes(chan), 's')
993 && !Channel_IsMemberOf(chan, Client))
996 /* Local channel and request is not from a user? */
997 if (Client_Type(Client) == CLIENT_SERVER
998 && Channel_IsLocal(chan))
1001 /* Concatenate channel names */
1002 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
1003 if( strchr( Channel_UserModes( chan, c ), 'o' )) strlcat( str, "@", sizeof( str ));
1004 else if( strchr( Channel_UserModes( chan, c ), 'v' )) strlcat( str, "+", sizeof( str ));
1005 strlcat( str, Channel_Name( chan ), sizeof( str ));
1007 if( strlen( str ) > ( LINE_LEN - CHANNEL_NAME_LEN - 4 ))
1009 /* Line becomes too long: send it! */
1010 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1011 snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
1014 if( str[strlen( str ) - 1] != ':')
1016 /* There is data left to send: */
1017 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1021 if( Client_HasMode( c, 'o' ))
1023 if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
1026 /* Connected using SSL? */
1027 if (Conn_UsesSSL(Client_Conn(c))) {
1028 if (!IRC_WriteStrClient
1029 (from, RPL_WHOISSSL_MSG, Client_ID(from), Client_ID(c)))
1030 return DISCONNECTED;
1033 /* Idle and signon time (local clients only!) */
1034 if (Client_Conn(c) > NONE ) {
1035 if (! IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
1036 Client_ID(from), Client_ID(c),
1037 (unsigned long)Conn_GetIdle(Client_Conn(c)),
1038 (unsigned long)Conn_GetSignon(Client_Conn(c))))
1039 return DISCONNECTED;
1043 if( Client_HasMode( c, 'a' ))
1045 if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( c ), Client_Away( c ))) return DISCONNECTED;
1049 return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
1054 WHOWAS_EntryWrite(CLIENT *prefix, WHOWAS *entry)
1058 (void)strftime(t_str, sizeof(t_str), "%a %b %d %H:%M:%S %Y",
1059 localtime(&entry->time));
1061 if (!IRC_WriteStrClient(prefix, RPL_WHOWASUSER_MSG, Client_ID(prefix),
1062 entry->id, entry->user, entry->host, entry->info))
1063 return DISCONNECTED;
1065 return IRC_WriteStrClient(prefix, RPL_WHOISSERVER_MSG, Client_ID(prefix),
1066 entry->id, entry->server, t_str);
1070 * IRC "WHOWAS" function.
1071 * This function implements the IRC command "WHOWHAS". It handles local
1072 * requests and request that should be forwarded to other servers.
1075 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1077 CLIENT *target, *prefix;
1079 char tok_buf[COMMAND_LEN];
1080 int max, last, count, i, nc;
1083 assert( Client != NULL );
1084 assert( Req != NULL );
1086 /* Wrong number of parameters? */
1088 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1089 Client_ID(Client), Req->command);
1091 return IRC_WriteStrClient(Client, ERR_NONICKNAMEGIVEN_MSG, Client_ID(Client));
1095 target = Client_Search(Req->argv[2]);
1097 target = Client_ThisServer();
1100 if (Client_Type(Client) == CLIENT_SERVER)
1101 prefix = Client_Search(Req->prefix);
1106 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1107 Client_ID(Client), Req->prefix);
1109 /* Forward to other server? */
1110 if (target != Client_ThisServer()) {
1111 if (!target || (Client_Type(target) != CLIENT_SERVER))
1112 return IRC_WriteStrClient(prefix, ERR_NOSUCHSERVER_MSG,
1113 Client_ID(prefix), Req->argv[2]);
1116 IRC_WriteStrClientPrefix( target, prefix, "WHOWAS %s %s %s",
1117 Req->argv[0], Req->argv[1],
1122 whowas = Client_GetWhowas( );
1123 last = Client_GetLastWhowasIndex( );
1127 max = DEFAULT_WHOWAS;
1128 if (Req->argc > 1) {
1129 max = atoi(Req->argv[1]);
1135 * Break up the nick argument into a list of nicks, if applicable
1136 * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1138 strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1139 nick = strtok(tok_buf, ",");
1141 for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1145 if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1146 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1147 return DISCONNECTED;
1151 /* previous entry */
1154 /* "underflow", wrap around */
1158 if (nc && count >= max)
1160 } while (i != last);
1162 if (nc == 0 && !IRC_WriteStrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1163 Client_ID(prefix), nick))
1164 return DISCONNECTED;
1166 return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG, Client_ID(prefix), Req->argv[0]);
1171 IRC_Send_LUSERS( CLIENT *Client )
1178 assert( Client != NULL );
1180 /* Users, services and serevers in the network */
1181 if( ! IRC_WriteStrClient( Client, RPL_LUSERCLIENT_MSG, Client_ID( Client ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
1183 /* Number of IRC operators */
1184 cnt = Client_OperCount( );
1187 if( ! IRC_WriteStrClient( Client, RPL_LUSEROP_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
1190 /* Unknown connections */
1191 cnt = Client_UnknownCount( );
1194 if( ! IRC_WriteStrClient( Client, RPL_LUSERUNKNOWN_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
1197 /* Number of created channels */
1198 if( ! IRC_WriteStrClient( Client, RPL_LUSERCHANNELS_MSG, Client_ID( Client ), Channel_Count( ))) return DISCONNECTED;
1200 /* Number of local users, services and servers */
1201 if( ! IRC_WriteStrClient( Client, RPL_LUSERME_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
1204 /* Maximum number of local users */
1205 cnt = Client_MyUserCount();
1206 max = Client_MyMaxUserCount();
1207 if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1208 cnt, max, cnt, max))
1209 return DISCONNECTED;
1210 /* Maximum number of users in the network */
1211 cnt = Client_UserCount();
1212 max = Client_MaxUserCount();
1213 if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1214 cnt, max, cnt, max))
1215 return DISCONNECTED;
1216 /* Connection counters */
1217 if (! IRC_WriteStrClient(Client, RPL_STATSCONN_MSG, Client_ID(Client),
1218 Conn_CountMax(), Conn_CountAccepted()))
1219 return DISCONNECTED;
1223 } /* IRC_Send_LUSERS */
1227 Show_MOTD_Start(CLIENT *Client)
1229 return IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG,
1230 Client_ID( Client ), Client_ID( Client_ThisServer( )));
1234 Show_MOTD_Sendline(CLIENT *Client, const char *msg)
1236 return IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID( Client ), msg);
1240 Show_MOTD_End(CLIENT *Client)
1242 return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
1246 static bool Show_MOTD_SSLInfo(CLIENT *Client)
1249 char buf[COMMAND_LEN] = "Connected using Cipher ";
1251 if (!Conn_GetCipherInfo(Client_Conn(Client), buf + 23, sizeof buf - 23))
1254 if (!Show_MOTD_Sendline(Client, buf))
1261 Show_MOTD_SSLInfo(UNUSED CLIENT *c)
1266 IRC_Show_MOTD( CLIENT *Client )
1269 size_t len_tot, len_str;
1271 assert( Client != NULL );
1273 len_tot = array_bytes(&Conf_Motd);
1274 if (len_tot == 0 && !Conn_UsesSSL(Client_Conn(Client)))
1275 return IRC_WriteStrClient(Client, ERR_NOMOTD_MSG, Client_ID(Client));
1277 if (!Show_MOTD_Start(Client))
1278 return DISCONNECTED;
1280 line = array_start(&Conf_Motd);
1281 while (len_tot > 0) {
1282 len_str = strlen(line) + 1;
1284 assert(len_tot >= len_str);
1287 if (!Show_MOTD_Sendline(Client, line))
1288 return DISCONNECTED;
1292 if (!Show_MOTD_SSLInfo(Client))
1293 return DISCONNECTED;
1294 return Show_MOTD_End(Client);
1295 } /* IRC_Show_MOTD */
1299 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
1301 bool is_visible, is_member;
1302 char str[LINE_LEN + 1];
1306 assert( Client != NULL );
1307 assert( Chan != NULL );
1309 if( Channel_IsMemberOf( Chan, Client )) is_member = true;
1310 else is_member = false;
1312 /* Secret channel? */
1313 if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
1315 /* Alle Mitglieder suchen */
1316 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1317 cl2chan = Channel_FirstMember( Chan );
1320 cl = Channel_GetClient( cl2chan );
1322 if( strchr( Client_Modes( cl ), 'i' )) is_visible = false;
1323 else is_visible = true;
1325 if( is_member || is_visible )
1327 /* Nick anhaengen */
1328 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
1329 if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
1330 else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
1331 strlcat( str, Client_ID( cl ), sizeof( str ));
1333 if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
1335 /* Zeile wird zu lang: senden! */
1336 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1337 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1341 /* naechstes Mitglied suchen */
1342 cl2chan = Channel_NextMember( Chan, cl2chan );
1344 if( str[strlen( str ) - 1] != ':')
1346 /* Es sind noch Daten da, die gesendet werden muessen */
1347 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1351 } /* IRC_Send_NAMES */
1355 * Send the ISUPPORT numeric (005).
1356 * This numeric indicates the features that are supported by this server.
1357 * See <http://www.irc.org/tech_docs/005.html> for details.
1360 IRC_Send_ISUPPORT(CLIENT * Client)
1362 if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1364 return DISCONNECTED;
1365 return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1366 CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1367 COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1369 } /* IRC_Send_ISUPPORT */