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