2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2012 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"
41 #include "irc-write.h"
48 IRC_ADMIN(CLIENT *Client, REQUEST *Req )
50 CLIENT *target, *prefix;
52 assert( Client != NULL );
53 assert( Req != NULL );
55 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
58 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
59 else target = Client_ThisServer( );
62 if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
64 if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
66 /* forwad message to another server? */
67 if( target != Client_ThisServer( ))
69 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
72 IRC_WriteStrClientPrefix( target, prefix, "ADMIN %s", Req->argv[0] );
76 /* mit Versionsinfo antworten */
77 if( ! IRC_WriteStrClient( Client, RPL_ADMINME_MSG, Client_ID( prefix ), Conf_ServerName )) return DISCONNECTED;
78 if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC1_MSG, Client_ID( prefix ), Conf_ServerAdmin1 )) return DISCONNECTED;
79 if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC2_MSG, Client_ID( prefix ), Conf_ServerAdmin2 )) return DISCONNECTED;
80 if( ! IRC_WriteStrClient( Client, RPL_ADMINEMAIL_MSG, Client_ID( prefix ), Conf_ServerAdminMail )) return DISCONNECTED;
82 IRC_SetPenalty( Client, 1 );
88 * Handler for the IRC command "INFO".
89 * See RFC 2812 section 3.4.10.
92 IRC_INFO(CLIENT * Client, REQUEST * Req)
94 CLIENT *target, *prefix;
97 assert(Client != NULL);
100 /* Wrong number of parameters? */
102 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
103 Client_ID(Client), Req->command);
105 /* Determine prefix */
106 if (Client_Type(Client) == CLIENT_SERVER)
107 prefix = Client_Search(Req->prefix);
111 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
112 Client_ID(Client), Req->prefix);
114 /* Look for a target */
116 target = Client_Search(Req->argv[0]);
118 target = Client_ThisServer();
120 /* Make sure that the target is a server */
121 if (target && Client_Type(target) != CLIENT_SERVER)
122 target = Client_Introducer(target);
125 return IRC_WriteStrClient(prefix, ERR_NOSUCHSERVER_MSG,
126 Client_ID(prefix), Req->argv[0]);
128 /* Pass on to another server? */
129 if (target != Client_ThisServer()) {
130 IRC_WriteStrClientPrefix(target, prefix, "INFO %s",
135 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix),
139 #if defined(__DATE__) && defined(__TIME__)
140 snprintf(msg, sizeof(msg), "Birth Date: %s at %s", __DATE__, __TIME__);
141 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
145 strlcpy(msg, "On-line since ", sizeof(msg));
146 strlcat(msg, NGIRCd_StartStr, sizeof(msg));
147 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
150 if (!IRC_WriteStrClient(Client, RPL_ENDOFINFO_MSG, Client_ID(prefix)))
153 IRC_SetPenalty(Client, 2);
159 * Handler for the IRC "ISON" command.
161 * See RFC 2812, 4.9 "Ison message".
163 * @param Client The client from which this command has been received.
164 * @param Req Request structure with prefix and all parameters.
165 * @return CONNECTED or DISCONNECTED.
168 IRC_ISON( CLIENT *Client, REQUEST *Req )
170 char rpl[COMMAND_LEN];
175 assert(Client != NULL);
178 /* Bad number of arguments? */
180 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
181 Client_ID(Client), Req->command);
183 strlcpy(rpl, RPL_ISON_MSG, sizeof rpl);
184 for (i = 0; i < Req->argc; i++) {
185 /* "All" ircd even parse ":<x> <y> ..." arguments and split
186 * them up; so we do the same ... */
187 ptr = strtok(Req->argv[i], " ");
190 c = Client_Search(ptr);
191 if (c && Client_Type(c) == CLIENT_USER) {
192 strlcat(rpl, Client_ID(c), sizeof(rpl));
193 strlcat(rpl, " ", sizeof(rpl));
195 ptr = strtok(NULL, " ");
198 ngt_TrimLastChr(rpl, ' ');
200 return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
205 * Handler for the IRC "LINKS" command.
207 * See RFC 2812, 3.4.5 "Links message".
209 * @param Client The client from which this command has been received.
210 * @param Req Request structure with prefix and all parameters.
211 * @return CONNECTED or DISCONNECTED.
214 IRC_LINKS(CLIENT *Client, REQUEST *Req)
216 CLIENT *target, *from, *c;
219 assert(Client != NULL);
222 IRC_SetPenalty(Client, 1);
225 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
226 Client_ID(Client), Req->command);
228 /* Get pointer to server mask or "*", if none given */
230 mask = Req->argv[Req->argc - 1];
234 if (Client_Type(Client) == CLIENT_SERVER)
235 from = Client_Search(Req->prefix);
239 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
240 Client_ID(Client), Req->prefix);
243 if (Req->argc == 2) {
244 target = Client_Search(Req->argv[0]);
245 if (! target || Client_Type(target) != CLIENT_SERVER)
246 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
250 if (target != Client_ThisServer())
251 return IRC_WriteStrClientPrefix(target, from,
252 "LINKS %s %s", Req->argv[0],
258 if (Client_Type(c) == CLIENT_SERVER
259 && MatchCaseInsensitive(mask, Client_ID(c))) {
260 if (!IRC_WriteStrClient(from, RPL_LINKS_MSG,
261 Client_ID(from), Client_ID(c),
262 Client_ID(Client_TopServer(c)
263 ? Client_TopServer(c)
264 : Client_ThisServer()),
265 Client_Hops(c), Client_Info(c)))
270 return IRC_WriteStrClient(from, RPL_ENDOFLINKS_MSG,
271 Client_ID(from), mask);
276 IRC_LUSERS( CLIENT *Client, REQUEST *Req )
278 CLIENT *target, *from;
280 assert( Client != NULL );
281 assert( Req != NULL );
283 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
285 /* Absender ermitteln */
286 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
288 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
290 /* An anderen Server forwarden? */
293 target = Client_Search( Req->argv[1] );
294 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
295 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LUSERS %s %s", Req->argv[0], Req->argv[1] );
298 /* Wer ist der Absender? */
299 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
300 else target = Client;
301 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
303 IRC_Send_LUSERS( target );
305 IRC_SetPenalty( target, 1 );
311 * Handler for the IRC command "SERVLIST".
312 * List registered services, see RFC 2811, section 3.5.1: the syntax is
313 * "SERVLIST [<mask> [<type>]]".
316 IRC_SERVLIST(CLIENT *Client, REQUEST *Req)
320 assert(Client != NULL);
324 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
325 Client_ID(Client), Req->command);
327 if (Req->argc < 2 || strcmp(Req->argv[1], "0") == 0) {
328 for (c = Client_First(); c!= NULL; c = Client_Next(c)) {
329 if (Client_Type(c) != CLIENT_SERVICE)
331 if (Req->argc > 0 && !MatchCaseInsensitive(Req->argv[0],
334 if (!IRC_WriteStrClient(Client, RPL_SERVLIST_MSG,
335 Client_ID(Client), Client_Mask(c),
336 Client_Mask(Client_Introducer(c)), "*",
337 0, Client_Hops(c), Client_Info(c)))
342 return IRC_WriteStrClient(Client, RPL_SERVLISTEND_MSG, Client_ID(Client),
343 Req->argc > 0 ? Req->argv[0] : "*",
344 Req->argc > 1 ? Req->argv[1] : "0");
349 IRC_MOTD( CLIENT *Client, REQUEST *Req )
351 CLIENT *from, *target;
353 assert( Client != NULL );
354 assert( Req != NULL );
356 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
358 /* From aus Prefix ermitteln */
359 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
361 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
366 target = Client_Search( Req->argv[0] );
367 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
369 if( target != Client_ThisServer( ))
371 /* Ok, anderer Server ist das Ziel: forwarden */
372 return IRC_WriteStrClientPrefix( target, from, "MOTD %s", Req->argv[0] );
376 IRC_SetPenalty( from, 3 );
377 return IRC_Show_MOTD( from );
382 IRC_NAMES( CLIENT *Client, REQUEST *Req )
384 char rpl[COMMAND_LEN], *ptr;
385 CLIENT *target, *from, *c;
388 assert( Client != NULL );
389 assert( Req != NULL );
391 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
393 /* use prefix to determine "From" */
394 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
396 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
400 /* forward to another server? */
401 target = Client_Search( Req->argv[1] );
402 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
404 if( target != Client_ThisServer( )) {
405 /* target is another server, forward */
406 return IRC_WriteStrClientPrefix( target, from, "NAMES %s :%s", Req->argv[0], Req->argv[1] );
412 /* bestimmte Channels durchgehen */
413 ptr = strtok( Req->argv[0], "," );
416 chan = Channel_Search( ptr );
420 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
422 if( ! IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), ptr )) return DISCONNECTED;
424 /* get next channel name */
425 ptr = strtok( NULL, "," );
430 chan = Channel_First( );
433 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
435 chan = Channel_Next( chan );
438 /* Now print all clients which are not in any channel */
440 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
443 if(( Client_Type( c ) == CLIENT_USER ) && ( Channel_FirstChannelOf( c ) == NULL ) && ( ! strchr( Client_Modes( c ), 'i' )))
445 /* its a user, concatenate ... */
446 if( rpl[strlen( rpl ) - 1] != ':' ) strlcat( rpl, " ", sizeof( rpl ));
447 strlcat( rpl, Client_ID( c ), sizeof( rpl ));
449 if( strlen( rpl ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
451 /* Line is gwoing too long, send now */
452 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
453 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
457 c = Client_Next( c );
459 if( rpl[strlen( rpl ) - 1] != ':')
461 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
464 IRC_SetPenalty( from, 1 );
465 return IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), "*" );
470 t_diff(time_t *t, const time_t d)
478 return (unsigned int)diff;
483 uptime_days(time_t *now)
485 return t_diff(now, 60 * 60 * 24);
490 uptime_hrs(time_t *now)
492 return t_diff(now, 60 * 60);
497 uptime_mins(time_t *now)
499 return t_diff(now, 60);
504 * Handler for the IRC command "STATS".
505 * See RFC 2812 section 3.4.4.
508 IRC_STATS( CLIENT *Client, REQUEST *Req )
510 CLIENT *from, *target, *cl;
515 unsigned int days, hrs, mins;
516 struct list_head *list;
517 struct list_elem *list_item;
519 assert(Client != NULL);
523 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
524 Client_ID(Client), Req->command);
526 /* use prefix to determine "From" */
527 if (Client_Type(Client) == CLIENT_SERVER)
528 from = Client_Search(Req->prefix);
533 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
534 Client_ID(Client), Req->prefix);
536 if (Req->argc == 2) {
537 /* forward to another server? */
538 target = Client_Search(Req->argv[1]);
539 if ((!target) || (Client_Type(target) != CLIENT_SERVER))
540 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
541 Client_ID(from), Req->argv[1]);
543 if (target != Client_ThisServer()) {
544 /* forward to another server */
545 return IRC_WriteStrClientPrefix(target, from,
546 "STATS %s %s", Req->argv[0], Req->argv[1]);
551 query = Req->argv[0][0] ? Req->argv[0][0] : '*';
556 case 'g': /* Network-wide bans ("G-Lines") */
558 case 'k': /* Server-local bans ("K-Lines") */
560 if (!Client_HasMode(from, 'o'))
561 return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG,
563 if (query == 'g' || query == 'G')
564 list = Class_GetList(CLASS_GLINE);
566 list = Class_GetList(CLASS_KLINE);
567 list_item = Lists_GetFirst(list);
569 if (!IRC_WriteStrClient(from, RPL_STATSXLINE_MSG,
570 Client_ID(from), query,
571 Lists_GetMask(list_item),
572 Lists_GetValidity(list_item),
573 Lists_GetReason(list_item)))
575 list_item = Lists_GetNext(list_item);
578 case 'l': /* Link status (servers and own link) */
580 time_now = time(NULL);
581 for (con = Conn_First(); con != NONE; con = Conn_Next(con)) {
582 cl = Conn_GetClient(con);
585 if ((Client_Type(cl) == CLIENT_SERVER)
587 /* Server link or our own connection */
589 if (Conn_Options(con) & CONN_ZIP) {
590 if (!IRC_WriteStrClient
591 (from, RPL_STATSLINKINFOZIP_MSG,
592 Client_ID(from), Client_Mask(cl),
593 Conn_SendQ(con), Conn_SendMsg(con),
599 (long)(time_now - Conn_StartTime(con))))
604 if (!IRC_WriteStrClient
605 (from, RPL_STATSLINKINFO_MSG,
606 Client_ID(from), Client_Mask(cl),
607 Conn_SendQ(con), Conn_SendMsg(con),
608 Conn_SendBytes(con), Conn_RecvMsg(con),
610 (long)(time_now - Conn_StartTime(con))))
615 case 'm': /* IRC command status (usage count) */
617 cmd = Parse_GetCommandStruct();
618 for (; cmd->name; cmd++) {
619 if (cmd->lcount == 0 && cmd->rcount == 0)
621 if (!IRC_WriteStrClient
622 (from, RPL_STATSCOMMANDS_MSG, Client_ID(from),
623 cmd->name, cmd->lcount, cmd->bytes, cmd->rcount))
627 case 'u': /* Server uptime */
629 time_now = time(NULL) - NGIRCd_Start;
630 days = uptime_days(&time_now);
631 hrs = uptime_hrs(&time_now);
632 mins = uptime_mins(&time_now);
633 if (!IRC_WriteStrClient(from, RPL_STATSUPTIME, Client_ID(from),
634 days, hrs, mins, (unsigned int)time_now))
639 IRC_SetPenalty(from, 2);
640 return IRC_WriteStrClient(from, RPL_ENDOFSTATS_MSG,
641 Client_ID(from), query);
646 * Handler for the IRC command "SUMMON".
647 * See RFC 2812 section 4.5. ngIRCd doesn't implement this functionality and
648 * therefore answers with ERR_SUMMONDISABLED.
651 IRC_SUMMON(CLIENT * Client, UNUSED REQUEST * Req)
653 return IRC_WriteStrClient(Client, ERR_SUMMONDISABLED_MSG,
659 IRC_TIME( CLIENT *Client, REQUEST *Req )
661 CLIENT *from, *target;
665 assert( Client != NULL );
666 assert( Req != NULL );
668 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
670 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
672 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
676 target = Client_Search( Req->argv[0] );
677 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
679 if( target != Client_ThisServer( ))
681 return IRC_WriteStrClientPrefix( target, from, "TIME %s", Req->argv[0] );
686 (void)strftime( t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime( &t ));
687 return IRC_WriteStrClient( from, RPL_TIME_MSG, Client_ID( from ), Client_ID( Client_ThisServer( )), t_str );
692 * Handler for the IRC command "USERHOST".
693 * See RFC 2812 section 4.8.
696 IRC_USERHOST(CLIENT *Client, REQUEST *Req)
698 char rpl[COMMAND_LEN];
702 assert(Client != NULL);
706 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
707 Client_ID(Client), Req->command);
714 strlcpy(rpl, RPL_USERHOST_MSG, sizeof rpl);
715 for (i = 0; i < max; i++) {
716 c = Client_Search(Req->argv[i]);
717 if (c && (Client_Type(c) == CLIENT_USER)) {
718 /* This Nick is "online" */
719 strlcat(rpl, Client_ID(c), sizeof(rpl));
720 if (Client_HasMode(c, 'o'))
721 strlcat(rpl, "*", sizeof(rpl));
722 strlcat(rpl, "=", sizeof(rpl));
723 if (Client_HasMode(c, 'a'))
724 strlcat(rpl, "-", sizeof(rpl));
726 strlcat(rpl, "+", sizeof(rpl));
727 strlcat(rpl, Client_User(c), sizeof(rpl));
728 strlcat(rpl, "@", sizeof(rpl));
729 strlcat(rpl, Client_HostnameCloaked(c), sizeof(rpl));
730 strlcat(rpl, " ", sizeof(rpl));
733 ngt_TrimLastChr(rpl, ' ');
735 return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
740 * Handler for the IRC command "USERS".
741 * See RFC 2812 section 4.6. As suggested there the command is disabled.
744 IRC_USERS(CLIENT * Client, UNUSED REQUEST * Req)
746 return IRC_WriteStrClient(Client, ERR_USERSDISABLED_MSG,
752 IRC_VERSION( CLIENT *Client, REQUEST *Req )
754 CLIENT *target, *prefix;
756 assert( Client != NULL );
757 assert( Req != NULL );
759 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
762 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
763 else target = Client_ThisServer( );
765 /* Prefix ermitteln */
766 if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
767 else prefix = Client;
768 if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
770 /* An anderen Server weiterleiten? */
771 if( target != Client_ThisServer( ))
773 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
776 IRC_WriteStrClientPrefix( target, prefix, "VERSION %s", Req->argv[0] );
780 /* send version information */
781 IRC_SetPenalty(Client, 1);
782 return IRC_WriteStrClient(Client, RPL_VERSION_MSG, Client_ID(prefix),
783 PACKAGE_NAME, PACKAGE_VERSION,
784 NGIRCd_DebugLevel, Conf_ServerName,
785 NGIRCd_VersionAddition);
790 write_whoreply(CLIENT *Client, CLIENT *c, const char *channelname, const char *flags)
792 return IRC_WriteStrClient(Client, RPL_WHOREPLY_MSG, Client_ID(Client),
793 channelname, Client_User(c),
794 Client_HostnameCloaked(c),
795 Client_ID(Client_Introducer(c)), Client_ID(c),
796 flags, Client_Hops(c), Client_Info(c));
801 who_flags_status(const char *client_modes)
803 if (strchr(client_modes, 'a'))
804 return "G"; /* away */
810 who_flags_qualifier(const char *chan_user_modes)
812 if (strchr(chan_user_modes, 'o'))
814 else if (strchr(chan_user_modes, 'v'))
821 * Send WHO reply for a "channel target" ("WHO #channel").
823 * @param Client Client requesting the information.
824 * @param Chan Channel being requested.
825 * @param OnlyOps Only display IRC operators.
826 * @return CONNECTED or DISCONNECTED.
829 IRC_WHO_Channel(CLIENT *Client, CHANNEL *Chan, bool OnlyOps)
831 bool is_visible, is_member, is_ircop;
833 const char *client_modes;
834 const char *chan_user_modes;
839 assert( Client != NULL );
840 assert( Chan != NULL );
842 is_member = Channel_IsMemberOf(Chan, Client);
844 /* Secret channel? */
845 if (!is_member && strchr(Channel_Modes(Chan), 's'))
846 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG,
847 Client_ID(Client), Channel_Name(Chan));
849 cl2chan = Channel_FirstMember(Chan);
850 for (; cl2chan ; cl2chan = Channel_NextMember(Chan, cl2chan)) {
851 c = Channel_GetClient(cl2chan);
853 client_modes = Client_Modes(c);
854 is_ircop = strchr(client_modes, 'o') != NULL;
855 if (OnlyOps && !is_ircop)
858 is_visible = strchr(client_modes, 'i') == NULL;
859 if (is_member || is_visible) {
860 if (IRC_CheckListTooBig(Client, count, MAX_RPL_WHO, "WHO"))
863 strcpy(flags, who_flags_status(client_modes));
865 strlcat(flags, "*", sizeof(flags));
867 chan_user_modes = Channel_UserModes(Chan, c);
868 strlcat(flags, who_flags_qualifier(chan_user_modes),
871 if (!write_whoreply(Client, c, Channel_Name(Chan),
877 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client),
883 * Send WHO reply for a "mask target" ("WHO m*sk").
885 * @param Client Client requesting the information.
886 * @param Mask Mask being requested or NULL for "all" clients.
887 * @param OnlyOps Only display IRC operators.
888 * @return CONNECTED or DISCONNECTED.
891 IRC_WHO_Mask(CLIENT *Client, char *Mask, bool OnlyOps)
896 bool client_match, is_visible;
900 assert (Client != NULL);
905 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
906 if (Client_Type(c) != CLIENT_USER)
909 if (OnlyOps && !Client_HasMode(c, 'o'))
913 /* Match pattern against user host/server/name/nick */
914 client_match = MatchCaseInsensitive(Mask,
917 client_match = MatchCaseInsensitive(Mask,
918 Client_ID(Client_Introducer(c)));
920 client_match = MatchCaseInsensitive(Mask,
923 client_match = MatchCaseInsensitive(Mask,
926 continue; /* no match: skip this client */
929 is_visible = !Client_HasMode(c, 'i');
931 /* Target client is invisible, but mask matches exactly? */
932 if (!is_visible && Mask && strcasecmp(Client_ID(c), Mask) == 0)
935 /* Target still invisible, but are both on the same channel? */
937 cl2chan = Channel_FirstChannelOf(Client);
938 while (cl2chan && !is_visible) {
939 chan = Channel_GetChannel(cl2chan);
940 if (Channel_IsMemberOf(chan, c))
942 cl2chan = Channel_NextChannelOf(Client, cl2chan);
946 if (!is_visible) /* target user is not visible */
949 if (IRC_CheckListTooBig(Client, count, MAX_RPL_WHO, "WHO"))
952 strcpy(flags, who_flags_status(Client_Modes(c)));
953 if (strchr(Client_Modes(c), 'o'))
954 strlcat(flags, "*", sizeof(flags));
956 if (!write_whoreply(Client, c, "*", flags))
961 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client),
967 * Handler for the IRC "WHO" command.
969 * See RFC 2812, 3.6.1 "Who query".
971 * @param Client The client from which this command has been received.
972 * @param Req Request structure with prefix and all parameters.
973 * @return CONNECTED or DISCONNECTED.
976 IRC_WHO(CLIENT *Client, REQUEST *Req)
981 assert (Client != NULL);
982 assert (Req != NULL);
985 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
986 Client_ID(Client), Req->command);
990 if (Req->argc == 2) {
991 if (strcmp(Req->argv[1], "o") == 0)
995 return IRC_WriteStrClient(Client,
996 ERR_NEEDMOREPARAMS_MSG,
1002 IRC_SetPenalty(Client, 1);
1003 if (Req->argc >= 1) {
1004 /* Channel or mask given */
1005 chan = Channel_Search(Req->argv[0]);
1007 /* Members of a channel have been requested */
1008 IRC_SetPenalty(Client, 1);
1009 return IRC_WHO_Channel(Client, chan, only_ops);
1011 if (strcmp(Req->argv[0], "0") != 0) {
1012 /* A mask has been given. But please note this RFC
1013 * stupidity: "0" is same as no arguments ... */
1014 IRC_SetPenalty(Client, 3);
1015 return IRC_WHO_Mask(Client, Req->argv[0], only_ops);
1019 /* No channel or (valid) mask given */
1020 IRC_SetPenalty(Client, 2);
1021 return IRC_WHO_Mask(Client, NULL, only_ops);
1026 * Generate WHOIS reply of one actual client.
1028 * @param Client The client from which this command has been received.
1029 * @param from The client requesting the information ("originator").
1030 * @param c The client of which information should be returned.
1031 * @return CONNECTED or DISCONNECTED.
1034 IRC_WHOIS_SendReply(CLIENT *Client, CLIENT *from, CLIENT *c)
1036 char str[LINE_LEN + 1];
1040 assert(Client != NULL);
1041 assert(from != NULL);
1044 /* Nick, user, hostname and client info */
1045 if (!IRC_WriteStrClient(from, RPL_WHOISUSER_MSG, Client_ID(from),
1046 Client_ID(c), Client_User(c),
1047 Client_HostnameCloaked(c), Client_Info(c)))
1048 return DISCONNECTED;
1051 if (!IRC_WriteStrClient(from, RPL_WHOISSERVER_MSG, Client_ID(from),
1052 Client_ID(c), Client_ID(Client_Introducer(c)),
1053 Client_Info(Client_Introducer(c))))
1054 return DISCONNECTED;
1057 snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
1058 Client_ID(from), Client_ID(c));
1059 cl2chan = Channel_FirstChannelOf(c);
1061 chan = Channel_GetChannel(cl2chan);
1062 assert(chan != NULL);
1065 cl2chan = Channel_NextChannelOf(c, cl2chan);
1067 /* Secret channel? */
1068 if (strchr(Channel_Modes(chan), 's')
1069 && !Channel_IsMemberOf(chan, Client))
1072 /* Local channel and request is not from a user? */
1073 if (Client_Type(Client) == CLIENT_SERVER
1074 && Channel_IsLocal(chan))
1077 /* Concatenate channel names */
1078 if (str[strlen(str) - 1] != ':')
1079 strlcat(str, " ", sizeof(str));
1081 strlcat(str, who_flags_qualifier(Channel_UserModes(chan, c)),
1083 strlcat(str, Channel_Name(chan), sizeof(str));
1085 if (strlen(str) > (LINE_LEN - CHANNEL_NAME_LEN - 4)) {
1086 /* Line becomes too long: send it! */
1087 if (!IRC_WriteStrClient(Client, "%s", str))
1088 return DISCONNECTED;
1089 snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
1090 Client_ID(from), Client_ID(c));
1093 if(str[strlen(str) - 1] != ':') {
1094 /* There is data left to send: */
1095 if (!IRC_WriteStrClient(Client, "%s", str))
1096 return DISCONNECTED;
1100 if (Client_HasMode(c, 'o') &&
1101 !IRC_WriteStrClient(from, RPL_WHOISOPERATOR_MSG,
1102 Client_ID(from), Client_ID(c)))
1103 return DISCONNECTED;
1105 /* Connected using SSL? */
1106 if (Conn_UsesSSL(Client_Conn(c)) &&
1107 !IRC_WriteStrClient(from, RPL_WHOISSSL_MSG, Client_ID(from),
1109 return DISCONNECTED;
1111 /* Registered nick name? */
1112 if (Client_HasMode(c, 'R') &&
1113 !IRC_WriteStrClient(from, RPL_WHOISREGNICK_MSG,
1114 Client_ID(from), Client_ID(c)))
1115 return DISCONNECTED;
1117 if (Client_Conn(c) > NONE && (Client_OperByMe(from) || from == c) &&
1118 !IRC_WriteStrClient(from, RPL_WHOISHOST_MSG, Client_ID(from),
1119 Client_ID(c), Client_Hostname(c),
1120 Conn_GetIPAInfo(Client_Conn(c))))
1121 return DISCONNECTED;
1123 /* Idle and signon time (local clients only!) */
1124 if (!Conf_MorePrivacy && Client_Conn(c) > NONE &&
1125 !IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
1126 Client_ID(from), Client_ID(c),
1127 (unsigned long)Conn_GetIdle(Client_Conn(c)),
1128 (unsigned long)Conn_GetSignon(Client_Conn(c))))
1129 return DISCONNECTED;
1132 if (Client_HasMode(c, 'a') &&
1133 !IRC_WriteStrClient(from, RPL_AWAY_MSG,
1134 Client_ID(from), Client_ID(c), Client_Away(c)))
1135 return DISCONNECTED;
1138 } /* IRC_WHOIS_SendReply */
1142 * Handler for the IRC "WHOIS" command.
1144 * See RFC 2812, 3.6.2 "Whois query".
1146 * @param Client The client from which this command has been received.
1147 * @param Req Request structure with prefix and all parameters.
1148 * @return CONNECTED or DISCONNECTED.
1151 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
1153 CLIENT *from, *target, *c;
1154 unsigned int match_count = 0, found = 0;
1155 bool has_wildcards, is_remote;
1156 bool got_wildcard = false;
1157 char mask[COMMAND_LEN], *query;
1159 assert( Client != NULL );
1160 assert( Req != NULL );
1162 /* Bad number of parameters? */
1163 if (Req->argc < 1 || Req->argc > 2)
1164 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1165 Client_ID(Client), Req->command);
1167 /* Search sender of the WHOIS */
1168 if (Client_Type(Client) == CLIENT_SERVER) {
1169 from = Client_Search(Req->prefix);
1171 IRC_SetPenalty(Client, 1);
1175 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1176 Client_ID(Client), Req->prefix);
1178 /* Get target server for this command */
1179 if (Req->argc > 1) {
1180 /* Search the target server, which can be specified as a
1181 * nick name on that server as well: */
1182 target = Client_Search(Req->argv[0]);
1184 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
1185 Client_ID(from), Req->argv[0]);
1187 target = Client_ThisServer();
1188 assert(target != NULL);
1190 /* Forward to other server? */
1191 if (Client_NextHop(target) != Client_ThisServer() &&
1192 Client_Type(Client_NextHop(target)) == CLIENT_SERVER)
1193 return IRC_WriteStrClientPrefix(target, from,
1195 Req->argv[0], Req->argv[1]);
1197 is_remote = Client_Conn(from) < 0;
1198 strlcpy(mask, Req->argv[Req->argc - 1], sizeof(mask));
1199 for (query = strtok(ngt_LowerStr(mask), ",");
1201 query = strtok(NULL, ","), found++)
1203 has_wildcards = query[strcspn(query, "*?")] != 0;
1205 * follows ircd 2.10 implementation:
1206 * - handle up to 3 targets
1207 * - no wildcards for remote clients
1208 * - only one wildcard target per local client
1210 * Also, at most MAX_RPL_WHOIS matches are returned.
1212 if (!has_wildcards || is_remote) {
1213 c = Client_Search(query);
1214 if (c && Client_Type(c) == CLIENT_USER) {
1215 if (!IRC_WHOIS_SendReply(Client, from, c))
1216 return DISCONNECTED;
1218 if (!IRC_WriteStrClient(Client,
1222 return DISCONNECTED;
1227 /* we already handled one wildcard query */
1228 if (!IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1229 Client_ID(Client), query))
1230 return DISCONNECTED;
1233 got_wildcard = true;
1234 IRC_SetPenalty(Client, 3);
1236 for (c = Client_First(); c; c = Client_Next(c)) {
1237 if (IRC_CheckListTooBig(Client, match_count,
1238 MAX_RPL_WHOIS, "WHOIS"))
1241 if (Client_Type(c) != CLIENT_USER)
1243 if (!MatchCaseInsensitive(query, Client_ID(c)))
1245 if (!IRC_WHOIS_SendReply(Client, from, c))
1246 return DISCONNECTED;
1251 if (match_count == 0)
1252 IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1254 Req->argv[Req->argc - 1]);
1256 return IRC_WriteStrClient(from, RPL_ENDOFWHOIS_MSG,
1257 Client_ID(from), Req->argv[Req->argc - 1]);
1262 WHOWAS_EntryWrite(CLIENT *prefix, WHOWAS *entry)
1266 (void)strftime(t_str, sizeof(t_str), "%a %b %d %H:%M:%S %Y",
1267 localtime(&entry->time));
1269 if (!IRC_WriteStrClient(prefix, RPL_WHOWASUSER_MSG, Client_ID(prefix),
1270 entry->id, entry->user, entry->host, entry->info))
1271 return DISCONNECTED;
1273 return IRC_WriteStrClient(prefix, RPL_WHOISSERVER_MSG, Client_ID(prefix),
1274 entry->id, entry->server, t_str);
1278 * IRC "WHOWAS" function.
1279 * This function implements the IRC command "WHOWHAS". It handles local
1280 * requests and request that should be forwarded to other servers.
1283 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1285 CLIENT *target, *prefix;
1287 char tok_buf[COMMAND_LEN];
1288 int max, last, count, i, nc;
1291 assert( Client != NULL );
1292 assert( Req != NULL );
1294 /* Do not reveal any info on disconnected users? */
1295 if (Conf_MorePrivacy)
1298 /* Wrong number of parameters? */
1300 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1301 Client_ID(Client), Req->command);
1303 return IRC_WriteStrClient(Client, ERR_NONICKNAMEGIVEN_MSG, Client_ID(Client));
1307 target = Client_Search(Req->argv[2]);
1309 target = Client_ThisServer();
1312 if (Client_Type(Client) == CLIENT_SERVER)
1313 prefix = Client_Search(Req->prefix);
1318 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1319 Client_ID(Client), Req->prefix);
1321 /* Forward to other server? */
1322 if (target != Client_ThisServer()) {
1323 if (!target || (Client_Type(target) != CLIENT_SERVER))
1324 return IRC_WriteStrClient(prefix, ERR_NOSUCHSERVER_MSG,
1325 Client_ID(prefix), Req->argv[2]);
1328 IRC_WriteStrClientPrefix( target, prefix, "WHOWAS %s %s %s",
1329 Req->argv[0], Req->argv[1],
1334 whowas = Client_GetWhowas( );
1335 last = Client_GetLastWhowasIndex( );
1339 max = DEF_RPL_WHOWAS;
1340 if (Req->argc > 1) {
1341 max = atoi(Req->argv[1]);
1343 max = MAX_RPL_WHOWAS;
1347 * Break up the nick argument into a list of nicks, if applicable
1348 * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1350 strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1351 nick = strtok(tok_buf, ",");
1353 for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1357 if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1358 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1359 return DISCONNECTED;
1363 /* previous entry */
1366 /* "underflow", wrap around */
1370 if (nc && count >= max)
1372 } while (i != last);
1374 if (nc == 0 && !IRC_WriteStrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1375 Client_ID(prefix), nick))
1376 return DISCONNECTED;
1378 return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG, Client_ID(prefix), Req->argv[0]);
1383 * Send LUSERS reply to a client.
1385 * @param Client The receipient of the information.
1386 * @return CONNECTED or DISCONNECTED.
1389 IRC_Send_LUSERS(CLIENT *Client)
1396 assert(Client != NULL);
1398 /* Users, services and serevers in the network */
1399 if (!IRC_WriteStrClient(Client, RPL_LUSERCLIENT_MSG, Client_ID(Client),
1400 Client_UserCount(), Client_ServiceCount(),
1401 Client_ServerCount()))
1402 return DISCONNECTED;
1404 /* Number of IRC operators */
1405 cnt = Client_OperCount( );
1407 if (!IRC_WriteStrClient(Client, RPL_LUSEROP_MSG,
1408 Client_ID(Client), cnt))
1409 return DISCONNECTED;
1412 /* Unknown connections */
1413 cnt = Client_UnknownCount( );
1415 if (!IRC_WriteStrClient(Client, RPL_LUSERUNKNOWN_MSG,
1416 Client_ID(Client), cnt))
1417 return DISCONNECTED;
1420 /* Number of created channels */
1421 if (!IRC_WriteStrClient(Client, RPL_LUSERCHANNELS_MSG,
1423 Channel_CountVisible(Client)))
1424 return DISCONNECTED;
1426 /* Number of local users, services and servers */
1427 if (!IRC_WriteStrClient(Client, RPL_LUSERME_MSG, Client_ID(Client),
1428 Client_MyUserCount(), Client_MyServiceCount(),
1429 Client_MyServerCount()))
1430 return DISCONNECTED;
1433 /* Maximum number of local users */
1434 cnt = Client_MyUserCount();
1435 max = Client_MyMaxUserCount();
1436 if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1437 cnt, max, cnt, max))
1438 return DISCONNECTED;
1439 /* Maximum number of users in the network */
1440 cnt = Client_UserCount();
1441 max = Client_MaxUserCount();
1442 if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1443 cnt, max, cnt, max))
1444 return DISCONNECTED;
1445 /* Connection counters */
1446 if (! IRC_WriteStrClient(Client, RPL_STATSCONN_MSG, Client_ID(Client),
1447 Conn_CountMax(), Conn_CountAccepted()))
1448 return DISCONNECTED;
1452 } /* IRC_Send_LUSERS */
1456 Show_MOTD_Start(CLIENT *Client)
1458 return IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG,
1459 Client_ID( Client ), Client_ID( Client_ThisServer( )));
1463 Show_MOTD_Sendline(CLIENT *Client, const char *msg)
1465 return IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID( Client ), msg);
1469 Show_MOTD_End(CLIENT *Client)
1471 return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
1475 static bool Show_MOTD_SSLInfo(CLIENT *Client)
1478 char buf[COMMAND_LEN] = "Connected using Cipher ";
1480 if (!Conn_GetCipherInfo(Client_Conn(Client), buf + 23, sizeof buf - 23))
1483 if (!Show_MOTD_Sendline(Client, buf))
1490 Show_MOTD_SSLInfo(UNUSED CLIENT *c)
1495 IRC_Show_MOTD( CLIENT *Client )
1498 size_t len_tot, len_str;
1500 assert( Client != NULL );
1502 len_tot = array_bytes(&Conf_Motd);
1503 if (len_tot == 0 && !Conn_UsesSSL(Client_Conn(Client)))
1504 return IRC_WriteStrClient(Client, ERR_NOMOTD_MSG, Client_ID(Client));
1506 if (!Show_MOTD_Start(Client))
1507 return DISCONNECTED;
1509 line = array_start(&Conf_Motd);
1510 while (len_tot > 0) {
1511 len_str = strlen(line) + 1;
1513 assert(len_tot >= len_str);
1516 if (!Show_MOTD_Sendline(Client, line))
1517 return DISCONNECTED;
1521 if (!Show_MOTD_SSLInfo(Client))
1522 return DISCONNECTED;
1523 return Show_MOTD_End(Client);
1524 } /* IRC_Show_MOTD */
1528 * Send NAMES reply for a specific client and channel.
1530 * @param Client The client requesting the NAMES information.
1531 * @param Chan The channel
1532 * @return CONNECTED or DISCONNECTED.
1535 IRC_Send_NAMES(CLIENT * Client, CHANNEL * Chan)
1537 bool is_visible, is_member;
1538 char str[LINE_LEN + 1];
1542 assert(Client != NULL);
1543 assert(Chan != NULL);
1545 if (Channel_IsMemberOf(Chan, Client))
1550 /* Do not print info on channel memberships to anyone that is not member? */
1551 if (Conf_MorePrivacy && !is_member)
1554 /* Secret channel? */
1555 if (!is_member && strchr(Channel_Modes(Chan), 's'))
1558 snprintf(str, sizeof(str), RPL_NAMREPLY_MSG, Client_ID(Client), "=",
1559 Channel_Name(Chan));
1560 cl2chan = Channel_FirstMember(Chan);
1562 cl = Channel_GetClient(cl2chan);
1564 if (strchr(Client_Modes(cl), 'i'))
1569 if (is_member || is_visible) {
1570 if (str[strlen(str) - 1] != ':')
1571 strlcat(str, " ", sizeof(str));
1572 if (strchr(Channel_UserModes(Chan, cl), 'o'))
1573 strlcat(str, "@", sizeof(str));
1574 else if (strchr(Channel_UserModes(Chan, cl), 'v'))
1575 strlcat(str, "+", sizeof(str));
1576 strlcat(str, Client_ID(cl), sizeof(str));
1578 if (strlen(str) > (LINE_LEN - CLIENT_NICK_LEN - 4)) {
1579 if (!IRC_WriteStrClient(Client, "%s", str))
1580 return DISCONNECTED;
1581 snprintf(str, sizeof(str), RPL_NAMREPLY_MSG,
1582 Client_ID(Client), "=",
1583 Channel_Name(Chan));
1587 cl2chan = Channel_NextMember(Chan, cl2chan);
1589 if (str[strlen(str) - 1] != ':') {
1590 if (!IRC_WriteStrClient(Client, "%s", str))
1591 return DISCONNECTED;
1595 } /* IRC_Send_NAMES */
1599 * Send the ISUPPORT numeric (005).
1600 * This numeric indicates the features that are supported by this server.
1601 * See <http://www.irc.org/tech_docs/005.html> for details.
1604 IRC_Send_ISUPPORT(CLIENT * Client)
1606 if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1608 return DISCONNECTED;
1609 return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1610 CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1611 COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1612 COMMAND_LEN - 113, MAX_HNDL_MODES_ARG,
1613 MAX_HNDL_CHANNEL_LISTS);
1614 } /* IRC_Send_ISUPPORT */