]> jfr.im git - solanum.git/blob - modules/core/m_message.c
Merge pull request #334 from edk0/massnotice
[solanum.git] / modules / core / m_message.c
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
23 */
24
25 #include "stdinc.h"
26 #include "client.h"
27 #include "ircd.h"
28 #include "numeric.h"
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"
35 #include "match.h"
36 #include "hash.h"
37 #include "class.h"
38 #include "msg.h"
39 #include "packet.h"
40 #include "send.h"
41 #include "s_newconf.h"
42 #include "s_stats.h"
43 #include "tgchange.h"
44 #include "inline/stringops.h"
45
46 static const char message_desc[] =
47 "Provides the PRIVMSG and NOTICE commands to send messages to users and channels";
48
49 static void m_message(enum message_type, struct MsgBuf *, struct Client *, struct Client *, int, const char **);
50 static void m_privmsg(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
51 static void m_notice(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
52
53 static void expire_tgchange(void *unused);
54 static struct ev_entry *expire_tgchange_event;
55
56 static int
57 modinit(void)
58 {
59 expire_tgchange_event = rb_event_addish("expire_tgchange", expire_tgchange, NULL, 300);
60 expire_tgchange(NULL);
61 return 0;
62 }
63
64 static void
65 moddeinit(void)
66 {
67 rb_event_delete(expire_tgchange_event);
68 }
69
70 struct Message privmsg_msgtab = {
71 "PRIVMSG", 0, 0, 0, 0,
72 {mg_unreg, {m_privmsg, 0}, {m_privmsg, 0}, mg_ignore, mg_ignore, {m_privmsg, 0}}
73 };
74 struct Message notice_msgtab = {
75 "NOTICE", 0, 0, 0, 0,
76 {mg_unreg, {m_notice, 0}, {m_notice, 0}, {m_notice, 0}, mg_ignore, {m_notice, 0}}
77 };
78
79 mapi_clist_av1 message_clist[] = { &privmsg_msgtab, &notice_msgtab, NULL };
80
81 DECLARE_MODULE_AV2(message, modinit, moddeinit, message_clist, NULL, NULL, NULL, NULL, message_desc);
82
83 struct entity
84 {
85 void *ptr;
86 int type;
87 int flags;
88 };
89
90 static int build_target_list(enum message_type msgtype,
91 struct Client *client_p,
92 struct Client *source_p, const char *nicks_channels, const char *text);
93
94 static bool flood_attack_client(enum message_type msgtype, struct Client *source_p, struct Client *target_p);
95
96 /* Fifteen seconds should be plenty for a client to reply a ctcp */
97 #define LARGE_CTCP_TIME 15
98
99 #define ENTITY_NONE 0
100 #define ENTITY_CHANNEL 1
101 #define ENTITY_CHANNEL_OPMOD 2
102 #define ENTITY_CHANOPS_ON_CHANNEL 3
103 #define ENTITY_CLIENT 4
104
105 static struct entity targets[512];
106 static int ntargets = 0;
107
108 static bool duplicate_ptr(void *);
109
110 static void msg_channel(enum message_type msgtype,
111 struct Client *client_p,
112 struct Client *source_p, struct Channel *chptr, const char *text);
113
114 static void msg_channel_opmod(enum message_type msgtype,
115 struct Client *client_p,
116 struct Client *source_p, struct Channel *chptr,
117 const char *text);
118
119 static void msg_channel_flags(enum message_type msgtype,
120 struct Client *client_p,
121 struct Client *source_p,
122 struct Channel *chptr, int flags, const char *text);
123
124 static void msg_client(enum message_type msgtype,
125 struct Client *source_p, struct Client *target_p, const char *text);
126
127 static void handle_special(enum message_type msgtype,
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 */
149 const char *cmdname[MESSAGE_TYPE_COUNT] = {
150 [MESSAGE_TYPE_PRIVMSG] = "PRIVMSG",
151 [MESSAGE_TYPE_NOTICE] = "NOTICE",
152 };
153
154 static void
155 m_privmsg(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
156 {
157 m_message(MESSAGE_TYPE_PRIVMSG, msgbuf_p, client_p, source_p, parc, parv);
158 }
159
160 static void
161 m_notice(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
162 {
163 m_message(MESSAGE_TYPE_NOTICE, msgbuf_p, client_p, source_p, parc, parv);
164 }
165
166 /*
167 * inputs - flag privmsg or notice
168 * - pointer to client_p
169 * - pointer to source_p
170 * - pointer to channel
171 */
172 static void
173 m_message(enum message_type msgtype, struct MsgBuf *msgbuf_p,
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 {
180 if(msgtype != MESSAGE_TYPE_NOTICE)
181 sendto_one(source_p, form_str(ERR_NORECIPIENT), me.name,
182 source_p->name, cmdname[msgtype]);
183 return;
184 }
185
186 if(parc < 3 || EmptyString(parv[2]))
187 {
188 if(msgtype != MESSAGE_TYPE_NOTICE)
189 sendto_one(source_p, form_str(ERR_NOTEXTTOSEND), me.name, source_p->name);
190 return;
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
199 if(build_target_list(msgtype, client_p, source_p, parv[1], parv[2]) < 0)
200 {
201 return;
202 }
203
204 for(i = 0; i < ntargets; i++)
205 {
206 switch (targets[i].type)
207 {
208 case ENTITY_CHANNEL:
209 msg_channel(msgtype, client_p, source_p,
210 (struct Channel *) targets[i].ptr, parv[2]);
211 break;
212
213 case ENTITY_CHANNEL_OPMOD:
214 msg_channel_opmod(msgtype, client_p, source_p,
215 (struct Channel *) targets[i].ptr, parv[2]);
216 break;
217
218 case ENTITY_CHANOPS_ON_CHANNEL:
219 msg_channel_flags(msgtype, client_p, source_p,
220 (struct Channel *) targets[i].ptr,
221 targets[i].flags, parv[2]);
222 break;
223
224 case ENTITY_CLIENT:
225 msg_client(msgtype, source_p,
226 (struct Client *) targets[i].ptr, parv[2]);
227 break;
228 }
229 }
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.
247 */
248
249 static int
250 build_target_list(enum message_type msgtype, struct Client *client_p,
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
262 for(nick = rb_strtok_r(target_list, ",", &p); nick; nick = rb_strtok_r(NULL, ",", &p))
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 */
292 else if(msgtype != MESSAGE_TYPE_NOTICE)
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),
344 me.name, source_p->name, cmdname[msgtype]);
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),
361 get_id(&me, source_p),
362 get_id(source_p, source_p),
363 with_prefix);
364 continue;
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 }
380 else if(msgtype != MESSAGE_TYPE_NOTICE)
381 {
382 sendto_one_numeric(source_p, ERR_NOSUCHNICK,
383 form_str(ERR_NOSUCHNICK), nick);
384 }
385
386 continue;
387 }
388
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 */
408 else if(msgtype != MESSAGE_TYPE_NOTICE)
409 sendto_one_numeric(source_p, ERR_NOSUCHNICK,
410 form_str(ERR_NOSUCHNICK), nick);
411
412 continue;
413 }
414
415 if(strchr(nick, '@') || (IsOper(source_p) && (*nick == '$')))
416 {
417 handle_special(msgtype, client_p, source_p, nick, text);
418 continue;
419 }
420
421 /* no matching anything found - error if not NOTICE */
422 if(msgtype != MESSAGE_TYPE_NOTICE)
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
447 * output - true if duplicate pointer in table, false if not.
448 * note, this does the canonize using pointers
449 * side effects - NONE
450 */
451 static bool
452 duplicate_ptr(void *ptr)
453 {
454 int i;
455 for(i = 0; i < ntargets; i++)
456 if(targets[i].ptr == ptr)
457 return true;
458 return false;
459 }
460
461 /*
462 * msg_channel
463 *
464 * inputs - flag privmsg or notice
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 */
473 static void
474 msg_channel(enum message_type msgtype,
475 struct Client *client_p, struct Client *source_p, struct Channel *chptr,
476 const char *text)
477 {
478 int result;
479 hook_data_privmsg_channel hdata;
480
481 if(MyClient(source_p))
482 {
483 /* idle time shouldnt be reset by notices --fl */
484 if(msgtype != MESSAGE_TYPE_NOTICE)
485 source_p->localClient->last = rb_current_time();
486 }
487
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
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
512 /* chanops and voiced can flood their own channel with impunity */
513 if((result = can_send(chptr, source_p, NULL)))
514 {
515 if(result != CAN_SEND_OPV && MyClient(source_p) &&
516 !IsOper(source_p) &&
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 }
523 if(result == CAN_SEND_OPV ||
524 !flood_attack_channel(msgtype, source_p, chptr, chptr->chname))
525 {
526 sendto_channel_flags(client_p, ALL_MEMBERS, source_p, chptr,
527 "%s %s :%s", cmdname[msgtype], chptr->chname, text);
528 }
529 }
530 else if(chptr->mode.mode & MODE_OPMODERATE &&
531 (!(chptr->mode.mode & MODE_NOPRIVMSGS) ||
532 IsMember(source_p, chptr)))
533 {
534 if(MyClient(source_p) && !IsOper(source_p) &&
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 }
541 if(!flood_attack_channel(msgtype, source_p, chptr, chptr->chname))
542 {
543 sendto_channel_opmod(client_p, source_p, chptr,
544 cmdname[msgtype], text);
545 }
546 }
547 else
548 {
549 if(msgtype != MESSAGE_TYPE_NOTICE)
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
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 */
566 static void
567 msg_channel_opmod(enum message_type msgtype,
568 struct Client *client_p, struct Client *source_p,
569 struct Channel *chptr, const char *text)
570 {
571 hook_data_privmsg_channel hdata;
572
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
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
597 if(chptr->mode.mode & MODE_OPMODERATE &&
598 (!(chptr->mode.mode & MODE_NOPRIVMSGS) ||
599 IsMember(source_p, chptr)))
600 {
601 if(!flood_attack_channel(msgtype, source_p, chptr, chptr->chname))
602 {
603 sendto_channel_opmod(client_p, source_p, chptr,
604 cmdname[msgtype], text);
605 }
606 }
607 else
608 {
609 if(msgtype != MESSAGE_TYPE_NOTICE)
610 sendto_one_numeric(source_p, ERR_CANNOTSENDTOCHAN,
611 form_str(ERR_CANNOTSENDTOCHAN), chptr->chname);
612 }
613 }
614
615 /*
616 * msg_channel_flags
617 *
618 * inputs - flag 0 if PRIVMSG 1 if NOTICE. RFC
619 * say NOTICE must not auto reply
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 */
628 static void
629 msg_channel_flags(enum message_type msgtype, struct Client *client_p,
630 struct Client *source_p, struct Channel *chptr, int flags, const char *text)
631 {
632 int type;
633 char c;
634 hook_data_privmsg_channel hdata;
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 */
650 if(msgtype != MESSAGE_TYPE_NOTICE)
651 source_p->localClient->last = rb_current_time();
652 }
653
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
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
677 sendto_channel_flags(client_p, type, source_p, chptr, "%s %c%s :%s",
678 cmdname[msgtype], c, chptr->chname, text);
679 }
680
681 static void
682 expire_tgchange(void *unused)
683 {
684 tgchange *target;
685 rb_dlink_node *ptr, *next_ptr;
686
687 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, tgchange_list.head)
688 {
689 target = ptr->data;
690
691 if(target->expiry < rb_current_time())
692 {
693 rb_dlinkDelete(ptr, &tgchange_list);
694 rb_patricia_remove(tgchange_tree, target->pnode);
695 rb_free(target->ip);
696 rb_free(target);
697 }
698 }
699 }
700
701 /*
702 * msg_client
703 *
704 * inputs - flag 0 if PRIVMSG 1 if NOTICE. RFC
705 * say NOTICE must not auto reply
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 */
712 static void
713 msg_client(enum message_type msgtype,
714 struct Client *source_p, struct Client *target_p, const char *text)
715 {
716 int do_floodcount = 0;
717 hook_data_privmsg_user hdata;
718
719 if(MyClient(source_p))
720 {
721 /*
722 * XXX: Controversial? Allow target users to send replies
723 * through a +g. Rationale is that people can presently use +g
724 * as a way to taunt users, e.g. harass them and hide behind +g
725 * as a way of griefing. --nenolod
726 */
727 if(msgtype != MESSAGE_TYPE_NOTICE &&
728 (IsSetCallerId(source_p) ||
729 (IsSetRegOnlyMsg(source_p) && !target_p->user->suser[0])) &&
730 !accept_message(target_p, source_p) &&
731 !IsOper(target_p))
732 {
733 if(rb_dlink_list_length(&source_p->localClient->allow_list) <
734 (unsigned long)ConfigFileEntry.max_accept)
735 {
736 rb_dlinkAddAlloc(target_p, &source_p->localClient->allow_list);
737 rb_dlinkAddAlloc(source_p, &target_p->on_allow_list);
738 }
739 else
740 {
741 sendto_one_numeric(source_p, ERR_OWNMODE,
742 form_str(ERR_OWNMODE),
743 target_p->name, "+g");
744 return;
745 }
746 }
747
748 /* reset idle time for message only if its not to self
749 * and its not a notice */
750 if(msgtype != MESSAGE_TYPE_NOTICE)
751 source_p->localClient->last = rb_current_time();
752
753 /* auto cprivmsg/cnotice */
754 do_floodcount = !IsOper(source_p) &&
755 !find_allowing_channel(source_p, target_p);
756
757 /* target change stuff, dont limit ctcp replies as that
758 * would allow people to start filling up random users
759 * targets just by ctcping them
760 */
761 if((msgtype != MESSAGE_TYPE_NOTICE || *text != '\001') &&
762 ConfigFileEntry.target_change && do_floodcount)
763 {
764 if(!add_target(source_p, target_p))
765 {
766 sendto_one(source_p, form_str(ERR_TARGCHANGE),
767 me.name, source_p->name, target_p->name);
768 return;
769 }
770 }
771
772 if (do_floodcount && msgtype == MESSAGE_TYPE_NOTICE && *text == '\001' &&
773 target_p->large_ctcp_sent + LARGE_CTCP_TIME >= rb_current_time())
774 do_floodcount = 0;
775
776 if (do_floodcount &&
777 flood_attack_client(msgtype, source_p, target_p))
778 return;
779
780 if (IsCapable(source_p, CLICAP_ECHO_MESSAGE) && target_p != source_p)
781 sendto_anywhere_echo(target_p, source_p, cmdname[msgtype], ":%s", text);
782 }
783 else if(source_p->from == target_p->from)
784 {
785 sendto_realops_snomask(SNO_DEBUG, L_ALL,
786 "Send message to %s[%s] dropped from %s(Fake Dir)",
787 target_p->name, target_p->from->name, source_p->name);
788 return;
789 }
790
791 if(MyConnect(source_p) && (msgtype != MESSAGE_TYPE_NOTICE) && target_p->user && target_p->user->away)
792 sendto_one_numeric(source_p, RPL_AWAY, form_str(RPL_AWAY),
793 target_p->name, target_p->user->away);
794
795 if(MyClient(target_p))
796 {
797 hdata.msgtype = msgtype;
798 hdata.source_p = source_p;
799 hdata.target_p = target_p;
800 hdata.text = text;
801 hdata.approved = 0;
802
803 call_hook(h_privmsg_user, &hdata);
804
805 /* buffer location may have changed. */
806 text = hdata.text;
807
808 if (hdata.approved != 0)
809 return;
810
811 if (EmptyString(text))
812 {
813 /* could be empty after colour stripping and
814 * that would cause problems later */
815 if(msgtype != MESSAGE_TYPE_NOTICE)
816 sendto_one(source_p, form_str(ERR_NOTEXTTOSEND), me.name, source_p->name);
817 return;
818 }
819
820 /* XXX Controversial? allow opers always to send through a +g */
821 if(!IsServer(source_p) && (IsSetCallerId(target_p) ||
822 (IsSetRegOnlyMsg(target_p) && !source_p->user->suser[0])))
823 {
824 /* Here is the anti-flood bot/spambot code -db */
825 if(accept_message(source_p, target_p) || IsOper(source_p))
826 {
827 add_reply_target(target_p, source_p);
828 sendto_one(target_p, ":%s!%s@%s %s %s :%s",
829 source_p->name,
830 source_p->username,
831 source_p->host, cmdname[msgtype], target_p->name, text);
832 }
833 else if (IsSetRegOnlyMsg(target_p) && !source_p->user->suser[0])
834 {
835 if (msgtype != MESSAGE_TYPE_NOTICE)
836 sendto_one_numeric(source_p, ERR_NONONREG,
837 form_str(ERR_NONONREG),
838 target_p->name);
839 }
840 else
841 {
842 /* check for accept, flag recipient incoming message */
843 if(msgtype != MESSAGE_TYPE_NOTICE)
844 {
845 sendto_one_numeric(source_p, ERR_TARGUMODEG,
846 form_str(ERR_TARGUMODEG),
847 target_p->name);
848 }
849
850 if((target_p->localClient->last_caller_id_time +
851 ConfigFileEntry.caller_id_wait) < rb_current_time())
852 {
853 if(msgtype != MESSAGE_TYPE_NOTICE)
854 sendto_one_numeric(source_p, RPL_TARGNOTIFY,
855 form_str(RPL_TARGNOTIFY),
856 target_p->name);
857
858 add_reply_target(target_p, source_p);
859 sendto_one(target_p, form_str(RPL_UMODEGMSG),
860 me.name, target_p->name, source_p->name,
861 source_p->username, source_p->host);
862
863 target_p->localClient->last_caller_id_time = rb_current_time();
864 }
865 }
866 }
867 else
868 {
869 add_reply_target(target_p, source_p);
870 sendto_anywhere(target_p, source_p, cmdname[msgtype], ":%s", text);
871 }
872 }
873 else
874 sendto_anywhere(target_p, source_p, cmdname[msgtype], ":%s", text);
875
876 return;
877 }
878
879 /*
880 * flood_attack_client
881 * inputs - flag 0 if PRIVMSG 1 if NOTICE. RFC
882 * says NOTICE must not auto reply
883 * - pointer to source Client
884 * - pointer to target Client
885 * output - true if target is under flood attack
886 * side effects - check for flood attack on target target_p
887 */
888 static bool
889 flood_attack_client(enum message_type msgtype, struct Client *source_p, struct Client *target_p)
890 {
891 int delta;
892
893 /* Services could get many messages legitimately and
894 * can be messaged without rate limiting via aliases
895 * and msg user@server.
896 * -- jilles
897 */
898 if(GlobalSetOptions.floodcount && IsClient(source_p) && source_p != target_p && !IsService(target_p))
899 {
900 if((target_p->first_received_message_time + 1) < rb_current_time())
901 {
902 delta = rb_current_time() - target_p->first_received_message_time;
903 target_p->received_number_of_privmsgs -= delta;
904 target_p->first_received_message_time = rb_current_time();
905 if(target_p->received_number_of_privmsgs <= 0)
906 {
907 target_p->received_number_of_privmsgs = 0;
908 target_p->flood_noticed = 0;
909 }
910 }
911
912 if((target_p->received_number_of_privmsgs >=
913 GlobalSetOptions.floodcount) || target_p->flood_noticed)
914 {
915 if(target_p->flood_noticed == 0)
916 {
917 sendto_realops_snomask(SNO_BOTS, L_NETWIDE,
918 "Possible Flooder %s[%s@%s] on %s target: %s",
919 source_p->name, source_p->username,
920 source_p->orighost,
921 source_p->servptr->name, target_p->name);
922 target_p->flood_noticed = 1;
923 /* add a bit of penalty */
924 target_p->received_number_of_privmsgs += 2;
925 }
926 if(MyClient(source_p) && (msgtype != MESSAGE_TYPE_NOTICE))
927 sendto_one(source_p,
928 ":%s NOTICE %s :*** Message to %s throttled due to flooding",
929 me.name, source_p->name, target_p->name);
930 return true;
931 }
932 else
933 target_p->received_number_of_privmsgs++;
934 }
935
936 return false;
937 }
938
939 /*
940 * handle_special
941 *
942 * inputs - server pointer
943 * - client pointer
944 * - nick stuff to grok for opers
945 * - text to send if grok
946 * output - none
947 * side effects - all the traditional oper type messages are parsed here.
948 * i.e. "/msg #some.host."
949 * However, syntax has been changed.
950 * previous syntax "/msg #some.host.mask"
951 * now becomes "/msg $#some.host.mask"
952 * previous syntax of: "/msg $some.server.mask" remains
953 * This disambiguates the syntax.
954 */
955 static void
956 handle_special(enum message_type msgtype, struct Client *client_p,
957 struct Client *source_p, const char *nick, const char *text)
958 {
959 struct Client *target_p;
960 char *server;
961
962 /* user[%host]@server addressed?
963 * NOTE: users can send to user@server, but not user%host@server
964 * or opers@server
965 */
966 if((server = strchr(nick, '@')) != NULL)
967 {
968 if((target_p = find_server(source_p, server + 1)) == NULL)
969 {
970 sendto_one_numeric(source_p, ERR_NOSUCHSERVER,
971 form_str(ERR_NOSUCHSERVER), server + 1);
972 return;
973 }
974
975 if(!IsOper(source_p))
976 {
977 if(strchr(nick, '%') || (strncmp(nick, "opers", 5) == 0))
978 {
979 sendto_one_numeric(source_p, ERR_NOSUCHNICK,
980 form_str(ERR_NOSUCHNICK), nick);
981 return;
982 }
983 }
984
985 /* somewhere else.. */
986 if(!IsMe(target_p))
987 {
988 sendto_one(target_p, ":%s %s %s :%s",
989 get_id(source_p, target_p), cmdname[msgtype], nick, text);
990 return;
991 }
992
993 /* Check if someones msg'ing opers@our.server */
994 if(strncmp(nick, "opers@", 6) == 0)
995 {
996 sendto_realops_snomask(SNO_GENERAL, L_ALL, "To opers: From: %s: %s",
997 source_p->name, text);
998 return;
999 }
1000
1001 /* This was not very useful except for bypassing certain
1002 * restrictions. Note that we still allow sending to
1003 * remote servers this way, for messaging pseudoservers
1004 * securely whether they have a service{} block or not.
1005 * -- jilles
1006 */
1007 sendto_one_numeric(source_p, ERR_NOSUCHNICK,
1008 form_str(ERR_NOSUCHNICK), nick);
1009 return;
1010 }
1011
1012 /*
1013 * the following two cases allow masks in NOTICEs
1014 * (for OPERs only)
1015 *
1016 * Armin, 8Jun90 (gruner@informatik.tu-muenchen.de)
1017 */
1018 if(IsOper(source_p) && *nick == '$')
1019 {
1020 if((*(nick + 1) == '$' || *(nick + 1) == '#'))
1021 nick++;
1022 else if(MyOper(source_p))
1023 {
1024 sendto_one(source_p,
1025 ":%s NOTICE %s :The command %s %s is no longer supported, please use $%s",
1026 me.name, source_p->name, cmdname[msgtype], nick, nick);
1027 return;
1028 }
1029
1030 if(MyClient(source_p) && !IsOperMassNotice(source_p))
1031 {
1032 sendto_one(source_p, form_str(ERR_NOPRIVS),
1033 me.name, source_p->name, "mass_notice");
1034 return;
1035 }
1036
1037 sendto_match_butone(IsServer(client_p) ? client_p : NULL, source_p,
1038 nick + 1,
1039 (*nick == '#') ? MATCH_HOST : MATCH_SERVER,
1040 "%s $%s :%s", cmdname[msgtype], nick, text);
1041 if (msgtype != MESSAGE_TYPE_NOTICE && *text == '\001')
1042 source_p->large_ctcp_sent = rb_current_time();
1043 return;
1044 }
1045 }