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