2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2008 Alexander Barton (alex@barton.de)
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.
26 #include "conn-func.h"
38 #include "irc-write.h"
45 IRC_ADMIN(CLIENT *Client, REQUEST *Req )
47 CLIENT *target, *prefix;
49 assert( Client != NULL );
50 assert( Req != NULL );
52 /* Falsche Anzahl Parameter? */
53 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
56 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
57 else target = Client_ThisServer( );
59 /* Prefix ermitteln */
60 if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
62 if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
64 /* An anderen Server weiterleiten? */
65 if( target != Client_ThisServer( ))
67 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
70 IRC_WriteStrClientPrefix( target, prefix, "ADMIN %s", Req->argv[0] );
74 /* mit Versionsinfo antworten */
75 if( ! IRC_WriteStrClient( Client, RPL_ADMINME_MSG, Client_ID( prefix ), Conf_ServerName )) return DISCONNECTED;
76 if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC1_MSG, Client_ID( prefix ), Conf_ServerAdmin1 )) return DISCONNECTED;
77 if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC2_MSG, Client_ID( prefix ), Conf_ServerAdmin2 )) return DISCONNECTED;
78 if( ! IRC_WriteStrClient( Client, RPL_ADMINEMAIL_MSG, Client_ID( prefix ), Conf_ServerAdminMail )) return DISCONNECTED;
80 IRC_SetPenalty( Client, 1 );
86 * Handler for the IRC command "INFO".
87 * See RFC 2812 section 3.4.10.
90 IRC_INFO(CLIENT * Client, REQUEST * Req)
92 CLIENT *target, *prefix;
95 assert(Client != NULL);
98 /* Wrong number of parameters? */
100 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
101 Client_ID(Client), Req->command);
103 /* Determine prefix */
104 if (Client_Type(Client) == CLIENT_SERVER)
105 prefix = Client_Search(Req->prefix);
109 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
110 Client_ID(Client), Req->prefix);
112 /* Look for a target */
114 target = Client_Search(Req->argv[0]);
116 target = Client_ThisServer();
118 /* Make sure that the target is a server */
119 if (target && Client_Type(target) != CLIENT_SERVER)
120 target = Client_Introducer(target);
123 return IRC_WriteStrClient(prefix, ERR_NOSUCHSERVER_MSG,
124 Client_ID(prefix), Req->argv[0]);
126 /* Pass on to another server? */
127 if (target != Client_ThisServer()) {
128 IRC_WriteStrClientPrefix(target, prefix, "INFO %s",
133 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix),
137 #if defined(__DATE__) && defined(__TIME__)
138 snprintf(msg, sizeof(msg), "Birth Date: %s at %s", __DATE__, __TIME__);
139 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
143 strlcpy(msg, "On-line since ", sizeof(msg));
144 strlcat(msg, NGIRCd_StartStr, sizeof(msg));
145 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
148 if (!IRC_WriteStrClient(Client, RPL_ENDOFINFO_MSG, Client_ID(prefix)))
151 IRC_SetPenalty(Client, 2);
157 IRC_ISON( CLIENT *Client, REQUEST *Req )
159 char rpl[COMMAND_LEN];
164 assert( Client != NULL );
165 assert( Req != NULL );
167 /* Falsche Anzahl Parameter? */
168 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
170 strlcpy( rpl, RPL_ISON_MSG, sizeof rpl );
171 for( i = 0; i < Req->argc; i++ )
173 ptr = strtok( Req->argv[i], " " );
177 c = Client_Search( ptr );
178 if( c && ( Client_Type( c ) == CLIENT_USER ))
180 /* Dieser Nick ist "online" */
181 strlcat( rpl, ptr, sizeof( rpl ));
182 strlcat( rpl, " ", sizeof( rpl ));
184 ptr = strtok( NULL, " " );
187 ngt_TrimLastChr(rpl, ' ');
189 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
194 IRC_LINKS( CLIENT *Client, REQUEST *Req )
196 CLIENT *target, *from, *c;
199 assert( Client != NULL );
200 assert( Req != NULL );
202 /* Falsche Anzahl Parameter? */
203 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
205 /* Server-Mask ermitteln */
206 if( Req->argc > 0 ) mask = Req->argv[Req->argc - 1];
209 /* Absender ermitteln */
210 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
212 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
214 /* An anderen Server forwarden? */
217 target = Client_Search( Req->argv[0] );
218 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
219 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LINKS %s %s", Req->argv[0], Req->argv[1] );
222 /* Wer ist der Absender? */
223 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
224 else target = Client;
225 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
230 if( Client_Type( c ) == CLIENT_SERVER )
232 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;
234 c = Client_Next( c );
237 IRC_SetPenalty( target, 1 );
238 return IRC_WriteStrClient( target, RPL_ENDOFLINKS_MSG, Client_ID( target ), mask );
243 IRC_LUSERS( CLIENT *Client, REQUEST *Req )
245 CLIENT *target, *from;
247 assert( Client != NULL );
248 assert( Req != NULL );
250 /* Falsche Anzahl Parameter? */
251 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
253 /* Absender ermitteln */
254 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
256 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
258 /* An anderen Server forwarden? */
261 target = Client_Search( Req->argv[1] );
262 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
263 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LUSERS %s %s", Req->argv[0], Req->argv[1] );
266 /* Wer ist der Absender? */
267 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
268 else target = Client;
269 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
271 IRC_Send_LUSERS( target );
273 IRC_SetPenalty( target, 1 );
279 * Handler for the IRC command "SERVLIST".
280 * List registered services, see RFC 2811, section 3.5.1: the syntax is
281 * "SERVLIST [<mask> [<type>]]".
284 IRC_SERVLIST(CLIENT *Client, REQUEST *Req)
288 assert(Client != NULL);
292 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
293 Client_ID(Client), Req->command);
295 if (Req->argc < 2 || strcmp(Req->argv[1], "0") == 0) {
296 for (c = Client_First(); c!= NULL; c = Client_Next(c)) {
297 if (Client_Type(c) != CLIENT_SERVICE)
299 if (Req->argc > 0 && !MatchCaseInsensitive(Req->argv[0],
302 if (!IRC_WriteStrClient(Client, RPL_SERVLIST_MSG,
303 Client_ID(Client), Client_Mask(c),
304 Client_Mask(Client_Introducer(c)), "*",
305 0, Client_Hops(c), Client_Info(c)))
310 return IRC_WriteStrClient(Client, RPL_SERVLISTEND_MSG, Client_ID(Client),
311 Req->argc > 0 ? Req->argv[0] : "*",
312 Req->argc > 1 ? Req->argv[1] : "0");
317 IRC_MOTD( CLIENT *Client, REQUEST *Req )
319 CLIENT *from, *target;
321 assert( Client != NULL );
322 assert( Req != NULL );
324 /* Falsche Anzahl Parameter? */
325 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
327 /* From aus Prefix ermitteln */
328 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
330 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
334 /* an anderen Server forwarden */
335 target = Client_Search( Req->argv[0] );
336 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
338 if( target != Client_ThisServer( ))
340 /* Ok, anderer Server ist das Ziel: forwarden */
341 return IRC_WriteStrClientPrefix( target, from, "MOTD %s", Req->argv[0] );
345 IRC_SetPenalty( from, 3 );
346 return IRC_Show_MOTD( from );
351 IRC_NAMES( CLIENT *Client, REQUEST *Req )
353 char rpl[COMMAND_LEN], *ptr;
354 CLIENT *target, *from, *c;
357 assert( Client != NULL );
358 assert( Req != NULL );
360 /* Falsche Anzahl Parameter? */
361 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
363 /* From aus Prefix ermitteln */
364 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
366 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
370 /* an anderen Server forwarden */
371 target = Client_Search( Req->argv[1] );
372 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
374 if( target != Client_ThisServer( ))
376 /* Ok, anderer Server ist das Ziel: forwarden */
377 return IRC_WriteStrClientPrefix( target, from, "NAMES %s :%s", Req->argv[0], Req->argv[1] );
383 /* bestimmte Channels durchgehen */
384 ptr = strtok( Req->argv[0], "," );
387 chan = Channel_Search( ptr );
391 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
393 if( ! IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), ptr )) return DISCONNECTED;
395 /* naechsten Namen ermitteln */
396 ptr = strtok( NULL, "," );
401 /* alle Channels durchgehen */
402 chan = Channel_First( );
406 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
408 /* naechster Channel */
409 chan = Channel_Next( chan );
412 /* Nun noch alle Clients ausgeben, die in keinem Channel sind */
414 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
417 if(( Client_Type( c ) == CLIENT_USER ) && ( Channel_FirstChannelOf( c ) == NULL ) && ( ! strchr( Client_Modes( c ), 'i' )))
419 /* Okay, das ist ein User: anhaengen */
420 if( rpl[strlen( rpl ) - 1] != ':' ) strlcat( rpl, " ", sizeof( rpl ));
421 strlcat( rpl, Client_ID( c ), sizeof( rpl ));
423 if( strlen( rpl ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
425 /* Zeile wird zu lang: senden! */
426 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
427 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
431 /* naechster Client */
432 c = Client_Next( c );
434 if( rpl[strlen( rpl ) - 1] != ':')
436 /* es wurden User gefunden */
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)
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 IRC_STATS( CLIENT *Client, REQUEST *Req )
482 CLIENT *from, *target, *cl;
487 unsigned int days, hrs, mins;
489 assert( Client != NULL );
490 assert( Req != NULL );
492 /* Falsche Anzahl Parameter? */
494 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
496 /* From aus Prefix ermitteln */
497 if (Client_Type(Client) == CLIENT_SERVER)
498 from = Client_Search(Req->prefix);
503 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix);
505 if (Req->argc == 2) {
506 /* an anderen Server forwarden */
507 target = Client_Search( Req->argv[1] );
508 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
509 return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
511 if( target != Client_ThisServer()) {
512 /* Ok, anderer Server ist das Ziel: forwarden */
513 return IRC_WriteStrClientPrefix( target, from, "STATS %s %s", Req->argv[0], Req->argv[1] );
518 query = Req->argv[0][0] ? Req->argv[0][0] : '*';
523 case 'l': /* Links */
525 time_now = time(NULL);
526 for (con = Conn_First(); con != NONE ;con = Conn_Next(con)) {
527 cl = Conn_GetClient(con);
530 if ((Client_Type(cl) == CLIENT_SERVER) || (cl == Client)) {
531 /* Server link or our own connection */
533 if (Conn_Options(con) & CONN_ZIP) {
534 if (!IRC_WriteStrClient(from, RPL_STATSLINKINFOZIP_MSG,
535 Client_ID(from), Client_Mask(cl), Conn_SendQ(con),
536 Conn_SendMsg(con), Zip_SendBytes(con), Conn_SendBytes(con),
537 Conn_RecvMsg(con), Zip_RecvBytes(con), Conn_RecvBytes(con), (long)(time_now - Conn_StartTime(con))))
542 if (!IRC_WriteStrClient(from, RPL_STATSLINKINFO_MSG, Client_ID(from),
543 Client_Mask(cl), Conn_SendQ(con), Conn_SendMsg(con), Conn_SendBytes(con),
544 Conn_RecvMsg(con), Conn_RecvBytes(con), (long)(time_now - Conn_StartTime(con))))
549 case 'm': /* IRC-Commands */
551 cmd = Parse_GetCommandStruct( );
552 for (; cmd->name ; cmd++) {
553 if (cmd->lcount == 0 && cmd->rcount == 0)
555 if (!IRC_WriteStrClient(from, RPL_STATSCOMMANDS_MSG, Client_ID(from),
556 cmd->name, cmd->lcount, cmd->bytes, cmd->rcount))
560 case 'u': /* server uptime */
562 time_now = time(NULL) - NGIRCd_Start;
563 days = uptime_days(&time_now);
564 hrs = uptime_hrs(&time_now);
565 mins = uptime_mins(&time_now);
566 if (!IRC_WriteStrClient(from, RPL_STATSUPTIME, Client_ID(from),
567 days, hrs, mins, (unsigned int) time_now))
572 IRC_SetPenalty(from, 2);
573 return IRC_WriteStrClient(from, RPL_ENDOFSTATS_MSG, Client_ID(from), query);
578 * Handler for the IRC command "SUMMON".
579 * See RFC 2812 section 4.5. ngIRCd doesn't implement this functionality and
580 * therefore answers with ERR_SUMMONDISABLED.
583 IRC_SUMMON(CLIENT * Client, REQUEST * Req)
585 return IRC_WriteStrClient(Client, ERR_SUMMONDISABLED_MSG,
586 Client_ID(Client), Req->command);
591 IRC_TIME( CLIENT *Client, REQUEST *Req )
593 CLIENT *from, *target;
597 assert( Client != NULL );
598 assert( Req != NULL );
600 /* Falsche Anzahl Parameter? */
601 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
603 /* From aus Prefix ermitteln */
604 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
606 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
610 /* an anderen Server forwarden */
611 target = Client_Search( Req->argv[0] );
612 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
614 if( target != Client_ThisServer( ))
616 /* Ok, anderer Server ist das Ziel: forwarden */
617 return IRC_WriteStrClientPrefix( target, from, "TIME %s", Req->argv[0] );
622 (void)strftime( t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime( &t ));
623 return IRC_WriteStrClient( from, RPL_TIME_MSG, Client_ID( from ), Client_ID( Client_ThisServer( )), t_str );
628 IRC_USERHOST( CLIENT *Client, REQUEST *Req )
630 char rpl[COMMAND_LEN];
634 assert( Client != NULL );
635 assert( Req != NULL );
637 /* Falsche Anzahl Parameter? */
638 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
640 if( Req->argc > 5 ) max = 5;
641 else max = Req->argc;
643 strlcpy( rpl, RPL_USERHOST_MSG, sizeof rpl );
644 for( i = 0; i < max; i++ )
646 c = Client_Search( Req->argv[i] );
647 if( c && ( Client_Type( c ) == CLIENT_USER ))
649 /* Dieser Nick ist "online" */
650 strlcat( rpl, Client_ID( c ), sizeof( rpl ));
651 if( Client_HasMode( c, 'o' )) strlcat( rpl, "*", sizeof( rpl ));
652 strlcat( rpl, "=", sizeof( rpl ));
653 if( Client_HasMode( c, 'a' )) strlcat( rpl, "-", sizeof( rpl ));
654 else strlcat( rpl, "+", sizeof( rpl ));
655 strlcat( rpl, Client_User( c ), sizeof( rpl ));
656 strlcat( rpl, "@", sizeof( rpl ));
657 strlcat( rpl, Client_Hostname( c ), sizeof( rpl ));
658 strlcat( rpl, " ", sizeof( rpl ));
661 ngt_TrimLastChr( rpl, ' ');
663 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
668 * Handler for the IRC command "USERS".
669 * See RFC 2812 section 4.6. As suggested there the command is disabled.
672 IRC_USERS(CLIENT * Client, REQUEST * Req)
674 return IRC_WriteStrClient(Client, ERR_USERSDISABLED_MSG,
675 Client_ID(Client), Req->command);
680 IRC_VERSION( CLIENT *Client, REQUEST *Req )
682 CLIENT *target, *prefix;
684 assert( Client != NULL );
685 assert( Req != NULL );
687 /* Falsche Anzahl Parameter? */
688 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
691 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
692 else target = Client_ThisServer( );
694 /* Prefix ermitteln */
695 if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
696 else prefix = Client;
697 if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
699 /* An anderen Server weiterleiten? */
700 if( target != Client_ThisServer( ))
702 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
705 IRC_WriteStrClientPrefix( target, prefix, "VERSION %s", Req->argv[0] );
709 /* send version information */
710 IRC_SetPenalty(Client, 1);
711 return IRC_WriteStrClient(Client, RPL_VERSION_MSG, Client_ID(prefix),
712 PACKAGE_NAME, PACKAGE_VERSION,
713 NGIRCd_DebugLevel, Conf_ServerName,
714 NGIRCd_VersionAddition);
719 write_whoreply(CLIENT *Client, CLIENT *c, const char *channelname, const char *flags)
721 return IRC_WriteStrClient(Client, RPL_WHOREPLY_MSG, Client_ID(Client), channelname,
722 Client_User(c), Client_Hostname(c), Client_ID(Client_Introducer(c)), Client_ID(c),
723 flags, Client_Hops(c), Client_Info(c));
728 who_flags_status(const char *client_modes)
730 if (strchr(client_modes, 'a'))
731 return "G"; /* away */
737 who_flags_qualifier(const char *chan_user_modes)
739 if (strchr(chan_user_modes, 'o'))
741 else if (strchr(chan_user_modes, 'v'))
748 IRC_Send_WHO(CLIENT *Client, CHANNEL *Chan, bool OnlyOps)
750 bool is_visible, is_member, is_ircop;
752 const char *client_modes;
753 const char *chan_user_modes;
757 assert( Client != NULL );
758 assert( Chan != NULL );
760 is_member = Channel_IsMemberOf(Chan, Client);
762 /* Secret channel? */
763 if (!is_member && strchr(Channel_Modes(Chan), 's'))
764 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), Channel_Name(Chan));
766 cl2chan = Channel_FirstMember(Chan);
767 for (; cl2chan ; cl2chan = Channel_NextMember(Chan, cl2chan)) {
768 c = Channel_GetClient(cl2chan);
770 client_modes = Client_Modes(c);
771 is_ircop = strchr(client_modes, 'o') != NULL;
772 if (OnlyOps && !is_ircop)
775 is_visible = strchr(client_modes, 'i') == NULL;
776 if (is_member || is_visible) {
777 strcpy(flags, who_flags_status(client_modes));
779 strlcat(flags, "*", sizeof(flags));
781 chan_user_modes = Channel_UserModes(Chan, c);
782 strlcat(flags, who_flags_qualifier(chan_user_modes), sizeof(flags));
784 if (!write_whoreply(Client, c, Channel_Name(Chan), flags))
788 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), Channel_Name(Chan));
793 IRC_WHO( CLIENT *Client, REQUEST *Req )
795 bool only_ops, have_arg, client_match;
796 const char *channelname, *client_modes, *chan_user_modes;
797 char pattern[COMMAND_LEN];
803 assert( Client != NULL );
804 assert( Req != NULL );
807 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
812 if (Req->argc == 2) {
813 if (strcmp(Req->argv[1], "o") == 0)
816 else return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
820 IRC_SetPenalty(Client, 1);
821 if (Req->argc >= 1) { /* Channel or Mask. */
822 chan = Channel_Search(Req->argv[0]);
824 return IRC_Send_WHO(Client, chan, only_ops);
825 if (strcmp(Req->argv[0], "0") != 0) { /* RFC stupidity, same as no arguments */
827 strlcpy(pattern, Req->argv[0], sizeof(pattern));
828 ngt_LowerStr(pattern);
829 IRC_SetPenalty(Client, 3);
833 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
834 if (Client_Type(c) != CLIENT_USER)
838 * In the absence of the parameter, all visible (users who aren't
839 * invisible (user mode +i) and who don't have a common channel
840 * with the requesting client) are listed.
842 * The same result can be achieved by using a [sic] of "0"
843 * or any wildcard which will end up matching every visible user.
845 * The [sic] passed to WHO is matched against users' host, server, real name and
846 * nickname if the channel cannot be found.
848 client_modes = Client_Modes(c);
849 if (strchr(client_modes, 'i'))
852 if (only_ops && !strchr(client_modes, 'o'))
855 if (have_arg) { /* match pattern against user host/server/name/nick */
856 client_match = MatchCaseInsensitive(pattern, Client_Hostname(c)); /* user's host */
858 client_match = MatchCaseInsensitive(pattern, Client_ID(Client_Introducer(c))); /* server */
860 client_match = Match(Req->argv[0], Client_Info(c)); /* realname */
862 client_match = MatchCaseInsensitive(pattern, Client_ID(c)); /* nick name */
864 if (!client_match) /* This isn't the client you're looking for */
868 strcpy(flags, who_flags_status(client_modes));
870 if (strchr(client_modes, 'o')) /* this client is an operator */
871 strlcat(flags, "*", sizeof(flags));
873 /* Search suitable channel */
874 cl2chan = Channel_FirstChannelOf(c);
876 cn = Channel_GetChannel(cl2chan);
877 if (Channel_IsMemberOf(cn, Client) ||
878 !strchr(Channel_Modes(cn), 's'))
880 channelname = Channel_Name(cn);
883 cl2chan = Channel_NextChannelOf(c, cl2chan);
886 chan = Channel_GetChannel(cl2chan);
887 chan_user_modes = Channel_UserModes(chan, c);
888 strlcat(flags, who_flags_qualifier(chan_user_modes), sizeof(flags));
892 if (!write_whoreply(Client, c, channelname, flags))
897 channelname = Req->argv[0];
901 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), channelname);
906 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
908 CLIENT *from, *target, *c;
909 char str[LINE_LEN + 1];
913 assert( Client != NULL );
914 assert( Req != NULL );
916 /* Bad number of parameters? */
917 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
920 c = Client_Search( Req->argv[Req->argc - 1] );
921 if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[Req->argc - 1] );
923 /* Search sender of the WHOIS */
924 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
926 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
928 /* Forward to other server? */
931 /* Search target server (can be specified as nick of that server!) */
932 target = Client_Search( Req->argv[0] );
933 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
935 else target = Client_ThisServer( );
937 assert( target != NULL );
939 if(( Client_NextHop( target ) != Client_ThisServer( )) && ( Client_Type( Client_NextHop( target )) == CLIENT_SERVER )) return IRC_WriteStrClientPrefix( target, from, "WHOIS %s :%s", Req->argv[0], Req->argv[1] );
941 /* Nick, user and name */
942 if( ! IRC_WriteStrClient( from, RPL_WHOISUSER_MSG, Client_ID( from ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
945 if( ! IRC_WriteStrClient( from, RPL_WHOISSERVER_MSG, Client_ID( from ), Client_ID( c ), Client_ID( Client_Introducer( c )), Client_Info( Client_Introducer( c )))) return DISCONNECTED;
948 snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
949 cl2chan = Channel_FirstChannelOf( c );
952 chan = Channel_GetChannel( cl2chan );
953 assert( chan != NULL );
956 cl2chan = Channel_NextChannelOf( c, cl2chan );
958 /* Secret channel? */
959 if (strchr(Channel_Modes(chan), 's')
960 && !Channel_IsMemberOf(chan, Client))
963 /* Local channel and request is not from a user? */
964 if (Client_Type(Client) == CLIENT_SERVER
965 && Channel_IsLocal(chan))
968 /* Concatenate channel names */
969 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
970 if( strchr( Channel_UserModes( chan, c ), 'o' )) strlcat( str, "@", sizeof( str ));
971 else if( strchr( Channel_UserModes( chan, c ), 'v' )) strlcat( str, "+", sizeof( str ));
972 strlcat( str, Channel_Name( chan ), sizeof( str ));
974 if( strlen( str ) > ( LINE_LEN - CHANNEL_NAME_LEN - 4 ))
976 /* Line becomes too long: send it! */
977 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
978 snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
981 if( str[strlen( str ) - 1] != ':')
983 /* There is data left to send: */
984 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
988 if( Client_HasMode( c, 'o' ))
990 if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
993 /* Idle and signon time (local clients only!) */
994 if (Client_Conn(c) > NONE ) {
995 if (! IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
996 Client_ID(from), Client_ID(c),
997 (unsigned long)Conn_GetIdle(Client_Conn(c)),
998 (unsigned long)Conn_GetSignon(Client_Conn(c))))
1003 if( Client_HasMode( c, 'a' ))
1005 if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( c ), Client_Away( c ))) return DISCONNECTED;
1009 return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
1014 WHOWAS_EntryWrite(CLIENT *prefix, WHOWAS *entry)
1018 (void)strftime(t_str, sizeof(t_str), "%a %b %d %H:%M:%S %Y",
1019 localtime(&entry->time));
1021 if (!IRC_WriteStrClient(prefix, RPL_WHOWASUSER_MSG, Client_ID(prefix),
1022 entry->id, entry->user, entry->host, entry->info))
1023 return DISCONNECTED;
1025 return IRC_WriteStrClient(prefix, RPL_WHOISSERVER_MSG, Client_ID(prefix),
1026 entry->id, entry->server, t_str);
1030 * IRC "WHOWAS" function.
1031 * This function implements the IRC command "WHOWHAS". It handles local
1032 * requests and request that should be forwarded to other servers.
1035 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1037 CLIENT *target, *prefix;
1039 char tok_buf[COMMAND_LEN];
1040 int max, last, count, i, nc;
1043 assert( Client != NULL );
1044 assert( Req != NULL );
1046 /* Wrong number of parameters? */
1048 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1049 Client_ID(Client), Req->command);
1051 return IRC_WriteStrClient(Client, ERR_NONICKNAMEGIVEN_MSG, Client_ID(Client));
1055 target = Client_Search(Req->argv[2]);
1057 target = Client_ThisServer();
1060 if (Client_Type(Client) == CLIENT_SERVER)
1061 prefix = Client_Search(Req->prefix);
1066 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1067 Client_ID(Client), Req->prefix);
1069 /* Forward to other server? */
1070 if (target != Client_ThisServer()) {
1071 if (!target || (Client_Type(target) != CLIENT_SERVER))
1072 return IRC_WriteStrClient(prefix, ERR_NOSUCHSERVER_MSG,
1073 Client_ID(prefix), Req->argv[2]);
1076 IRC_WriteStrClientPrefix( target, prefix, "WHOWAS %s %s %s",
1077 Req->argv[0], Req->argv[1],
1082 whowas = Client_GetWhowas( );
1083 last = Client_GetLastWhowasIndex( );
1087 max = DEFAULT_WHOWAS;
1088 if (Req->argc > 1) {
1089 max = atoi(Req->argv[1]);
1095 * Break up the nick argument into a list of nicks, if applicable
1096 * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1098 strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1099 nick = strtok(tok_buf, ",");
1101 for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1105 if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1106 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1107 return DISCONNECTED;
1111 /* previous entry */
1114 /* "underflow", wrap around */
1118 if (nc && count >= max)
1120 } while (i != last);
1122 if (nc == 0 && !IRC_WriteStrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1123 Client_ID(prefix), nick))
1124 return DISCONNECTED;
1126 return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG, Client_ID(prefix), Req->argv[0]);
1131 IRC_Send_LUSERS( CLIENT *Client )
1138 assert( Client != NULL );
1140 /* Users, services and serevers in the network */
1141 if( ! IRC_WriteStrClient( Client, RPL_LUSERCLIENT_MSG, Client_ID( Client ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
1143 /* Number of IRC operators */
1144 cnt = Client_OperCount( );
1147 if( ! IRC_WriteStrClient( Client, RPL_LUSEROP_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
1150 /* Unknown connections */
1151 cnt = Client_UnknownCount( );
1154 if( ! IRC_WriteStrClient( Client, RPL_LUSERUNKNOWN_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
1157 /* Number of created channels */
1158 if( ! IRC_WriteStrClient( Client, RPL_LUSERCHANNELS_MSG, Client_ID( Client ), Channel_Count( ))) return DISCONNECTED;
1160 /* Number of local users, services and servers */
1161 if( ! IRC_WriteStrClient( Client, RPL_LUSERME_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
1164 /* Maximum number of local users */
1165 cnt = Client_MyUserCount();
1166 max = Client_MyMaxUserCount();
1167 if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1168 cnt, max, cnt, max))
1169 return DISCONNECTED;
1170 /* Maximum number of users in the network */
1171 cnt = Client_UserCount();
1172 max = Client_MaxUserCount();
1173 if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1174 cnt, max, cnt, max))
1175 return DISCONNECTED;
1179 } /* IRC_Send_LUSERS */
1183 Show_MOTD_Start(CLIENT *Client)
1185 return IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG,
1186 Client_ID( Client ), Client_ID( Client_ThisServer( )));
1190 Show_MOTD_Sendline(CLIENT *Client, const char *msg)
1192 return IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID( Client ), msg);
1196 Show_MOTD_End(CLIENT *Client)
1198 return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
1202 static bool Show_MOTD_SSLInfo(CLIENT *Client)
1205 char buf[COMMAND_LEN] = "Connected using Cipher ";
1207 if (!Conn_GetCipherInfo(Client_Conn(Client), buf + 23, sizeof buf - 23))
1210 if (!Show_MOTD_Sendline(Client, buf))
1216 static inline bool Show_MOTD_SSLInfo(UNUSED CLIENT *c) { return true; }
1220 IRC_Show_MOTD( CLIENT *Client )
1225 assert( Client != NULL );
1227 if (Conf_MotdPhrase[0]) {
1228 if (!Show_MOTD_Start(Client))
1229 return DISCONNECTED;
1230 if (!Show_MOTD_Sendline(Client, Conf_MotdPhrase))
1231 return DISCONNECTED;
1235 fd = fopen( Conf_MotdFile, "r" );
1237 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
1238 if (Conn_UsesSSL(Client_Conn(Client))) {
1239 if (!Show_MOTD_Start(Client))
1240 return DISCONNECTED;
1243 return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
1246 if (!Show_MOTD_Start( Client )) {
1251 while (fgets( line, (int)sizeof line, fd )) {
1252 ngt_TrimLastChr( line, '\n');
1254 if( ! Show_MOTD_Sendline( Client, line)) {
1261 if (!Show_MOTD_SSLInfo(Client))
1262 return DISCONNECTED;
1263 return Show_MOTD_End(Client);
1264 } /* IRC_Show_MOTD */
1268 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
1270 bool is_visible, is_member;
1271 char str[LINE_LEN + 1];
1275 assert( Client != NULL );
1276 assert( Chan != NULL );
1278 if( Channel_IsMemberOf( Chan, Client )) is_member = true;
1279 else is_member = false;
1281 /* Secret channel? */
1282 if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
1284 /* Alle Mitglieder suchen */
1285 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1286 cl2chan = Channel_FirstMember( Chan );
1289 cl = Channel_GetClient( cl2chan );
1291 if( strchr( Client_Modes( cl ), 'i' )) is_visible = false;
1292 else is_visible = true;
1294 if( is_member || is_visible )
1296 /* Nick anhaengen */
1297 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
1298 if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
1299 else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
1300 strlcat( str, Client_ID( cl ), sizeof( str ));
1302 if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
1304 /* Zeile wird zu lang: senden! */
1305 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1306 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
1310 /* naechstes Mitglied suchen */
1311 cl2chan = Channel_NextMember( Chan, cl2chan );
1313 if( str[strlen( str ) - 1] != ':')
1315 /* Es sind noch Daten da, die gesendet werden muessen */
1316 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
1320 } /* IRC_Send_NAMES */
1325 * Send the ISUPPORT numeric (005).
1326 * This numeric indicates the features that are supported by this server.
1327 * See <http://www.irc.org/tech_docs/005.html> for details.
1330 IRC_Send_ISUPPORT PARAMS((CLIENT * Client))
1332 if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1334 return DISCONNECTED;
1335 return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1336 CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1337 COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1339 } /* IRC_Send_ISUPPORT */