]> jfr.im git - irc/quakenet/newserv.git/blobdiff - localuser/localuser.c
Add opername support to newserv, also add displaying of oper names in noperserv warni...
[irc/quakenet/newserv.git] / localuser / localuser.c
index 7cb6b4a80418efcf587daf4914e6f85e283b4732..463918f4eaf1f446d2d5018cab4768709ec420ca 100644 (file)
@@ -2,20 +2,35 @@
 
 #include "../nick/nick.h"
 #include "../lib/base64.h"
+#include "../lib/sstring.h"
 #include "../irc/irc.h"
 #include "../irc/irc_config.h"
 #include "../core/hooks.h"
 #include "../core/error.h"
+#include "../lib/version.h"
 #include "localuser.h"
 
 #include <string.h>
 #include <stdarg.h>
 #include <stdio.h>
 
+MODULE_VERSION("");
+
 int currentlocalunum;
 UserMessageHandler umhandlers[MAXLOCALUSER+1];
 
+typedef struct pendingkill {
+  nick *source, *target;
+  sstring *reason;
+  struct pendingkill *next;
+} pendingkill;
+
+pendingkill *pendingkilllist;
+
 void checklocalkill(int hooknum, void *nick);
+void clearpendingkills(int hooknum, void *arg);
+void checkpendingkills(int hooknum, void *arg);
+void _killuser(nick *source, nick *target, char *reason);
 
 void _init() {
   int i;
@@ -24,21 +39,43 @@ void _init() {
     umhandlers[i]=NULL;
   }
   currentlocalunum=1;
+  pendingkilllist=NULL;
   registerhook(HOOK_IRC_SENDBURSTNICKS,&sendnickburst);
-  registerhook(HOOK_NICK_LOSTNICK,&checklocalkill);
+  registerhook(HOOK_NICK_KILL,&checklocalkill);
+  registerhook(HOOK_NICK_LOSTNICK,&checkpendingkills); /* CHECK ME -> should this hook KILL or LOSTNICK or BOTH */
+  registerhook(HOOK_CORE_ENDOFHOOKSQUEUE,&clearpendingkills);
   registerserverhandler("P",&handleprivatemsgcmd,2);
   registerserverhandler("O",&handleprivatenoticecmd, 2);
 }
 
+void _fini() {
+  pendingkill *pk;
+
+  for (pk=pendingkilllist;pk;pk=pendingkilllist) {
+    pendingkilllist = pk->next;
+    freesstring(pk->reason);
+    free(pk);
+  }
+
+  deregisterhook(HOOK_IRC_SENDBURSTNICKS,&sendnickburst);
+  deregisterhook(HOOK_NICK_KILL,&checklocalkill);
+  deregisterhook(HOOK_NICK_LOSTNICK,&checkpendingkills); /* CHECK ME -> should this hook KILL or LOSTNICK or BOTH */
+  deregisterhook(HOOK_CORE_ENDOFHOOKSQUEUE,&clearpendingkills);
+  
+  deregisterserverhandler("P",&handleprivatemsgcmd);
+  deregisterserverhandler("O",&handleprivatenoticecmd);
+}
+
 /*
- * registerlocaluser:
+ * registerlocaluserwithuseridflags:
  *  This function creates a local user, and broadcasts it's existence to the net (if connected).
  */
 
