]>
jfr.im git - irc/evilnet/x3.git/blob - src/shun.c
1 /* shun.c - Shun database
2 * Copyright 2000-2004 srvx Development Team
4 * This file is part of x3.
6 * x3 is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with srvx; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
28 #ifdef HAVE_SYS_SOCKET_H
29 #include <sys/socket.h>
31 #ifdef HAVE_NETINET_IN_H
32 #include <netinet/in.h>
34 #ifdef HAVE_ARPA_INET_H
35 #include <arpa/inet.h>
41 #define KEY_REASON "reason"
42 #define KEY_EXPIRES "expires"
43 #define KEY_ISSUER "issuer"
44 #define KEY_ISSUED "issued"
46 static heap_t shun_heap
; /* key: expiry time, data: struct shun_entry* */
47 static dict_t shun_dict
; /* key: target, data: struct shun_entry* */
50 shun_comparator(const void *a
, const void *b
)
52 const struct shun
*ga
=a
, *gb
=b
;
53 return ga
->expires
- gb
->expires
;
57 free_shun_from_dict(void *data
)
59 struct shun
*ent
= data
;
67 free_shun(struct shun
*ent
)
69 dict_remove(shun_dict
, ent
->target
);
73 shun_for_p(UNUSED_ARG(void *key
), void *data
, void *extra
)
75 struct shun
*ge
= data
;
76 return !irccasecmp(ge
->target
, extra
);
80 delete_shun_for_p(UNUSED_ARG(void *key
), void *data
, void *extra
)
82 struct shun
*ge
= data
;
84 if (!irccasecmp(ge
->target
, extra
)) {
93 shun_expire(UNUSED_ARG(void *data
))
99 while (heap_size(shun_heap
)) {
100 heap_peek(shun_heap
, 0, &wraa
);
101 stopped
= ((struct shun
*)wraa
)->expires
;
107 if (heap_size(shun_heap
))
108 timeq_add(stopped
, shun_expire
, NULL
);
112 shun_remove(const char *target
, int announce
)
114 int res
= dict_find(shun_dict
, target
, NULL
) ? 1 : 0;
115 if (heap_remove_pred(shun_heap
, delete_shun_for_p
, (char*)target
)) {
117 struct shun
*new_first
;
118 heap_peek(shun_heap
, 0, &argh
);
121 timeq_del(0, shun_expire
, 0, TIMEQ_IGNORE_WHEN
|TIMEQ_IGNORE_DATA
);
122 timeq_add(new_first
->expires
, shun_expire
, 0);
131 shun_add(const char *issuer
, const char *target
, unsigned long duration
, const char *reason
, time_t issued
, int announce
)
134 struct shun
*prev_first
;
137 heap_peek(shun_heap
, 0, &argh
);
139 ent
= dict_find(shun_dict
, target
, NULL
);
141 heap_remove_pred(shun_heap
, shun_for_p
, (char*)target
);
142 if (ent
->expires
< (time_t)(now
+ duration
))
143 ent
->expires
= now
+ duration
;
145 ent
= malloc(sizeof(*ent
));
146 ent
->issued
= issued
;
147 ent
->issuer
= strdup(issuer
);
148 ent
->target
= strdup(target
);
149 ent
->expires
= now
+ duration
;
150 ent
->reason
= strdup(reason
);
151 dict_insert(shun_dict
, ent
->target
, ent
);
153 heap_insert(shun_heap
, ent
, ent
);
154 if (!prev_first
|| (ent
->expires
< prev_first
->expires
)) {
155 timeq_del(0, shun_expire
, 0, TIMEQ_IGNORE_WHEN
|TIMEQ_IGNORE_DATA
);
156 timeq_add(ent
->expires
, shun_expire
, 0);
164 shun_alternate_target(const char *target
)
166 const char *hostname
;
168 /* If no host part, bail. */
169 if (!(hostname
= strchr(target
, '@')))
171 /* If host part contains wildcards, bail. */
172 if (hostname
[strcspn(hostname
, "*?/")])
174 /* Get parsed address and canonical name for host. */
176 irc_in_addr_t in
; /* move this to the right place */
177 if (irc_pton(&in
, NULL
, hostname
+1)) {
178 if (getnameinfo(/*TODO*/))
180 } else if (!getaddrinfo(/*TODO*/)) {
188 shun_find(const char *target
)
194 res
= dict_find(shun_dict
, target
, NULL
);
197 /* Stock ircu requires BADCHANs to match exactly. */
198 if ((target
[0] == '#') || (target
[0] == '&'))
200 else if (target
[strcspn(target
, "*?")]) {
201 /* Wildcard: do an obnoxiously long search. */
202 for (it
= dict_first(shun_dict
); it
; it
= iter_next(it
)) {
204 if (match_ircglob(target
, res
->target
))
208 /* See if we can resolve the hostname part of the mask. */
209 if ((alt_target
= shun_alternate_target(target
))) {
210 res
= shun_find(alt_target
);
218 shun_refresh_helper(UNUSED_ARG(void *key
), void *data
, void *extra
)
220 struct shun
*ge
= data
;
226 shun_refresh_server(struct server
*srv
)
228 heap_remove_pred(shun_heap
, shun_refresh_helper
, srv
);
232 shun_refresh_all(void)
234 heap_remove_pred(shun_heap
, shun_refresh_helper
, 0);
240 return dict_size(shun_dict
);
244 shun_add_record(const char *key
, void *data
, UNUSED_ARG(void *extra
))
246 struct record_data
*rd
= data
;
247 const char *issuer
, *reason
, *dstr
;
248 time_t issued
, expiration
;
250 if (!(reason
= database_get_data(rd
->d
.object
, KEY_REASON
, RECDB_QSTRING
))) {
251 log_module(MAIN_LOG
, LOG_ERROR
, "Missing reason for shun %s", key
);
254 if (!(dstr
= database_get_data(rd
->d
.object
, KEY_EXPIRES
, RECDB_QSTRING
))) {
255 log_module(MAIN_LOG
, LOG_ERROR
, "Missing expiration for shun %s", key
);
258 expiration
= strtoul(dstr
, NULL
, 0);
259 if ((dstr
= database_get_data(rd
->d
.object
, KEY_ISSUED
, RECDB_QSTRING
))) {
260 issued
= strtoul(dstr
, NULL
, 0);
264 if (!(issuer
= database_get_data(rd
->d
.object
, KEY_ISSUER
, RECDB_QSTRING
))) {
265 issuer
= "<unknown>";
267 if (expiration
> now
)
268 shun_add(issuer
, key
, expiration
- now
, reason
, issued
, 0);
273 shun_saxdb_read(struct dict
*db
)
275 return dict_foreach(db
, shun_add_record
, 0) != NULL
;
279 shun_write_entry(UNUSED_ARG(void *key
), void *data
, void *extra
)
281 struct shun
*ent
= data
;
282 struct saxdb_context
*ctx
= extra
;
284 saxdb_start_record(ctx
, ent
->target
, 0);
285 saxdb_write_int(ctx
, KEY_EXPIRES
, ent
->expires
);
286 saxdb_write_int(ctx
, KEY_ISSUED
, ent
->issued
);
287 saxdb_write_string(ctx
, KEY_REASON
, ent
->reason
);
288 saxdb_write_string(ctx
, KEY_ISSUER
, ent
->issuer
);
289 saxdb_end_record(ctx
);
294 shun_saxdb_write(struct saxdb_context
*ctx
)
296 heap_remove_pred(shun_heap
, shun_write_entry
, ctx
);
301 shun_db_cleanup(UNUSED_ARG(void *extra
))
303 heap_delete(shun_heap
);
304 dict_delete(shun_dict
);
310 shun_heap
= heap_new(shun_comparator
);
311 shun_dict
= dict_new();
312 dict_set_free_data(shun_dict
, free_shun_from_dict
);
313 saxdb_register("shun", shun_saxdb_read
, shun_saxdb_write
);
314 reg_exit_func(shun_db_cleanup
, NULL
);
317 struct shun_discrim
*
318 shun_discrim_create(struct userNode
*user
, struct userNode
*src
, unsigned int argc
, char *argv
[])
321 struct shun_discrim
*discrim
;
323 discrim
= calloc(1, sizeof(*discrim
));
324 discrim
->max_issued
= now
;
327 for (i
=0; i
<argc
; i
++) {
329 send_message(user
, src
, "MSG_MISSING_PARAMS", argv
[i
]);
331 } else if (!irccasecmp(argv
[i
], "mask") || !irccasecmp(argv
[i
], "host")) {
332 if (!irccasecmp(argv
[++i
], "exact"))
333 discrim
->target_mask_type
= SEXACT
;
334 else if (!irccasecmp(argv
[i
], "subset"))
335 discrim
->target_mask_type
= SSUBSET
;
336 else if (!irccasecmp(argv
[i
], "superset"))
337 discrim
->target_mask_type
= SSUPERSET
;
339 discrim
->target_mask_type
= SSUBSET
, i
--;
341 send_message(user
, src
, "MSG_MISSING_PARAMS", argv
[i
-1]);
344 if (!is_shun(argv
[i
]) && !IsChannelName(argv
[i
])) {
345 send_message(user
, src
, "MSG_INVALID_SHUN", argv
[i
]);
348 discrim
->target_mask
= argv
[i
];
349 discrim
->alt_target_mask
= shun_alternate_target(discrim
->target_mask
);
350 } else if (!irccasecmp(argv
[i
], "limit"))
351 discrim
->limit
= strtoul(argv
[++i
], NULL
, 0);
352 else if (!irccasecmp(argv
[i
], "reason"))
353 discrim
->reason_mask
= argv
[++i
];
354 else if (!irccasecmp(argv
[i
], "issuer"))
355 discrim
->issuer_mask
= argv
[++i
];
356 else if (!irccasecmp(argv
[i
], "after"))
357 discrim
->min_expire
= now
+ ParseInterval(argv
[++i
]);
358 else if (!irccasecmp(argv
[i
], "before"))
359 discrim
->max_issued
= now
- ParseInterval(argv
[++i
]);
361 send_message(user
, src
, "MSG_INVALID_CRITERIA", argv
[i
]);
367 free(discrim
->alt_target_mask
);
373 struct shun_discrim
*discrim
;
374 shun_search_func func
;
380 shun_discrim_match(struct shun
*shun
, struct shun_discrim
*discrim
)
382 if ((discrim
->issuer_mask
&& !match_ircglob(shun
->issuer
, discrim
->issuer_mask
))
383 || (discrim
->reason_mask
&& !match_ircglob(shun
->reason
, discrim
->reason_mask
))
384 || (discrim
->target_mask
385 && (((discrim
->target_mask_type
== SSUBSET
)
386 && !match_ircglobs(discrim
->target_mask
, shun
->target
)
387 && (!discrim
->alt_target_mask
388 || !match_ircglobs(discrim
->alt_target_mask
, shun
->target
)))
389 || ((discrim
->target_mask_type
== SEXACT
)
390 && irccasecmp(discrim
->target_mask
, shun
->target
)
391 && (!discrim
->alt_target_mask
392 || !irccasecmp(discrim
->alt_target_mask
, shun
->target
)))
393 || ((discrim
->target_mask_type
== SSUPERSET
)
394 && !match_ircglobs(shun
->target
, discrim
->target_mask
)
395 && (!discrim
->alt_target_mask
396 || !match_ircglobs(discrim
->alt_target_mask
, shun
->target
)))))
397 || (discrim
->max_issued
< shun
->issued
)
398 || (discrim
->min_expire
> shun
->expires
)) {
405 shun_search_helper(UNUSED_ARG(void *key
), void *data
, void *extra
)
407 struct shun
*shun
= data
;
408 struct shun_search
*search
= extra
;
410 if (shun_discrim_match(shun
, search
->discrim
)
411 && (search
->hits
++ < search
->discrim
->limit
)) {
412 search
->func(shun
, search
->data
);
418 shun_discrim_search(struct shun_discrim
*discrim
, shun_search_func gsf
, void *data
)
420 struct shun_search search
;
421 search
.discrim
= discrim
;
425 heap_remove_pred(shun_heap
, shun_search_helper
, &search
);