2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2008 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"
38 #include "irc-write.h"
45 IRC_ADMIN(CLIENT *Client, REQUEST *Req )
47 CLIENT *target, *prefix;
49 assert( Client != NULL );
50 assert( Req != NULL );
52 /* Falsche Anzahl Parameter? */
53 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
56 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
57 else target = Client_ThisServer( );
59 /* Prefix ermitteln */
60 if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
62 if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
64 /* An anderen Server weiterleiten? */
65 if( target != Client_ThisServer( ))
67 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
70 IRC_WriteStrClientPrefix( target, prefix, "ADMIN %s", Req->argv[0] );
74 /* mit Versionsinfo antworten */
75 if( ! IRC_WriteStrClient( Client, RPL_ADMINME_MSG, Client_ID( prefix ), Conf_ServerName )) return DISCONNECTED;
76 if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC1_MSG, Client_ID( prefix ), Conf_ServerAdmin1 )) return DISCONNECTED;
77 if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC2_MSG, Client_ID( prefix ), Conf_ServerAdmin2 )) return DISCONNECTED;
78 if( ! IRC_WriteStrClient( Client, RPL_ADMINEMAIL_MSG, Client_ID( prefix ), Conf_ServerAdminMail )) return DISCONNECTED;
80 IRC_SetPenalty( Client, 1 );
86 * Handler for the IRC command "INFO".
87 * See RFC 2812 section 3.4.10.
90 IRC_INFO(CLIENT * Client, REQUEST * Req)
92 CLIENT *target, *prefix;
95 assert(Client != NULL);
98 /* Wrong number of parameters? */
100 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
101 Client_ID(Client), Req->command);
103 /* Determine prefix */
104 if (Client_Type(Client) == CLIENT_SERVER)
105 prefix = Client_Search(Req->prefix);
109 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
110 Client_ID(Client), Req->prefix);
112 /* Look for a target */
114 target = Client_Search(Req->argv[0]);
116 target = Client_ThisServer();
118 /* Make sure that the target is a server */
119 if (target && Client_Type(target) != CLIENT_SERVER)
120 target = Client_Introducer(target);
123 return IRC_WriteStrClient(prefix, ERR_NOSUCHSERVER_MSG,
124 Client_ID(prefix), Req->argv[0]);
126 /* Pass on to another server? */
127 if (target != Client_ThisServer()) {
128 IRC_WriteStrClientPrefix(target, prefix, "INFO %s",
133 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix),
137 strlcpy(msg, "Server has been started ", sizeof(msg));
138 strlcat(msg, NGIRCd_StartStr, sizeof(msg));
139 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
142 if (!IRC_WriteStrClient(Client, RPL_ENDOFINFO_MSG, Client_ID(prefix)))
145 IRC_SetPenalty(Client, 2);
151 IRC_ISON( CLIENT *Client, REQUEST *Req )
153 char rpl[COMMAND_LEN];
158 assert( Client != NULL );
159 assert( Req != NULL );
161 /* Falsche Anzahl Parameter? */
162 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
164 strlcpy( rpl, RPL_ISON_MSG, sizeof rpl );
165 for( i = 0; i < Req->argc; i++ )
167 ptr = strtok( Req->argv[i], " " );
171 c = Client_Search( ptr );
172 if( c && ( Client_Type( c ) == CLIENT_USER ))
174 /* Dieser Nick ist "online" */
175 strlcat( rpl, ptr, sizeof( rpl ));
176 strlcat( rpl, " ", sizeof( rpl ));
178 ptr = strtok( NULL, " " );
181 ngt_TrimLastChr(rpl, ' ');
183 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
188 IRC_LINKS( CLIENT *Client, REQUEST *Req )
190 CLIENT *target, *from, *c;
193 assert( Client != NULL );
194 assert( Req != NULL );
196 /* Falsche Anzahl Parameter? */
197 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
199 /* Server-Mask ermitteln */
200 if( Req->argc > 0 ) mask = Req->argv[Req->argc - 1];
203 /* Absender ermitteln */
204 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
206 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
208 /* An anderen Server forwarden? */
211 target = Client_Search( Req->argv[0] );
212 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
213 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LINKS %s %s", Req->argv[0], Req->argv[1] );
216 /* Wer ist der Absender? */
217 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
218 else target = Client;
219 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
224 if( Client_Type( c ) == CLIENT_SERVER )
226 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;
228 c = Client_Next( c );
231 IRC_SetPenalty( target, 1 );
232 return IRC_WriteStrClient( target, RPL_ENDOFLINKS_MSG, Client_ID( target ), mask );
237 IRC_LUSERS( CLIENT *Client, REQUEST *Req )
239 CLIENT *target, *from;
241 assert( Client != NULL );
242 assert( Req != NULL );
244 /* Falsche Anzahl Parameter? */
245 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
247 /* Absender ermitteln */
248 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
250 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
252 /* An anderen Server forwarden? */
255 target = Client_Search( Req->argv[1] );
256 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
257 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LUSERS %s %s", Req->argv[0], Req->argv[1] );
260 /* Wer ist der Absender? */
261 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
262 else target = Client;
263 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
265 IRC_Send_LUSERS( target );
267 IRC_SetPenalty( target, 1 );
273 * Handler for the IRC command "SERVLIST".
274 * List registered services, see RFC 2811, section 3.5.1: the syntax is
275 * "SERVLIST [<mask> [<type>]]".
278 IRC_SERVLIST(CLIENT *Client, REQUEST *Req)
282 assert(Client != NULL);
286 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
287 Client_ID(Client), Req->command);
289 if (Req->argc < 2 || strcmp(Req->argv[1], "0") == 0) {
290 for (c = Client_First(); c!= NULL; c = Client_Next(c)) {
291 if (Client_Type(c) != CLIENT_SERVICE)
293 if (Req->argc > 0 && !MatchCaseInsensitive(Req->argv[0],
296 if (!IRC_WriteStrClient(Client, RPL_SERVLIST_MSG,
297 Client_ID(Client), Client_Mask(c),
298 Client_Mask(Client_Introducer(c)), "*",
299 0, Client_Hops(c), Client_Info(c)))
304 return IRC_WriteStrClient(Client, RPL_SERVLISTEND_MSG, Client_ID(Client),
305 Req->argc > 0 ? Req->argv[0] : "*",
306 Req->argc > 1 ? Req->argv[1] : "0");
311 IRC_MOTD( CLIENT *Client, REQUEST *Req )
313 CLIENT *from, *target;
315 assert( Client != NULL );
316 assert( Req != NULL );
318 /* Falsche Anzahl Parameter? */
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 );
328 /* an anderen Server forwarden */
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 /* Falsche Anzahl Parameter? */
355 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
357 /* From aus Prefix ermitteln */
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 /* an anderen Server forwarden */
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( ))
370 /* Ok, anderer Server ist das Ziel: forwarden */
371 return IRC_WriteStrClientPrefix( target, from, "NAMES %s :%s", Req->argv[0], Req->argv[1] );
377 /* bestimmte Channels durchgehen */
378 ptr = strtok( Req->argv[0], "," );
381 chan = Channel_Search( ptr );
385 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
387 if( ! IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), ptr )) return DISCONNECTED;
389 /* naechsten Namen ermitteln */
390 ptr = strtok( NULL, "," );
395 /* alle Channels durchgehen */
396 chan = Channel_First( );
400 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
402 /* naechster Channel */
403 chan = Channel_Next( chan );
406 /* Nun noch alle Clients ausgeben, die in keinem Channel sind */
408 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
411 if(( Client_Type( c ) == CLIENT_USER ) && ( Channel_FirstChannelOf( c ) == NULL ) && ( ! strchr( Client_Modes( c ), 'i' )))
413 /* Okay, das ist ein User: anhaengen */
414 if( rpl[strlen( rpl ) - 1] != ':' ) strlcat( rpl, " ", sizeof( rpl ));
415 strlcat( rpl, Client_ID( c ), sizeof( rpl ));
417 if( strlen( rpl ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
419 /* Zeile wird zu lang: senden! */
420 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
421 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
425 /* naechster Client */
426 c = Client_Next( c );
428 if( rpl[strlen( rpl ) - 1] != ':')
430 /* es wurden User gefunden */
431 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
434 IRC_SetPenalty( from, 1 );
435 return IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), "*" );
440 t_diff(time_t *t, const time_t d)
453 uptime_days(time_t *now)
455 return t_diff(now, 60 * 60 * 24);
460 uptime_hrs(time_t *now)
462 return t_diff(now, 60 * 60);
467 uptime_mins(time_t *now)
469 return t_diff(now, 60);
474 IRC_STATS( CLIENT *Client, REQUEST *Req )
476 CLIENT *from, *target, *cl;
481 unsigned int days, hrs, mins;
483 assert( Client != NULL );
484 assert( Req != NULL );
486 /* Falsche Anzahl Parameter? */
488 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
490 /* From aus Prefix ermitteln */
491 if (Client_Type(Client) == CLIENT_SERVER)
492 from = Client_Search(Req->prefix);
497 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix);
499 if (Req->argc == 2) {
500 /* an anderen Server forwarden */
501 target = Client_Search( Req->argv[1] );
502 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
503 return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
505 if( target != Client_ThisServer()) {
506 /* Ok, anderer Server ist das Ziel: forwarden */
507 return IRC_WriteStrClientPrefix( target, from, "STATS %s %s", Req->argv[0], Req->argv[1] );
512 query = Req->argv[0][0] ? Req->argv[0][0] : '*';
517 case 'l': /* Links */
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) || (cl == Client)) {
525 /* Server link or our own connection */
527 if (Conn_Options(con) & CONN_ZIP) {
528 if (!IRC_WriteStrClient(from, RPL_STATSLINKINFOZIP_MSG,
529 Client_ID(from), Client_Mask(cl), Conn_SendQ(con),
530 Conn_SendMsg(con), Zip_SendBytes(con), Conn_SendBytes(con),
531 Conn_RecvMsg(con), Zip_RecvBytes(con), Conn_RecvBytes(con), (long)(time_now - Conn_StartTime(con))))
536 if (!IRC_WriteStrClient(from, RPL_STATSLINKINFO_MSG, Client_ID(from),
537 Client_Mask(cl), Conn_SendQ(con), Conn_SendMsg(con), Conn_SendBytes(con),
538 Conn_RecvMsg(con), Conn_RecvBytes(con), (long)(time_now - Conn_StartTime(con))))
543 case 'm': /* IRC-Commands */
545 cmd = Parse_GetCommandStruct( );
546 for (; cmd->name ; cmd++) {
547 if (cmd->lcount == 0 && cmd->rcount == 0)
549 if (!IRC_WriteStrClient(from, RPL_STATSCOMMANDS_MSG, Client_ID(from),
550 cmd->name, cmd->lcount, cmd->bytes, cmd->rcount))
554 case 'u': /* server uptime */
556 time_now = time(NULL) - NGIRCd_Start;
557 days = uptime_days(&time_now);
558 hrs = uptime_hrs(&time_now);
559 mins = uptime_mins(&time_now);
560 if (!IRC_WriteStrClient(from, RPL_STATSUPTIME, Client_ID(from),
561 days, hrs, mins, (unsigned int) time_now))
566 IRC_SetPenalty(from, 2);
567 return IRC_WriteStrClient(from, RPL_ENDOFSTATS_MSG, Client_ID(from), query);
572 * Handler for the IRC command "SUMMON".
573 * See RFC 2812 section 4.5. ngIRCd doesn't implement this functionality and
574 * therefore answers with ERR_SUMMONDISABLED.
577 IRC_SUMMON(CLIENT * Client, REQUEST * Req)
579 return IRC_WriteStrClient(Client, ERR_SUMMONDISABLED_MSG,
580 Client_ID(Client), Req->command);
585 IRC_TIME( CLIENT *Client, REQUEST *Req )
587 CLIENT *from, *target;
591 assert( Client != NULL );
592 assert( Req != NULL );
594 /* Falsche Anzahl Parameter? */
595 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
597 /* From aus Prefix ermitteln */
598 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
600 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
604 /* an anderen Server forwarden */
605 target = Client_Search( Req->argv[0] );
606 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
608 if( target != Client_ThisServer( ))
610 /* Ok, anderer Server ist das Ziel: forwarden */
611 return IRC_WriteStrClientPrefix( target, from, "TIME %s", Req->argv[0] );
616 (void)strftime( t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime( &t ));
617 return IRC_WriteStrClient( from, RPL_TIME_MSG, Client_ID( from ), Client_ID( Client_ThisServer( )), t_str );
622 IRC_USERHOST( CLIENT *Client, REQUEST *Req )
624 char rpl[COMMAND_LEN];
628 assert( Client != NULL );
629 assert( Req != NULL );
631 /* Falsche Anzahl Parameter? */
632 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
634 if( Req->argc > 5 ) max = 5;
635 else max = Req->argc;
637 strlcpy( rpl, RPL_USERHOST_MSG, sizeof rpl );
638 for( i = 0; i < max; i++ )
640 c = Client_Search( Req->argv[i] );
641 if( c && ( Client_Type( c ) == CLIENT_USER ))
643 /* Dieser Nick ist "online" */
644 strlcat( rpl, Client_ID( c ), sizeof( rpl ));
645 if( Client_HasMode( c, 'o' )) strlcat( rpl, "*", sizeof( rpl ));
646 strlcat( rpl, "=", sizeof( rpl ));
647 if( Client_HasMode( c, 'a' )) strlcat( rpl, "-", sizeof( rpl ));
648 else strlcat( rpl, "+", sizeof( rpl ));
649 strlcat( rpl, Client_User( c ), sizeof( rpl ));
650 strlcat( rpl, "@", sizeof( rpl ));
651 strlcat( rpl, Client_Hostname( c ), sizeof( rpl ));
652 strlcat( rpl, " ", sizeof( rpl ));
655 ngt_TrimLastChr( rpl, ' ');
657 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
662 * Handler for the IRC command "USERS".
663 * See RFC 2812 section 4.6. As suggested there the command is disabled.
666 IRC_USERS(CLIENT * Client, REQUEST * Req)
668 return IRC_WriteStrClient(Client, ERR_USERSDISABLED_MSG,
669 Client_ID(Client), Req->command);
674 IRC_VERSION( CLIENT *Client, REQUEST *Req )
676 CLIENT *target, *prefix;
678 assert( Client != NULL );
679 assert( Req != NULL );
681 /* Falsche Anzahl Parameter? */
682 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
685 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
686 else target = Client_ThisServer( );
688 /* Prefix ermitteln */
689 if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
690 else prefix = Client;
691 if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
693 /* An anderen Server weiterleiten? */
694 if( target != Client_ThisServer( ))
696 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
699 IRC_WriteStrClientPrefix( target, prefix, "VERSION %s", Req->argv[0] );
703 /* send version information */
704 IRC_SetPenalty(Client, 1);
705 return IRC_WriteStrClient(Client, RPL_VERSION_MSG, Client_ID(prefix),
706 PACKAGE_NAME, PACKAGE_VERSION,
707 NGIRCd_DebugLevel, Conf_ServerName,
708 NGIRCd_VersionAddition);
713 write_whoreply(CLIENT *Client, CLIENT *c, const char *channelname, const char *flags)
715 return IRC_WriteStrClient(Client, RPL_WHOREPLY_MSG, Client_ID(Client), channelname,
716 Client_User(c), Client_Hostname(c), Client_ID(Client_Introducer(c)), Client_ID(c),
717 flags, Client_Hops(c), Client_Info(c));
722 who_flags_status(const char *client_modes)
724 if (strchr(client_modes, 'a'))
725 return "G"; /* away */
731 who_flags_qualifier(const char *chan_user_modes)
733 if (strchr(chan_user_modes, 'o'))
735 else if (strchr(chan_user_modes, 'v'))
742 IRC_Send_WHO(CLIENT *Client, CHANNEL *Chan, bool OnlyOps)
744 bool is_visible, is_member, is_ircop;
746 const char *client_modes;
747 const char *chan_user_modes;
751 assert( Client != NULL );
752 assert( Chan != NULL );
754 is_member = Channel_IsMemberOf(Chan, Client);
756 /* Secret channel? */
757 if (!is_member && strchr(Channel_Modes(Chan), 's'))
758 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), Channel_Name(Chan));
760 cl2chan = Channel_FirstMember(Chan);
761 for (; cl2chan ; cl2chan = Channel_NextMember(Chan, cl2chan)) {
762 c = Channel_GetClient(cl2chan);
764 client_modes = Client_Modes(c);
765 is_ircop = strchr(client_modes, 'o') != NULL;
766 if (OnlyOps && !is_ircop)
769 is_visible = strchr(client_modes, 'i') == NULL;
770 if (is_member || is_visible) {
771 strcpy(flags, who_flags_status(client_modes));
773 strlcat(flags, "*", sizeof(flags));
775 chan_user_modes = Channel_UserModes(Chan, c);
776 strlcat(flags, who_flags_qualifier(chan_user_modes), sizeof(flags));
778 if (!write_whoreply(Client, c, Channel_Name(Chan), flags))
782 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), Channel_Name(Chan));
787 IRC_WHO( CLIENT *Client, REQUEST *Req )
789 bool only_ops, have_arg, client_match;
790 const char *channelname, *client_modes, *chan_user_modes;
791 char pattern[COMMAND_LEN];
797 assert( Client != NULL );
798 assert( Req != NULL );
801 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
806 if (Req->argc == 2) {
807 if (strcmp(Req->argv[1], "o") == 0)
810 else return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
814 IRC_SetPenalty(Client, 1);
815 if (Req->argc >= 1) { /* Channel or Mask. */
816 chan = Channel_Search(Req->argv[0]);
818 return IRC_Send_WHO(Client, chan, only_ops);
819 if (strcmp(Req->argv[0], "0") != 0) { /* RFC stupidity, same as no arguments */
821 strlcpy(pattern, Req->argv[0], sizeof(pattern));
822 ngt_LowerStr(pattern);
823 IRC_SetPenalty(Client, 3);
827 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
828 if (Client_Type(c) != CLIENT_USER)
832 * In the absence of the parameter, all visible (users who aren't
833 * invisible (user mode +i) and who don't have a common channel
834 * with the requesting client) are listed.
836 * The same result can be achieved by using a [sic] of "0"
837 * or any wildcard which will end up matching every visible user.
839 * The [sic] passed to WHO is matched against users' host, server, real name and
840 * nickname if the channel cannot be found.
842 client_modes = Client_Modes(c);
843 if (strchr(client_modes, 'i'))
846 if (only_ops && !strchr(client_modes, 'o'))
849 if (have_arg) { /* match pattern against user host/server/name/nick */
850 client_match = MatchCaseInsensitive(pattern, Client_Hostname(c)); /* user's host */
852 client_match = MatchCaseInsensitive(pattern, Client_ID(Client_Introducer(c))); /* server */
854 client_match = Match(Req->argv[0], Client_Info(c)); /* realname */
856 client_match = MatchCaseInsensitive(pattern, Client_ID(c)); /* nick name */
858 if (!client_match) /* This isn't the client you're looking for */
862 strcpy(flags, who_flags_status(client_modes));
864 if (strchr(client_modes, 'o')) /* this client is an operator */
865 strlcat(flags, "*", sizeof(flags));
867 /* Search suitable channel */
868 cl2chan = Channel_FirstChannelOf(c);
870 cn = Channel_GetChannel(cl2chan);
871 if (Channel_IsMemberOf(cn, Client) ||
872 !strchr(Channel_Modes(cn), 's'))
874 channelname = Channel_Name(cn);
877 cl2chan = Channel_NextChannelOf(c, cl2chan);
880 chan = Channel_GetChannel(cl2chan);
881 chan_user_modes = Channel_UserModes(chan, c);
882 strlcat(flags, who_flags_qualifier(chan_user_modes), sizeof(flags));
886 if (!write_whoreply(Client, c, channelname, flags))
891 channelname = Req->argv[0];
895 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), channelname);
900 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
902 CLIENT *from, *target, *c;
903 char str[LINE_LEN + 1];
907 assert( Client != NULL );
908 assert( Req != NULL );
910 /* Bad number of parameters? */
911 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
914 c = Client_Search( Req->argv[Req->argc - 1] );
915 if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[Req->argc - 1] );
917 /* Search sender of the WHOIS */
918 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
920 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
922 /* Forward to other server? */
925 /* Search target server (can be specified as nick of that server!) */
926 target = Client_Search( Req->argv[0] );
927 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
929 else target = Client_ThisServer( );
931 assert( target != NULL );
933 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] );
935 /* Nick, user and name */
936 if( ! IRC_WriteStrClient( from, RPL_WHOISUSER_MSG, Client_ID( from ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
939 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;
942 snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
943 cl2chan = Channel_FirstChannelOf( c );
946 chan = Channel_GetChannel( cl2chan );
947 assert( chan != NULL );
950 cl2chan = Channel_NextChannelOf( c, cl2chan );
952 /* Secret channel? */
953 if (strchr(Channel_Modes(chan), 's')
954 && !Channel_IsMemberOf(chan, Client))
957 /* Local channel and request is not from a user? */
958 if (Client_Type(Client) == CLIENT_SERVER
959 && Channel_IsLocal(chan))
962 /* Concatenate channel names */
963 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
964 if( strchr( Channel_UserModes( chan, c ), 'o' )) strlcat( str, "@", sizeof( str ));
965 else if( strchr( Channel_UserModes( chan, c ), 'v' )) strlcat( str, "+", sizeof( str ));
966 strlcat( str, Channel_Name( chan ), sizeof( str ));
968 if( strlen( str ) > ( LINE_LEN - CHANNEL_NAME_LEN - 4 ))
970 /* Line becomes too long: send it! */
971 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
972 snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
975 if( str[strlen( str ) - 1] != ':')
977 /* There is data left to send: */
978 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
982 if( Client_HasMode( c, 'o' ))
984 if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
987 /* Idle and signon time (local clients only!) */
988 if (Client_Conn(c) > NONE ) {
989 if (! IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
990 Client_ID(from), Client_ID(c),
991 (unsigned long)Conn_GetIdle(Client_Conn(c)),
992 (unsigned long)Conn_GetSignon(Client_Conn(c))))
997 if( Client_HasMode( c, 'a' ))
999 if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( c ), Client_Away( c ))) return DISCONNECTED;
1003 return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
1008 WHOWAS_EntryWrite(CLIENT *prefix, WHOWAS *entry)
1012 (void)strftime(t_str, sizeof(t_str), "%a %b %d %H:%M:%S %Y",
1013 localtime(&entry->time));
1015 if (!IRC_WriteStrClient(prefix, RPL_WHOWASUSER_MSG, Client_ID(prefix),
1016 entry->id, entry->user, entry->host, entry->info))
1017 return DISCONNECTED;
1019 return IRC_WriteStrClient(prefix, RPL_WHOISSERVER_MSG, Client_ID(prefix),
1020 entry->id, entry->server, t_str);
1024 * IRC "WHOWAS" function.
1025 * This function implements the IRC command "WHOWHAS". It handles local
1026 * requests and request that should be forwarded to other servers.
1029 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1031 CLIENT *target, *prefix;
1033 char tok_buf[COMMAND_LEN];
1034 int max, last, count, i, nc;
1037 assert( Client != NULL );
1038 assert( Req != NULL );
1040 /* Wrong number of parameters? */
1042 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1043 Client_ID(Client), Req->command);
1045 return IRC_WriteStrClient(Client, ERR_NONICKNAMEGIVEN_MSG, Client_ID(Client));
1049 target = Client_Search(Req->argv[2]);
1051 target = Client_ThisServer();
1054 if (Client_Type(Client) == CLIENT_SERVER)
1055 prefix = Client_Search(Req->prefix);
1060 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1061 Client_ID(Client), Req->prefix);
1063 /* Forward to other server? */
1064 if (target != Client_ThisServer()) {
1065 if (!target || (Client_Type(target) != CLIENT_SERVER))
1066 return IRC_WriteStrClient(prefix, ERR_NOSUCHSERVER_MSG,
1067 Client_ID(prefix), Req->argv[2]);
1070 IRC_WriteStrClientPrefix( target, prefix, "WHOWAS %s %s %s",
1071 Req->argv[0], Req->argv[1],
1076 whowas = Client_GetWhowas( );
1077 last = Client_GetLastWhowasIndex( );
1081 max = DEFAULT_WHOWAS;
1082 if (Req->argc > 1) {
1083 max = atoi(Req->argv[1]);
1089 * Break up the nick argument into a list of nicks, if applicable
1090 * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1092 strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1093 nick = strtok(tok_buf, ",");
1095 for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1099 if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1100 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1101 return DISCONNECTED;
1105 /* previous entry */
1108 /* "underflow", wrap around */
1112 if (nc && count >= max)
1114 } while (i != last);
1116 if (nc == 0 && !IRC_WriteStrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1117 Client_ID(prefix), nick))
1118 return DISCONNECTED;
1120 return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG, Client_ID(prefix), Req->argv[0]);
1125 IRC_Send_LUSERS( CLIENT *Client )
1132 assert( Client != NULL );
1134 /* Users, services and serevers in the network */
1135 if( ! IRC_WriteStrClient( Client, RPL_LUSERCLIENT_MSG, Client_ID( Client ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
1137 /* Number of IRC operators */
1138 cnt = Client_OperCount( );
1141 if( ! IRC_WriteStrClient( Client, RPL_LUSEROP_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
1144 /* Unknown connections */
1145 cnt = Client_UnknownCount( );
1148 if( ! IRC_WriteStrClient( Client, RPL_LUSERUNKNOWN_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
1151 /* Number of created channels */
1152 if( ! IRC_WriteStrClient( Client, RPL_LUSERCHANNELS_MSG, Client_ID( Client ), Channel_Count( ))) return DISCONNECTED;
1154 /* Number of local users, services and servers */
1155 if( ! IRC_WriteStrClient( Client, RPL_LUSERME_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
1158 /* Maximum number of local users */
1159 cnt = Client_MyUserCount();
1160 max = Client_MyMaxUserCount();
1161 if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1162 cnt, max, cnt, max))
1163 return DISCONNECTED;
1164 /* Maximum number of users in the network */
1165 cnt = Client_UserCount();
1166 max = Client_MaxUserCount();
1167 if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1168 cnt, max, cnt, max))
1169 return DISCONNECTED;
1173 } /* IRC_Send_LUSERS */
1177 Show_MOTD_Start(CLIENT *Client)
1179 return IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG,
1180 Client_ID( Client ), Client_ID( Client_ThisServer( )));
1184 Show_MOTD_Sendline(CLIENT *Client, const char *msg)
1186 return IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID( Client ), msg);
1190 Show_MOTD_End(CLIENT *Client)
1192 return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
1196 static bool Show_MOTD_SSLInfo(CLIENT *Client)
1199 char buf[COMMAND_LEN] = "Connected using Cipher ";
1201 if (!Conn_GetCipherInfo(Client_Conn(Client), buf + 23, sizeof buf - 23))
1204 if (!Show_MOTD_Sendline(Client, buf))
1210 static inline bool Show_MOTD_SSLInfo(UNUSED CLIENT *c) { return true; }
1214 IRC_Show_MOTD( CLIENT *Client )
1219 assert( Client != NULL );
1221 if (Conf_MotdPhrase[0]) {
1222 if (!Show_MOTD_Start(Client))
1223 return DISCONNECTED;
1224 if (!Show_MOTD_Sendline(Client, Conf_MotdPhrase))
1225 return DISCONNECTED;
1229 fd = fopen( Conf_MotdFile, "r" );
1231 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
1232 if (Conn_UsesSSL(Client_Conn(Client))) {
1233 if (!Show_MOTD_Start(Client))
1234 return DISCONNECTED;
1237 return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
1240 if (!Show_MOTD_Start( Client )) {
1245 while (fgets( line, (int)sizeof line, fd )) {
1246 ngt_TrimLastChr( line, '\n');
1248 if( ! Show_MOTD_Sendline( Client, line)) {
1255 if (!Show_MOTD_SSLInfo(Client))
1256 return DISCONNECTED;
1257 return Show_MOTD_End(Client);
1258 } /* IRC_Show_MOTD */
1262 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
1264 bool is_visible, is_member;
1265 char str[LINE_LEN + 1];
1269 assert( Client != NULL );
1270 assert( Chan != NULL );
1272 if( Channel_IsMemberOf( Chan, Client )) is_member = true;
1273 else is_member = false;
1275 /* Secret channel? */
1276 if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
1278 /* Alle Mitglieder suchen */
1279 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1280 cl2chan = Channel_FirstMember( Chan );
1283 cl = Channel_GetClient( cl2chan );
1285 if( strchr( Client_Modes( cl ), 'i' )) is_visible = false;
1286 else is_visible = true;
1288 if( is_member || is_visible )
1290 /* Nick anhaengen */
1291 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
1292 if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
1293 else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
1294 strlcat( str, Client_ID( cl ), sizeof( str ));
1296 if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
1298 /* Zeile wird zu lang: senden! */
1299 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1300 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1304 /* naechstes Mitglied suchen */
1305 cl2chan = Channel_NextMember( Chan, cl2chan );
1307 if( str[strlen( str ) - 1] != ':')
1309 /* Es sind noch Daten da, die gesendet werden muessen */
1310 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1314 } /* IRC_Send_NAMES */
1319 * Send the ISUPPORT numeric (005).
1320 * This numeric indicates the features that are supported by this server.
1321 * See <http://www.irc.org/tech_docs/005.html> for details.
1324 IRC_Send_ISUPPORT PARAMS((CLIENT * Client))
1326 if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1328 return DISCONNECTED;
1329 return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1330 CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1331 COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1333 } /* IRC_Send_ISUPPORT */