]> code.citadel.org Git - citadel.git/blobdiff - citadel/serv_network.c
centralized filename calculation
[citadel.git] / citadel / serv_network.c
index e6a154dbd00a910e846afee21f0305b6adb122fb..dc6aab1743062919931c245b98b8335858c73124 100644 (file)
@@ -65,6 +65,7 @@
 #include "serv_network.h"
 #include "clientsocket.h"
 #include "file_ops.h"
+#include "citadel_dirs.h"
 
 #ifndef HAVE_SNPRINTF
 #include "snprintf.h"
@@ -363,7 +364,7 @@ void cmd_gnet(char *argbuf) {
        FILE *fp;
 
        if (CtdlAccessCheck(ac_room_aide)) return;
-       assoc_file_name(filename, sizeof filename, &CC->room, "netconfigs");
+       assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
        cprintf("%d Network settings for room #%ld <%s>\n",
                LISTING_FOLLOWS,
                CC->room.QRnumber, CC->room.QRname);
@@ -390,8 +391,8 @@ void cmd_snet(char *argbuf) {
        unbuffer_output();
 
        if (CtdlAccessCheck(ac_room_aide)) return;
-       safestrncpy(tempfilename, tmpnam(NULL), sizeof tempfilename);
-       assoc_file_name(filename, sizeof filename, &CC->room, "netconfigs");
+       CtdlMakeTempFileName(tempfilename, sizeof tempfilename);
+       assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
 
        fp = fopen(tempfilename, "w");
        if (fp == NULL) {
@@ -456,7 +457,11 @@ void network_deliver_digest(struct SpoolControl *sc) {
                buf[i] = tolower(buf[i]);
        }
        msg->cm_fields['F'] = strdup(buf);
+       msg->cm_fields['R'] = strdup(buf);
 
+       /*
+        * Go fetch the contents of the digest
+        */
        fseek(sc->digestfp, 0L, SEEK_END);
        msglen = ftell(sc->digestfp);
 
@@ -477,7 +482,7 @@ void network_deliver_digest(struct SpoolControl *sc) {
         * Figure out how big a buffer we need to allocate
         */
        for (nptr = sc->digestrecps; nptr != NULL; nptr = nptr->next) {
-               instr_len = instr_len + strlen(nptr->name);
+               instr_len = instr_len + strlen(nptr->name) + 2;
        }
        
        /*
@@ -541,7 +546,7 @@ void network_deliver_list(struct CtdlMessage *msg, struct SpoolControl *sc) {
         * Figure out how big a buffer we need to allocate
         */
        for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
-               instr_len = instr_len + strlen(nptr->name);
+               instr_len = instr_len + strlen(nptr->name) + 2;
        }
        
        /*
@@ -634,8 +639,8 @@ void network_spool_msg(long msgnum, void *userdata) {
                        if (msg->cm_fields['R'] != NULL) {
                                free(msg->cm_fields['R']);
                        }
-                       msg->cm_fields['R'] = malloc(SIZ);
-                       snprintf(msg->cm_fields['R'], SIZ,
+                       msg->cm_fields['R'] = malloc(256);
+                       snprintf(msg->cm_fields['R'], 256,
                                "room_%s@%s", CC->room.QRname,
                                config.c_fqdn);
                        for (i=0; i<strlen(msg->cm_fields['R']); ++i) {
@@ -679,7 +684,7 @@ void network_spool_msg(long msgnum, void *userdata) {
                        CC->redirect_alloc = SIZ;
 
                        safestrncpy(CC->preferred_formats, "text/plain", sizeof CC->preferred_formats);
-                       CtdlOutputPreLoadedMsg(msg, 0L, MT_CITADEL, HEADERS_NONE, 0, 0);
+                       CtdlOutputPreLoadedMsg(msg, MT_CITADEL, HEADERS_NONE, 0, 0);
 
                        striplt(CC->redirect_buffer);
                        fprintf(sc->digestfp, "\n%s\n", CC->redirect_buffer);
@@ -833,13 +838,8 @@ void network_spool_msg(long msgnum, void *userdata) {
                                        serialize_message(&sermsg, msg);
 
                                        /* write it to the spool file */
-                                       snprintf(filename, sizeof filename,
-#ifndef HAVE_SPOOL_DIR
-                                                        "."
-#else
-                                                        SPOOL_DIR
-#endif /* HAVE_SPOOL_DIR */
-                                                        "/network/spoolout/%s",
+                                       snprintf(filename, sizeof filename,"%s/%s",
+                                                        ctdl_netout_dir,
                                                         mptr->remote_nodename);
                                        lprintf(CTDL_DEBUG, "Appending to %s\n", filename);
                                        fp = fopen(filename, "ab");
@@ -896,7 +896,7 @@ void network_spoolout_room(char *room_to_spool) {
        }
 
        memset(&sc, 0, sizeof(struct SpoolControl));
-       assoc_file_name(filename, sizeof filename, &CC->room, "netconfigs");
+       assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
 
        begin_critical_section(S_NETCONFIGS);
 
@@ -1092,7 +1092,7 @@ int network_sync_to(char *target_node) {
        FILE *fp;
 
        /* Grab the configuration line we're looking for */
-       assoc_file_name(filename, sizeof filename, &CC->room, "netconfigs");
+       assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
        begin_critical_section(S_NETCONFIGS);
        fp = fopen(filename, "r");
        if (fp == NULL) {
@@ -1390,13 +1390,11 @@ void network_process_buffer(char *buffer, long size) {
                                if (strlen(nexthop) == 0) {
                                        strcpy(nexthop, msg->cm_fields['D']);
                                }
-                               snprintf(filename, sizeof filename,
-#ifndef HAVE_SPOOL_DIR
-                                                "."
-#else
-                                                SPOOL_DIR
-#endif /* HAVE_SPOOL_DIR */
-                                                "/network/spoolout/%s", nexthop);
+                               snprintf(filename, 
+                                                sizeof filename,
+                                                "%s/%s",
+                                                ctdl_netout_dir,
+                                                nexthop);
                                lprintf(CTDL_DEBUG, "Appending to %s\n", filename);
                                fp = fopen(filename, "ab");
                                if (fp != NULL) {
@@ -1567,19 +1565,12 @@ void network_do_spoolin(void) {
        struct stat statbuf;
        char filename[256];
        static time_t last_spoolin_mtime = 0L;
-       const char *spoolin_dirname = 
-#ifndef HAVE_SPOOL_DIR
-                                "."
-#else
-                                SPOOL_DIR
-#endif /* HAVE_SPOOL_DIR */
-                                "/network/spoolin";
 
        /*
         * Check the spoolin directory's modification time.  If it hasn't
         * been touched, we don't need to scan it.
         */
-       if (stat(spoolin_dirname, &statbuf)) return;
+       if (stat(ctdl_netin_dir, &statbuf)) return;
        if (statbuf.st_mtime == last_spoolin_mtime) {
                lprintf(CTDL_DEBUG, "network: nothing in inbound queue\n");
                return;
@@ -1590,18 +1581,16 @@ void network_do_spoolin(void) {
        /*
         * Ok, there's something interesting in there, so scan it.
         */
-       dp = opendir(spoolin_dirname);
+       dp = opendir(ctdl_netin_dir);
        if (dp == NULL) return;
 
        while (d = readdir(dp), d != NULL) {
                if ((strcmp(d->d_name, ".")) && (strcmp(d->d_name, ".."))) {
-                       snprintf(filename, sizeof filename,
-#ifndef HAVE_SPOOL_DIR
-                                        "."
-#else
-                                        SPOOL_DIR
-#endif /* HAVE_SPOOL_DIR */
-                                        "/network/spoolin/%s", d->d_name);
+                       snprintf(filename, 
+                                        sizeof filename,
+                                        "%s/%s",
+                                        ctdl_netin_dir,
+                                        d->d_name);
                        network_process_file(filename);
                }
        }
@@ -1620,25 +1609,17 @@ void network_purge_spoolout(void) {
        char nexthop[256];
        int i;
 
-       dp = opendir(
-#ifndef HAVE_SPOOL_DIR
-                                "."
-#else
-                                SPOOL_DIR
-#endif /* HAVE_SPOOL_DIR */
-                                "/network/spoolout");
+       dp = opendir(ctdl_netout_dir);
        if (dp == NULL) return;
 
        while (d = readdir(dp), d != NULL) {
                if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
                        continue;
-               snprintf(filename, sizeof filename,
-#ifndef HAVE_SPOOL_DIR
-                                "."
-#else
-                                SPOOL_DIR
-#endif /* HAVE_SPOOL_DIR */
-                                "/network/spoolout/%s", d->d_name);
+               snprintf(filename, 
+                                sizeof filename,
+                                "%s/%s",
+                                ctdl_netout_dir,
+                                d->d_name);
 
                strcpy(nexthop, "");
                i = is_valid_node(nexthop, NULL, d->d_name);
@@ -1665,7 +1646,7 @@ void receive_spool(int sock, char *remote_nodename) {
        long plen;
        FILE *fp;
 
-       strcpy(tempfilename, tmpnam(NULL));
+       CtdlMakeTempFileName(tempfilename, sizeof tempfilename);
        if (sock_puts(sock, "NDOP") < 0) return;
        if (sock_gets(sock, buf) < 0) return;
        lprintf(CTDL_DEBUG, "<%s\n", buf);
@@ -1723,14 +1704,13 @@ void receive_spool(int sock, char *remote_nodename) {
                                download_len, remote_nodename);
        lprintf(CTDL_DEBUG, "%s", buf);
        /* TODO: make move inline. forking is verry expensive. */
-       snprintf(buf, sizeof buf, "mv %s "
-#ifndef HAVE_SPOOL_DIR
-                        "."
-#else
-                        SPOOL_DIR
-#endif /* HAVE_SPOOL_DIR */
-                        "/network/spoolin/%s.%ld",
-                        tempfilename, remote_nodename, (long) getpid());
+       snprintf(buf, 
+                        sizeof buf, 
+                        "mv %s %s/%s.%ld",
+                        tempfilename, 
+                        ctdl_netin_dir,
+                        remote_nodename, 
+                        (long) getpid());
        system(buf);
 }
 
@@ -1756,12 +1736,9 @@ void transmit_spool(int sock, char *remote_nodename)
        }
 
        snprintf(sfname, sizeof sfname, 
-#ifndef HAVE_SPOOL_DIR
-                        "."
-#else
-                        SPOOL_DIR
-#endif /* HAVE_SPOOL_DIR */
-                        "/network/spoolout/%s", remote_nodename);
+                        "%s/%s",
+                        ctdl_netout_dir,
+                        remote_nodename);
        fd = open(sfname, O_RDONLY);
        if (fd < 0) {
                if (errno != ENOENT) {
@@ -1887,13 +1864,11 @@ void network_poll_other_citadel_nodes(int full_poll) {
                   && (strlen(host) > 0) && strlen(port) > 0) {
                        poll = full_poll;
                        if (poll == 0) {
-                               snprintf(spoolfile, sizeof spoolfile,
-#ifndef HAVE_SPOOL_DIR
-                                                "."
-#else
-                                                SPOOL_DIR
-#endif
-                                                "/network/spoolout/%s", node);
+                               snprintf(spoolfile, 
+                                                sizeof spoolfile,
+                                                "%s/%s",
+                                                ctdl_netout_dir, 
+                                                node);
                                if (access(spoolfile, R_OK) == 0) {
                                        poll = 1;
                                }
@@ -1913,21 +1888,12 @@ void network_poll_other_citadel_nodes(int full_poll) {
  * It's ok if these directories already exist.  Just fail silently.
  */
 void create_spool_dirs(void) {
-#ifndef HAVE_SPOOL_DIR
-       mkdir("./network", 0700);
-       chown("./network", CTDLUID, (-1));
-       mkdir("./network/spoolin", 0700);
-       chown("./network/spoolin", CTDLUID, (-1));
-       mkdir("./network/spoolout", 0700);
-       chown("./network/spoolout", CTDLUID, (-1));
-#else
-       mkdir(SPOOL_DIR "/network", 0700);
-       chown(SPOOL_DIR "./network", CTDLUID, (-1));
-       mkdir(SPOOL_DIR "/network/spoolin", 0700);
-       chown(SPOOL_DIR "./network/spoolin", CTDLUID, (-1));
-       mkdir(SPOOL_DIR "/network/spoolout", 0700);
-       chown(SPOOL_DIR "./network/spoolout", CTDLUID, (-1));
-#endif /* HAVE_SPOOL_DIR */
+       mkdir(ctdl_spool_dir, 0700);
+       chown(ctdl_spool_dir, CTDLUID, (-1));
+       mkdir(ctdl_netin_dir, 0700);
+       chown(ctdl_netin_dir, CTDLUID, (-1));
+       mkdir(ctdl_netout_dir, 0700);
+       chown(ctdl_netout_dir, CTDLUID, (-1));
 }