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 * Return channel user mode prefix(es).
93 * @param Client The client requesting the mode prefixes.
94 * @param chan_user_modes String with channel user modes.
95 * @param str String buffer to which the prefix(es) will be appended.
96 * @param len Size of "str" buffer.
97 * @return Pointer to "str".
100 who_flags_qualifier(CLIENT *Client, const char *chan_user_modes,
101 char *str, size_t len)
103 assert(Client != NULL);
105 if (Client_Cap(Client) & CLIENT_CAP_MULTI_PREFIX) {
106 if (strchr(chan_user_modes, 'q'))
107 strlcat(str, "~", len);
108 if (strchr(chan_user_modes, 'a'))
109 strlcat(str, "&", len);
110 if (strchr(chan_user_modes, 'o'))
111 strlcat(str, "@", len);
112 if (strchr(chan_user_modes, 'h'))
113 strlcat(str, "%", len);
114 if (strchr(chan_user_modes, 'v'))
115 strlcat(str, "+", len);
120 if (strchr(chan_user_modes, 'q'))
121 strlcat(str, "~", len);
122 else if (strchr(chan_user_modes, 'a'))
123 strlcat(str, "&", len);
124 else if (strchr(chan_user_modes, 'o'))
125 strlcat(str, "@", len);
126 else if (strchr(chan_user_modes, 'h'))
127 strlcat(str, "%", len);
128 else if (strchr(chan_user_modes, 'v'))
129 strlcat(str, "+", len);
135 * Send WHO reply for a "channel target" ("WHO #channel").
137 * @param Client Client requesting the information.
138 * @param Chan Channel being requested.
139 * @param OnlyOps Only display IRC operators.
140 * @return CONNECTED or DISCONNECTED.
143 IRC_WHO_Channel(CLIENT *Client, CHANNEL *Chan, bool OnlyOps)
145 bool is_visible, is_member, is_ircop;
151 assert( Client != NULL );
152 assert( Chan != NULL );
154 IRC_SetPenalty(Client, 1);
156 is_member = Channel_IsMemberOf(Chan, Client);
158 /* Secret channel? */
159 if (!is_member && Channel_HasMode(Chan, 's'))
160 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG,
161 Client_ID(Client), Channel_Name(Chan));
163 cl2chan = Channel_FirstMember(Chan);
164 for (; cl2chan ; cl2chan = Channel_NextMember(Chan, cl2chan)) {
165 c = Channel_GetClient(cl2chan);
167 is_ircop = Client_HasMode(c, 'o');
168 if (OnlyOps && !is_ircop)
171 is_visible = Client_HasMode(c, 'i');
172 if (is_member || is_visible) {
173 memset(flags, 0, sizeof(flags));
175 if (Client_HasMode(c, 'a'))
176 flags[0] = 'G'; /* away */
183 who_flags_qualifier(Client, Channel_UserModes(Chan, c),
184 flags, sizeof(flags));
186 if (!write_whoreply(Client, c, Channel_Name(Chan),
193 /* If there are a lot of clients, augment penalty a bit */
194 if (count > MAX_RPL_WHO)
195 IRC_SetPenalty(Client, 1);
197 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client),
202 * Send WHO reply for a "mask target" ("WHO m*sk").
204 * @param Client Client requesting the information.
205 * @param Mask Mask being requested or NULL for "all" clients.
206 * @param OnlyOps Only display IRC operators.
207 * @return CONNECTED or DISCONNECTED.
210 IRC_WHO_Mask(CLIENT *Client, char *Mask, bool OnlyOps)
215 bool client_match, is_visible;
219 assert (Client != NULL);
224 IRC_SetPenalty(Client, 3);
225 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
226 if (Client_Type(c) != CLIENT_USER)
229 if (OnlyOps && !Client_HasMode(c, 'o'))
233 /* Match pattern against user host/server/name/nick */
234 client_match = MatchCaseInsensitive(Mask,
237 client_match = MatchCaseInsensitive(Mask,
238 Client_ID(Client_Introducer(c)));
240 client_match = MatchCaseInsensitive(Mask,
243 client_match = MatchCaseInsensitive(Mask,
246 continue; /* no match: skip this client */
249 is_visible = !Client_HasMode(c, 'i');
251 /* Target client is invisible, but mask matches exactly? */
252 if (!is_visible && Mask && strcasecmp(Client_ID(c), Mask) == 0)
255 /* Target still invisible, but are both on the same channel? */
257 cl2chan = Channel_FirstChannelOf(Client);
258 while (cl2chan && !is_visible) {
259 chan = Channel_GetChannel(cl2chan);
260 if (Channel_IsMemberOf(chan, c))
262 cl2chan = Channel_NextChannelOf(Client, cl2chan);
266 if (!is_visible) /* target user is not visible */
269 if (IRC_CheckListTooBig(Client, count, MAX_RPL_WHO, "WHO"))
272 memset(flags, 0, sizeof(flags));
274 if (Client_HasMode(c, 'a'))
275 flags[0] = 'G'; /* away */
279 if (Client_HasMode(c, 'o'))
282 if (!write_whoreply(Client, c, "*", flags))
287 return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client),
292 * Generate WHOIS reply of one actual client.
294 * @param Client The client from which this command has been received.
295 * @param from The client requesting the information ("originator").
296 * @param c The client of which information should be returned.
297 * @return CONNECTED or DISCONNECTED.
300 IRC_WHOIS_SendReply(CLIENT *Client, CLIENT *from, CLIENT *c)
302 char str[COMMAND_LEN];
306 assert(Client != NULL);
307 assert(from != NULL);
310 /* Nick, user, hostname and client info */
311 if (!IRC_WriteStrClient(from, RPL_WHOISUSER_MSG, Client_ID(from),
312 Client_ID(c), Client_User(c),
313 Client_HostnameDisplayed(c), Client_Info(c)))
317 if (!IRC_WriteStrClient(from, RPL_WHOISSERVER_MSG, Client_ID(from),
318 Client_ID(c), Client_ID(Client_Introducer(c)),
319 Client_Info(Client_Introducer(c))))
323 snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
324 Client_ID(from), Client_ID(c));
325 cl2chan = Channel_FirstChannelOf(c);
327 chan = Channel_GetChannel(cl2chan);
328 assert(chan != NULL);
331 cl2chan = Channel_NextChannelOf(c, cl2chan);
333 /* Secret channel? */
334 if (Channel_HasMode(chan, 's')
335 && !Channel_IsMemberOf(chan, Client))
338 /* Local channel and request is not from a user? */
339 if (Client_Type(Client) == CLIENT_SERVER
340 && Channel_IsLocal(chan))
343 /* Concatenate channel names */
344 if (str[strlen(str) - 1] != ':')
345 strlcat(str, " ", sizeof(str));
347 who_flags_qualifier(Client, Channel_UserModes(chan, c),
349 strlcat(str, Channel_Name(chan), sizeof(str));
351 if (strlen(str) > (COMMAND_LEN - CHANNEL_NAME_LEN - 4)) {
352 /* Line becomes too long: send it! */
353 if (!IRC_WriteStrClient(Client, "%s", str))
355 snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
356 Client_ID(from), Client_ID(c));
359 if(str[strlen(str) - 1] != ':') {
360 /* There is data left to send: */
361 if (!IRC_WriteStrClient(Client, "%s", str))
366 if (Client_Type(c) == CLIENT_SERVICE &&
367 !IRC_WriteStrClient(from, RPL_WHOISSERVICE_MSG,
368 Client_ID(from), Client_ID(c)))
372 if (Client_Type(c) != CLIENT_SERVICE &&
373 Client_HasMode(c, 'o') &&
374 !IRC_WriteStrClient(from, RPL_WHOISOPERATOR_MSG,
375 Client_ID(from), Client_ID(c)))
379 if (Client_HasMode(c, 'B') &&
380 !IRC_WriteStrClient(from, RPL_WHOISBOT_MSG,
381 Client_ID(from), Client_ID(c)))
384 /* Connected using SSL? */
385 if (Conn_UsesSSL(Client_Conn(c))) {
386 if (!IRC_WriteStrClient(from, RPL_WHOISSSL_MSG, Client_ID(from),
390 /* Certificate fingerprint? */
391 if (Conn_GetCertFp(Client_Conn(c)) &&
393 !IRC_WriteStrClient(from, RPL_WHOISCERTFP_MSG,
394 Client_ID(from), Client_ID(c),
395 Conn_GetCertFp(Client_Conn(c))))
399 /* Registered nickname? */
400 if (Client_HasMode(c, 'R') &&
401 !IRC_WriteStrClient(from, RPL_WHOISREGNICK_MSG,
402 Client_ID(from), Client_ID(c)))
405 /* Local client and requester is the user itself or an IRC Op? */
406 if (Client_Conn(c) > NONE &&
407 (from == c || (!Conf_MorePrivacy && Client_HasMode(from, 'o')))) {
408 /* Client hostname */
409 if (!IRC_WriteStrClient(from, RPL_WHOISHOST_MSG,
410 Client_ID(from), Client_ID(c),
411 Client_Hostname(c), Client_IPAText(c)))
414 if (!IRC_WriteStrClient(from, RPL_WHOISMODES_MSG,
415 Client_ID(from), Client_ID(c), Client_Modes(c)))
419 /* Idle and signon time (local clients only!) */
420 if (!Conf_MorePrivacy && Client_Conn(c) > NONE &&
421 !IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
422 Client_ID(from), Client_ID(c),
423 (unsigned long)Conn_GetIdle(Client_Conn(c)),
424 (unsigned long)Conn_GetSignon(Client_Conn(c))))
428 if (Client_HasMode(c, 'a') &&
429 !IRC_WriteStrClient(from, RPL_AWAY_MSG,
430 Client_ID(from), Client_ID(c), Client_Away(c)))
437 WHOWAS_EntryWrite(CLIENT *prefix, WHOWAS *entry)
441 (void)strftime(t_str, sizeof(t_str), "%a %b %d %H:%M:%S %Y",
442 localtime(&entry->time));
444 if (!IRC_WriteStrClient(prefix, RPL_WHOWASUSER_MSG, Client_ID(prefix),
445 entry->id, entry->user, entry->host, entry->info))
448 return IRC_WriteStrClient(prefix, RPL_WHOISSERVER_MSG, Client_ID(prefix),
449 entry->id, entry->server, t_str);
454 Show_MOTD_SSLInfo(CLIENT *Client)
456 char buf[COMMAND_LEN];
459 if (Conn_GetCipherInfo(Client_Conn(Client), c_str, sizeof(c_str))) {
460 snprintf(buf, sizeof(buf), "Connected using Cipher %s", c_str);
461 if (!IRC_WriteStrClient(Client, RPL_MOTD_MSG,
462 Client_ID(Client), buf))
466 if (Conn_GetCertFp(Client_Conn(Client))) {
467 snprintf(buf, sizeof(buf),
468 "Your client certificate fingerprint is: %s",
469 Conn_GetCertFp(Client_Conn(Client)));
470 if (!IRC_WriteStrClient(Client, RPL_MOTD_MSG,
471 Client_ID(Client), buf))
479 Show_MOTD_SSLInfo(UNUSED CLIENT *c)
485 /* Global functions */
488 * Handler for the IRC command "ADMIN".
490 * @param Client The client from which this command has been received.
491 * @param Req Request structure with prefix and all parameters.
492 * @return CONNECTED or DISCONNECTED.
495 IRC_ADMIN(CLIENT *Client, REQUEST *Req )
497 CLIENT *target, *prefix;
499 assert( Client != NULL );
500 assert( Req != NULL );
502 IRC_SetPenalty(Client, 1);
504 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
505 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
508 if(target != Client_ThisServer()) {
509 IRC_WriteStrClientPrefix(target, prefix,
510 "ADMIN %s", Client_ID(target));
514 if (!IRC_WriteStrClient(Client, RPL_ADMINME_MSG, Client_ID(prefix),
517 if (!IRC_WriteStrClient(Client, RPL_ADMINLOC1_MSG, Client_ID(prefix),
520 if (!IRC_WriteStrClient(Client, RPL_ADMINLOC2_MSG, Client_ID(prefix),
523 if (!IRC_WriteStrClient(Client, RPL_ADMINEMAIL_MSG, Client_ID(prefix),
524 Conf_ServerAdminMail))
531 * Handler for the IRC command "INFO".
533 * @param Client The client from which this command has been received.
534 * @param Req Request structure with prefix and all parameters.
535 * @return CONNECTED or DISCONNECTED.
538 IRC_INFO(CLIENT * Client, REQUEST * Req)
540 CLIENT *target, *prefix;
543 assert(Client != NULL);
546 IRC_SetPenalty(Client, 2);
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(__DATE__) && defined(__TIME__)
563 snprintf(msg, sizeof(msg), "Birth Date: %s at %s", __DATE__, __TIME__);
564 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
568 strlcpy(msg, "On-line since ", sizeof(msg));
569 strlcat(msg, NGIRCd_StartStr, sizeof(msg));
570 if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
573 if (!IRC_WriteStrClient(Client, RPL_ENDOFINFO_MSG, Client_ID(prefix)))
580 * Handler for the IRC "ISON" command.
582 * @param Client The client from which this command has been received.
583 * @param Req Request structure with prefix and all parameters.
584 * @return CONNECTED or DISCONNECTED.
587 IRC_ISON( CLIENT *Client, REQUEST *Req )
589 char rpl[COMMAND_LEN];
594 assert(Client != NULL);
597 strlcpy(rpl, RPL_ISON_MSG, sizeof rpl);
598 for (i = 0; i < Req->argc; i++) {
599 /* "All" ircd even parse ":<x> <y> ..." arguments and split
600 * them up; so we do the same ... */
601 ptr = strtok(Req->argv[i], " ");
604 c = Client_Search(ptr);
605 if (c && Client_Type(c) == CLIENT_USER) {
606 strlcat(rpl, Client_ID(c), sizeof(rpl));
607 strlcat(rpl, " ", sizeof(rpl));
609 ptr = strtok(NULL, " ");
612 ngt_TrimLastChr(rpl, ' ');
614 return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
618 * Handler for the IRC "LINKS" command.
620 * @param Client The client from which this command has been received.
621 * @param Req Request structure with prefix and all parameters.
622 * @return CONNECTED or DISCONNECTED.
625 IRC_LINKS(CLIENT *Client, REQUEST *Req)
627 CLIENT *target, *from, *c;
630 assert(Client != NULL);
633 IRC_SetPenalty(Client, 1);
635 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
637 /* Get pointer to server mask or "*", if none given */
639 mask = Req->argv[Req->argc - 1];
644 if (Req->argc == 2) {
645 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
646 if (target != Client_ThisServer()) {
647 IRC_WriteStrClientPrefix(target, from,
648 "LINKS %s %s", Client_ID(target),
656 if (Client_Type(c) == CLIENT_SERVER
657 && MatchCaseInsensitive(mask, Client_ID(c))) {
658 if (!IRC_WriteStrClient(from, RPL_LINKS_MSG,
659 Client_ID(from), Client_ID(c),
660 Client_ID(Client_TopServer(c)
661 ? Client_TopServer(c)
662 : Client_ThisServer()),
663 Client_Hops(c), Client_Info(c)))
668 return IRC_WriteStrClient(from, RPL_ENDOFLINKS_MSG,
669 Client_ID(from), mask);
673 * Handler for the IRC "LUSERS" command.
675 * @param Client The client from which this command has been received.
676 * @param Req Request structure with prefix and all parameters.
677 * @return CONNECTED or DISCONNECTED.
680 IRC_LUSERS( CLIENT *Client, REQUEST *Req )
682 CLIENT *target, *from;
684 assert( Client != NULL );
685 assert( Req != NULL );
687 IRC_SetPenalty(Client, 1);
689 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
690 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
693 if (target != Client_ThisServer()) {
694 IRC_WriteStrClientPrefix(target, from,
695 "LUSERS %s %s", Req->argv[0],
700 return IRC_Send_LUSERS(from);
704 * Handler for the IRC command "SERVLIST".
706 * @param Client The client from which this command has been received.
707 * @param Req Request structure with prefix and all parameters.
708 * @return CONNECTED or DISCONNECTED.
711 IRC_SERVLIST(CLIENT *Client, REQUEST *Req)
715 assert(Client != NULL);
718 IRC_SetPenalty(Client, 1);
720 if (Req->argc < 2 || strcmp(Req->argv[1], "0") == 0) {
721 for (c = Client_First(); c!= NULL; c = Client_Next(c)) {
722 if (Client_Type(c) != CLIENT_SERVICE)
724 if (Req->argc > 0 && !MatchCaseInsensitive(Req->argv[0],
727 if (!IRC_WriteStrClient(Client, RPL_SERVLIST_MSG,
728 Client_ID(Client), Client_Mask(c),
729 Client_Mask(Client_Introducer(c)), "*",
730 0, Client_Hops(c), Client_Info(c)))
735 return IRC_WriteStrClient(Client, RPL_SERVLISTEND_MSG, Client_ID(Client),
736 Req->argc > 0 ? Req->argv[0] : "*",
737 Req->argc > 1 ? Req->argv[1] : "0");
741 * Handler for the IRC command "MOTD".
743 * @param Client The client from which this command has been received.
744 * @param Req Request structure with prefix and all parameters.
745 * @return CONNECTED or DISCONNECTED.
748 IRC_MOTD( CLIENT *Client, REQUEST *Req )
750 CLIENT *from, *target;
752 assert( Client != NULL );
753 assert( Req != NULL );
755 IRC_SetPenalty(Client, 3);
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_SetPenalty(Client, 1);
789 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
790 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
793 if (target != Client_ThisServer()) {
794 IRC_WriteStrClientPrefix(target, from, "NAMES %s :%s",
795 Req->argv[0], Client_ID(target));
800 /* Return NAMES list for specific channels */
801 ptr = strtok(Req->argv[0], ",");
803 chan = Channel_Search(ptr);
804 if (chan && !IRC_Send_NAMES(from, chan))
806 if (!IRC_WriteStrClient(from, RPL_ENDOFNAMES_MSG,
807 Client_ID(from), ptr))
809 ptr = strtok( NULL, "," );
814 chan = Channel_First();
816 if (!IRC_Send_NAMES(from, chan))
818 chan = Channel_Next(chan);
821 /* Now print all clients which are not in any channel */
823 snprintf(rpl, sizeof(rpl), RPL_NAMREPLY_MSG, Client_ID(from), "*", "*");
825 if (Client_Type(c) == CLIENT_USER
826 && Channel_FirstChannelOf(c) == NULL
827 && !Client_HasMode(c, 'i'))
829 /* its a user, concatenate ... */
830 if (rpl[strlen(rpl) - 1] != ':')
831 strlcat(rpl, " ", sizeof(rpl));
832 strlcat(rpl, Client_ID(c), sizeof(rpl));
834 if (strlen(rpl) > COMMAND_LEN - CLIENT_NICK_LEN - 4) {
835 /* Line is gwoing too long, send now */
836 if (!IRC_WriteStrClient(from, "%s", rpl))
838 snprintf(rpl, sizeof(rpl), RPL_NAMREPLY_MSG,
839 Client_ID(from), "*", "*");
844 if (rpl[strlen(rpl) - 1] != ':' && !IRC_WriteStrClient(from, "%s", rpl))
847 return IRC_WriteStrClient(from, RPL_ENDOFNAMES_MSG, Client_ID(from), "*");
851 * Handler for the IRC command "STATS".
853 * @param Client The client from which this command has been received.
854 * @param Req Request structure with prefix and all parameters.
855 * @return CONNECTED or DISCONNECTED.
858 IRC_STATS( CLIENT *Client, REQUEST *Req )
860 CLIENT *from, *target, *cl;
865 unsigned int days, hrs, mins;
866 struct list_head *list;
867 struct list_elem *list_item;
869 assert(Client != NULL);
872 IRC_SetPenalty(Client, 2);
874 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
875 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
878 if (target != Client_ThisServer()) {
879 IRC_WriteStrClientPrefix(target, from, "STATS %s %s",
880 Req->argv[0], Client_ID(target));
885 query = Req->argv[0][0] ? Req->argv[0][0] : '*';
890 case 'g': /* Network-wide bans ("G-Lines") */
892 case 'k': /* Server-local bans ("K-Lines") */
894 if (!Client_HasMode(from, 'o'))
895 return IRC_WriteErrClient(from, ERR_NOPRIVILEGES_MSG,
897 if (query == 'g' || query == 'G')
898 list = Class_GetList(CLASS_GLINE);
900 list = Class_GetList(CLASS_KLINE);
901 list_item = Lists_GetFirst(list);
903 if (!IRC_WriteStrClient(from, RPL_STATSXLINE_MSG,
904 Client_ID(from), query,
905 Lists_GetMask(list_item),
906 Lists_GetValidity(list_item),
907 Lists_GetReason(list_item)))
909 list_item = Lists_GetNext(list_item);
912 case 'l': /* Link status (servers and own link) */
914 time_now = time(NULL);
915 for (con = Conn_First(); con != NONE; con = Conn_Next(con)) {
916 cl = Conn_GetClient(con);
919 if ((Client_Type(cl) == CLIENT_SERVER)
921 /* Server link or our own connection */
923 if (Conn_Options(con) & CONN_ZIP) {
924 if (!IRC_WriteStrClient
925 (from, RPL_STATSLINKINFOZIP_MSG,
926 Client_ID(from), Client_Mask(cl),
927 Conn_SendQ(con), Conn_SendMsg(con),
933 (long)(time_now - Conn_StartTime(con))))
938 if (!IRC_WriteStrClient
939 (from, RPL_STATSLINKINFO_MSG,
940 Client_ID(from), Client_Mask(cl),
941 Conn_SendQ(con), Conn_SendMsg(con),
942 Conn_SendBytes(con), Conn_RecvMsg(con),
944 (long)(time_now - Conn_StartTime(con))))
949 case 'm': /* IRC command status (usage count) */
951 cmd = Parse_GetCommandStruct();
952 for (; cmd->name; cmd++) {
953 if (cmd->lcount == 0 && cmd->rcount == 0)
955 if (!IRC_WriteStrClient
956 (from, RPL_STATSCOMMANDS_MSG, Client_ID(from),
957 cmd->name, cmd->lcount, cmd->bytes, cmd->rcount))
961 case 'u': /* Server uptime */
963 time_now = time(NULL) - NGIRCd_Start;
964 days = uptime_days(&time_now);
965 hrs = uptime_hrs(&time_now);
966 mins = uptime_mins(&time_now);
967 if (!IRC_WriteStrClient(from, RPL_STATSUPTIME, Client_ID(from),
968 days, hrs, mins, (unsigned int)time_now))
973 return IRC_WriteStrClient(from, RPL_ENDOFSTATS_MSG,
974 Client_ID(from), query);
978 * Handler for the IRC command "SUMMON".
980 * @param Client The client from which this command has been received.
981 * @param Req Request structure with prefix and all parameters.
982 * @return CONNECTED or DISCONNECTED.
985 IRC_SUMMON(CLIENT * Client, UNUSED REQUEST * Req)
987 assert(Client != NULL);
989 return IRC_WriteErrClient(Client, ERR_SUMMONDISABLED_MSG,
994 * Handler for the IRC command "TIME".
996 * @param Client The client from which this command has been received.
997 * @param Req Request structure with prefix and all parameters.
998 * @return CONNECTED or DISCONNECTED.
1001 IRC_TIME( CLIENT *Client, REQUEST *Req )
1003 CLIENT *from, *target;
1007 assert(Client != NULL);
1008 assert(Req != NULL);
1010 IRC_SetPenalty(Client, 1);
1012 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
1013 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
1016 if (target != Client_ThisServer()) {
1017 IRC_WriteStrClientPrefix(target, from, "TIME %s",
1023 (void)strftime(t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime(&t));
1024 return IRC_WriteStrClient(from, RPL_TIME_MSG, Client_ID(from),
1025 Client_ID(Client_ThisServer()), t_str);
1029 * Handler for the IRC command "USERHOST".
1031 * @param Client The client from which this command has been received.
1032 * @param Req Request structure with prefix and all parameters.
1033 * @return CONNECTED or DISCONNECTED.
1036 IRC_USERHOST(CLIENT *Client, REQUEST *Req)
1038 char rpl[COMMAND_LEN];
1042 assert(Client != NULL);
1043 assert(Req != NULL);
1045 IRC_SetPenalty(Client, 1);
1052 strlcpy(rpl, RPL_USERHOST_MSG, sizeof rpl);
1053 for (i = 0; i < max; i++) {
1054 c = Client_Search(Req->argv[i]);
1055 if (c && (Client_Type(c) == CLIENT_USER)) {
1056 /* This Nick is "online" */
1057 strlcat(rpl, Client_ID(c), sizeof(rpl));
1058 if (Client_HasMode(c, 'o'))
1059 strlcat(rpl, "*", sizeof(rpl));
1060 strlcat(rpl, "=", sizeof(rpl));
1061 if (Client_HasMode(c, 'a'))
1062 strlcat(rpl, "-", sizeof(rpl));
1064 strlcat(rpl, "+", sizeof(rpl));
1065 strlcat(rpl, Client_User(c), sizeof(rpl));
1066 strlcat(rpl, "@", sizeof(rpl));
1067 strlcat(rpl, Client_HostnameDisplayed(c), sizeof(rpl));
1068 strlcat(rpl, " ", sizeof(rpl));
1071 ngt_TrimLastChr(rpl, ' ');
1073 return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
1074 } /* IRC_USERHOST */
1077 * Handler for the IRC command "USERS".
1079 * @param Client The client from which this command has been received.
1080 * @param Req Request structure with prefix and all parameters.
1081 * @return CONNECTED or DISCONNECTED.
1084 IRC_USERS(CLIENT * Client, UNUSED REQUEST * Req)
1086 assert(Client != NULL);
1088 return IRC_WriteErrClient(Client, ERR_USERSDISABLED_MSG,
1093 * Handler for the IRC command "VERSION".
1095 * @param Client The client from which this command has been received.
1096 * @param Req Request structure with prefix and all parameters.
1097 * @return CONNECTED or DISCONNECTED.
1100 IRC_VERSION( CLIENT *Client, REQUEST *Req )
1102 CLIENT *target, *prefix;
1104 assert( Client != NULL );
1105 assert( Req != NULL );
1107 IRC_SetPenalty(Client, 1);
1109 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
1110 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
1113 if (target != Client_ThisServer()) {
1114 IRC_WriteStrClientPrefix(target, prefix, "VERSION %s",
1119 /* send version information */
1120 return IRC_WriteStrClient(Client, RPL_VERSION_MSG, Client_ID(prefix),
1121 PACKAGE_NAME, PACKAGE_VERSION,
1122 NGIRCd_DebugLevel, Conf_ServerName,
1123 NGIRCd_VersionAddition);
1127 * Handler for the IRC "WHO" command.
1129 * @param Client The client from which this command has been received.
1130 * @param Req Request structure with prefix and all parameters.
1131 * @return CONNECTED or DISCONNECTED.
1134 IRC_WHO(CLIENT *Client, REQUEST *Req)
1139 assert (Client != NULL);
1140 assert (Req != NULL);
1142 IRC_SetPenalty(Client, 1);
1145 if (Req->argc == 2) {
1146 if (strcmp(Req->argv[1], "o") == 0)
1150 IRC_SetPenalty(Client, 2);
1151 return IRC_WriteErrClient(Client,
1152 ERR_NEEDMOREPARAMS_MSG,
1159 if (Req->argc >= 1) {
1160 /* Channel or mask given */
1161 chan = Channel_Search(Req->argv[0]);
1163 /* Members of a channel have been requested */
1164 return IRC_WHO_Channel(Client, chan, only_ops);
1166 if (strcmp(Req->argv[0], "0") != 0) {
1167 /* A mask has been given. But please note this RFC
1168 * stupidity: "0" is same as no arguments ... */
1169 return IRC_WHO_Mask(Client, Req->argv[0], only_ops);
1173 /* No channel or (valid) mask given */
1174 IRC_SetPenalty(Client, 2);
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 IRC_SetPenalty(Client, 1);
1199 /* Wrong number of parameters? */
1201 return IRC_WriteErrClient(Client, ERR_NONICKNAMEGIVEN_MSG,
1204 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
1205 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
1207 /* Get target server for this command */
1208 if (Req->argc > 1) {
1209 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, Client)
1211 target = Client_ThisServer();
1213 assert(target != NULL);
1216 if (target != Client_ThisServer()) {
1217 IRC_WriteStrClientPrefix(target, from, "WHOIS %s :%s",
1218 Req->argv[0], Req->argv[1]);
1222 is_remote = Client_Conn(from) < 0;
1223 strlcpy(mask, Req->argv[Req->argc - 1], sizeof(mask));
1224 for (query = strtok(ngt_LowerStr(mask), ",");
1226 query = strtok(NULL, ","), found++)
1228 has_wildcards = query[strcspn(query, "*?")] != 0;
1230 * follows ircd 2.10 implementation:
1231 * - handle up to 3 targets
1232 * - no wildcards for remote clients
1233 * - only one wildcard target per local client
1235 * Also, at most MAX_RPL_WHOIS matches are returned.
1237 if (!has_wildcards || is_remote) {
1238 c = Client_Search(query);
1239 if (c && (Client_Type(c) == CLIENT_USER
1240 || Client_Type(c) == CLIENT_SERVICE)) {
1241 if (!IRC_WHOIS_SendReply(Client, from, c))
1242 return DISCONNECTED;
1244 if (!IRC_WriteErrClient(Client,
1248 return DISCONNECTED;
1253 /* we already handled one wildcard query */
1254 if (!IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
1255 Client_ID(Client), query))
1256 return DISCONNECTED;
1259 got_wildcard = true;
1260 IRC_SetPenalty(Client, 3);
1262 for (c = Client_First(); c; c = Client_Next(c)) {
1263 if (IRC_CheckListTooBig(Client, match_count,
1264 MAX_RPL_WHOIS, "WHOIS"))
1267 if (Client_Type(c) != CLIENT_USER)
1269 if (!MatchCaseInsensitive(query, Client_ID(c)))
1271 if (!IRC_WHOIS_SendReply(Client, from, c))
1272 return DISCONNECTED;
1277 if (match_count == 0)
1278 IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
1280 Req->argv[Req->argc - 1]);
1282 return IRC_WriteStrClient(from, RPL_ENDOFWHOIS_MSG,
1283 Client_ID(from), Req->argv[Req->argc - 1]);
1287 * Handler for the IRC "WHOWAS" command.
1289 * @param Client The client from which this command has been received.
1290 * @param Req Request structure with prefix and all parameters.
1291 * @return CONNECTED or DISCONNECTED.
1294 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1296 CLIENT *target, *prefix;
1298 char tok_buf[COMMAND_LEN];
1299 int max, last, count, i, nc;
1302 assert( Client != NULL );
1303 assert( Req != NULL );
1305 /* Wrong number of parameters? */
1307 return IRC_WriteErrClient(Client, ERR_NONICKNAMEGIVEN_MSG,
1310 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 3)
1311 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
1312 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 2, prefix)
1314 /* Do not reveal any info on disconnected users? */
1315 if (Conf_MorePrivacy)
1319 if (target != Client_ThisServer()) {
1320 IRC_WriteStrClientPrefix(target, prefix, "WHOWAS %s %s %s",
1321 Req->argv[0], Req->argv[1],
1326 whowas = Client_GetWhowas( );
1327 last = Client_GetLastWhowasIndex( );
1331 max = DEF_RPL_WHOWAS;
1332 if (Req->argc > 1) {
1333 max = atoi(Req->argv[1]);
1335 max = MAX_RPL_WHOWAS;
1339 * Break up the nick argument into a list of nicks, if applicable
1340 * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1342 strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1343 nick = strtok(tok_buf, ",");
1345 for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1349 if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1350 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1351 return DISCONNECTED;
1355 /* previous entry */
1358 /* "underflow", wrap around */
1362 if (nc && count >= max)
1364 } while (i != last);
1366 if (nc == 0 && !IRC_WriteErrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1367 Client_ID(prefix), nick))
1368 return DISCONNECTED;
1370 return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG,
1371 Client_ID(prefix), Req->argv[0]);
1375 * Send LUSERS reply to a client.
1377 * @param Client The receipient of the information.
1378 * @return CONNECTED or DISCONNECTED.
1381 IRC_Send_LUSERS(CLIENT *Client)
1388 assert(Client != NULL);
1390 /* Users, services and servers in the network */
1391 if (!IRC_WriteStrClient(Client, RPL_LUSERCLIENT_MSG, Client_ID(Client),
1392 Client_UserCount(), Client_ServiceCount(),
1393 Client_ServerCount()))
1394 return DISCONNECTED;
1396 /* Number of IRC operators */
1397 cnt = Client_OperCount( );
1399 if (!IRC_WriteStrClient(Client, RPL_LUSEROP_MSG,
1400 Client_ID(Client), cnt))
1401 return DISCONNECTED;
1404 /* Unknown connections */
1405 cnt = Client_UnknownCount( );
1407 if (!IRC_WriteStrClient(Client, RPL_LUSERUNKNOWN_MSG,
1408 Client_ID(Client), cnt))
1409 return DISCONNECTED;
1412 /* Number of created channels */
1413 if (!IRC_WriteStrClient(Client, RPL_LUSERCHANNELS_MSG,
1415 Channel_CountVisible(Client)))
1416 return DISCONNECTED;
1418 /* Number of local users, services and servers */
1419 if (!IRC_WriteStrClient(Client, RPL_LUSERME_MSG, Client_ID(Client),
1420 Client_MyUserCount(), Client_MyServiceCount(),
1421 Client_MyServerCount()))
1422 return DISCONNECTED;
1425 /* Maximum number of local users */
1426 cnt = Client_MyUserCount();
1427 max = Client_MyMaxUserCount();
1428 if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1429 cnt, max, cnt, max))
1430 return DISCONNECTED;
1431 /* Maximum number of users in the network */
1432 cnt = Client_UserCount();
1433 max = Client_MaxUserCount();
1434 if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1435 cnt, max, cnt, max))
1436 return DISCONNECTED;
1437 /* Connection counters */
1438 if (! IRC_WriteStrClient(Client, RPL_STATSCONN_MSG, Client_ID(Client),
1439 Conn_CountMax(), Conn_CountAccepted()))
1440 return DISCONNECTED;
1444 } /* IRC_Send_LUSERS */
1447 IRC_Show_MOTD( CLIENT *Client )
1450 size_t len_tot, len_str;
1452 assert( Client != NULL );
1454 len_tot = array_bytes(&Conf_Motd);
1455 if (len_tot == 0 && !Conn_UsesSSL(Client_Conn(Client)))
1456 return IRC_WriteErrClient(Client, ERR_NOMOTD_MSG, Client_ID(Client));
1458 if (!IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG, Client_ID(Client),
1459 Client_ID(Client_ThisServer())))
1460 return DISCONNECTED;
1462 line = array_start(&Conf_Motd);
1463 while (len_tot > 0) {
1464 len_str = strlen(line) + 1;
1466 assert(len_tot >= len_str);
1469 if (!IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID(Client), line))
1470 return DISCONNECTED;
1474 if (!Show_MOTD_SSLInfo(Client))
1475 return DISCONNECTED;
1477 if (!IRC_WriteStrClient(Client, RPL_ENDOFMOTD_MSG, Client_ID(Client)))
1478 return DISCONNECTED;
1480 if (*Conf_CloakHost)
1481 return IRC_WriteStrClient(Client, RPL_HOSTHIDDEN_MSG,
1483 Client_Hostname(Client));
1486 } /* IRC_Show_MOTD */
1489 * Send NAMES reply for a specific client and channel.
1491 * @param Client The client requesting the NAMES information.
1492 * @param Chan The channel
1493 * @return CONNECTED or DISCONNECTED.
1496 IRC_Send_NAMES(CLIENT * Client, CHANNEL * Chan)
1498 bool is_visible, is_member;
1499 char str[COMMAND_LEN];
1503 assert(Client != NULL);
1504 assert(Chan != NULL);
1506 if (Channel_IsMemberOf(Chan, Client))
1511 /* Do not print info on channel memberships to anyone that is not member? */
1512 if (Conf_MorePrivacy && !is_member)
1515 /* Secret channel? */
1516 if (!is_member && Channel_HasMode(Chan, 's'))
1519 snprintf(str, sizeof(str), RPL_NAMREPLY_MSG, Client_ID(Client), "=",
1520 Channel_Name(Chan));
1521 cl2chan = Channel_FirstMember(Chan);
1523 cl = Channel_GetClient(cl2chan);
1525 if (Client_HasMode(cl, 'i'))
1530 if (is_member || is_visible) {
1531 if (str[strlen(str) - 1] != ':')
1532 strlcat(str, " ", sizeof(str));
1534 who_flags_qualifier(Client, Channel_UserModes(Chan, cl),
1536 strlcat(str, Client_ID(cl), sizeof(str));
1538 if (strlen(str) > (COMMAND_LEN - CLIENT_NICK_LEN - 4)) {
1539 if (!IRC_WriteStrClient(Client, "%s", str))
1540 return DISCONNECTED;
1541 snprintf(str, sizeof(str), RPL_NAMREPLY_MSG,
1542 Client_ID(Client), "=",
1543 Channel_Name(Chan));
1547 cl2chan = Channel_NextMember(Chan, cl2chan);
1549 if (str[strlen(str) - 1] != ':') {
1550 if (!IRC_WriteStrClient(Client, "%s", str))
1551 return DISCONNECTED;
1555 } /* IRC_Send_NAMES */
1558 * Send the ISUPPORT numeric (005).
1559 * This numeric indicates the features that are supported by this server.
1560 * See <http://www.irc.org/tech_docs/005.html> for details.
1563 IRC_Send_ISUPPORT(CLIENT * Client)
1565 if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1566 CHANTYPES, CHANTYPES, Conf_MaxJoins))
1567 return DISCONNECTED;
1568 return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1569 CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1570 COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1571 COMMAND_LEN - 113, MAX_HNDL_MODES_ARG,
1572 MAX_HNDL_CHANNEL_LISTS);
1573 } /* IRC_Send_ISUPPORT */