]>
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"
18 const flag umodeflags
[] = {
20 { 'w', UMODE_WALLOPS
},
23 { 'k', UMODE_SERVICE
},
26 { 'r', UMODE_ACCOUNT
},
27 { 'n', UMODE_HIDECHAN
},
28 { 'x', UMODE_HIDEHOST
},
29 { 'h', UMODE_SETHOST
},
30 { 'R', UMODE_REGPRIV
},
31 { 'I', UMODE_HIDEIDLE
},
34 #define nickhash(x) ((crc32i(x))%NICKHASHSIZE)
36 nick
*nicktable
[NICKHASHSIZE
];
37 nick
**servernicks
[MAXSERVERS
];
39 sstring
*nickextnames
[MAXNICKEXTS
];
41 void nickstats(int hooknum
, void *arg
);
46 memset(nicktable
,0,sizeof(nicktable
));
47 memset(servernicks
,0,sizeof(servernicks
));
49 /* Register our hooks */
50 registerhook(HOOK_SERVER_NEWSERVER
,&handleserverchange
);
51 registerhook(HOOK_SERVER_LOSTSERVER
,&handleserverchange
);
52 registerhook(HOOK_CORE_STATSREQUEST
,&nickstats
);
54 /* And our server handlers */
55 registerserverhandler("N",&handlenickmsg
,10);
56 registerserverhandler("D",&handlekillmsg
,2);
57 registerserverhandler("Q",&handlequitmsg
,1);
58 registerserverhandler("M",&handleusermodemsg
,3);
59 registerserverhandler("W",&handlewhoismsg
,2);
60 registerserverhandler("AC",&handleaccountmsg
,2);
61 registerserverhandler("R",&handlestatsmsg
,2);
63 /* Fake the addition of our own server */
64 handleserverchange(HOOK_SERVER_NEWSERVER
,(void *)numerictolong(mynumeric
->content
,2));
68 * This function handles servers appearing and disappearing.
69 * For a new server, the client table is allocated.
70 * For a disappearing server, all it's clients are killed and the client table is freed.
73 void handleserverchange(int hooknum
, void *arg
) {
80 case HOOK_SERVER_NEWSERVER
:
81 servernicks
[servernum
]=(nick
**)malloc((serverlist
[servernum
].maxusernum
+1)*sizeof(nick
**));
82 memset(servernicks
[servernum
],0,(serverlist
[servernum
].maxusernum
+1)*sizeof(nick
**));
85 case HOOK_SERVER_LOSTSERVER
:
86 for (i
=0;i
<=serverlist
[servernum
].maxusernum
;i
++) {
87 if (servernicks
[servernum
][i
]!=NULL
) {
88 deletenick(servernicks
[servernum
][i
]);
91 free(servernicks
[servernum
]);
99 * This function handles the removal of a nick from the network
102 void deletenick(nick
*np
) {
105 /* Fire the hook. This will deal with removal from channels etc. */
106 triggerhook(HOOK_NICK_LOSTNICK
, np
);
108 /* Release the realname and hostname parts */
110 for (nh
=&(np
->realname
->nicks
);*nh
;nh
=&((*nh
)->nextbyrealname
)) {
112 *nh
=np
->nextbyrealname
;
117 for (nh
=&(np
->host
->nicks
);*nh
;nh
=&((*nh
)->nextbyhost
)) {
124 releaserealname(np
->realname
);
125 releasehost(np
->host
);
127 freesstring(np
->shident
); /* freesstring(NULL) is OK */
128 freesstring(np
->sethost
);
130 /* Delete the nick from the servernick table */
131 *(gethandlebynumericunsafe(np
->numeric
))=NULL
;
133 /* Remove the nick from the hash table */
134 removenickfromhash(np
);
139 void addnicktohash(nick
*np
) {
140 np
->next
=nicktable
[nickhash(np
->nick
)];
141 nicktable
[nickhash(np
->nick
)]=np
;
144 void removenickfromhash(nick
*np
) {
147 for (nh
=&(nicktable
[nickhash(np
->nick
)]);*nh
;nh
=&((*nh
)->next
)) {
155 nick
*getnickbynick(const char *name
) {
158 for (np
=nicktable
[nickhash(name
)];np
;np
=np
->next
) {
159 if (!ircd_strcmp(np
->nick
,name
))
166 void nickstats(int hooknum
, void *arg
) {
167 int total
,maxchain
,curchain
,i
,buckets
;
172 buckets
=total
=maxchain
=curchain
=0;
173 for (i
=0;i
<NICKHASHSIZE
;i
++,curchain
=0) {
177 for (;np
;np
=np
->next
) {
182 if (curchain
>maxchain
) {
189 sprintf(buf
,"Nick : %6d nicks (HASH: %6d/%6d, chain %3d)",total
,buckets
,NICKHASHSIZE
,maxchain
);
190 } else if ((int)arg
>2) {
191 sprintf(buf
,"Nick : %6d users on network.",total
);
195 triggerhook(HOOK_CORE_STATSREPLY
,buf
);
199 int registernickext(const char *name
) {
202 if (findnickext(name
)!=-1) {
203 Error("nick",ERR_WARNING
,"Tried to register duplicate nick extension %s",name
);
207 for (i
=0;i
<MAXNICKEXTS
;i
++) {
208 if (nickextnames
[i
]==NULL
) {
209 nickextnames
[i
]=getsstring(name
,100);
214 Error("channel",ERR_WARNING
,"Tried to register too many extensions: %s",name
);
218 int findnickext(const char *name
) {
221 for (i
=0;i
<MAXNICKEXTS
;i
++) {
222 if (nickextnames
[i
]!=NULL
&& !ircd_strcmp(name
,nickextnames
[i
]->content
)) {
230 void releasenickext(int index
) {
234 freesstring(nickextnames
[index
]);
235 nickextnames
[index
]=NULL
;
237 for (i
=0;i
<NICKHASHSIZE
;i
++) {
238 for (np
=nicktable
[i
];np
;np
=np
->next
) {
239 np
->exts
[index
]=NULL
;
245 * Produces the "apparent" hostmask as seen by network users.
248 char *visiblehostmask(nick
*np
, char *buf
) {
249 char hostbuf
[HOSTLEN
+1];
253 host
=np
->host
->name
->content
;
257 ident
=np
->shident
->content
;
260 host
=np
->sethost
->content
;
262 } else if (IsAccount(np
) && IsHideHost(np
)) {
263 sprintf(hostbuf
,"%s.%s", np
->authname
, HIS_HIDDENHOST
);
267 sprintf(buf
,"%s!%s@%s",np
->nick
,ident
,host
);
275 * gethandlebynumeric:
276 * Given a numeric, gives the location in the servernicks table
277 * where it should be. Does not check that the nick currently found
278 * there (if any) has the correct numeric; this is left to the
279 * calling function to figure out.
282 nick
**gethandlebynumeric(long numeric
) {
287 /* Shift off the client identifier part of the numeric to get the server ID */
288 servernum
=(numeric
>>18);
290 if ((serv
=getserverdata(servernum
))==NULL
) {
291 Error("nick",ERR_WARNING
,"Numeric %ld refers to non-existent server %d",numeric
,servernum
);
295 /* Compute the masked numeric */
296 maskednum
=numeric
&(serv
->maxusernum
);
298 return (servernicks
[servernum
])+maskednum
;
302 * getnickbynumeric[str]()
303 * These functions retrieve a nick based on it's numeric on the network
304 * Use the approriate function depending on how your numeric is expressed..
307 nick
*getnickbynumeric(long numeric
) {
310 nh
=gethandlebynumeric(numeric
);
312 if ((*nh
) && ((*nh
)->numeric
!=numeric
)) {
313 /* We found a masked numeric match, but the actual numeric
314 * is different. This counts as a miss. */
321 nick
*getnickbynumericstr(char *numericstr
) {
322 return getnickbynumeric(numerictolong(numericstr
,5));