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