NETSPOOL: combine collecting of email recipients into one function; participates...
[citadel.git] / citadel / modules / network / serv_netmail.c
index b98e30bb1416d54d3a1f6e5234f71256e78b7ae1..d9cd510930065c9d315bd80822239605d3fece97 100644 (file)
 #include "netspool.h"
 #include "netmail.h"
 
+void network_deliver_list(struct CtdlMessage *msg, SpoolControl *sc, const char *RoomName);
+
+void aggregate_recipients(StrBuf **recps, RoomNetCfg Which, OneRoomNetCfg *OneRNCfg)
+{
+       size_t recps_len = 0;
+       RoomNetCfgLine *nptr;
+       struct CitContext *CCC = CC;
+
+       *recps = NULL;
+       /*
+        * Figure out how big a buffer we need to allocate
+        */
+       for (nptr = OneRNCfg->NetConfigs[Which]; nptr != NULL; nptr = nptr->next) {
+               recps_len = recps_len + StrLength(nptr->Value[0]) + 2;
+       }
+
+       /* Nothing todo... */
+       if (recps_len == 0)
+               return;
+
+       *recps = NewStrBufPlain(NULL, recps_len);
+
+       if (*recps == NULL) {
+               QN_syslog(LOG_EMERG,
+                         "Cannot allocate %ld bytes for recps...\n",
+                         (long)recps_len);
+               abort();
+       }
+
+       /* Each recipient */
+       for (nptr = OneRNCfg->NetConfigs[Which]; nptr != NULL; nptr = nptr->next) {
+               if (nptr != OneRNCfg->NetConfigs[Which]) {
+                       StrBufAppendBufPlain(*recps, HKEY(","), 0);
+               }
+               StrBufAppendBuf(*recps, nptr->Value[0], 0);
+       }
+}
 
 /*
  * Deliver digest messages
  */
 void network_deliver_digest(SpoolControl *sc)
 {
-       struct CitContext *CCC = CC;
        char buf[SIZ];
        int i;
        struct CtdlMessage *msg = NULL;
        long msglen;
        StrBuf *recps = NULL;
        char *precps;
-       size_t recps_len = SIZ;
        struct recptypes *valid;
-       RoomNetCfgLine *nptr;
        char bounce_to[256];
 
        if (sc->num_msgs_spooled < 1) {
@@ -147,30 +181,7 @@ void network_deliver_digest(SpoolControl *sc)
        sc->digestfp = NULL;
 
        /* Now generate the delivery instructions */
-
-       /*
-        * Figure out how big a buffer we need to allocate
-        */
-       for (nptr = sc->RNCfg->NetConfigs[digestrecp]; nptr != NULL; nptr = nptr->next) {
-               recps_len = recps_len + StrLength(nptr->Value[0]) + 2;
-       }
-
-       recps = NewStrBufPlain(NULL, recps_len);
-
-       if (recps == NULL) {
-               QN_syslog(LOG_EMERG,
-                         "Cannot allocate %ld bytes for recps...\n",
-                         (long)recps_len);
-               abort();
-       }
-
-       /* Each recipient */
-       for (nptr = sc->RNCfg->NetConfigs[digestrecp]; nptr != NULL; nptr = nptr->next) {
-               if (nptr != sc->RNCfg->NetConfigs[digestrecp]) {
-                       StrBufAppendBufPlain(recps, HKEY(","), 0);
-               }
-               StrBufAppendBuf(recps, nptr->Value[0], 0);
-       }
+       aggregate_recipients(&recps, digestrecp, sc->RNCfg);
 
        /* Where do we want bounces and other noise to be heard?
         *Surely not the list members! */
@@ -371,7 +382,7 @@ void network_process_list(SpoolControl *sc, struct CtdlMessage *omsg, long *dele
        }
 
        /* Handle delivery */
-//                     network_deliver_list(msg, sc, CC->room.QRname);
+       network_deliver_list(msg, sc, CC->room.QRname);
        CtdlFreeMessage(msg);
 }
 
@@ -380,12 +391,9 @@ void network_process_list(SpoolControl *sc, struct CtdlMessage *omsg, long *dele
  */
 void network_deliver_list(struct CtdlMessage *msg, SpoolControl *sc, const char *RoomName)
 {
-       struct CitContext *CCC = CC;
        StrBuf *recps = NULL;
        char *precps = NULL;
-       size_t recps_len = SIZ;
        struct recptypes *valid;
-       RoomNetCfgLine *nptr;
        char bounce_to[256];
 
        /* Don't do this if there were no recipients! */
@@ -396,26 +404,7 @@ void network_deliver_list(struct CtdlMessage *msg, SpoolControl *sc, const char
        /*
         * Figure out how big a buffer we need to allocate
         */
-       for (nptr = sc->RNCfg->NetConfigs[listrecp]; nptr != NULL; nptr = nptr->next) {
-               recps_len = recps_len + StrLength(nptr->Value[0]) + 2;
-       }
-
-       recps = NewStrBufPlain(NULL, recps_len);
-
-       if (recps == NULL) {
-               QN_syslog(LOG_EMERG,
-                         "Cannot allocate %ld bytes for recps...\n",
-                         (long)recps_len);
-               abort();
-       }
-
-       /* Each recipient */
-       for (nptr = sc->RNCfg->NetConfigs[listrecp]; nptr != NULL; nptr = nptr->next) {
-               if (nptr != sc->RNCfg->NetConfigs[listrecp]) {
-                       StrBufAppendBufPlain(recps, HKEY(","), 0);
-               }
-               StrBufAppendBuf(recps, nptr->Value[0], 0);
-       }
+       aggregate_recipients(&recps, listrecp, sc->RNCfg);
 
        /* Where do we want bounces and other noise to be heard?
         *  Surely not the list members! */
@@ -442,7 +431,6 @@ void network_process_participate(SpoolControl *sc, struct CtdlMessage *omsg, lon
        int i;
        int ok_to_participate = 0;
        StrBuf *Buf = NULL;
-       RoomNetCfgLine *nptr;
        struct recptypes *valid;
 
        /*
@@ -475,7 +463,11 @@ void network_process_participate(SpoolControl *sc, struct CtdlMessage *omsg, lon
                        ok_to_participate = 1;
                }
        }
-       if (ok_to_participate) {
+       if (ok_to_participate)
+       {
+               StrBuf *recps = NULL;
+               char *precps;
+
                if (msg->cm_fields['F'] != NULL) {
                        free(msg->cm_fields['F']);
                }
@@ -494,26 +486,17 @@ void network_process_participate(SpoolControl *sc, struct CtdlMessage *omsg, lon
                                msg->cm_fields['F'][i] = '_';
                        }
                }
-               
-               /*
-                * Figure out how big a buffer we need to alloc
-                */
-               for (nptr = sc->RNCfg->NetConfigs[participate];
-                    nptr != NULL;
-                    nptr = nptr->next)
-               {
-                       if (msg->cm_fields['R'] != NULL) {
-                               free(msg->cm_fields['R']);
-                       }
-                       msg->cm_fields['R'] =
-                               strdup(ChrPtr(nptr->Value[0]));
-                       
-                       valid = validate_recipients(msg->cm_fields['R'],
-                                                   NULL, 0);
-                       
-                       CtdlSubmitMsg(msg, valid, "", 0);
-                       free_recipients(valid);
-               }
+
+               aggregate_recipients(&recps, participate, sc->RNCfg);
+               precps = SmashStrBuf(&recps);
+               valid = validate_recipients(precps, NULL, 0);
+
+               if (msg->cm_fields['R'] != NULL) {
+                       free(msg->cm_fields['R']);
+               }/* TODO: check whether 'R' is set appropriate later. */
+
+               CtdlSubmitMsg(msg, valid, "", 0);
+               free_recipients(valid);
        }
        FreeStrBuf(&Buf);
        CtdlFreeMessage(msg);