]>
Commit | Line | Data |
---|---|---|
212380e3 AC |
1 | /* |
2 | * ircd-ratbox: A slightly useful ircd. | |
3 | * m_message.c: Sends a (PRIVMSG|NOTICE) message to a user or channel. | |
4 | * | |
5 | * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center | |
6 | * Copyright (C) 1996-2002 Hybrid Development Team | |
7 | * Copyright (C) 2002-2005 ircd-ratbox development team | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, write to the Free Software | |
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
22 | * USA | |
23 | * | |
63aecfb9 | 24 | * $Id: m_message.c 3173 2007-01-31 23:57:18Z jilles $ |
212380e3 AC |
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" | |
4562c604 | 38 | #include "match.h" |
212380e3 AC |
39 | #include "hash.h" |
40 | #include "class.h" | |
41 | #include "msg.h" | |
42 | #include "packet.h" | |
43 | #include "send.h" | |
212380e3 | 44 | #include "s_newconf.h" |
43f8445d | 45 | #include "s_stats.h" |
4f2685f3 | 46 | #include "tgchange.h" |
ab428518 | 47 | #include "inline/stringops.h" |
212380e3 AC |
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); | |
0e7cb7e6 | 54 | static struct ev_entry *expire_tgchange_event; |
212380e3 AC |
55 | |
56 | static int | |
57 | modinit(void) | |
58 | { | |
0e7cb7e6 | 59 | expire_tgchange_event = rb_event_addish("expire_tgchange", expire_tgchange, NULL, 300); |
212380e3 AC |
60 | expire_tgchange(NULL); |
61 | return 0; | |
62 | } | |
63 | ||
64 | static void | |
65 | moddeinit(void) | |
66 | { | |
0e7cb7e6 | 67 | rb_event_delete(expire_tgchange_event); |
212380e3 AC |
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 | ||
63aecfb9 | 81 | DECLARE_MODULE_AV1(message, modinit, moddeinit, message_clist, NULL, NULL, "$Revision: 3173 $"); |
212380e3 AC |
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); | |
212380e3 | 95 | |
aa9c9ed2 JT |
96 | /* Fifteen seconds should be plenty for a client to reply a ctcp */ |
97 | #define LARGE_CTCP_TIME 15 | |
98 | ||
212380e3 AC |
99 | #define ENTITY_NONE 0 |
100 | #define ENTITY_CHANNEL 1 | |
c4d2d014 JT |
101 | #define ENTITY_CHANNEL_OPMOD 2 |
102 | #define ENTITY_CHANOPS_ON_CHANNEL 3 | |
103 | #define ENTITY_CLIENT 4 | |
212380e3 AC |
104 | |
105 | static struct entity targets[512]; | |
106 | static int ntargets = 0; | |
107 | ||
108 | static int duplicate_ptr(void *); | |
109 | ||
110 | static void msg_channel(int p_or_n, const char *command, | |
111 | struct Client *client_p, | |
112 | struct Client *source_p, struct Channel *chptr, const char *text); | |
113 | ||
c4d2d014 JT |
114 | static void msg_channel_opmod(int p_or_n, const char *command, |
115 | struct Client *client_p, | |
116 | struct Client *source_p, struct Channel *chptr, | |
117 | const char *text); | |
118 | ||
212380e3 AC |
119 | static void msg_channel_flags(int p_or_n, const char *command, |
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(int p_or_n, const char *command, | |
125 | struct Client *source_p, struct Client *target_p, const char *text); | |
126 | ||
127 | static void handle_special(int p_or_n, const char *command, | |
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 | ||
150 | #define PRIVMSG 0 | |
151 | #define NOTICE 1 | |
152 | ||
153 | static int | |
154 | m_privmsg(struct Client *client_p, struct Client *source_p, int parc, const char *parv[]) | |
155 | { | |
156 | return m_message(PRIVMSG, "PRIVMSG", client_p, source_p, parc, parv); | |
157 | } | |
158 | ||
159 | static int | |
160 | m_notice(struct Client *client_p, struct Client *source_p, int parc, const char *parv[]) | |
161 | { | |
162 | return m_message(NOTICE, "NOTICE", client_p, source_p, parc, parv); | |
163 | } | |
164 | ||
165 | /* | |
166 | * inputs - flag privmsg or notice | |
167 | * - pointer to command "PRIVMSG" or "NOTICE" | |
168 | * - pointer to client_p | |
169 | * - pointer to source_p | |
170 | * - pointer to channel | |
171 | */ | |
172 | static int | |
173 | m_message(int p_or_n, | |
174 | const char *command, | |
175 | struct Client *client_p, struct Client *source_p, int parc, const char *parv[]) | |
176 | { | |
177 | int i; | |
178 | ||
179 | if(parc < 2 || EmptyString(parv[1])) | |
180 | { | |
181 | if(p_or_n != NOTICE) | |
182 | sendto_one(source_p, form_str(ERR_NORECIPIENT), me.name, | |
183 | source_p->name, command); | |
184 | return 0; | |
185 | } | |
186 | ||
187 | if(parc < 3 || EmptyString(parv[2])) | |
188 | { | |
189 | if(p_or_n != NOTICE) | |
190 | sendto_one(source_p, form_str(ERR_NOTEXTTOSEND), me.name, source_p->name); | |
191 | return 0; | |
192 | } | |
193 | ||
194 | /* Finish the flood grace period if theyre not messaging themselves | |
195 | * as some clients (ircN) do this as a "lag check" | |
196 | */ | |
197 | if(MyClient(source_p) && !IsFloodDone(source_p) && irccmp(source_p->name, parv[1])) | |
198 | flood_endgrace(source_p); | |
199 | ||
200 | if(build_target_list(p_or_n, command, client_p, source_p, parv[1], parv[2]) < 0) | |
201 | { | |
202 | return 0; | |
203 | } | |
204 | ||
205 | for(i = 0; i < ntargets; i++) | |
206 | { | |
207 | switch (targets[i].type) | |
208 | { | |
209 | case ENTITY_CHANNEL: | |
210 | msg_channel(p_or_n, command, client_p, source_p, | |
211 | (struct Channel *) targets[i].ptr, parv[2]); | |
212 | break; | |
213 | ||
c4d2d014 JT |
214 | case ENTITY_CHANNEL_OPMOD: |
215 | msg_channel_opmod(p_or_n, command, client_p, source_p, | |
216 | (struct Channel *) targets[i].ptr, parv[2]); | |
217 | break; | |
218 | ||
212380e3 AC |
219 | case ENTITY_CHANOPS_ON_CHANNEL: |
220 | msg_channel_flags(p_or_n, command, client_p, source_p, | |
221 | (struct Channel *) targets[i].ptr, | |
222 | targets[i].flags, parv[2]); | |
223 | break; | |
224 | ||
225 | case ENTITY_CLIENT: | |
226 | msg_client(p_or_n, command, source_p, | |
227 | (struct Client *) targets[i].ptr, parv[2]); | |
228 | break; | |
229 | } | |
230 | } | |
231 | ||
232 | return 0; | |
233 | } | |
234 | ||
235 | /* | |
236 | * build_target_list | |
237 | * | |
238 | * inputs - pointer to given client_p (server) | |
239 | * - pointer to given source (oper/client etc.) | |
240 | * - pointer to list of nicks/channels | |
241 | * - pointer to table to place results | |
242 | * - pointer to text (only used if source_p is an oper) | |
243 | * output - number of valid entities | |
244 | * side effects - target_table is modified to contain a list of | |
245 | * pointers to channels or clients | |
246 | * if source client is an oper | |
247 | * all the classic old bizzare oper privmsg tricks | |
248 | * are parsed and sent as is, if prefixed with $ | |
249 | * to disambiguate. | |
250 | * | |
251 | */ | |
252 | ||
253 | static int | |
254 | build_target_list(int p_or_n, const char *command, struct Client *client_p, | |
255 | struct Client *source_p, const char *nicks_channels, const char *text) | |
256 | { | |
257 | int type; | |
258 | char *p, *nick, *target_list; | |
259 | struct Channel *chptr = NULL; | |
260 | struct Client *target_p; | |
261 | ||
262 | target_list = LOCAL_COPY(nicks_channels); /* skip strcpy for non-lazyleafs */ | |
263 | ||
264 | ntargets = 0; | |
265 | ||
4a2651e5 | 266 | for(nick = rb_strtok_r(target_list, ",", &p); nick; nick = rb_strtok_r(NULL, ",", &p)) |
212380e3 AC |
267 | { |
268 | char *with_prefix; | |
269 | /* | |
270 | * channels are privmsg'd a lot more than other clients, moved up | |
271 | * here plain old channel msg? | |
272 | */ | |
273 | ||
274 | if(IsChanPrefix(*nick)) | |
275 | { | |
276 | /* ignore send of local channel to a server (should not happen) */ | |
277 | if(IsServer(client_p) && *nick == '&') | |
278 | continue; | |
279 | ||
280 | if((chptr = find_channel(nick)) != NULL) | |
281 | { | |
282 | if(!duplicate_ptr(chptr)) | |
283 | { | |
284 | if(ntargets >= ConfigFileEntry.max_targets) | |
285 | { | |
286 | sendto_one(source_p, form_str(ERR_TOOMANYTARGETS), | |
287 | me.name, source_p->name, nick); | |
288 | return (1); | |
289 | } | |
290 | targets[ntargets].ptr = (void *) chptr; | |
291 | targets[ntargets++].type = ENTITY_CHANNEL; | |
292 | } | |
293 | } | |
294 | ||
295 | /* non existant channel */ | |
296 | else if(p_or_n != NOTICE) | |
297 | sendto_one_numeric(source_p, ERR_NOSUCHNICK, | |
298 | form_str(ERR_NOSUCHNICK), nick); | |
299 | ||
300 | continue; | |
301 | } | |
302 | ||
303 | if(MyClient(source_p)) | |
304 | target_p = find_named_person(nick); | |
305 | else | |
306 | target_p = find_person(nick); | |
307 | ||
308 | /* look for a privmsg to another client */ | |
309 | if(target_p) | |
310 | { | |
311 | if(!duplicate_ptr(target_p)) | |
312 | { | |
313 | if(ntargets >= ConfigFileEntry.max_targets) | |
314 | { | |
315 | sendto_one(source_p, form_str(ERR_TOOMANYTARGETS), | |
316 | me.name, source_p->name, nick); | |
317 | return (1); | |
318 | } | |
319 | targets[ntargets].ptr = (void *) target_p; | |
320 | targets[ntargets].type = ENTITY_CLIENT; | |
321 | targets[ntargets++].flags = 0; | |
322 | } | |
323 | continue; | |
324 | } | |
325 | ||
326 | /* @#channel or +#channel message ? */ | |
327 | ||
328 | type = 0; | |
329 | with_prefix = nick; | |
330 | /* allow %+@ if someone wants to do that */ | |
331 | for(;;) | |
332 | { | |
333 | if(*nick == '@') | |
334 | type |= CHFL_CHANOP; | |
335 | else if(*nick == '+') | |
336 | type |= CHFL_CHANOP | CHFL_VOICE; | |
337 | else | |
338 | break; | |
339 | nick++; | |
340 | } | |
341 | ||
342 | if(type != 0) | |
343 | { | |
344 | /* no recipient.. */ | |
345 | if(EmptyString(nick)) | |
346 | { | |
347 | sendto_one(source_p, form_str(ERR_NORECIPIENT), | |
348 | me.name, source_p->name, command); | |
349 | continue; | |
350 | } | |
351 | ||
352 | /* At this point, nick+1 should be a channel name i.e. #foo or &foo | |
353 | * if the channel is found, fine, if not report an error | |
354 | */ | |
355 | ||
356 | if((chptr = find_channel(nick)) != NULL) | |
357 | { | |
358 | struct membership *msptr; | |
359 | ||
360 | msptr = find_channel_membership(chptr, source_p); | |
361 | ||
362 | if(!IsServer(source_p) && !IsService(source_p) && !is_chanop_voiced(msptr)) | |
363 | { | |
364 | sendto_one(source_p, form_str(ERR_CHANOPRIVSNEEDED), | |
631b4a54 JT |
365 | get_id(&me, source_p), |
366 | get_id(source_p, source_p), | |
367 | with_prefix); | |
212380e3 AC |
368 | return (-1); |
369 | } | |
370 | ||
371 | if(!duplicate_ptr(chptr)) | |
372 | { | |
373 | if(ntargets >= ConfigFileEntry.max_targets) | |
374 | { | |
375 | sendto_one(source_p, form_str(ERR_TOOMANYTARGETS), | |
376 | me.name, source_p->name, nick); | |
377 | return (1); | |
378 | } | |
379 | targets[ntargets].ptr = (void *) chptr; | |
380 | targets[ntargets].type = ENTITY_CHANOPS_ON_CHANNEL; | |
381 | targets[ntargets++].flags = type; | |
382 | } | |
383 | } | |
384 | else if(p_or_n != NOTICE) | |
385 | { | |
386 | sendto_one_numeric(source_p, ERR_NOSUCHNICK, | |
387 | form_str(ERR_NOSUCHNICK), nick); | |
388 | } | |
389 | ||
390 | continue; | |
391 | } | |
392 | ||
c4d2d014 JT |
393 | if(IsServer(client_p) && *nick == '=' && nick[1] == '#') |
394 | { | |
395 | nick++; | |
396 | if((chptr = find_channel(nick)) != NULL) | |
397 | { | |
398 | if(!duplicate_ptr(chptr)) | |
399 | { | |
400 | if(ntargets >= ConfigFileEntry.max_targets) | |
401 | { | |
402 | sendto_one(source_p, form_str(ERR_TOOMANYTARGETS), | |
403 | me.name, source_p->name, nick); | |
404 | return (1); | |
405 | } | |
406 | targets[ntargets].ptr = (void *) chptr; | |
407 | targets[ntargets++].type = ENTITY_CHANNEL_OPMOD; | |
408 | } | |
409 | } | |
410 | ||
411 | /* non existant channel */ | |
412 | else if(p_or_n != NOTICE) | |
413 | sendto_one_numeric(source_p, ERR_NOSUCHNICK, | |
414 | form_str(ERR_NOSUCHNICK), nick); | |
415 | ||
416 | continue; | |
417 | } | |
418 | ||
1fd171a5 JT |
419 | if(strchr(nick, '@') || (IsOper(source_p) && (*nick == '$'))) |
420 | { | |
421 | handle_special(p_or_n, command, client_p, source_p, nick, text); | |
422 | continue; | |
423 | } | |
424 | ||
212380e3 AC |
425 | /* no matching anything found - error if not NOTICE */ |
426 | if(p_or_n != NOTICE) | |
427 | { | |
428 | /* dont give this numeric when source is local, | |
429 | * because its misleading --anfl | |
430 | */ | |
431 | if(!MyClient(source_p) && IsDigit(*nick)) | |
432 | sendto_one(source_p, ":%s %d %s * :Target left IRC. " | |
433 | "Failed to deliver: [%.20s]", | |
434 | get_id(&me, source_p), ERR_NOSUCHNICK, | |
435 | get_id(source_p, source_p), text); | |
436 | else | |
437 | sendto_one_numeric(source_p, ERR_NOSUCHNICK, | |
438 | form_str(ERR_NOSUCHNICK), nick); | |
439 | } | |
440 | ||
441 | } | |
442 | return (1); | |
443 | } | |
444 | ||
445 | /* | |
446 | * duplicate_ptr | |
447 | * | |
448 | * inputs - pointer to check | |
449 | * - pointer to table of entities | |
450 | * - number of valid entities so far | |
451 | * output - YES if duplicate pointer in table, NO if not. | |
452 | * note, this does the canonize using pointers | |
453 | * side effects - NONE | |
454 | */ | |
455 | static int | |
456 | duplicate_ptr(void *ptr) | |
457 | { | |
458 | int i; | |
459 | for(i = 0; i < ntargets; i++) | |
460 | if(targets[i].ptr == ptr) | |
461 | return YES; | |
462 | return NO; | |
463 | } | |
464 | ||
465 | /* | |
466 | * msg_channel | |
467 | * | |
468 | * inputs - flag privmsg or notice | |
469 | * - pointer to command "PRIVMSG" or "NOTICE" | |
470 | * - pointer to client_p | |
471 | * - pointer to source_p | |
472 | * - pointer to channel | |
473 | * output - NONE | |
474 | * side effects - message given channel | |
475 | * | |
476 | * XXX - We need to rework this a bit, it's a tad ugly. --nenolod | |
477 | */ | |
478 | static void | |
479 | msg_channel(int p_or_n, const char *command, | |
480 | struct Client *client_p, struct Client *source_p, struct Channel *chptr, | |
481 | const char *text) | |
482 | { | |
483 | int result; | |
484 | char text2[BUFSIZE]; | |
485 | ||
486 | if(MyClient(source_p)) | |
487 | { | |
488 | /* idle time shouldnt be reset by notices --fl */ | |
489 | if(p_or_n != NOTICE) | |
e3354945 | 490 | source_p->localClient->last = rb_current_time(); |
212380e3 AC |
491 | } |
492 | ||
493 | if(chptr->mode.mode & MODE_NOCOLOR) | |
494 | { | |
f427c8b0 | 495 | rb_strlcpy(text2, text, BUFSIZE); |
212380e3 AC |
496 | strip_colour(text2); |
497 | text = text2; | |
498 | if (EmptyString(text)) | |
499 | { | |
500 | /* could be empty after colour stripping and | |
501 | * that would cause problems later */ | |
502 | if(p_or_n != NOTICE) | |
503 | sendto_one(source_p, form_str(ERR_NOTEXTTOSEND), me.name, source_p->name); | |
504 | return; | |
505 | } | |
506 | } | |
507 | ||
508 | /* chanops and voiced can flood their own channel with impunity */ | |
509 | if((result = can_send(chptr, source_p, NULL))) | |
510 | { | |
717238d2 JT |
511 | if(result != CAN_SEND_OPV && MyClient(source_p) && |
512 | !IsOper(source_p) && | |
513 | !add_channel_target(source_p, chptr)) | |
514 | { | |
515 | sendto_one(source_p, form_str(ERR_TARGCHANGE), | |
516 | me.name, source_p->name, chptr->chname); | |
517 | return; | |
518 | } | |
212380e3 AC |
519 | if(result == CAN_SEND_OPV || |
520 | !flood_attack_channel(p_or_n, source_p, chptr, chptr->chname)) | |
521 | { | |
641eb2c3 | 522 | if (p_or_n != NOTICE && *text == '\001' && |
1f679bfe | 523 | strncasecmp(text + 1, "ACTION ", 7)) |
2e918bf5 | 524 | { |
641eb2c3 | 525 | if (chptr->mode.mode & MODE_NOCTCP) |
2e918bf5 AC |
526 | { |
527 | sendto_one_numeric(source_p, ERR_CANNOTSENDTOCHAN, | |
528 | form_str(ERR_CANNOTSENDTOCHAN), chptr->chname); | |
529 | return; | |
530 | } | |
531 | else if (rb_dlink_list_length(&chptr->locmembers) > (unsigned)(GlobalSetOptions.floodcount / 2)) | |
532 | source_p->large_ctcp_sent = rb_current_time(); | |
533 | } | |
212380e3 AC |
534 | sendto_channel_flags(client_p, ALL_MEMBERS, source_p, chptr, |
535 | "%s %s :%s", command, chptr->chname, text); | |
536 | } | |
537 | } | |
538 | else if(chptr->mode.mode & MODE_OPMODERATE && | |
8feca176 JT |
539 | (!(chptr->mode.mode & MODE_NOPRIVMSGS) || |
540 | IsMember(source_p, chptr))) | |
212380e3 | 541 | { |
717238d2 JT |
542 | if(MyClient(source_p) && !IsOper(source_p) && |
543 | !add_channel_target(source_p, chptr)) | |
544 | { | |
545 | sendto_one(source_p, form_str(ERR_TARGCHANGE), | |
546 | me.name, source_p->name, chptr->chname); | |
547 | return; | |
548 | } | |
212380e3 AC |
549 | if(!flood_attack_channel(p_or_n, source_p, chptr, chptr->chname)) |
550 | { | |
c4d2d014 JT |
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); | |
212380e3 AC |
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) | |
e3354945 | 651 | source_p->localClient->last = rb_current_time(); |
212380e3 AC |
652 | } |
653 | ||
654 | sendto_channel_flags(client_p, type, source_p, chptr, "%s %c%s :%s", | |
655 | command, c, chptr->chname, text); | |
656 | } | |
657 | ||
212380e3 AC |
658 | static void |
659 | expire_tgchange(void *unused) | |
660 | { | |
661 | tgchange *target; | |
637c4932 | 662 | rb_dlink_node *ptr, *next_ptr; |
212380e3 | 663 | |
637c4932 | 664 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, tgchange_list.head) |
212380e3 AC |
665 | { |
666 | target = ptr->data; | |
667 | ||
e3354945 | 668 | if(target->expiry < rb_current_time()) |
212380e3 | 669 | { |
ba200635 AC |
670 | rb_dlinkDelete(ptr, &tgchange_list); |
671 | rb_patricia_remove(tgchange_tree, target->pnode); | |
bd198292 VY |
672 | rb_free(target->ip); |
673 | rb_free(target); | |
212380e3 AC |
674 | } |
675 | } | |
676 | } | |
677 | ||
212380e3 AC |
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 | { | |
c2874388 JT |
694 | int do_floodcount = 0; |
695 | ||
212380e3 AC |
696 | if(MyClient(source_p)) |
697 | { | |
f5455d2c JT |
698 | /* |
699 | * XXX: Controversial? Allow target users to send replies | |
700 | * through a +g. Rationale is that people can presently use +g | |
701 | * as a way to taunt users, e.g. harass them and hide behind +g | |
702 | * as a way of griefing. --nenolod | |
703 | */ | |
dd9b78dc | 704 | if(p_or_n != NOTICE && IsSetCallerId(source_p) && |
f5455d2c JT |
705 | !accept_message(target_p, source_p) && |
706 | !IsOper(target_p)) | |
707 | { | |
708 | if(rb_dlink_list_length(&source_p->localClient->allow_list) < | |
709 | ConfigFileEntry.max_accept) | |
710 | { | |
711 | rb_dlinkAddAlloc(target_p, &source_p->localClient->allow_list); | |
712 | rb_dlinkAddAlloc(source_p, &target_p->on_allow_list); | |
713 | } | |
714 | else | |
715 | { | |
716 | sendto_one_numeric(source_p, ERR_OWNMODE, | |
717 | form_str(ERR_OWNMODE), | |
718 | target_p->name, "+g"); | |
719 | return; | |
720 | } | |
721 | } | |
722 | ||
212380e3 AC |
723 | /* reset idle time for message only if its not to self |
724 | * and its not a notice */ | |
725 | if(p_or_n != NOTICE) | |
e3354945 | 726 | source_p->localClient->last = rb_current_time(); |
212380e3 | 727 | |
c2874388 JT |
728 | /* auto cprivmsg/cnotice */ |
729 | do_floodcount = !IsOper(source_p) && | |
730 | !find_allowing_channel(source_p, target_p); | |
731 | ||
212380e3 AC |
732 | /* target change stuff, dont limit ctcp replies as that |
733 | * would allow people to start filling up random users | |
734 | * targets just by ctcping them | |
735 | */ | |
736 | if((p_or_n != NOTICE || *text != '\001') && | |
c2874388 | 737 | ConfigFileEntry.target_change && do_floodcount) |
212380e3 AC |
738 | { |
739 | if(!add_target(source_p, target_p)) | |
740 | { | |
741 | sendto_one(source_p, form_str(ERR_TARGCHANGE), | |
742 | me.name, source_p->name, target_p->name); | |
743 | return; | |
744 | } | |
745 | } | |
b7b1d686 | 746 | |
aa9c9ed2 JT |
747 | if (do_floodcount && p_or_n == NOTICE && *text == '\001' && |
748 | target_p->large_ctcp_sent + LARGE_CTCP_TIME >= rb_current_time()) | |
749 | do_floodcount = 0; | |
750 | ||
b7b1d686 JT |
751 | if (do_floodcount && |
752 | flood_attack_client(p_or_n, source_p, target_p)) | |
753 | return; | |
212380e3 AC |
754 | } |
755 | else if(source_p->from == target_p->from) | |
756 | { | |
757 | sendto_realops_snomask(SNO_DEBUG, L_ALL, | |
758 | "Send message to %s[%s] dropped from %s(Fake Dir)", | |
759 | target_p->name, target_p->from->name, source_p->name); | |
760 | return; | |
761 | } | |
762 | ||
c127b45b | 763 | if(MyConnect(source_p) && (p_or_n != NOTICE) && target_p->user && target_p->user->away) |
212380e3 | 764 | sendto_one_numeric(source_p, RPL_AWAY, form_str(RPL_AWAY), |
c127b45b | 765 | target_p->name, target_p->user->away); |
212380e3 AC |
766 | |
767 | if(MyClient(target_p)) | |
768 | { | |
769 | /* XXX Controversial? allow opers always to send through a +g */ | |
770 | if(!IsServer(source_p) && (IsSetCallerId(target_p) || | |
771 | (IsSetRegOnlyMsg(target_p) && !source_p->user->suser[0]))) | |
772 | { | |
773 | /* Here is the anti-flood bot/spambot code -db */ | |
774 | if(accept_message(source_p, target_p) || IsOper(source_p)) | |
775 | { | |
c9f01c4f | 776 | add_reply_target(target_p, source_p); |
212380e3 AC |
777 | sendto_one(target_p, ":%s!%s@%s %s %s :%s", |
778 | source_p->name, | |
779 | source_p->username, | |
780 | source_p->host, command, target_p->name, text); | |
781 | } | |
782 | else if (IsSetRegOnlyMsg(target_p) && !source_p->user->suser[0]) | |
783 | { | |
784 | if (p_or_n != NOTICE) | |
785 | sendto_one_numeric(source_p, ERR_NONONREG, | |
786 | form_str(ERR_NONONREG), | |
787 | target_p->name); | |
212380e3 AC |
788 | } |
789 | else | |
790 | { | |
791 | /* check for accept, flag recipient incoming message */ | |
792 | if(p_or_n != NOTICE) | |
793 | { | |
794 | sendto_one_numeric(source_p, ERR_TARGUMODEG, | |
795 | form_str(ERR_TARGUMODEG), | |
796 | target_p->name); | |
797 | } | |
798 | ||
799 | if((target_p->localClient->last_caller_id_time + | |
e3354945 | 800 | ConfigFileEntry.caller_id_wait) < rb_current_time()) |
212380e3 AC |
801 | { |
802 | if(p_or_n != NOTICE) | |
803 | sendto_one_numeric(source_p, RPL_TARGNOTIFY, | |
804 | form_str(RPL_TARGNOTIFY), | |
805 | target_p->name); | |
806 | ||
c9f01c4f | 807 | add_reply_target(target_p, source_p); |
212380e3 AC |
808 | sendto_one(target_p, form_str(RPL_UMODEGMSG), |
809 | me.name, target_p->name, source_p->name, | |
810 | source_p->username, source_p->host); | |
811 | ||
e3354945 | 812 | target_p->localClient->last_caller_id_time = rb_current_time(); |
212380e3 | 813 | } |
212380e3 AC |
814 | } |
815 | } | |
816 | else | |
c9f01c4f JT |
817 | { |
818 | add_reply_target(target_p, source_p); | |
b7b1d686 | 819 | sendto_anywhere(target_p, source_p, command, ":%s", text); |
c9f01c4f | 820 | } |
212380e3 | 821 | } |
b7b1d686 | 822 | else |
212380e3 AC |
823 | sendto_anywhere(target_p, source_p, command, ":%s", text); |
824 | ||
825 | return; | |
826 | } | |
827 | ||
828 | /* | |
829 | * flood_attack_client | |
830 | * inputs - flag 0 if PRIVMSG 1 if NOTICE. RFC | |
831 | * say NOTICE must not auto reply | |
832 | * - pointer to source Client | |
833 | * - pointer to target Client | |
834 | * output - 1 if target is under flood attack | |
835 | * side effects - check for flood attack on target target_p | |
836 | */ | |
837 | static int | |
838 | flood_attack_client(int p_or_n, struct Client *source_p, struct Client *target_p) | |
839 | { | |
840 | int delta; | |
841 | ||
c24efdc0 JT |
842 | /* Services could get many messages legitimately and |
843 | * can be messaged without rate limiting via aliases | |
844 | * and msg user@server. | |
845 | * -- jilles | |
846 | */ | |
847 | if(GlobalSetOptions.floodcount && IsClient(source_p) && source_p != target_p && !IsService(target_p)) | |
212380e3 | 848 | { |
c24efdc0 | 849 | if((target_p->first_received_message_time + 1) < rb_current_time()) |
212380e3 | 850 | { |
c24efdc0 JT |
851 | delta = rb_current_time() - target_p->first_received_message_time; |
852 | target_p->received_number_of_privmsgs -= delta; | |
853 | target_p->first_received_message_time = rb_current_time(); | |
854 | if(target_p->received_number_of_privmsgs <= 0) | |
212380e3 | 855 | { |
c24efdc0 JT |
856 | target_p->received_number_of_privmsgs = 0; |
857 | target_p->flood_noticed = 0; | |
212380e3 AC |
858 | } |
859 | } | |
860 | ||
c24efdc0 JT |
861 | if((target_p->received_number_of_privmsgs >= |
862 | GlobalSetOptions.floodcount) || target_p->flood_noticed) | |
212380e3 | 863 | { |
c24efdc0 | 864 | if(target_p->flood_noticed == 0) |
212380e3 | 865 | { |
f2c1b06b | 866 | sendto_realops_snomask(SNO_BOTS, L_NETWIDE, |
212380e3 AC |
867 | "Possible Flooder %s[%s@%s] on %s target: %s", |
868 | source_p->name, source_p->username, | |
63aecfb9 | 869 | source_p->orighost, |
c88cdb00 | 870 | source_p->servptr->name, target_p->name); |
c24efdc0 | 871 | target_p->flood_noticed = 1; |
212380e3 | 872 | /* add a bit of penalty */ |
c24efdc0 | 873 | target_p->received_number_of_privmsgs += 2; |
212380e3 AC |
874 | } |
875 | if(MyClient(source_p) && (p_or_n != NOTICE)) | |
876 | sendto_one(source_p, | |
877 | ":%s NOTICE %s :*** Message to %s throttled due to flooding", | |
878 | me.name, source_p->name, target_p->name); | |
879 | return 1; | |
880 | } | |
881 | else | |
c24efdc0 | 882 | target_p->received_number_of_privmsgs++; |
212380e3 AC |
883 | } |
884 | ||
885 | return 0; | |
886 | } | |
887 | ||
212380e3 AC |
888 | /* |
889 | * handle_special | |
890 | * | |
891 | * inputs - server pointer | |
892 | * - client pointer | |
893 | * - nick stuff to grok for opers | |
894 | * - text to send if grok | |
895 | * output - none | |
896 | * side effects - all the traditional oper type messages are parsed here. | |
897 | * i.e. "/msg #some.host." | |
898 | * However, syntax has been changed. | |
899 | * previous syntax "/msg #some.host.mask" | |
900 | * now becomes "/msg $#some.host.mask" | |
901 | * previous syntax of: "/msg $some.server.mask" remains | |
902 | * This disambiguates the syntax. | |
903 | */ | |
904 | static void | |
905 | handle_special(int p_or_n, const char *command, struct Client *client_p, | |
906 | struct Client *source_p, const char *nick, const char *text) | |
907 | { | |
908 | struct Client *target_p; | |
212380e3 AC |
909 | char *server; |
910 | char *s; | |
212380e3 AC |
911 | |
912 | /* user[%host]@server addressed? | |
913 | * NOTE: users can send to user@server, but not user%host@server | |
914 | * or opers@server | |
915 | */ | |
916 | if((server = strchr(nick, '@')) != NULL) | |
917 | { | |
918 | if((target_p = find_server(source_p, server + 1)) == NULL) | |
919 | { | |
920 | sendto_one_numeric(source_p, ERR_NOSUCHSERVER, | |
921 | form_str(ERR_NOSUCHSERVER), server + 1); | |
922 | return; | |
923 | } | |
924 | ||
212380e3 AC |
925 | if(!IsOper(source_p)) |
926 | { | |
927 | if(strchr(nick, '%') || (strncmp(nick, "opers", 5) == 0)) | |
928 | { | |
929 | sendto_one_numeric(source_p, ERR_NOSUCHNICK, | |
930 | form_str(ERR_NOSUCHNICK), nick); | |
931 | return; | |
932 | } | |
933 | } | |
934 | ||
935 | /* somewhere else.. */ | |
936 | if(!IsMe(target_p)) | |
937 | { | |
938 | sendto_one(target_p, ":%s %s %s :%s", | |
939 | get_id(source_p, target_p), command, nick, text); | |
940 | return; | |
941 | } | |
942 | ||
212380e3 | 943 | /* Check if someones msg'ing opers@our.server */ |
ef57f7fb | 944 | if(strncmp(nick, "opers@", 6) == 0) |
212380e3 AC |
945 | { |
946 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "To opers: From: %s: %s", | |
947 | source_p->name, text); | |
948 | return; | |
949 | } | |
950 | ||
ef57f7fb JT |
951 | /* This was not very useful except for bypassing certain |
952 | * restrictions. Note that we still allow sending to | |
953 | * remote servers this way, for messaging pseudoservers | |
954 | * securely whether they have a service{} block or not. | |
955 | * -- jilles | |
212380e3 | 956 | */ |
ef57f7fb JT |
957 | sendto_one_numeric(source_p, ERR_NOSUCHNICK, |
958 | form_str(ERR_NOSUCHNICK), nick); | |
959 | return; | |
212380e3 AC |
960 | } |
961 | ||
962 | /* | |
963 | * the following two cases allow masks in NOTICEs | |
964 | * (for OPERs only) | |
965 | * | |
966 | * Armin, 8Jun90 (gruner@informatik.tu-muenchen.de) | |
967 | */ | |
968 | if(IsOper(source_p) && *nick == '$') | |
969 | { | |
970 | if((*(nick + 1) == '$' || *(nick + 1) == '#')) | |
971 | nick++; | |
972 | else if(MyOper(source_p)) | |
973 | { | |
974 | sendto_one(source_p, | |
975 | ":%s NOTICE %s :The command %s %s is no longer supported, please use $%s", | |
976 | me.name, source_p->name, command, nick, nick); | |
977 | return; | |
978 | } | |
979 | ||
a6f4368b JT |
980 | if(MyClient(source_p) && !IsOperMassNotice(source_p)) |
981 | { | |
982 | sendto_one(source_p, form_str(ERR_NOPRIVS), | |
983 | me.name, source_p->name, "mass_notice"); | |
984 | return; | |
985 | } | |
986 | ||
212380e3 AC |
987 | if((s = strrchr(nick, '.')) == NULL) |
988 | { | |
989 | sendto_one_numeric(source_p, ERR_NOTOPLEVEL, | |
990 | form_str(ERR_NOTOPLEVEL), nick); | |
991 | return; | |
992 | } | |
993 | while(*++s) | |
994 | if(*s == '.' || *s == '*' || *s == '?') | |
995 | break; | |
996 | if(*s == '*' || *s == '?') | |
997 | { | |
998 | sendto_one_numeric(source_p, ERR_WILDTOPLEVEL, | |
999 | form_str(ERR_WILDTOPLEVEL), nick); | |
1000 | return; | |
1001 | } | |
1002 | ||
1003 | sendto_match_butone(IsServer(client_p) ? client_p : NULL, source_p, | |
1004 | nick + 1, | |
1005 | (*nick == '#') ? MATCH_HOST : MATCH_SERVER, | |
1006 | "%s $%s :%s", command, nick, text); | |
aa9c9ed2 JT |
1007 | if (p_or_n != NOTICE && *text == '\001') |
1008 | source_p->large_ctcp_sent = rb_current_time(); | |
212380e3 AC |
1009 | return; |
1010 | } | |
1011 | } |