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, is_oper;
147 assert( Client != NULL );
148 assert( Chan != NULL );
150 is_member = Channel_IsMemberOf(Chan, Client);
151 is_oper = Client_HasMode(Client, 'o');
153 /* Secret channel? */
154 if (!is_member && !is_oper && Channel_HasMode(Chan, 's'))
155 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG,
156 Client_ID(Client), Channel_Name(Chan));
158 cl2chan = Channel_FirstMember(Chan);
159 for (; cl2chan ; cl2chan = Channel_NextMember(Chan, cl2chan)) {
160 c = Channel_GetClient(cl2chan);
162 is_ircop = Client_HasMode(c, 'o');
163 if (OnlyOps && !is_ircop)
166 is_visible = !Client_HasMode(c, 'i');
167 if (is_member || is_visible || is_oper) {
168 memset(flags, 0, sizeof(flags));
170 if (Client_HasMode(c, 'a'))
171 flags[0] = 'G'; /* away */
178 who_flags_qualifier(Client, Channel_UserModes(Chan, c),
179 flags, sizeof(flags));
181 if (!write_whoreply(Client, c, Channel_Name(Chan),
188 /* If there are a lot of clients, increase the penalty a bit */
189 if (count > MAX_RPL_WHO)
190 IRC_SetPenalty(Client, 1);
192 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client),
197 * Send WHO reply for a "mask target" ("WHO m*sk").
199 * @param Client Client requesting the information.
200 * @param Mask Mask being requested or NULL for "all" clients.
201 * @param OnlyOps Only display IRC operators.
202 * @return CONNECTED or DISCONNECTED.
205 IRC_WHO_Mask(CLIENT *Client, char *Mask, bool OnlyOps)
210 bool client_match, is_visible;
214 assert (Client != NULL);
219 IRC_SetPenalty(Client, 3);
220 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
221 if (Client_Type(c) != CLIENT_USER)
224 if (OnlyOps && !Client_HasMode(c, 'o'))
228 /* Match pattern against user host/server/name/nick */
229 client_match = MatchCaseInsensitive(Mask,
232 client_match = MatchCaseInsensitive(Mask,
233 Client_ID(Client_Introducer(c)));
235 client_match = MatchCaseInsensitive(Mask,
238 client_match = MatchCaseInsensitive(Mask,
241 continue; /* no match: skip this client */
244 is_visible = !Client_HasMode(c, 'i');
246 /* Target client is invisible, but mask matches exactly? */
247 if (!is_visible && Mask && strcasecmp(Client_ID(c), Mask) == 0)
250 /* Target still invisible, but are both on the same channel? */
252 cl2chan = Channel_FirstChannelOf(Client);
253 while (cl2chan && !is_visible) {
254 chan = Channel_GetChannel(cl2chan);
255 if (Channel_IsMemberOf(chan, c))
257 cl2chan = Channel_NextChannelOf(Client, cl2chan);
261 if (!is_visible) /* target user is not visible */
264 if (IRC_CheckListTooBig(Client, count, MAX_RPL_WHO, "WHO"))
267 memset(flags, 0, sizeof(flags));
269 if (Client_HasMode(c, 'a'))
270 flags[0] = 'G'; /* away */
274 if (Client_HasMode(c, 'o'))
277 if (!write_whoreply(Client, c, "*", flags))
282 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client),
287 * Generate WHOIS reply of one actual client.
289 * @param Client The client from which this command has been received.
290 * @param from The client requesting the information ("originator").
291 * @param c The client of which information should be returned.
292 * @return CONNECTED or DISCONNECTED.
295 IRC_WHOIS_SendReply(CLIENT *Client, CLIENT *from, CLIENT *c)
297 char str[COMMAND_LEN];
301 assert(Client != NULL);
302 assert(from != NULL);
305 /* Nick, user, hostname and client info */
306 if (!IRC_WriteStrClient(from, RPL_WHOISUSER_MSG, Client_ID(from),
307 Client_ID(c), Client_User(c),
308 Client_HostnameDisplayed(c), Client_Info(c)))
312 if (!IRC_WriteStrClient(from, RPL_WHOISSERVER_MSG, Client_ID(from),
313 Client_ID(c), Client_ID(Client_Introducer(c)),
314 Client_Info(Client_Introducer(c))))
317 /* Channels, show only if client has no +I or if from is oper */
318 if(!(Client_HasMode(c, 'I')) || Client_HasMode(from, 'o')) {
319 snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
320 Client_ID(from), Client_ID(c));
321 cl2chan = Channel_FirstChannelOf(c);
323 chan = Channel_GetChannel(cl2chan);
324 assert(chan != NULL);
327 cl2chan = Channel_NextChannelOf(c, cl2chan);
329 /* Secret channel? */
330 if (Channel_HasMode(chan, 's')
331 && !Channel_IsMemberOf(chan, Client))
334 /* Local channel and request is not from a user? */
335 if (Client_Type(Client) == CLIENT_SERVER
336 && Channel_IsLocal(chan))
339 /* Concatenate channel names */
340 if (str[strlen(str) - 1] != ':')
341 strlcat(str, " ", sizeof(str));
343 who_flags_qualifier(Client, Channel_UserModes(chan, c),
345 strlcat(str, Channel_Name(chan), sizeof(str));
347 if (strlen(str) > (COMMAND_LEN - CHANNEL_NAME_LEN - 4)) {
348 /* Line becomes too long: send it! */
349 if (!IRC_WriteStrClient(Client, "%s", str))
351 snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
352 Client_ID(from), Client_ID(c));
355 if(str[strlen(str) - 1] != ':') {
356 /* There is data left to send: */
357 if (!IRC_WriteStrClient(Client, "%s", str))
363 if (Client_Type(c) == CLIENT_SERVICE &&
364 !IRC_WriteStrClient(from, RPL_WHOISSERVICE_MSG,
365 Client_ID(from), Client_ID(c)))
369 if (Client_Type(c) != CLIENT_SERVICE &&
370 Client_HasMode(c, 'o') &&
371 !IRC_WriteStrClient(from, RPL_WHOISOPERATOR_MSG,
372 Client_ID(from), Client_ID(c)))
376 if (Client_HasMode(c, 'B') &&
377 !IRC_WriteStrClient(from, RPL_WHOISBOT_MSG,
378 Client_ID(from), Client_ID(c)))
381 /* Connected using SSL? */
382 if (Conn_UsesSSL(Client_Conn(c))) {
383 if (!IRC_WriteStrClient(from, RPL_WHOISSSL_MSG, Client_ID(from),
387 /* Certificate fingerprint? */
388 if (Conn_GetCertFp(Client_Conn(c)) &&
390 !IRC_WriteStrClient(from, RPL_WHOISCERTFP_MSG,
391 Client_ID(from), Client_ID(c),
392 Conn_GetCertFp(Client_Conn(c))))
396 /* Registered nickname? */
397 if (Client_HasMode(c, 'R') &&
398 !IRC_WriteStrClient(from, RPL_WHOISREGNICK_MSG,
399 Client_ID(from), Client_ID(c)))
402 /* Account name metadata? */
403 if (Client_AccountName(c) &&
404 !IRC_WriteStrClient(from, RPL_WHOISLOGGEDIN_MSG,
405 Client_ID(from), Client_ID(c),
406 Client_AccountName(c)))
409 /* Local client and requester is the user itself or an IRC Op? */
410 if (Client_Conn(c) > NONE &&
411 (from == c || Client_HasMode(from, 'o'))) {
412 /* Client hostname */
413 if (!IRC_WriteStrClient(from, RPL_WHOISHOST_MSG,
414 Client_ID(from), Client_ID(c),
415 Client_Hostname(c), Client_IPAText(c)))
418 if (!IRC_WriteStrClient(from, RPL_WHOISMODES_MSG,
419 Client_ID(from), Client_ID(c), Client_Modes(c)))
423 /* Idle and signon time (local clients only!) */
424 if (!Conf_MorePrivacy && Client_Conn(c) > NONE &&
425 !IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
426 Client_ID(from), Client_ID(c),
427 (unsigned long)Conn_GetIdle(Client_Conn(c)),
428 (unsigned long)Conn_GetSignon(Client_Conn(c))))
432 if (Client_HasMode(c, 'a') &&
433 !IRC_WriteStrClient(from, RPL_AWAY_MSG,
434 Client_ID(from), Client_ID(c), Client_Away(c)))
441 WHOWAS_EntryWrite(CLIENT *prefix, WHOWAS *entry)
445 (void)strftime(t_str, sizeof(t_str), "%a %b %d %H:%M:%S %Y",
446 localtime(&entry->time));
448 if (!IRC_WriteStrClient(prefix, RPL_WHOWASUSER_MSG, Client_ID(prefix),
449 entry->id, entry->user, entry->host, entry->info))
452 return IRC_WriteStrClient(prefix, RPL_WHOISSERVER_MSG, Client_ID(prefix),
453 entry->id, entry->server, t_str);
458 Show_MOTD_SSLInfo(CLIENT *Client)
460 char buf[COMMAND_LEN];
463 if (Conn_GetCipherInfo(Client_Conn(Client), c_str, sizeof(c_str))) {
464 snprintf(buf, sizeof(buf), "Connected using Cipher %s", c_str);
465 if (!IRC_WriteStrClient(Client, RPL_MOTD_MSG,
466 Client_ID(Client), buf))
470 if (Conn_GetCertFp(Client_Conn(Client))) {
471 snprintf(buf, sizeof(buf),
472 "Your client certificate fingerprint is: %s",
473 Conn_GetCertFp(Client_Conn(Client)));
474 if (!IRC_WriteStrClient(Client, RPL_MOTD_MSG,
475 Client_ID(Client), buf))
483 Show_MOTD_SSLInfo(UNUSED CLIENT *c)
489 /* Global functions */
492 * Handler for the IRC command "ADMIN".
494 * @param Client The client from which this command has been received.
495 * @param Req Request structure with prefix and all parameters.
496 * @return CONNECTED or DISCONNECTED.
499 IRC_ADMIN(CLIENT *Client, REQUEST *Req )
501 CLIENT *target, *prefix;
503 assert( Client != NULL );
504 assert( Req != NULL );
506 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
507 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
510 if(target != Client_ThisServer()) {
511 IRC_WriteStrClientPrefix(target, prefix,
512 "ADMIN %s", Client_ID(target));
516 if (!IRC_WriteStrClient(Client, RPL_ADMINME_MSG, Client_ID(prefix),
519 if (!IRC_WriteStrClient(Client, RPL_ADMINLOC1_MSG, Client_ID(prefix),
522 if (!IRC_WriteStrClient(Client, RPL_ADMINLOC2_MSG, Client_ID(prefix),
525 if (!IRC_WriteStrClient(Client, RPL_ADMINEMAIL_MSG, Client_ID(prefix),
526 Conf_ServerAdminMail))
533 * Handler for the IRC command "INFO".
535 * @param Client The client from which this command has been received.
536 * @param Req Request structure with prefix and all parameters.
537 * @return CONNECTED or DISCONNECTED.
540 IRC_INFO(CLIENT * Client, REQUEST * Req)
542 CLIENT *target, *prefix;
545 assert(Client != NULL);
548 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
549 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
552 if (target != Client_ThisServer()) {
553 IRC_WriteStrClientPrefix(target, prefix, "INFO %s",
558 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix),
562 #if defined(BIRTHDATE)
564 time_t t = BIRTHDATE;
565 (void)strftime(t_str, sizeof(t_str), "%a %b %d %Y at %H:%M:%S (%Z)",
567 snprintf(msg, sizeof(msg), "Birth Date: %s", t_str);
568 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
570 #elif defined(__DATE__) && defined(__TIME__)
571 snprintf(msg, sizeof(msg), "Birth Date: %s at %s", __DATE__, __TIME__);
572 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
576 strlcpy(msg, "On-line since ", sizeof(msg));
577 strlcat(msg, NGIRCd_StartStr, sizeof(msg));
578 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
581 if (!IRC_WriteStrClient(Client, RPL_ENDOFINFO_MSG, Client_ID(prefix)))
588 * Handler for the IRC "ISON" command.
590 * @param Client The client from which this command has been received.
591 * @param Req Request structure with prefix and all parameters.
592 * @return CONNECTED or DISCONNECTED.
595 IRC_ISON( CLIENT *Client, REQUEST *Req )
597 char rpl[COMMAND_LEN];
602 assert(Client != NULL);
605 strlcpy(rpl, RPL_ISON_MSG, sizeof rpl);
606 for (i = 0; i < Req->argc; i++) {
607 /* "All" ircd even parse ":<x> <y> ..." arguments and split
608 * them up; so we do the same ... */
609 ptr = strtok(Req->argv[i], " ");
612 c = Client_Search(ptr);
613 if (c && Client_Type(c) == CLIENT_USER) {
614 strlcat(rpl, Client_ID(c), sizeof(rpl));
615 strlcat(rpl, " ", sizeof(rpl));
617 ptr = strtok(NULL, " ");
620 ngt_TrimLastChr(rpl, ' ');
622 return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
626 * Handler for the IRC "LINKS" command.
628 * @param Client The client from which this command has been received.
629 * @param Req Request structure with prefix and all parameters.
630 * @return CONNECTED or DISCONNECTED.
633 IRC_LINKS(CLIENT *Client, REQUEST *Req)
635 CLIENT *target, *from, *c;
638 assert(Client != NULL);
641 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
643 /* Get pointer to server mask or "*", if none given */
645 mask = Req->argv[Req->argc - 1];
650 if (Req->argc == 2) {
651 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
652 if (target != Client_ThisServer()) {
653 IRC_WriteStrClientPrefix(target, from,
654 "LINKS %s %s", Client_ID(target),
662 if (Client_Type(c) == CLIENT_SERVER
663 && MatchCaseInsensitive(mask, Client_ID(c))) {
664 if (!IRC_WriteStrClient(from, RPL_LINKS_MSG,
665 Client_ID(from), Client_ID(c),
666 Client_ID(Client_TopServer(c)
667 ? Client_TopServer(c)
668 : Client_ThisServer()),
669 Client_Hops(c), Client_Info(c)))
674 return IRC_WriteStrClient(from, RPL_ENDOFLINKS_MSG,
675 Client_ID(from), mask);
679 * Handler for the IRC "LUSERS" command.
681 * @param Client The client from which this command has been received.
682 * @param Req Request structure with prefix and all parameters.
683 * @return CONNECTED or DISCONNECTED.
686 IRC_LUSERS( CLIENT *Client, REQUEST *Req )
688 CLIENT *target, *from;
690 assert( Client != NULL );
691 assert( Req != NULL );
693 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
694 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
697 if (target != Client_ThisServer()) {
698 IRC_WriteStrClientPrefix(target, from,
699 "LUSERS %s %s", Req->argv[0],
704 return IRC_Send_LUSERS(from);
708 * Handler for the IRC command "SERVLIST".
710 * @param Client The client from which this command has been received.
711 * @param Req Request structure with prefix and all parameters.
712 * @return CONNECTED or DISCONNECTED.
715 IRC_SERVLIST(CLIENT *Client, REQUEST *Req)
719 assert(Client != NULL);
722 if (Req->argc < 2 || strcmp(Req->argv[1], "0") == 0) {
723 for (c = Client_First(); c!= NULL; c = Client_Next(c)) {
724 if (Client_Type(c) != CLIENT_SERVICE)
726 if (Req->argc > 0 && !MatchCaseInsensitive(Req->argv[0],
729 if (!IRC_WriteStrClient(Client, RPL_SERVLIST_MSG,
730 Client_ID(Client), Client_Mask(c),
731 Client_Mask(Client_Introducer(c)), "*",
732 0, Client_Hops(c), Client_Info(c)))
737 return IRC_WriteStrClient(Client, RPL_SERVLISTEND_MSG, Client_ID(Client),
738 Req->argc > 0 ? Req->argv[0] : "*",
739 Req->argc > 1 ? Req->argv[1] : "0");
743 * Handler for the IRC command "MOTD".
745 * @param Client The client from which this command has been received.
746 * @param Req Request structure with prefix and all parameters.
747 * @return CONNECTED or DISCONNECTED.
750 IRC_MOTD( CLIENT *Client, REQUEST *Req )
752 CLIENT *from, *target;
754 assert( Client != NULL );
755 assert( Req != NULL );
757 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
758 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
761 if (target != Client_ThisServer()) {
762 IRC_WriteStrClientPrefix(target, from, "MOTD %s",
767 return IRC_Show_MOTD(from);
771 * Handler for the IRC command "NAMES".
773 * @param Client The client from which this command has been received.
774 * @param Req Request structure with prefix and all parameters.
775 * @return CONNECTED or DISCONNECTED.
778 IRC_NAMES( CLIENT *Client, REQUEST *Req )
780 char rpl[COMMAND_LEN], *ptr;
781 CLIENT *target, *from, *c;
784 assert( Client != NULL );
785 assert( Req != NULL );
787 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
788 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
791 if (target != Client_ThisServer()) {
792 IRC_WriteStrClientPrefix(target, from, "NAMES %s :%s",
793 Req->argv[0], Client_ID(target));
798 /* Return NAMES list for specific channels */
799 ptr = strtok(Req->argv[0], ",");
801 chan = Channel_Search(ptr);
802 if (chan && !IRC_Send_NAMES(from, chan))
804 if (!IRC_WriteStrClient(from, RPL_ENDOFNAMES_MSG,
805 Client_ID(from), ptr))
807 ptr = strtok( NULL, "," );
812 chan = Channel_First();
814 if (!IRC_Send_NAMES(from, chan))
816 chan = Channel_Next(chan);
819 /* Now print all clients which are not in any channel */
821 snprintf(rpl, sizeof(rpl), RPL_NAMREPLY_MSG, Client_ID(from), "*", "*");
823 if (Client_Type(c) == CLIENT_USER
824 && Channel_FirstChannelOf(c) == NULL
825 && !Client_HasMode(c, 'i'))
827 /* its a user, concatenate ... */
828 if (rpl[strlen(rpl) - 1] != ':')
829 strlcat(rpl, " ", sizeof(rpl));
830 strlcat(rpl, Client_ID(c), sizeof(rpl));
832 if (strlen(rpl) > COMMAND_LEN - CLIENT_NICK_LEN - 4) {
833 /* Line is gwoing too long, send now */
834 if (!IRC_WriteStrClient(from, "%s", rpl))
836 snprintf(rpl, sizeof(rpl), RPL_NAMREPLY_MSG,
837 Client_ID(from), "*", "*");
842 if (rpl[strlen(rpl) - 1] != ':' && !IRC_WriteStrClient(from, "%s", rpl))
845 return IRC_WriteStrClient(from, RPL_ENDOFNAMES_MSG, Client_ID(from), "*");
849 * Handler for the IRC command "STATS".
851 * @param Client The client from which this command has been received.
852 * @param Req Request structure with prefix and all parameters.
853 * @return CONNECTED or DISCONNECTED.
856 IRC_STATS( CLIENT *Client, REQUEST *Req )
858 CLIENT *from, *target, *cl;
863 unsigned int days, hrs, mins;
864 struct list_head *list;
865 struct list_elem *list_item;
866 bool more_links = false;
868 assert(Client != NULL);
871 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
872 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
875 if (target != Client_ThisServer()) {
876 IRC_WriteStrClientPrefix(target, from, "STATS %s %s",
877 Req->argv[0], Client_ID(target));
882 query = Req->argv[0][0] ? Req->argv[0][0] : '*';
887 case 'g': /* Network-wide bans ("G-Lines") */
889 case 'k': /* Server-local bans ("K-Lines") */
891 if (!Client_HasMode(from, 'o'))
892 return IRC_WriteErrClient(from, ERR_NOPRIVILEGES_MSG,
894 if (query == 'g' || query == 'G')
895 list = Class_GetList(CLASS_GLINE);
897 list = Class_GetList(CLASS_KLINE);
898 list_item = Lists_GetFirst(list);
900 if (!IRC_WriteStrClient(from, RPL_STATSXLINE_MSG,
901 Client_ID(from), query,
902 Lists_GetMask(list_item),
903 Lists_GetValidity(list_item),
904 Lists_GetReason(list_item)))
906 list_item = Lists_GetNext(list_item);
909 case 'L': /* Link status (servers and user links) */
910 if (!Op_Check(from, Req))
911 return Op_NoPrivileges(from, Req);
914 case 'l': /* Link status (servers and own link) */
915 time_now = time(NULL);
916 for (con = Conn_First(); con != NONE; con = Conn_Next(con)) {
917 cl = Conn_GetClient(con);
920 if (Client_Type(cl) == CLIENT_SERVER ||
922 (more_links && Client_Type(cl) == CLIENT_USER)) {
924 if (Conn_Options(con) & CONN_ZIP) {
925 if (!IRC_WriteStrClient
926 (from, RPL_STATSLINKINFOZIP_MSG,
927 Client_ID(from), Client_Mask(cl),
928 Conn_SendQ(con), Conn_SendMsg(con),
934 (long)(time_now - Conn_StartTime(con))))
939 if (!IRC_WriteStrClient
940 (from, RPL_STATSLINKINFO_MSG,
941 Client_ID(from), Client_Mask(cl),
942 Conn_SendQ(con), Conn_SendMsg(con),
943 Conn_SendBytes(con), Conn_RecvMsg(con),
945 (long)(time_now - Conn_StartTime(con))))
950 case 'm': /* IRC command status (usage count) */
952 cmd = Parse_GetCommandStruct();
953 for (; cmd->name; cmd++) {
954 if (cmd->lcount == 0 && cmd->rcount == 0)
956 if (!IRC_WriteStrClient
957 (from, RPL_STATSCOMMANDS_MSG, Client_ID(from),
958 cmd->name, cmd->lcount, cmd->bytes, cmd->rcount))
962 case 'u': /* Server uptime */
964 time_now = time(NULL) - NGIRCd_Start;
965 days = uptime_days(&time_now);
966 hrs = uptime_hrs(&time_now);
967 mins = uptime_mins(&time_now);
968 if (!IRC_WriteStrClient(from, RPL_STATSUPTIME, Client_ID(from),
969 days, hrs, mins, (unsigned int)time_now))
974 return IRC_WriteStrClient(from, RPL_ENDOFSTATS_MSG,
975 Client_ID(from), query);
979 * Handler for the IRC command "SUMMON".
981 * @param Client The client from which this command has been received.
982 * @param Req Request structure with prefix and all parameters.
983 * @return CONNECTED or DISCONNECTED.
986 IRC_SUMMON(CLIENT * Client, UNUSED REQUEST * Req)
988 assert(Client != NULL);
990 return IRC_WriteErrClient(Client, ERR_SUMMONDISABLED_MSG,
995 * Handler for the IRC command "TIME".
997 * @param Client The client from which this command has been received.
998 * @param Req Request structure with prefix and all parameters.
999 * @return CONNECTED or DISCONNECTED.
1002 IRC_TIME( CLIENT *Client, REQUEST *Req )
1004 CLIENT *from, *target;
1008 assert(Client != NULL);
1009 assert(Req != NULL);
1011 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
1012 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
1015 if (target != Client_ThisServer()) {
1016 IRC_WriteStrClientPrefix(target, from, "TIME %s",
1022 (void)strftime(t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime(&t));
1023 return IRC_WriteStrClient(from, RPL_TIME_MSG, Client_ID(from),
1024 Client_ID(Client_ThisServer()), t_str);
1028 * Handler for the IRC command "USERHOST".
1030 * @param Client The client from which this command has been received.
1031 * @param Req Request structure with prefix and all parameters.
1032 * @return CONNECTED or DISCONNECTED.
1035 IRC_USERHOST(CLIENT *Client, REQUEST *Req)
1037 char rpl[COMMAND_LEN];
1041 assert(Client != NULL);
1042 assert(Req != NULL);
1049 strlcpy(rpl, RPL_USERHOST_MSG, sizeof rpl);
1050 for (i = 0; i < max; i++) {
1051 c = Client_Search(Req->argv[i]);
1052 if (c && (Client_Type(c) == CLIENT_USER)) {
1053 /* This Nick is "online" */
1054 strlcat(rpl, Client_ID(c), sizeof(rpl));
1055 if (Client_HasMode(c, 'o'))
1056 strlcat(rpl, "*", sizeof(rpl));
1057 strlcat(rpl, "=", sizeof(rpl));
1058 if (Client_HasMode(c, 'a'))
1059 strlcat(rpl, "-", sizeof(rpl));
1061 strlcat(rpl, "+", sizeof(rpl));
1062 strlcat(rpl, Client_User(c), sizeof(rpl));
1063 strlcat(rpl, "@", sizeof(rpl));
1064 strlcat(rpl, Client_HostnameDisplayed(c), sizeof(rpl));
1065 strlcat(rpl, " ", sizeof(rpl));
1068 ngt_TrimLastChr(rpl, ' ');
1070 return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
1071 } /* IRC_USERHOST */
1074 * Handler for the IRC command "USERS".
1076 * @param Client The client from which this command has been received.
1077 * @param Req Request structure with prefix and all parameters.
1078 * @return CONNECTED or DISCONNECTED.
1081 IRC_USERS(CLIENT * Client, UNUSED REQUEST * Req)
1083 assert(Client != NULL);
1085 return IRC_WriteErrClient(Client, ERR_USERSDISABLED_MSG,
1090 * Handler for the IRC command "VERSION".
1092 * @param Client The client from which this command has been received.
1093 * @param Req Request structure with prefix and all parameters.
1094 * @return CONNECTED or DISCONNECTED.
1097 IRC_VERSION( CLIENT *Client, REQUEST *Req )
1099 CLIENT *target, *prefix;
1101 assert( Client != NULL );
1102 assert( Req != NULL );
1104 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
1105 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
1108 if (target != Client_ThisServer()) {
1109 IRC_WriteStrClientPrefix(target, prefix, "VERSION %s",
1114 /* send version information */
1115 if (!IRC_WriteStrClient(Client, RPL_VERSION_MSG, Client_ID(prefix),
1116 PACKAGE_NAME, PACKAGE_VERSION,
1117 NGIRCd_DebugLevel, Conf_ServerName,
1118 NGIRCd_VersionAddition))
1119 return DISCONNECTED;
1122 /* send RPL_ISUPPORT(005) numerics */
1123 if (!IRC_Send_ISUPPORT(prefix))
1124 return DISCONNECTED;
1131 * Handler for the IRC "WHO" command.
1133 * @param Client The client from which this command has been received.
1134 * @param Req Request structure with prefix and all parameters.
1135 * @return CONNECTED or DISCONNECTED.
1138 IRC_WHO(CLIENT *Client, REQUEST *Req)
1143 assert (Client != NULL);
1144 assert (Req != NULL);
1147 if (Req->argc == 2) {
1148 if (strcmp(Req->argv[1], "o") == 0)
1152 return IRC_WriteErrClient(Client,
1153 ERR_NEEDMOREPARAMS_MSG,
1160 if (Req->argc >= 1) {
1161 /* Channel or mask given */
1162 chan = Channel_Search(Req->argv[0]);
1164 /* Members of a channel have been requested */
1165 return IRC_WHO_Channel(Client, chan, only_ops);
1167 if (strcmp(Req->argv[0], "0") != 0) {
1168 /* A mask has been given. But please note this RFC
1169 * stupidity: "0" is same as no arguments ... */
1170 return IRC_WHO_Mask(Client, Req->argv[0], only_ops);
1174 /* No channel or (valid) mask given */
1175 return IRC_WHO_Mask(Client, NULL, only_ops);
1179 * Handler for the IRC "WHOIS" command.
1181 * @param Client The client from which this command has been received.
1182 * @param Req Request structure with prefix and all parameters.
1183 * @return CONNECTED or DISCONNECTED.
1186 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
1188 CLIENT *from, *target, *c;
1189 unsigned int match_count = 0, found = 0;
1190 bool has_wildcards, is_remote;
1191 bool got_wildcard = false;
1192 char mask[COMMAND_LEN], *query;
1194 assert( Client != NULL );
1195 assert( Req != NULL );
1197 /* Wrong number of parameters? */
1199 return IRC_WriteErrClient(Client, ERR_NONICKNAMEGIVEN_MSG,
1202 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
1203 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
1205 /* Get target server for this command */
1206 if (Req->argc > 1) {
1207 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, Client)
1209 target = Client_ThisServer();
1211 assert(target != NULL);
1214 if (target != Client_ThisServer()) {
1215 IRC_WriteStrClientPrefix(target, from, "WHOIS %s :%s",
1216 Req->argv[0], Req->argv[1]);
1220 is_remote = Client_Conn(from) < 0;
1221 strlcpy(mask, Req->argv[Req->argc - 1], sizeof(mask));
1222 for (query = strtok(ngt_LowerStr(mask), ",");
1224 query = strtok(NULL, ","), found++)
1226 has_wildcards = query[strcspn(query, "*?")] != 0;
1228 * follows ircd 2.10 implementation:
1229 * - handle up to 3 targets
1230 * - no wildcards for remote clients
1231 * - only one wildcard target per local client
1233 * Also, at most MAX_RPL_WHOIS matches are returned.
1235 if (!has_wildcards || is_remote) {
1236 c = Client_Search(query);
1237 if (c && (Client_Type(c) == CLIENT_USER
1238 || Client_Type(c) == CLIENT_SERVICE)) {
1239 if (!IRC_WHOIS_SendReply(Client, from, c))
1240 return DISCONNECTED;
1242 if (!IRC_WriteErrClient(Client,
1246 return DISCONNECTED;
1251 /* we already handled one wildcard query */
1252 if (!IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
1253 Client_ID(Client), query))
1254 return DISCONNECTED;
1257 got_wildcard = true;
1258 /* Increase penalty for wildcard queries */
1259 IRC_SetPenalty(Client, 3);
1261 for (c = Client_First(); c; c = Client_Next(c)) {
1262 if (IRC_CheckListTooBig(Client, match_count,
1263 MAX_RPL_WHOIS, "WHOIS"))
1266 if (Client_Type(c) != CLIENT_USER)
1268 if (!MatchCaseInsensitive(query, Client_ID(c)))
1270 if (!IRC_WHOIS_SendReply(Client, from, c))
1271 return DISCONNECTED;
1276 if (match_count == 0)
1277 IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
1279 Req->argv[Req->argc - 1]);
1281 return IRC_WriteStrClient(from, RPL_ENDOFWHOIS_MSG,
1282 Client_ID(from), Req->argv[Req->argc - 1]);
1286 * Handler for the IRC "WHOWAS" command.
1288 * @param Client The client from which this command has been received.
1289 * @param Req Request structure with prefix and all parameters.
1290 * @return CONNECTED or DISCONNECTED.
1293 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1295 CLIENT *target, *prefix;
1297 char tok_buf[COMMAND_LEN];
1298 int max, last, count, i, nc;
1301 assert( Client != NULL );
1302 assert( Req != NULL );
1304 /* Wrong number of parameters? */
1306 return IRC_WriteErrClient(Client, ERR_NONICKNAMEGIVEN_MSG,
1309 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 3)
1310 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
1311 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 2, prefix)
1313 /* Do not reveal any info on disconnected users? */
1314 if (Conf_MorePrivacy)
1318 if (target != Client_ThisServer()) {
1319 IRC_WriteStrClientPrefix(target, prefix, "WHOWAS %s %s %s",
1320 Req->argv[0], Req->argv[1],
1325 whowas = Client_GetWhowas( );
1326 last = Client_GetLastWhowasIndex( );
1330 max = DEF_RPL_WHOWAS;
1331 if (Req->argc > 1) {
1332 max = atoi(Req->argv[1]);
1334 max = MAX_RPL_WHOWAS;
1338 * Break up the nick argument into a list of nicks, if applicable
1339 * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1341 strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1342 nick = strtok(tok_buf, ",");
1344 for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1348 if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1349 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1350 return DISCONNECTED;
1354 /* previous entry */
1357 /* "underflow", wrap around */
1361 if (nc && count >= max)
1363 } while (i != last);
1365 if (nc == 0 && !IRC_WriteErrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1366 Client_ID(prefix), nick))
1367 return DISCONNECTED;
1369 return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG,
1370 Client_ID(prefix), Req->argv[0]);
1374 * Send LUSERS reply to a client.
1376 * @param Client The receipient of the information.
1377 * @return CONNECTED or DISCONNECTED.
1380 IRC_Send_LUSERS(CLIENT *Client)
1387 assert(Client != NULL);
1389 /* Users, services and servers in the network */
1390 if (!IRC_WriteStrClient(Client, RPL_LUSERCLIENT_MSG, Client_ID(Client),
1391 Client_UserCount(), Client_ServiceCount(),
1392 Client_ServerCount()))
1393 return DISCONNECTED;
1395 /* Number of IRC operators */
1396 cnt = Client_OperCount( );
1398 if (!IRC_WriteStrClient(Client, RPL_LUSEROP_MSG,
1399 Client_ID(Client), cnt))
1400 return DISCONNECTED;
1403 /* Unknown connections */
1404 cnt = Client_UnknownCount( );
1406 if (!IRC_WriteStrClient(Client, RPL_LUSERUNKNOWN_MSG,
1407 Client_ID(Client), cnt))
1408 return DISCONNECTED;
1411 /* Number of created channels */
1412 if (!IRC_WriteStrClient(Client, RPL_LUSERCHANNELS_MSG,
1414 Channel_CountVisible(Client)))
1415 return DISCONNECTED;
1417 /* Number of local users, services and servers */
1418 if (!IRC_WriteStrClient(Client, RPL_LUSERME_MSG, Client_ID(Client),
1419 Client_MyUserCount(), Client_MyServiceCount(),
1420 Client_MyServerCount()))
1421 return DISCONNECTED;
1424 /* Maximum number of local users */
1425 cnt = Client_MyUserCount();
1426 max = Client_MyMaxUserCount();
1427 if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1428 cnt, max, cnt, max))
1429 return DISCONNECTED;
1430 /* Maximum number of users in the network */
1431 cnt = Client_UserCount();
1432 max = Client_MaxUserCount();
1433 if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1434 cnt, max, cnt, max))
1435 return DISCONNECTED;
1436 /* Connection counters */
1437 if (! IRC_WriteStrClient(Client, RPL_STATSCONN_MSG, Client_ID(Client),
1438 Conn_CountMax(), Conn_CountAccepted()))
1439 return DISCONNECTED;
1443 } /* IRC_Send_LUSERS */
1446 IRC_Show_MOTD( CLIENT *Client )
1449 size_t len_tot, len_str;
1451 assert( Client != NULL );
1453 len_tot = array_bytes(&Conf_Motd);
1454 if (len_tot == 0 && !Conn_UsesSSL(Client_Conn(Client)))
1455 return IRC_WriteErrClient(Client, ERR_NOMOTD_MSG, Client_ID(Client));
1457 if (!IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG, Client_ID(Client),
1458 Client_ID(Client_ThisServer())))
1459 return DISCONNECTED;
1461 line = array_start(&Conf_Motd);
1462 while (len_tot > 0) {
1463 len_str = strlen(line) + 1;
1465 assert(len_tot >= len_str);
1468 if (!IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID(Client), line))
1469 return DISCONNECTED;
1473 if (!Show_MOTD_SSLInfo(Client))
1474 return DISCONNECTED;
1476 if (!IRC_WriteStrClient(Client, RPL_ENDOFMOTD_MSG, Client_ID(Client)))
1477 return DISCONNECTED;
1479 if (*Conf_CloakHost)
1480 return IRC_WriteStrClient(Client, RPL_HOSTHIDDEN_MSG,
1482 Client_Hostname(Client));
1485 } /* IRC_Show_MOTD */
1488 * Send NAMES reply for a specific client and channel.
1490 * @param Client The client requesting the NAMES information.
1491 * @param Chan The channel
1492 * @return CONNECTED or DISCONNECTED.
1495 IRC_Send_NAMES(CLIENT * Client, CHANNEL * Chan)
1497 bool is_visible, is_member;
1498 char str[COMMAND_LEN];
1502 assert(Client != NULL);
1503 assert(Chan != NULL);
1505 if (Channel_IsMemberOf(Chan, Client))
1510 /* Do not print info on channel memberships to anyone that is not member? */
1511 if (Conf_MorePrivacy && !is_member)
1514 /* Secret channel? */
1515 if (!is_member && Channel_HasMode(Chan, 's'))
1518 snprintf(str, sizeof(str), RPL_NAMREPLY_MSG, Client_ID(Client), "=",
1519 Channel_Name(Chan));
1520 cl2chan = Channel_FirstMember(Chan);
1522 cl = Channel_GetClient(cl2chan);
1524 if (Client_HasMode(cl, 'i'))
1529 if (is_member || is_visible) {
1530 if (str[strlen(str) - 1] != ':')
1531 strlcat(str, " ", sizeof(str));
1533 who_flags_qualifier(Client, Channel_UserModes(Chan, cl),
1535 strlcat(str, Client_ID(cl), sizeof(str));
1537 if (strlen(str) > (COMMAND_LEN - CLIENT_NICK_LEN - 4)) {
1538 if (!IRC_WriteStrClient(Client, "%s", str))
1539 return DISCONNECTED;
1540 snprintf(str, sizeof(str), RPL_NAMREPLY_MSG,
1541 Client_ID(Client), "=",
1542 Channel_Name(Chan));
1546 cl2chan = Channel_NextMember(Chan, cl2chan);
1548 if (str[strlen(str) - 1] != ':') {
1549 if (!IRC_WriteStrClient(Client, "%s", str))
1550 return DISCONNECTED;
1554 } /* IRC_Send_NAMES */
1557 * Send the ISUPPORT numeric (005).
1558 * This numeric indicates the features that are supported by this server.
1559 * See <http://www.irc.org/tech_docs/005.html> for details.
1562 IRC_Send_ISUPPORT(CLIENT * Client)
1564 if (Conf_Network[0] && !IRC_WriteStrClient(Client, RPL_ISUPPORTNET_MSG,
1567 return DISCONNECTED;
1568 if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1569 CHANTYPES, CHANTYPES, Conf_MaxJoins))
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 */