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 IRC_USERHOST( CLIENT *Client, REQUEST *Req )
633 char rpl[COMMAND_LEN];
637 assert( Client != NULL );
638 assert( Req != NULL );
640 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
642 if( Req->argc > 5 ) max = 5;
643 else max = Req->argc;
645 strlcpy( rpl, RPL_USERHOST_MSG, sizeof rpl );
646 for( i = 0; i < max; i++ )
648 c = Client_Search( Req->argv[i] );
649 if( c && ( Client_Type( c ) == CLIENT_USER ))
651 /* This Nick is "online" */
652 strlcat( rpl, Client_ID( c ), sizeof( rpl ));
653 if( Client_HasMode( c, 'o' )) strlcat( rpl, "*", sizeof( rpl ));
654 strlcat( rpl, "=", sizeof( rpl ));
655 if( Client_HasMode( c, 'a' )) strlcat( rpl, "-", sizeof( rpl ));
656 else strlcat( rpl, "+", sizeof( rpl ));
657 strlcat( rpl, Client_User( c ), sizeof( rpl ));
658 strlcat( rpl, "@", sizeof( rpl ));
659 strlcat( rpl, Client_Hostname( c ), sizeof( rpl ));
660 strlcat( rpl, " ", sizeof( rpl ));
663 ngt_TrimLastChr( rpl, ' ');
665 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
670 * Handler for the IRC command "USERS".
671 * See RFC 2812 section 4.6. As suggested there the command is disabled.
674 IRC_USERS(CLIENT * Client, REQUEST * Req)
676 return IRC_WriteStrClient(Client, ERR_USERSDISABLED_MSG,
677 Client_ID(Client), Req->command);
682 IRC_VERSION( CLIENT *Client, REQUEST *Req )
684 CLIENT *target, *prefix;
686 assert( Client != NULL );
687 assert( Req != NULL );
689 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
692 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
693 else target = Client_ThisServer( );
695 /* Prefix ermitteln */
696 if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
697 else prefix = Client;
698 if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
700 /* An anderen Server weiterleiten? */
701 if( target != Client_ThisServer( ))
703 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
706 IRC_WriteStrClientPrefix( target, prefix, "VERSION %s", Req->argv[0] );
710 /* send version information */
711 IRC_SetPenalty(Client, 1);
712 return IRC_WriteStrClient(Client, RPL_VERSION_MSG, Client_ID(prefix),
713 PACKAGE_NAME, PACKAGE_VERSION,
714 NGIRCd_DebugLevel, Conf_ServerName,
715 NGIRCd_VersionAddition);
720 write_whoreply(CLIENT *Client, CLIENT *c, const char *channelname, const char *flags)
722 return IRC_WriteStrClient(Client, RPL_WHOREPLY_MSG, Client_ID(Client), channelname,
723 Client_User(c), Client_Hostname(c), Client_ID(Client_Introducer(c)), Client_ID(c),
724 flags, Client_Hops(c), Client_Info(c));
729 who_flags_status(const char *client_modes)
731 if (strchr(client_modes, 'a'))
732 return "G"; /* away */
738 who_flags_qualifier(const char *chan_user_modes)
740 if (strchr(chan_user_modes, 'o'))
742 else if (strchr(chan_user_modes, 'v'))
749 IRC_Send_WHO(CLIENT *Client, CHANNEL *Chan, bool OnlyOps)
751 bool is_visible, is_member, is_ircop;
753 const char *client_modes;
754 const char *chan_user_modes;
758 assert( Client != NULL );
759 assert( Chan != NULL );
761 is_member = Channel_IsMemberOf(Chan, Client);
763 /* Secret channel? */
764 if (!is_member && strchr(Channel_Modes(Chan), 's'))
765 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), Channel_Name(Chan));
767 cl2chan = Channel_FirstMember(Chan);
768 for (; cl2chan ; cl2chan = Channel_NextMember(Chan, cl2chan)) {
769 c = Channel_GetClient(cl2chan);
771 client_modes = Client_Modes(c);
772 is_ircop = strchr(client_modes, 'o') != NULL;
773 if (OnlyOps && !is_ircop)
776 is_visible = strchr(client_modes, 'i') == NULL;
777 if (is_member || is_visible) {
778 strcpy(flags, who_flags_status(client_modes));
780 strlcat(flags, "*", sizeof(flags));
782 chan_user_modes = Channel_UserModes(Chan, c);
783 strlcat(flags, who_flags_qualifier(chan_user_modes), sizeof(flags));
785 if (!write_whoreply(Client, c, Channel_Name(Chan), flags))
789 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), Channel_Name(Chan));
794 IRC_WHO( CLIENT *Client, REQUEST *Req )
796 bool only_ops, have_arg, client_match;
797 const char *channelname, *client_modes, *chan_user_modes;
798 char pattern[COMMAND_LEN];
804 assert( Client != NULL );
805 assert( Req != NULL );
808 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
813 if (Req->argc == 2) {
814 if (strcmp(Req->argv[1], "o") == 0)
817 else return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
821 IRC_SetPenalty(Client, 1);
822 if (Req->argc >= 1) { /* Channel or Mask. */
823 chan = Channel_Search(Req->argv[0]);
825 return IRC_Send_WHO(Client, chan, only_ops);
826 if (strcmp(Req->argv[0], "0") != 0) { /* RFC stupidity, same as no arguments */
828 strlcpy(pattern, Req->argv[0], sizeof(pattern));
829 ngt_LowerStr(pattern);
830 IRC_SetPenalty(Client, 3);
834 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
835 if (Client_Type(c) != CLIENT_USER)
839 * In the absence of the parameter, all visible (users who aren't
840 * invisible (user mode +i) and who don't have a common channel
841 * with the requesting client) are listed.
843 * The same result can be achieved by using a [sic] of "0"
844 * or any wildcard which will end up matching every visible user.
846 * The [sic] passed to WHO is matched against users' host, server, real name and
847 * nickname if the channel cannot be found.
849 client_modes = Client_Modes(c);
850 if (strchr(client_modes, 'i'))
853 if (only_ops && !strchr(client_modes, 'o'))
856 if (have_arg) { /* match pattern against user host/server/name/nick */
857 client_match = MatchCaseInsensitive(pattern, Client_Hostname(c)); /* user's host */
859 client_match = MatchCaseInsensitive(pattern, Client_ID(Client_Introducer(c))); /* server */
861 client_match = Match(Req->argv[0], Client_Info(c)); /* realname */
863 client_match = MatchCaseInsensitive(pattern, Client_ID(c)); /* nick name */
865 if (!client_match) /* This isn't the client you're looking for */
869 strcpy(flags, who_flags_status(client_modes));
871 if (strchr(client_modes, 'o')) /* this client is an operator */
872 strlcat(flags, "*", sizeof(flags));
874 /* Search suitable channel */
875 cl2chan = Channel_FirstChannelOf(c);
877 cn = Channel_GetChannel(cl2chan);
878 if (Channel_IsMemberOf(cn, Client) ||
879 !strchr(Channel_Modes(cn), 's'))
881 channelname = Channel_Name(cn);
884 cl2chan = Channel_NextChannelOf(c, cl2chan);
887 chan = Channel_GetChannel(cl2chan);
888 chan_user_modes = Channel_UserModes(chan, c);
889 strlcat(flags, who_flags_qualifier(chan_user_modes), sizeof(flags));
893 if (!write_whoreply(Client, c, channelname, flags))
898 channelname = Req->argv[0];
902 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), channelname);
907 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
909 CLIENT *from, *target, *c;
910 char str[LINE_LEN + 1];
914 assert( Client != NULL );
915 assert( Req != NULL );
917 /* Bad number of parameters? */
918 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
921 c = Client_Search( Req->argv[Req->argc - 1] );
922 if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[Req->argc - 1] );
924 /* Search sender of the WHOIS */
925 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
927 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
929 /* Forward to other server? */
932 /* Search target server (can be specified as nick of that server!) */
933 target = Client_Search( Req->argv[0] );
934 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
936 else target = Client_ThisServer( );
938 assert( target != NULL );
940 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] );
942 /* Nick, user, hostname and client info */
943 if (!IRC_WriteStrClient(from, RPL_WHOISUSER_MSG, Client_ID(from),
944 Client_ID(c), Client_User(c),
945 Client_HostnameCloaked(c), Client_Info(c)))
949 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;
952 snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
953 cl2chan = Channel_FirstChannelOf( c );
956 chan = Channel_GetChannel( cl2chan );
957 assert( chan != NULL );
960 cl2chan = Channel_NextChannelOf( c, cl2chan );
962 /* Secret channel? */
963 if (strchr(Channel_Modes(chan), 's')
964 && !Channel_IsMemberOf(chan, Client))
967 /* Local channel and request is not from a user? */
968 if (Client_Type(Client) == CLIENT_SERVER
969 && Channel_IsLocal(chan))
972 /* Concatenate channel names */
973 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
974 if( strchr( Channel_UserModes( chan, c ), 'o' )) strlcat( str, "@", sizeof( str ));
975 else if( strchr( Channel_UserModes( chan, c ), 'v' )) strlcat( str, "+", sizeof( str ));
976 strlcat( str, Channel_Name( chan ), sizeof( str ));
978 if( strlen( str ) > ( LINE_LEN - CHANNEL_NAME_LEN - 4 ))
980 /* Line becomes too long: send it! */
981 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
982 snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
985 if( str[strlen( str ) - 1] != ':')
987 /* There is data left to send: */
988 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
992 if( Client_HasMode( c, 'o' ))
994 if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
997 /* Connected using SSL? */
998 if (Conn_UsesSSL(Client_Conn(c))) {
999 if (!IRC_WriteStrClient
1000 (from, RPL_WHOISSSL_MSG, Client_ID(from), Client_ID(c)))
1001 return DISCONNECTED;
1004 /* Idle and signon time (local clients only!) */
1005 if (Client_Conn(c) > NONE ) {
1006 if (! IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
1007 Client_ID(from), Client_ID(c),
1008 (unsigned long)Conn_GetIdle(Client_Conn(c)),
1009 (unsigned long)Conn_GetSignon(Client_Conn(c))))
1010 return DISCONNECTED;
1014 if( Client_HasMode( c, 'a' ))
1016 if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( c ), Client_Away( c ))) return DISCONNECTED;
1020 return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
1025 WHOWAS_EntryWrite(CLIENT *prefix, WHOWAS *entry)
1029 (void)strftime(t_str, sizeof(t_str), "%a %b %d %H:%M:%S %Y",
1030 localtime(&entry->time));
1032 if (!IRC_WriteStrClient(prefix, RPL_WHOWASUSER_MSG, Client_ID(prefix),
1033 entry->id, entry->user, entry->host, entry->info))
1034 return DISCONNECTED;
1036 return IRC_WriteStrClient(prefix, RPL_WHOISSERVER_MSG, Client_ID(prefix),
1037 entry->id, entry->server, t_str);
1041 * IRC "WHOWAS" function.
1042 * This function implements the IRC command "WHOWHAS". It handles local
1043 * requests and request that should be forwarded to other servers.
1046 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1048 CLIENT *target, *prefix;
1050 char tok_buf[COMMAND_LEN];
1051 int max, last, count, i, nc;
1054 assert( Client != NULL );
1055 assert( Req != NULL );
1057 /* Wrong number of parameters? */
1059 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1060 Client_ID(Client), Req->command);
1062 return IRC_WriteStrClient(Client, ERR_NONICKNAMEGIVEN_MSG, Client_ID(Client));
1066 target = Client_Search(Req->argv[2]);
1068 target = Client_ThisServer();
1071 if (Client_Type(Client) == CLIENT_SERVER)
1072 prefix = Client_Search(Req->prefix);
1077 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1078 Client_ID(Client), Req->prefix);
1080 /* Forward to other server? */
1081 if (target != Client_ThisServer()) {
1082 if (!target || (Client_Type(target) != CLIENT_SERVER))
1083 return IRC_WriteStrClient(prefix, ERR_NOSUCHSERVER_MSG,
1084 Client_ID(prefix), Req->argv[2]);
1087 IRC_WriteStrClientPrefix( target, prefix, "WHOWAS %s %s %s",
1088 Req->argv[0], Req->argv[1],
1093 whowas = Client_GetWhowas( );
1094 last = Client_GetLastWhowasIndex( );
1098 max = DEFAULT_WHOWAS;
1099 if (Req->argc > 1) {
1100 max = atoi(Req->argv[1]);
1106 * Break up the nick argument into a list of nicks, if applicable
1107 * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1109 strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1110 nick = strtok(tok_buf, ",");
1112 for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1116 if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1117 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1118 return DISCONNECTED;
1122 /* previous entry */
1125 /* "underflow", wrap around */
1129 if (nc && count >= max)
1131 } while (i != last);
1133 if (nc == 0 && !IRC_WriteStrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1134 Client_ID(prefix), nick))
1135 return DISCONNECTED;
1137 return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG, Client_ID(prefix), Req->argv[0]);
1142 IRC_Send_LUSERS( CLIENT *Client )
1149 assert( Client != NULL );
1151 /* Users, services and serevers in the network */
1152 if( ! IRC_WriteStrClient( Client, RPL_LUSERCLIENT_MSG, Client_ID( Client ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
1154 /* Number of IRC operators */
1155 cnt = Client_OperCount( );
1158 if( ! IRC_WriteStrClient( Client, RPL_LUSEROP_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
1161 /* Unknown connections */
1162 cnt = Client_UnknownCount( );
1165 if( ! IRC_WriteStrClient( Client, RPL_LUSERUNKNOWN_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
1168 /* Number of created channels */
1169 if( ! IRC_WriteStrClient( Client, RPL_LUSERCHANNELS_MSG, Client_ID( Client ), Channel_Count( ))) return DISCONNECTED;
1171 /* Number of local users, services and servers */
1172 if( ! IRC_WriteStrClient( Client, RPL_LUSERME_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
1175 /* Maximum number of local users */
1176 cnt = Client_MyUserCount();
1177 max = Client_MyMaxUserCount();
1178 if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1179 cnt, max, cnt, max))
1180 return DISCONNECTED;
1181 /* Maximum number of users in the network */
1182 cnt = Client_UserCount();
1183 max = Client_MaxUserCount();
1184 if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1185 cnt, max, cnt, max))
1186 return DISCONNECTED;
1187 /* Connection counters */
1188 if (! IRC_WriteStrClient(Client, RPL_STATSCONN_MSG, Client_ID(Client),
1189 Conn_CountMax(), Conn_CountAccepted()))
1190 return DISCONNECTED;
1194 } /* IRC_Send_LUSERS */
1198 Show_MOTD_Start(CLIENT *Client)
1200 return IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG,
1201 Client_ID( Client ), Client_ID( Client_ThisServer( )));
1205 Show_MOTD_Sendline(CLIENT *Client, const char *msg)
1207 return IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID( Client ), msg);
1211 Show_MOTD_End(CLIENT *Client)
1213 return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
1217 static bool Show_MOTD_SSLInfo(CLIENT *Client)
1220 char buf[COMMAND_LEN] = "Connected using Cipher ";
1222 if (!Conn_GetCipherInfo(Client_Conn(Client), buf + 23, sizeof buf - 23))
1225 if (!Show_MOTD_Sendline(Client, buf))
1231 static inline bool Show_MOTD_SSLInfo(UNUSED CLIENT *c) { return true; }
1235 IRC_Show_MOTD( CLIENT *Client )
1238 size_t len_tot, len_str;
1240 assert( Client != NULL );
1242 len_tot = array_bytes(&Conf_Motd);
1243 if (len_tot == 0 && !Conn_UsesSSL(Client_Conn(Client)))
1244 return IRC_WriteStrClient(Client, ERR_NOMOTD_MSG, Client_ID(Client));
1246 if (!Show_MOTD_Start(Client))
1247 return DISCONNECTED;
1249 line = array_start(&Conf_Motd);
1250 while (len_tot > 0) {
1251 len_str = strlen(line) + 1;
1253 assert(len_tot >= len_str);
1256 if (!Show_MOTD_Sendline(Client, line))
1257 return DISCONNECTED;
1261 if (!Show_MOTD_SSLInfo(Client))
1262 return DISCONNECTED;
1263 return Show_MOTD_End(Client);
1264 } /* IRC_Show_MOTD */
1268 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
1270 bool is_visible, is_member;
1271 char str[LINE_LEN + 1];
1275 assert( Client != NULL );
1276 assert( Chan != NULL );
1278 if( Channel_IsMemberOf( Chan, Client )) is_member = true;
1279 else is_member = false;
1281 /* Secret channel? */
1282 if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
1284 /* Alle Mitglieder suchen */
1285 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1286 cl2chan = Channel_FirstMember( Chan );
1289 cl = Channel_GetClient( cl2chan );
1291 if( strchr( Client_Modes( cl ), 'i' )) is_visible = false;
1292 else is_visible = true;
1294 if( is_member || is_visible )
1296 /* Nick anhaengen */
1297 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
1298 if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
1299 else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
1300 strlcat( str, Client_ID( cl ), sizeof( str ));
1302 if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
1304 /* Zeile wird zu lang: senden! */
1305 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1306 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1310 /* naechstes Mitglied suchen */
1311 cl2chan = Channel_NextMember( Chan, cl2chan );
1313 if( str[strlen( str ) - 1] != ':')
1315 /* Es sind noch Daten da, die gesendet werden muessen */
1316 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1320 } /* IRC_Send_NAMES */
1325 * Send the ISUPPORT numeric (005).
1326 * This numeric indicates the features that are supported by this server.
1327 * See <http://www.irc.org/tech_docs/005.html> for details.
1330 IRC_Send_ISUPPORT PARAMS((CLIENT * Client))
1332 if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1334 return DISCONNECTED;
1335 return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1336 CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1337 COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1339 } /* IRC_Send_ISUPPORT */