| 1 |
/* |
| 2 |
* Copyright (c) 2006 Robin Burchell <[email protected]> |
| 3 |
* Rights to this code are documented in doc/LICENCE. |
| 4 |
* |
| 5 |
* This file contains functionality implementing OperServ RNC. |
| 6 |
* |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "atheme.h" |
| 10 |
|
| 11 |
DECLARE_MODULE_V1 |
| 12 |
( |
| 13 |
"operserv/rnc", false, _modinit, _moddeinit, |
| 14 |
PACKAGE_STRING, |
| 15 |
"Robin Burchell <[email protected]>" |
| 16 |
); |
| 17 |
|
| 18 |
static void os_cmd_rnc(sourceinfo_t *si, int parc, char *parv[]); |
| 19 |
|
| 20 |
command_t os_rnc = { "RNC", N_("Shows the most frequent realnames on the network"), PRIV_USER_AUSPEX, 1, os_cmd_rnc, { .path = "oservice/rnc" } }; |
| 21 |
|
| 22 |
typedef struct rnc_t_ rnc_t; |
| 23 |
struct rnc_t_ |
| 24 |
{ |
| 25 |
const char *gecos; |
| 26 |
int count; |
| 27 |
}; |
| 28 |
|
| 29 |
void _modinit(module_t *m) |
| 30 |
{ |
| 31 |
service_named_bind_command("operserv", &os_rnc); |
| 32 |
} |
| 33 |
|
| 34 |
void _moddeinit(module_unload_intent_t intent) |
| 35 |
{ |
| 36 |
service_named_unbind_command("operserv", &os_rnc); |
| 37 |
} |
| 38 |
|
| 39 |
static void os_cmd_rnc(sourceinfo_t *si, int parc, char *parv[]) |
| 40 |
{ |
| 41 |
char *param = parv[0]; |
| 42 |
int count = param ? atoi(param) : 20; |
| 43 |
user_t *u; |
| 44 |
rnc_t *rnc, *biggest; |
| 45 |
mowgli_patricia_t *realnames; |
| 46 |
int i, found = 0; |
| 47 |
mowgli_patricia_iteration_state_t state; |
| 48 |
|
| 49 |
realnames = mowgli_patricia_create(noopcanon); |
| 50 |
|
| 51 |
MOWGLI_PATRICIA_FOREACH(u, &state, userlist) |
| 52 |
{ |
| 53 |
rnc = mowgli_patricia_retrieve(realnames, u->gecos); |
| 54 |
if (rnc != NULL) |
| 55 |
rnc->count++; |
| 56 |
else |
| 57 |
{ |
| 58 |
rnc = malloc(sizeof(rnc_t)); |
| 59 |
rnc->gecos = u->gecos; |
| 60 |
rnc->count = 1; |
| 61 |
mowgli_patricia_add(realnames, rnc->gecos, rnc); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
/* this is ugly to the max :P */ |
| 66 |
for (i = 1; i <= count; i++) |
| 67 |
{ |
| 68 |
found = 0; |
| 69 |
biggest = NULL; |
| 70 |
MOWGLI_PATRICIA_FOREACH(rnc, &state, realnames) |
| 71 |
{ |
| 72 |
if (rnc->count > found) |
| 73 |
{ |
| 74 |
found = rnc->count; |
| 75 |
biggest = rnc; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
if (biggest == NULL) |
| 80 |
break; |
| 81 |
|
| 82 |
command_success_nodata(si, _("\2%d\2: \2%d\2 matches for realname \2%s\2"), i, biggest->count, biggest->gecos); |
| 83 |
mowgli_patricia_delete(realnames, biggest->gecos); |
| 84 |
free(biggest); |
| 85 |
} |
| 86 |
|
| 87 |
/* cleanup */ |
| 88 |
MOWGLI_PATRICIA_FOREACH(rnc, &state, realnames) |
| 89 |
{ |
| 90 |
mowgli_patricia_delete(realnames, rnc->gecos); |
| 91 |
free(rnc); |
| 92 |
} |
| 93 |
mowgli_patricia_destroy(realnames, NULL, NULL); |
| 94 |
|
| 95 |
logcommand(si, CMDLOG_ADMIN, "RNC: \2%d\2", count); |
| 96 |
} |
| 97 |
|
| 98 |
/* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs |
| 99 |
* vim:ts=8 |
| 100 |
* vim:sw=8 |
| 101 |
* vim:noexpandtab |
| 102 |
*/ |