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 IRC_LINKS( CLIENT *Client, REQUEST *Req )
206 CLIENT *target, *from, *c;
209 assert( Client != NULL );
210 assert( Req != NULL );
212 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
214 /* Server-Mask ermitteln */
215 if( Req->argc > 0 ) mask = Req->argv[Req->argc - 1];
218 /* Absender ermitteln */
219 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
221 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
223 /* An anderen Server forwarden? */
226 target = Client_Search( Req->argv[0] );
227 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
228 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LINKS %s %s", Req->argv[0], Req->argv[1] );
231 /* Wer ist der Absender? */
232 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
233 else target = Client;
234 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
239 if( Client_Type( c ) == CLIENT_SERVER )
241 if( ! IRC_WriteStrClient( target, RPL_LINKS_MSG, Client_ID( target ), Client_ID( c ), Client_ID( Client_TopServer( c ) ? Client_TopServer( c ) : Client_ThisServer( )), Client_Hops( c ), Client_Info( c ))) return DISCONNECTED;
243 c = Client_Next( c );
246 IRC_SetPenalty( target, 1 );
247 return IRC_WriteStrClient( target, RPL_ENDOFLINKS_MSG, Client_ID( target ), mask );
252 IRC_LUSERS( CLIENT *Client, REQUEST *Req )
254 CLIENT *target, *from;
256 assert( Client != NULL );
257 assert( Req != NULL );
259 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
261 /* Absender ermitteln */
262 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
264 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
266 /* An anderen Server forwarden? */
269 target = Client_Search( Req->argv[1] );
270 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
271 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LUSERS %s %s", Req->argv[0], Req->argv[1] );
274 /* Wer ist der Absender? */
275 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
276 else target = Client;
277 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
279 IRC_Send_LUSERS( target );
281 IRC_SetPenalty( target, 1 );
287 * Handler for the IRC command "SERVLIST".
288 * List registered services, see RFC 2811, section 3.5.1: the syntax is
289 * "SERVLIST [<mask> [<type>]]".
292 IRC_SERVLIST(CLIENT *Client, REQUEST *Req)
296 assert(Client != NULL);
300 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
301 Client_ID(Client), Req->command);
303 if (Req->argc < 2 || strcmp(Req->argv[1], "0") == 0) {
304 for (c = Client_First(); c!= NULL; c = Client_Next(c)) {
305 if (Client_Type(c) != CLIENT_SERVICE)
307 if (Req->argc > 0 && !MatchCaseInsensitive(Req->argv[0],
310 if (!IRC_WriteStrClient(Client, RPL_SERVLIST_MSG,
311 Client_ID(Client), Client_Mask(c),
312 Client_Mask(Client_Introducer(c)), "*",
313 0, Client_Hops(c), Client_Info(c)))
318 return IRC_WriteStrClient(Client, RPL_SERVLISTEND_MSG, Client_ID(Client),
319 Req->argc > 0 ? Req->argv[0] : "*",
320 Req->argc > 1 ? Req->argv[1] : "0");
325 IRC_MOTD( CLIENT *Client, REQUEST *Req )
327 CLIENT *from, *target;
329 assert( Client != NULL );
330 assert( Req != NULL );
332 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
334 /* From aus Prefix ermitteln */
335 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
337 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
342 target = Client_Search( Req->argv[0] );
343 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
345 if( target != Client_ThisServer( ))
347 /* Ok, anderer Server ist das Ziel: forwarden */
348 return IRC_WriteStrClientPrefix( target, from, "MOTD %s", Req->argv[0] );
352 IRC_SetPenalty( from, 3 );
353 return IRC_Show_MOTD( from );
358 IRC_NAMES( CLIENT *Client, REQUEST *Req )
360 char rpl[COMMAND_LEN], *ptr;
361 CLIENT *target, *from, *c;
364 assert( Client != NULL );
365 assert( Req != NULL );
367 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
369 /* use prefix to determine "From" */
370 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
372 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
376 /* forward to another server? */
377 target = Client_Search( Req->argv[1] );
378 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
380 if( target != Client_ThisServer( )) {
381 /* target is another server, forward */
382 return IRC_WriteStrClientPrefix( target, from, "NAMES %s :%s", Req->argv[0], Req->argv[1] );
388 /* bestimmte Channels durchgehen */
389 ptr = strtok( Req->argv[0], "," );
392 chan = Channel_Search( ptr );
396 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
398 if( ! IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), ptr )) return DISCONNECTED;
400 /* get next channel name */
401 ptr = strtok( NULL, "," );
406 chan = Channel_First( );
409 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
411 chan = Channel_Next( chan );
414 /* Now print all clients which are not in any channel */
416 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
419 if(( Client_Type( c ) == CLIENT_USER ) && ( Channel_FirstChannelOf( c ) == NULL ) && ( ! strchr( Client_Modes( c ), 'i' )))
421 /* its a user, concatenate ... */
422 if( rpl[strlen( rpl ) - 1] != ':' ) strlcat( rpl, " ", sizeof( rpl ));
423 strlcat( rpl, Client_ID( c ), sizeof( rpl ));
425 if( strlen( rpl ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
427 /* Line is gwoing too long, send now */
428 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
429 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
433 c = Client_Next( c );
435 if( rpl[strlen( rpl ) - 1] != ':')
437 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
440 IRC_SetPenalty( from, 1 );
441 return IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), "*" );
446 t_diff(time_t *t, const time_t d)
454 return (unsigned int)diff;
459 uptime_days(time_t *now)
461 return t_diff(now, 60 * 60 * 24);
466 uptime_hrs(time_t *now)
468 return t_diff(now, 60 * 60);
473 uptime_mins(time_t *now)
475 return t_diff(now, 60);
480 * Handler for the IRC command "STATS".
481 * See RFC 2812 section 3.4.4.
484 IRC_STATS( CLIENT *Client, REQUEST *Req )
486 CLIENT *from, *target, *cl;
491 unsigned int days, hrs, mins;
492 struct list_head *list;
493 struct list_elem *list_item;
495 assert(Client != NULL);
499 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
500 Client_ID(Client), Req->command);
502 /* use prefix to determine "From" */
503 if (Client_Type(Client) == CLIENT_SERVER)
504 from = Client_Search(Req->prefix);
509 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
510 Client_ID(Client), Req->prefix);
512 if (Req->argc == 2) {
513 /* forward to another server? */
514 target = Client_Search(Req->argv[1]);
515 if ((!target) || (Client_Type(target) != CLIENT_SERVER))
516 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
517 Client_ID(from), Req->argv[1]);
519 if (target != Client_ThisServer()) {
520 /* forward to another server */
521 return IRC_WriteStrClientPrefix(target, from,
522 "STATS %s %s", Req->argv[0], Req->argv[1]);
527 query = Req->argv[0][0] ? Req->argv[0][0] : '*';
532 case 'g': /* Network-wide bans ("G-Lines") */
534 case 'k': /* Server-local bans ("K-Lines") */
536 if (!Client_HasMode(from, 'o'))
537 return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG,
539 if (query == 'g' || query == 'G')
540 list = Class_GetList(CLASS_GLINE);
542 list = Class_GetList(CLASS_KLINE);
543 list_item = Lists_GetFirst(list);
545 if (!IRC_WriteStrClient(from, RPL_STATSXLINE_MSG,
546 Client_ID(from), query,
547 Lists_GetMask(list_item),
548 Lists_GetValidity(list_item),
549 Lists_GetReason(list_item)))
551 list_item = Lists_GetNext(list_item);
554 case 'l': /* Link status (servers and own link) */
556 time_now = time(NULL);
557 for (con = Conn_First(); con != NONE; con = Conn_Next(con)) {
558 cl = Conn_GetClient(con);
561 if ((Client_Type(cl) == CLIENT_SERVER)
563 /* Server link or our own connection */
565 if (Conn_Options(con) & CONN_ZIP) {
566 if (!IRC_WriteStrClient
567 (from, RPL_STATSLINKINFOZIP_MSG,
568 Client_ID(from), Client_Mask(cl),
569 Conn_SendQ(con), Conn_SendMsg(con),
575 (long)(time_now - Conn_StartTime(con))))
580 if (!IRC_WriteStrClient
581 (from, RPL_STATSLINKINFO_MSG,
582 Client_ID(from), Client_Mask(cl),
583 Conn_SendQ(con), Conn_SendMsg(con),
584 Conn_SendBytes(con), Conn_RecvMsg(con),
586 (long)(time_now - Conn_StartTime(con))))
591 case 'm': /* IRC command status (usage count) */
593 cmd = Parse_GetCommandStruct();
594 for (; cmd->name; cmd++) {
595 if (cmd->lcount == 0 && cmd->rcount == 0)
597 if (!IRC_WriteStrClient
598 (from, RPL_STATSCOMMANDS_MSG, Client_ID(from),
599 cmd->name, cmd->lcount, cmd->bytes, cmd->rcount))
603 case 'u': /* Server uptime */
605 time_now = time(NULL) - NGIRCd_Start;
606 days = uptime_days(&time_now);
607 hrs = uptime_hrs(&time_now);
608 mins = uptime_mins(&time_now);
609 if (!IRC_WriteStrClient(from, RPL_STATSUPTIME, Client_ID(from),
610 days, hrs, mins, (unsigned int)time_now))
615 IRC_SetPenalty(from, 2);
616 return IRC_WriteStrClient(from, RPL_ENDOFSTATS_MSG,
617 Client_ID(from), query);
622 * Handler for the IRC command "SUMMON".
623 * See RFC 2812 section 4.5. ngIRCd doesn't implement this functionality and
624 * therefore answers with ERR_SUMMONDISABLED.
627 IRC_SUMMON(CLIENT * Client, REQUEST * Req)
629 return IRC_WriteStrClient(Client, ERR_SUMMONDISABLED_MSG,
630 Client_ID(Client), Req->command);
635 IRC_TIME( CLIENT *Client, REQUEST *Req )
637 CLIENT *from, *target;
641 assert( Client != NULL );
642 assert( Req != NULL );
644 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
646 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
648 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
652 target = Client_Search( Req->argv[0] );
653 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
655 if( target != Client_ThisServer( ))
657 return IRC_WriteStrClientPrefix( target, from, "TIME %s", Req->argv[0] );
662 (void)strftime( t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime( &t ));
663 return IRC_WriteStrClient( from, RPL_TIME_MSG, Client_ID( from ), Client_ID( Client_ThisServer( )), t_str );
668 * Handler for the IRC command "USERHOST".
669 * See RFC 2812 section 4.8.
672 IRC_USERHOST(CLIENT *Client, REQUEST *Req)
674 char rpl[COMMAND_LEN];
678 assert(Client != NULL);
682 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
683 Client_ID(Client), Req->command);
690 strlcpy(rpl, RPL_USERHOST_MSG, sizeof rpl);
691 for (i = 0; i < max; i++) {
692 c = Client_Search(Req->argv[i]);
693 if (c && (Client_Type(c) == CLIENT_USER)) {
694 /* This Nick is "online" */
695 strlcat(rpl, Client_ID(c), sizeof(rpl));
696 if (Client_HasMode(c, 'o'))
697 strlcat(rpl, "*", sizeof(rpl));
698 strlcat(rpl, "=", sizeof(rpl));
699 if (Client_HasMode(c, 'a'))
700 strlcat(rpl, "-", sizeof(rpl));
702 strlcat(rpl, "+", sizeof(rpl));
703 strlcat(rpl, Client_User(c), sizeof(rpl));
704 strlcat(rpl, "@", sizeof(rpl));
705 strlcat(rpl, Client_HostnameCloaked(c), sizeof(rpl));
706 strlcat(rpl, " ", sizeof(rpl));
709 ngt_TrimLastChr(rpl, ' ');
711 return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
716 * Handler for the IRC command "USERS".
717 * See RFC 2812 section 4.6. As suggested there the command is disabled.
720 IRC_USERS(CLIENT * Client, REQUEST * Req)
722 return IRC_WriteStrClient(Client, ERR_USERSDISABLED_MSG,
723 Client_ID(Client), Req->command);
728 IRC_VERSION( CLIENT *Client, REQUEST *Req )
730 CLIENT *target, *prefix;
732 assert( Client != NULL );
733 assert( Req != NULL );
735 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
738 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
739 else target = Client_ThisServer( );
741 /* Prefix ermitteln */
742 if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
743 else prefix = Client;
744 if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
746 /* An anderen Server weiterleiten? */
747 if( target != Client_ThisServer( ))
749 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
752 IRC_WriteStrClientPrefix( target, prefix, "VERSION %s", Req->argv[0] );
756 /* send version information */
757 IRC_SetPenalty(Client, 1);
758 return IRC_WriteStrClient(Client, RPL_VERSION_MSG, Client_ID(prefix),
759 PACKAGE_NAME, PACKAGE_VERSION,
760 NGIRCd_DebugLevel, Conf_ServerName,
761 NGIRCd_VersionAddition);
766 write_whoreply(CLIENT *Client, CLIENT *c, const char *channelname, const char *flags)
768 return IRC_WriteStrClient(Client, RPL_WHOREPLY_MSG, Client_ID(Client),
769 channelname, Client_User(c),
770 Client_HostnameCloaked(c),
771 Client_ID(Client_Introducer(c)), Client_ID(c),
772 flags, Client_Hops(c), Client_Info(c));
777 who_flags_status(const char *client_modes)
779 if (strchr(client_modes, 'a'))
780 return "G"; /* away */
786 who_flags_qualifier(const char *chan_user_modes)
788 if (strchr(chan_user_modes, 'o'))
790 else if (strchr(chan_user_modes, 'v'))
797 * Send WHO reply for a "channel target" ("WHO #channel").
799 * @param Client Client requesting the information.
800 * @param Chan Channel being requested.
801 * @param OnlyOps Only display IRC operators.
802 * @return CONNECTED or DISCONNECTED.
805 IRC_WHO_Channel(CLIENT *Client, CHANNEL *Chan, bool OnlyOps)
807 bool is_visible, is_member, is_ircop;
809 const char *client_modes;
810 const char *chan_user_modes;
814 assert( Client != NULL );
815 assert( Chan != NULL );
817 is_member = Channel_IsMemberOf(Chan, Client);
819 /* Secret channel? */
820 if (!is_member && strchr(Channel_Modes(Chan), 's'))
821 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG,
822 Client_ID(Client), Channel_Name(Chan));
824 cl2chan = Channel_FirstMember(Chan);
825 for (; cl2chan ; cl2chan = Channel_NextMember(Chan, cl2chan)) {
826 c = Channel_GetClient(cl2chan);
828 client_modes = Client_Modes(c);
829 is_ircop = strchr(client_modes, 'o') != NULL;
830 if (OnlyOps && !is_ircop)
833 is_visible = strchr(client_modes, 'i') == NULL;
834 if (is_member || is_visible) {
835 strcpy(flags, who_flags_status(client_modes));
837 strlcat(flags, "*", sizeof(flags));
839 chan_user_modes = Channel_UserModes(Chan, c);
840 strlcat(flags, who_flags_qualifier(chan_user_modes),
843 if (!write_whoreply(Client, c, Channel_Name(Chan),
848 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client),
854 * Send WHO reply for a "mask target" ("WHO m*sk").
856 * @param Client Client requesting the information.
857 * @param Mask Mask being requested or NULL for "all" clients.
858 * @param OnlyOps Only display IRC operators.
859 * @return CONNECTED or DISCONNECTED.
862 IRC_WHO_Mask(CLIENT *Client, char *Mask, bool OnlyOps)
867 bool client_match, is_visible;
870 assert (Client != NULL);
875 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
876 if (Client_Type(c) != CLIENT_USER)
879 if (OnlyOps && !Client_HasMode(c, 'o'))
883 /* Match pattern against user host/server/name/nick */
884 client_match = MatchCaseInsensitive(Mask,
887 client_match = MatchCaseInsensitive(Mask,
888 Client_ID(Client_Introducer(c)));
890 client_match = MatchCaseInsensitive(Mask,
893 client_match = MatchCaseInsensitive(Mask,
896 continue; /* no match: skip this client */
899 is_visible = !Client_HasMode(c, 'i');
901 /* Target client is invisible, but mask matches exactly? */
902 if (!is_visible && Mask && strcasecmp(Client_ID(c), Mask) == 0)
905 /* Target still invisible, but are both on the same channel? */
907 cl2chan = Channel_FirstChannelOf(Client);
908 while (cl2chan && !is_visible) {
909 chan = Channel_GetChannel(cl2chan);
910 if (Channel_IsMemberOf(chan, c))
912 cl2chan = Channel_NextChannelOf(Client, cl2chan);
916 if (!is_visible) /* target user is not visible */
919 strcpy(flags, who_flags_status(Client_Modes(c)));
920 if (strchr(Client_Modes(c), 'o'))
921 strlcat(flags, "*", sizeof(flags));
923 if (!write_whoreply(Client, c, "*", flags))
928 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client),
934 * Handler for the IRC "WHO" command.
936 * See RFC 2812, 3.6.1 "Who query".
938 * @param Client The client from which this command has been received.
939 * @param Req Request structure with prefix and all parameters.
940 * @return CONNECTED or DISCONNECTED.
943 IRC_WHO(CLIENT *Client, REQUEST *Req)
948 assert (Client != NULL);
949 assert (Req != NULL);
952 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
953 Client_ID(Client), Req->command);
957 if (Req->argc == 2) {
958 if (strcmp(Req->argv[1], "o") == 0)
962 return IRC_WriteStrClient(Client,
963 ERR_NEEDMOREPARAMS_MSG,
969 IRC_SetPenalty(Client, 1);
970 if (Req->argc >= 1) {
971 /* Channel or mask given */
972 chan = Channel_Search(Req->argv[0]);
974 /* Members of a channel have been requested */
975 IRC_SetPenalty(Client, 1);
976 return IRC_WHO_Channel(Client, chan, only_ops);
978 if (strcmp(Req->argv[0], "0") != 0) {
979 /* A mask has been given. But please note this RFC
980 * stupidity: "0" is same as no arguments ... */
981 IRC_SetPenalty(Client, 3);
982 return IRC_WHO_Mask(Client, Req->argv[0], only_ops);
986 /* No channel or (valid) mask given */
987 IRC_SetPenalty(Client, 2);
988 return IRC_WHO_Mask(Client, NULL, only_ops);
993 * Generate WHOIS reply of one actual client.
995 * @param Client The client from which this command has been received.
996 * @param from The client requesting the information ("originator").
997 * @param c The client of which information should be returned.
998 * @returns CONNECTED or DISCONNECTED.
1001 IRC_WHOIS_SendReply(CLIENT *Client, CLIENT *from, CLIENT *c)
1003 char str[LINE_LEN + 1];
1007 /* Nick, user, hostname and client info */
1008 if (!IRC_WriteStrClient(from, RPL_WHOISUSER_MSG, Client_ID(from),
1009 Client_ID(c), Client_User(c),
1010 Client_HostnameCloaked(c), Client_Info(c)))
1011 return DISCONNECTED;
1014 if (!IRC_WriteStrClient(from, RPL_WHOISSERVER_MSG, Client_ID(from),
1015 Client_ID(c), Client_ID(Client_Introducer(c)),
1016 Client_Info(Client_Introducer(c))))
1017 return DISCONNECTED;
1020 snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
1021 Client_ID(from), Client_ID(c));
1022 cl2chan = Channel_FirstChannelOf(c);
1024 chan = Channel_GetChannel(cl2chan);
1025 assert(chan != NULL);
1028 cl2chan = Channel_NextChannelOf(c, cl2chan);
1030 /* Secret channel? */
1031 if (strchr(Channel_Modes(chan), 's')
1032 && !Channel_IsMemberOf(chan, Client))
1035 /* Local channel and request is not from a user? */
1036 if (Client_Type(Client) == CLIENT_SERVER
1037 && Channel_IsLocal(chan))
1040 /* Concatenate channel names */
1041 if (str[strlen(str) - 1] != ':')
1042 strlcat(str, " ", sizeof(str));
1044 strlcat(str, who_flags_qualifier(Channel_UserModes(chan, c)),
1046 strlcat(str, Channel_Name(chan), sizeof(str));
1048 if (strlen(str) > (LINE_LEN - CHANNEL_NAME_LEN - 4)) {
1049 /* Line becomes too long: send it! */
1050 if (!IRC_WriteStrClient(Client, "%s", str))
1051 return DISCONNECTED;
1052 snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
1053 Client_ID(from), Client_ID(c));
1056 if(str[strlen(str) - 1] != ':') {
1057 /* There is data left to send: */
1058 if (!IRC_WriteStrClient(Client, "%s", str))
1059 return DISCONNECTED;
1063 if (Client_HasMode(c, 'o') &&
1064 !IRC_WriteStrClient(from, RPL_WHOISOPERATOR_MSG,
1065 Client_ID(from), Client_ID(c)))
1066 return DISCONNECTED;
1068 /* Connected using SSL? */
1069 if (Conn_UsesSSL(Client_Conn(c)) &&
1070 !IRC_WriteStrClient(from, RPL_WHOISSSL_MSG,
1071 Client_ID(from), Client_ID(c)))
1072 return DISCONNECTED;
1074 /* Idle and signon time (local clients only!) */
1075 if (!Conf_MorePrivacy && Client_Conn(c) > NONE &&
1076 !IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
1077 Client_ID(from), Client_ID(c),
1078 (unsigned long)Conn_GetIdle(Client_Conn(c)),
1079 (unsigned long)Conn_GetSignon(Client_Conn(c))))
1080 return DISCONNECTED;
1083 if (Client_HasMode(c, 'a') &&
1084 !IRC_WriteStrClient(from, RPL_AWAY_MSG,
1085 Client_ID(from), Client_ID(c),
1087 return DISCONNECTED;
1089 return IRC_WriteStrClient(from, RPL_ENDOFWHOIS_MSG,
1090 Client_ID(from), Client_ID(c));
1091 } /* IRC_WHOIS_SendReply */
1095 * Handler for the IRC "WHOIS" command.
1097 * See RFC 2812, 3.6.2 "Whois query".
1099 * @param Client The client from which this command has been received.
1100 * @param Req Request structure with prefix and all parameters.
1101 * @return CONNECTED or DISCONNECTED.
1104 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
1106 CLIENT *from, *target, *c;
1107 unsigned int match_count = 0, found = 0;
1108 bool has_wildcards, is_remote;
1109 bool got_wildcard = false;
1112 assert( Client != NULL );
1113 assert( Req != NULL );
1115 /* Bad number of parameters? */
1116 if (Req->argc < 1 || Req->argc > 2)
1117 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1118 Client_ID(Client), Req->command);
1120 /* Search sender of the WHOIS */
1121 if (Client_Type(Client) == CLIENT_SERVER) {
1122 from = Client_Search(Req->prefix);
1124 IRC_SetPenalty(Client, 1);
1128 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1129 Client_ID(Client), Req->prefix);
1131 /* Get target server for this command */
1132 if (Req->argc > 1) {
1133 /* Search the target server, which can be specified as a
1134 * nick name on that server as well: */
1135 target = Client_Search(Req->argv[0]);
1137 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
1138 Client_ID(from), Req->argv[0]);
1140 target = Client_ThisServer();
1141 assert(target != NULL);
1143 /* Forward to other server? */
1144 if (Client_NextHop(target) != Client_ThisServer() &&
1145 Client_Type(Client_NextHop(target)) == CLIENT_SERVER)
1146 return IRC_WriteStrClientPrefix(target, from,
1148 Req->argv[0], Req->argv[1]);
1150 is_remote = Client_Conn(from) < 0;
1151 for (query = strtok(Req->argv[Req->argc - 1], ",");
1153 query = strtok(NULL, ","), found++)
1155 has_wildcards = query[strcspn(query, "*?")] != 0;
1157 * follows ircd 2.10 implementation:
1158 * - handle up to 3 targets
1159 * - no wildcards for remote clients
1160 * - only one wildcard target per local client
1162 * also, at most ten matches are returned.
1164 if (!has_wildcards || is_remote) {
1165 c = Client_Search(query);
1167 if (!IRC_WHOIS_SendReply(Client, from, c))
1168 return DISCONNECTED;
1170 if (!IRC_WriteStrClient(Client,
1174 return DISCONNECTED;
1179 /* we already handled one wildcard query */
1180 if (!IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1181 Client_ID(Client), query))
1182 return DISCONNECTED;
1185 got_wildcard = true;
1186 IRC_SetPenalty(Client, 3);
1188 for (c = Client_First(); c && match_count < 10; c = Client_Next(c)) {
1189 if (Client_Type(c) != CLIENT_USER)
1191 if (!MatchCaseInsensitive(query, Client_ID(c)))
1193 if (!IRC_WHOIS_SendReply(Client, from, c))
1194 return DISCONNECTED;
1198 if (match_count == 0)
1199 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1200 Client_ID(Client), Req->argv[Req->argc - 1]);
1207 WHOWAS_EntryWrite(CLIENT *prefix, WHOWAS *entry)
1211 (void)strftime(t_str, sizeof(t_str), "%a %b %d %H:%M:%S %Y",
1212 localtime(&entry->time));
1214 if (!IRC_WriteStrClient(prefix, RPL_WHOWASUSER_MSG, Client_ID(prefix),
1215 entry->id, entry->user, entry->host, entry->info))
1216 return DISCONNECTED;
1218 return IRC_WriteStrClient(prefix, RPL_WHOISSERVER_MSG, Client_ID(prefix),
1219 entry->id, entry->server, t_str);
1223 * IRC "WHOWAS" function.
1224 * This function implements the IRC command "WHOWHAS". It handles local
1225 * requests and request that should be forwarded to other servers.
1228 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1230 CLIENT *target, *prefix;
1232 char tok_buf[COMMAND_LEN];
1233 int max, last, count, i, nc;
1236 assert( Client != NULL );
1237 assert( Req != NULL );
1239 /* Do not reveal any info on disconnected users? */
1240 if (Conf_MorePrivacy)
1243 /* Wrong number of parameters? */
1245 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1246 Client_ID(Client), Req->command);
1248 return IRC_WriteStrClient(Client, ERR_NONICKNAMEGIVEN_MSG, Client_ID(Client));
1252 target = Client_Search(Req->argv[2]);
1254 target = Client_ThisServer();
1257 if (Client_Type(Client) == CLIENT_SERVER)
1258 prefix = Client_Search(Req->prefix);
1263 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1264 Client_ID(Client), Req->prefix);
1266 /* Forward to other server? */
1267 if (target != Client_ThisServer()) {
1268 if (!target || (Client_Type(target) != CLIENT_SERVER))
1269 return IRC_WriteStrClient(prefix, ERR_NOSUCHSERVER_MSG,
1270 Client_ID(prefix), Req->argv[2]);
1273 IRC_WriteStrClientPrefix( target, prefix, "WHOWAS %s %s %s",
1274 Req->argv[0], Req->argv[1],
1279 whowas = Client_GetWhowas( );
1280 last = Client_GetLastWhowasIndex( );
1284 max = DEFAULT_WHOWAS;
1285 if (Req->argc > 1) {
1286 max = atoi(Req->argv[1]);
1292 * Break up the nick argument into a list of nicks, if applicable
1293 * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1295 strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1296 nick = strtok(tok_buf, ",");
1298 for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1302 if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1303 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1304 return DISCONNECTED;
1308 /* previous entry */
1311 /* "underflow", wrap around */
1315 if (nc && count >= max)
1317 } while (i != last);
1319 if (nc == 0 && !IRC_WriteStrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1320 Client_ID(prefix), nick))
1321 return DISCONNECTED;
1323 return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG, Client_ID(prefix), Req->argv[0]);
1328 * Send LUSERS reply to a client.
1330 * @param Client The receipient of the information.
1331 * @return CONNECTED or DISCONNECTED.
1334 IRC_Send_LUSERS(CLIENT *Client)
1341 assert(Client != NULL);
1343 /* Users, services and serevers in the network */
1344 if (!IRC_WriteStrClient(Client, RPL_LUSERCLIENT_MSG, Client_ID(Client),
1345 Client_UserCount(), Client_ServiceCount(),
1346 Client_ServerCount()))
1347 return DISCONNECTED;
1349 /* Number of IRC operators */
1350 cnt = Client_OperCount( );
1352 if (!IRC_WriteStrClient(Client, RPL_LUSEROP_MSG,
1353 Client_ID(Client), cnt))
1354 return DISCONNECTED;
1357 /* Unknown connections */
1358 cnt = Client_UnknownCount( );
1360 if (!IRC_WriteStrClient(Client, RPL_LUSERUNKNOWN_MSG,
1361 Client_ID(Client), cnt))
1362 return DISCONNECTED;
1365 /* Number of created channels */
1366 if (!IRC_WriteStrClient(Client, RPL_LUSERCHANNELS_MSG,
1368 Channel_CountVisible(Client)))
1369 return DISCONNECTED;
1371 /* Number of local users, services and servers */
1372 if (!IRC_WriteStrClient(Client, RPL_LUSERME_MSG, Client_ID(Client),
1373 Client_MyUserCount(), Client_MyServiceCount(),
1374 Client_MyServerCount()))
1375 return DISCONNECTED;
1378 /* Maximum number of local users */
1379 cnt = Client_MyUserCount();
1380 max = Client_MyMaxUserCount();
1381 if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1382 cnt, max, cnt, max))
1383 return DISCONNECTED;
1384 /* Maximum number of users in the network */
1385 cnt = Client_UserCount();
1386 max = Client_MaxUserCount();
1387 if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1388 cnt, max, cnt, max))
1389 return DISCONNECTED;
1390 /* Connection counters */
1391 if (! IRC_WriteStrClient(Client, RPL_STATSCONN_MSG, Client_ID(Client),
1392 Conn_CountMax(), Conn_CountAccepted()))
1393 return DISCONNECTED;
1397 } /* IRC_Send_LUSERS */
1401 Show_MOTD_Start(CLIENT *Client)
1403 return IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG,
1404 Client_ID( Client ), Client_ID( Client_ThisServer( )));
1408 Show_MOTD_Sendline(CLIENT *Client, const char *msg)
1410 return IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID( Client ), msg);
1414 Show_MOTD_End(CLIENT *Client)
1416 return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
1420 static bool Show_MOTD_SSLInfo(CLIENT *Client)
1423 char buf[COMMAND_LEN] = "Connected using Cipher ";
1425 if (!Conn_GetCipherInfo(Client_Conn(Client), buf + 23, sizeof buf - 23))
1428 if (!Show_MOTD_Sendline(Client, buf))
1435 Show_MOTD_SSLInfo(UNUSED CLIENT *c)
1440 IRC_Show_MOTD( CLIENT *Client )
1443 size_t len_tot, len_str;
1445 assert( Client != NULL );
1447 len_tot = array_bytes(&Conf_Motd);
1448 if (len_tot == 0 && !Conn_UsesSSL(Client_Conn(Client)))
1449 return IRC_WriteStrClient(Client, ERR_NOMOTD_MSG, Client_ID(Client));
1451 if (!Show_MOTD_Start(Client))
1452 return DISCONNECTED;
1454 line = array_start(&Conf_Motd);
1455 while (len_tot > 0) {
1456 len_str = strlen(line) + 1;
1458 assert(len_tot >= len_str);
1461 if (!Show_MOTD_Sendline(Client, line))
1462 return DISCONNECTED;
1466 if (!Show_MOTD_SSLInfo(Client))
1467 return DISCONNECTED;
1468 return Show_MOTD_End(Client);
1469 } /* IRC_Show_MOTD */
1473 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
1475 bool is_visible, is_member;
1476 char str[LINE_LEN + 1];
1480 assert( Client != NULL );
1481 assert( Chan != NULL );
1483 if( Channel_IsMemberOf( Chan, Client )) is_member = true;
1484 else is_member = false;
1486 /* Do not print info on channel memberships to anyone that is not member? */
1487 if (Conf_MorePrivacy && !is_member)
1490 /* Secret channel? */
1491 if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
1493 /* Alle Mitglieder suchen */
1494 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1495 cl2chan = Channel_FirstMember( Chan );
1498 cl = Channel_GetClient( cl2chan );
1500 if( strchr( Client_Modes( cl ), 'i' )) is_visible = false;
1501 else is_visible = true;
1503 if( is_member || is_visible )
1505 /* Nick anhaengen */
1506 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
1507 if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
1508 else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
1509 strlcat( str, Client_ID( cl ), sizeof( str ));
1511 if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
1513 /* Zeile wird zu lang: senden! */
1514 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1515 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1519 /* naechstes Mitglied suchen */
1520 cl2chan = Channel_NextMember( Chan, cl2chan );
1522 if( str[strlen( str ) - 1] != ':')
1524 /* Es sind noch Daten da, die gesendet werden muessen */
1525 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1529 } /* IRC_Send_NAMES */
1533 * Send the ISUPPORT numeric (005).
1534 * This numeric indicates the features that are supported by this server.
1535 * See <http://www.irc.org/tech_docs/005.html> for details.
1538 IRC_Send_ISUPPORT(CLIENT * Client)
1540 if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1542 return DISCONNECTED;
1543 return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1544 CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1545 COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1547 } /* IRC_Send_ISUPPORT */