]>
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
];
51 sstring
*nodeextnames
[PATRICIA_MAXSLOTS
];
52 patricia_tree_t
*iptree
;
54 void nickstats(int hooknum
, void *arg
);
60 /* Clear up the nicks in authext */
61 for (i
=0;i
<AUTHNAMEHASHSIZE
;i
++)
62 for (anp
=authnametable
[i
];anp
;anp
=anp
->next
)
67 memset(nicktable
,0,sizeof(nicktable
));
68 memset(servernicks
,0,sizeof(servernicks
));
70 /* Register our hooks */
71 registerhook(HOOK_SERVER_NEWSERVER
,&handleserverchange
);
72 registerhook(HOOK_SERVER_LOSTSERVER
,&handleserverchange
);
73 registerhook(HOOK_CORE_STATSREQUEST
,&nickstats
);
75 /* And our server handlers */
76 registerserverhandler("N",&handlenickmsg
,10);
77 registerserverhandler("D",&handlekillmsg
,2);
78 registerserverhandler("Q",&handlequitmsg
,1);
79 registerserverhandler("M",&handleusermodemsg
,3);
80 registerserverhandler("W",&handlewhoismsg
,2);
81 registerserverhandler("AC",&handleaccountmsg
,4);
82 registerserverhandler("R",&handlestatsmsg
,2);
84 /* Fake the addition of our own server */
85 handleserverchange(HOOK_SERVER_NEWSERVER
,(void *)numerictolong(mynumeric
->content
,2));
87 iptree
= patricia_new_tree(PATRICIA_MAXBITS
);
96 for (i
=0;i
<NICKHASHSIZE
;i
++) {
97 for (np
=nicktable
[i
];np
;np
=np
->next
) {
98 freesstring(np
->shident
);
99 freesstring(np
->sethost
);
103 nsfreeall(POOL_NICK
);
106 deregisterhook(HOOK_SERVER_NEWSERVER
,&handleserverchange
);
107 deregisterhook(HOOK_SERVER_LOSTSERVER
,&handleserverchange
);
108 deregisterhook(HOOK_CORE_STATSREQUEST
,&nickstats
);
110 /* And our server handlers */
111 deregisterserverhandler("N",&handlenickmsg
);
112 deregisterserverhandler("D",&handlekillmsg
);
113 deregisterserverhandler("Q",&handlequitmsg
);
114 deregisterserverhandler("M",&handleusermodemsg
);
115 deregisterserverhandler("W",&handlewhoismsg
);
116 deregisterserverhandler("AC",&handleaccountmsg
);
117 deregisterserverhandler("R",&handlestatsmsg
);
121 * This function handles servers appearing and disappearing.
122 * For a new server, the client table is allocated.
123 * For a disappearing server, all it's clients are killed and the client table is freed.
126 void handleserverchange(int hooknum
, void *arg
) {
133 case HOOK_SERVER_NEWSERVER
:
134 servernicks
[servernum
]=(nick
**)nsmalloc(POOL_NICK
,(serverlist
[servernum
].maxusernum
+1)*sizeof(nick
**));
135 memset(servernicks
[servernum
],0,(serverlist
[servernum
].maxusernum
+1)*sizeof(nick
**));
138 case HOOK_SERVER_LOSTSERVER
:
139 for (i
=0;i
<=serverlist
[servernum
].maxusernum
;i
++) {
140 if (servernicks
[servernum
][i
]!=NULL
) {
141 deletenick(servernicks
[servernum
][i
]);
144 nsfree(POOL_NICK
,servernicks
[servernum
]);
152 * This function handles the removal of a nick from the network
155 void deletenick(nick
*np
) {
158 /* Fire the hook. This will deal with removal from channels etc. */
159 triggerhook(HOOK_NICK_LOSTNICK
, np
);
161 /* Release the realname and hostname parts */
163 for (nh
=&(np
->realname
->nicks
);*nh
;nh
=&((*nh
)->nextbyrealname
)) {
165 *nh
=np
->nextbyrealname
;
170 for (nh
=&(np
->host
->nicks
);*nh
;nh
=&((*nh
)->nextbyhost
)) {
177 releaserealname(np
->realname
);
178 releasehost(np
->host
);
180 if(IsAccount(np
) && np
->auth
)
182 np
->auth
->usercount
--;
184 for (nh
=&(np
->auth
->nicks
);*nh
;nh
=&((*nh
)->nextbyauthname
)) {
186 *nh
=np
->nextbyauthname
;
191 releaseauthname(np
->auth
);
194 freesstring(np
->shident
); /* freesstring(NULL) is OK */
195 freesstring(np
->sethost
);
197 np
->ipnode
->usercount
--;
198 derefnode(iptree
, np
->ipnode
);
200 /* TODO: figure out how to cleanly remove nodes without affecting other modules */
202 /* Delete the nick from the servernick table */
203 *(gethandlebynumericunsafe(np
->numeric
))=NULL
;
205 /* Remove the nick from the hash table */
206 removenickfromhash(np
);
211 void addnicktohash(nick
*np
) {
212 np
->next
=nicktable
[nickhash(np
->nick
)];
213 nicktable
[nickhash(np
->nick
)]=np
;
216 void removenickfromhash(nick
*np
) {
219 for (nh
=&(nicktable
[nickhash(np
->nick
)]);*nh
;nh
=&((*nh
)->next
)) {
227 nick
*getnickbynick(const char *name
) {
230 for (np
=nicktable
[nickhash(name
)];np
;np
=np
->next
) {
231 if (!ircd_strcmp(np
->nick
,name
))
238 void nickstats(int hooknum
, void *arg
) {
239 int total
,maxchain
,curchain
,i
,buckets
;
244 buckets
=total
=maxchain
=curchain
=0;
245 for (i
=0;i
<NICKHASHSIZE
;i
++,curchain
=0) {
249 for (;np
;np
=np
->next
) {
254 if (curchain
>maxchain
) {
261 sprintf(buf
,"Nick : %6d nicks (HASH: %6d/%6d, chain %3d)",total
,buckets
,NICKHASHSIZE
,maxchain
);
262 } else if ((long)arg
>2) {
263 sprintf(buf
,"Nick : %6d users on network.",total
);
267 triggerhook(HOOK_CORE_STATSREPLY
,buf
);
271 int registernickext(const char *name
) {
274 if (findnickext(name
)!=-1) {
275 Error("nick",ERR_WARNING
,"Tried to register duplicate nick extension %s",name
);
279 for (i
=0;i
<MAXNICKEXTS
;i
++) {
280 if (nickextnames
[i
]==NULL
) {
281 nickextnames
[i
]=getsstring(name
,100);
286 Error("nick",ERR_WARNING
,"Tried to register too many nick extensions: %s",name
);
290 int findnickext(const char *name
) {
293 for (i
=0;i
<MAXNICKEXTS
;i
++) {
294 if (nickextnames
[i
]!=NULL
&& !ircd_strcmp(name
,nickextnames
[i
]->content
)) {
302 void releasenickext(int index
) {
306 freesstring(nickextnames
[index
]);
307 nickextnames
[index
]=NULL
;
309 for (i
=0;i
<NICKHASHSIZE
;i
++) {
310 for (np
=nicktable
[i
];np
;np
=np
->next
) {
311 np
->exts
[index
]=NULL
;
317 * Produces the "apparent" hostmask as seen by network users.
320 char *visiblehostmask(nick
*np
, char *buf
) {
321 char uhbuf
[USERLEN
+HOSTLEN
+2];
323 visibleuserhost(np
, uhbuf
);
324 sprintf(buf
,"%s!%s",np
->nick
,uhbuf
);
330 * As above without nick
333 char *visibleuserhost(nick
*np
, char *buf
) {
334 char hostbuf
[HOSTLEN
+1];
338 host
=np
->host
->name
->content
;
342 ident
=np
->shident
->content
;
345 host
=np
->sethost
->content
;
347 } else if (IsAccount(np
) && IsHideHost(np
)) {
348 sprintf(hostbuf
,"%s.%s", np
->authname
, HIS_HIDDENHOST
);
352 sprintf(buf
,"%s@%s",ident
,host
);
360 * gethandlebynumeric:
361 * Given a numeric, gives the location in the servernicks table
362 * where it should be. Does not check that the nick currently found
363 * there (if any) has the correct numeric; this is left to the
364 * calling function to figure out.
367 nick
**gethandlebynumeric(long numeric
) {
372 /* Shift off the client identifier part of the numeric to get the server ID */
373 servernum
=(numeric
>>18);
375 if ((serv
=getserverdata(servernum
))==NULL
) {
376 Error("nick",ERR_WARNING
,"Numeric %ld refers to non-existent server %d",numeric
,servernum
);
380 /* Compute the masked numeric */
381 maskednum
=numeric
&(serv
->maxusernum
);
383 return (servernicks
[servernum
])+maskednum
;
387 * getnickbynumeric[str]()
388 * These functions retrieve a nick based on it's numeric on the network
389 * Use the approriate function depending on how your numeric is expressed..
392 nick
*getnickbynumeric(long numeric
) {
395 nh
=gethandlebynumeric(numeric
);
397 if ((*nh
) && ((*nh
)->numeric
!=numeric
)) {
398 /* We found a masked numeric match, but the actual numeric
399 * is different. This counts as a miss. */
406 nick
*getnickbynumericstr(char *numericstr
) {
407 return getnickbynumeric(numerictolong(numericstr
,5));
412 int registernodeext(const char *name
) {
415 if (findnodeext(name
)!=-1) {
416 Error("nick",ERR_WARNING
,"Tried to register duplicate node extension %s",name
);
420 for (i
=0;i
<PATRICIA_MAXSLOTS
;i
++) {
421 if (nodeextnames
[i
]==NULL
) {
422 nodeextnames
[i
]=getsstring(name
,100);
427 Error("nick",ERR_WARNING
,"Tried to register too many extensions: %s",name
);
431 int findnodeext(const char *name
) {
434 for (i
=0;i
<PATRICIA_MAXSLOTS
;i
++) {
435 if (nodeextnames
[i
]!=NULL
&& !ircd_strcmp(name
,nodeextnames
[i
]->content
)) {
443 void releasenodeext(int index
) {
444 patricia_node_t
*head
, *node
;
446 freesstring(nodeextnames
[index
]);
447 nodeextnames
[index
]=NULL
;
451 PATRICIA_WALK_ALL(head
, node
)
453 node
->exts
[index
]=NULL
;