-nick *registerlocaluser(char *nickname, char *ident, char *host, char *realname, char *authname, flag_t umodes, UserMessageHandler handler) {
+nick *registerlocaluserwithuseridflags(char *nickname, char *ident, char *host, char *realname, char *authname, unsigned long authid, flag_t umodes, flag_t accountflags, UserMessageHandler handler) {
   int i;  
   nick *newuser,*np; 
-  
+  struct irc_in_addr ipaddress;
   i=0;
   currentlocalunum=(currentlocalunum+1)%262142;
   while (servernicks[numerictolong(mynumeric->content,2)][currentlocalunum&MAXLOCALUSER]!=NULL) {
@@ -63,7 +100,16 @@ nick *registerlocaluser(char *nickname, char *ident, char *host, char *realname,
   newuser->nextbyrealname=newuser->realname->nicks;
   newuser->realname->nicks=newuser;
   newuser->umodes=umodes;
-  newuser->ipaddress=(127<<24)+(1<<8)+((currentlocalunum%253)+1); /* Make it look like a valid addr on 127.0.1.0/24 */
+  
+  memset(&ipaddress, 0, sizeof(ipaddress));
+  ((unsigned short *)(ipaddress.in6_16))[5] = 65535;
+  ((unsigned short *)(ipaddress.in6_16))[6] = 127;
+  ((unsigned char *)(ipaddress.in6_16))[14] = 1;
+  ((unsigned char *)(ipaddress.in6_16))[15] = (currentlocalunum%253)+1;
+
+  newuser->ipnode = refnode(iptree, &ipaddress, PATRICIA_MAXBITS);
+  node_increment_usercount(newuser->ipnode);
+
   newuser->timestamp=getnettime();
   newuser->shident=NULL;
   newuser->sethost=NULL;
@@ -72,8 +118,20 @@ nick *registerlocaluser(char *nickname, char *ident, char *host, char *realname,
 
   if (IsAccount(newuser)) {
     strncpy(newuser->authname,authname,ACCOUNTLEN);
+    newuser->accountts=newuser->timestamp;
+    if (authid) {
+      newuser->auth=findorcreateauthname(authid, newuser->authname);
+      newuser->auth->usercount++;
+      newuser->nextbyauthname=newuser->auth->nicks;
+      newuser->auth->nicks=newuser;
+      newuser->auth->flags=accountflags;
+    } else {
+      newuser->auth=NULL;
+    }
   } else {
     newuser->authname[0]='\0';
+    newuser->accountts=0;
+    newuser->auth=NULL;
   }
   
   if (connected) {
@@ -104,6 +162,7 @@ nick *registerlocaluser(char *nickname, char *ident, char *host, char *realname,
 
 int renamelocaluser(nick *np, char *newnick) {
   nick *np2;
+  char ipbuf[25];
   time_t timestamp=getnettime();
   
   if (!np)
@@ -120,8 +179,8 @@ int renamelocaluser(nick *np, char *newnick) {
       /* Case only name change */
       strncpy(np->nick,newnick,NICKLEN);
       np->nick[NICKLEN]='\0';
-      irc_send("%s N %s %d",longtonumeric(np->numeric,5),np->nick,np->timestamp);
-      triggerhook(HOOK_NICK_RENAME,np);        
+      irc_send("%s N %s %d",iptobase64(ipbuf, &(np->p_ipaddr), sizeof(ipbuf), 1),np->nick,np->timestamp);
+      triggerhook(HOOK_NICK_RENAME,np);     
       return 0;
     } else {
       /* Kill other user and drop through */
@@ -147,8 +206,9 @@ int renamelocaluser(nick *np, char *newnick) {
  */
  
 int deregisterlocaluser(nick *np, char *reason) {
-  int defaultreason=0;
   long numeric;
+  char reasonstr[512];
+  void *harg[2];
   
   if (np==NULL || (homeserver(np->numeric)!=mylongnum)) {
     /* Non-existent user, or user not on this server */
@@ -156,14 +216,21 @@ int deregisterlocaluser(nick *np, char *reason) {
   }
   
   if (reason==NULL || *reason=='\0') {
-    defaultreason=1;
+    sprintf(reasonstr,"Quit");
+  } else {
+    snprintf(reasonstr,510,"Quit: %s",reason);
   }
   
+  harg[0]=np;
+  harg[1]=reasonstr;
+  
+  triggerhook(HOOK_NICK_QUIT, harg);
+  
   numeric=np->numeric;
   umhandlers[np->numeric&MAXLOCALUSER]=NULL;
   deletenick(np);
   if (connected) {
-    irc_send("%s Q :Quit: %s",longtonumeric(numeric,5),(defaultreason?"Leaving":reason));
+    irc_send("%s Q :%s",longtonumeric(numeric,5),reasonstr);
   }
 
   return 0;
@@ -192,15 +259,38 @@ UserMessageHandler hooklocaluserhandler(nick *np, UserMessageHandler newhandler)
 
 void sendnickmsg(nick *np) {
   char numericbuf[6];
+  char ipbuf[25], operbuf[ACCOUNTLEN + 5];
+  char accountbuf[100];
 
   strncpy(numericbuf,longtonumeric(np->numeric,5),5);
   numericbuf[5]='\0';
-  
+
+  if(IsOper(np) && (serverlist[myhub].flags & SMODE_OPERNAME)) {
+    snprintf(operbuf,sizeof(operbuf)," %s",np->opername?np->opername->content:"-");
+  } else {
+    operbuf[0] = '\0';
+  }
+
+  if (IsAccount(np)) {
+    if (np->auth) {
+      if(np->auth->flags) {
+        snprintf(accountbuf,sizeof(accountbuf)," %s:%ld:%lu:" FLAG_T_SPECIFIER,np->authname,np->accountts,np->auth->userid,np->auth->flags);
+      } else {
+        snprintf(accountbuf,sizeof(accountbuf)," %s:%ld:%lu",np->authname,np->accountts,np->auth->userid);
+      }
+    } else if (np->accountts) {
+      snprintf(accountbuf,sizeof(accountbuf)," %s:%ld",np->authname,np->accountts);
+    } else {
+      snprintf(accountbuf,sizeof(accountbuf)," %s",np->authname);
+    }
+  } else {
+    accountbuf[0]='\0';
+  }
+
   irc_send("%s N %s 1 %ld %s %s %s%s%s %s %s :%s",
     mynumeric->content,np->nick,np->timestamp,np->ident,np->host->name->content,
-    printflags(np->umodes,umodeflags),IsAccount(np)?" ":"",
-    np->authname,longtonumeric(np->ipaddress,6),numericbuf,
-    np->realname->name->content);
+    printflags(np->umodes,umodeflags),operbuf,accountbuf,iptobase64(ipbuf,&(np->p_ipaddr),
+    sizeof(ipbuf),1),numericbuf,np->realname->name->content);
 }
 
 void sendnickburst(int hooknum, void *arg) {
@@ -217,17 +307,21 @@ void sendnickburst(int hooknum, void *arg) {
 }
 
 /* Check for a kill of a local user */
-void checklocalkill(int hooknum, void *target) {
+void checklocalkill(int hooknum, void *arg) {
+  void **args=arg;
+  nick *target=args[0];
+  char *reason=args[1];
   long numeric;
-  void *args[1];
-  
-  args[0]=NULL;
+
+  void *myargs[1];
+  myargs[0]=reason;
+
   
   numeric=((nick *)target)->numeric;
   
   if (homeserver(numeric)==mylongnum) {
     if (umhandlers[(numeric)&MAXLOCALUSER]!=NULL) {
-      (umhandlers[(numeric)&MAXLOCALUSER])((nick *)target,LU_KILLED,args);
+      (umhandlers[(numeric)&MAXLOCALUSER])((nick *)target,LU_KILLED,myargs);
     }
   }
 } 
@@ -378,9 +472,37 @@ void sendnoticetouser(nick *source, nick *target, char *format, ... ) {
 /* Kill user */
 void killuser(nick *source, nick *target, char *format, ... ) {
   char buf[BUFSIZE];
-  char senderstr[6];
-  char sourcestring[NICKLEN+USERLEN+3];
   va_list va;
+  pendingkill *pk;
+
+  va_start(va, format);
+  vsnprintf(buf, BUFSIZE-17, format, va);
+  va_end (va);
+
+  if (hookqueuelength) {
+    for (pk = pendingkilllist; pk; pk = pk->next)
+      if (pk->target == target)
+        return;
+
+    Error("localuser", ERR_DEBUG, "Adding pending kill for %s", target->nick);
+    pk = (pendingkill *)malloc(sizeof(pendingkill));
+    pk->source = source;
+    pk->target = target;
+    pk->reason = getsstring(buf, BUFSIZE);
+    pk->next = pendingkilllist;
+    pendingkilllist = pk;
+  } else {
+    _killuser(source, target, buf);
+  }
+}
+
+void sethostuser(nick *target, char *ident, char *host) {
+  irc_send("%s SH %s %s %s", mynumeric->content, longtonumeric(target->numeric, 5), ident, host);
+}
+
+void _killuser(nick *source, nick *target, char *reason) {
+  char senderstr[6];
+  char sourcestring[HOSTLEN+NICKLEN+3];
 
   if (!source) {
     /* If we have a null nick, use the server.. */
@@ -391,16 +513,46 @@ void killuser(nick *source, nick *target, char *format, ... ) {
     sprintf(sourcestring,"%s!%s",source->host->name->content, source->nick);
   }
 
-  va_start(va, format);
-  vsnprintf(buf, BUFSIZE-17, format, va);
-  va_end (va);
-
-  irc_send("%s D %s :%s (%s)",senderstr,longtonumeric(target->numeric,5),sourcestring,buf);
+  irc_send("%s D %s :%s (%s)",senderstr,longtonumeric(target->numeric,5),sourcestring,reason);
   deletenick(target);
 }
 
+void clearpendingkills(int hooknum, void *arg) {
+  pendingkill *pk;
+
+  pk = pendingkilllist;
+  while (pk) {
+    pendingkilllist = pk->next;
+
+    if (pk->target) {
+      Error("localuser", ERR_DEBUG, "Processing pending kill for %s", pk->target->nick);
+      _killuser(pk->source, pk->target, pk->reason->content);
+    }
+
+    freesstring(pk->reason);
+    free(pk);
+    pk = pendingkilllist;
+  }
+}
+
+void checkpendingkills(int hooknum, void *arg) {
+  nick *np = (nick *)arg;
+  pendingkill *pk;
+
+  for (pk=pendingkilllist; pk; pk = pk->next) {
+    if (pk->source == np) {
+      Error("localuser", ERR_INFO, "Pending kill source %s got deleted, NULL'ing source for pending kill", np->nick);
+      pk->source = NULL;
+    }
+    if (pk->target == np) {
+      Error("localuser", ERR_INFO, "Pending kill target %s got deleted, NULL'ing target for pending kill", np->nick);
+      pk->target = NULL;
+    }
+  }
+}
+
 /* Auth user */
-void localusersetaccount(nick *np, char *accname) {
+void localusersetaccountwithuseridflagsts(nick *np, char *accname, unsigned long accid, flag_t accountflags, time_t authTS) {
   if (IsAccount(np)) {
     Error("localuser",ERR_WARNING,"Tried to set account on user %s already authed", np->nick);
     return;
@@ -409,11 +561,37 @@ void localusersetaccount(nick *np, char *accname) {
   SetAccount(np);
   strncpy(np->authname, accname, ACCOUNTLEN);
   np->authname[ACCOUNTLEN]='\0';
+  np->accountts=authTS?authTS:getnettime();
+
+  if (accid) {
+    np->auth=findorcreateauthname(accid, accname);
+    np->auth->usercount++;
+    np->nextbyauthname=np->auth->nicks;
+    np->auth->nicks=np;
+    np->auth->flags=accountflags;
+  } else {
+    np->auth=NULL;
+  }
 
   if (connected) {
-    irc_send("%s AC %s %s %ld",mynumeric->content, longtonumeric(np->numeric,5), np->authname, getnettime());
+    if (np->auth) {
+      if (np->auth->flags) {
+        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);
+      } else {
+        irc_send("%s AC %s %s %ld %lu",mynumeric->content, longtonumeric(np->numeric,5), np->authname, np->accountts, np->auth->userid);
+      }
+    } else {
+      irc_send("%s AC %s %s %ld",mynumeric->content, longtonumeric(np->numeric,5), np->authname, np->accountts);
+    }
   }
 
   triggerhook(HOOK_NICK_ACCOUNT, np);
 }
 
+void localusersetumodes(nick *np, flag_t newmodes) {
+  if (connected) {
+    irc_send("%s M %s %s", longtonumeric(np->numeric,5), np->nick, printflagdiff(np->umodes, newmodes, umodeflags));
+  }
+
+  np->umodes = newmodes;
+}