]>
jfr.im git - irc/quakenet/newserv.git/blob - nick/nick.c
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"
23 const flag umodeflags
[] = {
25 { 'w', UMODE_WALLOPS
},
28 { 'k', UMODE_SERVICE
},
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 { 'C', UMODE_CLOAKED
},
41 const flag accountflags
[] = {
43 { 'h', AFLAG_SUPPORT
},
46 { 'd', AFLAG_DEVELOPER
},
49 #define nickhash(x) ((crc32i(x))%NICKHASHSIZE)
51 nick
*nicktable
[NICKHASHSIZE
];
52 nick
**servernicks
[MAXSERVERS
];
54 sstring
*nickextnames
[MAXNICKEXTS
];
56 void nickstats(int hooknum
, void *arg
);
58 char *NULLAUTHNAME
= "";
64 /* Clear up the nicks in authext */
65 for (i
=0;i
<AUTHNAMEHASHSIZE
;i
++)
66 for (anp
=authnametable
[i
];anp
;anp
=anp
->next
)
71 memset(nicktable
,0,sizeof(nicktable
));
72 memset(servernicks
,0,sizeof(servernicks
));
74 /* If we're connected to IRC, force a disconnect. This needs to be done
75 * before we register all our hooks which would otherwise get called
76 * during the disconnect. */
78 irc_send("%s SQ %s 0 :Resync [adding nick support]",mynumeric
->content
,myserver
->content
); irc_disconnected();
81 /* Register our hooks */
82 registerhook(HOOK_SERVER_NEWSERVER
,&handleserverchange
);
83 registerhook(HOOK_SERVER_LOSTSERVER
,&handleserverchange
);
84 registerhook(HOOK_CORE_STATSREQUEST
,&nickstats
);
86 /* And our server handlers */
87 registerserverhandler("N",&handlenickmsg
,10);
88 registerserverhandler("D",&handlekillmsg
,2);
89 registerserverhandler("Q",&handlequitmsg
,1);
90 registerserverhandler("M",&handleusermodemsg
,3);
91 registerserverhandler("AC",&handleaccountmsg
,4);
92 registerserverhandler("P",&handleprivmsg
,2);
93 registerserverhandler("A",&handleawaymsg
,1);
94 registerserverhandler("CA",&handleaddcloak
,1);
95 registerserverhandler("CU",&handleclearcloak
,0);
97 /* Fake the addition of our own server */
98 handleserverchange(HOOK_SERVER_NEWSERVER
,(void *)numerictolong(mynumeric
->content
,2));
107 for (i
=0;i
<NICKHASHSIZE
;i
++) {
108 for (np
=nicktable
[i
];np
;np
=np
->next
) {
109 freesstring(np
->shident
);
110 freesstring(np
->sethost
);
111 freesstring(np
->opername
);
112 if(!np
->auth
&& np
->authname
&& (np
->authname
!= NULLAUTHNAME
))
117 nsfreeall(POOL_NICK
);
120 deregisterhook(HOOK_SERVER_NEWSERVER
,&handleserverchange
);
121 deregisterhook(HOOK_SERVER_LOSTSERVER
,&handleserverchange
);
122 deregisterhook(HOOK_CORE_STATSREQUEST
,&nickstats
);
124 /* And our server handlers */
125 deregisterserverhandler("N",&handlenickmsg
);
126 deregisterserverhandler("D",&handlekillmsg
);
127 deregisterserverhandler("Q",&handlequitmsg
);
128 deregisterserverhandler("M",&handleusermodemsg
);
129 deregisterserverhandler("AC",&handleaccountmsg
);
130 deregisterserverhandler("P",&handleprivmsg
);
131 deregisterserverhandler("A",&handleawaymsg
);
132 deregisterserverhandler("CA",&handleaddcloak
);
133 deregisterserverhandler("CU",&handleclearcloak
);
137 * This function handles servers appearing and disappearing.
138 * For a new server, the client table is allocated.
139 * For a disappearing server, all it's clients are killed and the client table is freed.
142 void handleserverchange(int hooknum
, void *arg
) {
149 case HOOK_SERVER_NEWSERVER
:
150 servernicks
[servernum
]=(nick
**)nsmalloc(POOL_NICK
,(serverlist
[servernum
].maxusernum
+1)*sizeof(nick
**));
151 memset(servernicks
[servernum
],0,(serverlist
[servernum
].maxusernum
+1)*sizeof(nick
**));
154 case HOOK_SERVER_LOSTSERVER
:
155 for (i
=0;i
<=serverlist
[servernum
].maxusernum
;i
++) {
156 if (servernicks
[servernum
][i
]!=NULL
) {
157 deletenick(servernicks
[servernum
][i
]);
160 nsfree(POOL_NICK
,servernicks
[servernum
]);
168 * This function handles the removal of a nick from the network
171 void deletenick(nick
*np
) {
174 /* Fire the hook. This will deal with removal from channels etc. */
175 triggerhook(HOOK_NICK_LOSTNICK
, np
);
177 /* Release the realname and hostname parts */
179 for (nh
=&(np
->realname
->nicks
);*nh
;nh
=&((*nh
)->nextbyrealname
)) {
181 *nh
=np
->nextbyrealname
;
186 for (nh
=&(np
->host
->nicks
);*nh
;nh
=&((*nh
)->nextbyhost
)) {
193 releaserealname(np
->realname
);
194 releasehost(np
->host
);
198 if(np
->authname
&& (np
->authname
!= NULLAUTHNAME
))
201 np
->auth
->usercount
--;
203 for (nh
=&(np
->auth
->nicks
);*nh
;nh
=&((*nh
)->nextbyauthname
)) {
205 *nh
=np
->nextbyauthname
;
210 releaseauthname(np
->auth
);
214 freesstring(np
->shident
); /* freesstring(NULL) is OK */
215 freesstring(np
->sethost
);
216 freesstring(np
->opername
);
218 node_decrement_usercount(np
->ipnode
);
219 derefnode(iptree
, np
->ipnode
);
221 /* TODO: figure out how to cleanly remove nodes without affecting other modules */
223 /* Remove cloak entries for the user */
224 removecloaktarget(np
);
225 clearcloaktargets(np
);
227 /* Delete the nick from the servernick table */
228 *(gethandlebynumericunsafe(np
->numeric
))=NULL
;
230 /* Remove the nick from the hash table */
231 removenickfromhash(np
);
236 void addnicktohash(nick
*np
) {
237 np
->next
=nicktable
[nickhash(np
->nick
)];
238 nicktable
[nickhash(np
->nick
)]=np
;
241 void removenickfromhash(nick
*np
) {
244 for (nh
=&(nicktable
[nickhash(np
->nick
)]);*nh
;nh
=&((*nh
)->next
)) {
252 nick
*getnickbynick(const char *name
) {
255 for (np
=nicktable
[nickhash(name
)];np
;np
=np
->next
) {
256 if (!ircd_strcmp(np
->nick
,name
))
263 void nickstats(int hooknum
, void *arg
) {
264 int total
,maxchain
,curchain
,i
,buckets
;
269 buckets
=total
=maxchain
=curchain
=0;
270 for (i
=0;i
<NICKHASHSIZE
;i
++,curchain
=0) {
274 for (;np
;np
=np
->next
) {
279 if (curchain
>maxchain
) {
286 sprintf(buf
,"Nick : %6d nicks (HASH: %6d/%6d, chain %3d)",total
,buckets
,NICKHASHSIZE
,maxchain
);
287 } else if ((long)arg
>2) {
288 sprintf(buf
,"Nick : %6d users on network.",total
);
292 triggerhook(HOOK_CORE_STATSREPLY
,buf
);
296 int registernickext(const char *name
) {
299 if (findnickext(name
)!=-1) {
300 Error("nick",ERR_WARNING
,"Tried to register duplicate nick extension %s",name
);
304 for (i
=0;i
<MAXNICKEXTS
;i
++) {
305 if (nickextnames
[i
]==NULL
) {
306 nickextnames
[i
]=getsstring(name
,100);
311 Error("nick",ERR_WARNING
,"Tried to register too many nick extensions: %s",name
);
315 int findnickext(const char *name
) {
318 for (i
=0;i
<MAXNICKEXTS
;i
++) {
319 if (nickextnames
[i
]!=NULL
&& !ircd_strcmp(name
,nickextnames
[i
]->content
)) {
327 void releasenickext(int index
) {
331 freesstring(nickextnames
[index
]);
332 nickextnames
[index
]=NULL
;
334 for (i
=0;i
<NICKHASHSIZE
;i
++) {
335 for (np
=nicktable
[i
];np
;np
=np
->next
) {
336 np
->exts
[index
]=NULL
;
342 * Produces the "apparent" hostmask as seen by network users.
345 char *visiblehostmask(nick
*np
, char *buf
) {
346 char uhbuf
[USERLEN
+HOSTLEN
+2];
348 visibleuserhost(np
, uhbuf
);
349 sprintf(buf
,"%s!%s",np
->nick
,uhbuf
);
355 * As above without nick
358 char *visibleuserhost(nick
*np
, char *buf
) {
359 char hostbuf
[HOSTLEN
+1];
363 host
=np
->host
->name
->content
;
367 ident
=np
->shident
->content
;
370 host
=np
->sethost
->content
;
372 } else if (IsAccount(np
) && IsHideHost(np
)) {
373 sprintf(hostbuf
,"%s.%s", np
->authname
, HIS_HIDDENHOST
);
377 sprintf(buf
,"%s@%s",ident
,host
);
385 * gethandlebynumeric:
386 * Given a numeric, gives the location in the servernicks table
387 * where it should be. Does not check that the nick currently found
388 * there (if any) has the correct numeric; this is left to the
389 * calling function to figure out.
392 nick
**gethandlebynumeric(long numeric
) {
397 /* Shift off the client identifier part of the numeric to get the server ID */
398 servernum
=(numeric
>>18);
400 if ((serv
=getserverdata(servernum
))==NULL
) {
401 Error("nick",ERR_WARNING
,"Numeric %ld refers to non-existent server %d",numeric
,servernum
);
405 /* Compute the masked numeric */
406 maskednum
=numeric
&(serv
->maxusernum
);
408 return (servernicks
[servernum
])+maskednum
;
412 * getnickbynumeric[str]()
413 * These functions retrieve a nick based on it's numeric on the network
414 * Use the approriate function depending on how your numeric is expressed..
417 nick
*getnickbynumeric(long numeric
) {
420 nh
=gethandlebynumeric(numeric
);
422 if ((*nh
) && ((*nh
)->numeric
!=numeric
)) {
423 /* We found a masked numeric match, but the actual numeric
424 * is different. This counts as a miss. */
431 nick
*getnickbynumericstr(char *numericstr
) {
432 return getnickbynumeric(numerictolong(numericstr
,5));
437 int canseeuser(nick
*np
, nick
*cloaked
)
439 return (np
== cloaked
||
440 !IsCloaked(cloaked
) ||
441 np
->cloak_extra
== cloaked
);
444 void addcloaktarget(nick
*cloaked
, nick
*target
)
446 removecloaktarget(target
);
448 target
->cloak_extra
= cloaked
;
449 cloaked
->cloak_count
++;
452 void removecloaktarget(nick
*target
)
454 if (target
->cloak_extra
) {
455 target
->cloak_extra
->cloak_count
--;
456 target
->cloak_extra
= NULL
;
460 void clearcloaktargets(nick
*cloaked
)
465 for(j
=0;j
<NICKHASHSIZE
;j
++)
466 for(tnp
=nicktable
[j
];tnp
;tnp
=tnp
->next
)
467 if (tnp
->cloak_extra
== cloaked
)
468 tnp
->cloak_extra
= NULL
;
470 cloaked
->cloak_count
= 0;