]>
jfr.im git - irc/quakenet/newserv.git/blob - nick/nick.c
9b47eef72b5fb199a2b1bda6620317235530945c
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"
21 const flag umodeflags
[] = {
23 { 'w', UMODE_WALLOPS
},
26 { 'k', UMODE_SERVICE
},
29 { 'r', UMODE_ACCOUNT
},
30 { 'n', UMODE_HIDECHAN
},
31 { 'x', UMODE_HIDEHOST
},
32 { 'h', UMODE_SETHOST
},
33 { 'R', UMODE_REGPRIV
},
34 { 'I', UMODE_HIDEIDLE
},
37 #define nickhash(x) ((crc32i(x))%NICKHASHSIZE)
39 nick
*nicktable
[NICKHASHSIZE
];
40 nick
**servernicks
[MAXSERVERS
];
42 sstring
*nickextnames
[MAXNICKEXTS
];
43 sstring
*nodeextnames
[PATRICIA_MAXSLOTS
];
44 patricia_tree_t
*iptree
;
46 void nickstats(int hooknum
, void *arg
);
51 memset(nicktable
,0,sizeof(nicktable
));
52 memset(servernicks
,0,sizeof(servernicks
));
54 /* Register our hooks */
55 registerhook(HOOK_SERVER_NEWSERVER
,&handleserverchange
);
56 registerhook(HOOK_SERVER_LOSTSERVER
,&handleserverchange
);
57 registerhook(HOOK_CORE_STATSREQUEST
,&nickstats
);
59 /* And our server handlers */
60 registerserverhandler("N",&handlenickmsg
,10);
61 registerserverhandler("D",&handlekillmsg
,2);
62 registerserverhandler("Q",&handlequitmsg
,1);
63 registerserverhandler("M",&handleusermodemsg
,3);
64 registerserverhandler("W",&handlewhoismsg
,2);
65 registerserverhandler("AC",&handleaccountmsg
,4);
66 registerserverhandler("R",&handlestatsmsg
,2);
68 /* Fake the addition of our own server */
69 handleserverchange(HOOK_SERVER_NEWSERVER
,(void *)numerictolong(mynumeric
->content
,2));
71 iptree
= patricia_new_tree(PATRICIA_MAXBITS
);
75 * This function handles servers appearing and disappearing.
76 * For a new server, the client table is allocated.
77 * For a disappearing server, all it's clients are killed and the client table is freed.
80 void handleserverchange(int hooknum
, void *arg
) {
87 case HOOK_SERVER_NEWSERVER
:
88 servernicks
[servernum
]=(nick
**)malloc((serverlist
[servernum
].maxusernum
+1)*sizeof(nick
**));
89 memset(servernicks
[servernum
],0,(serverlist
[servernum
].maxusernum
+1)*sizeof(nick
**));
92 case HOOK_SERVER_LOSTSERVER
:
93 for (i
=0;i
<=serverlist
[servernum
].maxusernum
;i
++) {
94 if (servernicks
[servernum
][i
]!=NULL
) {
95 deletenick(servernicks
[servernum
][i
]);
98 free(servernicks
[servernum
]);
106 * This function handles the removal of a nick from the network
109 void deletenick(nick
*np
) {
112 /* Fire the hook. This will deal with removal from channels etc. */
113 triggerhook(HOOK_NICK_LOSTNICK
, np
);
115 /* Release the realname and hostname parts */
117 for (nh
=&(np
->realname
->nicks
);*nh
;nh
=&((*nh
)->nextbyrealname
)) {
119 *nh
=np
->nextbyrealname
;
124 for (nh
=&(np
->host
->nicks
);*nh
;nh
=&((*nh
)->nextbyhost
)) {
131 releaserealname(np
->realname
);
132 releasehost(np
->host
);
134 if(IsAccount(np
) && np
->auth
)
136 np
->auth
->usercount
--;
138 for (nh
=&(np
->auth
->nicks
);*nh
;nh
=&((*nh
)->nextbyauthname
)) {
140 *nh
=np
->nextbyauthname
;
145 releaseauthname(np
->auth
);
148 freesstring(np
->shident
); /* freesstring(NULL) is OK */
149 freesstring(np
->sethost
);
151 derefnode(iptree
, np
->ipnode
);
153 /* TODO: figure out how to cleanly remove nodes without affecting other modules */
155 /* Delete the nick from the servernick table */
156 *(gethandlebynumericunsafe(np
->numeric
))=NULL
;
158 /* Remove the nick from the hash table */
159 removenickfromhash(np
);
164 void addnicktohash(nick
*np
) {
165 np
->next
=nicktable
[nickhash(np
->nick
)];
166 nicktable
[nickhash(np
->nick
)]=np
;
169 void removenickfromhash(nick
*np
) {
172 for (nh
=&(nicktable
[nickhash(np
->nick
)]);*nh
;nh
=&((*nh
)->next
)) {
180 nick
*getnickbynick(const char *name
) {
183 for (np
=nicktable
[nickhash(name
)];np
;np
=np
->next
) {
184 if (!ircd_strcmp(np
->nick
,name
))
191 void nickstats(int hooknum
, void *arg
) {
192 int total
,maxchain
,curchain
,i
,buckets
;
197 buckets
=total
=maxchain
=curchain
=0;
198 for (i
=0;i
<NICKHASHSIZE
;i
++,curchain
=0) {
202 for (;np
;np
=np
->next
) {
207 if (curchain
>maxchain
) {
214 sprintf(buf
,"Nick : %6d nicks (HASH: %6d/%6d, chain %3d)",total
,buckets
,NICKHASHSIZE
,maxchain
);
215 } else if ((long)arg
>2) {
216 sprintf(buf
,"Nick : %6d users on network.",total
);
220 triggerhook(HOOK_CORE_STATSREPLY
,buf
);
224 int registernickext(const char *name
) {
227 if (findnickext(name
)!=-1) {
228 Error("nick",ERR_WARNING
,"Tried to register duplicate nick extension %s",name
);
232 for (i
=0;i
<MAXNICKEXTS
;i
++) {
233 if (nickextnames
[i
]==NULL
) {
234 nickextnames
[i
]=getsstring(name
,100);
239 Error("nick",ERR_WARNING
,"Tried to register too many nick extensions: %s",name
);
243 int findnickext(const char *name
) {
246 for (i
=0;i
<MAXNICKEXTS
;i
++) {
247 if (nickextnames
[i
]!=NULL
&& !ircd_strcmp(name
,nickextnames
[i
]->content
)) {
255 void releasenickext(int index
) {
259 freesstring(nickextnames
[index
]);
260 nickextnames
[index
]=NULL
;
262 for (i
=0;i
<NICKHASHSIZE
;i
++) {
263 for (np
=nicktable
[i
];np
;np
=np
->next
) {
264 np
->exts
[index
]=NULL
;
270 * Produces the "apparent" hostmask as seen by network users.
273 char *visiblehostmask(nick
*np
, char *buf
) {
274 char hostbuf
[HOSTLEN
+1];
278 host
=np
->host
->name
->content
;
282 ident
=np
->shident
->content
;
285 host
=np
->sethost
->content
;
287 } else if (IsAccount(np
) && IsHideHost(np
)) {
288 sprintf(hostbuf
,"%s.%s", np
->authname
, HIS_HIDDENHOST
);
292 sprintf(buf
,"%s!%s@%s",np
->nick
,ident
,host
);
300 * gethandlebynumeric:
301 * Given a numeric, gives the location in the servernicks table
302 * where it should be. Does not check that the nick currently found
303 * there (if any) has the correct numeric; this is left to the
304 * calling function to figure out.
307 nick
**gethandlebynumeric(long numeric
) {
312 /* Shift off the client identifier part of the numeric to get the server ID */
313 servernum
=(numeric
>>18);
315 if ((serv
=getserverdata(servernum
))==NULL
) {
316 Error("nick",ERR_WARNING
,"Numeric %ld refers to non-existent server %d",numeric
,servernum
);
320 /* Compute the masked numeric */
321 maskednum
=numeric
&(serv
->maxusernum
);
323 return (servernicks
[servernum
])+maskednum
;
327 * getnickbynumeric[str]()
328 * These functions retrieve a nick based on it's numeric on the network
329 * Use the approriate function depending on how your numeric is expressed..
332 nick
*getnickbynumeric(long numeric
) {
335 nh
=gethandlebynumeric(numeric
);
337 if ((*nh
) && ((*nh
)->numeric
!=numeric
)) {
338 /* We found a masked numeric match, but the actual numeric
339 * is different. This counts as a miss. */
346 nick
*getnickbynumericstr(char *numericstr
) {
347 return getnickbynumeric(numerictolong(numericstr
,5));
352 int registernodeext(const char *name
) {
355 if (findnodeext(name
)!=-1) {
356 Error("nick",ERR_WARNING
,"Tried to register duplicate node extension %s",name
);
360 for (i
=0;i
<PATRICIA_MAXSLOTS
;i
++) {
361 if (nodeextnames
[i
]==NULL
) {
362 nodeextnames
[i
]=getsstring(name
,100);
367 Error("nick",ERR_WARNING
,"Tried to register too many extensions: %s",name
);
371 int findnodeext(const char *name
) {
374 for (i
=0;i
<PATRICIA_MAXSLOTS
;i
++) {
375 if (nodeextnames
[i
]!=NULL
&& !ircd_strcmp(name
,nodeextnames
[i
]->content
)) {
383 void releasenodeext(int index
) {
384 patricia_node_t
*head
, *node
;
386 freesstring(nodeextnames
[index
]);
387 nodeextnames
[index
]=NULL
;
391 PATRICIA_WALK_ALL(head
, node
)
393 node
->slots
[index
]=NULL
;