]>
Commit | Line | Data |
---|---|---|
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 "../lib/ccassert.h" | |
16 | #include "../core/nsmalloc.h" | |
17 | ||
18 | #include <stdlib.h> | |
19 | #include <string.h> | |
20 | #include <stdio.h> | |
21 | ||
22 | MODULE_VERSION(""); | |
23 | ||
24 | CCASSERT(sizeof(host) == sizeof(realname)); | |
25 | ||
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 }, | |
40 | { 'P', UMODE_PARANOID }, | |
41 | { 'C', UMODE_CLOAKED }, | |
42 | { '\0', 0 } }; | |
43 | ||
44 | const flag accountflags[] = { | |
45 | { 'q', AFLAG_STAFF }, | |
46 | { 'h', AFLAG_SUPPORT }, | |
47 | { 'o', AFLAG_OPER }, | |
48 | { 'a', AFLAG_ADMIN }, | |
49 | { 'd', AFLAG_DEVELOPER }, | |
50 | { '\0', 0 } }; | |
51 | ||
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 | ||
61 | char *NULLAUTHNAME = ""; | |
62 | ||
63 | void _init() { | |
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 | ||
72 | initnickhelpers(); | |
73 | memset(nicktable,0,sizeof(nicktable)); | |
74 | memset(servernicks,0,sizeof(servernicks)); | |
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 | } | |
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); | |
93 | registerserverhandler("AC",&handleaccountmsg,4); | |
94 | registerserverhandler("P",&handleprivmsg,2); | |
95 | registerserverhandler("A",&handleawaymsg,1); | |
96 | registerserverhandler("CA",&handleaddcloak,1); | |
97 | registerserverhandler("CU",&handleclearcloak,0); | |
98 | ||
99 | /* Fake the addition of our own server */ | |
100 | handleserverchange(HOOK_SERVER_NEWSERVER,(void *)numerictolong(mynumeric->content,2)); | |
101 | } | |
102 | ||
103 | void _fini() { | |
104 | nick *np; | |
105 | int i; | |
106 | ||
107 | fininickhelpers(); | |
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); | |
113 | freesstring(np->opername); | |
114 | if(!np->auth && np->authname && (np->authname != NULLAUTHNAME)) | |
115 | free(np->authname); | |
116 | } | |
117 | } | |
118 | ||
119 | nsfreeall(POOL_NICK); | |
120 | ||
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); | |
131 | deregisterserverhandler("AC",&handleaccountmsg); | |
132 | deregisterserverhandler("P",&handleprivmsg); | |
133 | deregisterserverhandler("A",&handleawaymsg); | |
134 | deregisterserverhandler("CA",&handleaddcloak); | |
135 | deregisterserverhandler("CU",&handleclearcloak); | |
136 | } | |
137 | ||
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) { | |
145 | long servernum; | |
146 | int i; | |
147 | ||
148 | servernum=(long)arg; | |
149 | ||
150 | switch(hooknum) { | |
151 | case HOOK_SERVER_NEWSERVER: | |
152 | servernicks[servernum]=(nick **)nsmalloc(POOL_NICK,(serverlist[servernum].maxusernum+1)*sizeof(nick **)); | |
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 | } | |
162 | nsfree(POOL_NICK,servernicks[servernum]); | |
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 | ||
198 | if(IsAccount(np)) { | |
199 | if(!np->auth) { | |
200 | if(np->authname && (np->authname != NULLAUTHNAME)) | |
201 | free(np->authname); | |
202 | } else { | |
203 | np->auth->usercount--; | |
204 | ||
205 | for (nh=&(np->auth->nicks);*nh;nh=&((*nh)->nextbyauthname)) { | |
206 | if (*nh==np) { | |
207 | *nh=np->nextbyauthname; | |
208 | break; | |
209 | } | |
210 | } | |
211 | ||
212 | releaseauthname(np->auth); | |
213 | } | |
214 | } | |
215 | ||
216 | freesstring(np->shident); /* freesstring(NULL) is OK */ | |
217 | freesstring(np->sethost); | |
218 | freesstring(np->opername); | |
219 | ||
220 | node_decrement_usercount(np->ipnode); | |
221 | derefnode(iptree, np->ipnode); | |
222 | ||
223 | /* TODO: figure out how to cleanly remove nodes without affecting other modules */ | |
224 | ||
225 | /* Remove cloak entries for the user */ | |
226 | removecloaktarget(np); | |
227 | clearcloaktargets(np); | |
228 | ||
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 | ||
286 | if ((long)arg>5) { | |
287 | /* Full stats */ | |
288 | sprintf(buf,"Nick : %6d nicks (HASH: %6d/%6d, chain %3d)",total,buckets,NICKHASHSIZE,maxchain); | |
289 | } else if ((long)arg>2) { | |
290 | sprintf(buf,"Nick : %6d users on network.",total); | |
291 | } | |
292 | ||
293 | if ((long)arg>2) { | |
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 | ||
313 | Error("nick",ERR_WARNING,"Tried to register too many nick extensions: %s",name); | |
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) { | |
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) { | |
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 | ||
379 | sprintf(buf,"%s@%s",ident,host); | |
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 | ||
437 | ||
438 | #endif | |
439 | ||
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 | ||
468 | for(j=0;j<NICKHASHSIZE;j++) | |
469 | for(tnp=nicktable[j];tnp;tnp=tnp->next) | |
470 | if (tnp->cloak_extra == cloaked) | |
471 | tnp->cloak_extra = NULL; | |
472 | ||
473 | cloaked->cloak_count = 0; | |
474 | } | |
475 |