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