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 * Send WHO reply for a "channel target" ("WHO #channel").
790 * @param Client Client requesting the information.
791 * @param Chan Channel being requested.
792 * @param OnlyOps Only display IRC operators.
793 * @return CONNECTED or DISCONNECTED.
796 IRC_WHO_Channel(CLIENT *Client, CHANNEL *Chan, bool OnlyOps)
798 bool is_visible, is_member, is_ircop;
800 const char *client_modes;
801 const char *chan_user_modes;
805 assert( Client != NULL );
806 assert( Chan != NULL );
808 is_member = Channel_IsMemberOf(Chan, Client);
810 /* Secret channel? */
811 if (!is_member && strchr(Channel_Modes(Chan), 's'))
812 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG,
813 Client_ID(Client), Channel_Name(Chan));
815 cl2chan = Channel_FirstMember(Chan);
816 for (; cl2chan ; cl2chan = Channel_NextMember(Chan, cl2chan)) {
817 c = Channel_GetClient(cl2chan);
819 client_modes = Client_Modes(c);
820 is_ircop = strchr(client_modes, 'o') != NULL;
821 if (OnlyOps && !is_ircop)
824 is_visible = strchr(client_modes, 'i') == NULL;
825 if (is_member || is_visible) {
826 strcpy(flags, who_flags_status(client_modes));
828 strlcat(flags, "*", sizeof(flags));
830 chan_user_modes = Channel_UserModes(Chan, c);
831 strlcat(flags, who_flags_qualifier(chan_user_modes),
834 if (!write_whoreply(Client, c, Channel_Name(Chan),
839 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client),
845 * Send WHO reply for a "mask target" ("WHO m*sk").
847 * @param Client Client requesting the information.
848 * @param Mask Mask being requested or NULL for "all" clients.
849 * @param OnlyOps Only display IRC operators.
850 * @return CONNECTED or DISCONNECTED.
853 IRC_WHO_Mask(CLIENT *Client, char *Mask, bool OnlyOps)
858 bool client_match, is_visible;
861 assert (Client != NULL);
866 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
867 if (Client_Type(c) != CLIENT_USER)
870 if (OnlyOps && !Client_HasMode(c, 'o'))
874 /* Match pattern against user host/server/name/nick */
875 client_match = MatchCaseInsensitive(Mask,
878 client_match = MatchCaseInsensitive(Mask,
879 Client_ID(Client_Introducer(c)));
881 client_match = MatchCaseInsensitive(Mask,
884 client_match = MatchCaseInsensitive(Mask,
887 continue; /* no match: skip this client */
890 is_visible = !Client_HasMode(c, 'i');
892 /* Target client is invisible, but mask matches exactly? */
893 if (!is_visible && Mask && strcasecmp(Client_ID(c), Mask) == 0)
896 /* Target still invisible, but are both on the same channel? */
898 cl2chan = Channel_FirstChannelOf(Client);
899 while (cl2chan && !is_visible) {
900 chan = Channel_GetChannel(cl2chan);
901 if (Channel_IsMemberOf(chan, c))
903 cl2chan = Channel_NextChannelOf(Client, cl2chan);
907 if (!is_visible) /* target user is not visible */
910 strcpy(flags, who_flags_status(Client_Modes(c)));
911 if (strchr(Client_Modes(c), 'o'))
912 strlcat(flags, "*", sizeof(flags));
914 if (!write_whoreply(Client, c, "*", flags))
919 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client),
925 * Handler for the IRC "WHO" command.
927 * See RFC 2812, 3.6.1 "Who query".
929 * @param Client The client from which this command has been received.
930 * @param Req Request structure with prefix and all parameters.
931 * @return CONNECTED or DISCONNECTED.
934 IRC_WHO(CLIENT *Client, REQUEST *Req)
939 assert (Client != NULL);
940 assert (Req != NULL);
943 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
944 Client_ID(Client), Req->command);
948 if (Req->argc == 2) {
949 if (strcmp(Req->argv[1], "o") == 0)
953 return IRC_WriteStrClient(Client,
954 ERR_NEEDMOREPARAMS_MSG,
960 IRC_SetPenalty(Client, 1);
961 if (Req->argc >= 1) {
962 /* Channel or mask given */
963 chan = Channel_Search(Req->argv[0]);
965 /* Members of a channel have been requested */
966 IRC_SetPenalty(Client, 1);
967 return IRC_WHO_Channel(Client, chan, only_ops);
969 if (strcmp(Req->argv[0], "0") != 0) {
970 /* A mask has been given. But please note this RFC
971 * stupidity: "0" is same as no arguments ... */
972 IRC_SetPenalty(Client, 3);
973 return IRC_WHO_Mask(Client, Req->argv[0], only_ops);
977 /* No channel or (valid) mask given */
978 IRC_SetPenalty(Client, 2);
979 return IRC_WHO_Mask(Client, NULL, only_ops);
984 * Generate WHOIS reply of one actual client.
986 * @param Client The client from which this command has been received.
987 * @param from The client requesting the information ("originator").
988 * @param c The client of which information should be returned.
989 * @returns CONNECTED or DISCONNECTED.
992 IRC_WHOIS_SendReply(CLIENT *Client, CLIENT *from, CLIENT *c)
994 char str[LINE_LEN + 1];
998 /* Nick, user, hostname and client info */
999 if (!IRC_WriteStrClient(from, RPL_WHOISUSER_MSG, Client_ID(from),
1000 Client_ID(c), Client_User(c),
1001 Client_HostnameCloaked(c), Client_Info(c)))
1002 return DISCONNECTED;
1005 if (!IRC_WriteStrClient(from, RPL_WHOISSERVER_MSG, Client_ID(from),
1006 Client_ID(c), Client_ID(Client_Introducer(c)),
1007 Client_Info(Client_Introducer(c))))
1008 return DISCONNECTED;
1011 snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
1012 Client_ID(from), Client_ID(c));
1013 cl2chan = Channel_FirstChannelOf(c);
1015 chan = Channel_GetChannel(cl2chan);
1016 assert(chan != NULL);
1019 cl2chan = Channel_NextChannelOf(c, cl2chan);
1021 /* Secret channel? */
1022 if (strchr(Channel_Modes(chan), 's')
1023 && !Channel_IsMemberOf(chan, Client))
1026 /* Local channel and request is not from a user? */
1027 if (Client_Type(Client) == CLIENT_SERVER
1028 && Channel_IsLocal(chan))
1031 /* Concatenate channel names */
1032 if (str[strlen(str) - 1] != ':')
1033 strlcat(str, " ", sizeof(str));
1035 strlcat(str, who_flags_qualifier(Channel_UserModes(chan, c)),
1037 strlcat(str, Channel_Name(chan), sizeof(str));
1039 if (strlen(str) > (LINE_LEN - CHANNEL_NAME_LEN - 4)) {
1040 /* Line becomes too long: send it! */
1041 if (!IRC_WriteStrClient(Client, "%s", str))
1042 return DISCONNECTED;
1043 snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
1044 Client_ID(from), Client_ID(c));
1047 if(str[strlen(str) - 1] != ':') {
1048 /* There is data left to send: */
1049 if (!IRC_WriteStrClient(Client, "%s", str))
1050 return DISCONNECTED;
1054 if (Client_HasMode(c, 'o') &&
1055 !IRC_WriteStrClient(from, RPL_WHOISOPERATOR_MSG,
1056 Client_ID(from), Client_ID(c)))
1057 return DISCONNECTED;
1059 /* Connected using SSL? */
1060 if (Conn_UsesSSL(Client_Conn(c)) &&
1061 !IRC_WriteStrClient(from, RPL_WHOISSSL_MSG,
1062 Client_ID(from), Client_ID(c)))
1063 return DISCONNECTED;
1065 /* Idle and signon time (local clients only!) */
1066 if (!Conf_MorePrivacy && Client_Conn(c) > NONE &&
1067 !IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
1068 Client_ID(from), Client_ID(c),
1069 (unsigned long)Conn_GetIdle(Client_Conn(c)),
1070 (unsigned long)Conn_GetSignon(Client_Conn(c))))
1071 return DISCONNECTED;
1074 if (Client_HasMode(c, 'a') &&
1075 !IRC_WriteStrClient(from, RPL_AWAY_MSG,
1076 Client_ID(from), Client_ID(c),
1078 return DISCONNECTED;
1080 return IRC_WriteStrClient(from, RPL_ENDOFWHOIS_MSG,
1081 Client_ID(from), Client_ID(c));
1082 } /* IRC_WHOIS_SendReply */
1086 * Handler for the IRC "WHOIS" command.
1088 * See RFC 2812, 3.6.2 "Whois query".
1090 * @param Client The client from which this command has been received.
1091 * @param Req Request structure with prefix and all parameters.
1092 * @return CONNECTED or DISCONNECTED.
1095 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
1097 CLIENT *from, *target, *c;
1098 unsigned int match_count = 0, found = 0;
1099 bool has_wildcards, is_remote;
1100 bool got_wildcard = false;
1103 assert( Client != NULL );
1104 assert( Req != NULL );
1106 /* Bad number of parameters? */
1107 if (Req->argc < 1 || Req->argc > 2)
1108 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1109 Client_ID(Client), Req->command);
1111 /* Search sender of the WHOIS */
1112 if (Client_Type(Client) == CLIENT_SERVER) {
1113 from = Client_Search(Req->prefix);
1115 IRC_SetPenalty(Client, 1);
1119 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1120 Client_ID(Client), Req->prefix);
1122 /* Get target server for this command */
1123 if (Req->argc > 1) {
1124 /* Search the target server, which can be specified as a
1125 * nick name on that server as well: */
1126 target = Client_Search(Req->argv[0]);
1128 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
1129 Client_ID(from), Req->argv[0]);
1131 target = Client_ThisServer();
1132 assert(target != NULL);
1134 /* Forward to other server? */
1135 if (Client_NextHop(target) != Client_ThisServer() &&
1136 Client_Type(Client_NextHop(target)) == CLIENT_SERVER)
1137 return IRC_WriteStrClientPrefix(target, from,
1139 Req->argv[0], Req->argv[1]);
1141 is_remote = Client_Conn(from) < 0;
1142 for (query = strtok(Req->argv[Req->argc - 1], ",");
1144 query = strtok(NULL, ","), found++)
1146 has_wildcards = query[strcspn(query, "*?")] != 0;
1148 * follows ircd 2.10 implementation:
1149 * - handle up to 3 targets
1150 * - no wildcards for remote clients
1151 * - only one wildcard target per local client
1153 * also, at most ten matches are returned.
1155 if (!has_wildcards || is_remote) {
1156 c = Client_Search(query);
1158 if (!IRC_WHOIS_SendReply(Client, from, c))
1159 return DISCONNECTED;
1161 if (!IRC_WriteStrClient(Client,
1165 return DISCONNECTED;
1170 /* we already handled one wildcard query */
1171 if (!IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1172 Client_ID(Client), query))
1173 return DISCONNECTED;
1176 got_wildcard = true;
1177 IRC_SetPenalty(Client, 3);
1179 for (c = Client_First(); c && match_count < 10; c = Client_Next(c)) {
1180 if (Client_Type(c) != CLIENT_USER)
1182 if (!MatchCaseInsensitive(query, Client_ID(c)))
1184 if (!IRC_WHOIS_SendReply(Client, from, c))
1185 return DISCONNECTED;
1189 if (match_count == 0)
1190 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1191 Client_ID(Client), Req->argv[Req->argc - 1]);
1198 WHOWAS_EntryWrite(CLIENT *prefix, WHOWAS *entry)
1202 (void)strftime(t_str, sizeof(t_str), "%a %b %d %H:%M:%S %Y",
1203 localtime(&entry->time));
1205 if (!IRC_WriteStrClient(prefix, RPL_WHOWASUSER_MSG, Client_ID(prefix),
1206 entry->id, entry->user, entry->host, entry->info))
1207 return DISCONNECTED;
1209 return IRC_WriteStrClient(prefix, RPL_WHOISSERVER_MSG, Client_ID(prefix),
1210 entry->id, entry->server, t_str);
1214 * IRC "WHOWAS" function.
1215 * This function implements the IRC command "WHOWHAS". It handles local
1216 * requests and request that should be forwarded to other servers.
1219 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1221 CLIENT *target, *prefix;
1223 char tok_buf[COMMAND_LEN];
1224 int max, last, count, i, nc;
1227 assert( Client != NULL );
1228 assert( Req != NULL );
1230 /* Do not reveal any info on disconnected users? */
1231 if (Conf_MorePrivacy)
1234 /* Wrong number of parameters? */
1236 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1237 Client_ID(Client), Req->command);
1239 return IRC_WriteStrClient(Client, ERR_NONICKNAMEGIVEN_MSG, Client_ID(Client));
1243 target = Client_Search(Req->argv[2]);
1245 target = Client_ThisServer();
1248 if (Client_Type(Client) == CLIENT_SERVER)
1249 prefix = Client_Search(Req->prefix);
1254 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1255 Client_ID(Client), Req->prefix);
1257 /* Forward to other server? */
1258 if (target != Client_ThisServer()) {
1259 if (!target || (Client_Type(target) != CLIENT_SERVER))
1260 return IRC_WriteStrClient(prefix, ERR_NOSUCHSERVER_MSG,
1261 Client_ID(prefix), Req->argv[2]);
1264 IRC_WriteStrClientPrefix( target, prefix, "WHOWAS %s %s %s",
1265 Req->argv[0], Req->argv[1],
1270 whowas = Client_GetWhowas( );
1271 last = Client_GetLastWhowasIndex( );
1275 max = DEFAULT_WHOWAS;
1276 if (Req->argc > 1) {
1277 max = atoi(Req->argv[1]);
1283 * Break up the nick argument into a list of nicks, if applicable
1284 * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1286 strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1287 nick = strtok(tok_buf, ",");
1289 for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1293 if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1294 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1295 return DISCONNECTED;
1299 /* previous entry */
1302 /* "underflow", wrap around */
1306 if (nc && count >= max)
1308 } while (i != last);
1310 if (nc == 0 && !IRC_WriteStrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1311 Client_ID(prefix), nick))
1312 return DISCONNECTED;
1314 return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG, Client_ID(prefix), Req->argv[0]);
1319 * Send LUSERS reply to a client.
1321 * @param Client The receipient of the information.
1322 * @return CONNECTED or DISCONNECTED.
1325 IRC_Send_LUSERS(CLIENT *Client)
1332 assert(Client != NULL);
1334 /* Users, services and serevers in the network */
1335 if (!IRC_WriteStrClient(Client, RPL_LUSERCLIENT_MSG, Client_ID(Client),
1336 Client_UserCount(), Client_ServiceCount(),
1337 Client_ServerCount()))
1338 return DISCONNECTED;
1340 /* Number of IRC operators */
1341 cnt = Client_OperCount( );
1343 if (!IRC_WriteStrClient(Client, RPL_LUSEROP_MSG,
1344 Client_ID(Client), cnt))
1345 return DISCONNECTED;
1348 /* Unknown connections */
1349 cnt = Client_UnknownCount( );
1351 if (!IRC_WriteStrClient(Client, RPL_LUSERUNKNOWN_MSG,
1352 Client_ID(Client), cnt))
1353 return DISCONNECTED;
1356 /* Number of created channels */
1357 if (!IRC_WriteStrClient(Client, RPL_LUSERCHANNELS_MSG,
1359 Channel_CountVisible(Client)))
1360 return DISCONNECTED;
1362 /* Number of local users, services and servers */
1363 if (!IRC_WriteStrClient(Client, RPL_LUSERME_MSG, Client_ID(Client),
1364 Client_MyUserCount(), Client_MyServiceCount(),
1365 Client_MyServerCount()))
1366 return DISCONNECTED;
1369 /* Maximum number of local users */
1370 cnt = Client_MyUserCount();
1371 max = Client_MyMaxUserCount();
1372 if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1373 cnt, max, cnt, max))
1374 return DISCONNECTED;
1375 /* Maximum number of users in the network */
1376 cnt = Client_UserCount();
1377 max = Client_MaxUserCount();
1378 if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1379 cnt, max, cnt, max))
1380 return DISCONNECTED;
1381 /* Connection counters */
1382 if (! IRC_WriteStrClient(Client, RPL_STATSCONN_MSG, Client_ID(Client),
1383 Conn_CountMax(), Conn_CountAccepted()))
1384 return DISCONNECTED;
1388 } /* IRC_Send_LUSERS */
1392 Show_MOTD_Start(CLIENT *Client)
1394 return IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG,
1395 Client_ID( Client ), Client_ID( Client_ThisServer( )));
1399 Show_MOTD_Sendline(CLIENT *Client, const char *msg)
1401 return IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID( Client ), msg);
1405 Show_MOTD_End(CLIENT *Client)
1407 return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
1411 static bool Show_MOTD_SSLInfo(CLIENT *Client)
1414 char buf[COMMAND_LEN] = "Connected using Cipher ";
1416 if (!Conn_GetCipherInfo(Client_Conn(Client), buf + 23, sizeof buf - 23))
1419 if (!Show_MOTD_Sendline(Client, buf))
1426 Show_MOTD_SSLInfo(UNUSED CLIENT *c)
1431 IRC_Show_MOTD( CLIENT *Client )
1434 size_t len_tot, len_str;
1436 assert( Client != NULL );
1438 len_tot = array_bytes(&Conf_Motd);
1439 if (len_tot == 0 && !Conn_UsesSSL(Client_Conn(Client)))
1440 return IRC_WriteStrClient(Client, ERR_NOMOTD_MSG, Client_ID(Client));
1442 if (!Show_MOTD_Start(Client))
1443 return DISCONNECTED;
1445 line = array_start(&Conf_Motd);
1446 while (len_tot > 0) {
1447 len_str = strlen(line) + 1;
1449 assert(len_tot >= len_str);
1452 if (!Show_MOTD_Sendline(Client, line))
1453 return DISCONNECTED;
1457 if (!Show_MOTD_SSLInfo(Client))
1458 return DISCONNECTED;
1459 return Show_MOTD_End(Client);
1460 } /* IRC_Show_MOTD */
1464 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
1466 bool is_visible, is_member;
1467 char str[LINE_LEN + 1];
1471 assert( Client != NULL );
1472 assert( Chan != NULL );
1474 if( Channel_IsMemberOf( Chan, Client )) is_member = true;
1475 else is_member = false;
1477 /* Do not print info on channel memberships to anyone that is not member? */
1478 if (Conf_MorePrivacy && !is_member)
1481 /* Secret channel? */
1482 if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
1484 /* Alle Mitglieder suchen */
1485 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1486 cl2chan = Channel_FirstMember( Chan );
1489 cl = Channel_GetClient( cl2chan );
1491 if( strchr( Client_Modes( cl ), 'i' )) is_visible = false;
1492 else is_visible = true;
1494 if( is_member || is_visible )
1496 /* Nick anhaengen */
1497 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
1498 if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
1499 else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
1500 strlcat( str, Client_ID( cl ), sizeof( str ));
1502 if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
1504 /* Zeile wird zu lang: senden! */
1505 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1506 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1510 /* naechstes Mitglied suchen */
1511 cl2chan = Channel_NextMember( Chan, cl2chan );
1513 if( str[strlen( str ) - 1] != ':')
1515 /* Es sind noch Daten da, die gesendet werden muessen */
1516 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1520 } /* IRC_Send_NAMES */
1524 * Send the ISUPPORT numeric (005).
1525 * This numeric indicates the features that are supported by this server.
1526 * See <http://www.irc.org/tech_docs/005.html> for details.
1529 IRC_Send_ISUPPORT(CLIENT * Client)
1531 if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1533 return DISCONNECTED;
1534 return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1535 CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1536 COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1538 } /* IRC_Send_ISUPPORT */