2 * IRC - Internet Relay Chat, ircd/ircd_relay.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
6 * See file AUTHORS in IRC package for additional names of
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 1, or (at your option)
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * @brief Helper functions to relay various types of messages.
25 * @version $Id: ircd_relay.c 1271 2004-12-11 05:14:07Z klmitch $
27 * There are four basic types of messages, each with four subtypes.
29 * The basic types are: channel, directed, masked, and private.
30 * Channel messages are (perhaps obviously) sent directly to a
31 * channel. Directed messages are sent to "NICK[%host]@server", but
32 * only allowed if the server is a services server (to avoid
33 * information leaks for normal clients). Masked messages are sent to
34 * either *@*host.mask or *.server.mask. Private messages are sent to
37 * The subtypes for each type are: client message, client notice,
38 * server message, and server notice. Client subtypes are sent by a
39 * local user, and server subtypes are given to us by a server.
40 * Notice subtypes correspond to the NOTICE command, and message
41 * subtypes correspond to the PRIVMSG command.
43 * As a special note, directed messages do not have server subtypes,
44 * since there is no difference in handling them based on origin.
48 #include "ircd_relay.h"
53 #include "ircd_chattr.h"
54 #include "ircd_features.h"
56 #include "ircd_reply.h"
57 #include "ircd_string.h"
67 /* #include <assert.h> -- Now using assert in ircd_log.h */
73 * This file contains message relaying functions for client and server
74 * private messages and notices
75 * TODO: This file contains a lot of cut and paste code, and needs
76 * to be cleaned up a bit. The idea is to factor out the common checks
77 * but not introduce any IsOper/IsUser/MyUser/IsServer etc. stuff.
80 /** Relay a local user's message to a channel.
81 * Generates an error if the client cannot send to the channel.
82 * @param[in] sptr Client that originated the message.
83 * @param[in] name Name of target channel.
84 * @param[in] text %Message to relay.
85 * @param[in] ccount Count of channels we're sending the message to.
87 void relay_channel_message(struct Client* sptr, const char* name, const char* text, const int ccount)
89 struct Channel* chptr;
95 if (0 == (chptr = FindChannel(name))) {
96 send_reply(sptr, ERR_NOSUCHCHANNEL, name);
100 /* First thing we do is passing the message to the extended NOAMSG mode.
101 * It even can use the input when the message is dropped later on.
103 if(!IsXtraOp(sptr) && !HasPriv(sptr, PRIV_NOAMSG_OVERRIDE) && ext_amsg_block(sptr, chptr, text)) {
104 send_reply(sptr, ERR_CANNOTSENDTOCHAN, chptr->chname);
109 * This first: Almost never a server/service
111 if (!client_can_send_to_channel(sptr, chptr, 0)) {
112 send_reply(sptr, ERR_CANNOTSENDTOCHAN, chptr->chname);
115 if ((chptr->mode.mode & MODE_NOPRIVMSGS) &&
116 check_target_limit(sptr, chptr, chptr->chname, 0))
119 /* multi target message check */
120 if(!IsXtraOp(sptr) && !HasPriv(sptr, PRIV_NOAMSG_OVERRIDE) && (chptr->mode.mode & MODE_NOAMSGS) && (ccount > 1)) {
121 send_reply(sptr, ERR_CANNOTSENDTOCHAN, chptr->chname);
125 /* Check for color/ctcp. */
126 if((chptr->mode.mode & MODE_NOCOLOUR) && !IsOverrideCC(sptr)) {
127 for(c = text; *c; c++) {
128 if(*c == 3 || *c == 27) {
129 send_reply(sptr, ERR_CANNOTSENDTOCHAN, chptr->chname);
134 /* Still allow /me to function. */
135 if((chptr->mode.mode & MODE_NOCTCP) && !IsOverrideCC(sptr) && ircd_strncmp(text, "\001ACTION ", 8)) {
136 for(c = text; *c; ++c) {
138 send_reply(sptr, ERR_CANNOTSENDTOCHAN, chptr->chname);
144 RevealDelayedJoinIfNeeded(sptr, chptr);
145 sendcmdto_channel_butone(sptr, CMD_PRIVATE, chptr, cli_from(sptr),
146 SKIP_DEAF | SKIP_BURST, text[0], "%H :%s", chptr, text);
149 /** Relay a local user's notice to a channel.
150 * Silently exits if the client cannot send to the channel.
151 * @param[in] sptr Client that originated the message.
152 * @param[in] name Name of target channel.
153 * @param[in] text %Message to relay.
154 * @param[in] ccount Count of channels we're sending the message to.
156 void relay_channel_notice(struct Client* sptr, const char* name, const char* text, const int ccount)
158 struct Channel* chptr;
164 if (0 == (chptr = FindChannel(name)))
167 /* First thing we do is passing the message to the extended NOAMSG mode.
168 * It even can use the input when the message is dropped later on.
170 if(!IsXtraOp(sptr) && !HasPriv(sptr, PRIV_NOAMSG_OVERRIDE) && ext_amsg_block(sptr, chptr, text))
173 /* Now we check if the channel has the NONOTICE mode
174 * If NONOTICE is set only XtraOPS or ColorOverride users can send notices!
176 if((chptr->mode.mode & MODE_NONOTICE) && !IsOverrideCC(sptr) && !IsXtraOp(sptr)) {
177 send_reply(sptr, ERR_CANNOTSENDTOCHAN, chptr->chname);
182 * This first: Almost never a server/service
184 if (!client_can_send_to_channel(sptr, chptr, 0))
187 if ((chptr->mode.mode & MODE_NOPRIVMSGS) &&
188 check_target_limit(sptr, chptr, chptr->chname, 0))
191 /* multi target message check */
192 if(!IsXtraOp(sptr) && !HasPriv(sptr, PRIV_NOAMSG_OVERRIDE) && (chptr->mode.mode & MODE_NOAMSGS) && (ccount > 1))
195 /* Check for color/ctcp. */
196 if((chptr->mode.mode & MODE_NOCOLOUR) && !IsOverrideCC(sptr)) {
197 for(c = text; *c; c++) {
198 if(*c == 3 || *c == 27) {
203 /* Still allow /me to function. */
204 if((chptr->mode.mode & MODE_NOCTCP) && !IsOverrideCC(sptr) && ircd_strncmp(text, "\001ACTION ", 8)) {
205 for(c = text; *c; ++c) {
212 RevealDelayedJoinIfNeeded(sptr, chptr);
213 sendcmdto_channel_butone(sptr, CMD_NOTICE, chptr, cli_from(sptr),
214 SKIP_DEAF | SKIP_BURST, '\0', "%H :%s", chptr, text);
217 /** Relay a message to a channel.
218 * Generates an error if the client cannot send to the channel,
219 * or if the channel is a local channel
220 * @param[in] sptr Client that originated the message.
221 * @param[in] name Name of target channel.
222 * @param[in] text %Message to relay.
224 void server_relay_channel_message(struct Client* sptr, const char* name, const char* text)
226 struct Channel* chptr;
231 if (IsLocalChannel(name) || 0 == (chptr = FindChannel(name))) {
232 send_reply(sptr, ERR_NOSUCHCHANNEL, name);
236 * This first: Almost never a server/service
237 * Servers may have channel services, need to check for it here
239 if (client_can_send_to_channel(sptr, chptr, 1) || IsChannelService(sptr)) {
240 sendcmdto_channel_butone(sptr, CMD_PRIVATE, chptr, cli_from(sptr),
241 SKIP_DEAF | SKIP_BURST, text[0], "%H :%s", chptr, text);
244 send_reply(sptr, ERR_CANNOTSENDTOCHAN, chptr->chname);
247 /** Relay a notice to a channel.
248 * Generates an error if the client cannot send to the channel,
249 * or if the channel is a local channel
250 * @param[in] sptr Client that originated the message.
251 * @param[in] name Name of target channel.
252 * @param[in] text %Message to relay.
254 void server_relay_channel_notice(struct Client* sptr, const char* name, const char* text)
256 struct Channel* chptr;
261 if (IsLocalChannel(name) || 0 == (chptr = FindChannel(name)))
264 * This first: Almost never a server/service
265 * Servers may have channel services, need to check for it here
267 if (client_can_send_to_channel(sptr, chptr, 1) || IsChannelService(sptr)) {
268 sendcmdto_channel_butone(sptr, CMD_NOTICE, chptr, cli_from(sptr),
269 SKIP_DEAF | SKIP_BURST, '\0', "%H :%s", chptr, text);
273 /** Relay a directed message.
274 * Same as "relay_directed_message" but has some other privilege checks.
275 * Checks that the target user is on an ulined server.
276 * If this check fails, it takes \a server as an account
277 * name and checks whether \a name has the account set.
278 * If both checks fail, it prints a message to the user and returns.
279 * @param[in] sptr Client that originated the message.
280 * @param[in] name Target nickname.
281 * @param[in] server Name of target server.
282 * @param[in] text %Message to relay.
284 int relay_directed_account_server_message(struct Client* sptr, char* name, char* server, const char* text)
286 struct Client* acptr, *dest;
297 if ((host = strchr(name, '%')))
300 if (!(acptr = FindUser(name)) || (!EmptyString(host) && 0 != match(host, cli_user(acptr)->host)))
305 if((dest = FindServer(serv)) == NULL || dest != cli_user(acptr)->server || !IsService(dest)) {
306 if(!IsNetServ(acptr) || !IsAccount(acptr) || 0 != ircd_strcmp(cli_user(acptr)->account, serv)) {
315 sendcmdto_one(sptr, CMD_PRIVATE, acptr, "%s :%s", name, text);
321 sendcmdto_one(sptr, CMD_PRIVATE, acptr, "%C :%s", acptr, text);
325 if (!(is_silenced(sptr, acptr)))
326 sendcmdto_one(sptr, CMD_PRIVATE, acptr, "%C :%s", acptr, text);
336 /** Relay a directed message.
337 * Generates an error if the named server does not exist, if it is not
338 * a services server, or if \a name names a local user and a hostmask
339 * is specified but does not match.
340 * @param[in] sptr Client that originated the message.
341 * @param[in] name Target nickname, with optional "%hostname" suffix.
342 * @param[in] server Name of target server.
343 * @param[in] text %Message to relay.
345 void relay_directed_message(struct Client* sptr, char* name, char* server, const char* text)
347 struct Client* acptr;
355 if ((acptr = FindServer(server + 1)) == NULL || !IsService(acptr))
357 send_reply(sptr, ERR_NOSUCHNICK, name);
361 * NICK[%host]@server addressed? See if <server> is me first
365 sendcmdto_one(sptr, CMD_PRIVATE, acptr, "%s :%s", name, text);
369 * Look for an user whose NICK is equal to <name> and then
370 * check if it's hostname matches <host> and if it's a local
374 if ((host = strchr(name, '%')))
377 /* As reported by Vampire-, it's possible to brute force finding users
378 * by sending a message to each server and see which one succeeded.
379 * This means we have to remove error reporting. Sigh. Better than
380 * removing the ability to send directed messages to client servers
381 * Thanks for the suggestion Vampire=. -- Isomer 2001-08-28
382 * Argh, /ping nick@server, disallow messages to non +k clients :/ I hate
383 * this. -- Isomer 2001-09-16
385 if (!(acptr = FindUser(name)) || !MyUser(acptr) ||
386 (!EmptyString(host) && 0 != match(host, cli_user(acptr)->host)) ||
387 !IsChannelService(acptr))
390 * By this stage we might as well not bother because they will
391 * know that this server is currently linked because of the
394 send_reply(sptr, ERR_NOSUCHNICK, name);
402 if (!(is_silenced(sptr, acptr)))
403 sendcmdto_one(sptr, CMD_PRIVATE, acptr, "%s :%s", name, text);
406 /** Relay a directed notice.
407 * Generates an error if the named server does not exist, if it is not
408 * a services server, or if \a name names a local user and a hostmask
409 * is specified but does not match.
410 * @param[in] sptr Client that originated the message.
411 * @param[in] name Target nickname, with optional "%hostname" suffix.
412 * @param[in] server Name of target server.
413 * @param[in] text %Message to relay.
415 void relay_directed_notice(struct Client* sptr, char* name, char* server, const char* text)
417 struct Client* acptr;
425 if (0 == (acptr = FindServer(server + 1)))
428 * NICK[%host]@server addressed? See if <server> is me first
431 sendcmdto_one(sptr, CMD_NOTICE, acptr, "%s :%s", name, text);
435 * Look for an user whose NICK is equal to <name> and then
436 * check if it's hostname matches <host> and if it's a local
440 if ((host = strchr(name, '%')))
443 if (!(acptr = FindUser(name)) || !MyUser(acptr) ||
444 (!EmptyString(host) && 0 != match(host, cli_user(acptr)->host)))
451 if (!(is_silenced(sptr, acptr)))
452 sendcmdto_one(sptr, CMD_NOTICE, acptr, "%s :%s", name, text);
455 /** Relay a private message from a local user.
456 * Returns an error if the user does not exist or sending to him would
457 * exceed the source's free targets. Sends an AWAY status message if
458 * the target is marked as away.
459 * @param[in] sptr Client that originated the message.
460 * @param[in] name Nickname of target user.
461 * @param[in] text %Message to relay.
463 void relay_private_message(struct Client* sptr, const char* name, const char* text)
465 struct Client* acptr;
471 if (0 == (acptr = FindUser(name))) {
472 send_reply(sptr, ERR_NOSUCHNICK, name);
475 if ((!IsChannelService(acptr) &&
476 check_target_limit(sptr, acptr, cli_name(acptr), 0)) ||
477 is_silenced(sptr, acptr))
481 * send away message if user away
483 if (cli_user(acptr) && cli_user(acptr)->away)
484 send_reply(sptr, RPL_AWAY, cli_name(acptr), cli_user(acptr)->away);
486 * deliver the message
489 add_target(acptr, sptr);
491 sendcmdto_one(sptr, CMD_PRIVATE, acptr, "%C :%s", acptr, text);
494 /** Relay a private notice from a local user.
495 * Returns an error if the user does not exist or sending to him would
496 * exceed the source's free targets. Sends an AWAY status message if
497 * the target is marked as away.
498 * @param[in] sptr Client that originated the message.
499 * @param[in] name Nickname of target user.
500 * @param[in] text %Message to relay.
502 void relay_private_notice(struct Client* sptr, const char* name, const char* text)
504 struct Client* acptr;
509 if (0 == (acptr = FindUser(name)))
511 if ((!IsChannelService(acptr) &&
512 check_target_limit(sptr, acptr, cli_name(acptr), 0)) ||
513 is_silenced(sptr, acptr))
516 * deliver the message
519 add_target(acptr, sptr);
521 sendcmdto_one(sptr, CMD_NOTICE, acptr, "%C :%s", acptr, text);
524 /** Relay a private message that arrived from a server.
525 * Returns an error if the user does not exist.
526 * @param[in] sptr Client that originated the message.
527 * @param[in] name Nickname of target user.
528 * @param[in] text %Message to relay.
530 void server_relay_private_message(struct Client* sptr, const char* name, const char* text)
532 struct Client* acptr;
537 * nickname addressed?
539 if (0 == (acptr = findNUser(name)) || !IsUser(acptr)) {
540 send_reply(sptr, SND_EXPLICIT | ERR_NOSUCHNICK, "* :Target left %s. "
541 "Failed to deliver: [%.20s]", feature_str(FEAT_NETWORK),
545 if (is_silenced(sptr, acptr))
549 add_target(acptr, sptr);
551 sendcmdto_one(sptr, CMD_PRIVATE, acptr, "%C :%s", acptr, text);
555 /** Relay a private notice that arrived from a server.
556 * Returns an error if the user does not exist.
557 * @param[in] sptr Client that originated the message.
558 * @param[in] name Nickname of target user.
559 * @param[in] text %Message to relay.
561 void server_relay_private_notice(struct Client* sptr, const char* name, const char* text)
563 struct Client* acptr;
568 * nickname addressed?
570 if (0 == (acptr = findNUser(name)) || !IsUser(acptr))
573 if (is_silenced(sptr, acptr))
577 add_target(acptr, sptr);
579 sendcmdto_one(sptr, CMD_NOTICE, acptr, "%C :%s", acptr, text);
582 /** Relay a masked message from a local user.
583 * Sends an error response if there is no top-level domain label in \a
584 * mask, or if that TLD contains a wildcard.
585 * @param[in] sptr Client that originated the message.
586 * @param[in] mask Target mask for the message.
587 * @param[in] text %Message to relay.
589 void relay_masked_message(struct Client* sptr, const char* mask, const char* text)
598 * look for the last '.' in mask and scan forward
600 if (0 == (s = strrchr(mask, '.'))) {
601 send_reply(sptr, ERR_NOTOPLEVEL, mask);
605 if (*s == '.' || *s == '*' || *s == '?')
608 if (*s == '*' || *s == '?') {
609 send_reply(sptr, ERR_WILDTOPLEVEL, mask);
618 sendcmdto_match_butone(sptr, CMD_PRIVATE, s,
619 IsServer(cli_from(sptr)) ? cli_from(sptr) : 0,
620 host_mask ? MATCH_HOST : MATCH_SERVER,
621 "%s :%s", mask, text);
624 /** Relay a masked notice from a local user.
625 * Sends an error response if there is no top-level domain label in \a
626 * mask, or if that TLD contains a wildcard.
627 * @param[in] sptr Client that originated the message.
628 * @param[in] mask Target mask for the message.
629 * @param[in] text %Message to relay.
631 void relay_masked_notice(struct Client* sptr, const char* mask, const char* text)
640 * look for the last '.' in mask and scan forward
642 if (0 == (s = strrchr(mask, '.'))) {
643 send_reply(sptr, ERR_NOTOPLEVEL, mask);
647 if (*s == '.' || *s == '*' || *s == '?')
650 if (*s == '*' || *s == '?') {
651 send_reply(sptr, ERR_WILDTOPLEVEL, mask);
660 sendcmdto_match_butone(sptr, CMD_NOTICE, s,
661 IsServer(cli_from(sptr)) ? cli_from(sptr) : 0,
662 host_mask ? MATCH_HOST : MATCH_SERVER,
663 "%s :%s", mask, text);
666 /** Relay a masked message that arrived from a server.
667 * @param[in] sptr Client that originated the message.
668 * @param[in] mask Target mask for the message.
669 * @param[in] text %Message to relay.
671 void server_relay_masked_message(struct Client* sptr, const char* mask, const char* text)
673 const char* s = mask;
683 sendcmdto_match_butone(sptr, CMD_PRIVATE, s,
684 IsServer(cli_from(sptr)) ? cli_from(sptr) : 0,
685 host_mask ? MATCH_HOST : MATCH_SERVER,
686 "%s :%s", mask, text);
689 /** Relay a masked notice that arrived from a server.
690 * @param[in] sptr Client that originated the message.
691 * @param[in] mask Target mask for the message.
692 * @param[in] text %Message to relay.
694 void server_relay_masked_notice(struct Client* sptr, const char* mask, const char* text)
696 const char* s = mask;
706 sendcmdto_match_butone(sptr, CMD_NOTICE, s,
707 IsServer(cli_from(sptr)) ? cli_from(sptr) : 0,
708 host_mask ? MATCH_HOST : MATCH_SERVER,
709 "%s :%s", mask, text);