]> jfr.im git - irc/quakenet/newserv.git/blobdiff - trusts/db.c
fix indentation
[irc/quakenet/newserv.git] / trusts / db.c
index 06465f2d9ce152337365faff1f14f1c7bbe3ce7b..ae4af07eb79727538b07d2c9d097985a0e2bc52e 100644 (file)
@@ -13,6 +13,7 @@ int trustsdbloaded;
 
 void createtrusttables(int migration);
 void trusts_flush(void);
+void trusts_freeall(void);
 
 void createtrusttables(int migration) {
   char *groups, *hosts;
@@ -38,15 +39,20 @@ static void flushdatabase(void *arg) {
   trusts_flush();
 }
 
+static void triggerdbloaded(void *arg) {
+  triggerhook(HOOK_TRUSTS_DB_LOADED, NULL);
+}
+
 static void loadcomplete(void) {
   /* error has already been shown */
   if(loaderror)
     return;
 
+  th_linktree();
   trustsdbloaded = 1;
   flushschedule = schedulerecurring(time(NULL) + 300, 0, 300, flushdatabase, NULL);
 
-  triggerhook(HOOK_TRUSTS_DB_LOADED, NULL);
+  scheduleoneshot(time(NULL), triggerdbloaded, NULL);
 }
 
 static void loadhosts_data(const DBAPIResult *result, void *tag) {
@@ -172,10 +178,12 @@ static void loadgroups_fini(const DBAPIResult *result, void *tag) {
 }
 
 int trusts_loaddb(void) {
-  trustsdb = dbapi2open(NULL, "trusts");
   if(!trustsdb) {
-    Error("trusts", ERR_WARNING, "Unable to connect to db -- not loaded.");
-    return 0;
+    trustsdb = dbapi2open(NULL, "trusts");
+    if(!trustsdb) {
+      Error("trusts", ERR_WARNING, "Unable to connect to db -- not loaded.");
+      return 0;
+    }
   }
 
   createtrusttables(0);
@@ -188,25 +196,76 @@ int trusts_loaddb(void) {
   return 1;
 }
 
-void trusts_closedb(void) {
+void trusts_closedb(int closeconnection) {
   if(!trustsdb)
     return;
 
-  deleteschedule(flushschedule, flushdatabase, NULL);
-  flushdatabase(NULL);
+  if(flushschedule) {
+    deleteschedule(flushschedule, flushdatabase, NULL);
+    flushschedule = NULL;
+
+    flushdatabase(NULL);
+  }
 
   trusts_freeall();
+
   trustsdbloaded = 0;
   thmaxid = tgmaxid = 0;
 
-  trustsdb->close(trustsdb);
-  trustsdb = NULL;
+  if(closeconnection) {
+    trustsdb->close(trustsdb);
+    trustsdb = NULL;
+  }
+
+  triggerhook(HOOK_TRUSTS_DB_CLOSED, NULL);
 }
 
 void th_dbupdatecounts(trusthost *th) {
-  trustsdb->squery(trustsdb, "UPDATE ? SET lastseen = ?, maxusage = ? WHERE id = ?", "Ttuus", "hosts", th->lastseen, th->maxusage, th->id);
+  trustsdb->squery(trustsdb, "UPDATE ? SET lastseen = ?, maxusage = ? WHERE id = ?", "Ttuu", "hosts", th->lastseen, th->maxusage, th->id);
 }
 
 void tg_dbupdatecounts(trustgroup *tg) {
-  trustsdb->squery(trustsdb, "UPDATE ? SET lastseen = ?, maxusage = ? WHERE id = ?", "Ttuus", "groups", tg->lastseen, tg->maxusage, tg->id);
+  trustsdb->squery(trustsdb, "UPDATE ? SET lastseen = ?, maxusage = ? WHERE id = ?", "Ttuu", "groups", tg->lastseen, tg->maxusage, tg->id);
+}
+
+trusthost *th_new(trustgroup *tg, char *host) {
+  trusthost *th, *superset, *subset;
+  u_int32_t ip, mask;
+
+  /* ugh */
+  if(!trusts_str2cidr(host, &ip, &mask))
+    return NULL;
+
+  th_getsuperandsubsets(ip, mask, &superset, &subset);
+
+  th = th_add(tg, thmaxid + 1, host, 0, 0);
+  if(!th)
+    return NULL;
+
+  thmaxid++;
+
+  trustsdb->squery(trustsdb,
+    "INSERT INTO ? (id, groupid, host, maxusage, lastseen) VALUES (?, ?, ?, ?, ?)",
+    "Tuusut", "hosts", th->id, tg->id, trusts_cidr2str(th->ip, th->mask), th->maxusage, th->lastseen
+  );
+
+  th_adjusthosts(th, subset, superset);
+
+  th_linktree();
+  return th;
+}
+
+trustgroup *tg_new(char *name, unsigned int trustedfor, int mode, unsigned int maxperident, time_t expires, char *createdby, char *contact, char *comment) {
+  trustgroup *tg = tg_add(tgmaxid + 1, name, trustedfor, mode, maxperident, 0, expires, 0, 0, createdby, contact, comment);
+  if(!tg)
+    return NULL;
+
+  tgmaxid++;
+
+  trustsdb->squery(trustsdb,
+    "INSERT INTO ? (id, name, trustedfor, mode, maxperident, maxusage, expires, lastseen, lastmaxuserreset, createdby, contact, comment) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
+    "Tusuuuutttsss", "groups", tg->id, tg->name->content, tg->trustedfor, tg->mode, tg->maxperident, tg->maxusage, tg->expires, tg->lastseen, tg->lastmaxuserreset, tg->createdby->content, tg->contact->content, tg->comment->content
+  );
+
+  return tg;
 }