d49905070a80b79a0e0d65f0fcbb8eb835b96164
[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_reply.h"
34 #include "ircd_string.h"
35 #include "match.h"
36 #include "msg.h"
37 #include "numeric.h"
38 #include "numnicks.h"
39 #include "s_debug.h"
40 #include "s_misc.h"
41 #include "s_user.h"
42 #include "send.h"
43
44 #include <assert.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48
49 /*
50  * This file contains message relaying functions for client and server
51  * private messages and notices
52  * TODO: This file contains a lot of cut and paste code, and needs
53  * to be cleaned up a bit. The idea is to factor out the common checks
54  * but not introduce any IsOper/IsUser/MyUser/IsServer etc. stuff.
55  */
56 void relay_channel_message(struct Client* sptr, const char* name, const char* text)
57 {
58   struct Channel* chptr;
59   assert(0 != sptr);
60   assert(0 != name);
61   assert(0 != text);
62
63   if (0 == (chptr = FindChannel(name))) {
64     send_reply(sptr, ERR_NOSUCHCHANNEL, name);
65     return;
66   }
67   /*
68    * This first: Almost never a server/service
69    */
70   if (!client_can_send_to_channel(sptr, chptr)) {
71     send_reply(sptr, ERR_CANNOTSENDTOCHAN, chptr->chname);
72     return;
73   }
74   if ((chptr->mode.mode & MODE_NOPRIVMSGS) &&
75       check_target_limit(sptr, chptr, chptr->chname, 0))
76     return;
77
78   sendcmdto_channel_butone(sptr, CMD_PRIVATE, chptr, cli_from(sptr),
79                            SKIP_DEAF | SKIP_BURST, "%H :%s", chptr, text);
80 }
81
82 void relay_channel_notice(struct Client* sptr, const char* name, const char* text)
83 {
84   struct Channel* chptr;
85   assert(0 != sptr);
86   assert(0 != name);
87   assert(0 != text);
88
89   if (0 == (chptr = FindChannel(name)))
90     return;
91   /*
92    * This first: Almost never a server/service
93    */
94   if (!client_can_send_to_channel(sptr, chptr))
95     return;
96
97   if ((chptr->mode.mode & MODE_NOPRIVMSGS) &&
98       check_target_limit(sptr, chptr, chptr->chname, 0))
99     return;  
100
101   sendcmdto_channel_butone(sptr, CMD_NOTICE, chptr, cli_from(sptr),
102                            SKIP_DEAF | SKIP_BURST, "%H :%s", chptr, text);
103 }
104
105 void server_relay_channel_message(struct Client* sptr, const char* name, const char* text)
106 {
107   struct Channel* chptr;
108   assert(0 != sptr);
109   assert(0 != name);
110   assert(0 != text);
111
112   if (0 == (chptr = FindChannel(name))) {
113     /*
114      * XXX - do we need to send this back from a remote server?
115      */
116     send_reply(sptr, ERR_NOSUCHCHANNEL, name);
117     return;
118   }
119   /*
120    * This first: Almost never a server/service
121    * Servers may have channel services, need to check for it here
122    */
123   if (client_can_send_to_channel(sptr, chptr) || IsChannelService(sptr)) {
124     sendcmdto_channel_butone(sptr, CMD_PRIVATE, chptr, cli_from(sptr),
125                              SKIP_DEAF | SKIP_BURST, "%H :%s", chptr, text);
126   }
127   else
128     send_reply(sptr, ERR_CANNOTSENDTOCHAN, chptr->chname);
129 }
130
131 void server_relay_channel_notice(struct Client* sptr, const char* name, const char* text)
132 {
133   struct Channel* chptr;
134   assert(0 != sptr);
135   assert(0 != name);
136   assert(0 != text);
137
138   if (0 == (chptr = FindChannel(name)))
139     return;
140   /*
141    * This first: Almost never a server/service
142    * Servers may have channel services, need to check for it here
143    */
144   if (client_can_send_to_channel(sptr, chptr) || IsChannelService(sptr)) {
145     sendcmdto_channel_butone(sptr, CMD_NOTICE, chptr, cli_from(sptr),
146                              SKIP_DEAF | SKIP_BURST, "%H :%s", chptr, text);
147   }
148 }
149
150
151 void relay_directed_message(struct Client* sptr, char* name, char* server, const char* text)
152 {
153   struct Client* acptr;
154   char*          host;
155
156   assert(0 != sptr);
157   assert(0 != name);
158   assert(0 != text);
159   assert(0 != server);
160
161   if ((acptr = FindServer(server + 1)) == NULL ||
162       !IsChannelService(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 " NETWORK ". "
317                "Failed to deliver: [%.20s]", text);
318     return;
319   }
320   if (is_silenced(sptr, acptr))
321     return;
322
323   if (MyUser(acptr))
324     add_target(acptr, sptr);
325
326   sendcmdto_one(sptr, CMD_PRIVATE, acptr, "%C :%s", acptr, text);
327 }
328
329
330 void server_relay_private_notice(struct Client* sptr, const char* name, const char* text)
331 {
332   struct Client* acptr;
333   assert(0 != sptr);
334   assert(0 != name);
335   assert(0 != text);
336   /*
337    * nickname addressed?
338    */
339   if (0 == (acptr = findNUser(name)) || !IsUser(acptr))
340     return;
341
342   if (is_silenced(sptr, acptr))
343     return;
344
345   if (MyUser(acptr))
346     add_target(acptr, sptr);
347
348   sendcmdto_one(sptr, CMD_NOTICE, acptr, "%C :%s", acptr, text);
349 }
350
351 void relay_masked_message(struct Client* sptr, const char* mask, const char* text)
352 {
353   const char* s;
354   int   host_mask = 0;
355
356   assert(0 != sptr);
357   assert(0 != mask);
358   assert(0 != text);
359   /*
360    * look for the last '.' in mask and scan forward
361    */
362   if (0 == (s = strrchr(mask, '.'))) {
363     send_reply(sptr, ERR_NOTOPLEVEL, mask);
364     return;
365   }
366   while (*++s) {
367     if (*s == '.' || *s == '*' || *s == '?')
368        break;
369   }
370   if (*s == '*' || *s == '?') {
371     send_reply(sptr, ERR_WILDTOPLEVEL, mask);
372     return;
373   }
374   s = mask;
375   if ('@' == *++s) {
376     host_mask = 1;
377     ++s;
378   }
379
380   sendcmdto_match_butone(sptr, CMD_PRIVATE, s,
381                          IsServer(cli_from(sptr)) ? cli_from(sptr) : 0,
382                          host_mask ? MATCH_HOST : MATCH_SERVER,
383                          "%s :%s", mask, text);
384 }
385
386 void relay_masked_notice(struct Client* sptr, const char* mask, const char* text)
387 {
388   const char* s;
389   int   host_mask = 0;
390
391   assert(0 != sptr);
392   assert(0 != mask);
393   assert(0 != text);
394   /*
395    * look for the last '.' in mask and scan forward
396    */
397   if (0 == (s = strrchr(mask, '.'))) {
398     send_reply(sptr, ERR_NOTOPLEVEL, mask);
399     return;
400   }
401   while (*++s) {
402     if (*s == '.' || *s == '*' || *s == '?')
403        break;
404   }
405   if (*s == '*' || *s == '?') {
406     send_reply(sptr, ERR_WILDTOPLEVEL, mask);
407     return;
408   }
409   s = mask;
410   if ('@' == *++s) {
411     host_mask = 1;
412     ++s;
413   }
414
415   sendcmdto_match_butone(sptr, CMD_NOTICE, s,
416                          IsServer(cli_from(sptr)) ? cli_from(sptr) : 0,
417                          host_mask ? MATCH_HOST : MATCH_SERVER,
418                          "%s :%s", mask, text);
419 }
420
421 void server_relay_masked_message(struct Client* sptr, const char* mask, const char* text)
422 {
423   const char* s = mask;
424   int         host_mask = 0;
425   assert(0 != sptr);
426   assert(0 != mask);
427   assert(0 != text);
428
429   if ('@' == *++s) {
430     host_mask = 1;
431     ++s;
432   }
433   sendcmdto_match_butone(sptr, CMD_PRIVATE, s,
434                          IsServer(cli_from(sptr)) ? cli_from(sptr) : 0,
435                          host_mask ? MATCH_HOST : MATCH_SERVER,
436                          "%s :%s", mask, text);
437 }
438
439 void server_relay_masked_notice(struct Client* sptr, const char* mask, const char* text)
440 {
441   const char* s = mask;
442   int         host_mask = 0;
443   assert(0 != sptr);
444   assert(0 != mask);
445   assert(0 != text);
446
447   if ('@' == *++s) {
448     host_mask = 1;
449     ++s;
450   }
451   sendcmdto_match_butone(sptr, CMD_NOTICE, s,
452                          IsServer(cli_from(sptr)) ? cli_from(sptr) : 0,
453                          host_mask ? MATCH_HOST : MATCH_SERVER,
454                          "%s :%s", mask, text);
455 }
456