Added an elastic string buffer class to libcitadel. Why do I have a feeling I'm...
[citadel.git] / citadel / modules / listdeliver / serv_listdeliver.c
index c36a0fb5340e7074aa7049242b7a270fbdd38fce..7dd34630a0750c5fc5977fcf4f663163a50d18b9 100644 (file)
 int doing_listdeliver = 0;
 
 
+// data passed back and forth between listdeliver_do_msg() and listdeliver_sweep_room()
+struct lddata {
+       long msgnum;            // number of most recent message processed
+       char *netconf;          // netconfig for this room (contains the recipients)
+};
+
+
+
+void listdeliver_do_msg(long msgnum, void *userdata) {
+       struct lddata *ld = (struct lddata *) userdata;
+       if (!ld) return;
+       char buf[SIZ];
+
+       ld->msgnum = msgnum;
+       if (msgnum <= 0) return;
+
+       struct CtdlMessage *TheMessage = CtdlFetchMessage(msgnum, 1);
+       if (!TheMessage) return;
+
+       char *recipients = malloc(strlen(ld->netconf));
+       if (recipients) {
+               recipients[0] = 0;
+
+               int config_lines = num_tokens(ld->netconf, '\n');
+               for (int i=0; i<config_lines; ++i) {
+                       extract_token(buf, ld->netconf, i, '\n', sizeof buf);
+                       if (!strncasecmp(buf, "listrecp|", 9)) {
+                               if (recipients[0] != 0) {
+                                       strcat(recipients, ",");
+                               }
+                               strcat(recipients, &buf[9]);
+                       }
+                       if (!strncasecmp(buf, "digestrecp|", 11)) {
+                               if (recipients[0] != 0) {
+                                       strcat(recipients, ",");
+                               }
+                               strcat(recipients, &buf[11]);
+                       }
+               }
+               syslog(LOG_DEBUG, "\033[33m%s\033[0m", recipients);
+               struct recptypes *valid = validate_recipients(recipients, NULL, 0);
+               if (valid) {
+                       long new_msgnum = CtdlSubmitMsg(TheMessage, valid, "");
+                       free_recipients(valid);
+               }
+       }
+       CM_Free(TheMessage);
+}
+
 
 void listdeliver_sweep_room(struct ctdlroom *qrbuf, void *data) {
-       char *serialized_config = NULL;
+       char *netconfig = NULL;
        long lastsent = 0;
        char buf[SIZ];
        int config_lines;
        int i;
+       int number_of_messages_processed = 0;
+       int number_of_recipients = 0;
+       struct lddata ld;
 
-        serialized_config = LoadRoomNetConfigFile(qrbuf->QRnumber);
-        if (!serialized_config) {
-               syslog(LOG_DEBUG, "\033[31m %s has no netconfig \033[0m", qrbuf->QRname);
+       if (CtdlGetRoom(&CC->room, qrbuf->QRname)) {
+               syslog(LOG_DEBUG, "listdeliver: no room <%s>", qrbuf->QRname);
                return;
        }
 
-       syslog(LOG_DEBUG, "\033[32m %s has a netconfig \033[0m", qrbuf->QRname);
+        netconfig = LoadRoomNetConfigFile(qrbuf->QRnumber);
+        if (!netconfig) {
+               return;                         // no netconfig, no processing, no problem
+       }
 
-       config_lines = num_tokens(serialized_config, '\n');
+       config_lines = num_tokens(netconfig, '\n');
        for (i=0; i<config_lines; ++i) {
-               extract_token(buf, serialized_config, i, '\n', sizeof buf);
+               extract_token(buf, netconfig, i, '\n', sizeof buf);
 
                if (!strncasecmp(buf, "lastsent|", 9)) {
-                       lastsent = atol(buf[9]);
-                       syslog(LOG_DEBUG, "listdeliver: last message delivered = %ld", lastsent);
+                       lastsent = atol(&buf[9]);
+               }
+               else if ( (!strncasecmp(buf, "listrecp|", 9)) || (!strncasecmp(buf, "digestrecp|", 11)) ) {
+                       ++number_of_recipients;
                }
+       }
 
+       if (number_of_recipients > 0) {
+               syslog(LOG_DEBUG, "listdeliver: processing new messages in <%s> for <%d> recipients", qrbuf->QRname, number_of_recipients);
+               ld.netconf = netconfig;
+               number_of_messages_processed = CtdlForEachMessage(MSGS_GT, lastsent, NULL, NULL, NULL, listdeliver_do_msg, &ld);
+               syslog(LOG_DEBUG, "listdeliver: processed %d messages", number_of_messages_processed);
+       
+               if (number_of_messages_processed > 0) {
+                       syslog(LOG_DEBUG, "listdeliver: new lastsent is %ld", ld.msgnum);
 
+                       // FIXME write lastsent back to netconfig
+                       syslog(LOG_DEBUG, "\033[31mBEFORE:<%s>\033[0m", netconfig);
+                       syslog(LOG_DEBUG, "\033[32mAFTER:<%s>\033[0m", netconfig);
 
 
-               syslog(LOG_DEBUG, "%s", buf);
-       }
 
 
-       free(serialized_config);
+
+               }
+       }
+
+       free(netconfig);
 }
 
 
@@ -107,6 +177,8 @@ void listdeliver_sweep(void) {
        syslog(LOG_DEBUG, "listdeliver: ended");
        last_run = time(NULL);
        doing_listdeliver = 0;
+
+       exit(0);
 }