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