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 if (!IRC_WriteStrClient(from, RPL_LINKS_MSG,
259 Client_ID(from), Client_ID(c),
260 Client_ID(Client_TopServer(c)
261 ? Client_TopServer(c)
262 : Client_ThisServer()),
263 Client_Hops(c), Client_Info(c)))
268 return IRC_WriteStrClient(from, RPL_ENDOFLINKS_MSG,
269 Client_ID(from), mask);
274 IRC_LUSERS( CLIENT *Client, REQUEST *Req )
276 CLIENT *target, *from;
278 assert( Client != NULL );
279 assert( Req != NULL );
281 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
283 /* Absender ermitteln */
284 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
286 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
288 /* An anderen Server forwarden? */
291 target = Client_Search( Req->argv[1] );
292 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
293 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LUSERS %s %s", Req->argv[0], Req->argv[1] );
296 /* Wer ist der Absender? */
297 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
298 else target = Client;
299 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
301 IRC_Send_LUSERS( target );
303 IRC_SetPenalty( target, 1 );
309 * Handler for the IRC command "SERVLIST".
310 * List registered services, see RFC 2811, section 3.5.1: the syntax is
311 * "SERVLIST [<mask> [<type>]]".
314 IRC_SERVLIST(CLIENT *Client, REQUEST *Req)
318 assert(Client != NULL);
322 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
323 Client_ID(Client), Req->command);
325 if (Req->argc < 2 || strcmp(Req->argv[1], "0") == 0) {
326 for (c = Client_First(); c!= NULL; c = Client_Next(c)) {
327 if (Client_Type(c) != CLIENT_SERVICE)
329 if (Req->argc > 0 && !MatchCaseInsensitive(Req->argv[0],
332 if (!IRC_WriteStrClient(Client, RPL_SERVLIST_MSG,
333 Client_ID(Client), Client_Mask(c),
334 Client_Mask(Client_Introducer(c)), "*",
335 0, Client_Hops(c), Client_Info(c)))
340 return IRC_WriteStrClient(Client, RPL_SERVLISTEND_MSG, Client_ID(Client),
341 Req->argc > 0 ? Req->argv[0] : "*",
342 Req->argc > 1 ? Req->argv[1] : "0");
347 IRC_MOTD( CLIENT *Client, REQUEST *Req )
349 CLIENT *from, *target;
351 assert( Client != NULL );
352 assert( Req != NULL );
354 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
356 /* From aus Prefix ermitteln */
357 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
359 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
364 target = Client_Search( Req->argv[0] );
365 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
367 if( target != Client_ThisServer( ))
369 /* Ok, anderer Server ist das Ziel: forwarden */
370 return IRC_WriteStrClientPrefix( target, from, "MOTD %s", Req->argv[0] );
374 IRC_SetPenalty( from, 3 );
375 return IRC_Show_MOTD( from );
380 IRC_NAMES( CLIENT *Client, REQUEST *Req )
382 char rpl[COMMAND_LEN], *ptr;
383 CLIENT *target, *from, *c;
386 assert( Client != NULL );
387 assert( Req != NULL );
389 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
391 /* use prefix to determine "From" */
392 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
394 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
398 /* forward to another server? */
399 target = Client_Search( Req->argv[1] );
400 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
402 if( target != Client_ThisServer( )) {
403 /* target is another server, forward */
404 return IRC_WriteStrClientPrefix( target, from, "NAMES %s :%s", Req->argv[0], Req->argv[1] );
410 /* bestimmte Channels durchgehen */
411 ptr = strtok( Req->argv[0], "," );
414 chan = Channel_Search( ptr );
418 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
420 if( ! IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), ptr )) return DISCONNECTED;
422 /* get next channel name */
423 ptr = strtok( NULL, "," );
428 chan = Channel_First( );
431 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
433 chan = Channel_Next( chan );
436 /* Now print all clients which are not in any channel */
438 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
441 if(( Client_Type( c ) == CLIENT_USER ) && ( Channel_FirstChannelOf( c ) == NULL ) && ( ! strchr( Client_Modes( c ), 'i' )))
443 /* its a user, concatenate ... */
444 if( rpl[strlen( rpl ) - 1] != ':' ) strlcat( rpl, " ", sizeof( rpl ));
445 strlcat( rpl, Client_ID( c ), sizeof( rpl ));
447 if( strlen( rpl ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
449 /* Line is gwoing too long, send now */
450 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
451 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
455 c = Client_Next( c );
457 if( rpl[strlen( rpl ) - 1] != ':')
459 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
462 IRC_SetPenalty( from, 1 );
463 return IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), "*" );
468 t_diff(time_t *t, const time_t d)
476 return (unsigned int)diff;
481 uptime_days(time_t *now)
483 return t_diff(now, 60 * 60 * 24);
488 uptime_hrs(time_t *now)
490 return t_diff(now, 60 * 60);
495 uptime_mins(time_t *now)
497 return t_diff(now, 60);
502 * Handler for the IRC command "STATS".
503 * See RFC 2812 section 3.4.4.
506 IRC_STATS( CLIENT *Client, REQUEST *Req )
508 CLIENT *from, *target, *cl;
513 unsigned int days, hrs, mins;
514 struct list_head *list;
515 struct list_elem *list_item;
517 assert(Client != NULL);
521 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
522 Client_ID(Client), Req->command);
524 /* use prefix to determine "From" */
525 if (Client_Type(Client) == CLIENT_SERVER)
526 from = Client_Search(Req->prefix);
531 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
532 Client_ID(Client), Req->prefix);
534 if (Req->argc == 2) {
535 /* forward to another server? */
536 target = Client_Search(Req->argv[1]);
537 if ((!target) || (Client_Type(target) != CLIENT_SERVER))
538 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
539 Client_ID(from), Req->argv[1]);
541 if (target != Client_ThisServer()) {
542 /* forward to another server */
543 return IRC_WriteStrClientPrefix(target, from,
544 "STATS %s %s", Req->argv[0], Req->argv[1]);
549 query = Req->argv[0][0] ? Req->argv[0][0] : '*';
554 case 'g': /* Network-wide bans ("G-Lines") */
556 case 'k': /* Server-local bans ("K-Lines") */
558 if (!Client_HasMode(from, 'o'))
559 return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG,
561 if (query == 'g' || query == 'G')
562 list = Class_GetList(CLASS_GLINE);
564 list = Class_GetList(CLASS_KLINE);
565 list_item = Lists_GetFirst(list);
567 if (!IRC_WriteStrClient(from, RPL_STATSXLINE_MSG,
568 Client_ID(from), query,
569 Lists_GetMask(list_item),
570 Lists_GetValidity(list_item),
571 Lists_GetReason(list_item)))
573 list_item = Lists_GetNext(list_item);
576 case 'l': /* Link status (servers and own link) */
578 time_now = time(NULL);
579 for (con = Conn_First(); con != NONE; con = Conn_Next(con)) {
580 cl = Conn_GetClient(con);
583 if ((Client_Type(cl) == CLIENT_SERVER)
585 /* Server link or our own connection */
587 if (Conn_Options(con) & CONN_ZIP) {
588 if (!IRC_WriteStrClient
589 (from, RPL_STATSLINKINFOZIP_MSG,
590 Client_ID(from), Client_Mask(cl),
591 Conn_SendQ(con), Conn_SendMsg(con),
597 (long)(time_now - Conn_StartTime(con))))
602 if (!IRC_WriteStrClient
603 (from, RPL_STATSLINKINFO_MSG,
604 Client_ID(from), Client_Mask(cl),
605 Conn_SendQ(con), Conn_SendMsg(con),
606 Conn_SendBytes(con), Conn_RecvMsg(con),
608 (long)(time_now - Conn_StartTime(con))))
613 case 'm': /* IRC command status (usage count) */
615 cmd = Parse_GetCommandStruct();
616 for (; cmd->name; cmd++) {
617 if (cmd->lcount == 0 && cmd->rcount == 0)
619 if (!IRC_WriteStrClient
620 (from, RPL_STATSCOMMANDS_MSG, Client_ID(from),
621 cmd->name, cmd->lcount, cmd->bytes, cmd->rcount))
625 case 'u': /* Server uptime */
627 time_now = time(NULL) - NGIRCd_Start;
628 days = uptime_days(&time_now);
629 hrs = uptime_hrs(&time_now);
630 mins = uptime_mins(&time_now);
631 if (!IRC_WriteStrClient(from, RPL_STATSUPTIME, Client_ID(from),
632 days, hrs, mins, (unsigned int)time_now))
637 IRC_SetPenalty(from, 2);
638 return IRC_WriteStrClient(from, RPL_ENDOFSTATS_MSG,
639 Client_ID(from), query);
644 * Handler for the IRC command "SUMMON".
645 * See RFC 2812 section 4.5. ngIRCd doesn't implement this functionality and
646 * therefore answers with ERR_SUMMONDISABLED.
649 IRC_SUMMON(CLIENT * Client, REQUEST * Req)
651 return IRC_WriteStrClient(Client, ERR_SUMMONDISABLED_MSG,
652 Client_ID(Client), Req->command);
657 IRC_TIME( CLIENT *Client, REQUEST *Req )
659 CLIENT *from, *target;
663 assert( Client != NULL );
664 assert( Req != NULL );
666 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
668 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
670 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
674 target = Client_Search( Req->argv[0] );
675 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
677 if( target != Client_ThisServer( ))
679 return IRC_WriteStrClientPrefix( target, from, "TIME %s", Req->argv[0] );
684 (void)strftime( t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime( &t ));
685 return IRC_WriteStrClient( from, RPL_TIME_MSG, Client_ID( from ), Client_ID( Client_ThisServer( )), t_str );
690 * Handler for the IRC command "USERHOST".
691 * See RFC 2812 section 4.8.
694 IRC_USERHOST(CLIENT *Client, REQUEST *Req)
696 char rpl[COMMAND_LEN];
700 assert(Client != NULL);
704 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
705 Client_ID(Client), Req->command);
712 strlcpy(rpl, RPL_USERHOST_MSG, sizeof rpl);
713 for (i = 0; i < max; i++) {
714 c = Client_Search(Req->argv[i]);
715 if (c && (Client_Type(c) == CLIENT_USER)) {
716 /* This Nick is "online" */
717 strlcat(rpl, Client_ID(c), sizeof(rpl));
718 if (Client_HasMode(c, 'o'))
719 strlcat(rpl, "*", sizeof(rpl));
720 strlcat(rpl, "=", sizeof(rpl));
721 if (Client_HasMode(c, 'a'))
722 strlcat(rpl, "-", sizeof(rpl));
724 strlcat(rpl, "+", sizeof(rpl));
725 strlcat(rpl, Client_User(c), sizeof(rpl));
726 strlcat(rpl, "@", sizeof(rpl));
727 strlcat(rpl, Client_HostnameCloaked(c), sizeof(rpl));
728 strlcat(rpl, " ", sizeof(rpl));
731 ngt_TrimLastChr(rpl, ' ');
733 return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
738 * Handler for the IRC command "USERS".
739 * See RFC 2812 section 4.6. As suggested there the command is disabled.
742 IRC_USERS(CLIENT * Client, REQUEST * Req)
744 return IRC_WriteStrClient(Client, ERR_USERSDISABLED_MSG,
745 Client_ID(Client), Req->command);
750 IRC_VERSION( CLIENT *Client, REQUEST *Req )
752 CLIENT *target, *prefix;
754 assert( Client != NULL );
755 assert( Req != NULL );
757 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
760 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
761 else target = Client_ThisServer( );
763 /* Prefix ermitteln */
764 if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
765 else prefix = Client;
766 if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
768 /* An anderen Server weiterleiten? */
769 if( target != Client_ThisServer( ))
771 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
774 IRC_WriteStrClientPrefix( target, prefix, "VERSION %s", Req->argv[0] );
778 /* send version information */
779 IRC_SetPenalty(Client, 1);
780 return IRC_WriteStrClient(Client, RPL_VERSION_MSG, Client_ID(prefix),
781 PACKAGE_NAME, PACKAGE_VERSION,
782 NGIRCd_DebugLevel, Conf_ServerName,
783 NGIRCd_VersionAddition);
788 write_whoreply(CLIENT *Client, CLIENT *c, const char *channelname, const char *flags)
790 return IRC_WriteStrClient(Client, RPL_WHOREPLY_MSG, Client_ID(Client),
791 channelname, Client_User(c),
792 Client_HostnameCloaked(c),
793 Client_ID(Client_Introducer(c)), Client_ID(c),
794 flags, Client_Hops(c), Client_Info(c));
799 who_flags_status(const char *client_modes)
801 if (strchr(client_modes, 'a'))
802 return "G"; /* away */
808 who_flags_qualifier(const char *chan_user_modes)
810 if (strchr(chan_user_modes, 'o'))
812 else if (strchr(chan_user_modes, 'v'))
819 * Send WHO reply for a "channel target" ("WHO #channel").
821 * @param Client Client requesting the information.
822 * @param Chan Channel being requested.
823 * @param OnlyOps Only display IRC operators.
824 * @return CONNECTED or DISCONNECTED.
827 IRC_WHO_Channel(CLIENT *Client, CHANNEL *Chan, bool OnlyOps)
829 bool is_visible, is_member, is_ircop;
831 const char *client_modes;
832 const char *chan_user_modes;
836 assert( Client != NULL );
837 assert( Chan != NULL );
839 is_member = Channel_IsMemberOf(Chan, Client);
841 /* Secret channel? */
842 if (!is_member && strchr(Channel_Modes(Chan), 's'))
843 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG,
844 Client_ID(Client), Channel_Name(Chan));
846 cl2chan = Channel_FirstMember(Chan);
847 for (; cl2chan ; cl2chan = Channel_NextMember(Chan, cl2chan)) {
848 c = Channel_GetClient(cl2chan);
850 client_modes = Client_Modes(c);
851 is_ircop = strchr(client_modes, 'o') != NULL;
852 if (OnlyOps && !is_ircop)
855 is_visible = strchr(client_modes, 'i') == NULL;
856 if (is_member || is_visible) {
857 strcpy(flags, who_flags_status(client_modes));
859 strlcat(flags, "*", sizeof(flags));
861 chan_user_modes = Channel_UserModes(Chan, c);
862 strlcat(flags, who_flags_qualifier(chan_user_modes),
865 if (!write_whoreply(Client, c, Channel_Name(Chan),
870 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client),
876 * Send WHO reply for a "mask target" ("WHO m*sk").
878 * @param Client Client requesting the information.
879 * @param Mask Mask being requested or NULL for "all" clients.
880 * @param OnlyOps Only display IRC operators.
881 * @return CONNECTED or DISCONNECTED.
884 IRC_WHO_Mask(CLIENT *Client, char *Mask, bool OnlyOps)
889 bool client_match, is_visible;
892 assert (Client != NULL);
897 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
898 if (Client_Type(c) != CLIENT_USER)
901 if (OnlyOps && !Client_HasMode(c, 'o'))
905 /* Match pattern against user host/server/name/nick */
906 client_match = MatchCaseInsensitive(Mask,
909 client_match = MatchCaseInsensitive(Mask,
910 Client_ID(Client_Introducer(c)));
912 client_match = MatchCaseInsensitive(Mask,
915 client_match = MatchCaseInsensitive(Mask,
918 continue; /* no match: skip this client */
921 is_visible = !Client_HasMode(c, 'i');
923 /* Target client is invisible, but mask matches exactly? */
924 if (!is_visible && Mask && strcasecmp(Client_ID(c), Mask) == 0)
927 /* Target still invisible, but are both on the same channel? */
929 cl2chan = Channel_FirstChannelOf(Client);
930 while (cl2chan && !is_visible) {
931 chan = Channel_GetChannel(cl2chan);
932 if (Channel_IsMemberOf(chan, c))
934 cl2chan = Channel_NextChannelOf(Client, cl2chan);
938 if (!is_visible) /* target user is not visible */
941 strcpy(flags, who_flags_status(Client_Modes(c)));
942 if (strchr(Client_Modes(c), 'o'))
943 strlcat(flags, "*", sizeof(flags));
945 if (!write_whoreply(Client, c, "*", flags))
950 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client),
956 * Handler for the IRC "WHO" command.
958 * See RFC 2812, 3.6.1 "Who query".
960 * @param Client The client from which this command has been received.
961 * @param Req Request structure with prefix and all parameters.
962 * @return CONNECTED or DISCONNECTED.
965 IRC_WHO(CLIENT *Client, REQUEST *Req)
970 assert (Client != NULL);
971 assert (Req != NULL);
974 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
975 Client_ID(Client), Req->command);
979 if (Req->argc == 2) {
980 if (strcmp(Req->argv[1], "o") == 0)
984 return IRC_WriteStrClient(Client,
985 ERR_NEEDMOREPARAMS_MSG,
991 IRC_SetPenalty(Client, 1);
992 if (Req->argc >= 1) {
993 /* Channel or mask given */
994 chan = Channel_Search(Req->argv[0]);
996 /* Members of a channel have been requested */
997 IRC_SetPenalty(Client, 1);
998 return IRC_WHO_Channel(Client, chan, only_ops);
1000 if (strcmp(Req->argv[0], "0") != 0) {
1001 /* A mask has been given. But please note this RFC
1002 * stupidity: "0" is same as no arguments ... */
1003 IRC_SetPenalty(Client, 3);
1004 return IRC_WHO_Mask(Client, Req->argv[0], only_ops);
1008 /* No channel or (valid) mask given */
1009 IRC_SetPenalty(Client, 2);
1010 return IRC_WHO_Mask(Client, NULL, only_ops);
1015 * Generate WHOIS reply of one actual client.
1017 * @param Client The client from which this command has been received.
1018 * @param from The client requesting the information ("originator").
1019 * @param c The client of which information should be returned.
1020 * @returns CONNECTED or DISCONNECTED.
1023 IRC_WHOIS_SendReply(CLIENT *Client, CLIENT *from, CLIENT *c)
1025 char str[LINE_LEN + 1];
1029 /* Nick, user, hostname and client info */
1030 if (!IRC_WriteStrClient(from, RPL_WHOISUSER_MSG, Client_ID(from),
1031 Client_ID(c), Client_User(c),
1032 Client_HostnameCloaked(c), Client_Info(c)))
1033 return DISCONNECTED;
1036 if (!IRC_WriteStrClient(from, RPL_WHOISSERVER_MSG, Client_ID(from),
1037 Client_ID(c), Client_ID(Client_Introducer(c)),
1038 Client_Info(Client_Introducer(c))))
1039 return DISCONNECTED;
1042 snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
1043 Client_ID(from), Client_ID(c));
1044 cl2chan = Channel_FirstChannelOf(c);
1046 chan = Channel_GetChannel(cl2chan);
1047 assert(chan != NULL);
1050 cl2chan = Channel_NextChannelOf(c, cl2chan);
1052 /* Secret channel? */
1053 if (strchr(Channel_Modes(chan), 's')
1054 && !Channel_IsMemberOf(chan, Client))
1057 /* Local channel and request is not from a user? */
1058 if (Client_Type(Client) == CLIENT_SERVER
1059 && Channel_IsLocal(chan))
1062 /* Concatenate channel names */
1063 if (str[strlen(str) - 1] != ':')
1064 strlcat(str, " ", sizeof(str));
1066 strlcat(str, who_flags_qualifier(Channel_UserModes(chan, c)),
1068 strlcat(str, Channel_Name(chan), sizeof(str));
1070 if (strlen(str) > (LINE_LEN - CHANNEL_NAME_LEN - 4)) {
1071 /* Line becomes too long: send it! */
1072 if (!IRC_WriteStrClient(Client, "%s", str))
1073 return DISCONNECTED;
1074 snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
1075 Client_ID(from), Client_ID(c));
1078 if(str[strlen(str) - 1] != ':') {
1079 /* There is data left to send: */
1080 if (!IRC_WriteStrClient(Client, "%s", str))
1081 return DISCONNECTED;
1085 if (Client_HasMode(c, 'o') &&
1086 !IRC_WriteStrClient(from, RPL_WHOISOPERATOR_MSG,
1087 Client_ID(from), Client_ID(c)))
1088 return DISCONNECTED;
1090 /* Connected using SSL? */
1091 if (Conn_UsesSSL(Client_Conn(c)) &&
1092 !IRC_WriteStrClient(from, RPL_WHOISSSL_MSG,
1093 Client_ID(from), Client_ID(c)))
1094 return DISCONNECTED;
1096 /* Idle and signon time (local clients only!) */
1097 if (!Conf_MorePrivacy && Client_Conn(c) > NONE &&
1098 !IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
1099 Client_ID(from), Client_ID(c),
1100 (unsigned long)Conn_GetIdle(Client_Conn(c)),
1101 (unsigned long)Conn_GetSignon(Client_Conn(c))))
1102 return DISCONNECTED;
1105 if (Client_HasMode(c, 'a') &&
1106 !IRC_WriteStrClient(from, RPL_AWAY_MSG,
1107 Client_ID(from), Client_ID(c),
1109 return DISCONNECTED;
1111 return IRC_WriteStrClient(from, RPL_ENDOFWHOIS_MSG,
1112 Client_ID(from), Client_ID(c));
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;
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 for (query = strtok(Req->argv[Req->argc - 1], ",");
1175 query = strtok(NULL, ","), found++)
1177 has_wildcards = query[strcspn(query, "*?")] != 0;
1179 * follows ircd 2.10 implementation:
1180 * - handle up to 3 targets
1181 * - no wildcards for remote clients
1182 * - only one wildcard target per local client
1184 * also, at most ten matches are returned.
1186 if (!has_wildcards || is_remote) {
1187 c = Client_Search(query);
1189 if (!IRC_WHOIS_SendReply(Client, from, c))
1190 return DISCONNECTED;
1192 if (!IRC_WriteStrClient(Client,
1196 return DISCONNECTED;
1201 /* we already handled one wildcard query */
1202 if (!IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1203 Client_ID(Client), query))
1204 return DISCONNECTED;
1207 got_wildcard = true;
1208 IRC_SetPenalty(Client, 3);
1210 for (c = Client_First(); c && match_count < 10; c = Client_Next(c)) {
1211 if (Client_Type(c) != CLIENT_USER)
1213 if (!MatchCaseInsensitive(query, Client_ID(c)))
1215 if (!IRC_WHOIS_SendReply(Client, from, c))
1216 return DISCONNECTED;
1220 if (match_count == 0)
1221 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1222 Client_ID(Client), Req->argv[Req->argc - 1]);
1229 WHOWAS_EntryWrite(CLIENT *prefix, WHOWAS *entry)
1233 (void)strftime(t_str, sizeof(t_str), "%a %b %d %H:%M:%S %Y",
1234 localtime(&entry->time));
1236 if (!IRC_WriteStrClient(prefix, RPL_WHOWASUSER_MSG, Client_ID(prefix),
1237 entry->id, entry->user, entry->host, entry->info))
1238 return DISCONNECTED;
1240 return IRC_WriteStrClient(prefix, RPL_WHOISSERVER_MSG, Client_ID(prefix),
1241 entry->id, entry->server, t_str);
1245 * IRC "WHOWAS" function.
1246 * This function implements the IRC command "WHOWHAS". It handles local
1247 * requests and request that should be forwarded to other servers.
1250 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1252 CLIENT *target, *prefix;
1254 char tok_buf[COMMAND_LEN];
1255 int max, last, count, i, nc;
1258 assert( Client != NULL );
1259 assert( Req != NULL );
1261 /* Do not reveal any info on disconnected users? */
1262 if (Conf_MorePrivacy)
1265 /* Wrong number of parameters? */
1267 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1268 Client_ID(Client), Req->command);
1270 return IRC_WriteStrClient(Client, ERR_NONICKNAMEGIVEN_MSG, Client_ID(Client));
1274 target = Client_Search(Req->argv[2]);
1276 target = Client_ThisServer();
1279 if (Client_Type(Client) == CLIENT_SERVER)
1280 prefix = Client_Search(Req->prefix);
1285 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1286 Client_ID(Client), Req->prefix);
1288 /* Forward to other server? */
1289 if (target != Client_ThisServer()) {
1290 if (!target || (Client_Type(target) != CLIENT_SERVER))
1291 return IRC_WriteStrClient(prefix, ERR_NOSUCHSERVER_MSG,
1292 Client_ID(prefix), Req->argv[2]);
1295 IRC_WriteStrClientPrefix( target, prefix, "WHOWAS %s %s %s",
1296 Req->argv[0], Req->argv[1],
1301 whowas = Client_GetWhowas( );
1302 last = Client_GetLastWhowasIndex( );
1306 max = DEFAULT_WHOWAS;
1307 if (Req->argc > 1) {
1308 max = atoi(Req->argv[1]);
1314 * Break up the nick argument into a list of nicks, if applicable
1315 * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1317 strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1318 nick = strtok(tok_buf, ",");
1320 for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1324 if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1325 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1326 return DISCONNECTED;
1330 /* previous entry */
1333 /* "underflow", wrap around */
1337 if (nc && count >= max)
1339 } while (i != last);
1341 if (nc == 0 && !IRC_WriteStrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1342 Client_ID(prefix), nick))
1343 return DISCONNECTED;
1345 return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG, Client_ID(prefix), Req->argv[0]);
1350 * Send LUSERS reply to a client.
1352 * @param Client The receipient of the information.
1353 * @return CONNECTED or DISCONNECTED.
1356 IRC_Send_LUSERS(CLIENT *Client)
1363 assert(Client != NULL);
1365 /* Users, services and serevers in the network */
1366 if (!IRC_WriteStrClient(Client, RPL_LUSERCLIENT_MSG, Client_ID(Client),
1367 Client_UserCount(), Client_ServiceCount(),
1368 Client_ServerCount()))
1369 return DISCONNECTED;
1371 /* Number of IRC operators */
1372 cnt = Client_OperCount( );
1374 if (!IRC_WriteStrClient(Client, RPL_LUSEROP_MSG,
1375 Client_ID(Client), cnt))
1376 return DISCONNECTED;
1379 /* Unknown connections */
1380 cnt = Client_UnknownCount( );
1382 if (!IRC_WriteStrClient(Client, RPL_LUSERUNKNOWN_MSG,
1383 Client_ID(Client), cnt))
1384 return DISCONNECTED;
1387 /* Number of created channels */
1388 if (!IRC_WriteStrClient(Client, RPL_LUSERCHANNELS_MSG,
1390 Channel_CountVisible(Client)))
1391 return DISCONNECTED;
1393 /* Number of local users, services and servers */
1394 if (!IRC_WriteStrClient(Client, RPL_LUSERME_MSG, Client_ID(Client),
1395 Client_MyUserCount(), Client_MyServiceCount(),
1396 Client_MyServerCount()))
1397 return DISCONNECTED;
1400 /* Maximum number of local users */
1401 cnt = Client_MyUserCount();
1402 max = Client_MyMaxUserCount();
1403 if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1404 cnt, max, cnt, max))
1405 return DISCONNECTED;
1406 /* Maximum number of users in the network */
1407 cnt = Client_UserCount();
1408 max = Client_MaxUserCount();
1409 if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1410 cnt, max, cnt, max))
1411 return DISCONNECTED;
1412 /* Connection counters */
1413 if (! IRC_WriteStrClient(Client, RPL_STATSCONN_MSG, Client_ID(Client),
1414 Conn_CountMax(), Conn_CountAccepted()))
1415 return DISCONNECTED;
1419 } /* IRC_Send_LUSERS */
1423 Show_MOTD_Start(CLIENT *Client)
1425 return IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG,
1426 Client_ID( Client ), Client_ID( Client_ThisServer( )));
1430 Show_MOTD_Sendline(CLIENT *Client, const char *msg)
1432 return IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID( Client ), msg);
1436 Show_MOTD_End(CLIENT *Client)
1438 return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
1442 static bool Show_MOTD_SSLInfo(CLIENT *Client)
1445 char buf[COMMAND_LEN] = "Connected using Cipher ";
1447 if (!Conn_GetCipherInfo(Client_Conn(Client), buf + 23, sizeof buf - 23))
1450 if (!Show_MOTD_Sendline(Client, buf))
1457 Show_MOTD_SSLInfo(UNUSED CLIENT *c)
1462 IRC_Show_MOTD( CLIENT *Client )
1465 size_t len_tot, len_str;
1467 assert( Client != NULL );
1469 len_tot = array_bytes(&Conf_Motd);
1470 if (len_tot == 0 && !Conn_UsesSSL(Client_Conn(Client)))
1471 return IRC_WriteStrClient(Client, ERR_NOMOTD_MSG, Client_ID(Client));
1473 if (!Show_MOTD_Start(Client))
1474 return DISCONNECTED;
1476 line = array_start(&Conf_Motd);
1477 while (len_tot > 0) {
1478 len_str = strlen(line) + 1;
1480 assert(len_tot >= len_str);
1483 if (!Show_MOTD_Sendline(Client, line))
1484 return DISCONNECTED;
1488 if (!Show_MOTD_SSLInfo(Client))
1489 return DISCONNECTED;
1490 return Show_MOTD_End(Client);
1491 } /* IRC_Show_MOTD */
1495 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
1497 bool is_visible, is_member;
1498 char str[LINE_LEN + 1];
1502 assert( Client != NULL );
1503 assert( Chan != NULL );
1505 if( Channel_IsMemberOf( Chan, Client )) is_member = true;
1506 else is_member = false;
1508 /* Do not print info on channel memberships to anyone that is not member? */
1509 if (Conf_MorePrivacy && !is_member)
1512 /* Secret channel? */
1513 if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
1515 /* Alle Mitglieder suchen */
1516 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1517 cl2chan = Channel_FirstMember( Chan );
1520 cl = Channel_GetClient( cl2chan );
1522 if( strchr( Client_Modes( cl ), 'i' )) is_visible = false;
1523 else is_visible = true;
1525 if( is_member || is_visible )
1527 /* Nick anhaengen */
1528 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
1529 if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
1530 else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
1531 strlcat( str, Client_ID( cl ), sizeof( str ));
1533 if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
1535 /* Zeile wird zu lang: senden! */
1536 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1537 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1541 /* naechstes Mitglied suchen */
1542 cl2chan = Channel_NextMember( Chan, cl2chan );
1544 if( str[strlen( str ) - 1] != ':')
1546 /* Es sind noch Daten da, die gesendet werden muessen */
1547 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1551 } /* IRC_Send_NAMES */
1555 * Send the ISUPPORT numeric (005).
1556 * This numeric indicates the features that are supported by this server.
1557 * See <http://www.irc.org/tech_docs/005.html> for details.
1560 IRC_Send_ISUPPORT(CLIENT * Client)
1562 if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1564 return DISCONNECTED;
1565 return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1566 CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1567 COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1569 } /* IRC_Send_ISUPPORT */