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