]>
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
;
93 if (0 == (chptr
= FindChannel(name
))) {
94 send_reply(sptr
, ERR_NOSUCHCHANNEL
, name
);
98 * This first: Almost never a server/service
100 if (!client_can_send_to_channel(sptr
, chptr
, 1)) {
101 send_reply(sptr
, ERR_CANNOTSENDTOCHAN
, chptr
->chname
);
104 if ((chptr
->mode
.mode
& MODE_NOPRIVMSGS
) &&
105 check_target_limit(sptr
, chptr
, chptr
->chname
, 0))
108 sendcmdto_channel_butone(sptr
, CMD_PRIVATE
, chptr
, cli_from(sptr
),
109 SKIP_DEAF
| SKIP_BURST
, "%H :%s", chptr
, text
);
112 /** Relay a local user's notice to a channel.
113 * Silently exits if the client cannot send to the channel.
114 * @param[in] sptr Client that originated the message.
115 * @param[in] name Name of target channel.
116 * @param[in] text %Message to relay.
118 void relay_channel_notice(struct Client
* sptr
, const char* name
, const char* text
)
120 struct Channel
* chptr
;
125 if (0 == (chptr
= FindChannel(name
)))
128 * This first: Almost never a server/service
130 if (!client_can_send_to_channel(sptr
, chptr
, 1))
133 if ((chptr
->mode
.mode
& MODE_NOPRIVMSGS
) &&
134 check_target_limit(sptr
, chptr
, chptr
->chname
, 0))
137 sendcmdto_channel_butone(sptr
, CMD_NOTICE
, chptr
, cli_from(sptr
),
138 SKIP_DEAF
| SKIP_BURST
, "%H :%s", chptr
, text
);
141 /** Relay a message to a channel.
142 * Generates an error if the client cannot send to the channel,
143 * or if the channel is a local channel
144 * @param[in] sptr Client that originated the message.
145 * @param[in] name Name of target channel.
146 * @param[in] text %Message to relay.
148 void server_relay_channel_message(struct Client
* sptr
, const char* name
, const char* text
)
150 struct Channel
* chptr
;
155 if (IsLocalChannel(name
) || 0 == (chptr
= FindChannel(name
))) {
156 send_reply(sptr
, ERR_NOSUCHCHANNEL
, name
);
160 * This first: Almost never a server/service
161 * Servers may have channel services, need to check for it here
163 if (client_can_send_to_channel(sptr
, chptr
, 1) || IsChannelService(sptr
)) {
164 sendcmdto_channel_butone(sptr
, CMD_PRIVATE
, chptr
, cli_from(sptr
),
165 SKIP_DEAF
| SKIP_BURST
, "%H :%s", chptr
, text
);
168 send_reply(sptr
, ERR_CANNOTSENDTOCHAN
, chptr
->chname
);
171 /** Relay a notice to a channel.
172 * Generates an error if the client cannot send to the channel,
173 * or if the channel is a local channel
174 * @param[in] sptr Client that originated the message.
175 * @param[in] name Name of target channel.
176 * @param[in] text %Message to relay.
178 void server_relay_channel_notice(struct Client
* sptr
, const char* name
, const char* text
)
180 struct Channel
* chptr
;
185 if (IsLocalChannel(name
) || 0 == (chptr
= FindChannel(name
)))
188 * This first: Almost never a server/service
189 * Servers may have channel services, need to check for it here
191 if (client_can_send_to_channel(sptr
, chptr
, 1) || IsChannelService(sptr
)) {
192 sendcmdto_channel_butone(sptr
, CMD_NOTICE
, chptr
, cli_from(sptr
),
193 SKIP_DEAF
| SKIP_BURST
, "%H :%s", chptr
, text
);
197 /** Relay a directed message.
198 * Generates an error if the named server does not exist, if it is not
199 * a services server, or if \a name names a local user and a hostmask
200 * is specified but does not match.
201 * @param[in] sptr Client that originated the message.
202 * @param[in] name Target nickname, with optional "%hostname" suffix.
203 * @param[in] server Name of target server.
204 * @param[in] text %Message to relay.
206 void relay_directed_message(struct Client
* sptr
, char* name
, char* server
, const char* text
)
208 struct Client
* acptr
;
216 if ((acptr
= FindServer(server
+ 1)) == NULL
|| !IsService(acptr
))
218 send_reply(sptr
, ERR_NOSUCHNICK
, name
);
222 * NICK[%host]@server addressed? See if <server> is me first
226 sendcmdto_one(sptr
, CMD_PRIVATE
, acptr
, "%s :%s", name
, text
);
230 * Look for an user whose NICK is equal to <name> and then
231 * check if it's hostname matches <host> and if it's a local
235 if ((host
= strchr(name
, '%')))
238 /* As reported by Vampire-, it's possible to brute force finding users
239 * by sending a message to each server and see which one succeeded.
240 * This means we have to remove error reporting. Sigh. Better than
241 * removing the ability to send directed messages to client servers
242 * Thanks for the suggestion Vampire=. -- Isomer 2001-08-28
243 * Argh, /ping nick@server, disallow messages to non +k clients :/ I hate
244 * this. -- Isomer 2001-09-16
246 if (!(acptr
= FindUser(name
)) || !MyUser(acptr
) ||
247 (!EmptyString(host
) && 0 != match(host
, cli_user(acptr
)->host
)) ||
248 !IsChannelService(acptr
))
251 * By this stage we might as well not bother because they will
252 * know that this server is currently linked because of the
255 send_reply(sptr
, ERR_NOSUCHNICK
, name
);
263 if (!(is_silenced(sptr
, acptr
)))
264 sendcmdto_one(sptr
, CMD_PRIVATE
, acptr
, "%s :%s", name
, text
);
267 /** Relay a directed notice.
268 * Generates an error if the named server does not exist, if it is not
269 * a services server, or if \a name names a local user and a hostmask
270 * is specified but does not match.
271 * @param[in] sptr Client that originated the message.
272 * @param[in] name Target nickname, with optional "%hostname" suffix.
273 * @param[in] server Name of target server.
274 * @param[in] text %Message to relay.
276 void relay_directed_notice(struct Client
* sptr
, char* name
, char* server
, const char* text
)
278 struct Client
* acptr
;
286 if (0 == (acptr
= FindServer(server
+ 1)))
289 * NICK[%host]@server addressed? See if <server> is me first
292 sendcmdto_one(sptr
, CMD_NOTICE
, acptr
, "%s :%s", name
, text
);
296 * Look for an user whose NICK is equal to <name> and then
297 * check if it's hostname matches <host> and if it's a local
301 if ((host
= strchr(name
, '%')))
304 if (!(acptr
= FindUser(name
)) || !MyUser(acptr
) ||
305 (!EmptyString(host
) && 0 != match(host
, cli_user(acptr
)->host
)))
312 if (!(is_silenced(sptr
, acptr
)))
313 sendcmdto_one(sptr
, CMD_NOTICE
, acptr
, "%s :%s", name
, text
);
316 /** Relay a private message from a local user.
317 * Returns an error if the user does not exist or sending to him would
318 * exceed the source's free targets. Sends an AWAY status message if
319 * the target is marked as away.
320 * @param[in] sptr Client that originated the message.
321 * @param[in] name Nickname of target user.
322 * @param[in] text %Message to relay.
324 void relay_private_message(struct Client
* sptr
, const char* name
, const char* text
)
326 struct Client
* acptr
;
332 if (0 == (acptr
= FindUser(name
))) {
333 send_reply(sptr
, ERR_NOSUCHNICK
, name
);
336 if ((!IsChannelService(acptr
) &&
337 check_target_limit(sptr
, acptr
, cli_name(acptr
), 0)) ||
338 is_silenced(sptr
, acptr
))
342 * send away message if user away
344 if (cli_user(acptr
) && cli_user(acptr
)->away
)
345 send_reply(sptr
, RPL_AWAY
, cli_name(acptr
), cli_user(acptr
)->away
);
347 * deliver the message
350 add_target(acptr
, sptr
);
352 sendcmdto_one(sptr
, CMD_PRIVATE
, acptr
, "%C :%s", acptr
, text
);
355 /** Relay a private notice from a local user.
356 * Returns an error if the user does not exist or sending to him would
357 * exceed the source's free targets. Sends an AWAY status message if
358 * the target is marked as away.
359 * @param[in] sptr Client that originated the message.
360 * @param[in] name Nickname of target user.
361 * @param[in] text %Message to relay.
363 void relay_private_notice(struct Client
* sptr
, const char* name
, const char* text
)
365 struct Client
* acptr
;
370 if (0 == (acptr
= FindUser(name
)))
372 if ((!IsChannelService(acptr
) &&
373 check_target_limit(sptr
, acptr
, cli_name(acptr
), 0)) ||
374 is_silenced(sptr
, acptr
))
377 * deliver the message
380 add_target(acptr
, sptr
);
382 sendcmdto_one(sptr
, CMD_NOTICE
, acptr
, "%C :%s", acptr
, text
);
385 /** Relay a private message that arrived from a server.
386 * Returns an error if the user does not exist.
387 * @param[in] sptr Client that originated the message.
388 * @param[in] name Nickname of target user.
389 * @param[in] text %Message to relay.
391 void server_relay_private_message(struct Client
* sptr
, const char* name
, const char* text
)
393 struct Client
* acptr
;
398 * nickname addressed?
400 if (0 == (acptr
= findNUser(name
)) || !IsUser(acptr
)) {
401 send_reply(sptr
, SND_EXPLICIT
| ERR_NOSUCHNICK
, "* :Target left %s. "
402 "Failed to deliver: [%.20s]", feature_str(FEAT_NETWORK
),
406 if (is_silenced(sptr
, acptr
))
410 add_target(acptr
, sptr
);
412 sendcmdto_one(sptr
, CMD_PRIVATE
, acptr
, "%C :%s", acptr
, text
);
416 /** Relay a private notice that arrived from a server.
417 * Returns an error if the user does not exist.
418 * @param[in] sptr Client that originated the message.
419 * @param[in] name Nickname of target user.
420 * @param[in] text %Message to relay.
422 void server_relay_private_notice(struct Client
* sptr
, const char* name
, const char* text
)
424 struct Client
* acptr
;
429 * nickname addressed?
431 if (0 == (acptr
= findNUser(name
)) || !IsUser(acptr
))
434 if (is_silenced(sptr
, acptr
))
438 add_target(acptr
, sptr
);
440 sendcmdto_one(sptr
, CMD_NOTICE
, acptr
, "%C :%s", acptr
, text
);
443 /** Relay a masked message from a local user.
444 * Sends an error response if there is no top-level domain label in \a
445 * mask, or if that TLD contains a wildcard.
446 * @param[in] sptr Client that originated the message.
447 * @param[in] mask Target mask for the message.
448 * @param[in] text %Message to relay.
450 void relay_masked_message(struct Client
* sptr
, const char* mask
, const char* text
)
459 * look for the last '.' in mask and scan forward
461 if (0 == (s
= strrchr(mask
, '.'))) {
462 send_reply(sptr
, ERR_NOTOPLEVEL
, mask
);
466 if (*s
== '.' || *s
== '*' || *s
== '?')
469 if (*s
== '*' || *s
== '?') {
470 send_reply(sptr
, ERR_WILDTOPLEVEL
, mask
);
479 sendcmdto_match_butone(sptr
, CMD_PRIVATE
, s
,
480 IsServer(cli_from(sptr
)) ? cli_from(sptr
) : 0,
481 host_mask
? MATCH_HOST
: MATCH_SERVER
,
482 "%s :%s", mask
, text
);
485 /** Relay a masked notice from a local user.
486 * Sends an error response if there is no top-level domain label in \a
487 * mask, or if that TLD contains a wildcard.
488 * @param[in] sptr Client that originated the message.
489 * @param[in] mask Target mask for the message.
490 * @param[in] text %Message to relay.
492 void relay_masked_notice(struct Client
* sptr
, const char* mask
, const char* text
)
501 * look for the last '.' in mask and scan forward
503 if (0 == (s
= strrchr(mask
, '.'))) {
504 send_reply(sptr
, ERR_NOTOPLEVEL
, mask
);
508 if (*s
== '.' || *s
== '*' || *s
== '?')
511 if (*s
== '*' || *s
== '?') {
512 send_reply(sptr
, ERR_WILDTOPLEVEL
, mask
);
521 sendcmdto_match_butone(sptr
, CMD_NOTICE
, s
,
522 IsServer(cli_from(sptr
)) ? cli_from(sptr
) : 0,
523 host_mask
? MATCH_HOST
: MATCH_SERVER
,
524 "%s :%s", mask
, text
);
527 /** Relay a masked message that arrived from a server.
528 * @param[in] sptr Client that originated the message.
529 * @param[in] mask Target mask for the message.
530 * @param[in] text %Message to relay.
532 void server_relay_masked_message(struct Client
* sptr
, const char* mask
, const char* text
)
534 const char* s
= mask
;
544 sendcmdto_match_butone(sptr
, CMD_PRIVATE
, s
,
545 IsServer(cli_from(sptr
)) ? cli_from(sptr
) : 0,
546 host_mask
? MATCH_HOST
: MATCH_SERVER
,
547 "%s :%s", mask
, text
);
550 /** Relay a masked notice that arrived from a server.
551 * @param[in] sptr Client that originated the message.
552 * @param[in] mask Target mask for the message.
553 * @param[in] text %Message to relay.
555 void server_relay_masked_notice(struct Client
* sptr
, const char* mask
, const char* text
)
557 const char* s
= mask
;
567 sendcmdto_match_butone(sptr
, CMD_NOTICE
, s
,
568 IsServer(cli_from(sptr
)) ? cli_from(sptr
) : 0,
569 host_mask
? MATCH_HOST
: MATCH_SERVER
,
570 "%s :%s", mask
, text
);