]>
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
);
57 char *NULLAUTHNAME
= "";
63 /* Clear up the nicks in authext */
64 for (i
=0;i
<AUTHNAMEHASHSIZE
;i
++)
65 for (anp
=authnametable
[i
];anp
;anp
=anp
->next
)
70 memset(nicktable
,0,sizeof(nicktable
));
71 memset(servernicks
,0,sizeof(servernicks
));
73 /* If we're connected to IRC, force a disconnect. This needs to be done
74 * before we register all our hooks which would otherwise get called
75 * during the disconnect. */
77 irc_send("%s SQ %s 0 :Resync [adding nick support]",mynumeric
->content
,myserver
->content
); irc_disconnected();
80 /* Register our hooks */
81 registerhook(HOOK_SERVER_NEWSERVER
,&handleserverchange
);
82 registerhook(HOOK_SERVER_LOSTSERVER
,&handleserverchange
);
83 registerhook(HOOK_CORE_STATSREQUEST
,&nickstats
);
85 /* And our server handlers */
86 registerserverhandler("N",&handlenickmsg
,10);
87 registerserverhandler("D",&handlekillmsg
,2);
88 registerserverhandler("Q",&handlequitmsg
,1);
89 registerserverhandler("M",&handleusermodemsg
,3);
90 registerserverhandler("W",&handlewhoismsg
,2);
91 registerserverhandler("AC",&handleaccountmsg
,4);
92 registerserverhandler("R",&handlestatsmsg
,2);
93 registerserverhandler("P",&handleprivmsg
,2);
95 /* Fake the addition of our own server */
96 handleserverchange(HOOK_SERVER_NEWSERVER
,(void *)numerictolong(mynumeric
->content
,2));
105 for (i
=0;i
<NICKHASHSIZE
;i
++) {
106 for (np
=nicktable
[i
];np
;np
=np
->next
) {
107 freesstring(np
->shident
);
108 freesstring(np
->sethost
);
109 freesstring(np
->opername
);
110 if(!np
->auth
&& np
->authname
&& (np
->authname
!= NULLAUTHNAME
))
115 nsfreeall(POOL_NICK
);
118 deregisterhook(HOOK_SERVER_NEWSERVER
,&handleserverchange
);
119 deregisterhook(HOOK_SERVER_LOSTSERVER
,&handleserverchange
);
120 deregisterhook(HOOK_CORE_STATSREQUEST
,&nickstats
);
122 /* And our server handlers */
123 deregisterserverhandler("N",&handlenickmsg
);
124 deregisterserverhandler("D",&handlekillmsg
);
125 deregisterserverhandler("Q",&handlequitmsg
);
126 deregisterserverhandler("M",&handleusermodemsg
);
127 deregisterserverhandler("W",&handlewhoismsg
);
128 deregisterserverhandler("AC",&handleaccountmsg
);
129 deregisterserverhandler("R",&handlestatsmsg
);
130 deregisterserverhandler("P",&handleprivmsg
);
134 * This function handles servers appearing and disappearing.
135 * For a new server, the client table is allocated.
136 * For a disappearing server, all it's clients are killed and the client table is freed.
139 void handleserverchange(int hooknum
, void *arg
) {
146 case HOOK_SERVER_NEWSERVER
:
147 servernicks
[servernum
]=(nick
**)nsmalloc(POOL_NICK
,(serverlist
[servernum
].maxusernum
+1)*sizeof(nick
**));
148 memset(servernicks
[servernum
],0,(serverlist
[servernum
].maxusernum
+1)*sizeof(nick
**));
151 case HOOK_SERVER_LOSTSERVER
:
152 for (i
=0;i
<=serverlist
[servernum
].maxusernum
;i
++) {
153 if (servernicks
[servernum
][i
]!=NULL
) {
154 deletenick(servernicks
[servernum
][i
]);
157 nsfree(POOL_NICK
,servernicks
[servernum
]);
165 * This function handles the removal of a nick from the network
168 void deletenick(nick
*np
) {
171 /* Fire the hook. This will deal with removal from channels etc. */
172 triggerhook(HOOK_NICK_LOSTNICK
, np
);
174 /* Release the realname and hostname parts */
176 for (nh
=&(np
->realname
->nicks
);*nh
;nh
=&((*nh
)->nextbyrealname
)) {
178 *nh
=np
->nextbyrealname
;
183 for (nh
=&(np
->host
->nicks
);*nh
;nh
=&((*nh
)->nextbyhost
)) {
190 releaserealname(np
->realname
);
191 releasehost(np
->host
);
195 if(np
->authname
&& (np
->authname
!= NULLAUTHNAME
))
198 np
->auth
->usercount
--;
200 for (nh
=&(np
->auth
->nicks
);*nh
;nh
=&((*nh
)->nextbyauthname
)) {
202 *nh
=np
->nextbyauthname
;
207 releaseauthname(np
->auth
);
211 freesstring(np
->shident
); /* freesstring(NULL) is OK */
212 freesstring(np
->sethost
);
213 freesstring(np
->opername
);
215 node_decrement_usercount(np
->ipnode
);
216 derefnode(iptree
, np
->ipnode
);
218 /* TODO: figure out how to cleanly remove nodes without affecting other modules */
220 /* Delete the nick from the servernick table */
221 *(gethandlebynumericunsafe(np
->numeric
))=NULL
;
223 /* Remove the nick from the hash table */
224 removenickfromhash(np
);
229 void addnicktohash(nick
*np
) {
230 np
->next
=nicktable
[nickhash(np
->nick
)];
231 nicktable
[nickhash(np
->nick
)]=np
;
234 void removenickfromhash(nick
*np
) {
237 for (nh
=&(nicktable
[nickhash(np
->nick
)]);*nh
;nh
=&((*nh
)->next
)) {
245 nick
*getnickbynick(const char *name
) {
248 for (np
=nicktable
[nickhash(name
)];np
;np
=np
->next
) {
249 if (!ircd_strcmp(np
->nick
,name
))
256 void nickstats(int hooknum
, void *arg
) {
257 int total
,maxchain
,curchain
,i
,buckets
;
262 buckets
=total
=maxchain
=curchain
=0;
263 for (i
=0;i
<NICKHASHSIZE
;i
++,curchain
=0) {
267 for (;np
;np
=np
->next
) {
272 if (curchain
>maxchain
) {
279 sprintf(buf
,"Nick : %6d nicks (HASH: %6d/%6d, chain %3d)",total
,buckets
,NICKHASHSIZE
,maxchain
);
280 } else if ((long)arg
>2) {
281 sprintf(buf
,"Nick : %6d users on network.",total
);
285 triggerhook(HOOK_CORE_STATSREPLY
,buf
);
289 int registernickext(const char *name
) {
292 if (findnickext(name
)!=-1) {
293 Error("nick",ERR_WARNING
,"Tried to register duplicate nick extension %s",name
);
297 for (i
=0;i
<MAXNICKEXTS
;i
++) {
298 if (nickextnames
[i
]==NULL
) {
299 nickextnames
[i
]=getsstring(name
,100);
304 Error("nick",ERR_WARNING
,"Tried to register too many nick extensions: %s",name
);
308 int findnickext(const char *name
) {
311 for (i
=0;i
<MAXNICKEXTS
;i
++) {
312 if (nickextnames
[i
]!=NULL
&& !ircd_strcmp(name
,nickextnames
[i
]->content
)) {
320 void releasenickext(int index
) {
324 freesstring(nickextnames
[index
]);
325 nickextnames
[index
]=NULL
;
327 for (i
=0;i
<NICKHASHSIZE
;i
++) {
328 for (np
=nicktable
[i
];np
;np
=np
->next
) {
329 np
->exts
[index
]=NULL
;
335 * Produces the "apparent" hostmask as seen by network users.
338 char *visiblehostmask(nick
*np
, char *buf
) {
339 char uhbuf
[USERLEN
+HOSTLEN
+2];
341 visibleuserhost(np
, uhbuf
);
342 sprintf(buf
,"%s!%s",np
->nick
,uhbuf
);
348 * As above without nick
351 char *visibleuserhost(nick
*np
, char *buf
) {
352 char hostbuf
[HOSTLEN
+1];
356 host
=np
->host
->name
->content
;
360 ident
=np
->shident
->content
;
363 host
=np
->sethost
->content
;
365 } else if (IsAccount(np
) && IsHideHost(np
)) {
366 sprintf(hostbuf
,"%s.%s", np
->authname
, HIS_HIDDENHOST
);
370 sprintf(buf
,"%s@%s",ident
,host
);
378 * gethandlebynumeric:
379 * Given a numeric, gives the location in the servernicks table
380 * where it should be. Does not check that the nick currently found
381 * there (if any) has the correct numeric; this is left to the
382 * calling function to figure out.
385 nick
**gethandlebynumeric(long numeric
) {
390 /* Shift off the client identifier part of the numeric to get the server ID */
391 servernum
=(numeric
>>18);
393 if ((serv
=getserverdata(servernum
))==NULL
) {
394 Error("nick",ERR_WARNING
,"Numeric %ld refers to non-existent server %d",numeric
,servernum
);
398 /* Compute the masked numeric */
399 maskednum
=numeric
&(serv
->maxusernum
);
401 return (servernicks
[servernum
])+maskednum
;
405 * getnickbynumeric[str]()
406 * These functions retrieve a nick based on it's numeric on the network
407 * Use the approriate function depending on how your numeric is expressed..
410 nick
*getnickbynumeric(long numeric
) {
413 nh
=gethandlebynumeric(numeric
);
415 if ((*nh
) && ((*nh
)->numeric
!=numeric
)) {
416 /* We found a masked numeric match, but the actual numeric
417 * is different. This counts as a miss. */
424 nick
*getnickbynumericstr(char *numericstr
) {
425 return getnickbynumeric(numerictolong(numericstr
,5));