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 IRC_SetPenalty(Client, 1);
157 is_member = Channel_IsMemberOf(Chan, Client);
159 /* Secret channel? */
160 if (!is_member && Channel_HasMode(Chan, 's'))
161 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG,
162 Client_ID(Client), Channel_Name(Chan));
164 cl2chan = Channel_FirstMember(Chan);
165 for (; cl2chan ; cl2chan = Channel_NextMember(Chan, cl2chan)) {
166 c = Channel_GetClient(cl2chan);
168 is_ircop = Client_HasMode(c, 'o');
169 if (OnlyOps && !is_ircop)
172 is_visible = Client_HasMode(c, 'i');
173 if (is_member || is_visible) {
174 memset(flags, 0, sizeof(flags));
176 if (Client_HasMode(c, 'a'))
177 flags[0] = 'G'; /* away */
184 who_flags_qualifier(Client, Channel_UserModes(Chan, c),
185 flags, sizeof(flags));
187 if (!write_whoreply(Client, c, Channel_Name(Chan),
194 /* If there are a lot of clients, augment penalty a bit */
195 if (count > MAX_RPL_WHO)
196 IRC_SetPenalty(Client, 1);
198 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client),
203 * Send WHO reply for a "mask target" ("WHO m*sk").
205 * @param Client Client requesting the information.
206 * @param Mask Mask being requested or NULL for "all" clients.
207 * @param OnlyOps Only display IRC operators.
208 * @return CONNECTED or DISCONNECTED.
211 IRC_WHO_Mask(CLIENT *Client, char *Mask, bool OnlyOps)
216 bool client_match, is_visible;
220 assert (Client != NULL);
225 IRC_SetPenalty(Client, 3);
226 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
227 if (Client_Type(c) != CLIENT_USER)
230 if (OnlyOps && !Client_HasMode(c, 'o'))
234 /* Match pattern against user host/server/name/nick */
235 client_match = MatchCaseInsensitive(Mask,
238 client_match = MatchCaseInsensitive(Mask,
239 Client_ID(Client_Introducer(c)));
241 client_match = MatchCaseInsensitive(Mask,
244 client_match = MatchCaseInsensitive(Mask,
247 continue; /* no match: skip this client */
250 is_visible = !Client_HasMode(c, 'i');
252 /* Target client is invisible, but mask matches exactly? */
253 if (!is_visible && Mask && strcasecmp(Client_ID(c), Mask) == 0)
256 /* Target still invisible, but are both on the same channel? */
258 cl2chan = Channel_FirstChannelOf(Client);
259 while (cl2chan && !is_visible) {
260 chan = Channel_GetChannel(cl2chan);
261 if (Channel_IsMemberOf(chan, c))
263 cl2chan = Channel_NextChannelOf(Client, cl2chan);
267 if (!is_visible) /* target user is not visible */
270 if (IRC_CheckListTooBig(Client, count, MAX_RPL_WHO, "WHO"))
273 memset(flags, 0, sizeof(flags));
275 if (Client_HasMode(c, 'a'))
276 flags[0] = 'G'; /* away */
280 if (Client_HasMode(c, 'o'))
283 if (!write_whoreply(Client, c, "*", flags))
288 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client),
293 * Generate WHOIS reply of one actual client.
295 * @param Client The client from which this command has been received.
296 * @param from The client requesting the information ("originator").
297 * @param c The client of which information should be returned.
298 * @return CONNECTED or DISCONNECTED.
301 IRC_WHOIS_SendReply(CLIENT *Client, CLIENT *from, CLIENT *c)
303 char str[COMMAND_LEN];
307 assert(Client != NULL);
308 assert(from != NULL);
311 /* Nick, user, hostname and client info */
312 if (!IRC_WriteStrClient(from, RPL_WHOISUSER_MSG, Client_ID(from),
313 Client_ID(c), Client_User(c),
314 Client_HostnameDisplayed(c), Client_Info(c)))
318 if (!IRC_WriteStrClient(from, RPL_WHOISSERVER_MSG, Client_ID(from),
319 Client_ID(c), Client_ID(Client_Introducer(c)),
320 Client_Info(Client_Introducer(c))))
324 snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
325 Client_ID(from), Client_ID(c));
326 cl2chan = Channel_FirstChannelOf(c);
328 chan = Channel_GetChannel(cl2chan);
329 assert(chan != NULL);
332 cl2chan = Channel_NextChannelOf(c, cl2chan);
334 /* Secret channel? */
335 if (Channel_HasMode(chan, 's')
336 && !Channel_IsMemberOf(chan, Client))
339 /* Local channel and request is not from a user? */
340 if (Client_Type(Client) == CLIENT_SERVER
341 && Channel_IsLocal(chan))
344 /* Concatenate channel names */
345 if (str[strlen(str) - 1] != ':')
346 strlcat(str, " ", sizeof(str));
348 who_flags_qualifier(Client, Channel_UserModes(chan, c),
350 strlcat(str, Channel_Name(chan), sizeof(str));
352 if (strlen(str) > (COMMAND_LEN - CHANNEL_NAME_LEN - 4)) {
353 /* Line becomes too long: send it! */
354 if (!IRC_WriteStrClient(Client, "%s", str))
356 snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
357 Client_ID(from), Client_ID(c));
360 if(str[strlen(str) - 1] != ':') {
361 /* There is data left to send: */
362 if (!IRC_WriteStrClient(Client, "%s", str))
367 if (Client_Type(c) == CLIENT_SERVICE &&
368 !IRC_WriteStrClient(from, RPL_WHOISSERVICE_MSG,
369 Client_ID(from), Client_ID(c)))
373 if (Client_Type(c) != CLIENT_SERVICE &&
374 Client_HasMode(c, 'o') &&
375 !IRC_WriteStrClient(from, RPL_WHOISOPERATOR_MSG,
376 Client_ID(from), Client_ID(c)))
380 if (Client_HasMode(c, 'B') &&
381 !IRC_WriteStrClient(from, RPL_WHOISBOT_MSG,
382 Client_ID(from), Client_ID(c)))
385 /* Connected using SSL? */
386 if (Conn_UsesSSL(Client_Conn(c))) {
387 if (!IRC_WriteStrClient(from, RPL_WHOISSSL_MSG, Client_ID(from),
391 /* Certificate fingerprint? */
392 if (Conn_GetCertFp(Client_Conn(c)) &&
394 !IRC_WriteStrClient(from, RPL_WHOISCERTFP_MSG,
395 Client_ID(from), Client_ID(c),
396 Conn_GetCertFp(Client_Conn(c))))
400 /* Registered nickname? */
401 if (Client_HasMode(c, 'R') &&
402 !IRC_WriteStrClient(from, RPL_WHOISREGNICK_MSG,
403 Client_ID(from), Client_ID(c)))
406 /* Local client and requester is the user itself or an IRC Op? */
407 if (Client_Conn(c) > NONE &&
408 (from == c || (!Conf_MorePrivacy && Client_HasMode(from, 'o')))) {
409 /* Client hostname */
410 if (!IRC_WriteStrClient(from, RPL_WHOISHOST_MSG,
411 Client_ID(from), Client_ID(c),
412 Client_Hostname(c), Client_IPAText(c)))
415 if (!IRC_WriteStrClient(from, RPL_WHOISMODES_MSG,
416 Client_ID(from), Client_ID(c), Client_Modes(c)))
420 /* Idle and signon time (local clients only!) */
421 if (!Conf_MorePrivacy && Client_Conn(c) > NONE &&
422 !IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
423 Client_ID(from), Client_ID(c),
424 (unsigned long)Conn_GetIdle(Client_Conn(c)),
425 (unsigned long)Conn_GetSignon(Client_Conn(c))))
429 if (Client_HasMode(c, 'a') &&
430 !IRC_WriteStrClient(from, RPL_AWAY_MSG,
431 Client_ID(from), Client_ID(c), Client_Away(c)))
438 WHOWAS_EntryWrite(CLIENT *prefix, WHOWAS *entry)
442 (void)strftime(t_str, sizeof(t_str), "%a %b %d %H:%M:%S %Y",
443 localtime(&entry->time));
445 if (!IRC_WriteStrClient(prefix, RPL_WHOWASUSER_MSG, Client_ID(prefix),
446 entry->id, entry->user, entry->host, entry->info))
449 return IRC_WriteStrClient(prefix, RPL_WHOISSERVER_MSG, Client_ID(prefix),
450 entry->id, entry->server, t_str);
455 Show_MOTD_SSLInfo(CLIENT *Client)
457 char buf[COMMAND_LEN];
460 if (Conn_GetCipherInfo(Client_Conn(Client), c_str, sizeof(c_str))) {
461 snprintf(buf, sizeof(buf), "Connected using Cipher %s", c_str);
462 if (!IRC_WriteStrClient(Client, RPL_MOTD_MSG,
463 Client_ID(Client), buf))
467 if (Conn_GetCertFp(Client_Conn(Client))) {
468 snprintf(buf, sizeof(buf),
469 "Your client certificate fingerprint is: %s",
470 Conn_GetCertFp(Client_Conn(Client)));
471 if (!IRC_WriteStrClient(Client, RPL_MOTD_MSG,
472 Client_ID(Client), buf))
480 Show_MOTD_SSLInfo(UNUSED CLIENT *c)
486 /* Global functions */
489 * Handler for the IRC command "ADMIN".
491 * @param Client The client from which this command has been received.
492 * @param Req Request structure with prefix and all parameters.
493 * @return CONNECTED or DISCONNECTED.
496 IRC_ADMIN(CLIENT *Client, REQUEST *Req )
498 CLIENT *target, *prefix;
500 assert( Client != NULL );
501 assert( Req != NULL );
503 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
504 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
507 if(target != Client_ThisServer()) {
508 IRC_WriteStrClientPrefix(target, prefix,
509 "ADMIN %s", Client_ID(target));
513 if (!IRC_WriteStrClient(Client, RPL_ADMINME_MSG, Client_ID(prefix),
516 if (!IRC_WriteStrClient(Client, RPL_ADMINLOC1_MSG, Client_ID(prefix),
519 if (!IRC_WriteStrClient(Client, RPL_ADMINLOC2_MSG, Client_ID(prefix),
522 if (!IRC_WriteStrClient(Client, RPL_ADMINEMAIL_MSG, Client_ID(prefix),
523 Conf_ServerAdminMail))
530 * Handler for the IRC command "INFO".
532 * @param Client The client from which this command has been received.
533 * @param Req Request structure with prefix and all parameters.
534 * @return CONNECTED or DISCONNECTED.
537 IRC_INFO(CLIENT * Client, REQUEST * Req)
539 CLIENT *target, *prefix;
542 assert(Client != NULL);
545 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
546 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
549 if (target != Client_ThisServer()) {
550 IRC_WriteStrClientPrefix(target, prefix, "INFO %s",
555 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix),
559 #if defined(__DATE__) && defined(__TIME__)
560 snprintf(msg, sizeof(msg), "Birth Date: %s at %s", __DATE__, __TIME__);
561 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
565 strlcpy(msg, "On-line since ", sizeof(msg));
566 strlcat(msg, NGIRCd_StartStr, sizeof(msg));
567 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
570 if (!IRC_WriteStrClient(Client, RPL_ENDOFINFO_MSG, Client_ID(prefix)))
577 * Handler for the IRC "ISON" command.
579 * @param Client The client from which this command has been received.
580 * @param Req Request structure with prefix and all parameters.
581 * @return CONNECTED or DISCONNECTED.
584 IRC_ISON( CLIENT *Client, REQUEST *Req )
586 char rpl[COMMAND_LEN];
591 assert(Client != NULL);
594 strlcpy(rpl, RPL_ISON_MSG, sizeof rpl);
595 for (i = 0; i < Req->argc; i++) {
596 /* "All" ircd even parse ":<x> <y> ..." arguments and split
597 * them up; so we do the same ... */
598 ptr = strtok(Req->argv[i], " ");
601 c = Client_Search(ptr);
602 if (c && Client_Type(c) == CLIENT_USER) {
603 strlcat(rpl, Client_ID(c), sizeof(rpl));
604 strlcat(rpl, " ", sizeof(rpl));
606 ptr = strtok(NULL, " ");
609 ngt_TrimLastChr(rpl, ' ');
611 return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
615 * Handler for the IRC "LINKS" command.
617 * @param Client The client from which this command has been received.
618 * @param Req Request structure with prefix and all parameters.
619 * @return CONNECTED or DISCONNECTED.
622 IRC_LINKS(CLIENT *Client, REQUEST *Req)
624 CLIENT *target, *from, *c;
627 assert(Client != NULL);
630 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
632 /* Get pointer to server mask or "*", if none given */
634 mask = Req->argv[Req->argc - 1];
639 if (Req->argc == 2) {
640 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
641 if (target != Client_ThisServer()) {
642 IRC_WriteStrClientPrefix(target, from,
643 "LINKS %s %s", Client_ID(target),
651 if (Client_Type(c) == CLIENT_SERVER
652 && MatchCaseInsensitive(mask, Client_ID(c))) {
653 if (!IRC_WriteStrClient(from, RPL_LINKS_MSG,
654 Client_ID(from), Client_ID(c),
655 Client_ID(Client_TopServer(c)
656 ? Client_TopServer(c)
657 : Client_ThisServer()),
658 Client_Hops(c), Client_Info(c)))
663 return IRC_WriteStrClient(from, RPL_ENDOFLINKS_MSG,
664 Client_ID(from), mask);
668 * Handler for the IRC "LUSERS" command.
670 * @param Client The client from which this command has been received.
671 * @param Req Request structure with prefix and all parameters.
672 * @return CONNECTED or DISCONNECTED.
675 IRC_LUSERS( CLIENT *Client, REQUEST *Req )
677 CLIENT *target, *from;
679 assert( Client != NULL );
680 assert( Req != NULL );
682 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
683 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
686 if (target != Client_ThisServer()) {
687 IRC_WriteStrClientPrefix(target, from,
688 "LUSERS %s %s", Req->argv[0],
693 return IRC_Send_LUSERS(from);
697 * Handler for the IRC command "SERVLIST".
699 * @param Client The client from which this command has been received.
700 * @param Req Request structure with prefix and all parameters.
701 * @return CONNECTED or DISCONNECTED.
704 IRC_SERVLIST(CLIENT *Client, REQUEST *Req)
708 assert(Client != NULL);
711 if (Req->argc < 2 || strcmp(Req->argv[1], "0") == 0) {
712 for (c = Client_First(); c!= NULL; c = Client_Next(c)) {
713 if (Client_Type(c) != CLIENT_SERVICE)
715 if (Req->argc > 0 && !MatchCaseInsensitive(Req->argv[0],
718 if (!IRC_WriteStrClient(Client, RPL_SERVLIST_MSG,
719 Client_ID(Client), Client_Mask(c),
720 Client_Mask(Client_Introducer(c)), "*",
721 0, Client_Hops(c), Client_Info(c)))
726 return IRC_WriteStrClient(Client, RPL_SERVLISTEND_MSG, Client_ID(Client),
727 Req->argc > 0 ? Req->argv[0] : "*",
728 Req->argc > 1 ? Req->argv[1] : "0");
732 * Handler for the IRC command "MOTD".
734 * @param Client The client from which this command has been received.
735 * @param Req Request structure with prefix and all parameters.
736 * @return CONNECTED or DISCONNECTED.
739 IRC_MOTD( CLIENT *Client, REQUEST *Req )
741 CLIENT *from, *target;
743 assert( Client != NULL );
744 assert( Req != NULL );
746 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
747 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
750 if (target != Client_ThisServer()) {
751 IRC_WriteStrClientPrefix(target, from, "MOTD %s",
756 return IRC_Show_MOTD(from);
760 * Handler for the IRC command "NAMES".
762 * @param Client The client from which this command has been received.
763 * @param Req Request structure with prefix and all parameters.
764 * @return CONNECTED or DISCONNECTED.
767 IRC_NAMES( CLIENT *Client, REQUEST *Req )
769 char rpl[COMMAND_LEN], *ptr;
770 CLIENT *target, *from, *c;
773 assert( Client != NULL );
774 assert( Req != NULL );
776 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
777 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
780 if (target != Client_ThisServer()) {
781 IRC_WriteStrClientPrefix(target, from, "NAMES %s :%s",
782 Req->argv[0], Client_ID(target));
787 /* Return NAMES list for specific channels */
788 ptr = strtok(Req->argv[0], ",");
790 chan = Channel_Search(ptr);
791 if (chan && !IRC_Send_NAMES(from, chan))
793 if (!IRC_WriteStrClient(from, RPL_ENDOFNAMES_MSG,
794 Client_ID(from), ptr))
796 ptr = strtok( NULL, "," );
801 chan = Channel_First();
803 if (!IRC_Send_NAMES(from, chan))
805 chan = Channel_Next(chan);
808 /* Now print all clients which are not in any channel */
810 snprintf(rpl, sizeof(rpl), RPL_NAMREPLY_MSG, Client_ID(from), "*", "*");
812 if (Client_Type(c) == CLIENT_USER
813 && Channel_FirstChannelOf(c) == NULL
814 && !Client_HasMode(c, 'i'))
816 /* its a user, concatenate ... */
817 if (rpl[strlen(rpl) - 1] != ':')
818 strlcat(rpl, " ", sizeof(rpl));
819 strlcat(rpl, Client_ID(c), sizeof(rpl));
821 if (strlen(rpl) > COMMAND_LEN - CLIENT_NICK_LEN - 4) {
822 /* Line is gwoing too long, send now */
823 if (!IRC_WriteStrClient(from, "%s", rpl))
825 snprintf(rpl, sizeof(rpl), RPL_NAMREPLY_MSG,
826 Client_ID(from), "*", "*");
831 if (rpl[strlen(rpl) - 1] != ':' && !IRC_WriteStrClient(from, "%s", rpl))
834 return IRC_WriteStrClient(from, RPL_ENDOFNAMES_MSG, Client_ID(from), "*");
838 * Handler for the IRC command "STATS".
840 * @param Client The client from which this command has been received.
841 * @param Req Request structure with prefix and all parameters.
842 * @return CONNECTED or DISCONNECTED.
845 IRC_STATS( CLIENT *Client, REQUEST *Req )
847 CLIENT *from, *target, *cl;
852 unsigned int days, hrs, mins;
853 struct list_head *list;
854 struct list_elem *list_item;
855 bool more_links = false;
857 assert(Client != NULL);
860 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
861 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
864 if (target != Client_ThisServer()) {
865 IRC_WriteStrClientPrefix(target, from, "STATS %s %s",
866 Req->argv[0], Client_ID(target));
871 query = Req->argv[0][0] ? Req->argv[0][0] : '*';
876 case 'g': /* Network-wide bans ("G-Lines") */
878 case 'k': /* Server-local bans ("K-Lines") */
880 if (!Client_HasMode(from, 'o'))
881 return IRC_WriteErrClient(from, ERR_NOPRIVILEGES_MSG,
883 if (query == 'g' || query == 'G')
884 list = Class_GetList(CLASS_GLINE);
886 list = Class_GetList(CLASS_KLINE);
887 list_item = Lists_GetFirst(list);
889 if (!IRC_WriteStrClient(from, RPL_STATSXLINE_MSG,
890 Client_ID(from), query,
891 Lists_GetMask(list_item),
892 Lists_GetValidity(list_item),
893 Lists_GetReason(list_item)))
895 list_item = Lists_GetNext(list_item);
898 case 'L': /* Link status (servers and user links) */
899 if (!Op_Check(from, Req))
900 return Op_NoPrivileges(from, Req);
903 case 'l': /* Link status (servers and own link) */
904 time_now = time(NULL);
905 for (con = Conn_First(); con != NONE; con = Conn_Next(con)) {
906 cl = Conn_GetClient(con);
909 if (Client_Type(cl) == CLIENT_SERVER ||
911 (more_links && Client_Type(cl) == CLIENT_USER)) {
913 if (Conn_Options(con) & CONN_ZIP) {
914 if (!IRC_WriteStrClient
915 (from, RPL_STATSLINKINFOZIP_MSG,
916 Client_ID(from), Client_Mask(cl),
917 Conn_SendQ(con), Conn_SendMsg(con),
923 (long)(time_now - Conn_StartTime(con))))
928 if (!IRC_WriteStrClient
929 (from, RPL_STATSLINKINFO_MSG,
930 Client_ID(from), Client_Mask(cl),
931 Conn_SendQ(con), Conn_SendMsg(con),
932 Conn_SendBytes(con), Conn_RecvMsg(con),
934 (long)(time_now - Conn_StartTime(con))))
939 case 'm': /* IRC command status (usage count) */
941 cmd = Parse_GetCommandStruct();
942 for (; cmd->name; cmd++) {
943 if (cmd->lcount == 0 && cmd->rcount == 0)
945 if (!IRC_WriteStrClient
946 (from, RPL_STATSCOMMANDS_MSG, Client_ID(from),
947 cmd->name, cmd->lcount, cmd->bytes, cmd->rcount))
951 case 'u': /* Server uptime */
953 time_now = time(NULL) - NGIRCd_Start;
954 days = uptime_days(&time_now);
955 hrs = uptime_hrs(&time_now);
956 mins = uptime_mins(&time_now);
957 if (!IRC_WriteStrClient(from, RPL_STATSUPTIME, Client_ID(from),
958 days, hrs, mins, (unsigned int)time_now))
963 return IRC_WriteStrClient(from, RPL_ENDOFSTATS_MSG,
964 Client_ID(from), query);
968 * Handler for the IRC command "SUMMON".
970 * @param Client The client from which this command has been received.
971 * @param Req Request structure with prefix and all parameters.
972 * @return CONNECTED or DISCONNECTED.
975 IRC_SUMMON(CLIENT * Client, UNUSED REQUEST * Req)
977 assert(Client != NULL);
979 return IRC_WriteErrClient(Client, ERR_SUMMONDISABLED_MSG,
984 * Handler for the IRC command "TIME".
986 * @param Client The client from which this command has been received.
987 * @param Req Request structure with prefix and all parameters.
988 * @return CONNECTED or DISCONNECTED.
991 IRC_TIME( CLIENT *Client, REQUEST *Req )
993 CLIENT *from, *target;
997 assert(Client != NULL);
1000 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
1001 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
1004 if (target != Client_ThisServer()) {
1005 IRC_WriteStrClientPrefix(target, from, "TIME %s",
1011 (void)strftime(t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime(&t));
1012 return IRC_WriteStrClient(from, RPL_TIME_MSG, Client_ID(from),
1013 Client_ID(Client_ThisServer()), t_str);
1017 * Handler for the IRC command "USERHOST".
1019 * @param Client The client from which this command has been received.
1020 * @param Req Request structure with prefix and all parameters.
1021 * @return CONNECTED or DISCONNECTED.
1024 IRC_USERHOST(CLIENT *Client, REQUEST *Req)
1026 char rpl[COMMAND_LEN];
1030 assert(Client != NULL);
1031 assert(Req != NULL);
1038 strlcpy(rpl, RPL_USERHOST_MSG, sizeof rpl);
1039 for (i = 0; i < max; i++) {
1040 c = Client_Search(Req->argv[i]);
1041 if (c && (Client_Type(c) == CLIENT_USER)) {
1042 /* This Nick is "online" */
1043 strlcat(rpl, Client_ID(c), sizeof(rpl));
1044 if (Client_HasMode(c, 'o'))
1045 strlcat(rpl, "*", sizeof(rpl));
1046 strlcat(rpl, "=", sizeof(rpl));
1047 if (Client_HasMode(c, 'a'))
1048 strlcat(rpl, "-", sizeof(rpl));
1050 strlcat(rpl, "+", sizeof(rpl));
1051 strlcat(rpl, Client_User(c), sizeof(rpl));
1052 strlcat(rpl, "@", sizeof(rpl));
1053 strlcat(rpl, Client_HostnameDisplayed(c), sizeof(rpl));
1054 strlcat(rpl, " ", sizeof(rpl));
1057 ngt_TrimLastChr(rpl, ' ');
1059 return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
1060 } /* IRC_USERHOST */
1063 * Handler for the IRC command "USERS".
1065 * @param Client The client from which this command has been received.
1066 * @param Req Request structure with prefix and all parameters.
1067 * @return CONNECTED or DISCONNECTED.
1070 IRC_USERS(CLIENT * Client, UNUSED REQUEST * Req)
1072 assert(Client != NULL);
1074 return IRC_WriteErrClient(Client, ERR_USERSDISABLED_MSG,
1079 * Handler for the IRC command "VERSION".
1081 * @param Client The client from which this command has been received.
1082 * @param Req Request structure with prefix and all parameters.
1083 * @return CONNECTED or DISCONNECTED.
1086 IRC_VERSION( CLIENT *Client, REQUEST *Req )
1088 CLIENT *target, *prefix;
1090 assert( Client != NULL );
1091 assert( Req != NULL );
1093 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
1094 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
1097 if (target != Client_ThisServer()) {
1098 IRC_WriteStrClientPrefix(target, prefix, "VERSION %s",
1103 /* send version information */
1104 return IRC_WriteStrClient(Client, RPL_VERSION_MSG, Client_ID(prefix),
1105 PACKAGE_NAME, PACKAGE_VERSION,
1106 NGIRCd_DebugLevel, Conf_ServerName,
1107 NGIRCd_VersionAddition);
1111 * Handler for the IRC "WHO" command.
1113 * @param Client The client from which this command has been received.
1114 * @param Req Request structure with prefix and all parameters.
1115 * @return CONNECTED or DISCONNECTED.
1118 IRC_WHO(CLIENT *Client, REQUEST *Req)
1123 assert (Client != NULL);
1124 assert (Req != NULL);
1127 if (Req->argc == 2) {
1128 if (strcmp(Req->argv[1], "o") == 0)
1132 return IRC_WriteErrClient(Client,
1133 ERR_NEEDMOREPARAMS_MSG,
1140 if (Req->argc >= 1) {
1141 /* Channel or mask given */
1142 chan = Channel_Search(Req->argv[0]);
1144 /* Members of a channel have been requested */
1145 return IRC_WHO_Channel(Client, chan, only_ops);
1147 if (strcmp(Req->argv[0], "0") != 0) {
1148 /* A mask has been given. But please note this RFC
1149 * stupidity: "0" is same as no arguments ... */
1150 return IRC_WHO_Mask(Client, Req->argv[0], only_ops);
1154 /* No channel or (valid) mask given */
1155 IRC_SetPenalty(Client, 2);
1156 return IRC_WHO_Mask(Client, NULL, only_ops);
1160 * Handler for the IRC "WHOIS" command.
1162 * @param Client The client from which this command has been received.
1163 * @param Req Request structure with prefix and all parameters.
1164 * @return CONNECTED or DISCONNECTED.
1167 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
1169 CLIENT *from, *target, *c;
1170 unsigned int match_count = 0, found = 0;
1171 bool has_wildcards, is_remote;
1172 bool got_wildcard = false;
1173 char mask[COMMAND_LEN], *query;
1175 assert( Client != NULL );
1176 assert( Req != NULL );
1178 IRC_SetPenalty(Client, 1);
1180 /* Wrong number of parameters? */
1182 return IRC_WriteErrClient(Client, ERR_NONICKNAMEGIVEN_MSG,
1185 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
1186 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
1188 /* Get target server for this command */
1189 if (Req->argc > 1) {
1190 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, Client)
1192 target = Client_ThisServer();
1194 assert(target != NULL);
1197 if (target != Client_ThisServer()) {
1198 IRC_WriteStrClientPrefix(target, from, "WHOIS %s :%s",
1199 Req->argv[0], Req->argv[1]);
1203 is_remote = Client_Conn(from) < 0;
1204 strlcpy(mask, Req->argv[Req->argc - 1], sizeof(mask));
1205 for (query = strtok(ngt_LowerStr(mask), ",");
1207 query = strtok(NULL, ","), found++)
1209 has_wildcards = query[strcspn(query, "*?")] != 0;
1211 * follows ircd 2.10 implementation:
1212 * - handle up to 3 targets
1213 * - no wildcards for remote clients
1214 * - only one wildcard target per local client
1216 * Also, at most MAX_RPL_WHOIS matches are returned.
1218 if (!has_wildcards || is_remote) {
1219 c = Client_Search(query);
1220 if (c && (Client_Type(c) == CLIENT_USER
1221 || Client_Type(c) == CLIENT_SERVICE)) {
1222 if (!IRC_WHOIS_SendReply(Client, from, c))
1223 return DISCONNECTED;
1225 if (!IRC_WriteErrClient(Client,
1229 return DISCONNECTED;
1234 /* we already handled one wildcard query */
1235 if (!IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
1236 Client_ID(Client), query))
1237 return DISCONNECTED;
1240 got_wildcard = true;
1241 IRC_SetPenalty(Client, 3);
1243 for (c = Client_First(); c; c = Client_Next(c)) {
1244 if (IRC_CheckListTooBig(Client, match_count,
1245 MAX_RPL_WHOIS, "WHOIS"))
1248 if (Client_Type(c) != CLIENT_USER)
1250 if (!MatchCaseInsensitive(query, Client_ID(c)))
1252 if (!IRC_WHOIS_SendReply(Client, from, c))
1253 return DISCONNECTED;
1258 if (match_count == 0)
1259 IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
1261 Req->argv[Req->argc - 1]);
1263 return IRC_WriteStrClient(from, RPL_ENDOFWHOIS_MSG,
1264 Client_ID(from), Req->argv[Req->argc - 1]);
1268 * Handler for the IRC "WHOWAS" command.
1270 * @param Client The client from which this command has been received.
1271 * @param Req Request structure with prefix and all parameters.
1272 * @return CONNECTED or DISCONNECTED.
1275 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1277 CLIENT *target, *prefix;
1279 char tok_buf[COMMAND_LEN];
1280 int max, last, count, i, nc;
1283 assert( Client != NULL );
1284 assert( Req != NULL );
1286 /* Wrong number of parameters? */
1288 return IRC_WriteErrClient(Client, ERR_NONICKNAMEGIVEN_MSG,
1291 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 3)
1292 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
1293 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 2, prefix)
1295 /* Do not reveal any info on disconnected users? */
1296 if (Conf_MorePrivacy)
1300 if (target != Client_ThisServer()) {
1301 IRC_WriteStrClientPrefix(target, prefix, "WHOWAS %s %s %s",
1302 Req->argv[0], Req->argv[1],
1307 whowas = Client_GetWhowas( );
1308 last = Client_GetLastWhowasIndex( );
1312 max = DEF_RPL_WHOWAS;
1313 if (Req->argc > 1) {
1314 max = atoi(Req->argv[1]);
1316 max = MAX_RPL_WHOWAS;
1320 * Break up the nick argument into a list of nicks, if applicable
1321 * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1323 strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1324 nick = strtok(tok_buf, ",");
1326 for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1330 if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1331 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1332 return DISCONNECTED;
1336 /* previous entry */
1339 /* "underflow", wrap around */
1343 if (nc && count >= max)
1345 } while (i != last);
1347 if (nc == 0 && !IRC_WriteErrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1348 Client_ID(prefix), nick))
1349 return DISCONNECTED;
1351 return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG,
1352 Client_ID(prefix), Req->argv[0]);
1356 * Send LUSERS reply to a client.
1358 * @param Client The receipient of the information.
1359 * @return CONNECTED or DISCONNECTED.
1362 IRC_Send_LUSERS(CLIENT *Client)
1369 assert(Client != NULL);
1371 /* Users, services and servers in the network */
1372 if (!IRC_WriteStrClient(Client, RPL_LUSERCLIENT_MSG, Client_ID(Client),
1373 Client_UserCount(), Client_ServiceCount(),
1374 Client_ServerCount()))
1375 return DISCONNECTED;
1377 /* Number of IRC operators */
1378 cnt = Client_OperCount( );
1380 if (!IRC_WriteStrClient(Client, RPL_LUSEROP_MSG,
1381 Client_ID(Client), cnt))
1382 return DISCONNECTED;
1385 /* Unknown connections */
1386 cnt = Client_UnknownCount( );
1388 if (!IRC_WriteStrClient(Client, RPL_LUSERUNKNOWN_MSG,
1389 Client_ID(Client), cnt))
1390 return DISCONNECTED;
1393 /* Number of created channels */
1394 if (!IRC_WriteStrClient(Client, RPL_LUSERCHANNELS_MSG,
1396 Channel_CountVisible(Client)))
1397 return DISCONNECTED;
1399 /* Number of local users, services and servers */
1400 if (!IRC_WriteStrClient(Client, RPL_LUSERME_MSG, Client_ID(Client),
1401 Client_MyUserCount(), Client_MyServiceCount(),
1402 Client_MyServerCount()))
1403 return DISCONNECTED;
1406 /* Maximum number of local users */
1407 cnt = Client_MyUserCount();
1408 max = Client_MyMaxUserCount();
1409 if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1410 cnt, max, cnt, max))
1411 return DISCONNECTED;
1412 /* Maximum number of users in the network */
1413 cnt = Client_UserCount();
1414 max = Client_MaxUserCount();
1415 if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1416 cnt, max, cnt, max))
1417 return DISCONNECTED;
1418 /* Connection counters */
1419 if (! IRC_WriteStrClient(Client, RPL_STATSCONN_MSG, Client_ID(Client),
1420 Conn_CountMax(), Conn_CountAccepted()))
1421 return DISCONNECTED;
1425 } /* IRC_Send_LUSERS */
1428 IRC_Show_MOTD( CLIENT *Client )
1431 size_t len_tot, len_str;
1433 assert( Client != NULL );
1435 len_tot = array_bytes(&Conf_Motd);
1436 if (len_tot == 0 && !Conn_UsesSSL(Client_Conn(Client)))
1437 return IRC_WriteErrClient(Client, ERR_NOMOTD_MSG, Client_ID(Client));
1439 if (!IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG, Client_ID(Client),
1440 Client_ID(Client_ThisServer())))
1441 return DISCONNECTED;
1443 line = array_start(&Conf_Motd);
1444 while (len_tot > 0) {
1445 len_str = strlen(line) + 1;
1447 assert(len_tot >= len_str);
1450 if (!IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID(Client), line))
1451 return DISCONNECTED;
1455 if (!Show_MOTD_SSLInfo(Client))
1456 return DISCONNECTED;
1458 if (!IRC_WriteStrClient(Client, RPL_ENDOFMOTD_MSG, Client_ID(Client)))
1459 return DISCONNECTED;
1461 if (*Conf_CloakHost)
1462 return IRC_WriteStrClient(Client, RPL_HOSTHIDDEN_MSG,
1464 Client_Hostname(Client));
1467 } /* IRC_Show_MOTD */
1470 * Send NAMES reply for a specific client and channel.
1472 * @param Client The client requesting the NAMES information.
1473 * @param Chan The channel
1474 * @return CONNECTED or DISCONNECTED.
1477 IRC_Send_NAMES(CLIENT * Client, CHANNEL * Chan)
1479 bool is_visible, is_member;
1480 char str[COMMAND_LEN];
1484 assert(Client != NULL);
1485 assert(Chan != NULL);
1487 if (Channel_IsMemberOf(Chan, Client))
1492 /* Do not print info on channel memberships to anyone that is not member? */
1493 if (Conf_MorePrivacy && !is_member)
1496 /* Secret channel? */
1497 if (!is_member && Channel_HasMode(Chan, 's'))
1500 snprintf(str, sizeof(str), RPL_NAMREPLY_MSG, Client_ID(Client), "=",
1501 Channel_Name(Chan));
1502 cl2chan = Channel_FirstMember(Chan);
1504 cl = Channel_GetClient(cl2chan);
1506 if (Client_HasMode(cl, 'i'))
1511 if (is_member || is_visible) {
1512 if (str[strlen(str) - 1] != ':')
1513 strlcat(str, " ", sizeof(str));
1515 who_flags_qualifier(Client, Channel_UserModes(Chan, cl),
1517 strlcat(str, Client_ID(cl), sizeof(str));
1519 if (strlen(str) > (COMMAND_LEN - CLIENT_NICK_LEN - 4)) {
1520 if (!IRC_WriteStrClient(Client, "%s", str))
1521 return DISCONNECTED;
1522 snprintf(str, sizeof(str), RPL_NAMREPLY_MSG,
1523 Client_ID(Client), "=",
1524 Channel_Name(Chan));
1528 cl2chan = Channel_NextMember(Chan, cl2chan);
1530 if (str[strlen(str) - 1] != ':') {
1531 if (!IRC_WriteStrClient(Client, "%s", str))
1532 return DISCONNECTED;
1536 } /* IRC_Send_NAMES */
1539 * Send the ISUPPORT numeric (005).
1540 * This numeric indicates the features that are supported by this server.
1541 * See <http://www.irc.org/tech_docs/005.html> for details.
1544 IRC_Send_ISUPPORT(CLIENT * Client)
1546 if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1547 CHANTYPES, CHANTYPES, Conf_MaxJoins))
1548 return DISCONNECTED;
1549 return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1550 CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1551 COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1552 COMMAND_LEN - 113, MAX_HNDL_MODES_ARG,
1553 MAX_HNDL_CHANNEL_LISTS);
1554 } /* IRC_Send_ISUPPORT */