]> jfr.im git - irc/quakenet/newserv.git/blame - nick/nick.c
core: Create the data directory if it doesn't already exist.
[irc/quakenet/newserv.git] / nick / nick.c
CommitLineData
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"
4208eafd 15#include "../lib/ccassert.h"
103521a1 16#include "../core/nsmalloc.h"
17
c86edd1d
Q
18#include <stdlib.h>
19#include <string.h>
20#include <stdio.h>
21
70b0a4e5 22MODULE_VERSION("");
87698d77 23
4208eafd
GB
24CCASSERT(sizeof(host) == sizeof(realname));
25
c86edd1d
Q
26const 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 },
e98d808a 40 { 'P', UMODE_PARANOID },
c9d0dfa3
GB
41 { 'q', UMODE_COMCHANS },
42 { 'Q', UMODE_COMCHANSRESTR },
42d7af35 43 { 'C', UMODE_CLOAKED },
c86edd1d
Q
44 { '\0', 0 } };
45
c153c0dc 46const flag accountflags[] = {
c4ffdb9b 47 { 'q', AFLAG_STAFF },
3294b10b 48 { 'h', AFLAG_SUPPORT },
c4ffdb9b
CP
49 { 'o', AFLAG_OPER },
50 { 'a', AFLAG_ADMIN },
c153c0dc
CP
51 { 'd', AFLAG_DEVELOPER },
52 { '\0', 0 } };
53
c86edd1d
Q
54#define nickhash(x) ((crc32i(x))%NICKHASHSIZE)
55
56nick *nicktable[NICKHASHSIZE];
57nick **servernicks[MAXSERVERS];
58
59sstring *nickextnames[MAXNICKEXTS];
60
61void nickstats(int hooknum, void *arg);
62
3404a8f0
CP
63char *NULLAUTHNAME = "";
64
c86edd1d 65void _init() {
e31727be 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
c86edd1d
Q
74 initnickhelpers();
75 memset(nicktable,0,sizeof(nicktable));
76 memset(servernicks,0,sizeof(servernicks));
86a9e70c
P
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 }
c86edd1d
Q
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);
47657339 95 registerserverhandler("AC",&handleaccountmsg,4);
6885ae9c 96 registerserverhandler("P",&handleprivmsg,2);
5144ddc4 97 registerserverhandler("A",&handleawaymsg,1);
42d7af35
GB
98 registerserverhandler("CA",&handleaddcloak,1);
99 registerserverhandler("CU",&handleclearcloak,0);
c86edd1d
Q
100
101 /* Fake the addition of our own server */
102 handleserverchange(HOOK_SERVER_NEWSERVER,(void *)numerictolong(mynumeric->content,2));
103}
104
103521a1 105void _fini() {
9cf0b03f
CP
106 nick *np;
107 int i;
108
707c5824 109 fininickhelpers();
9cf0b03f
CP
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);
843184e3 115 freesstring(np->opername);
3404a8f0 116 if(!np->auth && np->authname && (np->authname != NULLAUTHNAME))
3294b10b 117 free(np->authname);
9cf0b03f
CP
118 }
119 }
120
df5ab174 121 nsfreeall(POOL_NICK);
707c5824 122
103521a1 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);
7bec4aeb 133 deregisterserverhandler("AC",&handleaccountmsg);
6885ae9c 134 deregisterserverhandler("P",&handleprivmsg);
5144ddc4 135 deregisterserverhandler("A",&handleawaymsg);
42d7af35
GB
136 deregisterserverhandler("CA",&handleaddcloak);
137 deregisterserverhandler("CU",&handleclearcloak);
103521a1 138}
139
c86edd1d
Q
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
146void handleserverchange(int hooknum, void *arg) {
c3db6f7e 147 long servernum;
c86edd1d
Q
148 int i;
149
c3db6f7e 150 servernum=(long)arg;
c86edd1d
Q
151
152 switch(hooknum) {
153 case HOOK_SERVER_NEWSERVER:
103521a1 154 servernicks[servernum]=(nick **)nsmalloc(POOL_NICK,(serverlist[servernum].maxusernum+1)*sizeof(nick **));
c86edd1d
Q
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 }
2a010087 164 nsfree(POOL_NICK,servernicks[servernum]);
c86edd1d
Q
165 break;
166 }
167}
168
169/*
170 * deletenick:
171 *
172 * This function handles the removal of a nick from the network
173 */
174
175void 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
3294b10b
CP
200 if(IsAccount(np)) {
201 if(!np->auth) {
3404a8f0 202 if(np->authname && (np->authname != NULLAUTHNAME))
3294b10b
CP
203 free(np->authname);
204 } else {
205 np->auth->usercount--;
47657339 206
3294b10b
CP
207 for (nh=&(np->auth->nicks);*nh;nh=&((*nh)->nextbyauthname)) {
208 if (*nh==np) {
209 *nh=np->nextbyauthname;
210 break;
211 }
47657339 212 }
47657339 213
3294b10b
CP
214 releaseauthname(np->auth);
215 }
47657339
C
216 }
217
c86edd1d
Q
218 freesstring(np->shident); /* freesstring(NULL) is OK */
219 freesstring(np->sethost);
843184e3 220 freesstring(np->opername);
c86edd1d 221
96644df6 222 node_decrement_usercount(np->ipnode);
526e7c1d
P
223 derefnode(iptree, np->ipnode);
224
225 /* TODO: figure out how to cleanly remove nodes without affecting other modules */
226
42d7af35
GB
227 /* Remove cloak entries for the user */
228 removecloaktarget(np);
229 clearcloaktargets(np);
230
c86edd1d
Q
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
240void addnicktohash(nick *np) {
241 np->next=nicktable[nickhash(np->nick)];
242 nicktable[nickhash(np->nick)]=np;
243}
244
245void 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
256nick *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
267void 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
c3db6f7e 288 if ((long)arg>5) {
c86edd1d
Q
289 /* Full stats */
290 sprintf(buf,"Nick : %6d nicks (HASH: %6d/%6d, chain %3d)",total,buckets,NICKHASHSIZE,maxchain);
c3db6f7e 291 } else if ((long)arg>2) {
c86edd1d
Q
292 sprintf(buf,"Nick : %6d users on network.",total);
293 }
294
c3db6f7e 295 if ((long)arg>2) {
c86edd1d
Q
296 triggerhook(HOOK_CORE_STATSREPLY,buf);
297 }
298}
299
300int 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
47657339 315 Error("nick",ERR_WARNING,"Tried to register too many nick extensions: %s",name);
c86edd1d
Q
316 return -1;
317}
318
319int 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
331void 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
349char *visiblehostmask(nick *np, char *buf) {
28252fef 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
362char *visibleuserhost(nick *np, char *buf) {
c86edd1d
Q
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
28252fef 381 sprintf(buf,"%s@%s",ident,host);
c86edd1d
Q
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
396nick **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
421nick *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
435nick *getnickbynumericstr(char *numericstr) {
436 return getnickbynumeric(numerictolong(numericstr,5));
437}
438
cebc4cab 439
c86edd1d 440#endif
526e7c1d 441
42d7af35
GB
442int canseeuser(nick *np, nick *cloaked)
443{
444 return (np == cloaked ||
445 !IsCloaked(cloaked) ||
446 np->cloak_extra == cloaked);
447}
448
449void addcloaktarget(nick *cloaked, nick *target)
450{
451 removecloaktarget(target);
452
453 target->cloak_extra = cloaked;
454 cloaked->cloak_count++;
455}
456
457void removecloaktarget(nick *target)
458{
459 if (target->cloak_extra) {
460 target->cloak_extra->cloak_count--;
461 target->cloak_extra = NULL;
462 }
463}
464
465void clearcloaktargets(nick *cloaked)
466{
467 nick *tnp;
468 int j;
469
260e4351
GB
470 if (cloaked->cloak_count == 0)
471 return;
472
42d7af35
GB
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