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, REQUEST * Req)
653 return IRC_WriteStrClient(Client, ERR_SUMMONDISABLED_MSG,
654 Client_ID(Client), Req->command);
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, REQUEST * Req)
746 return IRC_WriteStrClient(Client, ERR_USERSDISABLED_MSG,
747 Client_ID(Client), Req->command);
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 /* Idle and signon time (local clients only!) */
1118 if (!Conf_MorePrivacy && Client_Conn(c) > NONE &&
1119 !IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
1120 Client_ID(from), Client_ID(c),
1121 (unsigned long)Conn_GetIdle(Client_Conn(c)),
1122 (unsigned long)Conn_GetSignon(Client_Conn(c))))
1123 return DISCONNECTED;
1126 if (Client_HasMode(c, 'a') &&
1127 !IRC_WriteStrClient(from, RPL_AWAY_MSG,
1128 Client_ID(from), Client_ID(c), Client_Away(c)))
1129 return DISCONNECTED;
1132 } /* IRC_WHOIS_SendReply */
1136 * Handler for the IRC "WHOIS" command.
1138 * See RFC 2812, 3.6.2 "Whois query".
1140 * @param Client The client from which this command has been received.
1141 * @param Req Request structure with prefix and all parameters.
1142 * @return CONNECTED or DISCONNECTED.
1145 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
1147 CLIENT *from, *target, *c;
1148 unsigned int match_count = 0, found = 0;
1149 bool has_wildcards, is_remote;
1150 bool got_wildcard = false;
1151 char mask[COMMAND_LEN], *query;
1153 assert( Client != NULL );
1154 assert( Req != NULL );
1156 /* Bad number of parameters? */
1157 if (Req->argc < 1 || Req->argc > 2)
1158 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1159 Client_ID(Client), Req->command);
1161 /* Search sender of the WHOIS */
1162 if (Client_Type(Client) == CLIENT_SERVER) {
1163 from = Client_Search(Req->prefix);
1165 IRC_SetPenalty(Client, 1);
1169 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1170 Client_ID(Client), Req->prefix);
1172 /* Get target server for this command */
1173 if (Req->argc > 1) {
1174 /* Search the target server, which can be specified as a
1175 * nick name on that server as well: */
1176 target = Client_Search(Req->argv[0]);
1178 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
1179 Client_ID(from), Req->argv[0]);
1181 target = Client_ThisServer();
1182 assert(target != NULL);
1184 /* Forward to other server? */
1185 if (Client_NextHop(target) != Client_ThisServer() &&
1186 Client_Type(Client_NextHop(target)) == CLIENT_SERVER)
1187 return IRC_WriteStrClientPrefix(target, from,
1189 Req->argv[0], Req->argv[1]);
1191 is_remote = Client_Conn(from) < 0;
1192 strlcpy(mask, Req->argv[Req->argc - 1], sizeof(mask));
1193 for (query = strtok(ngt_LowerStr(mask), ",");
1195 query = strtok(NULL, ","), found++)
1197 has_wildcards = query[strcspn(query, "*?")] != 0;
1199 * follows ircd 2.10 implementation:
1200 * - handle up to 3 targets
1201 * - no wildcards for remote clients
1202 * - only one wildcard target per local client
1204 * Also, at most MAX_RPL_WHOIS matches are returned.
1206 if (!has_wildcards || is_remote) {
1207 c = Client_Search(query);
1208 if (c && Client_Type(c) == CLIENT_USER) {
1209 if (!IRC_WHOIS_SendReply(Client, from, c))
1210 return DISCONNECTED;
1212 if (!IRC_WriteStrClient(Client,
1216 return DISCONNECTED;
1221 /* we already handled one wildcard query */
1222 if (!IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1223 Client_ID(Client), query))
1224 return DISCONNECTED;
1227 got_wildcard = true;
1228 IRC_SetPenalty(Client, 3);
1230 for (c = Client_First(); c; c = Client_Next(c)) {
1231 if (IRC_CheckListTooBig(Client, match_count,
1232 MAX_RPL_WHOIS, "WHOIS"))
1235 if (Client_Type(c) != CLIENT_USER)
1237 if (!MatchCaseInsensitive(query, Client_ID(c)))
1239 if (!IRC_WHOIS_SendReply(Client, from, c))
1240 return DISCONNECTED;
1245 if (match_count == 0)
1246 IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1248 Req->argv[Req->argc - 1]);
1250 return IRC_WriteStrClient(from, RPL_ENDOFWHOIS_MSG,
1251 Client_ID(from), Req->argv[Req->argc - 1]);
1256 WHOWAS_EntryWrite(CLIENT *prefix, WHOWAS *entry)
1260 (void)strftime(t_str, sizeof(t_str), "%a %b %d %H:%M:%S %Y",
1261 localtime(&entry->time));
1263 if (!IRC_WriteStrClient(prefix, RPL_WHOWASUSER_MSG, Client_ID(prefix),
1264 entry->id, entry->user, entry->host, entry->info))
1265 return DISCONNECTED;
1267 return IRC_WriteStrClient(prefix, RPL_WHOISSERVER_MSG, Client_ID(prefix),
1268 entry->id, entry->server, t_str);
1272 * IRC "WHOWAS" function.
1273 * This function implements the IRC command "WHOWHAS". It handles local
1274 * requests and request that should be forwarded to other servers.
1277 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1279 CLIENT *target, *prefix;
1281 char tok_buf[COMMAND_LEN];
1282 int max, last, count, i, nc;
1285 assert( Client != NULL );
1286 assert( Req != NULL );
1288 /* Do not reveal any info on disconnected users? */
1289 if (Conf_MorePrivacy)
1292 /* Wrong number of parameters? */
1294 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1295 Client_ID(Client), Req->command);
1297 return IRC_WriteStrClient(Client, ERR_NONICKNAMEGIVEN_MSG, Client_ID(Client));
1301 target = Client_Search(Req->argv[2]);
1303 target = Client_ThisServer();
1306 if (Client_Type(Client) == CLIENT_SERVER)
1307 prefix = Client_Search(Req->prefix);
1312 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1313 Client_ID(Client), Req->prefix);
1315 /* Forward to other server? */
1316 if (target != Client_ThisServer()) {
1317 if (!target || (Client_Type(target) != CLIENT_SERVER))
1318 return IRC_WriteStrClient(prefix, ERR_NOSUCHSERVER_MSG,
1319 Client_ID(prefix), Req->argv[2]);
1322 IRC_WriteStrClientPrefix( target, prefix, "WHOWAS %s %s %s",
1323 Req->argv[0], Req->argv[1],
1328 whowas = Client_GetWhowas( );
1329 last = Client_GetLastWhowasIndex( );
1333 max = DEF_RPL_WHOWAS;
1334 if (Req->argc > 1) {
1335 max = atoi(Req->argv[1]);
1337 max = MAX_RPL_WHOWAS;
1341 * Break up the nick argument into a list of nicks, if applicable
1342 * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1344 strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1345 nick = strtok(tok_buf, ",");
1347 for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1351 if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1352 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1353 return DISCONNECTED;
1357 /* previous entry */
1360 /* "underflow", wrap around */
1364 if (nc && count >= max)
1366 } while (i != last);
1368 if (nc == 0 && !IRC_WriteStrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1369 Client_ID(prefix), nick))
1370 return DISCONNECTED;
1372 return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG, Client_ID(prefix), Req->argv[0]);
1377 * Send LUSERS reply to a client.
1379 * @param Client The receipient of the information.
1380 * @return CONNECTED or DISCONNECTED.
1383 IRC_Send_LUSERS(CLIENT *Client)
1390 assert(Client != NULL);
1392 /* Users, services and serevers in the network */
1393 if (!IRC_WriteStrClient(Client, RPL_LUSERCLIENT_MSG, Client_ID(Client),
1394 Client_UserCount(), Client_ServiceCount(),
1395 Client_ServerCount()))
1396 return DISCONNECTED;
1398 /* Number of IRC operators */
1399 cnt = Client_OperCount( );
1401 if (!IRC_WriteStrClient(Client, RPL_LUSEROP_MSG,
1402 Client_ID(Client), cnt))
1403 return DISCONNECTED;
1406 /* Unknown connections */
1407 cnt = Client_UnknownCount( );
1409 if (!IRC_WriteStrClient(Client, RPL_LUSERUNKNOWN_MSG,
1410 Client_ID(Client), cnt))
1411 return DISCONNECTED;
1414 /* Number of created channels */
1415 if (!IRC_WriteStrClient(Client, RPL_LUSERCHANNELS_MSG,
1417 Channel_CountVisible(Client)))
1418 return DISCONNECTED;
1420 /* Number of local users, services and servers */
1421 if (!IRC_WriteStrClient(Client, RPL_LUSERME_MSG, Client_ID(Client),
1422 Client_MyUserCount(), Client_MyServiceCount(),
1423 Client_MyServerCount()))
1424 return DISCONNECTED;
1427 /* Maximum number of local users */
1428 cnt = Client_MyUserCount();
1429 max = Client_MyMaxUserCount();
1430 if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1431 cnt, max, cnt, max))
1432 return DISCONNECTED;
1433 /* Maximum number of users in the network */
1434 cnt = Client_UserCount();
1435 max = Client_MaxUserCount();
1436 if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1437 cnt, max, cnt, max))
1438 return DISCONNECTED;
1439 /* Connection counters */
1440 if (! IRC_WriteStrClient(Client, RPL_STATSCONN_MSG, Client_ID(Client),
1441 Conn_CountMax(), Conn_CountAccepted()))
1442 return DISCONNECTED;
1446 } /* IRC_Send_LUSERS */
1450 Show_MOTD_Start(CLIENT *Client)
1452 return IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG,
1453 Client_ID( Client ), Client_ID( Client_ThisServer( )));
1457 Show_MOTD_Sendline(CLIENT *Client, const char *msg)
1459 return IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID( Client ), msg);
1463 Show_MOTD_End(CLIENT *Client)
1465 return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
1469 static bool Show_MOTD_SSLInfo(CLIENT *Client)
1472 char buf[COMMAND_LEN] = "Connected using Cipher ";
1474 if (!Conn_GetCipherInfo(Client_Conn(Client), buf + 23, sizeof buf - 23))
1477 if (!Show_MOTD_Sendline(Client, buf))
1484 Show_MOTD_SSLInfo(UNUSED CLIENT *c)
1489 IRC_Show_MOTD( CLIENT *Client )
1492 size_t len_tot, len_str;
1494 assert( Client != NULL );
1496 len_tot = array_bytes(&Conf_Motd);
1497 if (len_tot == 0 && !Conn_UsesSSL(Client_Conn(Client)))
1498 return IRC_WriteStrClient(Client, ERR_NOMOTD_MSG, Client_ID(Client));
1500 if (!Show_MOTD_Start(Client))
1501 return DISCONNECTED;
1503 line = array_start(&Conf_Motd);
1504 while (len_tot > 0) {
1505 len_str = strlen(line) + 1;
1507 assert(len_tot >= len_str);
1510 if (!Show_MOTD_Sendline(Client, line))
1511 return DISCONNECTED;
1515 if (!Show_MOTD_SSLInfo(Client))
1516 return DISCONNECTED;
1517 return Show_MOTD_End(Client);
1518 } /* IRC_Show_MOTD */
1522 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
1524 bool is_visible, is_member;
1525 char str[LINE_LEN + 1];
1529 assert( Client != NULL );
1530 assert( Chan != NULL );
1532 if( Channel_IsMemberOf( Chan, Client )) is_member = true;
1533 else is_member = false;
1535 /* Do not print info on channel memberships to anyone that is not member? */
1536 if (Conf_MorePrivacy && !is_member)
1539 /* Secret channel? */
1540 if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
1542 /* Alle Mitglieder suchen */
1543 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1544 cl2chan = Channel_FirstMember( Chan );
1547 cl = Channel_GetClient( cl2chan );
1549 if( strchr( Client_Modes( cl ), 'i' )) is_visible = false;
1550 else is_visible = true;
1552 if( is_member || is_visible )
1554 /* Nick anhaengen */
1555 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
1556 if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
1557 else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
1558 strlcat( str, Client_ID( cl ), sizeof( str ));
1560 if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
1562 /* Zeile wird zu lang: senden! */
1563 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1564 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1568 /* naechstes Mitglied suchen */
1569 cl2chan = Channel_NextMember( Chan, cl2chan );
1571 if( str[strlen( str ) - 1] != ':')
1573 /* Es sind noch Daten da, die gesendet werden muessen */
1574 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1578 } /* IRC_Send_NAMES */
1582 * Send the ISUPPORT numeric (005).
1583 * This numeric indicates the features that are supported by this server.
1584 * See <http://www.irc.org/tech_docs/005.html> for details.
1587 IRC_Send_ISUPPORT(CLIENT * Client)
1589 if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1591 return DISCONNECTED;
1592 return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1593 CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1594 COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1595 COMMAND_LEN - 113, MAX_HNDL_MODES_ARG,
1596 MAX_HNDL_CHANNEL_LISTS);
1597 } /* IRC_Send_ISUPPORT */