0cb7e80dded0bb8029717f275e92449520f30250
[ircu2.10.12-pk.git] / ircd / ircd_relay.c
1 /*
2  * IRC - Internet Relay Chat, ircd/ircd_relay.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Computing Center
5  *
6  * See file AUTHORS in IRC package for additional names of
7  * the programmers.
8  *
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)
12  * any later version.
13  *
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.
18  *
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.
22  */
23 /** @file
24  * @brief Helper functions to relay various types of messages.
25  * @version $Id$
26  *
27  * There are four basic types of messages, each with four subtypes.
28  *
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
35  * NICK.
36  *
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.
42  *
43  * As a special note, directed messages do not have server subtypes,
44  * since there is no difference in handling them based on origin.
45  */
46 #include "config.h"
47
48 #include "ircd_relay.h"
49 #include "channel.h"
50 #include "client.h"
51 #include "hash.h"
52 #include "ircd.h"
53 #include "ircd_chattr.h"
54 #include "ircd_features.h"
55 #include "ircd_log.h"
56 #include "ircd_reply.h"
57 #include "ircd_string.h"
58 #include "match.h"
59 #include "msg.h"
60 #include "numeric.h"
61 #include "numnicks.h"
62 #include "s_debug.h"
63 #include "s_misc.h"
64 #include "s_user.h"
65 #include "send.h"
66
67 /* #include <assert.h> -- Now using assert in ircd_log.h */
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <string.h>
71
72 /*
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.
78  */
79
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  */
86 void relay_channel_message(struct Client* sptr, const char* name, const char* text)
87 {
88   struct Channel* chptr;
89   assert(0 != sptr);
90   assert(0 != name);
91   assert(0 != text);
92
93   if (0 == (chptr = FindChannel(name))) {
94     send_reply(sptr, ERR_NOSUCHCHANNEL, name);
95     return;
96   }
97   /*
98    * This first: Almost never a server/service
99    */
100   if (!client_can_send_to_channel(sptr, chptr, 1)) {
101     send_reply(sptr, ERR_CANNOTSENDTOCHAN, chptr->chname);
102     return;
103   }
104   if ((chptr->mode.mode & MODE_NOPRIVMSGS) &&
105       check_target_limit(sptr, chptr, chptr->chname, 0))
106     return;
107
108   sendcmdto_channel_butone(sptr, CMD_PRIVATE, chptr, cli_from(sptr),
109                            SKIP_DEAF | SKIP_BURST, "%H :%s", chptr, text);
110 }
111
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.
117  */
118 void relay_channel_notice(struct Client* sptr, const char* name, const char* text)
119 {
120   struct Channel* chptr;
121   assert(0 != sptr);
122   assert(0 != name);
123   assert(0 != text);
124
125   if (0 == (chptr = FindChannel(name)))
126     return;
127   /*
128    * This first: Almost never a server/service
129    */
130   if (!client_can_send_to_channel(sptr, chptr, 1))
131     return;
132
133   if ((chptr->mode.mode & MODE_NOPRIVMSGS) &&
134       check_target_limit(sptr, chptr, chptr->chname, 0))
135     return;
136
137   sendcmdto_channel_butone(sptr, CMD_NOTICE, chptr, cli_from(sptr),
138                            SKIP_DEAF | SKIP_BURST, "%H :%s", chptr, text);
139 }
140
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.
147  */
148 void server_relay_channel_message(struct Client* sptr, const char* name, const char* text)
149 {
150   struct Channel* chptr;
151   assert(0 != sptr);
152   assert(0 != name);
153   assert(0 != text);
154
155   if (IsLocalChannel(name) || 0 == (chptr = FindChannel(name))) {
156     send_reply(sptr, ERR_NOSUCHCHANNEL, name);
157     return;
158   }
159   /*
160    * This first: Almost never a server/service
161    * Servers may have channel services, need to check for it here
162    */
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);
166   }
167   else
168     send_reply(sptr, ERR_CANNOTSENDTOCHAN, chptr->chname);
169 }
170
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.
177  */
178 void server_relay_channel_notice(struct Client* sptr, const char* name, const char* text)
179 {
180   struct Channel* chptr;
181   assert(0 != sptr);
182   assert(0 != name);
183   assert(0 != text);
184
185   if (IsLocalChannel(name) || 0 == (chptr = FindChannel(name)))
186     return;
187   /*
188    * This first: Almost never a server/service
189    * Servers may have channel services, need to check for it here
190    */
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);
194   }
195 }
196
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.
205  */
206 void relay_directed_message(struct Client* sptr, char* name, char* server, const char* text)
207 {
208   struct Client* acptr;
209   char*          host;
210
211   assert(0 != sptr);
212   assert(0 != name);
213   assert(0 != text);
214   assert(0 != server);
215
216   if ((acptr = FindServer(server + 1)) == NULL || !IsService(acptr))
217   {
218     send_reply(sptr, ERR_NOSUCHNICK, name);
219     return;
220   }
221   /*
222    * NICK[%host]@server addressed? See if <server> is me first
223    */
224   if (!IsMe(acptr))
225   {
226     sendcmdto_one(sptr, CMD_PRIVATE, acptr, "%s :%s", name, text);
227     return;
228   }
229   /*
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
232    * user.
233    */
234   *server = '\0';
235   if ((host = strchr(name, '%')))
236     *host++ = '\0';
237
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
245    */
246   if (!(acptr = FindUser(name)) || !MyUser(acptr) ||
247       (!EmptyString(host) && 0 != match(host, cli_user(acptr)->host)) ||
248       !IsChannelService(acptr))
249   {
250     /*
251      * By this stage we might as well not bother because they will
252      * know that this server is currently linked because of the
253      * increased lag.
254      */
255     send_reply(sptr, ERR_NOSUCHNICK, name);
256     return;
257   }
258
259   *server = '@';
260   if (host)
261     *--host = '%';
262
263   if (!(is_silenced(sptr, acptr)))
264     sendcmdto_one(sptr, CMD_PRIVATE, acptr, "%s :%s", name, text);
265 }
266
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.
275  */
276 void relay_directed_notice(struct Client* sptr, char* name, char* server, const char* text)
277 {
278   struct Client* acptr;
279   char*          host;
280
281   assert(0 != sptr);
282   assert(0 != name);
283   assert(0 != text);
284   assert(0 != server);
285
286   if (0 == (acptr = FindServer(server + 1)))
287     return;
288   /*
289    * NICK[%host]@server addressed? See if <server> is me first
290    */
291   if (!IsMe(acptr)) {
292     sendcmdto_one(sptr, CMD_NOTICE, acptr, "%s :%s", name, text);
293     return;
294   }
295   /*
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
298    * user.
299    */
300   *server = '\0';
301   if ((host = strchr(name, '%')))
302     *host++ = '\0';
303
304   if (!(acptr = FindUser(name)) || !MyUser(acptr) ||
305       (!EmptyString(host) && 0 != match(host, cli_user(acptr)->host)))
306     return;
307
308   *server = '@';
309   if (host)
310     *--host = '%';
311
312   if (!(is_silenced(sptr, acptr)))
313     sendcmdto_one(sptr, CMD_NOTICE, acptr, "%s :%s", name, text);
314 }
315
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.
323  */
324 void relay_private_message(struct Client* sptr, const char* name, const char* text)
325 {
326   struct Client* acptr;
327
328   assert(0 != sptr);
329   assert(0 != name);
330   assert(0 != text);
331
332   if (0 == (acptr = FindUser(name))) {
333     send_reply(sptr, ERR_NOSUCHNICK, name);
334     return;
335   }
336   if ((!IsChannelService(acptr) &&
337        check_target_limit(sptr, acptr, cli_name(acptr), 0)) ||
338       is_silenced(sptr, acptr))
339     return;
340
341   /*
342    * send away message if user away
343    */
344   if (cli_user(acptr) && cli_user(acptr)->away)
345     send_reply(sptr, RPL_AWAY, cli_name(acptr), cli_user(acptr)->away);
346   /*
347    * deliver the message
348    */
349   if (MyUser(acptr))
350     add_target(acptr, sptr);
351
352   sendcmdto_one(sptr, CMD_PRIVATE, acptr, "%C :%s", acptr, text);
353 }
354
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.
362  */
363 void relay_private_notice(struct Client* sptr, const char* name, const char* text)
364 {
365   struct Client* acptr;
366   assert(0 != sptr);
367   assert(0 != name);
368   assert(0 != text);
369
370   if (0 == (acptr = FindUser(name)))
371     return;
372   if ((!IsChannelService(acptr) && 
373        check_target_limit(sptr, acptr, cli_name(acptr), 0)) ||
374       is_silenced(sptr, acptr))
375     return;
376   /*
377    * deliver the message
378    */
379   if (MyUser(acptr))
380     add_target(acptr, sptr);
381
382   sendcmdto_one(sptr, CMD_NOTICE, acptr, "%C :%s", acptr, text);
383 }
384
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.
390  */
391 void server_relay_private_message(struct Client* sptr, const char* name, const char* text)
392 {
393   struct Client* acptr;
394   assert(0 != sptr);
395   assert(0 != name);
396   assert(0 != text);
397   /*
398    * nickname addressed?
399    */
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),
403                text);
404     return;
405   }
406   if (is_silenced(sptr, acptr))
407     return;
408
409   if (MyUser(acptr))
410     add_target(acptr, sptr);
411
412   sendcmdto_one(sptr, CMD_PRIVATE, acptr, "%C :%s", acptr, text);
413 }
414
415
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.
421  */
422 void server_relay_private_notice(struct Client* sptr, const char* name, const char* text)
423 {
424   struct Client* acptr;
425   assert(0 != sptr);
426   assert(0 != name);
427   assert(0 != text);
428   /*
429    * nickname addressed?
430    */
431   if (0 == (acptr = findNUser(name)) || !IsUser(acptr))
432     return;
433
434   if (is_silenced(sptr, acptr))
435     return;
436
437   if (MyUser(acptr))
438     add_target(acptr, sptr);
439
440   sendcmdto_one(sptr, CMD_NOTICE, acptr, "%C :%s", acptr, text);
441 }
442
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.
449  */
450 void relay_masked_message(struct Client* sptr, const char* mask, const char* text)
451 {
452   const char* s;
453   int   host_mask = 0;
454
455   assert(0 != sptr);
456   assert(0 != mask);
457   assert(0 != text);
458   /*
459    * look for the last '.' in mask and scan forward
460    */
461   if (0 == (s = strrchr(mask, '.'))) {
462     send_reply(sptr, ERR_NOTOPLEVEL, mask);
463     return;
464   }
465   while (*++s) {
466     if (*s == '.' || *s == '*' || *s == '?')
467        break;
468   }
469   if (*s == '*' || *s == '?') {
470     send_reply(sptr, ERR_WILDTOPLEVEL, mask);
471     return;
472   }
473   s = mask;
474   if ('@' == *++s) {
475     host_mask = 1;
476     ++s;
477   }
478
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);
483 }
484
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.
491  */
492 void relay_masked_notice(struct Client* sptr, const char* mask, const char* text)
493 {
494   const char* s;
495   int   host_mask = 0;
496
497   assert(0 != sptr);
498   assert(0 != mask);
499   assert(0 != text);
500   /*
501    * look for the last '.' in mask and scan forward
502    */
503   if (0 == (s = strrchr(mask, '.'))) {
504     send_reply(sptr, ERR_NOTOPLEVEL, mask);
505     return;
506   }
507   while (*++s) {
508     if (*s == '.' || *s == '*' || *s == '?')
509        break;
510   }
511   if (*s == '*' || *s == '?') {
512     send_reply(sptr, ERR_WILDTOPLEVEL, mask);
513     return;
514   }
515   s = mask;
516   if ('@' == *++s) {
517     host_mask = 1;
518     ++s;
519   }
520
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);
525 }
526
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.
531  */
532 void server_relay_masked_message(struct Client* sptr, const char* mask, const char* text)
533 {
534   const char* s = mask;
535   int         host_mask = 0;
536   assert(0 != sptr);
537   assert(0 != mask);
538   assert(0 != text);
539
540   if ('@' == *++s) {
541     host_mask = 1;
542     ++s;
543   }
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);
548 }
549
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.
554  */
555 void server_relay_masked_notice(struct Client* sptr, const char* mask, const char* text)
556 {
557   const char* s = mask;
558   int         host_mask = 0;
559   assert(0 != sptr);
560   assert(0 != mask);
561   assert(0 != text);
562
563   if ('@' == *++s) {
564     host_mask = 1;
565     ++s;
566   }
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);
571 }
572