]>
jfr.im git - irc/quakenet/snircd.git/blob - ircd/ircd_relay.c
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,v 1.17 2004/12/11 05:13:45 klmitch Exp $
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.
86 void relay_channel_message(struct Client
* sptr
, const char* name
, const char* text
)
88 struct Channel
* chptr
;
94 if (0 == (chptr
= FindChannel(name
))) {
95 send_reply(sptr
, ERR_NOSUCHCHANNEL
, name
);
99 * This first: Almost never a server/service
101 if (!client_can_send_to_channel(sptr
, chptr
, 1)) {
102 send_reply(sptr
, ERR_CANNOTSENDTOCHAN
, chptr
->chname
);
105 if ((chptr
->mode
.mode
& MODE_NOPRIVMSGS
) &&
106 check_target_limit(sptr
, chptr
, chptr
->chname
, 0))
110 if (chptr
->mode
.mode
& MODE_NOCOLOUR
)
111 for (ch
=text
;*ch
;ch
++)
112 if (*ch
==2 || *ch
==3 || *ch
==22 || *ch
==27 || *ch
==31) {
113 send_reply(sptr
, ERR_CANNOTSENDTOCHAN
, chptr
->chname
);
117 if ((chptr
->mode
.mode
& MODE_NOCTCP
) && ircd_strncmp(text
,"\001ACTION ",8))
120 send_reply(sptr
, ERR_CANNOTSENDTOCHAN
, chptr
->chname
);
125 sendcmdto_channel_butone(sptr
, CMD_PRIVATE
, chptr
, cli_from(sptr
),
126 SKIP_DEAF
| SKIP_BURST
, "%H :%s", chptr
, text
);
129 /** Relay a local user's notice to a channel.
130 * Silently exits if the client cannot send to the channel.
131 * @param[in] sptr Client that originated the message.
132 * @param[in] name Name of target channel.
133 * @param[in] text %Message to relay.
135 void relay_channel_notice(struct Client
* sptr
, const char* name
, const char* text
)
137 struct Channel
* chptr
;
143 if (0 == (chptr
= FindChannel(name
)))
146 * This first: Almost never a server/service
148 if (!client_can_send_to_channel(sptr
, chptr
, 1))
151 if ((chptr
->mode
.mode
& MODE_NOPRIVMSGS
) &&
152 check_target_limit(sptr
, chptr
, chptr
->chname
, 0))
155 if ((chptr
->mode
.mode
& MODE_NONOTICE
)) {
156 send_reply(sptr
, ERR_CANNOTSENDTOCHAN
, chptr
->chname
);
161 if (chptr
->mode
.mode
& MODE_NOCOLOUR
)
162 for (ch
=text
;*ch
;ch
++)
163 if (*ch
==2 || *ch
==3 || *ch
==22 || *ch
==27 || *ch
==31) {
164 send_reply(sptr
, ERR_CANNOTSENDTOCHAN
, chptr
->chname
);
168 if ((chptr
->mode
.mode
& MODE_NOCTCP
) && ircd_strncmp(text
,"\001ACTION ",8))
171 send_reply(sptr
, ERR_CANNOTSENDTOCHAN
, chptr
->chname
);
175 sendcmdto_channel_butone(sptr
, CMD_NOTICE
, chptr
, cli_from(sptr
),
176 SKIP_DEAF
| SKIP_BURST
, "%H :%s", chptr
, text
);
179 /** Relay a message to a channel.
180 * Generates an error if the client cannot send to the channel,
181 * or if the channel is a local channel
182 * @param[in] sptr Client that originated the message.
183 * @param[in] name Name of target channel.
184 * @param[in] text %Message to relay.
186 void server_relay_channel_message(struct Client
* sptr
, const char* name
, const char* text
)
188 struct Channel
* chptr
;
193 if (IsLocalChannel(name
) || 0 == (chptr
= FindChannel(name
))) {
194 send_reply(sptr
, ERR_NOSUCHCHANNEL
, name
);
198 * This first: Almost never a server/service
199 * Servers may have channel services, need to check for it here
201 if (client_can_send_to_channel(sptr
, chptr
, 1) || IsChannelService(sptr
)) {
202 sendcmdto_channel_butone(sptr
, CMD_PRIVATE
, chptr
, cli_from(sptr
),
203 SKIP_DEAF
| SKIP_BURST
, "%H :%s", chptr
, text
);
206 send_reply(sptr
, ERR_CANNOTSENDTOCHAN
, chptr
->chname
);
209 /** Relay a notice to a channel.
210 * Generates an error if the client cannot send to the channel,
211 * or if the channel is a local channel
212 * @param[in] sptr Client that originated the message.
213 * @param[in] name Name of target channel.
214 * @param[in] text %Message to relay.
216 void server_relay_channel_notice(struct Client
* sptr
, const char* name
, const char* text
)
218 struct Channel
* chptr
;
223 if (IsLocalChannel(name
) || 0 == (chptr
= FindChannel(name
)))
226 * This first: Almost never a server/service
227 * Servers may have channel services, need to check for it here
229 if (client_can_send_to_channel(sptr
, chptr
, 1) || IsChannelService(sptr
)) {
230 sendcmdto_channel_butone(sptr
, CMD_NOTICE
, chptr
, cli_from(sptr
),
231 SKIP_DEAF
| SKIP_BURST
, "%H :%s", chptr
, text
);
235 /** Relay a directed message.
236 * Generates an error if the named server does not exist, if it is not
237 * a services server, or if \a name names a local user and a hostmask
238 * is specified but does not match.
239 * @param[in] sptr Client that originated the message.
240 * @param[in] name Target nickname, with optional "%hostname" suffix.
241 * @param[in] server Name of target server.
242 * @param[in] text %Message to relay.
244 void relay_directed_message(struct Client
* sptr
, char* name
, char* server
, const char* text
)
246 struct Client
* acptr
;
254 if ((acptr
= FindServer(server
+ 1)) == NULL
|| !IsService(acptr
))
256 send_reply(sptr
, ERR_NOSUCHNICK
, name
);
260 * NICK[%host]@server addressed? See if <server> is me first
264 sendcmdto_one(sptr
, CMD_PRIVATE
, acptr
, "%s :%s", name
, text
);
268 * Look for an user whose NICK is equal to <name> and then
269 * check if it's hostname matches <host> and if it's a local
273 if ((host
= strchr(name
, '%')))
276 /* As reported by Vampire-, it's possible to brute force finding users
277 * by sending a message to each server and see which one succeeded.
278 * This means we have to remove error reporting. Sigh. Better than
279 * removing the ability to send directed messages to client servers
280 * Thanks for the suggestion Vampire=. -- Isomer 2001-08-28
281 * Argh, /ping nick@server, disallow messages to non +k clients :/ I hate
282 * this. -- Isomer 2001-09-16
284 if (!(acptr
= FindUser(name
)) || !MyUser(acptr
) ||
285 (!EmptyString(host
) && 0 != match(host
, cli_user(acptr
)->host
)) ||
286 !IsChannelService(acptr
))
289 * By this stage we might as well not bother because they will
290 * know that this server is currently linked because of the
293 send_reply(sptr
, ERR_NOSUCHNICK
, name
);
301 if (!(is_silenced(sptr
, acptr
)))
302 sendcmdto_one(sptr
, CMD_PRIVATE
, acptr
, "%s :%s", name
, text
);
305 /** Relay a directed notice.
306 * Generates an error if the named server does not exist, if it is not
307 * a services server, or if \a name names a local user and a hostmask
308 * is specified but does not match.
309 * @param[in] sptr Client that originated the message.
310 * @param[in] name Target nickname, with optional "%hostname" suffix.
311 * @param[in] server Name of target server.
312 * @param[in] text %Message to relay.
314 void relay_directed_notice(struct Client
* sptr
, char* name
, char* server
, const char* text
)
316 struct Client
* acptr
;
324 if (0 == (acptr
= FindServer(server
+ 1)))
327 * NICK[%host]@server addressed? See if <server> is me first
330 sendcmdto_one(sptr
, CMD_NOTICE
, acptr
, "%s :%s", name
, text
);
334 * Look for an user whose NICK is equal to <name> and then
335 * check if it's hostname matches <host> and if it's a local
339 if ((host
= strchr(name
, '%')))
342 if (!(acptr
= FindUser(name
)) || !MyUser(acptr
) ||
343 (!EmptyString(host
) && 0 != match(host
, cli_user(acptr
)->host
)))
350 if (!(is_silenced(sptr
, acptr
)))
351 sendcmdto_one(sptr
, CMD_NOTICE
, acptr
, "%s :%s", name
, text
);
354 /** Relay a private message from a local user.
355 * Returns an error if the user does not exist or sending to him would
356 * exceed the source's free targets. Sends an AWAY status message if
357 * the target is marked as away.
358 * @param[in] sptr Client that originated the message.
359 * @param[in] name Nickname of target user.
360 * @param[in] text %Message to relay.
362 void relay_private_message(struct Client
* sptr
, const char* name
, const char* text
)
364 struct Client
* acptr
;
370 if (0 == (acptr
= FindUser(name
))) {
371 send_reply(sptr
, ERR_NOSUCHNICK
, name
);
374 if ((!IsChannelService(acptr
) &&
375 check_target_limit(sptr
, acptr
, cli_name(acptr
), 0)) ||
376 is_silenced(sptr
, acptr
))
380 * +R check, if target is +R and we're not +r (or opered) then
384 if (IsAccountOnly(acptr
) && !IsAccount(sptr
) && !IsOper(sptr
)) {
385 send_reply(sptr
, ERR_ACCOUNTONLY
, cli_name(acptr
));
390 * send away message if user away
392 if (cli_user(acptr
) && cli_user(acptr
)->away
)
393 send_reply(sptr
, RPL_AWAY
, cli_name(acptr
), cli_user(acptr
)->away
);
395 * deliver the message
398 add_target(acptr
, sptr
);
400 sendcmdto_one(sptr
, CMD_PRIVATE
, acptr
, "%C :%s", acptr
, text
);
403 /** Relay a private notice from a local user.
404 * Returns an error if the user does not exist or sending to him would
405 * exceed the source's free targets. Sends an AWAY status message if
406 * the target is marked as away.
407 * @param[in] sptr Client that originated the message.
408 * @param[in] name Nickname of target user.
409 * @param[in] text %Message to relay.
411 void relay_private_notice(struct Client
* sptr
, const char* name
, const char* text
)
413 struct Client
* acptr
;
418 if (0 == (acptr
= FindUser(name
)))
420 if ((!IsChannelService(acptr
) &&
421 check_target_limit(sptr
, acptr
, cli_name(acptr
), 0)) ||
422 is_silenced(sptr
, acptr
))
426 * +R check, if target is +R and we're not +r (or opered) then
430 if (IsAccountOnly(acptr
) && !IsAccount(sptr
) && !IsOper(sptr
))
434 * deliver the message
437 add_target(acptr
, sptr
);
439 sendcmdto_one(sptr
, CMD_NOTICE
, acptr
, "%C :%s", acptr
, text
);
442 /** Relay a private message that arrived from a server.
443 * Returns an error if the user does not exist.
444 * @param[in] sptr Client that originated the message.
445 * @param[in] name Nickname of target user.
446 * @param[in] text %Message to relay.
448 void server_relay_private_message(struct Client
* sptr
, const char* name
, const char* text
)
450 struct Client
* acptr
;
455 * nickname addressed?
457 if (0 == (acptr
= findNUser(name
)) || !IsUser(acptr
)) {
458 send_reply(sptr
, SND_EXPLICIT
| ERR_NOSUCHNICK
, "* :Target left %s. "
459 "Failed to deliver: [%.20s]", feature_str(FEAT_NETWORK
),
463 if (is_silenced(sptr
, acptr
))
467 add_target(acptr
, sptr
);
469 sendcmdto_one(sptr
, CMD_PRIVATE
, acptr
, "%C :%s", acptr
, text
);
473 /** Relay a private notice that arrived from a server.
474 * Returns an error if the user does not exist.
475 * @param[in] sptr Client that originated the message.
476 * @param[in] name Nickname of target user.
477 * @param[in] text %Message to relay.
479 void server_relay_private_notice(struct Client
* sptr
, const char* name
, const char* text
)
481 struct Client
* acptr
;
486 * nickname addressed?
488 if (0 == (acptr
= findNUser(name
)) || !IsUser(acptr
))
491 if (is_silenced(sptr
, acptr
))
495 add_target(acptr
, sptr
);
497 sendcmdto_one(sptr
, CMD_NOTICE
, acptr
, "%C :%s", acptr
, text
);
500 /** Relay a masked message from a local user.
501 * Sends an error response if there is no top-level domain label in \a
502 * mask, or if that TLD contains a wildcard.
503 * @param[in] sptr Client that originated the message.
504 * @param[in] mask Target mask for the message.
505 * @param[in] text %Message to relay.
507 void relay_masked_message(struct Client
* sptr
, const char* mask
, const char* text
)
516 * look for the last '.' in mask and scan forward
518 if (0 == (s
= strrchr(mask
, '.'))) {
519 send_reply(sptr
, ERR_NOTOPLEVEL
, mask
);
523 if (*s
== '.' || *s
== '*' || *s
== '?')
526 if (*s
== '*' || *s
== '?') {
527 send_reply(sptr
, ERR_WILDTOPLEVEL
, mask
);
536 sendcmdto_match_butone(sptr
, CMD_PRIVATE
, s
,
537 IsServer(cli_from(sptr
)) ? cli_from(sptr
) : 0,
538 host_mask
? MATCH_HOST
: MATCH_SERVER
,
539 "%s :%s", mask
, text
);
542 /** Relay a masked notice from a local user.
543 * Sends an error response if there is no top-level domain label in \a
544 * mask, or if that TLD contains a wildcard.
545 * @param[in] sptr Client that originated the message.
546 * @param[in] mask Target mask for the message.
547 * @param[in] text %Message to relay.
549 void relay_masked_notice(struct Client
* sptr
, const char* mask
, const char* text
)
558 * look for the last '.' in mask and scan forward
560 if (0 == (s
= strrchr(mask
, '.'))) {
561 send_reply(sptr
, ERR_NOTOPLEVEL
, mask
);
565 if (*s
== '.' || *s
== '*' || *s
== '?')
568 if (*s
== '*' || *s
== '?') {
569 send_reply(sptr
, ERR_WILDTOPLEVEL
, mask
);
578 sendcmdto_match_butone(sptr
, CMD_NOTICE
, s
,
579 IsServer(cli_from(sptr
)) ? cli_from(sptr
) : 0,
580 host_mask
? MATCH_HOST
: MATCH_SERVER
,
581 "%s :%s", mask
, text
);
584 /** Relay a masked message that arrived from a server.
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 server_relay_masked_message(struct Client
* sptr
, const char* mask
, const char* text
)
591 const char* s
= mask
;
601 sendcmdto_match_butone(sptr
, CMD_PRIVATE
, s
,
602 IsServer(cli_from(sptr
)) ? cli_from(sptr
) : 0,
603 host_mask
? MATCH_HOST
: MATCH_SERVER
,
604 "%s :%s", mask
, text
);
607 /** Relay a masked notice that arrived from a server.
608 * @param[in] sptr Client that originated the message.
609 * @param[in] mask Target mask for the message.
610 * @param[in] text %Message to relay.
612 void server_relay_masked_notice(struct Client
* sptr
, const char* mask
, const char* text
)
614 const char* s
= mask
;
624 sendcmdto_match_butone(sptr
, CMD_NOTICE
, s
,
625 IsServer(cli_from(sptr
)) ? cli_from(sptr
) : 0,
626 host_mask
? MATCH_HOST
: MATCH_SERVER
,
627 "%s :%s", mask
, text
);