]> jfr.im git - solanum.git/blame - modules/core/m_message.c
Remove shared blocks
[solanum.git] / modules / core / m_message.c
CommitLineData
212380e3
AC
1/*
2 * ircd-ratbox: A slightly useful ircd.
3 * m_message.c: Sends a (PRIVMSG|NOTICE) message to a user or channel.
4 *
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
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 2 of the License, or
12 * (at your option) 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
212380e3
AC
23 */
24
25#include "stdinc.h"
26#include "client.h"
27#include "ircd.h"
28#include "numeric.h"
212380e3
AC
29#include "s_conf.h"
30#include "s_serv.h"
31#include "msg.h"
32#include "parse.h"
33#include "modules.h"
34#include "channel.h"
4562c604 35#include "match.h"
212380e3
AC
36#include "hash.h"
37#include "class.h"
38#include "msg.h"
39#include "packet.h"
40#include "send.h"
212380e3 41#include "s_newconf.h"
43f8445d 42#include "s_stats.h"
4f2685f3 43#include "tgchange.h"
ab428518 44#include "inline/stringops.h"
212380e3 45
eeabf33a
EM
46static const char message_desc[] =
47 "Provides the PRIVMSG and NOTICE commands to send messages to users and channels";
48
3c7d6fcc
EM
49static void m_message(enum message_type, struct MsgBuf *, struct Client *, struct Client *, int, const char **);
50static void m_privmsg(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
51static void m_notice(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
212380e3
AC
52
53static void expire_tgchange(void *unused);
0e7cb7e6 54static struct ev_entry *expire_tgchange_event;
212380e3
AC
55
56static int
57modinit(void)
58{
0e7cb7e6 59 expire_tgchange_event = rb_event_addish("expire_tgchange", expire_tgchange, NULL, 300);
212380e3
AC
60 expire_tgchange(NULL);
61 return 0;
62}
63
64static void
65moddeinit(void)
66{
0e7cb7e6 67 rb_event_delete(expire_tgchange_event);
212380e3
AC
68}
69
70struct Message privmsg_msgtab = {
7baa37a9 71 "PRIVMSG", 0, 0, 0, 0,
212380e3
AC
72 {mg_unreg, {m_privmsg, 0}, {m_privmsg, 0}, mg_ignore, mg_ignore, {m_privmsg, 0}}
73};
74struct Message notice_msgtab = {
7baa37a9 75 "NOTICE", 0, 0, 0, 0,
212380e3
AC
76 {mg_unreg, {m_notice, 0}, {m_notice, 0}, {m_notice, 0}, mg_ignore, {m_notice, 0}}
77};
78
79mapi_clist_av1 message_clist[] = { &privmsg_msgtab, &notice_msgtab, NULL };
80
8661d5ae 81DECLARE_MODULE_AV2(message, modinit, moddeinit, message_clist, NULL, NULL, NULL, NULL, message_desc);
212380e3
AC
82
83struct entity
84{
85 void *ptr;
86 int type;
87 int flags;
88};
89
ca4c2a86 90static int build_target_list(enum message_type msgtype,
212380e3
AC
91 struct Client *client_p,
92 struct Client *source_p, const char *nicks_channels, const char *text);
93
3c7d6fcc 94static bool flood_attack_client(enum message_type msgtype, struct Client *source_p, struct Client *target_p);
212380e3 95
aa9c9ed2
JT
96/* Fifteen seconds should be plenty for a client to reply a ctcp */
97#define LARGE_CTCP_TIME 15
98
212380e3
AC
99#define ENTITY_NONE 0
100#define ENTITY_CHANNEL 1
c4d2d014
JT
101#define ENTITY_CHANNEL_OPMOD 2
102#define ENTITY_CHANOPS_ON_CHANNEL 3
103#define ENTITY_CLIENT 4
212380e3
AC
104
105static struct entity targets[512];
106static int ntargets = 0;
107
3c7d6fcc 108static bool duplicate_ptr(void *);
212380e3 109
ca4c2a86 110static void msg_channel(enum message_type msgtype,
212380e3
AC
111 struct Client *client_p,
112 struct Client *source_p, struct Channel *chptr, const char *text);
113
ca4c2a86 114static void msg_channel_opmod(enum message_type msgtype,
c4d2d014
JT
115 struct Client *client_p,
116 struct Client *source_p, struct Channel *chptr,
117 const char *text);
118
ca4c2a86 119static void msg_channel_flags(enum message_type msgtype,
212380e3
AC
120 struct Client *client_p,
121 struct Client *source_p,
122 struct Channel *chptr, int flags, const char *text);
123
ca4c2a86 124static void msg_client(enum message_type msgtype,
212380e3
AC
125 struct Client *source_p, struct Client *target_p, const char *text);
126
ca4c2a86 127static void handle_special(enum message_type msgtype,
212380e3
AC
128 struct Client *client_p, struct Client *source_p, const char *nick,
129 const char *text);
130
131/*
132** m_privmsg
133**
134** massive cleanup
135** rev argv 6/91
136**
137** Another massive cleanup Nov, 2000
138** (I don't think there is a single line left from 6/91. Maybe.)
139** m_privmsg and m_notice do basically the same thing.
140** in the original 2.8.2 code base, they were the same function
141** "m_message.c." When we did the great cleanup in conjuncton with bleep
142** of ircu fame, we split m_privmsg.c and m_notice.c.
143** I don't see the point of that now. Its harder to maintain, its
144** easier to introduce bugs into one version and not the other etc.
145** Really, the penalty of an extra function call isn't that big a deal folks.
146** -db Nov 13, 2000
147**
148*/
ca4c2a86
AC
149const char *cmdname[MESSAGE_TYPE_COUNT] = {
150 [MESSAGE_TYPE_PRIVMSG] = "PRIVMSG",
151 [MESSAGE_TYPE_NOTICE] = "NOTICE",
152};
212380e3 153
3c7d6fcc 154static void
428ca87b 155m_privmsg(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
212380e3 156{
3c7d6fcc 157 m_message(MESSAGE_TYPE_PRIVMSG, msgbuf_p, client_p, source_p, parc, parv);
212380e3
AC
158}
159
3c7d6fcc 160static void
428ca87b 161m_notice(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
212380e3 162{
3c7d6fcc 163 m_message(MESSAGE_TYPE_NOTICE, msgbuf_p, client_p, source_p, parc, parv);
212380e3
AC
164}
165
166/*
167 * inputs - flag privmsg or notice
212380e3
AC
168 * - pointer to client_p
169 * - pointer to source_p
170 * - pointer to channel
171 */
3c7d6fcc 172static void
428ca87b 173m_message(enum message_type msgtype, struct MsgBuf *msgbuf_p,
212380e3
AC
174 struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
175{
176 int i;
177
178 if(parc < 2 || EmptyString(parv[1]))
179 {
ca4c2a86 180 if(msgtype != MESSAGE_TYPE_NOTICE)
212380e3 181 sendto_one(source_p, form_str(ERR_NORECIPIENT), me.name,
ca4c2a86 182 source_p->name, cmdname[msgtype]);
3c7d6fcc 183 return;
212380e3
AC
184 }
185
186 if(parc < 3 || EmptyString(parv[2]))
187 {
ca4c2a86 188 if(msgtype != MESSAGE_TYPE_NOTICE)
212380e3 189 sendto_one(source_p, form_str(ERR_NOTEXTTOSEND), me.name, source_p->name);
3c7d6fcc 190 return;
212380e3
AC
191 }
192
193 /* Finish the flood grace period if theyre not messaging themselves
194 * as some clients (ircN) do this as a "lag check"
195 */
196 if(MyClient(source_p) && !IsFloodDone(source_p) && irccmp(source_p->name, parv[1]))
197 flood_endgrace(source_p);
198
ca4c2a86 199 if(build_target_list(msgtype, client_p, source_p, parv[1], parv[2]) < 0)
212380e3 200 {
3c7d6fcc 201 return;
212380e3
AC
202 }
203
204 for(i = 0; i < ntargets; i++)
205 {
206 switch (targets[i].type)
207 {
208 case ENTITY_CHANNEL:
ca4c2a86 209 msg_channel(msgtype, client_p, source_p,
212380e3
AC
210 (struct Channel *) targets[i].ptr, parv[2]);
211 break;
212
c4d2d014 213 case ENTITY_CHANNEL_OPMOD:
ca4c2a86 214 msg_channel_opmod(msgtype, client_p, source_p,
c4d2d014
JT
215 (struct Channel *) targets[i].ptr, parv[2]);
216 break;
217
212380e3 218 case ENTITY_CHANOPS_ON_CHANNEL:
ca4c2a86 219 msg_channel_flags(msgtype, client_p, source_p,
212380e3
AC
220 (struct Channel *) targets[i].ptr,
221 targets[i].flags, parv[2]);
222 break;
223
224 case ENTITY_CLIENT:
ca4c2a86 225 msg_client(msgtype, source_p,
212380e3
AC
226 (struct Client *) targets[i].ptr, parv[2]);
227 break;
228 }
229 }
212380e3
AC
230}
231
232/*
233 * build_target_list
234 *
235 * inputs - pointer to given client_p (server)
236 * - pointer to given source (oper/client etc.)
237 * - pointer to list of nicks/channels
238 * - pointer to table to place results
239 * - pointer to text (only used if source_p is an oper)
240 * output - number of valid entities
241 * side effects - target_table is modified to contain a list of
242 * pointers to channels or clients
243 * if source client is an oper
244 * all the classic old bizzare oper privmsg tricks
245 * are parsed and sent as is, if prefixed with $
246 * to disambiguate.
212380e3
AC
247 */
248
249static int
ca4c2a86 250build_target_list(enum message_type msgtype, struct Client *client_p,
212380e3
AC
251 struct Client *source_p, const char *nicks_channels, const char *text)
252{
253 int type;
254 char *p, *nick, *target_list;
255 struct Channel *chptr = NULL;
256 struct Client *target_p;
257
258 target_list = LOCAL_COPY(nicks_channels); /* skip strcpy for non-lazyleafs */
259
260 ntargets = 0;
261
4a2651e5 262 for(nick = rb_strtok_r(target_list, ",", &p); nick; nick = rb_strtok_r(NULL, ",", &p))
212380e3
AC
263 {
264 char *with_prefix;
265 /*
266 * channels are privmsg'd a lot more than other clients, moved up
267 * here plain old channel msg?
268 */
269
270 if(IsChanPrefix(*nick))
271 {
272 /* ignore send of local channel to a server (should not happen) */
273 if(IsServer(client_p) && *nick == '&')
274 continue;
275
276 if((chptr = find_channel(nick)) != NULL)
277 {
278 if(!duplicate_ptr(chptr))
279 {
280 if(ntargets >= ConfigFileEntry.max_targets)
281 {
282 sendto_one(source_p, form_str(ERR_TOOMANYTARGETS),
283 me.name, source_p->name, nick);
284 return (1);
285 }
286 targets[ntargets].ptr = (void *) chptr;
287 targets[ntargets++].type = ENTITY_CHANNEL;
288 }
289 }
290
291 /* non existant channel */
ca4c2a86 292 else if(msgtype != MESSAGE_TYPE_NOTICE)
212380e3
AC
293 sendto_one_numeric(source_p, ERR_NOSUCHNICK,
294 form_str(ERR_NOSUCHNICK), nick);
295
296 continue;
297 }
298
299 if(MyClient(source_p))
300 target_p = find_named_person(nick);
301 else
302 target_p = find_person(nick);
303
304 /* look for a privmsg to another client */
305 if(target_p)
306 {
307 if(!duplicate_ptr(target_p))
308 {
309 if(ntargets >= ConfigFileEntry.max_targets)
310 {
311 sendto_one(source_p, form_str(ERR_TOOMANYTARGETS),
312 me.name, source_p->name, nick);
313 return (1);
314 }
315 targets[ntargets].ptr = (void *) target_p;
316 targets[ntargets].type = ENTITY_CLIENT;
317 targets[ntargets++].flags = 0;
318 }
319 continue;
320 }
321
322 /* @#channel or +#channel message ? */
323
324 type = 0;
325 with_prefix = nick;
326 /* allow %+@ if someone wants to do that */
327 for(;;)
328 {
329 if(*nick == '@')
330 type |= CHFL_CHANOP;
331 else if(*nick == '+')
332 type |= CHFL_CHANOP | CHFL_VOICE;
333 else
334 break;
335 nick++;
336 }
337
338 if(type != 0)
339 {
340 /* no recipient.. */
341 if(EmptyString(nick))
342 {
343 sendto_one(source_p, form_str(ERR_NORECIPIENT),
ca4c2a86 344 me.name, source_p->name, cmdname[msgtype]);
212380e3
AC
345 continue;
346 }
347
348 /* At this point, nick+1 should be a channel name i.e. #foo or &foo
349 * if the channel is found, fine, if not report an error
350 */
351
352 if((chptr = find_channel(nick)) != NULL)
353 {
354 struct membership *msptr;
355
356 msptr = find_channel_membership(chptr, source_p);
357
358 if(!IsServer(source_p) && !IsService(source_p) && !is_chanop_voiced(msptr))
359 {
360 sendto_one(source_p, form_str(ERR_CHANOPRIVSNEEDED),
631b4a54
JT
361 get_id(&me, source_p),
362 get_id(source_p, source_p),
363 with_prefix);
bc75c3ae 364 continue;
212380e3
AC
365 }
366
367 if(!duplicate_ptr(chptr))
368 {
369 if(ntargets >= ConfigFileEntry.max_targets)
370 {
371 sendto_one(source_p, form_str(ERR_TOOMANYTARGETS),
372 me.name, source_p->name, nick);
373 return (1);
374 }
375 targets[ntargets].ptr = (void *) chptr;
376 targets[ntargets].type = ENTITY_CHANOPS_ON_CHANNEL;
377 targets[ntargets++].flags = type;
378 }
379 }
ca4c2a86 380 else if(msgtype != MESSAGE_TYPE_NOTICE)
212380e3
AC
381 {
382 sendto_one_numeric(source_p, ERR_NOSUCHNICK,
383 form_str(ERR_NOSUCHNICK), nick);
384 }
385
386 continue;
387 }
388
c4d2d014
JT
389 if(IsServer(client_p) && *nick == '=' && nick[1] == '#')
390 {
391 nick++;
392 if((chptr = find_channel(nick)) != NULL)
393 {
394 if(!duplicate_ptr(chptr))
395 {
396 if(ntargets >= ConfigFileEntry.max_targets)
397 {
398 sendto_one(source_p, form_str(ERR_TOOMANYTARGETS),
399 me.name, source_p->name, nick);
400 return (1);
401 }
402 targets[ntargets].ptr = (void *) chptr;
403 targets[ntargets++].type = ENTITY_CHANNEL_OPMOD;
404 }
405 }
406
407 /* non existant channel */
ca4c2a86 408 else if(msgtype != MESSAGE_TYPE_NOTICE)
c4d2d014
JT
409 sendto_one_numeric(source_p, ERR_NOSUCHNICK,
410 form_str(ERR_NOSUCHNICK), nick);
411
412 continue;
413 }
414
1fd171a5
JT
415 if(strchr(nick, '@') || (IsOper(source_p) && (*nick == '$')))
416 {
ca4c2a86 417 handle_special(msgtype, client_p, source_p, nick, text);
1fd171a5
JT
418 continue;
419 }
420
212380e3 421 /* no matching anything found - error if not NOTICE */
ca4c2a86 422 if(msgtype != MESSAGE_TYPE_NOTICE)
212380e3
AC
423 {
424 /* dont give this numeric when source is local,
425 * because its misleading --anfl
426 */
427 if(!MyClient(source_p) && IsDigit(*nick))
428 sendto_one(source_p, ":%s %d %s * :Target left IRC. "
429 "Failed to deliver: [%.20s]",
430 get_id(&me, source_p), ERR_NOSUCHNICK,
431 get_id(source_p, source_p), text);
432 else
433 sendto_one_numeric(source_p, ERR_NOSUCHNICK,
434 form_str(ERR_NOSUCHNICK), nick);
435 }
436
437 }
438 return (1);
439}
440
441/*
442 * duplicate_ptr
443 *
444 * inputs - pointer to check
445 * - pointer to table of entities
446 * - number of valid entities so far
3c7d6fcc 447 * output - true if duplicate pointer in table, false if not.
212380e3
AC
448 * note, this does the canonize using pointers
449 * side effects - NONE
450 */
3c7d6fcc 451static bool
212380e3
AC
452duplicate_ptr(void *ptr)
453{
454 int i;
455 for(i = 0; i < ntargets; i++)
456 if(targets[i].ptr == ptr)
3c7d6fcc
EM
457 return true;
458 return false;
212380e3
AC
459}
460
461/*
462 * msg_channel
463 *
464 * inputs - flag privmsg or notice
212380e3
AC
465 * - pointer to client_p
466 * - pointer to source_p
467 * - pointer to channel
468 * output - NONE
469 * side effects - message given channel
470 *
471 * XXX - We need to rework this a bit, it's a tad ugly. --nenolod
472 */
473static void
ca4c2a86 474msg_channel(enum message_type msgtype,
212380e3
AC
475 struct Client *client_p, struct Client *source_p, struct Channel *chptr,
476 const char *text)
477{
478 int result;
ca4c2a86 479 hook_data_privmsg_channel hdata;
212380e3
AC
480
481 if(MyClient(source_p))
482 {
483 /* idle time shouldnt be reset by notices --fl */
ca4c2a86 484 if(msgtype != MESSAGE_TYPE_NOTICE)
e3354945 485 source_p->localClient->last = rb_current_time();
212380e3
AC
486 }
487
ca4c2a86
AC
488 hdata.msgtype = msgtype;
489 hdata.source_p = source_p;
490 hdata.chptr = chptr;
491 hdata.text = text;
492 hdata.approved = 0;
493
494 call_hook(h_privmsg_channel, &hdata);
495
496 /* memory buffer address may have changed, update pointer */
497 text = hdata.text;
498
499 if (hdata.approved != 0)
500 return;
501
6eb03360
AC
502 /* hook may have reduced the string to nothing. */
503 if (EmptyString(text))
504 {
505 /* could be empty after colour stripping and
506 * that would cause problems later */
507 if(msgtype != MESSAGE_TYPE_NOTICE)
508 sendto_one(source_p, form_str(ERR_NOTEXTTOSEND), me.name, source_p->name);
509 return;
510 }
511
212380e3
AC
512 /* chanops and voiced can flood their own channel with impunity */
513 if((result = can_send(chptr, source_p, NULL)))
514 {
717238d2 515 if(result != CAN_SEND_OPV && MyClient(source_p) &&
d4f7eb4c 516 !IsOperGeneral(source_p) &&
717238d2
JT
517 !add_channel_target(source_p, chptr))
518 {
519 sendto_one(source_p, form_str(ERR_TARGCHANGE),
520 me.name, source_p->name, chptr->chname);
521 return;
522 }
212380e3 523 if(result == CAN_SEND_OPV ||
ca4c2a86 524 !flood_attack_channel(msgtype, source_p, chptr, chptr->chname))
212380e3
AC
525 {
526 sendto_channel_flags(client_p, ALL_MEMBERS, source_p, chptr,
ca4c2a86 527 "%s %s :%s", cmdname[msgtype], chptr->chname, text);
212380e3
AC
528 }
529 }
530 else if(chptr->mode.mode & MODE_OPMODERATE &&
8feca176
JT
531 (!(chptr->mode.mode & MODE_NOPRIVMSGS) ||
532 IsMember(source_p, chptr)))
212380e3 533 {
d4f7eb4c 534 if(MyClient(source_p) && !IsOperGeneral(source_p) &&
717238d2
JT
535 !add_channel_target(source_p, chptr))
536 {
537 sendto_one(source_p, form_str(ERR_TARGCHANGE),
538 me.name, source_p->name, chptr->chname);
539 return;
540 }
ca4c2a86 541 if(!flood_attack_channel(msgtype, source_p, chptr, chptr->chname))
212380e3 542 {
c4d2d014 543 sendto_channel_opmod(client_p, source_p, chptr,
ca4c2a86 544 cmdname[msgtype], text);
c4d2d014
JT
545 }
546 }
547 else
548 {
ca4c2a86 549 if(msgtype != MESSAGE_TYPE_NOTICE)
c4d2d014
JT
550 sendto_one_numeric(source_p, ERR_CANNOTSENDTOCHAN,
551 form_str(ERR_CANNOTSENDTOCHAN), chptr->chname);
552 }
553}
554/*
555 * msg_channel_opmod
556 *
557 * inputs - flag privmsg or notice
c4d2d014
JT
558 * - pointer to client_p
559 * - pointer to source_p
560 * - pointer to channel
561 * output - NONE
562 * side effects - message given channel ops
563 *
564 * XXX - We need to rework this a bit, it's a tad ugly. --nenolod
565 */
566static void
ca4c2a86 567msg_channel_opmod(enum message_type msgtype,
c4d2d014
JT
568 struct Client *client_p, struct Client *source_p,
569 struct Channel *chptr, const char *text)
570{
ca4c2a86 571 hook_data_privmsg_channel hdata;
c4d2d014 572
ca4c2a86
AC
573 hdata.msgtype = msgtype;
574 hdata.source_p = source_p;
575 hdata.chptr = chptr;
576 hdata.text = text;
577 hdata.approved = 0;
578
579 call_hook(h_privmsg_channel, &hdata);
580
581 /* memory buffer address may have changed, update pointer */
582 text = hdata.text;
583
584 if (hdata.approved != 0)
585 return;
586
6eb03360
AC
587 /* hook may have reduced the string to nothing. */
588 if (EmptyString(text))
589 {
590 /* could be empty after colour stripping and
591 * that would cause problems later */
592 if(msgtype != MESSAGE_TYPE_NOTICE)
593 sendto_one(source_p, form_str(ERR_NOTEXTTOSEND), me.name, source_p->name);
594 return;
595 }
596
c4d2d014
JT
597 if(chptr->mode.mode & MODE_OPMODERATE &&
598 (!(chptr->mode.mode & MODE_NOPRIVMSGS) ||
599 IsMember(source_p, chptr)))
600 {
ca4c2a86 601 if(!flood_attack_channel(msgtype, source_p, chptr, chptr->chname))
c4d2d014
JT
602 {
603 sendto_channel_opmod(client_p, source_p, chptr,
ca4c2a86 604 cmdname[msgtype], text);
212380e3
AC
605 }
606 }
607 else
608 {
ca4c2a86 609 if(msgtype != MESSAGE_TYPE_NOTICE)
212380e3
AC
610 sendto_one_numeric(source_p, ERR_CANNOTSENDTOCHAN,
611 form_str(ERR_CANNOTSENDTOCHAN), chptr->chname);
612 }
613}
614
615/*
616 * msg_channel_flags
617 *
55abcbb2 618 * inputs - flag 0 if PRIVMSG 1 if NOTICE. RFC
212380e3 619 * say NOTICE must not auto reply
212380e3
AC
620 * - pointer to client_p
621 * - pointer to source_p
622 * - pointer to channel
623 * - flags
624 * - pointer to text to send
625 * output - NONE
626 * side effects - message given channel either chanop or voice
627 */
628static void
ca4c2a86 629msg_channel_flags(enum message_type msgtype, struct Client *client_p,
212380e3
AC
630 struct Client *source_p, struct Channel *chptr, int flags, const char *text)
631{
632 int type;
633 char c;
ca4c2a86 634 hook_data_privmsg_channel hdata;
212380e3
AC
635
636 if(flags & CHFL_VOICE)
637 {
638 type = ONLY_CHANOPSVOICED;
639 c = '+';
640 }
641 else
642 {
643 type = ONLY_CHANOPS;
644 c = '@';
645 }
646
647 if(MyClient(source_p))
648 {
649 /* idletime shouldnt be reset by notice --fl */
ca4c2a86 650 if(msgtype != MESSAGE_TYPE_NOTICE)
e3354945 651 source_p->localClient->last = rb_current_time();
212380e3
AC
652 }
653
ca4c2a86
AC
654 hdata.msgtype = msgtype;
655 hdata.source_p = source_p;
656 hdata.chptr = chptr;
657 hdata.text = text;
658 hdata.approved = 0;
659
660 call_hook(h_privmsg_channel, &hdata);
661
662 /* memory buffer address may have changed, update pointer */
663 text = hdata.text;
664
665 if (hdata.approved != 0)
666 return;
667
6eb03360
AC
668 if (EmptyString(text))
669 {
670 /* could be empty after colour stripping and
671 * that would cause problems later */
672 if(msgtype != MESSAGE_TYPE_NOTICE)
673 sendto_one(source_p, form_str(ERR_NOTEXTTOSEND), me.name, source_p->name);
674 return;
675 }
676
212380e3 677 sendto_channel_flags(client_p, type, source_p, chptr, "%s %c%s :%s",
ca4c2a86 678 cmdname[msgtype], c, chptr->chname, text);
212380e3
AC
679}
680
212380e3
AC
681static void
682expire_tgchange(void *unused)
683{
684 tgchange *target;
637c4932 685 rb_dlink_node *ptr, *next_ptr;
212380e3 686
637c4932 687 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, tgchange_list.head)
212380e3
AC
688 {
689 target = ptr->data;
690
e3354945 691 if(target->expiry < rb_current_time())
212380e3 692 {
ba200635
AC
693 rb_dlinkDelete(ptr, &tgchange_list);
694 rb_patricia_remove(tgchange_tree, target->pnode);
bd198292
VY
695 rb_free(target->ip);
696 rb_free(target);
212380e3
AC
697 }
698 }
699}
700
212380e3
AC
701/*
702 * msg_client
703 *
55abcbb2 704 * inputs - flag 0 if PRIVMSG 1 if NOTICE. RFC
212380e3 705 * say NOTICE must not auto reply
212380e3
AC
706 * - pointer to source_p source (struct Client *)
707 * - pointer to target_p target (struct Client *)
708 * - pointer to text
709 * output - NONE
710 * side effects - message given channel either chanop or voice
711 */
712static void
ca4c2a86 713msg_client(enum message_type msgtype,
212380e3
AC
714 struct Client *source_p, struct Client *target_p, const char *text)
715{
c2874388 716 int do_floodcount = 0;
ca4c2a86 717 hook_data_privmsg_user hdata;
c2874388 718
212380e3
AC
719 if(MyClient(source_p))
720 {
55abcbb2 721 /* reset idle time for message only if its not to self
212380e3 722 * and its not a notice */
ca4c2a86 723 if(msgtype != MESSAGE_TYPE_NOTICE)
e3354945 724 source_p->localClient->last = rb_current_time();
212380e3 725
c2874388 726 /* auto cprivmsg/cnotice */
d4f7eb4c 727 do_floodcount = !IsOperGeneral(source_p) &&
c2874388
JT
728 !find_allowing_channel(source_p, target_p);
729
212380e3
AC
730 /* target change stuff, dont limit ctcp replies as that
731 * would allow people to start filling up random users
732 * targets just by ctcping them
733 */
ca4c2a86 734 if((msgtype != MESSAGE_TYPE_NOTICE || *text != '\001') &&
c2874388 735 ConfigFileEntry.target_change && do_floodcount)
212380e3
AC
736 {
737 if(!add_target(source_p, target_p))
738 {
739 sendto_one(source_p, form_str(ERR_TARGCHANGE),
740 me.name, source_p->name, target_p->name);
741 return;
742 }
743 }
b7b1d686 744
ca4c2a86 745 if (do_floodcount && msgtype == MESSAGE_TYPE_NOTICE && *text == '\001' &&
aa9c9ed2
JT
746 target_p->large_ctcp_sent + LARGE_CTCP_TIME >= rb_current_time())
747 do_floodcount = 0;
748
b7b1d686 749 if (do_floodcount &&
ca4c2a86 750 flood_attack_client(msgtype, source_p, target_p))
b7b1d686 751 return;
e2d5ffd5
SA
752
753 if (IsCapable(source_p, CLICAP_ECHO_MESSAGE) && target_p != source_p)
754 sendto_anywhere_echo(target_p, source_p, cmdname[msgtype], ":%s", text);
212380e3
AC
755 }
756 else if(source_p->from == target_p->from)
757 {
758 sendto_realops_snomask(SNO_DEBUG, L_ALL,
759 "Send message to %s[%s] dropped from %s(Fake Dir)",
760 target_p->name, target_p->from->name, source_p->name);
761 return;
762 }
763
ca4c2a86 764 if(MyConnect(source_p) && (msgtype != MESSAGE_TYPE_NOTICE) && target_p->user && target_p->user->away)
212380e3 765 sendto_one_numeric(source_p, RPL_AWAY, form_str(RPL_AWAY),
c127b45b 766 target_p->name, target_p->user->away);
212380e3 767
92f5320e
AC
768 hdata.msgtype = msgtype;
769 hdata.source_p = source_p;
770 hdata.target_p = target_p;
771 hdata.text = text;
772 hdata.approved = 0;
ca4c2a86 773
92f5320e 774 call_hook(h_privmsg_user, &hdata);
ca4c2a86 775
92f5320e
AC
776 /* buffer location may have changed. */
777 text = hdata.text;
ca4c2a86 778
92f5320e
AC
779 if (hdata.approved != 0)
780 return;
ca4c2a86 781
92f5320e
AC
782 if(MyClient(target_p))
783 {
6eb03360
AC
784 if (EmptyString(text))
785 {
786 /* could be empty after colour stripping and
787 * that would cause problems later */
788 if(msgtype != MESSAGE_TYPE_NOTICE)
789 sendto_one(source_p, form_str(ERR_NOTEXTTOSEND), me.name, source_p->name);
790 return;
791 }
792
4a9cf869
AC
793 add_reply_target(target_p, source_p);
794 sendto_anywhere(target_p, source_p, cmdname[msgtype], ":%s", text);
212380e3 795 }
b7b1d686 796 else
ca4c2a86 797 sendto_anywhere(target_p, source_p, cmdname[msgtype], ":%s", text);
212380e3
AC
798
799 return;
800}
801
802/*
803 * flood_attack_client
804 * inputs - flag 0 if PRIVMSG 1 if NOTICE. RFC
3c7d6fcc 805 * says NOTICE must not auto reply
55abcbb2 806 * - pointer to source Client
212380e3 807 * - pointer to target Client
3c7d6fcc 808 * output - true if target is under flood attack
212380e3
AC
809 * side effects - check for flood attack on target target_p
810 */
3c7d6fcc 811static bool
ca4c2a86 812flood_attack_client(enum message_type msgtype, struct Client *source_p, struct Client *target_p)
212380e3
AC
813{
814 int delta;
815
c24efdc0
JT
816 /* Services could get many messages legitimately and
817 * can be messaged without rate limiting via aliases
818 * and msg user@server.
819 * -- jilles
820 */
821 if(GlobalSetOptions.floodcount && IsClient(source_p) && source_p != target_p && !IsService(target_p))
212380e3 822 {
c24efdc0 823 if((target_p->first_received_message_time + 1) < rb_current_time())
212380e3 824 {
c24efdc0
JT
825 delta = rb_current_time() - target_p->first_received_message_time;
826 target_p->received_number_of_privmsgs -= delta;
827 target_p->first_received_message_time = rb_current_time();
828 if(target_p->received_number_of_privmsgs <= 0)
212380e3 829 {
c24efdc0
JT
830 target_p->received_number_of_privmsgs = 0;
831 target_p->flood_noticed = 0;
212380e3
AC
832 }
833 }
834
c24efdc0
JT
835 if((target_p->received_number_of_privmsgs >=
836 GlobalSetOptions.floodcount) || target_p->flood_noticed)
212380e3 837 {
c24efdc0 838 if(target_p->flood_noticed == 0)
212380e3 839 {
f2c1b06b 840 sendto_realops_snomask(SNO_BOTS, L_NETWIDE,
212380e3
AC
841 "Possible Flooder %s[%s@%s] on %s target: %s",
842 source_p->name, source_p->username,
63aecfb9 843 source_p->orighost,
c88cdb00 844 source_p->servptr->name, target_p->name);
c24efdc0 845 target_p->flood_noticed = 1;
212380e3 846 /* add a bit of penalty */
c24efdc0 847 target_p->received_number_of_privmsgs += 2;
212380e3 848 }
ca4c2a86 849 if(MyClient(source_p) && (msgtype != MESSAGE_TYPE_NOTICE))
212380e3
AC
850 sendto_one(source_p,
851 ":%s NOTICE %s :*** Message to %s throttled due to flooding",
852 me.name, source_p->name, target_p->name);
3c7d6fcc 853 return true;
212380e3
AC
854 }
855 else
c24efdc0 856 target_p->received_number_of_privmsgs++;
212380e3
AC
857 }
858
3c7d6fcc 859 return false;
212380e3
AC
860}
861
212380e3
AC
862/*
863 * handle_special
864 *
865 * inputs - server pointer
866 * - client pointer
867 * - nick stuff to grok for opers
868 * - text to send if grok
869 * output - none
870 * side effects - all the traditional oper type messages are parsed here.
871 * i.e. "/msg #some.host."
872 * However, syntax has been changed.
873 * previous syntax "/msg #some.host.mask"
874 * now becomes "/msg $#some.host.mask"
875 * previous syntax of: "/msg $some.server.mask" remains
876 * This disambiguates the syntax.
877 */
878static void
ca4c2a86 879handle_special(enum message_type msgtype, struct Client *client_p,
212380e3
AC
880 struct Client *source_p, const char *nick, const char *text)
881{
882 struct Client *target_p;
212380e3 883 char *server;
212380e3
AC
884
885 /* user[%host]@server addressed?
886 * NOTE: users can send to user@server, but not user%host@server
887 * or opers@server
888 */
889 if((server = strchr(nick, '@')) != NULL)
890 {
891 if((target_p = find_server(source_p, server + 1)) == NULL)
892 {
893 sendto_one_numeric(source_p, ERR_NOSUCHSERVER,
894 form_str(ERR_NOSUCHSERVER), server + 1);
895 return;
896 }
897
212380e3
AC
898 if(!IsOper(source_p))
899 {
900 if(strchr(nick, '%') || (strncmp(nick, "opers", 5) == 0))
901 {
902 sendto_one_numeric(source_p, ERR_NOSUCHNICK,
903 form_str(ERR_NOSUCHNICK), nick);
904 return;
905 }
906 }
907
908 /* somewhere else.. */
909 if(!IsMe(target_p))
910 {
911 sendto_one(target_p, ":%s %s %s :%s",
ca4c2a86 912 get_id(source_p, target_p), cmdname[msgtype], nick, text);
212380e3
AC
913 return;
914 }
915
212380e3 916 /* Check if someones msg'ing opers@our.server */
ef57f7fb 917 if(strncmp(nick, "opers@", 6) == 0)
212380e3
AC
918 {
919 sendto_realops_snomask(SNO_GENERAL, L_ALL, "To opers: From: %s: %s",
920 source_p->name, text);
921 return;
922 }
923
ef57f7fb
JT
924 /* This was not very useful except for bypassing certain
925 * restrictions. Note that we still allow sending to
926 * remote servers this way, for messaging pseudoservers
927 * securely whether they have a service{} block or not.
928 * -- jilles
212380e3 929 */
ef57f7fb
JT
930 sendto_one_numeric(source_p, ERR_NOSUCHNICK,
931 form_str(ERR_NOSUCHNICK), nick);
932 return;
212380e3
AC
933 }
934
935 /*
936 * the following two cases allow masks in NOTICEs
937 * (for OPERs only)
938 *
939 * Armin, 8Jun90 (gruner@informatik.tu-muenchen.de)
940 */
941 if(IsOper(source_p) && *nick == '$')
942 {
943 if((*(nick + 1) == '$' || *(nick + 1) == '#'))
944 nick++;
945 else if(MyOper(source_p))
946 {
947 sendto_one(source_p,
948 ":%s NOTICE %s :The command %s %s is no longer supported, please use $%s",
ca4c2a86 949 me.name, source_p->name, cmdname[msgtype], nick, nick);
212380e3
AC
950 return;
951 }
952
a6f4368b
JT
953 if(MyClient(source_p) && !IsOperMassNotice(source_p))
954 {
955 sendto_one(source_p, form_str(ERR_NOPRIVS),
956 me.name, source_p->name, "mass_notice");
957 return;
958 }
959
f4b4216a
EK
960 if(MyClient(source_p))
961 {
962 sendto_realops_snomask(SNO_GENERAL, L_ALL | L_NETWIDE, "%s sent mass-%s to %s: %s",
963 get_oper_name(source_p),
964 msgtype == MESSAGE_TYPE_PRIVMSG ? "privmsg" : "notice",
965 nick, text);
966 }
967
212380e3
AC
968 sendto_match_butone(IsServer(client_p) ? client_p : NULL, source_p,
969 nick + 1,
970 (*nick == '#') ? MATCH_HOST : MATCH_SERVER,
ca4c2a86
AC
971 "%s $%s :%s", cmdname[msgtype], nick, text);
972 if (msgtype != MESSAGE_TYPE_NOTICE && *text == '\001')
aa9c9ed2 973 source_p->large_ctcp_sent = rb_current_time();
212380e3
AC
974 return;
975 }
976}