2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2014 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.
27 #include "conn-func.h"
37 #include "irc-macros.h"
38 #include "irc-write.h"
39 #include "client-cap.h"
47 t_diff(time_t *t, const time_t d)
55 return (unsigned int)diff;
59 uptime_days(time_t *now)
61 return t_diff(now, 60 * 60 * 24);
65 uptime_hrs(time_t *now)
67 return t_diff(now, 60 * 60);
71 uptime_mins(time_t *now)
73 return t_diff(now, 60);
77 write_whoreply(CLIENT *Client, CLIENT *c, const char *channelname, const char *flags)
79 return IRC_WriteStrClient(Client, RPL_WHOREPLY_MSG, Client_ID(Client),
80 channelname, Client_User(c),
81 Client_HostnameDisplayed(c),
82 Client_ID(Client_Introducer(c)), Client_ID(c),
83 flags, Client_Hops(c), Client_Info(c));
87 * Return channel user mode prefix(es).
89 * @param Client The client requesting the mode prefixes.
90 * @param chan_user_modes String with channel user modes.
91 * @param str String buffer to which the prefix(es) will be appended.
92 * @param len Size of "str" buffer.
93 * @return Pointer to "str".
96 who_flags_qualifier(CLIENT *Client, const char *chan_user_modes,
97 char *str, size_t len)
99 assert(Client != NULL);
101 if (Client_Cap(Client) & CLIENT_CAP_MULTI_PREFIX) {
102 if (strchr(chan_user_modes, 'q'))
103 strlcat(str, "~", len);
104 if (strchr(chan_user_modes, 'a'))
105 strlcat(str, "&", len);
106 if (strchr(chan_user_modes, 'o'))
107 strlcat(str, "@", len);
108 if (strchr(chan_user_modes, 'h'))
109 strlcat(str, "%", len);
110 if (strchr(chan_user_modes, 'v'))
111 strlcat(str, "+", len);
116 if (strchr(chan_user_modes, 'q'))
117 strlcat(str, "~", len);
118 else if (strchr(chan_user_modes, 'a'))
119 strlcat(str, "&", len);
120 else if (strchr(chan_user_modes, 'o'))
121 strlcat(str, "@", len);
122 else if (strchr(chan_user_modes, 'h'))
123 strlcat(str, "%", len);
124 else if (strchr(chan_user_modes, 'v'))
125 strlcat(str, "+", len);
131 * Send WHO reply for a "channel target" ("WHO #channel").
133 * @param Client Client requesting the information.
134 * @param Chan Channel being requested.
135 * @param OnlyOps Only display IRC operators.
136 * @return CONNECTED or DISCONNECTED.
139 IRC_WHO_Channel(CLIENT *Client, CHANNEL *Chan, bool OnlyOps)
141 bool is_visible, is_member, is_ircop;
147 assert( Client != NULL );
148 assert( Chan != NULL );
150 is_member = Channel_IsMemberOf(Chan, Client);
152 /* Secret channel? */
153 if (!is_member && Channel_HasMode(Chan, 's'))
154 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG,
155 Client_ID(Client), Channel_Name(Chan));
157 cl2chan = Channel_FirstMember(Chan);
158 for (; cl2chan ; cl2chan = Channel_NextMember(Chan, cl2chan)) {
159 c = Channel_GetClient(cl2chan);
161 is_ircop = Client_HasMode(c, 'o');
162 if (OnlyOps && !is_ircop)
165 is_visible = !Client_HasMode(c, 'i');
166 if (is_member || is_visible) {
167 memset(flags, 0, sizeof(flags));
169 if (Client_HasMode(c, 'a'))
170 flags[0] = 'G'; /* away */
177 who_flags_qualifier(Client, Channel_UserModes(Chan, c),
178 flags, sizeof(flags));
180 if (!write_whoreply(Client, c, Channel_Name(Chan),
187 /* If there are a lot of clients, increase the penalty a bit */
188 if (count > MAX_RPL_WHO)
189 IRC_SetPenalty(Client, 1);
191 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client),
196 * Send WHO reply for a "mask target" ("WHO m*sk").
198 * @param Client Client requesting the information.
199 * @param Mask Mask being requested or NULL for "all" clients.
200 * @param OnlyOps Only display IRC operators.
201 * @return CONNECTED or DISCONNECTED.
204 IRC_WHO_Mask(CLIENT *Client, char *Mask, bool OnlyOps)
209 bool client_match, is_visible;
213 assert (Client != NULL);
218 IRC_SetPenalty(Client, 3);
219 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
220 if (Client_Type(c) != CLIENT_USER)
223 if (OnlyOps && !Client_HasMode(c, 'o'))
227 /* Match pattern against user host/server/name/nick */
228 client_match = MatchCaseInsensitive(Mask,
231 client_match = MatchCaseInsensitive(Mask,
232 Client_ID(Client_Introducer(c)));
234 client_match = MatchCaseInsensitive(Mask,
237 client_match = MatchCaseInsensitive(Mask,
240 continue; /* no match: skip this client */
243 is_visible = !Client_HasMode(c, 'i');
245 /* Target client is invisible, but mask matches exactly? */
246 if (!is_visible && Mask && strcasecmp(Client_ID(c), Mask) == 0)
249 /* Target still invisible, but are both on the same channel? */
251 cl2chan = Channel_FirstChannelOf(Client);
252 while (cl2chan && !is_visible) {
253 chan = Channel_GetChannel(cl2chan);
254 if (Channel_IsMemberOf(chan, c))
256 cl2chan = Channel_NextChannelOf(Client, cl2chan);
260 if (!is_visible) /* target user is not visible */
263 if (IRC_CheckListTooBig(Client, count, MAX_RPL_WHO, "WHO"))
266 memset(flags, 0, sizeof(flags));
268 if (Client_HasMode(c, 'a'))
269 flags[0] = 'G'; /* away */
273 if (Client_HasMode(c, 'o'))
276 if (!write_whoreply(Client, c, "*", flags))
281 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client),
286 * Generate WHOIS reply of one actual client.
288 * @param Client The client from which this command has been received.
289 * @param from The client requesting the information ("originator").
290 * @param c The client of which information should be returned.
291 * @return CONNECTED or DISCONNECTED.
294 IRC_WHOIS_SendReply(CLIENT *Client, CLIENT *from, CLIENT *c)
296 char str[COMMAND_LEN];
300 assert(Client != NULL);
301 assert(from != NULL);
304 /* Nick, user, hostname and client info */
305 if (!IRC_WriteStrClient(from, RPL_WHOISUSER_MSG, Client_ID(from),
306 Client_ID(c), Client_User(c),
307 Client_HostnameDisplayed(c), Client_Info(c)))
311 if (!IRC_WriteStrClient(from, RPL_WHOISSERVER_MSG, Client_ID(from),
312 Client_ID(c), Client_ID(Client_Introducer(c)),
313 Client_Info(Client_Introducer(c))))
316 /* Channels, show only if client has no +I or if from is oper */
317 if(!(Client_HasMode(c, 'I')) || Client_HasMode(from, 'o')) {
318 snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
319 Client_ID(from), Client_ID(c));
320 cl2chan = Channel_FirstChannelOf(c);
322 chan = Channel_GetChannel(cl2chan);
323 assert(chan != NULL);
326 cl2chan = Channel_NextChannelOf(c, cl2chan);
328 /* Secret channel? */
329 if (Channel_HasMode(chan, 's')
330 && !Channel_IsMemberOf(chan, Client))
333 /* Local channel and request is not from a user? */
334 if (Client_Type(Client) == CLIENT_SERVER
335 && Channel_IsLocal(chan))
338 /* Concatenate channel names */
339 if (str[strlen(str) - 1] != ':')
340 strlcat(str, " ", sizeof(str));
342 who_flags_qualifier(Client, Channel_UserModes(chan, c),
344 strlcat(str, Channel_Name(chan), sizeof(str));
346 if (strlen(str) > (COMMAND_LEN - CHANNEL_NAME_LEN - 4)) {
347 /* Line becomes too long: send it! */
348 if (!IRC_WriteStrClient(Client, "%s", str))
350 snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
351 Client_ID(from), Client_ID(c));
354 if(str[strlen(str) - 1] != ':') {
355 /* There is data left to send: */
356 if (!IRC_WriteStrClient(Client, "%s", str))
362 if (Client_Type(c) == CLIENT_SERVICE &&
363 !IRC_WriteStrClient(from, RPL_WHOISSERVICE_MSG,
364 Client_ID(from), Client_ID(c)))
368 if (Client_Type(c) != CLIENT_SERVICE &&
369 Client_HasMode(c, 'o') &&
370 !IRC_WriteStrClient(from, RPL_WHOISOPERATOR_MSG,
371 Client_ID(from), Client_ID(c)))
375 if (Client_HasMode(c, 'B') &&
376 !IRC_WriteStrClient(from, RPL_WHOISBOT_MSG,
377 Client_ID(from), Client_ID(c)))
380 /* Connected using SSL? */
381 if (Conn_UsesSSL(Client_Conn(c))) {
382 if (!IRC_WriteStrClient(from, RPL_WHOISSSL_MSG, Client_ID(from),
386 /* Certificate fingerprint? */
387 if (Conn_GetCertFp(Client_Conn(c)) &&
389 !IRC_WriteStrClient(from, RPL_WHOISCERTFP_MSG,
390 Client_ID(from), Client_ID(c),
391 Conn_GetCertFp(Client_Conn(c))))
395 /* Registered nickname? */
396 if (Client_HasMode(c, 'R') &&
397 !IRC_WriteStrClient(from, RPL_WHOISREGNICK_MSG,
398 Client_ID(from), Client_ID(c)))
401 /* Account name metadata? */
402 if (Client_AccountName(c) &&
403 !IRC_WriteStrClient(from, RPL_WHOISLOGGEDIN_MSG,
404 Client_ID(from), Client_ID(c),
405 Client_AccountName(c)))
408 /* Local client and requester is the user itself or an IRC Op? */
409 if (Client_Conn(c) > NONE &&
410 (from == c || Client_HasMode(from, 'o'))) {
411 /* Client hostname */
412 if (!IRC_WriteStrClient(from, RPL_WHOISHOST_MSG,
413 Client_ID(from), Client_ID(c),
414 Client_Hostname(c), Client_IPAText(c)))
417 if (!IRC_WriteStrClient(from, RPL_WHOISMODES_MSG,
418 Client_ID(from), Client_ID(c), Client_Modes(c)))
422 /* Idle and signon time (local clients only!) */
423 if (!Conf_MorePrivacy && Client_Conn(c) > NONE &&
424 !IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
425 Client_ID(from), Client_ID(c),
426 (unsigned long)Conn_GetIdle(Client_Conn(c)),
427 (unsigned long)Conn_GetSignon(Client_Conn(c))))
431 if (Client_HasMode(c, 'a') &&
432 !IRC_WriteStrClient(from, RPL_AWAY_MSG,
433 Client_ID(from), Client_ID(c), Client_Away(c)))
440 WHOWAS_EntryWrite(CLIENT *prefix, WHOWAS *entry)
444 (void)strftime(t_str, sizeof(t_str), "%a %b %d %H:%M:%S %Y",
445 localtime(&entry->time));
447 if (!IRC_WriteStrClient(prefix, RPL_WHOWASUSER_MSG, Client_ID(prefix),
448 entry->id, entry->user, entry->host, entry->info))
451 return IRC_WriteStrClient(prefix, RPL_WHOISSERVER_MSG, Client_ID(prefix),
452 entry->id, entry->server, t_str);
457 Show_MOTD_SSLInfo(CLIENT *Client)
459 char buf[COMMAND_LEN];
462 if (Conn_GetCipherInfo(Client_Conn(Client), c_str, sizeof(c_str))) {
463 snprintf(buf, sizeof(buf), "Connected using Cipher %s", c_str);
464 if (!IRC_WriteStrClient(Client, RPL_MOTD_MSG,
465 Client_ID(Client), buf))
469 if (Conn_GetCertFp(Client_Conn(Client))) {
470 snprintf(buf, sizeof(buf),
471 "Your client certificate fingerprint is: %s",
472 Conn_GetCertFp(Client_Conn(Client)));
473 if (!IRC_WriteStrClient(Client, RPL_MOTD_MSG,
474 Client_ID(Client), buf))
482 Show_MOTD_SSLInfo(UNUSED CLIENT *c)
488 /* Global functions */
491 * Handler for the IRC command "ADMIN".
493 * @param Client The client from which this command has been received.
494 * @param Req Request structure with prefix and all parameters.
495 * @return CONNECTED or DISCONNECTED.
498 IRC_ADMIN(CLIENT *Client, REQUEST *Req )
500 CLIENT *target, *prefix;
502 assert( Client != NULL );
503 assert( Req != NULL );
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_GET_SENDER_OR_RETURN_(prefix, Req, Client)
548 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
551 if (target != Client_ThisServer()) {
552 IRC_WriteStrClientPrefix(target, prefix, "INFO %s",
557 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix),
561 #if defined(BIRTHDATE)
563 time_t t = BIRTHDATE;
564 (void)strftime(t_str, sizeof(t_str), "%a %b %d %Y at %H:%M:%S (%Z)",
566 snprintf(msg, sizeof(msg), "Birth Date: %s", t_str);
567 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
569 #elif defined(__DATE__) && defined(__TIME__)
570 snprintf(msg, sizeof(msg), "Birth Date: %s at %s", __DATE__, __TIME__);
571 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
575 strlcpy(msg, "On-line since ", sizeof(msg));
576 strlcat(msg, NGIRCd_StartStr, sizeof(msg));
577 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
580 if (!IRC_WriteStrClient(Client, RPL_ENDOFINFO_MSG, Client_ID(prefix)))
587 * Handler for the IRC "ISON" command.
589 * @param Client The client from which this command has been received.
590 * @param Req Request structure with prefix and all parameters.
591 * @return CONNECTED or DISCONNECTED.
594 IRC_ISON( CLIENT *Client, REQUEST *Req )
596 char rpl[COMMAND_LEN];
601 assert(Client != NULL);
604 strlcpy(rpl, RPL_ISON_MSG, sizeof rpl);
605 for (i = 0; i < Req->argc; i++) {
606 /* "All" ircd even parse ":<x> <y> ..." arguments and split
607 * them up; so we do the same ... */
608 ptr = strtok(Req->argv[i], " ");
611 c = Client_Search(ptr);
612 if (c && Client_Type(c) == CLIENT_USER) {
613 strlcat(rpl, Client_ID(c), sizeof(rpl));
614 strlcat(rpl, " ", sizeof(rpl));
616 ptr = strtok(NULL, " ");
619 ngt_TrimLastChr(rpl, ' ');
621 return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
625 * Handler for the IRC "LINKS" command.
627 * @param Client The client from which this command has been received.
628 * @param Req Request structure with prefix and all parameters.
629 * @return CONNECTED or DISCONNECTED.
632 IRC_LINKS(CLIENT *Client, REQUEST *Req)
634 CLIENT *target, *from, *c;
637 assert(Client != NULL);
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_GET_SENDER_OR_RETURN_(from, Req, Client)
693 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
696 if (target != Client_ThisServer()) {
697 IRC_WriteStrClientPrefix(target, from,
698 "LUSERS %s %s", Req->argv[0],
703 return IRC_Send_LUSERS(from);
707 * Handler for the IRC command "SERVLIST".
709 * @param Client The client from which this command has been received.
710 * @param Req Request structure with prefix and all parameters.
711 * @return CONNECTED or DISCONNECTED.
714 IRC_SERVLIST(CLIENT *Client, REQUEST *Req)
718 assert(Client != NULL);
721 if (Req->argc < 2 || strcmp(Req->argv[1], "0") == 0) {
722 for (c = Client_First(); c!= NULL; c = Client_Next(c)) {
723 if (Client_Type(c) != CLIENT_SERVICE)
725 if (Req->argc > 0 && !MatchCaseInsensitive(Req->argv[0],
728 if (!IRC_WriteStrClient(Client, RPL_SERVLIST_MSG,
729 Client_ID(Client), Client_Mask(c),
730 Client_Mask(Client_Introducer(c)), "*",
731 0, Client_Hops(c), Client_Info(c)))
736 return IRC_WriteStrClient(Client, RPL_SERVLISTEND_MSG, Client_ID(Client),
737 Req->argc > 0 ? Req->argv[0] : "*",
738 Req->argc > 1 ? Req->argv[1] : "0");
742 * Handler for the IRC command "MOTD".
744 * @param Client The client from which this command has been received.
745 * @param Req Request structure with prefix and all parameters.
746 * @return CONNECTED or DISCONNECTED.
749 IRC_MOTD( CLIENT *Client, REQUEST *Req )
751 CLIENT *from, *target;
753 assert( Client != NULL );
754 assert( Req != NULL );
756 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
757 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
760 if (target != Client_ThisServer()) {
761 IRC_WriteStrClientPrefix(target, from, "MOTD %s",
766 return IRC_Show_MOTD(from);
770 * Handler for the IRC command "NAMES".
772 * @param Client The client from which this command has been received.
773 * @param Req Request structure with prefix and all parameters.
774 * @return CONNECTED or DISCONNECTED.
777 IRC_NAMES( CLIENT *Client, REQUEST *Req )
779 char rpl[COMMAND_LEN], *ptr;
780 CLIENT *target, *from, *c;
783 assert( Client != NULL );
784 assert( Req != NULL );
786 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
787 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
790 if (target != Client_ThisServer()) {
791 IRC_WriteStrClientPrefix(target, from, "NAMES %s :%s",
792 Req->argv[0], Client_ID(target));
797 /* Return NAMES list for specific channels */
798 ptr = strtok(Req->argv[0], ",");
800 chan = Channel_Search(ptr);
801 if (chan && !IRC_Send_NAMES(from, chan))
803 if (!IRC_WriteStrClient(from, RPL_ENDOFNAMES_MSG,
804 Client_ID(from), ptr))
806 ptr = strtok( NULL, "," );
811 chan = Channel_First();
813 if (!IRC_Send_NAMES(from, chan))
815 chan = Channel_Next(chan);
818 /* Now print all clients which are not in any channel */
820 snprintf(rpl, sizeof(rpl), RPL_NAMREPLY_MSG, Client_ID(from), "*", "*");
822 if (Client_Type(c) == CLIENT_USER
823 && Channel_FirstChannelOf(c) == NULL
824 && !Client_HasMode(c, 'i'))
826 /* its a user, concatenate ... */
827 if (rpl[strlen(rpl) - 1] != ':')
828 strlcat(rpl, " ", sizeof(rpl));
829 strlcat(rpl, Client_ID(c), sizeof(rpl));
831 if (strlen(rpl) > COMMAND_LEN - CLIENT_NICK_LEN - 4) {
832 /* Line is gwoing too long, send now */
833 if (!IRC_WriteStrClient(from, "%s", rpl))
835 snprintf(rpl, sizeof(rpl), RPL_NAMREPLY_MSG,
836 Client_ID(from), "*", "*");
841 if (rpl[strlen(rpl) - 1] != ':' && !IRC_WriteStrClient(from, "%s", rpl))
844 return IRC_WriteStrClient(from, RPL_ENDOFNAMES_MSG, Client_ID(from), "*");
848 * Handler for the IRC command "STATS".
850 * @param Client The client from which this command has been received.
851 * @param Req Request structure with prefix and all parameters.
852 * @return CONNECTED or DISCONNECTED.
855 IRC_STATS( CLIENT *Client, REQUEST *Req )
857 CLIENT *from, *target, *cl;
862 unsigned int days, hrs, mins;
863 struct list_head *list;
864 struct list_elem *list_item;
865 bool more_links = false;
867 assert(Client != NULL);
870 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
871 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
874 if (target != Client_ThisServer()) {
875 IRC_WriteStrClientPrefix(target, from, "STATS %s %s",
876 Req->argv[0], Client_ID(target));
881 query = Req->argv[0][0] ? Req->argv[0][0] : '*';
886 case 'g': /* Network-wide bans ("G-Lines") */
888 case 'k': /* Server-local bans ("K-Lines") */
890 if (!Client_HasMode(from, 'o'))
891 return IRC_WriteErrClient(from, ERR_NOPRIVILEGES_MSG,
893 if (query == 'g' || query == 'G')
894 list = Class_GetList(CLASS_GLINE);
896 list = Class_GetList(CLASS_KLINE);
897 list_item = Lists_GetFirst(list);
899 if (!IRC_WriteStrClient(from, RPL_STATSXLINE_MSG,
900 Client_ID(from), query,
901 Lists_GetMask(list_item),
902 Lists_GetValidity(list_item),
903 Lists_GetReason(list_item)))
905 list_item = Lists_GetNext(list_item);
908 case 'L': /* Link status (servers and user links) */
909 if (!Op_Check(from, Req))
910 return Op_NoPrivileges(from, Req);
913 case 'l': /* Link status (servers and own link) */
914 time_now = time(NULL);
915 for (con = Conn_First(); con != NONE; con = Conn_Next(con)) {
916 cl = Conn_GetClient(con);
919 if (Client_Type(cl) == CLIENT_SERVER ||
921 (more_links && Client_Type(cl) == CLIENT_USER)) {
923 if (Conn_Options(con) & CONN_ZIP) {
924 if (!IRC_WriteStrClient
925 (from, RPL_STATSLINKINFOZIP_MSG,
926 Client_ID(from), Client_Mask(cl),
927 Conn_SendQ(con), Conn_SendMsg(con),
933 (long)(time_now - Conn_StartTime(con))))
938 if (!IRC_WriteStrClient
939 (from, RPL_STATSLINKINFO_MSG,
940 Client_ID(from), Client_Mask(cl),
941 Conn_SendQ(con), Conn_SendMsg(con),
942 Conn_SendBytes(con), Conn_RecvMsg(con),
944 (long)(time_now - Conn_StartTime(con))))
949 case 'm': /* IRC command status (usage count) */
951 cmd = Parse_GetCommandStruct();
952 for (; cmd->name; cmd++) {
953 if (cmd->lcount == 0 && cmd->rcount == 0)
955 if (!IRC_WriteStrClient
956 (from, RPL_STATSCOMMANDS_MSG, Client_ID(from),
957 cmd->name, cmd->lcount, cmd->bytes, cmd->rcount))
961 case 'u': /* Server uptime */
963 time_now = time(NULL) - NGIRCd_Start;
964 days = uptime_days(&time_now);
965 hrs = uptime_hrs(&time_now);
966 mins = uptime_mins(&time_now);
967 if (!IRC_WriteStrClient(from, RPL_STATSUPTIME, Client_ID(from),
968 days, hrs, mins, (unsigned int)time_now))
973 return IRC_WriteStrClient(from, RPL_ENDOFSTATS_MSG,
974 Client_ID(from), query);
978 * Handler for the IRC command "SUMMON".
980 * @param Client The client from which this command has been received.
981 * @param Req Request structure with prefix and all parameters.
982 * @return CONNECTED or DISCONNECTED.
985 IRC_SUMMON(CLIENT * Client, UNUSED REQUEST * Req)
987 assert(Client != NULL);
989 return IRC_WriteErrClient(Client, ERR_SUMMONDISABLED_MSG,
994 * Handler for the IRC command "TIME".
996 * @param Client The client from which this command has been received.
997 * @param Req Request structure with prefix and all parameters.
998 * @return CONNECTED or DISCONNECTED.
1001 IRC_TIME( CLIENT *Client, REQUEST *Req )
1003 CLIENT *from, *target;
1007 assert(Client != NULL);
1008 assert(Req != NULL);
1010 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
1011 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
1014 if (target != Client_ThisServer()) {
1015 IRC_WriteStrClientPrefix(target, from, "TIME %s",
1021 (void)strftime(t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime(&t));
1022 return IRC_WriteStrClient(from, RPL_TIME_MSG, Client_ID(from),
1023 Client_ID(Client_ThisServer()), t_str);
1027 * Handler for the IRC command "USERHOST".
1029 * @param Client The client from which this command has been received.
1030 * @param Req Request structure with prefix and all parameters.
1031 * @return CONNECTED or DISCONNECTED.
1034 IRC_USERHOST(CLIENT *Client, REQUEST *Req)
1036 char rpl[COMMAND_LEN];
1040 assert(Client != NULL);
1041 assert(Req != NULL);
1048 strlcpy(rpl, RPL_USERHOST_MSG, sizeof rpl);
1049 for (i = 0; i < max; i++) {
1050 c = Client_Search(Req->argv[i]);
1051 if (c && (Client_Type(c) == CLIENT_USER)) {
1052 /* This Nick is "online" */
1053 strlcat(rpl, Client_ID(c), sizeof(rpl));
1054 if (Client_HasMode(c, 'o'))
1055 strlcat(rpl, "*", sizeof(rpl));
1056 strlcat(rpl, "=", sizeof(rpl));
1057 if (Client_HasMode(c, 'a'))
1058 strlcat(rpl, "-", sizeof(rpl));
1060 strlcat(rpl, "+", sizeof(rpl));
1061 strlcat(rpl, Client_User(c), sizeof(rpl));
1062 strlcat(rpl, "@", sizeof(rpl));
1063 strlcat(rpl, Client_HostnameDisplayed(c), sizeof(rpl));
1064 strlcat(rpl, " ", sizeof(rpl));
1067 ngt_TrimLastChr(rpl, ' ');
1069 return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
1070 } /* IRC_USERHOST */
1073 * Handler for the IRC command "USERS".
1075 * @param Client The client from which this command has been received.
1076 * @param Req Request structure with prefix and all parameters.
1077 * @return CONNECTED or DISCONNECTED.
1080 IRC_USERS(CLIENT * Client, UNUSED REQUEST * Req)
1082 assert(Client != NULL);
1084 return IRC_WriteErrClient(Client, ERR_USERSDISABLED_MSG,
1089 * Handler for the IRC command "VERSION".
1091 * @param Client The client from which this command has been received.
1092 * @param Req Request structure with prefix and all parameters.
1093 * @return CONNECTED or DISCONNECTED.
1096 IRC_VERSION( CLIENT *Client, REQUEST *Req )
1098 CLIENT *target, *prefix;
1100 assert( Client != NULL );
1101 assert( Req != NULL );
1103 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
1104 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
1107 if (target != Client_ThisServer()) {
1108 IRC_WriteStrClientPrefix(target, prefix, "VERSION %s",
1113 /* send version information */
1114 if (!IRC_WriteStrClient(Client, RPL_VERSION_MSG, Client_ID(prefix),
1115 PACKAGE_NAME, PACKAGE_VERSION,
1116 NGIRCd_DebugLevel, Conf_ServerName,
1117 NGIRCd_VersionAddition))
1118 return DISCONNECTED;
1121 /* send RPL_ISUPPORT(005) numerics */
1122 if (!IRC_Send_ISUPPORT(prefix))
1123 return DISCONNECTED;
1130 * Handler for the IRC "WHO" command.
1132 * @param Client The client from which this command has been received.
1133 * @param Req Request structure with prefix and all parameters.
1134 * @return CONNECTED or DISCONNECTED.
1137 IRC_WHO(CLIENT *Client, REQUEST *Req)
1142 assert (Client != NULL);
1143 assert (Req != NULL);
1146 if (Req->argc == 2) {
1147 if (strcmp(Req->argv[1], "o") == 0)
1151 return IRC_WriteErrClient(Client,
1152 ERR_NEEDMOREPARAMS_MSG,
1159 if (Req->argc >= 1) {
1160 /* Channel or mask given */
1161 chan = Channel_Search(Req->argv[0]);
1163 /* Members of a channel have been requested */
1164 return IRC_WHO_Channel(Client, chan, only_ops);
1166 if (strcmp(Req->argv[0], "0") != 0) {
1167 /* A mask has been given. But please note this RFC
1168 * stupidity: "0" is same as no arguments ... */
1169 return IRC_WHO_Mask(Client, Req->argv[0], only_ops);
1173 /* No channel or (valid) mask given */
1174 return IRC_WHO_Mask(Client, NULL, only_ops);
1178 * Handler for the IRC "WHOIS" command.
1180 * @param Client The client from which this command has been received.
1181 * @param Req Request structure with prefix and all parameters.
1182 * @return CONNECTED or DISCONNECTED.
1185 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
1187 CLIENT *from, *target, *c;
1188 unsigned int match_count = 0, found = 0;
1189 bool has_wildcards, is_remote;
1190 bool got_wildcard = false;
1191 char mask[COMMAND_LEN], *query;
1193 assert( Client != NULL );
1194 assert( Req != NULL );
1196 /* Wrong number of parameters? */
1198 return IRC_WriteErrClient(Client, ERR_NONICKNAMEGIVEN_MSG,
1201 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
1202 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
1204 /* Get target server for this command */
1205 if (Req->argc > 1) {
1206 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, Client)
1208 target = Client_ThisServer();
1210 assert(target != NULL);
1213 if (target != Client_ThisServer()) {
1214 IRC_WriteStrClientPrefix(target, from, "WHOIS %s :%s",
1215 Req->argv[0], Req->argv[1]);
1219 is_remote = Client_Conn(from) < 0;
1220 strlcpy(mask, Req->argv[Req->argc - 1], sizeof(mask));
1221 for (query = strtok(ngt_LowerStr(mask), ",");
1223 query = strtok(NULL, ","), found++)
1225 has_wildcards = query[strcspn(query, "*?")] != 0;
1227 * follows ircd 2.10 implementation:
1228 * - handle up to 3 targets
1229 * - no wildcards for remote clients
1230 * - only one wildcard target per local client
1232 * Also, at most MAX_RPL_WHOIS matches are returned.
1234 if (!has_wildcards || is_remote) {
1235 c = Client_Search(query);
1236 if (c && (Client_Type(c) == CLIENT_USER
1237 || Client_Type(c) == CLIENT_SERVICE)) {
1238 if (!IRC_WHOIS_SendReply(Client, from, c))
1239 return DISCONNECTED;
1241 if (!IRC_WriteErrClient(Client,
1245 return DISCONNECTED;
1250 /* we already handled one wildcard query */
1251 if (!IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
1252 Client_ID(Client), query))
1253 return DISCONNECTED;
1256 got_wildcard = true;
1257 /* Increase penalty for wildcard queries */
1258 IRC_SetPenalty(Client, 3);
1260 for (c = Client_First(); c; c = Client_Next(c)) {
1261 if (IRC_CheckListTooBig(Client, match_count,
1262 MAX_RPL_WHOIS, "WHOIS"))
1265 if (Client_Type(c) != CLIENT_USER)
1267 if (!MatchCaseInsensitive(query, Client_ID(c)))
1269 if (!IRC_WHOIS_SendReply(Client, from, c))
1270 return DISCONNECTED;
1275 if (match_count == 0)
1276 IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
1278 Req->argv[Req->argc - 1]);
1280 return IRC_WriteStrClient(from, RPL_ENDOFWHOIS_MSG,
1281 Client_ID(from), Req->argv[Req->argc - 1]);
1285 * Handler for the IRC "WHOWAS" command.
1287 * @param Client The client from which this command has been received.
1288 * @param Req Request structure with prefix and all parameters.
1289 * @return CONNECTED or DISCONNECTED.
1292 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1294 CLIENT *target, *prefix;
1296 char tok_buf[COMMAND_LEN];
1297 int max, last, count, i, nc;
1300 assert( Client != NULL );
1301 assert( Req != NULL );
1303 /* Wrong number of parameters? */
1305 return IRC_WriteErrClient(Client, ERR_NONICKNAMEGIVEN_MSG,
1308 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 3)
1309 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
1310 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 2, prefix)
1312 /* Do not reveal any info on disconnected users? */
1313 if (Conf_MorePrivacy)
1317 if (target != Client_ThisServer()) {
1318 IRC_WriteStrClientPrefix(target, prefix, "WHOWAS %s %s %s",
1319 Req->argv[0], Req->argv[1],
1324 whowas = Client_GetWhowas( );
1325 last = Client_GetLastWhowasIndex( );
1329 max = DEF_RPL_WHOWAS;
1330 if (Req->argc > 1) {
1331 max = atoi(Req->argv[1]);
1333 max = MAX_RPL_WHOWAS;
1337 * Break up the nick argument into a list of nicks, if applicable
1338 * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1340 strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1341 nick = strtok(tok_buf, ",");
1343 for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1347 if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1348 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1349 return DISCONNECTED;
1353 /* previous entry */
1356 /* "underflow", wrap around */
1360 if (nc && count >= max)
1362 } while (i != last);
1364 if (nc == 0 && !IRC_WriteErrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1365 Client_ID(prefix), nick))
1366 return DISCONNECTED;
1368 return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG,
1369 Client_ID(prefix), Req->argv[0]);
1373 * Send LUSERS reply to a client.
1375 * @param Client The receipient of the information.
1376 * @return CONNECTED or DISCONNECTED.
1379 IRC_Send_LUSERS(CLIENT *Client)
1386 assert(Client != NULL);
1388 /* Users, services and servers in the network */
1389 if (!IRC_WriteStrClient(Client, RPL_LUSERCLIENT_MSG, Client_ID(Client),
1390 Client_UserCount(), Client_ServiceCount(),
1391 Client_ServerCount()))
1392 return DISCONNECTED;
1394 /* Number of IRC operators */
1395 cnt = Client_OperCount( );
1397 if (!IRC_WriteStrClient(Client, RPL_LUSEROP_MSG,
1398 Client_ID(Client), cnt))
1399 return DISCONNECTED;
1402 /* Unknown connections */
1403 cnt = Client_UnknownCount( );
1405 if (!IRC_WriteStrClient(Client, RPL_LUSERUNKNOWN_MSG,
1406 Client_ID(Client), cnt))
1407 return DISCONNECTED;
1410 /* Number of created channels */
1411 if (!IRC_WriteStrClient(Client, RPL_LUSERCHANNELS_MSG,
1413 Channel_CountVisible(Client)))
1414 return DISCONNECTED;
1416 /* Number of local users, services and servers */
1417 if (!IRC_WriteStrClient(Client, RPL_LUSERME_MSG, Client_ID(Client),
1418 Client_MyUserCount(), Client_MyServiceCount(),
1419 Client_MyServerCount()))
1420 return DISCONNECTED;
1423 /* Maximum number of local users */
1424 cnt = Client_MyUserCount();
1425 max = Client_MyMaxUserCount();
1426 if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1427 cnt, max, cnt, max))
1428 return DISCONNECTED;
1429 /* Maximum number of users in the network */
1430 cnt = Client_UserCount();
1431 max = Client_MaxUserCount();
1432 if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1433 cnt, max, cnt, max))
1434 return DISCONNECTED;
1435 /* Connection counters */
1436 if (! IRC_WriteStrClient(Client, RPL_STATSCONN_MSG, Client_ID(Client),
1437 Conn_CountMax(), Conn_CountAccepted()))
1438 return DISCONNECTED;
1442 } /* IRC_Send_LUSERS */
1445 IRC_Show_MOTD( CLIENT *Client )
1448 size_t len_tot, len_str;
1450 assert( Client != NULL );
1452 len_tot = array_bytes(&Conf_Motd);
1453 if (len_tot == 0 && !Conn_UsesSSL(Client_Conn(Client)))
1454 return IRC_WriteErrClient(Client, ERR_NOMOTD_MSG, Client_ID(Client));
1456 if (!IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG, Client_ID(Client),
1457 Client_ID(Client_ThisServer())))
1458 return DISCONNECTED;
1460 line = array_start(&Conf_Motd);
1461 while (len_tot > 0) {
1462 len_str = strlen(line) + 1;
1464 assert(len_tot >= len_str);
1467 if (!IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID(Client), line))
1468 return DISCONNECTED;
1472 if (!Show_MOTD_SSLInfo(Client))
1473 return DISCONNECTED;
1475 if (!IRC_WriteStrClient(Client, RPL_ENDOFMOTD_MSG, Client_ID(Client)))
1476 return DISCONNECTED;
1478 if (*Conf_CloakHost)
1479 return IRC_WriteStrClient(Client, RPL_HOSTHIDDEN_MSG,
1481 Client_Hostname(Client));
1484 } /* IRC_Show_MOTD */
1487 * Send NAMES reply for a specific client and channel.
1489 * @param Client The client requesting the NAMES information.
1490 * @param Chan The channel
1491 * @return CONNECTED or DISCONNECTED.
1494 IRC_Send_NAMES(CLIENT * Client, CHANNEL * Chan)
1496 bool is_visible, is_member;
1497 char str[COMMAND_LEN];
1501 assert(Client != NULL);
1502 assert(Chan != NULL);
1504 if (Channel_IsMemberOf(Chan, Client))
1509 /* Do not print info on channel memberships to anyone that is not member? */
1510 if (Conf_MorePrivacy && !is_member)
1513 /* Secret channel? */
1514 if (!is_member && Channel_HasMode(Chan, 's'))
1517 snprintf(str, sizeof(str), RPL_NAMREPLY_MSG, Client_ID(Client), "=",
1518 Channel_Name(Chan));
1519 cl2chan = Channel_FirstMember(Chan);
1521 cl = Channel_GetClient(cl2chan);
1523 if (Client_HasMode(cl, 'i'))
1528 if (is_member || is_visible) {
1529 if (str[strlen(str) - 1] != ':')
1530 strlcat(str, " ", sizeof(str));
1532 who_flags_qualifier(Client, Channel_UserModes(Chan, cl),
1534 strlcat(str, Client_ID(cl), sizeof(str));
1536 if (strlen(str) > (COMMAND_LEN - CLIENT_NICK_LEN - 4)) {
1537 if (!IRC_WriteStrClient(Client, "%s", str))
1538 return DISCONNECTED;
1539 snprintf(str, sizeof(str), RPL_NAMREPLY_MSG,
1540 Client_ID(Client), "=",
1541 Channel_Name(Chan));
1545 cl2chan = Channel_NextMember(Chan, cl2chan);
1547 if (str[strlen(str) - 1] != ':') {
1548 if (!IRC_WriteStrClient(Client, "%s", str))
1549 return DISCONNECTED;
1553 } /* IRC_Send_NAMES */
1556 * Send the ISUPPORT numeric (005).
1557 * This numeric indicates the features that are supported by this server.
1558 * See <http://www.irc.org/tech_docs/005.html> for details.
1561 IRC_Send_ISUPPORT(CLIENT * Client)
1563 if (Conf_Network[0] && !IRC_WriteStrClient(Client, RPL_ISUPPORTNET_MSG,
1566 return DISCONNECTED;
1567 if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1568 Conf_AllowedChannelTypes, Conf_AllowedChannelTypes,
1570 return DISCONNECTED;
1571 return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1572 CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1573 COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1574 COMMAND_LEN - 113, MAX_HNDL_MODES_ARG,
1575 MAX_HNDL_CHANNEL_LISTS);
1576 } /* IRC_Send_ISUPPORT */