]> jfr.im git - irc/quakenet/newserv.git/blobdiff - localuser/localuser.c
Merge pull request #1 from meeb/meeb
[irc/quakenet/newserv.git] / localuser / localuser.c
index 94062d0429a2e2d174d4f313e4ef0fe0807e7e01..5d36358d9441840724a8371e0027f25e32780db5 100644 (file)
@@ -13,6 +13,7 @@
 #include <string.h>
 #include <stdarg.h>
 #include <stdio.h>
+#include <inttypes.h>
 
 MODULE_VERSION("");
 
@@ -67,14 +68,14 @@ void _fini() {
 }
 
 /*
- * registerlocaluserwithuseridflags:
+ * registerlocaluserflagsip:
  *  This function creates a local user, and broadcasts it's existence to the net (if connected).
  */
 
-nick *registerlocaluserwithuseridflags(char *nickname, char *ident, char *host, char *realname, char *authname, unsigned long authid, flag_t umodes, flag_t accountflags, UserMessageHandler handler) {
+nick *registerlocaluserflagsip(char *nickname, char *ident, char *host, char *realname, char *authname, unsigned long authid, flag_t accountflags, flag_t umodes, struct irc_in_addr *ipaddress, UserMessageHandler handler) {
   int i;  
   nick *newuser,*np; 
-  struct irc_in_addr ipaddress;
+  struct irc_in_addr tmpipaddress;
  
   i=0;
   currentlocalunum=(currentlocalunum+1)%262142;
@@ -100,40 +101,63 @@ nick *registerlocaluserwithuseridflags(char *nickname, char *ident, char *host,
   newuser->nextbyrealname=newuser->realname->nicks;
   newuser->realname->nicks=newuser;
   newuser->umodes=umodes;
-  newuser->accountflags=accountflags;
-  
-  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);
+  if (!ipaddress) {
+    ipaddress = &tmpipaddress;
+
+    memset(ipaddress, 0, sizeof(struct irc_in_addr));
+    ((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;
+  }
+
+  memcpy(&newuser->ipaddress, ipaddress, sizeof(struct irc_in_addr));
+
+  newuser->ipnode = refnode(iptree, ipaddress, PATRICIA_MAXBITS);
   node_increment_usercount(newuser->ipnode);
 
   newuser->timestamp=getnettime();
   newuser->shident=NULL;
   newuser->sethost=NULL;
+  newuser->away=NULL;
   newuser->marker=0;
   memset(newuser->exts, 0, MAXNICKEXTS * sizeof(void *));
 
+  if (IsOper(newuser)) {
+    newuser->opername = getsstring("-", ACCOUNTLEN);
+  } else {
+    newuser->opername = NULL;
+  }
+
+  newuser->accountts=0;
+  newuser->auth=NULL;
+  newuser->authname=NULLAUTHNAME;
   if (IsAccount(newuser)) {
-    strncpy(newuser->authname,authname,ACCOUNTLEN);
     newuser->accountts=newuser->timestamp;
     if (authid) {
-      newuser->auth=findorcreateauthname(authid);
+      newuser->auth=findorcreateauthname(authid, authname);
+      newuser->authname=newuser->auth->name;
       newuser->auth->usercount++;
       newuser->nextbyauthname=newuser->auth->nicks;
       newuser->auth->nicks=newuser;
+      newuser->auth->flags=accountflags;
     } else {
-      newuser->auth=NULL;
+      /*
+         this is done for three reasons:
+         1: so I don't have to change 500 pieces of code
+         2: so services can be authed with special reserved ids
+         3: saves space over the old authname per user method
+       */
+      newuser->authname=malloc(strlen(authname) + 1);
+      strcpy(newuser->authname,authname);
     }
-  } else {
-    newuser->authname[0]='\0';
-    newuser->accountts=0;
-    newuser->auth=NULL;
   }
-  
+
+  newuser->cloak_count = 0;
+  newuser->cloak_extra = NULL;
+  newuser->message = NULL;
+
   if (connected) {
     /* Check for nick collision */
     if ((np=getnickbynick(nickname))!=NULL) {
@@ -164,7 +188,9 @@ int renamelocaluser(nick *np, char *newnick) {
   nick *np2;
   char ipbuf[25];
   time_t timestamp=getnettime();
-  
+  void *harg[2];
+  char oldnick[NICKLEN+1];
+
   if (!np)
     return -1;
   
@@ -174,13 +200,18 @@ int renamelocaluser(nick *np, char *newnick) {
   if (homeserver(np->numeric)!=mylongnum) 
     return -1;
 
+  strncpy(oldnick,np->nick,NICKLEN);
+  oldnick[NICKLEN]='\0';
+  harg[0]=(void *)np;
+  harg[1]=(void *)oldnick;
+
   if ((np2=getnickbynick(newnick))) {
     if (np2==np) {
       /* Case only name change */
       strncpy(np->nick,newnick,NICKLEN);
       np->nick[NICKLEN]='\0';
-      irc_send("%s N %s %d",iptobase64(ipbuf, &(np->p_ipaddr), sizeof(ipbuf), 1),np->nick,np->timestamp);
-      triggerhook(HOOK_NICK_RENAME,np);     
+      irc_send("%s N %s %jd",iptobase64(ipbuf, &(np->ipaddress), sizeof(ipbuf), 1),np->nick,(intmax_t)np->timestamp);
+      triggerhook(HOOK_NICK_RENAME,harg);     
       return 0;
     } else {
       /* Kill other user and drop through */
@@ -194,8 +225,8 @@ int renamelocaluser(nick *np, char *newnick) {
   strncpy(np->nick,newnick,NICKLEN);
   np->nick[NICKLEN]='\0';
   addnicktohash(np);
-  irc_send("%s N %s %d",longtonumeric(np->numeric,5),np->nick,np->timestamp);
-  triggerhook(HOOK_NICK_RENAME,np);
+  irc_send("%s N %s %jd",longtonumeric(np->numeric,5),np->nick,(intmax_t)np->timestamp);
+  triggerhook(HOOK_NICK_RENAME,harg);
   
   return 0;
 }
@@ -259,39 +290,35 @@ UserMessageHandler hooklocaluserhandler(nick *np, UserMessageHandler newhandler)
 
 void sendnickmsg(nick *np) {
   char numericbuf[6];
-  char ipbuf[25];
+  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';
+  }
+
+  accountbuf[0]='\0';
   if (IsAccount(np)) {
-    if(np->accountflags) {
-      irc_send("%s N %s 1 %ld %s %s %s %s:%ld:%lu:" FLAG_T_SPECIFIER " %s %s :%s",
-        mynumeric->content,np->nick,np->timestamp,np->ident,np->host->name->content,
-        printflags(np->umodes,umodeflags),np->authname,np->accountts,np->auth?np->auth->userid:0,np->accountflags,
-        iptobase64(ipbuf, &(np->p_ipaddr), sizeof(ipbuf), 1),numericbuf,np->realname->name->content);
-    } else if (np->auth) {
-        irc_send("%s N %s 1 %ld %s %s %s %s:%ld:%lu %s %s :%s",
-          mynumeric->content,np->nick,np->timestamp,np->ident,np->host->name->content,
-          printflags(np->umodes,umodeflags),np->authname,np->accountts,np->auth->userid,
-          iptobase64(ipbuf, &(np->p_ipaddr), sizeof(ipbuf), 1),numericbuf,np->realname->name->content);
-    } else if (np->accountts) {
-      irc_send("%s N %s 1 %ld %s %s %s %s:%ld %s %s :%s",
-        mynumeric->content,np->nick,np->timestamp,np->ident,np->host->name->content,
-        printflags(np->umodes,umodeflags),np->authname,np->accountts,
-        iptobase64(ipbuf, &(np->p_ipaddr), sizeof(ipbuf), 1),numericbuf,np->realname->name->content);
-    } else {
-      irc_send("%s N %s 1 %ld %s %s %s %s %s %s :%s",
-        mynumeric->content,np->nick,np->timestamp,np->ident,np->host->name->content,
-        printflags(np->umodes,umodeflags),np->authname,
-        iptobase64(ipbuf, &(np->p_ipaddr), sizeof(ipbuf), 1),numericbuf,np->realname->name->content);
+    if (np->auth) {
+      if(np->auth->flags) {
+        snprintf(accountbuf,sizeof(accountbuf)," %s:%ld:%lu:%"PRIu64,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->authname) {
+      snprintf(accountbuf,sizeof(accountbuf)," %s:%ld:0",np->authname,np->accountts);
     }
-  } else {
-    irc_send("%s N %s 1 %ld %s %s %s %s %s :%s",
-      mynumeric->content,np->nick,np->timestamp,np->ident,np->host->name->content,
-      printflags(np->umodes,umodeflags),iptobase64(ipbuf, &(np->p_ipaddr), sizeof(ipbuf), 1),
-      numericbuf,np->realname->name->content);
   }
+
+  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),operbuf,accountbuf,iptobase64(ipbuf,&(np->ipaddress),
+    sizeof(ipbuf),1),numericbuf,np->realname->name->content);
 }
 
 void sendnickburst(int hooknum, void *arg) {
@@ -342,7 +369,7 @@ int handlemessageornotice(void *source, int cargc, char **cargv, int isnotice) {
   char *ch;
   char targetnick[NICKLEN+1];
   int foundat;
-  void *nargs[2];
+  void *nargs[3];
   int i;
   
   /* Should have target and message */
@@ -350,15 +377,23 @@ int handlemessageornotice(void *source, int cargc, char **cargv, int isnotice) {
     return CMD_OK;
   }
 
-  if (cargv[0][0]=='#' || cargv[0][0]=='+') {
-    /* Channel message/notice */
-    return CMD_OK;
-  }
-  
   if ((sender=getnickbynumericstr((char *)source))==NULL) {
     Error("localuser",ERR_WARNING,"PRIVMSG from non existant user %s",(char *)source);
     return CMD_OK;
   }
+
+  freesstring(sender->message);
+  sender->message = getsstring(cargv[1], 512);
+
+  nargs[0] = sender;
+  nargs[1] = cargv[1];
+  nargs[2] = (void *)(uintptr_t)isnotice;
+  triggerhook(HOOK_NICK_MESSAGE, nargs);
+
+  if (cargv[0][0]=='#' || cargv[0][0]=='+') {
+    /* Channel message/notice */
+    return CMD_OK;
+  }
   
   /* Check for a "secure" message (foo@bar) */
   foundat=0;
@@ -504,6 +539,8 @@ void sethostuser(nick *target, char *ident, char *host) {
 void _killuser(nick *source, nick *target, char *reason) {
   char senderstr[6];
   char sourcestring[HOSTLEN+NICKLEN+3];
+  char reasonstr[512];
+  void *args[2];
 
   if (!source) {
     /* If we have a null nick, use the server.. */
@@ -514,7 +551,15 @@ void _killuser(nick *source, nick *target, char *reason) {
     sprintf(sourcestring,"%s!%s",source->host->name->content, source->nick);
   }
 
-  irc_send("%s D %s :%s (%s)",senderstr,longtonumeric(target->numeric,5),sourcestring,reason);
+  snprintf(reasonstr,512,"%s (%s)",sourcestring,reason);
+  reasonstr[511]='\0';
+
+  irc_send("%s D %s :%s",senderstr,longtonumeric(target->numeric,5),reasonstr);
+  
+  args[0]=target;
+  args[1]=reasonstr;
+  triggerhook(HOOK_NICK_KILL, args);
+
   deletenick(target);
 }
 
@@ -552,37 +597,44 @@ void checkpendingkills(int hooknum, void *arg) {
   }
 }
 
-/* Auth user */
-void localusersetaccountwithuseridflagsts(nick *np, char *accname, unsigned long accid, flag_t accountflags, time_t authTS) {
+void sendaccountmessage(nick *np) {
+  if (connected && IsAccount(np)) {
+    if (np->auth) {
+      if (np->auth->flags) {
+        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);
+      } 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 0",mynumeric->content, longtonumeric(np->numeric,5), np->authname, np->accountts);
+    }
+  }
+}
+
+/* Auth user, don't use to set flags after authing */
+void localusersetaccount(nick *np, char *accname, unsigned long accid, u_int64_t accountflags, time_t authTS) {
   if (IsAccount(np)) {
     Error("localuser",ERR_WARNING,"Tried to set account on user %s already authed", np->nick);
     return;
   }
 
   SetAccount(np);
-  strncpy(np->authname, accname, ACCOUNTLEN);
-  np->authname[ACCOUNTLEN]='\0';
   np->accountts=authTS?authTS:getnettime();
-  np->accountflags=accountflags;
 
   if (accid) {
-    np->auth=findorcreateauthname(accid);
+    np->auth=findorcreateauthname(accid, accname);
     np->auth->usercount++;
+    np->authname=np->auth->name;
     np->nextbyauthname=np->auth->nicks;
     np->auth->nicks=np;
+    np->auth->flags=accountflags;
   } else {
     np->auth=NULL;
+    np->authname=malloc(strlen(accname) + 1);
+    strcpy(np->authname,accname);
   }
 
-  if (connected) {
-    if (np->accountflags) {
-      irc_send("%s AC %s %s %ld %lu %lu",mynumeric->content, longtonumeric(np->numeric,5), np->authname, np->accountts, np->auth?np->auth->userid:0, np->accountflags);
-    } else if (np->auth) {
-      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);
-    }
-  }
+  sendaccountmessage(np);
 
   triggerhook(HOOK_NICK_ACCOUNT, np);
 }
@@ -594,3 +646,35 @@ void localusersetumodes(nick *np, flag_t newmodes) {
 
   np->umodes = newmodes;
 }
+
+void localusersetaccountflags(authname *anp, u_int64_t accountflags) {
+  void *arg[2];
+  nick *np;
+  u_int64_t oldflags = anp->flags;
+
+  arg[0] = anp;
+  arg[1] = &oldflags;
+  anp->flags = accountflags;
+
+  for(np=anp->nicks;np;np=np->nextbyauthname)
+    sendaccountmessage(np);
+
+  triggerhook(HOOK_AUTH_FLAGSUPDATED, arg);
+}
+
+void localuseraddcloaktarget(nick *np, nick *target) {
+  char snumeric[10], tnumeric[10];
+
+  strcpy(snumeric, longtonumeric(np->numeric,5));
+  strcpy(tnumeric, longtonumeric(target->numeric,5));
+
+  irc_send("%s CA %s", snumeric, tnumeric);
+
+  addcloaktarget(np, target);
+}
+
+void localuserclearcloaktargets(nick *np) {
+  irc_send("%s CU", longtonumeric(np->numeric,5));
+  clearcloaktargets(np);
+}
+