]>
Commit | Line | Data |
---|---|---|
1 | /* localuser.c */ | |
2 | ||
3 | #include "../nick/nick.h" | |
4 | #include "../lib/base64.h" | |
5 | #include "../lib/sstring.h" | |
6 | #include "../irc/irc.h" | |
7 | #include "../irc/irc_config.h" | |
8 | #include "../core/hooks.h" | |
9 | #include "../core/error.h" | |
10 | #include "../lib/version.h" | |
11 | #include "localuser.h" | |
12 | ||
13 | #include <string.h> | |
14 | #include <stdarg.h> | |
15 | #include <stdio.h> | |
16 | ||
17 | MODULE_VERSION(""); | |
18 | ||
19 | int currentlocalunum; | |
20 | UserMessageHandler umhandlers[MAXLOCALUSER+1]; | |
21 | ||
22 | typedef struct pendingkill { | |
23 | nick *source, *target; | |
24 | sstring *reason; | |
25 | struct pendingkill *next; | |
26 | } pendingkill; | |
27 | ||
28 | pendingkill *pendingkilllist; | |
29 | ||
30 | void checklocalkill(int hooknum, void *nick); | |
31 | void clearpendingkills(int hooknum, void *arg); | |
32 | void checkpendingkills(int hooknum, void *arg); | |
33 | void _killuser(nick *source, nick *target, char *reason); | |
34 | ||
35 | void _init() { | |
36 | int i; | |
37 | ||
38 | for (i=0;i<=MAXLOCALUSER;i++) { | |
39 | umhandlers[i]=NULL; | |
40 | } | |
41 | currentlocalunum=1; | |
42 | pendingkilllist=NULL; | |
43 | registerhook(HOOK_IRC_SENDBURSTNICKS,&sendnickburst); | |
44 | registerhook(HOOK_NICK_LOSTNICK,&checklocalkill); | |
45 | registerhook(HOOK_NICK_LOSTNICK,&checkpendingkills); | |
46 | registerhook(HOOK_CORE_ENDOFHOOKSQUEUE,&clearpendingkills); | |
47 | registerserverhandler("P",&handleprivatemsgcmd,2); | |
48 | registerserverhandler("O",&handleprivatenoticecmd, 2); | |
49 | } | |
50 | ||
51 | void _fini() { | |
52 | pendingkill *pk; | |
53 | ||
54 | for (pk=pendingkilllist;pk;pk=pendingkilllist) { | |
55 | pendingkilllist = pk->next; | |
56 | freesstring(pk->reason); | |
57 | free(pk); | |
58 | } | |
59 | } | |
60 | ||
61 | /* | |
62 | * registerlocaluser: | |
63 | * This function creates a local user, and broadcasts it's existence to the net (if connected). | |
64 | */ | |
65 | ||
66 | nick *registerlocaluser(char *nickname, char *ident, char *host, char *realname, char *authname, flag_t umodes, UserMessageHandler handler) { | |
67 | int i; | |
68 | nick *newuser,*np; | |
69 | struct irc_in_addr ipaddress; | |
70 | ||
71 | i=0; | |
72 | currentlocalunum=(currentlocalunum+1)%262142; | |
73 | while (servernicks[numerictolong(mynumeric->content,2)][currentlocalunum&MAXLOCALUSER]!=NULL) { | |
74 | /* Numeric 262143 on our server is used for "nouser" by the channels module, so cannot be allocated */ | |
75 | currentlocalunum=(currentlocalunum+1)%262142; | |
76 | if (++i>MAXLOCALUSER) { | |
77 | return NULL; | |
78 | } | |
79 | } | |
80 | ||
81 | /* This code is very similar to stuff in nick.c... */ | |
82 | newuser=newnick(); | |
83 | newuser->numeric=(numerictolong(mynumeric->content,2)<<18)|(currentlocalunum); | |
84 | strncpy(newuser->nick,nickname,NICKLEN); | |
85 | newuser->nick[NICKLEN]='\0'; | |
86 | strncpy(newuser->ident,ident,USERLEN); | |
87 | newuser->ident[USERLEN]='\0'; | |
88 | newuser->host=findorcreatehost(host); | |
89 | newuser->realname=findorcreaterealname(realname); | |
90 | newuser->nextbyhost=newuser->host->nicks; | |
91 | newuser->host->nicks=newuser; | |
92 | newuser->nextbyrealname=newuser->realname->nicks; | |
93 | newuser->realname->nicks=newuser; | |
94 | newuser->umodes=umodes; | |
95 | ||
96 | /* XXX shroud: need to port original code */ | |
97 | memset(&ipaddress, 0, sizeof(ipaddress)); | |
98 | ((char *)ipaddress.in6_16)[15] = 1; | |
99 | ||
100 | newuser->ipnode = refnode(iptree, &ipaddress, PATRICIA_MAXBITS); | |
101 | #if 0 | |
102 | newuser->ipaddress=(127<<24)+(1<<8)+((currentlocalunum%253)+1); /* Make it look like a valid addr on 127.0.1.0/24 */ | |
103 | #endif | |
104 | newuser->timestamp=getnettime(); | |
105 | newuser->shident=NULL; | |
106 | newuser->sethost=NULL; | |
107 | newuser->marker=0; | |
108 | memset(newuser->exts, 0, MAXNICKEXTS * sizeof(void *)); | |
109 | ||
110 | if (IsAccount(newuser)) { | |
111 | strncpy(newuser->authname,authname,ACCOUNTLEN); | |
112 | } else { | |
113 | newuser->authname[0]='\0'; | |
114 | } | |
115 | ||
116 | if (connected) { | |
117 | /* Check for nick collision */ | |
118 | if ((np=getnickbynick(nickname))!=NULL) { | |
119 | /* Make sure we will win the collision */ | |
120 | newuser->timestamp=(np->timestamp-1); | |
121 | killuser(NULL, np, "Nick collision"); | |
122 | } | |
123 | sendnickmsg(newuser); | |
124 | } | |
125 | ||
126 | if (handler!=NULL) { | |
127 | umhandlers[(currentlocalunum&MAXLOCALUSER)]=handler; | |
128 | } | |
129 | ||
130 | *(gethandlebynumeric(newuser->numeric))=newuser; | |
131 | addnicktohash(newuser); | |
132 | triggerhook(HOOK_NICK_NEWNICK,newuser); | |
133 | ||
134 | return newuser; | |
135 | } | |
136 | ||
137 | /* | |
138 | * renamelocaluser: | |
139 | * This function changes the name of a given local user | |
140 | */ | |
141 | ||
142 | int renamelocaluser(nick *np, char *newnick) { | |
143 | nick *np2; | |
144 | char ipbuf[25]; | |
145 | time_t timestamp=getnettime(); | |
146 | ||
147 | if (!np) | |
148 | return -1; | |
149 | ||
150 | if (strlen(newnick) > NICKLEN) | |
151 | return -1; | |
152 | ||
153 | if (homeserver(np->numeric)!=mylongnum) | |
154 | return -1; | |
155 | ||
156 | if ((np2=getnickbynick(newnick))) { | |
157 | if (np2==np) { | |
158 | /* Case only name change */ | |
159 | strncpy(np->nick,newnick,NICKLEN); | |
160 | np->nick[NICKLEN]='\0'; | |
161 | irc_send("%s N %s %d",iptobase64(ipbuf, &(np->p_ipaddr), sizeof(ipbuf), 1),np->nick,np->timestamp); | |
162 | triggerhook(HOOK_NICK_RENAME,np); | |
163 | return 0; | |
164 | } else { | |
165 | /* Kill other user and drop through */ | |
166 | timestamp=np2->timestamp-1; | |
167 | killuser(NULL, np2, "Nick collision"); | |
168 | } | |
169 | } | |
170 | ||
171 | np->timestamp=timestamp; | |
172 | removenickfromhash(np); | |
173 | strncpy(np->nick,newnick,NICKLEN); | |
174 | np->nick[NICKLEN]='\0'; | |
175 | addnicktohash(np); | |
176 | irc_send("%s N %s %d",longtonumeric(np->numeric,5),np->nick,np->timestamp); | |
177 | triggerhook(HOOK_NICK_RENAME,np); | |
178 | ||
179 | return 0; | |
180 | } | |
181 | ||
182 | /* | |
183 | * deregisterlocaluser: | |
184 | * This function removes the given local user from the network | |
185 | */ | |
186 | ||
187 | int deregisterlocaluser(nick *np, char *reason) { | |
188 | int defaultreason=0; | |
189 | long numeric; | |
190 | ||
191 | if (np==NULL || (homeserver(np->numeric)!=mylongnum)) { | |
192 | /* Non-existent user, or user not on this server */ | |
193 | return -1; | |
194 | } | |
195 | ||
196 | if (reason==NULL || *reason=='\0') { | |
197 | defaultreason=1; | |
198 | } | |
199 | ||
200 | numeric=np->numeric; | |
201 | umhandlers[np->numeric&MAXLOCALUSER]=NULL; | |
202 | deletenick(np); | |
203 | if (connected) { | |
204 | irc_send("%s Q :Quit: %s",longtonumeric(numeric,5),(defaultreason?"Leaving":reason)); | |
205 | } | |
206 | ||
207 | return 0; | |
208 | } | |
209 | ||
210 | /* | |
211 | * hooklocaluserhandler: | |
212 | * This function adds a new handler to the hook chain for a local user | |
213 | * THIS RELIES ON MODULES BEING UNLOADED IN THE CORRECT ORDER. | |
214 | */ | |
215 | UserMessageHandler hooklocaluserhandler(nick *np, UserMessageHandler newhandler) { | |
216 | UserMessageHandler oldhandler = NULL; | |
217 | if (np==NULL || (homeserver(np->numeric)!=mylongnum)) { | |
218 | /* Non-existent user, or user not on this server */ | |
219 | return NULL; | |
220 | } | |
221 | oldhandler = umhandlers[np->numeric&MAXLOCALUSER]; | |
222 | umhandlers[np->numeric&MAXLOCALUSER]=newhandler; | |
223 | return oldhandler; | |
224 | } | |
225 | ||
226 | /* | |
227 | * sendnickmsg: | |
228 | * Sends details of a given local nick to the network. | |
229 | */ | |
230 | ||
231 | void sendnickmsg(nick *np) { | |
232 | char numericbuf[6]; | |
233 | char ipbuf[25]; | |
234 | ||
235 | strncpy(numericbuf,longtonumeric(np->numeric,5),5); | |
236 | numericbuf[5]='\0'; | |
237 | ||
238 | irc_send("%s N %s 1 %ld %s %s %s%s%s %s %s :%s", | |
239 | mynumeric->content,np->nick,np->timestamp,np->ident,np->host->name->content, | |
240 | printflags(np->umodes,umodeflags),IsAccount(np)?" ":"", | |
241 | np->authname,iptobase64(ipbuf, &(np->p_ipaddr), sizeof(ipbuf), 1),numericbuf, | |
242 | np->realname->name->content); | |
243 | } | |
244 | ||
245 | void sendnickburst(int hooknum, void *arg) { | |
246 | /* Send nick messages for all local users */ | |
247 | nick **nh; | |
248 | int i; | |
249 | ||
250 | nh=servernicks[numerictolong(mynumeric->content,2)]; | |
251 | for (i=0;i<=MAXLOCALUSER;i++) { | |
252 | if (nh[i]!=NULL) { | |
253 | sendnickmsg(nh[i]); | |
254 | } | |
255 | } | |
256 | } | |
257 | ||
258 | /* Check for a kill of a local user */ | |
259 | void checklocalkill(int hooknum, void *target) { | |
260 | long numeric; | |
261 | void *args[1]; | |
262 | ||
263 | args[0]=NULL; | |
264 | ||
265 | numeric=((nick *)target)->numeric; | |
266 | ||
267 | if (homeserver(numeric)==mylongnum) { | |
268 | if (umhandlers[(numeric)&MAXLOCALUSER]!=NULL) { | |
269 | (umhandlers[(numeric)&MAXLOCALUSER])((nick *)target,LU_KILLED,args); | |
270 | } | |
271 | } | |
272 | } | |
273 | ||
274 | int handleprivatemsgcmd(void *source, int cargc, char **cargv) { | |
275 | return handlemessageornotice(source, cargc, cargv, 0); | |
276 | } | |
277 | ||
278 | int handleprivatenoticecmd(void *source, int cargc, char **cargv) { | |
279 | return handlemessageornotice(source, cargc, cargv, 1); | |
280 | } | |
281 | ||
282 | /* Handle privmsg or notice command addressed to user */ | |
283 | int handlemessageornotice(void *source, int cargc, char **cargv, int isnotice) { | |
284 | nick *sender; | |
285 | nick *target; | |
286 | char *ch; | |
287 | char targetnick[NICKLEN+1]; | |
288 | int foundat; | |
289 | void *nargs[2]; | |
290 | int i; | |
291 | ||
292 | /* Should have target and message */ | |
293 | if (cargc<2) { | |
294 | return CMD_OK; | |
295 | } | |
296 | ||
297 | if (cargv[0][0]=='#' || cargv[0][0]=='+') { | |
298 | /* Channel message/notice */ | |
299 | return CMD_OK; | |
300 | } | |
301 | ||
302 | if ((sender=getnickbynumericstr((char *)source))==NULL) { | |
303 | Error("localuser",ERR_WARNING,"PRIVMSG from non existant user %s",(char *)source); | |
304 | return CMD_OK; | |
305 | } | |
306 | ||
307 | /* Check for a "secure" message (foo@bar) */ | |
308 | foundat=0; | |
309 | for (i=0,ch=cargv[0];(i<=NICKLEN) && (*ch);i++,ch++) { | |
310 | if (*ch=='@') { | |
311 | targetnick[i]='\0'; | |
312 | foundat=1; | |
313 | break; | |
314 | } else { | |
315 | targetnick[i]=*ch; | |
316 | } | |
317 | } | |
318 | ||
319 | if (!foundat) { /* Didn't find an @ sign, assume it's a numeric */ | |
320 | if ((target=getnickbynumericstr(cargv[0]))==NULL) { | |
321 | Error("localuser",ERR_DEBUG,"Couldn't find target for %s",cargv[0]); | |
322 | return CMD_OK; | |
323 | } | |
324 | } else { /* Did find @, do a lookup by nick */ | |
325 | if ((target=getnickbynick(targetnick))==NULL) { | |
326 | Error("localuser",ERR_DEBUG,"Couldn't find target for %s",cargv[0]); | |
327 | irc_send(":%s 401 %s %s :No such nick",myserver->content,sender->nick,cargv[0]); | |
328 | return CMD_OK; | |
329 | } | |
330 | } | |
331 | ||
332 | if (homeserver(target->numeric)!=mylongnum) { | |
333 | Error("localuser",ERR_WARNING,"Got message/notice for someone not on my server"); | |
334 | irc_send(":%s 401 %s %s :No such nick",myserver->content,sender->nick,cargv[0]); | |
335 | return CMD_OK; | |
336 | } | |
337 | ||
338 | if (foundat && !IsService(target)) { | |
339 | Error("localuser",ERR_DEBUG,"Received secure message for %s, but user is not a service",cargv[0]); | |
340 | irc_send(":%s 401 %s %s :No such nick",myserver->content,sender->nick,cargv[0]); | |
341 | return CMD_OK; | |
342 | } | |
343 | ||
344 | if (umhandlers[(target->numeric)&MAXLOCALUSER]==NULL) { | |
345 | /* No handler anyhow.. */ | |
346 | return CMD_OK; | |
347 | } | |
348 | ||
349 | /* Dispatch the message. */ | |
350 | nargs[0]=(void *)sender; | |
351 | nargs[1]=(void *)cargv[1]; | |
352 | (umhandlers[(target->numeric)&MAXLOCALUSER])(target,isnotice?LU_PRIVNOTICE:(foundat?LU_SECUREMSG:LU_PRIVMSG),nargs); | |
353 | ||
354 | return CMD_OK; | |
355 | } | |
356 | ||
357 | /* Send message to user */ | |
358 | void sendmessagetouser(nick *source, nick *target, char *format, ... ) { | |
359 | char buf[BUFSIZE]; | |
360 | char senderstr[6]; | |
361 | va_list va; | |
362 | ||
363 | longtonumeric2(source->numeric,5,senderstr); | |
364 | ||
365 | va_start(va,format); | |
366 | /* 10 bytes of numeric, 5 bytes of fixed format + terminator = 17 bytes */ | |
367 | /* So max sendable message is 495 bytes. Of course, a client won't be able | |
368 | * to receive this.. */ | |
369 | ||
370 | vsnprintf(buf,BUFSIZE-17,format,va); | |
371 | va_end(va); | |
372 | ||
373 | if (homeserver(target->numeric)!=mylongnum) | |
374 | irc_send("%s P %s :%s",senderstr,longtonumeric(target->numeric,5),buf); | |
375 | } | |
376 | ||
377 | /* Send messageto server, we don't check, but noones going to want to put a server pointer in anyway... */ | |
378 | void sendsecuremessagetouser(nick *source, nick *target, char *servername, char *format, ... ) { | |
379 | char buf[BUFSIZE]; | |
380 | char senderstr[6]; | |
381 | va_list va; | |
382 | ||
383 | longtonumeric2(source->numeric,5,senderstr); | |
384 | ||
385 | va_start(va,format); | |
386 | /* 10 bytes of numeric, 5 bytes of fixed format + terminator = 17 bytes */ | |
387 | /* So max sendable message is 495 bytes. Of course, a client won't be able | |
388 | * to receive this.. */ | |
389 | ||
390 | vsnprintf(buf,BUFSIZE-17,format,va); | |
391 | va_end(va); | |
392 | ||
393 | if (homeserver(target->numeric)!=mylongnum) | |
394 | irc_send("%s P %s@%s :%s",senderstr,target->nick,servername,buf); | |
395 | } | |
396 | ||
397 | /* Send notice to user */ | |
398 | void sendnoticetouser(nick *source, nick *target, char *format, ... ) { | |
399 | char buf[BUFSIZE]; | |
400 | char senderstr[6]; | |
401 | va_list va; | |
402 | ||
403 | longtonumeric2(source->numeric,5,senderstr); | |
404 | ||
405 | va_start(va,format); | |
406 | /* 10 bytes of numeric, 5 bytes of fixed format + terminator = 17 bytes */ | |
407 | /* So max sendable message is 495 bytes. Of course, a client won't be able | |
408 | * to receive this.. */ | |
409 | ||
410 | vsnprintf(buf,BUFSIZE-17,format,va); | |
411 | va_end(va); | |
412 | ||
413 | if (homeserver(target->numeric)!=mylongnum) | |
414 | irc_send("%s O %s :%s",senderstr,longtonumeric(target->numeric,5),buf); | |
415 | } | |
416 | ||
417 | /* Kill user */ | |
418 | void killuser(nick *source, nick *target, char *format, ... ) { | |
419 | char buf[BUFSIZE]; | |
420 | va_list va; | |
421 | pendingkill *pk; | |
422 | ||
423 | va_start(va, format); | |
424 | vsnprintf(buf, BUFSIZE-17, format, va); | |
425 | va_end (va); | |
426 | ||
427 | if (hookqueuelength) { | |
428 | for (pk = pendingkilllist; pk; pk = pk->next) | |
429 | if (pk->target == target) | |
430 | return; | |
431 | ||
432 | Error("localuser", ERR_INFO, "Adding pending kill for %s", target->nick); | |
433 | pk = (pendingkill *)malloc(sizeof(pendingkill)); | |
434 | pk->source = source; | |
435 | pk->target = target; | |
436 | pk->reason = getsstring(buf, BUFSIZE); | |
437 | pk->next = pendingkilllist; | |
438 | pendingkilllist = pk; | |
439 | } else { | |
440 | _killuser(source, target, buf); | |
441 | } | |
442 | } | |
443 | ||
444 | void _killuser(nick *source, nick *target, char *reason) { | |
445 | char senderstr[6]; | |
446 | char sourcestring[NICKLEN+USERLEN+3]; | |
447 | ||
448 | if (!source) { | |
449 | /* If we have a null nick, use the server.. */ | |
450 | strcpy(senderstr, mynumeric->content); | |
451 | strcpy(sourcestring, myserver->content); | |
452 | } else { | |
453 | strcpy(senderstr, longtonumeric(source->numeric,5)); | |
454 | sprintf(sourcestring,"%s!%s",source->host->name->content, source->nick); | |
455 | } | |
456 | ||
457 | irc_send("%s D %s :%s (%s)",senderstr,longtonumeric(target->numeric,5),sourcestring,reason); | |
458 | deletenick(target); | |
459 | } | |
460 | ||
461 | void clearpendingkills(int hooknum, void *arg) { | |
462 | pendingkill *pk; | |
463 | ||
464 | pk = pendingkilllist; | |
465 | while (pk) { | |
466 | pendingkilllist = pk->next; | |
467 | ||
468 | if (pk->target) { | |
469 | Error("localuser", ERR_INFO, "Processing pending kill for %s", pk->target->nick); | |
470 | _killuser(pk->source, pk->target, pk->reason->content); | |
471 | } | |
472 | ||
473 | freesstring(pk->reason); | |
474 | free(pk); | |
475 | pk = pendingkilllist; | |
476 | } | |
477 | } | |
478 | ||
479 | void checkpendingkills(int hooknum, void *arg) { | |
480 | nick *np = (nick *)arg; | |
481 | pendingkill *pk; | |
482 | ||
483 | for (pk=pendingkilllist; pk; pk = pk->next) { | |
484 | if (pk->source == np) { | |
485 | Error("localuser", ERR_INFO, "Pending kill source %s got deleted, NULL'ing source for pending kill", np->nick); | |
486 | pk->source = NULL; | |
487 | } | |
488 | if (pk->target == np) { | |
489 | Error("localuser", ERR_INFO, "Pending kill target %s got deleted, NULL'ing target for pending kill", np->nick); | |
490 | pk->target = NULL; | |
491 | } | |
492 | } | |
493 | } | |
494 | ||
495 | /* Auth user */ | |
496 | void localusersetaccount(nick *np, char *accname) { | |
497 | if (IsAccount(np)) { | |
498 | Error("localuser",ERR_WARNING,"Tried to set account on user %s already authed", np->nick); | |
499 | return; | |
500 | } | |
501 | ||
502 | SetAccount(np); | |
503 | strncpy(np->authname, accname, ACCOUNTLEN); | |
504 | np->authname[ACCOUNTLEN]='\0'; | |
505 | ||
506 | if (connected) { | |
507 | irc_send("%s AC %s %s %ld",mynumeric->content, longtonumeric(np->numeric,5), np->authname, getnettime()); | |
508 | } | |
509 | ||
510 | triggerhook(HOOK_NICK_ACCOUNT, np); | |
511 | } | |
512 |