2004-05-09 Michael Poole <mdpoole@troilus.org>
[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  * $Id$
24  */
25 #include "config.h"
26
27 #include "ircd_relay.h"
28 #include "channel.h"
29 #include "client.h"
30 #include "hash.h"
31 #include "ircd.h"
32 #include "ircd_chattr.h"
33 #include "ircd_features.h"
34 #include "ircd_reply.h"
35 #include "ircd_string.h"
36 #include "match.h"
37 #include "msg.h"
38 #include "numeric.h"
39 #include "numnicks.h"
40 #include "s_debug.h"
41 #include "s_misc.h"
42 #include "s_user.h"
43 #include "send.h"
44
45 #include <assert.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49
50 /*
51  * This file contains message relaying functions for client and server
52  * private messages and notices
53  * TODO: This file contains a lot of cut and paste code, and needs
54  * to be cleaned up a bit. The idea is to factor out the common checks
55  * but not introduce any IsOper/IsUser/MyUser/IsServer etc. stuff.
56  */
57 void relay_channel_message(struct Client* sptr, const char* name, const char* text)
58 {
59   struct Channel* chptr;
60   assert(0 != sptr);
61   assert(0 != name);
62   assert(0 != text);
63
64   if (0 == (chptr = FindChannel(name))) {
65     send_reply(sptr, ERR_NOSUCHCHANNEL, name);
66     return;
67   }
68   /*
69    * This first: Almost never a server/service
70    */
71   if (!client_can_send_to_channel(sptr, chptr)) {
72     send_reply(sptr, ERR_CANNOTSENDTOCHAN, chptr->chname);
73     return;
74   }
75   if ((chptr->mode.mode & MODE_NOPRIVMSGS) &&
76       check_target_limit(sptr, chptr, chptr->chname, 0))
77     return;
78
79   sendcmdto_channel_butone(sptr, CMD_PRIVATE, chptr, cli_from(sptr),
80                            SKIP_DEAF | SKIP_BURST, "%H :%s", chptr, text);
81 }
82
83 void relay_channel_notice(struct Client* sptr, const char* name, const char* text)
84 {
85   struct Channel* chptr;
86   assert(0 != sptr);
87   assert(0 != name);
88   assert(0 != text);
89
90   if (0 == (chptr = FindChannel(name)))
91     return;
92   /*
93    * This first: Almost never a server/service
94    */
95   if (!client_can_send_to_channel(sptr, chptr))
96     return;
97
98   if ((chptr->mode.mode & MODE_NOPRIVMSGS) &&
99       check_target_limit(sptr, chptr, chptr->chname, 0))
100     return;  
101
102   sendcmdto_channel_butone(sptr, CMD_NOTICE, chptr, cli_from(sptr),
103                            SKIP_DEAF | SKIP_BURST, "%H :%s", chptr, text);
104 }
105
106 void server_relay_channel_message(struct Client* sptr, const char* name, const char* text)
107 {
108   struct Channel* chptr;
109   assert(0 != sptr);
110   assert(0 != name);
111   assert(0 != text);
112
113   if (0 == (chptr = FindChannel(name))) {
114     /*
115      * XXX - do we need to send this back from a remote server?
116      */
117     send_reply(sptr, ERR_NOSUCHCHANNEL, name);
118     return;
119   }
120   /*
121    * This first: Almost never a server/service
122    * Servers may have channel services, need to check for it here
123    */
124   if (client_can_send_to_channel(sptr, chptr) || IsChannelService(sptr)) {
125     sendcmdto_channel_butone(sptr, CMD_PRIVATE, chptr, cli_from(sptr),
126                              SKIP_DEAF | SKIP_BURST, "%H :%s", chptr, text);
127   }
128   else
129     send_reply(sptr, ERR_CANNOTSENDTOCHAN, chptr->chname);
130 }
131
132 void server_relay_channel_notice(struct Client* sptr, const char* name, const char* text)
133 {
134   struct Channel* chptr;
135   assert(0 != sptr);
136   assert(0 != name);
137   assert(0 != text);
138
139   if (0 == (chptr = FindChannel(name)))
140     return;
141   /*
142    * This first: Almost never a server/service
143    * Servers may have channel services, need to check for it here
144    */
145   if (client_can_send_to_channel(sptr, chptr) || IsChannelService(sptr)) {
146     sendcmdto_channel_butone(sptr, CMD_NOTICE, chptr, cli_from(sptr),
147                              SKIP_DEAF | SKIP_BURST, "%H :%s", chptr, text);
148   }
149 }
150
151
152 void relay_directed_message(struct Client* sptr, char* name, char* server, const char* text)
153 {
154   struct Client* acptr;
155   char*          host;
156
157   assert(0 != sptr);
158   assert(0 != name);
159   assert(0 != text);
160   assert(0 != server);
161
162   if ((acptr = FindServer(server + 1)) == NULL || !IsService(acptr))
163   {
164     send_reply(sptr, ERR_NOSUCHNICK, name);
165     return;
166   }
167   /*
168    * NICK[%host]@server addressed? See if <server> is me first
169    */
170   if (!IsMe(acptr))
171   {
172     sendcmdto_one(sptr, CMD_PRIVATE, acptr, "%s :%s", name, text);
173     return;
174   }
175   /*
176    * Look for an user whose NICK is equal to <name> and then
177    * check if it's hostname matches <host> and if it's a local
178    * user.
179    */
180   *server = '\0';
181   if ((host = strchr(name, '%')))
182     *host++ = '\0';
183
184   /* As reported by Vampire-, it's possible to brute force finding users
185    * by sending a message to each server and see which one succeeded.
186    * This means we have to remove error reporting.  Sigh.  Better than
187    * removing the ability to send directed messages to client servers 
188    * Thanks for the suggestion Vampire=.  -- Isomer 2001-08-28
189    * Argh, /ping nick@server, disallow messages to non +k clients :/  I hate
190    * this. -- Isomer 2001-09-16
191    */
192   if (!(acptr = FindUser(name)) || !MyUser(acptr) ||
193       (!EmptyString(host) && 0 != match(host, cli_user(acptr)->host)) ||
194       !IsChannelService(acptr))
195   {
196     /*
197      * By this stage we might as well not bother because they will
198      * know that this server is currently linked because of the
199      * increased lag.
200      */
201     send_reply(sptr, ERR_NOSUCHNICK, name);
202     return;
203   }
204
205   *server = '@';
206   if (host)
207     *--host = '%';
208
209   if (!(is_silenced(sptr, acptr)))
210     sendcmdto_one(sptr, CMD_PRIVATE, acptr, "%s :%s", name, text);
211 }
212
213 void relay_directed_notice(struct Client* sptr, char* name, char* server, const char* text)
214 {
215   struct Client* acptr;
216   char*          host;
217
218   assert(0 != sptr);
219   assert(0 != name);
220   assert(0 != text);
221   assert(0 != server);
222
223   if (0 == (acptr = FindServer(server + 1)))
224     return;
225   /*
226    * NICK[%host]@server addressed? See if <server> is me first
227    */
228   if (!IsMe(acptr)) {
229     sendcmdto_one(sptr, CMD_NOTICE, acptr, "%s :%s", name, text);
230     return;
231   }
232   /*
233    * Look for an user whose NICK is equal to <name> and then
234    * check if it's hostname matches <host> and if it's a local
235    * user.
236    */
237   *server = '\0';
238   if ((host = strchr(name, '%')))
239     *host++ = '\0';
240
241   if (!(acptr = FindUser(name)) || !MyUser(acptr) ||
242       (!EmptyString(host) && 0 != match(host, cli_user(acptr)->host)))
243     return;
244
245   *server = '@';
246   if (host)
247     *--host = '%';
248
249   if (!(is_silenced(sptr, acptr)))
250     sendcmdto_one(sptr, CMD_NOTICE, acptr, "%s :%s", name, text);
251 }
252
253 void relay_private_message(struct Client* sptr, const char* name, const char* text)
254 {
255   struct Client* acptr;
256
257   assert(0 != sptr);
258   assert(0 != name);
259   assert(0 != text);
260
261   if (0 == (acptr = FindUser(name))) {
262     send_reply(sptr, ERR_NOSUCHNICK, name);
263     return;
264   }
265   if ((!IsChannelService(acptr) &&
266        check_target_limit(sptr, acptr, cli_name(acptr), 0)) ||
267       is_silenced(sptr, acptr))
268     return;
269
270   /*
271    * send away message if user away
272    */
273   if (cli_user(acptr) && cli_user(acptr)->away)
274     send_reply(sptr, RPL_AWAY, cli_name(acptr), cli_user(acptr)->away);
275   /*
276    * deliver the message
277    */
278   if (MyUser(acptr))
279     add_target(acptr, sptr);
280
281   sendcmdto_one(sptr, CMD_PRIVATE, acptr, "%C :%s", acptr, text);
282 }
283
284 void relay_private_notice(struct Client* sptr, const char* name, const char* text)
285 {
286   struct Client* acptr;
287   assert(0 != sptr);
288   assert(0 != name);
289   assert(0 != text);
290
291   if (0 == (acptr = FindUser(name)))
292     return;
293   if ((!IsChannelService(acptr) && 
294        check_target_limit(sptr, acptr, cli_name(acptr), 0)) ||
295       is_silenced(sptr, acptr))
296     return;
297   /*
298    * deliver the message
299    */
300   if (MyUser(acptr))
301     add_target(acptr, sptr);
302
303   sendcmdto_one(sptr, CMD_NOTICE, acptr, "%C :%s", acptr, text);
304 }
305
306 void server_relay_private_message(struct Client* sptr, const char* name, const char* text)
307 {
308   struct Client* acptr;
309   assert(0 != sptr);
310   assert(0 != name);
311   assert(0 != text);
312   /*
313    * nickname addressed?
314    */
315   if (0 == (acptr = findNUser(name)) || !IsUser(acptr)) {
316     send_reply(sptr, SND_EXPLICIT | ERR_NOSUCHNICK, "* :Target left %s. "
317                "Failed to deliver: [%.20s]", feature_str(FEAT_NETWORK),
318                text);
319     return;
320   }
321   if (is_silenced(sptr, acptr))
322     return;
323
324   if (MyUser(acptr))
325     add_target(acptr, sptr);
326
327   sendcmdto_one(sptr, CMD_PRIVATE, acptr, "%C :%s", acptr, text);
328 }
329
330
331 void server_relay_private_notice(struct Client* sptr, const char* name, const char* text)
332 {
333   struct Client* acptr;
334   assert(0 != sptr);
335   assert(0 != name);
336   assert(0 != text);
337   /*
338    * nickname addressed?
339    */
340   if (0 == (acptr = findNUser(name)) || !IsUser(acptr))
341     return;
342
343   if (is_silenced(sptr, acptr))
344     return;
345
346   if (MyUser(acptr))
347     add_target(acptr, sptr);
348
349   sendcmdto_one(sptr, CMD_NOTICE, acptr, "%C :%s", acptr, text);
350 }
351
352 void relay_masked_message(struct Client* sptr, const char* mask, const char* text)
353 {
354   const char* s;
355   int   host_mask = 0;
356
357   assert(0 != sptr);
358   assert(0 != mask);
359   assert(0 != text);
360   /*
361    * look for the last '.' in mask and scan forward
362    */
363   if (0 == (s = strrchr(mask, '.'))) {
364     send_reply(sptr, ERR_NOTOPLEVEL, mask);
365     return;
366   }
367   while (*++s) {
368     if (*s == '.' || *s == '*' || *s == '?')
369        break;
370   }
371   if (*s == '*' || *s == '?') {
372     send_reply(sptr, ERR_WILDTOPLEVEL, mask);
373     return;
374   }
375   s = mask;
376   if ('@' == *++s) {
377     host_mask = 1;
378     ++s;
379   }
380
381   sendcmdto_match_butone(sptr, CMD_PRIVATE, s,
382                          IsServer(cli_from(sptr)) ? cli_from(sptr) : 0,
383                          host_mask ? MATCH_HOST : MATCH_SERVER,
384                          "%s :%s", mask, text);
385 }
386
387 void relay_masked_notice(struct Client* sptr, const char* mask, const char* text)
388 {
389   const char* s;
390   int   host_mask = 0;
391
392   assert(0 != sptr);
393   assert(0 != mask);
394   assert(0 != text);
395   /*
396    * look for the last '.' in mask and scan forward
397    */
398   if (0 == (s = strrchr(mask, '.'))) {
399     send_reply(sptr, ERR_NOTOPLEVEL, mask);
400     return;
401   }
402   while (*++s) {
403     if (*s == '.' || *s == '*' || *s == '?')
404        break;
405   }
406   if (*s == '*' || *s == '?') {
407     send_reply(sptr, ERR_WILDTOPLEVEL, mask);
408     return;
409   }
410   s = mask;
411   if ('@' == *++s) {
412     host_mask = 1;
413     ++s;
414   }
415
416   sendcmdto_match_butone(sptr, CMD_NOTICE, s,
417                          IsServer(cli_from(sptr)) ? cli_from(sptr) : 0,
418                          host_mask ? MATCH_HOST : MATCH_SERVER,
419                          "%s :%s", mask, text);
420 }
421
422 void server_relay_masked_message(struct Client* sptr, const char* mask, const char* text)
423 {
424   const char* s = mask;
425   int         host_mask = 0;
426   assert(0 != sptr);
427   assert(0 != mask);
428   assert(0 != text);
429
430   if ('@' == *++s) {
431     host_mask = 1;
432     ++s;
433   }
434   sendcmdto_match_butone(sptr, CMD_PRIVATE, s,
435                          IsServer(cli_from(sptr)) ? cli_from(sptr) : 0,
436                          host_mask ? MATCH_HOST : MATCH_SERVER,
437                          "%s :%s", mask, text);
438 }
439
440 void server_relay_masked_notice(struct Client* sptr, const char* mask, const char* text)
441 {
442   const char* s = mask;
443   int         host_mask = 0;
444   assert(0 != sptr);
445   assert(0 != mask);
446   assert(0 != text);
447
448   if ('@' == *++s) {
449     host_mask = 1;
450     ++s;
451   }
452   sendcmdto_match_butone(sptr, CMD_NOTICE, s,
453                          IsServer(cli_from(sptr)) ? cli_from(sptr) : 0,
454                          host_mask ? MATCH_HOST : MATCH_SERVER,
455                          "%s :%s", mask, text);
456 }
457