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