]> jfr.im git - irc/quakenet/newserv.git/blob - localuser/localuser.c
Merge default.
[irc/quakenet/newserv.git] / localuser / localuser.c
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 #include <inttypes.h>
17
18 MODULE_VERSION("");
19
20 int currentlocalunum;
21 UserMessageHandler umhandlers[MAXLOCALUSER+1];
22
23 typedef struct pendingkill {
24 nick *source, *target;
25 sstring *reason;
26 struct pendingkill *next;
27 } pendingkill;
28
29 pendingkill *pendingkilllist;
30
31 void checklocalkill(int hooknum, void *nick);
32 void clearpendingkills(int hooknum, void *arg);
33 void checkpendingkills(int hooknum, void *arg);
34 void _killuser(nick *source, nick *target, char *reason);
35
36 void _init() {
37 int i;
38
39 for (i=0;i<=MAXLOCALUSER;i++) {
40 umhandlers[i]=NULL;
41 }
42 currentlocalunum=1;
43 pendingkilllist=NULL;
44 registerhook(HOOK_IRC_SENDBURSTNICKS,&sendnickburst);
45 registerhook(HOOK_NICK_KILL,&checklocalkill);
46 registerhook(HOOK_NICK_LOSTNICK,&checkpendingkills); /* CHECK ME -> should this hook KILL or LOSTNICK or BOTH */
47 registerhook(HOOK_CORE_ENDOFHOOKSQUEUE,&clearpendingkills);
48 registerserverhandler("P",&handleprivatemsgcmd,2);
49 registerserverhandler("O",&handleprivatenoticecmd, 2);
50 }
51
52 void _fini() {
53 pendingkill *pk;
54
55 for (pk=pendingkilllist;pk;pk=pendingkilllist) {
56 pendingkilllist = pk->next;
57 freesstring(pk->reason);
58 free(pk);
59 }
60
61 deregisterhook(HOOK_IRC_SENDBURSTNICKS,&sendnickburst);
62 deregisterhook(HOOK_NICK_KILL,&checklocalkill);
63 deregisterhook(HOOK_NICK_LOSTNICK,&checkpendingkills); /* CHECK ME -> should this hook KILL or LOSTNICK or BOTH */
64 deregisterhook(HOOK_CORE_ENDOFHOOKSQUEUE,&clearpendingkills);
65
66 deregisterserverhandler("P",&handleprivatemsgcmd);
67 deregisterserverhandler("O",&handleprivatenoticecmd);
68 }
69
70 /*
71 * registerlocaluserflags:
72 * This function creates a local user, and broadcasts it's existence to the net (if connected).
73 */
74
75 nick *registerlocaluserflags(char *nickname, char *ident, char *host, char *realname, char *authname, unsigned long authid, flag_t accountflags, flag_t umodes, UserMessageHandler handler) {
76 int i;
77 nick *newuser,*np;
78 struct irc_in_addr ipaddress;
79
80 i=0;
81 currentlocalunum=(currentlocalunum+1)%262142;
82 while (servernicks[numerictolong(mynumeric->content,2)][currentlocalunum&MAXLOCALUSER]!=NULL) {
83 /* Numeric 262143 on our server is used for "nouser" by the channels module, so cannot be allocated */
84 currentlocalunum=(currentlocalunum+1)%262142;
85 if (++i>MAXLOCALUSER) {
86 return NULL;
87 }
88 }
89
90 /* This code is very similar to stuff in nick.c... */
91 newuser=newnick();
92 newuser->numeric=(numerictolong(mynumeric->content,2)<<18)|(currentlocalunum);
93 strncpy(newuser->nick,nickname,NICKLEN);
94 newuser->nick[NICKLEN]='\0';
95 strncpy(newuser->ident,ident,USERLEN);
96 newuser->ident[USERLEN]='\0';
97 newuser->host=findorcreatehost(host);
98 newuser->realname=findorcreaterealname(realname);
99 newuser->nextbyhost=newuser->host->nicks;
100 newuser->host->nicks=newuser;
101 newuser->nextbyrealname=newuser->realname->nicks;
102 newuser->realname->nicks=newuser;
103 newuser->umodes=umodes;
104
105 memset(&ipaddress, 0, sizeof(ipaddress));
106 ((unsigned short *)(ipaddress.in6_16))[5] = 65535;
107 ((unsigned short *)(ipaddress.in6_16))[6] = 127;
108 ((unsigned char *)(ipaddress.in6_16))[14] = 1;
109 ((unsigned char *)(ipaddress.in6_16))[15] = (currentlocalunum%253)+1;
110
111 memcpy(&newuser->ipaddress, &ipaddress, sizeof(ipaddress));
112
113 newuser->ipnode = refnode(iptree, &ipaddress, PATRICIA_MAXBITS);
114 node_increment_usercount(newuser->ipnode);
115
116 newuser->timestamp=getnettime();
117 newuser->shident=NULL;
118 newuser->sethost=NULL;
119 newuser->away=NULL;
120 newuser->marker=0;
121 memset(newuser->exts, 0, MAXNICKEXTS * sizeof(void *));
122
123 if (IsOper(newuser)) {
124 newuser->opername = getsstring("-", ACCOUNTLEN);
125 } else {
126 newuser->opername = NULL;
127 }
128
129 newuser->accountts=0;
130 newuser->auth=NULL;
131 newuser->authname=NULLAUTHNAME;
132 if (IsAccount(newuser)) {
133 newuser->accountts=newuser->timestamp;
134 if (authid) {
135 newuser->auth=findorcreateauthname(authid, authname);
136 newuser->authname=newuser->auth->name;
137 newuser->auth->usercount++;
138 newuser->nextbyauthname=newuser->auth->nicks;
139 newuser->auth->nicks=newuser;
140 newuser->auth->flags=accountflags;
141 } else {
142 /*
143 this is done for three reasons:
144 1: so I don't have to change 500 pieces of code
145 2: so services can be authed with special reserved ids
146 3: saves space over the old authname per user method
147 */
148 newuser->authname=malloc(strlen(authname) + 1);
149 strcpy(newuser->authname,authname);
150 }
151 }
152
153 if (connected) {
154 /* Check for nick collision */
155 if ((np=getnickbynick(nickname))!=NULL) {
156 /* Make sure we will win the collision */
157 newuser->timestamp=(np->timestamp-1);
158 killuser(NULL, np, "Nick collision");
159 }
160 sendnickmsg(newuser);
161 }
162
163 if (handler!=NULL) {
164 umhandlers[(currentlocalunum&MAXLOCALUSER)]=handler;
165 }
166
167 *(gethandlebynumeric(newuser->numeric))=newuser;
168 addnicktohash(newuser);
169 triggerhook(HOOK_NICK_NEWNICK,newuser);
170
171 return newuser;
172 }
173
174 /*
175 * renamelocaluser:
176 * This function changes the name of a given local user
177 */
178
179 int renamelocaluser(nick *np, char *newnick) {
180 nick *np2;
181 char ipbuf[25];
182 time_t timestamp=getnettime();
183 void *harg[2];
184 char oldnick[NICKLEN+1];
185
186 if (!np)
187 return -1;
188
189 if (strlen(newnick) > NICKLEN)
190 return -1;
191
192 if (homeserver(np->numeric)!=mylongnum)
193 return -1;
194
195 strncpy(oldnick,np->nick,NICKLEN);
196 oldnick[NICKLEN]='\0';
197 harg[0]=(void *)np;
198 harg[1]=(void *)oldnick;
199
200 if ((np2=getnickbynick(newnick))) {
201 if (np2==np) {
202 /* Case only name change */
203 strncpy(np->nick,newnick,NICKLEN);
204 np->nick[NICKLEN]='\0';
205 irc_send("%s N %s %jd",iptobase64(ipbuf, &(np->p_ipaddr), sizeof(ipbuf), 1),np->nick,(intmax_t)np->timestamp);
206 triggerhook(HOOK_NICK_RENAME,harg);
207 return 0;
208 } else {
209 /* Kill other user and drop through */
210 timestamp=np2->timestamp-1;
211 killuser(NULL, np2, "Nick collision");
212 }
213 }
214
215 np->timestamp=timestamp;
216 removenickfromhash(np);
217 strncpy(np->nick,newnick,NICKLEN);
218 np->nick[NICKLEN]='\0';
219 addnicktohash(np);
220 irc_send("%s N %s %jd",longtonumeric(np->numeric,5),np->nick,(intmax_t)np->timestamp);
221 triggerhook(HOOK_NICK_RENAME,harg);
222
223 return 0;
224 }
225
226 /*
227 * deregisterlocaluser:
228 * This function removes the given local user from the network
229 */
230
231 int deregisterlocaluser(nick *np, char *reason) {
232 long numeric;
233 char reasonstr[512];
234 void *harg[2];
235
236 if (np==NULL || (homeserver(np->numeric)!=mylongnum)) {
237 /* Non-existent user, or user not on this server */
238 return -1;
239 }
240
241 if (reason==NULL || *reason=='\0') {
242 sprintf(reasonstr,"Quit");
243 } else {
244 snprintf(reasonstr,510,"Quit: %s",reason);
245 }
246
247 harg[0]=np;
248 harg[1]=reasonstr;
249
250 triggerhook(HOOK_NICK_QUIT, harg);
251
252 numeric=np->numeric;
253 umhandlers[np->numeric&MAXLOCALUSER]=NULL;
254 deletenick(np);
255 if (connected) {
256 irc_send("%s Q :%s",longtonumeric(numeric,5),reasonstr);
257 }
258
259 return 0;
260 }
261
262 /*
263 * hooklocaluserhandler:
264 * This function adds a new handler to the hook chain for a local user
265 * THIS RELIES ON MODULES BEING UNLOADED IN THE CORRECT ORDER.
266 */
267 UserMessageHandler hooklocaluserhandler(nick *np, UserMessageHandler newhandler) {
268 UserMessageHandler oldhandler = NULL;
269 if (np==NULL || (homeserver(np->numeric)!=mylongnum)) {
270 /* Non-existent user, or user not on this server */
271 return NULL;
272 }
273 oldhandler = umhandlers[np->numeric&MAXLOCALUSER];
274 umhandlers[np->numeric&MAXLOCALUSER]=newhandler;
275 return oldhandler;
276 }
277
278 /*
279 * sendnickmsg:
280 * Sends details of a given local nick to the network.
281 */
282
283 void sendnickmsg(nick *np) {
284 char numericbuf[6];
285 char ipbuf[25], operbuf[ACCOUNTLEN + 5];
286 char accountbuf[100];
287
288 strncpy(numericbuf,longtonumeric(np->numeric,5),5);
289 numericbuf[5]='\0';
290
291 if(IsOper(np) && (serverlist[myhub].flags & SMODE_OPERNAME)) {
292 snprintf(operbuf,sizeof(operbuf)," %s",np->opername?np->opername->content:"-");
293 } else {
294 operbuf[0] = '\0';
295 }
296
297 accountbuf[0]='\0';
298 if (IsAccount(np)) {
299 if (np->auth) {
300 if(np->auth->flags) {
301 snprintf(accountbuf,sizeof(accountbuf)," %s:%ld:%lu:%"PRIu64,np->authname,np->accountts,np->auth->userid,np->auth->flags);
302 } else {
303 snprintf(accountbuf,sizeof(accountbuf)," %s:%ld:%lu",np->authname,np->accountts,np->auth->userid);
304 }
305 } else if(np->authname) {
306 snprintf(accountbuf,sizeof(accountbuf)," %s:%ld:0",np->authname,np->accountts);
307 }
308 }
309
310 irc_send("%s N %s 1 %ld %s %s %s%s%s %s %s :%s",
311 mynumeric->content,np->nick,np->timestamp,np->ident,np->host->name->content,
312 printflags(np->umodes,umodeflags),operbuf,accountbuf,iptobase64(ipbuf,&(np->p_ipaddr),
313 sizeof(ipbuf),1),numericbuf,np->realname->name->content);
314 }
315
316 void sendnickburst(int hooknum, void *arg) {
317 /* Send nick messages for all local users */
318 nick **nh;
319 int i;
320
321 nh=servernicks[numerictolong(mynumeric->content,2)];
322 for (i=0;i<=MAXLOCALUSER;i++) {
323 if (nh[i]!=NULL) {
324 sendnickmsg(nh[i]);
325 }
326 }
327 }
328
329 /* Check for a kill of a local user */
330 void checklocalkill(int hooknum, void *arg) {
331 void **args=arg;
332 nick *target=args[0];
333 char *reason=args[1];
334 long numeric;
335
336 void *myargs[1];
337 myargs[0]=reason;
338
339
340 numeric=((nick *)target)->numeric;
341
342 if (homeserver(numeric)==mylongnum) {
343 if (umhandlers[(numeric)&MAXLOCALUSER]!=NULL) {
344 (umhandlers[(numeric)&MAXLOCALUSER])((nick *)target,LU_KILLED,myargs);
345 }
346 }
347 }
348
349 int handleprivatemsgcmd(void *source, int cargc, char **cargv) {
350 return handlemessageornotice(source, cargc, cargv, 0);
351 }
352
353 int handleprivatenoticecmd(void *source, int cargc, char **cargv) {
354 return handlemessageornotice(source, cargc, cargv, 1);
355 }
356
357 /* Handle privmsg or notice command addressed to user */
358 int handlemessageornotice(void *source, int cargc, char **cargv, int isnotice) {
359 nick *sender;
360 nick *target;
361 char *ch;
362 char targetnick[NICKLEN+1];
363 int foundat;
364 void *nargs[2];
365 int i;
366
367 /* Should have target and message */
368 if (cargc<2) {
369 return CMD_OK;
370 }
371
372 if (cargv[0][0]=='#' || cargv[0][0]=='+') {
373 /* Channel message/notice */
374 return CMD_OK;
375 }
376
377 if ((sender=getnickbynumericstr((char *)source))==NULL) {
378 Error("localuser",ERR_WARNING,"PRIVMSG from non existant user %s",(char *)source);
379 return CMD_OK;
380 }
381
382 /* Check for a "secure" message (foo@bar) */
383 foundat=0;
384 for (i=0,ch=cargv[0];(i<=NICKLEN) && (*ch);i++,ch++) {
385 if (*ch=='@') {
386 targetnick[i]='\0';
387 foundat=1;
388 break;
389 } else {
390 targetnick[i]=*ch;
391 }
392 }
393
394 if (!foundat) { /* Didn't find an @ sign, assume it's a numeric */
395 if ((target=getnickbynumericstr(cargv[0]))==NULL) {
396 Error("localuser",ERR_DEBUG,"Couldn't find target for %s",cargv[0]);
397 return CMD_OK;
398 }
399 } else { /* Did find @, do a lookup by nick */
400 if ((target=getnickbynick(targetnick))==NULL) {
401 Error("localuser",ERR_DEBUG,"Couldn't find target for %s",cargv[0]);
402 irc_send(":%s 401 %s %s :No such nick",myserver->content,sender->nick,cargv[0]);
403 return CMD_OK;
404 }
405 }
406
407 if (homeserver(target->numeric)!=mylongnum) {
408 Error("localuser",ERR_WARNING,"Got message/notice for someone not on my server");
409 irc_send(":%s 401 %s %s :No such nick",myserver->content,sender->nick,cargv[0]);
410 return CMD_OK;
411 }
412
413 if (foundat && !IsService(target)) {
414 Error("localuser",ERR_DEBUG,"Received secure message for %s, but user is not a service",cargv[0]);
415 irc_send(":%s 401 %s %s :No such nick",myserver->content,sender->nick,cargv[0]);
416 return CMD_OK;
417 }
418
419 if (umhandlers[(target->numeric)&MAXLOCALUSER]==NULL) {
420 /* No handler anyhow.. */
421 return CMD_OK;
422 }
423
424 /* Dispatch the message. */
425 nargs[0]=(void *)sender;
426 nargs[1]=(void *)cargv[1];
427 (umhandlers[(target->numeric)&MAXLOCALUSER])(target,isnotice?LU_PRIVNOTICE:(foundat?LU_SECUREMSG:LU_PRIVMSG),nargs);
428
429 return CMD_OK;
430 }
431
432 /* Send message to user */
433 void sendmessagetouser(nick *source, nick *target, char *format, ... ) {
434 char buf[BUFSIZE];
435 char senderstr[6];
436 va_list va;
437
438 longtonumeric2(source->numeric,5,senderstr);
439
440 va_start(va,format);
441 /* 10 bytes of numeric, 5 bytes of fixed format + terminator = 17 bytes */
442 /* So max sendable message is 495 bytes. Of course, a client won't be able
443 * to receive this.. */
444
445 vsnprintf(buf,BUFSIZE-17,format,va);
446 va_end(va);
447
448 if (homeserver(target->numeric)!=mylongnum)
449 irc_send("%s P %s :%s",senderstr,longtonumeric(target->numeric,5),buf);
450 }
451
452 /* Send messageto server, we don't check, but noones going to want to put a server pointer in anyway... */
453 void sendsecuremessagetouser(nick *source, nick *target, char *servername, char *format, ... ) {
454 char buf[BUFSIZE];
455 char senderstr[6];
456 va_list va;
457
458 longtonumeric2(source->numeric,5,senderstr);
459
460 va_start(va,format);
461 /* 10 bytes of numeric, 5 bytes of fixed format + terminator = 17 bytes */
462 /* So max sendable message is 495 bytes. Of course, a client won't be able
463 * to receive this.. */
464
465 vsnprintf(buf,BUFSIZE-17,format,va);
466 va_end(va);
467
468 if (homeserver(target->numeric)!=mylongnum)
469 irc_send("%s P %s@%s :%s",senderstr,target->nick,servername,buf);
470 }
471
472 /* Send notice to user */
473 void sendnoticetouser(nick *source, nick *target, char *format, ... ) {
474 char buf[BUFSIZE];
475 char senderstr[6];
476 va_list va;
477
478 longtonumeric2(source->numeric,5,senderstr);
479
480 va_start(va,format);
481 /* 10 bytes of numeric, 5 bytes of fixed format + terminator = 17 bytes */
482 /* So max sendable message is 495 bytes. Of course, a client won't be able
483 * to receive this.. */
484
485 vsnprintf(buf,BUFSIZE-17,format,va);
486 va_end(va);
487
488 if (homeserver(target->numeric)!=mylongnum)
489 irc_send("%s O %s :%s",senderstr,longtonumeric(target->numeric,5),buf);
490 }
491
492 /* Kill user */
493 void killuser(nick *source, nick *target, char *format, ... ) {
494 char buf[BUFSIZE];
495 va_list va;
496 pendingkill *pk;
497
498 va_start(va, format);
499 vsnprintf(buf, BUFSIZE-17, format, va);
500 va_end (va);
501
502 if (hookqueuelength) {
503 for (pk = pendingkilllist; pk; pk = pk->next)
504 if (pk->target == target)
505 return;
506
507 Error("localuser", ERR_DEBUG, "Adding pending kill for %s", target->nick);
508 pk = (pendingkill *)malloc(sizeof(pendingkill));
509 pk->source = source;
510 pk->target = target;
511 pk->reason = getsstring(buf, BUFSIZE);
512 pk->next = pendingkilllist;
513 pendingkilllist = pk;
514 } else {
515 _killuser(source, target, buf);
516 }
517 }
518
519 void sethostuser(nick *target, char *ident, char *host) {
520 irc_send("%s SH %s %s %s", mynumeric->content, longtonumeric(target->numeric, 5), ident, host);
521 }
522
523 void _killuser(nick *source, nick *target, char *reason) {
524 char senderstr[6];
525 char sourcestring[HOSTLEN+NICKLEN+3];
526 char reasonstr[512];
527 void *args[2];
528
529 if (!source) {
530 /* If we have a null nick, use the server.. */
531 strcpy(senderstr, mynumeric->content);
532 strcpy(sourcestring, myserver->content);
533 } else {
534 strcpy(senderstr, longtonumeric(source->numeric,5));
535 sprintf(sourcestring,"%s!%s",source->host->name->content, source->nick);
536 }
537
538 snprintf(reasonstr,512,"%s (%s)",sourcestring,reason);
539 reasonstr[511]='\0';
540
541 irc_send("%s D %s :%s",senderstr,longtonumeric(target->numeric,5),reasonstr);
542
543 args[0]=target;
544 args[1]=reasonstr;
545 triggerhook(HOOK_NICK_KILL, args);
546
547 deletenick(target);
548 }
549
550 void clearpendingkills(int hooknum, void *arg) {
551 pendingkill *pk;
552
553 pk = pendingkilllist;
554 while (pk) {
555 pendingkilllist = pk->next;
556
557 if (pk->target) {
558 Error("localuser", ERR_DEBUG, "Processing pending kill for %s", pk->target->nick);
559 _killuser(pk->source, pk->target, pk->reason->content);
560 }
561
562 freesstring(pk->reason);
563 free(pk);
564 pk = pendingkilllist;
565 }
566 }
567
568 void checkpendingkills(int hooknum, void *arg) {
569 nick *np = (nick *)arg;
570 pendingkill *pk;
571
572 for (pk=pendingkilllist; pk; pk = pk->next) {
573 if (pk->source == np) {
574 Error("localuser", ERR_INFO, "Pending kill source %s got deleted, NULL'ing source for pending kill", np->nick);
575 pk->source = NULL;
576 }
577 if (pk->target == np) {
578 Error("localuser", ERR_INFO, "Pending kill target %s got deleted, NULL'ing target for pending kill", np->nick);
579 pk->target = NULL;
580 }
581 }
582 }
583
584 void sendaccountmessage(nick *np) {
585 if (connected && IsAccount(np)) {
586 if (np->auth) {
587 if (np->auth->flags) {
588 irc_send("%s AC %s %s %ld %lu %"PRIu64,mynumeric->content, longtonumeric(np->numeric,5), np->authname, np->accountts, np->auth->userid, np->auth->flags);
589 } else {
590 irc_send("%s AC %s %s %ld %lu",mynumeric->content, longtonumeric(np->numeric,5), np->authname, np->accountts, np->auth->userid);
591 }
592 } else {
593 irc_send("%s AC %s %s %ld 0",mynumeric->content, longtonumeric(np->numeric,5), np->authname, np->accountts);
594 }
595 }
596 }
597
598 /* Auth user, don't use to set flags after authing */
599 void localusersetaccount(nick *np, char *accname, unsigned long accid, u_int64_t accountflags, time_t authTS) {
600 if (IsAccount(np)) {
601 Error("localuser",ERR_WARNING,"Tried to set account on user %s already authed", np->nick);
602 return;
603 }
604
605 SetAccount(np);
606 np->accountts=authTS?authTS:getnettime();
607
608 if (accid) {
609 np->auth=findorcreateauthname(accid, accname);
610 np->auth->usercount++;
611 np->authname=np->auth->name;
612 np->nextbyauthname=np->auth->nicks;
613 np->auth->nicks=np;
614 np->auth->flags=accountflags;
615 } else {
616 np->auth=NULL;
617 np->authname=malloc(strlen(accname) + 1);
618 strcpy(np->authname,accname);
619 }
620
621 sendaccountmessage(np);
622
623 triggerhook(HOOK_NICK_ACCOUNT, np);
624 }
625
626 void localusersetumodes(nick *np, flag_t newmodes) {
627 if (connected) {
628 irc_send("%s M %s %s", longtonumeric(np->numeric,5), np->nick, printflagdiff(np->umodes, newmodes, umodeflags));
629 }
630
631 np->umodes = newmodes;
632 }
633
634 void localusersetaccountflags(authname *anp, u_int64_t accountflags) {
635 void *arg[2];
636 nick *np;
637 u_int64_t oldflags = anp->flags;
638
639 arg[0] = anp;
640 arg[1] = &oldflags;
641 anp->flags = accountflags;
642
643 for(np=anp->nicks;np;np=np->nextbyauthname)
644 sendaccountmessage(np);
645
646 triggerhook(HOOK_AUTH_FLAGSUPDATED, arg);
647 }