]> jfr.im git - irc/quakenet/newserv.git/blame - nick/nick.c
Remove custom allocators from nick/patricia.
[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 },
42d7af35 38 { 'C', UMODE_CLOAKED },
c86edd1d
Q
39 { '\0', 0 } };
40
c153c0dc 41const flag accountflags[] = {
c4ffdb9b 42 { 'q', AFLAG_STAFF },
3294b10b 43 { 'h', AFLAG_SUPPORT },
c4ffdb9b
CP
44 { 'o', AFLAG_OPER },
45 { 'a', AFLAG_ADMIN },
c153c0dc
CP
46 { 'd', AFLAG_DEVELOPER },
47 { '\0', 0 } };
48
c86edd1d
Q
49#define nickhash(x) ((crc32i(x))%NICKHASHSIZE)
50
51nick *nicktable[NICKHASHSIZE];
52nick **servernicks[MAXSERVERS];
53
54sstring *nickextnames[MAXNICKEXTS];
55
56void nickstats(int hooknum, void *arg);
57
3404a8f0
CP
58char *NULLAUTHNAME = "";
59
c86edd1d 60void _init() {
e31727be 61 unsigned int i;
62 authname *anp;
63
64 /* Clear up the nicks in authext */
65 for (i=0;i<AUTHNAMEHASHSIZE;i++)
66 for (anp=authnametable[i];anp;anp=anp->next)
67 anp->nicks=NULL;
68
c86edd1d
Q
69 initnickhelpers();
70 memset(nicktable,0,sizeof(nicktable));
71 memset(servernicks,0,sizeof(servernicks));
86a9e70c
P
72
73 /* If we're connected to IRC, force a disconnect. This needs to be done
74 * before we register all our hooks which would otherwise get called
75 * during the disconnect. */
76 if (connected) {
77 irc_send("%s SQ %s 0 :Resync [adding nick support]",mynumeric->content,myserver->content); irc_disconnected();
78 }
c86edd1d
Q
79
80 /* Register our hooks */
81 registerhook(HOOK_SERVER_NEWSERVER,&handleserverchange);
82 registerhook(HOOK_SERVER_LOSTSERVER,&handleserverchange);
83 registerhook(HOOK_CORE_STATSREQUEST,&nickstats);
84
85 /* And our server handlers */
86 registerserverhandler("N",&handlenickmsg,10);
87 registerserverhandler("D",&handlekillmsg,2);
88 registerserverhandler("Q",&handlequitmsg,1);
89 registerserverhandler("M",&handleusermodemsg,3);
47657339 90 registerserverhandler("AC",&handleaccountmsg,4);
6885ae9c 91 registerserverhandler("P",&handleprivmsg,2);
5144ddc4 92 registerserverhandler("A",&handleawaymsg,1);
42d7af35
GB
93 registerserverhandler("CA",&handleaddcloak,1);
94 registerserverhandler("CU",&handleclearcloak,0);
c86edd1d
Q
95
96 /* Fake the addition of our own server */
97 handleserverchange(HOOK_SERVER_NEWSERVER,(void *)numerictolong(mynumeric->content,2));
98}
99
103521a1 100void _fini() {
9cf0b03f
CP
101 nick *np;
102 int i;
103
707c5824 104 fininickhelpers();
9cf0b03f
CP
105
106 for (i=0;i<NICKHASHSIZE;i++) {
107 for (np=nicktable[i];np;np=np->next) {
108 freesstring(np->shident);
109 freesstring(np->sethost);
843184e3 110 freesstring(np->opername);
3404a8f0 111 if(!np->auth && np->authname && (np->authname != NULLAUTHNAME))
3294b10b 112 free(np->authname);
9cf0b03f
CP
113 }
114 }
115
df5ab174 116 nsfreeall(POOL_NICK);
707c5824 117
103521a1 118 /* Free the hooks */
119 deregisterhook(HOOK_SERVER_NEWSERVER,&handleserverchange);
120 deregisterhook(HOOK_SERVER_LOSTSERVER,&handleserverchange);
121 deregisterhook(HOOK_CORE_STATSREQUEST,&nickstats);
122
123 /* And our server handlers */
124 deregisterserverhandler("N",&handlenickmsg);
125 deregisterserverhandler("D",&handlekillmsg);
126 deregisterserverhandler("Q",&handlequitmsg);
127 deregisterserverhandler("M",&handleusermodemsg);
7bec4aeb 128 deregisterserverhandler("AC",&handleaccountmsg);
6885ae9c 129 deregisterserverhandler("P",&handleprivmsg);
5144ddc4 130 deregisterserverhandler("A",&handleawaymsg);
42d7af35
GB
131 deregisterserverhandler("CA",&handleaddcloak);
132 deregisterserverhandler("CU",&handleclearcloak);
103521a1 133}
134
c86edd1d
Q
135/*
136 * This function handles servers appearing and disappearing.
137 * For a new server, the client table is allocated.
138 * For a disappearing server, all it's clients are killed and the client table is freed.
139 */
140
141void handleserverchange(int hooknum, void *arg) {
c3db6f7e 142 long servernum;
c86edd1d
Q
143 int i;
144
c3db6f7e 145 servernum=(long)arg;
c86edd1d
Q
146
147 switch(hooknum) {
148 case HOOK_SERVER_NEWSERVER:
103521a1 149 servernicks[servernum]=(nick **)nsmalloc(POOL_NICK,(serverlist[servernum].maxusernum+1)*sizeof(nick **));
c86edd1d
Q
150 memset(servernicks[servernum],0,(serverlist[servernum].maxusernum+1)*sizeof(nick **));
151 break;
152
153 case HOOK_SERVER_LOSTSERVER:
154 for (i=0;i<=serverlist[servernum].maxusernum;i++) {
155 if (servernicks[servernum][i]!=NULL) {
156 deletenick(servernicks[servernum][i]);
157 }
158 }
2a010087 159 nsfree(POOL_NICK,servernicks[servernum]);
c86edd1d
Q
160 break;
161 }
162}
163
164/*
165 * deletenick:
166 *
167 * This function handles the removal of a nick from the network
168 */
169
170void deletenick(nick *np) {
171 nick **nh;
172
173 /* Fire the hook. This will deal with removal from channels etc. */
174 triggerhook(HOOK_NICK_LOSTNICK, np);
175
176 /* Release the realname and hostname parts */
177
178 for (nh=&(np->realname->nicks);*nh;nh=&((*nh)->nextbyrealname)) {
179 if (*nh==np) {
180 *nh=np->nextbyrealname;
181 break;
182 }
183 }
184
185 for (nh=&(np->host->nicks);*nh;nh=&((*nh)->nextbyhost)) {
186 if (*nh==np) {
187 *nh=np->nextbyhost;
188 break;
189 }
190 }
191
192 releaserealname(np->realname);
193 releasehost(np->host);
194
3294b10b
CP
195 if(IsAccount(np)) {
196 if(!np->auth) {
3404a8f0 197 if(np->authname && (np->authname != NULLAUTHNAME))
3294b10b
CP
198 free(np->authname);
199 } else {
200 np->auth->usercount--;
47657339 201
3294b10b
CP
202 for (nh=&(np->auth->nicks);*nh;nh=&((*nh)->nextbyauthname)) {
203 if (*nh==np) {
204 *nh=np->nextbyauthname;
205 break;
206 }
47657339 207 }
47657339 208
3294b10b
CP
209 releaseauthname(np->auth);
210 }
47657339
C
211 }
212
c86edd1d
Q
213 freesstring(np->shident); /* freesstring(NULL) is OK */
214 freesstring(np->sethost);
843184e3 215 freesstring(np->opername);
c86edd1d 216
96644df6 217 node_decrement_usercount(np->ipnode);
526e7c1d
P
218 derefnode(iptree, np->ipnode);
219
220 /* TODO: figure out how to cleanly remove nodes without affecting other modules */
221
42d7af35
GB
222 /* Remove cloak entries for the user */
223 removecloaktarget(np);
224 clearcloaktargets(np);
225
c86edd1d
Q
226 /* Delete the nick from the servernick table */
227 *(gethandlebynumericunsafe(np->numeric))=NULL;
228
229 /* Remove the nick from the hash table */
230 removenickfromhash(np);
231
232 freenick(np);
233}
234
235void addnicktohash(nick *np) {
236 np->next=nicktable[nickhash(np->nick)];
237 nicktable[nickhash(np->nick)]=np;
238}
239
240void removenickfromhash(nick *np) {
241 nick **nh;
242
243 for (nh=&(nicktable[nickhash(np->nick)]);*nh;nh=&((*nh)->next)) {
244 if ((*nh)==np) {
245 (*nh)=np->next;
246 break;
247 }
248 }
249}
250
251nick *getnickbynick(const char *name) {
252 nick *np;
253
254 for (np=nicktable[nickhash(name)];np;np=np->next) {
255 if (!ircd_strcmp(np->nick,name))
256 return np;
257 }
258
259 return NULL;
260}
261
262void nickstats(int hooknum, void *arg) {
263 int total,maxchain,curchain,i,buckets;
264 nick *np;
265 char buf[200];
266
267 /* Get nick stats */
268 buckets=total=maxchain=curchain=0;
269 for (i=0;i<NICKHASHSIZE;i++,curchain=0) {
270 np=nicktable[i];
271 if (np!=NULL) {
272 buckets++;
273 for (;np;np=np->next) {
274 total++;
275 curchain++;
276 }
277 }
278 if (curchain>maxchain) {
279 maxchain=curchain;
280 }
281 }
282
c3db6f7e 283 if ((long)arg>5) {
c86edd1d
Q
284 /* Full stats */
285 sprintf(buf,"Nick : %6d nicks (HASH: %6d/%6d, chain %3d)",total,buckets,NICKHASHSIZE,maxchain);
c3db6f7e 286 } else if ((long)arg>2) {
c86edd1d
Q
287 sprintf(buf,"Nick : %6d users on network.",total);
288 }
289
c3db6f7e 290 if ((long)arg>2) {
c86edd1d
Q
291 triggerhook(HOOK_CORE_STATSREPLY,buf);
292 }
293}
294
295int registernickext(const char *name) {
296 int i;
297
298 if (findnickext(name)!=-1) {
299 Error("nick",ERR_WARNING,"Tried to register duplicate nick extension %s",name);
300 return -1;
301 }
302
303 for (i=0;i<MAXNICKEXTS;i++) {
304 if (nickextnames[i]==NULL) {
305 nickextnames[i]=getsstring(name,100);
306 return i;
307 }
308 }
309
47657339 310 Error("nick",ERR_WARNING,"Tried to register too many nick extensions: %s",name);
c86edd1d
Q
311 return -1;
312}
313
314int findnickext(const char *name) {
315 int i;
316
317 for (i=0;i<MAXNICKEXTS;i++) {
318 if (nickextnames[i]!=NULL && !ircd_strcmp(name,nickextnames[i]->content)) {
319 return i;
320 }
321 }
322
323 return -1;
324}
325
326void releasenickext(int index) {
327 int i;
328 nick *np;
329
330 freesstring(nickextnames[index]);
331 nickextnames[index]=NULL;
332
333 for (i=0;i<NICKHASHSIZE;i++) {
334 for (np=nicktable[i];np;np=np->next) {
335 np->exts[index]=NULL;
336 }
337 }
338}
339
340/* visiblehostmask
341 * Produces the "apparent" hostmask as seen by network users.
342 */
343
344char *visiblehostmask(nick *np, char *buf) {
28252fef 345 char uhbuf[USERLEN+HOSTLEN+2];
346
347 visibleuserhost(np, uhbuf);
348 sprintf(buf,"%s!%s",np->nick,uhbuf);
349
350 return buf;
351}
352
353/* visibleuserhost
354 * As above without nick
355 */
356
357char *visibleuserhost(nick *np, char *buf) {
c86edd1d
Q
358 char hostbuf[HOSTLEN+1];
359 char *ident, *host;
360
361 ident=np->ident;
362 host=np->host->name->content;
363
364 if (IsSetHost(np)) {
365 if (np->shident) {
366 ident=np->shident->content;
367 }
368 if (np->sethost) {
369 host=np->sethost->content;
370 }
371 } else if (IsAccount(np) && IsHideHost(np)) {
372 sprintf(hostbuf,"%s.%s", np->authname, HIS_HIDDENHOST);
373 host=hostbuf;
374 }
375
28252fef 376 sprintf(buf,"%s@%s",ident,host);
c86edd1d
Q
377
378 return buf;
379}
380
381#if 0
382
383/*
384 * gethandlebynumeric:
385 * Given a numeric, gives the location in the servernicks table
386 * where it should be. Does not check that the nick currently found
387 * there (if any) has the correct numeric; this is left to the
388 * calling function to figure out.
389 */
390
391nick **gethandlebynumeric(long numeric) {
392 int servernum;
393 int maskednum;
394 server *serv;
395
396 /* Shift off the client identifier part of the numeric to get the server ID */
397 servernum=(numeric>>18);
398
399 if ((serv=getserverdata(servernum))==NULL) {
400 Error("nick",ERR_WARNING,"Numeric %ld refers to non-existent server %d",numeric,servernum);
401 return NULL;
402 }
403
404 /* Compute the masked numeric */
405 maskednum=numeric&(serv->maxusernum);
406
407 return (servernicks[servernum])+maskednum;
408}
409
410/*
411 * getnickbynumeric[str]()
412 * These functions retrieve a nick based on it's numeric on the network
413 * Use the approriate function depending on how your numeric is expressed..
414 */
415
416nick *getnickbynumeric(long numeric) {
417 nick **nh;
418
419 nh=gethandlebynumeric(numeric);
420
421 if ((*nh) && ((*nh)->numeric!=numeric)) {
422 /* We found a masked numeric match, but the actual numeric
423 * is different. This counts as a miss. */
424 return NULL;
425 }
426
427 return (*nh);
428}
429
430nick *getnickbynumericstr(char *numericstr) {
431 return getnickbynumeric(numerictolong(numericstr,5));
432}
433
434#endif
526e7c1d 435
42d7af35
GB
436int canseeuser(nick *np, nick *cloaked)
437{
438 return (np == cloaked ||
439 !IsCloaked(cloaked) ||
440 np->cloak_extra == cloaked);
441}
442
443void addcloaktarget(nick *cloaked, nick *target)
444{
445 removecloaktarget(target);
446
447 target->cloak_extra = cloaked;
448 cloaked->cloak_count++;
449}
450
451void removecloaktarget(nick *target)
452{
453 if (target->cloak_extra) {
454 target->cloak_extra->cloak_count--;
455 target->cloak_extra = NULL;
456 }
457}
458
459void clearcloaktargets(nick *cloaked)
460{
461 nick *tnp;
462 int j;
463
464 for(j=0;j<NICKHASHSIZE;j++)
465 for(tnp=nicktable[j];tnp;tnp=tnp->next)
466 if (tnp->cloak_extra == cloaked)
467 tnp->cloak_extra = NULL;
468
469 cloaked->cloak_count = 0;
470}
471