]>
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 nsfree(POOL_NICK
,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 np
->ipnode
->usercount
--;
172 derefnode(iptree
, np
->ipnode
);
174 /* TODO: figure out how to cleanly remove nodes without affecting other modules */
176 /* Delete the nick from the servernick table */
177 *(gethandlebynumericunsafe(np
->numeric
))=NULL
;
179 /* Remove the nick from the hash table */
180 removenickfromhash(np
);
185 void addnicktohash(nick
*np
) {
186 np
->next
=nicktable
[nickhash(np
->nick
)];
187 nicktable
[nickhash(np
->nick
)]=np
;
190 void removenickfromhash(nick
*np
) {
193 for (nh
=&(nicktable
[nickhash(np
->nick
)]);*nh
;nh
=&((*nh
)->next
)) {
201 nick
*getnickbynick(const char *name
) {
204 for (np
=nicktable
[nickhash(name
)];np
;np
=np
->next
) {
205 if (!ircd_strcmp(np
->nick
,name
))
212 void nickstats(int hooknum
, void *arg
) {
213 int total
,maxchain
,curchain
,i
,buckets
;
218 buckets
=total
=maxchain
=curchain
=0;
219 for (i
=0;i
<NICKHASHSIZE
;i
++,curchain
=0) {
223 for (;np
;np
=np
->next
) {
228 if (curchain
>maxchain
) {
235 sprintf(buf
,"Nick : %6d nicks (HASH: %6d/%6d, chain %3d)",total
,buckets
,NICKHASHSIZE
,maxchain
);
236 } else if ((long)arg
>2) {
237 sprintf(buf
,"Nick : %6d users on network.",total
);
241 triggerhook(HOOK_CORE_STATSREPLY
,buf
);
245 int registernickext(const char *name
) {
248 if (findnickext(name
)!=-1) {
249 Error("nick",ERR_WARNING
,"Tried to register duplicate nick extension %s",name
);
253 for (i
=0;i
<MAXNICKEXTS
;i
++) {
254 if (nickextnames
[i
]==NULL
) {
255 nickextnames
[i
]=getsstring(name
,100);
260 Error("nick",ERR_WARNING
,"Tried to register too many nick extensions: %s",name
);
264 int findnickext(const char *name
) {
267 for (i
=0;i
<MAXNICKEXTS
;i
++) {
268 if (nickextnames
[i
]!=NULL
&& !ircd_strcmp(name
,nickextnames
[i
]->content
)) {
276 void releasenickext(int index
) {
280 freesstring(nickextnames
[index
]);
281 nickextnames
[index
]=NULL
;
283 for (i
=0;i
<NICKHASHSIZE
;i
++) {
284 for (np
=nicktable
[i
];np
;np
=np
->next
) {
285 np
->exts
[index
]=NULL
;
291 * Produces the "apparent" hostmask as seen by network users.
294 char *visiblehostmask(nick
*np
, char *buf
) {
295 char hostbuf
[HOSTLEN
+1];
299 host
=np
->host
->name
->content
;
303 ident
=np
->shident
->content
;
306 host
=np
->sethost
->content
;
308 } else if (IsAccount(np
) && IsHideHost(np
)) {
309 sprintf(hostbuf
,"%s.%s", np
->authname
, HIS_HIDDENHOST
);
313 sprintf(buf
,"%s!%s@%s",np
->nick
,ident
,host
);
321 * gethandlebynumeric:
322 * Given a numeric, gives the location in the servernicks table
323 * where it should be. Does not check that the nick currently found
324 * there (if any) has the correct numeric; this is left to the
325 * calling function to figure out.
328 nick
**gethandlebynumeric(long numeric
) {
333 /* Shift off the client identifier part of the numeric to get the server ID */
334 servernum
=(numeric
>>18);
336 if ((serv
=getserverdata(servernum
))==NULL
) {
337 Error("nick",ERR_WARNING
,"Numeric %ld refers to non-existent server %d",numeric
,servernum
);
341 /* Compute the masked numeric */
342 maskednum
=numeric
&(serv
->maxusernum
);
344 return (servernicks
[servernum
])+maskednum
;
348 * getnickbynumeric[str]()
349 * These functions retrieve a nick based on it's numeric on the network
350 * Use the approriate function depending on how your numeric is expressed..
353 nick
*getnickbynumeric(long numeric
) {
356 nh
=gethandlebynumeric(numeric
);
358 if ((*nh
) && ((*nh
)->numeric
!=numeric
)) {
359 /* We found a masked numeric match, but the actual numeric
360 * is different. This counts as a miss. */
367 nick
*getnickbynumericstr(char *numericstr
) {
368 return getnickbynumeric(numerictolong(numericstr
,5));
373 int registernodeext(const char *name
) {
376 if (findnodeext(name
)!=-1) {
377 Error("nick",ERR_WARNING
,"Tried to register duplicate node extension %s",name
);
381 for (i
=0;i
<PATRICIA_MAXSLOTS
;i
++) {
382 if (nodeextnames
[i
]==NULL
) {
383 nodeextnames
[i
]=getsstring(name
,100);
388 Error("nick",ERR_WARNING
,"Tried to register too many extensions: %s",name
);
392 int findnodeext(const char *name
) {
395 for (i
=0;i
<PATRICIA_MAXSLOTS
;i
++) {
396 if (nodeextnames
[i
]!=NULL
&& !ircd_strcmp(name
,nodeextnames
[i
]->content
)) {
404 void releasenodeext(int index
) {
405 patricia_node_t
*head
, *node
;
407 freesstring(nodeextnames
[index
]);
408 nodeextnames
[index
]=NULL
;
412 PATRICIA_WALK_ALL(head
, node
)
414 node
->slots
[index
]=NULL
;