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