Author: Isomer <isomer@coders.net>
[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 (0 == (acptr = FindServer(server + 1))) {
162     send_reply(sptr, ERR_NOSUCHNICK, name);
163     return;
164   }
165   /*
166    * NICK[%host]@server addressed? See if <server> is me first
167    */
168   if (!IsMe(acptr)) {
169     sendcmdto_one(sptr, CMD_PRIVATE, acptr, "%s :%s", name, text);
170     return;
171   }
172   /*
173    * Look for an user whose NICK is equal to <name> and then
174    * check if it's hostname matches <host> and if it's a local
175    * user.
176    */
177   *server = '\0';
178   if ((host = strchr(name, '%')))
179     *host++ = '\0';
180
181   /* As reported by Vampire-, it's possible to brute force finding users
182    * by sending a message to each server and see which one succeeded.
183    * This means we have to remove error reporting.  Sigh.  Better than
184    * removing the ability to send directed messages to client servers 
185    * Thanks for the suggestion Vampire=.  -- Isomer 2001-08-28
186    * Argh, /ping nick@server, disallow messages to non +k clients :/  I hate
187    * this. -- Isomer 2001-09-16
188    */
189   if (!(acptr = FindUser(name)) || !MyUser(acptr) ||
190       (!EmptyString(host) && 0 != match(host, cli_user(acptr)->host)) ||
191       !IsChannelService(acptr)) {
192 #if 0
193     send_reply(sptr, ERR_NOSUCHNICK, name);
194 #endif
195     return;
196   }
197
198   *server = '@';
199   if (host)
200     *--host = '%';
201
202   if (!(is_silenced(sptr, acptr)))
203     sendcmdto_one(sptr, CMD_PRIVATE, acptr, "%s :%s", name, text);
204 }
205
206 void relay_directed_notice(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 (0 == (acptr = FindServer(server + 1)))
217     return;
218   /*
219    * NICK[%host]@server addressed? See if <server> is me first
220    */
221   if (!IsMe(acptr)) {
222     sendcmdto_one(sptr, CMD_NOTICE, acptr, "%s :%s", name, text);
223     return;
224   }
225   /*
226    * Look for an user whose NICK is equal to <name> and then
227    * check if it's hostname matches <host> and if it's a local
228    * user.
229    */
230   *server = '\0';
231   if ((host = strchr(name, '%')))
232     *host++ = '\0';
233
234   if (!(acptr = FindUser(name)) || !MyUser(acptr) ||
235       (!EmptyString(host) && 0 != match(host, cli_user(acptr)->host)))
236     return;
237
238   *server = '@';
239   if (host)
240     *--host = '%';
241
242   if (!(is_silenced(sptr, acptr)))
243     sendcmdto_one(sptr, CMD_NOTICE, acptr, "%s :%s", name, text);
244 }
245
246 void relay_private_message(struct Client* sptr, const char* name, const char* text)
247 {
248   struct Client* acptr;
249
250   assert(0 != sptr);
251   assert(0 != name);
252   assert(0 != text);
253
254   if (0 == (acptr = FindUser(name))) {
255     send_reply(sptr, ERR_NOSUCHNICK, name);
256     return;
257   }
258   if ((!IsChannelService(acptr) &&
259        check_target_limit(sptr, acptr, cli_name(acptr), 0)) ||
260       is_silenced(sptr, acptr))
261     return;
262
263   /*
264    * send away message if user away
265    */
266   if (cli_user(acptr) && cli_user(acptr)->away)
267     send_reply(sptr, RPL_AWAY, cli_name(acptr), cli_user(acptr)->away);
268   /*
269    * deliver the message
270    */
271   if (MyUser(acptr))
272     add_target(acptr, sptr);
273
274   sendcmdto_one(sptr, CMD_PRIVATE, acptr, "%C :%s", acptr, text);
275 }
276
277 void relay_private_notice(struct Client* sptr, const char* name, const char* text)
278 {
279   struct Client* acptr;
280   assert(0 != sptr);
281   assert(0 != name);
282   assert(0 != text);
283
284   if (0 == (acptr = FindUser(name)))
285     return;
286   if ((!IsChannelService(acptr) && 
287        check_target_limit(sptr, acptr, cli_name(acptr), 0)) ||
288       is_silenced(sptr, acptr))
289     return;
290   /*
291    * deliver the message
292    */
293   if (MyUser(acptr))
294     add_target(acptr, sptr);
295
296   sendcmdto_one(sptr, CMD_NOTICE, acptr, "%C :%s", acptr, text);
297 }
298
299 void server_relay_private_message(struct Client* sptr, const char* name, const char* text)
300 {
301   struct Client* acptr;
302   assert(0 != sptr);
303   assert(0 != name);
304   assert(0 != text);
305   /*
306    * nickname addressed?
307    */
308   if (0 == (acptr = findNUser(name)) || !IsUser(acptr)) {
309     send_reply(sptr, SND_EXPLICIT | ERR_NOSUCHNICK, "* :Target left " NETWORK ". "
310                "Failed to deliver: [%.20s]", text);
311     return;
312   }
313   if (is_silenced(sptr, acptr))
314     return;
315
316   if (MyUser(acptr))
317     add_target(acptr, sptr);
318
319   sendcmdto_one(sptr, CMD_PRIVATE, acptr, "%C :%s", acptr, text);
320 }
321
322
323 void server_relay_private_notice(struct Client* sptr, const char* name, const char* text)
324 {
325   struct Client* acptr;
326   assert(0 != sptr);
327   assert(0 != name);
328   assert(0 != text);
329   /*
330    * nickname addressed?
331    */
332   if (0 == (acptr = findNUser(name)) || !IsUser(acptr))
333     return;
334
335   if (is_silenced(sptr, acptr))
336     return;
337
338   if (MyUser(acptr))
339     add_target(acptr, sptr);
340
341   sendcmdto_one(sptr, CMD_NOTICE, acptr, "%C :%s", acptr, text);
342 }
343
344 void relay_masked_message(struct Client* sptr, const char* mask, const char* text)
345 {
346   const char* s;
347   int   host_mask = 0;
348
349   assert(0 != sptr);
350   assert(0 != mask);
351   assert(0 != text);
352   /*
353    * look for the last '.' in mask and scan forward
354    */
355   if (0 == (s = strrchr(mask, '.'))) {
356     send_reply(sptr, ERR_NOTOPLEVEL, mask);
357     return;
358   }
359   while (*++s) {
360     if (*s == '.' || *s == '*' || *s == '?')
361        break;
362   }
363   if (*s == '*' || *s == '?') {
364     send_reply(sptr, ERR_WILDTOPLEVEL, mask);
365     return;
366   }
367   s = mask;
368   if ('@' == *++s) {
369     host_mask = 1;
370     ++s;
371   }
372
373   sendcmdto_match_butone(sptr, CMD_PRIVATE, s,
374                          IsServer(cli_from(sptr)) ? cli_from(sptr) : 0,
375                          host_mask ? MATCH_HOST : MATCH_SERVER,
376                          "%s :%s", mask, text);
377 }
378
379 void relay_masked_notice(struct Client* sptr, const char* mask, const char* text)
380 {
381   const char* s;
382   int   host_mask = 0;
383
384   assert(0 != sptr);
385   assert(0 != mask);
386   assert(0 != text);
387   /*
388    * look for the last '.' in mask and scan forward
389    */
390   if (0 == (s = strrchr(mask, '.'))) {
391     send_reply(sptr, ERR_NOTOPLEVEL, mask);
392     return;
393   }
394   while (*++s) {
395     if (*s == '.' || *s == '*' || *s == '?')
396        break;
397   }
398   if (*s == '*' || *s == '?') {
399     send_reply(sptr, ERR_WILDTOPLEVEL, mask);
400     return;
401   }
402   s = mask;
403   if ('@' == *++s) {
404     host_mask = 1;
405     ++s;
406   }
407
408   sendcmdto_match_butone(sptr, CMD_NOTICE, s,
409                          IsServer(cli_from(sptr)) ? cli_from(sptr) : 0,
410                          host_mask ? MATCH_HOST : MATCH_SERVER,
411                          "%s :%s", mask, text);
412 }
413
414 void server_relay_masked_message(struct Client* sptr, const char* mask, const char* text)
415 {
416   const char* s = mask;
417   int         host_mask = 0;
418   assert(0 != sptr);
419   assert(0 != mask);
420   assert(0 != text);
421
422   if ('@' == *++s) {
423     host_mask = 1;
424     ++s;
425   }
426   sendcmdto_match_butone(sptr, CMD_PRIVATE, s,
427                          IsServer(cli_from(sptr)) ? cli_from(sptr) : 0,
428                          host_mask ? MATCH_HOST : MATCH_SERVER,
429                          "%s :%s", mask, text);
430 }
431
432 void server_relay_masked_notice(struct Client* sptr, const char* mask, const char* text)
433 {
434   const char* s = mask;
435   int         host_mask = 0;
436   assert(0 != sptr);
437   assert(0 != mask);
438   assert(0 != text);
439
440   if ('@' == *++s) {
441     host_mask = 1;
442     ++s;
443   }
444   sendcmdto_match_butone(sptr, CMD_NOTICE, s,
445                          IsServer(cli_from(sptr)) ? cli_from(sptr) : 0,
446                          host_mask ? MATCH_HOST : MATCH_SERVER,
447                          "%s :%s", mask, text);
448 }
449