Author: Bleep <tomh@inxpress.net>
[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 #if 0
83 /*
84  * No need to include handlers.h here the signatures must match
85  * and we don't need to force a rebuild of all the handlers everytime
86  * we add a new one to the list. --Bleep
87  */
88 #include "handlers.h"
89 #endif /* 0 */
90 #include "client.h"
91 #include "ircd_chattr.h"
92 #include "ircd_relay.h"
93 #include "ircd_reply.h"
94 #include "ircd_string.h"
95 #include "match.h"
96 #include "msg.h"
97 #include "numeric.h"
98 #include "send.h"
99
100 #include <assert.h>
101 #include <string.h>
102
103 #if !defined(XXX_BOGUS_TEMP_HACK)
104 #include "handlers.h"
105 #endif
106
107 /*
108  * m_notice - generic message handler
109  */
110 int m_notice(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
111 {
112   char*           name;
113   char*           server;
114   int             i;
115   int             count;
116   char*           vector[MAXTARGETS];
117
118   assert(0 != cptr);
119   assert(cptr == sptr);
120
121   sptr->flags &= ~FLAGS_TS8;
122
123   if (parc < 2 || EmptyString(parv[1]))
124     return send_error_to_client(sptr, ERR_NORECIPIENT, MSG_NOTICE);
125
126   if (parc < 3 || EmptyString(parv[parc - 1]))
127     return send_error_to_client(sptr, ERR_NOTEXTTOSEND);
128
129   if (parv[1][0] == '@' && IsChannelPrefix(parv[1][1])) {
130     parv[1]++;                        /* Get rid of '@' */
131     return m_wallchops(cptr, sptr, parc, parv);
132   }
133
134   count = unique_name_vector(parv[1], ',', vector, MAXTARGETS);
135
136   for (i = 0; i < count; ++i) {
137     name = vector[i];
138     /*
139      * channel msg?
140      */
141     if (IsChannelPrefix(*name)) {
142       relay_channel_notice(sptr, name, parv[parc - 1]);
143     }
144     /*
145      * we have to check for the '@' at least once no matter what we do
146      * handle it first so we don't have to do it twice
147      */
148     else if ((server = strchr(name, '@')))
149       relay_directed_notice(sptr, name, server, parv[parc - 1]);
150     else 
151       relay_private_notice(sptr, name, parv[parc - 1]);
152   }
153   return 0;
154 }
155
156 /*
157  * ms_notice - server message handler template
158  */
159 int ms_notice(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
160 {
161   char* name;
162   char* server;
163
164   sptr->flags &= ~FLAGS_TS8;
165
166   if (parc < 3) {
167     /*
168      * we can't deliver it, sending an error back is pointless
169      */
170     return 0;
171   }
172   name = parv[1];
173   /*
174    * channel msg?
175    */
176   if (IsChannelPrefix(*name)) {
177     server_relay_channel_notice(sptr, name, parv[parc - 1]);
178   }
179   /*
180    * coming from another server, we have to check this here
181    */
182   else if ('$' == *name && IsOper(sptr)) {
183     server_relay_masked_notice(sptr, name, parv[parc - 1]);
184   }
185   else if ((server = strchr(name, '@'))) {
186     /*
187      * XXX - can't get away with not doing everything
188      * relay_directed_notice has to do
189      */
190     relay_directed_notice(sptr, name, server, parv[parc - 1]);
191   }
192   else {
193     server_relay_private_notice(sptr, name, parv[parc - 1]);
194   }
195   return 0;
196 }
197
198 /*
199  * mo_notice - oper message handler
200  */
201 int mo_notice(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
202 {
203   char*           name;
204   char*           server;
205   int             i;
206   int             count;
207   char*           vector[MAXTARGETS];
208   assert(0 != cptr);
209   assert(cptr == sptr);
210
211   sptr->flags &= ~FLAGS_TS8;
212
213   if (parc < 2 || EmptyString(parv[1]))
214     return send_error_to_client(sptr, ERR_NORECIPIENT, MSG_NOTICE);
215
216   if (parc < 3 || EmptyString(parv[parc - 1]))
217     return send_error_to_client(sptr, ERR_NOTEXTTOSEND);
218
219   if (parv[1][0] == '@' && IsChannelPrefix(parv[1][1])) {
220     parv[1]++;                        /* Get rid of '@' */
221     return m_wallchops(cptr, sptr, parc, parv);
222   }
223
224   count = unique_name_vector(parv[1], ',', vector, MAXTARGETS);
225
226   for (i = 0; i < count; ++i) {
227     name = vector[i];
228     /*
229      * channel msg?
230      */
231     if (IsChannelPrefix(*name))
232       relay_channel_notice(sptr, name, parv[parc - 1]);
233
234     else if (*name == '$')
235       relay_masked_notice(sptr, name, parv[parc - 1]);
236
237     else if ((server = strchr(name, '@')))
238       relay_directed_notice(sptr, name, server, parv[parc - 1]);
239
240     else 
241       relay_private_notice(sptr, name, parv[parc - 1]);
242   }
243   return 0;
244 }
245
246
247 #if 0
248 /*
249  * m_message (used in m_private() and m_notice())
250  *
251  * The general function to deliver MSG's between users/channels
252  *
253  * parv[0] = sender prefix
254  * parv[1] = receiver list
255  * parv[parc-1] = message text
256  *
257  * massive cleanup
258  * rev argv 6/91
259  */
260 static int m_message(struct Client *cptr, struct Client *sptr,
261     int parc, char *parv[], int notice)
262 {
263   struct Client*  acptr;
264   char*           s;
265   struct Channel* chptr;
266   char*           nick;
267   char*           server;
268   char*           cmd;
269   char*           host;
270   int             i;
271   int             count;
272   char*           vector[MAXTARGETS];
273
274   sptr->flags &= ~FLAGS_TS8;
275
276   cmd = notice ? MSG_NOTICE : MSG_PRIVATE;
277
278   if (parc < 2 || EmptyString(parv[1]))
279     return send_error_to_client(sptr, ERR_NORECIPIENT, cmd);
280
281   if (parc < 3 || EmptyString(parv[parc - 1]))
282     return send_error_to_client(sptr, ERR_NOTEXTTOSEND);
283
284
285 #if 0
286   if (MyUser(sptr))
287     parv[1] = canonize(parv[1]);
288   for (p = 0, nick = ircd_strtok(&p, parv[1], ","); nick;
289       nick = ircd_strtok(&p, 0, ","))
290 #endif
291
292   count = unique_name_vector(parv[1], ',', vector, MAXTARGETS);
293   for (i = 0; i < count; ++i) {
294     nick = vector[i];
295     /*
296      * channel msg?
297      */
298     if (IsChannelName(nick))
299     {
300       if ((chptr = FindChannel(nick)))
301       {
302         /* This first: Almost never a server/service */
303         if (client_can_send_to_channel(sptr, chptr) || IsChannelService(sptr))
304         {
305           if (MyUser(sptr) && (chptr->mode.mode & MODE_NOPRIVMSGS) &&
306               check_target_limit(sptr, chptr, chptr->chname, 0))
307             continue;
308           sendmsgto_channel_butone(cptr, sptr, chptr,
309               parv[0], (notice ? TOK_NOTICE : TOK_PRIVATE), 
310               chptr->chname, parv[parc - 1]);
311         }
312         else if (!notice)
313           sendto_one(sptr, err_str(ERR_CANNOTSENDTOCHAN),
314               me.name, parv[0], chptr->chname);
315         continue;
316       }
317     }
318     else if (*nick != '$' && !strchr(nick, '@'))
319     {
320       /*
321        * nickname addressed?
322        */
323       if (MyUser(sptr))
324         acptr = FindUser(nick);
325       else if ((acptr = findNUser(nick)) && !IsUser(acptr))
326         acptr = 0;
327       if (acptr)
328       {
329         if (MyUser(sptr) && check_target_limit(sptr, acptr, acptr->name, 0))
330           continue;
331         if (!is_silenced(sptr, acptr))
332         {
333           if (!notice && MyConnect(sptr) && acptr->user && acptr->user->away)
334             sendto_one(sptr, rpl_str(RPL_AWAY),
335                 me.name, parv[0], acptr->name, acptr->user->away);
336           if (MyUser(acptr))
337           {
338             add_target(acptr, sptr);
339             sendto_prefix_one(acptr, sptr, ":%s %s %s :%s",
340                 parv[0], cmd, acptr->name, parv[parc - 1]);
341           }
342           else
343             sendto_prefix_one(acptr, sptr, ":%s %s %s%s :%s",
344                 parv[0], (notice ? TOK_NOTICE : TOK_PRIVATE),
345                 NumNick(acptr), parv[parc - 1]);
346         }
347       }
348       else if (MyUser(sptr))
349         sendto_one(sptr, err_str(ERR_NOSUCHNICK), me.name, parv[0], nick);
350       else
351         sendto_one(sptr,
352             ":%s %d %s * :Target left UnderNet. Failed to deliver: [%.50s]",
353             me.name, ERR_NOSUCHNICK, sptr->name, parv[parc - 1]);
354       continue;
355     }
356     /*
357      * The following two cases allow masks in NOTICEs
358      * (for OPERs only)
359      *
360      * Armin, 8Jun90 (gruner@informatik.tu-muenchen.de)
361      */
362     if ((*nick == '$' || *nick == '#') && IsAnOper(sptr))
363     {
364       if (MyConnect(sptr))
365       {
366         if (!(s = strrchr(nick, '.')))
367         {
368           sendto_one(sptr, err_str(ERR_NOTOPLEVEL), me.name, parv[0], nick);
369           continue;
370         }
371         while (*++s)
372           if (*s == '.' || *s == '*' || *s == '?')
373             break;
374         if (*s == '*' || *s == '?')
375         {
376           sendto_one(sptr, err_str(ERR_WILDTOPLEVEL), me.name, parv[0], nick);
377           continue;
378         }
379       }
380       sendto_match_butone(IsServer(cptr) ? cptr : 0,
381           sptr, nick + 1, (*nick == '#') ? MATCH_HOST : MATCH_SERVER,
382           ":%s %s %s :%s", parv[0], cmd, nick, parv[parc - 1]);
383       continue;
384     }
385     else if ((server = strchr(nick, '@')) && (acptr = FindServer(server + 1)))
386     {
387       /*
388        * NICK[%host]@server addressed? See if <server> is me first
389        */
390       if (!IsMe(acptr))
391       {
392         sendto_one(acptr, ":%s %s %s :%s", parv[0], cmd, nick, parv[parc - 1]);
393         continue;
394       }
395
396       /* Look for an user whose NICK is equal to <nick> and then
397        * check if it's hostname matches <host> and if it's a local
398        * user. */
399       *server = '\0';
400       if ((host = strchr(nick, '%')))
401         *host++ = '\0';
402
403       if ((!(acptr = FindUser(nick))) ||
404           (!(MyUser(acptr))) ||
405           ((!(EmptyString(host))) && match(host, acptr->user->host)))
406         acptr = 0;
407
408       *server = '@';
409       if (host)
410         *--host = '%';
411
412       if (acptr)
413       {
414         if (!(is_silenced(sptr, acptr)))
415           sendto_prefix_one(acptr, sptr, ":%s %s %s :%s",
416               parv[0], cmd, nick, parv[parc - 1]);
417         continue;
418       }
419     }
420     if (IsChannelName(nick))
421       sendto_one(sptr, err_str(ERR_NOSUCHCHANNEL), me.name, parv[0], nick);
422     else
423       sendto_one(sptr, err_str(ERR_NOSUCHNICK), me.name, parv[0], nick);
424   }
425   return 0;
426 }
427
428 /*
429  * m_private
430  *
431  * parv[0] = sender prefix
432  * parv[1] = receiver list
433  * parv[parc-1] = message text
434  */
435 int m_private(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
436 {
437   return m_message(cptr, sptr, parc, parv, 0);
438 }
439
440 /*
441  * m_notice
442  *
443  * parv[0] = sender prefix
444  * parv[1] = receiver list
445  * parv[parc-1] = notice text
446  */
447 int m_notice(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
448 {
449   if (MyUser(sptr) && parv[1] && parv[1][0] == '@' &&
450       IsChannelName(&parv[1][1]))
451   {
452     parv[1]++;                        /* Get rid of '@' */
453     return m_wallchops(cptr, sptr, parc, parv);
454   }
455   return m_message(cptr, sptr, parc, parv, 1);
456 }
457
458 #endif