]>
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
},
40 const flag accountflags
[] = {
42 { 'h', AFLAG_SUPPORT
},
45 { 'd', AFLAG_DEVELOPER
},
48 #define nickhash(x) ((crc32i(x))%NICKHASHSIZE)
50 nick
*nicktable
[NICKHASHSIZE
];
51 nick
**servernicks
[MAXSERVERS
];
53 sstring
*nickextnames
[MAXNICKEXTS
];
55 void nickstats(int hooknum
, void *arg
);
57 char *NULLAUTHNAME
= "";
63 /* Clear up the nicks in authext */
64 for (i
=0;i
<AUTHNAMEHASHSIZE
;i
++)
65 for (anp
=authnametable
[i
];anp
;anp
=anp
->next
)
70 memset(nicktable
,0,sizeof(nicktable
));
71 memset(servernicks
,0,sizeof(servernicks
));
73 /* Register our hooks */
74 registerhook(HOOK_SERVER_NEWSERVER
,&handleserverchange
);
75 registerhook(HOOK_SERVER_LOSTSERVER
,&handleserverchange
);
76 registerhook(HOOK_CORE_STATSREQUEST
,&nickstats
);
78 /* And our server handlers */
79 registerserverhandler("N",&handlenickmsg
,10);
80 registerserverhandler("D",&handlekillmsg
,2);
81 registerserverhandler("Q",&handlequitmsg
,1);
82 registerserverhandler("M",&handleusermodemsg
,3);
83 registerserverhandler("W",&handlewhoismsg
,2);
84 registerserverhandler("AC",&handleaccountmsg
,4);
85 registerserverhandler("R",&handlestatsmsg
,2);
86 registerserverhandler("P",&handleprivmsg
,2);
88 /* Fake the addition of our own server */
89 handleserverchange(HOOK_SERVER_NEWSERVER
,(void *)numerictolong(mynumeric
->content
,2));
98 for (i
=0;i
<NICKHASHSIZE
;i
++) {
99 for (np
=nicktable
[i
];np
;np
=np
->next
) {
100 freesstring(np
->shident
);
101 freesstring(np
->sethost
);
102 freesstring(np
->opername
);
103 if(!np
->auth
&& np
->authname
&& (np
->authname
!= NULLAUTHNAME
))
108 nsfreeall(POOL_NICK
);
111 deregisterhook(HOOK_SERVER_NEWSERVER
,&handleserverchange
);
112 deregisterhook(HOOK_SERVER_LOSTSERVER
,&handleserverchange
);
113 deregisterhook(HOOK_CORE_STATSREQUEST
,&nickstats
);
115 /* And our server handlers */
116 deregisterserverhandler("N",&handlenickmsg
);
117 deregisterserverhandler("D",&handlekillmsg
);
118 deregisterserverhandler("Q",&handlequitmsg
);
119 deregisterserverhandler("M",&handleusermodemsg
);
120 deregisterserverhandler("W",&handlewhoismsg
);
121 deregisterserverhandler("AC",&handleaccountmsg
);
122 deregisterserverhandler("R",&handlestatsmsg
);
123 deregisterserverhandler("P",&handleprivmsg
);
127 * This function handles servers appearing and disappearing.
128 * For a new server, the client table is allocated.
129 * For a disappearing server, all it's clients are killed and the client table is freed.
132 void handleserverchange(int hooknum
, void *arg
) {
139 case HOOK_SERVER_NEWSERVER
:
140 servernicks
[servernum
]=(nick
**)nsmalloc(POOL_NICK
,(serverlist
[servernum
].maxusernum
+1)*sizeof(nick
**));
141 memset(servernicks
[servernum
],0,(serverlist
[servernum
].maxusernum
+1)*sizeof(nick
**));
144 case HOOK_SERVER_LOSTSERVER
:
145 for (i
=0;i
<=serverlist
[servernum
].maxusernum
;i
++) {
146 if (servernicks
[servernum
][i
]!=NULL
) {
147 deletenick(servernicks
[servernum
][i
]);
150 nsfree(POOL_NICK
,servernicks
[servernum
]);
158 * This function handles the removal of a nick from the network
161 void deletenick(nick
*np
) {
164 /* Fire the hook. This will deal with removal from channels etc. */
165 triggerhook(HOOK_NICK_LOSTNICK
, np
);
167 /* Release the realname and hostname parts */
169 for (nh
=&(np
->realname
->nicks
);*nh
;nh
=&((*nh
)->nextbyrealname
)) {
171 *nh
=np
->nextbyrealname
;
176 for (nh
=&(np
->host
->nicks
);*nh
;nh
=&((*nh
)->nextbyhost
)) {
183 releaserealname(np
->realname
);
184 releasehost(np
->host
);
188 if(np
->authname
&& (np
->authname
!= NULLAUTHNAME
))
191 np
->auth
->usercount
--;
193 for (nh
=&(np
->auth
->nicks
);*nh
;nh
=&((*nh
)->nextbyauthname
)) {
195 *nh
=np
->nextbyauthname
;
200 releaseauthname(np
->auth
);
204 freesstring(np
->shident
); /* freesstring(NULL) is OK */
205 freesstring(np
->sethost
);
206 freesstring(np
->opername
);
208 node_decrement_usercount(np
->ipnode
);
209 derefnode(iptree
, np
->ipnode
);
211 /* TODO: figure out how to cleanly remove nodes without affecting other modules */
213 /* Delete the nick from the servernick table */
214 *(gethandlebynumericunsafe(np
->numeric
))=NULL
;
216 /* Remove the nick from the hash table */
217 removenickfromhash(np
);
222 void addnicktohash(nick
*np
) {
223 np
->next
=nicktable
[nickhash(np
->nick
)];
224 nicktable
[nickhash(np
->nick
)]=np
;
227 void removenickfromhash(nick
*np
) {
230 for (nh
=&(nicktable
[nickhash(np
->nick
)]);*nh
;nh
=&((*nh
)->next
)) {
238 nick
*getnickbynick(const char *name
) {
241 for (np
=nicktable
[nickhash(name
)];np
;np
=np
->next
) {
242 if (!ircd_strcmp(np
->nick
,name
))
249 void nickstats(int hooknum
, void *arg
) {
250 int total
,maxchain
,curchain
,i
,buckets
;
255 buckets
=total
=maxchain
=curchain
=0;
256 for (i
=0;i
<NICKHASHSIZE
;i
++,curchain
=0) {
260 for (;np
;np
=np
->next
) {
265 if (curchain
>maxchain
) {
272 sprintf(buf
,"Nick : %6d nicks (HASH: %6d/%6d, chain %3d)",total
,buckets
,NICKHASHSIZE
,maxchain
);
273 } else if ((long)arg
>2) {
274 sprintf(buf
,"Nick : %6d users on network.",total
);
278 triggerhook(HOOK_CORE_STATSREPLY
,buf
);
282 int registernickext(const char *name
) {
285 if (findnickext(name
)!=-1) {
286 Error("nick",ERR_WARNING
,"Tried to register duplicate nick extension %s",name
);
290 for (i
=0;i
<MAXNICKEXTS
;i
++) {
291 if (nickextnames
[i
]==NULL
) {
292 nickextnames
[i
]=getsstring(name
,100);
297 Error("nick",ERR_WARNING
,"Tried to register too many nick extensions: %s",name
);
301 int findnickext(const char *name
) {
304 for (i
=0;i
<MAXNICKEXTS
;i
++) {
305 if (nickextnames
[i
]!=NULL
&& !ircd_strcmp(name
,nickextnames
[i
]->content
)) {
313 void releasenickext(int index
) {
317 freesstring(nickextnames
[index
]);
318 nickextnames
[index
]=NULL
;
320 for (i
=0;i
<NICKHASHSIZE
;i
++) {
321 for (np
=nicktable
[i
];np
;np
=np
->next
) {
322 np
->exts
[index
]=NULL
;
328 * Produces the "apparent" hostmask as seen by network users.
331 char *visiblehostmask(nick
*np
, char *buf
) {
332 char uhbuf
[USERLEN
+HOSTLEN
+2];
334 visibleuserhost(np
, uhbuf
);
335 sprintf(buf
,"%s!%s",np
->nick
,uhbuf
);
341 * As above without nick
344 char *visibleuserhost(nick
*np
, char *buf
) {
345 char hostbuf
[HOSTLEN
+1];
349 host
=np
->host
->name
->content
;
353 ident
=np
->shident
->content
;
356 host
=np
->sethost
->content
;
358 } else if (IsAccount(np
) && IsHideHost(np
)) {
359 sprintf(hostbuf
,"%s.%s", np
->authname
, HIS_HIDDENHOST
);
363 sprintf(buf
,"%s@%s",ident
,host
);
371 * gethandlebynumeric:
372 * Given a numeric, gives the location in the servernicks table
373 * where it should be. Does not check that the nick currently found
374 * there (if any) has the correct numeric; this is left to the
375 * calling function to figure out.
378 nick
**gethandlebynumeric(long numeric
) {
383 /* Shift off the client identifier part of the numeric to get the server ID */
384 servernum
=(numeric
>>18);
386 if ((serv
=getserverdata(servernum
))==NULL
) {
387 Error("nick",ERR_WARNING
,"Numeric %ld refers to non-existent server %d",numeric
,servernum
);
391 /* Compute the masked numeric */
392 maskednum
=numeric
&(serv
->maxusernum
);
394 return (servernicks
[servernum
])+maskednum
;
398 * getnickbynumeric[str]()
399 * These functions retrieve a nick based on it's numeric on the network
400 * Use the approriate function depending on how your numeric is expressed..
403 nick
*getnickbynumeric(long numeric
) {
406 nh
=gethandlebynumeric(numeric
);
408 if ((*nh
) && ((*nh
)->numeric
!=numeric
)) {
409 /* We found a masked numeric match, but the actual numeric
410 * is different. This counts as a miss. */
417 nick
*getnickbynumericstr(char *numericstr
) {
418 return getnickbynumeric(numerictolong(numericstr
,5));