]> jfr.im git - irc/quakenet/newserv.git/blame - nick/nick.c
Merge.
[irc/quakenet/newserv.git] / nick / nick.c
CommitLineData
c86edd1d
Q
1/* nick.c */
2
3#include "nick.h"
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"
87698d77 14#include "../lib/version.h"
103521a1 15#include "../core/nsmalloc.h"
16
c86edd1d
Q
17#include <stdlib.h>
18#include <string.h>
19#include <stdio.h>
20
70b0a4e5 21MODULE_VERSION("");
87698d77 22
c86edd1d
Q
23const flag umodeflags[] = {
24 { 'i', UMODE_INV },
25 { 'w', UMODE_WALLOPS },
26 { 'g', UMODE_DEBUG },
27 { 'o', UMODE_OPER },
28 { 'k', UMODE_SERVICE },
29 { 'X', UMODE_XOPER },
30 { 'd', UMODE_DEAF },
31 { 'r', UMODE_ACCOUNT },
32 { 'n', UMODE_HIDECHAN },
33 { 'x', UMODE_HIDEHOST },
34 { 'h', UMODE_SETHOST },
35 { 'R', UMODE_REGPRIV },
36 { 'I', UMODE_HIDEIDLE },
e98d808a 37 { 'P', UMODE_PARANOID },
c86edd1d
Q
38 { '\0', 0 } };
39
c153c0dc 40const flag accountflags[] = {
c4ffdb9b 41 { 'q', AFLAG_STAFF },
3294b10b 42 { 'h', AFLAG_SUPPORT },
c4ffdb9b
CP
43 { 'o', AFLAG_OPER },
44 { 'a', AFLAG_ADMIN },
c153c0dc
CP
45 { 'd', AFLAG_DEVELOPER },
46 { '\0', 0 } };
47
c86edd1d
Q
48#define nickhash(x) ((crc32i(x))%NICKHASHSIZE)
49
50nick *nicktable[NICKHASHSIZE];
51nick **servernicks[MAXSERVERS];
52
53sstring *nickextnames[MAXNICKEXTS];
54
55void nickstats(int hooknum, void *arg);
56
3404a8f0
CP
57char *NULLAUTHNAME = "";
58
c86edd1d 59void _init() {
e31727be 60 unsigned int i;
61 authname *anp;
62
63 /* Clear up the nicks in authext */
64 for (i=0;i<AUTHNAMEHASHSIZE;i++)
65 for (anp=authnametable[i];anp;anp=anp->next)
66 anp->nicks=NULL;
67
c86edd1d
Q
68 initnickalloc();
69 initnickhelpers();
70 memset(nicktable,0,sizeof(nicktable));
71 memset(servernicks,0,sizeof(servernicks));
72
73 /* Register our hooks */
74 registerhook(HOOK_SERVER_NEWSERVER,&handleserverchange);
75 registerhook(HOOK_SERVER_LOSTSERVER,&handleserverchange);
76 registerhook(HOOK_CORE_STATSREQUEST,&nickstats);
77
78 /* And our server handlers */
79 registerserverhandler("N",&handlenickmsg,10);
80 registerserverhandler("D",&handlekillmsg,2);
81 registerserverhandler("Q",&handlequitmsg,1);
82 registerserverhandler("M",&handleusermodemsg,3);
83 registerserverhandler("W",&handlewhoismsg,2);
47657339 84 registerserverhandler("AC",&handleaccountmsg,4);
c86edd1d 85 registerserverhandler("R",&handlestatsmsg,2);
6885ae9c 86 registerserverhandler("P",&handleprivmsg,2);
c86edd1d
Q
87
88 /* Fake the addition of our own server */
89 handleserverchange(HOOK_SERVER_NEWSERVER,(void *)numerictolong(mynumeric->content,2));
90}
91
103521a1 92void _fini() {
9cf0b03f
CP
93 nick *np;
94 int i;
95
707c5824 96 fininickhelpers();
9cf0b03f
CP
97
98 for (i=0;i<NICKHASHSIZE;i++) {
99 for (np=nicktable[i];np;np=np->next) {
100 freesstring(np->shident);
101 freesstring(np->sethost);
843184e3 102 freesstring(np->opername);
3404a8f0 103 if(!np->auth && np->authname && (np->authname != NULLAUTHNAME))
3294b10b 104 free(np->authname);
9cf0b03f
CP
105 }
106 }
107
df5ab174 108 nsfreeall(POOL_NICK);
707c5824 109
103521a1 110 /* Free the hooks */
111 deregisterhook(HOOK_SERVER_NEWSERVER,&handleserverchange);
112 deregisterhook(HOOK_SERVER_LOSTSERVER,&handleserverchange);
113 deregisterhook(HOOK_CORE_STATSREQUEST,&nickstats);
114
115 /* And our server handlers */
116 deregisterserverhandler("N",&handlenickmsg);
117 deregisterserverhandler("D",&handlekillmsg);
118 deregisterserverhandler("Q",&handlequitmsg);
119 deregisterserverhandler("M",&handleusermodemsg);
120 deregisterserverhandler("W",&handlewhoismsg);
121 deregisterserverhandler("AC",&handleaccountmsg);
122 deregisterserverhandler("R",&handlestatsmsg);
6885ae9c 123 deregisterserverhandler("P",&handleprivmsg);
103521a1 124}
125
c86edd1d
Q
126/*
127 * This function handles servers appearing and disappearing.
128 * For a new server, the client table is allocated.
129 * For a disappearing server, all it's clients are killed and the client table is freed.
130 */
131
132void handleserverchange(int hooknum, void *arg) {
c3db6f7e 133 long servernum;
c86edd1d
Q
134 int i;
135
c3db6f7e 136 servernum=(long)arg;
c86edd1d
Q
137
138 switch(hooknum) {
139 case HOOK_SERVER_NEWSERVER:
103521a1 140 servernicks[servernum]=(nick **)nsmalloc(POOL_NICK,(serverlist[servernum].maxusernum+1)*sizeof(nick **));
c86edd1d
Q
141 memset(servernicks[servernum],0,(serverlist[servernum].maxusernum+1)*sizeof(nick **));
142 break;
143
144 case HOOK_SERVER_LOSTSERVER:
145 for (i=0;i<=serverlist[servernum].maxusernum;i++) {
146 if (servernicks[servernum][i]!=NULL) {
147 deletenick(servernicks[servernum][i]);
148 }
149 }
2a010087 150 nsfree(POOL_NICK,servernicks[servernum]);
c86edd1d
Q
151 break;
152 }
153}
154
155/*
156 * deletenick:
157 *
158 * This function handles the removal of a nick from the network
159 */
160
161void deletenick(nick *np) {
162 nick **nh;
163
164 /* Fire the hook. This will deal with removal from channels etc. */
165 triggerhook(HOOK_NICK_LOSTNICK, np);
166
167 /* Release the realname and hostname parts */
168
169 for (nh=&(np->realname->nicks);*nh;nh=&((*nh)->nextbyrealname)) {
170 if (*nh==np) {
171 *nh=np->nextbyrealname;
172 break;
173 }
174 }
175
176 for (nh=&(np->host->nicks);*nh;nh=&((*nh)->nextbyhost)) {
177 if (*nh==np) {
178 *nh=np->nextbyhost;
179 break;
180 }
181 }
182
183 releaserealname(np->realname);
184 releasehost(np->host);
185
3294b10b
CP
186 if(IsAccount(np)) {
187 if(!np->auth) {
3404a8f0 188 if(np->authname && (np->authname != NULLAUTHNAME))
3294b10b
CP
189 free(np->authname);
190 } else {
191 np->auth->usercount--;
47657339 192
3294b10b
CP
193 for (nh=&(np->auth->nicks);*nh;nh=&((*nh)->nextbyauthname)) {
194 if (*nh==np) {
195 *nh=np->nextbyauthname;
196 break;
197 }
47657339 198 }
47657339 199
3294b10b
CP
200 releaseauthname(np->auth);
201 }
47657339
C
202 }
203
c86edd1d
Q
204 freesstring(np->shident); /* freesstring(NULL) is OK */
205 freesstring(np->sethost);
843184e3 206 freesstring(np->opername);
c86edd1d 207
96644df6 208 node_decrement_usercount(np->ipnode);
526e7c1d
P
209 derefnode(iptree, np->ipnode);
210
211 /* TODO: figure out how to cleanly remove nodes without affecting other modules */
212
c86edd1d
Q
213 /* Delete the nick from the servernick table */
214 *(gethandlebynumericunsafe(np->numeric))=NULL;
215
216 /* Remove the nick from the hash table */
217 removenickfromhash(np);
218
219 freenick(np);
220}
221
222void addnicktohash(nick *np) {
223 np->next=nicktable[nickhash(np->nick)];
224 nicktable[nickhash(np->nick)]=np;
225}
226
227void removenickfromhash(nick *np) {
228 nick **nh;
229
230 for (nh=&(nicktable[nickhash(np->nick)]);*nh;nh=&((*nh)->next)) {
231 if ((*nh)==np) {
232 (*nh)=np->next;
233 break;
234 }
235 }
236}
237
238nick *getnickbynick(const char *name) {
239 nick *np;
240
241 for (np=nicktable[nickhash(name)];np;np=np->next) {
242 if (!ircd_strcmp(np->nick,name))
243 return np;
244 }
245
246 return NULL;
247}
248
249void nickstats(int hooknum, void *arg) {
250 int total,maxchain,curchain,i,buckets;
251 nick *np;
252 char buf[200];
253
254 /* Get nick stats */
255 buckets=total=maxchain=curchain=0;
256 for (i=0;i<NICKHASHSIZE;i++,curchain=0) {
257 np=nicktable[i];
258 if (np!=NULL) {
259 buckets++;
260 for (;np;np=np->next) {
261 total++;
262 curchain++;
263 }
264 }
265 if (curchain>maxchain) {
266 maxchain=curchain;
267 }
268 }
269
c3db6f7e 270 if ((long)arg>5) {
c86edd1d
Q
271 /* Full stats */
272 sprintf(buf,"Nick : %6d nicks (HASH: %6d/%6d, chain %3d)",total,buckets,NICKHASHSIZE,maxchain);
c3db6f7e 273 } else if ((long)arg>2) {
c86edd1d
Q
274 sprintf(buf,"Nick : %6d users on network.",total);
275 }
276
c3db6f7e 277 if ((long)arg>2) {
c86edd1d
Q
278 triggerhook(HOOK_CORE_STATSREPLY,buf);
279 }
280}
281
282int registernickext(const char *name) {
283 int i;
284
285 if (findnickext(name)!=-1) {
286 Error("nick",ERR_WARNING,"Tried to register duplicate nick extension %s",name);
287 return -1;
288 }
289
290 for (i=0;i<MAXNICKEXTS;i++) {
291 if (nickextnames[i]==NULL) {
292 nickextnames[i]=getsstring(name,100);
293 return i;
294 }
295 }
296
47657339 297 Error("nick",ERR_WARNING,"Tried to register too many nick extensions: %s",name);
c86edd1d
Q
298 return -1;
299}
300
301int findnickext(const char *name) {
302 int i;
303
304 for (i=0;i<MAXNICKEXTS;i++) {
305 if (nickextnames[i]!=NULL && !ircd_strcmp(name,nickextnames[i]->content)) {
306 return i;
307 }
308 }
309
310 return -1;
311}
312
313void releasenickext(int index) {
314 int i;
315 nick *np;
316
317 freesstring(nickextnames[index]);
318 nickextnames[index]=NULL;
319
320 for (i=0;i<NICKHASHSIZE;i++) {
321 for (np=nicktable[i];np;np=np->next) {
322 np->exts[index]=NULL;
323 }
324 }
325}
326
327/* visiblehostmask
328 * Produces the "apparent" hostmask as seen by network users.
329 */
330
331char *visiblehostmask(nick *np, char *buf) {
28252fef 332 char uhbuf[USERLEN+HOSTLEN+2];
333
334 visibleuserhost(np, uhbuf);
335 sprintf(buf,"%s!%s",np->nick,uhbuf);
336
337 return buf;
338}
339
340/* visibleuserhost
341 * As above without nick
342 */
343
344char *visibleuserhost(nick *np, char *buf) {
c86edd1d
Q
345 char hostbuf[HOSTLEN+1];
346 char *ident, *host;
347
348 ident=np->ident;
349 host=np->host->name->content;
350
351 if (IsSetHost(np)) {
352 if (np->shident) {
353 ident=np->shident->content;
354 }
355 if (np->sethost) {
356 host=np->sethost->content;
357 }
358 } else if (IsAccount(np) && IsHideHost(np)) {
359 sprintf(hostbuf,"%s.%s", np->authname, HIS_HIDDENHOST);
360 host=hostbuf;
361 }
362
28252fef 363 sprintf(buf,"%s@%s",ident,host);
c86edd1d
Q
364
365 return buf;
366}
367
368#if 0
369
370/*
371 * gethandlebynumeric:
372 * Given a numeric, gives the location in the servernicks table
373 * where it should be. Does not check that the nick currently found
374 * there (if any) has the correct numeric; this is left to the
375 * calling function to figure out.
376 */
377
378nick **gethandlebynumeric(long numeric) {
379 int servernum;
380 int maskednum;
381 server *serv;
382
383 /* Shift off the client identifier part of the numeric to get the server ID */
384 servernum=(numeric>>18);
385
386 if ((serv=getserverdata(servernum))==NULL) {
387 Error("nick",ERR_WARNING,"Numeric %ld refers to non-existent server %d",numeric,servernum);
388 return NULL;
389 }
390
391 /* Compute the masked numeric */
392 maskednum=numeric&(serv->maxusernum);
393
394 return (servernicks[servernum])+maskednum;
395}
396
397/*
398 * getnickbynumeric[str]()
399 * These functions retrieve a nick based on it's numeric on the network
400 * Use the approriate function depending on how your numeric is expressed..
401 */
402
403nick *getnickbynumeric(long numeric) {
404 nick **nh;
405
406 nh=gethandlebynumeric(numeric);
407
408 if ((*nh) && ((*nh)->numeric!=numeric)) {
409 /* We found a masked numeric match, but the actual numeric
410 * is different. This counts as a miss. */
411 return NULL;
412 }
413
414 return (*nh);
415}
416
417nick *getnickbynumericstr(char *numericstr) {
418 return getnickbynumeric(numerictolong(numericstr,5));
419}
420
421#endif
526e7c1d 422