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 * @returns CONNECTED or DISCONNECTED.
1034 IRC_WHOIS_SendReply(CLIENT *Client, CLIENT *from, CLIENT *c)
1036 char str[LINE_LEN + 1];
1040 /* Nick, user, hostname and client info */
1041 if (!IRC_WriteStrClient(from, RPL_WHOISUSER_MSG, Client_ID(from),
1042 Client_ID(c), Client_User(c),
1043 Client_HostnameCloaked(c), Client_Info(c)))
1044 return DISCONNECTED;
1047 if (!IRC_WriteStrClient(from, RPL_WHOISSERVER_MSG, Client_ID(from),
1048 Client_ID(c), Client_ID(Client_Introducer(c)),
1049 Client_Info(Client_Introducer(c))))
1050 return DISCONNECTED;
1053 snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
1054 Client_ID(from), Client_ID(c));
1055 cl2chan = Channel_FirstChannelOf(c);
1057 chan = Channel_GetChannel(cl2chan);
1058 assert(chan != NULL);
1061 cl2chan = Channel_NextChannelOf(c, cl2chan);
1063 /* Secret channel? */
1064 if (strchr(Channel_Modes(chan), 's')
1065 && !Channel_IsMemberOf(chan, Client))
1068 /* Local channel and request is not from a user? */
1069 if (Client_Type(Client) == CLIENT_SERVER
1070 && Channel_IsLocal(chan))
1073 /* Concatenate channel names */
1074 if (str[strlen(str) - 1] != ':')
1075 strlcat(str, " ", sizeof(str));
1077 strlcat(str, who_flags_qualifier(Channel_UserModes(chan, c)),
1079 strlcat(str, Channel_Name(chan), sizeof(str));
1081 if (strlen(str) > (LINE_LEN - CHANNEL_NAME_LEN - 4)) {
1082 /* Line becomes too long: send it! */
1083 if (!IRC_WriteStrClient(Client, "%s", str))
1084 return DISCONNECTED;
1085 snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
1086 Client_ID(from), Client_ID(c));
1089 if(str[strlen(str) - 1] != ':') {
1090 /* There is data left to send: */
1091 if (!IRC_WriteStrClient(Client, "%s", str))
1092 return DISCONNECTED;
1096 if (Client_HasMode(c, 'o') &&
1097 !IRC_WriteStrClient(from, RPL_WHOISOPERATOR_MSG,
1098 Client_ID(from), Client_ID(c)))
1099 return DISCONNECTED;
1101 /* Connected using SSL? */
1102 if (Conn_UsesSSL(Client_Conn(c)) &&
1103 !IRC_WriteStrClient(from, RPL_WHOISSSL_MSG,
1104 Client_ID(from), Client_ID(c)))
1105 return DISCONNECTED;
1107 /* Idle and signon time (local clients only!) */
1108 if (!Conf_MorePrivacy && Client_Conn(c) > NONE &&
1109 !IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
1110 Client_ID(from), Client_ID(c),
1111 (unsigned long)Conn_GetIdle(Client_Conn(c)),
1112 (unsigned long)Conn_GetSignon(Client_Conn(c))))
1113 return DISCONNECTED;
1116 if (Client_HasMode(c, 'a') &&
1117 !IRC_WriteStrClient(from, RPL_AWAY_MSG,
1118 Client_ID(from), Client_ID(c),
1120 return DISCONNECTED;
1123 } /* IRC_WHOIS_SendReply */
1127 * Handler for the IRC "WHOIS" command.
1129 * See RFC 2812, 3.6.2 "Whois query".
1131 * @param Client The client from which this command has been received.
1132 * @param Req Request structure with prefix and all parameters.
1133 * @return CONNECTED or DISCONNECTED.
1136 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
1138 CLIENT *from, *target, *c;
1139 unsigned int match_count = 0, found = 0;
1140 bool has_wildcards, is_remote;
1141 bool got_wildcard = false;
1142 char mask[COMMAND_LEN], *query;
1144 assert( Client != NULL );
1145 assert( Req != NULL );
1147 /* Bad number of parameters? */
1148 if (Req->argc < 1 || Req->argc > 2)
1149 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1150 Client_ID(Client), Req->command);
1152 /* Search sender of the WHOIS */
1153 if (Client_Type(Client) == CLIENT_SERVER) {
1154 from = Client_Search(Req->prefix);
1156 IRC_SetPenalty(Client, 1);
1160 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1161 Client_ID(Client), Req->prefix);
1163 /* Get target server for this command */
1164 if (Req->argc > 1) {
1165 /* Search the target server, which can be specified as a
1166 * nick name on that server as well: */
1167 target = Client_Search(Req->argv[0]);
1169 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
1170 Client_ID(from), Req->argv[0]);
1172 target = Client_ThisServer();
1173 assert(target != NULL);
1175 /* Forward to other server? */
1176 if (Client_NextHop(target) != Client_ThisServer() &&
1177 Client_Type(Client_NextHop(target)) == CLIENT_SERVER)
1178 return IRC_WriteStrClientPrefix(target, from,
1180 Req->argv[0], Req->argv[1]);
1182 is_remote = Client_Conn(from) < 0;
1183 strlcpy(mask, Req->argv[Req->argc - 1], sizeof(mask));
1184 for (query = strtok(ngt_LowerStr(mask), ",");
1186 query = strtok(NULL, ","), found++)
1188 has_wildcards = query[strcspn(query, "*?")] != 0;
1190 * follows ircd 2.10 implementation:
1191 * - handle up to 3 targets
1192 * - no wildcards for remote clients
1193 * - only one wildcard target per local client
1195 * Also, at most MAX_RPL_WHOIS matches are returned.
1197 if (!has_wildcards || is_remote) {
1198 c = Client_Search(query);
1199 if (c && Client_Type(c) == CLIENT_USER) {
1200 if (!IRC_WHOIS_SendReply(Client, from, c))
1201 return DISCONNECTED;
1203 if (!IRC_WriteStrClient(Client,
1207 return DISCONNECTED;
1212 /* we already handled one wildcard query */
1213 if (!IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1214 Client_ID(Client), query))
1215 return DISCONNECTED;
1218 got_wildcard = true;
1219 IRC_SetPenalty(Client, 3);
1221 for (c = Client_First(); c; c = Client_Next(c)) {
1222 if (IRC_CheckListTooBig(Client, match_count,
1223 MAX_RPL_WHOIS, "WHOIS"))
1226 if (Client_Type(c) != CLIENT_USER)
1228 if (!MatchCaseInsensitive(query, Client_ID(c)))
1230 if (!IRC_WHOIS_SendReply(Client, from, c))
1231 return DISCONNECTED;
1236 if (match_count == 0)
1237 IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1239 Req->argv[Req->argc - 1]);
1241 return IRC_WriteStrClient(from, RPL_ENDOFWHOIS_MSG,
1242 Client_ID(from), Req->argv[Req->argc - 1]);
1247 WHOWAS_EntryWrite(CLIENT *prefix, WHOWAS *entry)
1251 (void)strftime(t_str, sizeof(t_str), "%a %b %d %H:%M:%S %Y",
1252 localtime(&entry->time));
1254 if (!IRC_WriteStrClient(prefix, RPL_WHOWASUSER_MSG, Client_ID(prefix),
1255 entry->id, entry->user, entry->host, entry->info))
1256 return DISCONNECTED;
1258 return IRC_WriteStrClient(prefix, RPL_WHOISSERVER_MSG, Client_ID(prefix),
1259 entry->id, entry->server, t_str);
1263 * IRC "WHOWAS" function.
1264 * This function implements the IRC command "WHOWHAS". It handles local
1265 * requests and request that should be forwarded to other servers.
1268 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1270 CLIENT *target, *prefix;
1272 char tok_buf[COMMAND_LEN];
1273 int max, last, count, i, nc;
1276 assert( Client != NULL );
1277 assert( Req != NULL );
1279 /* Do not reveal any info on disconnected users? */
1280 if (Conf_MorePrivacy)
1283 /* Wrong number of parameters? */
1285 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1286 Client_ID(Client), Req->command);
1288 return IRC_WriteStrClient(Client, ERR_NONICKNAMEGIVEN_MSG, Client_ID(Client));
1292 target = Client_Search(Req->argv[2]);
1294 target = Client_ThisServer();
1297 if (Client_Type(Client) == CLIENT_SERVER)
1298 prefix = Client_Search(Req->prefix);
1303 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1304 Client_ID(Client), Req->prefix);
1306 /* Forward to other server? */
1307 if (target != Client_ThisServer()) {
1308 if (!target || (Client_Type(target) != CLIENT_SERVER))
1309 return IRC_WriteStrClient(prefix, ERR_NOSUCHSERVER_MSG,
1310 Client_ID(prefix), Req->argv[2]);
1313 IRC_WriteStrClientPrefix( target, prefix, "WHOWAS %s %s %s",
1314 Req->argv[0], Req->argv[1],
1319 whowas = Client_GetWhowas( );
1320 last = Client_GetLastWhowasIndex( );
1324 max = DEF_RPL_WHOWAS;
1325 if (Req->argc > 1) {
1326 max = atoi(Req->argv[1]);
1328 max = MAX_RPL_WHOWAS;
1332 * Break up the nick argument into a list of nicks, if applicable
1333 * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1335 strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1336 nick = strtok(tok_buf, ",");
1338 for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1342 if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1343 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1344 return DISCONNECTED;
1348 /* previous entry */
1351 /* "underflow", wrap around */
1355 if (nc && count >= max)
1357 } while (i != last);
1359 if (nc == 0 && !IRC_WriteStrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1360 Client_ID(prefix), nick))
1361 return DISCONNECTED;
1363 return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG, Client_ID(prefix), Req->argv[0]);
1368 * Send LUSERS reply to a client.
1370 * @param Client The receipient of the information.
1371 * @return CONNECTED or DISCONNECTED.
1374 IRC_Send_LUSERS(CLIENT *Client)
1381 assert(Client != NULL);
1383 /* Users, services and serevers in the network */
1384 if (!IRC_WriteStrClient(Client, RPL_LUSERCLIENT_MSG, Client_ID(Client),
1385 Client_UserCount(), Client_ServiceCount(),
1386 Client_ServerCount()))
1387 return DISCONNECTED;
1389 /* Number of IRC operators */
1390 cnt = Client_OperCount( );
1392 if (!IRC_WriteStrClient(Client, RPL_LUSEROP_MSG,
1393 Client_ID(Client), cnt))
1394 return DISCONNECTED;
1397 /* Unknown connections */
1398 cnt = Client_UnknownCount( );
1400 if (!IRC_WriteStrClient(Client, RPL_LUSERUNKNOWN_MSG,
1401 Client_ID(Client), cnt))
1402 return DISCONNECTED;
1405 /* Number of created channels */
1406 if (!IRC_WriteStrClient(Client, RPL_LUSERCHANNELS_MSG,
1408 Channel_CountVisible(Client)))
1409 return DISCONNECTED;
1411 /* Number of local users, services and servers */
1412 if (!IRC_WriteStrClient(Client, RPL_LUSERME_MSG, Client_ID(Client),
1413 Client_MyUserCount(), Client_MyServiceCount(),
1414 Client_MyServerCount()))
1415 return DISCONNECTED;
1418 /* Maximum number of local users */
1419 cnt = Client_MyUserCount();
1420 max = Client_MyMaxUserCount();
1421 if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1422 cnt, max, cnt, max))
1423 return DISCONNECTED;
1424 /* Maximum number of users in the network */
1425 cnt = Client_UserCount();
1426 max = Client_MaxUserCount();
1427 if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1428 cnt, max, cnt, max))
1429 return DISCONNECTED;
1430 /* Connection counters */
1431 if (! IRC_WriteStrClient(Client, RPL_STATSCONN_MSG, Client_ID(Client),
1432 Conn_CountMax(), Conn_CountAccepted()))
1433 return DISCONNECTED;
1437 } /* IRC_Send_LUSERS */
1441 Show_MOTD_Start(CLIENT *Client)
1443 return IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG,
1444 Client_ID( Client ), Client_ID( Client_ThisServer( )));
1448 Show_MOTD_Sendline(CLIENT *Client, const char *msg)
1450 return IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID( Client ), msg);
1454 Show_MOTD_End(CLIENT *Client)
1456 return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
1460 static bool Show_MOTD_SSLInfo(CLIENT *Client)
1463 char buf[COMMAND_LEN] = "Connected using Cipher ";
1465 if (!Conn_GetCipherInfo(Client_Conn(Client), buf + 23, sizeof buf - 23))
1468 if (!Show_MOTD_Sendline(Client, buf))
1475 Show_MOTD_SSLInfo(UNUSED CLIENT *c)
1480 IRC_Show_MOTD( CLIENT *Client )
1483 size_t len_tot, len_str;
1485 assert( Client != NULL );
1487 len_tot = array_bytes(&Conf_Motd);
1488 if (len_tot == 0 && !Conn_UsesSSL(Client_Conn(Client)))
1489 return IRC_WriteStrClient(Client, ERR_NOMOTD_MSG, Client_ID(Client));
1491 if (!Show_MOTD_Start(Client))
1492 return DISCONNECTED;
1494 line = array_start(&Conf_Motd);
1495 while (len_tot > 0) {
1496 len_str = strlen(line) + 1;
1498 assert(len_tot >= len_str);
1501 if (!Show_MOTD_Sendline(Client, line))
1502 return DISCONNECTED;
1506 if (!Show_MOTD_SSLInfo(Client))
1507 return DISCONNECTED;
1508 return Show_MOTD_End(Client);
1509 } /* IRC_Show_MOTD */
1513 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
1515 bool is_visible, is_member;
1516 char str[LINE_LEN + 1];
1520 assert( Client != NULL );
1521 assert( Chan != NULL );
1523 if( Channel_IsMemberOf( Chan, Client )) is_member = true;
1524 else is_member = false;
1526 /* Do not print info on channel memberships to anyone that is not member? */
1527 if (Conf_MorePrivacy && !is_member)
1530 /* Secret channel? */
1531 if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
1533 /* Alle Mitglieder suchen */
1534 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1535 cl2chan = Channel_FirstMember( Chan );
1538 cl = Channel_GetClient( cl2chan );
1540 if( strchr( Client_Modes( cl ), 'i' )) is_visible = false;
1541 else is_visible = true;
1543 if( is_member || is_visible )
1545 /* Nick anhaengen */
1546 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
1547 if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
1548 else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
1549 strlcat( str, Client_ID( cl ), sizeof( str ));
1551 if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
1553 /* Zeile wird zu lang: senden! */
1554 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1555 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1559 /* naechstes Mitglied suchen */
1560 cl2chan = Channel_NextMember( Chan, cl2chan );
1562 if( str[strlen( str ) - 1] != ':')
1564 /* Es sind noch Daten da, die gesendet werden muessen */
1565 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1569 } /* IRC_Send_NAMES */
1573 * Send the ISUPPORT numeric (005).
1574 * This numeric indicates the features that are supported by this server.
1575 * See <http://www.irc.org/tech_docs/005.html> for details.
1578 IRC_Send_ISUPPORT(CLIENT * Client)
1580 if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1582 return DISCONNECTED;
1583 return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1584 CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1585 COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1586 COMMAND_LEN - 113, MAX_HNDL_MODES_ARG);
1587 } /* IRC_Send_ISUPPORT */