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