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