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 return IRC_WriteErrClient(Client,
1151 ERR_NEEDMOREPARAMS_MSG,
1157 if (Req->argc >= 1) {
1158 /* Channel or mask given */
1159 chan = Channel_Search(Req->argv[0]);
1161 /* Members of a channel have been requested */
1162 return IRC_WHO_Channel(Client, chan, only_ops);
1164 if (strcmp(Req->argv[0], "0") != 0) {
1165 /* A mask has been given. But please note this RFC
1166 * stupidity: "0" is same as no arguments ... */
1167 return IRC_WHO_Mask(Client, Req->argv[0], only_ops);
1171 /* No channel or (valid) mask given */
1172 IRC_SetPenalty(Client, 2);
1173 return IRC_WHO_Mask(Client, NULL, only_ops);
1177 * Handler for the IRC "WHOIS" command.
1179 * @param Client The client from which this command has been received.
1180 * @param Req Request structure with prefix and all parameters.
1181 * @return CONNECTED or DISCONNECTED.
1184 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
1186 CLIENT *from, *target, *c;
1187 unsigned int match_count = 0, found = 0;
1188 bool has_wildcards, is_remote;
1189 bool got_wildcard = false;
1190 char mask[COMMAND_LEN], *query;
1192 assert( Client != NULL );
1193 assert( Req != NULL );
1195 IRC_SetPenalty(Client, 1);
1197 /* Wrong number of parameters? */
1199 return IRC_WriteErrClient(Client, ERR_NONICKNAMEGIVEN_MSG,
1202 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
1203 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
1205 /* Get target server for this command */
1206 if (Req->argc > 1) {
1207 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, Client)
1209 target = Client_ThisServer();
1211 assert(target != NULL);
1214 if (target != Client_ThisServer()) {
1215 IRC_WriteStrClientPrefix(target, from, "WHOIS %s :%s",
1216 Req->argv[0], Req->argv[1]);
1220 is_remote = Client_Conn(from) < 0;
1221 strlcpy(mask, Req->argv[Req->argc - 1], sizeof(mask));
1222 for (query = strtok(ngt_LowerStr(mask), ",");
1224 query = strtok(NULL, ","), found++)
1226 has_wildcards = query[strcspn(query, "*?")] != 0;
1228 * follows ircd 2.10 implementation:
1229 * - handle up to 3 targets
1230 * - no wildcards for remote clients
1231 * - only one wildcard target per local client
1233 * Also, at most MAX_RPL_WHOIS matches are returned.
1235 if (!has_wildcards || is_remote) {
1236 c = Client_Search(query);
1237 if (c && (Client_Type(c) == CLIENT_USER
1238 || Client_Type(c) == CLIENT_SERVICE)) {
1239 if (!IRC_WHOIS_SendReply(Client, from, c))
1240 return DISCONNECTED;
1242 if (!IRC_WriteErrClient(Client,
1246 return DISCONNECTED;
1251 /* we already handled one wildcard query */
1252 if (!IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
1253 Client_ID(Client), query))
1254 return DISCONNECTED;
1257 got_wildcard = true;
1258 IRC_SetPenalty(Client, 3);
1260 for (c = Client_First(); c; c = Client_Next(c)) {
1261 if (IRC_CheckListTooBig(Client, match_count,
1262 MAX_RPL_WHOIS, "WHOIS"))
1265 if (Client_Type(c) != CLIENT_USER)
1267 if (!MatchCaseInsensitive(query, Client_ID(c)))
1269 if (!IRC_WHOIS_SendReply(Client, from, c))
1270 return DISCONNECTED;
1275 if (match_count == 0)
1276 IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
1278 Req->argv[Req->argc - 1]);
1280 return IRC_WriteStrClient(from, RPL_ENDOFWHOIS_MSG,
1281 Client_ID(from), Req->argv[Req->argc - 1]);
1285 * Handler for the IRC "WHOWAS" command.
1287 * @param Client The client from which this command has been received.
1288 * @param Req Request structure with prefix and all parameters.
1289 * @return CONNECTED or DISCONNECTED.
1292 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
1294 CLIENT *target, *prefix;
1296 char tok_buf[COMMAND_LEN];
1297 int max, last, count, i, nc;
1300 assert( Client != NULL );
1301 assert( Req != NULL );
1303 /* Wrong number of parameters? */
1305 return IRC_WriteErrClient(Client, ERR_NONICKNAMEGIVEN_MSG,
1308 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 3)
1309 _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
1310 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 2, prefix)
1312 /* Do not reveal any info on disconnected users? */
1313 if (Conf_MorePrivacy)
1317 if (target != Client_ThisServer()) {
1318 IRC_WriteStrClientPrefix(target, prefix, "WHOWAS %s %s %s",
1319 Req->argv[0], Req->argv[1],
1324 whowas = Client_GetWhowas( );
1325 last = Client_GetLastWhowasIndex( );
1329 max = DEF_RPL_WHOWAS;
1330 if (Req->argc > 1) {
1331 max = atoi(Req->argv[1]);
1333 max = MAX_RPL_WHOWAS;
1337 * Break up the nick argument into a list of nicks, if applicable
1338 * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
1340 strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
1341 nick = strtok(tok_buf, ",");
1343 for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
1347 if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
1348 if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
1349 return DISCONNECTED;
1353 /* previous entry */
1356 /* "underflow", wrap around */
1360 if (nc && count >= max)
1362 } while (i != last);
1364 if (nc == 0 && !IRC_WriteErrClient(prefix, ERR_WASNOSUCHNICK_MSG,
1365 Client_ID(prefix), nick))
1366 return DISCONNECTED;
1368 return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG,
1369 Client_ID(prefix), Req->argv[0]);
1373 * Send LUSERS reply to a client.
1375 * @param Client The receipient of the information.
1376 * @return CONNECTED or DISCONNECTED.
1379 IRC_Send_LUSERS(CLIENT *Client)
1386 assert(Client != NULL);
1388 /* Users, services and servers in the network */
1389 if (!IRC_WriteStrClient(Client, RPL_LUSERCLIENT_MSG, Client_ID(Client),
1390 Client_UserCount(), Client_ServiceCount(),
1391 Client_ServerCount()))
1392 return DISCONNECTED;
1394 /* Number of IRC operators */
1395 cnt = Client_OperCount( );
1397 if (!IRC_WriteStrClient(Client, RPL_LUSEROP_MSG,
1398 Client_ID(Client), cnt))
1399 return DISCONNECTED;
1402 /* Unknown connections */
1403 cnt = Client_UnknownCount( );
1405 if (!IRC_WriteStrClient(Client, RPL_LUSERUNKNOWN_MSG,
1406 Client_ID(Client), cnt))
1407 return DISCONNECTED;
1410 /* Number of created channels */
1411 if (!IRC_WriteStrClient(Client, RPL_LUSERCHANNELS_MSG,
1413 Channel_CountVisible(Client)))
1414 return DISCONNECTED;
1416 /* Number of local users, services and servers */
1417 if (!IRC_WriteStrClient(Client, RPL_LUSERME_MSG, Client_ID(Client),
1418 Client_MyUserCount(), Client_MyServiceCount(),
1419 Client_MyServerCount()))
1420 return DISCONNECTED;
1423 /* Maximum number of local users */
1424 cnt = Client_MyUserCount();
1425 max = Client_MyMaxUserCount();
1426 if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
1427 cnt, max, cnt, max))
1428 return DISCONNECTED;
1429 /* Maximum number of users in the network */
1430 cnt = Client_UserCount();
1431 max = Client_MaxUserCount();
1432 if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
1433 cnt, max, cnt, max))
1434 return DISCONNECTED;
1435 /* Connection counters */
1436 if (! IRC_WriteStrClient(Client, RPL_STATSCONN_MSG, Client_ID(Client),
1437 Conn_CountMax(), Conn_CountAccepted()))
1438 return DISCONNECTED;
1442 } /* IRC_Send_LUSERS */
1445 IRC_Show_MOTD( CLIENT *Client )
1448 size_t len_tot, len_str;
1450 assert( Client != NULL );
1452 len_tot = array_bytes(&Conf_Motd);
1453 if (len_tot == 0 && !Conn_UsesSSL(Client_Conn(Client)))
1454 return IRC_WriteErrClient(Client, ERR_NOMOTD_MSG, Client_ID(Client));
1456 if (!IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG, Client_ID(Client),
1457 Client_ID(Client_ThisServer())))
1458 return DISCONNECTED;
1460 line = array_start(&Conf_Motd);
1461 while (len_tot > 0) {
1462 len_str = strlen(line) + 1;
1464 assert(len_tot >= len_str);
1467 if (!IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID(Client), line))
1468 return DISCONNECTED;
1472 if (!Show_MOTD_SSLInfo(Client))
1473 return DISCONNECTED;
1475 if (!IRC_WriteStrClient(Client, RPL_ENDOFMOTD_MSG, Client_ID(Client)))
1476 return DISCONNECTED;
1478 if (*Conf_CloakHost)
1479 return IRC_WriteStrClient(Client, RPL_HOSTHIDDEN_MSG,
1481 Client_Hostname(Client));
1484 } /* IRC_Show_MOTD */
1487 * Send NAMES reply for a specific client and channel.
1489 * @param Client The client requesting the NAMES information.
1490 * @param Chan The channel
1491 * @return CONNECTED or DISCONNECTED.
1494 IRC_Send_NAMES(CLIENT * Client, CHANNEL * Chan)
1496 bool is_visible, is_member;
1497 char str[COMMAND_LEN];
1501 assert(Client != NULL);
1502 assert(Chan != NULL);
1504 if (Channel_IsMemberOf(Chan, Client))
1509 /* Do not print info on channel memberships to anyone that is not member? */
1510 if (Conf_MorePrivacy && !is_member)
1513 /* Secret channel? */
1514 if (!is_member && Channel_HasMode(Chan, 's'))
1517 snprintf(str, sizeof(str), RPL_NAMREPLY_MSG, Client_ID(Client), "=",
1518 Channel_Name(Chan));
1519 cl2chan = Channel_FirstMember(Chan);
1521 cl = Channel_GetClient(cl2chan);
1523 if (Client_HasMode(cl, 'i'))
1528 if (is_member || is_visible) {
1529 if (str[strlen(str) - 1] != ':')
1530 strlcat(str, " ", sizeof(str));
1532 who_flags_qualifier(Client, Channel_UserModes(Chan, cl),
1534 strlcat(str, Client_ID(cl), sizeof(str));
1536 if (strlen(str) > (COMMAND_LEN - CLIENT_NICK_LEN - 4)) {
1537 if (!IRC_WriteStrClient(Client, "%s", str))
1538 return DISCONNECTED;
1539 snprintf(str, sizeof(str), RPL_NAMREPLY_MSG,
1540 Client_ID(Client), "=",
1541 Channel_Name(Chan));
1545 cl2chan = Channel_NextMember(Chan, cl2chan);
1547 if (str[strlen(str) - 1] != ':') {
1548 if (!IRC_WriteStrClient(Client, "%s", str))
1549 return DISCONNECTED;
1553 } /* IRC_Send_NAMES */
1556 * Send the ISUPPORT numeric (005).
1557 * This numeric indicates the features that are supported by this server.
1558 * See <http://www.irc.org/tech_docs/005.html> for details.
1561 IRC_Send_ISUPPORT(CLIENT * Client)
1563 if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
1564 CHANTYPES, CHANTYPES, Conf_MaxJoins))
1565 return DISCONNECTED;
1566 return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
1567 CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
1568 COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
1569 COMMAND_LEN - 113, MAX_HNDL_MODES_ARG,
1570 MAX_HNDL_CHANNEL_LISTS);
1571 } /* IRC_Send_ISUPPORT */