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.
26 #include "conn-func.h"
36 #include "irc-write.h"
43 IRC_ADMIN(CLIENT *Client, REQUEST *Req )
45 CLIENT *target, *prefix;
47 assert( Client != NULL );
48 assert( Req != NULL );
50 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
53 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
54 else target = Client_ThisServer( );
57 if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
59 if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
61 /* forwad message to another server? */
62 if( target != Client_ThisServer( ))
64 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
67 IRC_WriteStrClientPrefix( target, prefix, "ADMIN %s", Req->argv[0] );
71 /* mit Versionsinfo antworten */
72 if( ! IRC_WriteStrClient( Client, RPL_ADMINME_MSG, Client_ID( prefix ), Conf_ServerName )) return DISCONNECTED;
73 if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC1_MSG, Client_ID( prefix ), Conf_ServerAdmin1 )) return DISCONNECTED;
74 if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC2_MSG, Client_ID( prefix ), Conf_ServerAdmin2 )) return DISCONNECTED;
75 if( ! IRC_WriteStrClient( Client, RPL_ADMINEMAIL_MSG, Client_ID( prefix ), Conf_ServerAdminMail )) return DISCONNECTED;
77 IRC_SetPenalty( Client, 1 );
83 * Handler for the IRC command "INFO".
84 * See RFC 2812 section 3.4.10.
87 IRC_INFO(CLIENT * Client, REQUEST * Req)
89 CLIENT *target, *prefix;
92 assert(Client != NULL);
95 /* Wrong number of parameters? */
97 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
98 Client_ID(Client), Req->command);
100 /* Determine prefix */
101 if (Client_Type(Client) == CLIENT_SERVER)
102 prefix = Client_Search(Req->prefix);
106 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
107 Client_ID(Client), Req->prefix);
109 /* Look for a target */
111 target = Client_Search(Req->argv[0]);
113 target = Client_ThisServer();
115 /* Make sure that the target is a server */
116 if (target && Client_Type(target) != CLIENT_SERVER)
117 target = Client_Introducer(target);
120 return IRC_WriteStrClient(prefix, ERR_NOSUCHSERVER_MSG,
121 Client_ID(prefix), Req->argv[0]);
123 /* Pass on to another server? */
124 if (target != Client_ThisServer()) {
125 IRC_WriteStrClientPrefix(target, prefix, "INFO %s",
130 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix),
134 #if defined(__DATE__) && defined(__TIME__)
135 snprintf(msg, sizeof(msg), "Birth Date: %s at %s", __DATE__, __TIME__);
136 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
140 strlcpy(msg, "On-line since ", 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 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
201 /* Server-Mask ermitteln */
202 if( Req->argc > 0 ) mask = Req->argv[Req->argc - 1];
205 /* Absender ermitteln */
206 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
208 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
210 /* An anderen Server forwarden? */
213 target = Client_Search( Req->argv[0] );
214 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
215 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LINKS %s %s", Req->argv[0], Req->argv[1] );
218 /* Wer ist der Absender? */
219 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
220 else target = Client;
221 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
226 if( Client_Type( c ) == CLIENT_SERVER )
228 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;
230 c = Client_Next( c );
233 IRC_SetPenalty( target, 1 );
234 return IRC_WriteStrClient( target, RPL_ENDOFLINKS_MSG, Client_ID( target ), mask );
239 IRC_LUSERS( CLIENT *Client, REQUEST *Req )
241 CLIENT *target, *from;
243 assert( Client != NULL );
244 assert( Req != NULL );
246 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
248 /* Absender ermitteln */
249 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
251 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
253 /* An anderen Server forwarden? */
256 target = Client_Search( Req->argv[1] );
257 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
258 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LUSERS %s %s", Req->argv[0], Req->argv[1] );
261 /* Wer ist der Absender? */
262 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
263 else target = Client;
264 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
266 IRC_Send_LUSERS( target );
268 IRC_SetPenalty( target, 1 );
274 * Handler for the IRC command "SERVLIST".
275 * List registered services, see RFC 2811, section 3.5.1: the syntax is
276 * "SERVLIST [<mask> [<type>]]".
279 IRC_SERVLIST(CLIENT *Client, REQUEST *Req)
283 assert(Client != NULL);
287 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
288 Client_ID(Client), Req->command);
290 if (Req->argc < 2 || strcmp(Req->argv[1], "0") == 0) {
291 for (c = Client_First(); c!= NULL; c = Client_Next(c)) {
292 if (Client_Type(c) != CLIENT_SERVICE)
294 if (Req->argc > 0 && !MatchCaseInsensitive(Req->argv[0],
297 if (!IRC_WriteStrClient(Client, RPL_SERVLIST_MSG,
298 Client_ID(Client), Client_Mask(c),
299 Client_Mask(Client_Introducer(c)), "*",
300 0, Client_Hops(c), Client_Info(c)))
305 return IRC_WriteStrClient(Client, RPL_SERVLISTEND_MSG, Client_ID(Client),
306 Req->argc > 0 ? Req->argv[0] : "*",
307 Req->argc > 1 ? Req->argv[1] : "0");
312 IRC_MOTD( CLIENT *Client, REQUEST *Req )
314 CLIENT *from, *target;
316 assert( Client != NULL );
317 assert( Req != NULL );
319 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
321 /* From aus Prefix ermitteln */
322 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
324 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
329 target = Client_Search( Req->argv[0] );
330 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
332 if( target != Client_ThisServer( ))
334 /* Ok, anderer Server ist das Ziel: forwarden */
335 return IRC_WriteStrClientPrefix( target, from, "MOTD %s", Req->argv[0] );
339 IRC_SetPenalty( from, 3 );
340 return IRC_Show_MOTD( from );
345 IRC_NAMES( CLIENT *Client, REQUEST *Req )
347 char rpl[COMMAND_LEN], *ptr;
348 CLIENT *target, *from, *c;
351 assert( Client != NULL );
352 assert( Req != NULL );
354 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
356 /* use prefix to determine "From" */
357 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
359 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
363 /* forward to another server? */
364 target = Client_Search( Req->argv[1] );
365 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
367 if( target != Client_ThisServer( )) {
368 /* target is another server, forward */
369 return IRC_WriteStrClientPrefix( target, from, "NAMES %s :%s", Req->argv[0], Req->argv[1] );
375 /* bestimmte Channels durchgehen */
376 ptr = strtok( Req->argv[0], "," );
379 chan = Channel_Search( ptr );
383 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
385 if( ! IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), ptr )) return DISCONNECTED;
387 /* get next channel name */
388 ptr = strtok( NULL, "," );
393 chan = Channel_First( );
396 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
398 chan = Channel_Next( chan );
401 /* Now print all clients which are not in any channel */
403 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
406 if(( Client_Type( c ) == CLIENT_USER ) && ( Channel_FirstChannelOf( c ) == NULL ) && ( ! strchr( Client_Modes( c ), 'i' )))
408 /* its a user, concatenate ... */
409 if( rpl[strlen( rpl ) - 1] != ':' ) strlcat( rpl, " ", sizeof( rpl ));
410 strlcat( rpl, Client_ID( c ), sizeof( rpl ));
412 if( strlen( rpl ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
414 /* Line is gwoing too long, send now */
415 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
416 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
420 c = Client_Next( c );
422 if( rpl[strlen( rpl ) - 1] != ':')
424 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
427 IRC_SetPenalty( from, 1 );
428 return IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), "*" );
433 t_diff(time_t *t, const time_t d)
441 return (unsigned int)diff;
446 uptime_days(time_t *now)
448 return t_diff(now, 60 * 60 * 24);
453 uptime_hrs(time_t *now)
455 return t_diff(now, 60 * 60);
460 uptime_mins(time_t *now)
462 return t_diff(now, 60);
467 * Handler for the IRC command "STATS".
468 * See RFC 2812 section 3.4.4.
471 IRC_STATS( CLIENT *Client, REQUEST *Req )
473 CLIENT *from, *target, *cl;
478 unsigned int days, hrs, mins;
480 assert(Client != NULL);
484 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
485 Client_ID(Client), Req->command);
487 /* use prefix to determine "From" */
488 if (Client_Type(Client) == CLIENT_SERVER)
489 from = Client_Search(Req->prefix);
494 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
495 Client_ID(Client), Req->prefix);
497 if (Req->argc == 2) {
498 /* forward to another server? */
499 target = Client_Search(Req->argv[1]);
500 if ((!target) || (Client_Type(target) != CLIENT_SERVER))
501 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
502 Client_ID(from), Req->argv[1]);
504 if (target != Client_ThisServer()) {
505 /* forward to another server */
506 return IRC_WriteStrClientPrefix(target, from,
507 "STATS %s %s", Req->argv[0], Req->argv[1]);
512 query = Req->argv[0][0] ? Req->argv[0][0] : '*';
517 case 'l': /* Link status (servers and own link) */
519 time_now = time(NULL);
520 for (con = Conn_First(); con != NONE; con = Conn_Next(con)) {
521 cl = Conn_GetClient(con);
524 if ((Client_Type(cl) == CLIENT_SERVER)
526 /* Server link or our own connection */
528 if (Conn_Options(con) & CONN_ZIP) {
529 if (!IRC_WriteStrClient
530 (from, RPL_STATSLINKINFOZIP_MSG,
531 Client_ID(from), Client_Mask(cl),
532 Conn_SendQ(con), Conn_SendMsg(con),
538 (long)(time_now - Conn_StartTime(con))))
543 if (!IRC_WriteStrClient
544 (from, RPL_STATSLINKINFO_MSG,
545 Client_ID(from), Client_Mask(cl),
546 Conn_SendQ(con), Conn_SendMsg(con),
547 Conn_SendBytes(con), Conn_RecvMsg(con),
549 (long)(time_now - Conn_StartTime(con))))
554 case 'm': /* IRC command status (usage count) */
556 cmd = Parse_GetCommandStruct();
557 for (; cmd->name; cmd++) {
558 if (cmd->lcount == 0 && cmd->rcount == 0)
560 if (!IRC_WriteStrClient
561 (from, RPL_STATSCOMMANDS_MSG, Client_ID(from),
562 cmd->name, cmd->lcount, cmd->bytes, cmd->rcount))
566 case 'u': /* Server uptime */
568 time_now = time(NULL) - NGIRCd_Start;
569 days = uptime_days(&time_now);
570 hrs = uptime_hrs(&time_now);
571 mins = uptime_mins(&time_now);
572 if (!IRC_WriteStrClient(from, RPL_STATSUPTIME, Client_ID(from),
573 days, hrs, mins, (unsigned int)time_now))
578 IRC_SetPenalty(from, 2);
579 return IRC_WriteStrClient(from, RPL_ENDOFSTATS_MSG,
580 Client_ID(from), query);
585 * Handler for the IRC command "SUMMON".
586 * See RFC 2812 section 4.5. ngIRCd doesn't implement this functionality and
587 * therefore answers with ERR_SUMMONDISABLED.
590 IRC_SUMMON(CLIENT * Client, REQUEST * Req)
592 return IRC_WriteStrClient(Client, ERR_SUMMONDISABLED_MSG,
593 Client_ID(Client), Req->command);
598 IRC_TIME( CLIENT *Client, REQUEST *Req )
600 CLIENT *from, *target;
604 assert( Client != NULL );
605 assert( Req != NULL );
607 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
609 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
611 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
615 target = Client_Search( Req->argv[0] );
616 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
618 if( target != Client_ThisServer( ))
620 return IRC_WriteStrClientPrefix( target, from, "TIME %s", Req->argv[0] );
625 (void)strftime( t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime( &t ));
626 return IRC_WriteStrClient( from, RPL_TIME_MSG, Client_ID( from ), Client_ID( Client_ThisServer( )), t_str );
631 * Handler for the IRC command "USERHOST".
632 * See RFC 2812 section 4.8.
635 IRC_USERHOST(CLIENT *Client, REQUEST *Req)
637 char rpl[COMMAND_LEN];
641 assert(Client != NULL);
645 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
646 Client_ID(Client), Req->command);
653 strlcpy(rpl, RPL_USERHOST_MSG, sizeof rpl);
654 for (i = 0; i < max; i++) {
655 c = Client_Search(Req->argv[i]);
656 if (c && (Client_Type(c) == CLIENT_USER)) {
657 /* This Nick is "online" */
658 strlcat(rpl, Client_ID(c), sizeof(rpl));
659 if (Client_HasMode(c, 'o'))
660 strlcat(rpl, "*", sizeof(rpl));
661 strlcat(rpl, "=", sizeof(rpl));
662 if (Client_HasMode(c, 'a'))
663 strlcat(rpl, "-", sizeof(rpl));
665 strlcat(rpl, "+", sizeof(rpl));
666 strlcat(rpl, Client_User(c), sizeof(rpl));
667 strlcat(rpl, "@", sizeof(rpl));
668 strlcat(rpl, Client_HostnameCloaked(c), sizeof(rpl));
669 strlcat(rpl, " ", sizeof(rpl));
672 ngt_TrimLastChr(rpl, ' ');
674 return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
679 * Handler for the IRC command "USERS".
680 * See RFC 2812 section 4.6. As suggested there the command is disabled.
683 IRC_USERS(CLIENT * Client, REQUEST * Req)
685 return IRC_WriteStrClient(Client, ERR_USERSDISABLED_MSG,
686 Client_ID(Client), Req->command);
691 IRC_VERSION( CLIENT *Client, REQUEST *Req )
693 CLIENT *target, *prefix;
695 assert( Client != NULL );
696 assert( Req != NULL );
698 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
701 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
702 else target = Client_ThisServer( );
704 /* Prefix ermitteln */
705 if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
706 else prefix = Client;
707 if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
709 /* An anderen Server weiterleiten? */
710 if( target != Client_ThisServer( ))
712 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
715 IRC_WriteStrClientPrefix( target, prefix, "VERSION %s", Req->argv[0] );
719 /* send version information */
720 IRC_SetPenalty(Client, 1);
721 return IRC_WriteStrClient(Client, RPL_VERSION_MSG, Client_ID(prefix),
722 PACKAGE_NAME, PACKAGE_VERSION,
723 NGIRCd_DebugLevel, Conf_ServerName,
724 NGIRCd_VersionAddition);
729 write_whoreply(CLIENT *Client, CLIENT *c, const char *channelname, const char *flags)
731 return IRC_WriteStrClient(Client, RPL_WHOREPLY_MSG, Client_ID(Client),
732 channelname, Client_User(c),
733 Client_HostnameCloaked(c),
734 Client_ID(Client_Introducer(c)), Client_ID(c),
735 flags, Client_Hops(c), Client_Info(c));
740 who_flags_status(const char *client_modes)
742 if (strchr(client_modes, 'a'))
743 return "G"; /* away */
749 who_flags_qualifier(const char *chan_user_modes)
751 if (strchr(chan_user_modes, 'o'))
753 else if (strchr(chan_user_modes, 'v'))
760 IRC_Send_WHO(CLIENT *Client, CHANNEL *Chan, bool OnlyOps)
762 bool is_visible, is_member, is_ircop;
764 const char *client_modes;
765 const char *chan_user_modes;
769 assert( Client != NULL );
770 assert( Chan != NULL );
772 is_member = Channel_IsMemberOf(Chan, Client);
774 /* Secret channel? */
775 if (!is_member && strchr(Channel_Modes(Chan), 's'))
776 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), Channel_Name(Chan));
778 cl2chan = Channel_FirstMember(Chan);
779 for (; cl2chan ; cl2chan = Channel_NextMember(Chan, cl2chan)) {
780 c = Channel_GetClient(cl2chan);
782 client_modes = Client_Modes(c);
783 is_ircop = strchr(client_modes, 'o') != NULL;
784 if (OnlyOps && !is_ircop)
787 is_visible = strchr(client_modes, 'i') == NULL;
788 if (is_member || is_visible) {
789 strcpy(flags, who_flags_status(client_modes));
791 strlcat(flags, "*", sizeof(flags));
793 chan_user_modes = Channel_UserModes(Chan, c);
794 strlcat(flags, who_flags_qualifier(chan_user_modes), sizeof(flags));
796 if (!write_whoreply(Client, c, Channel_Name(Chan), flags))
800 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), Channel_Name(Chan));
805 IRC_WHO( CLIENT *Client, REQUEST *Req )
807 bool only_ops, have_arg, client_match;
808 const char *channelname, *client_modes, *chan_user_modes;
809 char pattern[COMMAND_LEN];
815 assert( Client != NULL );
816 assert( Req != NULL );
819 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
824 if (Req->argc == 2) {
825 if (strcmp(Req->argv[1], "o") == 0)
828 else return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
832 IRC_SetPenalty(Client, 1);
833 if (Req->argc >= 1) { /* Channel or Mask. */
834 chan = Channel_Search(Req->argv[0]);
836 return IRC_Send_WHO(Client, chan, only_ops);
837 if (strcmp(Req->argv[0], "0") != 0) { /* RFC stupidity, same as no arguments */
839 strlcpy(pattern, Req->argv[0], sizeof(pattern));
840 ngt_LowerStr(pattern);
841 IRC_SetPenalty(Client, 3);
845 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
846 if (Client_Type(c) != CLIENT_USER)
850 * In the absence of the parameter, all visible (users who aren't
851 * invisible (user mode +i) and who don't have a common channel
852 * with the requesting client) are listed.
854 * The same result can be achieved by using a [sic] of "0"
855 * or any wildcard which will end up matching every visible user.
857 * The [sic] passed to WHO is matched against users' host, server, real name and
858 * nickname if the channel cannot be found.
860 client_modes = Client_Modes(c);
861 if (strchr(client_modes, 'i'))
864 if (only_ops && !strchr(client_modes, 'o'))
867 if (have_arg) { /* match pattern against user host/server/name/nick */
868 client_match = MatchCaseInsensitive(pattern, Client_Hostname(c)); /* user's host */
870 client_match = MatchCaseInsensitive(pattern, Client_ID(Client_Introducer(c))); /* server */
872 client_match = Match(Req->argv[0], Client_Info(c)); /* realname */
874 client_match = MatchCaseInsensitive(pattern, Client_ID(c)); /* nick name */
876 if (!client_match) /* This isn't the client you're looking for */
880 strcpy(flags, who_flags_status(client_modes));
882 if (strchr(client_modes, 'o')) /* this client is an operator */
883 strlcat(flags, "*", sizeof(flags));
885 /* Search suitable channel */
886 cl2chan = Channel_FirstChannelOf(c);
888 cn = Channel_GetChannel(cl2chan);
889 if (Channel_IsMemberOf(cn, Client) ||
890 !strchr(Channel_Modes(cn), 's'))
892 channelname = Channel_Name(cn);
895 cl2chan = Channel_NextChannelOf(c, cl2chan);
898 chan = Channel_GetChannel(cl2chan);
899 chan_user_modes = Channel_UserModes(chan, c);
900 strlcat(flags, who_flags_qualifier(chan_user_modes), sizeof(flags));
904 if (!write_whoreply(Client, c, channelname, flags))
909 channelname = Req->argv[0];
913 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), channelname);
918 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
920 CLIENT *from, *target, *c;
921 char str[LINE_LEN + 1];
925 assert( Client != NULL );
926 assert( Req != NULL );
928 /* Bad number of parameters? */
929 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
932 c = Client_Search( Req->argv[Req->argc - 1] );
933 if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[Req->argc - 1] );
935 /* Search sender of the WHOIS */
936 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
938 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
940 /* Forward to other server? */
943 /* Search target server (can be specified as nick of that server!) */
944 target = Client_Search( Req->argv[0] );
945 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
947 else target = Client_ThisServer( );
949 assert( target != NULL );
951 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] );
953 /* Nick, user, hostname and client info */
954 if (!IRC_WriteStrClient(from, RPL_WHOISUSER_MSG, Client_ID(from),
955 Client_ID(c), Client_User(c),
956 Client_HostnameCloaked(c), Client_Info(c)))
960 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;
963 snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
964 cl2chan = Channel_FirstChannelOf( c );
967 chan = Channel_GetChannel( cl2chan );
968 assert( chan != NULL );
971 cl2chan = Channel_NextChannelOf( c, cl2chan );
973 /* Secret channel? */
974 if (strchr(Channel_Modes(chan), 's')
975 && !Channel_IsMemberOf(chan, Client))
978 /* Local channel and request is not from a user? */
979 if (Client_Type(Client) == CLIENT_SERVER
980 && Channel_IsLocal(chan))
983 /* Concatenate channel names */
984 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
985 if( strchr( Channel_UserModes( chan, c ), 'o' )) strlcat( str, "@", sizeof( str ));
986 else if( strchr( Channel_UserModes( chan, c ), 'v' )) strlcat( str, "+", sizeof( str ));
987 strlcat( str, Channel_Name( chan ), sizeof( str ));
989 if( strlen( str ) > ( LINE_LEN - CHANNEL_NAME_LEN - 4 ))
991 /* Line becomes too long: send it! */
992 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
993 snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
996 if( str[strlen( str ) - 1] != ':')
998 /* There is data left to send: */
999 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1003 if( Client_HasMode( c, 'o' ))
1005 if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
1008 /* Connected using SSL? */
1009 if (Conn_UsesSSL(Client_Conn(c))) {
1010 if (!IRC_WriteStrClient
1011 (from, RPL_WHOISSSL_MSG, Client_ID(from), Client_ID(c)))
1012 return DISCONNECTED;
1015 /* Idle and signon time (local clients only!) */
1016 if (Client_Conn(c) > NONE ) {
1017 if (! IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
1018 Client_ID(from), Client_ID(c),
1019 (unsigned long)Conn_GetIdle(Client_Conn(c)),
1020 (unsigned long)Conn_GetSignon(Client_Conn(c))))
1021 return DISCONNECTED;
1025 if( Client_HasMode( c, 'a' ))
1027 if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( c ), Client_Away( c ))) return DISCONNECTED;
1031 return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
1036 WHOWAS_EntryWrite(CLIENT *prefix, WHOWAS *entry)
1040 (void)strftime(t_str, sizeof(t_str), "%a %b %d %H:%M:%S %Y",
1041 localtime(&entry->time));
1043 if (!IRC_WriteStrClient(prefix, RPL_WHOWASUSER_MSG, Client_ID(prefix),
1044 entry->id, entry->user, entry->host, entry->info))
1045 return DISCONNECTED;
1047 return IRC_WriteStrClient(prefix, RPL_WHOISSERVER_MSG, Client_ID(prefix),
1048 entry->id, entry->server, t_str);
1052 * IRC "WHOWAS" function.
1053 * This function implements the IRC command "WHOWHAS". It handles local
1054 * requests and request that should be forwarded to other servers.
1057 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1059 CLIENT *target, *prefix;
1061 char tok_buf[COMMAND_LEN];
1062 int max, last, count, i, nc;
1065 assert( Client != NULL );
1066 assert( Req != NULL );
1068 /* Wrong number of parameters? */
1070 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1071 Client_ID(Client), Req->command);
1073 return IRC_WriteStrClient(Client, ERR_NONICKNAMEGIVEN_MSG, Client_ID(Client));
1077 target = Client_Search(Req->argv[2]);
1079 target = Client_ThisServer();
1082 if (Client_Type(Client) == CLIENT_SERVER)
1083 prefix = Client_Search(Req->prefix);
1088 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1089 Client_ID(Client), Req->prefix);
1091 /* Forward to other server? */
1092 if (target != Client_ThisServer()) {
1093 if (!target || (Client_Type(target) != CLIENT_SERVER))
1094 return IRC_WriteStrClient(prefix, ERR_NOSUCHSERVER_MSG,
1095 Client_ID(prefix), Req->argv[2]);
1098 IRC_WriteStrClientPrefix( target, prefix, "WHOWAS %s %s %s",
1099 Req->argv[0], Req->argv[1],
1104 whowas = Client_GetWhowas( );
1105 last = Client_GetLastWhowasIndex( );
1109 max = DEFAULT_WHOWAS;
1110 if (Req->argc > 1) {
1111 max = atoi(Req->argv[1]);
1117 * Break up the nick argument into a list of nicks, if applicable
1118 * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1120 strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1121 nick = strtok(tok_buf, ",");
1123 for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1127 if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1128 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1129 return DISCONNECTED;
1133 /* previous entry */
1136 /* "underflow", wrap around */
1140 if (nc && count >= max)
1142 } while (i != last);
1144 if (nc == 0 && !IRC_WriteStrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1145 Client_ID(prefix), nick))
1146 return DISCONNECTED;
1148 return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG, Client_ID(prefix), Req->argv[0]);
1153 IRC_Send_LUSERS( CLIENT *Client )
1160 assert( Client != NULL );
1162 /* Users, services and serevers in the network */
1163 if( ! IRC_WriteStrClient( Client, RPL_LUSERCLIENT_MSG, Client_ID( Client ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
1165 /* Number of IRC operators */
1166 cnt = Client_OperCount( );
1169 if( ! IRC_WriteStrClient( Client, RPL_LUSEROP_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
1172 /* Unknown connections */
1173 cnt = Client_UnknownCount( );
1176 if( ! IRC_WriteStrClient( Client, RPL_LUSERUNKNOWN_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
1179 /* Number of created channels */
1180 if( ! IRC_WriteStrClient( Client, RPL_LUSERCHANNELS_MSG, Client_ID( Client ), Channel_Count( ))) return DISCONNECTED;
1182 /* Number of local users, services and servers */
1183 if( ! IRC_WriteStrClient( Client, RPL_LUSERME_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
1186 /* Maximum number of local users */
1187 cnt = Client_MyUserCount();
1188 max = Client_MyMaxUserCount();
1189 if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1190 cnt, max, cnt, max))
1191 return DISCONNECTED;
1192 /* Maximum number of users in the network */
1193 cnt = Client_UserCount();
1194 max = Client_MaxUserCount();
1195 if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1196 cnt, max, cnt, max))
1197 return DISCONNECTED;
1198 /* Connection counters */
1199 if (! IRC_WriteStrClient(Client, RPL_STATSCONN_MSG, Client_ID(Client),
1200 Conn_CountMax(), Conn_CountAccepted()))
1201 return DISCONNECTED;
1205 } /* IRC_Send_LUSERS */
1209 Show_MOTD_Start(CLIENT *Client)
1211 return IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG,
1212 Client_ID( Client ), Client_ID( Client_ThisServer( )));
1216 Show_MOTD_Sendline(CLIENT *Client, const char *msg)
1218 return IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID( Client ), msg);
1222 Show_MOTD_End(CLIENT *Client)
1224 return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
1228 static bool Show_MOTD_SSLInfo(CLIENT *Client)
1231 char buf[COMMAND_LEN] = "Connected using Cipher ";
1233 if (!Conn_GetCipherInfo(Client_Conn(Client), buf + 23, sizeof buf - 23))
1236 if (!Show_MOTD_Sendline(Client, buf))
1242 static inline bool Show_MOTD_SSLInfo(UNUSED CLIENT *c) { return true; }
1246 IRC_Show_MOTD( CLIENT *Client )
1249 size_t len_tot, len_str;
1251 assert( Client != NULL );
1253 len_tot = array_bytes(&Conf_Motd);
1254 if (len_tot == 0 && !Conn_UsesSSL(Client_Conn(Client)))
1255 return IRC_WriteStrClient(Client, ERR_NOMOTD_MSG, Client_ID(Client));
1257 if (!Show_MOTD_Start(Client))
1258 return DISCONNECTED;
1260 line = array_start(&Conf_Motd);
1261 while (len_tot > 0) {
1262 len_str = strlen(line) + 1;
1264 assert(len_tot >= len_str);
1267 if (!Show_MOTD_Sendline(Client, line))
1268 return DISCONNECTED;
1272 if (!Show_MOTD_SSLInfo(Client))
1273 return DISCONNECTED;
1274 return Show_MOTD_End(Client);
1275 } /* IRC_Show_MOTD */
1279 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
1281 bool is_visible, is_member;
1282 char str[LINE_LEN + 1];
1286 assert( Client != NULL );
1287 assert( Chan != NULL );
1289 if( Channel_IsMemberOf( Chan, Client )) is_member = true;
1290 else is_member = false;
1292 /* Secret channel? */
1293 if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
1295 /* Alle Mitglieder suchen */
1296 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1297 cl2chan = Channel_FirstMember( Chan );
1300 cl = Channel_GetClient( cl2chan );
1302 if( strchr( Client_Modes( cl ), 'i' )) is_visible = false;
1303 else is_visible = true;
1305 if( is_member || is_visible )
1307 /* Nick anhaengen */
1308 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
1309 if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
1310 else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
1311 strlcat( str, Client_ID( cl ), sizeof( str ));
1313 if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
1315 /* Zeile wird zu lang: senden! */
1316 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1317 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1321 /* naechstes Mitglied suchen */
1322 cl2chan = Channel_NextMember( Chan, cl2chan );
1324 if( str[strlen( str ) - 1] != ':')
1326 /* Es sind noch Daten da, die gesendet werden muessen */
1327 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1331 } /* IRC_Send_NAMES */
1335 * Send the ISUPPORT numeric (005).
1336 * This numeric indicates the features that are supported by this server.
1337 * See <http://www.irc.org/tech_docs/005.html> for details.
1340 IRC_Send_ISUPPORT(CLIENT * Client)
1342 if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1344 return DISCONNECTED;
1345 return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1346 CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1347 COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1349 } /* IRC_Send_ISUPPORT */