2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2011 Alexander Barton (alex@barton.de) and Contributors.
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.
28 #include "conn-func.h"
40 #include "irc-write.h"
47 IRC_ADMIN(CLIENT *Client, REQUEST *Req )
49 CLIENT *target, *prefix;
51 assert( Client != NULL );
52 assert( Req != NULL );
54 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
57 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
58 else target = Client_ThisServer( );
61 if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
63 if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
65 /* forwad message to another server? */
66 if( target != Client_ThisServer( ))
68 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
71 IRC_WriteStrClientPrefix( target, prefix, "ADMIN %s", Req->argv[0] );
75 /* mit Versionsinfo antworten */
76 if( ! IRC_WriteStrClient( Client, RPL_ADMINME_MSG, Client_ID( prefix ), Conf_ServerName )) return DISCONNECTED;
77 if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC1_MSG, Client_ID( prefix ), Conf_ServerAdmin1 )) return DISCONNECTED;
78 if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC2_MSG, Client_ID( prefix ), Conf_ServerAdmin2 )) return DISCONNECTED;
79 if( ! IRC_WriteStrClient( Client, RPL_ADMINEMAIL_MSG, Client_ID( prefix ), Conf_ServerAdminMail )) return DISCONNECTED;
81 IRC_SetPenalty( Client, 1 );
87 * Handler for the IRC command "INFO".
88 * See RFC 2812 section 3.4.10.
91 IRC_INFO(CLIENT * Client, REQUEST * Req)
93 CLIENT *target, *prefix;
96 assert(Client != NULL);
99 /* Wrong number of parameters? */
101 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
102 Client_ID(Client), Req->command);
104 /* Determine prefix */
105 if (Client_Type(Client) == CLIENT_SERVER)
106 prefix = Client_Search(Req->prefix);
110 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
111 Client_ID(Client), Req->prefix);
113 /* Look for a target */
115 target = Client_Search(Req->argv[0]);
117 target = Client_ThisServer();
119 /* Make sure that the target is a server */
120 if (target && Client_Type(target) != CLIENT_SERVER)
121 target = Client_Introducer(target);
124 return IRC_WriteStrClient(prefix, ERR_NOSUCHSERVER_MSG,
125 Client_ID(prefix), Req->argv[0]);
127 /* Pass on to another server? */
128 if (target != Client_ThisServer()) {
129 IRC_WriteStrClientPrefix(target, prefix, "INFO %s",
134 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix),
138 #if defined(__DATE__) && defined(__TIME__)
139 snprintf(msg, sizeof(msg), "Birth Date: %s at %s", __DATE__, __TIME__);
140 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
144 strlcpy(msg, "On-line since ", sizeof(msg));
145 strlcat(msg, NGIRCd_StartStr, sizeof(msg));
146 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
149 if (!IRC_WriteStrClient(Client, RPL_ENDOFINFO_MSG, Client_ID(prefix)))
152 IRC_SetPenalty(Client, 2);
158 IRC_ISON( CLIENT *Client, REQUEST *Req )
160 char rpl[COMMAND_LEN];
165 assert( Client != NULL );
166 assert( Req != NULL );
168 /* Falsche Anzahl Parameter? */
169 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
171 strlcpy( rpl, RPL_ISON_MSG, sizeof rpl );
172 for( i = 0; i < Req->argc; i++ )
174 ptr = strtok( Req->argv[i], " " );
178 c = Client_Search( ptr );
179 if( c && ( Client_Type( c ) == CLIENT_USER ))
181 /* Dieser Nick ist "online" */
182 strlcat( rpl, ptr, sizeof( rpl ));
183 strlcat( rpl, " ", sizeof( rpl ));
185 ptr = strtok( NULL, " " );
188 ngt_TrimLastChr(rpl, ' ');
190 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
195 IRC_LINKS( CLIENT *Client, REQUEST *Req )
197 CLIENT *target, *from, *c;
200 assert( Client != NULL );
201 assert( Req != NULL );
203 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
205 /* Server-Mask ermitteln */
206 if( Req->argc > 0 ) mask = Req->argv[Req->argc - 1];
209 /* Absender ermitteln */
210 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
212 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
214 /* An anderen Server forwarden? */
217 target = Client_Search( Req->argv[0] );
218 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
219 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LINKS %s %s", Req->argv[0], Req->argv[1] );
222 /* Wer ist der Absender? */
223 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
224 else target = Client;
225 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
230 if( Client_Type( c ) == CLIENT_SERVER )
232 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;
234 c = Client_Next( c );
237 IRC_SetPenalty( target, 1 );
238 return IRC_WriteStrClient( target, RPL_ENDOFLINKS_MSG, Client_ID( target ), mask );
243 IRC_LUSERS( CLIENT *Client, REQUEST *Req )
245 CLIENT *target, *from;
247 assert( Client != NULL );
248 assert( Req != NULL );
250 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
252 /* Absender ermitteln */
253 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
255 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
257 /* An anderen Server forwarden? */
260 target = Client_Search( Req->argv[1] );
261 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
262 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LUSERS %s %s", Req->argv[0], Req->argv[1] );
265 /* Wer ist der Absender? */
266 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
267 else target = Client;
268 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
270 IRC_Send_LUSERS( target );
272 IRC_SetPenalty( target, 1 );
278 * Handler for the IRC command "SERVLIST".
279 * List registered services, see RFC 2811, section 3.5.1: the syntax is
280 * "SERVLIST [<mask> [<type>]]".
283 IRC_SERVLIST(CLIENT *Client, REQUEST *Req)
287 assert(Client != NULL);
291 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
292 Client_ID(Client), Req->command);
294 if (Req->argc < 2 || strcmp(Req->argv[1], "0") == 0) {
295 for (c = Client_First(); c!= NULL; c = Client_Next(c)) {
296 if (Client_Type(c) != CLIENT_SERVICE)
298 if (Req->argc > 0 && !MatchCaseInsensitive(Req->argv[0],
301 if (!IRC_WriteStrClient(Client, RPL_SERVLIST_MSG,
302 Client_ID(Client), Client_Mask(c),
303 Client_Mask(Client_Introducer(c)), "*",
304 0, Client_Hops(c), Client_Info(c)))
309 return IRC_WriteStrClient(Client, RPL_SERVLISTEND_MSG, Client_ID(Client),
310 Req->argc > 0 ? Req->argv[0] : "*",
311 Req->argc > 1 ? Req->argv[1] : "0");
316 IRC_MOTD( CLIENT *Client, REQUEST *Req )
318 CLIENT *from, *target;
320 assert( Client != NULL );
321 assert( Req != NULL );
323 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
325 /* From aus Prefix ermitteln */
326 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
328 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
333 target = Client_Search( Req->argv[0] );
334 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
336 if( target != Client_ThisServer( ))
338 /* Ok, anderer Server ist das Ziel: forwarden */
339 return IRC_WriteStrClientPrefix( target, from, "MOTD %s", Req->argv[0] );
343 IRC_SetPenalty( from, 3 );
344 return IRC_Show_MOTD( from );
349 IRC_NAMES( CLIENT *Client, REQUEST *Req )
351 char rpl[COMMAND_LEN], *ptr;
352 CLIENT *target, *from, *c;
355 assert( Client != NULL );
356 assert( Req != NULL );
358 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
360 /* use prefix to determine "From" */
361 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
363 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
367 /* forward to another server? */
368 target = Client_Search( Req->argv[1] );
369 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
371 if( target != Client_ThisServer( )) {
372 /* target is another server, forward */
373 return IRC_WriteStrClientPrefix( target, from, "NAMES %s :%s", Req->argv[0], Req->argv[1] );
379 /* bestimmte Channels durchgehen */
380 ptr = strtok( Req->argv[0], "," );
383 chan = Channel_Search( ptr );
387 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
389 if( ! IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), ptr )) return DISCONNECTED;
391 /* get next channel name */
392 ptr = strtok( NULL, "," );
397 chan = Channel_First( );
400 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
402 chan = Channel_Next( chan );
405 /* Now print all clients which are not in any channel */
407 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
410 if(( Client_Type( c ) == CLIENT_USER ) && ( Channel_FirstChannelOf( c ) == NULL ) && ( ! strchr( Client_Modes( c ), 'i' )))
412 /* its a user, concatenate ... */
413 if( rpl[strlen( rpl ) - 1] != ':' ) strlcat( rpl, " ", sizeof( rpl ));
414 strlcat( rpl, Client_ID( c ), sizeof( rpl ));
416 if( strlen( rpl ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
418 /* Line is gwoing too long, send now */
419 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
420 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
424 c = Client_Next( c );
426 if( rpl[strlen( rpl ) - 1] != ':')
428 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
431 IRC_SetPenalty( from, 1 );
432 return IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), "*" );
437 t_diff(time_t *t, const time_t d)
445 return (unsigned int)diff;
450 uptime_days(time_t *now)
452 return t_diff(now, 60 * 60 * 24);
457 uptime_hrs(time_t *now)
459 return t_diff(now, 60 * 60);
464 uptime_mins(time_t *now)
466 return t_diff(now, 60);
471 * Handler for the IRC command "STATS".
472 * See RFC 2812 section 3.4.4.
475 IRC_STATS( CLIENT *Client, REQUEST *Req )
477 CLIENT *from, *target, *cl;
482 unsigned int days, hrs, mins;
483 struct list_head *list;
484 struct list_elem *list_item;
486 assert(Client != NULL);
490 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
491 Client_ID(Client), Req->command);
493 /* use prefix to determine "From" */
494 if (Client_Type(Client) == CLIENT_SERVER)
495 from = Client_Search(Req->prefix);
500 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
501 Client_ID(Client), Req->prefix);
503 if (Req->argc == 2) {
504 /* forward to another server? */
505 target = Client_Search(Req->argv[1]);
506 if ((!target) || (Client_Type(target) != CLIENT_SERVER))
507 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
508 Client_ID(from), Req->argv[1]);
510 if (target != Client_ThisServer()) {
511 /* forward to another server */
512 return IRC_WriteStrClientPrefix(target, from,
513 "STATS %s %s", Req->argv[0], Req->argv[1]);
518 query = Req->argv[0][0] ? Req->argv[0][0] : '*';
523 case 'g': /* Network-wide bans ("G-Lines") */
525 case 'k': /* Server-local bans ("K-Lines") */
527 if (!Client_HasMode(from, 'o'))
528 return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG,
530 if (query == 'g' || query == 'G')
531 list = Class_GetList(CLASS_GLINE);
533 list = Class_GetList(CLASS_KLINE);
534 list_item = Lists_GetFirst(list);
536 if (!IRC_WriteStrClient(from, RPL_STATSXLINE_MSG,
537 Client_ID(from), query,
538 Lists_GetMask(list_item),
539 Lists_GetValidity(list_item),
540 Lists_GetReason(list_item)))
542 list_item = Lists_GetNext(list_item);
545 case 'l': /* Link status (servers and own link) */
547 time_now = time(NULL);
548 for (con = Conn_First(); con != NONE; con = Conn_Next(con)) {
549 cl = Conn_GetClient(con);
552 if ((Client_Type(cl) == CLIENT_SERVER)
554 /* Server link or our own connection */
556 if (Conn_Options(con) & CONN_ZIP) {
557 if (!IRC_WriteStrClient
558 (from, RPL_STATSLINKINFOZIP_MSG,
559 Client_ID(from), Client_Mask(cl),
560 Conn_SendQ(con), Conn_SendMsg(con),
566 (long)(time_now - Conn_StartTime(con))))
571 if (!IRC_WriteStrClient
572 (from, RPL_STATSLINKINFO_MSG,
573 Client_ID(from), Client_Mask(cl),
574 Conn_SendQ(con), Conn_SendMsg(con),
575 Conn_SendBytes(con), Conn_RecvMsg(con),
577 (long)(time_now - Conn_StartTime(con))))
582 case 'm': /* IRC command status (usage count) */
584 cmd = Parse_GetCommandStruct();
585 for (; cmd->name; cmd++) {
586 if (cmd->lcount == 0 && cmd->rcount == 0)
588 if (!IRC_WriteStrClient
589 (from, RPL_STATSCOMMANDS_MSG, Client_ID(from),
590 cmd->name, cmd->lcount, cmd->bytes, cmd->rcount))
594 case 'u': /* Server uptime */
596 time_now = time(NULL) - NGIRCd_Start;
597 days = uptime_days(&time_now);
598 hrs = uptime_hrs(&time_now);
599 mins = uptime_mins(&time_now);
600 if (!IRC_WriteStrClient(from, RPL_STATSUPTIME, Client_ID(from),
601 days, hrs, mins, (unsigned int)time_now))
606 IRC_SetPenalty(from, 2);
607 return IRC_WriteStrClient(from, RPL_ENDOFSTATS_MSG,
608 Client_ID(from), query);
613 * Handler for the IRC command "SUMMON".
614 * See RFC 2812 section 4.5. ngIRCd doesn't implement this functionality and
615 * therefore answers with ERR_SUMMONDISABLED.
618 IRC_SUMMON(CLIENT * Client, REQUEST * Req)
620 return IRC_WriteStrClient(Client, ERR_SUMMONDISABLED_MSG,
621 Client_ID(Client), Req->command);
626 IRC_TIME( CLIENT *Client, REQUEST *Req )
628 CLIENT *from, *target;
632 assert( Client != NULL );
633 assert( Req != NULL );
635 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
637 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
639 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
643 target = Client_Search( Req->argv[0] );
644 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
646 if( target != Client_ThisServer( ))
648 return IRC_WriteStrClientPrefix( target, from, "TIME %s", Req->argv[0] );
653 (void)strftime( t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime( &t ));
654 return IRC_WriteStrClient( from, RPL_TIME_MSG, Client_ID( from ), Client_ID( Client_ThisServer( )), t_str );
659 * Handler for the IRC command "USERHOST".
660 * See RFC 2812 section 4.8.
663 IRC_USERHOST(CLIENT *Client, REQUEST *Req)
665 char rpl[COMMAND_LEN];
669 assert(Client != NULL);
673 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
674 Client_ID(Client), Req->command);
681 strlcpy(rpl, RPL_USERHOST_MSG, sizeof rpl);
682 for (i = 0; i < max; i++) {
683 c = Client_Search(Req->argv[i]);
684 if (c && (Client_Type(c) == CLIENT_USER)) {
685 /* This Nick is "online" */
686 strlcat(rpl, Client_ID(c), sizeof(rpl));
687 if (Client_HasMode(c, 'o'))
688 strlcat(rpl, "*", sizeof(rpl));
689 strlcat(rpl, "=", sizeof(rpl));
690 if (Client_HasMode(c, 'a'))
691 strlcat(rpl, "-", sizeof(rpl));
693 strlcat(rpl, "+", sizeof(rpl));
694 strlcat(rpl, Client_User(c), sizeof(rpl));
695 strlcat(rpl, "@", sizeof(rpl));
696 strlcat(rpl, Client_HostnameCloaked(c), sizeof(rpl));
697 strlcat(rpl, " ", sizeof(rpl));
700 ngt_TrimLastChr(rpl, ' ');
702 return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
707 * Handler for the IRC command "USERS".
708 * See RFC 2812 section 4.6. As suggested there the command is disabled.
711 IRC_USERS(CLIENT * Client, REQUEST * Req)
713 return IRC_WriteStrClient(Client, ERR_USERSDISABLED_MSG,
714 Client_ID(Client), Req->command);
719 IRC_VERSION( CLIENT *Client, REQUEST *Req )
721 CLIENT *target, *prefix;
723 assert( Client != NULL );
724 assert( Req != NULL );
726 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
729 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
730 else target = Client_ThisServer( );
732 /* Prefix ermitteln */
733 if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
734 else prefix = Client;
735 if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
737 /* An anderen Server weiterleiten? */
738 if( target != Client_ThisServer( ))
740 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
743 IRC_WriteStrClientPrefix( target, prefix, "VERSION %s", Req->argv[0] );
747 /* send version information */
748 IRC_SetPenalty(Client, 1);
749 return IRC_WriteStrClient(Client, RPL_VERSION_MSG, Client_ID(prefix),
750 PACKAGE_NAME, PACKAGE_VERSION,
751 NGIRCd_DebugLevel, Conf_ServerName,
752 NGIRCd_VersionAddition);
757 write_whoreply(CLIENT *Client, CLIENT *c, const char *channelname, const char *flags)
759 return IRC_WriteStrClient(Client, RPL_WHOREPLY_MSG, Client_ID(Client),
760 channelname, Client_User(c),
761 Client_HostnameCloaked(c),
762 Client_ID(Client_Introducer(c)), Client_ID(c),
763 flags, Client_Hops(c), Client_Info(c));
768 who_flags_status(const char *client_modes)
770 if (strchr(client_modes, 'a'))
771 return "G"; /* away */
777 who_flags_qualifier(const char *chan_user_modes)
779 if (strchr(chan_user_modes, 'o'))
781 else if (strchr(chan_user_modes, 'v'))
788 IRC_Send_WHO(CLIENT *Client, CHANNEL *Chan, bool OnlyOps)
790 bool is_visible, is_member, is_ircop;
792 const char *client_modes;
793 const char *chan_user_modes;
797 assert( Client != NULL );
798 assert( Chan != NULL );
800 is_member = Channel_IsMemberOf(Chan, Client);
802 /* Secret channel? */
803 if (!is_member && strchr(Channel_Modes(Chan), 's'))
804 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), Channel_Name(Chan));
806 cl2chan = Channel_FirstMember(Chan);
807 for (; cl2chan ; cl2chan = Channel_NextMember(Chan, cl2chan)) {
808 c = Channel_GetClient(cl2chan);
810 client_modes = Client_Modes(c);
811 is_ircop = strchr(client_modes, 'o') != NULL;
812 if (OnlyOps && !is_ircop)
815 is_visible = strchr(client_modes, 'i') == NULL;
816 if (is_member || is_visible) {
817 strcpy(flags, who_flags_status(client_modes));
819 strlcat(flags, "*", sizeof(flags));
821 chan_user_modes = Channel_UserModes(Chan, c);
822 strlcat(flags, who_flags_qualifier(chan_user_modes), sizeof(flags));
824 if (!write_whoreply(Client, c, Channel_Name(Chan), flags))
828 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), Channel_Name(Chan));
833 IRC_WHO( CLIENT *Client, REQUEST *Req )
835 bool only_ops, have_arg, client_match;
836 const char *channelname, *client_modes, *chan_user_modes;
837 char pattern[COMMAND_LEN];
843 assert( Client != NULL );
844 assert( Req != NULL );
847 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
852 if (Req->argc == 2) {
853 if (strcmp(Req->argv[1], "o") == 0)
856 else return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
860 IRC_SetPenalty(Client, 1);
861 if (Req->argc >= 1) { /* Channel or Mask. */
862 chan = Channel_Search(Req->argv[0]);
864 return IRC_Send_WHO(Client, chan, only_ops);
865 if (strcmp(Req->argv[0], "0") != 0) { /* RFC stupidity, same as no arguments */
867 strlcpy(pattern, Req->argv[0], sizeof(pattern));
868 ngt_LowerStr(pattern);
869 IRC_SetPenalty(Client, 3);
873 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
874 if (Client_Type(c) != CLIENT_USER)
878 * In the absence of the parameter, all visible (users who aren't
879 * invisible (user mode +i) and who don't have a common channel
880 * with the requesting client) are listed.
882 * The same result can be achieved by using a [sic] of "0"
883 * or any wildcard which will end up matching every visible user.
885 * The [sic] passed to WHO is matched against users' host, server, real name and
886 * nickname if the channel cannot be found.
888 client_modes = Client_Modes(c);
889 if (strchr(client_modes, 'i'))
892 if (only_ops && !strchr(client_modes, 'o'))
895 if (have_arg) { /* match pattern against user host/server/name/nick */
896 client_match = MatchCaseInsensitive(pattern, Client_Hostname(c)); /* user's host */
898 client_match = MatchCaseInsensitive(pattern, Client_ID(Client_Introducer(c))); /* server */
900 client_match = Match(Req->argv[0], Client_Info(c)); /* realname */
902 client_match = MatchCaseInsensitive(pattern, Client_ID(c)); /* nick name */
904 if (!client_match) /* This isn't the client you're looking for */
908 strcpy(flags, who_flags_status(client_modes));
910 if (strchr(client_modes, 'o')) /* this client is an operator */
911 strlcat(flags, "*", sizeof(flags));
913 /* Search suitable channel */
914 cl2chan = Channel_FirstChannelOf(c);
916 cn = Channel_GetChannel(cl2chan);
917 if (Channel_IsMemberOf(cn, Client) ||
918 !strchr(Channel_Modes(cn), 's'))
920 channelname = Channel_Name(cn);
923 cl2chan = Channel_NextChannelOf(c, cl2chan);
926 chan = Channel_GetChannel(cl2chan);
927 chan_user_modes = Channel_UserModes(chan, c);
928 strlcat(flags, who_flags_qualifier(chan_user_modes), sizeof(flags));
932 if (!write_whoreply(Client, c, channelname, flags))
937 channelname = Req->argv[0];
941 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), channelname);
946 * Generate WHOIS reply of one actual client.
948 * @param Client The client from which this command has been received.
949 * @param from The client requesting the information ("originator").
950 * @param c The client of which information should be returned.
951 * @returns CONNECTED or DISCONNECTED.
954 IRC_WHOIS_SendReply(CLIENT *Client, CLIENT *from, CLIENT *c)
956 char str[LINE_LEN + 1];
960 /* Nick, user, hostname and client info */
961 if (!IRC_WriteStrClient(from, RPL_WHOISUSER_MSG, Client_ID(from),
962 Client_ID(c), Client_User(c),
963 Client_HostnameCloaked(c), Client_Info(c)))
967 if (!IRC_WriteStrClient(from, RPL_WHOISSERVER_MSG, Client_ID(from),
968 Client_ID(c), Client_ID(Client_Introducer(c)),
969 Client_Info(Client_Introducer(c))))
973 snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
974 Client_ID(from), Client_ID(c));
975 cl2chan = Channel_FirstChannelOf(c);
977 chan = Channel_GetChannel(cl2chan);
978 assert(chan != NULL);
981 cl2chan = Channel_NextChannelOf(c, cl2chan);
983 /* Secret channel? */
984 if (strchr(Channel_Modes(chan), 's')
985 && !Channel_IsMemberOf(chan, Client))
988 /* Local channel and request is not from a user? */
989 if (Client_Type(Client) == CLIENT_SERVER
990 && Channel_IsLocal(chan))
993 /* Concatenate channel names */
994 if (str[strlen(str) - 1] != ':')
995 strlcat(str, " ", sizeof(str));
997 strlcat(str, who_flags_qualifier(Channel_UserModes(chan, c)),
999 strlcat(str, Channel_Name(chan), sizeof(str));
1001 if (strlen(str) > (LINE_LEN - CHANNEL_NAME_LEN - 4)) {
1002 /* Line becomes too long: send it! */
1003 if (!IRC_WriteStrClient(Client, "%s", str))
1004 return DISCONNECTED;
1005 snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
1006 Client_ID(from), Client_ID(c));
1009 if(str[strlen(str) - 1] != ':') {
1010 /* There is data left to send: */
1011 if (!IRC_WriteStrClient(Client, "%s", str))
1012 return DISCONNECTED;
1016 if (Client_HasMode(c, 'o') &&
1017 !IRC_WriteStrClient(from, RPL_WHOISOPERATOR_MSG,
1018 Client_ID(from), Client_ID(c)))
1019 return DISCONNECTED;
1021 /* Connected using SSL? */
1022 if (Conn_UsesSSL(Client_Conn(c)) &&
1023 !IRC_WriteStrClient(from, RPL_WHOISSSL_MSG,
1024 Client_ID(from), Client_ID(c)))
1025 return DISCONNECTED;
1027 /* Idle and signon time (local clients only!) */
1028 if (!Conf_MorePrivacy && Client_Conn(c) > NONE &&
1029 !IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
1030 Client_ID(from), Client_ID(c),
1031 (unsigned long)Conn_GetIdle(Client_Conn(c)),
1032 (unsigned long)Conn_GetSignon(Client_Conn(c))))
1033 return DISCONNECTED;
1036 if (Client_HasMode(c, 'a') &&
1037 !IRC_WriteStrClient(from, RPL_AWAY_MSG,
1038 Client_ID(from), Client_ID(c),
1040 return DISCONNECTED;
1042 return IRC_WriteStrClient(from, RPL_ENDOFWHOIS_MSG,
1043 Client_ID(from), Client_ID(c));
1044 } /* IRC_WHOIS_SendReply */
1048 * Handler for the IRC "WHOIS" command.
1050 * See RFC 2812, 3.6.2 "Whois query".
1052 * @param Client The client from which this command has been received.
1053 * @param Req Request structure with prefix and all parameters.
1054 * @return CONNECTED or DISCONNECTED.
1057 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
1059 CLIENT *from, *target, *c;
1060 unsigned int match_count = 0, found = 0;
1061 bool has_wildcards, is_remote;
1062 bool got_wildcard = false;
1065 assert( Client != NULL );
1066 assert( Req != NULL );
1068 /* Bad number of parameters? */
1069 if (Req->argc < 1 || Req->argc > 2)
1070 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1071 Client_ID(Client), Req->command);
1073 /* Search sender of the WHOIS */
1074 if (Client_Type(Client) == CLIENT_SERVER) {
1075 from = Client_Search(Req->prefix);
1077 IRC_SetPenalty(Client, 1);
1081 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1082 Client_ID(Client), Req->prefix);
1084 /* Get target server for this command */
1085 if (Req->argc > 1) {
1086 /* Search the target server, which can be specified as a
1087 * nick name on that server as well: */
1088 target = Client_Search(Req->argv[0]);
1090 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
1091 Client_ID(from), Req->argv[0]);
1093 target = Client_ThisServer();
1094 assert(target != NULL);
1096 /* Forward to other server? */
1097 if (Client_NextHop(target) != Client_ThisServer() &&
1098 Client_Type(Client_NextHop(target)) == CLIENT_SERVER)
1099 return IRC_WriteStrClientPrefix(target, from,
1101 Req->argv[0], Req->argv[1]);
1103 is_remote = Client_Conn(from) < 0;
1104 for (query = strtok(Req->argv[Req->argc - 1], ",");
1106 query = strtok(NULL, ","), found++)
1108 has_wildcards = query[strcspn(query, "*?")] != 0;
1110 * follows ircd 2.10 implementation:
1111 * - handle up to 3 targets
1112 * - no wildcards for remote clients
1113 * - only one wildcard target per local client
1115 * also, at most ten matches are returned.
1117 if (!has_wildcards || is_remote) {
1118 c = Client_Search(query);
1120 if (!IRC_WHOIS_SendReply(Client, from, c))
1121 return DISCONNECTED;
1123 if (!IRC_WriteStrClient(Client,
1127 return DISCONNECTED;
1132 /* we already handled one wildcard query */
1133 if (!IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1134 Client_ID(Client), query))
1135 return DISCONNECTED;
1138 got_wildcard = true;
1139 IRC_SetPenalty(Client, 3);
1141 for (c = Client_First(); c && match_count < 10; c = Client_Next(c)) {
1142 if (Client_Type(c) != CLIENT_USER)
1144 if (!MatchCaseInsensitive(query, Client_ID(c)))
1146 if (!IRC_WHOIS_SendReply(Client, from, c))
1147 return DISCONNECTED;
1151 if (match_count == 0)
1152 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1153 Client_ID(Client), Req->argv[Req->argc - 1]);
1160 WHOWAS_EntryWrite(CLIENT *prefix, WHOWAS *entry)
1164 (void)strftime(t_str, sizeof(t_str), "%a %b %d %H:%M:%S %Y",
1165 localtime(&entry->time));
1167 if (!IRC_WriteStrClient(prefix, RPL_WHOWASUSER_MSG, Client_ID(prefix),
1168 entry->id, entry->user, entry->host, entry->info))
1169 return DISCONNECTED;
1171 return IRC_WriteStrClient(prefix, RPL_WHOISSERVER_MSG, Client_ID(prefix),
1172 entry->id, entry->server, t_str);
1176 * IRC "WHOWAS" function.
1177 * This function implements the IRC command "WHOWHAS". It handles local
1178 * requests and request that should be forwarded to other servers.
1181 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1183 CLIENT *target, *prefix;
1185 char tok_buf[COMMAND_LEN];
1186 int max, last, count, i, nc;
1189 assert( Client != NULL );
1190 assert( Req != NULL );
1192 /* Do not reveal any info on disconnected users? */
1193 if (Conf_MorePrivacy)
1196 /* Wrong number of parameters? */
1198 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1199 Client_ID(Client), Req->command);
1201 return IRC_WriteStrClient(Client, ERR_NONICKNAMEGIVEN_MSG, Client_ID(Client));
1205 target = Client_Search(Req->argv[2]);
1207 target = Client_ThisServer();
1210 if (Client_Type(Client) == CLIENT_SERVER)
1211 prefix = Client_Search(Req->prefix);
1216 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1217 Client_ID(Client), Req->prefix);
1219 /* Forward to other server? */
1220 if (target != Client_ThisServer()) {
1221 if (!target || (Client_Type(target) != CLIENT_SERVER))
1222 return IRC_WriteStrClient(prefix, ERR_NOSUCHSERVER_MSG,
1223 Client_ID(prefix), Req->argv[2]);
1226 IRC_WriteStrClientPrefix( target, prefix, "WHOWAS %s %s %s",
1227 Req->argv[0], Req->argv[1],
1232 whowas = Client_GetWhowas( );
1233 last = Client_GetLastWhowasIndex( );
1237 max = DEFAULT_WHOWAS;
1238 if (Req->argc > 1) {
1239 max = atoi(Req->argv[1]);
1245 * Break up the nick argument into a list of nicks, if applicable
1246 * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1248 strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1249 nick = strtok(tok_buf, ",");
1251 for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1255 if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1256 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1257 return DISCONNECTED;
1261 /* previous entry */
1264 /* "underflow", wrap around */
1268 if (nc && count >= max)
1270 } while (i != last);
1272 if (nc == 0 && !IRC_WriteStrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1273 Client_ID(prefix), nick))
1274 return DISCONNECTED;
1276 return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG, Client_ID(prefix), Req->argv[0]);
1281 * Send LUSERS reply to a client.
1283 * @param Client The receipient of the information.
1284 * @return CONNECTED or DISCONNECTED.
1287 IRC_Send_LUSERS(CLIENT *Client)
1294 assert(Client != NULL);
1296 /* Users, services and serevers in the network */
1297 if (!IRC_WriteStrClient(Client, RPL_LUSERCLIENT_MSG, Client_ID(Client),
1298 Client_UserCount(), Client_ServiceCount(),
1299 Client_ServerCount()))
1300 return DISCONNECTED;
1302 /* Number of IRC operators */
1303 cnt = Client_OperCount( );
1305 if (!IRC_WriteStrClient(Client, RPL_LUSEROP_MSG,
1306 Client_ID(Client), cnt))
1307 return DISCONNECTED;
1310 /* Unknown connections */
1311 cnt = Client_UnknownCount( );
1313 if (!IRC_WriteStrClient(Client, RPL_LUSERUNKNOWN_MSG,
1314 Client_ID(Client), cnt))
1315 return DISCONNECTED;
1318 /* Number of created channels */
1319 if (!IRC_WriteStrClient(Client, RPL_LUSERCHANNELS_MSG,
1321 Channel_CountVisible(Client)))
1322 return DISCONNECTED;
1324 /* Number of local users, services and servers */
1325 if (!IRC_WriteStrClient(Client, RPL_LUSERME_MSG, Client_ID(Client),
1326 Client_MyUserCount(), Client_MyServiceCount(),
1327 Client_MyServerCount()))
1328 return DISCONNECTED;
1331 /* Maximum number of local users */
1332 cnt = Client_MyUserCount();
1333 max = Client_MyMaxUserCount();
1334 if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1335 cnt, max, cnt, max))
1336 return DISCONNECTED;
1337 /* Maximum number of users in the network */
1338 cnt = Client_UserCount();
1339 max = Client_MaxUserCount();
1340 if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1341 cnt, max, cnt, max))
1342 return DISCONNECTED;
1343 /* Connection counters */
1344 if (! IRC_WriteStrClient(Client, RPL_STATSCONN_MSG, Client_ID(Client),
1345 Conn_CountMax(), Conn_CountAccepted()))
1346 return DISCONNECTED;
1350 } /* IRC_Send_LUSERS */
1354 Show_MOTD_Start(CLIENT *Client)
1356 return IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG,
1357 Client_ID( Client ), Client_ID( Client_ThisServer( )));
1361 Show_MOTD_Sendline(CLIENT *Client, const char *msg)
1363 return IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID( Client ), msg);
1367 Show_MOTD_End(CLIENT *Client)
1369 return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
1373 static bool Show_MOTD_SSLInfo(CLIENT *Client)
1376 char buf[COMMAND_LEN] = "Connected using Cipher ";
1378 if (!Conn_GetCipherInfo(Client_Conn(Client), buf + 23, sizeof buf - 23))
1381 if (!Show_MOTD_Sendline(Client, buf))
1388 Show_MOTD_SSLInfo(UNUSED CLIENT *c)
1393 IRC_Show_MOTD( CLIENT *Client )
1396 size_t len_tot, len_str;
1398 assert( Client != NULL );
1400 len_tot = array_bytes(&Conf_Motd);
1401 if (len_tot == 0 && !Conn_UsesSSL(Client_Conn(Client)))
1402 return IRC_WriteStrClient(Client, ERR_NOMOTD_MSG, Client_ID(Client));
1404 if (!Show_MOTD_Start(Client))
1405 return DISCONNECTED;
1407 line = array_start(&Conf_Motd);
1408 while (len_tot > 0) {
1409 len_str = strlen(line) + 1;
1411 assert(len_tot >= len_str);
1414 if (!Show_MOTD_Sendline(Client, line))
1415 return DISCONNECTED;
1419 if (!Show_MOTD_SSLInfo(Client))
1420 return DISCONNECTED;
1421 return Show_MOTD_End(Client);
1422 } /* IRC_Show_MOTD */
1426 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
1428 bool is_visible, is_member;
1429 char str[LINE_LEN + 1];
1433 assert( Client != NULL );
1434 assert( Chan != NULL );
1436 if( Channel_IsMemberOf( Chan, Client )) is_member = true;
1437 else is_member = false;
1439 /* Do not print info on channel memberships to anyone that is not member? */
1440 if (Conf_MorePrivacy && !is_member)
1443 /* Secret channel? */
1444 if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
1446 /* Alle Mitglieder suchen */
1447 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1448 cl2chan = Channel_FirstMember( Chan );
1451 cl = Channel_GetClient( cl2chan );
1453 if( strchr( Client_Modes( cl ), 'i' )) is_visible = false;
1454 else is_visible = true;
1456 if( is_member || is_visible )
1458 /* Nick anhaengen */
1459 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
1460 if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
1461 else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
1462 strlcat( str, Client_ID( cl ), sizeof( str ));
1464 if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
1466 /* Zeile wird zu lang: senden! */
1467 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1468 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1472 /* naechstes Mitglied suchen */
1473 cl2chan = Channel_NextMember( Chan, cl2chan );
1475 if( str[strlen( str ) - 1] != ':')
1477 /* Es sind noch Daten da, die gesendet werden muessen */
1478 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1482 } /* IRC_Send_NAMES */
1486 * Send the ISUPPORT numeric (005).
1487 * This numeric indicates the features that are supported by this server.
1488 * See <http://www.irc.org/tech_docs/005.html> for details.
1491 IRC_Send_ISUPPORT(CLIENT * Client)
1493 if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1495 return DISCONNECTED;
1496 return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1497 CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1498 COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1500 } /* IRC_Send_ISUPPORT */