2 * charybdis: an advanced Internet Relay Chat Daemon(ircd).
4 * Copyright (C) 2006 charybdis development team
17 #include "s_newconf.h"
21 #define HURT_CUTOFF (10) /* protocol messages. */
22 #define HURT_DEFAULT_EXPIRE (7 * 24 * 60) /* minutes. */
23 #define HURT_EXIT_REASON "Hurt: Failed to identify to services"
30 typedef struct _hurt_state
{
33 rb_dlink_list hurt_clients
;
35 time_t default_expire
;
36 const char *exit_reason
;
39 typedef struct _hurt
{
41 struct sockaddr
*saddr
;
49 static int mo_hurt(struct Client
*, struct Client
*, int, const char **);
50 static int me_hurt(struct Client
*, struct Client
*, int, const char **);
51 static int mo_heal(struct Client
*, struct Client
*, int, const char **);
52 static int me_heal(struct Client
*, struct Client
*, int, const char **);
54 static int modinit(void);
55 static void modfini(void);
57 static void client_exit_hook(hook_data_client_exit
*);
58 static void new_local_user_hook(struct Client
*);
59 static void doing_stats_hook(hook_data_int
*hdata
);
61 static void hurt_check_event(void *);
62 static void hurt_expire_event(void *);
64 static hurt_t
*hurt_new(time_t, const char *, const char *);
65 static void hurt_add(hurt_t
*);
66 static void hurt_propagate(struct Client
*, struct Client
*, hurt_t
*);
67 static hurt_t
*hurt_find(const char *ip
);
68 static hurt_t
*hurt_find_exact(const char *ip
);
69 static void hurt_remove(const char *ip
);
70 static void hurt_destroy(void *hurt
);
72 static int heal_nick(struct Client
*, struct Client
*);
74 static int nick_is_valid(const char *);
77 /* {{{ State containers */
79 rb_dlink_list hurt_confs
= { NULL
, NULL
, 0 };
84 struct Message hurt_msgtab
= {
85 "HURT", 0, 0, 0, MFLG_SLOW
, {
86 mg_ignore
, mg_ignore
, mg_ignore
,
87 mg_ignore
, {me_hurt
, 0}, {mo_hurt
, 3}
91 struct Message heal_msgtab
= {
92 "HEAL", 0, 0, 0, MFLG_SLOW
, {
93 mg_ignore
, mg_ignore
, mg_ignore
,
94 mg_ignore
, {me_heal
, 0}, {mo_heal
, 2}
99 /* {{{ Misc module stuff */
100 mapi_hfn_list_av1 hurt_hfnlist
[] = {
101 {"client_exit", (hookfn
) client_exit_hook
},
102 {"new_local_user", (hookfn
) new_local_user_hook
},
103 {"doing_stats", (hookfn
) doing_stats_hook
},
107 mapi_clist_av1 hurt_clist
[] = { &hurt_msgtab
, &heal_msgtab
, NULL
};
120 hurt_state_t hurt_state
= {
121 .cutoff
= HURT_CUTOFF
,
122 .default_expire
= HURT_DEFAULT_EXPIRE
,
123 .exit_reason
= HURT_EXIT_REASON
,
127 * Module constructor/destructor.
130 /* {{{ static int modinit() */
132 struct ev_entry
*hurt_expire_ev
= NULL
;
133 struct ev_entry
*hurt_check_ev
= NULL
;
138 /* set-up hurt_state. */
139 hurt_state
.start_time
= rb_current_time();
141 /* add our event handlers. */
142 hurt_expire_ev
= rb_event_add("hurt_expire", hurt_expire_event
, NULL
, 60);
143 hurt_check_ev
= rb_event_add("hurt_check", hurt_check_event
, NULL
, 5);
149 /* {{{ static void modfini() */
153 rb_dlink_node
*ptr
, *next_ptr
;
155 /* and delete our events. */
156 rb_event_delete(hurt_expire_ev
);
157 rb_event_delete(hurt_check_ev
);
159 RB_DLINK_FOREACH_SAFE (ptr
, next_ptr
, hurt_state
.hurt_clients
.head
)
161 rb_dlinkDestroy(ptr
, &hurt_state
.hurt_clients
);
170 /* {{{ static int mo_hurt()
172 * HURT [<expire>] <ip> <reason>
174 * parv[1] - expire or ip
175 * parv[2] - ip or reason
176 * parv[3] - reason or NULL
179 mo_hurt(struct Client
*client_p
, struct Client
*source_p
,
180 int parc
, const char **parv
)
182 const char *ip
, *expire
, *reason
;
185 struct Client
*target_p
;
187 if (!IsOperK(source_p
)) {
188 sendto_one(source_p
, form_str(ERR_NOPRIVS
), me
.name
,
189 source_p
->name
, "kline");
194 expire
= NULL
, ip
= parv
[1], reason
= parv
[2];
196 expire
= parv
[1], ip
= parv
[2], reason
= parv
[3];
199 expire_time
= HURT_DEFAULT_EXPIRE
;
200 if (expire
&& (expire_time
= valid_temp_time(expire
)) < 1) {
201 sendto_one_notice(source_p
, ":Permanent HURTs are not supported");
204 if (EmptyString(reason
)) {
205 sendto_one_notice(source_p
, ":Empty HURT reasons are bad for business");
209 /* Is this a client? */
210 if (strchr(ip
, '.') == NULL
&& strchr(ip
, ':') == NULL
)
212 target_p
= find_named_person(ip
);
213 if (target_p
== NULL
)
215 sendto_one_numeric(source_p
, ERR_NOSUCHNICK
,
216 form_str(ERR_NOSUCHNICK
), ip
);
219 ip
= target_p
->orighost
;
223 if (!strncmp(ip
, "*@", 2))
225 if (strchr(ip
, '!') || strchr(ip
, '@'))
227 sendto_one_notice(source_p
, ":Invalid HURT mask [%s]",
233 if (hurt_find(ip
) != NULL
) {
234 sendto_one(source_p
, ":[%s] already HURT", ip
);
239 * okay, we've got this far, now it's time to add the the HURT locally
240 * and propagate it to other servers on the network.
242 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
243 "%s added HURT on [%s] for %ld minutes with reason [%s]",
244 get_oper_name(source_p
), ip
, (long) expire_time
/ 60, reason
);
246 hurt
= hurt_new(expire_time
, ip
, reason
);
248 hurt_propagate(NULL
, source_p
, hurt
);
254 /* {{{ static int me_hurt()
256 * [ENCAP mask] HURT <target> <expire> <ip> <reason>
263 me_hurt(struct Client
*client_p
, struct Client
*source_p
,
264 int parc
, const char **parv
)
270 * right... if we don't get enough arguments, or if we get any invalid
271 * arguments, just ignore this request - shit happens, and it's not worth
272 * dropping a server over.
274 if (parc
< 4 || !IsPerson(source_p
))
276 if ((expire_time
= atoi(parv
[1])) < 1)
278 if (hurt_find(parv
[2]) != NULL
)
280 if (EmptyString(parv
[3]))
283 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
284 "%s added HURT on [%s] for %ld minutes with reason [%s]",
285 get_oper_name(source_p
), parv
[2], (long) expire_time
/ 60, parv
[3]);
286 hurt
= hurt_new(expire_time
, parv
[2], parv
[3]);
293 /* {{{ static int mo_heal()
297 * parv[1] - nick or ip
300 mo_heal(struct Client
*client_p
, struct Client
*source_p
,
301 int parc
, const char **parv
)
303 struct Client
*target_p
;
305 if (!IsOperUnkline(source_p
))
307 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
308 me
.name
, source_p
->name
, "unkline");
312 if (nick_is_valid(parv
[1]))
314 target_p
= find_named_person(parv
[1]);
315 if (target_p
== NULL
)
317 sendto_one_numeric(source_p
, ERR_NOSUCHNICK
,
318 form_str(ERR_NOSUCHNICK
), parv
[1]);
321 if (MyConnect(target_p
))
322 heal_nick(source_p
, target_p
);
324 sendto_one(target_p
, ":%s ENCAP %s HEAL %s",
325 get_id(source_p
, target_p
),
326 target_p
->servptr
->name
,
327 get_id(target_p
, target_p
));
329 else if (strchr(parv
[1], '.'))
331 if (hurt_find_exact(parv
[1]) == NULL
)
333 sendto_one_notice(source_p
, ":Mask [%s] is not HURT", parv
[1]);
336 hurt_remove(parv
[1]);
337 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "%s removed HURT on %s",
338 get_oper_name(source_p
), parv
[1]);
339 sendto_server(NULL
, NULL
, NOCAPS
, NOCAPS
, ":%s ENCAP * HEAL %s",
340 source_p
->name
, parv
[1]);
344 sendto_one(source_p
, ":[%s] is not a valid IP address/nick", parv
[1]);
353 me_heal(struct Client
*client_p
, struct Client
*source_p
,
354 int parc
, const char **parv
)
356 struct Client
*target_p
;
358 /* as noted in me_hurt(), if we don't get sufficient arguments...
359 * *poof*, it's dropped...
364 if (nick_is_valid(parv
[1]))
366 target_p
= find_person(parv
[1]);
367 if (target_p
!= NULL
&& MyConnect(target_p
))
368 heal_nick(source_p
, target_p
);
370 else if (strchr(parv
[1], '.')) /* host or mask to remove ban for */
372 if (hurt_find_exact(parv
[1]) == NULL
)
375 hurt_remove(parv
[1]);
376 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "%s removed HURT on %s",
377 get_oper_name(source_p
), parv
[1]);
389 /* {{{ static void hurt_check_event() */
391 hurt_check_event(void *arg
)
393 rb_dlink_node
*ptr
, *next_ptr
;
394 struct Client
*client_p
;
396 RB_DLINK_FOREACH_SAFE (ptr
, next_ptr
, hurt_state
.hurt_clients
.head
) {
397 client_p
= ptr
->data
;
398 if (!EmptyString(client_p
->user
->suser
))
400 rb_dlinkDestroy(ptr
, &hurt_state
.hurt_clients
);
401 sendto_one_notice(client_p
, ":HURT restriction removed for this session");
402 client_p
->localClient
->target_last
= rb_current_time(); /* don't ask --nenolod */
404 else if (client_p
->localClient
->receiveM
> hurt_state
.cutoff
)
405 exit_client(NULL
, client_p
, &me
, hurt_state
.exit_reason
);
410 /* {{{ static void hurt_expire_event() */
412 hurt_expire_event(void *unused
)
414 rb_dlink_node
*ptr
, *next_ptr
;
417 RB_DLINK_FOREACH_SAFE (ptr
, next_ptr
, hurt_confs
.head
)
419 hurt
= (hurt_t
*) ptr
->data
;
421 if (hurt
->expire
<= rb_current_time())
423 rb_dlinkFindDestroy(hurt
, &hurt_confs
);
434 /* {{{ static void client_exit_hook() */
436 client_exit_hook(hook_data_client_exit
*data
)
438 s_assert(data
!= NULL
);
439 s_assert(data
->target
!= NULL
);
441 rb_dlinkFindDestroy(data
->target
, &hurt_state
.hurt_clients
);
445 /* {{{ static void new_local_user_hook() */
447 new_local_user_hook(struct Client
*source_p
)
449 if (IsAnyDead(source_p
) || !EmptyString(source_p
->user
->suser
) ||
450 IsExemptKline(source_p
))
453 if (hurt_find(source_p
->sockhost
) || hurt_find(source_p
->orighost
))
455 source_p
->localClient
->target_last
= rb_current_time() + 600; /* don't ask --nenolod */
456 SetTGChange(source_p
);
457 rb_dlinkAddAlloc(source_p
, &hurt_state
.hurt_clients
);
458 sendto_one_notice(source_p
, ":You are hurt. Please identify to services immediately, or use /stats p for assistance.");
463 /* {{{ static void doing_stats_hook() */
465 doing_stats_hook(hook_data_int
*hdata
)
469 struct Client
*source_p
;
472 s_assert(hdata
->client
);
474 source_p
= hdata
->client
;
475 if(hdata
->arg2
!= (int) 's')
477 if((ConfigFileEntry
.stats_k_oper_only
== 2) && !IsOper(source_p
))
479 if ((ConfigFileEntry
.stats_k_oper_only
== 1) && !IsOper(source_p
))
481 hurt
= hurt_find(source_p
->sockhost
);
484 sendto_one_numeric(source_p
, RPL_STATSKLINE
,
485 form_str(RPL_STATSKLINE
), 's',
486 "*", hurt
->ip
, hurt
->reason
, "", "");
490 hurt
= hurt_find(source_p
->orighost
);
493 sendto_one_numeric(source_p
, RPL_STATSKLINE
,
494 form_str(RPL_STATSKLINE
), 's',
495 "*", hurt
->ip
, hurt
->reason
, "", "");
501 RB_DLINK_FOREACH(ptr
, hurt_confs
.head
)
503 hurt
= (hurt_t
*) ptr
->data
;
504 sendto_one_numeric(source_p
, RPL_STATSKLINE
,
505 form_str(RPL_STATSKLINE
), 's',
506 "*", hurt
->ip
, hurt
->reason
, "", "");
511 /* {{{ static void hurt_propagate()
513 * client_p - specific server to propagate HURT to, or NULL to propagate to all
515 * source_p - source (oper who added the HURT)
516 * hurt - HURT to be propagated
519 hurt_propagate(struct Client
*client_p
, struct Client
*source_p
, hurt_t
*hurt
)
523 ":%s ENCAP %s HURT %ld %s :%s",
524 source_p
->name
, client_p
->name
,
525 (long)(hurt
->expire
- rb_current_time()),
526 hurt
->ip
, hurt
->reason
);
528 sendto_server(&me
, NULL
, NOCAPS
, NOCAPS
,
529 ":%s ENCAP * HURT %ld %s :%s",
531 (long)(hurt
->expire
- rb_current_time()),
532 hurt
->ip
, hurt
->reason
);
536 /* {{{ static hurt_t *hurt_new() */
538 hurt_new(time_t expire
, const char *ip
, const char *reason
)
542 hurt
= rb_malloc(sizeof(hurt_t
));
544 hurt
->ip
= rb_strdup(ip
);
545 hurt
->reason
= rb_strdup(reason
);
546 hurt
->expire
= rb_current_time() + expire
;
552 /* {{{ static void hurt_destroy() */
554 hurt_destroy(void *hurt
)
569 hurt_add(hurt_t
*hurt
)
571 rb_dlinkAddAlloc(hurt
, &hurt_confs
);
575 hurt_find_exact(const char *ip
)
580 RB_DLINK_FOREACH(ptr
, hurt_confs
.head
)
582 hurt
= (hurt_t
*) ptr
->data
;
584 if (!strcasecmp(ip
, hurt
->ip
))
592 hurt_find(const char *ip
)
597 RB_DLINK_FOREACH(ptr
, hurt_confs
.head
)
599 hurt
= (hurt_t
*) ptr
->data
;
601 if (match(hurt
->ip
, ip
))
609 hurt_remove(const char *ip
)
611 hurt_t
*hurt
= hurt_find_exact(ip
);
613 rb_dlinkFindDestroy(hurt
, &hurt_confs
);
617 /* {{{ static int heal_nick() */
619 heal_nick(struct Client
*source_p
, struct Client
*target_p
)
621 if (rb_dlinkFindDestroy(target_p
, &hurt_state
.hurt_clients
))
623 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "%s used HEAL on %s",
624 get_oper_name(source_p
), get_client_name(target_p
, HIDE_IP
));
625 sendto_one_notice(target_p
, ":HURT restriction temporarily removed by operator");
626 sendto_one_notice(source_p
, ":HURT restriction on %s temporarily removed", target_p
->name
);
627 target_p
->localClient
->target_last
= rb_current_time(); /* don't ask --nenolod */
632 sendto_one_notice(source_p
, ":%s was not hurt", target_p
->name
);
642 /* {{{ static int nick_is_valid() */
644 nick_is_valid(const char *nick
)
646 const char *s
= nick
;
648 for (; *s
!= '\0'; s
++) {
658 * vim: ts=8 sw=8 noet fdm=marker tw=80