]>
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"
19 MODULE_VERSION("$Id: nick.c 663 2006-05-16 17:27:36Z newserv $")
21 const flag umodeflags
[] = {
23 { 'w', UMODE_WALLOPS
},
26 { 'k', UMODE_SERVICE
},
29 { 'r', UMODE_ACCOUNT
},
30 { 'n', UMODE_HIDECHAN
},
31 { 'x', UMODE_HIDEHOST
},
32 { 'h', UMODE_SETHOST
},
33 { 'R', UMODE_REGPRIV
},
34 { 'I', UMODE_HIDEIDLE
},
37 #define nickhash(x) ((crc32i(x))%NICKHASHSIZE)
39 nick
*nicktable
[NICKHASHSIZE
];
40 nick
**servernicks
[MAXSERVERS
];
42 sstring
*nickextnames
[MAXNICKEXTS
];
44 void nickstats(int hooknum
, void *arg
);
49 memset(nicktable
,0,sizeof(nicktable
));
50 memset(servernicks
,0,sizeof(servernicks
));
52 /* Register our hooks */
53 registerhook(HOOK_SERVER_NEWSERVER
,&handleserverchange
);
54 registerhook(HOOK_SERVER_LOSTSERVER
,&handleserverchange
);
55 registerhook(HOOK_CORE_STATSREQUEST
,&nickstats
);
57 /* And our server handlers */
58 registerserverhandler("N",&handlenickmsg
,10);
59 registerserverhandler("D",&handlekillmsg
,2);
60 registerserverhandler("Q",&handlequitmsg
,1);
61 registerserverhandler("M",&handleusermodemsg
,3);
62 registerserverhandler("W",&handlewhoismsg
,2);
63 registerserverhandler("AC",&handleaccountmsg
,2);
64 registerserverhandler("R",&handlestatsmsg
,2);
66 /* Fake the addition of our own server */
67 handleserverchange(HOOK_SERVER_NEWSERVER
,(void *)numerictolong(mynumeric
->content
,2));
71 * This function handles servers appearing and disappearing.
72 * For a new server, the client table is allocated.
73 * For a disappearing server, all it's clients are killed and the client table is freed.
76 void handleserverchange(int hooknum
, void *arg
) {
83 case HOOK_SERVER_NEWSERVER
:
84 servernicks
[servernum
]=(nick
**)malloc((serverlist
[servernum
].maxusernum
+1)*sizeof(nick
**));
85 memset(servernicks
[servernum
],0,(serverlist
[servernum
].maxusernum
+1)*sizeof(nick
**));
88 case HOOK_SERVER_LOSTSERVER
:
89 for (i
=0;i
<=serverlist
[servernum
].maxusernum
;i
++) {
90 if (servernicks
[servernum
][i
]!=NULL
) {
91 deletenick(servernicks
[servernum
][i
]);
94 free(servernicks
[servernum
]);
102 * This function handles the removal of a nick from the network
105 void deletenick(nick
*np
) {
108 /* Fire the hook. This will deal with removal from channels etc. */
109 triggerhook(HOOK_NICK_LOSTNICK
, np
);
111 /* Release the realname and hostname parts */
113 for (nh
=&(np
->realname
->nicks
);*nh
;nh
=&((*nh
)->nextbyrealname
)) {
115 *nh
=np
->nextbyrealname
;
120 for (nh
=&(np
->host
->nicks
);*nh
;nh
=&((*nh
)->nextbyhost
)) {
127 releaserealname(np
->realname
);
128 releasehost(np
->host
);
130 freesstring(np
->shident
); /* freesstring(NULL) is OK */
131 freesstring(np
->sethost
);
133 /* Delete the nick from the servernick table */
134 *(gethandlebynumericunsafe(np
->numeric
))=NULL
;
136 /* Remove the nick from the hash table */
137 removenickfromhash(np
);
142 void addnicktohash(nick
*np
) {
143 np
->next
=nicktable
[nickhash(np
->nick
)];
144 nicktable
[nickhash(np
->nick
)]=np
;
147 void removenickfromhash(nick
*np
) {
150 for (nh
=&(nicktable
[nickhash(np
->nick
)]);*nh
;nh
=&((*nh
)->next
)) {
158 nick
*getnickbynick(const char *name
) {
161 for (np
=nicktable
[nickhash(name
)];np
;np
=np
->next
) {
162 if (!ircd_strcmp(np
->nick
,name
))
169 void nickstats(int hooknum
, void *arg
) {
170 int total
,maxchain
,curchain
,i
,buckets
;
175 buckets
=total
=maxchain
=curchain
=0;
176 for (i
=0;i
<NICKHASHSIZE
;i
++,curchain
=0) {
180 for (;np
;np
=np
->next
) {
185 if (curchain
>maxchain
) {
192 sprintf(buf
,"Nick : %6d nicks (HASH: %6d/%6d, chain %3d)",total
,buckets
,NICKHASHSIZE
,maxchain
);
193 } else if ((int)arg
>2) {
194 sprintf(buf
,"Nick : %6d users on network.",total
);
198 triggerhook(HOOK_CORE_STATSREPLY
,buf
);
202 int registernickext(const char *name
) {
205 if (findnickext(name
)!=-1) {
206 Error("nick",ERR_WARNING
,"Tried to register duplicate nick extension %s",name
);
210 for (i
=0;i
<MAXNICKEXTS
;i
++) {
211 if (nickextnames
[i
]==NULL
) {
212 nickextnames
[i
]=getsstring(name
,100);
217 Error("channel",ERR_WARNING
,"Tried to register too many extensions: %s",name
);
221 int findnickext(const char *name
) {
224 for (i
=0;i
<MAXNICKEXTS
;i
++) {
225 if (nickextnames
[i
]!=NULL
&& !ircd_strcmp(name
,nickextnames
[i
]->content
)) {
233 void releasenickext(int index
) {
237 freesstring(nickextnames
[index
]);
238 nickextnames
[index
]=NULL
;
240 for (i
=0;i
<NICKHASHSIZE
;i
++) {
241 for (np
=nicktable
[i
];np
;np
=np
->next
) {
242 np
->exts
[index
]=NULL
;
248 * Produces the "apparent" hostmask as seen by network users.
251 char *visiblehostmask(nick
*np
, char *buf
) {
252 char hostbuf
[HOSTLEN
+1];
256 host
=np
->host
->name
->content
;
260 ident
=np
->shident
->content
;
263 host
=np
->sethost
->content
;
265 } else if (IsAccount(np
) && IsHideHost(np
)) {
266 sprintf(hostbuf
,"%s.%s", np
->authname
, HIS_HIDDENHOST
);
270 sprintf(buf
,"%s!%s@%s",np
->nick
,ident
,host
);
278 * gethandlebynumeric:
279 * Given a numeric, gives the location in the servernicks table
280 * where it should be. Does not check that the nick currently found
281 * there (if any) has the correct numeric; this is left to the
282 * calling function to figure out.
285 nick
**gethandlebynumeric(long numeric
) {
290 /* Shift off the client identifier part of the numeric to get the server ID */
291 servernum
=(numeric
>>18);
293 if ((serv
=getserverdata(servernum
))==NULL
) {
294 Error("nick",ERR_WARNING
,"Numeric %ld refers to non-existent server %d",numeric
,servernum
);
298 /* Compute the masked numeric */
299 maskednum
=numeric
&(serv
->maxusernum
);
301 return (servernicks
[servernum
])+maskednum
;
305 * getnickbynumeric[str]()
306 * These functions retrieve a nick based on it's numeric on the network
307 * Use the approriate function depending on how your numeric is expressed..
310 nick
*getnickbynumeric(long numeric
) {
313 nh
=gethandlebynumeric(numeric
);
315 if ((*nh
) && ((*nh
)->numeric
!=numeric
)) {
316 /* We found a masked numeric match, but the actual numeric
317 * is different. This counts as a miss. */
324 nick
*getnickbynumericstr(char *numericstr
) {
325 return getnickbynumeric(numerictolong(numericstr
,5));