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 * Return channel user mode prefix(es).
93 * @param Client The client requesting the mode prefixes.
94 * @param chan_user_modes String with channel user modes.
95 * @param str String buffer to which the prefix(es) will be appended.
96 * @param len Size of "str" buffer.
97 * @return Pointer to "str".
100 who_flags_qualifier(CLIENT *Client, const char *chan_user_modes,
101 char *str, size_t len)
103 assert(Client != NULL);
105 if (Client_Cap(Client) & CLIENT_CAP_MULTI_PREFIX) {
106 if (strchr(chan_user_modes, 'q'))
107 strlcat(str, "~", len);
108 if (strchr(chan_user_modes, 'a'))
109 strlcat(str, "&", len);
110 if (strchr(chan_user_modes, 'o'))
111 strlcat(str, "@", len);
112 if (strchr(chan_user_modes, 'h'))
113 strlcat(str, "%", len);
114 if (strchr(chan_user_modes, 'v'))
115 strlcat(str, "+", len);
120 if (strchr(chan_user_modes, 'q'))
121 strlcat(str, "~", len);
122 else if (strchr(chan_user_modes, 'a'))
123 strlcat(str, "&", len);
124 else if (strchr(chan_user_modes, 'o'))
125 strlcat(str, "@", len);
126 else if (strchr(chan_user_modes, 'h'))
127 strlcat(str, "%", len);
128 else if (strchr(chan_user_modes, 'v'))
129 strlcat(str, "+", len);
135 * Send WHO reply for a "channel target" ("WHO #channel").
137 * @param Client Client requesting the information.
138 * @param Chan Channel being requested.
139 * @param OnlyOps Only display IRC operators.
140 * @return CONNECTED or DISCONNECTED.
143 IRC_WHO_Channel(CLIENT *Client, CHANNEL *Chan, bool OnlyOps)
145 bool is_visible, is_member, is_ircop;
151 assert( Client != NULL );
152 assert( Chan != NULL );
154 IRC_SetPenalty(Client, 1);
156 is_member = Channel_IsMemberOf(Chan, Client);
158 /* Secret channel? */
159 if (!is_member && Channel_HasMode(Chan, 's'))
160 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG,
161 Client_ID(Client), Channel_Name(Chan));
163 cl2chan = Channel_FirstMember(Chan);
164 for (; cl2chan ; cl2chan = Channel_NextMember(Chan, cl2chan)) {
165 c = Channel_GetClient(cl2chan);
167 is_ircop = Client_HasMode(c, 'o');
168 if (OnlyOps && !is_ircop)
171 is_visible = Client_HasMode(c, 'i');
172 if (is_member || is_visible) {
173 memset(flags, 0, sizeof(flags));
175 if (Client_HasMode(c, 'a'))
176 flags[0] = 'G'; /* away */
183 who_flags_qualifier(Client, Channel_UserModes(Chan, c),
184 flags, sizeof(flags));
186 if (!write_whoreply(Client, c, Channel_Name(Chan),
193 /* If there are a lot of clients, augment penalty a bit */
194 if (count > MAX_RPL_WHO)
195 IRC_SetPenalty(Client, 1);
197 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client),
202 * Send WHO reply for a "mask target" ("WHO m*sk").
204 * @param Client Client requesting the information.
205 * @param Mask Mask being requested or NULL for "all" clients.
206 * @param OnlyOps Only display IRC operators.
207 * @return CONNECTED or DISCONNECTED.
210 IRC_WHO_Mask(CLIENT *Client, char *Mask, bool OnlyOps)
215 bool client_match, is_visible;
219 assert (Client != NULL);
224 IRC_SetPenalty(Client, 3);
225 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
226 if (Client_Type(c) != CLIENT_USER)
229 if (OnlyOps && !Client_HasMode(c, 'o'))
233 /* Match pattern against user host/server/name/nick */
234 client_match = MatchCaseInsensitive(Mask,
237 client_match = MatchCaseInsensitive(Mask,
238 Client_ID(Client_Introducer(c)));
240 client_match = MatchCaseInsensitive(Mask,
243 client_match = MatchCaseInsensitive(Mask,
246 continue; /* no match: skip this client */
249 is_visible = !Client_HasMode(c, 'i');
251 /* Target client is invisible, but mask matches exactly? */
252 if (!is_visible && Mask && strcasecmp(Client_ID(c), Mask) == 0)
255 /* Target still invisible, but are both on the same channel? */
257 cl2chan = Channel_FirstChannelOf(Client);
258 while (cl2chan && !is_visible) {
259 chan = Channel_GetChannel(cl2chan);
260 if (Channel_IsMemberOf(chan, c))
262 cl2chan = Channel_NextChannelOf(Client, cl2chan);
266 if (!is_visible) /* target user is not visible */
269 if (IRC_CheckListTooBig(Client, count, MAX_RPL_WHO, "WHO"))
272 memset(flags, 0, sizeof(flags));
274 if (Client_HasMode(c, 'a'))
275 flags[0] = 'G'; /* away */
279 if (Client_HasMode(c, 'o'))
282 if (!write_whoreply(Client, c, "*", flags))
287 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client),
292 * Generate WHOIS reply of one actual client.
294 * @param Client The client from which this command has been received.
295 * @param from The client requesting the information ("originator").
296 * @param c The client of which information should be returned.
297 * @return CONNECTED or DISCONNECTED.
300 IRC_WHOIS_SendReply(CLIENT *Client, CLIENT *from, CLIENT *c)
302 char str[COMMAND_LEN];
306 assert(Client != NULL);
307 assert(from != NULL);
310 /* Nick, user, hostname and client info */
311 if (!IRC_WriteStrClient(from, RPL_WHOISUSER_MSG, Client_ID(from),
312 Client_ID(c), Client_User(c),
313 Client_HostnameDisplayed(c), Client_Info(c)))
317 if (!IRC_WriteStrClient(from, RPL_WHOISSERVER_MSG, Client_ID(from),
318 Client_ID(c), Client_ID(Client_Introducer(c)),
319 Client_Info(Client_Introducer(c))))
323 snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
324 Client_ID(from), Client_ID(c));
325 cl2chan = Channel_FirstChannelOf(c);
327 chan = Channel_GetChannel(cl2chan);
328 assert(chan != NULL);
331 cl2chan = Channel_NextChannelOf(c, cl2chan);
333 /* Secret channel? */
334 if (Channel_HasMode(chan, 's')
335 && !Channel_IsMemberOf(chan, Client))
338 /* Local channel and request is not from a user? */
339 if (Client_Type(Client) == CLIENT_SERVER
340 && Channel_IsLocal(chan))
343 /* Concatenate channel names */
344 if (str[strlen(str) - 1] != ':')
345 strlcat(str, " ", sizeof(str));
347 who_flags_qualifier(Client, Channel_UserModes(chan, c),
349 strlcat(str, Channel_Name(chan), sizeof(str));
351 if (strlen(str) > (COMMAND_LEN - CHANNEL_NAME_LEN - 4)) {
352 /* Line becomes too long: send it! */
353 if (!IRC_WriteStrClient(Client, "%s", str))
355 snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
356 Client_ID(from), Client_ID(c));
359 if(str[strlen(str) - 1] != ':') {
360 /* There is data left to send: */
361 if (!IRC_WriteStrClient(Client, "%s", str))
366 if (Client_Type(c) == CLIENT_SERVICE &&
367 !IRC_WriteStrClient(from, RPL_WHOISSERVICE_MSG,
368 Client_ID(from), Client_ID(c)))
372 if (Client_Type(c) != CLIENT_SERVICE &&
373 Client_HasMode(c, 'o') &&
374 !IRC_WriteStrClient(from, RPL_WHOISOPERATOR_MSG,
375 Client_ID(from), Client_ID(c)))
379 if (Client_HasMode(c, 'B') &&
380 !IRC_WriteStrClient(from, RPL_WHOISBOT_MSG,
381 Client_ID(from), Client_ID(c)))
384 /* Connected using SSL? */
385 if (Conn_UsesSSL(Client_Conn(c))) {
386 if (!IRC_WriteStrClient(from, RPL_WHOISSSL_MSG, Client_ID(from),
390 /* Certificate fingerprint? */
391 if (Conn_GetCertFp(Client_Conn(c)) &&
393 !IRC_WriteStrClient(from, RPL_WHOISCERTFP_MSG,
394 Client_ID(from), Client_ID(c),
395 Conn_GetCertFp(Client_Conn(c))))
399 /* Registered nickname? */
400 if (Client_HasMode(c, 'R') &&
401 !IRC_WriteStrClient(from, RPL_WHOISREGNICK_MSG,
402 Client_ID(from), Client_ID(c)))
405 /* Local client and requester is the user itself or an IRC Op? */
406 if (Client_Conn(c) > NONE &&
407 (from == c || (!Conf_MorePrivacy && Client_HasMode(from, 'o')))) {
408 /* Client hostname */
409 if (!IRC_WriteStrClient(from, RPL_WHOISHOST_MSG,
410 Client_ID(from), Client_ID(c),
411 Client_Hostname(c), Client_IPAText(c)))
414 if (!IRC_WriteStrClient(from, RPL_WHOISMODES_MSG,
415 Client_ID(from), Client_ID(c), Client_Modes(c)))
419 /* Idle and signon time (local clients only!) */
420 if (!Conf_MorePrivacy && Client_Conn(c) > NONE &&
421 !IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
422 Client_ID(from), Client_ID(c),
423 (unsigned long)Conn_GetIdle(Client_Conn(c)),
424 (unsigned long)Conn_GetSignon(Client_Conn(c))))
428 if (Client_HasMode(c, 'a') &&
429 !IRC_WriteStrClient(from, RPL_AWAY_MSG,
430 Client_ID(from), Client_ID(c), Client_Away(c)))
437 WHOWAS_EntryWrite(CLIENT *prefix, WHOWAS *entry)
441 (void)strftime(t_str, sizeof(t_str), "%a %b %d %H:%M:%S %Y",
442 localtime(&entry->time));
444 if (!IRC_WriteStrClient(prefix, RPL_WHOWASUSER_MSG, Client_ID(prefix),
445 entry->id, entry->user, entry->host, entry->info))
448 return IRC_WriteStrClient(prefix, RPL_WHOISSERVER_MSG, Client_ID(prefix),
449 entry->id, entry->server, t_str);
454 Show_MOTD_SSLInfo(CLIENT *Client)
456 char buf[COMMAND_LEN];
459 if (Conn_GetCipherInfo(Client_Conn(Client), c_str, sizeof(c_str))) {
460 snprintf(buf, sizeof(buf), "Connected using Cipher %s", c_str);
461 if (!IRC_WriteStrClient(Client, RPL_MOTD_MSG,
462 Client_ID(Client), buf))
466 if (Conn_GetCertFp(Client_Conn(Client))) {
467 snprintf(buf, sizeof(buf),
468 "Your client certificate fingerprint is: %s",
469 Conn_GetCertFp(Client_Conn(Client)));
470 if (!IRC_WriteStrClient(Client, RPL_MOTD_MSG,
471 Client_ID(Client), buf))
479 Show_MOTD_SSLInfo(UNUSED CLIENT *c)
485 /* Global functions */
488 * Handler for the IRC command "ADMIN".
490 * @param Client The client from which this command has been received.
491 * @param Req Request structure with prefix and all parameters.
492 * @return CONNECTED or DISCONNECTED.
495 IRC_ADMIN(CLIENT *Client, REQUEST *Req )
497 CLIENT *target, *prefix;
499 assert( Client != NULL );
500 assert( Req != NULL );
502 IRC_SetPenalty(Client, 1);
504 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
505 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
506 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
509 if(target != Client_ThisServer()) {
510 IRC_WriteStrClientPrefix(target, prefix,
511 "ADMIN %s", Client_ID(target));
515 if (!IRC_WriteStrClient(Client, RPL_ADMINME_MSG, Client_ID(prefix),
518 if (!IRC_WriteStrClient(Client, RPL_ADMINLOC1_MSG, Client_ID(prefix),
521 if (!IRC_WriteStrClient(Client, RPL_ADMINLOC2_MSG, Client_ID(prefix),
524 if (!IRC_WriteStrClient(Client, RPL_ADMINEMAIL_MSG, Client_ID(prefix),
525 Conf_ServerAdminMail))
532 * Handler for the IRC command "INFO".
534 * @param Client The client from which this command has been received.
535 * @param Req Request structure with prefix and all parameters.
536 * @return CONNECTED or DISCONNECTED.
539 IRC_INFO(CLIENT * Client, REQUEST * Req)
541 CLIENT *target, *prefix;
544 assert(Client != NULL);
547 IRC_SetPenalty(Client, 2);
549 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
550 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
551 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
554 if (target != Client_ThisServer()) {
555 IRC_WriteStrClientPrefix(target, prefix, "INFO %s",
560 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix),
564 #if defined(__DATE__) && defined(__TIME__)
565 snprintf(msg, sizeof(msg), "Birth Date: %s at %s", __DATE__, __TIME__);
566 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
570 strlcpy(msg, "On-line since ", sizeof(msg));
571 strlcat(msg, NGIRCd_StartStr, sizeof(msg));
572 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
575 if (!IRC_WriteStrClient(Client, RPL_ENDOFINFO_MSG, Client_ID(prefix)))
582 * Handler for the IRC "ISON" command.
584 * @param Client The client from which this command has been received.
585 * @param Req Request structure with prefix and all parameters.
586 * @return CONNECTED or DISCONNECTED.
589 IRC_ISON( CLIENT *Client, REQUEST *Req )
591 char rpl[COMMAND_LEN];
596 assert(Client != NULL);
599 _IRC_ARGC_GE_OR_RETURN_(Client, Req, 1)
601 strlcpy(rpl, RPL_ISON_MSG, sizeof rpl);
602 for (i = 0; i < Req->argc; i++) {
603 /* "All" ircd even parse ":<x> <y> ..." arguments and split
604 * them up; so we do the same ... */
605 ptr = strtok(Req->argv[i], " ");
608 c = Client_Search(ptr);
609 if (c && Client_Type(c) == CLIENT_USER) {
610 strlcat(rpl, Client_ID(c), sizeof(rpl));
611 strlcat(rpl, " ", sizeof(rpl));
613 ptr = strtok(NULL, " ");
616 ngt_TrimLastChr(rpl, ' ');
618 return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
622 * Handler for the IRC "LINKS" command.
624 * @param Client The client from which this command has been received.
625 * @param Req Request structure with prefix and all parameters.
626 * @return CONNECTED or DISCONNECTED.
629 IRC_LINKS(CLIENT *Client, REQUEST *Req)
631 CLIENT *target, *from, *c;
634 assert(Client != NULL);
637 IRC_SetPenalty(Client, 1);
639 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
640 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
642 /* Get pointer to server mask or "*", if none given */
644 mask = Req->argv[Req->argc - 1];
649 if (Req->argc == 2) {
650 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
651 if (target != Client_ThisServer()) {
652 IRC_WriteStrClientPrefix(target, from,
653 "LINKS %s %s", Client_ID(target),
661 if (Client_Type(c) == CLIENT_SERVER
662 && MatchCaseInsensitive(mask, Client_ID(c))) {
663 if (!IRC_WriteStrClient(from, RPL_LINKS_MSG,
664 Client_ID(from), Client_ID(c),
665 Client_ID(Client_TopServer(c)
666 ? Client_TopServer(c)
667 : Client_ThisServer()),
668 Client_Hops(c), Client_Info(c)))
673 return IRC_WriteStrClient(from, RPL_ENDOFLINKS_MSG,
674 Client_ID(from), mask);
678 * Handler for the IRC "LUSERS" command.
680 * @param Client The client from which this command has been received.
681 * @param Req Request structure with prefix and all parameters.
682 * @return CONNECTED or DISCONNECTED.
685 IRC_LUSERS( CLIENT *Client, REQUEST *Req )
687 CLIENT *target, *from;
689 assert( Client != NULL );
690 assert( Req != NULL );
692 IRC_SetPenalty(Client, 1);
694 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
695 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
696 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
699 if (target != Client_ThisServer()) {
700 IRC_WriteStrClientPrefix(target, from,
701 "LUSERS %s %s", Req->argv[0],
706 return IRC_Send_LUSERS(from);
710 * Handler for the IRC command "SERVLIST".
712 * @param Client The client from which this command has been received.
713 * @param Req Request structure with prefix and all parameters.
714 * @return CONNECTED or DISCONNECTED.
717 IRC_SERVLIST(CLIENT *Client, REQUEST *Req)
721 IRC_SetPenalty(Client, 1);
723 assert(Client != NULL);
726 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
728 if (Req->argc < 2 || strcmp(Req->argv[1], "0") == 0) {
729 for (c = Client_First(); c!= NULL; c = Client_Next(c)) {
730 if (Client_Type(c) != CLIENT_SERVICE)
732 if (Req->argc > 0 && !MatchCaseInsensitive(Req->argv[0],
735 if (!IRC_WriteStrClient(Client, RPL_SERVLIST_MSG,
736 Client_ID(Client), Client_Mask(c),
737 Client_Mask(Client_Introducer(c)), "*",
738 0, Client_Hops(c), Client_Info(c)))
743 return IRC_WriteStrClient(Client, RPL_SERVLISTEND_MSG, Client_ID(Client),
744 Req->argc > 0 ? Req->argv[0] : "*",
745 Req->argc > 1 ? Req->argv[1] : "0");
749 * Handler for the IRC command "MOTD".
751 * @param Client The client from which this command has been received.
752 * @param Req Request structure with prefix and all parameters.
753 * @return CONNECTED or DISCONNECTED.
756 IRC_MOTD( CLIENT *Client, REQUEST *Req )
758 CLIENT *from, *target;
760 assert( Client != NULL );
761 assert( Req != NULL );
763 IRC_SetPenalty(Client, 3);
765 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
766 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
767 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
770 if (target != Client_ThisServer()) {
771 IRC_WriteStrClientPrefix(target, from, "MOTD %s",
776 return IRC_Show_MOTD(from);
780 * Handler for the IRC command "NAMES".
782 * @param Client The client from which this command has been received.
783 * @param Req Request structure with prefix and all parameters.
784 * @return CONNECTED or DISCONNECTED.
787 IRC_NAMES( CLIENT *Client, REQUEST *Req )
789 char rpl[COMMAND_LEN], *ptr;
790 CLIENT *target, *from, *c;
793 assert( Client != NULL );
794 assert( Req != NULL );
796 IRC_SetPenalty(Client, 1);
798 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
799 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
800 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
803 if (target != Client_ThisServer()) {
804 IRC_WriteStrClientPrefix(target, from, "NAMES %s :%s",
805 Req->argv[0], Client_ID(target));
810 /* Return NAMES list for specific channels */
811 ptr = strtok(Req->argv[0], ",");
813 chan = Channel_Search(ptr);
814 if (chan && !IRC_Send_NAMES(from, chan))
816 if (!IRC_WriteStrClient(from, RPL_ENDOFNAMES_MSG,
817 Client_ID(from), ptr))
819 ptr = strtok( NULL, "," );
824 chan = Channel_First();
826 if (!IRC_Send_NAMES(from, chan))
828 chan = Channel_Next(chan);
831 /* Now print all clients which are not in any channel */
833 snprintf(rpl, sizeof(rpl), RPL_NAMREPLY_MSG, Client_ID(from), "*", "*");
835 if (Client_Type(c) == CLIENT_USER
836 && Channel_FirstChannelOf(c) == NULL
837 && !Client_HasMode(c, 'i'))
839 /* its a user, concatenate ... */
840 if (rpl[strlen(rpl) - 1] != ':')
841 strlcat(rpl, " ", sizeof(rpl));
842 strlcat(rpl, Client_ID(c), sizeof(rpl));
844 if (strlen(rpl) > COMMAND_LEN - CLIENT_NICK_LEN - 4) {
845 /* Line is gwoing too long, send now */
846 if (!IRC_WriteStrClient(from, "%s", rpl))
848 snprintf(rpl, sizeof(rpl), RPL_NAMREPLY_MSG,
849 Client_ID(from), "*", "*");
854 if (rpl[strlen(rpl) - 1] != ':' && !IRC_WriteStrClient(from, "%s", rpl))
857 return IRC_WriteStrClient(from, RPL_ENDOFNAMES_MSG, Client_ID(from), "*");
861 * Handler for the IRC command "STATS".
863 * @param Client The client from which this command has been received.
864 * @param Req Request structure with prefix and all parameters.
865 * @return CONNECTED or DISCONNECTED.
868 IRC_STATS( CLIENT *Client, REQUEST *Req )
870 CLIENT *from, *target, *cl;
875 unsigned int days, hrs, mins;
876 struct list_head *list;
877 struct list_elem *list_item;
879 assert(Client != NULL);
882 IRC_SetPenalty(Client, 2);
884 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
885 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
886 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
889 if (target != Client_ThisServer()) {
890 IRC_WriteStrClientPrefix(target, from, "STATS %s %s",
891 Req->argv[0], Client_ID(target));
896 query = Req->argv[0][0] ? Req->argv[0][0] : '*';
901 case 'g': /* Network-wide bans ("G-Lines") */
903 case 'k': /* Server-local bans ("K-Lines") */
905 if (!Client_HasMode(from, 'o'))
906 return IRC_WriteErrClient(from, ERR_NOPRIVILEGES_MSG,
908 if (query == 'g' || query == 'G')
909 list = Class_GetList(CLASS_GLINE);
911 list = Class_GetList(CLASS_KLINE);
912 list_item = Lists_GetFirst(list);
914 if (!IRC_WriteStrClient(from, RPL_STATSXLINE_MSG,
915 Client_ID(from), query,
916 Lists_GetMask(list_item),
917 Lists_GetValidity(list_item),
918 Lists_GetReason(list_item)))
920 list_item = Lists_GetNext(list_item);
923 case 'l': /* Link status (servers and own link) */
925 time_now = time(NULL);
926 for (con = Conn_First(); con != NONE; con = Conn_Next(con)) {
927 cl = Conn_GetClient(con);
930 if ((Client_Type(cl) == CLIENT_SERVER)
932 /* Server link or our own connection */
934 if (Conn_Options(con) & CONN_ZIP) {
935 if (!IRC_WriteStrClient
936 (from, RPL_STATSLINKINFOZIP_MSG,
937 Client_ID(from), Client_Mask(cl),
938 Conn_SendQ(con), Conn_SendMsg(con),
944 (long)(time_now - Conn_StartTime(con))))
949 if (!IRC_WriteStrClient
950 (from, RPL_STATSLINKINFO_MSG,
951 Client_ID(from), Client_Mask(cl),
952 Conn_SendQ(con), Conn_SendMsg(con),
953 Conn_SendBytes(con), Conn_RecvMsg(con),
955 (long)(time_now - Conn_StartTime(con))))
960 case 'm': /* IRC command status (usage count) */
962 cmd = Parse_GetCommandStruct();
963 for (; cmd->name; cmd++) {
964 if (cmd->lcount == 0 && cmd->rcount == 0)
966 if (!IRC_WriteStrClient
967 (from, RPL_STATSCOMMANDS_MSG, Client_ID(from),
968 cmd->name, cmd->lcount, cmd->bytes, cmd->rcount))
972 case 'u': /* Server uptime */
974 time_now = time(NULL) - NGIRCd_Start;
975 days = uptime_days(&time_now);
976 hrs = uptime_hrs(&time_now);
977 mins = uptime_mins(&time_now);
978 if (!IRC_WriteStrClient(from, RPL_STATSUPTIME, Client_ID(from),
979 days, hrs, mins, (unsigned int)time_now))
984 return IRC_WriteStrClient(from, RPL_ENDOFSTATS_MSG,
985 Client_ID(from), query);
989 * Handler for the IRC command "SUMMON".
991 * @param Client The client from which this command has been received.
992 * @param Req Request structure with prefix and all parameters.
993 * @return CONNECTED or DISCONNECTED.
996 IRC_SUMMON(CLIENT * Client, UNUSED REQUEST * Req)
998 assert(Client != NULL);
1000 return IRC_WriteErrClient(Client, ERR_SUMMONDISABLED_MSG,
1005 * Handler for the IRC command "TIME".
1007 * @param Client The client from which this command has been received.
1008 * @param Req Request structure with prefix and all parameters.
1009 * @return CONNECTED or DISCONNECTED.
1012 IRC_TIME( CLIENT *Client, REQUEST *Req )
1014 CLIENT *from, *target;
1018 assert(Client != NULL);
1019 assert(Req != NULL);
1021 IRC_SetPenalty(Client, 1);
1023 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
1024 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
1025 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
1028 if (target != Client_ThisServer()) {
1029 IRC_WriteStrClientPrefix(target, from, "TIME %s",
1035 (void)strftime(t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime(&t));
1036 return IRC_WriteStrClient(from, RPL_TIME_MSG, Client_ID(from),
1037 Client_ID(Client_ThisServer()), t_str);
1041 * Handler for the IRC command "USERHOST".
1043 * @param Client The client from which this command has been received.
1044 * @param Req Request structure with prefix and all parameters.
1045 * @return CONNECTED or DISCONNECTED.
1048 IRC_USERHOST(CLIENT *Client, REQUEST *Req)
1050 char rpl[COMMAND_LEN];
1054 assert(Client != NULL);
1055 assert(Req != NULL);
1057 IRC_SetPenalty(Client, 1);
1059 _IRC_ARGC_GE_OR_RETURN_(Client, Req, 1)
1066 strlcpy(rpl, RPL_USERHOST_MSG, sizeof rpl);
1067 for (i = 0; i < max; i++) {
1068 c = Client_Search(Req->argv[i]);
1069 if (c && (Client_Type(c) == CLIENT_USER)) {
1070 /* This Nick is "online" */
1071 strlcat(rpl, Client_ID(c), sizeof(rpl));
1072 if (Client_HasMode(c, 'o'))
1073 strlcat(rpl, "*", sizeof(rpl));
1074 strlcat(rpl, "=", sizeof(rpl));
1075 if (Client_HasMode(c, 'a'))
1076 strlcat(rpl, "-", sizeof(rpl));
1078 strlcat(rpl, "+", sizeof(rpl));
1079 strlcat(rpl, Client_User(c), sizeof(rpl));
1080 strlcat(rpl, "@", sizeof(rpl));
1081 strlcat(rpl, Client_HostnameDisplayed(c), sizeof(rpl));
1082 strlcat(rpl, " ", sizeof(rpl));
1085 ngt_TrimLastChr(rpl, ' ');
1087 return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
1088 } /* IRC_USERHOST */
1091 * Handler for the IRC command "USERS".
1093 * @param Client The client from which this command has been received.
1094 * @param Req Request structure with prefix and all parameters.
1095 * @return CONNECTED or DISCONNECTED.
1098 IRC_USERS(CLIENT * Client, UNUSED REQUEST * Req)
1100 assert(Client != NULL);
1102 return IRC_WriteErrClient(Client, ERR_USERSDISABLED_MSG,
1107 * Handler for the IRC command "VERSION".
1109 * @param Client The client from which this command has been received.
1110 * @param Req Request structure with prefix and all parameters.
1111 * @return CONNECTED or DISCONNECTED.
1114 IRC_VERSION( CLIENT *Client, REQUEST *Req )
1116 CLIENT *target, *prefix;
1118 assert( Client != NULL );
1119 assert( Req != NULL );
1121 IRC_SetPenalty(Client, 1);
1123 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
1124 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
1125 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
1128 if (target != Client_ThisServer()) {
1129 IRC_WriteStrClientPrefix(target, prefix, "VERSION %s",
1134 /* send version information */
1135 return IRC_WriteStrClient(Client, RPL_VERSION_MSG, Client_ID(prefix),
1136 PACKAGE_NAME, PACKAGE_VERSION,
1137 NGIRCd_DebugLevel, Conf_ServerName,
1138 NGIRCd_VersionAddition);
1142 * Handler for the IRC "WHO" command.
1144 * @param Client The client from which this command has been received.
1145 * @param Req Request structure with prefix and all parameters.
1146 * @return CONNECTED or DISCONNECTED.
1149 IRC_WHO(CLIENT *Client, REQUEST *Req)
1154 assert (Client != NULL);
1155 assert (Req != NULL);
1157 IRC_SetPenalty(Client, 1);
1159 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
1162 if (Req->argc == 2) {
1163 if (strcmp(Req->argv[1], "o") == 0)
1167 return IRC_WriteErrClient(Client,
1168 ERR_NEEDMOREPARAMS_MSG,
1174 if (Req->argc >= 1) {
1175 /* Channel or mask given */
1176 chan = Channel_Search(Req->argv[0]);
1178 /* Members of a channel have been requested */
1179 return IRC_WHO_Channel(Client, chan, only_ops);
1181 if (strcmp(Req->argv[0], "0") != 0) {
1182 /* A mask has been given. But please note this RFC
1183 * stupidity: "0" is same as no arguments ... */
1184 return IRC_WHO_Mask(Client, Req->argv[0], only_ops);
1188 /* No channel or (valid) mask given */
1189 IRC_SetPenalty(Client, 2);
1190 return IRC_WHO_Mask(Client, NULL, only_ops);
1194 * Handler for the IRC "WHOIS" command.
1196 * @param Client The client from which this command has been received.
1197 * @param Req Request structure with prefix and all parameters.
1198 * @return CONNECTED or DISCONNECTED.
1201 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
1203 CLIENT *from, *target, *c;
1204 unsigned int match_count = 0, found = 0;
1205 bool has_wildcards, is_remote;
1206 bool got_wildcard = false;
1207 char mask[COMMAND_LEN], *query;
1209 assert( Client != NULL );
1210 assert( Req != NULL );
1212 IRC_SetPenalty(Client, 1);
1214 /* Bad number of parameters? */
1215 if (Req->argc < 1 || Req->argc > 2)
1216 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1217 Client_ID(Client), Req->command);
1219 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
1221 /* Get target server for this command */
1222 if (Req->argc > 1) {
1223 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, Client)
1225 target = Client_ThisServer();
1227 assert(target != NULL);
1230 if (target != Client_ThisServer()) {
1231 IRC_WriteStrClientPrefix(target, from, "WHOIS %s :%s",
1232 Req->argv[0], Req->argv[1]);
1236 is_remote = Client_Conn(from) < 0;
1237 strlcpy(mask, Req->argv[Req->argc - 1], sizeof(mask));
1238 for (query = strtok(ngt_LowerStr(mask), ",");
1240 query = strtok(NULL, ","), found++)
1242 has_wildcards = query[strcspn(query, "*?")] != 0;
1244 * follows ircd 2.10 implementation:
1245 * - handle up to 3 targets
1246 * - no wildcards for remote clients
1247 * - only one wildcard target per local client
1249 * Also, at most MAX_RPL_WHOIS matches are returned.
1251 if (!has_wildcards || is_remote) {
1252 c = Client_Search(query);
1253 if (c && (Client_Type(c) == CLIENT_USER
1254 || Client_Type(c) == CLIENT_SERVICE)) {
1255 if (!IRC_WHOIS_SendReply(Client, from, c))
1256 return DISCONNECTED;
1258 if (!IRC_WriteErrClient(Client,
1262 return DISCONNECTED;
1267 /* we already handled one wildcard query */
1268 if (!IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
1269 Client_ID(Client), query))
1270 return DISCONNECTED;
1273 got_wildcard = true;
1274 IRC_SetPenalty(Client, 3);
1276 for (c = Client_First(); c; c = Client_Next(c)) {
1277 if (IRC_CheckListTooBig(Client, match_count,
1278 MAX_RPL_WHOIS, "WHOIS"))
1281 if (Client_Type(c) != CLIENT_USER)
1283 if (!MatchCaseInsensitive(query, Client_ID(c)))
1285 if (!IRC_WHOIS_SendReply(Client, from, c))
1286 return DISCONNECTED;
1291 if (match_count == 0)
1292 IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
1294 Req->argv[Req->argc - 1]);
1296 return IRC_WriteStrClient(from, RPL_ENDOFWHOIS_MSG,
1297 Client_ID(from), Req->argv[Req->argc - 1]);
1301 * Handler for the IRC "WHOWAS" command.
1303 * @param Client The client from which this command has been received.
1304 * @param Req Request structure with prefix and all parameters.
1305 * @return CONNECTED or DISCONNECTED.
1308 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1310 CLIENT *target, *prefix;
1312 char tok_buf[COMMAND_LEN];
1313 int max, last, count, i, nc;
1316 assert( Client != NULL );
1317 assert( Req != NULL );
1319 /* Do not reveal any info on disconnected users? */
1320 if (Conf_MorePrivacy)
1323 /* Wrong number of parameters? */
1325 return IRC_WriteErrClient(Client, ERR_NONICKNAMEGIVEN_MSG,
1328 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 3)
1329 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
1330 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 2, prefix)
1333 if (target != Client_ThisServer()) {
1334 IRC_WriteStrClientPrefix(target, prefix, "WHOWAS %s %s %s",
1335 Req->argv[0], Req->argv[1],
1340 whowas = Client_GetWhowas( );
1341 last = Client_GetLastWhowasIndex( );
1345 max = DEF_RPL_WHOWAS;
1346 if (Req->argc > 1) {
1347 max = atoi(Req->argv[1]);
1349 max = MAX_RPL_WHOWAS;
1353 * Break up the nick argument into a list of nicks, if applicable
1354 * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1356 strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1357 nick = strtok(tok_buf, ",");
1359 for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1363 if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1364 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1365 return DISCONNECTED;
1369 /* previous entry */
1372 /* "underflow", wrap around */
1376 if (nc && count >= max)
1378 } while (i != last);
1380 if (nc == 0 && !IRC_WriteErrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1381 Client_ID(prefix), nick))
1382 return DISCONNECTED;
1384 return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG,
1385 Client_ID(prefix), Req->argv[0]);
1389 * Send LUSERS reply to a client.
1391 * @param Client The receipient of the information.
1392 * @return CONNECTED or DISCONNECTED.
1395 IRC_Send_LUSERS(CLIENT *Client)
1402 assert(Client != NULL);
1404 /* Users, services and servers in the network */
1405 if (!IRC_WriteStrClient(Client, RPL_LUSERCLIENT_MSG, Client_ID(Client),
1406 Client_UserCount(), Client_ServiceCount(),
1407 Client_ServerCount()))
1408 return DISCONNECTED;
1410 /* Number of IRC operators */
1411 cnt = Client_OperCount( );
1413 if (!IRC_WriteStrClient(Client, RPL_LUSEROP_MSG,
1414 Client_ID(Client), cnt))
1415 return DISCONNECTED;
1418 /* Unknown connections */
1419 cnt = Client_UnknownCount( );
1421 if (!IRC_WriteStrClient(Client, RPL_LUSERUNKNOWN_MSG,
1422 Client_ID(Client), cnt))
1423 return DISCONNECTED;
1426 /* Number of created channels */
1427 if (!IRC_WriteStrClient(Client, RPL_LUSERCHANNELS_MSG,
1429 Channel_CountVisible(Client)))
1430 return DISCONNECTED;
1432 /* Number of local users, services and servers */
1433 if (!IRC_WriteStrClient(Client, RPL_LUSERME_MSG, Client_ID(Client),
1434 Client_MyUserCount(), Client_MyServiceCount(),
1435 Client_MyServerCount()))
1436 return DISCONNECTED;
1439 /* Maximum number of local users */
1440 cnt = Client_MyUserCount();
1441 max = Client_MyMaxUserCount();
1442 if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1443 cnt, max, cnt, max))
1444 return DISCONNECTED;
1445 /* Maximum number of users in the network */
1446 cnt = Client_UserCount();
1447 max = Client_MaxUserCount();
1448 if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1449 cnt, max, cnt, max))
1450 return DISCONNECTED;
1451 /* Connection counters */
1452 if (! IRC_WriteStrClient(Client, RPL_STATSCONN_MSG, Client_ID(Client),
1453 Conn_CountMax(), Conn_CountAccepted()))
1454 return DISCONNECTED;
1458 } /* IRC_Send_LUSERS */
1461 IRC_Show_MOTD( CLIENT *Client )
1464 size_t len_tot, len_str;
1466 assert( Client != NULL );
1468 len_tot = array_bytes(&Conf_Motd);
1469 if (len_tot == 0 && !Conn_UsesSSL(Client_Conn(Client)))
1470 return IRC_WriteErrClient(Client, ERR_NOMOTD_MSG, Client_ID(Client));
1472 if (!IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG, Client_ID(Client),
1473 Client_ID(Client_ThisServer())))
1474 return DISCONNECTED;
1476 line = array_start(&Conf_Motd);
1477 while (len_tot > 0) {
1478 len_str = strlen(line) + 1;
1480 assert(len_tot >= len_str);
1483 if (!IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID(Client), line))
1484 return DISCONNECTED;
1488 if (!Show_MOTD_SSLInfo(Client))
1489 return DISCONNECTED;
1491 if (!IRC_WriteStrClient(Client, RPL_ENDOFMOTD_MSG, Client_ID(Client)))
1492 return DISCONNECTED;
1494 if (*Conf_CloakHost)
1495 return IRC_WriteStrClient(Client, RPL_HOSTHIDDEN_MSG,
1497 Client_Hostname(Client));
1500 } /* IRC_Show_MOTD */
1503 * Send NAMES reply for a specific client and channel.
1505 * @param Client The client requesting the NAMES information.
1506 * @param Chan The channel
1507 * @return CONNECTED or DISCONNECTED.
1510 IRC_Send_NAMES(CLIENT * Client, CHANNEL * Chan)
1512 bool is_visible, is_member;
1513 char str[COMMAND_LEN];
1517 assert(Client != NULL);
1518 assert(Chan != NULL);
1520 if (Channel_IsMemberOf(Chan, Client))
1525 /* Do not print info on channel memberships to anyone that is not member? */
1526 if (Conf_MorePrivacy && !is_member)
1529 /* Secret channel? */
1530 if (!is_member && Channel_HasMode(Chan, 's'))
1533 snprintf(str, sizeof(str), RPL_NAMREPLY_MSG, Client_ID(Client), "=",
1534 Channel_Name(Chan));
1535 cl2chan = Channel_FirstMember(Chan);
1537 cl = Channel_GetClient(cl2chan);
1539 if (Client_HasMode(cl, 'i'))
1544 if (is_member || is_visible) {
1545 if (str[strlen(str) - 1] != ':')
1546 strlcat(str, " ", sizeof(str));
1548 who_flags_qualifier(Client, Channel_UserModes(Chan, cl),
1550 strlcat(str, Client_ID(cl), sizeof(str));
1552 if (strlen(str) > (COMMAND_LEN - CLIENT_NICK_LEN - 4)) {
1553 if (!IRC_WriteStrClient(Client, "%s", str))
1554 return DISCONNECTED;
1555 snprintf(str, sizeof(str), RPL_NAMREPLY_MSG,
1556 Client_ID(Client), "=",
1557 Channel_Name(Chan));
1561 cl2chan = Channel_NextMember(Chan, cl2chan);
1563 if (str[strlen(str) - 1] != ':') {
1564 if (!IRC_WriteStrClient(Client, "%s", str))
1565 return DISCONNECTED;
1569 } /* IRC_Send_NAMES */
1572 * Send the ISUPPORT numeric (005).
1573 * This numeric indicates the features that are supported by this server.
1574 * See <http://www.irc.org/tech_docs/005.html> for details.
1577 IRC_Send_ISUPPORT(CLIENT * Client)
1579 if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1580 CHANTYPES, CHANTYPES, Conf_MaxJoins))
1581 return DISCONNECTED;
1582 return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1583 CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1584 COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1585 COMMAND_LEN - 113, MAX_HNDL_MODES_ARG,
1586 MAX_HNDL_CHANNEL_LISTS);
1587 } /* IRC_Send_ISUPPORT */