]>
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);
82 /* Fake the addition of our own server */
83 handleserverchange(HOOK_SERVER_NEWSERVER
,(void *)numerictolong(mynumeric
->content
,2));
92 for (i
=0;i
<NICKHASHSIZE
;i
++) {
93 for (np
=nicktable
[i
];np
;np
=np
->next
) {
94 freesstring(np
->shident
);
95 freesstring(np
->sethost
);
102 deregisterhook(HOOK_SERVER_NEWSERVER
,&handleserverchange
);
103 deregisterhook(HOOK_SERVER_LOSTSERVER
,&handleserverchange
);
104 deregisterhook(HOOK_CORE_STATSREQUEST
,&nickstats
);
106 /* And our server handlers */
107 deregisterserverhandler("N",&handlenickmsg
);
108 deregisterserverhandler("D",&handlekillmsg
);
109 deregisterserverhandler("Q",&handlequitmsg
);
110 deregisterserverhandler("M",&handleusermodemsg
);
111 deregisterserverhandler("W",&handlewhoismsg
);
112 deregisterserverhandler("AC",&handleaccountmsg
);
113 deregisterserverhandler("R",&handlestatsmsg
);
117 * This function handles servers appearing and disappearing.
118 * For a new server, the client table is allocated.
119 * For a disappearing server, all it's clients are killed and the client table is freed.
122 void handleserverchange(int hooknum
, void *arg
) {
129 case HOOK_SERVER_NEWSERVER
:
130 servernicks
[servernum
]=(nick
**)nsmalloc(POOL_NICK
,(serverlist
[servernum
].maxusernum
+1)*sizeof(nick
**));
131 memset(servernicks
[servernum
],0,(serverlist
[servernum
].maxusernum
+1)*sizeof(nick
**));
134 case HOOK_SERVER_LOSTSERVER
:
135 for (i
=0;i
<=serverlist
[servernum
].maxusernum
;i
++) {
136 if (servernicks
[servernum
][i
]!=NULL
) {
137 deletenick(servernicks
[servernum
][i
]);
140 nsfree(POOL_NICK
,servernicks
[servernum
]);
148 * This function handles the removal of a nick from the network
151 void deletenick(nick
*np
) {
154 /* Fire the hook. This will deal with removal from channels etc. */
155 triggerhook(HOOK_NICK_LOSTNICK
, np
);
157 /* Release the realname and hostname parts */
159 for (nh
=&(np
->realname
->nicks
);*nh
;nh
=&((*nh
)->nextbyrealname
)) {
161 *nh
=np
->nextbyrealname
;
166 for (nh
=&(np
->host
->nicks
);*nh
;nh
=&((*nh
)->nextbyhost
)) {
173 releaserealname(np
->realname
);
174 releasehost(np
->host
);
176 if(IsAccount(np
) && np
->auth
)
178 np
->auth
->usercount
--;
180 for (nh
=&(np
->auth
->nicks
);*nh
;nh
=&((*nh
)->nextbyauthname
)) {
182 *nh
=np
->nextbyauthname
;
187 releaseauthname(np
->auth
);
190 freesstring(np
->shident
); /* freesstring(NULL) is OK */
191 freesstring(np
->sethost
);
193 np
->ipnode
->usercount
--;
194 derefnode(iptree
, np
->ipnode
);
196 /* TODO: figure out how to cleanly remove nodes without affecting other modules */
198 /* Delete the nick from the servernick table */
199 *(gethandlebynumericunsafe(np
->numeric
))=NULL
;
201 /* Remove the nick from the hash table */
202 removenickfromhash(np
);
207 void addnicktohash(nick
*np
) {
208 np
->next
=nicktable
[nickhash(np
->nick
)];
209 nicktable
[nickhash(np
->nick
)]=np
;
212 void removenickfromhash(nick
*np
) {
215 for (nh
=&(nicktable
[nickhash(np
->nick
)]);*nh
;nh
=&((*nh
)->next
)) {
223 nick
*getnickbynick(const char *name
) {
226 for (np
=nicktable
[nickhash(name
)];np
;np
=np
->next
) {
227 if (!ircd_strcmp(np
->nick
,name
))
234 void nickstats(int hooknum
, void *arg
) {
235 int total
,maxchain
,curchain
,i
,buckets
;
240 buckets
=total
=maxchain
=curchain
=0;
241 for (i
=0;i
<NICKHASHSIZE
;i
++,curchain
=0) {
245 for (;np
;np
=np
->next
) {
250 if (curchain
>maxchain
) {
257 sprintf(buf
,"Nick : %6d nicks (HASH: %6d/%6d, chain %3d)",total
,buckets
,NICKHASHSIZE
,maxchain
);
258 } else if ((long)arg
>2) {
259 sprintf(buf
,"Nick : %6d users on network.",total
);
263 triggerhook(HOOK_CORE_STATSREPLY
,buf
);
267 int registernickext(const char *name
) {
270 if (findnickext(name
)!=-1) {
271 Error("nick",ERR_WARNING
,"Tried to register duplicate nick extension %s",name
);
275 for (i
=0;i
<MAXNICKEXTS
;i
++) {
276 if (nickextnames
[i
]==NULL
) {
277 nickextnames
[i
]=getsstring(name
,100);
282 Error("nick",ERR_WARNING
,"Tried to register too many nick extensions: %s",name
);
286 int findnickext(const char *name
) {
289 for (i
=0;i
<MAXNICKEXTS
;i
++) {
290 if (nickextnames
[i
]!=NULL
&& !ircd_strcmp(name
,nickextnames
[i
]->content
)) {
298 void releasenickext(int index
) {
302 freesstring(nickextnames
[index
]);
303 nickextnames
[index
]=NULL
;
305 for (i
=0;i
<NICKHASHSIZE
;i
++) {
306 for (np
=nicktable
[i
];np
;np
=np
->next
) {
307 np
->exts
[index
]=NULL
;
313 * Produces the "apparent" hostmask as seen by network users.
316 char *visiblehostmask(nick
*np
, char *buf
) {
317 char uhbuf
[USERLEN
+HOSTLEN
+2];
319 visibleuserhost(np
, uhbuf
);
320 sprintf(buf
,"%s!%s",np
->nick
,uhbuf
);
326 * As above without nick
329 char *visibleuserhost(nick
*np
, char *buf
) {
330 char hostbuf
[HOSTLEN
+1];
334 host
=np
->host
->name
->content
;
338 ident
=np
->shident
->content
;
341 host
=np
->sethost
->content
;
343 } else if (IsAccount(np
) && IsHideHost(np
)) {
344 sprintf(hostbuf
,"%s.%s", np
->authname
, HIS_HIDDENHOST
);
348 sprintf(buf
,"%s@%s",ident
,host
);
356 * gethandlebynumeric:
357 * Given a numeric, gives the location in the servernicks table
358 * where it should be. Does not check that the nick currently found
359 * there (if any) has the correct numeric; this is left to the
360 * calling function to figure out.
363 nick
**gethandlebynumeric(long numeric
) {
368 /* Shift off the client identifier part of the numeric to get the server ID */
369 servernum
=(numeric
>>18);
371 if ((serv
=getserverdata(servernum
))==NULL
) {
372 Error("nick",ERR_WARNING
,"Numeric %ld refers to non-existent server %d",numeric
,servernum
);
376 /* Compute the masked numeric */
377 maskednum
=numeric
&(serv
->maxusernum
);
379 return (servernicks
[servernum
])+maskednum
;
383 * getnickbynumeric[str]()
384 * These functions retrieve a nick based on it's numeric on the network
385 * Use the approriate function depending on how your numeric is expressed..
388 nick
*getnickbynumeric(long numeric
) {
391 nh
=gethandlebynumeric(numeric
);
393 if ((*nh
) && ((*nh
)->numeric
!=numeric
)) {
394 /* We found a masked numeric match, but the actual numeric
395 * is different. This counts as a miss. */
402 nick
*getnickbynumericstr(char *numericstr
) {
403 return getnickbynumeric(numerictolong(numericstr
,5));