]>
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 | ||
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, ¬ice_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 | !IsOperGeneral(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) && !IsOperGeneral(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 | /* reset idle time for message only if its not to self | |
722 | * and its not a notice */ | |
723 | if(msgtype != MESSAGE_TYPE_NOTICE) | |
724 | source_p->localClient->last = rb_current_time(); | |
725 | ||
726 | /* auto cprivmsg/cnotice */ | |
727 | do_floodcount = !IsOperGeneral(source_p) && | |
728 | !find_allowing_channel(source_p, target_p); | |
729 | ||
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 | */ | |
734 | if((msgtype != MESSAGE_TYPE_NOTICE || *text != '\001') && | |
735 | ConfigFileEntry.target_change && do_floodcount) | |
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 | } | |
744 | ||
745 | if (do_floodcount && msgtype == MESSAGE_TYPE_NOTICE && *text == '\001' && | |
746 | target_p->large_ctcp_sent + LARGE_CTCP_TIME >= rb_current_time()) | |
747 | do_floodcount = 0; | |
748 | ||
749 | if (do_floodcount && | |
750 | flood_attack_client(msgtype, source_p, target_p)) | |
751 | return; | |
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); | |
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 | ||
764 | if(MyConnect(source_p) && (msgtype != MESSAGE_TYPE_NOTICE) && target_p->user && target_p->user->away) | |
765 | sendto_one_numeric(source_p, RPL_AWAY, form_str(RPL_AWAY), | |
766 | target_p->name, target_p->user->away); | |
767 | ||
768 | hdata.msgtype = msgtype; | |
769 | hdata.source_p = source_p; | |
770 | hdata.target_p = target_p; | |
771 | hdata.text = text; | |
772 | hdata.approved = 0; | |
773 | ||
774 | call_hook(h_privmsg_user, &hdata); | |
775 | ||
776 | /* buffer location may have changed. */ | |
777 | text = hdata.text; | |
778 | ||
779 | if (hdata.approved != 0) | |
780 | return; | |
781 | ||
782 | if(MyClient(target_p)) | |
783 | { | |
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 | ||
793 | add_reply_target(target_p, source_p); | |
794 | sendto_anywhere(target_p, source_p, cmdname[msgtype], ":%s", text); | |
795 | } | |
796 | else | |
797 | sendto_anywhere(target_p, source_p, cmdname[msgtype], ":%s", text); | |
798 | ||
799 | return; | |
800 | } | |
801 | ||
802 | /* | |
803 | * flood_attack_client | |
804 | * inputs - flag 0 if PRIVMSG 1 if NOTICE. RFC | |
805 | * says NOTICE must not auto reply | |
806 | * - pointer to source Client | |
807 | * - pointer to target Client | |
808 | * output - true if target is under flood attack | |
809 | * side effects - check for flood attack on target target_p | |
810 | */ | |
811 | static bool | |
812 | flood_attack_client(enum message_type msgtype, struct Client *source_p, struct Client *target_p) | |
813 | { | |
814 | int delta; | |
815 | ||
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)) | |
822 | { | |
823 | if((target_p->first_received_message_time + 1) < rb_current_time()) | |
824 | { | |
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) | |
829 | { | |
830 | target_p->received_number_of_privmsgs = 0; | |
831 | target_p->flood_noticed = 0; | |
832 | } | |
833 | } | |
834 | ||
835 | if((target_p->received_number_of_privmsgs >= | |
836 | GlobalSetOptions.floodcount) || target_p->flood_noticed) | |
837 | { | |
838 | if(target_p->flood_noticed == 0) | |
839 | { | |
840 | sendto_realops_snomask(SNO_BOTS, L_NETWIDE, | |
841 | "Possible Flooder %s[%s@%s] on %s target: %s", | |
842 | source_p->name, source_p->username, | |
843 | source_p->orighost, | |
844 | source_p->servptr->name, target_p->name); | |
845 | target_p->flood_noticed = 1; | |
846 | /* add a bit of penalty */ | |
847 | target_p->received_number_of_privmsgs += 2; | |
848 | } | |
849 | if(MyClient(source_p) && (msgtype != MESSAGE_TYPE_NOTICE)) | |
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); | |
853 | return true; | |
854 | } | |
855 | else | |
856 | target_p->received_number_of_privmsgs++; | |
857 | } | |
858 | ||
859 | return false; | |
860 | } | |
861 | ||
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 | */ | |
878 | static void | |
879 | handle_special(enum message_type msgtype, struct Client *client_p, | |
880 | struct Client *source_p, const char *nick, const char *text) | |
881 | { | |
882 | struct Client *target_p; | |
883 | char *server; | |
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 | ||
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", | |
912 | get_id(source_p, target_p), cmdname[msgtype], nick, text); | |
913 | return; | |
914 | } | |
915 | ||
916 | /* Check if someones msg'ing opers@our.server */ | |
917 | if(strncmp(nick, "opers@", 6) == 0) | |
918 | { | |
919 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "To opers: From: %s: %s", | |
920 | source_p->name, text); | |
921 | return; | |
922 | } | |
923 | ||
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 | |
929 | */ | |
930 | sendto_one_numeric(source_p, ERR_NOSUCHNICK, | |
931 | form_str(ERR_NOSUCHNICK), nick); | |
932 | return; | |
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", | |
949 | me.name, source_p->name, cmdname[msgtype], nick, nick); | |
950 | return; | |
951 | } | |
952 | ||
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 | ||
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 | ||
968 | sendto_match_butone(IsServer(client_p) ? client_p : NULL, source_p, | |
969 | nick + 1, | |
970 | (*nick == '#') ? MATCH_HOST : MATCH_SERVER, | |
971 | "%s $%s :%s", cmdname[msgtype], nick, text); | |
972 | if (msgtype != MESSAGE_TYPE_NOTICE && *text == '\001') | |
973 | source_p->large_ctcp_sent = rb_current_time(); | |
974 | return; | |
975 | } | |
976 | } |