]> jfr.im git - irc/quakenet/newserv.git/blame - nick/nick.c
Merge pull request #1 from meeb/meeb
[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
729c4971 54#define nickhash(x) ((irc_crc32i(x))%NICKHASHSIZE)
c86edd1d
Q
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) {
6c510e06 82 irc_send("%s SQ %s 0 :Resync [adding nick support]",mynumeric->content,myserver->content); irc_disconnected(0);
86a9e70c 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
b2b2f35f 178 /* Fire a pre-lostnick trigger to allow hooks to check the channels etc. of a lost nick */
179 triggerhook(HOOK_NICK_PRE_LOSTNICK, np);
180
c86edd1d
Q
181 /* Fire the hook. This will deal with removal from channels etc. */
182 triggerhook(HOOK_NICK_LOSTNICK, np);
183
184 /* Release the realname and hostname parts */
185
186 for (nh=&(np->realname->nicks);*nh;nh=&((*nh)->nextbyrealname)) {
187 if (*nh==np) {
188 *nh=np->nextbyrealname;
189 break;
190 }
191 }
192
193 for (nh=&(np->host->nicks);*nh;nh=&((*nh)->nextbyhost)) {
194 if (*nh==np) {
195 *nh=np->nextbyhost;
196 break;
197 }
198 }
199
200 releaserealname(np->realname);
201 releasehost(np->host);
202
3294b10b
CP
203 if(IsAccount(np)) {
204 if(!np->auth) {
3404a8f0 205 if(np->authname && (np->authname != NULLAUTHNAME))
3294b10b
CP
206 free(np->authname);
207 } else {
208 np->auth->usercount--;
47657339 209
3294b10b
CP
210 for (nh=&(np->auth->nicks);*nh;nh=&((*nh)->nextbyauthname)) {
211 if (*nh==np) {
212 *nh=np->nextbyauthname;
213 break;
214 }
47657339 215 }
47657339 216
3294b10b
CP
217 releaseauthname(np->auth);
218 }
47657339
C
219 }
220
c86edd1d
Q
221 freesstring(np->shident); /* freesstring(NULL) is OK */
222 freesstring(np->sethost);
843184e3 223 freesstring(np->opername);
80e32fcd 224 freesstring(np->message);
c86edd1d 225
96644df6 226 node_decrement_usercount(np->ipnode);
526e7c1d
P
227 derefnode(iptree, np->ipnode);
228
229 /* TODO: figure out how to cleanly remove nodes without affecting other modules */
230
42d7af35
GB
231 /* Remove cloak entries for the user */
232 removecloaktarget(np);
233 clearcloaktargets(np);
234
c86edd1d
Q
235 /* Delete the nick from the servernick table */
236 *(gethandlebynumericunsafe(np->numeric))=NULL;
237
238 /* Remove the nick from the hash table */
239 removenickfromhash(np);
240
241 freenick(np);
242}
243
244void addnicktohash(nick *np) {
245 np->next=nicktable[nickhash(np->nick)];
246 nicktable[nickhash(np->nick)]=np;
247}
248
249void removenickfromhash(nick *np) {
250 nick **nh;
251
252 for (nh=&(nicktable[nickhash(np->nick)]);*nh;nh=&((*nh)->next)) {
253 if ((*nh)==np) {
254 (*nh)=np->next;
255 break;
256 }
257 }
258}
259
260nick *getnickbynick(const char *name) {
261 nick *np;
262
263 for (np=nicktable[nickhash(name)];np;np=np->next) {
264 if (!ircd_strcmp(np->nick,name))
265 return np;
266 }
267
268 return NULL;
269}
270
271void nickstats(int hooknum, void *arg) {
272 int total,maxchain,curchain,i,buckets;
273 nick *np;
274 char buf[200];
275
276 /* Get nick stats */
277 buckets=total=maxchain=curchain=0;
278 for (i=0;i<NICKHASHSIZE;i++,curchain=0) {
279 np=nicktable[i];
280 if (np!=NULL) {
281 buckets++;
282 for (;np;np=np->next) {
283 total++;
284 curchain++;
285 }
286 }
287 if (curchain>maxchain) {
288 maxchain=curchain;
289 }
290 }
291
c3db6f7e 292 if ((long)arg>5) {
c86edd1d
Q
293 /* Full stats */
294 sprintf(buf,"Nick : %6d nicks (HASH: %6d/%6d, chain %3d)",total,buckets,NICKHASHSIZE,maxchain);
c3db6f7e 295 } else if ((long)arg>2) {
c86edd1d
Q
296 sprintf(buf,"Nick : %6d users on network.",total);
297 }
298
c3db6f7e 299 if ((long)arg>2) {
c86edd1d
Q
300 triggerhook(HOOK_CORE_STATSREPLY,buf);
301 }
302}
303
304int registernickext(const char *name) {
305 int i;
306
307 if (findnickext(name)!=-1) {
308 Error("nick",ERR_WARNING,"Tried to register duplicate nick extension %s",name);
309 return -1;
310 }
311
312 for (i=0;i<MAXNICKEXTS;i++) {
313 if (nickextnames[i]==NULL) {
314 nickextnames[i]=getsstring(name,100);
315 return i;
316 }
317 }
318
47657339 319 Error("nick",ERR_WARNING,"Tried to register too many nick extensions: %s",name);
c86edd1d
Q
320 return -1;
321}
322
323int findnickext(const char *name) {
324 int i;
325
326 for (i=0;i<MAXNICKEXTS;i++) {
327 if (nickextnames[i]!=NULL && !ircd_strcmp(name,nickextnames[i]->content)) {
328 return i;
329 }
330 }
331
332 return -1;
333}
334
335void releasenickext(int index) {
336 int i;
337 nick *np;
338
339 freesstring(nickextnames[index]);
340 nickextnames[index]=NULL;
341
342 for (i=0;i<NICKHASHSIZE;i++) {
343 for (np=nicktable[i];np;np=np->next) {
344 np->exts[index]=NULL;
345 }
346 }
347}
348
349/* visiblehostmask
350 * Produces the "apparent" hostmask as seen by network users.
351 */
352
353char *visiblehostmask(nick *np, char *buf) {
28252fef 354 char uhbuf[USERLEN+HOSTLEN+2];
355
356 visibleuserhost(np, uhbuf);
357 sprintf(buf,"%s!%s",np->nick,uhbuf);
358
359 return buf;
360}
361
362/* visibleuserhost
363 * As above without nick
364 */
365
366char *visibleuserhost(nick *np, char *buf) {
c86edd1d
Q
367 char hostbuf[HOSTLEN+1];
368 char *ident, *host;
369
370 ident=np->ident;
371 host=np->host->name->content;
372
373 if (IsSetHost(np)) {
374 if (np->shident) {
375 ident=np->shident->content;
376 }
377 if (np->sethost) {
378 host=np->sethost->content;
379 }
380 } else if (IsAccount(np) && IsHideHost(np)) {
381 sprintf(hostbuf,"%s.%s", np->authname, HIS_HIDDENHOST);
382 host=hostbuf;
383 }
384
28252fef 385 sprintf(buf,"%s@%s",ident,host);
c86edd1d
Q
386
387 return buf;
388}
389
390#if 0
391
392/*
393 * gethandlebynumeric:
394 * Given a numeric, gives the location in the servernicks table
395 * where it should be. Does not check that the nick currently found
396 * there (if any) has the correct numeric; this is left to the
397 * calling function to figure out.
398 */
399
400nick **gethandlebynumeric(long numeric) {
401 int servernum;
402 int maskednum;
403 server *serv;
404
405 /* Shift off the client identifier part of the numeric to get the server ID */
406 servernum=(numeric>>18);
407
408 if ((serv=getserverdata(servernum))==NULL) {
409 Error("nick",ERR_WARNING,"Numeric %ld refers to non-existent server %d",numeric,servernum);
410 return NULL;
411 }
412
413 /* Compute the masked numeric */
414 maskednum=numeric&(serv->maxusernum);
415
416 return (servernicks[servernum])+maskednum;
417}
418
419/*
420 * getnickbynumeric[str]()
421 * These functions retrieve a nick based on it's numeric on the network
422 * Use the approriate function depending on how your numeric is expressed..
423 */
424
425nick *getnickbynumeric(long numeric) {
426 nick **nh;
427
428 nh=gethandlebynumeric(numeric);
429
430 if ((*nh) && ((*nh)->numeric!=numeric)) {
431 /* We found a masked numeric match, but the actual numeric
432 * is different. This counts as a miss. */
433 return NULL;
434 }
435
436 return (*nh);
437}
438
439nick *getnickbynumericstr(char *numericstr) {
440 return getnickbynumeric(numerictolong(numericstr,5));
441}
442
cebc4cab 443
c86edd1d 444#endif
526e7c1d 445
42d7af35
GB
446int canseeuser(nick *np, nick *cloaked)
447{
448 return (np == cloaked ||
449 !IsCloaked(cloaked) ||
450 np->cloak_extra == cloaked);
451}
452
453void addcloaktarget(nick *cloaked, nick *target)
454{
455 removecloaktarget(target);
456
457 target->cloak_extra = cloaked;
458 cloaked->cloak_count++;
459}
460
461void removecloaktarget(nick *target)
462{
463 if (target->cloak_extra) {
464 target->cloak_extra->cloak_count--;
465 target->cloak_extra = NULL;
466 }
467}
468
469void clearcloaktargets(nick *cloaked)
470{
471 nick *tnp;
472 int j;
473
260e4351
GB
474 if (cloaked->cloak_count == 0)
475 return;
476
42d7af35
GB
477 for(j=0;j<NICKHASHSIZE;j++)
478 for(tnp=nicktable[j];tnp;tnp=tnp->next)
479 if (tnp->cloak_extra == cloaked)
480 tnp->cloak_extra = NULL;
481
482 cloaked->cloak_count = 0;
483}
484