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"
20 UserMessageHandler umhandlers
[MAXLOCALUSER
+1];
22 typedef struct pendingkill
{
23 nick
*source
, *target
;
25 struct pendingkill
*next
;
28 pendingkill
*pendingkilllist
;
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
);
38 for (i
=0;i
<=MAXLOCALUSER
;i
++) {
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);
54 for (pk
=pendingkilllist
;pk
;pk
=pendingkilllist
) {
55 pendingkilllist
= pk
->next
;
56 freesstring(pk
->reason
);
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
);
65 deregisterserverhandler("P",&handleprivatemsgcmd
);
66 deregisterserverhandler("O",&handleprivatenoticecmd
);
70 * registerlocaluserwithuseridflags:
71 * This function creates a local user, and broadcasts it's existence to the net (if connected).
74 nick
*registerlocaluserwithuseridflags(char *nickname
, char *ident
, char *host
, char *realname
, char *authname
, unsigned long authid
, flag_t umodes
, flag_t accountflags
, UserMessageHandler handler
) {
77 struct irc_in_addr ipaddress
;
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
) {
89 /* This code is very similar to stuff in nick.c... */
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
;
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;
110 newuser
->ipnode
= refnode(iptree
, &ipaddress
, PATRICIA_MAXBITS
);
111 node_increment_usercount(newuser
->ipnode
);
113 newuser
->timestamp
=getnettime();
114 newuser
->shident
=NULL
;
115 newuser
->sethost
=NULL
;
117 memset(newuser
->exts
, 0, MAXNICKEXTS
* sizeof(void *));
119 if (IsOper(newuser
)) {
120 newuser
->opername
= getsstring("-", ACCOUNTLEN
);
122 newuser
->opername
= NULL
;
124 if (IsAccount(newuser
)) {
125 strncpy(newuser
->authname
,authname
,ACCOUNTLEN
);
126 newuser
->accountts
=newuser
->timestamp
;
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
;
137 newuser
->authname
[0]='\0';
138 newuser
->accountts
=0;
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");
149 sendnickmsg(newuser
);
153 umhandlers
[(currentlocalunum
&MAXLOCALUSER
)]=handler
;
156 *(gethandlebynumeric(newuser
->numeric
))=newuser
;
157 addnicktohash(newuser
);
158 triggerhook(HOOK_NICK_NEWNICK
,newuser
);
165 * This function changes the name of a given local user
168 int renamelocaluser(nick
*np
, char *newnick
) {
171 time_t timestamp
=getnettime();
176 if (strlen(newnick
) > NICKLEN
)
179 if (homeserver(np
->numeric
)!=mylongnum
)
182 if ((np2
=getnickbynick(newnick
))) {
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
);
191 /* Kill other user and drop through */
192 timestamp
=np2
->timestamp
-1;
193 killuser(NULL
, np2
, "Nick collision");
197 np
->timestamp
=timestamp
;
198 removenickfromhash(np
);
199 strncpy(np
->nick
,newnick
,NICKLEN
);
200 np
->nick
[NICKLEN
]='\0';
202 irc_send("%s N %s %d",longtonumeric(np
->numeric
,5),np
->nick
,np
->timestamp
);
203 triggerhook(HOOK_NICK_RENAME
,np
);
209 * deregisterlocaluser:
210 * This function removes the given local user from the network
213 int deregisterlocaluser(nick
*np
, char *reason
) {
218 if (np
==NULL
|| (homeserver(np
->numeric
)!=mylongnum
)) {
219 /* Non-existent user, or user not on this server */
223 if (reason
==NULL
|| *reason
=='\0') {
224 sprintf(reasonstr
,"Quit");
226 snprintf(reasonstr
,510,"Quit: %s",reason
);
232 triggerhook(HOOK_NICK_QUIT
, harg
);
235 umhandlers
[np
->numeric
&MAXLOCALUSER
]=NULL
;
238 irc_send("%s Q :%s",longtonumeric(numeric
,5),reasonstr
);
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.
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 */
255 oldhandler
= umhandlers
[np
->numeric
&MAXLOCALUSER
];
256 umhandlers
[np
->numeric
&MAXLOCALUSER
]=newhandler
;
262 * Sends details of a given local nick to the network.
265 void sendnickmsg(nick
*np
) {
267 char ipbuf
[25], operbuf
[ACCOUNTLEN
+ 5];
268 char accountbuf
[100];
270 strncpy(numericbuf
,longtonumeric(np
->numeric
,5),5);
273 if(IsOper(np
) && (serverlist
[myhub
].flags
& SMODE_OPERNAME
)) {
274 snprintf(operbuf
,sizeof(operbuf
)," %s",np
->opername
?np
->opername
->content
:"-");
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
);
284 snprintf(accountbuf
,sizeof(accountbuf
)," %s:%ld:%lu",np
->authname
,np
->accountts
,np
->auth
->userid
);
286 } else if (np
->accountts
) {
287 snprintf(accountbuf
,sizeof(accountbuf
)," %s:%ld",np
->authname
,np
->accountts
);
289 snprintf(accountbuf
,sizeof(accountbuf
)," %s",np
->authname
);
295 irc_send("%s N %s 1 %ld %s %s %s%s%s %s %s :%s",
296 mynumeric
->content
,np
->nick
,np
->timestamp
,np
->ident
,np
->host
->name
->content
,
297 printflags(np
->umodes
,umodeflags
),operbuf
,accountbuf
,iptobase64(ipbuf
,&(np
->p_ipaddr
),
298 sizeof(ipbuf
),1),numericbuf
,np
->realname
->name
->content
);
301 void sendnickburst(int hooknum
, void *arg
) {
302 /* Send nick messages for all local users */
306 nh
=servernicks
[numerictolong(mynumeric
->content
,2)];
307 for (i
=0;i
<=MAXLOCALUSER
;i
++) {
314 /* Check for a kill of a local user */
315 void checklocalkill(int hooknum
, void *arg
) {
317 nick
*target
=args
[0];
318 char *reason
=args
[1];
325 numeric
=((nick
*)target
)->numeric
;
327 if (homeserver(numeric
)==mylongnum
) {
328 if (umhandlers
[(numeric
)&MAXLOCALUSER
]!=NULL
) {
329 (umhandlers
[(numeric
)&MAXLOCALUSER
])((nick
*)target
,LU_KILLED
,myargs
);
334 int handleprivatemsgcmd(void *source
, int cargc
, char **cargv
) {
335 return handlemessageornotice(source
, cargc
, cargv
, 0);
338 int handleprivatenoticecmd(void *source
, int cargc
, char **cargv
) {
339 return handlemessageornotice(source
, cargc
, cargv
, 1);
342 /* Handle privmsg or notice command addressed to user */
343 int handlemessageornotice(void *source
, int cargc
, char **cargv
, int isnotice
) {
347 char targetnick
[NICKLEN
+1];
352 /* Should have target and message */
357 if (cargv
[0][0]=='#' || cargv
[0][0]=='+') {
358 /* Channel message/notice */
362 if ((sender
=getnickbynumericstr((char *)source
))==NULL
) {
363 Error("localuser",ERR_WARNING
,"PRIVMSG from non existant user %s",(char *)source
);
367 /* Check for a "secure" message (foo@bar) */
369 for (i
=0,ch
=cargv
[0];(i
<=NICKLEN
) && (*ch
);i
++,ch
++) {
379 if (!foundat
) { /* Didn't find an @ sign, assume it's a numeric */
380 if ((target
=getnickbynumericstr(cargv
[0]))==NULL
) {
381 Error("localuser",ERR_DEBUG
,"Couldn't find target for %s",cargv
[0]);
384 } else { /* Did find @, do a lookup by nick */
385 if ((target
=getnickbynick(targetnick
))==NULL
) {
386 Error("localuser",ERR_DEBUG
,"Couldn't find target for %s",cargv
[0]);
387 irc_send(":%s 401 %s %s :No such nick",myserver
->content
,sender
->nick
,cargv
[0]);
392 if (homeserver(target
->numeric
)!=mylongnum
) {
393 Error("localuser",ERR_WARNING
,"Got message/notice for someone not on my server");
394 irc_send(":%s 401 %s %s :No such nick",myserver
->content
,sender
->nick
,cargv
[0]);
398 if (foundat
&& !IsService(target
)) {
399 Error("localuser",ERR_DEBUG
,"Received secure message for %s, but user is not a service",cargv
[0]);
400 irc_send(":%s 401 %s %s :No such nick",myserver
->content
,sender
->nick
,cargv
[0]);
404 if (umhandlers
[(target
->numeric
)&MAXLOCALUSER
]==NULL
) {
405 /* No handler anyhow.. */
409 /* Dispatch the message. */
410 nargs
[0]=(void *)sender
;
411 nargs
[1]=(void *)cargv
[1];
412 (umhandlers
[(target
->numeric
)&MAXLOCALUSER
])(target
,isnotice
?LU_PRIVNOTICE
:(foundat
?LU_SECUREMSG
:LU_PRIVMSG
),nargs
);
417 /* Send message to user */
418 void sendmessagetouser(nick
*source
, nick
*target
, char *format
, ... ) {
423 longtonumeric2(source
->numeric
,5,senderstr
);
426 /* 10 bytes of numeric, 5 bytes of fixed format + terminator = 17 bytes */
427 /* So max sendable message is 495 bytes. Of course, a client won't be able
428 * to receive this.. */
430 vsnprintf(buf
,BUFSIZE
-17,format
,va
);
433 if (homeserver(target
->numeric
)!=mylongnum
)
434 irc_send("%s P %s :%s",senderstr
,longtonumeric(target
->numeric
,5),buf
);
437 /* Send messageto server, we don't check, but noones going to want to put a server pointer in anyway... */
438 void sendsecuremessagetouser(nick
*source
, nick
*target
, char *servername
, char *format
, ... ) {
443 longtonumeric2(source
->numeric
,5,senderstr
);
446 /* 10 bytes of numeric, 5 bytes of fixed format + terminator = 17 bytes */
447 /* So max sendable message is 495 bytes. Of course, a client won't be able
448 * to receive this.. */
450 vsnprintf(buf
,BUFSIZE
-17,format
,va
);
453 if (homeserver(target
->numeric
)!=mylongnum
)
454 irc_send("%s P %s@%s :%s",senderstr
,target
->nick
,servername
,buf
);
457 /* Send notice to user */
458 void sendnoticetouser(nick
*source
, nick
*target
, char *format
, ... ) {
463 longtonumeric2(source
->numeric
,5,senderstr
);
466 /* 10 bytes of numeric, 5 bytes of fixed format + terminator = 17 bytes */
467 /* So max sendable message is 495 bytes. Of course, a client won't be able
468 * to receive this.. */
470 vsnprintf(buf
,BUFSIZE
-17,format
,va
);
473 if (homeserver(target
->numeric
)!=mylongnum
)
474 irc_send("%s O %s :%s",senderstr
,longtonumeric(target
->numeric
,5),buf
);
478 void killuser(nick
*source
, nick
*target
, char *format
, ... ) {
483 va_start(va
, format
);
484 vsnprintf(buf
, BUFSIZE
-17, format
, va
);
487 if (hookqueuelength
) {
488 for (pk
= pendingkilllist
; pk
; pk
= pk
->next
)
489 if (pk
->target
== target
)
492 Error("localuser", ERR_DEBUG
, "Adding pending kill for %s", target
->nick
);
493 pk
= (pendingkill
*)malloc(sizeof(pendingkill
));
496 pk
->reason
= getsstring(buf
, BUFSIZE
);
497 pk
->next
= pendingkilllist
;
498 pendingkilllist
= pk
;
500 _killuser(source
, target
, buf
);
504 void sethostuser(nick
*target
, char *ident
, char *host
) {
505 irc_send("%s SH %s %s %s", mynumeric
->content
, longtonumeric(target
->numeric
, 5), ident
, host
);
508 void _killuser(nick
*source
, nick
*target
, char *reason
) {
510 char sourcestring
[HOSTLEN
+NICKLEN
+3];
513 /* If we have a null nick, use the server.. */
514 strcpy(senderstr
, mynumeric
->content
);
515 strcpy(sourcestring
, myserver
->content
);
517 strcpy(senderstr
, longtonumeric(source
->numeric
,5));
518 sprintf(sourcestring
,"%s!%s",source
->host
->name
->content
, source
->nick
);
521 irc_send("%s D %s :%s (%s)",senderstr
,longtonumeric(target
->numeric
,5),sourcestring
,reason
);
525 void clearpendingkills(int hooknum
, void *arg
) {
528 pk
= pendingkilllist
;
530 pendingkilllist
= pk
->next
;
533 Error("localuser", ERR_DEBUG
, "Processing pending kill for %s", pk
->target
->nick
);
534 _killuser(pk
->source
, pk
->target
, pk
->reason
->content
);
537 freesstring(pk
->reason
);
539 pk
= pendingkilllist
;
543 void checkpendingkills(int hooknum
, void *arg
) {
544 nick
*np
= (nick
*)arg
;
547 for (pk
=pendingkilllist
; pk
; pk
= pk
->next
) {
548 if (pk
->source
== np
) {
549 Error("localuser", ERR_INFO
, "Pending kill source %s got deleted, NULL'ing source for pending kill", np
->nick
);
552 if (pk
->target
== np
) {
553 Error("localuser", ERR_INFO
, "Pending kill target %s got deleted, NULL'ing target for pending kill", np
->nick
);
560 void localusersetaccountwithuseridflagsts(nick
*np
, char *accname
, unsigned long accid
, flag_t accountflags
, time_t authTS
) {
562 Error("localuser",ERR_WARNING
,"Tried to set account on user %s already authed", np
->nick
);
567 strncpy(np
->authname
, accname
, ACCOUNTLEN
);
568 np
->authname
[ACCOUNTLEN
]='\0';
569 np
->accountts
=authTS
?authTS
:getnettime();
572 np
->auth
=findorcreateauthname(accid
, accname
);
573 np
->auth
->usercount
++;
574 np
->nextbyauthname
=np
->auth
->nicks
;
576 np
->auth
->flags
=accountflags
;
583 if (np
->auth
->flags
) {
584 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
);
586 irc_send("%s AC %s %s %ld %lu",mynumeric
->content
, longtonumeric(np
->numeric
,5), np
->authname
, np
->accountts
, np
->auth
->userid
);
589 irc_send("%s AC %s %s %ld",mynumeric
->content
, longtonumeric(np
->numeric
,5), np
->authname
, np
->accountts
);
593 triggerhook(HOOK_NICK_ACCOUNT
, np
);
596 void localusersetumodes(nick
*np
, flag_t newmodes
) {
598 irc_send("%s M %s %s", longtonumeric(np
->numeric
,5), np
->nick
, printflagdiff(np
->umodes
, newmodes
, umodeflags
));
601 np
->umodes
= newmodes
;