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"
37 #include "irc-write.h"
44 IRC_ADMIN(CLIENT *Client, REQUEST *Req )
46 CLIENT *target, *prefix;
48 assert( Client != NULL );
49 assert( Req != NULL );
51 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
54 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
55 else target = Client_ThisServer( );
58 if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
60 if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
62 /* forwad message to another server? */
63 if( target != Client_ThisServer( ))
65 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
68 IRC_WriteStrClientPrefix( target, prefix, "ADMIN %s", Req->argv[0] );
72 /* mit Versionsinfo antworten */
73 if( ! IRC_WriteStrClient( Client, RPL_ADMINME_MSG, Client_ID( prefix ), Conf_ServerName )) return DISCONNECTED;
74 if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC1_MSG, Client_ID( prefix ), Conf_ServerAdmin1 )) return DISCONNECTED;
75 if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC2_MSG, Client_ID( prefix ), Conf_ServerAdmin2 )) return DISCONNECTED;
76 if( ! IRC_WriteStrClient( Client, RPL_ADMINEMAIL_MSG, Client_ID( prefix ), Conf_ServerAdminMail )) return DISCONNECTED;
78 IRC_SetPenalty( Client, 1 );
84 * Handler for the IRC command "INFO".
85 * See RFC 2812 section 3.4.10.
88 IRC_INFO(CLIENT * Client, REQUEST * Req)
90 CLIENT *target, *prefix;
93 assert(Client != NULL);
96 /* Wrong number of parameters? */
98 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
99 Client_ID(Client), Req->command);
101 /* Determine prefix */
102 if (Client_Type(Client) == CLIENT_SERVER)
103 prefix = Client_Search(Req->prefix);
107 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
108 Client_ID(Client), Req->prefix);
110 /* Look for a target */
112 target = Client_Search(Req->argv[0]);
114 target = Client_ThisServer();
116 /* Make sure that the target is a server */
117 if (target && Client_Type(target) != CLIENT_SERVER)
118 target = Client_Introducer(target);
121 return IRC_WriteStrClient(prefix, ERR_NOSUCHSERVER_MSG,
122 Client_ID(prefix), Req->argv[0]);
124 /* Pass on to another server? */
125 if (target != Client_ThisServer()) {
126 IRC_WriteStrClientPrefix(target, prefix, "INFO %s",
131 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix),
135 #if defined(__DATE__) && defined(__TIME__)
136 snprintf(msg, sizeof(msg), "Birth Date: %s at %s", __DATE__, __TIME__);
137 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
141 strlcpy(msg, "On-line since ", sizeof(msg));
142 strlcat(msg, NGIRCd_StartStr, sizeof(msg));
143 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
146 if (!IRC_WriteStrClient(Client, RPL_ENDOFINFO_MSG, Client_ID(prefix)))
149 IRC_SetPenalty(Client, 2);
155 IRC_ISON( CLIENT *Client, REQUEST *Req )
157 char rpl[COMMAND_LEN];
162 assert( Client != NULL );
163 assert( Req != NULL );
165 /* Falsche Anzahl Parameter? */
166 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
168 strlcpy( rpl, RPL_ISON_MSG, sizeof rpl );
169 for( i = 0; i < Req->argc; i++ )
171 ptr = strtok( Req->argv[i], " " );
175 c = Client_Search( ptr );
176 if( c && ( Client_Type( c ) == CLIENT_USER ))
178 /* Dieser Nick ist "online" */
179 strlcat( rpl, ptr, sizeof( rpl ));
180 strlcat( rpl, " ", sizeof( rpl ));
182 ptr = strtok( NULL, " " );
185 ngt_TrimLastChr(rpl, ' ');
187 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
192 IRC_LINKS( CLIENT *Client, REQUEST *Req )
194 CLIENT *target, *from, *c;
197 assert( Client != NULL );
198 assert( Req != NULL );
200 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
202 /* Server-Mask ermitteln */
203 if( Req->argc > 0 ) mask = Req->argv[Req->argc - 1];
206 /* Absender ermitteln */
207 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
209 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
211 /* An anderen Server forwarden? */
214 target = Client_Search( Req->argv[0] );
215 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
216 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LINKS %s %s", Req->argv[0], Req->argv[1] );
219 /* Wer ist der Absender? */
220 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
221 else target = Client;
222 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
227 if( Client_Type( c ) == CLIENT_SERVER )
229 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;
231 c = Client_Next( c );
234 IRC_SetPenalty( target, 1 );
235 return IRC_WriteStrClient( target, RPL_ENDOFLINKS_MSG, Client_ID( target ), mask );
240 IRC_LUSERS( CLIENT *Client, REQUEST *Req )
242 CLIENT *target, *from;
244 assert( Client != NULL );
245 assert( Req != NULL );
247 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
249 /* Absender ermitteln */
250 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
252 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
254 /* An anderen Server forwarden? */
257 target = Client_Search( Req->argv[1] );
258 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
259 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LUSERS %s %s", Req->argv[0], Req->argv[1] );
262 /* Wer ist der Absender? */
263 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
264 else target = Client;
265 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
267 IRC_Send_LUSERS( target );
269 IRC_SetPenalty( target, 1 );
275 * Handler for the IRC command "SERVLIST".
276 * List registered services, see RFC 2811, section 3.5.1: the syntax is
277 * "SERVLIST [<mask> [<type>]]".
280 IRC_SERVLIST(CLIENT *Client, REQUEST *Req)
284 assert(Client != NULL);
288 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
289 Client_ID(Client), Req->command);
291 if (Req->argc < 2 || strcmp(Req->argv[1], "0") == 0) {
292 for (c = Client_First(); c!= NULL; c = Client_Next(c)) {
293 if (Client_Type(c) != CLIENT_SERVICE)
295 if (Req->argc > 0 && !MatchCaseInsensitive(Req->argv[0],
298 if (!IRC_WriteStrClient(Client, RPL_SERVLIST_MSG,
299 Client_ID(Client), Client_Mask(c),
300 Client_Mask(Client_Introducer(c)), "*",
301 0, Client_Hops(c), Client_Info(c)))
306 return IRC_WriteStrClient(Client, RPL_SERVLISTEND_MSG, Client_ID(Client),
307 Req->argc > 0 ? Req->argv[0] : "*",
308 Req->argc > 1 ? Req->argv[1] : "0");
313 IRC_MOTD( CLIENT *Client, REQUEST *Req )
315 CLIENT *from, *target;
317 assert( Client != NULL );
318 assert( Req != NULL );
320 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
322 /* From aus Prefix ermitteln */
323 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
325 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
330 target = Client_Search( Req->argv[0] );
331 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
333 if( target != Client_ThisServer( ))
335 /* Ok, anderer Server ist das Ziel: forwarden */
336 return IRC_WriteStrClientPrefix( target, from, "MOTD %s", Req->argv[0] );
340 IRC_SetPenalty( from, 3 );
341 return IRC_Show_MOTD( from );
346 IRC_NAMES( CLIENT *Client, REQUEST *Req )
348 char rpl[COMMAND_LEN], *ptr;
349 CLIENT *target, *from, *c;
352 assert( Client != NULL );
353 assert( Req != NULL );
355 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
357 /* use prefix to determine "From" */
358 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
360 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
364 /* forward to another server? */
365 target = Client_Search( Req->argv[1] );
366 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
368 if( target != Client_ThisServer( )) {
369 /* target is another server, forward */
370 return IRC_WriteStrClientPrefix( target, from, "NAMES %s :%s", Req->argv[0], Req->argv[1] );
376 /* bestimmte Channels durchgehen */
377 ptr = strtok( Req->argv[0], "," );
380 chan = Channel_Search( ptr );
384 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
386 if( ! IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), ptr )) return DISCONNECTED;
388 /* get next channel name */
389 ptr = strtok( NULL, "," );
394 chan = Channel_First( );
397 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
399 chan = Channel_Next( chan );
402 /* Now print all clients which are not in any channel */
404 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
407 if(( Client_Type( c ) == CLIENT_USER ) && ( Channel_FirstChannelOf( c ) == NULL ) && ( ! strchr( Client_Modes( c ), 'i' )))
409 /* its a user, concatenate ... */
410 if( rpl[strlen( rpl ) - 1] != ':' ) strlcat( rpl, " ", sizeof( rpl ));
411 strlcat( rpl, Client_ID( c ), sizeof( rpl ));
413 if( strlen( rpl ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
415 /* Line is gwoing too long, send now */
416 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
417 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
421 c = Client_Next( c );
423 if( rpl[strlen( rpl ) - 1] != ':')
425 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
428 IRC_SetPenalty( from, 1 );
429 return IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), "*" );
434 t_diff(time_t *t, const time_t d)
442 return (unsigned int)diff;
447 uptime_days(time_t *now)
449 return t_diff(now, 60 * 60 * 24);
454 uptime_hrs(time_t *now)
456 return t_diff(now, 60 * 60);
461 uptime_mins(time_t *now)
463 return t_diff(now, 60);
468 * Handler for the IRC command "STATS".
469 * See RFC 2812 section 3.4.4.
472 IRC_STATS( CLIENT *Client, REQUEST *Req )
474 CLIENT *from, *target, *cl;
479 unsigned int days, hrs, mins;
481 assert(Client != NULL);
485 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
486 Client_ID(Client), Req->command);
488 /* use prefix to determine "From" */
489 if (Client_Type(Client) == CLIENT_SERVER)
490 from = Client_Search(Req->prefix);
495 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
496 Client_ID(Client), Req->prefix);
498 if (Req->argc == 2) {
499 /* forward to another server? */
500 target = Client_Search(Req->argv[1]);
501 if ((!target) || (Client_Type(target) != CLIENT_SERVER))
502 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
503 Client_ID(from), Req->argv[1]);
505 if (target != Client_ThisServer()) {
506 /* forward to another server */
507 return IRC_WriteStrClientPrefix(target, from,
508 "STATS %s %s", Req->argv[0], Req->argv[1]);
513 query = Req->argv[0][0] ? Req->argv[0][0] : '*';
518 case 'l': /* Link status (servers and own link) */
520 time_now = time(NULL);
521 for (con = Conn_First(); con != NONE; con = Conn_Next(con)) {
522 cl = Conn_GetClient(con);
525 if ((Client_Type(cl) == CLIENT_SERVER)
527 /* Server link or our own connection */
529 if (Conn_Options(con) & CONN_ZIP) {
530 if (!IRC_WriteStrClient
531 (from, RPL_STATSLINKINFOZIP_MSG,
532 Client_ID(from), Client_Mask(cl),
533 Conn_SendQ(con), Conn_SendMsg(con),
539 (long)(time_now - Conn_StartTime(con))))
544 if (!IRC_WriteStrClient
545 (from, RPL_STATSLINKINFO_MSG,
546 Client_ID(from), Client_Mask(cl),
547 Conn_SendQ(con), Conn_SendMsg(con),
548 Conn_SendBytes(con), Conn_RecvMsg(con),
550 (long)(time_now - Conn_StartTime(con))))
555 case 'm': /* IRC command status (usage count) */
557 cmd = Parse_GetCommandStruct();
558 for (; cmd->name; cmd++) {
559 if (cmd->lcount == 0 && cmd->rcount == 0)
561 if (!IRC_WriteStrClient
562 (from, RPL_STATSCOMMANDS_MSG, Client_ID(from),
563 cmd->name, cmd->lcount, cmd->bytes, cmd->rcount))
567 case 'u': /* Server uptime */
569 time_now = time(NULL) - NGIRCd_Start;
570 days = uptime_days(&time_now);
571 hrs = uptime_hrs(&time_now);
572 mins = uptime_mins(&time_now);
573 if (!IRC_WriteStrClient(from, RPL_STATSUPTIME, Client_ID(from),
574 days, hrs, mins, (unsigned int)time_now))
579 IRC_SetPenalty(from, 2);
580 return IRC_WriteStrClient(from, RPL_ENDOFSTATS_MSG,
581 Client_ID(from), query);
586 * Handler for the IRC command "SUMMON".
587 * See RFC 2812 section 4.5. ngIRCd doesn't implement this functionality and
588 * therefore answers with ERR_SUMMONDISABLED.
591 IRC_SUMMON(CLIENT * Client, REQUEST * Req)
593 return IRC_WriteStrClient(Client, ERR_SUMMONDISABLED_MSG,
594 Client_ID(Client), Req->command);
599 IRC_TIME( CLIENT *Client, REQUEST *Req )
601 CLIENT *from, *target;
605 assert( Client != NULL );
606 assert( Req != NULL );
608 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
610 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
612 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
616 target = Client_Search( Req->argv[0] );
617 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
619 if( target != Client_ThisServer( ))
621 return IRC_WriteStrClientPrefix( target, from, "TIME %s", Req->argv[0] );
626 (void)strftime( t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime( &t ));
627 return IRC_WriteStrClient( from, RPL_TIME_MSG, Client_ID( from ), Client_ID( Client_ThisServer( )), t_str );
632 IRC_USERHOST( CLIENT *Client, REQUEST *Req )
634 char rpl[COMMAND_LEN];
638 assert( Client != NULL );
639 assert( Req != NULL );
641 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
643 if( Req->argc > 5 ) max = 5;
644 else max = Req->argc;
646 strlcpy( rpl, RPL_USERHOST_MSG, sizeof rpl );
647 for( i = 0; i < max; i++ )
649 c = Client_Search( Req->argv[i] );
650 if( c && ( Client_Type( c ) == CLIENT_USER ))
652 /* This Nick is "online" */
653 strlcat( rpl, Client_ID( c ), sizeof( rpl ));
654 if( Client_HasMode( c, 'o' )) strlcat( rpl, "*", sizeof( rpl ));
655 strlcat( rpl, "=", sizeof( rpl ));
656 if( Client_HasMode( c, 'a' )) strlcat( rpl, "-", sizeof( rpl ));
657 else strlcat( rpl, "+", sizeof( rpl ));
658 strlcat( rpl, Client_User( c ), sizeof( rpl ));
659 strlcat( rpl, "@", sizeof( rpl ));
660 strlcat( rpl, Client_Hostname( c ), sizeof( rpl ));
661 strlcat( rpl, " ", sizeof( rpl ));
664 ngt_TrimLastChr( rpl, ' ');
666 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
671 * Handler for the IRC command "USERS".
672 * See RFC 2812 section 4.6. As suggested there the command is disabled.
675 IRC_USERS(CLIENT * Client, REQUEST * Req)
677 return IRC_WriteStrClient(Client, ERR_USERSDISABLED_MSG,
678 Client_ID(Client), Req->command);
683 IRC_VERSION( CLIENT *Client, REQUEST *Req )
685 CLIENT *target, *prefix;
687 assert( Client != NULL );
688 assert( Req != NULL );
690 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
693 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
694 else target = Client_ThisServer( );
696 /* Prefix ermitteln */
697 if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
698 else prefix = Client;
699 if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
701 /* An anderen Server weiterleiten? */
702 if( target != Client_ThisServer( ))
704 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
707 IRC_WriteStrClientPrefix( target, prefix, "VERSION %s", Req->argv[0] );
711 /* send version information */
712 IRC_SetPenalty(Client, 1);
713 return IRC_WriteStrClient(Client, RPL_VERSION_MSG, Client_ID(prefix),
714 PACKAGE_NAME, PACKAGE_VERSION,
715 NGIRCd_DebugLevel, Conf_ServerName,
716 NGIRCd_VersionAddition);
721 write_whoreply(CLIENT *Client, CLIENT *c, const char *channelname, const char *flags)
723 return IRC_WriteStrClient(Client, RPL_WHOREPLY_MSG, Client_ID(Client), channelname,
724 Client_User(c), Client_Hostname(c), Client_ID(Client_Introducer(c)), Client_ID(c),
725 flags, Client_Hops(c), Client_Info(c));
730 who_flags_status(const char *client_modes)
732 if (strchr(client_modes, 'a'))
733 return "G"; /* away */
739 who_flags_qualifier(const char *chan_user_modes)
741 if (strchr(chan_user_modes, 'o'))
743 else if (strchr(chan_user_modes, 'v'))
750 IRC_Send_WHO(CLIENT *Client, CHANNEL *Chan, bool OnlyOps)
752 bool is_visible, is_member, is_ircop;
754 const char *client_modes;
755 const char *chan_user_modes;
759 assert( Client != NULL );
760 assert( Chan != NULL );
762 is_member = Channel_IsMemberOf(Chan, Client);
764 /* Secret channel? */
765 if (!is_member && strchr(Channel_Modes(Chan), 's'))
766 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), Channel_Name(Chan));
768 cl2chan = Channel_FirstMember(Chan);
769 for (; cl2chan ; cl2chan = Channel_NextMember(Chan, cl2chan)) {
770 c = Channel_GetClient(cl2chan);
772 client_modes = Client_Modes(c);
773 is_ircop = strchr(client_modes, 'o') != NULL;
774 if (OnlyOps && !is_ircop)
777 is_visible = strchr(client_modes, 'i') == NULL;
778 if (is_member || is_visible) {
779 strcpy(flags, who_flags_status(client_modes));
781 strlcat(flags, "*", sizeof(flags));
783 chan_user_modes = Channel_UserModes(Chan, c);
784 strlcat(flags, who_flags_qualifier(chan_user_modes), sizeof(flags));
786 if (!write_whoreply(Client, c, Channel_Name(Chan), flags))
790 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), Channel_Name(Chan));
795 IRC_WHO( CLIENT *Client, REQUEST *Req )
797 bool only_ops, have_arg, client_match;
798 const char *channelname, *client_modes, *chan_user_modes;
799 char pattern[COMMAND_LEN];
805 assert( Client != NULL );
806 assert( Req != NULL );
809 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
814 if (Req->argc == 2) {
815 if (strcmp(Req->argv[1], "o") == 0)
818 else return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
822 IRC_SetPenalty(Client, 1);
823 if (Req->argc >= 1) { /* Channel or Mask. */
824 chan = Channel_Search(Req->argv[0]);
826 return IRC_Send_WHO(Client, chan, only_ops);
827 if (strcmp(Req->argv[0], "0") != 0) { /* RFC stupidity, same as no arguments */
829 strlcpy(pattern, Req->argv[0], sizeof(pattern));
830 ngt_LowerStr(pattern);
831 IRC_SetPenalty(Client, 3);
835 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
836 if (Client_Type(c) != CLIENT_USER)
840 * In the absence of the parameter, all visible (users who aren't
841 * invisible (user mode +i) and who don't have a common channel
842 * with the requesting client) are listed.
844 * The same result can be achieved by using a [sic] of "0"
845 * or any wildcard which will end up matching every visible user.
847 * The [sic] passed to WHO is matched against users' host, server, real name and
848 * nickname if the channel cannot be found.
850 client_modes = Client_Modes(c);
851 if (strchr(client_modes, 'i'))
854 if (only_ops && !strchr(client_modes, 'o'))
857 if (have_arg) { /* match pattern against user host/server/name/nick */
858 client_match = MatchCaseInsensitive(pattern, Client_Hostname(c)); /* user's host */
860 client_match = MatchCaseInsensitive(pattern, Client_ID(Client_Introducer(c))); /* server */
862 client_match = Match(Req->argv[0], Client_Info(c)); /* realname */
864 client_match = MatchCaseInsensitive(pattern, Client_ID(c)); /* nick name */
866 if (!client_match) /* This isn't the client you're looking for */
870 strcpy(flags, who_flags_status(client_modes));
872 if (strchr(client_modes, 'o')) /* this client is an operator */
873 strlcat(flags, "*", sizeof(flags));
875 /* Search suitable channel */
876 cl2chan = Channel_FirstChannelOf(c);
878 cn = Channel_GetChannel(cl2chan);
879 if (Channel_IsMemberOf(cn, Client) ||
880 !strchr(Channel_Modes(cn), 's'))
882 channelname = Channel_Name(cn);
885 cl2chan = Channel_NextChannelOf(c, cl2chan);
888 chan = Channel_GetChannel(cl2chan);
889 chan_user_modes = Channel_UserModes(chan, c);
890 strlcat(flags, who_flags_qualifier(chan_user_modes), sizeof(flags));
894 if (!write_whoreply(Client, c, channelname, flags))
899 channelname = Req->argv[0];
903 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), channelname);
908 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
910 CLIENT *from, *target, *c;
911 char str[LINE_LEN + 1];
915 assert( Client != NULL );
916 assert( Req != NULL );
918 /* Bad number of parameters? */
919 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
922 c = Client_Search( Req->argv[Req->argc - 1] );
923 if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[Req->argc - 1] );
925 /* Search sender of the WHOIS */
926 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
928 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
930 /* Forward to other server? */
933 /* Search target server (can be specified as nick of that server!) */
934 target = Client_Search( Req->argv[0] );
935 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
937 else target = Client_ThisServer( );
939 assert( target != NULL );
941 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] );
943 /* Nick, user and name */
944 if( ! IRC_WriteStrClient( from, RPL_WHOISUSER_MSG, Client_ID( from ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
947 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;
950 snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
951 cl2chan = Channel_FirstChannelOf( c );
954 chan = Channel_GetChannel( cl2chan );
955 assert( chan != NULL );
958 cl2chan = Channel_NextChannelOf( c, cl2chan );
960 /* Secret channel? */
961 if (strchr(Channel_Modes(chan), 's')
962 && !Channel_IsMemberOf(chan, Client))
965 /* Local channel and request is not from a user? */
966 if (Client_Type(Client) == CLIENT_SERVER
967 && Channel_IsLocal(chan))
970 /* Concatenate channel names */
971 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
972 if( strchr( Channel_UserModes( chan, c ), 'o' )) strlcat( str, "@", sizeof( str ));
973 else if( strchr( Channel_UserModes( chan, c ), 'v' )) strlcat( str, "+", sizeof( str ));
974 strlcat( str, Channel_Name( chan ), sizeof( str ));
976 if( strlen( str ) > ( LINE_LEN - CHANNEL_NAME_LEN - 4 ))
978 /* Line becomes too long: send it! */
979 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
980 snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
983 if( str[strlen( str ) - 1] != ':')
985 /* There is data left to send: */
986 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
990 if( Client_HasMode( c, 'o' ))
992 if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
995 /* Connected using SSL? */
996 if (Conn_UsesSSL(Client_Conn(c))) {
997 if (!IRC_WriteStrClient
998 (from, RPL_WHOISSSL_MSG, Client_ID(from), Client_ID(c)))
1002 /* Idle and signon time (local clients only!) */
1003 if (Client_Conn(c) > NONE ) {
1004 if (! IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
1005 Client_ID(from), Client_ID(c),
1006 (unsigned long)Conn_GetIdle(Client_Conn(c)),
1007 (unsigned long)Conn_GetSignon(Client_Conn(c))))
1008 return DISCONNECTED;
1012 if( Client_HasMode( c, 'a' ))
1014 if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( c ), Client_Away( c ))) return DISCONNECTED;
1018 return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
1023 WHOWAS_EntryWrite(CLIENT *prefix, WHOWAS *entry)
1027 (void)strftime(t_str, sizeof(t_str), "%a %b %d %H:%M:%S %Y",
1028 localtime(&entry->time));
1030 if (!IRC_WriteStrClient(prefix, RPL_WHOWASUSER_MSG, Client_ID(prefix),
1031 entry->id, entry->user, entry->host, entry->info))
1032 return DISCONNECTED;
1034 return IRC_WriteStrClient(prefix, RPL_WHOISSERVER_MSG, Client_ID(prefix),
1035 entry->id, entry->server, t_str);
1039 * IRC "WHOWAS" function.
1040 * This function implements the IRC command "WHOWHAS". It handles local
1041 * requests and request that should be forwarded to other servers.
1044 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1046 CLIENT *target, *prefix;
1048 char tok_buf[COMMAND_LEN];
1049 int max, last, count, i, nc;
1052 assert( Client != NULL );
1053 assert( Req != NULL );
1055 /* Wrong number of parameters? */
1057 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1058 Client_ID(Client), Req->command);
1060 return IRC_WriteStrClient(Client, ERR_NONICKNAMEGIVEN_MSG, Client_ID(Client));
1064 target = Client_Search(Req->argv[2]);
1066 target = Client_ThisServer();
1069 if (Client_Type(Client) == CLIENT_SERVER)
1070 prefix = Client_Search(Req->prefix);
1075 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1076 Client_ID(Client), Req->prefix);
1078 /* Forward to other server? */
1079 if (target != Client_ThisServer()) {
1080 if (!target || (Client_Type(target) != CLIENT_SERVER))
1081 return IRC_WriteStrClient(prefix, ERR_NOSUCHSERVER_MSG,
1082 Client_ID(prefix), Req->argv[2]);
1085 IRC_WriteStrClientPrefix( target, prefix, "WHOWAS %s %s %s",
1086 Req->argv[0], Req->argv[1],
1091 whowas = Client_GetWhowas( );
1092 last = Client_GetLastWhowasIndex( );
1096 max = DEFAULT_WHOWAS;
1097 if (Req->argc > 1) {
1098 max = atoi(Req->argv[1]);
1104 * Break up the nick argument into a list of nicks, if applicable
1105 * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1107 strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1108 nick = strtok(tok_buf, ",");
1110 for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1114 if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1115 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1116 return DISCONNECTED;
1120 /* previous entry */
1123 /* "underflow", wrap around */
1127 if (nc && count >= max)
1129 } while (i != last);
1131 if (nc == 0 && !IRC_WriteStrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1132 Client_ID(prefix), nick))
1133 return DISCONNECTED;
1135 return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG, Client_ID(prefix), Req->argv[0]);
1140 IRC_Send_LUSERS( CLIENT *Client )
1147 assert( Client != NULL );
1149 /* Users, services and serevers in the network */
1150 if( ! IRC_WriteStrClient( Client, RPL_LUSERCLIENT_MSG, Client_ID( Client ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
1152 /* Number of IRC operators */
1153 cnt = Client_OperCount( );
1156 if( ! IRC_WriteStrClient( Client, RPL_LUSEROP_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
1159 /* Unknown connections */
1160 cnt = Client_UnknownCount( );
1163 if( ! IRC_WriteStrClient( Client, RPL_LUSERUNKNOWN_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
1166 /* Number of created channels */
1167 if( ! IRC_WriteStrClient( Client, RPL_LUSERCHANNELS_MSG, Client_ID( Client ), Channel_Count( ))) return DISCONNECTED;
1169 /* Number of local users, services and servers */
1170 if( ! IRC_WriteStrClient( Client, RPL_LUSERME_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
1173 /* Maximum number of local users */
1174 cnt = Client_MyUserCount();
1175 max = Client_MyMaxUserCount();
1176 if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1177 cnt, max, cnt, max))
1178 return DISCONNECTED;
1179 /* Maximum number of users in the network */
1180 cnt = Client_UserCount();
1181 max = Client_MaxUserCount();
1182 if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1183 cnt, max, cnt, max))
1184 return DISCONNECTED;
1185 /* Connection counters */
1186 if (! IRC_WriteStrClient(Client, RPL_STATSCONN_MSG, Client_ID(Client),
1187 Conn_CountMax(), Conn_CountAccepted()))
1188 return DISCONNECTED;
1192 } /* IRC_Send_LUSERS */
1196 Show_MOTD_Start(CLIENT *Client)
1198 return IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG,
1199 Client_ID( Client ), Client_ID( Client_ThisServer( )));
1203 Show_MOTD_Sendline(CLIENT *Client, const char *msg)
1205 return IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID( Client ), msg);
1209 Show_MOTD_End(CLIENT *Client)
1211 return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
1215 static bool Show_MOTD_SSLInfo(CLIENT *Client)
1218 char buf[COMMAND_LEN] = "Connected using Cipher ";
1220 if (!Conn_GetCipherInfo(Client_Conn(Client), buf + 23, sizeof buf - 23))
1223 if (!Show_MOTD_Sendline(Client, buf))
1229 static inline bool Show_MOTD_SSLInfo(UNUSED CLIENT *c) { return true; }
1233 IRC_Show_MOTD( CLIENT *Client )
1238 assert( Client != NULL );
1240 if (Conf_MotdPhrase[0]) {
1241 if (!Show_MOTD_Start(Client))
1242 return DISCONNECTED;
1243 if (!Show_MOTD_Sendline(Client, Conf_MotdPhrase))
1244 return DISCONNECTED;
1248 fd = fopen( Conf_MotdFile, "r" );
1250 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
1251 if (Conn_UsesSSL(Client_Conn(Client))) {
1252 if (!Show_MOTD_Start(Client))
1253 return DISCONNECTED;
1256 return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
1259 if (!Show_MOTD_Start( Client )) {
1264 while (fgets( line, (int)sizeof line, fd )) {
1265 ngt_TrimLastChr( line, '\n');
1267 if( ! Show_MOTD_Sendline( Client, line)) {
1274 if (!Show_MOTD_SSLInfo(Client))
1275 return DISCONNECTED;
1276 return Show_MOTD_End(Client);
1277 } /* IRC_Show_MOTD */
1281 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
1283 bool is_visible, is_member;
1284 char str[LINE_LEN + 1];
1288 assert( Client != NULL );
1289 assert( Chan != NULL );
1291 if( Channel_IsMemberOf( Chan, Client )) is_member = true;
1292 else is_member = false;
1294 /* Secret channel? */
1295 if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
1297 /* Alle Mitglieder suchen */
1298 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1299 cl2chan = Channel_FirstMember( Chan );
1302 cl = Channel_GetClient( cl2chan );
1304 if( strchr( Client_Modes( cl ), 'i' )) is_visible = false;
1305 else is_visible = true;
1307 if( is_member || is_visible )
1309 /* Nick anhaengen */
1310 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
1311 if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
1312 else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
1313 strlcat( str, Client_ID( cl ), sizeof( str ));
1315 if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
1317 /* Zeile wird zu lang: senden! */
1318 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1319 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1323 /* naechstes Mitglied suchen */
1324 cl2chan = Channel_NextMember( Chan, cl2chan );
1326 if( str[strlen( str ) - 1] != ':')
1328 /* Es sind noch Daten da, die gesendet werden muessen */
1329 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1333 } /* IRC_Send_NAMES */
1338 * Send the ISUPPORT numeric (005).
1339 * This numeric indicates the features that are supported by this server.
1340 * See <http://www.irc.org/tech_docs/005.html> for details.
1343 IRC_Send_ISUPPORT PARAMS((CLIENT * Client))
1345 if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1347 return DISCONNECTED;
1348 return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1349 CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1350 COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1352 } /* IRC_Send_ISUPPORT */