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 and name */
943 if( ! IRC_WriteStrClient( from, RPL_WHOISUSER_MSG, Client_ID( from ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
946 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;
949 snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
950 cl2chan = Channel_FirstChannelOf( c );
953 chan = Channel_GetChannel( cl2chan );
954 assert( chan != NULL );
957 cl2chan = Channel_NextChannelOf( c, cl2chan );
959 /* Secret channel? */
960 if (strchr(Channel_Modes(chan), 's')
961 && !Channel_IsMemberOf(chan, Client))
964 /* Local channel and request is not from a user? */
965 if (Client_Type(Client) == CLIENT_SERVER
966 && Channel_IsLocal(chan))
969 /* Concatenate channel names */
970 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
971 if( strchr( Channel_UserModes( chan, c ), 'o' )) strlcat( str, "@", sizeof( str ));
972 else if( strchr( Channel_UserModes( chan, c ), 'v' )) strlcat( str, "+", sizeof( str ));
973 strlcat( str, Channel_Name( chan ), sizeof( str ));
975 if( strlen( str ) > ( LINE_LEN - CHANNEL_NAME_LEN - 4 ))
977 /* Line becomes too long: send it! */
978 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
979 snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
982 if( str[strlen( str ) - 1] != ':')
984 /* There is data left to send: */
985 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
989 if( Client_HasMode( c, 'o' ))
991 if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
994 /* Connected using SSL? */
995 if (Conn_UsesSSL(Client_Conn(c))) {
996 if (!IRC_WriteStrClient
997 (from, RPL_WHOISSSL_MSG, Client_ID(from), Client_ID(c)))
1001 /* Idle and signon time (local clients only!) */
1002 if (Client_Conn(c) > NONE ) {
1003 if (! IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
1004 Client_ID(from), Client_ID(c),
1005 (unsigned long)Conn_GetIdle(Client_Conn(c)),
1006 (unsigned long)Conn_GetSignon(Client_Conn(c))))
1007 return DISCONNECTED;
1011 if( Client_HasMode( c, 'a' ))
1013 if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( c ), Client_Away( c ))) return DISCONNECTED;
1017 return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
1022 WHOWAS_EntryWrite(CLIENT *prefix, WHOWAS *entry)
1026 (void)strftime(t_str, sizeof(t_str), "%a %b %d %H:%M:%S %Y",
1027 localtime(&entry->time));
1029 if (!IRC_WriteStrClient(prefix, RPL_WHOWASUSER_MSG, Client_ID(prefix),
1030 entry->id, entry->user, entry->host, entry->info))
1031 return DISCONNECTED;
1033 return IRC_WriteStrClient(prefix, RPL_WHOISSERVER_MSG, Client_ID(prefix),
1034 entry->id, entry->server, t_str);
1038 * IRC "WHOWAS" function.
1039 * This function implements the IRC command "WHOWHAS". It handles local
1040 * requests and request that should be forwarded to other servers.
1043 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1045 CLIENT *target, *prefix;
1047 char tok_buf[COMMAND_LEN];
1048 int max, last, count, i, nc;
1051 assert( Client != NULL );
1052 assert( Req != NULL );
1054 /* Wrong number of parameters? */
1056 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1057 Client_ID(Client), Req->command);
1059 return IRC_WriteStrClient(Client, ERR_NONICKNAMEGIVEN_MSG, Client_ID(Client));
1063 target = Client_Search(Req->argv[2]);
1065 target = Client_ThisServer();
1068 if (Client_Type(Client) == CLIENT_SERVER)
1069 prefix = Client_Search(Req->prefix);
1074 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1075 Client_ID(Client), Req->prefix);
1077 /* Forward to other server? */
1078 if (target != Client_ThisServer()) {
1079 if (!target || (Client_Type(target) != CLIENT_SERVER))
1080 return IRC_WriteStrClient(prefix, ERR_NOSUCHSERVER_MSG,
1081 Client_ID(prefix), Req->argv[2]);
1084 IRC_WriteStrClientPrefix( target, prefix, "WHOWAS %s %s %s",
1085 Req->argv[0], Req->argv[1],
1090 whowas = Client_GetWhowas( );
1091 last = Client_GetLastWhowasIndex( );
1095 max = DEFAULT_WHOWAS;
1096 if (Req->argc > 1) {
1097 max = atoi(Req->argv[1]);
1103 * Break up the nick argument into a list of nicks, if applicable
1104 * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1106 strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1107 nick = strtok(tok_buf, ",");
1109 for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1113 if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1114 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1115 return DISCONNECTED;
1119 /* previous entry */
1122 /* "underflow", wrap around */
1126 if (nc && count >= max)
1128 } while (i != last);
1130 if (nc == 0 && !IRC_WriteStrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1131 Client_ID(prefix), nick))
1132 return DISCONNECTED;
1134 return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG, Client_ID(prefix), Req->argv[0]);
1139 IRC_Send_LUSERS( CLIENT *Client )
1146 assert( Client != NULL );
1148 /* Users, services and serevers in the network */
1149 if( ! IRC_WriteStrClient( Client, RPL_LUSERCLIENT_MSG, Client_ID( Client ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
1151 /* Number of IRC operators */
1152 cnt = Client_OperCount( );
1155 if( ! IRC_WriteStrClient( Client, RPL_LUSEROP_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
1158 /* Unknown connections */
1159 cnt = Client_UnknownCount( );
1162 if( ! IRC_WriteStrClient( Client, RPL_LUSERUNKNOWN_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
1165 /* Number of created channels */
1166 if( ! IRC_WriteStrClient( Client, RPL_LUSERCHANNELS_MSG, Client_ID( Client ), Channel_Count( ))) return DISCONNECTED;
1168 /* Number of local users, services and servers */
1169 if( ! IRC_WriteStrClient( Client, RPL_LUSERME_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
1172 /* Maximum number of local users */
1173 cnt = Client_MyUserCount();
1174 max = Client_MyMaxUserCount();
1175 if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1176 cnt, max, cnt, max))
1177 return DISCONNECTED;
1178 /* Maximum number of users in the network */
1179 cnt = Client_UserCount();
1180 max = Client_MaxUserCount();
1181 if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1182 cnt, max, cnt, max))
1183 return DISCONNECTED;
1184 /* Connection counters */
1185 if (! IRC_WriteStrClient(Client, RPL_STATSCONN_MSG, Client_ID(Client),
1186 Conn_CountMax(), Conn_CountAccepted()))
1187 return DISCONNECTED;
1191 } /* IRC_Send_LUSERS */
1195 Show_MOTD_Start(CLIENT *Client)
1197 return IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG,
1198 Client_ID( Client ), Client_ID( Client_ThisServer( )));
1202 Show_MOTD_Sendline(CLIENT *Client, const char *msg)
1204 return IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID( Client ), msg);
1208 Show_MOTD_End(CLIENT *Client)
1210 return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
1214 static bool Show_MOTD_SSLInfo(CLIENT *Client)
1217 char buf[COMMAND_LEN] = "Connected using Cipher ";
1219 if (!Conn_GetCipherInfo(Client_Conn(Client), buf + 23, sizeof buf - 23))
1222 if (!Show_MOTD_Sendline(Client, buf))
1228 static inline bool Show_MOTD_SSLInfo(UNUSED CLIENT *c) { return true; }
1232 IRC_Show_MOTD( CLIENT *Client )
1237 assert( Client != NULL );
1239 if (Conf_MotdPhrase[0]) {
1240 if (!Show_MOTD_Start(Client))
1241 return DISCONNECTED;
1242 if (!Show_MOTD_Sendline(Client, Conf_MotdPhrase))
1243 return DISCONNECTED;
1247 fd = fopen( Conf_MotdFile, "r" );
1249 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
1250 if (Conn_UsesSSL(Client_Conn(Client))) {
1251 if (!Show_MOTD_Start(Client))
1252 return DISCONNECTED;
1255 return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
1258 if (!Show_MOTD_Start( Client )) {
1263 while (fgets( line, (int)sizeof line, fd )) {
1264 ngt_TrimLastChr( line, '\n');
1266 if( ! Show_MOTD_Sendline( Client, line)) {
1273 if (!Show_MOTD_SSLInfo(Client))
1274 return DISCONNECTED;
1275 return Show_MOTD_End(Client);
1276 } /* IRC_Show_MOTD */
1280 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
1282 bool is_visible, is_member;
1283 char str[LINE_LEN + 1];
1287 assert( Client != NULL );
1288 assert( Chan != NULL );
1290 if( Channel_IsMemberOf( Chan, Client )) is_member = true;
1291 else is_member = false;
1293 /* Secret channel? */
1294 if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
1296 /* Alle Mitglieder suchen */
1297 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1298 cl2chan = Channel_FirstMember( Chan );
1301 cl = Channel_GetClient( cl2chan );
1303 if( strchr( Client_Modes( cl ), 'i' )) is_visible = false;
1304 else is_visible = true;
1306 if( is_member || is_visible )
1308 /* Nick anhaengen */
1309 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
1310 if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
1311 else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
1312 strlcat( str, Client_ID( cl ), sizeof( str ));
1314 if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
1316 /* Zeile wird zu lang: senden! */
1317 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1318 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1322 /* naechstes Mitglied suchen */
1323 cl2chan = Channel_NextMember( Chan, cl2chan );
1325 if( str[strlen( str ) - 1] != ':')
1327 /* Es sind noch Daten da, die gesendet werden muessen */
1328 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1332 } /* IRC_Send_NAMES */
1337 * Send the ISUPPORT numeric (005).
1338 * This numeric indicates the features that are supported by this server.
1339 * See <http://www.irc.org/tech_docs/005.html> for details.
1342 IRC_Send_ISUPPORT PARAMS((CLIENT * Client))
1344 if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1346 return DISCONNECTED;
1347 return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1348 CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1349 COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1351 } /* IRC_Send_ISUPPORT */