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