]> jfr.im git - irc/quakenet/newserv.git/blame - nick/nick.c
nterface to nterfacer, now using nsmalloc.
[irc/quakenet/newserv.git] / nick / nick.c
CommitLineData
c86edd1d
Q
1/* nick.c */
2
3#include "nick.h"
4#include "../lib/flags.h"
5#include "../lib/irc_string.h"
6#include "../lib/base64.h"
7#include "../irc/irc.h"
8#include "../irc/irc_config.h"
9#include "../core/error.h"
10#include "../core/hooks.h"
11#include "../lib/sstring.h"
12#include "../server/server.h"
13#include "../parser/parser.h"
87698d77 14#include "../lib/version.h"
103521a1 15#include "../core/nsmalloc.h"
16
c86edd1d
Q
17#include <stdlib.h>
18#include <string.h>
19#include <stdio.h>
20
70b0a4e5 21MODULE_VERSION("");
87698d77 22
c86edd1d
Q
23const flag umodeflags[] = {
24 { 'i', UMODE_INV },
25 { 'w', UMODE_WALLOPS },
26 { 'g', UMODE_DEBUG },
27 { 'o', UMODE_OPER },
28 { 'k', UMODE_SERVICE },
29 { 'X', UMODE_XOPER },
30 { 'd', UMODE_DEAF },
31 { 'r', UMODE_ACCOUNT },
32 { 'n', UMODE_HIDECHAN },
33 { 'x', UMODE_HIDEHOST },
34 { 'h', UMODE_SETHOST },
35 { 'R', UMODE_REGPRIV },
36 { 'I', UMODE_HIDEIDLE },
e98d808a 37 { 'P', UMODE_PARANOID },
c86edd1d
Q
38 { '\0', 0 } };
39
40#define nickhash(x) ((crc32i(x))%NICKHASHSIZE)
41
42nick *nicktable[NICKHASHSIZE];
43nick **servernicks[MAXSERVERS];
44
45sstring *nickextnames[MAXNICKEXTS];
526e7c1d
P
46sstring *nodeextnames[PATRICIA_MAXSLOTS];
47patricia_tree_t *iptree;
c86edd1d
Q
48
49void nickstats(int hooknum, void *arg);
50
51void _init() {
52 initnickalloc();
53 initnickhelpers();
54 memset(nicktable,0,sizeof(nicktable));
55 memset(servernicks,0,sizeof(servernicks));
56
57 /* Register our hooks */
58 registerhook(HOOK_SERVER_NEWSERVER,&handleserverchange);
59 registerhook(HOOK_SERVER_LOSTSERVER,&handleserverchange);
60 registerhook(HOOK_CORE_STATSREQUEST,&nickstats);
61
62 /* And our server handlers */
63 registerserverhandler("N",&handlenickmsg,10);
64 registerserverhandler("D",&handlekillmsg,2);
65 registerserverhandler("Q",&handlequitmsg,1);
66 registerserverhandler("M",&handleusermodemsg,3);
67 registerserverhandler("W",&handlewhoismsg,2);
47657339 68 registerserverhandler("AC",&handleaccountmsg,4);
c86edd1d
Q
69 registerserverhandler("R",&handlestatsmsg,2);
70
71 /* Fake the addition of our own server */
72 handleserverchange(HOOK_SERVER_NEWSERVER,(void *)numerictolong(mynumeric->content,2));
526e7c1d
P
73
74 iptree = patricia_new_tree(PATRICIA_MAXBITS);
c86edd1d
Q
75}
76
103521a1 77void _fini() {
78 nsfreeall(POOL_NICK);
79
80 /* Free the hooks */
81 deregisterhook(HOOK_SERVER_NEWSERVER,&handleserverchange);
82 deregisterhook(HOOK_SERVER_LOSTSERVER,&handleserverchange);
83 deregisterhook(HOOK_CORE_STATSREQUEST,&nickstats);
84
85 /* And our server handlers */
86 deregisterserverhandler("N",&handlenickmsg);
87 deregisterserverhandler("D",&handlekillmsg);
88 deregisterserverhandler("Q",&handlequitmsg);
89 deregisterserverhandler("M",&handleusermodemsg);
90 deregisterserverhandler("W",&handlewhoismsg);
91 deregisterserverhandler("AC",&handleaccountmsg);
92 deregisterserverhandler("R",&handlestatsmsg);
93}
94
c86edd1d
Q
95/*
96 * This function handles servers appearing and disappearing.
97 * For a new server, the client table is allocated.
98 * For a disappearing server, all it's clients are killed and the client table is freed.
99 */
100
101void handleserverchange(int hooknum, void *arg) {
c3db6f7e 102 long servernum;
c86edd1d
Q
103 int i;
104
c3db6f7e 105 servernum=(long)arg;
c86edd1d
Q
106
107 switch(hooknum) {
108 case HOOK_SERVER_NEWSERVER:
103521a1 109 servernicks[servernum]=(nick **)nsmalloc(POOL_NICK,(serverlist[servernum].maxusernum+1)*sizeof(nick **));
c86edd1d
Q
110 memset(servernicks[servernum],0,(serverlist[servernum].maxusernum+1)*sizeof(nick **));
111 break;
112
113 case HOOK_SERVER_LOSTSERVER:
114 for (i=0;i<=serverlist[servernum].maxusernum;i++) {
115 if (servernicks[servernum][i]!=NULL) {
116 deletenick(servernicks[servernum][i]);
117 }
118 }
2a010087 119 nsfree(POOL_NICK,servernicks[servernum]);
c86edd1d
Q
120 break;
121 }
122}
123
124/*
125 * deletenick:
126 *
127 * This function handles the removal of a nick from the network
128 */
129
130void deletenick(nick *np) {
131 nick **nh;
132
133 /* Fire the hook. This will deal with removal from channels etc. */
134 triggerhook(HOOK_NICK_LOSTNICK, np);
135
136 /* Release the realname and hostname parts */
137
138 for (nh=&(np->realname->nicks);*nh;nh=&((*nh)->nextbyrealname)) {
139 if (*nh==np) {
140 *nh=np->nextbyrealname;
141 break;
142 }
143 }
144
145 for (nh=&(np->host->nicks);*nh;nh=&((*nh)->nextbyhost)) {
146 if (*nh==np) {
147 *nh=np->nextbyhost;
148 break;
149 }
150 }
151
152 releaserealname(np->realname);
153 releasehost(np->host);
154
47657339
C
155 if(IsAccount(np) && np->auth)
156 {
157 np->auth->usercount--;
158
159 for (nh=&(np->auth->nicks);*nh;nh=&((*nh)->nextbyauthname)) {
160 if (*nh==np) {
161 *nh=np->nextbyauthname;
162 break;
163 }
164 }
165
166 releaseauthname(np->auth);
167 }
168
c86edd1d
Q
169 freesstring(np->shident); /* freesstring(NULL) is OK */
170 freesstring(np->sethost);
171
c426783d 172 np->ipnode->usercount--;
526e7c1d
P
173 derefnode(iptree, np->ipnode);
174
175 /* TODO: figure out how to cleanly remove nodes without affecting other modules */
176
c86edd1d
Q
177 /* Delete the nick from the servernick table */
178 *(gethandlebynumericunsafe(np->numeric))=NULL;
179
180 /* Remove the nick from the hash table */
181 removenickfromhash(np);
182
183 freenick(np);
184}
185
186void addnicktohash(nick *np) {
187 np->next=nicktable[nickhash(np->nick)];
188 nicktable[nickhash(np->nick)]=np;
189}
190
191void removenickfromhash(nick *np) {
192 nick **nh;
193
194 for (nh=&(nicktable[nickhash(np->nick)]);*nh;nh=&((*nh)->next)) {
195 if ((*nh)==np) {
196 (*nh)=np->next;
197 break;
198 }
199 }
200}
201
202nick *getnickbynick(const char *name) {
203 nick *np;
204
205 for (np=nicktable[nickhash(name)];np;np=np->next) {
206 if (!ircd_strcmp(np->nick,name))
207 return np;
208 }
209
210 return NULL;
211}
212
213void nickstats(int hooknum, void *arg) {
214 int total,maxchain,curchain,i,buckets;
215 nick *np;
216 char buf[200];
217
218 /* Get nick stats */
219 buckets=total=maxchain=curchain=0;
220 for (i=0;i<NICKHASHSIZE;i++,curchain=0) {
221 np=nicktable[i];
222 if (np!=NULL) {
223 buckets++;
224 for (;np;np=np->next) {
225 total++;
226 curchain++;
227 }
228 }
229 if (curchain>maxchain) {
230 maxchain=curchain;
231 }
232 }
233
c3db6f7e 234 if ((long)arg>5) {
c86edd1d
Q
235 /* Full stats */
236 sprintf(buf,"Nick : %6d nicks (HASH: %6d/%6d, chain %3d)",total,buckets,NICKHASHSIZE,maxchain);
c3db6f7e 237 } else if ((long)arg>2) {
c86edd1d
Q
238 sprintf(buf,"Nick : %6d users on network.",total);
239 }
240
c3db6f7e 241 if ((long)arg>2) {
c86edd1d
Q
242 triggerhook(HOOK_CORE_STATSREPLY,buf);
243 }
244}
245
246int registernickext(const char *name) {
247 int i;
248
249 if (findnickext(name)!=-1) {
250 Error("nick",ERR_WARNING,"Tried to register duplicate nick extension %s",name);
251 return -1;
252 }
253
254 for (i=0;i<MAXNICKEXTS;i++) {
255 if (nickextnames[i]==NULL) {
256 nickextnames[i]=getsstring(name,100);
257 return i;
258 }
259 }
260
47657339 261 Error("nick",ERR_WARNING,"Tried to register too many nick extensions: %s",name);
c86edd1d
Q
262 return -1;
263}
264
265int findnickext(const char *name) {
266 int i;
267
268 for (i=0;i<MAXNICKEXTS;i++) {
269 if (nickextnames[i]!=NULL && !ircd_strcmp(name,nickextnames[i]->content)) {
270 return i;
271 }
272 }
273
274 return -1;
275}
276
277void releasenickext(int index) {
278 int i;
279 nick *np;
280
281 freesstring(nickextnames[index]);
282 nickextnames[index]=NULL;
283
284 for (i=0;i<NICKHASHSIZE;i++) {
285 for (np=nicktable[i];np;np=np->next) {
286 np->exts[index]=NULL;
287 }
288 }
289}
290
291/* visiblehostmask
292 * Produces the "apparent" hostmask as seen by network users.
293 */
294
295char *visiblehostmask(nick *np, char *buf) {
296 char hostbuf[HOSTLEN+1];
297 char *ident, *host;
298
299 ident=np->ident;
300 host=np->host->name->content;
301
302 if (IsSetHost(np)) {
303 if (np->shident) {
304 ident=np->shident->content;
305 }
306 if (np->sethost) {
307 host=np->sethost->content;
308 }
309 } else if (IsAccount(np) && IsHideHost(np)) {
310 sprintf(hostbuf,"%s.%s", np->authname, HIS_HIDDENHOST);
311 host=hostbuf;
312 }
313
314 sprintf(buf,"%s!%s@%s",np->nick,ident,host);
315
316 return buf;
317}
318
319#if 0
320
321/*
322 * gethandlebynumeric:
323 * Given a numeric, gives the location in the servernicks table
324 * where it should be. Does not check that the nick currently found
325 * there (if any) has the correct numeric; this is left to the
326 * calling function to figure out.
327 */
328
329nick **gethandlebynumeric(long numeric) {
330 int servernum;
331 int maskednum;
332 server *serv;
333
334 /* Shift off the client identifier part of the numeric to get the server ID */
335 servernum=(numeric>>18);
336
337 if ((serv=getserverdata(servernum))==NULL) {
338 Error("nick",ERR_WARNING,"Numeric %ld refers to non-existent server %d",numeric,servernum);
339 return NULL;
340 }
341
342 /* Compute the masked numeric */
343 maskednum=numeric&(serv->maxusernum);
344
345 return (servernicks[servernum])+maskednum;
346}
347
348/*
349 * getnickbynumeric[str]()
350 * These functions retrieve a nick based on it's numeric on the network
351 * Use the approriate function depending on how your numeric is expressed..
352 */
353
354nick *getnickbynumeric(long numeric) {
355 nick **nh;
356
357 nh=gethandlebynumeric(numeric);
358
359 if ((*nh) && ((*nh)->numeric!=numeric)) {
360 /* We found a masked numeric match, but the actual numeric
361 * is different. This counts as a miss. */
362 return NULL;
363 }
364
365 return (*nh);
366}
367
368nick *getnickbynumericstr(char *numericstr) {
369 return getnickbynumeric(numerictolong(numericstr,5));
370}
371
372#endif
526e7c1d
P
373
374int registernodeext(const char *name) {
375 int i;
376
377 if (findnodeext(name)!=-1) {
378 Error("nick",ERR_WARNING,"Tried to register duplicate node extension %s",name);
379 return -1;
380 }
381
382 for (i=0;i<PATRICIA_MAXSLOTS;i++) {
383 if (nodeextnames[i]==NULL) {
384 nodeextnames[i]=getsstring(name,100);
385 return i;
386 }
387 }
388
389 Error("nick",ERR_WARNING,"Tried to register too many extensions: %s",name);
390 return -1;
391}
392
393int findnodeext(const char *name) {
394 int i;
395
396 for (i=0;i<PATRICIA_MAXSLOTS;i++) {
397 if (nodeextnames[i]!=NULL && !ircd_strcmp(name,nodeextnames[i]->content)) {
398 return i;
399 }
400 }
401
402 return -1;
403}
404
405void releasenodeext(int index) {
406 patricia_node_t *head, *node;
407
408 freesstring(nodeextnames[index]);
409 nodeextnames[index]=NULL;
410
411 head = iptree->head;
412
413 PATRICIA_WALK_ALL(head, node)
414 {
a8ba1373 415 node->exts[index]=NULL;
526e7c1d
P
416 } PATRICIA_WALK_END;
417}