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