]>
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
);
61 /* Clear up the nicks in authext */
62 for (i
=0;i
<AUTHNAMEHASHSIZE
;i
++)
63 for (anp
=authnametable
[i
];anp
;anp
=anp
->next
)
68 memset(nicktable
,0,sizeof(nicktable
));
69 memset(servernicks
,0,sizeof(servernicks
));
71 /* Register our hooks */
72 registerhook(HOOK_SERVER_NEWSERVER
,&handleserverchange
);
73 registerhook(HOOK_SERVER_LOSTSERVER
,&handleserverchange
);
74 registerhook(HOOK_CORE_STATSREQUEST
,&nickstats
);
76 /* And our server handlers */
77 registerserverhandler("N",&handlenickmsg
,10);
78 registerserverhandler("D",&handlekillmsg
,2);
79 registerserverhandler("Q",&handlequitmsg
,1);
80 registerserverhandler("M",&handleusermodemsg
,3);
81 registerserverhandler("W",&handlewhoismsg
,2);
82 registerserverhandler("AC",&handleaccountmsg
,4);
83 registerserverhandler("R",&handlestatsmsg
,2);
84 registerserverhandler("P",&handleprivmsg
,2);
86 /* Fake the addition of our own server */
87 handleserverchange(HOOK_SERVER_NEWSERVER
,(void *)numerictolong(mynumeric
->content
,2));
96 for (i
=0;i
<NICKHASHSIZE
;i
++) {
97 for (np
=nicktable
[i
];np
;np
=np
->next
) {
98 freesstring(np
->shident
);
99 freesstring(np
->sethost
);
100 freesstring(np
->opername
);
101 if(!np
->auth
&& np
->authname
)
106 nsfreeall(POOL_NICK
);
109 deregisterhook(HOOK_SERVER_NEWSERVER
,&handleserverchange
);
110 deregisterhook(HOOK_SERVER_LOSTSERVER
,&handleserverchange
);
111 deregisterhook(HOOK_CORE_STATSREQUEST
,&nickstats
);
113 /* And our server handlers */
114 deregisterserverhandler("N",&handlenickmsg
);
115 deregisterserverhandler("D",&handlekillmsg
);
116 deregisterserverhandler("Q",&handlequitmsg
);
117 deregisterserverhandler("M",&handleusermodemsg
);
118 deregisterserverhandler("W",&handlewhoismsg
);
119 deregisterserverhandler("AC",&handleaccountmsg
);
120 deregisterserverhandler("R",&handlestatsmsg
);
121 deregisterserverhandler("P",&handleprivmsg
);
125 * This function handles servers appearing and disappearing.
126 * For a new server, the client table is allocated.
127 * For a disappearing server, all it's clients are killed and the client table is freed.
130 void handleserverchange(int hooknum
, void *arg
) {
137 case HOOK_SERVER_NEWSERVER
:
138 servernicks
[servernum
]=(nick
**)nsmalloc(POOL_NICK
,(serverlist
[servernum
].maxusernum
+1)*sizeof(nick
**));
139 memset(servernicks
[servernum
],0,(serverlist
[servernum
].maxusernum
+1)*sizeof(nick
**));
142 case HOOK_SERVER_LOSTSERVER
:
143 for (i
=0;i
<=serverlist
[servernum
].maxusernum
;i
++) {
144 if (servernicks
[servernum
][i
]!=NULL
) {
145 deletenick(servernicks
[servernum
][i
]);
148 nsfree(POOL_NICK
,servernicks
[servernum
]);
156 * This function handles the removal of a nick from the network
159 void deletenick(nick
*np
) {
162 /* Fire the hook. This will deal with removal from channels etc. */
163 triggerhook(HOOK_NICK_LOSTNICK
, np
);
165 /* Release the realname and hostname parts */
167 for (nh
=&(np
->realname
->nicks
);*nh
;nh
=&((*nh
)->nextbyrealname
)) {
169 *nh
=np
->nextbyrealname
;
174 for (nh
=&(np
->host
->nicks
);*nh
;nh
=&((*nh
)->nextbyhost
)) {
181 releaserealname(np
->realname
);
182 releasehost(np
->host
);
189 np
->auth
->usercount
--;
191 for (nh
=&(np
->auth
->nicks
);*nh
;nh
=&((*nh
)->nextbyauthname
)) {
193 *nh
=np
->nextbyauthname
;
198 releaseauthname(np
->auth
);
202 freesstring(np
->shident
); /* freesstring(NULL) is OK */
203 freesstring(np
->sethost
);
204 freesstring(np
->opername
);
206 node_decrement_usercount(np
->ipnode
);
207 derefnode(iptree
, np
->ipnode
);
209 /* TODO: figure out how to cleanly remove nodes without affecting other modules */
211 /* Delete the nick from the servernick table */
212 *(gethandlebynumericunsafe(np
->numeric
))=NULL
;
214 /* Remove the nick from the hash table */
215 removenickfromhash(np
);
220 void addnicktohash(nick
*np
) {
221 np
->next
=nicktable
[nickhash(np
->nick
)];
222 nicktable
[nickhash(np
->nick
)]=np
;
225 void removenickfromhash(nick
*np
) {
228 for (nh
=&(nicktable
[nickhash(np
->nick
)]);*nh
;nh
=&((*nh
)->next
)) {
236 nick
*getnickbynick(const char *name
) {
239 for (np
=nicktable
[nickhash(name
)];np
;np
=np
->next
) {
240 if (!ircd_strcmp(np
->nick
,name
))
247 void nickstats(int hooknum
, void *arg
) {
248 int total
,maxchain
,curchain
,i
,buckets
;
253 buckets
=total
=maxchain
=curchain
=0;
254 for (i
=0;i
<NICKHASHSIZE
;i
++,curchain
=0) {
258 for (;np
;np
=np
->next
) {
263 if (curchain
>maxchain
) {
270 sprintf(buf
,"Nick : %6d nicks (HASH: %6d/%6d, chain %3d)",total
,buckets
,NICKHASHSIZE
,maxchain
);
271 } else if ((long)arg
>2) {
272 sprintf(buf
,"Nick : %6d users on network.",total
);
276 triggerhook(HOOK_CORE_STATSREPLY
,buf
);
280 int registernickext(const char *name
) {
283 if (findnickext(name
)!=-1) {
284 Error("nick",ERR_WARNING
,"Tried to register duplicate nick extension %s",name
);
288 for (i
=0;i
<MAXNICKEXTS
;i
++) {
289 if (nickextnames
[i
]==NULL
) {
290 nickextnames
[i
]=getsstring(name
,100);
295 Error("nick",ERR_WARNING
,"Tried to register too many nick extensions: %s",name
);
299 int findnickext(const char *name
) {
302 for (i
=0;i
<MAXNICKEXTS
;i
++) {
303 if (nickextnames
[i
]!=NULL
&& !ircd_strcmp(name
,nickextnames
[i
]->content
)) {
311 void releasenickext(int index
) {
315 freesstring(nickextnames
[index
]);
316 nickextnames
[index
]=NULL
;
318 for (i
=0;i
<NICKHASHSIZE
;i
++) {
319 for (np
=nicktable
[i
];np
;np
=np
->next
) {
320 np
->exts
[index
]=NULL
;
326 * Produces the "apparent" hostmask as seen by network users.
329 char *visiblehostmask(nick
*np
, char *buf
) {
330 char uhbuf
[USERLEN
+HOSTLEN
+2];
332 visibleuserhost(np
, uhbuf
);
333 sprintf(buf
,"%s!%s",np
->nick
,uhbuf
);
339 * As above without nick
342 char *visibleuserhost(nick
*np
, char *buf
) {
343 char hostbuf
[HOSTLEN
+1];
347 host
=np
->host
->name
->content
;
351 ident
=np
->shident
->content
;
354 host
=np
->sethost
->content
;
356 } else if (IsAccount(np
) && IsHideHost(np
)) {
357 sprintf(hostbuf
,"%s.%s", np
->authname
, HIS_HIDDENHOST
);
361 sprintf(buf
,"%s@%s",ident
,host
);
369 * gethandlebynumeric:
370 * Given a numeric, gives the location in the servernicks table
371 * where it should be. Does not check that the nick currently found
372 * there (if any) has the correct numeric; this is left to the
373 * calling function to figure out.
376 nick
**gethandlebynumeric(long numeric
) {
381 /* Shift off the client identifier part of the numeric to get the server ID */
382 servernum
=(numeric
>>18);
384 if ((serv
=getserverdata(servernum
))==NULL
) {
385 Error("nick",ERR_WARNING
,"Numeric %ld refers to non-existent server %d",numeric
,servernum
);
389 /* Compute the masked numeric */
390 maskednum
=numeric
&(serv
->maxusernum
);
392 return (servernicks
[servernum
])+maskednum
;
396 * getnickbynumeric[str]()
397 * These functions retrieve a nick based on it's numeric on the network
398 * Use the approriate function depending on how your numeric is expressed..
401 nick
*getnickbynumeric(long numeric
) {
404 nh
=gethandlebynumeric(numeric
);
406 if ((*nh
) && ((*nh
)->numeric
!=numeric
)) {
407 /* We found a masked numeric match, but the actual numeric
408 * is different. This counts as a miss. */
415 nick
*getnickbynumericstr(char *numericstr
) {
416 return getnickbynumeric(numerictolong(numericstr
,5));