2 * IRC - Internet Relay Chat, ircd/m_privmsg.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
6 * See file AUTHORS in IRC package for additional names of
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)
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.
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.
27 * m_functions execute protocol messages on this server:
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...).
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.
39 * (!IsServer(cptr)) => (cptr == sptr), because
40 * prefixes are taken *only* from servers...
43 * (sptr == cptr) => the message didn't
46 * (sptr != cptr && IsServer(sptr) means
47 * the prefix specified servername. (?)
49 * (sptr != cptr && !IsServer(sptr) means
50 * that message originated from a remote
55 * (!IsServer(sptr)) means that, sptr can safely
56 * taken as defining the target structure of the
57 * message in this server.
59 * *Always* true (if 'parse' and others are working correct):
61 * 1) sptr->from == cptr (note: cptr->from == cptr)
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 ]
68 * parc number of variable parameter strings (if zero,
69 * parv is allowed to be NULL)
71 * parv a NULL terminated list of parameter pointers,
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*
79 * note: it is guaranteed that parv[0]..parv[parc-1] are all
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
92 #include "ircd_chattr.h"
93 #include "ircd_relay.h"
94 #include "ircd_reply.h"
95 #include "ircd_string.h"
105 * m_privmsg - generic message handler
107 int m_privmsg(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
113 char* vector[MAXTARGETS];
116 assert(cptr == sptr);
117 assert(0 != sptr->user);
119 sptr->flags &= ~FLAGS_TS8;
122 sptr->user->last = CurrentTime;
125 if (parc < 2 || EmptyString(parv[1]))
126 return send_error_to_client(sptr, ERR_NORECIPIENT, MSG_PRIVATE);
128 if (parc < 3 || EmptyString(parv[parc - 1]))
129 return send_error_to_client(sptr, ERR_NOTEXTTOSEND);
131 count = unique_name_vector(parv[1], ',', vector, MAXTARGETS);
133 for (i = 0; i < count; ++i) {
138 if (IsChannelPrefix(*name)) {
139 relay_channel_message(sptr, name, parv[parc - 1]);
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
145 else if ((server = strchr(name, '@')))
146 relay_directed_message(sptr, name, server, parv[parc - 1]);
148 relay_private_message(sptr, name, parv[parc - 1]);
154 * ms_privmsg - server message handler template
156 int ms_privmsg(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
161 sptr->flags &= ~FLAGS_TS8;
165 * we can't deliver it, sending an error back is pointless
173 if (IsChannelPrefix(*name)) {
174 server_relay_channel_message(sptr, name, parv[parc - 1]);
177 * coming from another server, we have to check this here
179 else if ('$' == *name && IsOper(sptr)) {
180 server_relay_masked_message(sptr, name, parv[parc - 1]);
182 else if ((server = strchr(name, '@'))) {
184 * XXX - can't get away with not doing everything
185 * relay_directed_message has to do
187 relay_directed_message(sptr, name, server, parv[parc - 1]);
190 server_relay_private_message(sptr, name, parv[parc - 1]);
197 * mo_privmsg - oper message handler
199 int mo_privmsg(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
205 char* vector[MAXTARGETS];
207 assert(cptr == sptr);
208 assert(0 != sptr->user);
210 sptr->flags &= ~FLAGS_TS8;
213 sptr->user->last = CurrentTime;
216 if (parc < 2 || EmptyString(parv[1]))
217 return send_error_to_client(sptr, ERR_NORECIPIENT, MSG_PRIVATE);
219 if (parc < 3 || EmptyString(parv[parc - 1]))
220 return send_error_to_client(sptr, ERR_NOTEXTTOSEND);
222 count = unique_name_vector(parv[1], ',', vector, MAXTARGETS);
224 for (i = 0; i < count; ++i) {
229 if (IsChannelPrefix(*name))
230 relay_channel_message(sptr, name, parv[parc - 1]);
232 else if (*name == '$')
233 relay_masked_message(sptr, name, parv[parc - 1]);
235 else if ((server = strchr(name, '@')))
236 relay_directed_message(sptr, name, server, parv[parc - 1]);
239 relay_private_message(sptr, name, parv[parc - 1]);
247 * m_message (used in m_private() and m_notice())
249 * The general function to deliver MSG's between users/channels
251 * parv[0] = sender prefix
252 * parv[1] = receiver list
253 * parv[parc-1] = message text
258 static int m_message(struct Client *cptr, struct Client *sptr,
259 int parc, char *parv[], int notice)
261 struct Client* acptr;
263 struct Channel* chptr;
270 char* vector[MAXTARGETS];
272 sptr->flags &= ~FLAGS_TS8;
274 cmd = notice ? MSG_NOTICE : MSG_PRIVATE;
276 if (parc < 2 || EmptyString(parv[1]))
277 return send_error_to_client(sptr, ERR_NORECIPIENT, cmd);
279 if (parc < 3 || EmptyString(parv[parc - 1]))
280 return send_error_to_client(sptr, ERR_NOTEXTTOSEND);
285 parv[1] = canonize(parv[1]);
286 for (p = 0, nick = ircd_strtok(&p, parv[1], ","); nick;
287 nick = ircd_strtok(&p, 0, ","))
290 count = unique_name_vector(parv[1], ',', vector, MAXTARGETS);
291 for (i = 0; i < count; ++i) {
296 if (IsChannelName(nick))
298 if ((chptr = FindChannel(nick)))
300 /* This first: Almost never a server/service */
301 if (client_can_send_to_channel(sptr, chptr) || IsChannelService(sptr))
303 if (MyUser(sptr) && (chptr->mode.mode & MODE_NOPRIVMSGS) &&
304 check_target_limit(sptr, chptr, chptr->chname, 0))
306 sendmsgto_channel_butone(cptr, sptr, chptr,
307 parv[0], (notice ? TOK_NOTICE : TOK_PRIVATE),
308 chptr->chname, parv[parc - 1]);
311 sendto_one(sptr, err_str(ERR_CANNOTSENDTOCHAN),
312 me.name, parv[0], chptr->chname);
316 else if (*nick != '$' && !strchr(nick, '@'))
319 * nickname addressed?
322 acptr = FindUser(nick);
323 else if ((acptr = findNUser(nick)) && !IsUser(acptr))
327 if (MyUser(sptr) && check_target_limit(sptr, acptr, acptr->name, 0))
329 if (!is_silenced(sptr, acptr))
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);
336 add_target(acptr, sptr);
337 sendto_prefix_one(acptr, sptr, ":%s %s %s :%s",
338 parv[0], cmd, acptr->name, parv[parc - 1]);
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]);
346 else if (MyUser(sptr))
347 sendto_one(sptr, err_str(ERR_NOSUCHNICK), me.name, parv[0], nick);
350 ":%s %d %s * :Target left UnderNet. Failed to deliver: [%.50s]",
351 me.name, ERR_NOSUCHNICK, sptr->name, parv[parc - 1]);
355 * The following two cases allow masks in NOTICEs
358 * Armin, 8Jun90 (gruner@informatik.tu-muenchen.de)
360 if ((*nick == '$' || *nick == '#') && IsAnOper(sptr))
364 if (!(s = strrchr(nick, '.')))
366 sendto_one(sptr, err_str(ERR_NOTOPLEVEL), me.name, parv[0], nick);
370 if (*s == '.' || *s == '*' || *s == '?')
372 if (*s == '*' || *s == '?')
374 sendto_one(sptr, err_str(ERR_WILDTOPLEVEL), me.name, parv[0], nick);
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]);
383 else if ((server = strchr(nick, '@')) && (acptr = FindServer(server + 1)))
386 * NICK[%host]@server addressed? See if <server> is me first
390 sendto_one(acptr, ":%s %s %s :%s", parv[0], cmd, nick, parv[parc - 1]);
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
398 if ((host = strchr(nick, '%')))
401 if ((!(acptr = FindUser(nick))) ||
402 (!(MyUser(acptr))) ||
403 ((!(EmptyString(host))) && match(host, acptr->user->host)))
412 if (!(is_silenced(sptr, acptr)))
413 sendto_prefix_one(acptr, sptr, ":%s %s %s :%s",
414 parv[0], cmd, nick, parv[parc - 1]);
418 if (IsChannelName(nick))
419 sendto_one(sptr, err_str(ERR_NOSUCHCHANNEL), me.name, parv[0], nick);
421 sendto_one(sptr, err_str(ERR_NOSUCHNICK), me.name, parv[0], nick);
429 * parv[0] = sender prefix
430 * parv[1] = receiver list
431 * parv[parc-1] = message text
433 int m_private(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
435 return m_message(cptr, sptr, parc, parv, 0);
438 #if !defined(XXX_BOGUS_TEMP_HACK)
439 #include "handlers.h"
444 * parv[0] = sender prefix
445 * parv[1] = receiver list
446 * parv[parc-1] = notice text
448 int m_notice(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
450 if (MyUser(sptr) && parv[1] && parv[1][0] == '@' &&
451 IsChannelName(&parv[1][1]))
453 parv[1]++; /* Get rid of '@' */
454 return m_wallchops(cptr, sptr, parc, parv);
456 return m_message(cptr, sptr, parc, parv, 1);