]>
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
},
39 #define nickhash(x) ((crc32i(x))%NICKHASHSIZE)
41 nick
*nicktable
[NICKHASHSIZE
];
42 nick
**servernicks
[MAXSERVERS
];
44 sstring
*nickextnames
[MAXNICKEXTS
];
45 sstring
*nodeextnames
[PATRICIA_MAXSLOTS
];
46 patricia_tree_t
*iptree
;
48 void nickstats(int hooknum
, void *arg
);
53 memset(nicktable
,0,sizeof(nicktable
));
54 memset(servernicks
,0,sizeof(servernicks
));
56 /* Register our hooks */
57 registerhook(HOOK_SERVER_NEWSERVER
,&handleserverchange
);
58 registerhook(HOOK_SERVER_LOSTSERVER
,&handleserverchange
);
59 registerhook(HOOK_CORE_STATSREQUEST
,&nickstats
);
61 /* And our server handlers */
62 registerserverhandler("N",&handlenickmsg
,10);
63 registerserverhandler("D",&handlekillmsg
,2);
64 registerserverhandler("Q",&handlequitmsg
,1);
65 registerserverhandler("M",&handleusermodemsg
,3);
66 registerserverhandler("W",&handlewhoismsg
,2);
67 registerserverhandler("AC",&handleaccountmsg
,4);
68 registerserverhandler("R",&handlestatsmsg
,2);
70 /* Fake the addition of our own server */
71 handleserverchange(HOOK_SERVER_NEWSERVER
,(void *)numerictolong(mynumeric
->content
,2));
73 iptree
= patricia_new_tree(PATRICIA_MAXBITS
);
80 deregisterhook(HOOK_SERVER_NEWSERVER
,&handleserverchange
);
81 deregisterhook(HOOK_SERVER_LOSTSERVER
,&handleserverchange
);
82 deregisterhook(HOOK_CORE_STATSREQUEST
,&nickstats
);
84 /* And our server handlers */
85 deregisterserverhandler("N",&handlenickmsg
);
86 deregisterserverhandler("D",&handlekillmsg
);
87 deregisterserverhandler("Q",&handlequitmsg
);
88 deregisterserverhandler("M",&handleusermodemsg
);
89 deregisterserverhandler("W",&handlewhoismsg
);
90 deregisterserverhandler("AC",&handleaccountmsg
);
91 deregisterserverhandler("R",&handlestatsmsg
);
95 * This function handles servers appearing and disappearing.
96 * For a new server, the client table is allocated.
97 * For a disappearing server, all it's clients are killed and the client table is freed.
100 void handleserverchange(int hooknum
, void *arg
) {
107 case HOOK_SERVER_NEWSERVER
:
108 servernicks
[servernum
]=(nick
**)nsmalloc(POOL_NICK
,(serverlist
[servernum
].maxusernum
+1)*sizeof(nick
**));
109 memset(servernicks
[servernum
],0,(serverlist
[servernum
].maxusernum
+1)*sizeof(nick
**));
112 case HOOK_SERVER_LOSTSERVER
:
113 for (i
=0;i
<=serverlist
[servernum
].maxusernum
;i
++) {
114 if (servernicks
[servernum
][i
]!=NULL
) {
115 deletenick(servernicks
[servernum
][i
]);
118 free(servernicks
[servernum
]);
126 * This function handles the removal of a nick from the network
129 void deletenick(nick
*np
) {
132 /* Fire the hook. This will deal with removal from channels etc. */
133 triggerhook(HOOK_NICK_LOSTNICK
, np
);
135 /* Release the realname and hostname parts */
137 for (nh
=&(np
->realname
->nicks
);*nh
;nh
=&((*nh
)->nextbyrealname
)) {
139 *nh
=np
->nextbyrealname
;
144 for (nh
=&(np
->host
->nicks
);*nh
;nh
=&((*nh
)->nextbyhost
)) {
151 releaserealname(np
->realname
);
152 releasehost(np
->host
);
154 if(IsAccount(np
) && np
->auth
)
156 np
->auth
->usercount
--;
158 for (nh
=&(np
->auth
->nicks
);*nh
;nh
=&((*nh
)->nextbyauthname
)) {
160 *nh
=np
->nextbyauthname
;
165 releaseauthname(np
->auth
);
168 freesstring(np
->shident
); /* freesstring(NULL) is OK */
169 freesstring(np
->sethost
);
171 derefnode(iptree
, np
->ipnode
);
173 /* TODO: figure out how to cleanly remove nodes without affecting other modules */
175 /* Delete the nick from the servernick table */
176 *(gethandlebynumericunsafe(np
->numeric
))=NULL
;
178 /* Remove the nick from the hash table */
179 removenickfromhash(np
);
184 void addnicktohash(nick
*np
) {
185 np
->next
=nicktable
[nickhash(np
->nick
)];
186 nicktable
[nickhash(np
->nick
)]=np
;
189 void removenickfromhash(nick
*np
) {
192 for (nh
=&(nicktable
[nickhash(np
->nick
)]);*nh
;nh
=&((*nh
)->next
)) {
200 nick
*getnickbynick(const char *name
) {
203 for (np
=nicktable
[nickhash(name
)];np
;np
=np
->next
) {
204 if (!ircd_strcmp(np
->nick
,name
))
211 void nickstats(int hooknum
, void *arg
) {
212 int total
,maxchain
,curchain
,i
,buckets
;
217 buckets
=total
=maxchain
=curchain
=0;
218 for (i
=0;i
<NICKHASHSIZE
;i
++,curchain
=0) {
222 for (;np
;np
=np
->next
) {
227 if (curchain
>maxchain
) {
234 sprintf(buf
,"Nick : %6d nicks (HASH: %6d/%6d, chain %3d)",total
,buckets
,NICKHASHSIZE
,maxchain
);
235 } else if ((long)arg
>2) {
236 sprintf(buf
,"Nick : %6d users on network.",total
);
240 triggerhook(HOOK_CORE_STATSREPLY
,buf
);
244 int registernickext(const char *name
) {
247 if (findnickext(name
)!=-1) {
248 Error("nick",ERR_WARNING
,"Tried to register duplicate nick extension %s",name
);
252 for (i
=0;i
<MAXNICKEXTS
;i
++) {
253 if (nickextnames
[i
]==NULL
) {
254 nickextnames
[i
]=getsstring(name
,100);
259 Error("nick",ERR_WARNING
,"Tried to register too many nick extensions: %s",name
);
263 int findnickext(const char *name
) {
266 for (i
=0;i
<MAXNICKEXTS
;i
++) {
267 if (nickextnames
[i
]!=NULL
&& !ircd_strcmp(name
,nickextnames
[i
]->content
)) {
275 void releasenickext(int index
) {
279 freesstring(nickextnames
[index
]);
280 nickextnames
[index
]=NULL
;
282 for (i
=0;i
<NICKHASHSIZE
;i
++) {
283 for (np
=nicktable
[i
];np
;np
=np
->next
) {
284 np
->exts
[index
]=NULL
;
290 * Produces the "apparent" hostmask as seen by network users.
293 char *visiblehostmask(nick
*np
, char *buf
) {
294 char hostbuf
[HOSTLEN
+1];
298 host
=np
->host
->name
->content
;
302 ident
=np
->shident
->content
;
305 host
=np
->sethost
->content
;
307 } else if (IsAccount(np
) && IsHideHost(np
)) {
308 sprintf(hostbuf
,"%s.%s", np
->authname
, HIS_HIDDENHOST
);
312 sprintf(buf
,"%s!%s@%s",np
->nick
,ident
,host
);
320 * gethandlebynumeric:
321 * Given a numeric, gives the location in the servernicks table
322 * where it should be. Does not check that the nick currently found
323 * there (if any) has the correct numeric; this is left to the
324 * calling function to figure out.
327 nick
**gethandlebynumeric(long numeric
) {
332 /* Shift off the client identifier part of the numeric to get the server ID */
333 servernum
=(numeric
>>18);
335 if ((serv
=getserverdata(servernum
))==NULL
) {
336 Error("nick",ERR_WARNING
,"Numeric %ld refers to non-existent server %d",numeric
,servernum
);
340 /* Compute the masked numeric */
341 maskednum
=numeric
&(serv
->maxusernum
);
343 return (servernicks
[servernum
])+maskednum
;
347 * getnickbynumeric[str]()
348 * These functions retrieve a nick based on it's numeric on the network
349 * Use the approriate function depending on how your numeric is expressed..
352 nick
*getnickbynumeric(long numeric
) {
355 nh
=gethandlebynumeric(numeric
);
357 if ((*nh
) && ((*nh
)->numeric
!=numeric
)) {
358 /* We found a masked numeric match, but the actual numeric
359 * is different. This counts as a miss. */
366 nick
*getnickbynumericstr(char *numericstr
) {
367 return getnickbynumeric(numerictolong(numericstr
,5));
372 int registernodeext(const char *name
) {
375 if (findnodeext(name
)!=-1) {
376 Error("nick",ERR_WARNING
,"Tried to register duplicate node extension %s",name
);
380 for (i
=0;i
<PATRICIA_MAXSLOTS
;i
++) {
381 if (nodeextnames
[i
]==NULL
) {
382 nodeextnames
[i
]=getsstring(name
,100);
387 Error("nick",ERR_WARNING
,"Tried to register too many extensions: %s",name
);
391 int findnodeext(const char *name
) {
394 for (i
=0;i
<PATRICIA_MAXSLOTS
;i
++) {
395 if (nodeextnames
[i
]!=NULL
&& !ircd_strcmp(name
,nodeextnames
[i
]->content
)) {
403 void releasenodeext(int index
) {
404 patricia_node_t
*head
, *node
;
406 freesstring(nodeextnames
[index
]);
407 nodeextnames
[index
]=NULL
;
411 PATRICIA_WALK_ALL(head
, node
)
413 node
->slots
[index
]=NULL
;