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_HasMode(c, 'o') &&
366 !IRC_WriteStrClient(from, RPL_WHOISOPERATOR_MSG,
367 Client_ID(from), Client_ID(c)))
371 if (Client_HasMode(c, 'B') &&
372 !IRC_WriteStrClient(from, RPL_WHOISBOT_MSG,
373 Client_ID(from), Client_ID(c)))
376 /* Connected using SSL? */
377 if (Conn_UsesSSL(Client_Conn(c)) &&
378 !IRC_WriteStrClient(from, RPL_WHOISSSL_MSG, Client_ID(from),
382 /* Registered nickname? */
383 if (Client_HasMode(c, 'R') &&
384 !IRC_WriteStrClient(from, RPL_WHOISREGNICK_MSG,
385 Client_ID(from), Client_ID(c)))
388 /* Local client and requester is the user itself or an IRC Op? */
389 if (Client_Conn(c) > NONE &&
390 (from == c || (!Conf_MorePrivacy && Client_HasMode(from, 'o')))) {
391 /* Client hostname */
392 if (!IRC_WriteStrClient(from, RPL_WHOISHOST_MSG,
393 Client_ID(from), Client_ID(c), Client_Hostname(c),
394 Conn_GetIPAInfo(Client_Conn(c))))
397 if (!IRC_WriteStrClient(from, RPL_WHOISMODES_MSG,
398 Client_ID(from), Client_ID(c), Client_Modes(c)))
402 /* Idle and signon time (local clients only!) */
403 if (!Conf_MorePrivacy && Client_Conn(c) > NONE &&
404 !IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
405 Client_ID(from), Client_ID(c),
406 (unsigned long)Conn_GetIdle(Client_Conn(c)),
407 (unsigned long)Conn_GetSignon(Client_Conn(c))))
411 if (Client_HasMode(c, 'a') &&
412 !IRC_WriteStrClient(from, RPL_AWAY_MSG,
413 Client_ID(from), Client_ID(c), Client_Away(c)))
420 WHOWAS_EntryWrite(CLIENT *prefix, WHOWAS *entry)
424 (void)strftime(t_str, sizeof(t_str), "%a %b %d %H:%M:%S %Y",
425 localtime(&entry->time));
427 if (!IRC_WriteStrClient(prefix, RPL_WHOWASUSER_MSG, Client_ID(prefix),
428 entry->id, entry->user, entry->host, entry->info))
431 return IRC_WriteStrClient(prefix, RPL_WHOISSERVER_MSG, Client_ID(prefix),
432 entry->id, entry->server, t_str);
436 Show_MOTD_Start(CLIENT *Client)
438 return IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG,
439 Client_ID( Client ), Client_ID( Client_ThisServer( )));
443 Show_MOTD_Sendline(CLIENT *Client, const char *msg)
445 return IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID( Client ), msg);
449 Show_MOTD_End(CLIENT *Client)
451 if (!IRC_WriteStrClient(Client, RPL_ENDOFMOTD_MSG, Client_ID(Client)))
455 return IRC_WriteStrClient(Client, RPL_HOSTHIDDEN_MSG,
457 Client_Hostname(Client));
463 static bool Show_MOTD_SSLInfo(CLIENT *Client)
466 char buf[COMMAND_LEN] = "Connected using Cipher ";
468 if (!Conn_GetCipherInfo(Client_Conn(Client), buf + 23, sizeof buf - 23))
471 if (!Show_MOTD_Sendline(Client, buf))
478 Show_MOTD_SSLInfo(UNUSED CLIENT *c)
482 /* Global functions */
485 * Handler for the IRC command "ADMIN".
487 * @param Client The client from which this command has been received.
488 * @param Req Request structure with prefix and all parameters.
489 * @return CONNECTED or DISCONNECTED.
492 IRC_ADMIN(CLIENT *Client, REQUEST *Req )
494 CLIENT *target, *prefix;
496 assert( Client != NULL );
497 assert( Req != NULL );
499 IRC_SetPenalty(Client, 1);
501 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
502 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
503 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
506 if(target != Client_ThisServer()) {
507 IRC_WriteStrClientPrefix(target, prefix,
508 "ADMIN %s", Client_ID(target));
512 if (!IRC_WriteStrClient(Client, RPL_ADMINME_MSG, Client_ID(prefix),
515 if (!IRC_WriteStrClient(Client, RPL_ADMINLOC1_MSG, Client_ID(prefix),
518 if (!IRC_WriteStrClient(Client, RPL_ADMINLOC2_MSG, Client_ID(prefix),
521 if (!IRC_WriteStrClient(Client, RPL_ADMINEMAIL_MSG, Client_ID(prefix),
522 Conf_ServerAdminMail))
529 * Handler for the IRC command "INFO".
531 * @param Client The client from which this command has been received.
532 * @param Req Request structure with prefix and all parameters.
533 * @return CONNECTED or DISCONNECTED.
536 IRC_INFO(CLIENT * Client, REQUEST * Req)
538 CLIENT *target, *prefix;
541 assert(Client != NULL);
544 IRC_SetPenalty(Client, 2);
546 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
547 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
548 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
551 if (target != Client_ThisServer()) {
552 IRC_WriteStrClientPrefix(target, prefix, "INFO %s",
557 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix),
561 #if defined(__DATE__) && defined(__TIME__)
562 snprintf(msg, sizeof(msg), "Birth Date: %s at %s", __DATE__, __TIME__);
563 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
567 strlcpy(msg, "On-line since ", sizeof(msg));
568 strlcat(msg, NGIRCd_StartStr, sizeof(msg));
569 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
572 if (!IRC_WriteStrClient(Client, RPL_ENDOFINFO_MSG, Client_ID(prefix)))
579 * Handler for the IRC "ISON" command.
581 * @param Client The client from which this command has been received.
582 * @param Req Request structure with prefix and all parameters.
583 * @return CONNECTED or DISCONNECTED.
586 IRC_ISON( CLIENT *Client, REQUEST *Req )
588 char rpl[COMMAND_LEN];
593 assert(Client != NULL);
596 _IRC_ARGC_GE_OR_RETURN_(Client, Req, 1)
598 strlcpy(rpl, RPL_ISON_MSG, sizeof rpl);
599 for (i = 0; i < Req->argc; i++) {
600 /* "All" ircd even parse ":<x> <y> ..." arguments and split
601 * them up; so we do the same ... */
602 ptr = strtok(Req->argv[i], " ");
605 c = Client_Search(ptr);
606 if (c && Client_Type(c) == CLIENT_USER) {
607 strlcat(rpl, Client_ID(c), sizeof(rpl));
608 strlcat(rpl, " ", sizeof(rpl));
610 ptr = strtok(NULL, " ");
613 ngt_TrimLastChr(rpl, ' ');
615 return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
619 * Handler for the IRC "LINKS" command.
621 * @param Client The client from which this command has been received.
622 * @param Req Request structure with prefix and all parameters.
623 * @return CONNECTED or DISCONNECTED.
626 IRC_LINKS(CLIENT *Client, REQUEST *Req)
628 CLIENT *target, *from, *c;
631 assert(Client != NULL);
634 IRC_SetPenalty(Client, 1);
636 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
637 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
639 /* Get pointer to server mask or "*", if none given */
641 mask = Req->argv[Req->argc - 1];
646 if (Req->argc == 2) {
647 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
648 if (target != Client_ThisServer()) {
649 IRC_WriteStrClientPrefix(target, from,
650 "LINKS %s %s", Client_ID(target),
658 if (Client_Type(c) == CLIENT_SERVER
659 && MatchCaseInsensitive(mask, Client_ID(c))) {
660 if (!IRC_WriteStrClient(from, RPL_LINKS_MSG,
661 Client_ID(from), Client_ID(c),
662 Client_ID(Client_TopServer(c)
663 ? Client_TopServer(c)
664 : Client_ThisServer()),
665 Client_Hops(c), Client_Info(c)))
670 return IRC_WriteStrClient(from, RPL_ENDOFLINKS_MSG,
671 Client_ID(from), mask);
675 * Handler for the IRC "LUSERS" command.
677 * @param Client The client from which this command has been received.
678 * @param Req Request structure with prefix and all parameters.
679 * @return CONNECTED or DISCONNECTED.
682 IRC_LUSERS( CLIENT *Client, REQUEST *Req )
684 CLIENT *target, *from;
686 assert( Client != NULL );
687 assert( Req != NULL );
689 IRC_SetPenalty(Client, 1);
691 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
692 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
693 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
696 if (target != Client_ThisServer()) {
697 IRC_WriteStrClientPrefix(target, from,
698 "LUSERS %s %s", Req->argv[0],
703 return IRC_Send_LUSERS(from);
707 * Handler for the IRC command "SERVLIST".
709 * @param Client The client from which this command has been received.
710 * @param Req Request structure with prefix and all parameters.
711 * @return CONNECTED or DISCONNECTED.
714 IRC_SERVLIST(CLIENT *Client, REQUEST *Req)
718 IRC_SetPenalty(Client, 1);
720 assert(Client != NULL);
723 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
725 if (Req->argc < 2 || strcmp(Req->argv[1], "0") == 0) {
726 for (c = Client_First(); c!= NULL; c = Client_Next(c)) {
727 if (Client_Type(c) != CLIENT_SERVICE)
729 if (Req->argc > 0 && !MatchCaseInsensitive(Req->argv[0],
732 if (!IRC_WriteStrClient(Client, RPL_SERVLIST_MSG,
733 Client_ID(Client), Client_Mask(c),
734 Client_Mask(Client_Introducer(c)), "*",
735 0, Client_Hops(c), Client_Info(c)))
740 return IRC_WriteStrClient(Client, RPL_SERVLISTEND_MSG, Client_ID(Client),
741 Req->argc > 0 ? Req->argv[0] : "*",
742 Req->argc > 1 ? Req->argv[1] : "0");
746 * Handler for the IRC command "MOTD".
748 * @param Client The client from which this command has been received.
749 * @param Req Request structure with prefix and all parameters.
750 * @return CONNECTED or DISCONNECTED.
753 IRC_MOTD( CLIENT *Client, REQUEST *Req )
755 CLIENT *from, *target;
757 assert( Client != NULL );
758 assert( Req != NULL );
760 IRC_SetPenalty(Client, 3);
762 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
763 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
764 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
767 if (target != Client_ThisServer()) {
768 IRC_WriteStrClientPrefix(target, from, "MOTD %s",
773 return IRC_Show_MOTD(from);
777 * Handler for the IRC command "NAMES".
779 * @param Client The client from which this command has been received.
780 * @param Req Request structure with prefix and all parameters.
781 * @return CONNECTED or DISCONNECTED.
784 IRC_NAMES( CLIENT *Client, REQUEST *Req )
786 char rpl[COMMAND_LEN], *ptr;
787 CLIENT *target, *from, *c;
790 assert( Client != NULL );
791 assert( Req != NULL );
793 IRC_SetPenalty(Client, 1);
795 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
796 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
797 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
800 if (target != Client_ThisServer()) {
801 IRC_WriteStrClientPrefix(target, from, "NAMES %s :%s",
802 Req->argv[0], Client_ID(target));
807 /* Return NAMES list for specific channels */
808 ptr = strtok(Req->argv[0], ",");
810 chan = Channel_Search(ptr);
811 if (chan && !IRC_Send_NAMES(from, chan))
813 if (!IRC_WriteStrClient(from, RPL_ENDOFNAMES_MSG,
814 Client_ID(from), ptr))
816 ptr = strtok( NULL, "," );
821 chan = Channel_First();
823 if (!IRC_Send_NAMES(from, chan))
825 chan = Channel_Next(chan);
828 /* Now print all clients which are not in any channel */
830 snprintf(rpl, sizeof(rpl), RPL_NAMREPLY_MSG, Client_ID(from), "*", "*");
832 if (Client_Type(c) == CLIENT_USER
833 && Channel_FirstChannelOf(c) == NULL
834 && !strchr(Client_Modes(c), 'i'))
836 /* its a user, concatenate ... */
837 if (rpl[strlen(rpl) - 1] != ':')
838 strlcat(rpl, " ", sizeof(rpl));
839 strlcat(rpl, Client_ID(c), sizeof(rpl));
841 if (strlen(rpl) > COMMAND_LEN - CLIENT_NICK_LEN - 4) {
842 /* Line is gwoing too long, send now */
843 if (!IRC_WriteStrClient(from, "%s", rpl))
845 snprintf(rpl, sizeof(rpl), RPL_NAMREPLY_MSG,
846 Client_ID(from), "*", "*");
851 if (rpl[strlen(rpl) - 1] != ':' && !IRC_WriteStrClient(from, "%s", rpl))
854 return IRC_WriteStrClient(from, RPL_ENDOFNAMES_MSG, Client_ID(from), "*");
858 * Handler for the IRC command "STATS".
860 * @param Client The client from which this command has been received.
861 * @param Req Request structure with prefix and all parameters.
862 * @return CONNECTED or DISCONNECTED.
865 IRC_STATS( CLIENT *Client, REQUEST *Req )
867 CLIENT *from, *target, *cl;
872 unsigned int days, hrs, mins;
873 struct list_head *list;
874 struct list_elem *list_item;
876 assert(Client != NULL);
879 IRC_SetPenalty(Client, 2);
881 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
882 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
883 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
886 if (target != Client_ThisServer()) {
887 IRC_WriteStrClientPrefix(target, from, "STATS %s %s",
888 Req->argv[0], Client_ID(target));
893 query = Req->argv[0][0] ? Req->argv[0][0] : '*';
898 case 'g': /* Network-wide bans ("G-Lines") */
900 case 'k': /* Server-local bans ("K-Lines") */
902 if (!Client_HasMode(from, 'o'))
903 return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG,
905 if (query == 'g' || query == 'G')
906 list = Class_GetList(CLASS_GLINE);
908 list = Class_GetList(CLASS_KLINE);
909 list_item = Lists_GetFirst(list);
911 if (!IRC_WriteStrClient(from, RPL_STATSXLINE_MSG,
912 Client_ID(from), query,
913 Lists_GetMask(list_item),
914 Lists_GetValidity(list_item),
915 Lists_GetReason(list_item)))
917 list_item = Lists_GetNext(list_item);
920 case 'l': /* Link status (servers and own link) */
922 time_now = time(NULL);
923 for (con = Conn_First(); con != NONE; con = Conn_Next(con)) {
924 cl = Conn_GetClient(con);
927 if ((Client_Type(cl) == CLIENT_SERVER)
929 /* Server link or our own connection */
931 if (Conn_Options(con) & CONN_ZIP) {
932 if (!IRC_WriteStrClient
933 (from, RPL_STATSLINKINFOZIP_MSG,
934 Client_ID(from), Client_Mask(cl),
935 Conn_SendQ(con), Conn_SendMsg(con),
941 (long)(time_now - Conn_StartTime(con))))
946 if (!IRC_WriteStrClient
947 (from, RPL_STATSLINKINFO_MSG,
948 Client_ID(from), Client_Mask(cl),
949 Conn_SendQ(con), Conn_SendMsg(con),
950 Conn_SendBytes(con), Conn_RecvMsg(con),
952 (long)(time_now - Conn_StartTime(con))))
957 case 'm': /* IRC command status (usage count) */
959 cmd = Parse_GetCommandStruct();
960 for (; cmd->name; cmd++) {
961 if (cmd->lcount == 0 && cmd->rcount == 0)
963 if (!IRC_WriteStrClient
964 (from, RPL_STATSCOMMANDS_MSG, Client_ID(from),
965 cmd->name, cmd->lcount, cmd->bytes, cmd->rcount))
969 case 'u': /* Server uptime */
971 time_now = time(NULL) - NGIRCd_Start;
972 days = uptime_days(&time_now);
973 hrs = uptime_hrs(&time_now);
974 mins = uptime_mins(&time_now);
975 if (!IRC_WriteStrClient(from, RPL_STATSUPTIME, Client_ID(from),
976 days, hrs, mins, (unsigned int)time_now))
981 return IRC_WriteStrClient(from, RPL_ENDOFSTATS_MSG,
982 Client_ID(from), query);
986 * Handler for the IRC command "SUMMON".
988 * @param Client The client from which this command has been received.
989 * @param Req Request structure with prefix and all parameters.
990 * @return CONNECTED or DISCONNECTED.
993 IRC_SUMMON(CLIENT * Client, UNUSED REQUEST * Req)
995 assert(Client != NULL);
997 IRC_SetPenalty(Client, 1);
999 return IRC_WriteStrClient(Client, ERR_SUMMONDISABLED_MSG,
1004 * Handler for the IRC command "TIME".
1006 * @param Client The client from which this command has been received.
1007 * @param Req Request structure with prefix and all parameters.
1008 * @return CONNECTED or DISCONNECTED.
1011 IRC_TIME( CLIENT *Client, REQUEST *Req )
1013 CLIENT *from, *target;
1017 assert(Client != NULL);
1018 assert(Req != NULL);
1020 IRC_SetPenalty(Client, 1);
1022 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
1023 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
1024 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
1027 if (target != Client_ThisServer()) {
1028 IRC_WriteStrClientPrefix(target, from, "TIME %s",
1034 (void)strftime(t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime(&t));
1035 return IRC_WriteStrClient(from, RPL_TIME_MSG, Client_ID(from),
1036 Client_ID(Client_ThisServer()), t_str);
1040 * Handler for the IRC command "USERHOST".
1042 * @param Client The client from which this command has been received.
1043 * @param Req Request structure with prefix and all parameters.
1044 * @return CONNECTED or DISCONNECTED.
1047 IRC_USERHOST(CLIENT *Client, REQUEST *Req)
1049 char rpl[COMMAND_LEN];
1053 assert(Client != NULL);
1054 assert(Req != NULL);
1056 IRC_SetPenalty(Client, 1);
1058 _IRC_ARGC_GE_OR_RETURN_(Client, Req, 1)
1065 strlcpy(rpl, RPL_USERHOST_MSG, sizeof rpl);
1066 for (i = 0; i < max; i++) {
1067 c = Client_Search(Req->argv[i]);
1068 if (c && (Client_Type(c) == CLIENT_USER)) {
1069 /* This Nick is "online" */
1070 strlcat(rpl, Client_ID(c), sizeof(rpl));
1071 if (Client_HasMode(c, 'o'))
1072 strlcat(rpl, "*", sizeof(rpl));
1073 strlcat(rpl, "=", sizeof(rpl));
1074 if (Client_HasMode(c, 'a'))
1075 strlcat(rpl, "-", sizeof(rpl));
1077 strlcat(rpl, "+", sizeof(rpl));
1078 strlcat(rpl, Client_User(c), sizeof(rpl));
1079 strlcat(rpl, "@", sizeof(rpl));
1080 strlcat(rpl, Client_HostnameDisplayed(c), sizeof(rpl));
1081 strlcat(rpl, " ", sizeof(rpl));
1084 ngt_TrimLastChr(rpl, ' ');
1086 return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
1087 } /* IRC_USERHOST */
1090 * Handler for the IRC command "USERS".
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_USERS(CLIENT * Client, UNUSED REQUEST * Req)
1099 assert(Client != NULL);
1101 IRC_SetPenalty(Client, 1);
1103 return IRC_WriteStrClient(Client, ERR_USERSDISABLED_MSG,
1108 * Handler for the IRC command "VERSION".
1110 * @param Client The client from which this command has been received.
1111 * @param Req Request structure with prefix and all parameters.
1112 * @return CONNECTED or DISCONNECTED.
1115 IRC_VERSION( CLIENT *Client, REQUEST *Req )
1117 CLIENT *target, *prefix;
1119 assert( Client != NULL );
1120 assert( Req != NULL );
1122 IRC_SetPenalty(Client, 1);
1124 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
1125 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
1126 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
1129 if (target != Client_ThisServer()) {
1130 IRC_WriteStrClientPrefix(target, prefix, "VERSION %s",
1135 /* send version information */
1136 return IRC_WriteStrClient(Client, RPL_VERSION_MSG, Client_ID(prefix),
1137 PACKAGE_NAME, PACKAGE_VERSION,
1138 NGIRCd_DebugLevel, Conf_ServerName,
1139 NGIRCd_VersionAddition);
1143 * Handler for the IRC "WHO" command.
1145 * @param Client The client from which this command has been received.
1146 * @param Req Request structure with prefix and all parameters.
1147 * @return CONNECTED or DISCONNECTED.
1150 IRC_WHO(CLIENT *Client, REQUEST *Req)
1155 assert (Client != NULL);
1156 assert (Req != NULL);
1158 IRC_SetPenalty(Client, 1);
1160 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
1163 if (Req->argc == 2) {
1164 if (strcmp(Req->argv[1], "o") == 0)
1168 return IRC_WriteStrClient(Client,
1169 ERR_NEEDMOREPARAMS_MSG,
1175 if (Req->argc >= 1) {
1176 /* Channel or mask given */
1177 chan = Channel_Search(Req->argv[0]);
1179 /* Members of a channel have been requested */
1180 return IRC_WHO_Channel(Client, chan, only_ops);
1182 if (strcmp(Req->argv[0], "0") != 0) {
1183 /* A mask has been given. But please note this RFC
1184 * stupidity: "0" is same as no arguments ... */
1185 return IRC_WHO_Mask(Client, Req->argv[0], only_ops);
1189 /* No channel or (valid) mask given */
1190 IRC_SetPenalty(Client, 2);
1191 return IRC_WHO_Mask(Client, NULL, only_ops);
1195 * Handler for the IRC "WHOIS" command.
1197 * @param Client The client from which this command has been received.
1198 * @param Req Request structure with prefix and all parameters.
1199 * @return CONNECTED or DISCONNECTED.
1202 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
1204 CLIENT *from, *target, *c;
1205 unsigned int match_count = 0, found = 0;
1206 bool has_wildcards, is_remote;
1207 bool got_wildcard = false;
1208 char mask[COMMAND_LEN], *query;
1210 assert( Client != NULL );
1211 assert( Req != NULL );
1213 IRC_SetPenalty(Client, 1);
1215 /* Bad number of parameters? */
1216 if (Req->argc < 1 || Req->argc > 2)
1217 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
1218 Client_ID(Client), Req->command);
1220 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
1222 /* Get target server for this command */
1223 if (Req->argc > 1) {
1224 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, Client)
1226 target = Client_ThisServer();
1228 assert(target != NULL);
1231 if (target != Client_ThisServer()) {
1232 IRC_WriteStrClientPrefix(target, from, "WHOIS %s :%s",
1233 Req->argv[0], Req->argv[1]);
1237 is_remote = Client_Conn(from) < 0;
1238 strlcpy(mask, Req->argv[Req->argc - 1], sizeof(mask));
1239 for (query = strtok(ngt_LowerStr(mask), ",");
1241 query = strtok(NULL, ","), found++)
1243 has_wildcards = query[strcspn(query, "*?")] != 0;
1245 * follows ircd 2.10 implementation:
1246 * - handle up to 3 targets
1247 * - no wildcards for remote clients
1248 * - only one wildcard target per local client
1250 * Also, at most MAX_RPL_WHOIS matches are returned.
1252 if (!has_wildcards || is_remote) {
1253 c = Client_Search(query);
1254 if (c && (Client_Type(c) == CLIENT_USER
1255 || Client_Type(c) == CLIENT_SERVICE)) {
1256 if (!IRC_WHOIS_SendReply(Client, from, c))
1257 return DISCONNECTED;
1259 if (!IRC_WriteStrClient(Client,
1263 return DISCONNECTED;
1268 /* we already handled one wildcard query */
1269 if (!IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1270 Client_ID(Client), query))
1271 return DISCONNECTED;
1274 got_wildcard = true;
1275 IRC_SetPenalty(Client, 3);
1277 for (c = Client_First(); c; c = Client_Next(c)) {
1278 if (IRC_CheckListTooBig(Client, match_count,
1279 MAX_RPL_WHOIS, "WHOIS"))
1282 if (Client_Type(c) != CLIENT_USER)
1284 if (!MatchCaseInsensitive(query, Client_ID(c)))
1286 if (!IRC_WHOIS_SendReply(Client, from, c))
1287 return DISCONNECTED;
1292 if (match_count == 0)
1293 IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
1295 Req->argv[Req->argc - 1]);
1297 return IRC_WriteStrClient(from, RPL_ENDOFWHOIS_MSG,
1298 Client_ID(from), Req->argv[Req->argc - 1]);
1302 * Handler for the IRC "WHOWAS" command.
1304 * @param Client The client from which this command has been received.
1305 * @param Req Request structure with prefix and all parameters.
1306 * @return CONNECTED or DISCONNECTED.
1309 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1311 CLIENT *target, *prefix;
1313 char tok_buf[COMMAND_LEN];
1314 int max, last, count, i, nc;
1317 assert( Client != NULL );
1318 assert( Req != NULL );
1320 /* Do not reveal any info on disconnected users? */
1321 if (Conf_MorePrivacy)
1324 /* Wrong number of parameters? */
1326 return IRC_WriteStrClient(Client, ERR_NONICKNAMEGIVEN_MSG,
1329 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 3)
1330 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
1331 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 2, prefix)
1334 if (target != Client_ThisServer()) {
1335 IRC_WriteStrClientPrefix(target, prefix, "WHOWAS %s %s %s",
1336 Req->argv[0], Req->argv[1],
1341 whowas = Client_GetWhowas( );
1342 last = Client_GetLastWhowasIndex( );
1346 max = DEF_RPL_WHOWAS;
1347 if (Req->argc > 1) {
1348 max = atoi(Req->argv[1]);
1350 max = MAX_RPL_WHOWAS;
1354 * Break up the nick argument into a list of nicks, if applicable
1355 * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1357 strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1358 nick = strtok(tok_buf, ",");
1360 for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1364 if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1365 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1366 return DISCONNECTED;
1370 /* previous entry */
1373 /* "underflow", wrap around */
1377 if (nc && count >= max)
1379 } while (i != last);
1381 if (nc == 0 && !IRC_WriteStrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1382 Client_ID(prefix), nick))
1383 return DISCONNECTED;
1385 return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG,
1386 Client_ID(prefix), Req->argv[0]);
1390 * Send LUSERS reply to a client.
1392 * @param Client The receipient of the information.
1393 * @return CONNECTED or DISCONNECTED.
1396 IRC_Send_LUSERS(CLIENT *Client)
1403 assert(Client != NULL);
1405 /* Users, services and serevers in the network */
1406 if (!IRC_WriteStrClient(Client, RPL_LUSERCLIENT_MSG, Client_ID(Client),
1407 Client_UserCount(), Client_ServiceCount(),
1408 Client_ServerCount()))
1409 return DISCONNECTED;
1411 /* Number of IRC operators */
1412 cnt = Client_OperCount( );
1414 if (!IRC_WriteStrClient(Client, RPL_LUSEROP_MSG,
1415 Client_ID(Client), cnt))
1416 return DISCONNECTED;
1419 /* Unknown connections */
1420 cnt = Client_UnknownCount( );
1422 if (!IRC_WriteStrClient(Client, RPL_LUSERUNKNOWN_MSG,
1423 Client_ID(Client), cnt))
1424 return DISCONNECTED;
1427 /* Number of created channels */
1428 if (!IRC_WriteStrClient(Client, RPL_LUSERCHANNELS_MSG,
1430 Channel_CountVisible(Client)))
1431 return DISCONNECTED;
1433 /* Number of local users, services and servers */
1434 if (!IRC_WriteStrClient(Client, RPL_LUSERME_MSG, Client_ID(Client),
1435 Client_MyUserCount(), Client_MyServiceCount(),
1436 Client_MyServerCount()))
1437 return DISCONNECTED;
1440 /* Maximum number of local users */
1441 cnt = Client_MyUserCount();
1442 max = Client_MyMaxUserCount();
1443 if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1444 cnt, max, cnt, max))
1445 return DISCONNECTED;
1446 /* Maximum number of users in the network */
1447 cnt = Client_UserCount();
1448 max = Client_MaxUserCount();
1449 if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1450 cnt, max, cnt, max))
1451 return DISCONNECTED;
1452 /* Connection counters */
1453 if (! IRC_WriteStrClient(Client, RPL_STATSCONN_MSG, Client_ID(Client),
1454 Conn_CountMax(), Conn_CountAccepted()))
1455 return DISCONNECTED;
1459 } /* IRC_Send_LUSERS */
1462 IRC_Show_MOTD( CLIENT *Client )
1465 size_t len_tot, len_str;
1467 assert( Client != NULL );
1469 len_tot = array_bytes(&Conf_Motd);
1470 if (len_tot == 0 && !Conn_UsesSSL(Client_Conn(Client)))
1471 return IRC_WriteStrClient(Client, ERR_NOMOTD_MSG, Client_ID(Client));
1473 if (!Show_MOTD_Start(Client))
1474 return DISCONNECTED;
1476 line = array_start(&Conf_Motd);
1477 while (len_tot > 0) {
1478 len_str = strlen(line) + 1;
1480 assert(len_tot >= len_str);
1483 if (!Show_MOTD_Sendline(Client, line))
1484 return DISCONNECTED;
1488 if (!Show_MOTD_SSLInfo(Client))
1489 return DISCONNECTED;
1490 return Show_MOTD_End(Client);
1491 } /* IRC_Show_MOTD */
1494 * Send NAMES reply for a specific client and channel.
1496 * @param Client The client requesting the NAMES information.
1497 * @param Chan The channel
1498 * @return CONNECTED or DISCONNECTED.
1501 IRC_Send_NAMES(CLIENT * Client, CHANNEL * Chan)
1503 bool is_visible, is_member;
1504 char str[COMMAND_LEN];
1508 assert(Client != NULL);
1509 assert(Chan != NULL);
1511 if (Channel_IsMemberOf(Chan, Client))
1516 /* Do not print info on channel memberships to anyone that is not member? */
1517 if (Conf_MorePrivacy && !is_member)
1520 /* Secret channel? */
1521 if (!is_member && strchr(Channel_Modes(Chan), 's'))
1524 snprintf(str, sizeof(str), RPL_NAMREPLY_MSG, Client_ID(Client), "=",
1525 Channel_Name(Chan));
1526 cl2chan = Channel_FirstMember(Chan);
1528 cl = Channel_GetClient(cl2chan);
1530 if (strchr(Client_Modes(cl), 'i'))
1535 if (is_member || is_visible) {
1536 if (str[strlen(str) - 1] != ':')
1537 strlcat(str, " ", sizeof(str));
1539 who_flags_qualifier(Client, Channel_UserModes(Chan, cl),
1541 strlcat(str, Client_ID(cl), sizeof(str));
1543 if (strlen(str) > (COMMAND_LEN - CLIENT_NICK_LEN - 4)) {
1544 if (!IRC_WriteStrClient(Client, "%s", str))
1545 return DISCONNECTED;
1546 snprintf(str, sizeof(str), RPL_NAMREPLY_MSG,
1547 Client_ID(Client), "=",
1548 Channel_Name(Chan));
1552 cl2chan = Channel_NextMember(Chan, cl2chan);
1554 if (str[strlen(str) - 1] != ':') {
1555 if (!IRC_WriteStrClient(Client, "%s", str))
1556 return DISCONNECTED;
1560 } /* IRC_Send_NAMES */
1563 * Send the ISUPPORT numeric (005).
1564 * This numeric indicates the features that are supported by this server.
1565 * See <http://www.irc.org/tech_docs/005.html> for details.
1568 IRC_Send_ISUPPORT(CLIENT * Client)
1570 if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1572 return DISCONNECTED;
1573 return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1574 CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1575 COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1576 COMMAND_LEN - 113, MAX_HNDL_MODES_ARG,
1577 MAX_HNDL_CHANNEL_LISTS);
1578 } /* IRC_Send_ISUPPORT */