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"
21 UserMessageHandler umhandlers
[MAXLOCALUSER
+1];
23 typedef struct pendingkill
{
24 nick
*source
, *target
;
26 struct pendingkill
*next
;
29 pendingkill
*pendingkilllist
;
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
);
39 for (i
=0;i
<=MAXLOCALUSER
;i
++) {
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);
55 for (pk
=pendingkilllist
;pk
;pk
=pendingkilllist
) {
56 pendingkilllist
= pk
->next
;
57 freesstring(pk
->reason
);
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
);
66 deregisterserverhandler("P",&handleprivatemsgcmd
);
67 deregisterserverhandler("O",&handleprivatenoticecmd
);
71 * registerlocaluserflags:
72 * This function creates a local user, and broadcasts it's existence to the net (if connected).
75 nick
*registerlocaluserflags(char *nickname
, char *ident
, char *host
, char *realname
, char *authname
, unsigned long authid
, flag_t accountflags
, flag_t umodes
, UserMessageHandler handler
) {
78 struct irc_in_addr ipaddress
;
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
) {
90 /* This code is very similar to stuff in nick.c... */
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
;
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;
111 newuser
->ipnode
= refnode(iptree
, &ipaddress
, PATRICIA_MAXBITS
);
112 node_increment_usercount(newuser
->ipnode
);
114 newuser
->timestamp
=getnettime();
115 newuser
->shident
=NULL
;
116 newuser
->sethost
=NULL
;
119 memset(newuser
->exts
, 0, MAXNICKEXTS
* sizeof(void *));
121 if (IsOper(newuser
)) {
122 newuser
->opername
= getsstring("-", ACCOUNTLEN
);
124 newuser
->opername
= NULL
;
127 newuser
->accountts
=0;
129 newuser
->authname
=NULLAUTHNAME
;
130 if (IsAccount(newuser
)) {
131 newuser
->accountts
=newuser
->timestamp
;
133 newuser
->auth
=findorcreateauthname(authid
, authname
);
134 newuser
->authname
=newuser
->auth
->name
;
135 newuser
->auth
->usercount
++;
136 newuser
->nextbyauthname
=newuser
->auth
->nicks
;
137 newuser
->auth
->nicks
=newuser
;
138 newuser
->auth
->flags
=accountflags
;
141 this is done for three reasons:
142 1: so I don't have to change 500 pieces of code
143 2: so services can be authed with special reserved ids
144 3: saves space over the old authname per user method
146 newuser
->authname
=malloc(strlen(authname
) + 1);
147 strcpy(newuser
->authname
,authname
);
151 newuser
->cloak_count
= 0;
152 newuser
->cloak_extra
= NULL
;
155 /* Check for nick collision */
156 if ((np
=getnickbynick(nickname
))!=NULL
) {
157 /* Make sure we will win the collision */
158 newuser
->timestamp
=(np
->timestamp
-1);
159 killuser(NULL
, np
, "Nick collision");
161 sendnickmsg(newuser
);
165 umhandlers
[(currentlocalunum
&MAXLOCALUSER
)]=handler
;
168 *(gethandlebynumeric(newuser
->numeric
))=newuser
;
169 addnicktohash(newuser
);
170 triggerhook(HOOK_NICK_NEWNICK
,newuser
);
177 * This function changes the name of a given local user
180 int renamelocaluser(nick
*np
, char *newnick
) {
183 time_t timestamp
=getnettime();
185 char oldnick
[NICKLEN
+1];
190 if (strlen(newnick
) > NICKLEN
)
193 if (homeserver(np
->numeric
)!=mylongnum
)
196 strncpy(oldnick
,np
->nick
,NICKLEN
);
197 oldnick
[NICKLEN
]='\0';
199 harg
[1]=(void *)oldnick
;
201 if ((np2
=getnickbynick(newnick
))) {
203 /* Case only name change */
204 strncpy(np
->nick
,newnick
,NICKLEN
);
205 np
->nick
[NICKLEN
]='\0';
206 irc_send("%s N %s %jd",iptobase64(ipbuf
, &(np
->p_ipaddr
), sizeof(ipbuf
), 1),np
->nick
,(intmax_t)np
->timestamp
);
207 triggerhook(HOOK_NICK_RENAME
,harg
);
210 /* Kill other user and drop through */
211 timestamp
=np2
->timestamp
-1;
212 killuser(NULL
, np2
, "Nick collision");
216 np
->timestamp
=timestamp
;
217 removenickfromhash(np
);
218 strncpy(np
->nick
,newnick
,NICKLEN
);
219 np
->nick
[NICKLEN
]='\0';
221 irc_send("%s N %s %jd",longtonumeric(np
->numeric
,5),np
->nick
,(intmax_t)np
->timestamp
);
222 triggerhook(HOOK_NICK_RENAME
,harg
);
228 * deregisterlocaluser:
229 * This function removes the given local user from the network
232 int deregisterlocaluser(nick
*np
, char *reason
) {
237 if (np
==NULL
|| (homeserver(np
->numeric
)!=mylongnum
)) {
238 /* Non-existent user, or user not on this server */
242 if (reason
==NULL
|| *reason
=='\0') {
243 sprintf(reasonstr
,"Quit");
245 snprintf(reasonstr
,510,"Quit: %s",reason
);
251 triggerhook(HOOK_NICK_QUIT
, harg
);
254 umhandlers
[np
->numeric
&MAXLOCALUSER
]=NULL
;
257 irc_send("%s Q :%s",longtonumeric(numeric
,5),reasonstr
);
264 * hooklocaluserhandler:
265 * This function adds a new handler to the hook chain for a local user
266 * THIS RELIES ON MODULES BEING UNLOADED IN THE CORRECT ORDER.
268 UserMessageHandler
hooklocaluserhandler(nick
*np
, UserMessageHandler newhandler
) {
269 UserMessageHandler oldhandler
= NULL
;
270 if (np
==NULL
|| (homeserver(np
->numeric
)!=mylongnum
)) {
271 /* Non-existent user, or user not on this server */
274 oldhandler
= umhandlers
[np
->numeric
&MAXLOCALUSER
];
275 umhandlers
[np
->numeric
&MAXLOCALUSER
]=newhandler
;
281 * Sends details of a given local nick to the network.
284 void sendnickmsg(nick
*np
) {
286 char ipbuf
[25], operbuf
[ACCOUNTLEN
+ 5];
287 char accountbuf
[100];
289 strncpy(numericbuf
,longtonumeric(np
->numeric
,5),5);
292 if(IsOper(np
) && (serverlist
[myhub
].flags
& SMODE_OPERNAME
)) {
293 snprintf(operbuf
,sizeof(operbuf
)," %s",np
->opername
?np
->opername
->content
:"-");
301 if(np
->auth
->flags
) {
302 snprintf(accountbuf
,sizeof(accountbuf
)," %s:%ld:%lu:%"PRIu64
,np
->authname
,np
->accountts
,np
->auth
->userid
,np
->auth
->flags
);
304 snprintf(accountbuf
,sizeof(accountbuf
)," %s:%ld:%lu",np
->authname
,np
->accountts
,np
->auth
->userid
);
306 } else if(np
->authname
) {
307 snprintf(accountbuf
,sizeof(accountbuf
)," %s:%ld:0",np
->authname
,np
->accountts
);
311 irc_send("%s N %s 1 %ld %s %s %s%s%s %s %s :%s",
312 mynumeric
->content
,np
->nick
,np
->timestamp
,np
->ident
,np
->host
->name
->content
,
313 printflags(np
->umodes
,umodeflags
),operbuf
,accountbuf
,iptobase64(ipbuf
,&(np
->p_ipaddr
),
314 sizeof(ipbuf
),1),numericbuf
,np
->realname
->name
->content
);
317 void sendnickburst(int hooknum
, void *arg
) {
318 /* Send nick messages for all local users */
322 nh
=servernicks
[numerictolong(mynumeric
->content
,2)];
323 for (i
=0;i
<=MAXLOCALUSER
;i
++) {
330 /* Check for a kill of a local user */
331 void checklocalkill(int hooknum
, void *arg
) {
333 nick
*target
=args
[0];
334 char *reason
=args
[1];
341 numeric
=((nick
*)target
)->numeric
;
343 if (homeserver(numeric
)==mylongnum
) {
344 if (umhandlers
[(numeric
)&MAXLOCALUSER
]!=NULL
) {
345 (umhandlers
[(numeric
)&MAXLOCALUSER
])((nick
*)target
,LU_KILLED
,myargs
);
350 int handleprivatemsgcmd(void *source
, int cargc
, char **cargv
) {
351 return handlemessageornotice(source
, cargc
, cargv
, 0);
354 int handleprivatenoticecmd(void *source
, int cargc
, char **cargv
) {
355 return handlemessageornotice(source
, cargc
, cargv
, 1);
358 /* Handle privmsg or notice command addressed to user */
359 int handlemessageornotice(void *source
, int cargc
, char **cargv
, int isnotice
) {
363 char targetnick
[NICKLEN
+1];
368 /* Should have target and message */
373 if (cargv
[0][0]=='#' || cargv
[0][0]=='+') {
374 /* Channel message/notice */
378 if ((sender
=getnickbynumericstr((char *)source
))==NULL
) {
379 Error("localuser",ERR_WARNING
,"PRIVMSG from non existant user %s",(char *)source
);
383 /* Check for a "secure" message (foo@bar) */
385 for (i
=0,ch
=cargv
[0];(i
<=NICKLEN
) && (*ch
);i
++,ch
++) {
395 if (!foundat
) { /* Didn't find an @ sign, assume it's a numeric */
396 if ((target
=getnickbynumericstr(cargv
[0]))==NULL
) {
397 Error("localuser",ERR_DEBUG
,"Couldn't find target for %s",cargv
[0]);
400 } else { /* Did find @, do a lookup by nick */
401 if ((target
=getnickbynick(targetnick
))==NULL
) {
402 Error("localuser",ERR_DEBUG
,"Couldn't find target for %s",cargv
[0]);
403 irc_send(":%s 401 %s %s :No such nick",myserver
->content
,sender
->nick
,cargv
[0]);
408 if (homeserver(target
->numeric
)!=mylongnum
) {
409 Error("localuser",ERR_WARNING
,"Got message/notice for someone not on my server");
410 irc_send(":%s 401 %s %s :No such nick",myserver
->content
,sender
->nick
,cargv
[0]);
414 if (foundat
&& !IsService(target
)) {
415 Error("localuser",ERR_DEBUG
,"Received secure message for %s, but user is not a service",cargv
[0]);
416 irc_send(":%s 401 %s %s :No such nick",myserver
->content
,sender
->nick
,cargv
[0]);
420 if (umhandlers
[(target
->numeric
)&MAXLOCALUSER
]==NULL
) {
421 /* No handler anyhow.. */
425 /* Dispatch the message. */
426 nargs
[0]=(void *)sender
;
427 nargs
[1]=(void *)cargv
[1];
428 (umhandlers
[(target
->numeric
)&MAXLOCALUSER
])(target
,isnotice
?LU_PRIVNOTICE
:(foundat
?LU_SECUREMSG
:LU_PRIVMSG
),nargs
);
433 /* Send message to user */
434 void sendmessagetouser(nick
*source
, nick
*target
, char *format
, ... ) {
439 longtonumeric2(source
->numeric
,5,senderstr
);
442 /* 10 bytes of numeric, 5 bytes of fixed format + terminator = 17 bytes */
443 /* So max sendable message is 495 bytes. Of course, a client won't be able
444 * to receive this.. */
446 vsnprintf(buf
,BUFSIZE
-17,format
,va
);
449 if (homeserver(target
->numeric
)!=mylongnum
)
450 irc_send("%s P %s :%s",senderstr
,longtonumeric(target
->numeric
,5),buf
);
453 /* Send messageto server, we don't check, but noones going to want to put a server pointer in anyway... */
454 void sendsecuremessagetouser(nick
*source
, nick
*target
, char *servername
, char *format
, ... ) {
459 longtonumeric2(source
->numeric
,5,senderstr
);
462 /* 10 bytes of numeric, 5 bytes of fixed format + terminator = 17 bytes */
463 /* So max sendable message is 495 bytes. Of course, a client won't be able
464 * to receive this.. */
466 vsnprintf(buf
,BUFSIZE
-17,format
,va
);
469 if (homeserver(target
->numeric
)!=mylongnum
)
470 irc_send("%s P %s@%s :%s",senderstr
,target
->nick
,servername
,buf
);
473 /* Send notice to user */
474 void sendnoticetouser(nick
*source
, nick
*target
, char *format
, ... ) {
479 longtonumeric2(source
->numeric
,5,senderstr
);
482 /* 10 bytes of numeric, 5 bytes of fixed format + terminator = 17 bytes */
483 /* So max sendable message is 495 bytes. Of course, a client won't be able
484 * to receive this.. */
486 vsnprintf(buf
,BUFSIZE
-17,format
,va
);
489 if (homeserver(target
->numeric
)!=mylongnum
)
490 irc_send("%s O %s :%s",senderstr
,longtonumeric(target
->numeric
,5),buf
);
494 void killuser(nick
*source
, nick
*target
, char *format
, ... ) {
499 va_start(va
, format
);
500 vsnprintf(buf
, BUFSIZE
-17, format
, va
);
503 if (hookqueuelength
) {
504 for (pk
= pendingkilllist
; pk
; pk
= pk
->next
)
505 if (pk
->target
== target
)
508 Error("localuser", ERR_DEBUG
, "Adding pending kill for %s", target
->nick
);
509 pk
= (pendingkill
*)malloc(sizeof(pendingkill
));
512 pk
->reason
= getsstring(buf
, BUFSIZE
);
513 pk
->next
= pendingkilllist
;
514 pendingkilllist
= pk
;
516 _killuser(source
, target
, buf
);
520 void sethostuser(nick
*target
, char *ident
, char *host
) {
521 irc_send("%s SH %s %s %s", mynumeric
->content
, longtonumeric(target
->numeric
, 5), ident
, host
);
524 void _killuser(nick
*source
, nick
*target
, char *reason
) {
526 char sourcestring
[HOSTLEN
+NICKLEN
+3];
531 /* If we have a null nick, use the server.. */
532 strcpy(senderstr
, mynumeric
->content
);
533 strcpy(sourcestring
, myserver
->content
);
535 strcpy(senderstr
, longtonumeric(source
->numeric
,5));
536 sprintf(sourcestring
,"%s!%s",source
->host
->name
->content
, source
->nick
);
539 snprintf(reasonstr
,512,"%s (%s)",sourcestring
,reason
);
542 irc_send("%s D %s :%s",senderstr
,longtonumeric(target
->numeric
,5),reasonstr
);
546 triggerhook(HOOK_NICK_KILL
, args
);
551 void clearpendingkills(int hooknum
, void *arg
) {
554 pk
= pendingkilllist
;
556 pendingkilllist
= pk
->next
;
559 Error("localuser", ERR_DEBUG
, "Processing pending kill for %s", pk
->target
->nick
);
560 _killuser(pk
->source
, pk
->target
, pk
->reason
->content
);
563 freesstring(pk
->reason
);
565 pk
= pendingkilllist
;
569 void checkpendingkills(int hooknum
, void *arg
) {
570 nick
*np
= (nick
*)arg
;
573 for (pk
=pendingkilllist
; pk
; pk
= pk
->next
) {
574 if (pk
->source
== np
) {
575 Error("localuser", ERR_INFO
, "Pending kill source %s got deleted, NULL'ing source for pending kill", np
->nick
);
578 if (pk
->target
== np
) {
579 Error("localuser", ERR_INFO
, "Pending kill target %s got deleted, NULL'ing target for pending kill", np
->nick
);
585 void sendaccountmessage(nick
*np
) {
586 if (connected
&& IsAccount(np
)) {
588 if (np
->auth
->flags
) {
589 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
);
591 irc_send("%s AC %s %s %ld %lu",mynumeric
->content
, longtonumeric(np
->numeric
,5), np
->authname
, np
->accountts
, np
->auth
->userid
);
594 irc_send("%s AC %s %s %ld 0",mynumeric
->content
, longtonumeric(np
->numeric
,5), np
->authname
, np
->accountts
);
599 /* Auth user, don't use to set flags after authing */
600 void localusersetaccount(nick
*np
, char *accname
, unsigned long accid
, u_int64_t accountflags
, time_t authTS
) {
602 Error("localuser",ERR_WARNING
,"Tried to set account on user %s already authed", np
->nick
);
607 np
->accountts
=authTS
?authTS
:getnettime();
610 np
->auth
=findorcreateauthname(accid
, accname
);
611 np
->auth
->usercount
++;
612 np
->authname
=np
->auth
->name
;
613 np
->nextbyauthname
=np
->auth
->nicks
;
615 np
->auth
->flags
=accountflags
;
618 np
->authname
=malloc(strlen(accname
) + 1);
619 strcpy(np
->authname
,accname
);
622 sendaccountmessage(np
);
624 triggerhook(HOOK_NICK_ACCOUNT
, np
);
627 void localusersetumodes(nick
*np
, flag_t newmodes
) {
629 irc_send("%s M %s %s", longtonumeric(np
->numeric
,5), np
->nick
, printflagdiff(np
->umodes
, newmodes
, umodeflags
));
632 np
->umodes
= newmodes
;
635 void localusersetaccountflags(authname
*anp
, u_int64_t accountflags
) {
638 u_int64_t oldflags
= anp
->flags
;
642 anp
->flags
= accountflags
;
644 for(np
=anp
->nicks
;np
;np
=np
->nextbyauthname
)
645 sendaccountmessage(np
);
647 triggerhook(HOOK_AUTH_FLAGSUPDATED
, arg
);
650 void localuseraddcloaktarget(nick
*np
, nick
*target
) {
651 char snumeric
[10], tnumeric
[10];
653 strcpy(snumeric
, longtonumeric(np
->numeric
,5));
654 strcpy(tnumeric
, longtonumeric(target
->numeric
,5));
656 irc_send("%s CA %s", snumeric
, tnumeric
);
658 addcloaktarget(np
, target
);
661 void localuserclearcloaktargets(nick
*np
) {
662 irc_send("%s CU", longtonumeric(np
->numeric
,5));
663 clearcloaktargets(np
);