]> jfr.im git - irc/quakenet/newserv.git/blobdiff - trusts/formats.c
trust search ast
[irc/quakenet/newserv.git] / trusts / formats.c
index a3397df726aa65d29fd8acc4ebf5b780c81ae529..47f1d053d8c45278404dc5f077d1a9986af41d7d 100644 (file)
@@ -1,6 +1,10 @@
 #include <stdio.h>
 #include <stdint.h>
 #include <time.h>
+#include <string.h>
+#include <stdlib.h>
+#include "../lib/strlfunc.h"
+#include "trusts.h"
 
 int trusts_parsecidr(const char *host, uint32_t *ip, short *mask) {
   unsigned int octet1 = 0, octet2 = 0, octet3 = 0, octet4 = 0, umask = 32;
@@ -69,3 +73,177 @@ char *trusts_timetostr(time_t t) {
   return buf;
 }
 
+char *dumpth(trusthost *th, int oformat) {
+  static char buf[512];
+
+  if(oformat) {
+    snprintf(buf, sizeof(buf), "#%u,%s,%u,%u,%jd", th->group->id, trusts_cidr2str(th->ip, th->mask), th->count, th->maxusage, (intmax_t)th->lastseen);
+  } else {
+    snprintf(buf, sizeof(buf), "%u,%s,%u,%u,%jd", th->group->id, trusts_cidr2str(th->ip, th->mask), th->id, th->maxusage, (intmax_t)th->lastseen);
+  }
+
+  return buf;
+}
+
+char *dumptg(trustgroup *tg, int oformat) {
+  static char buf[512];
+
+  if(oformat) {
+    snprintf(buf, sizeof(buf), "#%u,%s,%u,%u,%d,%u,%u,%jd,%jd,%jd,%s,%s,%s", tg->id, tg->name->content, tg->count, tg->trustedfor, tg->mode, tg->maxperident, tg->maxusage, (intmax_t)tg->expires, (intmax_t)tg->lastseen, (intmax_t)tg->lastmaxusereset, tg->createdby->content, tg->contact->content, tg->comment->content);
+  } else {
+    snprintf(buf, sizeof(buf), "%u,%s,%u,%d,%u,%u,%jd,%jd,%jd,%s,%s,%s", tg->id, tg->name->content, tg->trustedfor, tg->mode, tg->maxperident, tg->maxusage, (intmax_t)tg->expires, (intmax_t)tg->lastseen, (intmax_t)tg->lastmaxusereset, tg->createdby->content, tg->contact->content, tg->comment->content);
+  }
+
+  return buf;
+}
+
+int parsetg(char *buf, trustgroup *tg, int oformat) {
+  char *line, *createdby, *contact, *comment, *name, *id;
+  unsigned long expires, lastseen, lastmaxusereset;
+  char xbuf[1024];
+  int pos;
+
+/* #id,ticket35153,14,20,1,1,17,1879854575,1222639249,0,nterfacer,Qwhois&2120764,Non-Commercial Bouncer (Created by: doomie)
+      ,name       ,current
+                     ,trustedfor
+                        ,mode
+                          ,maxperident
+                            ,maxusage
+                               ,expires  ,lastseen   ,lastmaxusereset
+                                                       ,createdby,contact       ,comment
+*/
+  int r;
+
+  strlcpy(xbuf, buf, sizeof(xbuf));
+  line = xbuf;
+
+  if(!*line)
+    return 0;
+
+  if(oformat) {
+    if(line[0] != '#')
+      return 0;
+    line++;
+  }
+
+  id = line;
+  line = strchr(xbuf, ',');
+  if(!line)
+    return 0;
+  *line++ = '\0';
+
+  if(oformat && (id[0] == '#'))
+    id++;
+
+  tg->id = strtoul(id, NULL, 10);
+  if(!tg->id)
+    return 0;
+
+  name = line;
+  line = strchr(line, ',');
+  if(!line)
+    return 0;
+  *line++ = '\0';
+
+  if(oformat) {
+    r = sscanf(line, "%*u,%u,%u,%u,%u,%lu,%lu,%lu,%n",
+               /*current, */ &tg->trustedfor, &tg->mode, &tg->maxperident,
+               &tg->maxusage, &expires, &lastseen, &lastmaxusereset, &pos);
+  } else {
+    r = sscanf(line, "%u,%u,%u,%u,%lu,%lu,%lu,%n",
+               &tg->trustedfor, &tg->mode, &tg->maxperident,
+               &tg->maxusage, &expires, &lastseen, &lastmaxusereset, &pos);
+  }
+  if(r != 7)
+    return 0;
+
+  tg->expires = (time_t)expires;
+  tg->lastseen = (time_t)lastseen;
+  tg->lastmaxusereset = (time_t)lastmaxusereset;
+
+  createdby = &line[pos];
+  contact = strchr(createdby, ',');
+  if(!contact)
+    return 0;
+  *contact++ = '\0';
+
+  comment = strchr(contact, ',');
+  if(!comment)
+    return 0;
+  *comment++ = '\0';
+
+  tg->name = getsstring(name, TRUSTNAMELEN);
+  tg->createdby = getsstring(createdby, CREATEDBYLEN);
+  tg->comment = getsstring(comment, COMMENTLEN);
+  tg->contact = getsstring(contact, CONTACTLEN);
+  if(!tg->name || !tg->createdby || !tg->comment || !tg->contact) {
+    freesstring(tg->name);
+    freesstring(tg->createdby);
+    freesstring(tg->comment);
+    freesstring(tg->contact);
+    return 0;
+  }
+
+  return 1;
+}
+
+int parseth(char *line, trusthost *th, unsigned int *tgid, int oformat) {
+  unsigned long lastseen;
+  char *ip, xbuf[1024], *id;
+
+/* #id,213.230.192.128/26,20,23,1222732944
+       ip                ,cur,max,lastseen */
+
+  strlcpy(xbuf, line, sizeof(xbuf));
+  id = line = xbuf;
+
+  line = strchr(line, ',');
+  if(!line)
+    return 0;
+  *line++ = '\0';
+
+  if(oformat && (id[0] == '#'))
+    id++;
+
+  *tgid = strtoul(id, NULL, 10);
+  if(!*tgid)
+    return 0;
+
+  ip = line;
+  line = strchr(line, ',');
+  if(!line)
+    return 0;
+  *line++ = '\0';
+
+  if(!trusts_str2cidr(ip, &th->ip, &th->mask))
+    return 0;
+
+  if(oformat) {
+    if(sscanf(line, "%*u,%u,%lu", /*current, */&th->maxusage, &lastseen) != 2)
+      return 0;
+  } else {
+    if(sscanf(line, "%u,%u,%lu", &th->id, &th->maxusage, &lastseen) != 3)
+      return 0;
+  }
+
+  th->lastseen = (time_t)lastseen;
+
+  return 1;
+}
+
+char *rtrim(char *buf) {
+  static char obuf[1024];
+  size_t len = strlcpy(obuf, buf, sizeof(obuf));
+
+  if((len < sizeof(obuf)) && (len > 0)) {
+    size_t i;
+    for(i=len-1;i>=0;i--) {
+      if(obuf[i] != ' ')
+        break;
+
+      obuf[i] = '\0';
+    }
+  }
+
+  return obuf;
+}