1 |
/** |
2 |
* rnc.C: This file contains functionality implementing OperServ RNC. |
3 |
* |
4 |
* Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team |
5 |
* Rights to this code are as documented in COPYING. |
6 |
* |
7 |
* |
8 |
* Portions of this file were derived from sources bearing the following license: |
9 |
* Copyright © 2006 Robin Burchell <surreal.w00t@gmail.com> |
10 |
* Rights to this code are documented in doc/LICENCE. |
11 |
* |
12 |
* $Id: rnc.C,v 1.5 2007-09-16 18:54:44 pippijn Exp $ |
13 |
*/ |
14 |
|
15 |
#include <boost/foreach.hpp> |
16 |
|
17 |
#include "atheme.h" |
18 |
#include <ermyth/module.h> |
19 |
|
20 |
static char const rcsid[] = "$Id: rnc.C,v 1.5 2007-09-16 18:54:44 pippijn Exp $"; |
21 |
|
22 |
REGISTER_MODULE ("operserv/rnc", false, "Robin Burchell <surreal.w00t@gmail.com>"); |
23 |
|
24 |
static void os_cmd_rnc (sourceinfo_t *si, int parc, char *parv[]); |
25 |
|
26 |
command_t const os_rnc = { "RNC", N_("Shows the most frequent realnames on the network"), PRIV_USER_AUSPEX, 1, os_cmd_rnc }; |
27 |
|
28 |
E cmdvec os_cmdtree; |
29 |
E helpvec os_helptree; |
30 |
|
31 |
typedef struct rnc_t_ rnc_t; |
32 |
struct rnc_t_ |
33 |
{ |
34 |
char gecos[GECOSLEN]; |
35 |
int count; |
36 |
}; |
37 |
|
38 |
bool |
39 |
_modinit (module *m) |
40 |
{ |
41 |
os_cmdtree << os_rnc; |
42 |
help_addentry (os_helptree, "RNC", "help/operserv/rnc", NULL); |
43 |
|
44 |
return true; |
45 |
} |
46 |
|
47 |
void |
48 |
_moddeinit (void) |
49 |
{ |
50 |
os_cmdtree >> os_rnc; |
51 |
help_delentry (os_helptree, "RNC"); |
52 |
} |
53 |
|
54 |
static void |
55 |
os_cmd_rnc (sourceinfo_t *si, int parc, char *parv[]) |
56 |
{ |
57 |
char *param = parv[0]; |
58 |
int count = param ? atoi (param) : 20; |
59 |
node_t *n1, *n2, *biggest = NULL; |
60 |
user_t *u; |
61 |
rnc_t *rnc; |
62 |
list_t realnames; |
63 |
int i, found = 0; |
64 |
|
65 |
realnames.head = NULL; |
66 |
realnames.tail = NULL; |
67 |
realnames.count = 0; |
68 |
|
69 |
foreach (user_t::pair_type &up, user_t::map) |
70 |
{ |
71 |
u = up.second; |
72 |
LIST_FOREACH (n2, realnames.head) |
73 |
{ |
74 |
rnc = static_cast<rnc_t *> (n2->data); |
75 |
|
76 |
if (strcmp (rnc->gecos, u->gecos) == 0) |
77 |
{ |
78 |
/* existing match */ |
79 |
rnc->count++; |
80 |
found = 1; |
81 |
} |
82 |
} |
83 |
|
84 |
if (found == 0) |
85 |
{ |
86 |
/* new realname */ |
87 |
rnc = new rnc_t; |
88 |
sprintf (rnc->gecos, "%s", u->gecos); |
89 |
rnc->count = 1; |
90 |
node_add (rnc, node_create (), &realnames); |
91 |
} |
92 |
|
93 |
found = 0; |
94 |
} |
95 |
|
96 |
found = 0; |
97 |
|
98 |
/* XXX: this is ugly to the max :P */ |
99 |
for (i = 1; i <= count; i++) |
100 |
{ |
101 |
LIST_FOREACH (n1, realnames.head) |
102 |
{ |
103 |
rnc = static_cast<rnc_t *> (n1->data); |
104 |
|
105 |
if (rnc->count > found) |
106 |
{ |
107 |
found = rnc->count; |
108 |
biggest = n1; |
109 |
} |
110 |
} |
111 |
|
112 |
if (biggest == NULL) |
113 |
break; |
114 |
|
115 |
command_success_nodata (si, _("\2%d\2: \2%d\2 matches for realname \2%s\2"), i, ((rnc_t *) (biggest->data))->count, ((rnc_t *) biggest->data)->gecos); |
116 |
delete static_cast<rnc_t *> (biggest->data); |
117 |
node_del (biggest, &realnames); |
118 |
node_free (biggest); |
119 |
|
120 |
found = 0; |
121 |
biggest = NULL; |
122 |
} |
123 |
|
124 |
/* cleanup */ |
125 |
LIST_FOREACH_SAFE (n1, n2, realnames.head) |
126 |
{ |
127 |
rnc = static_cast < rnc_t * >(n1->data); |
128 |
|
129 |
delete rnc; |
130 |
node_del (n1, &realnames); |
131 |
node_free (n1); |
132 |
} |
133 |
|
134 |
logcommand (si, CMDLOG_ADMIN, "RNC %d", count); |
135 |
snoop ("RNC: by \2%s\2", get_oper_name (si)); |
136 |
} |