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