8610da35516cbda82ba5783d02e1002f71032552
[ircu2.10.12-pk.git] / ircd / m_notice.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_notice.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
26 /*
27  * m_functions execute protocol messages on this server:
28  *
29  *    cptr    is always NON-NULL, pointing to a *LOCAL* client
30  *            structure (with an open socket connected!). This
31  *            identifies the physical socket where the message
32  *            originated (or which caused the m_function to be
33  *            executed--some m_functions may call others...).
34  *
35  *    sptr    is the source of the message, defined by the
36  *            prefix part of the message if present. If not
37  *            or prefix not found, then sptr==cptr.
38  *
39  *            (!IsServer(cptr)) => (cptr == sptr), because
40  *            prefixes are taken *only* from servers...
41  *
42  *            (IsServer(cptr))
43  *                    (sptr == cptr) => the message didn't
44  *                    have the prefix.
45  *
46  *                    (sptr != cptr && IsServer(sptr) means
47  *                    the prefix specified servername. (?)
48  *
49  *                    (sptr != cptr && !IsServer(sptr) means
50  *                    that message originated from a remote
51  *                    user (not local).
52  *
53  *            combining
54  *
55  *            (!IsServer(sptr)) means that, sptr can safely
56  *            taken as defining the target structure of the
57  *            message in this server.
58  *
59  *    *Always* true (if 'parse' and others are working correct):
60  *
61  *    1)      sptr->from == cptr  (note: cptr->from == cptr)
62  *
63  *    2)      MyConnect(sptr) <=> sptr == cptr (e.g. sptr
64  *            *cannot* be a local connection, unless it's
65  *            actually cptr!). [MyConnect(x) should probably
66  *            be defined as (x == x->from) --msa ]
67  *
68  *    parc    number of variable parameter strings (if zero,
69  *            parv is allowed to be NULL)
70  *
71  *    parv    a NULL terminated list of parameter pointers,
72  *
73  *                    parv[0], sender (prefix string), if not present
74  *                            this points to an empty string.
75  *                    parv[1]...parv[parc-1]
76  *                            pointers to additional parameters
77  *                    parv[parc] == NULL, *always*
78  *
79  *            note:   it is guaranteed that parv[0]..parv[parc-1] are all
80  *                    non-NULL pointers.
81  */
82 #include "config.h"
83
84 #if 0
85 /*
86  * No need to include handlers.h here the signatures must match
87  * and we don't need to force a rebuild of all the handlers everytime
88  * we add a new one to the list. --Bleep
89  */
90 #include "handlers.h"
91 #endif /* 0 */
92 #include "client.h"
93 #include "ircd_chattr.h"
94 #include "ircd_relay.h"
95 #include "ircd_reply.h"
96 #include "ircd_string.h"
97 #include "match.h"
98 #include "msg.h"
99 #include "numeric.h"
100 #include "send.h"
101
102 #include <assert.h>
103 #include <string.h>
104
105 #if !defined(XXX_BOGUS_TEMP_HACK)
106 #include "handlers.h"
107 #endif
108
109 /*
110  * m_notice - generic message handler
111  */
112 int m_notice(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
113 {
114   char*           name;
115   char*           server;
116   int             i;
117   int             count;
118   char*           vector[MAXTARGETS];
119
120   assert(0 != cptr);
121   assert(cptr == sptr);
122
123   cli_flags(sptr) &= ~FLAGS_TS8;
124
125   if (parc < 2 || EmptyString(parv[1]))
126     return send_reply(sptr, ERR_NORECIPIENT, MSG_NOTICE);
127
128   if (parc < 3 || EmptyString(parv[parc - 1]))
129     return send_reply(sptr, ERR_NOTEXTTOSEND);
130
131   if (parv[1][0] == '@' && IsChannelPrefix(parv[1][1])) {
132     parv[1]++;                        /* Get rid of '@' */
133     return m_wallchops(cptr, sptr, parc, parv);
134   }
135
136   count = unique_name_vector(parv[1], ',', vector, MAXTARGETS);
137
138   for (i = 0; i < count; ++i) {
139     name = vector[i];
140     /*
141      * channel msg?
142      */
143     if (IsChannelPrefix(*name)) {
144       relay_channel_notice(sptr, name, parv[parc - 1]);
145     }
146     /*
147      * we have to check for the '@' at least once no matter what we do
148      * handle it first so we don't have to do it twice
149      */
150     else if ((server = strchr(name, '@')))
151       relay_directed_notice(sptr, name, server, parv[parc - 1]);
152     else 
153       relay_private_notice(sptr, name, parv[parc - 1]);
154   }
155   return 0;
156 }
157
158 /*
159  * ms_notice - server message handler template
160  */
161 int ms_notice(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
162 {
163   char* name;
164   char* server;
165
166   cli_flags(sptr) &= ~FLAGS_TS8;
167
168   if (parc < 3) {
169     /*
170      * we can't deliver it, sending an error back is pointless
171      */
172     return protocol_violation(sptr,"Not enough params for NOTICE");
173   }
174   name = parv[1];
175   /*
176    * channel msg?
177    */
178   if (IsChannelPrefix(*name)) {
179     server_relay_channel_notice(sptr, name, parv[parc - 1]);
180   }
181   /*
182    * coming from another server, we have to check this here
183    */
184   else if ('$' == *name && IsOper(sptr)) {
185     server_relay_masked_notice(sptr, name, parv[parc - 1]);
186   }
187   else if ((server = strchr(name, '@'))) {
188     /*
189      * XXX - can't get away with not doing everything
190      * relay_directed_notice has to do
191      */
192     relay_directed_notice(sptr, name, server, parv[parc - 1]);
193   }
194   else {
195     server_relay_private_notice(sptr, name, parv[parc - 1]);
196   }
197   return 0;
198 }
199
200 /*
201  * mo_notice - oper message handler
202  */
203 int mo_notice(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
204 {
205   char*           name;
206   char*           server;
207   int             i;
208   int             count;
209   char*           vector[MAXTARGETS];
210   assert(0 != cptr);
211   assert(cptr == sptr);
212
213   cli_flags(sptr) &= ~FLAGS_TS8;
214
215   if (parc < 2 || EmptyString(parv[1]))
216     return send_reply(sptr, ERR_NORECIPIENT, MSG_NOTICE);
217
218   if (parc < 3 || EmptyString(parv[parc - 1]))
219     return send_reply(sptr, ERR_NOTEXTTOSEND);
220
221   if (parv[1][0] == '@' && IsChannelPrefix(parv[1][1])) {
222     parv[1]++;                        /* Get rid of '@' */
223     return m_wallchops(cptr, sptr, parc, parv);
224   }
225
226   count = unique_name_vector(parv[1], ',', vector, MAXTARGETS);
227
228   for (i = 0; i < count; ++i) {
229     name = vector[i];
230     /*
231      * channel msg?
232      */
233     if (IsChannelPrefix(*name))
234       relay_channel_notice(sptr, name, parv[parc - 1]);
235
236     else if (*name == '$')
237       relay_masked_notice(sptr, name, parv[parc - 1]);
238
239     else if ((server = strchr(name, '@')))
240       relay_directed_notice(sptr, name, server, parv[parc - 1]);
241
242     else 
243       relay_private_notice(sptr, name, parv[parc - 1]);
244   }
245   return 0;
246 }
247
248
249 #if 0
250 /*
251  * m_message (used in m_private() and m_notice())
252  *
253  * The general function to deliver MSG's between users/channels
254  *
255  * parv[0] = sender prefix
256  * parv[1] = receiver list
257  * parv[parc-1] = message text
258  *
259  * massive cleanup
260  * rev argv 6/91
261  */
262 static int m_message(struct Client *cptr, struct Client *sptr,
263     int parc, char *parv[], int notice)
264 {
265   struct Client*  acptr;
266   char*           s;
267   struct Channel* chptr;
268   char*           nick;
269   char*           server;
270   char*           cmd;
271   char*           host;
272   int             i;
273   int             count;
274   char*           vector[MAXTARGETS];
275
276   sptr->flags &= ~FLAGS_TS8;
277
278   cmd = notice ? MSG_NOTICE : MSG_PRIVATE;
279
280   if (parc < 2 || EmptyString(parv[1]))
281     return send_error_to_client(sptr, ERR_NORECIPIENT, cmd); /* XXX DEAD */
282
283   if (parc < 3 || EmptyString(parv[parc - 1]))
284     return send_error_to_client(sptr, ERR_NOTEXTTOSEND); /* XXX DEAD */
285
286
287 #if 0
288   if (MyUser(sptr))
289     parv[1] = canonize(parv[1]);
290   for (p = 0, nick = ircd_strtok(&p, parv[1], ","); nick;
291       nick = ircd_strtok(&p, 0, ","))
292 #endif
293
294   count = unique_name_vector(parv[1], ',', vector, MAXTARGETS);
295   for (i = 0; i < count; ++i) {
296     nick = vector[i];
297     /*
298      * channel msg?
299      */
300     if (IsChannelName(nick))
301     {
302       if ((chptr = FindChannel(nick)))
303       {
304         /* This first: Almost never a server/service */
305         if (client_can_send_to_channel(sptr, chptr) || IsChannelService(sptr))
306         {
307           if (MyUser(sptr) && (chptr->mode.mode & MODE_NOPRIVMSGS) &&
308               check_target_limit(sptr, chptr, chptr->chname, 0))
309             continue;
310           sendmsgto_channel_butone(cptr, sptr, chptr, /* XXX DEAD */
311               parv[0], (notice ? TOK_NOTICE : TOK_PRIVATE), 
312               chptr->chname, parv[parc - 1]);
313         }
314         else if (!notice)
315           sendto_one(sptr, err_str(ERR_CANNOTSENDTOCHAN), /* XXX DEAD */
316               me.name, parv[0], chptr->chname);
317         continue;
318       }
319     }
320     else if (*nick != '$' && !strchr(nick, '@'))
321     {
322       /*
323        * nickname addressed?
324        */
325       if (MyUser(sptr))
326         acptr = FindUser(nick);
327       else if ((acptr = findNUser(nick)) && !IsUser(acptr))
328         acptr = 0;
329       if (acptr)
330       {
331         if (MyUser(sptr) && !IsChannelService(acptr) && 
332             check_target_limit(sptr, acptr, acptr->name, 0))
333           continue;
334         if (!is_silenced(sptr, acptr))
335         {
336           if (!notice && MyConnect(sptr) && acptr->user && acptr->user->away)
337             sendto_one(sptr, rpl_str(RPL_AWAY), /* XXX DEAD */
338                 me.name, parv[0], acptr->name, acptr->user->away);
339           if (MyUser(acptr))
340           {
341             add_target(acptr, sptr);
342             sendto_prefix_one(acptr, sptr, ":%s %s %s :%s", /* XXX DEAD */
343                 parv[0], cmd, acptr->name, parv[parc - 1]);
344           }
345           else
346             sendto_prefix_one(acptr, sptr, ":%s %s %s%s :%s", /* XXX DEAD */
347                 parv[0], (notice ? TOK_NOTICE : TOK_PRIVATE),
348                 NumNick(acptr), parv[parc - 1]);
349         }
350       }
351       else if (MyUser(sptr))
352         sendto_one(sptr, err_str(ERR_NOSUCHNICK), me.name, parv[0], nick); /* XXX DEAD */
353       else
354         sendto_one(sptr, /* XXX DEAD */
355             ":%s %d %s * :Target left UnderNet. Failed to deliver: [%.50s]",
356             me.name, ERR_NOSUCHNICK, sptr->name, parv[parc - 1]);
357       continue;
358     }
359     /*
360      * The following two cases allow masks in NOTICEs
361      * (for OPERs only)
362      *
363      * Armin, 8Jun90 (gruner@informatik.tu-muenchen.de)
364      */
365     if ((*nick == '$' || *nick == '#') && IsAnOper(sptr))
366     {
367       if (MyConnect(sptr))
368       {
369         if (!(s = strrchr(nick, '.')))
370         {
371           sendto_one(sptr, err_str(ERR_NOTOPLEVEL), me.name, parv[0], nick); /* XXX DEAD */
372           continue;
373         }
374         while (*++s)
375           if (*s == '.' || *s == '*' || *s == '?')
376             break;
377         if (*s == '*' || *s == '?')
378         {
379           sendto_one(sptr, err_str(ERR_WILDTOPLEVEL), me.name, parv[0], nick); /* XXX DEAD */
380           continue;
381         }
382       }
383       sendto_match_butone(IsServer(cptr) ? cptr : 0, /* XXX DEAD */
384           sptr, nick + 1, (*nick == '#') ? MATCH_HOST : MATCH_SERVER,
385           ":%s %s %s :%s", parv[0], cmd, nick, parv[parc - 1]);
386       continue;
387     }
388     else if ((server = strchr(nick, '@')) && (acptr = FindServer(server + 1)))
389     {
390       /*
391        * NICK[%host]@server addressed? See if <server> is me first
392        */
393       if (!IsMe(acptr))
394       {
395         sendto_one(acptr, ":%s %s %s :%s", parv[0], cmd, nick, parv[parc - 1]); /* XXX DEAD */
396         continue;
397       }
398
399       /* Look for an user whose NICK is equal to <nick> and then
400        * check if it's hostname matches <host> and if it's a local
401        * user. */
402       *server = '\0';
403       if ((host = strchr(nick, '%')))
404         *host++ = '\0';
405
406       if ((!(acptr = FindUser(nick))) ||
407           (!(MyUser(acptr))) ||
408           ((!(EmptyString(host))) && match(host, acptr->user->host)))
409         acptr = 0;
410
411       *server = '@';
412       if (host)
413         *--host = '%';
414
415       if (acptr)
416       {
417         if (!(is_silenced(sptr, acptr)))
418           sendto_prefix_one(acptr, sptr, ":%s %s %s :%s", /* XXX DEAD */
419               parv[0], cmd, nick, parv[parc - 1]);
420         continue;
421       }
422     }
423     if (IsChannelName(nick))
424       sendto_one(sptr, err_str(ERR_NOSUCHCHANNEL), me.name, parv[0], nick); /* XXX DEAD */
425     else
426       sendto_one(sptr, err_str(ERR_NOSUCHNICK), me.name, parv[0], nick); /* XXX DEAD */
427   }
428   return 0;
429 }
430
431 /*
432  * m_private
433  *
434  * parv[0] = sender prefix
435  * parv[1] = receiver list
436  * parv[parc-1] = message text
437  */
438 int m_private(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
439 {
440   return m_message(cptr, sptr, parc, parv, 0);
441 }
442
443 /*
444  * m_notice
445  *
446  * parv[0] = sender prefix
447  * parv[1] = receiver list
448  * parv[parc-1] = notice text
449  */
450 int m_notice(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
451 {
452   if (MyUser(sptr) && parv[1] && parv[1][0] == '@' &&
453       IsChannelName(&parv[1][1]))
454   {
455     parv[1]++;                        /* Get rid of '@' */
456     return m_wallchops(cptr, sptr, parc, parv);
457   }
458   return m_message(cptr, sptr, parc, parv, 1);
459 }
460
461 #endif