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);
452 Show_MOTD_Start(CLIENT *Client)
454 return IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG,
455 Client_ID( Client ), Client_ID( Client_ThisServer( )));
459 Show_MOTD_Sendline(CLIENT *Client, const char *msg)
461 return IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID( Client ), msg);
465 Show_MOTD_End(CLIENT *Client)
467 if (!IRC_WriteStrClient(Client, RPL_ENDOFMOTD_MSG, Client_ID(Client)))
471 return IRC_WriteStrClient(Client, RPL_HOSTHIDDEN_MSG,
473 Client_Hostname(Client));
479 static bool Show_MOTD_SSLInfo(CLIENT *Client)
481 char buf[COMMAND_LEN];
484 if (Conn_GetCipherInfo(Client_Conn(Client), c_str, sizeof(c_str))) {
485 snprintf(buf, sizeof(buf), "Connected using Cipher %s", c_str);
486 if (!IRC_WriteStrClient(Client, RPL_MOTD_MSG,
487 Client_ID(Client), buf))
491 if (Conn_GetFingerprint(Client_Conn(Client))) {
492 snprintf(buf, sizeof(buf),
493 "Your client certificate fingerprint is: %s",
494 Conn_GetFingerprint(Client_Conn(Client)));
495 if (!IRC_WriteStrClient(Client, RPL_MOTD_MSG,
496 Client_ID(Client), buf))
504 Show_MOTD_SSLInfo(UNUSED CLIENT *c)
508 /* Global functions */
511 * Handler for the IRC command "ADMIN".
513 * @param Client The client from which this command has been received.
514 * @param Req Request structure with prefix and all parameters.
515 * @return CONNECTED or DISCONNECTED.
518 IRC_ADMIN(CLIENT *Client, REQUEST *Req )
520 CLIENT *target, *prefix;
522 assert( Client != NULL );
523 assert( Req != NULL );
525 IRC_SetPenalty(Client, 1);
527 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
528 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
529 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
532 if(target != Client_ThisServer()) {
533 IRC_WriteStrClientPrefix(target, prefix,
534 "ADMIN %s", Client_ID(target));
538 if (!IRC_WriteStrClient(Client, RPL_ADMINME_MSG, Client_ID(prefix),
541 if (!IRC_WriteStrClient(Client, RPL_ADMINLOC1_MSG, Client_ID(prefix),
544 if (!IRC_WriteStrClient(Client, RPL_ADMINLOC2_MSG, Client_ID(prefix),
547 if (!IRC_WriteStrClient(Client, RPL_ADMINEMAIL_MSG, Client_ID(prefix),
548 Conf_ServerAdminMail))
555 * Handler for the IRC command "INFO".
557 * @param Client The client from which this command has been received.
558 * @param Req Request structure with prefix and all parameters.
559 * @return CONNECTED or DISCONNECTED.
562 IRC_INFO(CLIENT * Client, REQUEST * Req)
564 CLIENT *target, *prefix;
567 assert(Client != NULL);
570 IRC_SetPenalty(Client, 2);
572 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
573 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
574 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
577 if (target != Client_ThisServer()) {
578 IRC_WriteStrClientPrefix(target, prefix, "INFO %s",
583 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix),
587 #if defined(__DATE__) && defined(__TIME__)
588 snprintf(msg, sizeof(msg), "Birth Date: %s at %s", __DATE__, __TIME__);
589 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
593 strlcpy(msg, "On-line since ", sizeof(msg));
594 strlcat(msg, NGIRCd_StartStr, sizeof(msg));
595 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
598 if (!IRC_WriteStrClient(Client, RPL_ENDOFINFO_MSG, Client_ID(prefix)))
605 * Handler for the IRC "ISON" command.
607 * @param Client The client from which this command has been received.
608 * @param Req Request structure with prefix and all parameters.
609 * @return CONNECTED or DISCONNECTED.
612 IRC_ISON( CLIENT *Client, REQUEST *Req )
614 char rpl[COMMAND_LEN];
619 assert(Client != NULL);
622 _IRC_ARGC_GE_OR_RETURN_(Client, Req, 1)
624 strlcpy(rpl, RPL_ISON_MSG, sizeof rpl);
625 for (i = 0; i < Req->argc; i++) {
626 /* "All" ircd even parse ":<x> <y> ..." arguments and split
627 * them up; so we do the same ... */
628 ptr = strtok(Req->argv[i], " ");
631 c = Client_Search(ptr);
632 if (c && Client_Type(c) == CLIENT_USER) {
633 strlcat(rpl, Client_ID(c), sizeof(rpl));
634 strlcat(rpl, " ", sizeof(rpl));
636 ptr = strtok(NULL, " ");
639 ngt_TrimLastChr(rpl, ' ');
641 return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
645 * Handler for the IRC "LINKS" command.
647 * @param Client The client from which this command has been received.
648 * @param Req Request structure with prefix and all parameters.
649 * @return CONNECTED or DISCONNECTED.
652 IRC_LINKS(CLIENT *Client, REQUEST *Req)
654 CLIENT *target, *from, *c;
657 assert(Client != NULL);
660 IRC_SetPenalty(Client, 1);
662 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
663 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
665 /* Get pointer to server mask or "*", if none given */
667 mask = Req->argv[Req->argc - 1];
672 if (Req->argc == 2) {
673 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
674 if (target != Client_ThisServer()) {
675 IRC_WriteStrClientPrefix(target, from,
676 "LINKS %s %s", Client_ID(target),
684 if (Client_Type(c) == CLIENT_SERVER
685 && MatchCaseInsensitive(mask, Client_ID(c))) {
686 if (!IRC_WriteStrClient(from, RPL_LINKS_MSG,
687 Client_ID(from), Client_ID(c),
688 Client_ID(Client_TopServer(c)
689 ? Client_TopServer(c)
690 : Client_ThisServer()),
691 Client_Hops(c), Client_Info(c)))
696 return IRC_WriteStrClient(from, RPL_ENDOFLINKS_MSG,
697 Client_ID(from), mask);
701 * Handler for the IRC "LUSERS" command.
703 * @param Client The client from which this command has been received.
704 * @param Req Request structure with prefix and all parameters.
705 * @return CONNECTED or DISCONNECTED.
708 IRC_LUSERS( CLIENT *Client, REQUEST *Req )
710 CLIENT *target, *from;
712 assert( Client != NULL );
713 assert( Req != NULL );
715 IRC_SetPenalty(Client, 1);
717 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
718 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
719 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
722 if (target != Client_ThisServer()) {
723 IRC_WriteStrClientPrefix(target, from,
724 "LUSERS %s %s", Req->argv[0],
729 return IRC_Send_LUSERS(from);
733 * Handler for the IRC command "SERVLIST".
735 * @param Client The client from which this command has been received.
736 * @param Req Request structure with prefix and all parameters.
737 * @return CONNECTED or DISCONNECTED.
740 IRC_SERVLIST(CLIENT *Client, REQUEST *Req)
744 IRC_SetPenalty(Client, 1);
746 assert(Client != NULL);
749 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
751 if (Req->argc < 2 || strcmp(Req->argv[1], "0") == 0) {
752 for (c = Client_First(); c!= NULL; c = Client_Next(c)) {
753 if (Client_Type(c) != CLIENT_SERVICE)
755 if (Req->argc > 0 && !MatchCaseInsensitive(Req->argv[0],
758 if (!IRC_WriteStrClient(Client, RPL_SERVLIST_MSG,
759 Client_ID(Client), Client_Mask(c),
760 Client_Mask(Client_Introducer(c)), "*",
761 0, Client_Hops(c), Client_Info(c)))
766 return IRC_WriteStrClient(Client, RPL_SERVLISTEND_MSG, Client_ID(Client),
767 Req->argc > 0 ? Req->argv[0] : "*",
768 Req->argc > 1 ? Req->argv[1] : "0");
772 * Handler for the IRC command "MOTD".
774 * @param Client The client from which this command has been received.
775 * @param Req Request structure with prefix and all parameters.
776 * @return CONNECTED or DISCONNECTED.
779 IRC_MOTD( CLIENT *Client, REQUEST *Req )
781 CLIENT *from, *target;
783 assert( Client != NULL );
784 assert( Req != NULL );
786 IRC_SetPenalty(Client, 3);
788 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
789 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
790 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
793 if (target != Client_ThisServer()) {
794 IRC_WriteStrClientPrefix(target, from, "MOTD %s",
799 return IRC_Show_MOTD(from);
803 * Handler for the IRC command "NAMES".
805 * @param Client The client from which this command has been received.
806 * @param Req Request structure with prefix and all parameters.
807 * @return CONNECTED or DISCONNECTED.
810 IRC_NAMES( CLIENT *Client, REQUEST *Req )
812 char rpl[COMMAND_LEN], *ptr;
813 CLIENT *target, *from, *c;
816 assert( Client != NULL );
817 assert( Req != NULL );
819 IRC_SetPenalty(Client, 1);
821 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
822 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
823 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
826 if (target != Client_ThisServer()) {
827 IRC_WriteStrClientPrefix(target, from, "NAMES %s :%s",
828 Req->argv[0], Client_ID(target));
833 /* Return NAMES list for specific channels */
834 ptr = strtok(Req->argv[0], ",");
836 chan = Channel_Search(ptr);
837 if (chan && !IRC_Send_NAMES(from, chan))
839 if (!IRC_WriteStrClient(from, RPL_ENDOFNAMES_MSG,
840 Client_ID(from), ptr))
842 ptr = strtok( NULL, "," );
847 chan = Channel_First();
849 if (!IRC_Send_NAMES(from, chan))
851 chan = Channel_Next(chan);
854 /* Now print all clients which are not in any channel */
856 snprintf(rpl, sizeof(rpl), RPL_NAMREPLY_MSG, Client_ID(from), "*", "*");
858 if (Client_Type(c) == CLIENT_USER
859 && Channel_FirstChannelOf(c) == NULL
860 && !strchr(Client_Modes(c), 'i'))
862 /* its a user, concatenate ... */
863 if (rpl[strlen(rpl) - 1] != ':')
864 strlcat(rpl, " ", sizeof(rpl));
865 strlcat(rpl, Client_ID(c), sizeof(rpl));
867 if (strlen(rpl) > COMMAND_LEN - CLIENT_NICK_LEN - 4) {
868 /* Line is gwoing too long, send now */
869 if (!IRC_WriteStrClient(from, "%s", rpl))
871 snprintf(rpl, sizeof(rpl), RPL_NAMREPLY_MSG,
872 Client_ID(from), "*", "*");
877 if (rpl[strlen(rpl) - 1] != ':' && !IRC_WriteStrClient(from, "%s", rpl))
880 return IRC_WriteStrClient(from, RPL_ENDOFNAMES_MSG, Client_ID(from), "*");
884 * Handler for the IRC command "STATS".
886 * @param Client The client from which this command has been received.
887 * @param Req Request structure with prefix and all parameters.
888 * @return CONNECTED or DISCONNECTED.
891 IRC_STATS( CLIENT *Client, REQUEST *Req )
893 CLIENT *from, *target, *cl;
898 unsigned int days, hrs, mins;
899 struct list_head *list;
900 struct list_elem *list_item;
902 assert(Client != NULL);
905 IRC_SetPenalty(Client, 2);
907 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
908 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
909 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
912 if (target != Client_ThisServer()) {
913 IRC_WriteStrClientPrefix(target, from, "STATS %s %s",
914 Req->argv[0], Client_ID(target));
919 query = Req->argv[0][0] ? Req->argv[0][0] : '*';
924 case 'g': /* Network-wide bans ("G-Lines") */
926 case 'k': /* Server-local bans ("K-Lines") */
928 if (!Client_HasMode(from, 'o'))
929 return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG,
931 if (query == 'g' || query == 'G')
932 list = Class_GetList(CLASS_GLINE);
934 list = Class_GetList(CLASS_KLINE);
935 list_item = Lists_GetFirst(list);
937 if (!IRC_WriteStrClient(from, RPL_STATSXLINE_MSG,
938 Client_ID(from), query,
939 Lists_GetMask(list_item),
940 Lists_GetValidity(list_item),
941 Lists_GetReason(list_item)))
943 list_item = Lists_GetNext(list_item);
946 case 'l': /* Link status (servers and own link) */
948 time_now = time(NULL);
949 for (con = Conn_First(); con != NONE; con = Conn_Next(con)) {
950 cl = Conn_GetClient(con);
953 if ((Client_Type(cl) == CLIENT_SERVER)
955 /* Server link or our own connection */
957 if (Conn_Options(con) & CONN_ZIP) {
958 if (!IRC_WriteStrClient
959 (from, RPL_STATSLINKINFOZIP_MSG,
960 Client_ID(from), Client_Mask(cl),
961 Conn_SendQ(con), Conn_SendMsg(con),
967 (long)(time_now - Conn_StartTime(con))))
972 if (!IRC_WriteStrClient
973 (from, RPL_STATSLINKINFO_MSG,
974 Client_ID(from), Client_Mask(cl),
975 Conn_SendQ(con), Conn_SendMsg(con),
976 Conn_SendBytes(con), Conn_RecvMsg(con),
978 (long)(time_now - Conn_StartTime(con))))
983 case 'm': /* IRC command status (usage count) */
985 cmd = Parse_GetCommandStruct();
986 for (; cmd->name; cmd++) {
987 if (cmd->lcount == 0 && cmd->rcount == 0)
989 if (!IRC_WriteStrClient
990 (from, RPL_STATSCOMMANDS_MSG, Client_ID(from),
991 cmd->name, cmd->lcount, cmd->bytes, cmd->rcount))
995 case 'u': /* Server uptime */
997 time_now = time(NULL) - NGIRCd_Start;
998 days = uptime_days(&time_now);
999 hrs = uptime_hrs(&time_now);
1000 mins = uptime_mins(&time_now);
1001 if (!IRC_WriteStrClient(from, RPL_STATSUPTIME, Client_ID(from),
1002 days, hrs, mins, (unsigned int)time_now))
1003 return DISCONNECTED;
1007 return IRC_WriteStrClient(from, RPL_ENDOFSTATS_MSG,
1008 Client_ID(from), query);
1012 * Handler for the IRC command "SUMMON".
1014 * @param Client The client from which this command has been received.
1015 * @param Req Request structure with prefix and all parameters.
1016 * @return CONNECTED or DISCONNECTED.
1019 IRC_SUMMON(CLIENT * Client, UNUSED REQUEST * Req)
1021 assert(Client != NULL);
1023 IRC_SetPenalty(Client, 1);
1025 return IRC_WriteStrClient(Client, ERR_SUMMONDISABLED_MSG,
1030 * Handler for the IRC command "TIME".
1032 * @param Client The client from which this command has been received.
1033 * @param Req Request structure with prefix and all parameters.
1034 * @return CONNECTED or DISCONNECTED.
1037 IRC_TIME( CLIENT *Client, REQUEST *Req )
1039 CLIENT *from, *target;
1043 assert(Client != NULL);
1044 assert(Req != NULL);
1046 IRC_SetPenalty(Client, 1);
1048 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
1049 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
1050 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
1053 if (target != Client_ThisServer()) {
1054 IRC_WriteStrClientPrefix(target, from, "TIME %s",
1060 (void)strftime(t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime(&t));
1061 return IRC_WriteStrClient(from, RPL_TIME_MSG, Client_ID(from),
1062 Client_ID(Client_ThisServer()), t_str);
1066 * Handler for the IRC command "USERHOST".
1068 * @param Client The client from which this command has been received.
1069 * @param Req Request structure with prefix and all parameters.
1070 * @return CONNECTED or DISCONNECTED.
1073 IRC_USERHOST(CLIENT *Client, REQUEST *Req)
1075 char rpl[COMMAND_LEN];
1079 assert(Client != NULL);
1080 assert(Req != NULL);
1082 IRC_SetPenalty(Client, 1);
1084 _IRC_ARGC_GE_OR_RETURN_(Client, Req, 1)
1091 strlcpy(rpl, RPL_USERHOST_MSG, sizeof rpl);
1092 for (i = 0; i < max; i++) {
1093 c = Client_Search(Req->argv[i]);
1094 if (c && (Client_Type(c) == CLIENT_USER)) {
1095 /* This Nick is "online" */
1096 strlcat(rpl, Client_ID(c), sizeof(rpl));
1097 if (Client_HasMode(c, 'o'))
1098 strlcat(rpl, "*", sizeof(rpl));
1099 strlcat(rpl, "=", sizeof(rpl));
1100 if (Client_HasMode(c, 'a'))
1101 strlcat(rpl, "-", sizeof(rpl));
1103 strlcat(rpl, "+", sizeof(rpl));
1104 strlcat(rpl, Client_User(c), sizeof(rpl));
1105 strlcat(rpl, "@", sizeof(rpl));
1106 strlcat(rpl, Client_HostnameDisplayed(c), sizeof(rpl));
1107 strlcat(rpl, " ", sizeof(rpl));
1110 ngt_TrimLastChr(rpl, ' ');
1112 return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
1113 } /* IRC_USERHOST */
1116 * Handler for the IRC command "USERS".
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_USERS(CLIENT * Client, UNUSED REQUEST * Req)
1125 assert(Client != NULL);
1127 IRC_SetPenalty(Client, 1);
1129 return IRC_WriteStrClient(Client, ERR_USERSDISABLED_MSG,
1134 * Handler for the IRC command "VERSION".
1136 * @param Client The client from which this command has been received.
1137 * @param Req Request structure with prefix and all parameters.
1138 * @return CONNECTED or DISCONNECTED.
1141 IRC_VERSION( CLIENT *Client, REQUEST *Req )
1143 CLIENT *target, *prefix;
1145 assert( Client != NULL );
1146 assert( Req != NULL );
1148 IRC_SetPenalty(Client, 1);
1150 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
1151 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
1152 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
1155 if (target != Client_ThisServer()) {
1156 IRC_WriteStrClientPrefix(target, prefix, "VERSION %s",
1161 /* send version information */
1162 return IRC_WriteStrClient(Client, RPL_VERSION_MSG, Client_ID(prefix),
1163 PACKAGE_NAME, PACKAGE_VERSION,
1164 NGIRCd_DebugLevel, Conf_ServerName,
1165 NGIRCd_VersionAddition);
1169 * Handler for the IRC "WHO" command.
1171 * @param Client The client from which this command has been received.
1172 * @param Req Request structure with prefix and all parameters.
1173 * @return CONNECTED or DISCONNECTED.
1176 IRC_WHO(CLIENT *Client, REQUEST *Req)
1181 assert (Client != NULL);
1182 assert (Req != NULL);
1184 IRC_SetPenalty(Client, 1);
1186 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
1189 if (Req->argc == 2) {
1190 if (strcmp(Req->argv[1], "o") == 0)
1194 return IRC_WriteStrClient(Client,
1195 ERR_NEEDMOREPARAMS_MSG,
1201 if (Req->argc >= 1) {
1202 /* Channel or mask given */
1203 chan = Channel_Search(Req->argv[0]);
1205 /* Members of a channel have been requested */
1206 return IRC_WHO_Channel(Client, chan, only_ops);
1208 if (strcmp(Req->argv[0], "0") != 0) {
1209 /* A mask has been given. But please note this RFC
1210 * stupidity: "0" is same as no arguments ... */
1211 return IRC_WHO_Mask(Client, Req->argv[0], only_ops);
1215 /* No channel or (valid) mask given */
1216 IRC_SetPenalty(Client, 2);
1217 return IRC_WHO_Mask(Client, NULL, only_ops);
1221 * Handler for the IRC "WHOIS" command.
1223 * @param Client The client from which this command has been received.
1224 * @param Req Request structure with prefix and all parameters.
1225 * @return CONNECTED or DISCONNECTED.
1228 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
1230 CLIENT *from, *target, *c;
1231 unsigned int match_count = 0, found = 0;
1232 bool has_wildcards, is_remote;
1233 bool got_wildcard = false;
1234 char mask[COMMAND_LEN], *query;
1236 assert( Client != NULL );
1237 assert( Req != NULL );
1239 IRC_SetPenalty(Client, 1);
1241 /* Bad number of parameters? */
1242 if (Req->argc < 1 || Req->argc > 2)
1243 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1244 Client_ID(Client), Req->command);
1246 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
1248 /* Get target server for this command */
1249 if (Req->argc > 1) {
1250 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, Client)
1252 target = Client_ThisServer();
1254 assert(target != NULL);
1257 if (target != Client_ThisServer()) {
1258 IRC_WriteStrClientPrefix(target, from, "WHOIS %s :%s",
1259 Req->argv[0], Req->argv[1]);
1263 is_remote = Client_Conn(from) < 0;
1264 strlcpy(mask, Req->argv[Req->argc - 1], sizeof(mask));
1265 for (query = strtok(ngt_LowerStr(mask), ",");
1267 query = strtok(NULL, ","), found++)
1269 has_wildcards = query[strcspn(query, "*?")] != 0;
1271 * follows ircd 2.10 implementation:
1272 * - handle up to 3 targets
1273 * - no wildcards for remote clients
1274 * - only one wildcard target per local client
1276 * Also, at most MAX_RPL_WHOIS matches are returned.
1278 if (!has_wildcards || is_remote) {
1279 c = Client_Search(query);
1280 if (c && (Client_Type(c) == CLIENT_USER
1281 || Client_Type(c) == CLIENT_SERVICE)) {
1282 if (!IRC_WHOIS_SendReply(Client, from, c))
1283 return DISCONNECTED;
1285 if (!IRC_WriteStrClient(Client,
1289 return DISCONNECTED;
1294 /* we already handled one wildcard query */
1295 if (!IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1296 Client_ID(Client), query))
1297 return DISCONNECTED;
1300 got_wildcard = true;
1301 IRC_SetPenalty(Client, 3);
1303 for (c = Client_First(); c; c = Client_Next(c)) {
1304 if (IRC_CheckListTooBig(Client, match_count,
1305 MAX_RPL_WHOIS, "WHOIS"))
1308 if (Client_Type(c) != CLIENT_USER)
1310 if (!MatchCaseInsensitive(query, Client_ID(c)))
1312 if (!IRC_WHOIS_SendReply(Client, from, c))
1313 return DISCONNECTED;
1318 if (match_count == 0)
1319 IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1321 Req->argv[Req->argc - 1]);
1323 return IRC_WriteStrClient(from, RPL_ENDOFWHOIS_MSG,
1324 Client_ID(from), Req->argv[Req->argc - 1]);
1328 * Handler for the IRC "WHOWAS" command.
1330 * @param Client The client from which this command has been received.
1331 * @param Req Request structure with prefix and all parameters.
1332 * @return CONNECTED or DISCONNECTED.
1335 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1337 CLIENT *target, *prefix;
1339 char tok_buf[COMMAND_LEN];
1340 int max, last, count, i, nc;
1343 assert( Client != NULL );
1344 assert( Req != NULL );
1346 /* Do not reveal any info on disconnected users? */
1347 if (Conf_MorePrivacy)
1350 /* Wrong number of parameters? */
1352 return IRC_WriteStrClient(Client, ERR_NONICKNAMEGIVEN_MSG,
1355 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 3)
1356 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
1357 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 2, prefix)
1360 if (target != Client_ThisServer()) {
1361 IRC_WriteStrClientPrefix(target, prefix, "WHOWAS %s %s %s",
1362 Req->argv[0], Req->argv[1],
1367 whowas = Client_GetWhowas( );
1368 last = Client_GetLastWhowasIndex( );
1372 max = DEF_RPL_WHOWAS;
1373 if (Req->argc > 1) {
1374 max = atoi(Req->argv[1]);
1376 max = MAX_RPL_WHOWAS;
1380 * Break up the nick argument into a list of nicks, if applicable
1381 * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1383 strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1384 nick = strtok(tok_buf, ",");
1386 for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1390 if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1391 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1392 return DISCONNECTED;
1396 /* previous entry */
1399 /* "underflow", wrap around */
1403 if (nc && count >= max)
1405 } while (i != last);
1407 if (nc == 0 && !IRC_WriteStrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1408 Client_ID(prefix), nick))
1409 return DISCONNECTED;
1411 return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG,
1412 Client_ID(prefix), Req->argv[0]);
1416 * Send LUSERS reply to a client.
1418 * @param Client The receipient of the information.
1419 * @return CONNECTED or DISCONNECTED.
1422 IRC_Send_LUSERS(CLIENT *Client)
1429 assert(Client != NULL);
1431 /* Users, services and serevers in the network */
1432 if (!IRC_WriteStrClient(Client, RPL_LUSERCLIENT_MSG, Client_ID(Client),
1433 Client_UserCount(), Client_ServiceCount(),
1434 Client_ServerCount()))
1435 return DISCONNECTED;
1437 /* Number of IRC operators */
1438 cnt = Client_OperCount( );
1440 if (!IRC_WriteStrClient(Client, RPL_LUSEROP_MSG,
1441 Client_ID(Client), cnt))
1442 return DISCONNECTED;
1445 /* Unknown connections */
1446 cnt = Client_UnknownCount( );
1448 if (!IRC_WriteStrClient(Client, RPL_LUSERUNKNOWN_MSG,
1449 Client_ID(Client), cnt))
1450 return DISCONNECTED;
1453 /* Number of created channels */
1454 if (!IRC_WriteStrClient(Client, RPL_LUSERCHANNELS_MSG,
1456 Channel_CountVisible(Client)))
1457 return DISCONNECTED;
1459 /* Number of local users, services and servers */
1460 if (!IRC_WriteStrClient(Client, RPL_LUSERME_MSG, Client_ID(Client),
1461 Client_MyUserCount(), Client_MyServiceCount(),
1462 Client_MyServerCount()))
1463 return DISCONNECTED;
1466 /* Maximum number of local users */
1467 cnt = Client_MyUserCount();
1468 max = Client_MyMaxUserCount();
1469 if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1470 cnt, max, cnt, max))
1471 return DISCONNECTED;
1472 /* Maximum number of users in the network */
1473 cnt = Client_UserCount();
1474 max = Client_MaxUserCount();
1475 if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1476 cnt, max, cnt, max))
1477 return DISCONNECTED;
1478 /* Connection counters */
1479 if (! IRC_WriteStrClient(Client, RPL_STATSCONN_MSG, Client_ID(Client),
1480 Conn_CountMax(), Conn_CountAccepted()))
1481 return DISCONNECTED;
1485 } /* IRC_Send_LUSERS */
1488 IRC_Show_MOTD( CLIENT *Client )
1491 size_t len_tot, len_str;
1493 assert( Client != NULL );
1495 len_tot = array_bytes(&Conf_Motd);
1496 if (len_tot == 0 && !Conn_UsesSSL(Client_Conn(Client)))
1497 return IRC_WriteStrClient(Client, ERR_NOMOTD_MSG, Client_ID(Client));
1499 if (!Show_MOTD_Start(Client))
1500 return DISCONNECTED;
1502 line = array_start(&Conf_Motd);
1503 while (len_tot > 0) {
1504 len_str = strlen(line) + 1;
1506 assert(len_tot >= len_str);
1509 if (!Show_MOTD_Sendline(Client, line))
1510 return DISCONNECTED;
1514 if (!Show_MOTD_SSLInfo(Client))
1515 return DISCONNECTED;
1516 return Show_MOTD_End(Client);
1517 } /* IRC_Show_MOTD */
1520 * Send NAMES reply for a specific client and channel.
1522 * @param Client The client requesting the NAMES information.
1523 * @param Chan The channel
1524 * @return CONNECTED or DISCONNECTED.
1527 IRC_Send_NAMES(CLIENT * Client, CHANNEL * Chan)
1529 bool is_visible, is_member;
1530 char str[COMMAND_LEN];
1534 assert(Client != NULL);
1535 assert(Chan != NULL);
1537 if (Channel_IsMemberOf(Chan, Client))
1542 /* Do not print info on channel memberships to anyone that is not member? */
1543 if (Conf_MorePrivacy && !is_member)
1546 /* Secret channel? */
1547 if (!is_member && strchr(Channel_Modes(Chan), 's'))
1550 snprintf(str, sizeof(str), RPL_NAMREPLY_MSG, Client_ID(Client), "=",
1551 Channel_Name(Chan));
1552 cl2chan = Channel_FirstMember(Chan);
1554 cl = Channel_GetClient(cl2chan);
1556 if (strchr(Client_Modes(cl), 'i'))
1561 if (is_member || is_visible) {
1562 if (str[strlen(str) - 1] != ':')
1563 strlcat(str, " ", sizeof(str));
1565 who_flags_qualifier(Client, Channel_UserModes(Chan, cl),
1567 strlcat(str, Client_ID(cl), sizeof(str));
1569 if (strlen(str) > (COMMAND_LEN - CLIENT_NICK_LEN - 4)) {
1570 if (!IRC_WriteStrClient(Client, "%s", str))
1571 return DISCONNECTED;
1572 snprintf(str, sizeof(str), RPL_NAMREPLY_MSG,
1573 Client_ID(Client), "=",
1574 Channel_Name(Chan));
1578 cl2chan = Channel_NextMember(Chan, cl2chan);
1580 if (str[strlen(str) - 1] != ':') {
1581 if (!IRC_WriteStrClient(Client, "%s", str))
1582 return DISCONNECTED;
1586 } /* IRC_Send_NAMES */
1589 * Send the ISUPPORT numeric (005).
1590 * This numeric indicates the features that are supported by this server.
1591 * See <http://www.irc.org/tech_docs/005.html> for details.
1594 IRC_Send_ISUPPORT(CLIENT * Client)
1596 if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1598 return DISCONNECTED;
1599 return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1600 CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1601 COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1602 COMMAND_LEN - 113, MAX_HNDL_MODES_ARG,
1603 MAX_HNDL_CHANNEL_LISTS);
1604 } /* IRC_Send_ISUPPORT */