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"
51 t_diff(time_t *t, const time_t d)
59 return (unsigned int)diff;
63 uptime_days(time_t *now)
65 return t_diff(now, 60 * 60 * 24);
69 uptime_hrs(time_t *now)
71 return t_diff(now, 60 * 60);
75 uptime_mins(time_t *now)
77 return t_diff(now, 60);
81 write_whoreply(CLIENT *Client, CLIENT *c, const char *channelname, const char *flags)
83 return IRC_WriteStrClient(Client, RPL_WHOREPLY_MSG, Client_ID(Client),
84 channelname, Client_User(c),
85 Client_HostnameDisplayed(c),
86 Client_ID(Client_Introducer(c)), Client_ID(c),
87 flags, Client_Hops(c), Client_Info(c));
91 who_flags_status(const char *client_modes)
93 if (strchr(client_modes, 'a'))
94 return "G"; /* away */
99 * Return channel user mode prefix(es).
101 * @param Client The client requesting the mode prefixes.
102 * @param chan_user_modes String with channel user modes.
103 * @param str String buffer to which the prefix(es) will be appended.
104 * @param len Size of "str" buffer.
105 * @return Pointer to "str".
108 who_flags_qualifier(CLIENT *Client, const char *chan_user_modes,
109 char *str, size_t len)
111 assert(Client != NULL);
113 if (Client_Cap(Client) & CLIENT_CAP_MULTI_PREFIX) {
114 if (strchr(chan_user_modes, 'q'))
115 strlcat(str, "~", len);
116 if (strchr(chan_user_modes, 'a'))
117 strlcat(str, "&", len);
118 if (strchr(chan_user_modes, 'o'))
119 strlcat(str, "@", len);
120 if (strchr(chan_user_modes, 'h'))
121 strlcat(str, "%", len);
122 if (strchr(chan_user_modes, 'v'))
123 strlcat(str, "+", len);
128 if (strchr(chan_user_modes, 'q'))
129 strlcat(str, "~", len);
130 else if (strchr(chan_user_modes, 'a'))
131 strlcat(str, "&", len);
132 else if (strchr(chan_user_modes, 'o'))
133 strlcat(str, "@", len);
134 else if (strchr(chan_user_modes, 'h'))
135 strlcat(str, "%", len);
136 else if (strchr(chan_user_modes, 'v'))
137 strlcat(str, "+", len);
143 * Send WHO reply for a "channel target" ("WHO #channel").
145 * @param Client Client requesting the information.
146 * @param Chan Channel being requested.
147 * @param OnlyOps Only display IRC operators.
148 * @return CONNECTED or DISCONNECTED.
151 IRC_WHO_Channel(CLIENT *Client, CHANNEL *Chan, bool OnlyOps)
153 bool is_visible, is_member, is_ircop;
155 const char *client_modes;
160 assert( Client != NULL );
161 assert( Chan != NULL );
163 IRC_SetPenalty(Client, 1);
165 is_member = Channel_IsMemberOf(Chan, Client);
167 /* Secret channel? */
168 if (!is_member && strchr(Channel_Modes(Chan), 's'))
169 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG,
170 Client_ID(Client), Channel_Name(Chan));
172 cl2chan = Channel_FirstMember(Chan);
173 for (; cl2chan ; cl2chan = Channel_NextMember(Chan, cl2chan)) {
174 c = Channel_GetClient(cl2chan);
176 client_modes = Client_Modes(c);
177 is_ircop = strchr(client_modes, 'o') != NULL;
178 if (OnlyOps && !is_ircop)
181 is_visible = strchr(client_modes, 'i') == NULL;
182 if (is_member || is_visible) {
183 strlcpy(flags, who_flags_status(client_modes),
186 strlcat(flags, "*", sizeof(flags));
188 who_flags_qualifier(Client, Channel_UserModes(Chan, c),
189 flags, sizeof(flags));
191 if (!write_whoreply(Client, c, Channel_Name(Chan),
198 /* If there are a lot of clients, augment penalty a bit */
199 if (count > MAX_RPL_WHO)
200 IRC_SetPenalty(Client, 1);
202 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client),
207 * Send WHO reply for a "mask target" ("WHO m*sk").
209 * @param Client Client requesting the information.
210 * @param Mask Mask being requested or NULL for "all" clients.
211 * @param OnlyOps Only display IRC operators.
212 * @return CONNECTED or DISCONNECTED.
215 IRC_WHO_Mask(CLIENT *Client, char *Mask, bool OnlyOps)
220 bool client_match, is_visible;
224 assert (Client != NULL);
229 IRC_SetPenalty(Client, 3);
230 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
231 if (Client_Type(c) != CLIENT_USER)
234 if (OnlyOps && !Client_HasMode(c, 'o'))
238 /* Match pattern against user host/server/name/nick */
239 client_match = MatchCaseInsensitive(Mask,
242 client_match = MatchCaseInsensitive(Mask,
243 Client_ID(Client_Introducer(c)));
245 client_match = MatchCaseInsensitive(Mask,
248 client_match = MatchCaseInsensitive(Mask,
251 continue; /* no match: skip this client */
254 is_visible = !Client_HasMode(c, 'i');
256 /* Target client is invisible, but mask matches exactly? */
257 if (!is_visible && Mask && strcasecmp(Client_ID(c), Mask) == 0)
260 /* Target still invisible, but are both on the same channel? */
262 cl2chan = Channel_FirstChannelOf(Client);
263 while (cl2chan && !is_visible) {
264 chan = Channel_GetChannel(cl2chan);
265 if (Channel_IsMemberOf(chan, c))
267 cl2chan = Channel_NextChannelOf(Client, cl2chan);
271 if (!is_visible) /* target user is not visible */
274 if (IRC_CheckListTooBig(Client, count, MAX_RPL_WHO, "WHO"))
277 strlcpy(flags, who_flags_status(Client_Modes(c)), sizeof(flags));
278 if (strchr(Client_Modes(c), 'o'))
279 strlcat(flags, "*", sizeof(flags));
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 (strchr(Channel_Modes(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_GetFingerprint(Client_Conn(c)) &&
392 !IRC_WriteStrClient(from, RPL_WHOISCERTFP_MSG,
393 Client_ID(from), Client_ID(c),
394 Conn_GetFingerprint(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 /* Local client and requester is the user itself or an IRC Op? */
405 if (Client_Conn(c) > NONE &&
406 (from == c || (!Conf_MorePrivacy && Client_HasMode(from, 'o')))) {
407 /* Client hostname */
408 if (!IRC_WriteStrClient(from, RPL_WHOISHOST_MSG,
409 Client_ID(from), Client_ID(c), Client_Hostname(c),
410 Conn_GetIPAInfo(Client_Conn(c))))
413 if (!IRC_WriteStrClient(from, RPL_WHOISMODES_MSG,
414 Client_ID(from), Client_ID(c), Client_Modes(c)))
418 /* Idle and signon time (local clients only!) */
419 if (!Conf_MorePrivacy && Client_Conn(c) > NONE &&
420 !IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
421 Client_ID(from), Client_ID(c),
422 (unsigned long)Conn_GetIdle(Client_Conn(c)),
423 (unsigned long)Conn_GetSignon(Client_Conn(c))))
427 if (Client_HasMode(c, 'a') &&
428 !IRC_WriteStrClient(from, RPL_AWAY_MSG,
429 Client_ID(from), Client_ID(c), Client_Away(c)))
436 WHOWAS_EntryWrite(CLIENT *prefix, WHOWAS *entry)
440 (void)strftime(t_str, sizeof(t_str), "%a %b %d %H:%M:%S %Y",
441 localtime(&entry->time));
443 if (!IRC_WriteStrClient(prefix, RPL_WHOWASUSER_MSG, Client_ID(prefix),
444 entry->id, entry->user, entry->host, entry->info))
447 return IRC_WriteStrClient(prefix, RPL_WHOISSERVER_MSG, Client_ID(prefix),
448 entry->id, entry->server, t_str);
453 Show_MOTD_SSLInfo(CLIENT *Client)
455 char buf[COMMAND_LEN];
458 if (Conn_GetCipherInfo(Client_Conn(Client), c_str, sizeof(c_str))) {
459 snprintf(buf, sizeof(buf), "Connected using Cipher %s", c_str);
460 if (!IRC_WriteStrClient(Client, RPL_MOTD_MSG,
461 Client_ID(Client), buf))
465 if (Conn_GetFingerprint(Client_Conn(Client))) {
466 snprintf(buf, sizeof(buf),
467 "Your client certificate fingerprint is: %s",
468 Conn_GetFingerprint(Client_Conn(Client)));
469 if (!IRC_WriteStrClient(Client, RPL_MOTD_MSG,
470 Client_ID(Client), buf))
478 Show_MOTD_SSLInfo(UNUSED CLIENT *c)
484 /* Global functions */
487 * Handler for the IRC command "ADMIN".
489 * @param Client The client from which this command has been received.
490 * @param Req Request structure with prefix and all parameters.
491 * @return CONNECTED or DISCONNECTED.
494 IRC_ADMIN(CLIENT *Client, REQUEST *Req )
496 CLIENT *target, *prefix;
498 assert( Client != NULL );
499 assert( Req != NULL );
501 IRC_SetPenalty(Client, 1);
503 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
504 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
505 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
508 if(target != Client_ThisServer()) {
509 IRC_WriteStrClientPrefix(target, prefix,
510 "ADMIN %s", Client_ID(target));
514 if (!IRC_WriteStrClient(Client, RPL_ADMINME_MSG, Client_ID(prefix),
517 if (!IRC_WriteStrClient(Client, RPL_ADMINLOC1_MSG, Client_ID(prefix),
520 if (!IRC_WriteStrClient(Client, RPL_ADMINLOC2_MSG, Client_ID(prefix),
523 if (!IRC_WriteStrClient(Client, RPL_ADMINEMAIL_MSG, Client_ID(prefix),
524 Conf_ServerAdminMail))
531 * Handler for the IRC command "INFO".
533 * @param Client The client from which this command has been received.
534 * @param Req Request structure with prefix and all parameters.
535 * @return CONNECTED or DISCONNECTED.
538 IRC_INFO(CLIENT * Client, REQUEST * Req)
540 CLIENT *target, *prefix;
543 assert(Client != NULL);
546 IRC_SetPenalty(Client, 2);
548 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
549 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
550 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
553 if (target != Client_ThisServer()) {
554 IRC_WriteStrClientPrefix(target, prefix, "INFO %s",
559 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix),
563 #if defined(__DATE__) && defined(__TIME__)
564 snprintf(msg, sizeof(msg), "Birth Date: %s at %s", __DATE__, __TIME__);
565 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
569 strlcpy(msg, "On-line since ", sizeof(msg));
570 strlcat(msg, NGIRCd_StartStr, sizeof(msg));
571 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
574 if (!IRC_WriteStrClient(Client, RPL_ENDOFINFO_MSG, Client_ID(prefix)))
581 * Handler for the IRC "ISON" command.
583 * @param Client The client from which this command has been received.
584 * @param Req Request structure with prefix and all parameters.
585 * @return CONNECTED or DISCONNECTED.
588 IRC_ISON( CLIENT *Client, REQUEST *Req )
590 char rpl[COMMAND_LEN];
595 assert(Client != NULL);
598 _IRC_ARGC_GE_OR_RETURN_(Client, Req, 1)
600 strlcpy(rpl, RPL_ISON_MSG, sizeof rpl);
601 for (i = 0; i < Req->argc; i++) {
602 /* "All" ircd even parse ":<x> <y> ..." arguments and split
603 * them up; so we do the same ... */
604 ptr = strtok(Req->argv[i], " ");
607 c = Client_Search(ptr);
608 if (c && Client_Type(c) == CLIENT_USER) {
609 strlcat(rpl, Client_ID(c), sizeof(rpl));
610 strlcat(rpl, " ", sizeof(rpl));
612 ptr = strtok(NULL, " ");
615 ngt_TrimLastChr(rpl, ' ');
617 return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
621 * Handler for the IRC "LINKS" command.
623 * @param Client The client from which this command has been received.
624 * @param Req Request structure with prefix and all parameters.
625 * @return CONNECTED or DISCONNECTED.
628 IRC_LINKS(CLIENT *Client, REQUEST *Req)
630 CLIENT *target, *from, *c;
633 assert(Client != NULL);
636 IRC_SetPenalty(Client, 1);
638 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
639 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
641 /* Get pointer to server mask or "*", if none given */
643 mask = Req->argv[Req->argc - 1];
648 if (Req->argc == 2) {
649 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
650 if (target != Client_ThisServer()) {
651 IRC_WriteStrClientPrefix(target, from,
652 "LINKS %s %s", Client_ID(target),
660 if (Client_Type(c) == CLIENT_SERVER
661 && MatchCaseInsensitive(mask, Client_ID(c))) {
662 if (!IRC_WriteStrClient(from, RPL_LINKS_MSG,
663 Client_ID(from), Client_ID(c),
664 Client_ID(Client_TopServer(c)
665 ? Client_TopServer(c)
666 : Client_ThisServer()),
667 Client_Hops(c), Client_Info(c)))
672 return IRC_WriteStrClient(from, RPL_ENDOFLINKS_MSG,
673 Client_ID(from), mask);
677 * Handler for the IRC "LUSERS" command.
679 * @param Client The client from which this command has been received.
680 * @param Req Request structure with prefix and all parameters.
681 * @return CONNECTED or DISCONNECTED.
684 IRC_LUSERS( CLIENT *Client, REQUEST *Req )
686 CLIENT *target, *from;
688 assert( Client != NULL );
689 assert( Req != NULL );
691 IRC_SetPenalty(Client, 1);
693 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
694 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
695 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
698 if (target != Client_ThisServer()) {
699 IRC_WriteStrClientPrefix(target, from,
700 "LUSERS %s %s", Req->argv[0],
705 return IRC_Send_LUSERS(from);
709 * Handler for the IRC command "SERVLIST".
711 * @param Client The client from which this command has been received.
712 * @param Req Request structure with prefix and all parameters.
713 * @return CONNECTED or DISCONNECTED.
716 IRC_SERVLIST(CLIENT *Client, REQUEST *Req)
720 IRC_SetPenalty(Client, 1);
722 assert(Client != NULL);
725 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
727 if (Req->argc < 2 || strcmp(Req->argv[1], "0") == 0) {
728 for (c = Client_First(); c!= NULL; c = Client_Next(c)) {
729 if (Client_Type(c) != CLIENT_SERVICE)
731 if (Req->argc > 0 && !MatchCaseInsensitive(Req->argv[0],
734 if (!IRC_WriteStrClient(Client, RPL_SERVLIST_MSG,
735 Client_ID(Client), Client_Mask(c),
736 Client_Mask(Client_Introducer(c)), "*",
737 0, Client_Hops(c), Client_Info(c)))
742 return IRC_WriteStrClient(Client, RPL_SERVLISTEND_MSG, Client_ID(Client),
743 Req->argc > 0 ? Req->argv[0] : "*",
744 Req->argc > 1 ? Req->argv[1] : "0");
748 * Handler for the IRC command "MOTD".
750 * @param Client The client from which this command has been received.
751 * @param Req Request structure with prefix and all parameters.
752 * @return CONNECTED or DISCONNECTED.
755 IRC_MOTD( CLIENT *Client, REQUEST *Req )
757 CLIENT *from, *target;
759 assert( Client != NULL );
760 assert( Req != NULL );
762 IRC_SetPenalty(Client, 3);
764 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
765 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
766 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
769 if (target != Client_ThisServer()) {
770 IRC_WriteStrClientPrefix(target, from, "MOTD %s",
775 return IRC_Show_MOTD(from);
779 * Handler for the IRC command "NAMES".
781 * @param Client The client from which this command has been received.
782 * @param Req Request structure with prefix and all parameters.
783 * @return CONNECTED or DISCONNECTED.
786 IRC_NAMES( CLIENT *Client, REQUEST *Req )
788 char rpl[COMMAND_LEN], *ptr;
789 CLIENT *target, *from, *c;
792 assert( Client != NULL );
793 assert( Req != NULL );
795 IRC_SetPenalty(Client, 1);
797 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
798 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
799 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
802 if (target != Client_ThisServer()) {
803 IRC_WriteStrClientPrefix(target, from, "NAMES %s :%s",
804 Req->argv[0], Client_ID(target));
809 /* Return NAMES list for specific channels */
810 ptr = strtok(Req->argv[0], ",");
812 chan = Channel_Search(ptr);
813 if (chan && !IRC_Send_NAMES(from, chan))
815 if (!IRC_WriteStrClient(from, RPL_ENDOFNAMES_MSG,
816 Client_ID(from), ptr))
818 ptr = strtok( NULL, "," );
823 chan = Channel_First();
825 if (!IRC_Send_NAMES(from, chan))
827 chan = Channel_Next(chan);
830 /* Now print all clients which are not in any channel */
832 snprintf(rpl, sizeof(rpl), RPL_NAMREPLY_MSG, Client_ID(from), "*", "*");
834 if (Client_Type(c) == CLIENT_USER
835 && Channel_FirstChannelOf(c) == NULL
836 && !strchr(Client_Modes(c), 'i'))
838 /* its a user, concatenate ... */
839 if (rpl[strlen(rpl) - 1] != ':')
840 strlcat(rpl, " ", sizeof(rpl));
841 strlcat(rpl, Client_ID(c), sizeof(rpl));
843 if (strlen(rpl) > COMMAND_LEN - CLIENT_NICK_LEN - 4) {
844 /* Line is gwoing too long, send now */
845 if (!IRC_WriteStrClient(from, "%s", rpl))
847 snprintf(rpl, sizeof(rpl), RPL_NAMREPLY_MSG,
848 Client_ID(from), "*", "*");
853 if (rpl[strlen(rpl) - 1] != ':' && !IRC_WriteStrClient(from, "%s", rpl))
856 return IRC_WriteStrClient(from, RPL_ENDOFNAMES_MSG, Client_ID(from), "*");
860 * Handler for the IRC command "STATS".
862 * @param Client The client from which this command has been received.
863 * @param Req Request structure with prefix and all parameters.
864 * @return CONNECTED or DISCONNECTED.
867 IRC_STATS( CLIENT *Client, REQUEST *Req )
869 CLIENT *from, *target, *cl;
874 unsigned int days, hrs, mins;
875 struct list_head *list;
876 struct list_elem *list_item;
878 assert(Client != NULL);
881 IRC_SetPenalty(Client, 2);
883 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
884 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
885 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
888 if (target != Client_ThisServer()) {
889 IRC_WriteStrClientPrefix(target, from, "STATS %s %s",
890 Req->argv[0], Client_ID(target));
895 query = Req->argv[0][0] ? Req->argv[0][0] : '*';
900 case 'g': /* Network-wide bans ("G-Lines") */
902 case 'k': /* Server-local bans ("K-Lines") */
904 if (!Client_HasMode(from, 'o'))
905 return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG,
907 if (query == 'g' || query == 'G')
908 list = Class_GetList(CLASS_GLINE);
910 list = Class_GetList(CLASS_KLINE);
911 list_item = Lists_GetFirst(list);
913 if (!IRC_WriteStrClient(from, RPL_STATSXLINE_MSG,
914 Client_ID(from), query,
915 Lists_GetMask(list_item),
916 Lists_GetValidity(list_item),
917 Lists_GetReason(list_item)))
919 list_item = Lists_GetNext(list_item);
922 case 'l': /* Link status (servers and own link) */
924 time_now = time(NULL);
925 for (con = Conn_First(); con != NONE; con = Conn_Next(con)) {
926 cl = Conn_GetClient(con);
929 if ((Client_Type(cl) == CLIENT_SERVER)
931 /* Server link or our own connection */
933 if (Conn_Options(con) & CONN_ZIP) {
934 if (!IRC_WriteStrClient
935 (from, RPL_STATSLINKINFOZIP_MSG,
936 Client_ID(from), Client_Mask(cl),
937 Conn_SendQ(con), Conn_SendMsg(con),
943 (long)(time_now - Conn_StartTime(con))))
948 if (!IRC_WriteStrClient
949 (from, RPL_STATSLINKINFO_MSG,
950 Client_ID(from), Client_Mask(cl),
951 Conn_SendQ(con), Conn_SendMsg(con),
952 Conn_SendBytes(con), Conn_RecvMsg(con),
954 (long)(time_now - Conn_StartTime(con))))
959 case 'm': /* IRC command status (usage count) */
961 cmd = Parse_GetCommandStruct();
962 for (; cmd->name; cmd++) {
963 if (cmd->lcount == 0 && cmd->rcount == 0)
965 if (!IRC_WriteStrClient
966 (from, RPL_STATSCOMMANDS_MSG, Client_ID(from),
967 cmd->name, cmd->lcount, cmd->bytes, cmd->rcount))
971 case 'u': /* Server uptime */
973 time_now = time(NULL) - NGIRCd_Start;
974 days = uptime_days(&time_now);
975 hrs = uptime_hrs(&time_now);
976 mins = uptime_mins(&time_now);
977 if (!IRC_WriteStrClient(from, RPL_STATSUPTIME, Client_ID(from),
978 days, hrs, mins, (unsigned int)time_now))
983 return IRC_WriteStrClient(from, RPL_ENDOFSTATS_MSG,
984 Client_ID(from), query);
988 * Handler for the IRC command "SUMMON".
990 * @param Client The client from which this command has been received.
991 * @param Req Request structure with prefix and all parameters.
992 * @return CONNECTED or DISCONNECTED.
995 IRC_SUMMON(CLIENT * Client, UNUSED REQUEST * Req)
997 assert(Client != NULL);
999 IRC_SetPenalty(Client, 1);
1001 return IRC_WriteStrClient(Client, ERR_SUMMONDISABLED_MSG,
1006 * Handler for the IRC command "TIME".
1008 * @param Client The client from which this command has been received.
1009 * @param Req Request structure with prefix and all parameters.
1010 * @return CONNECTED or DISCONNECTED.
1013 IRC_TIME( CLIENT *Client, REQUEST *Req )
1015 CLIENT *from, *target;
1019 assert(Client != NULL);
1020 assert(Req != NULL);
1022 IRC_SetPenalty(Client, 1);
1024 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
1025 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
1026 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
1029 if (target != Client_ThisServer()) {
1030 IRC_WriteStrClientPrefix(target, from, "TIME %s",
1036 (void)strftime(t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime(&t));
1037 return IRC_WriteStrClient(from, RPL_TIME_MSG, Client_ID(from),
1038 Client_ID(Client_ThisServer()), t_str);
1042 * Handler for the IRC command "USERHOST".
1044 * @param Client The client from which this command has been received.
1045 * @param Req Request structure with prefix and all parameters.
1046 * @return CONNECTED or DISCONNECTED.
1049 IRC_USERHOST(CLIENT *Client, REQUEST *Req)
1051 char rpl[COMMAND_LEN];
1055 assert(Client != NULL);
1056 assert(Req != NULL);
1058 IRC_SetPenalty(Client, 1);
1060 _IRC_ARGC_GE_OR_RETURN_(Client, Req, 1)
1067 strlcpy(rpl, RPL_USERHOST_MSG, sizeof rpl);
1068 for (i = 0; i < max; i++) {
1069 c = Client_Search(Req->argv[i]);
1070 if (c && (Client_Type(c) == CLIENT_USER)) {
1071 /* This Nick is "online" */
1072 strlcat(rpl, Client_ID(c), sizeof(rpl));
1073 if (Client_HasMode(c, 'o'))
1074 strlcat(rpl, "*", sizeof(rpl));
1075 strlcat(rpl, "=", sizeof(rpl));
1076 if (Client_HasMode(c, 'a'))
1077 strlcat(rpl, "-", sizeof(rpl));
1079 strlcat(rpl, "+", sizeof(rpl));
1080 strlcat(rpl, Client_User(c), sizeof(rpl));
1081 strlcat(rpl, "@", sizeof(rpl));
1082 strlcat(rpl, Client_HostnameDisplayed(c), sizeof(rpl));
1083 strlcat(rpl, " ", sizeof(rpl));
1086 ngt_TrimLastChr(rpl, ' ');
1088 return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
1089 } /* IRC_USERHOST */
1092 * Handler for the IRC command "USERS".
1094 * @param Client The client from which this command has been received.
1095 * @param Req Request structure with prefix and all parameters.
1096 * @return CONNECTED or DISCONNECTED.
1099 IRC_USERS(CLIENT * Client, UNUSED REQUEST * Req)
1101 assert(Client != NULL);
1103 IRC_SetPenalty(Client, 1);
1105 return IRC_WriteStrClient(Client, ERR_USERSDISABLED_MSG,
1110 * Handler for the IRC command "VERSION".
1112 * @param Client The client from which this command has been received.
1113 * @param Req Request structure with prefix and all parameters.
1114 * @return CONNECTED or DISCONNECTED.
1117 IRC_VERSION( CLIENT *Client, REQUEST *Req )
1119 CLIENT *target, *prefix;
1121 assert( Client != NULL );
1122 assert( Req != NULL );
1124 IRC_SetPenalty(Client, 1);
1126 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
1127 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
1128 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
1131 if (target != Client_ThisServer()) {
1132 IRC_WriteStrClientPrefix(target, prefix, "VERSION %s",
1137 /* send version information */
1138 return IRC_WriteStrClient(Client, RPL_VERSION_MSG, Client_ID(prefix),
1139 PACKAGE_NAME, PACKAGE_VERSION,
1140 NGIRCd_DebugLevel, Conf_ServerName,
1141 NGIRCd_VersionAddition);
1145 * Handler for the IRC "WHO" command.
1147 * @param Client The client from which this command has been received.
1148 * @param Req Request structure with prefix and all parameters.
1149 * @return CONNECTED or DISCONNECTED.
1152 IRC_WHO(CLIENT *Client, REQUEST *Req)
1157 assert (Client != NULL);
1158 assert (Req != NULL);
1160 IRC_SetPenalty(Client, 1);
1162 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
1165 if (Req->argc == 2) {
1166 if (strcmp(Req->argv[1], "o") == 0)
1170 return IRC_WriteStrClient(Client,
1171 ERR_NEEDMOREPARAMS_MSG,
1177 if (Req->argc >= 1) {
1178 /* Channel or mask given */
1179 chan = Channel_Search(Req->argv[0]);
1181 /* Members of a channel have been requested */
1182 return IRC_WHO_Channel(Client, chan, only_ops);
1184 if (strcmp(Req->argv[0], "0") != 0) {
1185 /* A mask has been given. But please note this RFC
1186 * stupidity: "0" is same as no arguments ... */
1187 return IRC_WHO_Mask(Client, Req->argv[0], only_ops);
1191 /* No channel or (valid) mask given */
1192 IRC_SetPenalty(Client, 2);
1193 return IRC_WHO_Mask(Client, NULL, only_ops);
1197 * Handler for the IRC "WHOIS" command.
1199 * @param Client The client from which this command has been received.
1200 * @param Req Request structure with prefix and all parameters.
1201 * @return CONNECTED or DISCONNECTED.
1204 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
1206 CLIENT *from, *target, *c;
1207 unsigned int match_count = 0, found = 0;
1208 bool has_wildcards, is_remote;
1209 bool got_wildcard = false;
1210 char mask[COMMAND_LEN], *query;
1212 assert( Client != NULL );
1213 assert( Req != NULL );
1215 IRC_SetPenalty(Client, 1);
1217 /* Bad number of parameters? */
1218 if (Req->argc < 1 || Req->argc > 2)
1219 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1220 Client_ID(Client), Req->command);
1222 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
1224 /* Get target server for this command */
1225 if (Req->argc > 1) {
1226 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, Client)
1228 target = Client_ThisServer();
1230 assert(target != NULL);
1233 if (target != Client_ThisServer()) {
1234 IRC_WriteStrClientPrefix(target, from, "WHOIS %s :%s",
1235 Req->argv[0], Req->argv[1]);
1239 is_remote = Client_Conn(from) < 0;
1240 strlcpy(mask, Req->argv[Req->argc - 1], sizeof(mask));
1241 for (query = strtok(ngt_LowerStr(mask), ",");
1243 query = strtok(NULL, ","), found++)
1245 has_wildcards = query[strcspn(query, "*?")] != 0;
1247 * follows ircd 2.10 implementation:
1248 * - handle up to 3 targets
1249 * - no wildcards for remote clients
1250 * - only one wildcard target per local client
1252 * Also, at most MAX_RPL_WHOIS matches are returned.
1254 if (!has_wildcards || is_remote) {
1255 c = Client_Search(query);
1256 if (c && (Client_Type(c) == CLIENT_USER
1257 || Client_Type(c) == CLIENT_SERVICE)) {
1258 if (!IRC_WHOIS_SendReply(Client, from, c))
1259 return DISCONNECTED;
1261 if (!IRC_WriteStrClient(Client,
1265 return DISCONNECTED;
1270 /* we already handled one wildcard query */
1271 if (!IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1272 Client_ID(Client), query))
1273 return DISCONNECTED;
1276 got_wildcard = true;
1277 IRC_SetPenalty(Client, 3);
1279 for (c = Client_First(); c; c = Client_Next(c)) {
1280 if (IRC_CheckListTooBig(Client, match_count,
1281 MAX_RPL_WHOIS, "WHOIS"))
1284 if (Client_Type(c) != CLIENT_USER)
1286 if (!MatchCaseInsensitive(query, Client_ID(c)))
1288 if (!IRC_WHOIS_SendReply(Client, from, c))
1289 return DISCONNECTED;
1294 if (match_count == 0)
1295 IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1297 Req->argv[Req->argc - 1]);
1299 return IRC_WriteStrClient(from, RPL_ENDOFWHOIS_MSG,
1300 Client_ID(from), Req->argv[Req->argc - 1]);
1304 * Handler for the IRC "WHOWAS" command.
1306 * @param Client The client from which this command has been received.
1307 * @param Req Request structure with prefix and all parameters.
1308 * @return CONNECTED or DISCONNECTED.
1311 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1313 CLIENT *target, *prefix;
1315 char tok_buf[COMMAND_LEN];
1316 int max, last, count, i, nc;
1319 assert( Client != NULL );
1320 assert( Req != NULL );
1322 /* Do not reveal any info on disconnected users? */
1323 if (Conf_MorePrivacy)
1326 /* Wrong number of parameters? */
1328 return IRC_WriteStrClient(Client, ERR_NONICKNAMEGIVEN_MSG,
1331 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 3)
1332 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
1333 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 2, prefix)
1336 if (target != Client_ThisServer()) {
1337 IRC_WriteStrClientPrefix(target, prefix, "WHOWAS %s %s %s",
1338 Req->argv[0], Req->argv[1],
1343 whowas = Client_GetWhowas( );
1344 last = Client_GetLastWhowasIndex( );
1348 max = DEF_RPL_WHOWAS;
1349 if (Req->argc > 1) {
1350 max = atoi(Req->argv[1]);
1352 max = MAX_RPL_WHOWAS;
1356 * Break up the nick argument into a list of nicks, if applicable
1357 * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1359 strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1360 nick = strtok(tok_buf, ",");
1362 for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1366 if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1367 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1368 return DISCONNECTED;
1372 /* previous entry */
1375 /* "underflow", wrap around */
1379 if (nc && count >= max)
1381 } while (i != last);
1383 if (nc == 0 && !IRC_WriteStrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1384 Client_ID(prefix), nick))
1385 return DISCONNECTED;
1387 return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG,
1388 Client_ID(prefix), Req->argv[0]);
1392 * Send LUSERS reply to a client.
1394 * @param Client The receipient of the information.
1395 * @return CONNECTED or DISCONNECTED.
1398 IRC_Send_LUSERS(CLIENT *Client)
1405 assert(Client != NULL);
1407 /* Users, services and serevers in the network */
1408 if (!IRC_WriteStrClient(Client, RPL_LUSERCLIENT_MSG, Client_ID(Client),
1409 Client_UserCount(), Client_ServiceCount(),
1410 Client_ServerCount()))
1411 return DISCONNECTED;
1413 /* Number of IRC operators */
1414 cnt = Client_OperCount( );
1416 if (!IRC_WriteStrClient(Client, RPL_LUSEROP_MSG,
1417 Client_ID(Client), cnt))
1418 return DISCONNECTED;
1421 /* Unknown connections */
1422 cnt = Client_UnknownCount( );
1424 if (!IRC_WriteStrClient(Client, RPL_LUSERUNKNOWN_MSG,
1425 Client_ID(Client), cnt))
1426 return DISCONNECTED;
1429 /* Number of created channels */
1430 if (!IRC_WriteStrClient(Client, RPL_LUSERCHANNELS_MSG,
1432 Channel_CountVisible(Client)))
1433 return DISCONNECTED;
1435 /* Number of local users, services and servers */
1436 if (!IRC_WriteStrClient(Client, RPL_LUSERME_MSG, Client_ID(Client),
1437 Client_MyUserCount(), Client_MyServiceCount(),
1438 Client_MyServerCount()))
1439 return DISCONNECTED;
1442 /* Maximum number of local users */
1443 cnt = Client_MyUserCount();
1444 max = Client_MyMaxUserCount();
1445 if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1446 cnt, max, cnt, max))
1447 return DISCONNECTED;
1448 /* Maximum number of users in the network */
1449 cnt = Client_UserCount();
1450 max = Client_MaxUserCount();
1451 if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1452 cnt, max, cnt, max))
1453 return DISCONNECTED;
1454 /* Connection counters */
1455 if (! IRC_WriteStrClient(Client, RPL_STATSCONN_MSG, Client_ID(Client),
1456 Conn_CountMax(), Conn_CountAccepted()))
1457 return DISCONNECTED;
1461 } /* IRC_Send_LUSERS */
1464 IRC_Show_MOTD( CLIENT *Client )
1467 size_t len_tot, len_str;
1469 assert( Client != NULL );
1471 len_tot = array_bytes(&Conf_Motd);
1472 if (len_tot == 0 && !Conn_UsesSSL(Client_Conn(Client)))
1473 return IRC_WriteStrClient(Client, ERR_NOMOTD_MSG, Client_ID(Client));
1475 if (!IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG, Client_ID(Client),
1476 Client_ID(Client_ThisServer())))
1477 return DISCONNECTED;
1479 line = array_start(&Conf_Motd);
1480 while (len_tot > 0) {
1481 len_str = strlen(line) + 1;
1483 assert(len_tot >= len_str);
1486 if (!IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID(Client), line))
1487 return DISCONNECTED;
1491 if (!Show_MOTD_SSLInfo(Client))
1492 return DISCONNECTED;
1494 if (!IRC_WriteStrClient(Client, RPL_ENDOFMOTD_MSG, Client_ID(Client)))
1495 return DISCONNECTED;
1497 if (*Conf_CloakHost)
1498 return IRC_WriteStrClient(Client, RPL_HOSTHIDDEN_MSG,
1500 Client_Hostname(Client));
1503 } /* IRC_Show_MOTD */
1506 * Send NAMES reply for a specific client and channel.
1508 * @param Client The client requesting the NAMES information.
1509 * @param Chan The channel
1510 * @return CONNECTED or DISCONNECTED.
1513 IRC_Send_NAMES(CLIENT * Client, CHANNEL * Chan)
1515 bool is_visible, is_member;
1516 char str[COMMAND_LEN];
1520 assert(Client != NULL);
1521 assert(Chan != NULL);
1523 if (Channel_IsMemberOf(Chan, Client))
1528 /* Do not print info on channel memberships to anyone that is not member? */
1529 if (Conf_MorePrivacy && !is_member)
1532 /* Secret channel? */
1533 if (!is_member && strchr(Channel_Modes(Chan), 's'))
1536 snprintf(str, sizeof(str), RPL_NAMREPLY_MSG, Client_ID(Client), "=",
1537 Channel_Name(Chan));
1538 cl2chan = Channel_FirstMember(Chan);
1540 cl = Channel_GetClient(cl2chan);
1542 if (strchr(Client_Modes(cl), 'i'))
1547 if (is_member || is_visible) {
1548 if (str[strlen(str) - 1] != ':')
1549 strlcat(str, " ", sizeof(str));
1551 who_flags_qualifier(Client, Channel_UserModes(Chan, cl),
1553 strlcat(str, Client_ID(cl), sizeof(str));
1555 if (strlen(str) > (COMMAND_LEN - CLIENT_NICK_LEN - 4)) {
1556 if (!IRC_WriteStrClient(Client, "%s", str))
1557 return DISCONNECTED;
1558 snprintf(str, sizeof(str), RPL_NAMREPLY_MSG,
1559 Client_ID(Client), "=",
1560 Channel_Name(Chan));
1564 cl2chan = Channel_NextMember(Chan, cl2chan);
1566 if (str[strlen(str) - 1] != ':') {
1567 if (!IRC_WriteStrClient(Client, "%s", str))
1568 return DISCONNECTED;
1572 } /* IRC_Send_NAMES */
1575 * Send the ISUPPORT numeric (005).
1576 * This numeric indicates the features that are supported by this server.
1577 * See <http://www.irc.org/tech_docs/005.html> for details.
1580 IRC_Send_ISUPPORT(CLIENT * Client)
1582 if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1584 return DISCONNECTED;
1585 return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1586 CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1587 COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1588 COMMAND_LEN - 113, MAX_HNDL_MODES_ARG,
1589 MAX_HNDL_CHANNEL_LISTS);
1590 } /* IRC_Send_ISUPPORT */