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