]> jfr.im git - irc/quakenet/newserv.git/blob - patricianick/patricianick.c
408de701d1dad55614c0295564036a40634d801b
[irc/quakenet/newserv.git] / patricianick / patricianick.c
1 /* patricianick.c */
2
3 #include "patricianick.h"
4 #include "../irc/irc_config.h"
5 #include "../lib/irc_string.h"
6 #include "../core/error.h"
7 #include "../core/nsmalloc.h"
8 #include "../control/control.h"
9 #include "../core/schedule.h"
10
11 #include <stdio.h>
12 #include <string.h>
13
14 #define ALLOCUNIT 100
15
16 patricianick_t *freepatricianicks;
17 int pnode_ext;
18 int pnick_ext;
19
20 int pn_cmd_nodeuserlist(void *source, int cargc, char **cargv);
21
22 void _init() {
23 nick *np, *nnp;
24 int i;
25
26 pnode_ext = registernodeext("patricianick");
27 if (pnode_ext == -1) {
28 Error("patricianick", ERR_FATAL, "Could not register a required node extension");
29 return;
30 }
31
32 pnick_ext = registernickext("patricianick");
33 if (pnick_ext == -1) {
34 Error("patricianick", ERR_FATAL, "Could not register a required nick extension");
35 return;
36 }
37
38 for (i=0;i<NICKHASHSIZE;i++)
39 for (np=nicktable[i];np;np=nnp) {
40 nnp=np->next;
41 addnicktonode(np->ipnode, np);
42 }
43
44 registerhook(HOOK_NICK_NEWNICK, &pn_hook_newuser);
45 registerhook(HOOK_NICK_LOSTNICK, &pn_hook_lostuser);
46
47 registercontrolhelpcmd("nodeuserlist", NO_OPER, 1, &pn_cmd_nodeuserlist, "Usage: nodeuserlist <ipv4|ipv6|cidr4|cidr6>\nLists all users on a given IP address or CIDR range.");
48 }
49
50 void _fini() {
51 nsfreeall(POOL_PATRICIANICK);
52
53 if (pnode_ext != -1) {
54 releasenodeext(pnode_ext);
55 }
56
57 if (pnick_ext != -1) {
58 releasenickext(pnick_ext);
59 }
60
61 deregisterhook(HOOK_NICK_NEWNICK, &pn_hook_newuser);
62 deregisterhook(HOOK_NICK_LOSTNICK, &pn_hook_lostuser);
63
64 deregistercontrolcmd("nodeuserlist", &pn_cmd_nodeuserlist);
65 }
66
67 patricianick_t *getpatricianick() {
68 int i;
69 patricianick_t *pnp;
70
71 if (freepatricianicks==NULL) {
72 freepatricianicks=(patricianick_t *)nsmalloc(POOL_PATRICIANICK, ALLOCUNIT*sizeof(patricianick_t));
73 for(i=0;i<ALLOCUNIT-1;i++) {
74 freepatricianicks[i].identhash[0]=(nick *)&(freepatricianicks[i+1]);
75 }
76 freepatricianicks[ALLOCUNIT-1].identhash[0]=NULL;
77 }
78
79 pnp=freepatricianicks;
80 freepatricianicks=(patricianick_t *)pnp->identhash[0];
81
82 memset(pnp, 0, sizeof(patricianick_t));
83 return pnp;
84 }
85
86 void addnicktonode(patricia_node_t *node, nick *np) {
87 unsigned long hash;
88
89 if (!(node->exts[pnode_ext])) {
90 node->exts[pnode_ext] = getpatricianick();
91 }
92
93 hash = pn_getidenthash(np->ident);
94 np->exts[pnick_ext] = ((patricianick_t *)node->exts[pnode_ext])->identhash[hash];
95 ((patricianick_t *)node->exts[pnode_ext])->identhash[hash] = np;
96 }
97
98 void deletenickfromnode(patricia_node_t *node, nick *np) {
99 nick **tnp;
100 int found, i;
101
102 found = 0;
103
104 for (tnp = &(((patricianick_t *)node->exts[pnode_ext])->identhash[pn_getidenthash(np->ident)]); *tnp; tnp = (nick **)(&((*tnp)->exts[pnick_ext]))) {
105 if (*tnp == np) {
106 *tnp = np->exts[pnick_ext];
107 found = 1;
108 break;
109 }
110 }
111
112 if (!found) {
113 Error("patricianick", ERR_ERROR, "Could not remove %s!%s from %s", np->nick, np->ident, IPtostr(node->prefix->sin));
114 return;
115 }
116
117 for (i = 0; i < PATRICIANICK_HASHSIZE; i++) {
118 if (((patricianick_t *)node->exts[pnode_ext])->identhash[i]) {
119 return;
120 }
121 }
122
123 freepatricianick(node->exts[pnode_ext]);
124 node->exts[pnode_ext]= NULL;
125 }
126
127 void freepatricianick(patricianick_t *pnp) {
128 pnp->identhash[0]=(nick *)freepatricianicks;
129 freepatricianicks=pnp;
130 }
131
132 void pn_hook_newuser(int hook, void *arg) {
133 nick *np = (nick *)arg;
134
135 addnicktonode(np->ipnode, np);
136 }
137
138 void pn_hook_lostuser(int hook, void *arg) {
139 nick *np = (nick *)arg;
140
141 deletenickfromnode(np->ipnode, np);
142 }
143
144 int pn_cmd_nodeuserlist(void *source, int cargc, char **cargv) {
145 nick *np=(nick *)source;
146 struct irc_in_addr sin;
147 unsigned char bits;
148 unsigned int count, i;
149 patricia_node_t *head, *node;
150 patricianick_t *pnp;
151 nick *npp;
152
153 if (cargc < 1) {
154 return CMD_USAGE;
155 }
156
157 if (ipmask_parse(cargv[0], &sin, &bits) == 0) {
158 controlreply(np, "Invalid mask.");
159 return CMD_ERROR;
160 }
161
162 head = refnode(iptree, &sin, bits);
163 count = 0;
164
165 PATRICIA_WALK(head, node)
166 {
167 pnp = node->exts[pnode_ext];
168 if (pnp) {
169 if (count < PATRICIANICK_MAXRESULTS) {
170 for (i = 0; i < PATRICIANICK_HASHSIZE; i++) {
171 for (npp = pnp->identhash[i]; npp; npp=npp->exts[pnick_ext]) {
172 controlreply(np, "%s!%s@%s%s%s (%s)", npp->nick, npp->ident, npp->host->name->content, IsAccount(npp) ? "/" : "", npp->authname, IPtostr(node->prefix->sin));
173 }
174 }
175
176 count += node->usercount;
177
178 if (count >= PATRICIANICK_MAXRESULTS) {
179 controlreply(np, "Too many results, output truncated");
180 }
181 } else {
182 count += node->usercount;
183 }
184 }
185 }
186 PATRICIA_WALK_END;
187
188 controlreply(np, "Total users on %s: %d", cargv[0], count);
189 return CMD_OK;
190 }