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