]>
jfr.im git - irc/quakenet/newserv.git/blob - nick/nick.c
809a41d331b2c05a005b077bab6697127a64e130
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 { 's', 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
);
104 nsfreeall(POOL_NICK
);
107 deregisterhook(HOOK_SERVER_NEWSERVER
,&handleserverchange
);
108 deregisterhook(HOOK_SERVER_LOSTSERVER
,&handleserverchange
);
109 deregisterhook(HOOK_CORE_STATSREQUEST
,&nickstats
);
111 /* And our server handlers */
112 deregisterserverhandler("N",&handlenickmsg
);
113 deregisterserverhandler("D",&handlekillmsg
);
114 deregisterserverhandler("Q",&handlequitmsg
);
115 deregisterserverhandler("M",&handleusermodemsg
);
116 deregisterserverhandler("W",&handlewhoismsg
);
117 deregisterserverhandler("AC",&handleaccountmsg
);
118 deregisterserverhandler("R",&handlestatsmsg
);
119 deregisterserverhandler("P",&handleprivmsg
);
123 * This function handles servers appearing and disappearing.
124 * For a new server, the client table is allocated.
125 * For a disappearing server, all it's clients are killed and the client table is freed.
128 void handleserverchange(int hooknum
, void *arg
) {
135 case HOOK_SERVER_NEWSERVER
:
136 servernicks
[servernum
]=(nick
**)nsmalloc(POOL_NICK
,(serverlist
[servernum
].maxusernum
+1)*sizeof(nick
**));
137 memset(servernicks
[servernum
],0,(serverlist
[servernum
].maxusernum
+1)*sizeof(nick
**));
140 case HOOK_SERVER_LOSTSERVER
:
141 for (i
=0;i
<=serverlist
[servernum
].maxusernum
;i
++) {
142 if (servernicks
[servernum
][i
]!=NULL
) {
143 deletenick(servernicks
[servernum
][i
]);
146 nsfree(POOL_NICK
,servernicks
[servernum
]);
154 * This function handles the removal of a nick from the network
157 void deletenick(nick
*np
) {
160 /* Fire the hook. This will deal with removal from channels etc. */
161 triggerhook(HOOK_NICK_LOSTNICK
, np
);
163 /* Release the realname and hostname parts */
165 for (nh
=&(np
->realname
->nicks
);*nh
;nh
=&((*nh
)->nextbyrealname
)) {
167 *nh
=np
->nextbyrealname
;
172 for (nh
=&(np
->host
->nicks
);*nh
;nh
=&((*nh
)->nextbyhost
)) {
179 releaserealname(np
->realname
);
180 releasehost(np
->host
);
182 if(IsAccount(np
) && np
->auth
)
184 np
->auth
->usercount
--;
186 for (nh
=&(np
->auth
->nicks
);*nh
;nh
=&((*nh
)->nextbyauthname
)) {
188 *nh
=np
->nextbyauthname
;
193 releaseauthname(np
->auth
);
196 freesstring(np
->shident
); /* freesstring(NULL) is OK */
197 freesstring(np
->sethost
);
198 freesstring(np
->opername
);
200 node_decrement_usercount(np
->ipnode
);
201 derefnode(iptree
, np
->ipnode
);
203 /* TODO: figure out how to cleanly remove nodes without affecting other modules */
205 /* Delete the nick from the servernick table */
206 *(gethandlebynumericunsafe(np
->numeric
))=NULL
;
208 /* Remove the nick from the hash table */
209 removenickfromhash(np
);
214 void addnicktohash(nick
*np
) {
215 np
->next
=nicktable
[nickhash(np
->nick
)];
216 nicktable
[nickhash(np
->nick
)]=np
;
219 void removenickfromhash(nick
*np
) {
222 for (nh
=&(nicktable
[nickhash(np
->nick
)]);*nh
;nh
=&((*nh
)->next
)) {
230 nick
*getnickbynick(const char *name
) {
233 for (np
=nicktable
[nickhash(name
)];np
;np
=np
->next
) {
234 if (!ircd_strcmp(np
->nick
,name
))
241 void nickstats(int hooknum
, void *arg
) {
242 int total
,maxchain
,curchain
,i
,buckets
;
247 buckets
=total
=maxchain
=curchain
=0;
248 for (i
=0;i
<NICKHASHSIZE
;i
++,curchain
=0) {
252 for (;np
;np
=np
->next
) {
257 if (curchain
>maxchain
) {
264 sprintf(buf
,"Nick : %6d nicks (HASH: %6d/%6d, chain %3d)",total
,buckets
,NICKHASHSIZE
,maxchain
);
265 } else if ((long)arg
>2) {
266 sprintf(buf
,"Nick : %6d users on network.",total
);
270 triggerhook(HOOK_CORE_STATSREPLY
,buf
);
274 int registernickext(const char *name
) {
277 if (findnickext(name
)!=-1) {
278 Error("nick",ERR_WARNING
,"Tried to register duplicate nick extension %s",name
);
282 for (i
=0;i
<MAXNICKEXTS
;i
++) {
283 if (nickextnames
[i
]==NULL
) {
284 nickextnames
[i
]=getsstring(name
,100);
289 Error("nick",ERR_WARNING
,"Tried to register too many nick extensions: %s",name
);
293 int findnickext(const char *name
) {
296 for (i
=0;i
<MAXNICKEXTS
;i
++) {
297 if (nickextnames
[i
]!=NULL
&& !ircd_strcmp(name
,nickextnames
[i
]->content
)) {
305 void releasenickext(int index
) {
309 freesstring(nickextnames
[index
]);
310 nickextnames
[index
]=NULL
;
312 for (i
=0;i
<NICKHASHSIZE
;i
++) {
313 for (np
=nicktable
[i
];np
;np
=np
->next
) {
314 np
->exts
[index
]=NULL
;
320 * Produces the "apparent" hostmask as seen by network users.
323 char *visiblehostmask(nick
*np
, char *buf
) {
324 char uhbuf
[USERLEN
+HOSTLEN
+2];
326 visibleuserhost(np
, uhbuf
);
327 sprintf(buf
,"%s!%s",np
->nick
,uhbuf
);
333 * As above without nick
336 char *visibleuserhost(nick
*np
, char *buf
) {
337 char hostbuf
[HOSTLEN
+1];
341 host
=np
->host
->name
->content
;
345 ident
=np
->shident
->content
;
348 host
=np
->sethost
->content
;
350 } else if (IsAccount(np
) && IsHideHost(np
)) {
351 sprintf(hostbuf
,"%s.%s", np
->authname
, HIS_HIDDENHOST
);
355 sprintf(buf
,"%s@%s",ident
,host
);
363 * gethandlebynumeric:
364 * Given a numeric, gives the location in the servernicks table
365 * where it should be. Does not check that the nick currently found
366 * there (if any) has the correct numeric; this is left to the
367 * calling function to figure out.
370 nick
**gethandlebynumeric(long numeric
) {
375 /* Shift off the client identifier part of the numeric to get the server ID */
376 servernum
=(numeric
>>18);
378 if ((serv
=getserverdata(servernum
))==NULL
) {
379 Error("nick",ERR_WARNING
,"Numeric %ld refers to non-existent server %d",numeric
,servernum
);
383 /* Compute the masked numeric */
384 maskednum
=numeric
&(serv
->maxusernum
);
386 return (servernicks
[servernum
])+maskednum
;
390 * getnickbynumeric[str]()
391 * These functions retrieve a nick based on it's numeric on the network
392 * Use the approriate function depending on how your numeric is expressed..
395 nick
*getnickbynumeric(long numeric
) {
398 nh
=gethandlebynumeric(numeric
);
400 if ((*nh
) && ((*nh
)->numeric
!=numeric
)) {
401 /* We found a masked numeric match, but the actual numeric
402 * is different. This counts as a miss. */
409 nick
*getnickbynumericstr(char *numericstr
) {
410 return getnickbynumeric(numerictolong(numericstr
,5));