]>
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" |
103521a1 | 15 | #include "../core/nsmalloc.h" |
16 | ||
c86edd1d Q |
17 | #include <stdlib.h> |
18 | #include <string.h> | |
19 | #include <stdio.h> | |
20 | ||
70b0a4e5 | 21 | MODULE_VERSION(""); |
87698d77 | 22 | |
c86edd1d Q |
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 }, | |
e98d808a | 37 | { 'P', UMODE_PARANOID }, |
42d7af35 | 38 | { 'C', UMODE_CLOAKED }, |
c86edd1d Q |
39 | { '\0', 0 } }; |
40 | ||
c153c0dc | 41 | const flag accountflags[] = { |
c4ffdb9b | 42 | { 'q', AFLAG_STAFF }, |
3294b10b | 43 | { 'h', AFLAG_SUPPORT }, |
c4ffdb9b CP |
44 | { 'o', AFLAG_OPER }, |
45 | { 'a', AFLAG_ADMIN }, | |
c153c0dc CP |
46 | { 'd', AFLAG_DEVELOPER }, |
47 | { '\0', 0 } }; | |
48 | ||
c86edd1d Q |
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 | ||
3404a8f0 CP |
58 | char *NULLAUTHNAME = ""; |
59 | ||
c86edd1d | 60 | void _init() { |
e31727be | 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 | ||
c86edd1d Q |
69 | initnickalloc(); |
70 | initnickhelpers(); | |
71 | memset(nicktable,0,sizeof(nicktable)); | |
72 | memset(servernicks,0,sizeof(servernicks)); | |
86a9e70c P |
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 | } | |
c86edd1d Q |
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); | |
47657339 | 91 | registerserverhandler("AC",&handleaccountmsg,4); |
6885ae9c | 92 | registerserverhandler("P",&handleprivmsg,2); |
5144ddc4 | 93 | registerserverhandler("A",&handleawaymsg,1); |
42d7af35 GB |
94 | registerserverhandler("CA",&handleaddcloak,1); |
95 | registerserverhandler("CU",&handleclearcloak,0); | |
c86edd1d Q |
96 | |
97 | /* Fake the addition of our own server */ | |
98 | handleserverchange(HOOK_SERVER_NEWSERVER,(void *)numerictolong(mynumeric->content,2)); | |
99 | } | |
100 | ||
103521a1 | 101 | void _fini() { |
9cf0b03f CP |
102 | nick *np; |
103 | int i; | |
104 | ||
707c5824 | 105 | fininickhelpers(); |
9cf0b03f CP |
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); | |
843184e3 | 111 | freesstring(np->opername); |
3404a8f0 | 112 | if(!np->auth && np->authname && (np->authname != NULLAUTHNAME)) |
3294b10b | 113 | free(np->authname); |
9cf0b03f CP |
114 | } |
115 | } | |
116 | ||
df5ab174 | 117 | nsfreeall(POOL_NICK); |
707c5824 | 118 | |
103521a1 | 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); | |
7bec4aeb | 129 | deregisterserverhandler("AC",&handleaccountmsg); |
6885ae9c | 130 | deregisterserverhandler("P",&handleprivmsg); |
5144ddc4 | 131 | deregisterserverhandler("A",&handleawaymsg); |
42d7af35 GB |
132 | deregisterserverhandler("CA",&handleaddcloak); |
133 | deregisterserverhandler("CU",&handleclearcloak); | |
103521a1 | 134 | } |
135 | ||
c86edd1d Q |
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) { | |
c3db6f7e | 143 | long servernum; |
c86edd1d Q |
144 | int i; |
145 | ||
c3db6f7e | 146 | servernum=(long)arg; |
c86edd1d Q |
147 | |
148 | switch(hooknum) { | |
149 | case HOOK_SERVER_NEWSERVER: | |
103521a1 | 150 | servernicks[servernum]=(nick **)nsmalloc(POOL_NICK,(serverlist[servernum].maxusernum+1)*sizeof(nick **)); |
c86edd1d Q |
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 | } | |
2a010087 | 160 | nsfree(POOL_NICK,servernicks[servernum]); |
c86edd1d Q |
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 | ||
3294b10b CP |
196 | if(IsAccount(np)) { |
197 | if(!np->auth) { | |
3404a8f0 | 198 | if(np->authname && (np->authname != NULLAUTHNAME)) |
3294b10b CP |
199 | free(np->authname); |
200 | } else { | |
201 | np->auth->usercount--; | |
47657339 | 202 | |
3294b10b CP |
203 | for (nh=&(np->auth->nicks);*nh;nh=&((*nh)->nextbyauthname)) { |
204 | if (*nh==np) { | |
205 | *nh=np->nextbyauthname; | |
206 | break; | |
207 | } | |
47657339 | 208 | } |
47657339 | 209 | |
3294b10b CP |
210 | releaseauthname(np->auth); |
211 | } | |
47657339 C |
212 | } |
213 | ||
c86edd1d Q |
214 | freesstring(np->shident); /* freesstring(NULL) is OK */ |
215 | freesstring(np->sethost); | |
843184e3 | 216 | freesstring(np->opername); |
c86edd1d | 217 | |
96644df6 | 218 | node_decrement_usercount(np->ipnode); |
526e7c1d P |
219 | derefnode(iptree, np->ipnode); |
220 | ||
221 | /* TODO: figure out how to cleanly remove nodes without affecting other modules */ | |
222 | ||
42d7af35 GB |
223 | /* Remove cloak entries for the user */ |
224 | removecloaktarget(np); | |
225 | clearcloaktargets(np); | |
226 | ||
c86edd1d Q |
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 | ||
c3db6f7e | 284 | if ((long)arg>5) { |
c86edd1d Q |
285 | /* Full stats */ |
286 | sprintf(buf,"Nick : %6d nicks (HASH: %6d/%6d, chain %3d)",total,buckets,NICKHASHSIZE,maxchain); | |
c3db6f7e | 287 | } else if ((long)arg>2) { |
c86edd1d Q |
288 | sprintf(buf,"Nick : %6d users on network.",total); |
289 | } | |
290 | ||
c3db6f7e | 291 | if ((long)arg>2) { |
c86edd1d Q |
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 | ||
47657339 | 311 | Error("nick",ERR_WARNING,"Tried to register too many nick extensions: %s",name); |
c86edd1d Q |
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) { | |
28252fef | 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) { | |
c86edd1d Q |
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 | ||
28252fef | 377 | sprintf(buf,"%s@%s",ident,host); |
c86edd1d Q |
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 | ||
cebc4cab | 435 | |
c86edd1d | 436 | #endif |
526e7c1d | 437 | |
42d7af35 GB |
438 | int canseeuser(nick *np, nick *cloaked) |
439 | { | |
440 | return (np == cloaked || | |
441 | !IsCloaked(cloaked) || | |
442 | np->cloak_extra == cloaked); | |
443 | } | |
444 | ||
445 | void addcloaktarget(nick *cloaked, nick *target) | |
446 | { | |
447 | removecloaktarget(target); | |
448 | ||
449 | target->cloak_extra = cloaked; | |
450 | cloaked->cloak_count++; | |
451 | } | |
452 | ||
453 | void removecloaktarget(nick *target) | |
454 | { | |
455 | if (target->cloak_extra) { | |
456 | target->cloak_extra->cloak_count--; | |
457 | target->cloak_extra = NULL; | |
458 | } | |
459 | } | |
460 | ||
461 | void clearcloaktargets(nick *cloaked) | |
462 | { | |
463 | nick *tnp; | |
464 | int j; | |
465 | ||
466 | for(j=0;j<NICKHASHSIZE;j++) | |
467 | for(tnp=nicktable[j];tnp;tnp=tnp->next) | |
468 | if (tnp->cloak_extra == cloaked) | |
469 | tnp->cloak_extra = NULL; | |
470 | ||
471 | cloaked->cloak_count = 0; | |
472 | } | |
473 |