use an Enum for the cm_fields vector instead of nameless chars
[citadel.git] / citadel / modules / network / serv_netspool.c
index dd049a878445e24f91b3a39c6442d5dae006592b..3700ee42b385673925feaac2ac8eae079838ebaf 100644 (file)
@@ -224,6 +224,7 @@ void InspectQueuedRoom(SpoolControl **pSC,
 
 void CalcListID(SpoolControl *sc)
 {
+       StrBuf *RoomName;
        const char *err;
        int fd;
        struct CitContext *CCC = CC;
@@ -233,7 +234,7 @@ void CalcListID(SpoolControl *sc)
        assoc_file_name(filename, sizeof filename, &sc->room, ctdl_info_dir);
        fd = open(filename, 0);
 
-       if (fd != 0) {
+       if (fd > 0) {
                struct stat stbuf;
 
                if ((fstat(fd, &stbuf) == 0) &&
@@ -264,6 +265,9 @@ void CalcListID(SpoolControl *sc)
        }
 
        StrBufAppendBufPlain(sc->ListID, HKEY("<"), 0);
+       RoomName = NewStrBufPlain (sc->room.QRname, -1);
+       StrBufAsciify(RoomName, '_');
+       StrBufReplaceChars(RoomName, ' ', '_');
 
        if (StrLength(sc->Users[roommailalias]) > 0)
        {
@@ -282,7 +286,7 @@ void CalcListID(SpoolControl *sc)
        else
        {
                StrBufAppendBufPlain(sc->ListID, HKEY("room_"), 0);
-               StrBufAppendBufPlain(sc->ListID, sc->room.QRname, -1, 0);
+               StrBufAppendBuf(sc->ListID, RoomName, 0);
                StrBufAppendBufPlain(sc->ListID, HKEY("."), 0);
                StrBufAppendBufPlain(sc->ListID, config.c_fqdn, -1, 0);
                /*
@@ -299,15 +303,15 @@ void CalcListID(SpoolControl *sc)
        {
                sc->Users[roommailalias] = NewStrBuf();
                
-               StrBufPrintf(sc->Users[roommailalias],
-                            "room_%s@%s",
-                            CCC->room.QRname,
-                            config.c_fqdn);
+               StrBufAppendBufPlain(sc->Users[roommailalias], HKEY("room_"), 0);
+               StrBufAppendBuf(sc->Users[roommailalias], RoomName, 0);
+               StrBufAppendBufPlain(sc->Users[roommailalias], HKEY("@"), 0);
+               StrBufAppendBufPlain(sc->Users[roommailalias], config.c_fqdn, -1, 0);
 
-               StrBufAsciify(sc->Users[roommailalias], '_');
                StrBufLowerCase(sc->Users[roommailalias]);
        }
 
+       FreeStrBuf(&RoomName);
 }
 
 
@@ -395,6 +399,7 @@ void network_spoolout_room(SpoolControl *sc)
  */
 void network_process_buffer(char *buffer, long size, HashList *working_ignetcfg, HashList *the_netmap, int *netmap_changed)
 {
+       long len;
        struct CitContext *CCC = CC;
        StrBuf *Buf = NULL;
        struct CtdlMessage *msg = NULL;
@@ -433,16 +438,18 @@ void network_process_buffer(char *buffer, long size, HashList *working_ignetcfg,
 
        for (pos = 3; pos < size; ++pos) {
                field = buffer[pos];
-               msg->cm_fields[field] = strdup(&buffer[pos+1]);
-               pos = pos + strlen(&buffer[(int)pos]);
+               len = strlen(buffer + pos + 1);
+               msg->cm_fields[field] = malloc(len + 1);
+               memcpy (msg->cm_fields[field], buffer+ pos + 1, len + 1);
+               pos = pos + len + 1;
        }
 
        /* Check for message routing */
-       if (msg->cm_fields['D'] != NULL) {
-               if (strcasecmp(msg->cm_fields['D'], config.c_nodename)) {
+       if (msg->cm_fields[eDestination] != NULL) {
+               if (strcasecmp(msg->cm_fields[eDestination], config.c_nodename)) {
 
                        /* route the message */
-                       Buf = NewStrBufPlain(msg->cm_fields['D'], -1);
+                       Buf = NewStrBufPlain(msg->cm_fields[eDestination], -1);
                        if (CtdlIsValidNode(&nexthop, 
                                            NULL, 
                                            Buf, 
@@ -450,16 +457,16 @@ void network_process_buffer(char *buffer, long size, HashList *working_ignetcfg,
                                            the_netmap) == 0) 
                        {
                                /* prepend our node to the path */
-                               if (msg->cm_fields['P'] != NULL) {
-                                       oldpath = msg->cm_fields['P'];
-                                       msg->cm_fields['P'] = NULL;
+                               if (msg->cm_fields[eMessagePath] != NULL) {
+                                       oldpath = msg->cm_fields[eMessagePath];
+                                       msg->cm_fields[eMessagePath] = NULL;
                                }
                                else {
                                        oldpath = strdup("unknown_user");
                                }
                                size = strlen(oldpath) + SIZ;
-                               msg->cm_fields['P'] = malloc(size);
-                               snprintf(msg->cm_fields['P'], size, "%s!%s",
+                               msg->cm_fields[eMessagePath] = malloc(size);
+                               snprintf(msg->cm_fields[eMessagePath], size, "%s!%s",
                                        config.c_nodename, oldpath);
                                free(oldpath);
 
@@ -518,9 +525,9 @@ void network_process_buffer(char *buffer, long size, HashList *working_ignetcfg,
        }
 
        /* Learn network topology from the path */
-       if ((msg->cm_fields['N'] != NULL) && (msg->cm_fields['P'] != NULL)) {
-               NetworkLearnTopology(msg->cm_fields['N'], 
-                                    msg->cm_fields['P'], 
+       if ((msg->cm_fields[eNodeName] != NULL) && (msg->cm_fields[eMessagePath] != NULL)) {
+               NetworkLearnTopology(msg->cm_fields[eNodeName], 
+                                    msg->cm_fields[eMessagePath], 
                                     the_netmap, 
                                     netmap_changed);
        }
@@ -528,13 +535,13 @@ void network_process_buffer(char *buffer, long size, HashList *working_ignetcfg,
        /* Is the sending node giving us a very persuasive suggestion about
         * which room this message should be saved in?  If so, go with that.
         */
-       if (msg->cm_fields['C'] != NULL) {
-               safestrncpy(target_room, msg->cm_fields['C'], sizeof target_room);
+       if (msg->cm_fields[eRemoteRoom] != NULL) {
+               safestrncpy(target_room, msg->cm_fields[eRemoteRoom], sizeof target_room);
        }
 
        /* Otherwise, does it have a recipient?  If so, validate it... */
-       else if (msg->cm_fields['R'] != NULL) {
-               recp = validate_recipients(msg->cm_fields['R'], NULL, 0);
+       else if (msg->cm_fields[eRecipient] != NULL) {
+               recp = validate_recipients(msg->cm_fields[eRecipient], NULL, 0);
                if (recp != NULL) if (recp->num_error != 0) {
                        network_bounce(msg,
                                "A message you sent could not be delivered due to an invalid address.\n"
@@ -548,20 +555,20 @@ void network_process_buffer(char *buffer, long size, HashList *working_ignetcfg,
        }
 
        /* Our last shot at finding a home for this message is to see if
-        * it has the O field (Originating room) set.
+        * it has the eOriginalRoom (O) field (Originating room) set.
         */
-       else if (msg->cm_fields['O'] != NULL) {
-               safestrncpy(target_room, msg->cm_fields['O'], sizeof target_room);
+       else if (msg->cm_fields[eOriginalRoom] != NULL) {
+               safestrncpy(target_room, msg->cm_fields[eOriginalRoom], sizeof target_room);
        }
 
        /* Strip out fields that are only relevant during transit */
-       if (msg->cm_fields['D'] != NULL) {
-               free(msg->cm_fields['D']);
-               msg->cm_fields['D'] = NULL;
+       if (msg->cm_fields[eDestination] != NULL) {
+               free(msg->cm_fields[eDestination]);
+               msg->cm_fields[eDestination] = NULL;
        }
-       if (msg->cm_fields['C'] != NULL) {
-               free(msg->cm_fields['C']);
-               msg->cm_fields['C'] = NULL;
+       if (msg->cm_fields[eRemoteRoom] != NULL) {
+               free(msg->cm_fields[eRemoteRoom]);
+               msg->cm_fields[eRemoteRoom] = NULL;
        }
 
        /* save the message into a room */
@@ -923,6 +930,11 @@ void network_consolidate_spoolout(HashList *working_ignetcfg, HashList *the_netm
                        do {} while ((FileMoveChunked(&FDIO, &err) > 0) && (err == NULL));
                        if (err == NULL) {
                                unlink(filename);
+                               QN_syslog(LOG_DEBUG,
+                                         "Spoolfile %s now "SIZE_T_FMT" k\n",
+                                         spooloutfilename,
+                                         (dsize + fsize)/1024
+                                       );                              
                        }
                        else {
                                nFailed++;