]>
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 { 'q', UMODE_COMCHANS
},
42 { 'Q', UMODE_COMCHANSRESTR
},
43 { 'C', UMODE_CLOAKED
},
46 const flag accountflags
[] = {
48 { 'h', AFLAG_SUPPORT
},
51 { 'd', AFLAG_DEVELOPER
},
54 #define nickhash(x) ((crc32i(x))%NICKHASHSIZE)
56 nick
*nicktable
[NICKHASHSIZE
];
57 nick
**servernicks
[MAXSERVERS
];
59 sstring
*nickextnames
[MAXNICKEXTS
];
61 void nickstats(int hooknum
, void *arg
);
63 char *NULLAUTHNAME
= "";
69 /* Clear up the nicks in authext */
70 for (i
=0;i
<AUTHNAMEHASHSIZE
;i
++)
71 for (anp
=authnametable
[i
];anp
;anp
=anp
->next
)
75 memset(nicktable
,0,sizeof(nicktable
));
76 memset(servernicks
,0,sizeof(servernicks
));
78 /* If we're connected to IRC, force a disconnect. This needs to be done
79 * before we register all our hooks which would otherwise get called
80 * during the disconnect. */
82 irc_send("%s SQ %s 0 :Resync [adding nick support]",mynumeric
->content
,myserver
->content
); irc_disconnected();
85 /* Register our hooks */
86 registerhook(HOOK_SERVER_NEWSERVER
,&handleserverchange
);
87 registerhook(HOOK_SERVER_LOSTSERVER
,&handleserverchange
);
88 registerhook(HOOK_CORE_STATSREQUEST
,&nickstats
);
90 /* And our server handlers */
91 registerserverhandler("N",&handlenickmsg
,10);
92 registerserverhandler("D",&handlekillmsg
,2);
93 registerserverhandler("Q",&handlequitmsg
,1);
94 registerserverhandler("M",&handleusermodemsg
,3);
95 registerserverhandler("AC",&handleaccountmsg
,4);
96 registerserverhandler("P",&handleprivmsg
,2);
97 registerserverhandler("A",&handleawaymsg
,1);
98 registerserverhandler("CA",&handleaddcloak
,1);
99 registerserverhandler("CU",&handleclearcloak
,0);
101 /* Fake the addition of our own server */
102 handleserverchange(HOOK_SERVER_NEWSERVER
,(void *)numerictolong(mynumeric
->content
,2));
111 for (i
=0;i
<NICKHASHSIZE
;i
++) {
112 for (np
=nicktable
[i
];np
;np
=np
->next
) {
113 freesstring(np
->shident
);
114 freesstring(np
->sethost
);
115 freesstring(np
->opername
);
116 if(!np
->auth
&& np
->authname
&& (np
->authname
!= NULLAUTHNAME
))
121 nsfreeall(POOL_NICK
);
124 deregisterhook(HOOK_SERVER_NEWSERVER
,&handleserverchange
);
125 deregisterhook(HOOK_SERVER_LOSTSERVER
,&handleserverchange
);
126 deregisterhook(HOOK_CORE_STATSREQUEST
,&nickstats
);
128 /* And our server handlers */
129 deregisterserverhandler("N",&handlenickmsg
);
130 deregisterserverhandler("D",&handlekillmsg
);
131 deregisterserverhandler("Q",&handlequitmsg
);
132 deregisterserverhandler("M",&handleusermodemsg
);
133 deregisterserverhandler("AC",&handleaccountmsg
);
134 deregisterserverhandler("P",&handleprivmsg
);
135 deregisterserverhandler("A",&handleawaymsg
);
136 deregisterserverhandler("CA",&handleaddcloak
);
137 deregisterserverhandler("CU",&handleclearcloak
);
141 * This function handles servers appearing and disappearing.
142 * For a new server, the client table is allocated.
143 * For a disappearing server, all it's clients are killed and the client table is freed.
146 void handleserverchange(int hooknum
, void *arg
) {
153 case HOOK_SERVER_NEWSERVER
:
154 servernicks
[servernum
]=(nick
**)nsmalloc(POOL_NICK
,(serverlist
[servernum
].maxusernum
+1)*sizeof(nick
**));
155 memset(servernicks
[servernum
],0,(serverlist
[servernum
].maxusernum
+1)*sizeof(nick
**));
158 case HOOK_SERVER_LOSTSERVER
:
159 for (i
=0;i
<=serverlist
[servernum
].maxusernum
;i
++) {
160 if (servernicks
[servernum
][i
]!=NULL
) {
161 deletenick(servernicks
[servernum
][i
]);
164 nsfree(POOL_NICK
,servernicks
[servernum
]);
172 * This function handles the removal of a nick from the network
175 void deletenick(nick
*np
) {
178 /* Fire the hook. This will deal with removal from channels etc. */
179 triggerhook(HOOK_NICK_LOSTNICK
, np
);
181 /* Release the realname and hostname parts */
183 for (nh
=&(np
->realname
->nicks
);*nh
;nh
=&((*nh
)->nextbyrealname
)) {
185 *nh
=np
->nextbyrealname
;
190 for (nh
=&(np
->host
->nicks
);*nh
;nh
=&((*nh
)->nextbyhost
)) {
197 releaserealname(np
->realname
);
198 releasehost(np
->host
);
202 if(np
->authname
&& (np
->authname
!= NULLAUTHNAME
))
205 np
->auth
->usercount
--;
207 for (nh
=&(np
->auth
->nicks
);*nh
;nh
=&((*nh
)->nextbyauthname
)) {
209 *nh
=np
->nextbyauthname
;
214 releaseauthname(np
->auth
);
218 freesstring(np
->shident
); /* freesstring(NULL) is OK */
219 freesstring(np
->sethost
);
220 freesstring(np
->opername
);
221 freesstring(np
->message
);
223 node_decrement_usercount(np
->ipnode
);
224 derefnode(iptree
, np
->ipnode
);
226 /* TODO: figure out how to cleanly remove nodes without affecting other modules */
228 /* Remove cloak entries for the user */
229 removecloaktarget(np
);
230 clearcloaktargets(np
);
232 /* Delete the nick from the servernick table */
233 *(gethandlebynumericunsafe(np
->numeric
))=NULL
;
235 /* Remove the nick from the hash table */
236 removenickfromhash(np
);
241 void addnicktohash(nick
*np
) {
242 np
->next
=nicktable
[nickhash(np
->nick
)];
243 nicktable
[nickhash(np
->nick
)]=np
;
246 void removenickfromhash(nick
*np
) {
249 for (nh
=&(nicktable
[nickhash(np
->nick
)]);*nh
;nh
=&((*nh
)->next
)) {
257 nick
*getnickbynick(const char *name
) {
260 for (np
=nicktable
[nickhash(name
)];np
;np
=np
->next
) {
261 if (!ircd_strcmp(np
->nick
,name
))
268 void nickstats(int hooknum
, void *arg
) {
269 int total
,maxchain
,curchain
,i
,buckets
;
274 buckets
=total
=maxchain
=curchain
=0;
275 for (i
=0;i
<NICKHASHSIZE
;i
++,curchain
=0) {
279 for (;np
;np
=np
->next
) {
284 if (curchain
>maxchain
) {
291 sprintf(buf
,"Nick : %6d nicks (HASH: %6d/%6d, chain %3d)",total
,buckets
,NICKHASHSIZE
,maxchain
);
292 } else if ((long)arg
>2) {
293 sprintf(buf
,"Nick : %6d users on network.",total
);
297 triggerhook(HOOK_CORE_STATSREPLY
,buf
);
301 int registernickext(const char *name
) {
304 if (findnickext(name
)!=-1) {
305 Error("nick",ERR_WARNING
,"Tried to register duplicate nick extension %s",name
);
309 for (i
=0;i
<MAXNICKEXTS
;i
++) {
310 if (nickextnames
[i
]==NULL
) {
311 nickextnames
[i
]=getsstring(name
,100);
316 Error("nick",ERR_WARNING
,"Tried to register too many nick extensions: %s",name
);
320 int findnickext(const char *name
) {
323 for (i
=0;i
<MAXNICKEXTS
;i
++) {
324 if (nickextnames
[i
]!=NULL
&& !ircd_strcmp(name
,nickextnames
[i
]->content
)) {
332 void releasenickext(int index
) {
336 freesstring(nickextnames
[index
]);
337 nickextnames
[index
]=NULL
;
339 for (i
=0;i
<NICKHASHSIZE
;i
++) {
340 for (np
=nicktable
[i
];np
;np
=np
->next
) {
341 np
->exts
[index
]=NULL
;
347 * Produces the "apparent" hostmask as seen by network users.
350 char *visiblehostmask(nick
*np
, char *buf
) {
351 char uhbuf
[USERLEN
+HOSTLEN
+2];
353 visibleuserhost(np
, uhbuf
);
354 sprintf(buf
,"%s!%s",np
->nick
,uhbuf
);
360 * As above without nick
363 char *visibleuserhost(nick
*np
, char *buf
) {
364 char hostbuf
[HOSTLEN
+1];
368 host
=np
->host
->name
->content
;
372 ident
=np
->shident
->content
;
375 host
=np
->sethost
->content
;
377 } else if (IsAccount(np
) && IsHideHost(np
)) {
378 sprintf(hostbuf
,"%s.%s", np
->authname
, HIS_HIDDENHOST
);
382 sprintf(buf
,"%s@%s",ident
,host
);
390 * gethandlebynumeric:
391 * Given a numeric, gives the location in the servernicks table
392 * where it should be. Does not check that the nick currently found
393 * there (if any) has the correct numeric; this is left to the
394 * calling function to figure out.
397 nick
**gethandlebynumeric(long numeric
) {
402 /* Shift off the client identifier part of the numeric to get the server ID */
403 servernum
=(numeric
>>18);
405 if ((serv
=getserverdata(servernum
))==NULL
) {
406 Error("nick",ERR_WARNING
,"Numeric %ld refers to non-existent server %d",numeric
,servernum
);
410 /* Compute the masked numeric */
411 maskednum
=numeric
&(serv
->maxusernum
);
413 return (servernicks
[servernum
])+maskednum
;
417 * getnickbynumeric[str]()
418 * These functions retrieve a nick based on it's numeric on the network
419 * Use the approriate function depending on how your numeric is expressed..
422 nick
*getnickbynumeric(long numeric
) {
425 nh
=gethandlebynumeric(numeric
);
427 if ((*nh
) && ((*nh
)->numeric
!=numeric
)) {
428 /* We found a masked numeric match, but the actual numeric
429 * is different. This counts as a miss. */
436 nick
*getnickbynumericstr(char *numericstr
) {
437 return getnickbynumeric(numerictolong(numericstr
,5));
443 int canseeuser(nick
*np
, nick
*cloaked
)
445 return (np
== cloaked
||
446 !IsCloaked(cloaked
) ||
447 np
->cloak_extra
== cloaked
);
450 void addcloaktarget(nick
*cloaked
, nick
*target
)
452 removecloaktarget(target
);
454 target
->cloak_extra
= cloaked
;
455 cloaked
->cloak_count
++;
458 void removecloaktarget(nick
*target
)
460 if (target
->cloak_extra
) {
461 target
->cloak_extra
->cloak_count
--;
462 target
->cloak_extra
= NULL
;
466 void clearcloaktargets(nick
*cloaked
)
471 if (cloaked
->cloak_count
== 0)
474 for(j
=0;j
<NICKHASHSIZE
;j
++)
475 for(tnp
=nicktable
[j
];tnp
;tnp
=tnp
->next
)
476 if (tnp
->cloak_extra
== cloaked
)
477 tnp
->cloak_extra
= NULL
;
479 cloaked
->cloak_count
= 0;