2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2011 Alexander Barton (alex@barton.de) and Contributors.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * Please read the file COPYING, README and AUTHORS for more information.
28 #include "conn-func.h"
40 #include "irc-write.h"
47 IRC_ADMIN(CLIENT *Client, REQUEST *Req )
49 CLIENT *target, *prefix;
51 assert( Client != NULL );
52 assert( Req != NULL );
54 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
57 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
58 else target = Client_ThisServer( );
61 if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
63 if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
65 /* forwad message to another server? */
66 if( target != Client_ThisServer( ))
68 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
71 IRC_WriteStrClientPrefix( target, prefix, "ADMIN %s", Req->argv[0] );
75 /* mit Versionsinfo antworten */
76 if( ! IRC_WriteStrClient( Client, RPL_ADMINME_MSG, Client_ID( prefix ), Conf_ServerName )) return DISCONNECTED;
77 if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC1_MSG, Client_ID( prefix ), Conf_ServerAdmin1 )) return DISCONNECTED;
78 if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC2_MSG, Client_ID( prefix ), Conf_ServerAdmin2 )) return DISCONNECTED;
79 if( ! IRC_WriteStrClient( Client, RPL_ADMINEMAIL_MSG, Client_ID( prefix ), Conf_ServerAdminMail )) return DISCONNECTED;
81 IRC_SetPenalty( Client, 1 );
87 * Handler for the IRC command "INFO".
88 * See RFC 2812 section 3.4.10.
91 IRC_INFO(CLIENT * Client, REQUEST * Req)
93 CLIENT *target, *prefix;
96 assert(Client != NULL);
99 /* Wrong number of parameters? */
101 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
102 Client_ID(Client), Req->command);
104 /* Determine prefix */
105 if (Client_Type(Client) == CLIENT_SERVER)
106 prefix = Client_Search(Req->prefix);
110 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
111 Client_ID(Client), Req->prefix);
113 /* Look for a target */
115 target = Client_Search(Req->argv[0]);
117 target = Client_ThisServer();
119 /* Make sure that the target is a server */
120 if (target && Client_Type(target) != CLIENT_SERVER)
121 target = Client_Introducer(target);
124 return IRC_WriteStrClient(prefix, ERR_NOSUCHSERVER_MSG,
125 Client_ID(prefix), Req->argv[0]);
127 /* Pass on to another server? */
128 if (target != Client_ThisServer()) {
129 IRC_WriteStrClientPrefix(target, prefix, "INFO %s",
134 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix),
138 #if defined(__DATE__) && defined(__TIME__)
139 snprintf(msg, sizeof(msg), "Birth Date: %s at %s", __DATE__, __TIME__);
140 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
144 strlcpy(msg, "On-line since ", sizeof(msg));
145 strlcat(msg, NGIRCd_StartStr, sizeof(msg));
146 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
149 if (!IRC_WriteStrClient(Client, RPL_ENDOFINFO_MSG, Client_ID(prefix)))
152 IRC_SetPenalty(Client, 2);
158 * Handler for the IRC "ISON" command.
160 * See RFC 2812, 4.9 "Ison message".
162 * @param Client The client from which this command has been received.
163 * @param Req Request structure with prefix and all parameters.
164 * @return CONNECTED or DISCONNECTED.
167 IRC_ISON( CLIENT *Client, REQUEST *Req )
169 char rpl[COMMAND_LEN];
174 assert(Client != NULL);
177 /* Bad number of arguments? */
179 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
180 Client_ID(Client), Req->command);
182 strlcpy(rpl, RPL_ISON_MSG, sizeof rpl);
183 for (i = 0; i < Req->argc; i++) {
184 /* "All" ircd even parse ":<x> <y> ..." arguments and split
185 * them up; so we do the same ... */
186 ptr = strtok(Req->argv[i], " ");
189 c = Client_Search(ptr);
190 if (c && Client_Type(c) == CLIENT_USER) {
191 strlcat(rpl, Client_ID(c), sizeof(rpl));
192 strlcat(rpl, " ", sizeof(rpl));
194 ptr = strtok(NULL, " ");
197 ngt_TrimLastChr(rpl, ' ');
199 return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
204 * Handler for the IRC "LINKS" command.
206 * See RFC 2812, 3.4.5 "Links message".
208 * @param Client The client from which this command has been received.
209 * @param Req Request structure with prefix and all parameters.
210 * @return CONNECTED or DISCONNECTED.
213 IRC_LINKS(CLIENT *Client, REQUEST *Req)
215 CLIENT *target, *from, *c;
218 assert(Client != NULL);
221 IRC_SetPenalty(Client, 1);
224 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
225 Client_ID(Client), Req->command);
227 /* Get pointer to server mask or "*", if none given */
229 mask = Req->argv[Req->argc - 1];
233 if (Client_Type(Client) == CLIENT_SERVER)
234 from = Client_Search(Req->prefix);
238 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
239 Client_ID(Client), Req->prefix);
242 if (Req->argc == 2) {
243 target = Client_Search(Req->argv[0]);
244 if (! target || Client_Type(target) != CLIENT_SERVER)
245 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
249 if (target != Client_ThisServer())
250 return IRC_WriteStrClientPrefix(target, from,
251 "LINKS %s %s", Req->argv[0],
257 if (Client_Type(c) == CLIENT_SERVER
258 && MatchCaseInsensitive(mask, Client_ID(c))) {
259 if (!IRC_WriteStrClient(from, RPL_LINKS_MSG,
260 Client_ID(from), Client_ID(c),
261 Client_ID(Client_TopServer(c)
262 ? Client_TopServer(c)
263 : Client_ThisServer()),
264 Client_Hops(c), Client_Info(c)))
269 return IRC_WriteStrClient(from, RPL_ENDOFLINKS_MSG,
270 Client_ID(from), mask);
275 IRC_LUSERS( CLIENT *Client, REQUEST *Req )
277 CLIENT *target, *from;
279 assert( Client != NULL );
280 assert( Req != NULL );
282 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
284 /* Absender ermitteln */
285 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
287 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
289 /* An anderen Server forwarden? */
292 target = Client_Search( Req->argv[1] );
293 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
294 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LUSERS %s %s", Req->argv[0], Req->argv[1] );
297 /* Wer ist der Absender? */
298 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
299 else target = Client;
300 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
302 IRC_Send_LUSERS( target );
304 IRC_SetPenalty( target, 1 );
310 * Handler for the IRC command "SERVLIST".
311 * List registered services, see RFC 2811, section 3.5.1: the syntax is
312 * "SERVLIST [<mask> [<type>]]".
315 IRC_SERVLIST(CLIENT *Client, REQUEST *Req)
319 assert(Client != NULL);
323 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
324 Client_ID(Client), Req->command);
326 if (Req->argc < 2 || strcmp(Req->argv[1], "0") == 0) {
327 for (c = Client_First(); c!= NULL; c = Client_Next(c)) {
328 if (Client_Type(c) != CLIENT_SERVICE)
330 if (Req->argc > 0 && !MatchCaseInsensitive(Req->argv[0],
333 if (!IRC_WriteStrClient(Client, RPL_SERVLIST_MSG,
334 Client_ID(Client), Client_Mask(c),
335 Client_Mask(Client_Introducer(c)), "*",
336 0, Client_Hops(c), Client_Info(c)))
341 return IRC_WriteStrClient(Client, RPL_SERVLISTEND_MSG, Client_ID(Client),
342 Req->argc > 0 ? Req->argv[0] : "*",
343 Req->argc > 1 ? Req->argv[1] : "0");
348 IRC_MOTD( CLIENT *Client, REQUEST *Req )
350 CLIENT *from, *target;
352 assert( Client != NULL );
353 assert( Req != NULL );
355 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
357 /* From aus Prefix ermitteln */
358 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
360 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
365 target = Client_Search( Req->argv[0] );
366 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
368 if( target != Client_ThisServer( ))
370 /* Ok, anderer Server ist das Ziel: forwarden */
371 return IRC_WriteStrClientPrefix( target, from, "MOTD %s", Req->argv[0] );
375 IRC_SetPenalty( from, 3 );
376 return IRC_Show_MOTD( from );
381 IRC_NAMES( CLIENT *Client, REQUEST *Req )
383 char rpl[COMMAND_LEN], *ptr;
384 CLIENT *target, *from, *c;
387 assert( Client != NULL );
388 assert( Req != NULL );
390 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
392 /* use prefix to determine "From" */
393 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
395 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
399 /* forward to another server? */
400 target = Client_Search( Req->argv[1] );
401 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
403 if( target != Client_ThisServer( )) {
404 /* target is another server, forward */
405 return IRC_WriteStrClientPrefix( target, from, "NAMES %s :%s", Req->argv[0], Req->argv[1] );
411 /* bestimmte Channels durchgehen */
412 ptr = strtok( Req->argv[0], "," );
415 chan = Channel_Search( ptr );
419 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
421 if( ! IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), ptr )) return DISCONNECTED;
423 /* get next channel name */
424 ptr = strtok( NULL, "," );
429 chan = Channel_First( );
432 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
434 chan = Channel_Next( chan );
437 /* Now print all clients which are not in any channel */
439 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
442 if(( Client_Type( c ) == CLIENT_USER ) && ( Channel_FirstChannelOf( c ) == NULL ) && ( ! strchr( Client_Modes( c ), 'i' )))
444 /* its a user, concatenate ... */
445 if( rpl[strlen( rpl ) - 1] != ':' ) strlcat( rpl, " ", sizeof( rpl ));
446 strlcat( rpl, Client_ID( c ), sizeof( rpl ));
448 if( strlen( rpl ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
450 /* Line is gwoing too long, send now */
451 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
452 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
456 c = Client_Next( c );
458 if( rpl[strlen( rpl ) - 1] != ':')
460 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
463 IRC_SetPenalty( from, 1 );
464 return IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), "*" );
469 t_diff(time_t *t, const time_t d)
477 return (unsigned int)diff;
482 uptime_days(time_t *now)
484 return t_diff(now, 60 * 60 * 24);
489 uptime_hrs(time_t *now)
491 return t_diff(now, 60 * 60);
496 uptime_mins(time_t *now)
498 return t_diff(now, 60);
503 * Handler for the IRC command "STATS".
504 * See RFC 2812 section 3.4.4.
507 IRC_STATS( CLIENT *Client, REQUEST *Req )
509 CLIENT *from, *target, *cl;
514 unsigned int days, hrs, mins;
515 struct list_head *list;
516 struct list_elem *list_item;
518 assert(Client != NULL);
522 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
523 Client_ID(Client), Req->command);
525 /* use prefix to determine "From" */
526 if (Client_Type(Client) == CLIENT_SERVER)
527 from = Client_Search(Req->prefix);
532 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
533 Client_ID(Client), Req->prefix);
535 if (Req->argc == 2) {
536 /* forward to another server? */
537 target = Client_Search(Req->argv[1]);
538 if ((!target) || (Client_Type(target) != CLIENT_SERVER))
539 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
540 Client_ID(from), Req->argv[1]);
542 if (target != Client_ThisServer()) {
543 /* forward to another server */
544 return IRC_WriteStrClientPrefix(target, from,
545 "STATS %s %s", Req->argv[0], Req->argv[1]);
550 query = Req->argv[0][0] ? Req->argv[0][0] : '*';
555 case 'g': /* Network-wide bans ("G-Lines") */
557 case 'k': /* Server-local bans ("K-Lines") */
559 if (!Client_HasMode(from, 'o'))
560 return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG,
562 if (query == 'g' || query == 'G')
563 list = Class_GetList(CLASS_GLINE);
565 list = Class_GetList(CLASS_KLINE);
566 list_item = Lists_GetFirst(list);
568 if (!IRC_WriteStrClient(from, RPL_STATSXLINE_MSG,
569 Client_ID(from), query,
570 Lists_GetMask(list_item),
571 Lists_GetValidity(list_item),
572 Lists_GetReason(list_item)))
574 list_item = Lists_GetNext(list_item);
577 case 'l': /* Link status (servers and own link) */
579 time_now = time(NULL);
580 for (con = Conn_First(); con != NONE; con = Conn_Next(con)) {
581 cl = Conn_GetClient(con);
584 if ((Client_Type(cl) == CLIENT_SERVER)
586 /* Server link or our own connection */
588 if (Conn_Options(con) & CONN_ZIP) {
589 if (!IRC_WriteStrClient
590 (from, RPL_STATSLINKINFOZIP_MSG,
591 Client_ID(from), Client_Mask(cl),
592 Conn_SendQ(con), Conn_SendMsg(con),
598 (long)(time_now - Conn_StartTime(con))))
603 if (!IRC_WriteStrClient
604 (from, RPL_STATSLINKINFO_MSG,
605 Client_ID(from), Client_Mask(cl),
606 Conn_SendQ(con), Conn_SendMsg(con),
607 Conn_SendBytes(con), Conn_RecvMsg(con),
609 (long)(time_now - Conn_StartTime(con))))
614 case 'm': /* IRC command status (usage count) */
616 cmd = Parse_GetCommandStruct();
617 for (; cmd->name; cmd++) {
618 if (cmd->lcount == 0 && cmd->rcount == 0)
620 if (!IRC_WriteStrClient
621 (from, RPL_STATSCOMMANDS_MSG, Client_ID(from),
622 cmd->name, cmd->lcount, cmd->bytes, cmd->rcount))
626 case 'u': /* Server uptime */
628 time_now = time(NULL) - NGIRCd_Start;
629 days = uptime_days(&time_now);
630 hrs = uptime_hrs(&time_now);
631 mins = uptime_mins(&time_now);
632 if (!IRC_WriteStrClient(from, RPL_STATSUPTIME, Client_ID(from),
633 days, hrs, mins, (unsigned int)time_now))
638 IRC_SetPenalty(from, 2);
639 return IRC_WriteStrClient(from, RPL_ENDOFSTATS_MSG,
640 Client_ID(from), query);
645 * Handler for the IRC command "SUMMON".
646 * See RFC 2812 section 4.5. ngIRCd doesn't implement this functionality and
647 * therefore answers with ERR_SUMMONDISABLED.
650 IRC_SUMMON(CLIENT * Client, REQUEST * Req)
652 return IRC_WriteStrClient(Client, ERR_SUMMONDISABLED_MSG,
653 Client_ID(Client), Req->command);
658 IRC_TIME( CLIENT *Client, REQUEST *Req )
660 CLIENT *from, *target;
664 assert( Client != NULL );
665 assert( Req != NULL );
667 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
669 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
671 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
675 target = Client_Search( Req->argv[0] );
676 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
678 if( target != Client_ThisServer( ))
680 return IRC_WriteStrClientPrefix( target, from, "TIME %s", Req->argv[0] );
685 (void)strftime( t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime( &t ));
686 return IRC_WriteStrClient( from, RPL_TIME_MSG, Client_ID( from ), Client_ID( Client_ThisServer( )), t_str );
691 * Handler for the IRC command "USERHOST".
692 * See RFC 2812 section 4.8.
695 IRC_USERHOST(CLIENT *Client, REQUEST *Req)
697 char rpl[COMMAND_LEN];
701 assert(Client != NULL);
705 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
706 Client_ID(Client), Req->command);
713 strlcpy(rpl, RPL_USERHOST_MSG, sizeof rpl);
714 for (i = 0; i < max; i++) {
715 c = Client_Search(Req->argv[i]);
716 if (c && (Client_Type(c) == CLIENT_USER)) {
717 /* This Nick is "online" */
718 strlcat(rpl, Client_ID(c), sizeof(rpl));
719 if (Client_HasMode(c, 'o'))
720 strlcat(rpl, "*", sizeof(rpl));
721 strlcat(rpl, "=", sizeof(rpl));
722 if (Client_HasMode(c, 'a'))
723 strlcat(rpl, "-", sizeof(rpl));
725 strlcat(rpl, "+", sizeof(rpl));
726 strlcat(rpl, Client_User(c), sizeof(rpl));
727 strlcat(rpl, "@", sizeof(rpl));
728 strlcat(rpl, Client_HostnameCloaked(c), sizeof(rpl));
729 strlcat(rpl, " ", sizeof(rpl));
732 ngt_TrimLastChr(rpl, ' ');
734 return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
739 * Handler for the IRC command "USERS".
740 * See RFC 2812 section 4.6. As suggested there the command is disabled.
743 IRC_USERS(CLIENT * Client, REQUEST * Req)
745 return IRC_WriteStrClient(Client, ERR_USERSDISABLED_MSG,
746 Client_ID(Client), Req->command);
751 IRC_VERSION( CLIENT *Client, REQUEST *Req )
753 CLIENT *target, *prefix;
755 assert( Client != NULL );
756 assert( Req != NULL );
758 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
761 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
762 else target = Client_ThisServer( );
764 /* Prefix ermitteln */
765 if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
766 else prefix = Client;
767 if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
769 /* An anderen Server weiterleiten? */
770 if( target != Client_ThisServer( ))
772 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
775 IRC_WriteStrClientPrefix( target, prefix, "VERSION %s", Req->argv[0] );
779 /* send version information */
780 IRC_SetPenalty(Client, 1);
781 return IRC_WriteStrClient(Client, RPL_VERSION_MSG, Client_ID(prefix),
782 PACKAGE_NAME, PACKAGE_VERSION,
783 NGIRCd_DebugLevel, Conf_ServerName,
784 NGIRCd_VersionAddition);
789 write_whoreply(CLIENT *Client, CLIENT *c, const char *channelname, const char *flags)
791 return IRC_WriteStrClient(Client, RPL_WHOREPLY_MSG, Client_ID(Client),
792 channelname, Client_User(c),
793 Client_HostnameCloaked(c),
794 Client_ID(Client_Introducer(c)), Client_ID(c),
795 flags, Client_Hops(c), Client_Info(c));
800 who_flags_status(const char *client_modes)
802 if (strchr(client_modes, 'a'))
803 return "G"; /* away */
809 who_flags_qualifier(const char *chan_user_modes)
811 if (strchr(chan_user_modes, 'o'))
813 else if (strchr(chan_user_modes, 'v'))
820 * Send WHO reply for a "channel target" ("WHO #channel").
822 * @param Client Client requesting the information.
823 * @param Chan Channel being requested.
824 * @param OnlyOps Only display IRC operators.
825 * @return CONNECTED or DISCONNECTED.
828 IRC_WHO_Channel(CLIENT *Client, CHANNEL *Chan, bool OnlyOps)
830 bool is_visible, is_member, is_ircop;
832 const char *client_modes;
833 const char *chan_user_modes;
837 assert( Client != NULL );
838 assert( Chan != NULL );
840 is_member = Channel_IsMemberOf(Chan, Client);
842 /* Secret channel? */
843 if (!is_member && strchr(Channel_Modes(Chan), 's'))
844 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG,
845 Client_ID(Client), Channel_Name(Chan));
847 cl2chan = Channel_FirstMember(Chan);
848 for (; cl2chan ; cl2chan = Channel_NextMember(Chan, cl2chan)) {
849 c = Channel_GetClient(cl2chan);
851 client_modes = Client_Modes(c);
852 is_ircop = strchr(client_modes, 'o') != NULL;
853 if (OnlyOps && !is_ircop)
856 is_visible = strchr(client_modes, 'i') == NULL;
857 if (is_member || is_visible) {
858 strcpy(flags, who_flags_status(client_modes));
860 strlcat(flags, "*", sizeof(flags));
862 chan_user_modes = Channel_UserModes(Chan, c);
863 strlcat(flags, who_flags_qualifier(chan_user_modes),
866 if (!write_whoreply(Client, c, Channel_Name(Chan),
871 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client),
877 * Send WHO reply for a "mask target" ("WHO m*sk").
879 * @param Client Client requesting the information.
880 * @param Mask Mask being requested or NULL for "all" clients.
881 * @param OnlyOps Only display IRC operators.
882 * @return CONNECTED or DISCONNECTED.
885 IRC_WHO_Mask(CLIENT *Client, char *Mask, bool OnlyOps)
890 bool client_match, is_visible;
893 assert (Client != NULL);
898 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
899 if (Client_Type(c) != CLIENT_USER)
902 if (OnlyOps && !Client_HasMode(c, 'o'))
906 /* Match pattern against user host/server/name/nick */
907 client_match = MatchCaseInsensitive(Mask,
910 client_match = MatchCaseInsensitive(Mask,
911 Client_ID(Client_Introducer(c)));
913 client_match = MatchCaseInsensitive(Mask,
916 client_match = MatchCaseInsensitive(Mask,
919 continue; /* no match: skip this client */
922 is_visible = !Client_HasMode(c, 'i');
924 /* Target client is invisible, but mask matches exactly? */
925 if (!is_visible && Mask && strcasecmp(Client_ID(c), Mask) == 0)
928 /* Target still invisible, but are both on the same channel? */
930 cl2chan = Channel_FirstChannelOf(Client);
931 while (cl2chan && !is_visible) {
932 chan = Channel_GetChannel(cl2chan);
933 if (Channel_IsMemberOf(chan, c))
935 cl2chan = Channel_NextChannelOf(Client, cl2chan);
939 if (!is_visible) /* target user is not visible */
942 strcpy(flags, who_flags_status(Client_Modes(c)));
943 if (strchr(Client_Modes(c), 'o'))
944 strlcat(flags, "*", sizeof(flags));
946 if (!write_whoreply(Client, c, "*", flags))
951 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client),
957 * Handler for the IRC "WHO" command.
959 * See RFC 2812, 3.6.1 "Who query".
961 * @param Client The client from which this command has been received.
962 * @param Req Request structure with prefix and all parameters.
963 * @return CONNECTED or DISCONNECTED.
966 IRC_WHO(CLIENT *Client, REQUEST *Req)
971 assert (Client != NULL);
972 assert (Req != NULL);
975 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
976 Client_ID(Client), Req->command);
980 if (Req->argc == 2) {
981 if (strcmp(Req->argv[1], "o") == 0)
985 return IRC_WriteStrClient(Client,
986 ERR_NEEDMOREPARAMS_MSG,
992 IRC_SetPenalty(Client, 1);
993 if (Req->argc >= 1) {
994 /* Channel or mask given */
995 chan = Channel_Search(Req->argv[0]);
997 /* Members of a channel have been requested */
998 IRC_SetPenalty(Client, 1);
999 return IRC_WHO_Channel(Client, chan, only_ops);
1001 if (strcmp(Req->argv[0], "0") != 0) {
1002 /* A mask has been given. But please note this RFC
1003 * stupidity: "0" is same as no arguments ... */
1004 IRC_SetPenalty(Client, 3);
1005 return IRC_WHO_Mask(Client, Req->argv[0], only_ops);
1009 /* No channel or (valid) mask given */
1010 IRC_SetPenalty(Client, 2);
1011 return IRC_WHO_Mask(Client, NULL, only_ops);
1016 * Generate WHOIS reply of one actual client.
1018 * @param Client The client from which this command has been received.
1019 * @param from The client requesting the information ("originator").
1020 * @param c The client of which information should be returned.
1021 * @returns CONNECTED or DISCONNECTED.
1024 IRC_WHOIS_SendReply(CLIENT *Client, CLIENT *from, CLIENT *c)
1026 char str[LINE_LEN + 1];
1030 /* Nick, user, hostname and client info */
1031 if (!IRC_WriteStrClient(from, RPL_WHOISUSER_MSG, Client_ID(from),
1032 Client_ID(c), Client_User(c),
1033 Client_HostnameCloaked(c), Client_Info(c)))
1034 return DISCONNECTED;
1037 if (!IRC_WriteStrClient(from, RPL_WHOISSERVER_MSG, Client_ID(from),
1038 Client_ID(c), Client_ID(Client_Introducer(c)),
1039 Client_Info(Client_Introducer(c))))
1040 return DISCONNECTED;
1043 snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
1044 Client_ID(from), Client_ID(c));
1045 cl2chan = Channel_FirstChannelOf(c);
1047 chan = Channel_GetChannel(cl2chan);
1048 assert(chan != NULL);
1051 cl2chan = Channel_NextChannelOf(c, cl2chan);
1053 /* Secret channel? */
1054 if (strchr(Channel_Modes(chan), 's')
1055 && !Channel_IsMemberOf(chan, Client))
1058 /* Local channel and request is not from a user? */
1059 if (Client_Type(Client) == CLIENT_SERVER
1060 && Channel_IsLocal(chan))
1063 /* Concatenate channel names */
1064 if (str[strlen(str) - 1] != ':')
1065 strlcat(str, " ", sizeof(str));
1067 strlcat(str, who_flags_qualifier(Channel_UserModes(chan, c)),
1069 strlcat(str, Channel_Name(chan), sizeof(str));
1071 if (strlen(str) > (LINE_LEN - CHANNEL_NAME_LEN - 4)) {
1072 /* Line becomes too long: send it! */
1073 if (!IRC_WriteStrClient(Client, "%s", str))
1074 return DISCONNECTED;
1075 snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
1076 Client_ID(from), Client_ID(c));
1079 if(str[strlen(str) - 1] != ':') {
1080 /* There is data left to send: */
1081 if (!IRC_WriteStrClient(Client, "%s", str))
1082 return DISCONNECTED;
1086 if (Client_HasMode(c, 'o') &&
1087 !IRC_WriteStrClient(from, RPL_WHOISOPERATOR_MSG,
1088 Client_ID(from), Client_ID(c)))
1089 return DISCONNECTED;
1091 /* Connected using SSL? */
1092 if (Conn_UsesSSL(Client_Conn(c)) &&
1093 !IRC_WriteStrClient(from, RPL_WHOISSSL_MSG,
1094 Client_ID(from), Client_ID(c)))
1095 return DISCONNECTED;
1097 /* Idle and signon time (local clients only!) */
1098 if (!Conf_MorePrivacy && Client_Conn(c) > NONE &&
1099 !IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
1100 Client_ID(from), Client_ID(c),
1101 (unsigned long)Conn_GetIdle(Client_Conn(c)),
1102 (unsigned long)Conn_GetSignon(Client_Conn(c))))
1103 return DISCONNECTED;
1106 if (Client_HasMode(c, 'a') &&
1107 !IRC_WriteStrClient(from, RPL_AWAY_MSG,
1108 Client_ID(from), Client_ID(c),
1110 return DISCONNECTED;
1113 } /* IRC_WHOIS_SendReply */
1117 * Handler for the IRC "WHOIS" command.
1119 * See RFC 2812, 3.6.2 "Whois query".
1121 * @param Client The client from which this command has been received.
1122 * @param Req Request structure with prefix and all parameters.
1123 * @return CONNECTED or DISCONNECTED.
1126 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
1128 CLIENT *from, *target, *c;
1129 unsigned int match_count = 0, found = 0;
1130 bool has_wildcards, is_remote;
1131 bool got_wildcard = false;
1132 char mask[COMMAND_LEN], *query;
1134 assert( Client != NULL );
1135 assert( Req != NULL );
1137 /* Bad number of parameters? */
1138 if (Req->argc < 1 || Req->argc > 2)
1139 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1140 Client_ID(Client), Req->command);
1142 /* Search sender of the WHOIS */
1143 if (Client_Type(Client) == CLIENT_SERVER) {
1144 from = Client_Search(Req->prefix);
1146 IRC_SetPenalty(Client, 1);
1150 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1151 Client_ID(Client), Req->prefix);
1153 /* Get target server for this command */
1154 if (Req->argc > 1) {
1155 /* Search the target server, which can be specified as a
1156 * nick name on that server as well: */
1157 target = Client_Search(Req->argv[0]);
1159 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
1160 Client_ID(from), Req->argv[0]);
1162 target = Client_ThisServer();
1163 assert(target != NULL);
1165 /* Forward to other server? */
1166 if (Client_NextHop(target) != Client_ThisServer() &&
1167 Client_Type(Client_NextHop(target)) == CLIENT_SERVER)
1168 return IRC_WriteStrClientPrefix(target, from,
1170 Req->argv[0], Req->argv[1]);
1172 is_remote = Client_Conn(from) < 0;
1173 strlcpy(mask, Req->argv[Req->argc - 1], sizeof(mask));
1174 for (query = strtok(ngt_LowerStr(mask), ",");
1176 query = strtok(NULL, ","), found++)
1178 has_wildcards = query[strcspn(query, "*?")] != 0;
1180 * follows ircd 2.10 implementation:
1181 * - handle up to 3 targets
1182 * - no wildcards for remote clients
1183 * - only one wildcard target per local client
1185 * also, at most ten matches are returned.
1187 if (!has_wildcards || is_remote) {
1188 c = Client_Search(query);
1189 if (c && Client_Type(c) == CLIENT_USER) {
1190 if (!IRC_WHOIS_SendReply(Client, from, c))
1191 return DISCONNECTED;
1193 if (!IRC_WriteStrClient(Client,
1197 return DISCONNECTED;
1202 /* we already handled one wildcard query */
1203 if (!IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1204 Client_ID(Client), query))
1205 return DISCONNECTED;
1208 got_wildcard = true;
1209 IRC_SetPenalty(Client, 3);
1211 for (c = Client_First(); c && match_count < 10; c = Client_Next(c)) {
1212 if (Client_Type(c) != CLIENT_USER)
1214 if (!MatchCaseInsensitive(query, Client_ID(c)))
1216 if (!IRC_WHOIS_SendReply(Client, from, c))
1217 return DISCONNECTED;
1221 if (match_count == 0)
1222 IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1224 Req->argv[Req->argc - 1]);
1226 return IRC_WriteStrClient(from, RPL_ENDOFWHOIS_MSG,
1227 Client_ID(from), Req->argv[Req->argc - 1]);
1232 WHOWAS_EntryWrite(CLIENT *prefix, WHOWAS *entry)
1236 (void)strftime(t_str, sizeof(t_str), "%a %b %d %H:%M:%S %Y",
1237 localtime(&entry->time));
1239 if (!IRC_WriteStrClient(prefix, RPL_WHOWASUSER_MSG, Client_ID(prefix),
1240 entry->id, entry->user, entry->host, entry->info))
1241 return DISCONNECTED;
1243 return IRC_WriteStrClient(prefix, RPL_WHOISSERVER_MSG, Client_ID(prefix),
1244 entry->id, entry->server, t_str);
1248 * IRC "WHOWAS" function.
1249 * This function implements the IRC command "WHOWHAS". It handles local
1250 * requests and request that should be forwarded to other servers.
1253 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1255 CLIENT *target, *prefix;
1257 char tok_buf[COMMAND_LEN];
1258 int max, last, count, i, nc;
1261 assert( Client != NULL );
1262 assert( Req != NULL );
1264 /* Do not reveal any info on disconnected users? */
1265 if (Conf_MorePrivacy)
1268 /* Wrong number of parameters? */
1270 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1271 Client_ID(Client), Req->command);
1273 return IRC_WriteStrClient(Client, ERR_NONICKNAMEGIVEN_MSG, Client_ID(Client));
1277 target = Client_Search(Req->argv[2]);
1279 target = Client_ThisServer();
1282 if (Client_Type(Client) == CLIENT_SERVER)
1283 prefix = Client_Search(Req->prefix);
1288 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1289 Client_ID(Client), Req->prefix);
1291 /* Forward to other server? */
1292 if (target != Client_ThisServer()) {
1293 if (!target || (Client_Type(target) != CLIENT_SERVER))
1294 return IRC_WriteStrClient(prefix, ERR_NOSUCHSERVER_MSG,
1295 Client_ID(prefix), Req->argv[2]);
1298 IRC_WriteStrClientPrefix( target, prefix, "WHOWAS %s %s %s",
1299 Req->argv[0], Req->argv[1],
1304 whowas = Client_GetWhowas( );
1305 last = Client_GetLastWhowasIndex( );
1309 max = DEFAULT_WHOWAS;
1310 if (Req->argc > 1) {
1311 max = atoi(Req->argv[1]);
1317 * Break up the nick argument into a list of nicks, if applicable
1318 * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1320 strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1321 nick = strtok(tok_buf, ",");
1323 for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1327 if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1328 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1329 return DISCONNECTED;
1333 /* previous entry */
1336 /* "underflow", wrap around */
1340 if (nc && count >= max)
1342 } while (i != last);
1344 if (nc == 0 && !IRC_WriteStrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1345 Client_ID(prefix), nick))
1346 return DISCONNECTED;
1348 return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG, Client_ID(prefix), Req->argv[0]);
1353 * Send LUSERS reply to a client.
1355 * @param Client The receipient of the information.
1356 * @return CONNECTED or DISCONNECTED.
1359 IRC_Send_LUSERS(CLIENT *Client)
1366 assert(Client != NULL);
1368 /* Users, services and serevers in the network */
1369 if (!IRC_WriteStrClient(Client, RPL_LUSERCLIENT_MSG, Client_ID(Client),
1370 Client_UserCount(), Client_ServiceCount(),
1371 Client_ServerCount()))
1372 return DISCONNECTED;
1374 /* Number of IRC operators */
1375 cnt = Client_OperCount( );
1377 if (!IRC_WriteStrClient(Client, RPL_LUSEROP_MSG,
1378 Client_ID(Client), cnt))
1379 return DISCONNECTED;
1382 /* Unknown connections */
1383 cnt = Client_UnknownCount( );
1385 if (!IRC_WriteStrClient(Client, RPL_LUSERUNKNOWN_MSG,
1386 Client_ID(Client), cnt))
1387 return DISCONNECTED;
1390 /* Number of created channels */
1391 if (!IRC_WriteStrClient(Client, RPL_LUSERCHANNELS_MSG,
1393 Channel_CountVisible(Client)))
1394 return DISCONNECTED;
1396 /* Number of local users, services and servers */
1397 if (!IRC_WriteStrClient(Client, RPL_LUSERME_MSG, Client_ID(Client),
1398 Client_MyUserCount(), Client_MyServiceCount(),
1399 Client_MyServerCount()))
1400 return DISCONNECTED;
1403 /* Maximum number of local users */
1404 cnt = Client_MyUserCount();
1405 max = Client_MyMaxUserCount();
1406 if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1407 cnt, max, cnt, max))
1408 return DISCONNECTED;
1409 /* Maximum number of users in the network */
1410 cnt = Client_UserCount();
1411 max = Client_MaxUserCount();
1412 if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1413 cnt, max, cnt, max))
1414 return DISCONNECTED;
1415 /* Connection counters */
1416 if (! IRC_WriteStrClient(Client, RPL_STATSCONN_MSG, Client_ID(Client),
1417 Conn_CountMax(), Conn_CountAccepted()))
1418 return DISCONNECTED;
1422 } /* IRC_Send_LUSERS */
1426 Show_MOTD_Start(CLIENT *Client)
1428 return IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG,
1429 Client_ID( Client ), Client_ID( Client_ThisServer( )));
1433 Show_MOTD_Sendline(CLIENT *Client, const char *msg)
1435 return IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID( Client ), msg);
1439 Show_MOTD_End(CLIENT *Client)
1441 return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
1445 static bool Show_MOTD_SSLInfo(CLIENT *Client)
1448 char buf[COMMAND_LEN] = "Connected using Cipher ";
1450 if (!Conn_GetCipherInfo(Client_Conn(Client), buf + 23, sizeof buf - 23))
1453 if (!Show_MOTD_Sendline(Client, buf))
1460 Show_MOTD_SSLInfo(UNUSED CLIENT *c)
1465 IRC_Show_MOTD( CLIENT *Client )
1468 size_t len_tot, len_str;
1470 assert( Client != NULL );
1472 len_tot = array_bytes(&Conf_Motd);
1473 if (len_tot == 0 && !Conn_UsesSSL(Client_Conn(Client)))
1474 return IRC_WriteStrClient(Client, ERR_NOMOTD_MSG, Client_ID(Client));
1476 if (!Show_MOTD_Start(Client))
1477 return DISCONNECTED;
1479 line = array_start(&Conf_Motd);
1480 while (len_tot > 0) {
1481 len_str = strlen(line) + 1;
1483 assert(len_tot >= len_str);
1486 if (!Show_MOTD_Sendline(Client, line))
1487 return DISCONNECTED;
1491 if (!Show_MOTD_SSLInfo(Client))
1492 return DISCONNECTED;
1493 return Show_MOTD_End(Client);
1494 } /* IRC_Show_MOTD */
1498 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
1500 bool is_visible, is_member;
1501 char str[LINE_LEN + 1];
1505 assert( Client != NULL );
1506 assert( Chan != NULL );
1508 if( Channel_IsMemberOf( Chan, Client )) is_member = true;
1509 else is_member = false;
1511 /* Do not print info on channel memberships to anyone that is not member? */
1512 if (Conf_MorePrivacy && !is_member)
1515 /* Secret channel? */
1516 if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
1518 /* Alle Mitglieder suchen */
1519 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1520 cl2chan = Channel_FirstMember( Chan );
1523 cl = Channel_GetClient( cl2chan );
1525 if( strchr( Client_Modes( cl ), 'i' )) is_visible = false;
1526 else is_visible = true;
1528 if( is_member || is_visible )
1530 /* Nick anhaengen */
1531 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
1532 if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
1533 else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
1534 strlcat( str, Client_ID( cl ), sizeof( str ));
1536 if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
1538 /* Zeile wird zu lang: senden! */
1539 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1540 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1544 /* naechstes Mitglied suchen */
1545 cl2chan = Channel_NextMember( Chan, cl2chan );
1547 if( str[strlen( str ) - 1] != ':')
1549 /* Es sind noch Daten da, die gesendet werden muessen */
1550 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1554 } /* IRC_Send_NAMES */
1558 * Send the ISUPPORT numeric (005).
1559 * This numeric indicates the features that are supported by this server.
1560 * See <http://www.irc.org/tech_docs/005.html> for details.
1563 IRC_Send_ISUPPORT(CLIENT * Client)
1565 if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1567 return DISCONNECTED;
1568 return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1569 CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1570 COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1572 } /* IRC_Send_ISUPPORT */