Author: Kev <klmitch@mit.edu>
[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   if (!(acptr = FindUser(name)) || !MyUser(acptr) ||
182       (!EmptyString(host) && 0 != match(host, cli_user(acptr)->host))) {
183     send_reply(sptr, ERR_NOSUCHNICK, name);
184     return;
185   }
186
187   *server = '@';
188   if (host)
189     *--host = '%';
190
191   if (!(is_silenced(sptr, acptr)))
192     sendcmdto_one(sptr, CMD_PRIVATE, acptr, "%s :%s", name, text);
193 }
194
195 void relay_directed_notice(struct Client* sptr, char* name, char* server, const char* text)
196 {
197   struct Client* acptr;
198   char*          host;
199
200   assert(0 != sptr);
201   assert(0 != name);
202   assert(0 != text);
203   assert(0 != server);
204
205   if (0 == (acptr = FindServer(server + 1)))
206     return;
207   /*
208    * NICK[%host]@server addressed? See if <server> is me first
209    */
210   if (!IsMe(acptr)) {
211     sendcmdto_one(sptr, CMD_NOTICE, acptr, "%s :%s", name, text);
212     return;
213   }
214   /*
215    * Look for an user whose NICK is equal to <name> and then
216    * check if it's hostname matches <host> and if it's a local
217    * user.
218    */
219   *server = '\0';
220   if ((host = strchr(name, '%')))
221     *host++ = '\0';
222
223   if (!(acptr = FindUser(name)) || !MyUser(acptr) ||
224       (!EmptyString(host) && 0 != match(host, cli_user(acptr)->host)))
225     return;
226
227   *server = '@';
228   if (host)
229     *--host = '%';
230
231   if (!(is_silenced(sptr, acptr)))
232     sendcmdto_one(sptr, CMD_NOTICE, acptr, "%s :%s", name, text);
233 }
234
235 void relay_private_message(struct Client* sptr, const char* name, const char* text)
236 {
237   struct Client* acptr;
238
239   assert(0 != sptr);
240   assert(0 != name);
241   assert(0 != text);
242
243   if (0 == (acptr = FindUser(name))) {
244     send_reply(sptr, ERR_NOSUCHNICK, name);
245     return;
246   }
247   if ((!IsChannelService(acptr) &&
248        check_target_limit(sptr, acptr, cli_name(acptr), 0)) ||
249       is_silenced(sptr, acptr))
250     return;
251
252   /*
253    * send away message if user away
254    */
255   if (cli_user(acptr) && cli_user(acptr)->away)
256     send_reply(sptr, RPL_AWAY, cli_name(acptr), cli_user(acptr)->away);
257   /*
258    * deliver the message
259    */
260   if (MyUser(acptr))
261     add_target(acptr, sptr);
262
263   sendcmdto_one(sptr, CMD_PRIVATE, acptr, "%C :%s", acptr, text);
264 }
265
266 void relay_private_notice(struct Client* sptr, const char* name, const char* text)
267 {
268   struct Client* acptr;
269   assert(0 != sptr);
270   assert(0 != name);
271   assert(0 != text);
272
273   if (0 == (acptr = FindUser(name)))
274     return;
275   if ((!IsChannelService(acptr) && 
276        check_target_limit(sptr, acptr, cli_name(acptr), 0)) ||
277       is_silenced(sptr, acptr))
278     return;
279   /*
280    * deliver the message
281    */
282   if (MyUser(acptr))
283     add_target(acptr, sptr);
284
285   sendcmdto_one(sptr, CMD_NOTICE, acptr, "%C :%s", acptr, text);
286 }
287
288 void server_relay_private_message(struct Client* sptr, const char* name, const char* text)
289 {
290   struct Client* acptr;
291   assert(0 != sptr);
292   assert(0 != name);
293   assert(0 != text);
294   /*
295    * nickname addressed?
296    */
297   if (0 == (acptr = findNUser(name)) || !IsUser(acptr)) {
298     send_reply(sptr, SND_EXPLICIT | ERR_NOSUCHNICK, "* :Target left UnderNet. "
299                "Failed to deliver: [%.20s]", text);
300     return;
301   }
302   if (is_silenced(sptr, acptr))
303     return;
304
305   if (MyUser(acptr))
306     add_target(acptr, sptr);
307
308   sendcmdto_one(sptr, CMD_PRIVATE, acptr, "%C :%s", acptr, text);
309 }
310
311
312 void server_relay_private_notice(struct Client* sptr, const char* name, const char* text)
313 {
314   struct Client* acptr;
315   assert(0 != sptr);
316   assert(0 != name);
317   assert(0 != text);
318   /*
319    * nickname addressed?
320    */
321   if (0 == (acptr = findNUser(name)) || !IsUser(acptr))
322     return;
323
324   if (is_silenced(sptr, acptr))
325     return;
326
327   if (MyUser(acptr))
328     add_target(acptr, sptr);
329
330   sendcmdto_one(sptr, CMD_NOTICE, acptr, "%C :%s", acptr, text);
331 }
332
333 void relay_masked_message(struct Client* sptr, const char* mask, const char* text)
334 {
335   const char* s;
336   int   host_mask = 0;
337
338   assert(0 != sptr);
339   assert(0 != mask);
340   assert(0 != text);
341   /*
342    * look for the last '.' in mask and scan forward
343    */
344   if (0 == (s = strrchr(mask, '.'))) {
345     send_reply(sptr, ERR_NOTOPLEVEL, mask);
346     return;
347   }
348   while (*++s) {
349     if (*s == '.' || *s == '*' || *s == '?')
350        break;
351   }
352   if (*s == '*' || *s == '?') {
353     send_reply(sptr, ERR_WILDTOPLEVEL, mask);
354     return;
355   }
356   s = mask;
357   if ('@' == *++s) {
358     host_mask = 1;
359     ++s;
360   }
361
362   sendcmdto_match_butone(sptr, CMD_PRIVATE, s,
363                          IsServer(cli_from(sptr)) ? cli_from(sptr) : 0,
364                          host_mask ? MATCH_HOST : MATCH_SERVER,
365                          "%s :%s", mask, text);
366 }
367
368 void relay_masked_notice(struct Client* sptr, const char* mask, const char* text)
369 {
370   const char* s;
371   int   host_mask = 0;
372
373   assert(0 != sptr);
374   assert(0 != mask);
375   assert(0 != text);
376   /*
377    * look for the last '.' in mask and scan forward
378    */
379   if (0 == (s = strrchr(mask, '.'))) {
380     send_reply(sptr, ERR_NOTOPLEVEL, mask);
381     return;
382   }
383   while (*++s) {
384     if (*s == '.' || *s == '*' || *s == '?')
385        break;
386   }
387   if (*s == '*' || *s == '?') {
388     send_reply(sptr, ERR_WILDTOPLEVEL, mask);
389     return;
390   }
391   s = mask;
392   if ('@' == *++s) {
393     host_mask = 1;
394     ++s;
395   }
396
397   sendcmdto_match_butone(sptr, CMD_NOTICE, s,
398                          IsServer(cli_from(sptr)) ? cli_from(sptr) : 0,
399                          host_mask ? MATCH_HOST : MATCH_SERVER,
400                          "%s :%s", mask, text);
401 }
402
403 void server_relay_masked_message(struct Client* sptr, const char* mask, const char* text)
404 {
405   const char* s = mask;
406   int         host_mask = 0;
407   assert(0 != sptr);
408   assert(0 != mask);
409   assert(0 != text);
410
411   if ('@' == *++s) {
412     host_mask = 1;
413     ++s;
414   }
415   sendcmdto_match_butone(sptr, CMD_PRIVATE, 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_notice(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_NOTICE, s,
434                          IsServer(cli_from(sptr)) ? cli_from(sptr) : 0,
435                          host_mask ? MATCH_HOST : MATCH_SERVER,
436                          "%s :%s", mask, text);
437 }
438