]> jfr.im git - irc/quakenet/newserv.git/blob - nick/nick.c
LUA: Add servername and servernumeric nickpushers.
[irc/quakenet/newserv.git] / nick / nick.c
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"
14 #include "../lib/version.h"
15 #include "../core/nsmalloc.h"
16
17 #include <stdlib.h>
18 #include <string.h>
19 #include <stdio.h>
20
21 MODULE_VERSION("");
22
23 const 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 },
37 { 'P', UMODE_PARANOID },
38 { '\0', 0 } };
39
40 const flag accountflags[] = {
41 { 'q', AFLAG_STAFF },
42 { 'h', AFLAG_SUPPORT },
43 { 'o', AFLAG_OPER },
44 { 'a', AFLAG_ADMIN },
45 { 'd', AFLAG_DEVELOPER },
46 { '\0', 0 } };
47
48 #define nickhash(x) ((crc32i(x))%NICKHASHSIZE)
49
50 nick *nicktable[NICKHASHSIZE];
51 nick **servernicks[MAXSERVERS];
52
53 sstring *nickextnames[MAXNICKEXTS];
54
55 void nickstats(int hooknum, void *arg);
56
57 char *NULLAUTHNAME = "";
58
59 void _init() {
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
68 initnickalloc();
69 initnickhelpers();
70 memset(nicktable,0,sizeof(nicktable));
71 memset(servernicks,0,sizeof(servernicks));
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 }
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("AC",&handleaccountmsg,4);
91 registerserverhandler("P",&handleprivmsg,2);
92 registerserverhandler("A",&handleawaymsg,1);
93
94 /* Fake the addition of our own server */
95 handleserverchange(HOOK_SERVER_NEWSERVER,(void *)numerictolong(mynumeric->content,2));
96 }
97
98 void _fini() {
99 nick *np;
100 int i;
101
102 fininickhelpers();
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);
108 freesstring(np->opername);
109 if(!np->auth && np->authname && (np->authname != NULLAUTHNAME))
110 free(np->authname);
111 }
112 }
113
114 nsfreeall(POOL_NICK);
115
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);
126 deregisterserverhandler("AC",&handleaccountmsg);
127 deregisterserverhandler("P",&handleprivmsg);
128 deregisterserverhandler("A",&handleawaymsg);
129 }
130
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
137 void handleserverchange(int hooknum, void *arg) {
138 long servernum;
139 int i;
140
141 servernum=(long)arg;
142
143 switch(hooknum) {
144 case HOOK_SERVER_NEWSERVER:
145 servernicks[servernum]=(nick **)nsmalloc(POOL_NICK,(serverlist[servernum].maxusernum+1)*sizeof(nick **));
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 }
155 nsfree(POOL_NICK,servernicks[servernum]);
156 break;
157 }
158 }
159
160 /*
161 * deletenick:
162 *
163 * This function handles the removal of a nick from the network
164 */
165
166 void 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
191 if(IsAccount(np)) {
192 if(!np->auth) {
193 if(np->authname && (np->authname != NULLAUTHNAME))
194 free(np->authname);
195 } else {
196 np->auth->usercount--;
197
198 for (nh=&(np->auth->nicks);*nh;nh=&((*nh)->nextbyauthname)) {
199 if (*nh==np) {
200 *nh=np->nextbyauthname;
201 break;
202 }
203 }
204
205 releaseauthname(np->auth);
206 }
207 }
208
209 freesstring(np->shident); /* freesstring(NULL) is OK */
210 freesstring(np->sethost);
211 freesstring(np->opername);
212
213 node_decrement_usercount(np->ipnode);
214 derefnode(iptree, np->ipnode);
215
216 /* TODO: figure out how to cleanly remove nodes without affecting other modules */
217
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
227 void addnicktohash(nick *np) {
228 np->next=nicktable[nickhash(np->nick)];
229 nicktable[nickhash(np->nick)]=np;
230 }
231
232 void 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
243 nick *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
254 void 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
275 if ((long)arg>5) {
276 /* Full stats */
277 sprintf(buf,"Nick : %6d nicks (HASH: %6d/%6d, chain %3d)",total,buckets,NICKHASHSIZE,maxchain);
278 } else if ((long)arg>2) {
279 sprintf(buf,"Nick : %6d users on network.",total);
280 }
281
282 if ((long)arg>2) {
283 triggerhook(HOOK_CORE_STATSREPLY,buf);
284 }
285 }
286
287 int 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
302 Error("nick",ERR_WARNING,"Tried to register too many nick extensions: %s",name);
303 return -1;
304 }
305
306 int 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
318 void 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
336 char *visiblehostmask(nick *np, char *buf) {
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
349 char *visibleuserhost(nick *np, char *buf) {
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
368 sprintf(buf,"%s@%s",ident,host);
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
383 nick **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
408 nick *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
422 nick *getnickbynumericstr(char *numericstr) {
423 return getnickbynumeric(numerictolong(numericstr,5));
424 }
425
426 #endif
427