]>
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 "../lib/ccassert.h"
16 #include "../core/nsmalloc.h"
24 CCASSERT(sizeof(host
) == sizeof(realname
));
26 const flag umodeflags
[] = {
28 { 'w', UMODE_WALLOPS
},
31 { 'k', UMODE_SERVICE
},
34 { 'r', UMODE_ACCOUNT
},
35 { 'n', UMODE_HIDECHAN
},
36 { 'x', UMODE_HIDEHOST
},
37 { 'h', UMODE_SETHOST
},
38 { 'R', UMODE_REGPRIV
},
39 { 'I', UMODE_HIDEIDLE
},
40 { 'P', UMODE_PARANOID
},
41 { 'C', UMODE_CLOAKED
},
44 const flag accountflags
[] = {
46 { 'h', AFLAG_SUPPORT
},
49 { 'd', AFLAG_DEVELOPER
},
52 #define nickhash(x) ((crc32i(x))%NICKHASHSIZE)
54 nick
*nicktable
[NICKHASHSIZE
];
55 nick
**servernicks
[MAXSERVERS
];
57 sstring
*nickextnames
[MAXNICKEXTS
];
59 void nickstats(int hooknum
, void *arg
);
61 char *NULLAUTHNAME
= "";
67 /* Clear up the nicks in authext */
68 for (i
=0;i
<AUTHNAMEHASHSIZE
;i
++)
69 for (anp
=authnametable
[i
];anp
;anp
=anp
->next
)
73 memset(nicktable
,0,sizeof(nicktable
));
74 memset(servernicks
,0,sizeof(servernicks
));
76 /* If we're connected to IRC, force a disconnect. This needs to be done
77 * before we register all our hooks which would otherwise get called
78 * during the disconnect. */
80 irc_send("%s SQ %s 0 :Resync [adding nick support]",mynumeric
->content
,myserver
->content
); irc_disconnected();
83 /* Register our hooks */
84 registerhook(HOOK_SERVER_NEWSERVER
,&handleserverchange
);
85 registerhook(HOOK_SERVER_LOSTSERVER
,&handleserverchange
);
86 registerhook(HOOK_CORE_STATSREQUEST
,&nickstats
);
88 /* And our server handlers */
89 registerserverhandler("N",&handlenickmsg
,10);
90 registerserverhandler("D",&handlekillmsg
,2);
91 registerserverhandler("Q",&handlequitmsg
,1);
92 registerserverhandler("M",&handleusermodemsg
,3);
93 registerserverhandler("AC",&handleaccountmsg
,4);
94 registerserverhandler("P",&handleprivmsg
,2);
95 registerserverhandler("A",&handleawaymsg
,1);
96 registerserverhandler("CA",&handleaddcloak
,1);
97 registerserverhandler("CU",&handleclearcloak
,0);
99 /* Fake the addition of our own server */
100 handleserverchange(HOOK_SERVER_NEWSERVER
,(void *)numerictolong(mynumeric
->content
,2));
109 for (i
=0;i
<NICKHASHSIZE
;i
++) {
110 for (np
=nicktable
[i
];np
;np
=np
->next
) {
111 freesstring(np
->shident
);
112 freesstring(np
->sethost
);
113 freesstring(np
->opername
);
114 if(!np
->auth
&& np
->authname
&& (np
->authname
!= NULLAUTHNAME
))
119 nsfreeall(POOL_NICK
);
122 deregisterhook(HOOK_SERVER_NEWSERVER
,&handleserverchange
);
123 deregisterhook(HOOK_SERVER_LOSTSERVER
,&handleserverchange
);
124 deregisterhook(HOOK_CORE_STATSREQUEST
,&nickstats
);
126 /* And our server handlers */
127 deregisterserverhandler("N",&handlenickmsg
);
128 deregisterserverhandler("D",&handlekillmsg
);
129 deregisterserverhandler("Q",&handlequitmsg
);
130 deregisterserverhandler("M",&handleusermodemsg
);
131 deregisterserverhandler("AC",&handleaccountmsg
);
132 deregisterserverhandler("P",&handleprivmsg
);
133 deregisterserverhandler("A",&handleawaymsg
);
134 deregisterserverhandler("CA",&handleaddcloak
);
135 deregisterserverhandler("CU",&handleclearcloak
);
139 * This function handles servers appearing and disappearing.
140 * For a new server, the client table is allocated.
141 * For a disappearing server, all it's clients are killed and the client table is freed.
144 void handleserverchange(int hooknum
, void *arg
) {
151 case HOOK_SERVER_NEWSERVER
:
152 servernicks
[servernum
]=(nick
**)nsmalloc(POOL_NICK
,(serverlist
[servernum
].maxusernum
+1)*sizeof(nick
**));
153 memset(servernicks
[servernum
],0,(serverlist
[servernum
].maxusernum
+1)*sizeof(nick
**));
156 case HOOK_SERVER_LOSTSERVER
:
157 for (i
=0;i
<=serverlist
[servernum
].maxusernum
;i
++) {
158 if (servernicks
[servernum
][i
]!=NULL
) {
159 deletenick(servernicks
[servernum
][i
]);
162 nsfree(POOL_NICK
,servernicks
[servernum
]);
170 * This function handles the removal of a nick from the network
173 void deletenick(nick
*np
) {
176 /* Fire the hook. This will deal with removal from channels etc. */
177 triggerhook(HOOK_NICK_LOSTNICK
, np
);
179 /* Release the realname and hostname parts */
181 for (nh
=&(np
->realname
->nicks
);*nh
;nh
=&((*nh
)->nextbyrealname
)) {
183 *nh
=np
->nextbyrealname
;
188 for (nh
=&(np
->host
->nicks
);*nh
;nh
=&((*nh
)->nextbyhost
)) {
195 releaserealname(np
->realname
);
196 releasehost(np
->host
);
200 if(np
->authname
&& (np
->authname
!= NULLAUTHNAME
))
203 np
->auth
->usercount
--;
205 for (nh
=&(np
->auth
->nicks
);*nh
;nh
=&((*nh
)->nextbyauthname
)) {
207 *nh
=np
->nextbyauthname
;
212 releaseauthname(np
->auth
);
216 freesstring(np
->shident
); /* freesstring(NULL) is OK */
217 freesstring(np
->sethost
);
218 freesstring(np
->opername
);
220 node_decrement_usercount(np
->ipnode
);
221 derefnode(iptree
, np
->ipnode
);
223 /* TODO: figure out how to cleanly remove nodes without affecting other modules */
225 /* Remove cloak entries for the user */
226 removecloaktarget(np
);
227 clearcloaktargets(np
);
229 /* Delete the nick from the servernick table */
230 *(gethandlebynumericunsafe(np
->numeric
))=NULL
;
232 /* Remove the nick from the hash table */
233 removenickfromhash(np
);
238 void addnicktohash(nick
*np
) {
239 np
->next
=nicktable
[nickhash(np
->nick
)];
240 nicktable
[nickhash(np
->nick
)]=np
;
243 void removenickfromhash(nick
*np
) {
246 for (nh
=&(nicktable
[nickhash(np
->nick
)]);*nh
;nh
=&((*nh
)->next
)) {
254 nick
*getnickbynick(const char *name
) {
257 for (np
=nicktable
[nickhash(name
)];np
;np
=np
->next
) {
258 if (!ircd_strcmp(np
->nick
,name
))
265 void nickstats(int hooknum
, void *arg
) {
266 int total
,maxchain
,curchain
,i
,buckets
;
271 buckets
=total
=maxchain
=curchain
=0;
272 for (i
=0;i
<NICKHASHSIZE
;i
++,curchain
=0) {
276 for (;np
;np
=np
->next
) {
281 if (curchain
>maxchain
) {
288 sprintf(buf
,"Nick : %6d nicks (HASH: %6d/%6d, chain %3d)",total
,buckets
,NICKHASHSIZE
,maxchain
);
289 } else if ((long)arg
>2) {
290 sprintf(buf
,"Nick : %6d users on network.",total
);
294 triggerhook(HOOK_CORE_STATSREPLY
,buf
);
298 int registernickext(const char *name
) {
301 if (findnickext(name
)!=-1) {
302 Error("nick",ERR_WARNING
,"Tried to register duplicate nick extension %s",name
);
306 for (i
=0;i
<MAXNICKEXTS
;i
++) {
307 if (nickextnames
[i
]==NULL
) {
308 nickextnames
[i
]=getsstring(name
,100);
313 Error("nick",ERR_WARNING
,"Tried to register too many nick extensions: %s",name
);
317 int findnickext(const char *name
) {
320 for (i
=0;i
<MAXNICKEXTS
;i
++) {
321 if (nickextnames
[i
]!=NULL
&& !ircd_strcmp(name
,nickextnames
[i
]->content
)) {
329 void releasenickext(int index
) {
333 freesstring(nickextnames
[index
]);
334 nickextnames
[index
]=NULL
;
336 for (i
=0;i
<NICKHASHSIZE
;i
++) {
337 for (np
=nicktable
[i
];np
;np
=np
->next
) {
338 np
->exts
[index
]=NULL
;
344 * Produces the "apparent" hostmask as seen by network users.
347 char *visiblehostmask(nick
*np
, char *buf
) {
348 char uhbuf
[USERLEN
+HOSTLEN
+2];
350 visibleuserhost(np
, uhbuf
);
351 sprintf(buf
,"%s!%s",np
->nick
,uhbuf
);
357 * As above without nick
360 char *visibleuserhost(nick
*np
, char *buf
) {
361 char hostbuf
[HOSTLEN
+1];
365 host
=np
->host
->name
->content
;
369 ident
=np
->shident
->content
;
372 host
=np
->sethost
->content
;
374 } else if (IsAccount(np
) && IsHideHost(np
)) {
375 sprintf(hostbuf
,"%s.%s", np
->authname
, HIS_HIDDENHOST
);
379 sprintf(buf
,"%s@%s",ident
,host
);
387 * gethandlebynumeric:
388 * Given a numeric, gives the location in the servernicks table
389 * where it should be. Does not check that the nick currently found
390 * there (if any) has the correct numeric; this is left to the
391 * calling function to figure out.
394 nick
**gethandlebynumeric(long numeric
) {
399 /* Shift off the client identifier part of the numeric to get the server ID */
400 servernum
=(numeric
>>18);
402 if ((serv
=getserverdata(servernum
))==NULL
) {
403 Error("nick",ERR_WARNING
,"Numeric %ld refers to non-existent server %d",numeric
,servernum
);
407 /* Compute the masked numeric */
408 maskednum
=numeric
&(serv
->maxusernum
);
410 return (servernicks
[servernum
])+maskednum
;
414 * getnickbynumeric[str]()
415 * These functions retrieve a nick based on it's numeric on the network
416 * Use the approriate function depending on how your numeric is expressed..
419 nick
*getnickbynumeric(long numeric
) {
422 nh
=gethandlebynumeric(numeric
);
424 if ((*nh
) && ((*nh
)->numeric
!=numeric
)) {
425 /* We found a masked numeric match, but the actual numeric
426 * is different. This counts as a miss. */
433 nick
*getnickbynumericstr(char *numericstr
) {
434 return getnickbynumeric(numerictolong(numericstr
,5));
440 int canseeuser(nick
*np
, nick
*cloaked
)
442 return (np
== cloaked
||
443 !IsCloaked(cloaked
) ||
444 np
->cloak_extra
== cloaked
);
447 void addcloaktarget(nick
*cloaked
, nick
*target
)
449 removecloaktarget(target
);
451 target
->cloak_extra
= cloaked
;
452 cloaked
->cloak_count
++;
455 void removecloaktarget(nick
*target
)
457 if (target
->cloak_extra
) {
458 target
->cloak_extra
->cloak_count
--;
459 target
->cloak_extra
= NULL
;
463 void clearcloaktargets(nick
*cloaked
)
468 if (cloaked
->cloak_count
== 0)
471 for(j
=0;j
<NICKHASHSIZE
;j
++)
472 for(tnp
=nicktable
[j
];tnp
;tnp
=tnp
->next
)
473 if (tnp
->cloak_extra
== cloaked
)
474 tnp
->cloak_extra
= NULL
;
476 cloaked
->cloak_count
= 0;