+
+
+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;
+
+
+
+ // FIXME munge the headers so it looks like it came from a mailing list
+
+
+
+ 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);
+}
+
+
+/*
+ * Sweep through one room looking for mailing list deliveries to do
+ */
+void listdeliver_sweep_room(char *roomname) {
+ char *netconfig = NULL;
+ char *newnetconfig = 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;
+
+ if (CtdlGetRoom(&CC->room, roomname)) {
+ syslog(LOG_DEBUG, "listdeliver: no room <%s>", roomname);
+ return;
+ }
+
+ netconfig = LoadRoomNetConfigFile(CC->room.QRnumber);
+ if (!netconfig) {
+ return; // no netconfig, no processing, no problem
+ }
+
+ syslog(LOG_DEBUG, "listdeliver: sweeping %s", roomname);
+
+ config_lines = num_tokens(netconfig, '\n');
+ for (i=0; i<config_lines; ++i) {
+ extract_token(buf, netconfig, i, '\n', sizeof buf);
+
+ if (!strncasecmp(buf, "lastsent|", 9)) {
+ 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", CC->room.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);
+
+ // Update this room's netconfig with the updated lastsent
+ netconfig = LoadRoomNetConfigFile(CC->room.QRnumber);
+ if (!netconfig) {
+ netconfig = strdup("");
+ }
+
+ // The new netconfig begins with the new lastsent directive
+ newnetconfig = malloc(strlen(netconfig) + 1024);
+ sprintf(newnetconfig, "lastsent|%ld\n", ld.msgnum);
+
+ // And then we append all of the old netconfig, minus the old lastsent. Also omit blank lines.
+ config_lines = num_tokens(netconfig, '\n');
+ for (i=0; i<config_lines; ++i) {
+ extract_token(buf, netconfig, i, '\n', sizeof buf);
+ if ( (!IsEmptyStr(buf)) && (strncasecmp(buf, "lastsent|", 9)) ) {
+ sprintf(&newnetconfig[strlen(newnetconfig)], "%s\n", buf);
+ }
+ }
+
+ // Write the new netconfig back to disk
+ SaveRoomNetConfigFile(CC->room.QRnumber, newnetconfig);
+ free(newnetconfig);
+ }
+ }
+ free(netconfig);