]> jfr.im git - irc/quakenet/newserv.git/blame - localuser/localuser.c
Sync Z: iptrie code, spamscan2, disable event logging from regexgline to db, hook...
[irc/quakenet/newserv.git] / localuser / localuser.c
CommitLineData
c86edd1d
Q
1/* localuser.c */
2
3#include "../nick/nick.h"
4#include "../lib/base64.h"
526e7c1d 5#include "../lib/sstring.h"
c86edd1d
Q
6#include "../irc/irc.h"
7#include "../irc/irc_config.h"
8#include "../core/hooks.h"
9#include "../core/error.h"
87698d77 10#include "../lib/version.h"
c86edd1d
Q
11#include "localuser.h"
12
13#include <string.h>
14#include <stdarg.h>
15#include <stdio.h>
16
70b0a4e5 17MODULE_VERSION("");
87698d77 18
c86edd1d
Q
19int currentlocalunum;
20UserMessageHandler umhandlers[MAXLOCALUSER+1];
21
526e7c1d
P
22typedef struct pendingkill {
23 nick *source, *target;
24 sstring *reason;
25 struct pendingkill *next;
26} pendingkill;
27
28pendingkill *pendingkilllist;
29
c86edd1d 30void checklocalkill(int hooknum, void *nick);
526e7c1d
P
31void clearpendingkills(int hooknum, void *arg);
32void checkpendingkills(int hooknum, void *arg);
33void _killuser(nick *source, nick *target, char *reason);
c86edd1d
Q
34
35void _init() {
36 int i;
37
38 for (i=0;i<=MAXLOCALUSER;i++) {
39 umhandlers[i]=NULL;
40 }
41 currentlocalunum=1;
526e7c1d 42 pendingkilllist=NULL;
c86edd1d
Q
43 registerhook(HOOK_IRC_SENDBURSTNICKS,&sendnickburst);
44 registerhook(HOOK_NICK_LOSTNICK,&checklocalkill);
526e7c1d
P
45 registerhook(HOOK_NICK_LOSTNICK,&checkpendingkills);
46 registerhook(HOOK_CORE_ENDOFHOOKSQUEUE,&clearpendingkills);
c86edd1d
Q
47 registerserverhandler("P",&handleprivatemsgcmd,2);
48 registerserverhandler("O",&handleprivatenoticecmd, 2);
49}
50
51/*
52 * registerlocaluser:
53 * This function creates a local user, and broadcasts it's existence to the net (if connected).
54 */
55
56nick *registerlocaluser(char *nickname, char *ident, char *host, char *realname, char *authname, flag_t umodes, UserMessageHandler handler) {
57 int i;
58 nick *newuser,*np;
526e7c1d
P
59 struct irc_in_addr ipaddress;
60
c86edd1d 61 i=0;
95ee3dac 62 currentlocalunum=(currentlocalunum+1)%262142;
c86edd1d
Q
63 while (servernicks[numerictolong(mynumeric->content,2)][currentlocalunum&MAXLOCALUSER]!=NULL) {
64 /* Numeric 262143 on our server is used for "nouser" by the channels module, so cannot be allocated */
65 currentlocalunum=(currentlocalunum+1)%262142;
66 if (++i>MAXLOCALUSER) {
67 return NULL;
68 }
69 }
70
71 /* This code is very similar to stuff in nick.c... */
72 newuser=newnick();
73 newuser->numeric=(numerictolong(mynumeric->content,2)<<18)|(currentlocalunum);
74 strncpy(newuser->nick,nickname,NICKLEN);
75 newuser->nick[NICKLEN]='\0';
76 strncpy(newuser->ident,ident,USERLEN);
77 newuser->ident[USERLEN]='\0';
78 newuser->host=findorcreatehost(host);
79 newuser->realname=findorcreaterealname(realname);
80 newuser->nextbyhost=newuser->host->nicks;
81 newuser->host->nicks=newuser;
82 newuser->nextbyrealname=newuser->realname->nicks;
83 newuser->realname->nicks=newuser;
84 newuser->umodes=umodes;
526e7c1d
P
85
86 /* XXX shroud: need to port original code */
87 memset(&ipaddress, 0, sizeof(ipaddress));
88 ((char *)ipaddress.in6_16)[15] = 1;
89
90 newuser->ipnode = refnode(iptree, &ipaddress, PATRICIA_MAXBITS);
91#if 0
c86edd1d 92 newuser->ipaddress=(127<<24)+(1<<8)+((currentlocalunum%253)+1); /* Make it look like a valid addr on 127.0.1.0/24 */
526e7c1d 93#endif
c86edd1d
Q
94 newuser->timestamp=getnettime();
95 newuser->shident=NULL;
96 newuser->sethost=NULL;
97 newuser->marker=0;
98 memset(newuser->exts, 0, MAXNICKEXTS * sizeof(void *));
99
100 if (IsAccount(newuser)) {
101 strncpy(newuser->authname,authname,ACCOUNTLEN);
102 } else {
103 newuser->authname[0]='\0';
104 }
105
106 if (connected) {
107 /* Check for nick collision */
108 if ((np=getnickbynick(nickname))!=NULL) {
109 /* Make sure we will win the collision */
110 newuser->timestamp=(np->timestamp-1);
111 killuser(NULL, np, "Nick collision");
112 }
113 sendnickmsg(newuser);
114 }
115
116 if (handler!=NULL) {
117 umhandlers[(currentlocalunum&MAXLOCALUSER)]=handler;
118 }
119
120 *(gethandlebynumeric(newuser->numeric))=newuser;
121 addnicktohash(newuser);
122 triggerhook(HOOK_NICK_NEWNICK,newuser);
123
124 return newuser;
125}
126
127/*
128 * renamelocaluser:
129 * This function changes the name of a given local user
130 */
131
132int renamelocaluser(nick *np, char *newnick) {
133 nick *np2;
526e7c1d 134 char ipbuf[25];
c86edd1d
Q
135 time_t timestamp=getnettime();
136
137 if (!np)
138 return -1;
139
140 if (strlen(newnick) > NICKLEN)
141 return -1;
142
143 if (homeserver(np->numeric)!=mylongnum)
144 return -1;
145
146 if ((np2=getnickbynick(newnick))) {
147 if (np2==np) {
148 /* Case only name change */
149 strncpy(np->nick,newnick,NICKLEN);
150 np->nick[NICKLEN]='\0';
526e7c1d
P
151 irc_send("%s N %s %d",iptobase64(ipbuf, &(np->p_ipaddr), sizeof(ipbuf), 1),np->nick,np->timestamp);
152 triggerhook(HOOK_NICK_RENAME,np);
c86edd1d
Q
153 return 0;
154 } else {
155 /* Kill other user and drop through */
156 timestamp=np2->timestamp-1;
157 killuser(NULL, np2, "Nick collision");
158 }
159 }
160
161 np->timestamp=timestamp;
162 removenickfromhash(np);
163 strncpy(np->nick,newnick,NICKLEN);
164 np->nick[NICKLEN]='\0';
165 addnicktohash(np);
166 irc_send("%s N %s %d",longtonumeric(np->numeric,5),np->nick,np->timestamp);
167 triggerhook(HOOK_NICK_RENAME,np);
168
169 return 0;
170}
171
172/*
173 * deregisterlocaluser:
174 * This function removes the given local user from the network
175 */
176
177int deregisterlocaluser(nick *np, char *reason) {
178 int defaultreason=0;
179 long numeric;
180
181 if (np==NULL || (homeserver(np->numeric)!=mylongnum)) {
182 /* Non-existent user, or user not on this server */
183 return -1;
184 }
185
186 if (reason==NULL || *reason=='\0') {
187 defaultreason=1;
188 }
189
190 numeric=np->numeric;
191 umhandlers[np->numeric&MAXLOCALUSER]=NULL;
192 deletenick(np);
193 if (connected) {
194 irc_send("%s Q :Quit: %s",longtonumeric(numeric,5),(defaultreason?"Leaving":reason));
195 }
196
197 return 0;
198}
199
c5f1f827
CP
200/*
201 * hooklocaluserhandler:
202 * This function adds a new handler to the hook chain for a local user
203 * THIS RELIES ON MODULES BEING UNLOADED IN THE CORRECT ORDER.
204 */
205UserMessageHandler hooklocaluserhandler(nick *np, UserMessageHandler newhandler) {
206 UserMessageHandler oldhandler = NULL;
207 if (np==NULL || (homeserver(np->numeric)!=mylongnum)) {
208 /* Non-existent user, or user not on this server */
209 return NULL;
210 }
211 oldhandler = umhandlers[np->numeric&MAXLOCALUSER];
212 umhandlers[np->numeric&MAXLOCALUSER]=newhandler;
213 return oldhandler;
214}
215
c86edd1d
Q
216/*
217 * sendnickmsg:
218 * Sends details of a given local nick to the network.
219 */
220
221void sendnickmsg(nick *np) {
222 char numericbuf[6];
526e7c1d 223 char ipbuf[25];
c86edd1d
Q
224
225 strncpy(numericbuf,longtonumeric(np->numeric,5),5);
226 numericbuf[5]='\0';
227
228 irc_send("%s N %s 1 %ld %s %s %s%s%s %s %s :%s",
229 mynumeric->content,np->nick,np->timestamp,np->ident,np->host->name->content,
230 printflags(np->umodes,umodeflags),IsAccount(np)?" ":"",
526e7c1d 231 np->authname,iptobase64(ipbuf, &(np->p_ipaddr), sizeof(ipbuf), 1),numericbuf,
c86edd1d
Q
232 np->realname->name->content);
233}
234
235void sendnickburst(int hooknum, void *arg) {
236 /* Send nick messages for all local users */
237 nick **nh;
238 int i;
239
240 nh=servernicks[numerictolong(mynumeric->content,2)];
241 for (i=0;i<=MAXLOCALUSER;i++) {
242 if (nh[i]!=NULL) {
243 sendnickmsg(nh[i]);
244 }
245 }
246}
247
248/* Check for a kill of a local user */
249void checklocalkill(int hooknum, void *target) {
250 long numeric;
251 void *args[1];
252
253 args[0]=NULL;
254
255 numeric=((nick *)target)->numeric;
256
257 if (homeserver(numeric)==mylongnum) {
258 if (umhandlers[(numeric)&MAXLOCALUSER]!=NULL) {
259 (umhandlers[(numeric)&MAXLOCALUSER])((nick *)target,LU_KILLED,args);
260 }
261 }
262}
263
264int handleprivatemsgcmd(void *source, int cargc, char **cargv) {
265 return handlemessageornotice(source, cargc, cargv, 0);
266}
267
268int handleprivatenoticecmd(void *source, int cargc, char **cargv) {
269 return handlemessageornotice(source, cargc, cargv, 1);
270}
271
272/* Handle privmsg or notice command addressed to user */
273int handlemessageornotice(void *source, int cargc, char **cargv, int isnotice) {
274 nick *sender;
275 nick *target;
276 char *ch;
277 char targetnick[NICKLEN+1];
278 int foundat;
279 void *nargs[2];
280 int i;
281
282 /* Should have target and message */
283 if (cargc<2) {
284 return CMD_OK;
285 }
286
287 if (cargv[0][0]=='#' || cargv[0][0]=='+') {
288 /* Channel message/notice */
289 return CMD_OK;
290 }
291
292 if ((sender=getnickbynumericstr((char *)source))==NULL) {
293 Error("localuser",ERR_WARNING,"PRIVMSG from non existant user %s",(char *)source);
294 return CMD_OK;
295 }
296
297 /* Check for a "secure" message (foo@bar) */
298 foundat=0;
299 for (i=0,ch=cargv[0];(i<=NICKLEN) && (*ch);i++,ch++) {
300 if (*ch=='@') {
301 targetnick[i]='\0';
302 foundat=1;
303 break;
304 } else {
305 targetnick[i]=*ch;
306 }
307 }
308
309 if (!foundat) { /* Didn't find an @ sign, assume it's a numeric */
310 if ((target=getnickbynumericstr(cargv[0]))==NULL) {
311 Error("localuser",ERR_DEBUG,"Couldn't find target for %s",cargv[0]);
312 return CMD_OK;
313 }
314 } else { /* Did find @, do a lookup by nick */
315 if ((target=getnickbynick(targetnick))==NULL) {
316 Error("localuser",ERR_DEBUG,"Couldn't find target for %s",cargv[0]);
bc81aa36 317 irc_send(":%s 401 %s %s :No such nick",myserver->content,sender->nick,cargv[0]);
c86edd1d
Q
318 return CMD_OK;
319 }
320 }
321
322 if (homeserver(target->numeric)!=mylongnum) {
323 Error("localuser",ERR_WARNING,"Got message/notice for someone not on my server");
bc81aa36
C
324 irc_send(":%s 401 %s %s :No such nick",myserver->content,sender->nick,cargv[0]);
325 return CMD_OK;
326 }
327
328 if (foundat && !IsService(target)) {
329 Error("localuser",ERR_DEBUG,"Received secure message for %s, but user is not a service",cargv[0]);
330 irc_send(":%s 401 %s %s :No such nick",myserver->content,sender->nick,cargv[0]);
c86edd1d
Q
331 return CMD_OK;
332 }
333
334 if (umhandlers[(target->numeric)&MAXLOCALUSER]==NULL) {
335 /* No handler anyhow.. */
336 return CMD_OK;
337 }
338
339 /* Dispatch the message. */
340 nargs[0]=(void *)sender;
341 nargs[1]=(void *)cargv[1];
342 (umhandlers[(target->numeric)&MAXLOCALUSER])(target,isnotice?LU_PRIVNOTICE:(foundat?LU_SECUREMSG:LU_PRIVMSG),nargs);
343
344 return CMD_OK;
345}
346
347/* Send message to user */
348void sendmessagetouser(nick *source, nick *target, char *format, ... ) {
349 char buf[BUFSIZE];
350 char senderstr[6];
351 va_list va;
352
353 longtonumeric2(source->numeric,5,senderstr);
354
355 va_start(va,format);
356 /* 10 bytes of numeric, 5 bytes of fixed format + terminator = 17 bytes */
357 /* So max sendable message is 495 bytes. Of course, a client won't be able
358 * to receive this.. */
359
360 vsnprintf(buf,BUFSIZE-17,format,va);
361 va_end(va);
362
363 if (homeserver(target->numeric)!=mylongnum)
364 irc_send("%s P %s :%s",senderstr,longtonumeric(target->numeric,5),buf);
365}
366
367/* Send messageto server, we don't check, but noones going to want to put a server pointer in anyway... */
368void sendsecuremessagetouser(nick *source, nick *target, char *servername, char *format, ... ) {
369 char buf[BUFSIZE];
370 char senderstr[6];
371 va_list va;
372
373 longtonumeric2(source->numeric,5,senderstr);
374
375 va_start(va,format);
376 /* 10 bytes of numeric, 5 bytes of fixed format + terminator = 17 bytes */
377 /* So max sendable message is 495 bytes. Of course, a client won't be able
378 * to receive this.. */
379
380 vsnprintf(buf,BUFSIZE-17,format,va);
381 va_end(va);
382
383 if (homeserver(target->numeric)!=mylongnum)
384 irc_send("%s P %s@%s :%s",senderstr,target->nick,servername,buf);
385}
386
387/* Send notice to user */
388void sendnoticetouser(nick *source, nick *target, char *format, ... ) {
389 char buf[BUFSIZE];
390 char senderstr[6];
391 va_list va;
392
393 longtonumeric2(source->numeric,5,senderstr);
394
395 va_start(va,format);
396 /* 10 bytes of numeric, 5 bytes of fixed format + terminator = 17 bytes */
397 /* So max sendable message is 495 bytes. Of course, a client won't be able
398 * to receive this.. */
399
400 vsnprintf(buf,BUFSIZE-17,format,va);
401 va_end(va);
402
403 if (homeserver(target->numeric)!=mylongnum)
404 irc_send("%s O %s :%s",senderstr,longtonumeric(target->numeric,5),buf);
405}
406
407/* Kill user */
408void killuser(nick *source, nick *target, char *format, ... ) {
409 char buf[BUFSIZE];
526e7c1d
P
410 va_list va;
411 pendingkill *pk;
412
413 va_start(va, format);
414 vsnprintf(buf, BUFSIZE-17, format, va);
415 va_end (va);
416
417 if (hookqueuelength) {
418 for (pk = pendingkilllist; pk; pk = pk->next)
419 if (pk->target == target)
420 return;
421
422 Error("localuser", ERR_INFO, "Adding pending kill for %s", target->nick);
423 pk = (pendingkill *)malloc(sizeof(pendingkill));
424 pk->source = source;
425 pk->target = target;
426 pk->reason = getsstring(buf, BUFSIZE);
427 pk->next = pendingkilllist;
428 pendingkilllist = pk;
429 } else {
430 _killuser(source, target, buf);
431 }
432}
433
434void _killuser(nick *source, nick *target, char *reason) {
c86edd1d
Q
435 char senderstr[6];
436 char sourcestring[NICKLEN+USERLEN+3];
c86edd1d
Q
437
438 if (!source) {
439 /* If we have a null nick, use the server.. */
440 strcpy(senderstr, mynumeric->content);
441 strcpy(sourcestring, myserver->content);
442 } else {
443 strcpy(senderstr, longtonumeric(source->numeric,5));
444 sprintf(sourcestring,"%s!%s",source->host->name->content, source->nick);
445 }
446
526e7c1d 447 irc_send("%s D %s :%s (%s)",senderstr,longtonumeric(target->numeric,5),sourcestring,reason);
c86edd1d
Q
448 deletenick(target);
449}
450
526e7c1d
P
451void clearpendingkills(int hooknum, void *arg) {
452 pendingkill *pk;
453
454 pk = pendingkilllist;
455 while (pk) {
456 pendingkilllist = pk->next;
457
458 if (pk->target) {
459 Error("localuser", ERR_INFO, "Processing pending kill for %s", pk->target->nick);
460 _killuser(pk->source, pk->target, pk->reason->content);
461 }
462
463 freesstring(pk->reason);
464 free(pk);
465 pk = pendingkilllist;
466 }
467}
468
469void checkpendingkills(int hooknum, void *arg) {
470 nick *np = (nick *)arg;
471 pendingkill *pk;
472
473 for (pk=pendingkilllist; pk; pk = pk->next) {
474 if (pk->source == np) {
475 Error("localuser", ERR_INFO, "Pending kill source %s got deleted, NULL'ing source for pending kill", np->nick);
476 pk->source = NULL;
477 }
478 if (pk->target == np) {
479 Error("localuser", ERR_INFO, "Pending kill target %s got deleted, NULL'ing target for pending kill", np->nick);
480 pk->target = NULL;
481 }
482 }
483}
484
c86edd1d
Q
485/* Auth user */
486void localusersetaccount(nick *np, char *accname) {
487 if (IsAccount(np)) {
488 Error("localuser",ERR_WARNING,"Tried to set account on user %s already authed", np->nick);
489 return;
490 }
491
492 SetAccount(np);
493 strncpy(np->authname, accname, ACCOUNTLEN);
494 np->authname[ACCOUNTLEN]='\0';
495
496 if (connected) {
497 irc_send("%s AC %s %s %ld",mynumeric->content, longtonumeric(np->numeric,5), np->authname, getnettime());
498 }
499
500 triggerhook(HOOK_NICK_ACCOUNT, np);
501}
502