]> jfr.im git - irc/quakenet/newserv.git/blob - nick/nick.c
Initial Import
[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 <stdlib.h>
15 #include <string.h>
16 #include <stdio.h>
17
18 const flag umodeflags[] = {
19 { 'i', UMODE_INV },
20 { 'w', UMODE_WALLOPS },
21 { 'g', UMODE_DEBUG },
22 { 'o', UMODE_OPER },
23 { 'k', UMODE_SERVICE },
24 { 'X', UMODE_XOPER },
25 { 'd', UMODE_DEAF },
26 { 'r', UMODE_ACCOUNT },
27 { 'n', UMODE_HIDECHAN },
28 { 'x', UMODE_HIDEHOST },
29 { 'h', UMODE_SETHOST },
30 { 'R', UMODE_REGPRIV },
31 { 'I', UMODE_HIDEIDLE },
32 { '\0', 0 } };
33
34 #define nickhash(x) ((crc32i(x))%NICKHASHSIZE)
35
36 nick *nicktable[NICKHASHSIZE];
37 nick **servernicks[MAXSERVERS];
38
39 sstring *nickextnames[MAXNICKEXTS];
40
41 void nickstats(int hooknum, void *arg);
42
43 void _init() {
44 initnickalloc();
45 initnickhelpers();
46 memset(nicktable,0,sizeof(nicktable));
47 memset(servernicks,0,sizeof(servernicks));
48
49 /* Register our hooks */
50 registerhook(HOOK_SERVER_NEWSERVER,&handleserverchange);
51 registerhook(HOOK_SERVER_LOSTSERVER,&handleserverchange);
52 registerhook(HOOK_CORE_STATSREQUEST,&nickstats);
53
54 /* And our server handlers */
55 registerserverhandler("N",&handlenickmsg,10);
56 registerserverhandler("D",&handlekillmsg,2);
57 registerserverhandler("Q",&handlequitmsg,1);
58 registerserverhandler("M",&handleusermodemsg,3);
59 registerserverhandler("W",&handlewhoismsg,2);
60 registerserverhandler("AC",&handleaccountmsg,2);
61 registerserverhandler("R",&handlestatsmsg,2);
62
63 /* Fake the addition of our own server */
64 handleserverchange(HOOK_SERVER_NEWSERVER,(void *)numerictolong(mynumeric->content,2));
65 }
66
67 /*
68 * This function handles servers appearing and disappearing.
69 * For a new server, the client table is allocated.
70 * For a disappearing server, all it's clients are killed and the client table is freed.
71 */
72
73 void handleserverchange(int hooknum, void *arg) {
74 int servernum;
75 int i;
76
77 servernum=(int)arg;
78
79 switch(hooknum) {
80 case HOOK_SERVER_NEWSERVER:
81 servernicks[servernum]=(nick **)malloc((serverlist[servernum].maxusernum+1)*sizeof(nick **));
82 memset(servernicks[servernum],0,(serverlist[servernum].maxusernum+1)*sizeof(nick **));
83 break;
84
85 case HOOK_SERVER_LOSTSERVER:
86 for (i=0;i<=serverlist[servernum].maxusernum;i++) {
87 if (servernicks[servernum][i]!=NULL) {
88 deletenick(servernicks[servernum][i]);
89 }
90 }
91 free(servernicks[servernum]);
92 break;
93 }
94 }
95
96 /*
97 * deletenick:
98 *
99 * This function handles the removal of a nick from the network
100 */
101
102 void deletenick(nick *np) {
103 nick **nh;
104
105 /* Fire the hook. This will deal with removal from channels etc. */
106 triggerhook(HOOK_NICK_LOSTNICK, np);
107
108 /* Release the realname and hostname parts */
109
110 for (nh=&(np->realname->nicks);*nh;nh=&((*nh)->nextbyrealname)) {
111 if (*nh==np) {
112 *nh=np->nextbyrealname;
113 break;
114 }
115 }
116
117 for (nh=&(np->host->nicks);*nh;nh=&((*nh)->nextbyhost)) {
118 if (*nh==np) {
119 *nh=np->nextbyhost;
120 break;
121 }
122 }
123
124 releaserealname(np->realname);
125 releasehost(np->host);
126
127 freesstring(np->shident); /* freesstring(NULL) is OK */
128 freesstring(np->sethost);
129
130 /* Delete the nick from the servernick table */
131 *(gethandlebynumericunsafe(np->numeric))=NULL;
132
133 /* Remove the nick from the hash table */
134 removenickfromhash(np);
135
136 freenick(np);
137 }
138
139 void addnicktohash(nick *np) {
140 np->next=nicktable[nickhash(np->nick)];
141 nicktable[nickhash(np->nick)]=np;
142 }
143
144 void removenickfromhash(nick *np) {
145 nick **nh;
146
147 for (nh=&(nicktable[nickhash(np->nick)]);*nh;nh=&((*nh)->next)) {
148 if ((*nh)==np) {
149 (*nh)=np->next;
150 break;
151 }
152 }
153 }
154
155 nick *getnickbynick(const char *name) {
156 nick *np;
157
158 for (np=nicktable[nickhash(name)];np;np=np->next) {
159 if (!ircd_strcmp(np->nick,name))
160 return np;
161 }
162
163 return NULL;
164 }
165
166 void nickstats(int hooknum, void *arg) {
167 int total,maxchain,curchain,i,buckets;
168 nick *np;
169 char buf[200];
170
171 /* Get nick stats */
172 buckets=total=maxchain=curchain=0;
173 for (i=0;i<NICKHASHSIZE;i++,curchain=0) {
174 np=nicktable[i];
175 if (np!=NULL) {
176 buckets++;
177 for (;np;np=np->next) {
178 total++;
179 curchain++;
180 }
181 }
182 if (curchain>maxchain) {
183 maxchain=curchain;
184 }
185 }
186
187 if ((int)arg>5) {
188 /* Full stats */
189 sprintf(buf,"Nick : %6d nicks (HASH: %6d/%6d, chain %3d)",total,buckets,NICKHASHSIZE,maxchain);
190 } else if ((int)arg>2) {
191 sprintf(buf,"Nick : %6d users on network.",total);
192 }
193
194 if ((int)arg>2) {
195 triggerhook(HOOK_CORE_STATSREPLY,buf);
196 }
197 }
198
199 int registernickext(const char *name) {
200 int i;
201
202 if (findnickext(name)!=-1) {
203 Error("nick",ERR_WARNING,"Tried to register duplicate nick extension %s",name);
204 return -1;
205 }
206
207 for (i=0;i<MAXNICKEXTS;i++) {
208 if (nickextnames[i]==NULL) {
209 nickextnames[i]=getsstring(name,100);
210 return i;
211 }
212 }
213
214 Error("channel",ERR_WARNING,"Tried to register too many extensions: %s",name);
215 return -1;
216 }
217
218 int findnickext(const char *name) {
219 int i;
220
221 for (i=0;i<MAXNICKEXTS;i++) {
222 if (nickextnames[i]!=NULL && !ircd_strcmp(name,nickextnames[i]->content)) {
223 return i;
224 }
225 }
226
227 return -1;
228 }
229
230 void releasenickext(int index) {
231 int i;
232 nick *np;
233
234 freesstring(nickextnames[index]);
235 nickextnames[index]=NULL;
236
237 for (i=0;i<NICKHASHSIZE;i++) {
238 for (np=nicktable[i];np;np=np->next) {
239 np->exts[index]=NULL;
240 }
241 }
242 }
243
244 /* visiblehostmask
245 * Produces the "apparent" hostmask as seen by network users.
246 */
247
248 char *visiblehostmask(nick *np, char *buf) {
249 char hostbuf[HOSTLEN+1];
250 char *ident, *host;
251
252 ident=np->ident;
253 host=np->host->name->content;
254
255 if (IsSetHost(np)) {
256 if (np->shident) {
257 ident=np->shident->content;
258 }
259 if (np->sethost) {
260 host=np->sethost->content;
261 }
262 } else if (IsAccount(np) && IsHideHost(np)) {
263 sprintf(hostbuf,"%s.%s", np->authname, HIS_HIDDENHOST);
264 host=hostbuf;
265 }
266
267 sprintf(buf,"%s!%s@%s",np->nick,ident,host);
268
269 return buf;
270 }
271
272 #if 0
273
274 /*
275 * gethandlebynumeric:
276 * Given a numeric, gives the location in the servernicks table
277 * where it should be. Does not check that the nick currently found
278 * there (if any) has the correct numeric; this is left to the
279 * calling function to figure out.
280 */
281
282 nick **gethandlebynumeric(long numeric) {
283 int servernum;
284 int maskednum;
285 server *serv;
286
287 /* Shift off the client identifier part of the numeric to get the server ID */
288 servernum=(numeric>>18);
289
290 if ((serv=getserverdata(servernum))==NULL) {
291 Error("nick",ERR_WARNING,"Numeric %ld refers to non-existent server %d",numeric,servernum);
292 return NULL;
293 }
294
295 /* Compute the masked numeric */
296 maskednum=numeric&(serv->maxusernum);
297
298 return (servernicks[servernum])+maskednum;
299 }
300
301 /*
302 * getnickbynumeric[str]()
303 * These functions retrieve a nick based on it's numeric on the network
304 * Use the approriate function depending on how your numeric is expressed..
305 */
306
307 nick *getnickbynumeric(long numeric) {
308 nick **nh;
309
310 nh=gethandlebynumeric(numeric);
311
312 if ((*nh) && ((*nh)->numeric!=numeric)) {
313 /* We found a masked numeric match, but the actual numeric
314 * is different. This counts as a miss. */
315 return NULL;
316 }
317
318 return (*nh);
319 }
320
321 nick *getnickbynumericstr(char *numericstr) {
322 return getnickbynumeric(numerictolong(numericstr,5));
323 }
324
325 #endif