]> jfr.im git - irc/quakenet/newserv.git/blame - patricianick/patricianick.c
chanserv: fixed cleanupdb to also clear history databases
[irc/quakenet/newserv.git] / patricianick / patricianick.c
CommitLineData
84ca44e7
P
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
32a0a978 14#define ALLOCUNIT 100
84ca44e7
P
15
16patricianick_t *freepatricianicks;
17int pnode_ext;
18int pnick_ext;
19
35a8a596 20int pn_cmd_nodeuserlist(void *source, int cargc, char **cargv);
84ca44e7
P
21
22void _init() {
23 nick *np, *nnp;
24 int i;
25
26 pnode_ext = registernodeext("patricianick");
32a0a978 27 if (pnode_ext == -1) {
84ca44e7
P
28 Error("patricianick", ERR_FATAL, "Could not register a required node extension");
29 return;
30 }
31
32 pnick_ext = registernickext("patricianick");
32a0a978 33 if (pnick_ext == -1) {
84ca44e7
P
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
2394d0d8 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.");
84ca44e7
P
48}
49
50void _fini() {
51 nsfreeall(POOL_PATRICIANICK);
32a0a978 52
84ca44e7
P
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
35a8a596 64 deregistercontrolcmd("nodeuserlist", &pn_cmd_nodeuserlist);
84ca44e7
P
65}
66
67patricianick_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++) {
32a0a978 74 freepatricianicks[i].identhash[0]=(nick *)&(freepatricianicks[i+1]);
84ca44e7 75 }
32a0a978 76 freepatricianicks[ALLOCUNIT-1].identhash[0]=NULL;
84ca44e7
P
77 }
78
79 pnp=freepatricianicks;
32a0a978 80 freepatricianicks=(patricianick_t *)pnp->identhash[0];
84ca44e7 81
32a0a978 82 memset(pnp, 0, sizeof(patricianick_t));
84ca44e7
P
83 return pnp;
84}
85
86void addnicktonode(patricia_node_t *node, nick *np) {
32a0a978
C
87 unsigned long hash;
88
94a4dc1c
P
89 patricia_ref_prefix(node->prefix);
90
32a0a978 91 if (!(node->exts[pnode_ext])) {
84ca44e7
P
92 node->exts[pnode_ext] = getpatricianick();
93 }
32a0a978
C
94
95 hash = pn_getidenthash(np->ident);
96 np->exts[pnick_ext] = ((patricianick_t *)node->exts[pnode_ext])->identhash[hash];
97 ((patricianick_t *)node->exts[pnode_ext])->identhash[hash] = np;
84ca44e7
P
98}
99
100void deletenickfromnode(patricia_node_t *node, nick *np) {
101 nick **tnp;
32a0a978
C
102 int found, i;
103
104 found = 0;
84ca44e7 105
32a0a978
C
106 for (tnp = &(((patricianick_t *)node->exts[pnode_ext])->identhash[pn_getidenthash(np->ident)]); *tnp; tnp = (nick **)(&((*tnp)->exts[pnick_ext]))) {
107 if (*tnp == np) {
84ca44e7
P
108 *tnp = np->exts[pnick_ext];
109 found = 1;
110 break;
111 }
112 }
113
114 if (!found) {
32a0a978
C
115 Error("patricianick", ERR_ERROR, "Could not remove %s!%s from %s", np->nick, np->ident, IPtostr(node->prefix->sin));
116 return;
84ca44e7
P
117 }
118
32a0a978
C
119 for (i = 0; i < PATRICIANICK_HASHSIZE; i++) {
120 if (((patricianick_t *)node->exts[pnode_ext])->identhash[i]) {
121 return;
122 }
84ca44e7
P
123 }
124
32a0a978
C
125 freepatricianick(node->exts[pnode_ext]);
126 node->exts[pnode_ext]= NULL;
84ca44e7
P
127}
128
129void freepatricianick(patricianick_t *pnp) {
32a0a978 130 pnp->identhash[0]=(nick *)freepatricianicks;
84ca44e7
P
131 freepatricianicks=pnp;
132}
133
134void pn_hook_newuser(int hook, void *arg) {
135 nick *np = (nick *)arg;
136
137 addnicktonode(np->ipnode, np);
138}
139
140void pn_hook_lostuser(int hook, void *arg) {
141 nick *np = (nick *)arg;
142
143 deletenickfromnode(np->ipnode, np);
144}
145
35a8a596 146int pn_cmd_nodeuserlist(void *source, int cargc, char **cargv) {
84ca44e7
P
147 nick *np=(nick *)source;
148 struct irc_in_addr sin;
149 unsigned char bits;
32a0a978 150 unsigned int count, i;
84ca44e7
P
151 patricia_node_t *head, *node;
152 patricianick_t *pnp;
153 nick *npp;
84ca44e7
P
154
155 if (cargc < 1) {
2394d0d8 156 return CMD_USAGE;
84ca44e7
P
157 }
158
159 if (ipmask_parse(cargv[0], &sin, &bits) == 0) {
160 controlreply(np, "Invalid mask.");
2394d0d8 161 return CMD_ERROR;
84ca44e7
P
162 }
163
164 head = refnode(iptree, &sin, bits);
32a0a978 165 count = 0;
84ca44e7
P
166
167 PATRICIA_WALK(head, node)
168 {
169 pnp = node->exts[pnode_ext];
32a0a978
C
170 if (pnp) {
171 if (count < PATRICIANICK_MAXRESULTS) {
172 for (i = 0; i < PATRICIANICK_HASHSIZE; i++) {
173 for (npp = pnp->identhash[i]; npp; npp=npp->exts[pnick_ext]) {
174 controlreply(np, "%s!%s@%s%s%s (%s)", npp->nick, npp->ident, npp->host->name->content, IsAccount(npp) ? "/" : "", npp->authname, IPtostr(node->prefix->sin));
175 }
176 }
177
178 count += node->usercount;
179
180 if (count >= PATRICIANICK_MAXRESULTS) {
181 controlreply(np, "Too many results, output truncated");
182 }
183 } else {
184 count += node->usercount;
185 }
84ca44e7
P
186 }
187 }
188 PATRICIA_WALK_END;
8ac1acd3 189 derefnode(iptree, head);
84ca44e7 190
32a0a978 191 controlreply(np, "Total users on %s: %d", cargv[0], count);
84ca44e7
P
192 return CMD_OK;
193}