2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2013 Alexander Barton (alex@barton.de) and Contributors.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * Please read the file COPYING, README and AUTHORS for more information.
28 #include "conn-func.h"
41 #include "irc-macros.h"
42 #include "irc-write.h"
43 #include "client-cap.h"
51 t_diff(time_t *t, const time_t d)
59 return (unsigned int)diff;
63 uptime_days(time_t *now)
65 return t_diff(now, 60 * 60 * 24);
69 uptime_hrs(time_t *now)
71 return t_diff(now, 60 * 60);
75 uptime_mins(time_t *now)
77 return t_diff(now, 60);
81 write_whoreply(CLIENT *Client, CLIENT *c, const char *channelname, const char *flags)
83 return IRC_WriteStrClient(Client, RPL_WHOREPLY_MSG, Client_ID(Client),
84 channelname, Client_User(c),
85 Client_HostnameDisplayed(c),
86 Client_ID(Client_Introducer(c)), Client_ID(c),
87 flags, Client_Hops(c), Client_Info(c));
91 who_flags_status(const char *client_modes)
93 if (strchr(client_modes, 'a'))
94 return "G"; /* away */
99 * Return channel user mode prefix(es).
101 * @param Client The client requesting the mode prefixes.
102 * @param chan_user_modes String with channel user modes.
103 * @param str String buffer to which the prefix(es) will be appended.
104 * @param len Size of "str" buffer.
105 * @return Pointer to "str".
108 who_flags_qualifier(CLIENT *Client, const char *chan_user_modes,
109 char *str, size_t len)
111 assert(Client != NULL);
113 if (Client_Cap(Client) & CLIENT_CAP_MULTI_PREFIX) {
114 if (strchr(chan_user_modes, 'q'))
115 strlcat(str, "~", len);
116 if (strchr(chan_user_modes, 'a'))
117 strlcat(str, "&", len);
118 if (strchr(chan_user_modes, 'o'))
119 strlcat(str, "@", len);
120 if (strchr(chan_user_modes, 'h'))
121 strlcat(str, "%", len);
122 if (strchr(chan_user_modes, 'v'))
123 strlcat(str, "+", len);
128 if (strchr(chan_user_modes, 'q'))
129 strlcat(str, "~", len);
130 else if (strchr(chan_user_modes, 'a'))
131 strlcat(str, "&", len);
132 else if (strchr(chan_user_modes, 'o'))
133 strlcat(str, "@", len);
134 else if (strchr(chan_user_modes, 'h'))
135 strlcat(str, "%", len);
136 else if (strchr(chan_user_modes, 'v'))
137 strlcat(str, "+", len);
143 * Send WHO reply for a "channel target" ("WHO #channel").
145 * @param Client Client requesting the information.
146 * @param Chan Channel being requested.
147 * @param OnlyOps Only display IRC operators.
148 * @return CONNECTED or DISCONNECTED.
151 IRC_WHO_Channel(CLIENT *Client, CHANNEL *Chan, bool OnlyOps)
153 bool is_visible, is_member, is_ircop;
155 const char *client_modes;
160 assert( Client != NULL );
161 assert( Chan != NULL );
163 IRC_SetPenalty(Client, 1);
165 is_member = Channel_IsMemberOf(Chan, Client);
167 /* Secret channel? */
168 if (!is_member && strchr(Channel_Modes(Chan), 's'))
169 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG,
170 Client_ID(Client), Channel_Name(Chan));
172 cl2chan = Channel_FirstMember(Chan);
173 for (; cl2chan ; cl2chan = Channel_NextMember(Chan, cl2chan)) {
174 c = Channel_GetClient(cl2chan);
176 client_modes = Client_Modes(c);
177 is_ircop = strchr(client_modes, 'o') != NULL;
178 if (OnlyOps && !is_ircop)
181 is_visible = strchr(client_modes, 'i') == NULL;
182 if (is_member || is_visible) {
183 strlcpy(flags, who_flags_status(client_modes),
186 strlcat(flags, "*", sizeof(flags));
188 who_flags_qualifier(Client, Channel_UserModes(Chan, c),
189 flags, sizeof(flags));
191 if (!write_whoreply(Client, c, Channel_Name(Chan),
198 /* If there are a lot of clients, augment penalty a bit */
199 if (count > MAX_RPL_WHO)
200 IRC_SetPenalty(Client, 1);
202 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client),
207 * Send WHO reply for a "mask target" ("WHO m*sk").
209 * @param Client Client requesting the information.
210 * @param Mask Mask being requested or NULL for "all" clients.
211 * @param OnlyOps Only display IRC operators.
212 * @return CONNECTED or DISCONNECTED.
215 IRC_WHO_Mask(CLIENT *Client, char *Mask, bool OnlyOps)
220 bool client_match, is_visible;
224 assert (Client != NULL);
229 IRC_SetPenalty(Client, 3);
230 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
231 if (Client_Type(c) != CLIENT_USER)
234 if (OnlyOps && !Client_HasMode(c, 'o'))
238 /* Match pattern against user host/server/name/nick */
239 client_match = MatchCaseInsensitive(Mask,
242 client_match = MatchCaseInsensitive(Mask,
243 Client_ID(Client_Introducer(c)));
245 client_match = MatchCaseInsensitive(Mask,
248 client_match = MatchCaseInsensitive(Mask,
251 continue; /* no match: skip this client */
254 is_visible = !Client_HasMode(c, 'i');
256 /* Target client is invisible, but mask matches exactly? */
257 if (!is_visible && Mask && strcasecmp(Client_ID(c), Mask) == 0)
260 /* Target still invisible, but are both on the same channel? */
262 cl2chan = Channel_FirstChannelOf(Client);
263 while (cl2chan && !is_visible) {
264 chan = Channel_GetChannel(cl2chan);
265 if (Channel_IsMemberOf(chan, c))
267 cl2chan = Channel_NextChannelOf(Client, cl2chan);
271 if (!is_visible) /* target user is not visible */
274 if (IRC_CheckListTooBig(Client, count, MAX_RPL_WHO, "WHO"))
277 strlcpy(flags, who_flags_status(Client_Modes(c)), sizeof(flags));
278 if (strchr(Client_Modes(c), 'o'))
279 strlcat(flags, "*", sizeof(flags));
281 if (!write_whoreply(Client, c, "*", flags))
286 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client),
291 * Generate WHOIS reply of one actual client.
293 * @param Client The client from which this command has been received.
294 * @param from The client requesting the information ("originator").
295 * @param c The client of which information should be returned.
296 * @return CONNECTED or DISCONNECTED.
299 IRC_WHOIS_SendReply(CLIENT *Client, CLIENT *from, CLIENT *c)
301 char str[COMMAND_LEN];
305 assert(Client != NULL);
306 assert(from != NULL);
309 /* Nick, user, hostname and client info */
310 if (!IRC_WriteStrClient(from, RPL_WHOISUSER_MSG, Client_ID(from),
311 Client_ID(c), Client_User(c),
312 Client_HostnameDisplayed(c), Client_Info(c)))
316 if (!IRC_WriteStrClient(from, RPL_WHOISSERVER_MSG, Client_ID(from),
317 Client_ID(c), Client_ID(Client_Introducer(c)),
318 Client_Info(Client_Introducer(c))))
322 snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
323 Client_ID(from), Client_ID(c));
324 cl2chan = Channel_FirstChannelOf(c);
326 chan = Channel_GetChannel(cl2chan);
327 assert(chan != NULL);
330 cl2chan = Channel_NextChannelOf(c, cl2chan);
332 /* Secret channel? */
333 if (strchr(Channel_Modes(chan), 's')
334 && !Channel_IsMemberOf(chan, Client))
337 /* Local channel and request is not from a user? */
338 if (Client_Type(Client) == CLIENT_SERVER
339 && Channel_IsLocal(chan))
342 /* Concatenate channel names */
343 if (str[strlen(str) - 1] != ':')
344 strlcat(str, " ", sizeof(str));
346 who_flags_qualifier(Client, Channel_UserModes(chan, c),
348 strlcat(str, Channel_Name(chan), sizeof(str));
350 if (strlen(str) > (COMMAND_LEN - CHANNEL_NAME_LEN - 4)) {
351 /* Line becomes too long: send it! */
352 if (!IRC_WriteStrClient(Client, "%s", str))
354 snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
355 Client_ID(from), Client_ID(c));
358 if(str[strlen(str) - 1] != ':') {
359 /* There is data left to send: */
360 if (!IRC_WriteStrClient(Client, "%s", str))
365 if (Client_Type(c) == CLIENT_SERVICE &&
366 !IRC_WriteStrClient(from, RPL_WHOISSERVICE_MSG,
367 Client_ID(from), Client_ID(c)))
371 if (Client_Type(c) != CLIENT_SERVICE &&
372 Client_HasMode(c, 'o') &&
373 !IRC_WriteStrClient(from, RPL_WHOISOPERATOR_MSG,
374 Client_ID(from), Client_ID(c)))
378 if (Client_HasMode(c, 'B') &&
379 !IRC_WriteStrClient(from, RPL_WHOISBOT_MSG,
380 Client_ID(from), Client_ID(c)))
383 /* Connected using SSL? */
384 if (Conn_UsesSSL(Client_Conn(c)) &&
385 !IRC_WriteStrClient(from, RPL_WHOISSSL_MSG, Client_ID(from),
389 /* Registered nickname? */
390 if (Client_HasMode(c, 'R') &&
391 !IRC_WriteStrClient(from, RPL_WHOISREGNICK_MSG,
392 Client_ID(from), Client_ID(c)))
395 /* Local client and requester is the user itself or an IRC Op? */
396 if (Client_Conn(c) > NONE &&
397 (from == c || (!Conf_MorePrivacy && Client_HasMode(from, 'o')))) {
398 /* Client hostname */
399 if (!IRC_WriteStrClient(from, RPL_WHOISHOST_MSG,
400 Client_ID(from), Client_ID(c), Client_Hostname(c),
401 Conn_GetIPAInfo(Client_Conn(c))))
404 if (!IRC_WriteStrClient(from, RPL_WHOISMODES_MSG,
405 Client_ID(from), Client_ID(c), Client_Modes(c)))
409 /* Idle and signon time (local clients only!) */
410 if (!Conf_MorePrivacy && Client_Conn(c) > NONE &&
411 !IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
412 Client_ID(from), Client_ID(c),
413 (unsigned long)Conn_GetIdle(Client_Conn(c)),
414 (unsigned long)Conn_GetSignon(Client_Conn(c))))
418 if (Client_HasMode(c, 'a') &&
419 !IRC_WriteStrClient(from, RPL_AWAY_MSG,
420 Client_ID(from), Client_ID(c), Client_Away(c)))
427 WHOWAS_EntryWrite(CLIENT *prefix, WHOWAS *entry)
431 (void)strftime(t_str, sizeof(t_str), "%a %b %d %H:%M:%S %Y",
432 localtime(&entry->time));
434 if (!IRC_WriteStrClient(prefix, RPL_WHOWASUSER_MSG, Client_ID(prefix),
435 entry->id, entry->user, entry->host, entry->info))
438 return IRC_WriteStrClient(prefix, RPL_WHOISSERVER_MSG, Client_ID(prefix),
439 entry->id, entry->server, t_str);
443 Show_MOTD_Start(CLIENT *Client)
445 return IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG,
446 Client_ID( Client ), Client_ID( Client_ThisServer( )));
450 Show_MOTD_Sendline(CLIENT *Client, const char *msg)
452 return IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID( Client ), msg);
456 Show_MOTD_End(CLIENT *Client)
458 if (!IRC_WriteStrClient(Client, RPL_ENDOFMOTD_MSG, Client_ID(Client)))
462 return IRC_WriteStrClient(Client, RPL_HOSTHIDDEN_MSG,
464 Client_Hostname(Client));
470 static bool Show_MOTD_SSLInfo(CLIENT *Client)
473 char buf[COMMAND_LEN] = "Connected using Cipher ";
475 if (!Conn_GetCipherInfo(Client_Conn(Client), buf + 23, sizeof buf - 23))
478 if (!Show_MOTD_Sendline(Client, buf))
485 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_SetPenalty(Client, 1);
508 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
509 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
510 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
513 if(target != Client_ThisServer()) {
514 IRC_WriteStrClientPrefix(target, prefix,
515 "ADMIN %s", Client_ID(target));
519 if (!IRC_WriteStrClient(Client, RPL_ADMINME_MSG, Client_ID(prefix),
522 if (!IRC_WriteStrClient(Client, RPL_ADMINLOC1_MSG, Client_ID(prefix),
525 if (!IRC_WriteStrClient(Client, RPL_ADMINLOC2_MSG, Client_ID(prefix),
528 if (!IRC_WriteStrClient(Client, RPL_ADMINEMAIL_MSG, Client_ID(prefix),
529 Conf_ServerAdminMail))
536 * Handler for the IRC command "INFO".
538 * @param Client The client from which this command has been received.
539 * @param Req Request structure with prefix and all parameters.
540 * @return CONNECTED or DISCONNECTED.
543 IRC_INFO(CLIENT * Client, REQUEST * Req)
545 CLIENT *target, *prefix;
548 assert(Client != NULL);
551 IRC_SetPenalty(Client, 2);
553 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
554 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
555 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
558 if (target != Client_ThisServer()) {
559 IRC_WriteStrClientPrefix(target, prefix, "INFO %s",
564 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix),
568 #if defined(__DATE__) && defined(__TIME__)
569 snprintf(msg, sizeof(msg), "Birth Date: %s at %s", __DATE__, __TIME__);
570 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
574 strlcpy(msg, "On-line since ", sizeof(msg));
575 strlcat(msg, NGIRCd_StartStr, sizeof(msg));
576 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
579 if (!IRC_WriteStrClient(Client, RPL_ENDOFINFO_MSG, Client_ID(prefix)))
586 * Handler for the IRC "ISON" command.
588 * @param Client The client from which this command has been received.
589 * @param Req Request structure with prefix and all parameters.
590 * @return CONNECTED or DISCONNECTED.
593 IRC_ISON( CLIENT *Client, REQUEST *Req )
595 char rpl[COMMAND_LEN];
600 assert(Client != NULL);
603 _IRC_ARGC_GE_OR_RETURN_(Client, Req, 1)
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_SetPenalty(Client, 1);
643 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
644 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
646 /* Get pointer to server mask or "*", if none given */
648 mask = Req->argv[Req->argc - 1];
653 if (Req->argc == 2) {
654 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
655 if (target != Client_ThisServer()) {
656 IRC_WriteStrClientPrefix(target, from,
657 "LINKS %s %s", Client_ID(target),
665 if (Client_Type(c) == CLIENT_SERVER
666 && MatchCaseInsensitive(mask, Client_ID(c))) {
667 if (!IRC_WriteStrClient(from, RPL_LINKS_MSG,
668 Client_ID(from), Client_ID(c),
669 Client_ID(Client_TopServer(c)
670 ? Client_TopServer(c)
671 : Client_ThisServer()),
672 Client_Hops(c), Client_Info(c)))
677 return IRC_WriteStrClient(from, RPL_ENDOFLINKS_MSG,
678 Client_ID(from), mask);
682 * Handler for the IRC "LUSERS" command.
684 * @param Client The client from which this command has been received.
685 * @param Req Request structure with prefix and all parameters.
686 * @return CONNECTED or DISCONNECTED.
689 IRC_LUSERS( CLIENT *Client, REQUEST *Req )
691 CLIENT *target, *from;
693 assert( Client != NULL );
694 assert( Req != NULL );
696 IRC_SetPenalty(Client, 1);
698 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
699 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
700 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
703 if (target != Client_ThisServer()) {
704 IRC_WriteStrClientPrefix(target, from,
705 "LUSERS %s %s", Req->argv[0],
710 return IRC_Send_LUSERS(from);
714 * Handler for the IRC command "SERVLIST".
716 * @param Client The client from which this command has been received.
717 * @param Req Request structure with prefix and all parameters.
718 * @return CONNECTED or DISCONNECTED.
721 IRC_SERVLIST(CLIENT *Client, REQUEST *Req)
725 IRC_SetPenalty(Client, 1);
727 assert(Client != NULL);
730 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
732 if (Req->argc < 2 || strcmp(Req->argv[1], "0") == 0) {
733 for (c = Client_First(); c!= NULL; c = Client_Next(c)) {
734 if (Client_Type(c) != CLIENT_SERVICE)
736 if (Req->argc > 0 && !MatchCaseInsensitive(Req->argv[0],
739 if (!IRC_WriteStrClient(Client, RPL_SERVLIST_MSG,
740 Client_ID(Client), Client_Mask(c),
741 Client_Mask(Client_Introducer(c)), "*",
742 0, Client_Hops(c), Client_Info(c)))
747 return IRC_WriteStrClient(Client, RPL_SERVLISTEND_MSG, Client_ID(Client),
748 Req->argc > 0 ? Req->argv[0] : "*",
749 Req->argc > 1 ? Req->argv[1] : "0");
753 * Handler for the IRC command "MOTD".
755 * @param Client The client from which this command has been received.
756 * @param Req Request structure with prefix and all parameters.
757 * @return CONNECTED or DISCONNECTED.
760 IRC_MOTD( CLIENT *Client, REQUEST *Req )
762 CLIENT *from, *target;
764 assert( Client != NULL );
765 assert( Req != NULL );
767 IRC_SetPenalty(Client, 3);
769 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
770 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
771 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
774 if (target != Client_ThisServer()) {
775 IRC_WriteStrClientPrefix(target, from, "MOTD %s",
780 return IRC_Show_MOTD(from);
784 * Handler for the IRC command "NAMES".
786 * @param Client The client from which this command has been received.
787 * @param Req Request structure with prefix and all parameters.
788 * @return CONNECTED or DISCONNECTED.
791 IRC_NAMES( CLIENT *Client, REQUEST *Req )
793 char rpl[COMMAND_LEN], *ptr;
794 CLIENT *target, *from, *c;
797 assert( Client != NULL );
798 assert( Req != NULL );
800 IRC_SetPenalty(Client, 1);
802 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
803 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
804 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
807 if (target != Client_ThisServer()) {
808 IRC_WriteStrClientPrefix(target, from, "NAMES %s :%s",
809 Req->argv[0], Client_ID(target));
814 /* Return NAMES list for specific channels */
815 ptr = strtok(Req->argv[0], ",");
817 chan = Channel_Search(ptr);
818 if (chan && !IRC_Send_NAMES(from, chan))
820 if (!IRC_WriteStrClient(from, RPL_ENDOFNAMES_MSG,
821 Client_ID(from), ptr))
823 ptr = strtok( NULL, "," );
828 chan = Channel_First();
830 if (!IRC_Send_NAMES(from, chan))
832 chan = Channel_Next(chan);
835 /* Now print all clients which are not in any channel */
837 snprintf(rpl, sizeof(rpl), RPL_NAMREPLY_MSG, Client_ID(from), "*", "*");
839 if (Client_Type(c) == CLIENT_USER
840 && Channel_FirstChannelOf(c) == NULL
841 && !strchr(Client_Modes(c), 'i'))
843 /* its a user, concatenate ... */
844 if (rpl[strlen(rpl) - 1] != ':')
845 strlcat(rpl, " ", sizeof(rpl));
846 strlcat(rpl, Client_ID(c), sizeof(rpl));
848 if (strlen(rpl) > COMMAND_LEN - CLIENT_NICK_LEN - 4) {
849 /* Line is gwoing too long, send now */
850 if (!IRC_WriteStrClient(from, "%s", rpl))
852 snprintf(rpl, sizeof(rpl), RPL_NAMREPLY_MSG,
853 Client_ID(from), "*", "*");
858 if (rpl[strlen(rpl) - 1] != ':' && !IRC_WriteStrClient(from, "%s", rpl))
861 return IRC_WriteStrClient(from, RPL_ENDOFNAMES_MSG, Client_ID(from), "*");
865 * Handler for the IRC command "STATS".
867 * @param Client The client from which this command has been received.
868 * @param Req Request structure with prefix and all parameters.
869 * @return CONNECTED or DISCONNECTED.
872 IRC_STATS( CLIENT *Client, REQUEST *Req )
874 CLIENT *from, *target, *cl;
879 unsigned int days, hrs, mins;
880 struct list_head *list;
881 struct list_elem *list_item;
883 assert(Client != NULL);
886 IRC_SetPenalty(Client, 2);
888 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
889 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
890 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
893 if (target != Client_ThisServer()) {
894 IRC_WriteStrClientPrefix(target, from, "STATS %s %s",
895 Req->argv[0], Client_ID(target));
900 query = Req->argv[0][0] ? Req->argv[0][0] : '*';
905 case 'g': /* Network-wide bans ("G-Lines") */
907 case 'k': /* Server-local bans ("K-Lines") */
909 if (!Client_HasMode(from, 'o'))
910 return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG,
912 if (query == 'g' || query == 'G')
913 list = Class_GetList(CLASS_GLINE);
915 list = Class_GetList(CLASS_KLINE);
916 list_item = Lists_GetFirst(list);
918 if (!IRC_WriteStrClient(from, RPL_STATSXLINE_MSG,
919 Client_ID(from), query,
920 Lists_GetMask(list_item),
921 Lists_GetValidity(list_item),
922 Lists_GetReason(list_item)))
924 list_item = Lists_GetNext(list_item);
927 case 'l': /* Link status (servers and own link) */
929 time_now = time(NULL);
930 for (con = Conn_First(); con != NONE; con = Conn_Next(con)) {
931 cl = Conn_GetClient(con);
934 if ((Client_Type(cl) == CLIENT_SERVER)
936 /* Server link or our own connection */
938 if (Conn_Options(con) & CONN_ZIP) {
939 if (!IRC_WriteStrClient
940 (from, RPL_STATSLINKINFOZIP_MSG,
941 Client_ID(from), Client_Mask(cl),
942 Conn_SendQ(con), Conn_SendMsg(con),
948 (long)(time_now - Conn_StartTime(con))))
953 if (!IRC_WriteStrClient
954 (from, RPL_STATSLINKINFO_MSG,
955 Client_ID(from), Client_Mask(cl),
956 Conn_SendQ(con), Conn_SendMsg(con),
957 Conn_SendBytes(con), Conn_RecvMsg(con),
959 (long)(time_now - Conn_StartTime(con))))
964 case 'm': /* IRC command status (usage count) */
966 cmd = Parse_GetCommandStruct();
967 for (; cmd->name; cmd++) {
968 if (cmd->lcount == 0 && cmd->rcount == 0)
970 if (!IRC_WriteStrClient
971 (from, RPL_STATSCOMMANDS_MSG, Client_ID(from),
972 cmd->name, cmd->lcount, cmd->bytes, cmd->rcount))
976 case 'u': /* Server uptime */
978 time_now = time(NULL) - NGIRCd_Start;
979 days = uptime_days(&time_now);
980 hrs = uptime_hrs(&time_now);
981 mins = uptime_mins(&time_now);
982 if (!IRC_WriteStrClient(from, RPL_STATSUPTIME, Client_ID(from),
983 days, hrs, mins, (unsigned int)time_now))
988 return IRC_WriteStrClient(from, RPL_ENDOFSTATS_MSG,
989 Client_ID(from), query);
993 * Handler for the IRC command "SUMMON".
995 * @param Client The client from which this command has been received.
996 * @param Req Request structure with prefix and all parameters.
997 * @return CONNECTED or DISCONNECTED.
1000 IRC_SUMMON(CLIENT * Client, UNUSED REQUEST * Req)
1002 assert(Client != NULL);
1004 IRC_SetPenalty(Client, 1);
1006 return IRC_WriteStrClient(Client, ERR_SUMMONDISABLED_MSG,
1011 * Handler for the IRC command "TIME".
1013 * @param Client The client from which this command has been received.
1014 * @param Req Request structure with prefix and all parameters.
1015 * @return CONNECTED or DISCONNECTED.
1018 IRC_TIME( CLIENT *Client, REQUEST *Req )
1020 CLIENT *from, *target;
1024 assert(Client != NULL);
1025 assert(Req != NULL);
1027 IRC_SetPenalty(Client, 1);
1029 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
1030 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
1031 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
1034 if (target != Client_ThisServer()) {
1035 IRC_WriteStrClientPrefix(target, from, "TIME %s",
1041 (void)strftime(t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime(&t));
1042 return IRC_WriteStrClient(from, RPL_TIME_MSG, Client_ID(from),
1043 Client_ID(Client_ThisServer()), t_str);
1047 * Handler for the IRC command "USERHOST".
1049 * @param Client The client from which this command has been received.
1050 * @param Req Request structure with prefix and all parameters.
1051 * @return CONNECTED or DISCONNECTED.
1054 IRC_USERHOST(CLIENT *Client, REQUEST *Req)
1056 char rpl[COMMAND_LEN];
1060 assert(Client != NULL);
1061 assert(Req != NULL);
1063 IRC_SetPenalty(Client, 1);
1065 _IRC_ARGC_GE_OR_RETURN_(Client, Req, 1)
1072 strlcpy(rpl, RPL_USERHOST_MSG, sizeof rpl);
1073 for (i = 0; i < max; i++) {
1074 c = Client_Search(Req->argv[i]);
1075 if (c && (Client_Type(c) == CLIENT_USER)) {
1076 /* This Nick is "online" */
1077 strlcat(rpl, Client_ID(c), sizeof(rpl));
1078 if (Client_HasMode(c, 'o'))
1079 strlcat(rpl, "*", sizeof(rpl));
1080 strlcat(rpl, "=", sizeof(rpl));
1081 if (Client_HasMode(c, 'a'))
1082 strlcat(rpl, "-", sizeof(rpl));
1084 strlcat(rpl, "+", sizeof(rpl));
1085 strlcat(rpl, Client_User(c), sizeof(rpl));
1086 strlcat(rpl, "@", sizeof(rpl));
1087 strlcat(rpl, Client_HostnameDisplayed(c), sizeof(rpl));
1088 strlcat(rpl, " ", sizeof(rpl));
1091 ngt_TrimLastChr(rpl, ' ');
1093 return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
1094 } /* IRC_USERHOST */
1097 * Handler for the IRC command "USERS".
1099 * @param Client The client from which this command has been received.
1100 * @param Req Request structure with prefix and all parameters.
1101 * @return CONNECTED or DISCONNECTED.
1104 IRC_USERS(CLIENT * Client, UNUSED REQUEST * Req)
1106 assert(Client != NULL);
1108 IRC_SetPenalty(Client, 1);
1110 return IRC_WriteStrClient(Client, ERR_USERSDISABLED_MSG,
1115 * Handler for the IRC command "VERSION".
1117 * @param Client The client from which this command has been received.
1118 * @param Req Request structure with prefix and all parameters.
1119 * @return CONNECTED or DISCONNECTED.
1122 IRC_VERSION( CLIENT *Client, REQUEST *Req )
1124 CLIENT *target, *prefix;
1126 assert( Client != NULL );
1127 assert( Req != NULL );
1129 IRC_SetPenalty(Client, 1);
1131 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
1132 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
1133 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
1136 if (target != Client_ThisServer()) {
1137 IRC_WriteStrClientPrefix(target, prefix, "VERSION %s",
1142 /* send version information */
1143 return IRC_WriteStrClient(Client, RPL_VERSION_MSG, Client_ID(prefix),
1144 PACKAGE_NAME, PACKAGE_VERSION,
1145 NGIRCd_DebugLevel, Conf_ServerName,
1146 NGIRCd_VersionAddition);
1150 * Handler for the IRC "WHO" command.
1152 * @param Client The client from which this command has been received.
1153 * @param Req Request structure with prefix and all parameters.
1154 * @return CONNECTED or DISCONNECTED.
1157 IRC_WHO(CLIENT *Client, REQUEST *Req)
1162 assert (Client != NULL);
1163 assert (Req != NULL);
1165 IRC_SetPenalty(Client, 1);
1167 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
1170 if (Req->argc == 2) {
1171 if (strcmp(Req->argv[1], "o") == 0)
1175 return IRC_WriteStrClient(Client,
1176 ERR_NEEDMOREPARAMS_MSG,
1182 if (Req->argc >= 1) {
1183 /* Channel or mask given */
1184 chan = Channel_Search(Req->argv[0]);
1186 /* Members of a channel have been requested */
1187 return IRC_WHO_Channel(Client, chan, only_ops);
1189 if (strcmp(Req->argv[0], "0") != 0) {
1190 /* A mask has been given. But please note this RFC
1191 * stupidity: "0" is same as no arguments ... */
1192 return IRC_WHO_Mask(Client, Req->argv[0], only_ops);
1196 /* No channel or (valid) mask given */
1197 IRC_SetPenalty(Client, 2);
1198 return IRC_WHO_Mask(Client, NULL, only_ops);
1202 * Handler for the IRC "WHOIS" command.
1204 * @param Client The client from which this command has been received.
1205 * @param Req Request structure with prefix and all parameters.
1206 * @return CONNECTED or DISCONNECTED.
1209 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
1211 CLIENT *from, *target, *c;
1212 unsigned int match_count = 0, found = 0;
1213 bool has_wildcards, is_remote;
1214 bool got_wildcard = false;
1215 char mask[COMMAND_LEN], *query;
1217 assert( Client != NULL );
1218 assert( Req != NULL );
1220 IRC_SetPenalty(Client, 1);
1222 /* Bad number of parameters? */
1223 if (Req->argc < 1 || Req->argc > 2)
1224 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1225 Client_ID(Client), Req->command);
1227 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
1229 /* Get target server for this command */
1230 if (Req->argc > 1) {
1231 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, Client)
1233 target = Client_ThisServer();
1235 assert(target != NULL);
1238 if (target != Client_ThisServer()) {
1239 IRC_WriteStrClientPrefix(target, from, "WHOIS %s :%s",
1240 Req->argv[0], Req->argv[1]);
1244 is_remote = Client_Conn(from) < 0;
1245 strlcpy(mask, Req->argv[Req->argc - 1], sizeof(mask));
1246 for (query = strtok(ngt_LowerStr(mask), ",");
1248 query = strtok(NULL, ","), found++)
1250 has_wildcards = query[strcspn(query, "*?")] != 0;
1252 * follows ircd 2.10 implementation:
1253 * - handle up to 3 targets
1254 * - no wildcards for remote clients
1255 * - only one wildcard target per local client
1257 * Also, at most MAX_RPL_WHOIS matches are returned.
1259 if (!has_wildcards || is_remote) {
1260 c = Client_Search(query);
1261 if (c && (Client_Type(c) == CLIENT_USER
1262 || Client_Type(c) == CLIENT_SERVICE)) {
1263 if (!IRC_WHOIS_SendReply(Client, from, c))
1264 return DISCONNECTED;
1266 if (!IRC_WriteStrClient(Client,
1270 return DISCONNECTED;
1275 /* we already handled one wildcard query */
1276 if (!IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1277 Client_ID(Client), query))
1278 return DISCONNECTED;
1281 got_wildcard = true;
1282 IRC_SetPenalty(Client, 3);
1284 for (c = Client_First(); c; c = Client_Next(c)) {
1285 if (IRC_CheckListTooBig(Client, match_count,
1286 MAX_RPL_WHOIS, "WHOIS"))
1289 if (Client_Type(c) != CLIENT_USER)
1291 if (!MatchCaseInsensitive(query, Client_ID(c)))
1293 if (!IRC_WHOIS_SendReply(Client, from, c))
1294 return DISCONNECTED;
1299 if (match_count == 0)
1300 IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1302 Req->argv[Req->argc - 1]);
1304 return IRC_WriteStrClient(from, RPL_ENDOFWHOIS_MSG,
1305 Client_ID(from), Req->argv[Req->argc - 1]);
1309 * Handler for the IRC "WHOWAS" command.
1311 * @param Client The client from which this command has been received.
1312 * @param Req Request structure with prefix and all parameters.
1313 * @return CONNECTED or DISCONNECTED.
1316 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1318 CLIENT *target, *prefix;
1320 char tok_buf[COMMAND_LEN];
1321 int max, last, count, i, nc;
1324 assert( Client != NULL );
1325 assert( Req != NULL );
1327 /* Do not reveal any info on disconnected users? */
1328 if (Conf_MorePrivacy)
1331 /* Wrong number of parameters? */
1333 return IRC_WriteStrClient(Client, ERR_NONICKNAMEGIVEN_MSG,
1336 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 3)
1337 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
1338 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 2, prefix)
1341 if (target != Client_ThisServer()) {
1342 IRC_WriteStrClientPrefix(target, prefix, "WHOWAS %s %s %s",
1343 Req->argv[0], Req->argv[1],
1348 whowas = Client_GetWhowas( );
1349 last = Client_GetLastWhowasIndex( );
1353 max = DEF_RPL_WHOWAS;
1354 if (Req->argc > 1) {
1355 max = atoi(Req->argv[1]);
1357 max = MAX_RPL_WHOWAS;
1361 * Break up the nick argument into a list of nicks, if applicable
1362 * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1364 strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1365 nick = strtok(tok_buf, ",");
1367 for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1371 if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1372 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1373 return DISCONNECTED;
1377 /* previous entry */
1380 /* "underflow", wrap around */
1384 if (nc && count >= max)
1386 } while (i != last);
1388 if (nc == 0 && !IRC_WriteStrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1389 Client_ID(prefix), nick))
1390 return DISCONNECTED;
1392 return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG,
1393 Client_ID(prefix), Req->argv[0]);
1397 * Send LUSERS reply to a client.
1399 * @param Client The receipient of the information.
1400 * @return CONNECTED or DISCONNECTED.
1403 IRC_Send_LUSERS(CLIENT *Client)
1410 assert(Client != NULL);
1412 /* Users, services and serevers in the network */
1413 if (!IRC_WriteStrClient(Client, RPL_LUSERCLIENT_MSG, Client_ID(Client),
1414 Client_UserCount(), Client_ServiceCount(),
1415 Client_ServerCount()))
1416 return DISCONNECTED;
1418 /* Number of IRC operators */
1419 cnt = Client_OperCount( );
1421 if (!IRC_WriteStrClient(Client, RPL_LUSEROP_MSG,
1422 Client_ID(Client), cnt))
1423 return DISCONNECTED;
1426 /* Unknown connections */
1427 cnt = Client_UnknownCount( );
1429 if (!IRC_WriteStrClient(Client, RPL_LUSERUNKNOWN_MSG,
1430 Client_ID(Client), cnt))
1431 return DISCONNECTED;
1434 /* Number of created channels */
1435 if (!IRC_WriteStrClient(Client, RPL_LUSERCHANNELS_MSG,
1437 Channel_CountVisible(Client)))
1438 return DISCONNECTED;
1440 /* Number of local users, services and servers */
1441 if (!IRC_WriteStrClient(Client, RPL_LUSERME_MSG, Client_ID(Client),
1442 Client_MyUserCount(), Client_MyServiceCount(),
1443 Client_MyServerCount()))
1444 return DISCONNECTED;
1447 /* Maximum number of local users */
1448 cnt = Client_MyUserCount();
1449 max = Client_MyMaxUserCount();
1450 if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1451 cnt, max, cnt, max))
1452 return DISCONNECTED;
1453 /* Maximum number of users in the network */
1454 cnt = Client_UserCount();
1455 max = Client_MaxUserCount();
1456 if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1457 cnt, max, cnt, max))
1458 return DISCONNECTED;
1459 /* Connection counters */
1460 if (! IRC_WriteStrClient(Client, RPL_STATSCONN_MSG, Client_ID(Client),
1461 Conn_CountMax(), Conn_CountAccepted()))
1462 return DISCONNECTED;
1466 } /* IRC_Send_LUSERS */
1469 IRC_Show_MOTD( CLIENT *Client )
1472 size_t len_tot, len_str;
1474 assert( Client != NULL );
1476 len_tot = array_bytes(&Conf_Motd);
1477 if (len_tot == 0 && !Conn_UsesSSL(Client_Conn(Client)))
1478 return IRC_WriteStrClient(Client, ERR_NOMOTD_MSG, Client_ID(Client));
1480 if (!Show_MOTD_Start(Client))
1481 return DISCONNECTED;
1483 line = array_start(&Conf_Motd);
1484 while (len_tot > 0) {
1485 len_str = strlen(line) + 1;
1487 assert(len_tot >= len_str);
1490 if (!Show_MOTD_Sendline(Client, line))
1491 return DISCONNECTED;
1495 if (!Show_MOTD_SSLInfo(Client))
1496 return DISCONNECTED;
1497 return Show_MOTD_End(Client);
1498 } /* IRC_Show_MOTD */
1501 * Send NAMES reply for a specific client and channel.
1503 * @param Client The client requesting the NAMES information.
1504 * @param Chan The channel
1505 * @return CONNECTED or DISCONNECTED.
1508 IRC_Send_NAMES(CLIENT * Client, CHANNEL * Chan)
1510 bool is_visible, is_member;
1511 char str[COMMAND_LEN];
1515 assert(Client != NULL);
1516 assert(Chan != NULL);
1518 if (Channel_IsMemberOf(Chan, Client))
1523 /* Do not print info on channel memberships to anyone that is not member? */
1524 if (Conf_MorePrivacy && !is_member)
1527 /* Secret channel? */
1528 if (!is_member && strchr(Channel_Modes(Chan), 's'))
1531 snprintf(str, sizeof(str), RPL_NAMREPLY_MSG, Client_ID(Client), "=",
1532 Channel_Name(Chan));
1533 cl2chan = Channel_FirstMember(Chan);
1535 cl = Channel_GetClient(cl2chan);
1537 if (strchr(Client_Modes(cl), 'i'))
1542 if (is_member || is_visible) {
1543 if (str[strlen(str) - 1] != ':')
1544 strlcat(str, " ", sizeof(str));
1546 who_flags_qualifier(Client, Channel_UserModes(Chan, cl),
1548 strlcat(str, Client_ID(cl), sizeof(str));
1550 if (strlen(str) > (COMMAND_LEN - CLIENT_NICK_LEN - 4)) {
1551 if (!IRC_WriteStrClient(Client, "%s", str))
1552 return DISCONNECTED;
1553 snprintf(str, sizeof(str), RPL_NAMREPLY_MSG,
1554 Client_ID(Client), "=",
1555 Channel_Name(Chan));
1559 cl2chan = Channel_NextMember(Chan, cl2chan);
1561 if (str[strlen(str) - 1] != ':') {
1562 if (!IRC_WriteStrClient(Client, "%s", str))
1563 return DISCONNECTED;
1567 } /* IRC_Send_NAMES */
1570 * Send the ISUPPORT numeric (005).
1571 * This numeric indicates the features that are supported by this server.
1572 * See <http://www.irc.org/tech_docs/005.html> for details.
1575 IRC_Send_ISUPPORT(CLIENT * Client)
1577 if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1579 return DISCONNECTED;
1580 return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1581 CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1582 COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1583 COMMAND_LEN - 113, MAX_HNDL_MODES_ARG,
1584 MAX_HNDL_CHANNEL_LISTS);
1585 } /* IRC_Send_ISUPPORT */