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