]>
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 { 'd', AFLAG_DEVELOPER
},
45 #define nickhash(x) ((crc32i(x))%NICKHASHSIZE)
47 nick
*nicktable
[NICKHASHSIZE
];
48 nick
**servernicks
[MAXSERVERS
];
50 sstring
*nickextnames
[MAXNICKEXTS
];
52 void nickstats(int hooknum
, void *arg
);
58 /* Clear up the nicks in authext */
59 for (i
=0;i
<AUTHNAMEHASHSIZE
;i
++)
60 for (anp
=authnametable
[i
];anp
;anp
=anp
->next
)
65 memset(nicktable
,0,sizeof(nicktable
));
66 memset(servernicks
,0,sizeof(servernicks
));
68 /* Register our hooks */
69 registerhook(HOOK_SERVER_NEWSERVER
,&handleserverchange
);
70 registerhook(HOOK_SERVER_LOSTSERVER
,&handleserverchange
);
71 registerhook(HOOK_CORE_STATSREQUEST
,&nickstats
);
73 /* And our server handlers */
74 registerserverhandler("N",&handlenickmsg
,10);
75 registerserverhandler("D",&handlekillmsg
,2);
76 registerserverhandler("Q",&handlequitmsg
,1);
77 registerserverhandler("M",&handleusermodemsg
,3);
78 registerserverhandler("W",&handlewhoismsg
,2);
79 registerserverhandler("AC",&handleaccountmsg
,4);
80 registerserverhandler("R",&handlestatsmsg
,2);
81 registerserverhandler("P",&handleprivmsg
,2);
83 /* Fake the addition of our own server */
84 handleserverchange(HOOK_SERVER_NEWSERVER
,(void *)numerictolong(mynumeric
->content
,2));
93 for (i
=0;i
<NICKHASHSIZE
;i
++) {
94 for (np
=nicktable
[i
];np
;np
=np
->next
) {
95 freesstring(np
->shident
);
96 freesstring(np
->sethost
);
100 nsfreeall(POOL_NICK
);
103 deregisterhook(HOOK_SERVER_NEWSERVER
,&handleserverchange
);
104 deregisterhook(HOOK_SERVER_LOSTSERVER
,&handleserverchange
);
105 deregisterhook(HOOK_CORE_STATSREQUEST
,&nickstats
);
107 /* And our server handlers */
108 deregisterserverhandler("N",&handlenickmsg
);
109 deregisterserverhandler("D",&handlekillmsg
);
110 deregisterserverhandler("Q",&handlequitmsg
);
111 deregisterserverhandler("M",&handleusermodemsg
);
112 deregisterserverhandler("W",&handlewhoismsg
);
113 deregisterserverhandler("AC",&handleaccountmsg
);
114 deregisterserverhandler("R",&handlestatsmsg
);
115 deregisterserverhandler("P",&handleprivmsg
);
119 * This function handles servers appearing and disappearing.
120 * For a new server, the client table is allocated.
121 * For a disappearing server, all it's clients are killed and the client table is freed.
124 void handleserverchange(int hooknum
, void *arg
) {
131 case HOOK_SERVER_NEWSERVER
:
132 servernicks
[servernum
]=(nick
**)nsmalloc(POOL_NICK
,(serverlist
[servernum
].maxusernum
+1)*sizeof(nick
**));
133 memset(servernicks
[servernum
],0,(serverlist
[servernum
].maxusernum
+1)*sizeof(nick
**));
136 case HOOK_SERVER_LOSTSERVER
:
137 for (i
=0;i
<=serverlist
[servernum
].maxusernum
;i
++) {
138 if (servernicks
[servernum
][i
]!=NULL
) {
139 deletenick(servernicks
[servernum
][i
]);
142 nsfree(POOL_NICK
,servernicks
[servernum
]);
150 * This function handles the removal of a nick from the network
153 void deletenick(nick
*np
) {
156 /* Fire the hook. This will deal with removal from channels etc. */
157 triggerhook(HOOK_NICK_LOSTNICK
, np
);
159 /* Release the realname and hostname parts */
161 for (nh
=&(np
->realname
->nicks
);*nh
;nh
=&((*nh
)->nextbyrealname
)) {
163 *nh
=np
->nextbyrealname
;
168 for (nh
=&(np
->host
->nicks
);*nh
;nh
=&((*nh
)->nextbyhost
)) {
175 releaserealname(np
->realname
);
176 releasehost(np
->host
);
178 if(IsAccount(np
) && np
->auth
)
180 np
->auth
->usercount
--;
182 for (nh
=&(np
->auth
->nicks
);*nh
;nh
=&((*nh
)->nextbyauthname
)) {
184 *nh
=np
->nextbyauthname
;
189 releaseauthname(np
->auth
);
192 freesstring(np
->shident
); /* freesstring(NULL) is OK */
193 freesstring(np
->sethost
);
195 node_decrement_usercount(np
->ipnode
);
196 derefnode(iptree
, np
->ipnode
);
198 /* TODO: figure out how to cleanly remove nodes without affecting other modules */
200 /* Delete the nick from the servernick table */
201 *(gethandlebynumericunsafe(np
->numeric
))=NULL
;
203 /* Remove the nick from the hash table */
204 removenickfromhash(np
);
209 void addnicktohash(nick
*np
) {
210 np
->next
=nicktable
[nickhash(np
->nick
)];
211 nicktable
[nickhash(np
->nick
)]=np
;
214 void removenickfromhash(nick
*np
) {
217 for (nh
=&(nicktable
[nickhash(np
->nick
)]);*nh
;nh
=&((*nh
)->next
)) {
225 nick
*getnickbynick(const char *name
) {
228 for (np
=nicktable
[nickhash(name
)];np
;np
=np
->next
) {
229 if (!ircd_strcmp(np
->nick
,name
))
236 void nickstats(int hooknum
, void *arg
) {
237 int total
,maxchain
,curchain
,i
,buckets
;
242 buckets
=total
=maxchain
=curchain
=0;
243 for (i
=0;i
<NICKHASHSIZE
;i
++,curchain
=0) {
247 for (;np
;np
=np
->next
) {
252 if (curchain
>maxchain
) {
259 sprintf(buf
,"Nick : %6d nicks (HASH: %6d/%6d, chain %3d)",total
,buckets
,NICKHASHSIZE
,maxchain
);
260 } else if ((long)arg
>2) {
261 sprintf(buf
,"Nick : %6d users on network.",total
);
265 triggerhook(HOOK_CORE_STATSREPLY
,buf
);
269 int registernickext(const char *name
) {
272 if (findnickext(name
)!=-1) {
273 Error("nick",ERR_WARNING
,"Tried to register duplicate nick extension %s",name
);
277 for (i
=0;i
<MAXNICKEXTS
;i
++) {
278 if (nickextnames
[i
]==NULL
) {
279 nickextnames
[i
]=getsstring(name
,100);
284 Error("nick",ERR_WARNING
,"Tried to register too many nick extensions: %s",name
);
288 int findnickext(const char *name
) {
291 for (i
=0;i
<MAXNICKEXTS
;i
++) {
292 if (nickextnames
[i
]!=NULL
&& !ircd_strcmp(name
,nickextnames
[i
]->content
)) {
300 void releasenickext(int index
) {
304 freesstring(nickextnames
[index
]);
305 nickextnames
[index
]=NULL
;
307 for (i
=0;i
<NICKHASHSIZE
;i
++) {
308 for (np
=nicktable
[i
];np
;np
=np
->next
) {
309 np
->exts
[index
]=NULL
;
315 * Produces the "apparent" hostmask as seen by network users.
318 char *visiblehostmask(nick
*np
, char *buf
) {
319 char uhbuf
[USERLEN
+HOSTLEN
+2];
321 visibleuserhost(np
, uhbuf
);
322 sprintf(buf
,"%s!%s",np
->nick
,uhbuf
);
328 * As above without nick
331 char *visibleuserhost(nick
*np
, char *buf
) {
332 char hostbuf
[HOSTLEN
+1];
336 host
=np
->host
->name
->content
;
340 ident
=np
->shident
->content
;
343 host
=np
->sethost
->content
;
345 } else if (IsAccount(np
) && IsHideHost(np
)) {
346 sprintf(hostbuf
,"%s.%s", np
->authname
, HIS_HIDDENHOST
);
350 sprintf(buf
,"%s@%s",ident
,host
);
358 * gethandlebynumeric:
359 * Given a numeric, gives the location in the servernicks table
360 * where it should be. Does not check that the nick currently found
361 * there (if any) has the correct numeric; this is left to the
362 * calling function to figure out.
365 nick
**gethandlebynumeric(long numeric
) {
370 /* Shift off the client identifier part of the numeric to get the server ID */
371 servernum
=(numeric
>>18);
373 if ((serv
=getserverdata(servernum
))==NULL
) {
374 Error("nick",ERR_WARNING
,"Numeric %ld refers to non-existent server %d",numeric
,servernum
);
378 /* Compute the masked numeric */
379 maskednum
=numeric
&(serv
->maxusernum
);
381 return (servernicks
[servernum
])+maskednum
;
385 * getnickbynumeric[str]()
386 * These functions retrieve a nick based on it's numeric on the network
387 * Use the approriate function depending on how your numeric is expressed..
390 nick
*getnickbynumeric(long numeric
) {
393 nh
=gethandlebynumeric(numeric
);
395 if ((*nh
) && ((*nh
)->numeric
!=numeric
)) {
396 /* We found a masked numeric match, but the actual numeric
397 * is different. This counts as a miss. */
404 nick
*getnickbynumericstr(char *numericstr
) {
405 return getnickbynumeric(numerictolong(numericstr
,5));