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