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