]>
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 /* If we're connected to IRC, force a disconnect. This needs to be done
69 * before we register all our hooks which would otherwise get called
70 * during the disconnect. */
72 irc_send("%s SQ %s 0 :Resync [adding nick support]",mynumeric
->content
,myserver
->content
); irc_disconnected();
75 /* Register our hooks */
76 registerhook(HOOK_SERVER_NEWSERVER
,&handleserverchange
);
77 registerhook(HOOK_SERVER_LOSTSERVER
,&handleserverchange
);
78 registerhook(HOOK_CORE_STATSREQUEST
,&nickstats
);
80 /* And our server handlers */
81 registerserverhandler("N",&handlenickmsg
,10);
82 registerserverhandler("D",&handlekillmsg
,2);
83 registerserverhandler("Q",&handlequitmsg
,1);
84 registerserverhandler("M",&handleusermodemsg
,3);
85 registerserverhandler("W",&handlewhoismsg
,2);
86 registerserverhandler("AC",&handleaccountmsg
,4);
87 registerserverhandler("R",&handlestatsmsg
,2);
88 registerserverhandler("P",&handleprivmsg
,2);
90 /* Fake the addition of our own server */
91 handleserverchange(HOOK_SERVER_NEWSERVER
,(void *)numerictolong(mynumeric
->content
,2));
100 for (i
=0;i
<NICKHASHSIZE
;i
++) {
101 for (np
=nicktable
[i
];np
;np
=np
->next
) {
102 freesstring(np
->shident
);
103 freesstring(np
->sethost
);
104 freesstring(np
->opername
);
108 nsfreeall(POOL_NICK
);
111 deregisterhook(HOOK_SERVER_NEWSERVER
,&handleserverchange
);
112 deregisterhook(HOOK_SERVER_LOSTSERVER
,&handleserverchange
);
113 deregisterhook(HOOK_CORE_STATSREQUEST
,&nickstats
);
115 /* And our server handlers */
116 deregisterserverhandler("N",&handlenickmsg
);
117 deregisterserverhandler("D",&handlekillmsg
);
118 deregisterserverhandler("Q",&handlequitmsg
);
119 deregisterserverhandler("M",&handleusermodemsg
);
120 deregisterserverhandler("W",&handlewhoismsg
);
121 deregisterserverhandler("AC",&handleaccountmsg
);
122 deregisterserverhandler("R",&handlestatsmsg
);
123 deregisterserverhandler("P",&handleprivmsg
);
127 * This function handles servers appearing and disappearing.
128 * For a new server, the client table is allocated.
129 * For a disappearing server, all it's clients are killed and the client table is freed.
132 void handleserverchange(int hooknum
, void *arg
) {
139 case HOOK_SERVER_NEWSERVER
:
140 servernicks
[servernum
]=(nick
**)nsmalloc(POOL_NICK
,(serverlist
[servernum
].maxusernum
+1)*sizeof(nick
**));
141 memset(servernicks
[servernum
],0,(serverlist
[servernum
].maxusernum
+1)*sizeof(nick
**));
144 case HOOK_SERVER_LOSTSERVER
:
145 for (i
=0;i
<=serverlist
[servernum
].maxusernum
;i
++) {
146 if (servernicks
[servernum
][i
]!=NULL
) {
147 deletenick(servernicks
[servernum
][i
]);
150 nsfree(POOL_NICK
,servernicks
[servernum
]);
158 * This function handles the removal of a nick from the network
161 void deletenick(nick
*np
) {
164 /* Fire the hook. This will deal with removal from channels etc. */
165 triggerhook(HOOK_NICK_LOSTNICK
, np
);
167 /* Release the realname and hostname parts */
169 for (nh
=&(np
->realname
->nicks
);*nh
;nh
=&((*nh
)->nextbyrealname
)) {
171 *nh
=np
->nextbyrealname
;
176 for (nh
=&(np
->host
->nicks
);*nh
;nh
=&((*nh
)->nextbyhost
)) {
183 releaserealname(np
->realname
);
184 releasehost(np
->host
);
186 if(IsAccount(np
) && np
->auth
)
188 np
->auth
->usercount
--;
190 for (nh
=&(np
->auth
->nicks
);*nh
;nh
=&((*nh
)->nextbyauthname
)) {
192 *nh
=np
->nextbyauthname
;
197 releaseauthname(np
->auth
);
200 freesstring(np
->shident
); /* freesstring(NULL) is OK */
201 freesstring(np
->sethost
);
202 freesstring(np
->opername
);
204 node_decrement_usercount(np
->ipnode
);
205 derefnode(iptree
, np
->ipnode
);
207 /* TODO: figure out how to cleanly remove nodes without affecting other modules */
209 /* Delete the nick from the servernick table */
210 *(gethandlebynumericunsafe(np
->numeric
))=NULL
;
212 /* Remove the nick from the hash table */
213 removenickfromhash(np
);
218 void addnicktohash(nick
*np
) {
219 np
->next
=nicktable
[nickhash(np
->nick
)];
220 nicktable
[nickhash(np
->nick
)]=np
;
223 void removenickfromhash(nick
*np
) {
226 for (nh
=&(nicktable
[nickhash(np
->nick
)]);*nh
;nh
=&((*nh
)->next
)) {
234 nick
*getnickbynick(const char *name
) {
237 for (np
=nicktable
[nickhash(name
)];np
;np
=np
->next
) {
238 if (!ircd_strcmp(np
->nick
,name
))
245 void nickstats(int hooknum
, void *arg
) {
246 int total
,maxchain
,curchain
,i
,buckets
;
251 buckets
=total
=maxchain
=curchain
=0;
252 for (i
=0;i
<NICKHASHSIZE
;i
++,curchain
=0) {
256 for (;np
;np
=np
->next
) {
261 if (curchain
>maxchain
) {
268 sprintf(buf
,"Nick : %6d nicks (HASH: %6d/%6d, chain %3d)",total
,buckets
,NICKHASHSIZE
,maxchain
);
269 } else if ((long)arg
>2) {
270 sprintf(buf
,"Nick : %6d users on network.",total
);
274 triggerhook(HOOK_CORE_STATSREPLY
,buf
);
278 int registernickext(const char *name
) {
281 if (findnickext(name
)!=-1) {
282 Error("nick",ERR_WARNING
,"Tried to register duplicate nick extension %s",name
);
286 for (i
=0;i
<MAXNICKEXTS
;i
++) {
287 if (nickextnames
[i
]==NULL
) {
288 nickextnames
[i
]=getsstring(name
,100);
293 Error("nick",ERR_WARNING
,"Tried to register too many nick extensions: %s",name
);
297 int findnickext(const char *name
) {
300 for (i
=0;i
<MAXNICKEXTS
;i
++) {
301 if (nickextnames
[i
]!=NULL
&& !ircd_strcmp(name
,nickextnames
[i
]->content
)) {
309 void releasenickext(int index
) {
313 freesstring(nickextnames
[index
]);
314 nickextnames
[index
]=NULL
;
316 for (i
=0;i
<NICKHASHSIZE
;i
++) {
317 for (np
=nicktable
[i
];np
;np
=np
->next
) {
318 np
->exts
[index
]=NULL
;
324 * Produces the "apparent" hostmask as seen by network users.
327 char *visiblehostmask(nick
*np
, char *buf
) {
328 char uhbuf
[USERLEN
+HOSTLEN
+2];
330 visibleuserhost(np
, uhbuf
);
331 sprintf(buf
,"%s!%s",np
->nick
,uhbuf
);
337 * As above without nick
340 char *visibleuserhost(nick
*np
, char *buf
) {
341 char hostbuf
[HOSTLEN
+1];
345 host
=np
->host
->name
->content
;
349 ident
=np
->shident
->content
;
352 host
=np
->sethost
->content
;
354 } else if (IsAccount(np
) && IsHideHost(np
)) {
355 sprintf(hostbuf
,"%s.%s", np
->authname
, HIS_HIDDENHOST
);
359 sprintf(buf
,"%s@%s",ident
,host
);
367 * gethandlebynumeric:
368 * Given a numeric, gives the location in the servernicks table
369 * where it should be. Does not check that the nick currently found
370 * there (if any) has the correct numeric; this is left to the
371 * calling function to figure out.
374 nick
**gethandlebynumeric(long numeric
) {
379 /* Shift off the client identifier part of the numeric to get the server ID */
380 servernum
=(numeric
>>18);
382 if ((serv
=getserverdata(servernum
))==NULL
) {
383 Error("nick",ERR_WARNING
,"Numeric %ld refers to non-existent server %d",numeric
,servernum
);
387 /* Compute the masked numeric */
388 maskednum
=numeric
&(serv
->maxusernum
);
390 return (servernicks
[servernum
])+maskednum
;
394 * getnickbynumeric[str]()
395 * These functions retrieve a nick based on it's numeric on the network
396 * Use the approriate function depending on how your numeric is expressed..
399 nick
*getnickbynumeric(long numeric
) {
402 nh
=gethandlebynumeric(numeric
);
404 if ((*nh
) && ((*nh
)->numeric
!=numeric
)) {
405 /* We found a masked numeric match, but the actual numeric
406 * is different. This counts as a miss. */
413 nick
*getnickbynumericstr(char *numericstr
) {
414 return getnickbynumeric(numerictolong(numericstr
,5));