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),
343 strlcat(str, Channel_Name(chan), sizeof(str));
345 if (strlen(str) > (COMMAND_LEN - CHANNEL_NAME_LEN - 4)) {
346 /* Line becomes too long: send it! */
347 if (!IRC_WriteStrClient(Client, "%s", str))
349 snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
350 Client_ID(from), Client_ID(c));
353 if(str[strlen(str) - 1] != ':') {
354 /* There is data left to send: */
355 if (!IRC_WriteStrClient(Client, "%s", str))
361 if (Client_Type(c) == CLIENT_SERVICE &&
362 !IRC_WriteStrClient(from, RPL_WHOISSERVICE_MSG,
363 Client_ID(from), Client_ID(c)))
367 if (Client_Type(c) != CLIENT_SERVICE &&
368 Client_HasMode(c, 'o') &&
369 !IRC_WriteStrClient(from, RPL_WHOISOPERATOR_MSG,
370 Client_ID(from), Client_ID(c)))
374 if (Client_HasMode(c, 'B') &&
375 !IRC_WriteStrClient(from, RPL_WHOISBOT_MSG,
376 Client_ID(from), Client_ID(c)))
379 /* Connected using SSL? */
380 if (Conn_UsesSSL(Client_Conn(c))) {
381 if (!IRC_WriteStrClient(from, RPL_WHOISSSL_MSG, Client_ID(from),
385 /* Certificate fingerprint? */
386 if (Conn_GetCertFp(Client_Conn(c)) &&
388 !IRC_WriteStrClient(from, RPL_WHOISCERTFP_MSG,
389 Client_ID(from), Client_ID(c),
390 Conn_GetCertFp(Client_Conn(c))))
394 /* Registered nickname? */
395 if (Client_HasMode(c, 'R') &&
396 !IRC_WriteStrClient(from, RPL_WHOISREGNICK_MSG,
397 Client_ID(from), Client_ID(c)))
400 /* Account name metadata? */
401 if (Client_AccountName(c) &&
402 !IRC_WriteStrClient(from, RPL_WHOISLOGGEDIN_MSG,
403 Client_ID(from), Client_ID(c),
404 Client_AccountName(c)))
407 /* Local client and requester is the user itself or an IRC Op? */
408 if (Client_Conn(c) > NONE &&
409 (from == c || (!Conf_MorePrivacy && Client_HasMode(from, 'o')))) {
410 /* Client hostname */
411 if (!IRC_WriteStrClient(from, RPL_WHOISHOST_MSG,
412 Client_ID(from), Client_ID(c),
413 Client_Hostname(c), Client_IPAText(c)))
416 if (!IRC_WriteStrClient(from, RPL_WHOISMODES_MSG,
417 Client_ID(from), Client_ID(c), Client_Modes(c)))
421 /* Idle and signon time (local clients only!) */
422 if (!Conf_MorePrivacy && Client_Conn(c) > NONE &&
423 !IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
424 Client_ID(from), Client_ID(c),
425 (unsigned long)Conn_GetIdle(Client_Conn(c)),
426 (unsigned long)Conn_GetSignon(Client_Conn(c))))
430 if (Client_HasMode(c, 'a') &&
431 !IRC_WriteStrClient(from, RPL_AWAY_MSG,
432 Client_ID(from), Client_ID(c), Client_Away(c)))
439 WHOWAS_EntryWrite(CLIENT *prefix, WHOWAS *entry)
443 (void)strftime(t_str, sizeof(t_str), "%a %b %d %H:%M:%S %Y",
444 localtime(&entry->time));
446 if (!IRC_WriteStrClient(prefix, RPL_WHOWASUSER_MSG, Client_ID(prefix),
447 entry->id, entry->user, entry->host, entry->info))
450 return IRC_WriteStrClient(prefix, RPL_WHOISSERVER_MSG, Client_ID(prefix),
451 entry->id, entry->server, t_str);
456 Show_MOTD_SSLInfo(CLIENT *Client)
458 char buf[COMMAND_LEN];
461 if (Conn_GetCipherInfo(Client_Conn(Client), c_str, sizeof(c_str))) {
462 snprintf(buf, sizeof(buf), "Connected using Cipher %s", c_str);
463 if (!IRC_WriteStrClient(Client, RPL_MOTD_MSG,
464 Client_ID(Client), buf))
468 if (Conn_GetCertFp(Client_Conn(Client))) {
469 snprintf(buf, sizeof(buf),
470 "Your client certificate fingerprint is: %s",
471 Conn_GetCertFp(Client_Conn(Client)));
472 if (!IRC_WriteStrClient(Client, RPL_MOTD_MSG,
473 Client_ID(Client), buf))
481 Show_MOTD_SSLInfo(UNUSED CLIENT *c)
487 /* Global functions */
490 * Handler for the IRC command "ADMIN".
492 * @param Client The client from which this command has been received.
493 * @param Req Request structure with prefix and all parameters.
494 * @return CONNECTED or DISCONNECTED.
497 IRC_ADMIN(CLIENT *Client, REQUEST *Req )
499 CLIENT *target, *prefix;
501 assert( Client != NULL );
502 assert( Req != NULL );
504 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
505 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
508 if(target != Client_ThisServer()) {
509 IRC_WriteStrClientPrefix(target, prefix,
510 "ADMIN %s", Client_ID(target));
514 if (!IRC_WriteStrClient(Client, RPL_ADMINME_MSG, Client_ID(prefix),
517 if (!IRC_WriteStrClient(Client, RPL_ADMINLOC1_MSG, Client_ID(prefix),
520 if (!IRC_WriteStrClient(Client, RPL_ADMINLOC2_MSG, Client_ID(prefix),
523 if (!IRC_WriteStrClient(Client, RPL_ADMINEMAIL_MSG, Client_ID(prefix),
524 Conf_ServerAdminMail))
531 * Handler for the IRC command "INFO".
533 * @param Client The client from which this command has been received.
534 * @param Req Request structure with prefix and all parameters.
535 * @return CONNECTED or DISCONNECTED.
538 IRC_INFO(CLIENT * Client, REQUEST * Req)
540 CLIENT *target, *prefix;
543 assert(Client != NULL);
546 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
547 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
550 if (target != Client_ThisServer()) {
551 IRC_WriteStrClientPrefix(target, prefix, "INFO %s",
556 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix),
560 #if defined(__DATE__) && defined(__TIME__)
561 snprintf(msg, sizeof(msg), "Birth Date: %s at %s", __DATE__, __TIME__);
562 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
566 strlcpy(msg, "On-line since ", sizeof(msg));
567 strlcat(msg, NGIRCd_StartStr, sizeof(msg));
568 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
571 if (!IRC_WriteStrClient(Client, RPL_ENDOFINFO_MSG, Client_ID(prefix)))
578 * Handler for the IRC "ISON" command.
580 * @param Client The client from which this command has been received.
581 * @param Req Request structure with prefix and all parameters.
582 * @return CONNECTED or DISCONNECTED.
585 IRC_ISON( CLIENT *Client, REQUEST *Req )
587 char rpl[COMMAND_LEN];
592 assert(Client != NULL);
595 strlcpy(rpl, RPL_ISON_MSG, sizeof rpl);
596 for (i = 0; i < Req->argc; i++) {
597 /* "All" ircd even parse ":<x> <y> ..." arguments and split
598 * them up; so we do the same ... */
599 ptr = strtok(Req->argv[i], " ");
602 c = Client_Search(ptr);
603 if (c && Client_Type(c) == CLIENT_USER) {
604 strlcat(rpl, Client_ID(c), sizeof(rpl));
605 strlcat(rpl, " ", sizeof(rpl));
607 ptr = strtok(NULL, " ");
610 ngt_TrimLastChr(rpl, ' ');
612 return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
616 * Handler for the IRC "LINKS" command.
618 * @param Client The client from which this command has been received.
619 * @param Req Request structure with prefix and all parameters.
620 * @return CONNECTED or DISCONNECTED.
623 IRC_LINKS(CLIENT *Client, REQUEST *Req)
625 CLIENT *target, *from, *c;
628 assert(Client != NULL);
631 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
633 /* Get pointer to server mask or "*", if none given */
635 mask = Req->argv[Req->argc - 1];
640 if (Req->argc == 2) {
641 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
642 if (target != Client_ThisServer()) {
643 IRC_WriteStrClientPrefix(target, from,
644 "LINKS %s %s", Client_ID(target),
652 if (Client_Type(c) == CLIENT_SERVER
653 && MatchCaseInsensitive(mask, Client_ID(c))) {
654 if (!IRC_WriteStrClient(from, RPL_LINKS_MSG,
655 Client_ID(from), Client_ID(c),
656 Client_ID(Client_TopServer(c)
657 ? Client_TopServer(c)
658 : Client_ThisServer()),
659 Client_Hops(c), Client_Info(c)))
664 return IRC_WriteStrClient(from, RPL_ENDOFLINKS_MSG,
665 Client_ID(from), mask);
669 * Handler for the IRC "LUSERS" command.
671 * @param Client The client from which this command has been received.
672 * @param Req Request structure with prefix and all parameters.
673 * @return CONNECTED or DISCONNECTED.
676 IRC_LUSERS( CLIENT *Client, REQUEST *Req )
678 CLIENT *target, *from;
680 assert( Client != NULL );
681 assert( Req != NULL );
683 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
684 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
687 if (target != Client_ThisServer()) {
688 IRC_WriteStrClientPrefix(target, from,
689 "LUSERS %s %s", Req->argv[0],
694 return IRC_Send_LUSERS(from);
698 * Handler for the IRC command "SERVLIST".
700 * @param Client The client from which this command has been received.
701 * @param Req Request structure with prefix and all parameters.
702 * @return CONNECTED or DISCONNECTED.
705 IRC_SERVLIST(CLIENT *Client, REQUEST *Req)
709 assert(Client != NULL);
712 if (Req->argc < 2 || strcmp(Req->argv[1], "0") == 0) {
713 for (c = Client_First(); c!= NULL; c = Client_Next(c)) {
714 if (Client_Type(c) != CLIENT_SERVICE)
716 if (Req->argc > 0 && !MatchCaseInsensitive(Req->argv[0],
719 if (!IRC_WriteStrClient(Client, RPL_SERVLIST_MSG,
720 Client_ID(Client), Client_Mask(c),
721 Client_Mask(Client_Introducer(c)), "*",
722 0, Client_Hops(c), Client_Info(c)))
727 return IRC_WriteStrClient(Client, RPL_SERVLISTEND_MSG, Client_ID(Client),
728 Req->argc > 0 ? Req->argv[0] : "*",
729 Req->argc > 1 ? Req->argv[1] : "0");
733 * Handler for the IRC command "MOTD".
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_MOTD( CLIENT *Client, REQUEST *Req )
742 CLIENT *from, *target;
744 assert( Client != NULL );
745 assert( Req != NULL );
747 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
748 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
751 if (target != Client_ThisServer()) {
752 IRC_WriteStrClientPrefix(target, from, "MOTD %s",
757 return IRC_Show_MOTD(from);
761 * Handler for the IRC command "NAMES".
763 * @param Client The client from which this command has been received.
764 * @param Req Request structure with prefix and all parameters.
765 * @return CONNECTED or DISCONNECTED.
768 IRC_NAMES( CLIENT *Client, REQUEST *Req )
770 char rpl[COMMAND_LEN], *ptr;
771 CLIENT *target, *from, *c;
774 assert( Client != NULL );
775 assert( Req != NULL );
777 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
778 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
781 if (target != Client_ThisServer()) {
782 IRC_WriteStrClientPrefix(target, from, "NAMES %s :%s",
783 Req->argv[0], Client_ID(target));
788 /* Return NAMES list for specific channels */
789 ptr = strtok(Req->argv[0], ",");
791 chan = Channel_Search(ptr);
792 if (chan && !IRC_Send_NAMES(from, chan))
794 if (!IRC_WriteStrClient(from, RPL_ENDOFNAMES_MSG,
795 Client_ID(from), ptr))
797 ptr = strtok( NULL, "," );
802 chan = Channel_First();
804 if (!IRC_Send_NAMES(from, chan))
806 chan = Channel_Next(chan);
809 /* Now print all clients which are not in any channel */
811 snprintf(rpl, sizeof(rpl), RPL_NAMREPLY_MSG, Client_ID(from), "*", "*");
813 if (Client_Type(c) == CLIENT_USER
814 && Channel_FirstChannelOf(c) == NULL
815 && !Client_HasMode(c, 'i'))
817 /* its a user, concatenate ... */
818 if (rpl[strlen(rpl) - 1] != ':')
819 strlcat(rpl, " ", sizeof(rpl));
820 strlcat(rpl, Client_ID(c), sizeof(rpl));
822 if (strlen(rpl) > COMMAND_LEN - CLIENT_NICK_LEN - 4) {
823 /* Line is gwoing too long, send now */
824 if (!IRC_WriteStrClient(from, "%s", rpl))
826 snprintf(rpl, sizeof(rpl), RPL_NAMREPLY_MSG,
827 Client_ID(from), "*", "*");
832 if (rpl[strlen(rpl) - 1] != ':' && !IRC_WriteStrClient(from, "%s", rpl))
835 return IRC_WriteStrClient(from, RPL_ENDOFNAMES_MSG, Client_ID(from), "*");
839 * Handler for the IRC command "STATS".
841 * @param Client The client from which this command has been received.
842 * @param Req Request structure with prefix and all parameters.
843 * @return CONNECTED or DISCONNECTED.
846 IRC_STATS( CLIENT *Client, REQUEST *Req )
848 CLIENT *from, *target, *cl;
853 unsigned int days, hrs, mins;
854 struct list_head *list;
855 struct list_elem *list_item;
856 bool more_links = false;
858 assert(Client != NULL);
861 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
862 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
865 if (target != Client_ThisServer()) {
866 IRC_WriteStrClientPrefix(target, from, "STATS %s %s",
867 Req->argv[0], Client_ID(target));
872 query = Req->argv[0][0] ? Req->argv[0][0] : '*';
877 case 'g': /* Network-wide bans ("G-Lines") */
879 case 'k': /* Server-local bans ("K-Lines") */
881 if (!Client_HasMode(from, 'o'))
882 return IRC_WriteErrClient(from, ERR_NOPRIVILEGES_MSG,
884 if (query == 'g' || query == 'G')
885 list = Class_GetList(CLASS_GLINE);
887 list = Class_GetList(CLASS_KLINE);
888 list_item = Lists_GetFirst(list);
890 if (!IRC_WriteStrClient(from, RPL_STATSXLINE_MSG,
891 Client_ID(from), query,
892 Lists_GetMask(list_item),
893 Lists_GetValidity(list_item),
894 Lists_GetReason(list_item)))
896 list_item = Lists_GetNext(list_item);
899 case 'L': /* Link status (servers and user links) */
900 if (!Op_Check(from, Req))
901 return Op_NoPrivileges(from, Req);
904 case 'l': /* Link status (servers and own link) */
905 time_now = time(NULL);
906 for (con = Conn_First(); con != NONE; con = Conn_Next(con)) {
907 cl = Conn_GetClient(con);
910 if (Client_Type(cl) == CLIENT_SERVER ||
912 (more_links && Client_Type(cl) == CLIENT_USER)) {
914 if (Conn_Options(con) & CONN_ZIP) {
915 if (!IRC_WriteStrClient
916 (from, RPL_STATSLINKINFOZIP_MSG,
917 Client_ID(from), Client_Mask(cl),
918 Conn_SendQ(con), Conn_SendMsg(con),
924 (long)(time_now - Conn_StartTime(con))))
929 if (!IRC_WriteStrClient
930 (from, RPL_STATSLINKINFO_MSG,
931 Client_ID(from), Client_Mask(cl),
932 Conn_SendQ(con), Conn_SendMsg(con),
933 Conn_SendBytes(con), Conn_RecvMsg(con),
935 (long)(time_now - Conn_StartTime(con))))
940 case 'm': /* IRC command status (usage count) */
942 cmd = Parse_GetCommandStruct();
943 for (; cmd->name; cmd++) {
944 if (cmd->lcount == 0 && cmd->rcount == 0)
946 if (!IRC_WriteStrClient
947 (from, RPL_STATSCOMMANDS_MSG, Client_ID(from),
948 cmd->name, cmd->lcount, cmd->bytes, cmd->rcount))
952 case 'u': /* Server uptime */
954 time_now = time(NULL) - NGIRCd_Start;
955 days = uptime_days(&time_now);
956 hrs = uptime_hrs(&time_now);
957 mins = uptime_mins(&time_now);
958 if (!IRC_WriteStrClient(from, RPL_STATSUPTIME, Client_ID(from),
959 days, hrs, mins, (unsigned int)time_now))
964 return IRC_WriteStrClient(from, RPL_ENDOFSTATS_MSG,
965 Client_ID(from), query);
969 * Handler for the IRC command "SUMMON".
971 * @param Client The client from which this command has been received.
972 * @param Req Request structure with prefix and all parameters.
973 * @return CONNECTED or DISCONNECTED.
976 IRC_SUMMON(CLIENT * Client, UNUSED REQUEST * Req)
978 assert(Client != NULL);
980 return IRC_WriteErrClient(Client, ERR_SUMMONDISABLED_MSG,
985 * Handler for the IRC command "TIME".
987 * @param Client The client from which this command has been received.
988 * @param Req Request structure with prefix and all parameters.
989 * @return CONNECTED or DISCONNECTED.
992 IRC_TIME( CLIENT *Client, REQUEST *Req )
994 CLIENT *from, *target;
998 assert(Client != NULL);
1001 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
1002 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
1005 if (target != Client_ThisServer()) {
1006 IRC_WriteStrClientPrefix(target, from, "TIME %s",
1012 (void)strftime(t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime(&t));
1013 return IRC_WriteStrClient(from, RPL_TIME_MSG, Client_ID(from),
1014 Client_ID(Client_ThisServer()), t_str);
1018 * Handler for the IRC command "USERHOST".
1020 * @param Client The client from which this command has been received.
1021 * @param Req Request structure with prefix and all parameters.
1022 * @return CONNECTED or DISCONNECTED.
1025 IRC_USERHOST(CLIENT *Client, REQUEST *Req)
1027 char rpl[COMMAND_LEN];
1031 assert(Client != NULL);
1032 assert(Req != NULL);
1039 strlcpy(rpl, RPL_USERHOST_MSG, sizeof rpl);
1040 for (i = 0; i < max; i++) {
1041 c = Client_Search(Req->argv[i]);
1042 if (c && (Client_Type(c) == CLIENT_USER)) {
1043 /* This Nick is "online" */
1044 strlcat(rpl, Client_ID(c), sizeof(rpl));
1045 if (Client_HasMode(c, 'o'))
1046 strlcat(rpl, "*", sizeof(rpl));
1047 strlcat(rpl, "=", sizeof(rpl));
1048 if (Client_HasMode(c, 'a'))
1049 strlcat(rpl, "-", sizeof(rpl));
1051 strlcat(rpl, "+", sizeof(rpl));
1052 strlcat(rpl, Client_User(c), sizeof(rpl));
1053 strlcat(rpl, "@", sizeof(rpl));
1054 strlcat(rpl, Client_HostnameDisplayed(c), sizeof(rpl));
1055 strlcat(rpl, " ", sizeof(rpl));
1058 ngt_TrimLastChr(rpl, ' ');
1060 return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
1061 } /* IRC_USERHOST */
1064 * Handler for the IRC command "USERS".
1066 * @param Client The client from which this command has been received.
1067 * @param Req Request structure with prefix and all parameters.
1068 * @return CONNECTED or DISCONNECTED.
1071 IRC_USERS(CLIENT * Client, UNUSED REQUEST * Req)
1073 assert(Client != NULL);
1075 return IRC_WriteErrClient(Client, ERR_USERSDISABLED_MSG,
1080 * Handler for the IRC command "VERSION".
1082 * @param Client The client from which this command has been received.
1083 * @param Req Request structure with prefix and all parameters.
1084 * @return CONNECTED or DISCONNECTED.
1087 IRC_VERSION( CLIENT *Client, REQUEST *Req )
1089 CLIENT *target, *prefix;
1091 assert( Client != NULL );
1092 assert( Req != NULL );
1094 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
1095 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
1098 if (target != Client_ThisServer()) {
1099 IRC_WriteStrClientPrefix(target, prefix, "VERSION %s",
1104 /* send version information */
1105 if (!IRC_WriteStrClient(Client, RPL_VERSION_MSG, Client_ID(prefix),
1106 PACKAGE_NAME, PACKAGE_VERSION,
1107 NGIRCd_DebugLevel, Conf_ServerName,
1108 NGIRCd_VersionAddition))
1109 return DISCONNECTED;
1112 /* send RPL_ISUPPORT(005) numerics */
1113 if (!IRC_Send_ISUPPORT(prefix))
1114 return DISCONNECTED;
1121 * Handler for the IRC "WHO" command.
1123 * @param Client The client from which this command has been received.
1124 * @param Req Request structure with prefix and all parameters.
1125 * @return CONNECTED or DISCONNECTED.
1128 IRC_WHO(CLIENT *Client, REQUEST *Req)
1133 assert (Client != NULL);
1134 assert (Req != NULL);
1137 if (Req->argc == 2) {
1138 if (strcmp(Req->argv[1], "o") == 0)
1142 return IRC_WriteErrClient(Client,
1143 ERR_NEEDMOREPARAMS_MSG,
1150 if (Req->argc >= 1) {
1151 /* Channel or mask given */
1152 chan = Channel_Search(Req->argv[0]);
1154 /* Members of a channel have been requested */
1155 return IRC_WHO_Channel(Client, chan, only_ops);
1157 if (strcmp(Req->argv[0], "0") != 0) {
1158 /* A mask has been given. But please note this RFC
1159 * stupidity: "0" is same as no arguments ... */
1160 return IRC_WHO_Mask(Client, Req->argv[0], only_ops);
1164 /* No channel or (valid) mask given */
1165 return IRC_WHO_Mask(Client, NULL, only_ops);
1169 * Handler for the IRC "WHOIS" 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_WHOIS( CLIENT *Client, REQUEST *Req )
1178 CLIENT *from, *target, *c;
1179 unsigned int match_count = 0, found = 0;
1180 bool has_wildcards, is_remote;
1181 bool got_wildcard = false;
1182 char mask[COMMAND_LEN], *query;
1184 assert( Client != NULL );
1185 assert( Req != NULL );
1187 /* Wrong number of parameters? */
1189 return IRC_WriteErrClient(Client, ERR_NONICKNAMEGIVEN_MSG,
1192 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
1193 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
1195 /* Get target server for this command */
1196 if (Req->argc > 1) {
1197 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, Client)
1199 target = Client_ThisServer();
1201 assert(target != NULL);
1204 if (target != Client_ThisServer()) {
1205 IRC_WriteStrClientPrefix(target, from, "WHOIS %s :%s",
1206 Req->argv[0], Req->argv[1]);
1210 is_remote = Client_Conn(from) < 0;
1211 strlcpy(mask, Req->argv[Req->argc - 1], sizeof(mask));
1212 for (query = strtok(ngt_LowerStr(mask), ",");
1214 query = strtok(NULL, ","), found++)
1216 has_wildcards = query[strcspn(query, "*?")] != 0;
1218 * follows ircd 2.10 implementation:
1219 * - handle up to 3 targets
1220 * - no wildcards for remote clients
1221 * - only one wildcard target per local client
1223 * Also, at most MAX_RPL_WHOIS matches are returned.
1225 if (!has_wildcards || is_remote) {
1226 c = Client_Search(query);
1227 if (c && (Client_Type(c) == CLIENT_USER
1228 || Client_Type(c) == CLIENT_SERVICE)) {
1229 if (!IRC_WHOIS_SendReply(Client, from, c))
1230 return DISCONNECTED;
1232 if (!IRC_WriteErrClient(Client,
1236 return DISCONNECTED;
1241 /* we already handled one wildcard query */
1242 if (!IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
1243 Client_ID(Client), query))
1244 return DISCONNECTED;
1247 got_wildcard = true;
1248 /* Increase penalty for wildcard queries */
1249 IRC_SetPenalty(Client, 3);
1251 for (c = Client_First(); c; c = Client_Next(c)) {
1252 if (IRC_CheckListTooBig(Client, match_count,
1253 MAX_RPL_WHOIS, "WHOIS"))
1256 if (Client_Type(c) != CLIENT_USER)
1258 if (!MatchCaseInsensitive(query, Client_ID(c)))
1260 if (!IRC_WHOIS_SendReply(Client, from, c))
1261 return DISCONNECTED;
1266 if (match_count == 0)
1267 IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
1269 Req->argv[Req->argc - 1]);
1271 return IRC_WriteStrClient(from, RPL_ENDOFWHOIS_MSG,
1272 Client_ID(from), Req->argv[Req->argc - 1]);
1276 * Handler for the IRC "WHOWAS" command.
1278 * @param Client The client from which this command has been received.
1279 * @param Req Request structure with prefix and all parameters.
1280 * @return CONNECTED or DISCONNECTED.
1283 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1285 CLIENT *target, *prefix;
1287 char tok_buf[COMMAND_LEN];
1288 int max, last, count, i, nc;
1291 assert( Client != NULL );
1292 assert( Req != NULL );
1294 /* Wrong number of parameters? */
1296 return IRC_WriteErrClient(Client, ERR_NONICKNAMEGIVEN_MSG,
1299 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 3)
1300 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
1301 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 2, prefix)
1303 /* Do not reveal any info on disconnected users? */
1304 if (Conf_MorePrivacy)
1308 if (target != Client_ThisServer()) {
1309 IRC_WriteStrClientPrefix(target, prefix, "WHOWAS %s %s %s",
1310 Req->argv[0], Req->argv[1],
1315 whowas = Client_GetWhowas( );
1316 last = Client_GetLastWhowasIndex( );
1320 max = DEF_RPL_WHOWAS;
1321 if (Req->argc > 1) {
1322 max = atoi(Req->argv[1]);
1324 max = MAX_RPL_WHOWAS;
1328 * Break up the nick argument into a list of nicks, if applicable
1329 * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1331 strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1332 nick = strtok(tok_buf, ",");
1334 for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1338 if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1339 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1340 return DISCONNECTED;
1344 /* previous entry */
1347 /* "underflow", wrap around */
1351 if (nc && count >= max)
1353 } while (i != last);
1355 if (nc == 0 && !IRC_WriteErrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1356 Client_ID(prefix), nick))
1357 return DISCONNECTED;
1359 return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG,
1360 Client_ID(prefix), Req->argv[0]);
1364 * Send LUSERS reply to a client.
1366 * @param Client The receipient of the information.
1367 * @return CONNECTED or DISCONNECTED.
1370 IRC_Send_LUSERS(CLIENT *Client)
1377 assert(Client != NULL);
1379 /* Users, services and servers in the network */
1380 if (!IRC_WriteStrClient(Client, RPL_LUSERCLIENT_MSG, Client_ID(Client),
1381 Client_UserCount(), Client_ServiceCount(),
1382 Client_ServerCount()))
1383 return DISCONNECTED;
1385 /* Number of IRC operators */
1386 cnt = Client_OperCount( );
1388 if (!IRC_WriteStrClient(Client, RPL_LUSEROP_MSG,
1389 Client_ID(Client), cnt))
1390 return DISCONNECTED;
1393 /* Unknown connections */
1394 cnt = Client_UnknownCount( );
1396 if (!IRC_WriteStrClient(Client, RPL_LUSERUNKNOWN_MSG,
1397 Client_ID(Client), cnt))
1398 return DISCONNECTED;
1401 /* Number of created channels */
1402 if (!IRC_WriteStrClient(Client, RPL_LUSERCHANNELS_MSG,
1404 Channel_CountVisible(Client)))
1405 return DISCONNECTED;
1407 /* Number of local users, services and servers */
1408 if (!IRC_WriteStrClient(Client, RPL_LUSERME_MSG, Client_ID(Client),
1409 Client_MyUserCount(), Client_MyServiceCount(),
1410 Client_MyServerCount()))
1411 return DISCONNECTED;
1414 /* Maximum number of local users */
1415 cnt = Client_MyUserCount();
1416 max = Client_MyMaxUserCount();
1417 if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1418 cnt, max, cnt, max))
1419 return DISCONNECTED;
1420 /* Maximum number of users in the network */
1421 cnt = Client_UserCount();
1422 max = Client_MaxUserCount();
1423 if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1424 cnt, max, cnt, max))
1425 return DISCONNECTED;
1426 /* Connection counters */
1427 if (! IRC_WriteStrClient(Client, RPL_STATSCONN_MSG, Client_ID(Client),
1428 Conn_CountMax(), Conn_CountAccepted()))
1429 return DISCONNECTED;
1433 } /* IRC_Send_LUSERS */
1436 IRC_Show_MOTD( CLIENT *Client )
1439 size_t len_tot, len_str;
1441 assert( Client != NULL );
1443 len_tot = array_bytes(&Conf_Motd);
1444 if (len_tot == 0 && !Conn_UsesSSL(Client_Conn(Client)))
1445 return IRC_WriteErrClient(Client, ERR_NOMOTD_MSG, Client_ID(Client));
1447 if (!IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG, Client_ID(Client),
1448 Client_ID(Client_ThisServer())))
1449 return DISCONNECTED;
1451 line = array_start(&Conf_Motd);
1452 while (len_tot > 0) {
1453 len_str = strlen(line) + 1;
1455 assert(len_tot >= len_str);
1458 if (!IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID(Client), line))
1459 return DISCONNECTED;
1463 if (!Show_MOTD_SSLInfo(Client))
1464 return DISCONNECTED;
1466 if (!IRC_WriteStrClient(Client, RPL_ENDOFMOTD_MSG, Client_ID(Client)))
1467 return DISCONNECTED;
1469 if (*Conf_CloakHost)
1470 return IRC_WriteStrClient(Client, RPL_HOSTHIDDEN_MSG,
1472 Client_Hostname(Client));
1475 } /* IRC_Show_MOTD */
1478 * Send NAMES reply for a specific client and channel.
1480 * @param Client The client requesting the NAMES information.
1481 * @param Chan The channel
1482 * @return CONNECTED or DISCONNECTED.
1485 IRC_Send_NAMES(CLIENT * Client, CHANNEL * Chan)
1487 bool is_visible, is_member;
1488 char str[COMMAND_LEN];
1492 assert(Client != NULL);
1493 assert(Chan != NULL);
1495 if (Channel_IsMemberOf(Chan, Client))
1500 /* Do not print info on channel memberships to anyone that is not member? */
1501 if (Conf_MorePrivacy && !is_member)
1504 /* Secret channel? */
1505 if (!is_member && Channel_HasMode(Chan, 's'))
1508 snprintf(str, sizeof(str), RPL_NAMREPLY_MSG, Client_ID(Client), "=",
1509 Channel_Name(Chan));
1510 cl2chan = Channel_FirstMember(Chan);
1512 cl = Channel_GetClient(cl2chan);
1514 if (Client_HasMode(cl, 'i'))
1519 if (is_member || is_visible) {
1520 if (str[strlen(str) - 1] != ':')
1521 strlcat(str, " ", sizeof(str));
1523 who_flags_qualifier(Client, Channel_UserModes(Chan, cl),
1525 strlcat(str, Client_ID(cl), sizeof(str));
1527 if (strlen(str) > (COMMAND_LEN - CLIENT_NICK_LEN - 4)) {
1528 if (!IRC_WriteStrClient(Client, "%s", str))
1529 return DISCONNECTED;
1530 snprintf(str, sizeof(str), RPL_NAMREPLY_MSG,
1531 Client_ID(Client), "=",
1532 Channel_Name(Chan));
1536 cl2chan = Channel_NextMember(Chan, cl2chan);
1538 if (str[strlen(str) - 1] != ':') {
1539 if (!IRC_WriteStrClient(Client, "%s", str))
1540 return DISCONNECTED;
1544 } /* IRC_Send_NAMES */
1547 * Send the ISUPPORT numeric (005).
1548 * This numeric indicates the features that are supported by this server.
1549 * See <http://www.irc.org/tech_docs/005.html> for details.
1552 IRC_Send_ISUPPORT(CLIENT * Client)
1554 if (Conf_Network[0] && !IRC_WriteStrClient(Client, RPL_ISUPPORTNET_MSG,
1557 return DISCONNECTED;
1558 if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1559 CHANTYPES, CHANTYPES, Conf_MaxJoins))
1560 return DISCONNECTED;
1561 return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1562 CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1563 COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1564 COMMAND_LEN - 113, MAX_HNDL_MODES_ARG,
1565 MAX_HNDL_CHANNEL_LISTS);
1566 } /* IRC_Send_ISUPPORT */