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