2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2013 Alexander Barton (alex@barton.de) and Contributors.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * Please read the file COPYING, README and AUTHORS for more information.
28 #include "conn-func.h"
41 #include "irc-macros.h"
42 #include "irc-write.h"
43 #include "client-cap.h"
52 t_diff(time_t *t, const time_t d)
60 return (unsigned int)diff;
64 uptime_days(time_t *now)
66 return t_diff(now, 60 * 60 * 24);
70 uptime_hrs(time_t *now)
72 return t_diff(now, 60 * 60);
76 uptime_mins(time_t *now)
78 return t_diff(now, 60);
82 write_whoreply(CLIENT *Client, CLIENT *c, const char *channelname, const char *flags)
84 return IRC_WriteStrClient(Client, RPL_WHOREPLY_MSG, Client_ID(Client),
85 channelname, Client_User(c),
86 Client_HostnameDisplayed(c),
87 Client_ID(Client_Introducer(c)), Client_ID(c),
88 flags, Client_Hops(c), Client_Info(c));
92 * Return channel user mode prefix(es).
94 * @param Client The client requesting the mode prefixes.
95 * @param chan_user_modes String with channel user modes.
96 * @param str String buffer to which the prefix(es) will be appended.
97 * @param len Size of "str" buffer.
98 * @return Pointer to "str".
101 who_flags_qualifier(CLIENT *Client, const char *chan_user_modes,
102 char *str, size_t len)
104 assert(Client != NULL);
106 if (Client_Cap(Client) & CLIENT_CAP_MULTI_PREFIX) {
107 if (strchr(chan_user_modes, 'q'))
108 strlcat(str, "~", len);
109 if (strchr(chan_user_modes, 'a'))
110 strlcat(str, "&", len);
111 if (strchr(chan_user_modes, 'o'))
112 strlcat(str, "@", len);
113 if (strchr(chan_user_modes, 'h'))
114 strlcat(str, "%", len);
115 if (strchr(chan_user_modes, 'v'))
116 strlcat(str, "+", len);
121 if (strchr(chan_user_modes, 'q'))
122 strlcat(str, "~", len);
123 else if (strchr(chan_user_modes, 'a'))
124 strlcat(str, "&", len);
125 else if (strchr(chan_user_modes, 'o'))
126 strlcat(str, "@", len);
127 else if (strchr(chan_user_modes, 'h'))
128 strlcat(str, "%", len);
129 else if (strchr(chan_user_modes, 'v'))
130 strlcat(str, "+", len);
136 * Send WHO reply for a "channel target" ("WHO #channel").
138 * @param Client Client requesting the information.
139 * @param Chan Channel being requested.
140 * @param OnlyOps Only display IRC operators.
141 * @return CONNECTED or DISCONNECTED.
144 IRC_WHO_Channel(CLIENT *Client, CHANNEL *Chan, bool OnlyOps)
146 bool is_visible, is_member, is_ircop;
152 assert( Client != NULL );
153 assert( Chan != NULL );
155 is_member = Channel_IsMemberOf(Chan, Client);
157 /* Secret channel? */
158 if (!is_member && Channel_HasMode(Chan, 's'))
159 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG,
160 Client_ID(Client), Channel_Name(Chan));
162 cl2chan = Channel_FirstMember(Chan);
163 for (; cl2chan ; cl2chan = Channel_NextMember(Chan, cl2chan)) {
164 c = Channel_GetClient(cl2chan);
166 is_ircop = Client_HasMode(c, 'o');
167 if (OnlyOps && !is_ircop)
170 is_visible = Client_HasMode(c, 'i');
171 if (is_member || is_visible) {
172 memset(flags, 0, sizeof(flags));
174 if (Client_HasMode(c, 'a'))
175 flags[0] = 'G'; /* away */
182 who_flags_qualifier(Client, Channel_UserModes(Chan, c),
183 flags, sizeof(flags));
185 if (!write_whoreply(Client, c, Channel_Name(Chan),
192 /* If there are a lot of clients, increase the penalty a bit */
193 if (count > MAX_RPL_WHO)
194 IRC_SetPenalty(Client, 1);
196 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client),
201 * Send WHO reply for a "mask target" ("WHO m*sk").
203 * @param Client Client requesting the information.
204 * @param Mask Mask being requested or NULL for "all" clients.
205 * @param OnlyOps Only display IRC operators.
206 * @return CONNECTED or DISCONNECTED.
209 IRC_WHO_Mask(CLIENT *Client, char *Mask, bool OnlyOps)
214 bool client_match, is_visible;
218 assert (Client != NULL);
223 IRC_SetPenalty(Client, 3);
224 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
225 if (Client_Type(c) != CLIENT_USER)
228 if (OnlyOps && !Client_HasMode(c, 'o'))
232 /* Match pattern against user host/server/name/nick */
233 client_match = MatchCaseInsensitive(Mask,
236 client_match = MatchCaseInsensitive(Mask,
237 Client_ID(Client_Introducer(c)));
239 client_match = MatchCaseInsensitive(Mask,
242 client_match = MatchCaseInsensitive(Mask,
245 continue; /* no match: skip this client */
248 is_visible = !Client_HasMode(c, 'i');
250 /* Target client is invisible, but mask matches exactly? */
251 if (!is_visible && Mask && strcasecmp(Client_ID(c), Mask) == 0)
254 /* Target still invisible, but are both on the same channel? */
256 cl2chan = Channel_FirstChannelOf(Client);
257 while (cl2chan && !is_visible) {
258 chan = Channel_GetChannel(cl2chan);
259 if (Channel_IsMemberOf(chan, c))
261 cl2chan = Channel_NextChannelOf(Client, cl2chan);
265 if (!is_visible) /* target user is not visible */
268 if (IRC_CheckListTooBig(Client, count, MAX_RPL_WHO, "WHO"))
271 memset(flags, 0, sizeof(flags));
273 if (Client_HasMode(c, 'a'))
274 flags[0] = 'G'; /* away */
278 if (Client_HasMode(c, 'o'))
281 if (!write_whoreply(Client, c, "*", flags))
286 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client),
291 * Generate WHOIS reply of one actual client.
293 * @param Client The client from which this command has been received.
294 * @param from The client requesting the information ("originator").
295 * @param c The client of which information should be returned.
296 * @return CONNECTED or DISCONNECTED.
299 IRC_WHOIS_SendReply(CLIENT *Client, CLIENT *from, CLIENT *c)
301 char str[COMMAND_LEN];
305 assert(Client != NULL);
306 assert(from != NULL);
309 /* Nick, user, hostname and client info */
310 if (!IRC_WriteStrClient(from, RPL_WHOISUSER_MSG, Client_ID(from),
311 Client_ID(c), Client_User(c),
312 Client_HostnameDisplayed(c), Client_Info(c)))
316 if (!IRC_WriteStrClient(from, RPL_WHOISSERVER_MSG, Client_ID(from),
317 Client_ID(c), Client_ID(Client_Introducer(c)),
318 Client_Info(Client_Introducer(c))))
322 snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
323 Client_ID(from), Client_ID(c));
324 cl2chan = Channel_FirstChannelOf(c);
326 chan = Channel_GetChannel(cl2chan);
327 assert(chan != NULL);
330 cl2chan = Channel_NextChannelOf(c, cl2chan);
332 /* Secret channel? */
333 if (Channel_HasMode(chan, 's')
334 && !Channel_IsMemberOf(chan, Client))
337 /* Local channel and request is not from a user? */
338 if (Client_Type(Client) == CLIENT_SERVER
339 && Channel_IsLocal(chan))
342 /* Concatenate channel names */
343 if (str[strlen(str) - 1] != ':')
344 strlcat(str, " ", sizeof(str));
346 who_flags_qualifier(Client, Channel_UserModes(chan, c),
348 strlcat(str, Channel_Name(chan), sizeof(str));
350 if (strlen(str) > (COMMAND_LEN - CHANNEL_NAME_LEN - 4)) {
351 /* Line becomes too long: send it! */
352 if (!IRC_WriteStrClient(Client, "%s", str))
354 snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
355 Client_ID(from), Client_ID(c));
358 if(str[strlen(str) - 1] != ':') {
359 /* There is data left to send: */
360 if (!IRC_WriteStrClient(Client, "%s", str))
365 if (Client_Type(c) == CLIENT_SERVICE &&
366 !IRC_WriteStrClient(from, RPL_WHOISSERVICE_MSG,
367 Client_ID(from), Client_ID(c)))
371 if (Client_Type(c) != CLIENT_SERVICE &&
372 Client_HasMode(c, 'o') &&
373 !IRC_WriteStrClient(from, RPL_WHOISOPERATOR_MSG,
374 Client_ID(from), Client_ID(c)))
378 if (Client_HasMode(c, 'B') &&
379 !IRC_WriteStrClient(from, RPL_WHOISBOT_MSG,
380 Client_ID(from), Client_ID(c)))
383 /* Connected using SSL? */
384 if (Conn_UsesSSL(Client_Conn(c))) {
385 if (!IRC_WriteStrClient(from, RPL_WHOISSSL_MSG, Client_ID(from),
389 /* Certificate fingerprint? */
390 if (Conn_GetCertFp(Client_Conn(c)) &&
392 !IRC_WriteStrClient(from, RPL_WHOISCERTFP_MSG,
393 Client_ID(from), Client_ID(c),
394 Conn_GetCertFp(Client_Conn(c))))
398 /* Registered nickname? */
399 if (Client_HasMode(c, 'R') &&
400 !IRC_WriteStrClient(from, RPL_WHOISREGNICK_MSG,
401 Client_ID(from), Client_ID(c)))
404 /* Account name metadata? */
405 if (Client_AccountName(c) &&
406 !IRC_WriteStrClient(from, RPL_WHOISLOGGEDIN_MSG,
407 Client_ID(from), Client_ID(c),
408 Client_AccountName(c)))
411 /* Local client and requester is the user itself or an IRC Op? */
412 if (Client_Conn(c) > NONE &&
413 (from == c || (!Conf_MorePrivacy && Client_HasMode(from, 'o')))) {
414 /* Client hostname */
415 if (!IRC_WriteStrClient(from, RPL_WHOISHOST_MSG,
416 Client_ID(from), Client_ID(c),
417 Client_Hostname(c), Client_IPAText(c)))
420 if (!IRC_WriteStrClient(from, RPL_WHOISMODES_MSG,
421 Client_ID(from), Client_ID(c), Client_Modes(c)))
425 /* Idle and signon time (local clients only!) */
426 if (!Conf_MorePrivacy && Client_Conn(c) > NONE &&
427 !IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
428 Client_ID(from), Client_ID(c),
429 (unsigned long)Conn_GetIdle(Client_Conn(c)),
430 (unsigned long)Conn_GetSignon(Client_Conn(c))))
434 if (Client_HasMode(c, 'a') &&
435 !IRC_WriteStrClient(from, RPL_AWAY_MSG,
436 Client_ID(from), Client_ID(c), Client_Away(c)))
443 WHOWAS_EntryWrite(CLIENT *prefix, WHOWAS *entry)
447 (void)strftime(t_str, sizeof(t_str), "%a %b %d %H:%M:%S %Y",
448 localtime(&entry->time));
450 if (!IRC_WriteStrClient(prefix, RPL_WHOWASUSER_MSG, Client_ID(prefix),
451 entry->id, entry->user, entry->host, entry->info))
454 return IRC_WriteStrClient(prefix, RPL_WHOISSERVER_MSG, Client_ID(prefix),
455 entry->id, entry->server, t_str);
460 Show_MOTD_SSLInfo(CLIENT *Client)
462 char buf[COMMAND_LEN];
465 if (Conn_GetCipherInfo(Client_Conn(Client), c_str, sizeof(c_str))) {
466 snprintf(buf, sizeof(buf), "Connected using Cipher %s", c_str);
467 if (!IRC_WriteStrClient(Client, RPL_MOTD_MSG,
468 Client_ID(Client), buf))
472 if (Conn_GetCertFp(Client_Conn(Client))) {
473 snprintf(buf, sizeof(buf),
474 "Your client certificate fingerprint is: %s",
475 Conn_GetCertFp(Client_Conn(Client)));
476 if (!IRC_WriteStrClient(Client, RPL_MOTD_MSG,
477 Client_ID(Client), buf))
485 Show_MOTD_SSLInfo(UNUSED CLIENT *c)
491 /* Global functions */
494 * Handler for the IRC command "ADMIN".
496 * @param Client The client from which this command has been received.
497 * @param Req Request structure with prefix and all parameters.
498 * @return CONNECTED or DISCONNECTED.
501 IRC_ADMIN(CLIENT *Client, REQUEST *Req )
503 CLIENT *target, *prefix;
505 assert( Client != NULL );
506 assert( Req != NULL );
508 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
509 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
512 if(target != Client_ThisServer()) {
513 IRC_WriteStrClientPrefix(target, prefix,
514 "ADMIN %s", Client_ID(target));
518 if (!IRC_WriteStrClient(Client, RPL_ADMINME_MSG, Client_ID(prefix),
521 if (!IRC_WriteStrClient(Client, RPL_ADMINLOC1_MSG, Client_ID(prefix),
524 if (!IRC_WriteStrClient(Client, RPL_ADMINLOC2_MSG, Client_ID(prefix),
527 if (!IRC_WriteStrClient(Client, RPL_ADMINEMAIL_MSG, Client_ID(prefix),
528 Conf_ServerAdminMail))
535 * Handler for the IRC command "INFO".
537 * @param Client The client from which this command has been received.
538 * @param Req Request structure with prefix and all parameters.
539 * @return CONNECTED or DISCONNECTED.
542 IRC_INFO(CLIENT * Client, REQUEST * Req)
544 CLIENT *target, *prefix;
547 assert(Client != NULL);
550 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
551 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
554 if (target != Client_ThisServer()) {
555 IRC_WriteStrClientPrefix(target, prefix, "INFO %s",
560 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix),
564 #if defined(__DATE__) && defined(__TIME__)
565 snprintf(msg, sizeof(msg), "Birth Date: %s at %s", __DATE__, __TIME__);
566 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
570 strlcpy(msg, "On-line since ", sizeof(msg));
571 strlcat(msg, NGIRCd_StartStr, sizeof(msg));
572 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
575 if (!IRC_WriteStrClient(Client, RPL_ENDOFINFO_MSG, Client_ID(prefix)))
582 * Handler for the IRC "ISON" command.
584 * @param Client The client from which this command has been received.
585 * @param Req Request structure with prefix and all parameters.
586 * @return CONNECTED or DISCONNECTED.
589 IRC_ISON( CLIENT *Client, REQUEST *Req )
591 char rpl[COMMAND_LEN];
596 assert(Client != NULL);
599 strlcpy(rpl, RPL_ISON_MSG, sizeof rpl);
600 for (i = 0; i < Req->argc; i++) {
601 /* "All" ircd even parse ":<x> <y> ..." arguments and split
602 * them up; so we do the same ... */
603 ptr = strtok(Req->argv[i], " ");
606 c = Client_Search(ptr);
607 if (c && Client_Type(c) == CLIENT_USER) {
608 strlcat(rpl, Client_ID(c), sizeof(rpl));
609 strlcat(rpl, " ", sizeof(rpl));
611 ptr = strtok(NULL, " ");
614 ngt_TrimLastChr(rpl, ' ');
616 return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
620 * Handler for the IRC "LINKS" command.
622 * @param Client The client from which this command has been received.
623 * @param Req Request structure with prefix and all parameters.
624 * @return CONNECTED or DISCONNECTED.
627 IRC_LINKS(CLIENT *Client, REQUEST *Req)
629 CLIENT *target, *from, *c;
632 assert(Client != NULL);
635 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
637 /* Get pointer to server mask or "*", if none given */
639 mask = Req->argv[Req->argc - 1];
644 if (Req->argc == 2) {
645 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
646 if (target != Client_ThisServer()) {
647 IRC_WriteStrClientPrefix(target, from,
648 "LINKS %s %s", Client_ID(target),
656 if (Client_Type(c) == CLIENT_SERVER
657 && MatchCaseInsensitive(mask, Client_ID(c))) {
658 if (!IRC_WriteStrClient(from, RPL_LINKS_MSG,
659 Client_ID(from), Client_ID(c),
660 Client_ID(Client_TopServer(c)
661 ? Client_TopServer(c)
662 : Client_ThisServer()),
663 Client_Hops(c), Client_Info(c)))
668 return IRC_WriteStrClient(from, RPL_ENDOFLINKS_MSG,
669 Client_ID(from), mask);
673 * Handler for the IRC "LUSERS" command.
675 * @param Client The client from which this command has been received.
676 * @param Req Request structure with prefix and all parameters.
677 * @return CONNECTED or DISCONNECTED.
680 IRC_LUSERS( CLIENT *Client, REQUEST *Req )
682 CLIENT *target, *from;
684 assert( Client != NULL );
685 assert( Req != NULL );
687 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
688 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
691 if (target != Client_ThisServer()) {
692 IRC_WriteStrClientPrefix(target, from,
693 "LUSERS %s %s", Req->argv[0],
698 return IRC_Send_LUSERS(from);
702 * Handler for the IRC command "SERVLIST".
704 * @param Client The client from which this command has been received.
705 * @param Req Request structure with prefix and all parameters.
706 * @return CONNECTED or DISCONNECTED.
709 IRC_SERVLIST(CLIENT *Client, REQUEST *Req)
713 assert(Client != NULL);
716 if (Req->argc < 2 || strcmp(Req->argv[1], "0") == 0) {
717 for (c = Client_First(); c!= NULL; c = Client_Next(c)) {
718 if (Client_Type(c) != CLIENT_SERVICE)
720 if (Req->argc > 0 && !MatchCaseInsensitive(Req->argv[0],
723 if (!IRC_WriteStrClient(Client, RPL_SERVLIST_MSG,
724 Client_ID(Client), Client_Mask(c),
725 Client_Mask(Client_Introducer(c)), "*",
726 0, Client_Hops(c), Client_Info(c)))
731 return IRC_WriteStrClient(Client, RPL_SERVLISTEND_MSG, Client_ID(Client),
732 Req->argc > 0 ? Req->argv[0] : "*",
733 Req->argc > 1 ? Req->argv[1] : "0");
737 * Handler for the IRC command "MOTD".
739 * @param Client The client from which this command has been received.
740 * @param Req Request structure with prefix and all parameters.
741 * @return CONNECTED or DISCONNECTED.
744 IRC_MOTD( CLIENT *Client, REQUEST *Req )
746 CLIENT *from, *target;
748 assert( Client != NULL );
749 assert( Req != NULL );
751 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
752 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
755 if (target != Client_ThisServer()) {
756 IRC_WriteStrClientPrefix(target, from, "MOTD %s",
761 return IRC_Show_MOTD(from);
765 * Handler for the IRC command "NAMES".
767 * @param Client The client from which this command has been received.
768 * @param Req Request structure with prefix and all parameters.
769 * @return CONNECTED or DISCONNECTED.
772 IRC_NAMES( CLIENT *Client, REQUEST *Req )
774 char rpl[COMMAND_LEN], *ptr;
775 CLIENT *target, *from, *c;
778 assert( Client != NULL );
779 assert( Req != NULL );
781 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
782 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
785 if (target != Client_ThisServer()) {
786 IRC_WriteStrClientPrefix(target, from, "NAMES %s :%s",
787 Req->argv[0], Client_ID(target));
792 /* Return NAMES list for specific channels */
793 ptr = strtok(Req->argv[0], ",");
795 chan = Channel_Search(ptr);
796 if (chan && !IRC_Send_NAMES(from, chan))
798 if (!IRC_WriteStrClient(from, RPL_ENDOFNAMES_MSG,
799 Client_ID(from), ptr))
801 ptr = strtok( NULL, "," );
806 chan = Channel_First();
808 if (!IRC_Send_NAMES(from, chan))
810 chan = Channel_Next(chan);
813 /* Now print all clients which are not in any channel */
815 snprintf(rpl, sizeof(rpl), RPL_NAMREPLY_MSG, Client_ID(from), "*", "*");
817 if (Client_Type(c) == CLIENT_USER
818 && Channel_FirstChannelOf(c) == NULL
819 && !Client_HasMode(c, 'i'))
821 /* its a user, concatenate ... */
822 if (rpl[strlen(rpl) - 1] != ':')
823 strlcat(rpl, " ", sizeof(rpl));
824 strlcat(rpl, Client_ID(c), sizeof(rpl));
826 if (strlen(rpl) > COMMAND_LEN - CLIENT_NICK_LEN - 4) {
827 /* Line is gwoing too long, send now */
828 if (!IRC_WriteStrClient(from, "%s", rpl))
830 snprintf(rpl, sizeof(rpl), RPL_NAMREPLY_MSG,
831 Client_ID(from), "*", "*");
836 if (rpl[strlen(rpl) - 1] != ':' && !IRC_WriteStrClient(from, "%s", rpl))
839 return IRC_WriteStrClient(from, RPL_ENDOFNAMES_MSG, Client_ID(from), "*");
843 * Handler for the IRC command "STATS".
845 * @param Client The client from which this command has been received.
846 * @param Req Request structure with prefix and all parameters.
847 * @return CONNECTED or DISCONNECTED.
850 IRC_STATS( CLIENT *Client, REQUEST *Req )
852 CLIENT *from, *target, *cl;
857 unsigned int days, hrs, mins;
858 struct list_head *list;
859 struct list_elem *list_item;
860 bool more_links = false;
862 assert(Client != NULL);
865 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
866 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
869 if (target != Client_ThisServer()) {
870 IRC_WriteStrClientPrefix(target, from, "STATS %s %s",
871 Req->argv[0], Client_ID(target));
876 query = Req->argv[0][0] ? Req->argv[0][0] : '*';
881 case 'g': /* Network-wide bans ("G-Lines") */
883 case 'k': /* Server-local bans ("K-Lines") */
885 if (!Client_HasMode(from, 'o'))
886 return IRC_WriteErrClient(from, ERR_NOPRIVILEGES_MSG,
888 if (query == 'g' || query == 'G')
889 list = Class_GetList(CLASS_GLINE);
891 list = Class_GetList(CLASS_KLINE);
892 list_item = Lists_GetFirst(list);
894 if (!IRC_WriteStrClient(from, RPL_STATSXLINE_MSG,
895 Client_ID(from), query,
896 Lists_GetMask(list_item),
897 Lists_GetValidity(list_item),
898 Lists_GetReason(list_item)))
900 list_item = Lists_GetNext(list_item);
903 case 'L': /* Link status (servers and user links) */
904 if (!Op_Check(from, Req))
905 return Op_NoPrivileges(from, Req);
908 case 'l': /* Link status (servers and own link) */
909 time_now = time(NULL);
910 for (con = Conn_First(); con != NONE; con = Conn_Next(con)) {
911 cl = Conn_GetClient(con);
914 if (Client_Type(cl) == CLIENT_SERVER ||
916 (more_links && Client_Type(cl) == CLIENT_USER)) {
918 if (Conn_Options(con) & CONN_ZIP) {
919 if (!IRC_WriteStrClient
920 (from, RPL_STATSLINKINFOZIP_MSG,
921 Client_ID(from), Client_Mask(cl),
922 Conn_SendQ(con), Conn_SendMsg(con),
928 (long)(time_now - Conn_StartTime(con))))
933 if (!IRC_WriteStrClient
934 (from, RPL_STATSLINKINFO_MSG,
935 Client_ID(from), Client_Mask(cl),
936 Conn_SendQ(con), Conn_SendMsg(con),
937 Conn_SendBytes(con), Conn_RecvMsg(con),
939 (long)(time_now - Conn_StartTime(con))))
944 case 'm': /* IRC command status (usage count) */
946 cmd = Parse_GetCommandStruct();
947 for (; cmd->name; cmd++) {
948 if (cmd->lcount == 0 && cmd->rcount == 0)
950 if (!IRC_WriteStrClient
951 (from, RPL_STATSCOMMANDS_MSG, Client_ID(from),
952 cmd->name, cmd->lcount, cmd->bytes, cmd->rcount))
956 case 'u': /* Server uptime */
958 time_now = time(NULL) - NGIRCd_Start;
959 days = uptime_days(&time_now);
960 hrs = uptime_hrs(&time_now);
961 mins = uptime_mins(&time_now);
962 if (!IRC_WriteStrClient(from, RPL_STATSUPTIME, Client_ID(from),
963 days, hrs, mins, (unsigned int)time_now))
968 return IRC_WriteStrClient(from, RPL_ENDOFSTATS_MSG,
969 Client_ID(from), query);
973 * Handler for the IRC command "SUMMON".
975 * @param Client The client from which this command has been received.
976 * @param Req Request structure with prefix and all parameters.
977 * @return CONNECTED or DISCONNECTED.
980 IRC_SUMMON(CLIENT * Client, UNUSED REQUEST * Req)
982 assert(Client != NULL);
984 return IRC_WriteErrClient(Client, ERR_SUMMONDISABLED_MSG,
989 * Handler for the IRC command "TIME".
991 * @param Client The client from which this command has been received.
992 * @param Req Request structure with prefix and all parameters.
993 * @return CONNECTED or DISCONNECTED.
996 IRC_TIME( CLIENT *Client, REQUEST *Req )
998 CLIENT *from, *target;
1002 assert(Client != NULL);
1003 assert(Req != NULL);
1005 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
1006 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
1009 if (target != Client_ThisServer()) {
1010 IRC_WriteStrClientPrefix(target, from, "TIME %s",
1016 (void)strftime(t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime(&t));
1017 return IRC_WriteStrClient(from, RPL_TIME_MSG, Client_ID(from),
1018 Client_ID(Client_ThisServer()), t_str);
1022 * Handler for the IRC command "USERHOST".
1024 * @param Client The client from which this command has been received.
1025 * @param Req Request structure with prefix and all parameters.
1026 * @return CONNECTED or DISCONNECTED.
1029 IRC_USERHOST(CLIENT *Client, REQUEST *Req)
1031 char rpl[COMMAND_LEN];
1035 assert(Client != NULL);
1036 assert(Req != NULL);
1043 strlcpy(rpl, RPL_USERHOST_MSG, sizeof rpl);
1044 for (i = 0; i < max; i++) {
1045 c = Client_Search(Req->argv[i]);
1046 if (c && (Client_Type(c) == CLIENT_USER)) {
1047 /* This Nick is "online" */
1048 strlcat(rpl, Client_ID(c), sizeof(rpl));
1049 if (Client_HasMode(c, 'o'))
1050 strlcat(rpl, "*", sizeof(rpl));
1051 strlcat(rpl, "=", sizeof(rpl));
1052 if (Client_HasMode(c, 'a'))
1053 strlcat(rpl, "-", sizeof(rpl));
1055 strlcat(rpl, "+", sizeof(rpl));
1056 strlcat(rpl, Client_User(c), sizeof(rpl));
1057 strlcat(rpl, "@", sizeof(rpl));
1058 strlcat(rpl, Client_HostnameDisplayed(c), sizeof(rpl));
1059 strlcat(rpl, " ", sizeof(rpl));
1062 ngt_TrimLastChr(rpl, ' ');
1064 return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
1065 } /* IRC_USERHOST */
1068 * Handler for the IRC command "USERS".
1070 * @param Client The client from which this command has been received.
1071 * @param Req Request structure with prefix and all parameters.
1072 * @return CONNECTED or DISCONNECTED.
1075 IRC_USERS(CLIENT * Client, UNUSED REQUEST * Req)
1077 assert(Client != NULL);
1079 return IRC_WriteErrClient(Client, ERR_USERSDISABLED_MSG,
1084 * Handler for the IRC command "VERSION".
1086 * @param Client The client from which this command has been received.
1087 * @param Req Request structure with prefix and all parameters.
1088 * @return CONNECTED or DISCONNECTED.
1091 IRC_VERSION( CLIENT *Client, REQUEST *Req )
1093 CLIENT *target, *prefix;
1095 assert( Client != NULL );
1096 assert( Req != NULL );
1098 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
1099 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
1102 if (target != Client_ThisServer()) {
1103 IRC_WriteStrClientPrefix(target, prefix, "VERSION %s",
1108 /* send version information */
1109 return IRC_WriteStrClient(Client, RPL_VERSION_MSG, Client_ID(prefix),
1110 PACKAGE_NAME, PACKAGE_VERSION,
1111 NGIRCd_DebugLevel, Conf_ServerName,
1112 NGIRCd_VersionAddition);
1116 * Handler for the IRC "WHO" command.
1118 * @param Client The client from which this command has been received.
1119 * @param Req Request structure with prefix and all parameters.
1120 * @return CONNECTED or DISCONNECTED.
1123 IRC_WHO(CLIENT *Client, REQUEST *Req)
1128 assert (Client != NULL);
1129 assert (Req != NULL);
1132 if (Req->argc == 2) {
1133 if (strcmp(Req->argv[1], "o") == 0)
1137 return IRC_WriteErrClient(Client,
1138 ERR_NEEDMOREPARAMS_MSG,
1145 if (Req->argc >= 1) {
1146 /* Channel or mask given */
1147 chan = Channel_Search(Req->argv[0]);
1149 /* Members of a channel have been requested */
1150 return IRC_WHO_Channel(Client, chan, only_ops);
1152 if (strcmp(Req->argv[0], "0") != 0) {
1153 /* A mask has been given. But please note this RFC
1154 * stupidity: "0" is same as no arguments ... */
1155 return IRC_WHO_Mask(Client, Req->argv[0], only_ops);
1159 /* No channel or (valid) mask given */
1160 return IRC_WHO_Mask(Client, NULL, only_ops);
1164 * Handler for the IRC "WHOIS" command.
1166 * @param Client The client from which this command has been received.
1167 * @param Req Request structure with prefix and all parameters.
1168 * @return CONNECTED or DISCONNECTED.
1171 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
1173 CLIENT *from, *target, *c;
1174 unsigned int match_count = 0, found = 0;
1175 bool has_wildcards, is_remote;
1176 bool got_wildcard = false;
1177 char mask[COMMAND_LEN], *query;
1179 assert( Client != NULL );
1180 assert( Req != NULL );
1182 /* Wrong number of parameters? */
1184 return IRC_WriteErrClient(Client, ERR_NONICKNAMEGIVEN_MSG,
1187 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
1188 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
1190 /* Get target server for this command */
1191 if (Req->argc > 1) {
1192 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, Client)
1194 target = Client_ThisServer();
1196 assert(target != NULL);
1199 if (target != Client_ThisServer()) {
1200 IRC_WriteStrClientPrefix(target, from, "WHOIS %s :%s",
1201 Req->argv[0], Req->argv[1]);
1205 is_remote = Client_Conn(from) < 0;
1206 strlcpy(mask, Req->argv[Req->argc - 1], sizeof(mask));
1207 for (query = strtok(ngt_LowerStr(mask), ",");
1209 query = strtok(NULL, ","), found++)
1211 has_wildcards = query[strcspn(query, "*?")] != 0;
1213 * follows ircd 2.10 implementation:
1214 * - handle up to 3 targets
1215 * - no wildcards for remote clients
1216 * - only one wildcard target per local client
1218 * Also, at most MAX_RPL_WHOIS matches are returned.
1220 if (!has_wildcards || is_remote) {
1221 c = Client_Search(query);
1222 if (c && (Client_Type(c) == CLIENT_USER
1223 || Client_Type(c) == CLIENT_SERVICE)) {
1224 if (!IRC_WHOIS_SendReply(Client, from, c))
1225 return DISCONNECTED;
1227 if (!IRC_WriteErrClient(Client,
1231 return DISCONNECTED;
1236 /* we already handled one wildcard query */
1237 if (!IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
1238 Client_ID(Client), query))
1239 return DISCONNECTED;
1242 got_wildcard = true;
1243 /* Increase penalty for wildcard queries */
1244 IRC_SetPenalty(Client, 3);
1246 for (c = Client_First(); c; c = Client_Next(c)) {
1247 if (IRC_CheckListTooBig(Client, match_count,
1248 MAX_RPL_WHOIS, "WHOIS"))
1251 if (Client_Type(c) != CLIENT_USER)
1253 if (!MatchCaseInsensitive(query, Client_ID(c)))
1255 if (!IRC_WHOIS_SendReply(Client, from, c))
1256 return DISCONNECTED;
1261 if (match_count == 0)
1262 IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
1264 Req->argv[Req->argc - 1]);
1266 return IRC_WriteStrClient(from, RPL_ENDOFWHOIS_MSG,
1267 Client_ID(from), Req->argv[Req->argc - 1]);
1271 * Handler for the IRC "WHOWAS" command.
1273 * @param Client The client from which this command has been received.
1274 * @param Req Request structure with prefix and all parameters.
1275 * @return CONNECTED or DISCONNECTED.
1278 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1280 CLIENT *target, *prefix;
1282 char tok_buf[COMMAND_LEN];
1283 int max, last, count, i, nc;
1286 assert( Client != NULL );
1287 assert( Req != NULL );
1289 /* Wrong number of parameters? */
1291 return IRC_WriteErrClient(Client, ERR_NONICKNAMEGIVEN_MSG,
1294 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 3)
1295 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
1296 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 2, prefix)
1298 /* Do not reveal any info on disconnected users? */
1299 if (Conf_MorePrivacy)
1303 if (target != Client_ThisServer()) {
1304 IRC_WriteStrClientPrefix(target, prefix, "WHOWAS %s %s %s",
1305 Req->argv[0], Req->argv[1],
1310 whowas = Client_GetWhowas( );
1311 last = Client_GetLastWhowasIndex( );
1315 max = DEF_RPL_WHOWAS;
1316 if (Req->argc > 1) {
1317 max = atoi(Req->argv[1]);
1319 max = MAX_RPL_WHOWAS;
1323 * Break up the nick argument into a list of nicks, if applicable
1324 * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1326 strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1327 nick = strtok(tok_buf, ",");
1329 for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1333 if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1334 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1335 return DISCONNECTED;
1339 /* previous entry */
1342 /* "underflow", wrap around */
1346 if (nc && count >= max)
1348 } while (i != last);
1350 if (nc == 0 && !IRC_WriteErrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1351 Client_ID(prefix), nick))
1352 return DISCONNECTED;
1354 return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG,
1355 Client_ID(prefix), Req->argv[0]);
1359 * Send LUSERS reply to a client.
1361 * @param Client The receipient of the information.
1362 * @return CONNECTED or DISCONNECTED.
1365 IRC_Send_LUSERS(CLIENT *Client)
1372 assert(Client != NULL);
1374 /* Users, services and servers in the network */
1375 if (!IRC_WriteStrClient(Client, RPL_LUSERCLIENT_MSG, Client_ID(Client),
1376 Client_UserCount(), Client_ServiceCount(),
1377 Client_ServerCount()))
1378 return DISCONNECTED;
1380 /* Number of IRC operators */
1381 cnt = Client_OperCount( );
1383 if (!IRC_WriteStrClient(Client, RPL_LUSEROP_MSG,
1384 Client_ID(Client), cnt))
1385 return DISCONNECTED;
1388 /* Unknown connections */
1389 cnt = Client_UnknownCount( );
1391 if (!IRC_WriteStrClient(Client, RPL_LUSERUNKNOWN_MSG,
1392 Client_ID(Client), cnt))
1393 return DISCONNECTED;
1396 /* Number of created channels */
1397 if (!IRC_WriteStrClient(Client, RPL_LUSERCHANNELS_MSG,
1399 Channel_CountVisible(Client)))
1400 return DISCONNECTED;
1402 /* Number of local users, services and servers */
1403 if (!IRC_WriteStrClient(Client, RPL_LUSERME_MSG, Client_ID(Client),
1404 Client_MyUserCount(), Client_MyServiceCount(),
1405 Client_MyServerCount()))
1406 return DISCONNECTED;
1409 /* Maximum number of local users */
1410 cnt = Client_MyUserCount();
1411 max = Client_MyMaxUserCount();
1412 if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1413 cnt, max, cnt, max))
1414 return DISCONNECTED;
1415 /* Maximum number of users in the network */
1416 cnt = Client_UserCount();
1417 max = Client_MaxUserCount();
1418 if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1419 cnt, max, cnt, max))
1420 return DISCONNECTED;
1421 /* Connection counters */
1422 if (! IRC_WriteStrClient(Client, RPL_STATSCONN_MSG, Client_ID(Client),
1423 Conn_CountMax(), Conn_CountAccepted()))
1424 return DISCONNECTED;
1428 } /* IRC_Send_LUSERS */
1431 IRC_Show_MOTD( CLIENT *Client )
1434 size_t len_tot, len_str;
1436 assert( Client != NULL );
1438 len_tot = array_bytes(&Conf_Motd);
1439 if (len_tot == 0 && !Conn_UsesSSL(Client_Conn(Client)))
1440 return IRC_WriteErrClient(Client, ERR_NOMOTD_MSG, Client_ID(Client));
1442 if (!IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG, Client_ID(Client),
1443 Client_ID(Client_ThisServer())))
1444 return DISCONNECTED;
1446 line = array_start(&Conf_Motd);
1447 while (len_tot > 0) {
1448 len_str = strlen(line) + 1;
1450 assert(len_tot >= len_str);
1453 if (!IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID(Client), line))
1454 return DISCONNECTED;
1458 if (!Show_MOTD_SSLInfo(Client))
1459 return DISCONNECTED;
1461 if (!IRC_WriteStrClient(Client, RPL_ENDOFMOTD_MSG, Client_ID(Client)))
1462 return DISCONNECTED;
1464 if (*Conf_CloakHost)
1465 return IRC_WriteStrClient(Client, RPL_HOSTHIDDEN_MSG,
1467 Client_Hostname(Client));
1470 } /* IRC_Show_MOTD */
1473 * Send NAMES reply for a specific client and channel.
1475 * @param Client The client requesting the NAMES information.
1476 * @param Chan The channel
1477 * @return CONNECTED or DISCONNECTED.
1480 IRC_Send_NAMES(CLIENT * Client, CHANNEL * Chan)
1482 bool is_visible, is_member;
1483 char str[COMMAND_LEN];
1487 assert(Client != NULL);
1488 assert(Chan != NULL);
1490 if (Channel_IsMemberOf(Chan, Client))
1495 /* Do not print info on channel memberships to anyone that is not member? */
1496 if (Conf_MorePrivacy && !is_member)
1499 /* Secret channel? */
1500 if (!is_member && Channel_HasMode(Chan, 's'))
1503 snprintf(str, sizeof(str), RPL_NAMREPLY_MSG, Client_ID(Client), "=",
1504 Channel_Name(Chan));
1505 cl2chan = Channel_FirstMember(Chan);
1507 cl = Channel_GetClient(cl2chan);
1509 if (Client_HasMode(cl, 'i'))
1514 if (is_member || is_visible) {
1515 if (str[strlen(str) - 1] != ':')
1516 strlcat(str, " ", sizeof(str));
1518 who_flags_qualifier(Client, Channel_UserModes(Chan, cl),
1520 strlcat(str, Client_ID(cl), sizeof(str));
1522 if (strlen(str) > (COMMAND_LEN - CLIENT_NICK_LEN - 4)) {
1523 if (!IRC_WriteStrClient(Client, "%s", str))
1524 return DISCONNECTED;
1525 snprintf(str, sizeof(str), RPL_NAMREPLY_MSG,
1526 Client_ID(Client), "=",
1527 Channel_Name(Chan));
1531 cl2chan = Channel_NextMember(Chan, cl2chan);
1533 if (str[strlen(str) - 1] != ':') {
1534 if (!IRC_WriteStrClient(Client, "%s", str))
1535 return DISCONNECTED;
1539 } /* IRC_Send_NAMES */
1542 * Send the ISUPPORT numeric (005).
1543 * This numeric indicates the features that are supported by this server.
1544 * See <http://www.irc.org/tech_docs/005.html> for details.
1547 IRC_Send_ISUPPORT(CLIENT * Client)
1549 if (Conf_Network[0] && !IRC_WriteStrClient(Client, RPL_ISUPPORTNET_MSG,
1552 return DISCONNECTED;
1553 if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1554 CHANTYPES, CHANTYPES, Conf_MaxJoins))
1555 return DISCONNECTED;
1556 return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1557 CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1558 COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1559 COMMAND_LEN - 113, MAX_HNDL_MODES_ARG,
1560 MAX_HNDL_CHANNEL_LISTS);
1561 } /* IRC_Send_ISUPPORT */