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