| 1 |
/* chanfix - channel fixing service |
| 2 |
* Copyright (c) 2010 Atheme Development Group |
| 3 |
*/ |
| 4 |
|
| 5 |
#include "atheme.h" |
| 6 |
#include "chanfix.h" |
| 7 |
|
| 8 |
#define CFDB_VERSION 1 |
| 9 |
|
| 10 |
mowgli_patricia_t *chanfix_channels = NULL; |
| 11 |
|
| 12 |
mowgli_heap_t *chanfix_channel_heap = NULL; |
| 13 |
mowgli_heap_t *chanfix_oprecord_heap = NULL; |
| 14 |
|
| 15 |
mowgli_eventloop_timer_t *chanfix_gather_timer = NULL; |
| 16 |
mowgli_eventloop_timer_t *chanfix_expire_timer = NULL; |
| 17 |
|
| 18 |
static int loading_cfdbv = 0; |
| 19 |
|
| 20 |
/*************************************************************************************/ |
| 21 |
|
| 22 |
chanfix_oprecord_t *chanfix_oprecord_create(chanfix_channel_t *chan, user_t *u) |
| 23 |
{ |
| 24 |
chanfix_oprecord_t *orec; |
| 25 |
|
| 26 |
return_val_if_fail(chan != NULL, NULL); |
| 27 |
|
| 28 |
if (u != NULL) |
| 29 |
{ |
| 30 |
return_val_if_fail((orec = chanfix_oprecord_find(chan, u)) == NULL, orec); |
| 31 |
} |
| 32 |
|
| 33 |
orec = mowgli_heap_alloc(chanfix_oprecord_heap); |
| 34 |
|
| 35 |
orec->chan = chan; |
| 36 |
|
| 37 |
orec->firstseen = CURRTIME; |
| 38 |
orec->lastevent = CURRTIME; |
| 39 |
|
| 40 |
orec->age = 1; |
| 41 |
|
| 42 |
if (u != NULL) |
| 43 |
{ |
| 44 |
orec->entity = entity(u->myuser); |
| 45 |
|
| 46 |
mowgli_strlcpy(orec->user, u->user, sizeof orec->user); |
| 47 |
mowgli_strlcpy(orec->host, u->vhost, sizeof orec->host); |
| 48 |
} |
| 49 |
|
| 50 |
mowgli_node_add(orec, &orec->node, &chan->oprecords); |
| 51 |
|
| 52 |
return orec; |
| 53 |
} |
| 54 |
|
| 55 |
chanfix_oprecord_t *chanfix_oprecord_find(chanfix_channel_t *chan, user_t *u) |
| 56 |
{ |
| 57 |
mowgli_node_t *n; |
| 58 |
|
| 59 |
return_val_if_fail(chan != NULL, NULL); |
| 60 |
return_val_if_fail(u != NULL, NULL); |
| 61 |
|
| 62 |
MOWGLI_ITER_FOREACH(n, chan->oprecords.head) |
| 63 |
{ |
| 64 |
chanfix_oprecord_t *orec = n->data; |
| 65 |
|
| 66 |
if (orec->entity != NULL && orec->entity == entity(u->myuser)) |
| 67 |
return orec; |
| 68 |
|
| 69 |
if (!irccasecmp(orec->user, u->user) && !irccasecmp(orec->host, u->vhost)) |
| 70 |
return orec; |
| 71 |
} |
| 72 |
|
| 73 |
return NULL; |
| 74 |
} |
| 75 |
|
| 76 |
void chanfix_oprecord_update(chanfix_channel_t *chan, user_t *u) |
| 77 |
{ |
| 78 |
chanfix_oprecord_t *orec; |
| 79 |
|
| 80 |
return_if_fail(chan != NULL); |
| 81 |
return_if_fail(u != NULL); |
| 82 |
|
| 83 |
orec = chanfix_oprecord_find(chan, u); |
| 84 |
if (orec != NULL) |
| 85 |
{ |
| 86 |
orec->age++; |
| 87 |
orec->lastevent = CURRTIME; |
| 88 |
|
| 89 |
if (orec->entity == NULL && u->myuser != NULL) |
| 90 |
orec->entity = entity(u->myuser); |
| 91 |
|
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
chanfix_oprecord_create(chan, u); |
| 96 |
chan->lastupdate = CURRTIME; |
| 97 |
} |
| 98 |
|
| 99 |
void chanfix_oprecord_delete(chanfix_oprecord_t *orec) |
| 100 |
{ |
| 101 |
return_if_fail(orec != NULL); |
| 102 |
|
| 103 |
mowgli_node_delete(&orec->node, &orec->chan->oprecords); |
| 104 |
mowgli_heap_free(chanfix_oprecord_heap, orec); |
| 105 |
} |
| 106 |
|
| 107 |
/*************************************************************************************/ |
| 108 |
|
| 109 |
static void chanfix_channel_delete(chanfix_channel_t *c) |
| 110 |
{ |
| 111 |
mowgli_node_t *n, *tn; |
| 112 |
|
| 113 |
return_if_fail(c != NULL); |
| 114 |
|
| 115 |
mowgli_patricia_delete(chanfix_channels, c->name); |
| 116 |
|
| 117 |
MOWGLI_ITER_FOREACH_SAFE(n, tn, c->oprecords.head) |
| 118 |
{ |
| 119 |
chanfix_oprecord_t *orec = n->data; |
| 120 |
|
| 121 |
chanfix_oprecord_delete(orec); |
| 122 |
} |
| 123 |
|
| 124 |
free(c->name); |
| 125 |
mowgli_heap_free(chanfix_channel_heap, c); |
| 126 |
} |
| 127 |
|
| 128 |
chanfix_channel_t *chanfix_channel_create(const char *name, channel_t *chan) |
| 129 |
{ |
| 130 |
chanfix_channel_t *c; |
| 131 |
|
| 132 |
return_val_if_fail(name != NULL, NULL); |
| 133 |
|
| 134 |
c = mowgli_heap_alloc(chanfix_channel_heap); |
| 135 |
object_init(object(c), name, (destructor_t) chanfix_channel_delete); |
| 136 |
|
| 137 |
c->name = sstrdup(name); |
| 138 |
c->chan = chan; |
| 139 |
c->fix_started = 0; |
| 140 |
|
| 141 |
if (c->chan != NULL) |
| 142 |
c->ts = c->chan->ts; |
| 143 |
|
| 144 |
mowgli_patricia_add(chanfix_channels, c->name, c); |
| 145 |
|
| 146 |
return c; |
| 147 |
} |
| 148 |
|
| 149 |
chanfix_channel_t *chanfix_channel_find(const char *name) |
| 150 |
{ |
| 151 |
return mowgli_patricia_retrieve(chanfix_channels, name); |
| 152 |
} |
| 153 |
|
| 154 |
chanfix_channel_t *chanfix_channel_get(channel_t *chan) |
| 155 |
{ |
| 156 |
return_val_if_fail(chan != NULL, NULL); |
| 157 |
|
| 158 |
return mowgli_patricia_retrieve(chanfix_channels, chan->name); |
| 159 |
} |
| 160 |
|
| 161 |
/*************************************************************************************/ |
| 162 |
|
| 163 |
static void chanfix_channel_add_ev(channel_t *ch) |
| 164 |
{ |
| 165 |
chanfix_channel_t *chan; |
| 166 |
|
| 167 |
return_if_fail(ch != NULL); |
| 168 |
|
| 169 |
if ((chan = chanfix_channel_get(ch)) != NULL) |
| 170 |
{ |
| 171 |
chan->chan = ch; |
| 172 |
return; |
| 173 |
} |
| 174 |
|
| 175 |
chanfix_channel_create(ch->name, ch); |
| 176 |
} |
| 177 |
|
| 178 |
static void chanfix_channel_delete_ev(channel_t *ch) |
| 179 |
{ |
| 180 |
chanfix_channel_t *chan; |
| 181 |
|
| 182 |
return_if_fail(ch != NULL); |
| 183 |
|
| 184 |
if ((chan = chanfix_channel_get(ch)) != NULL) |
| 185 |
{ |
| 186 |
chan->chan = NULL; |
| 187 |
return; |
| 188 |
} |
| 189 |
|
| 190 |
chanfix_channel_create(ch->name, NULL); |
| 191 |
} |
| 192 |
|
| 193 |
void chanfix_gather(void *unused) |
| 194 |
{ |
| 195 |
channel_t *ch; |
| 196 |
mowgli_patricia_iteration_state_t state; |
| 197 |
int chans = 0, oprecords = 0; |
| 198 |
|
| 199 |
MOWGLI_PATRICIA_FOREACH(ch, &state, chanlist) |
| 200 |
{ |
| 201 |
mychan_t *mc; |
| 202 |
mowgli_node_t *n; |
| 203 |
chanfix_channel_t *chan; |
| 204 |
|
| 205 |
if ((mc = mychan_find(ch->name)) != NULL) |
| 206 |
continue; |
| 207 |
|
| 208 |
chan = chanfix_channel_get(ch); |
| 209 |
if (chan == NULL) |
| 210 |
chan = chanfix_channel_create(ch->name, ch); |
| 211 |
|
| 212 |
MOWGLI_ITER_FOREACH(n, ch->members.head) |
| 213 |
{ |
| 214 |
chanuser_t *cu = n->data; |
| 215 |
|
| 216 |
if (cu->modes & CSTATUS_OP) |
| 217 |
{ |
| 218 |
chanfix_oprecord_update(chan, cu->user); |
| 219 |
oprecords++; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
chans++; |
| 224 |
} |
| 225 |
|
| 226 |
slog(LG_DEBUG, "chanfix_gather(): gathered %d channels and %d oprecords.", chans, oprecords); |
| 227 |
} |
| 228 |
|
| 229 |
void chanfix_expire(void *unused) |
| 230 |
{ |
| 231 |
chanfix_channel_t *chan; |
| 232 |
mowgli_patricia_iteration_state_t state; |
| 233 |
|
| 234 |
MOWGLI_PATRICIA_FOREACH(chan, &state, chanfix_channels) |
| 235 |
{ |
| 236 |
mowgli_node_t *n, *tn; |
| 237 |
|
| 238 |
MOWGLI_ITER_FOREACH_SAFE(n, tn, chan->oprecords.head) |
| 239 |
{ |
| 240 |
chanfix_oprecord_t *orec = n->data; |
| 241 |
|
| 242 |
/* Simple exponential decay, rounding the decay up |
| 243 |
* so that low scores expire sooner. |
| 244 |
*/ |
| 245 |
orec->age -= (orec->age + CHANFIX_EXPIRE_DIVISOR - 1) / |
| 246 |
CHANFIX_EXPIRE_DIVISOR; |
| 247 |
|
| 248 |
if (orec->age > 0 && CURRTIME - orec->lastevent < CHANFIX_RETENTION_TIME) |
| 249 |
continue; |
| 250 |
|
| 251 |
chanfix_oprecord_delete(orec); |
| 252 |
} |
| 253 |
|
| 254 |
if (MOWGLI_LIST_LENGTH(&chan->oprecords) > 0 && |
| 255 |
CURRTIME - chan->lastupdate < CHANFIX_RETENTION_TIME) |
| 256 |
continue; |
| 257 |
|
| 258 |
object_unref(chan); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
/*************************************************************************************/ |
| 263 |
|
| 264 |
static void write_chanfixdb(database_handle_t *db) |
| 265 |
{ |
| 266 |
chanfix_channel_t *chan; |
| 267 |
mowgli_patricia_iteration_state_t state; |
| 268 |
|
| 269 |
return_if_fail(db != NULL); |
| 270 |
|
| 271 |
db_start_row(db, "CFDBV"); |
| 272 |
db_write_uint(db, CFDB_VERSION); |
| 273 |
db_commit_row(db); |
| 274 |
|
| 275 |
MOWGLI_PATRICIA_FOREACH(chan, &state, chanfix_channels) |
| 276 |
{ |
| 277 |
mowgli_node_t *n; |
| 278 |
|
| 279 |
db_start_row(db, "CFCHAN"); |
| 280 |
db_write_word(db, chan->name); |
| 281 |
db_write_time(db, chan->ts); |
| 282 |
db_write_time(db, chan->lastupdate); |
| 283 |
db_commit_row(db); |
| 284 |
|
| 285 |
MOWGLI_ITER_FOREACH(n, chan->oprecords.head) |
| 286 |
{ |
| 287 |
chanfix_oprecord_t *orec = n->data; |
| 288 |
|
| 289 |
db_start_row(db, "CFOP"); |
| 290 |
db_write_word(db, chan->name); |
| 291 |
|
| 292 |
if (orec->entity) |
| 293 |
db_write_word(db, orec->entity->name); |
| 294 |
else |
| 295 |
db_write_word(db, "*"); |
| 296 |
|
| 297 |
db_write_word(db, orec->user); |
| 298 |
db_write_word(db, orec->host); |
| 299 |
|
| 300 |
db_write_time(db, orec->firstseen); |
| 301 |
db_write_time(db, orec->lastevent); |
| 302 |
|
| 303 |
db_write_uint(db, orec->age); |
| 304 |
|
| 305 |
db_commit_row(db); |
| 306 |
} |
| 307 |
|
| 308 |
if (object(chan)->metadata != NULL) |
| 309 |
{ |
| 310 |
mowgli_patricia_iteration_state_t state2; |
| 311 |
metadata_t *md; |
| 312 |
|
| 313 |
MOWGLI_PATRICIA_FOREACH(md, &state2, object(chan)->metadata) |
| 314 |
{ |
| 315 |
db_start_row(db, "CFMD"); |
| 316 |
db_write_word(db, chan->name); |
| 317 |
db_write_word(db, md->name); |
| 318 |
db_write_str(db, md->value); |
| 319 |
db_commit_row(db); |
| 320 |
} |
| 321 |
} |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
static void db_h_cfdbv(database_handle_t *db, const char *type) |
| 326 |
{ |
| 327 |
loading_cfdbv = db_sread_uint(db); |
| 328 |
slog(LG_INFO, "chanfix: opensex data schema version is %d.", loading_cfdbv); |
| 329 |
} |
| 330 |
|
| 331 |
static void db_h_cfchan(database_handle_t *db, const char *type) |
| 332 |
{ |
| 333 |
const char *name; |
| 334 |
time_t ts, lastupdate; |
| 335 |
chanfix_channel_t *chan; |
| 336 |
|
| 337 |
name = db_sread_word(db); |
| 338 |
ts = db_sread_time(db); |
| 339 |
lastupdate = db_sread_time(db); |
| 340 |
|
| 341 |
chan = chanfix_channel_create(name, NULL); |
| 342 |
chan->ts = ts; |
| 343 |
chan->lastupdate = lastupdate; |
| 344 |
} |
| 345 |
|
| 346 |
static void db_h_cfop(database_handle_t *db, const char *type) |
| 347 |
{ |
| 348 |
const char *name, *entity, *user, *host; |
| 349 |
time_t firstseen, lastevent; |
| 350 |
unsigned int age; |
| 351 |
chanfix_channel_t *chan; |
| 352 |
chanfix_oprecord_t *orec; |
| 353 |
|
| 354 |
name = db_sread_word(db); |
| 355 |
entity = db_sread_word(db); |
| 356 |
user = db_sread_word(db); |
| 357 |
host = db_sread_word(db); |
| 358 |
|
| 359 |
firstseen = db_sread_time(db); |
| 360 |
lastevent = db_sread_time(db); |
| 361 |
|
| 362 |
age = db_sread_uint(db); |
| 363 |
|
| 364 |
chan = chanfix_channel_find(name); |
| 365 |
orec = chanfix_oprecord_create(chan, NULL); |
| 366 |
|
| 367 |
orec->entity = myentity_find(entity); |
| 368 |
mowgli_strlcpy(orec->user, user, sizeof orec->user); |
| 369 |
mowgli_strlcpy(orec->host, host, sizeof orec->host); |
| 370 |
|
| 371 |
orec->firstseen = firstseen; |
| 372 |
orec->lastevent = lastevent; |
| 373 |
|
| 374 |
orec->age = age; |
| 375 |
} |
| 376 |
|
| 377 |
static void db_h_cfmd(database_handle_t *db, const char *type) |
| 378 |
{ |
| 379 |
const char *chname, *key, *value; |
| 380 |
chanfix_channel_t *chan; |
| 381 |
|
| 382 |
chname = db_sread_word(db); |
| 383 |
key = db_sread_word(db); |
| 384 |
value = db_sread_str(db); |
| 385 |
|
| 386 |
chan = chanfix_channel_find(chname); |
| 387 |
metadata_add(chan, key, value); |
| 388 |
} |
| 389 |
|
| 390 |
/*************************************************************************************/ |
| 391 |
|
| 392 |
void chanfix_gather_init(chanfix_persist_record_t *rec) |
| 393 |
{ |
| 394 |
hook_add_db_write(write_chanfixdb); |
| 395 |
hook_add_channel_add(chanfix_channel_add_ev); |
| 396 |
hook_add_channel_delete(chanfix_channel_delete_ev); |
| 397 |
|
| 398 |
db_register_type_handler("CFDBV", db_h_cfdbv); |
| 399 |
db_register_type_handler("CFCHAN", db_h_cfchan); |
| 400 |
db_register_type_handler("CFOP", db_h_cfop); |
| 401 |
db_register_type_handler("CFMD", db_h_cfmd); |
| 402 |
|
| 403 |
if (rec != NULL) |
| 404 |
{ |
| 405 |
chanfix_channel_heap = rec->chanfix_channel_heap; |
| 406 |
chanfix_oprecord_heap = rec->chanfix_oprecord_heap; |
| 407 |
|
| 408 |
chanfix_channels = rec->chanfix_channels; |
| 409 |
return; |
| 410 |
} |
| 411 |
|
| 412 |
chanfix_channel_heap = mowgli_heap_create(sizeof(chanfix_channel_t), 32, BH_LAZY); |
| 413 |
chanfix_oprecord_heap = mowgli_heap_create(sizeof(chanfix_oprecord_t), 32, BH_LAZY); |
| 414 |
|
| 415 |
chanfix_channels = mowgli_patricia_create(strcasecanon); |
| 416 |
|
| 417 |
chanfix_expire_timer = mowgli_timer_add(base_eventloop, "chanfix_expire", chanfix_expire, NULL, CHANFIX_EXPIRE_INTERVAL); |
| 418 |
chanfix_gather_timer = mowgli_timer_add(base_eventloop, "chanfix_gather", chanfix_gather, NULL, CHANFIX_GATHER_INTERVAL); |
| 419 |
} |
| 420 |
|
| 421 |
void chanfix_gather_deinit(module_unload_intent_t intent, chanfix_persist_record_t *rec) |
| 422 |
{ |
| 423 |
hook_del_db_write(write_chanfixdb); |
| 424 |
hook_del_channel_add(chanfix_channel_add_ev); |
| 425 |
hook_del_channel_delete(chanfix_channel_delete_ev); |
| 426 |
|
| 427 |
db_unregister_type_handler("CFDBV"); |
| 428 |
db_unregister_type_handler("CFCHAN"); |
| 429 |
db_unregister_type_handler("CFOP"); |
| 430 |
|
| 431 |
mowgli_timer_destroy(base_eventloop, chanfix_expire_timer); |
| 432 |
mowgli_timer_destroy(base_eventloop, chanfix_gather_timer); |
| 433 |
|
| 434 |
switch (intent) |
| 435 |
{ |
| 436 |
case MODULE_UNLOAD_INTENT_RELOAD: |
| 437 |
rec->chanfix_channel_heap = chanfix_channel_heap; |
| 438 |
rec->chanfix_oprecord_heap = chanfix_oprecord_heap; |
| 439 |
|
| 440 |
rec->chanfix_channels = chanfix_channels; |
| 441 |
break; |
| 442 |
|
| 443 |
case MODULE_UNLOAD_INTENT_PERM: |
| 444 |
default: |
| 445 |
mowgli_patricia_destroy(chanfix_channels, NULL, NULL); |
| 446 |
|
| 447 |
mowgli_heap_destroy(chanfix_channel_heap); |
| 448 |
mowgli_heap_destroy(chanfix_oprecord_heap); |
| 449 |
break; |
| 450 |
} |
| 451 |
} |